2 * This code is derived from software copyrighted by the Free Software
5 * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
7 * Modified 1993 by Paul Kranenburg, Erasmus University
11 static char sccsid[] = "@(#)ld.c 6.10 (Berkeley) 5/22/91";
14 /* Linker `ld' for GNU
15 Copyright (C) 1988 Free Software Foundation, Inc.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 1, or (at your option)
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
31 /* Written by Richard Stallman with some help from Eric Albert.
32 Set, indirect, and warning symbol features added by Randy Smith. */
35 * $FreeBSD: src/gnu/usr.bin/ld/ld.c,v 1.47 1999/08/27 23:36:00 peter Exp $
38 /* Define how to initialize system-dependent header fields. */
40 #include <sys/param.h>
41 #include <sys/types.h>
45 #include <sys/resource.h>
61 /* Vector of entries for input files specified by arguments.
62 These are all the input files except for members of specified libraries. */
63 struct file_entry *file_table;
66 /* 1 => write relocation into output file so can re-input it later. */
67 int relocatable_output;
69 /* 1 => building a shared object, set by `-Bshareable'. */
70 int building_shared_object;
72 /* 1 => create the output executable. */
75 /* Force the executable to be output, even if there are non-fatal errors */
78 /* 1 => assign space to common symbols even if `relocatable_output'. */
79 int force_common_definition;
81 /* 1 => assign jmp slots to text symbols in shared objects even if non-PIC */
82 int force_alias_definition;
84 /* 1 => some files contain PIC code, affects relocation bits
85 if `relocatable_output'. */
88 /* 1 => segments must be page aligned (ZMAGIC, QMAGIC) */
89 int page_align_segments;
91 /* 1 => data segment must be page aligned, even if `-n' or `-N' */
94 /* 1 => do not use standard library search path */
97 /* Version number to put in __DYNAMIC (set by -V) */
100 int text_size; /* total size of text. */
101 int text_start; /* start of text */
102 int text_pad; /* clear space between text and data */
103 int data_size; /* total size of data. */
104 int data_start; /* start of data */
105 int data_pad; /* part of bss segment as part of data */
107 int bss_size; /* total size of bss. */
108 int bss_start; /* start of bss */
110 int text_reloc_size; /* total size of text relocation. */
111 int data_reloc_size; /* total size of data relocation. */
113 int rrs_section_type; /* What's in the RRS section */
114 int rrs_text_size; /* Size of RRS text additions */
115 int rrs_text_start; /* Location of above */
116 int rrs_data_size; /* Size of RRS data additions */
117 int rrs_data_start; /* Location of above */
119 /* Specifications of start and length of the area reserved at the end
120 of the data segment for the set vectors. Computed in 'digest_symbols' */
121 int set_sect_start; /* start of set element vectors */
122 int set_sect_size; /* size of above */
124 int link_mode; /* Current link mode */
125 int pic_type; /* PIC type */
128 * When loading the text and data, we can avoid doing a close
129 * and another open between members of the same library.
131 * These two variables remember the file that is currently open.
132 * Both are zero if no file is open.
134 * See `each_file' and `file_close'.
136 struct file_entry *input_file;
139 /* The name of the file to write; "a.out" by default. */
140 char *output_filename; /* Output file name. */
141 char *real_output_filename; /* Output file name. */
142 FILE *outstream; /* Output file descriptor. */
143 struct exec outheader; /* Output file header. */
144 int magic; /* Output file magic. */
146 int relocatable_output; /* `-r'-ed output */
148 symbol *entry_symbol; /* specified by `-e' */
149 int entry_offset; /* program entry if no `-e' given */
151 int page_size; /* Size of a page (machine dependent) */
154 * Keep a list of any symbols referenced from the command line (so
155 * that error messages for these guys can be generated). This list is
158 symbol **cmdline_references;
159 int cl_refs_allocated;
162 * Which symbols should be stripped (omitted from the output): none, all, or
166 STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER
170 * Which local symbols should be omitted: none, all, or those starting with L.
171 * This is irrelevant if STRIP_NONE.
174 DISCARD_NONE, DISCARD_ALL, DISCARD_L
177 int global_sym_count; /* # of nlist entries for global symbols */
178 int size_sym_count; /* # of N_SIZE nlist entries for output
179 (relocatable_output only) */
180 int local_sym_count; /* # of nlist entries for local symbols. */
181 int non_L_local_sym_count; /* # of nlist entries for non-L symbols */
182 int debugger_sym_count; /* # of nlist entries for debugger info. */
183 int undefined_global_sym_count; /* # of global symbols referenced and
185 int undefined_shobj_sym_count; /* # of undefined symbols referenced
187 int multiple_def_count; /* # of multiply defined symbols. */
188 int defined_global_sym_count; /* # of defined global symbols. */
189 int common_defined_global_count; /* # of common symbols. */
190 int undefined_weak_sym_count; /* # of weak symbols referenced and
194 int special_sym_count; /* # of linker defined symbols. */
195 /* XXX - Currently, only __DYNAMIC and _G_O_T_ go here if required,
196 * perhaps _etext, _edata and _end should go here too.
199 int global_alias_count; /* # of aliased symbols */
200 int set_symbol_count; /* # of N_SET* symbols. */
201 int set_vector_count; /* # of set vectors in output. */
202 int warn_sym_count; /* # of warning symbols encountered. */
203 int flag_list_files; /* 1 => print pathnames of files, don't link */
204 int list_warning_symbols; /* 1 => warning symbols referenced */
206 struct string_list_element *set_element_prefixes;
208 int trace_files; /* print names of input files as processed (`-t'). */
209 int write_map; /* write a load map (`-M') */
212 * `text-start' address is normally this much plus a page boundary.
213 * This is not a user option; it is fixed for each system.
215 int text_start_alignment;
218 * Nonzero if -T was specified in the command line.
219 * This prevents text_start from being set later to default values.
221 int T_flag_specified;
224 * Nonzero if -Tdata was specified in the command line.
225 * This prevents data_start from being set later to default values.
227 int Tdata_flag_specified;
230 * Size to pad data section up to.
231 * We simply increase the size of the data section, padding with zeros,
232 * and reduce the size of the bss section to match.
234 int specified_data_size;
239 static void decode_option __P((char *, char *));
240 static void decode_command __P((int, char **));
241 static int classify_arg __P((char *));
242 static void load_symbols __P((void));
243 static void enter_global_ref __P((struct localsymbol *,
244 char *, struct file_entry *));
245 static void digest_symbols __P((void));
246 static void digest_pass1 __P((void)), digest_pass2 __P((void));
247 static void consider_file_section_lengths __P((struct file_entry *));
248 static void relocate_file_addresses __P((struct file_entry *));
249 static void consider_relocation __P((struct file_entry *, int));
250 static void consider_local_symbols __P((struct file_entry *));
251 static void perform_relocation __P((char *, int,
252 struct relocation_info *, int,
253 struct file_entry *, int));
254 static void copy_text __P((struct file_entry *));
255 static void copy_data __P((struct file_entry *));
256 static void coptxtrel __P((struct file_entry *));
257 static void copdatrel __P((struct file_entry *));
258 static void write_output __P((void));
259 static void write_header __P((void));
260 static void write_text __P((void));
261 static void write_data __P((void));
262 static void write_rel __P((void));
263 static void write_syms __P((void));
264 static void assign_symbolnums __P((struct file_entry *, int *));
265 static void cleanup __P((void));
266 static int parse __P((char *, char *, char *));
267 static void list_files __P((void));
276 /* Added this to stop ld core-dumping on very large .o files. */
278 /* Get rid of any avoidable limit on stack size. */
282 /* Set the stack limit huge so that alloca does not fail. */
283 if (getrlimit(RLIMIT_STACK, &rlim) != 0)
286 rlim.rlim_cur = rlim.rlim_max;
287 if (setrlimit(RLIMIT_STACK, &rlim) != 0)
291 #endif /* RLIMIT_STACK */
295 /* Clear the cumulative info on the output file. */
305 page_align_segments = 0;
308 /* Initialize the data about options. */
310 specified_data_size = 0;
311 strip_symbols = STRIP_NONE;
313 discard_locals = DISCARD_NONE;
316 relocatable_output = 0;
317 force_common_definition = 0;
318 T_flag_specified = 0;
319 Tdata_flag_specified = 0;
320 magic = DEFAULT_MAGIC;
322 force_executable = 0;
325 link_mode |= SILLYARCHIVE;
327 soversion = DEFAULT_SOVERSION;
329 /* Initialize the cumulative counts of symbols. */
332 non_L_local_sym_count = 0;
333 debugger_sym_count = 0;
334 undefined_global_sym_count = 0;
336 list_warning_symbols = 0;
337 multiple_def_count = 0;
338 common_defined_global_count = 0;
340 /* Keep a list of symbols referenced from the command line */
341 cl_refs_allocated = 10;
342 cmdline_references = (symbol **)
343 xmalloc(cl_refs_allocated * sizeof(symbol *));
344 *cmdline_references = 0;
346 /* Completely decode ARGV. */
347 decode_command(argc, argv);
352 building_shared_object =
353 (!relocatable_output && (link_mode & SHAREABLE));
355 if (building_shared_object && entry_symbol) {
356 errx(1,"`-Bshareable' and `-e' options are mutually exclusive");
359 /* Create the symbols `etext', `edata' and `end'. */
360 symtab_init(relocatable_output);
363 * Determine whether to count the header as part of the text size,
364 * and initialize the text size accordingly. This depends on the kind
365 * of system and on the output format selected.
368 if (magic == ZMAGIC || magic == QMAGIC)
369 page_align_segments = 1;
371 md_init_header(&outheader, magic, 0);
373 text_size = sizeof(struct exec);
374 text_size -= N_TXTOFF(outheader);
378 entry_offset = text_size;
380 if (!T_flag_specified && !relocatable_output)
381 text_start = TEXT_START(outheader);
383 /* The text-start address is normally this far past a page boundary. */
384 text_start_alignment = text_start % page_size;
387 * Load symbols of all input files. Also search all libraries and
388 * decide which library members to load.
392 /* Compute where each file's sections go, and relocate symbols. */
396 * Print error messages for any missing symbols, for any warning
397 * symbols, and possibly multiple definitions
399 make_executable &= do_warnings(stderr);
401 /* Print a map, if requested. */
403 print_symbols(stdout);
405 /* Write the output file. */
406 if (make_executable || force_executable)
409 exit(!make_executable);
413 * Analyze a command line argument. Return 0 if the argument is a filename.
414 * Return 1 if the argument is a option complete in itself. Return 2 if the
415 * argument is a option which uses an argument.
417 * Thus, the value is the number of consecutive arguments that are part of
429 if (!strcmp(&arg[2], "ssert"))
447 if (!strcmp(&arg[2], "static"))
449 if (!strcmp(&arg[2], "dynamic"))
451 if (!strcmp(&arg[2], "forcedynamic"))
457 if (!strcmp(&arg[2], "text"))
459 if (!strcmp(&arg[2], "data"))
468 * Process the command arguments, setting up file_table with an entry for
469 * each input file, and setting variables according to the options.
473 decode_command(argc, argv)
478 register struct file_entry *p;
481 output_filename = "a.out";
484 * First compute number_of_files so we know how long to make
486 * Also process most options completely.
489 for (i = 1; i < argc; i++) {
490 register int code = classify_arg(argv[i]);
493 errx(1, "no argument following %s", argv[i]);
495 decode_option(argv[i], argv[i + 1]);
497 if (argv[i][1] == 'l' || argv[i][1] == 'A')
505 if (!number_of_files) {
508 errx(1, "No input files specified");
511 p = file_table = (struct file_entry *)
512 xmalloc(number_of_files * sizeof(struct file_entry));
513 bzero(p, number_of_files * sizeof(struct file_entry));
515 /* Now scan again and fill in file_table. */
516 /* All options except -A and -l are ignored here. */
518 for (i = 1; i < argc; i++) {
520 register int code = classify_arg(argv[i]);
523 p->filename = argv[i];
524 p->local_sym_name = argv[i];
529 string = argv[i + 1];
531 string = &argv[i][2];
533 if (argv[i][1] == 'B') {
534 if (strcmp(string, "static") == 0)
535 link_mode &= ~DYNAMIC;
536 else if (strcmp(string, "dynamic") == 0)
537 link_mode |= DYNAMIC;
538 else if (strcmp(string, "forcedynamic") == 0)
539 link_mode |= DYNAMIC|FORCEDYNAMIC;
540 else if (strcmp(string, "symbolic") == 0)
541 link_mode |= SYMBOLIC;
542 else if (strcmp(string, "forcearchive") == 0)
543 link_mode |= FORCEARCHIVE;
544 else if (strcmp(string, "shareable") == 0)
545 link_mode |= SHAREABLE;
547 else if (strcmp(string, "silly") == 0)
548 link_mode |= SILLYARCHIVE;
549 else if (strcmp(string, "~silly") == 0)
550 link_mode &= ~SILLYARCHIVE;
553 if (!strcmp(argv[i] + 1, "assert")) {
554 if (!strcmp(string, "pure-text"))
555 link_mode |= WARNRRSTEXT;
557 if (argv[i][1] == 'A') {
559 errx(1, "-A specified before an input file other than the first");
560 p->filename = string;
561 p->local_sym_name = string;
562 p->flags |= E_JUST_SYMS;
563 link_mode &= ~DYNAMIC;
566 if (argv[i][1] == 'l') {
567 p->filename = string;
568 p->local_sym_name = concat("-l", string, "");
569 p->flags |= E_SEARCH_DIRS;
570 if (link_mode & DYNAMIC && !relocatable_output)
571 p->flags |= E_SEARCH_DYNAMIC;
577 /* Now check some option settings for consistency. */
579 if (page_align_segments &&
580 (text_start - text_start_alignment) & (page_size - 1))
581 errx(1, "incorrect alignment of text start address");
583 /* Append the standard search directories to the user-specified ones. */
584 add_search_path(getenv("LD_LIBRARY_PATH"));
595 for (ptr = cmdline_references;
596 ptr < cmdline_references + cl_refs_allocated && *ptr;
599 if (ptr >= cmdline_references + cl_refs_allocated - 1) {
600 int diff = ptr - cmdline_references;
602 cl_refs_allocated *= 2;
603 cmdline_references = (symbol **)
604 xrealloc(cmdline_references,
605 cl_refs_allocated * sizeof(symbol *));
606 ptr = cmdline_references + diff;
613 set_element_prefixed_p(name)
616 struct string_list_element *p;
619 for (p = set_element_prefixes; p; p = p->next) {
621 for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++);
622 if (p->str[i] == '\0')
629 * Record an option and arrange to act on it later. ARG should be the
630 * following command argument, which may or may not be used by this option.
632 * The `l' and `A' options are ignored here since they actually specify input
637 decode_option(swt, arg)
638 register char *swt, *arg;
640 if (!strcmp(swt + 1, "Bstatic"))
642 if (!strcmp(swt + 1, "Bdynamic"))
644 if (!strcmp(swt + 1, "Bforcedynamic"))
646 if (!strcmp(swt + 1, "Bsymbolic"))
648 if (!strcmp(swt + 1, "Bforcearchive"))
650 if (!strcmp(swt + 1, "Bshareable"))
652 if (!strcmp(swt + 1, "assert"))
655 if (!strcmp(swt + 1, "Bsilly"))
658 if (!strcmp(swt + 1, "Ttext")) {
659 text_start = parse(arg, "%x", "invalid argument to -Ttext");
660 T_flag_specified = 1;
663 if (!strcmp(swt + 1, "Tdata")) {
664 rrs_data_start = parse(arg, "%x", "invalid argument to -Tdata");
665 Tdata_flag_specified = 1;
668 if (!strcmp(swt + 1, "noinhibit-exec")) {
669 force_executable = 1;
672 if (!strcmp(swt + 1, "nostdlib")) {
684 specified_data_size = parse(arg, "%x", "invalid argument to -D");
688 if (swt[2] == 0 || *arg == 'c')
689 force_common_definition = 1;
690 else if (*arg == 'p')
691 force_alias_definition = 1;
693 errx(1, "-d option takes 'c' or 'p' argument");
697 entry_symbol = getsym(arg);
698 if (!entry_symbol->defined &&
699 !(entry_symbol->flags & GS_REFERENCED))
700 undefined_global_sym_count++;
701 entry_symbol->flags |= GS_REFERENCED;
702 add_cmdline_ref(entry_symbol);
729 output_filename = xmalloc(strlen(arg)+5);
730 strcpy(output_filename, arg);
731 strcat(output_filename, ".tmp");
732 real_output_filename = arg;
736 output_filename = arg;
750 relocatable_output = 1;
756 rrs_search_paths = (rrs_search_paths == NULL)
758 : concat(rrs_search_paths, ":", arg);
762 strip_symbols = STRIP_DEBUGGER;
766 strip_symbols = STRIP_ALL;
770 text_start = parse(arg, "%x", "invalid argument to -T");
771 T_flag_specified = 1;
780 register symbol *sp = getsym(arg);
782 if (!sp->defined && !(sp->flags & GS_REFERENCED))
783 undefined_global_sym_count++;
784 sp->flags |= GS_REFERENCED;
791 soversion = parse(arg, "%d", "invalid argument to -V");
796 discard_locals = DISCARD_L;
800 discard_locals = DISCARD_ALL;
805 register symbol *sp = getsym(&swt[2]);
806 sp->flags |= GS_TRACE;
819 magic = oldmagic = ZMAGIC;
826 errx(1, "invalid command option `%s'", swt);
830 /* Convenient functions for operating on one or all files being loaded. */
833 * Call FUNCTION on each input file entry. Do not call for entries for
834 * libraries; instead, call once for each library member that is being
837 * FUNCTION receives two arguments: the entry, and ARG.
841 each_file(function, arg)
842 register void (*function)();
847 for (i = 0; i < number_of_files; i++) {
848 register struct file_entry *entry = &file_table[i];
849 register struct file_entry *subentry;
851 if (entry->flags & E_SCRAPPED)
854 if (!(entry->flags & E_IS_LIBRARY))
855 (*function)(entry, arg);
857 subentry = entry->subfiles;
858 for (; subentry; subentry = subentry->chain) {
859 if (subentry->flags & E_SCRAPPED)
861 (*function)(subentry, arg);
865 if (entry->silly_archive) {
867 if (!(entry->flags & E_DYNAMIC))
870 if (!(entry->silly_archive->flags & E_IS_LIBRARY))
873 subentry = entry->silly_archive->subfiles;
874 for (; subentry; subentry = subentry->chain) {
875 if (subentry->flags & E_SCRAPPED)
877 (*function)(subentry, arg);
885 * Call FUNCTION on each input file entry until it returns a non-zero value.
886 * Return this value. Do not call for entries for libraries; instead, call
887 * once for each library member that is being loaded.
889 * FUNCTION receives two arguments: the entry, and ARG. It must be a function
890 * returning unsigned long (though this can probably be fudged).
894 check_each_file(function, arg)
895 register unsigned long (*function)();
899 register unsigned long return_val;
901 for (i = 0; i < number_of_files; i++) {
902 register struct file_entry *entry = &file_table[i];
903 if (entry->flags & E_SCRAPPED)
905 if (entry->flags & E_IS_LIBRARY) {
906 register struct file_entry *subentry = entry->subfiles;
907 for (; subentry; subentry = subentry->chain) {
908 if (subentry->flags & E_SCRAPPED)
910 if ( (return_val = (*function)(subentry, arg)) )
913 } else if ( (return_val = (*function)(entry, arg)) )
919 /* Like `each_file' but ignore files that were just for symbol definitions. */
922 each_full_file(function, arg)
923 register void (*function)();
928 for (i = 0; i < number_of_files; i++) {
929 register struct file_entry *entry = &file_table[i];
930 register struct file_entry *subentry;
932 if (entry->flags & (E_SCRAPPED | E_JUST_SYMS))
936 if (entry->silly_archive) {
938 if (!(entry->flags & E_DYNAMIC))
941 if (!(entry->silly_archive->flags & E_IS_LIBRARY))
944 subentry = entry->silly_archive->subfiles;
945 for (; subentry; subentry = subentry->chain) {
946 if (subentry->flags & E_SCRAPPED)
948 (*function)(subentry, arg);
952 if (entry->flags & E_DYNAMIC)
955 if (!(entry->flags & E_IS_LIBRARY))
956 (*function)(entry, arg);
958 subentry = entry->subfiles;
959 for (; subentry; subentry = subentry->chain) {
960 if (subentry->flags & E_SCRAPPED)
962 (*function)(subentry, arg);
968 /* Close the input file that is now open. */
979 * Open the input file specified by 'entry', and return a descriptor. The
980 * open file is remembered; if the same file is opened twice in a row, a new
981 * open is not actually done.
985 register struct file_entry *entry;
989 if (entry->superfile && (entry->superfile->flags & E_IS_LIBRARY))
990 return file_open(entry->superfile);
992 if (entry == input_file)
998 if (entry->flags & E_SEARCH_DIRS) {
1001 fd = open(entry->filename, O_RDONLY, 0);
1009 if (entry->flags & E_SEARCH_DIRS)
1010 errx(1, "%s: no match", entry->local_sym_name);
1012 err(1, "%s", entry->filename);
1018 struct file_entry *entry;
1020 return entry->starting_offset + N_TXTOFF (entry->header);
1023 /*---------------------------------------------------------------------------*/
1026 * Read a file's header into the proper place in the file_entry. FD is the
1027 * descriptor on which the file is open. ENTRY is the file's entry.
1030 read_header(fd, entry)
1032 struct file_entry *entry;
1036 if (lseek(fd, entry->starting_offset, L_SET) !=
1037 entry->starting_offset)
1038 err(1, "%s: read_header: lseek", get_file_name(entry));
1040 len = read(fd, &entry->header, sizeof(struct exec));
1041 if (len != sizeof (struct exec))
1042 err(1, "%s: read_header: read", get_file_name(entry));
1044 md_swapin_exec_hdr(&entry->header);
1046 if (N_BADMAG (entry->header))
1047 errx(1, "%s: bad magic", get_file_name(entry));
1049 if (N_BADMID(entry->header))
1050 errx(1, "%s: non-native input file", get_file_name(entry));
1052 entry->flags |= E_HEADER_VALID;
1056 * Read the symbols of file ENTRY into core. Assume it is already open, on
1057 * descriptor FD. Also read the length of the string table, which follows
1058 * the symbol table, but don't read the contents of the string table.
1062 read_entry_symbols(fd, entry)
1063 struct file_entry *entry;
1070 if (!(entry->flags & E_HEADER_VALID))
1071 read_header(fd, entry);
1073 np = (struct nlist *)alloca(entry->header.a_syms);
1074 entry->nsymbols = entry->header.a_syms / sizeof(struct nlist);
1075 if (entry->nsymbols == 0)
1078 entry->symbols = (struct localsymbol *)
1079 xmalloc(entry->nsymbols * sizeof(struct localsymbol));
1081 if (lseek(fd, N_SYMOFF(entry->header) + entry->starting_offset, L_SET)
1082 != N_SYMOFF(entry->header) + entry->starting_offset)
1083 err(1, "%s: read_symbols: lseek(syms)", get_file_name(entry));
1085 if (entry->header.a_syms != read(fd, np, entry->header.a_syms))
1086 errx(1, "%s: read_symbols: premature end of file in symbols",
1087 get_file_name(entry));
1089 md_swapin_symbols(np, entry->header.a_syms / sizeof(struct nlist));
1091 for (i = 0; i < entry->nsymbols; i++) {
1092 entry->symbols[i].nzlist.nlist = *np++;
1093 entry->symbols[i].nzlist.nz_size = 0;
1094 entry->symbols[i].symbol = NULL;
1095 entry->symbols[i].next = NULL;
1096 entry->symbols[i].entry = entry;
1097 entry->symbols[i].gotslot_offset = -1;
1098 entry->symbols[i].flags = 0;
1101 entry->strings_offset = N_STROFF(entry->header) +
1102 entry->starting_offset;
1103 if (lseek(fd, entry->strings_offset, 0) == (off_t)-1)
1104 err(1, "%s: read_symbols: lseek(strings)",
1105 get_file_name(entry));
1106 if (sizeof str_size != read(fd, &str_size, sizeof str_size))
1107 errx(1, "%s: read_symbols: cannot read string table size",
1108 get_file_name(entry));
1110 entry->string_size = md_swap_long(str_size);
1114 * Read the string table of file ENTRY open on descriptor FD, into core.
1117 read_entry_strings(fd, entry)
1118 struct file_entry *entry;
1122 if (entry->string_size == 0)
1125 if (!(entry->flags & E_HEADER_VALID) || !entry->strings_offset)
1126 errx(1, "%s: read_strings: string table unavailable",
1127 get_file_name(entry));
1129 if (lseek(fd, entry->strings_offset, L_SET) !=
1130 entry->strings_offset)
1131 err(1, "%s: read_strings: lseek",
1132 get_file_name(entry));
1134 if (read(fd, entry->strings, entry->string_size) !=
1136 errx(1, "%s: read_strings: premature end of file in strings",
1137 get_file_name(entry));
1142 /* Read in the relocation sections of ENTRY if necessary */
1145 read_entry_relocation(fd, entry)
1147 struct file_entry *entry;
1149 register struct relocation_info *reloc;
1152 if (!entry->textrel) {
1154 reloc = (struct relocation_info *)
1155 xmalloc(entry->header.a_trsize);
1157 pos = text_offset(entry) +
1158 entry->header.a_text + entry->header.a_data;
1160 if (lseek(fd, pos, L_SET) != pos)
1161 err(1, "%s: read_reloc(text): lseek",
1162 get_file_name(entry));
1164 if (read(fd, reloc, entry->header.a_trsize) !=
1165 entry->header.a_trsize)
1166 errx(1, "%s: read_reloc(text): premature EOF",
1167 get_file_name(entry));
1169 md_swapin_reloc(reloc, entry->header.a_trsize / sizeof(*reloc));
1170 entry->textrel = reloc;
1171 entry->ntextrel = entry->header.a_trsize / sizeof(*reloc);
1175 if (!entry->datarel) {
1177 reloc = (struct relocation_info *)
1178 xmalloc(entry->header.a_drsize);
1180 pos = text_offset(entry) + entry->header.a_text +
1181 entry->header.a_data + entry->header.a_trsize;
1183 if (lseek(fd, pos, L_SET) != pos)
1184 err(1, "%s: read_reloc(data): lseek",
1185 get_file_name(entry));
1187 if (read(fd, reloc, entry->header.a_drsize) !=
1188 entry->header.a_drsize)
1189 errx(1, "%s: read_reloc(data): premature EOF",
1190 get_file_name(entry));
1192 md_swapin_reloc(reloc, entry->header.a_drsize / sizeof(*reloc));
1193 entry->datarel = reloc;
1194 entry->ndatarel = entry->header.a_drsize / sizeof(*reloc);
1199 /*---------------------------------------------------------------------------*/
1202 * Read in the symbols of all input files.
1210 fprintf(stderr, "Loading symbols:\n\n");
1212 for (i = 0; i < number_of_files; i++)
1213 read_file_symbols(&file_table[i]);
1216 fprintf(stderr, "\n");
1220 * If ENTRY is a rel file, read its symbol and string sections into core. If
1221 * it is a library, search it and load the appropriate members (which means
1222 * calling this function recursively on those members).
1226 read_file_symbols(entry)
1227 register struct file_entry *entry;
1233 fd = file_open(entry);
1235 len = read(fd, &hdr, sizeof hdr);
1236 if (len != sizeof hdr)
1237 errx(1, "%s: read_file_symbols(header): premature EOF",
1238 get_file_name(entry));
1240 md_swapin_exec_hdr(&hdr);
1242 if (!N_BADMAG (hdr)) {
1243 if (N_IS_DYNAMIC(hdr) && !(entry->flags & E_JUST_SYMS)) {
1244 if (relocatable_output) {
1246 "%s: -r and shared objects currently not supported",
1247 get_file_name(entry));
1250 #if notyet /* Compatibility */
1251 if (!(N_GETFLAG(hdr) & EX_PIC))
1252 warnx("%s: EX_PIC not set",
1253 get_file_name(entry));
1255 entry->flags |= E_DYNAMIC;
1256 if (entry->superfile || rrs_add_shobj(entry))
1257 read_shared_object(fd, entry);
1259 entry->flags |= E_SCRAPPED;
1261 if (N_GETFLAG(hdr) & EX_PIC)
1263 read_entry_symbols(fd, entry);
1264 entry->strings = (char *)alloca(entry->string_size);
1265 read_entry_strings(fd, entry);
1266 read_entry_relocation(fd, entry);
1267 enter_file_symbols(entry);
1274 if (SARMAG != read(fd, armag, SARMAG) ||
1275 strncmp (armag, ARMAG, SARMAG))
1277 "%s: malformed input file (not rel or archive)",
1278 get_file_name(entry));
1279 entry->flags |= E_IS_LIBRARY;
1280 search_library(fd, entry);
1288 * Enter the external symbol defs and refs of ENTRY in the hash table.
1292 enter_file_symbols(entry)
1293 struct file_entry *entry;
1295 struct localsymbol *lsp, *lspend;
1298 prline_file_name(entry, stderr);
1300 lspend = entry->symbols + entry->nsymbols;
1302 for (lsp = entry->symbols; lsp < lspend; lsp++) {
1303 register struct nlist *p = &lsp->nzlist.nlist;
1305 if (p->n_type == (N_SETV | N_EXT))
1309 * Turn magically prefixed symbols into set symbols of
1310 * a corresponding type.
1312 if (set_element_prefixes &&
1313 set_element_prefixed_p(entry->strings+lsp->nzlist.nz_strx))
1314 lsp->nzlist.nz_type += (N_SETA - N_ABS);
1316 if (SET_ELEMENT_P(p->n_type)) {
1318 if (!relocatable_output)
1319 enter_global_ref(lsp,
1320 p->n_un.n_strx + entry->strings, entry);
1321 } else if (p->n_type == N_WARNING) {
1322 char *msg = p->n_un.n_strx + entry->strings;
1324 /* Grab the next entry. */
1326 p = &lsp->nzlist.nlist;
1327 if (p->n_type != (N_UNDF | N_EXT)) {
1329 "%s: Warning symbol without external reference following.",
1330 get_file_name(entry));
1331 make_executable = 0;
1332 lsp--; /* Process normally. */
1335 char *name = p->n_un.n_strx + entry->strings;
1336 /* Deal with the warning symbol. */
1337 lsp->flags |= LS_WARNING;
1338 enter_global_ref(lsp, name, entry);
1340 if (sp->warning == NULL) {
1341 sp->warning = (char *)
1342 xmalloc(strlen(msg)+1);
1343 strcpy(sp->warning, msg);
1345 } else if (strcmp(sp->warning, msg))
1347 "%s: multiple definitions for warning symbol `%s'",
1348 get_file_name(entry), demangle(sp->name));
1350 } else if (p->n_type & N_EXT) {
1351 enter_global_ref(lsp,
1352 p->n_un.n_strx + entry->strings, entry);
1353 } else if (p->n_un.n_strx &&
1354 (p->n_un.n_strx + entry->strings)[0] == LPREFIX)
1355 lsp->flags |= LS_L_SYMBOL;
1361 * Enter one global symbol in the hash table. LSP points to the `struct
1362 * localsymbol' from the file that describes the global symbol. NAME is the
1363 * symbol's name. ENTRY is the file entry for the file the symbol comes from.
1365 * LSP is put on the chain of all such structs that refer to the same symbol.
1366 * This chain starts in the `refs' for symbols from relocatable objects. A
1367 * backpointer to the global symbol is kept in LSP.
1369 * Symbols from shared objects are linked through `soref'. For such symbols
1370 * that's all we do at this stage, with the exception of the case where the
1371 * symbol is a common. The `referenced' bit is only set for references from
1372 * relocatable objects.
1377 enter_global_ref(lsp, name, entry)
1378 struct localsymbol *lsp;
1380 struct file_entry *entry;
1382 register struct nzlist *nzp = &lsp->nzlist;
1383 register symbol *sp = getsym(name);
1384 register int type = nzp->nz_type;
1385 int oldref = (sp->flags & GS_REFERENCED);
1386 int olddef = sp->defined;
1387 int com = sp->defined && sp->common_size;
1389 if (type == (N_INDR | N_EXT) && !olddef) {
1390 sp->alias = getsym(entry->strings + (lsp + 1)->nzlist.nz_strx);
1391 if (sp == sp->alias) {
1392 warnx("%s: %s is alias for itself",
1393 get_file_name(entry), name);
1394 /* Rewrite symbol as global text symbol with value 0 */
1395 lsp->nzlist.nz_type = N_TEXT|N_EXT;
1396 lsp->nzlist.nz_value = 0;
1397 make_executable = 0;
1400 if (sp->flags & GS_REFERENCED)
1401 sp->alias->flags |= GS_REFERENCED;
1405 if (entry->flags & E_DYNAMIC) {
1406 lsp->next = sp->sorefs;
1411 * Handle commons from shared objects:
1412 * 1) If symbol hitherto undefined, turn it into a common.
1413 * 2) If symbol already common, update size if necessary.
1415 /*XXX - look at case where commons are only in shared objects */
1416 if (type == (N_UNDF | N_EXT) && nzp->nz_value) {
1419 undefined_global_sym_count--;
1420 common_defined_global_count++;
1421 sp->common_size = nzp->nz_value;
1422 sp->defined = N_UNDF | N_EXT;
1423 } else if (com && sp->common_size < nzp->nz_value) {
1424 sp->common_size = nzp->nz_value;
1426 } else if (type != (N_UNDF | N_EXT) && !oldref) {
1428 * This is an ex common...
1431 common_defined_global_count--;
1432 sp->common_size = 0;
1437 * Handle size information in shared objects.
1439 if (nzp->nz_size > sp->size)
1440 sp->size = nzp->nz_size;
1442 if ((lsp->flags & LS_WARNING) && (sp->flags & GS_REFERENCED))
1444 * Prevent warning symbols from getting
1445 * gratuitously referenced.
1447 list_warning_symbols = 1;
1451 lsp->next = sp->refs;
1455 if (lsp->flags & LS_WARNING) {
1457 * Prevent warning symbols from getting
1458 * gratuitously referenced.
1460 if (sp->flags & GS_REFERENCED)
1461 list_warning_symbols = 1;
1466 list_warning_symbols = 1;
1468 sp->flags |= GS_REFERENCED;
1470 if (sp == dynamic_symbol || sp == got_symbol) {
1471 if (type != (N_UNDF | N_EXT) && !(entry->flags & E_JUST_SYMS))
1472 errx(1,"Linker reserved symbol %s defined as type %x ",
1477 if (olddef && N_ISWEAK(&nzp->nlist) && !(sp->flags & GS_WEAK)) {
1479 printf("%s: not overridden by weak symbol from %s\n",
1480 demangle(sp->name), get_file_name(entry));
1485 if (type == (N_SIZE | N_EXT)) {
1487 if (relocatable_output && nzp->nz_value != 0 && sp->size == 0)
1489 if (sp->size < nzp->nz_value)
1490 sp->size = nzp->nz_value;
1492 } else if (type != (N_UNDF | N_EXT) || nzp->nz_value) {
1495 * Set `->defined' here, so commons and undefined globals
1496 * can be counted correctly.
1498 if (!sp->defined || sp->defined == (N_UNDF | N_EXT)) {
1502 if ((sp->flags & GS_WEAK) && !N_ISWEAK(&nzp->nlist)) {
1504 * Upgrade an existing weak definition.
1505 * We fake it by pretending the symbol is undefined;
1506 * must undo any common fiddling, however.
1509 errx(1, "internal error: enter_glob_ref: "
1510 "weak symbol not referenced");
1511 if (!olddef && !com)
1512 undefined_weak_sym_count--;
1513 undefined_global_sym_count++;
1515 sp->flags &= ~GS_WEAK;
1518 common_defined_global_count--;
1520 sp->common_size = 0;
1522 if (oldref && !olddef) {
1524 * It used to be undefined and we're defining it.
1526 undefined_global_sym_count--;
1527 if (sp->flags & GS_WEAK)
1528 /* Used to be a weak reference */
1529 undefined_weak_sym_count--;
1530 if (undefined_global_sym_count < 0 ||
1531 undefined_weak_sym_count < 0)
1532 errx(1, "internal error: enter_glob_ref: "
1533 "undefined_global_sym_count = %d, "
1534 "undefined_weak_sym_count = %d",
1535 undefined_global_sym_count,
1536 undefined_weak_sym_count);
1540 if (N_ISWEAK(&nzp->nlist))
1541 /* The definition is weak */
1542 sp->flags |= GS_WEAK;
1544 if (!olddef && type == (N_UNDF | N_EXT) && nzp->nz_value) {
1546 * First definition and it's common.
1548 common_defined_global_count++;
1549 sp->common_size = nzp->nz_value;
1550 } else if (com && type != (N_UNDF | N_EXT)) {
1552 * It used to be common and we're defining
1553 * it as something else.
1555 common_defined_global_count--;
1556 sp->common_size = 0;
1557 } else if (com && type == (N_UNDF | N_EXT) &&
1558 sp->common_size < nzp->nz_value)
1560 * It used to be common and this is a new common entry
1561 * to which we need to pay attention.
1563 sp->common_size = nzp->nz_value;
1565 if (SET_ELEMENT_P(type) && (!olddef || com))
1568 } else if (!oldref && !com) {
1570 * An unreferenced symbol can already be defined
1571 * as common by shared objects.
1573 undefined_global_sym_count++;
1574 if (N_ISWEAK(&nzp->nlist)) {
1575 /* The reference is weak */
1576 sp->flags |= GS_WEAK;
1577 undefined_weak_sym_count++;
1581 if (sp == end_symbol && (entry->flags & E_JUST_SYMS) &&
1583 text_start = nzp->nz_value;
1585 if (sp->flags & GS_TRACE) {
1586 register char *reftype;
1587 switch (type & N_TYPE) {
1589 reftype = nzp->nz_value
1590 ? "defined as common" : "referenced";
1594 reftype = "defined as absolute";
1598 reftype = "defined in text section";
1602 reftype = "defined in data section";
1606 reftype = "defined in BSS section";
1614 reftype = "size spec";
1618 reftype = "I don't know this type";
1622 fprintf(stderr, "symbol %s %s%s in ", demangle(sp->name),
1623 (N_ISWEAK(&nzp->nlist))?"weakly ":"", reftype);
1624 print_file_name (entry, stderr);
1625 fprintf(stderr, "\n");
1630 * This returns 0 if the given file entry's symbol table does *not* contain
1631 * the nlist point entry, and it returns the files entry pointer (cast to
1632 * unsigned long) if it does.
1636 contains_symbol(entry, np)
1637 struct file_entry *entry;
1638 register struct nlist *np;
1640 if (np >= &entry->symbols->nzlist.nlist &&
1641 np < &(entry->symbols + entry->nsymbols)->nzlist.nlist)
1642 return (unsigned long) entry;
1648 * Having entered all the global symbols and found the sizes of sections of
1649 * all files to be linked, make all appropriate deductions from this data.
1651 * We propagate global symbol values from definitions to references. We compute
1652 * the layout of the output file and where each input file's contents fit
1655 * This is now done in several stages.
1657 * 1) All global symbols are examined for definitions in relocatable (.o)
1658 * files. The symbols' type is set according to the definition found,
1659 * but its value can not yet be determined. In stead, we keep a pointer
1660 * to the file entry's localsymbol that bequeathed the global symbol with
1661 * its definition. Also, multiple (incompatible) definitions are checked
1662 * for in this pass. If no definition comes forward, the set of local
1663 * symbols originating from shared objects is searched for a definition.
1665 * 2) Then the relocation information of each relocatable file is examined
1666 * for possible contributions to the RRS section.
1668 * 3) When this is done, the sizes and start addresses are set of all segments
1669 * that will appear in the output file (including the RRS segment).
1671 * 4) Finally, all symbols are relocated according according to the start
1672 * of the entry they are part of. Then global symbols are assigned their
1673 * final values. Also, space for commons and imported data are allocated
1674 * during this pass, if the link mode in effect so demands.
1683 fprintf(stderr, "Digesting symbol information:\n\n");
1685 if (!relocatable_output) {
1687 * The set sector size is the number of set elements + a word
1688 * for each symbol for the length word at the beginning of
1689 * the vector, plus a word for each symbol for a zero at the
1690 * end of the vector (for incremental linking).
1692 set_sect_size = (set_symbol_count + 2 * set_vector_count) *
1693 sizeof (unsigned long);
1694 set_vectors = (long *)xmalloc (set_sect_size);
1695 setv_fill_count = 0;
1698 /* Pass 1: check and define symbols */
1699 defined_global_sym_count = 0;
1702 each_full_file(consider_relocation, (void *)0); /* Text */
1703 each_full_file(consider_relocation, (void *)1); /* Data */
1705 each_file(consider_local_symbols, (void *)0);
1708 * Compute total size of sections.
1709 * RRS data is the first output data section, RRS text is the last
1710 * text section. Thus, DATA_START is calculated from RRS_DATA_START
1711 * and RRS_DATA_SIZE, while RRS_TEXT_START is derived from TEXT_START
1714 consider_rrs_section_lengths();
1715 each_full_file(consider_file_section_lengths, 0);
1716 rrs_text_start = text_start + text_size;
1717 text_size += rrs_text_size;
1718 data_size += rrs_data_size;
1721 * If necessary, pad text section to full page in the file. Include
1722 * the padding in the text segment size.
1725 if (page_align_segments || page_align_data) {
1726 int text_end = text_size + N_TXTOFF(outheader);
1727 text_pad = PALIGN(text_end, page_size) - text_end;
1728 text_size += text_pad;
1730 outheader.a_text = text_size;
1733 * Make the data segment address start in memory on a suitable
1737 if (!Tdata_flag_specified)
1738 rrs_data_start = text_start +
1739 DATA_START(outheader) - TEXT_START(outheader);
1741 data_start = rrs_data_start + rrs_data_size;
1742 if (!relocatable_output) {
1743 set_sect_start = rrs_data_start + data_size;
1744 data_size += MALIGN(set_sect_size);
1746 bss_start = rrs_data_start + data_size;
1749 printf("textstart = %#x, textsize = %#x, rrs_text_start = %#x, rrs_text_size %#x\n",
1750 text_start, text_size, rrs_text_start, rrs_text_size);
1751 printf("datastart = %#x, datasize = %#x, rrs_data_start %#x, rrs_data_size %#x\n",
1752 data_start, data_size, rrs_data_start, rrs_data_size);
1753 printf("bssstart = %#x, bsssize = %#x\n",
1754 bss_start, bss_size);
1755 printf("set_sect_start = %#x, set_sect_size = %#x\n",
1756 set_sect_start, set_sect_size);
1759 /* Compute start addresses of each file's sections and symbols. */
1761 each_full_file(relocate_file_addresses, 0);
1762 relocate_rrs_addresses();
1764 /* Pass 2: assign values to symbols */
1767 if (end_symbol) { /* These are null if -r. */
1768 etext_symbol->value = text_start + text_size - text_pad;
1769 edata_symbol->value = rrs_data_start + data_size;
1770 end_symbol->value = rrs_data_start + data_size + bss_size;
1773 * Figure the data_pad now, so that it overlaps with the bss
1777 if (specified_data_size && specified_data_size > data_size)
1778 data_pad = specified_data_size - data_size;
1780 if (page_align_segments)
1781 data_pad = PALIGN(data_pad + data_size, page_size) - data_size;
1783 bss_size -= data_pad;
1787 data_size += data_pad;
1790 * Calculate total number of symbols that will go into
1791 * the output symbol table (barring DISCARD_* settings).
1793 global_sym_count = defined_global_sym_count +
1794 undefined_global_sym_count;
1796 if (dynamic_symbol->flags & GS_REFERENCED)
1799 if (got_symbol->flags & GS_REFERENCED)
1802 if (relocatable_output || building_shared_object) {
1803 /* For each alias we write out two struct nlists */
1804 global_sym_count += global_alias_count;
1805 /* Propagate warning symbols; costs two extra struct nlists */
1806 global_sym_count += 2 * warn_sym_count;
1809 if (relocatable_output)
1810 /* We write out the original N_SIZE symbols */
1811 global_sym_count += size_sym_count;
1815 "global symbols %d "
1816 "(defined %d, undefined %d, weak %d, aliases %d, warnings 2 * %d, "
1817 "size symbols %d)\ncommons %d, locals: %d, debug symbols: %d, set_symbols %d\n",
1819 defined_global_sym_count, undefined_global_sym_count,
1820 undefined_weak_sym_count,
1821 global_alias_count, warn_sym_count, size_sym_count,
1822 common_defined_global_count, local_sym_count,
1823 debugger_sym_count, set_symbol_count);
1828 * Determine the definition of each global symbol.
1835 * For each symbol, verify that it is defined globally at most
1836 * once within relocatable files (except when building a shared lib).
1837 * and set the `defined' field if there is a definition.
1839 * Then check the shared object symbol chain for any remaining
1840 * undefined symbols. Set the `so_defined' field for any
1841 * definition find this way.
1843 FOR_EACH_SYMBOL(i, sp) {
1845 struct localsymbol *lsp;
1848 if (!(sp->flags & GS_REFERENCED)) {
1850 /* Check for undefined symbols in shared objects */
1852 for (lsp = sp->sorefs; lsp; lsp = lsp->next) {
1853 type = lsp->nzlist.nlist.n_type;
1854 if ((type & N_EXT) && type != (N_UNDF | N_EXT))
1857 if ((type & N_EXT) && type == (N_UNDF | N_EXT))
1858 undefined_shobj_sym_count++;
1861 /* Superfluous symbol from shared object */
1865 /* Already examined; must have been an alias */
1868 if (sp == got_symbol || sp == dynamic_symbol)
1871 for (lsp = sp->refs; lsp; lsp = lsp->next) {
1872 register struct nlist *p = &lsp->nzlist.nlist;
1873 register int type = p->n_type;
1875 if (SET_ELEMENT_P(type)) {
1876 if (relocatable_output)
1878 "internal error: global ref to set el %s with -r",
1879 demangle(sp->name));
1881 sp->defined = N_SETV | N_EXT;
1883 setv_fill_count++ * sizeof(long);
1884 } else if ((sp->defined & N_TYPE) != N_SETV) {
1886 multiple_def_count++;
1888 /* Keep count and remember symbol */
1890 set_vectors[setv_fill_count++] = (long)p;
1891 if (building_shared_object) {
1892 struct relocation_info reloc;
1895 * Make sure to relocate the contents
1896 * of this set vector.
1898 bzero(&reloc, sizeof(reloc));
1899 RELOC_INIT_SEGMENT_RELOC(&reloc);
1900 RELOC_ADDRESS(&reloc) =
1901 setv_fill_count * sizeof(long);
1902 alloc_rrs_segment_reloc(NULL, &reloc);
1905 } else if ((type & N_EXT) && type != (N_UNDF | N_EXT)
1906 && (type & N_TYPE) != N_FN
1907 && (type & N_TYPE) != N_SIZE) {
1908 /* non-common definition */
1913 multiple_def_count++;
1914 } else if (!N_ISWEAK(p) ||
1915 (!sp->def_lsp && !sp->common_size)) {
1917 lsp->entry->flags |= E_SYMBOLS_USED;
1925 * If this symbol has acquired final definition, we're done.
1926 * Commons must be allowed to bind to shared object data
1930 (sp->common_size == 0 ||
1931 relocatable_output || building_shared_object)) {
1932 if ((sp->defined & N_TYPE) == N_SETV)
1933 /* Allocate zero entry in set vector */
1936 * At this stage, we do not know whether an alias
1937 * is going to be defined for real here, or whether
1938 * it refers to a shared object symbol. The decision
1939 * is deferred until digest_pass2().
1942 defined_global_sym_count++;
1946 if (relocatable_output)
1951 * Still undefined, search the shared object symbols for a
1952 * definition. This symbol must go into the RRS.
1954 if (building_shared_object) {
1955 /* Just punt for now */
1956 undefined_global_sym_count--;
1957 if (undefined_global_sym_count < 0)
1959 "internal error: digest_pass1,1: %s: undefined_global_sym_count = %d",
1960 demangle(sp->name), undefined_global_sym_count);
1966 for (lsp = sp->sorefs; lsp; lsp = lsp->next) {
1967 register struct nlist *p = &lsp->nzlist.nlist;
1968 register int type = p->n_type;
1970 if ((type & N_EXT) && type != (N_UNDF | N_EXT) &&
1971 (type & N_TYPE) != N_FN) {
1972 /* non-common definition */
1973 if (sp->common_size) {
1975 * This common has an so defn; switch
1976 * to it iff defn is: data, first-class
1979 if (N_AUX(p) != AUX_OBJECT ||
1981 (lsp->entry->flags & E_SECONDCLASS))
1985 * Change common to so ref. First,
1986 * downgrade common to undefined.
1988 sp->common_size = 0;
1990 common_defined_global_count--;
1991 undefined_global_sym_count++;
1994 sp->so_defined = type;
1996 if (lsp->entry->flags & E_SECONDCLASS)
1997 /* Keep looking for something better */
2000 /* Keep looking for something better */
2007 printf("pass1: SO definition for %s, type %x in %s at %#x\n",
2008 demangle(sp->name), sp->so_defined, get_file_name(sp->def_lsp->entry),
2009 sp->def_lsp->nzlist.nz_value);
2011 sp->def_lsp->entry->flags |= E_SYMBOLS_USED;
2012 if (sp->flags & GS_REFERENCED) {
2013 undefined_global_sym_count--;
2015 sp->flags |= GS_REFERENCED;
2016 if (undefined_global_sym_count < 0)
2017 errx(1, "internal error: digest_pass1,2: "
2018 "%s: undefined_global_sym_count = %d",
2019 demangle(sp->name), undefined_global_sym_count);
2021 !(sp->alias->flags & GS_REFERENCED)) {
2025 } else if (sp->defined) {
2026 if (sp->common_size == 0)
2027 errx(1, "internal error: digest_pass1,3: "
2028 "%s: not a common: %x",
2029 demangle(sp->name), sp->defined);
2031 * Common not bound to shared object data; treat
2032 * it now like other defined symbols were above.
2035 defined_global_sym_count++;
2040 if (setv_fill_count != set_sect_size/sizeof(long))
2041 errx(1, "internal error: allocated set symbol space (%d) "
2042 "doesn't match actual (%d)",
2043 set_sect_size/sizeof(long), setv_fill_count);
2048 * Scan relocation info in ENTRY for contributions to the RRS section
2049 * of the output file.
2052 consider_relocation(entry, dataseg)
2053 struct file_entry *entry;
2056 struct relocation_info *reloc, *end;
2057 struct localsymbol *lsp;
2061 /* Text relocations */
2062 reloc = entry->textrel;
2063 end = entry->textrel + entry->ntextrel;
2065 /* Data relocations */
2066 reloc = entry->datarel;
2067 end = entry->datarel + entry->ndatarel;
2070 for (; reloc < end; reloc++) {
2072 if (relocatable_output) {
2073 lsp = &entry->symbols[reloc->r_symbolnum];
2074 if (RELOC_BASEREL_P(reloc)) {
2075 pic_code_seen = 1; /* Compatibility */
2076 if (!RELOC_EXTERN_P(reloc))
2077 lsp->flags |= LS_RENAME;
2083 * First, do the PIC specific relocs.
2084 * r_relative and r_copy should not occur at this point
2085 * (we do output them). The others break down to these
2088 * jmptab: extern: needs jmp slot
2089 * !extern: "intersegment" jump/call,
2090 * should get resolved in output
2092 * baserel: extern: need GOT entry
2093 * !extern: may need GOT entry,
2096 * baserel's always refer to symbol through `r_symbolnum'
2097 * whether extern or not. Internal baserels refer to statics
2098 * that must be accessed either *through* the GOT table like
2099 * global data, or by means of an offset from the GOT table.
2100 * The macro RELOC_STATICS_THROUGH_GOT_P() determines which
2101 * applies, since this is a machine (compiler?) dependent
2105 if (RELOC_JMPTAB_P(reloc)) {
2107 if (!RELOC_EXTERN_P(reloc))
2110 lsp = &entry->symbols[reloc->r_symbolnum];
2114 if (sp->flags & GS_TRACE) {
2115 fprintf(stderr, "symbol %s has jmpslot in %s\n",
2116 demangle(sp->name), get_file_name(entry));
2118 alloc_rrs_jmpslot(entry, sp);
2120 } else if (RELOC_BASEREL_P(reloc)) {
2122 lsp = &entry->symbols[reloc->r_symbolnum];
2123 alloc_rrs_gotslot(entry, reloc, lsp);
2124 if (pic_type != PIC_TYPE_NONE &&
2125 RELOC_PIC_TYPE(reloc) != pic_type)
2126 errx(1, "%s: illegal reloc type mix",
2127 get_file_name(entry));
2128 pic_type = RELOC_PIC_TYPE(reloc);
2130 } else if (RELOC_EXTERN_P(reloc)) {
2133 * Non-PIC relocations.
2134 * If the definition comes from a shared object
2135 * we need a relocation entry in RRS.
2137 * If the .so definition is N_TEXT a jmpslot is
2140 * If it is N_DATA we allocate an address in BSS (?)
2141 * and arrange for the data to be copied at run-time.
2142 * The symbol is temporarily marked with N_SIZE in
2143 * the `defined' field, so we know what to do in
2144 * pass2() and during actual relocation. We convert
2145 * the type back to something real again when writing
2149 lsp = &entry->symbols[reloc->r_symbolnum];
2152 errx(1, "%s: bogus relocation record",
2153 get_file_name(entry));
2159 * Skip refs to _GLOBAL_OFFSET_TABLE_ and __DYNAMIC
2161 if (sp == got_symbol) {
2162 if (!CHECK_GOT_RELOC(reloc))
2164 "%s: Unexpected relocation type for GOT symbol",
2165 get_file_name(entry));
2170 * This symbol gives rise to a RRS entry
2173 if (building_shared_object) {
2174 if (sp->flags & GS_TRACE) {
2176 "symbol %s RRS entry in %s\n",
2177 demangle(sp->name), get_file_name(entry));
2179 alloc_rrs_reloc(entry, sp);
2183 if (force_alias_definition && sp->so_defined &&
2184 sp->aux == AUX_FUNC) {
2186 /* Call to shared library procedure */
2187 alloc_rrs_jmpslot(entry, sp);
2189 } else if (sp->size && sp->so_defined &&
2190 sp->aux == AUX_OBJECT) {
2192 /* Reference to shared library data */
2193 alloc_rrs_cpy_reloc(entry, sp);
2194 sp->defined = N_SIZE;
2196 } else if (!sp->defined && sp->common_size == 0 &&
2198 alloc_rrs_reloc(entry, sp);
2202 * Segment relocation.
2203 * Prepare an RRS relocation as these are load
2204 * address dependent.
2206 if (building_shared_object && !RELOC_PCREL_P(reloc)) {
2207 alloc_rrs_segment_reloc(entry, reloc);
2214 * Determine the disposition of each local symbol.
2217 consider_local_symbols(entry)
2218 register struct file_entry *entry;
2220 register struct localsymbol *lsp, *lspend;
2222 if (entry->flags & E_DYNAMIC)
2225 lspend = entry->symbols + entry->nsymbols;
2228 * For each symbol determine whether it should go
2229 * in the output symbol table.
2232 for (lsp = entry->symbols; lsp < lspend; lsp++) {
2233 register struct nlist *p = &lsp->nzlist.nlist;
2234 register int type = p->n_type;
2236 if (type == N_WARNING)
2239 if (SET_ELEMENT_P (type)) {
2241 * This occurs even if global. These types of
2242 * symbols are never written globally, though
2243 * they are stored globally.
2245 if (relocatable_output)
2246 lsp->flags |= LS_WRITE;
2248 } else if (!(type & (N_STAB | N_EXT))) {
2251 * Ordinary local symbol
2253 if ((lsp->flags & LS_RENAME) || (
2254 discard_locals != DISCARD_ALL &&
2255 !(discard_locals == DISCARD_L &&
2256 (lsp->flags & LS_L_SYMBOL))) ) {
2258 lsp->flags |= LS_WRITE;
2262 } else if (!(type & N_EXT)) {
2267 if (strip_symbols == STRIP_NONE) {
2268 lsp->flags |= LS_WRITE;
2269 debugger_sym_count++;
2275 * Count one for the local symbol that we generate,
2276 * whose name is the file's name (usually) and whose address
2277 * is the start of the file's text.
2279 if (discard_locals != DISCARD_ALL)
2284 * Accumulate the section sizes of input file ENTRY into the section sizes of
2288 consider_file_section_lengths(entry)
2289 register struct file_entry *entry;
2292 entry->text_start_address = text_size;
2293 /* If there were any vectors, we need to chop them off */
2294 text_size += entry->header.a_text;
2295 entry->data_start_address = data_size;
2296 data_size += entry->header.a_data;
2297 entry->bss_start_address = bss_size;
2298 bss_size += MALIGN(entry->header.a_bss);
2300 text_reloc_size += entry->header.a_trsize;
2301 data_reloc_size += entry->header.a_drsize;
2305 * Determine where the sections of ENTRY go into the output file,
2306 * whose total section sizes are already known.
2307 * Also relocate the addresses of the file's local and debugger symbols.
2310 relocate_file_addresses(entry)
2311 register struct file_entry *entry;
2313 register struct localsymbol *lsp, *lspend;
2315 entry->text_start_address += text_start;
2317 * Note that `data_start' and `data_size' have not yet been
2318 * adjusted for `data_pad'. If they had been, we would get the wrong
2321 entry->data_start_address += data_start;
2322 entry->bss_start_address += bss_start;
2324 printf("%s: datastart: %#x, bss %#x\n", get_file_name(entry),
2325 entry->data_start_address, entry->bss_start_address);
2328 lspend = entry->symbols + entry->nsymbols;
2330 for (lsp = entry->symbols; lsp < lspend; lsp++) {
2331 register struct nlist *p = &lsp->nzlist.nlist;
2332 register int type = p->n_type;
2335 * If this belongs to a section, update it
2336 * by the section's start address
2339 switch (type & N_TYPE) {
2342 p->n_value += entry->text_start_address;
2348 * A symbol whose value is in the data section is
2349 * present in the input file as if the data section
2350 * started at an address equal to the length of the
2353 p->n_value += entry->data_start_address -
2354 entry->header.a_text;
2358 /* likewise for symbols with value in BSS. */
2359 p->n_value += entry->bss_start_address -
2360 (entry->header.a_text +
2361 entry->header.a_data);
2370 * Assign a value to each global symbol.
2375 FOR_EACH_SYMBOL(i, sp) {
2377 int align = sizeof(int);
2379 if (!(sp->flags & GS_REFERENCED))
2383 (relocatable_output || building_shared_object ||
2384 (sp->alias->defined && !sp->alias->so_defined))) {
2386 * The alias points at a defined symbol, so it
2387 * must itself be counted as one too, in order to
2388 * compute the correct number of symbol table entries.
2392 * Change aliased symbol's definition too.
2393 * These things happen if shared object commons
2394 * or data is going into our symbol table.
2396 if (sp->so_defined != (N_INDR+N_EXT))
2397 warnx( "pass2: %s: alias isn't",
2398 demangle(sp->name));
2399 sp->defined = sp->so_defined;
2402 defined_global_sym_count++;
2406 * Count the aliases that will appear in the output.
2408 if (sp->alias && !sp->so_defined && !sp->alias->so_defined &&
2409 (sp->defined || relocatable_output ||
2410 !building_shared_object))
2411 global_alias_count++;
2413 if ((sp->defined & N_TYPE) == N_SETV) {
2415 * Set length word at front of vector and zero byte
2416 * at end. Reverse the vector itself to put it in
2419 unsigned long i, *p, *q;
2420 unsigned long length_word_index =
2421 sp->value / sizeof(long);
2423 /* Relocate symbol value */
2424 sp->value += set_sect_start;
2426 set_vectors[length_word_index] = sp->setv_count;
2429 * Relocate vector to final address.
2431 for (i = 0; i < sp->setv_count; i++) {
2432 struct nlist *p = (struct nlist *)
2433 set_vectors[1+i+length_word_index];
2435 set_vectors[1+i+length_word_index] = p->n_value;
2436 if (building_shared_object) {
2437 struct relocation_info reloc;
2439 bzero(&reloc, sizeof(reloc));
2440 RELOC_INIT_SEGMENT_RELOC(&reloc);
2441 RELOC_ADDRESS(&reloc) =
2442 (1 + i + length_word_index) *
2445 RELOC_TYPE(&reloc) =
2446 (p->n_type - (N_SETA - N_ABS)) & N_TYPE;
2447 claim_rrs_segment_reloc(NULL, &reloc);
2452 * Reverse the vector.
2454 p = &set_vectors[length_word_index + 1];
2455 q = &set_vectors[length_word_index + sp->setv_count];
2457 unsigned long tmp = *p;
2462 /* Clear terminating entry */
2463 set_vectors[length_word_index + sp->setv_count + 1] = 0;
2468 if (sp->defined && (sp->defined & ~N_EXT) != N_SETV)
2469 sp->value = sp->def_lsp->nzlist.nz_value;
2470 if (sp->so_defined &&
2471 (sp->def_lsp->entry->flags & E_SECONDCLASS))
2472 /* Flag second-hand definitions */
2473 undefined_global_sym_count++;
2474 if (sp->flags & GS_TRACE)
2475 printf("symbol %s assigned to location %#lx\n",
2476 demangle(sp->name), sp->value);
2480 * If not -r'ing, allocate common symbols in the BSS section.
2482 if (building_shared_object && !(link_mode & SYMBOLIC))
2483 /* No common allocation in shared objects */
2486 if ((size = sp->common_size) != 0) {
2490 if (sp->defined != (N_UNDF + N_EXT))
2491 errx(1, "%s: common isn't", demangle(sp->name));
2493 } else if ((size = sp->size) != 0 && sp->defined == N_SIZE) {
2495 * It's data from shared object with size info.
2497 if (!sp->so_defined)
2498 errx(1, "%s: Bogus N_SIZE item", demangle(sp->name));
2507 if (relocatable_output && !force_common_definition) {
2509 undefined_global_sym_count++;
2510 defined_global_sym_count--;
2515 * Round up to nearest sizeof (int). I don't know whether
2516 * this is necessary or not (given that alignment is taken
2517 * care of later), but it's traditional, so I'll leave it in.
2518 * Note that if this size alignment is ever removed, ALIGN
2519 * above will have to be initialized to 1 instead of sizeof
2523 size = PALIGN(size, sizeof(int));
2525 while (align < MAX_ALIGNMENT && !(size & align))
2528 bss_size = PALIGN(bss_size + data_size + rrs_data_start, align)
2529 - (data_size + rrs_data_start);
2531 sp->value = rrs_data_start + data_size + bss_size;
2532 if (sp->defined == (N_UNDF | N_EXT))
2533 sp->defined = N_BSS | N_EXT;
2536 defined_global_sym_count++;
2540 printf("Allocating %s %s: %x at %lx\n",
2541 sp->defined==(N_BSS|N_EXT)?"common":"data",
2542 demangle(sp->name), size, sp->value);
2548 /* -------------------------------------------------------------------*/
2550 /* Write the output file */
2554 struct stat statbuf;
2558 if (lstat(output_filename, &statbuf) == 0) {
2559 if (S_ISREG(statbuf.st_mode))
2560 (void)unlink(output_filename);
2564 (void)umask(u_mask);
2566 outstream = fopen(output_filename, "w");
2567 if (outstream == NULL)
2568 err(1, "fopen: %s", output_filename);
2570 if (atexit(cleanup))
2573 if (fstat(fileno(outstream), &statbuf) < 0)
2574 err(1, "fstat: %s", output_filename);
2576 filemode = statbuf.st_mode;
2578 if (S_ISREG(statbuf.st_mode) &&
2579 chmod(output_filename, filemode & ~0111) == -1)
2580 err(1, "chmod: %s", output_filename);
2582 /* Output the a.out header. */
2585 /* Output the text and data segments, relocating as we go. */
2589 /* Output the merged relocation info, if requested with `-r'. */
2590 if (relocatable_output)
2593 /* Output the symbol table (both globals and locals). */
2596 /* Output the RSS section */
2599 if (chmod (output_filename, filemode | (0111 & ~u_mask)) == -1)
2600 err(1, "chmod: %s", output_filename);
2603 /* Report I/O error such as disk full. */
2604 if (ferror(outstream) || fclose(outstream) != 0)
2605 err(1, "write_output: %s", output_filename);
2607 if (real_output_filename)
2608 if (rename(output_filename, real_output_filename))
2609 err(1, "rename output: %s to %s",
2610 output_filename, real_output_filename);
2613 /* Total number of symbols to be written in the output file. */
2621 if (link_mode & SHAREABLE)
2622 /* Output is shared object */
2623 flags = EX_DYNAMIC | EX_PIC;
2624 else if (relocatable_output && pic_code_seen)
2625 /* Output is relocatable and contains PIC code */
2627 else if (rrs_section_type == RRS_FULL)
2628 /* Output is a dynamic executable */
2632 * Output is a static executable
2633 * or a non-PIC relocatable object
2637 if (oldmagic && (flags & EX_DPMASK) && !(link_mode & FORCEDYNAMIC))
2638 warnx("Cannot set flag in old magic headers\n");
2640 N_SET_FLAG (outheader, flags);
2642 outheader.a_text = text_size;
2643 outheader.a_data = data_size;
2644 outheader.a_bss = bss_size;
2645 outheader.a_entry = (entry_symbol ? entry_symbol->value
2646 : text_start + entry_offset);
2648 if (strip_symbols == STRIP_ALL)
2651 nsyms = global_sym_count + local_sym_count + debugger_sym_count;
2653 if (relocatable_output)
2654 nsyms += set_symbol_count;
2656 outheader.a_syms = nsyms * sizeof (struct nlist);
2658 if (relocatable_output) {
2659 outheader.a_trsize = text_reloc_size;
2660 outheader.a_drsize = data_reloc_size;
2662 outheader.a_trsize = 0;
2663 outheader.a_drsize = 0;
2666 md_swapout_exec_hdr(&outheader);
2667 mywrite(&outheader, 1, sizeof (struct exec), outstream);
2668 md_swapin_exec_hdr(&outheader);
2671 * Output whatever padding is required in the executable file
2672 * between the header and the start of the text.
2675 #ifndef COFF_ENCAPSULATE
2676 padfile(N_TXTOFF(outheader) - sizeof outheader, outstream);
2681 * Relocate the text segment of each input file
2682 * and write to the output file.
2689 fprintf(stderr, "Copying and relocating text:\n\n");
2691 each_full_file(copy_text, 0);
2695 fprintf(stderr, "\n");
2697 padfile(text_pad, outstream);
2701 * Read the text segment contents of ENTRY, relocate them, and write the
2702 * result to the output file. If `-r', save the text relocation for later
2707 struct file_entry *entry;
2709 register char *bytes;
2713 prline_file_name(entry, stderr);
2715 fd = file_open(entry);
2717 /* Allocate space for the file's text section */
2718 bytes = (char *)alloca(entry->header.a_text);
2720 /* Deal with relocation information however is appropriate */
2721 if (entry->textrel == NULL)
2722 errx(1, "%s: no text relocation", get_file_name(entry));
2724 /* Read the text section into core. */
2725 if (lseek(fd, text_offset(entry), L_SET) == (off_t)-1)
2726 err(1, "%s: copy_text: lseek", get_file_name(entry));
2727 if (entry->header.a_text != read(fd, bytes, entry->header.a_text))
2728 errx(1, "%s: copy_text: premature EOF", get_file_name(entry));
2730 /* Relocate the text according to the text relocation. */
2731 perform_relocation (bytes, entry->header.a_text,
2732 entry->textrel, entry->ntextrel, entry, 0);
2734 /* Write the relocated text to the output file. */
2735 mywrite(bytes, entry->header.a_text, 1, outstream);
2739 * Relocate the data segment of each input file
2740 * and write to the output file.
2749 fprintf(stderr, "Copying and relocating data:\n\n");
2751 pos = N_DATOFF(outheader) + data_start - rrs_data_start;
2752 if (fseek(outstream, pos, SEEK_SET) != 0)
2753 errx(1, "write_data: fseek");
2755 each_full_file(copy_data, 0);
2759 * Write out the set element vectors. See digest symbols for
2760 * description of length of the set vector section.
2763 if (set_vector_count) {
2764 swap_longs(set_vectors, set_symbol_count + 2*set_vector_count);
2765 mywrite(set_vectors, set_symbol_count + 2*set_vector_count,
2766 sizeof (unsigned long), outstream);
2770 fprintf(stderr, "\n");
2772 padfile(data_pad, outstream);
2776 * Read the data segment contents of ENTRY, relocate them, and write the
2777 * result to the output file. If `-r', save the data relocation for later
2778 * reuse. See comments in `copy_text'.
2782 struct file_entry *entry;
2784 register char *bytes;
2788 prline_file_name (entry, stderr);
2790 fd = file_open(entry);
2792 bytes = (char *)alloca(entry->header.a_data);
2794 if (entry->datarel == NULL)
2795 errx(1, "%s: no data relocation", get_file_name(entry));
2797 if (lseek(fd, text_offset(entry) + entry->header.a_text, L_SET) ==
2799 err(1, "%s: copy_data: lseek", get_file_name(entry));
2800 if (entry->header.a_data != read(fd, bytes, entry->header.a_data))
2801 errx(1, "%s: copy_data: premature EOF", get_file_name(entry));
2803 perform_relocation(bytes, entry->header.a_data,
2804 entry->datarel, entry->ndatarel, entry, 1);
2806 mywrite(bytes, entry->header.a_data, 1, outstream);
2810 * Relocate ENTRY's text or data section contents. DATA is the address of the
2811 * contents, in core. DATA_SIZE is the length of the contents. PC_RELOCATION
2812 * is the difference between the address of the contents in the output file
2813 * and its address in the input file. RELOC is the address of the
2814 * relocation info, in core. NRELOC says how many there are.
2820 perform_relocation(data, data_size, reloc, nreloc, entry, dataseg)
2823 struct relocation_info *reloc;
2825 struct file_entry *entry;
2829 register struct relocation_info *r = reloc;
2830 struct relocation_info *end = reloc + nreloc;
2832 int text_relocation = entry->text_start_address;
2833 int data_relocation = entry->data_start_address - entry->header.a_text;
2834 int bss_relocation = entry->bss_start_address -
2835 entry->header.a_text - entry->header.a_data;
2836 pc_relocation = dataseg
2837 ? entry->data_start_address - entry->header.a_text
2838 : entry->text_start_address;
2840 for (; r < end; r++) {
2841 int addr = RELOC_ADDRESS(r);
2842 long addend = md_get_addend(r, data+addr);
2846 * Loop over the relocations again as we did in
2847 * consider_relocation(), claiming the reserved RRS
2851 if (addr >= data_size)
2852 errx(1, "%s: relocation address out of range",
2853 get_file_name(entry));
2855 if (RELOC_JMPTAB_P(r)) {
2857 int symindex = RELOC_SYMBOL(r);
2858 struct localsymbol *lsp = &entry->symbols[symindex];
2861 if (symindex >= entry->nsymbols)
2862 errx(1, "%s: relocation symbolnum out of range",
2863 get_file_name(entry));
2867 errx(1, "%s: bogus relocation record",
2868 get_file_name(entry));
2872 if (relocatable_output)
2873 relocation = addend;
2874 else if (!RELOC_EXTERN_P(r)) {
2875 relocation = addend +
2876 data_relocation - text_relocation;
2878 relocation = addend +
2879 claim_rrs_jmpslot(entry, r, sp, addend);
2881 } else if (RELOC_BASEREL_P(r)) {
2883 int symindex = RELOC_SYMBOL(r);
2884 struct localsymbol *lsp = &entry->symbols[symindex];
2886 if (symindex >= entry->nsymbols)
2887 errx(1, "%s: relocation symbolnum out of range",
2888 get_file_name(entry));
2890 if (relocatable_output)
2891 relocation = addend;
2892 else if (!RELOC_EXTERN_P(r))
2893 relocation = claim_rrs_internal_gotslot(
2894 entry, r, lsp, addend);
2896 relocation = claim_rrs_gotslot(
2897 entry, r, lsp, addend);
2899 } else if (RELOC_EXTERN_P(r)) {
2901 int symindex = RELOC_SYMBOL(r);
2904 if (symindex >= entry->nsymbols)
2905 errx(1, "%s: relocation symbolnum out of range",
2906 get_file_name(entry));
2908 sp = entry->symbols[symindex].symbol;
2910 errx(1, "%s: bogus relocation record",
2911 get_file_name(entry));
2915 if (relocatable_output) {
2916 relocation = addend;
2918 * In PIC code, we keep the reference to the
2919 * external symbol, even if defined now.
2922 relocation += sp->value;
2923 } else if (sp->defined) {
2924 if (sp->flags & GS_TRACE) {
2926 "symbol %s defined as %x in %s\n",
2927 demangle(sp->name), sp->defined,
2928 get_file_name(entry) );
2930 if (sp == got_symbol) {
2931 /* Handle _GOT_ refs */
2932 relocation = addend + sp->value
2934 } else if (building_shared_object) {
2936 * Normal (non-PIC) relocation needs
2937 * to be converted into an RRS reloc
2938 * when building a shared object.
2940 r->r_address += dataseg?
2941 entry->data_start_address:
2942 entry->text_start_address;
2943 relocation = addend;
2944 if (claim_rrs_reloc(
2945 entry, r, sp, &relocation))
2947 } else if (sp->defined == N_SIZE) {
2949 * If size is known, arrange a
2953 errx(1, "Copy item isn't: %s",
2954 demangle(sp->name));
2956 relocation = addend + sp->value;
2957 r->r_address = sp->value;
2958 claim_rrs_cpy_reloc(entry, r, sp);
2960 /* Plain old relocation */
2961 relocation = addend + sp->value;
2964 * If the symbol is undefined, we relocate it
2965 * in a way similar to -r case. We use an
2966 * RRS relocation to resolve the symbol at
2967 * run-time. The r_address field is updated
2968 * to reflect the changed position in the
2971 if (sp->flags & GS_TRACE) {
2973 "symbol %s claims RRS in %s%s\n",
2974 demangle(sp->name), get_file_name(entry),
2975 (sp->so_defined == (N_TEXT+N_EXT) &&
2976 sp->flags & GS_HASJMPSLOT)?
2979 if (sp->so_defined == (N_TEXT+N_EXT) &&
2980 sp->flags & GS_HASJMPSLOT) {
2982 * Claim a jmpslot if one was allocated.
2984 * At this point, a jmpslot can only
2985 * result from a shared object reference
2986 * while `force_alias' is in effect.
2988 relocation = addend +
2990 entry, r, sp, addend);
2992 r->r_address += dataseg?
2993 entry->data_start_address:
2994 entry->text_start_address;
2995 relocation = addend;
2996 if ((building_shared_object ||
2998 claim_rrs_reloc(entry, r, sp,
3006 switch (RELOC_TYPE(r)) {
3008 case N_TEXT | N_EXT:
3009 relocation = addend + text_relocation;
3013 case N_DATA | N_EXT:
3015 * A word that points to beginning of the the
3016 * data section initially contains not 0 but
3017 * rather the "address" of that section in
3018 * the input file, which is the length of the
3021 relocation = addend + data_relocation;
3027 * Similarly, an input word pointing to the
3028 * beginning of the bss initially contains
3029 * the length of text plus data of the file.
3031 relocation = addend + bss_relocation;
3037 * Don't know why this code would occur, but
3038 * apparently it does.
3043 errx(1, "%s: nonexternal relocation invalid",
3044 get_file_name(entry));
3048 * When building a shared object, these segment
3049 * relocations need a "load address relative"
3052 if (building_shared_object && !RELOC_PCREL_P(r)) {
3053 r->r_address += dataseg?
3054 entry->data_start_address:
3055 entry->text_start_address;
3056 claim_rrs_segment_reloc(entry, r);
3060 if (RELOC_PCREL_P(r))
3061 relocation -= pc_relocation;
3063 md_relocate(r, relocation, data+addr, relocatable_output);
3070 * For relocatable_output only: write out the relocation,
3071 * relocating the addresses-to-be-relocated.
3079 fprintf(stderr, "Writing text relocation:\n\n");
3082 * Assign each global symbol a sequence number, giving the order
3083 * in which `write_syms' will write it.
3084 * This is so we can store the proper symbolnum fields
3085 * in relocation entries we write.
3088 /* BLECH - Assign number 0 to __DYNAMIC (!! Sun compatibility) */
3090 if (dynamic_symbol->flags & GS_REFERENCED)
3091 dynamic_symbol->symbolnum = count++;
3092 FOR_EACH_SYMBOL(i, sp) {
3093 if (sp == dynamic_symbol)
3097 if (!(sp->flags & GS_REFERENCED))
3099 sp->symbolnum = count++;
3106 if (count != global_sym_count)
3107 errx(1, "internal error: write_rel: count = %d", count);
3109 each_full_file(assign_symbolnums, &count);
3111 /* Write out the relocations of all files, remembered from copy_text. */
3112 each_full_file(coptxtrel, 0);
3115 fprintf(stderr, "\nWriting data relocation:\n\n");
3117 each_full_file(copdatrel, 0);
3120 fprintf(stderr, "\n");
3125 * Assign symbol ordinal numbers to local symbols in each entry.
3128 assign_symbolnums(entry, countp)
3129 struct file_entry *entry;
3132 struct localsymbol *lsp, *lspend;
3135 lspend = entry->symbols + entry->nsymbols;
3137 if (discard_locals != DISCARD_ALL)
3138 /* Count the N_FN symbol for this entry */
3141 for (lsp = entry->symbols; lsp < lspend; lsp++) {
3142 if (lsp->flags & LS_WRITE)
3143 lsp->symbolnum = n++;
3150 struct file_entry *entry;
3152 register struct relocation_info *r, *end;
3153 register int reloc = entry->text_start_address;
3156 end = r + entry->ntextrel;
3158 for (; r < end; r++) {
3159 register int symindex;
3160 struct localsymbol *lsp;
3163 RELOC_ADDRESS(r) += reloc;
3165 symindex = RELOC_SYMBOL(r);
3166 lsp = &entry->symbols[symindex];
3168 if (!RELOC_EXTERN_P(r)) {
3171 if (RELOC_BASEREL_P(r))
3172 RELOC_SYMBOL(r) = lsp->symbolnum;
3176 if (symindex >= entry->nsymbols)
3177 errx(1, "%s: relocation symbolnum out of range",
3178 get_file_name(entry));
3183 /* Resolve indirection. */
3184 if ((sp->defined & ~N_EXT) == N_INDR) {
3185 if (sp->alias == NULL)
3186 errx(1, "internal error: alias in hyperspace");
3192 * If the symbol is now defined, change the external
3193 * relocation to an internal one.
3197 if (!pic_code_seen) {
3198 RELOC_EXTERN_P(r) = 0;
3199 RELOC_SYMBOL(r) = (sp->defined & N_TYPE);
3201 RELOC_SYMBOL(r) = sp->symbolnum;
3204 * Global symbols come first.
3206 RELOC_SYMBOL(r) = sp->symbolnum;
3208 md_swapout_reloc(entry->textrel, entry->ntextrel);
3209 mywrite(entry->textrel, entry->ntextrel,
3210 sizeof(struct relocation_info), outstream);
3215 struct file_entry *entry;
3217 register struct relocation_info *r, *end;
3219 * Relocate the address of the relocation. Old address is relative to
3220 * start of the input file's data section. New address is relative to
3221 * start of the output file's data section.
3223 register int reloc = entry->data_start_address - text_size;
3226 end = r + entry->ndatarel;
3228 for (; r < end; r++) {
3229 register int symindex;
3233 RELOC_ADDRESS(r) += reloc;
3235 if (!RELOC_EXTERN_P(r)) {
3236 if (RELOC_BASEREL_P(r))
3237 errx(1, "%s: Unsupported relocation type",
3238 get_file_name(entry));
3242 symindex = RELOC_SYMBOL(r);
3243 sp = entry->symbols[symindex].symbol;
3245 if (symindex >= entry->header.a_syms)
3246 errx(1, "%s: relocation symbolnum out of range",
3247 get_file_name(entry));
3250 /* Resolve indirection. */
3251 if ((sp->defined & ~N_EXT) == N_INDR) {
3252 if (sp->alias == NULL)
3253 errx(1, "internal error: alias in hyperspace");
3258 symtype = sp->defined & N_TYPE;
3260 if (!pic_code_seen && ( symtype == N_BSS ||
3261 symtype == N_DATA ||
3262 symtype == N_TEXT ||
3263 symtype == N_ABS)) {
3264 RELOC_EXTERN_P(r) = 0;
3265 RELOC_SYMBOL(r) = symtype;
3268 * Global symbols come first.
3271 entry->symbols[symindex].symbol->symbolnum;
3273 md_swapout_reloc(entry->datarel, entry->ndatarel);
3274 mywrite(entry->datarel, entry->ndatarel,
3275 sizeof(struct relocation_info), outstream);
3278 void write_file_syms __P((struct file_entry *, int *));
3279 void write_string_table __P((void));
3281 /* Offsets and current lengths of symbol and string tables in output file. */
3283 static int symtab_offset;
3284 static int symtab_len;
3286 /* Address in output file where string table starts. */
3287 static int strtab_offset;
3289 /* Offset within string table
3290 where the strings in `strtab_vector' should be written. */
3291 static int strtab_len;
3293 /* Total size of string table strings allocated so far,
3294 including strings in `strtab_vector'. */
3295 static int strtab_size;
3297 /* Vector whose elements are strings to be added to the string table. */
3298 static char **strtab_vector;
3300 /* Vector whose elements are the lengths of those strings. */
3301 static int *strtab_lens;
3303 /* Index in `strtab_vector' at which the next string will be stored. */
3304 static int strtab_index;
3307 * Add the string NAME to the output file string table. Record it in
3308 * `strtab_vector' to be output later. Return the index within the string
3309 * table that this string will have.
3313 assign_string_table_index(name)
3316 register int index = strtab_size;
3317 register int len = strlen(name) + 1;
3320 strtab_vector[strtab_index] = name;
3321 strtab_lens[strtab_index++] = len;
3327 * Write the contents of `strtab_vector' into the string table. This is done
3328 * once for each file's local&debugger symbols and once for the global
3332 write_string_table()
3336 if (fseek(outstream, strtab_offset + strtab_len, SEEK_SET) != 0)
3337 err(1, "write_string_table: %s: fseek", output_filename);
3339 for (i = 0; i < strtab_index; i++) {
3340 mywrite(strtab_vector[i], strtab_lens[i], 1, outstream);
3341 strtab_len += strtab_lens[i];
3345 /* Write the symbol table and string table of the output file. */
3350 /* Number of symbols written so far. */
3351 int syms_written = 0;
3355 * Buffer big enough for all the global symbols. One extra struct
3356 * for each indirect symbol to hold the extra reference following.
3358 struct nlist *buf = (struct nlist *)
3359 alloca(global_sym_count * sizeof(struct nlist));
3360 /* Pointer for storing into BUF. */
3361 register struct nlist *bufp = buf;
3363 /* Size of string table includes the bytes that store the size. */
3364 strtab_size = sizeof strtab_size;
3366 symtab_offset = N_SYMOFF(outheader);
3368 strtab_offset = N_STROFF(outheader);
3369 strtab_len = strtab_size;
3371 if (strip_symbols == STRIP_ALL)
3374 /* First, write out the global symbols. */
3377 * Allocate two vectors that record the data to generate the string
3378 * table from the global symbols written so far. This must include
3379 * extra space for the references following indirect outputs.
3382 strtab_vector = (char **)alloca((global_sym_count) * sizeof(char *));
3383 strtab_lens = (int *)alloca((global_sym_count) * sizeof(int));
3387 * __DYNAMIC symbol *must* be first for Sun compatibility, as Sun's
3388 * ld.so reads the shared object's first symbol. This means that
3389 * (Sun's) shared libraries cannot be stripped! (We only assume
3390 * that __DYNAMIC is the first item in the data segment)
3392 * If defined (ie. not relocatable_output), make it look
3393 * like an internal symbol.
3395 if (dynamic_symbol->flags & GS_REFERENCED) {
3398 nl.n_type = dynamic_symbol->defined;
3399 if (nl.n_type == N_UNDF)
3402 nl.n_type &= ~N_EXT;
3403 nl.n_value = dynamic_symbol->value;
3404 nl.n_un.n_strx = assign_string_table_index(dynamic_symbol->name);
3409 /* Scan the symbol hash table, bucket by bucket. */
3411 FOR_EACH_SYMBOL(i, sp) {
3413 if (sp == dynamic_symbol)
3414 /* Already dealt with above */
3418 * Propagate N_WARNING symbols.
3420 if ((relocatable_output || building_shared_object)
3422 nl.n_type = N_WARNING;
3423 nl.n_un.n_strx = assign_string_table_index(sp->warning);
3430 nl.n_type = N_UNDF + N_EXT;
3431 nl.n_un.n_strx = assign_string_table_index(sp->name);
3439 if (!(sp->flags & GS_REFERENCED))
3440 /* Came from shared object but was not used */
3443 if (sp->so_defined || (sp->alias && sp->alias->so_defined))
3445 * Definition came from shared object,
3446 * don't mention it here
3450 if (!sp->defined && !relocatable_output) {
3452 * We're building a shared object and there
3453 * are still undefined symbols. Don't output
3454 * these, symbol was discounted in digest_pass1()
3455 * (they are in the RRS symbol table).
3457 if (building_shared_object)
3459 if (!(sp->flags & GS_WEAK))
3460 warnx("symbol %s remains undefined", demangle(sp->name));
3463 if (syms_written >= global_sym_count)
3465 "internal error: number of symbols exceeds alloc'd %d",
3469 * Construct a `struct nlist' for the symbol.
3474 if (sp->defined > 1) {
3476 * defined with known type
3478 if (!relocatable_output && !building_shared_object &&
3479 sp->alias && sp->alias->defined > 1) {
3481 * If the target of an indirect symbol has
3482 * been defined and we are outputting an
3483 * executable, resolve the indirection; it's
3486 nl.n_type = sp->alias->defined;
3487 nl.n_value = sp->alias->value;
3488 nl.n_other = N_OTHER(0, sp->alias->aux);
3492 if (sp->defined == N_SIZE)
3493 nl.n_type = N_DATA | N_EXT;
3495 nl.n_type = sp->defined;
3496 if (nl.n_type == (N_INDR|N_EXT) &&
3498 errx(1, "%s: N_INDR has value %#lx",
3499 demangle(sp->name), sp->value);
3500 nl.n_value = sp->value;
3502 bind = N_BIND(&sp->def_lsp->nzlist.nlist);
3503 nl.n_other = N_OTHER(bind, sp->aux);
3506 } else if (sp->common_size) {
3508 * defined as common but not allocated,
3509 * happens only with -r and not -d, write out
3510 * a common definition.
3512 * common condition needs to be before undefined
3513 * condition because unallocated commons are set
3514 * undefined in digest_symbols.
3516 nl.n_type = N_UNDF | N_EXT;
3517 nl.n_value = sp->common_size;
3518 } else if (!sp->defined) {
3519 /* undefined -- legit only if -r */
3520 nl.n_type = N_UNDF | N_EXT;
3524 "internal error: %s defined in mysterious way",
3525 demangle(sp->name));
3528 * Allocate string table space for the symbol name.
3531 nl.n_un.n_strx = assign_string_table_index(sp->name);
3533 /* Output to the buffer and count it. */
3539 * Write second symbol of an alias pair.
3541 if (nl.n_type == N_INDR + N_EXT) {
3542 if (sp->alias == NULL)
3543 errx(1, "internal error: alias in hyperspace");
3544 nl.n_type = N_UNDF + N_EXT;
3546 assign_string_table_index(sp->alias->name);
3555 * Write N_SIZE symbol for a symbol with a known size.
3557 if (relocatable_output && sp->size) {
3558 nl.n_type = N_SIZE + N_EXT;
3559 nl.n_un.n_strx = assign_string_table_index(sp->name);
3560 nl.n_value = sp->size;
3568 printf("writesym(#%d): %s, type %x\n", syms_written, demangle(sp->name), sp->defined);
3572 if (syms_written != strtab_index || strtab_index != global_sym_count)
3573 errx(1, "internal error: wrong number (%d) of global symbols "
3574 "written into output file, should be %d",
3575 syms_written, global_sym_count);
3577 /* Output the buffer full of `struct nlist's. */
3579 if (fseek(outstream, symtab_offset + symtab_len, SEEK_SET) != 0)
3580 err(1, "write_syms: fseek");
3581 md_swapout_symbols(buf, bufp - buf);
3582 mywrite(buf, bufp - buf, sizeof(struct nlist), outstream);
3583 symtab_len += sizeof(struct nlist) * (bufp - buf);
3585 /* Write the strings for the global symbols. */
3586 write_string_table();
3588 /* Write the local symbols defined by the various files. */
3589 each_file(write_file_syms, (void *)&syms_written);
3592 if (syms_written != nsyms)
3593 errx(1, "internal error: wrong number of symbols (%d) "
3594 "written into output file, should be %d",
3595 syms_written, nsyms);
3597 if (symtab_offset + symtab_len != strtab_offset)
3599 "internal error: inconsistent symbol table length: %d vs %d",
3600 symtab_offset + symtab_len, strtab_offset);
3602 if (fseek(outstream, strtab_offset, SEEK_SET) != 0)
3603 err(1, "write_syms: fseek");
3604 strtab_size = md_swap_long(strtab_size);
3605 mywrite(&strtab_size, sizeof(int), 1, outstream);
3610 * Write the local and debugger symbols of file ENTRY. Increment
3611 * *SYMS_WRITTEN_ADDR for each symbol that is written.
3615 * Note that we do not combine identical names of local symbols. dbx or gdb
3616 * would be confused if we did that.
3619 write_file_syms(entry, syms_written_addr)
3620 struct file_entry *entry;
3621 int *syms_written_addr;
3623 struct localsymbol *lsp, *lspend;
3625 /* Upper bound on number of syms to be written here. */
3626 int max_syms = entry->nsymbols + 1;
3629 * Buffer to accumulate all the syms before writing them. It has one
3630 * extra slot for the local symbol we generate here.
3632 struct nlist *buf = (struct nlist *)
3633 alloca(max_syms * sizeof(struct nlist));
3635 register struct nlist *bufp = buf;
3637 if (entry->flags & E_DYNAMIC)
3641 * Make tables that record, for each symbol, its name and its name's
3642 * length. The elements are filled in by `assign_string_table_index'.
3645 strtab_vector = (char **)alloca(max_syms * sizeof(char *));
3646 strtab_lens = (int *)alloca(max_syms * sizeof(int));
3649 /* Generate a local symbol for the start of this file's text. */
3651 if (discard_locals != DISCARD_ALL) {
3654 nl.n_type = N_FN | N_EXT;
3656 assign_string_table_index(entry->local_sym_name);
3657 nl.n_value = entry->text_start_address;
3661 (*syms_written_addr)++;
3663 /* Read the file's string table. */
3665 entry->strings = (char *)alloca(entry->string_size);
3666 read_entry_strings(file_open(entry), entry);
3668 lspend = entry->symbols + entry->nsymbols;
3670 for (lsp = entry->symbols; lsp < lspend; lsp++) {
3671 register struct nlist *p = &lsp->nzlist.nlist;
3674 if (!(lsp->flags & LS_WRITE))
3677 if (discard_locals == DISCARD_ALL ||
3678 (discard_locals == DISCARD_L &&
3679 (lsp->flags & LS_L_SYMBOL))) {
3681 * The user wants to discard this symbol, but it
3682 * is referenced by a relocation. We can still
3683 * save some file space by suppressing the unique
3684 * renaming of the symbol.
3686 lsp->flags &= ~LS_RENAME;
3689 if (p->n_un.n_strx == 0)
3691 else if (!(lsp->flags & LS_RENAME))
3692 name = p->n_un.n_strx + entry->strings;
3694 char *cp = p->n_un.n_strx + entry->strings;
3695 name = (char *)alloca(
3696 strlen(entry->local_sym_name) +
3698 (void)sprintf(name, "%s.%s", entry->local_sym_name, cp);
3702 * If this symbol has a name, allocate space for it
3703 * in the output string table.
3707 p->n_un.n_strx = assign_string_table_index(name);
3709 /* Output this symbol to the buffer and count it. */
3712 (*syms_written_addr)++;
3715 /* All the symbols are now in BUF; write them. */
3717 if (fseek(outstream, symtab_offset + symtab_len, SEEK_SET) != 0)
3718 err(1, "write local symbols: fseek");
3719 md_swapout_symbols(buf, bufp - buf);
3720 mywrite(buf, bufp - buf, sizeof(struct nlist), outstream);
3721 symtab_len += sizeof(struct nlist) * (bufp - buf);
3724 * Write the string-table data for the symbols just written, using
3725 * the data in vectors `strtab_vector' and `strtab_lens'.
3728 write_string_table();
3729 entry->strings = 0; /* Since it will disappear anyway. */
3733 * Parse the string ARG using scanf format FORMAT, and return the result.
3734 * If it does not parse, report fatal error
3735 * generating the error message using format string ERROR and ARG as arg.
3739 parse(arg, format, error)
3740 char *arg, *format, *error;
3744 if (1 != sscanf(arg, format, &x))
3745 errx(1, error, arg);
3750 * Output COUNT*ELTSIZE bytes of data at BUF to the descriptor FD.
3753 mywrite(buf, count, eltsize, fd)
3760 if (fwrite(buf, eltsize, count, fd) != count)
3767 struct stat statbuf;
3772 if (fstat(fileno(outstream), &statbuf) == 0) {
3773 if (S_ISREG(statbuf.st_mode))
3774 (void)unlink(output_filename);
3779 * Output PADDING zero-bytes to descriptor FD.
3780 * PADDING may be negative; in that case, do nothing.
3783 padfile(padding, fd)
3791 buf = (char *)alloca(padding);
3792 bzero(buf, padding);
3793 mywrite(buf, padding, 1, fd);
3802 for (i = 0; i < number_of_files; i++) {
3803 register struct file_entry *entry = &file_table[i];
3806 if (entry->flags & E_SEARCH_DIRS)
3807 fd = findlib(entry);
3809 fd = open(entry->filename, O_RDONLY, 0);
3816 * Print the name even if the file doesn't exist except in
3817 * the -lfoo case. This allows `ld -f' to work as well as
3818 * possible when it is used to generate dependencies before
3819 * the libraries exist.
3821 if (fd >= 0 || !(entry->flags & E_SEARCH_DIRS))
3822 printf("%s\n", entry->filename);