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
9 * @(#)ld.c 6.10 (Berkeley) 5/22/91
12 /* Linker `ld' for GNU
13 Copyright (C) 1988 Free Software Foundation, Inc.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 1, or (at your option)
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 /* Written by Richard Stallman with some help from Eric Albert.
30 Set, indirect, and warning symbol features added by Randy Smith. */
33 * $FreeBSD: src/gnu/usr.bin/ld/ld.c,v 1.47 1999/08/27 23:36:00 peter Exp $
34 * $DragonFly: src/gnu/usr.bin/ld/Attic/ld.c,v 1.2 2003/06/17 04:25:46 dillon Exp $
37 /* Define how to initialize system-dependent header fields. */
39 #include <sys/param.h>
40 #include <sys/types.h>
44 #include <sys/resource.h>
60 /* Vector of entries for input files specified by arguments.
61 These are all the input files except for members of specified libraries. */
62 struct file_entry *file_table;
65 /* 1 => write relocation into output file so can re-input it later. */
66 int relocatable_output;
68 /* 1 => building a shared object, set by `-Bshareable'. */
69 int building_shared_object;
71 /* 1 => create the output executable. */
74 /* Force the executable to be output, even if there are non-fatal errors */
77 /* 1 => assign space to common symbols even if `relocatable_output'. */
78 int force_common_definition;
80 /* 1 => assign jmp slots to text symbols in shared objects even if non-PIC */
81 int force_alias_definition;
83 /* 1 => some files contain PIC code, affects relocation bits
84 if `relocatable_output'. */
87 /* 1 => segments must be page aligned (ZMAGIC, QMAGIC) */
88 int page_align_segments;
90 /* 1 => data segment must be page aligned, even if `-n' or `-N' */
93 /* 1 => do not use standard library search path */
96 /* Version number to put in __DYNAMIC (set by -V) */
99 int text_size; /* total size of text. */
100 int text_start; /* start of text */
101 int text_pad; /* clear space between text and data */
102 int data_size; /* total size of data. */
103 int data_start; /* start of data */
104 int data_pad; /* part of bss segment as part of data */
106 int bss_size; /* total size of bss. */
107 int bss_start; /* start of bss */
109 int text_reloc_size; /* total size of text relocation. */
110 int data_reloc_size; /* total size of data relocation. */
112 int rrs_section_type; /* What's in the RRS section */
113 int rrs_text_size; /* Size of RRS text additions */
114 int rrs_text_start; /* Location of above */
115 int rrs_data_size; /* Size of RRS data additions */
116 int rrs_data_start; /* Location of above */
118 /* Specifications of start and length of the area reserved at the end
119 of the data segment for the set vectors. Computed in 'digest_symbols' */
120 int set_sect_start; /* start of set element vectors */
121 int set_sect_size; /* size of above */
123 int link_mode; /* Current link mode */
124 int pic_type; /* PIC type */
127 * When loading the text and data, we can avoid doing a close
128 * and another open between members of the same library.
130 * These two variables remember the file that is currently open.
131 * Both are zero if no file is open.
133 * See `each_file' and `file_close'.
135 struct file_entry *input_file;
138 /* The name of the file to write; "a.out" by default. */
139 char *output_filename; /* Output file name. */
140 char *real_output_filename; /* Output file name. */
141 FILE *outstream; /* Output file descriptor. */
142 struct exec outheader; /* Output file header. */
143 int magic; /* Output file magic. */
145 int relocatable_output; /* `-r'-ed output */
147 symbol *entry_symbol; /* specified by `-e' */
148 int entry_offset; /* program entry if no `-e' given */
150 int page_size; /* Size of a page (machine dependent) */
153 * Keep a list of any symbols referenced from the command line (so
154 * that error messages for these guys can be generated). This list is
157 symbol **cmdline_references;
158 int cl_refs_allocated;
161 * Which symbols should be stripped (omitted from the output): none, all, or
165 STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER
169 * Which local symbols should be omitted: none, all, or those starting with L.
170 * This is irrelevant if STRIP_NONE.
173 DISCARD_NONE, DISCARD_ALL, DISCARD_L
176 int global_sym_count; /* # of nlist entries for global symbols */
177 int size_sym_count; /* # of N_SIZE nlist entries for output
178 (relocatable_output only) */
179 int local_sym_count; /* # of nlist entries for local symbols. */
180 int non_L_local_sym_count; /* # of nlist entries for non-L symbols */
181 int debugger_sym_count; /* # of nlist entries for debugger info. */
182 int undefined_global_sym_count; /* # of global symbols referenced and
184 int undefined_shobj_sym_count; /* # of undefined symbols referenced
186 int multiple_def_count; /* # of multiply defined symbols. */
187 int defined_global_sym_count; /* # of defined global symbols. */
188 int common_defined_global_count; /* # of common symbols. */
189 int undefined_weak_sym_count; /* # of weak symbols referenced and
193 int special_sym_count; /* # of linker defined symbols. */
194 /* XXX - Currently, only __DYNAMIC and _G_O_T_ go here if required,
195 * perhaps _etext, _edata and _end should go here too.
198 int global_alias_count; /* # of aliased symbols */
199 int set_symbol_count; /* # of N_SET* symbols. */
200 int set_vector_count; /* # of set vectors in output. */
201 int warn_sym_count; /* # of warning symbols encountered. */
202 int flag_list_files; /* 1 => print pathnames of files, don't link */
203 int list_warning_symbols; /* 1 => warning symbols referenced */
205 struct string_list_element *set_element_prefixes;
207 int trace_files; /* print names of input files as processed (`-t'). */
208 int write_map; /* write a load map (`-M') */
211 * `text-start' address is normally this much plus a page boundary.
212 * This is not a user option; it is fixed for each system.
214 int text_start_alignment;
217 * Nonzero if -T was specified in the command line.
218 * This prevents text_start from being set later to default values.
220 int T_flag_specified;
223 * Nonzero if -Tdata was specified in the command line.
224 * This prevents data_start from being set later to default values.
226 int Tdata_flag_specified;
229 * Size to pad data section up to.
230 * We simply increase the size of the data section, padding with zeros,
231 * and reduce the size of the bss section to match.
233 int specified_data_size;
238 static void decode_option __P((char *, char *));
239 static void decode_command __P((int, char **));
240 static int classify_arg __P((char *));
241 static void load_symbols __P((void));
242 static void enter_global_ref __P((struct localsymbol *,
243 char *, struct file_entry *));
244 static void digest_symbols __P((void));
245 static void digest_pass1 __P((void)), digest_pass2 __P((void));
246 static void consider_file_section_lengths __P((struct file_entry *));
247 static void relocate_file_addresses __P((struct file_entry *));
248 static void consider_relocation __P((struct file_entry *, int));
249 static void consider_local_symbols __P((struct file_entry *));
250 static void perform_relocation __P((char *, int,
251 struct relocation_info *, int,
252 struct file_entry *, int));
253 static void copy_text __P((struct file_entry *));
254 static void copy_data __P((struct file_entry *));
255 static void coptxtrel __P((struct file_entry *));
256 static void copdatrel __P((struct file_entry *));
257 static void write_output __P((void));
258 static void write_header __P((void));
259 static void write_text __P((void));
260 static void write_data __P((void));
261 static void write_rel __P((void));
262 static void write_syms __P((void));
263 static void assign_symbolnums __P((struct file_entry *, int *));
264 static void cleanup __P((void));
265 static int parse __P((char *, char *, char *));
266 static void list_files __P((void));
275 /* Added this to stop ld core-dumping on very large .o files. */
277 /* Get rid of any avoidable limit on stack size. */
281 /* Set the stack limit huge so that alloca does not fail. */
282 if (getrlimit(RLIMIT_STACK, &rlim) != 0)
285 rlim.rlim_cur = rlim.rlim_max;
286 if (setrlimit(RLIMIT_STACK, &rlim) != 0)
290 #endif /* RLIMIT_STACK */
294 /* Clear the cumulative info on the output file. */
304 page_align_segments = 0;
307 /* Initialize the data about options. */
309 specified_data_size = 0;
310 strip_symbols = STRIP_NONE;
312 discard_locals = DISCARD_NONE;
315 relocatable_output = 0;
316 force_common_definition = 0;
317 T_flag_specified = 0;
318 Tdata_flag_specified = 0;
319 magic = DEFAULT_MAGIC;
321 force_executable = 0;
324 link_mode |= SILLYARCHIVE;
326 soversion = DEFAULT_SOVERSION;
328 /* Initialize the cumulative counts of symbols. */
331 non_L_local_sym_count = 0;
332 debugger_sym_count = 0;
333 undefined_global_sym_count = 0;
335 list_warning_symbols = 0;
336 multiple_def_count = 0;
337 common_defined_global_count = 0;
339 /* Keep a list of symbols referenced from the command line */
340 cl_refs_allocated = 10;
341 cmdline_references = (symbol **)
342 xmalloc(cl_refs_allocated * sizeof(symbol *));
343 *cmdline_references = 0;
345 /* Completely decode ARGV. */
346 decode_command(argc, argv);
351 building_shared_object =
352 (!relocatable_output && (link_mode & SHAREABLE));
354 if (building_shared_object && entry_symbol) {
355 errx(1,"`-Bshareable' and `-e' options are mutually exclusive");
358 /* Create the symbols `etext', `edata' and `end'. */
359 symtab_init(relocatable_output);
362 * Determine whether to count the header as part of the text size,
363 * and initialize the text size accordingly. This depends on the kind
364 * of system and on the output format selected.
367 if (magic == ZMAGIC || magic == QMAGIC)
368 page_align_segments = 1;
370 md_init_header(&outheader, magic, 0);
372 text_size = sizeof(struct exec);
373 text_size -= N_TXTOFF(outheader);
377 entry_offset = text_size;
379 if (!T_flag_specified && !relocatable_output)
380 text_start = TEXT_START(outheader);
382 /* The text-start address is normally this far past a page boundary. */
383 text_start_alignment = text_start % page_size;
386 * Load symbols of all input files. Also search all libraries and
387 * decide which library members to load.
391 /* Compute where each file's sections go, and relocate symbols. */
395 * Print error messages for any missing symbols, for any warning
396 * symbols, and possibly multiple definitions
398 make_executable &= do_warnings(stderr);
400 /* Print a map, if requested. */
402 print_symbols(stdout);
404 /* Write the output file. */
405 if (make_executable || force_executable)
408 exit(!make_executable);
412 * Analyze a command line argument. Return 0 if the argument is a filename.
413 * Return 1 if the argument is a option complete in itself. Return 2 if the
414 * argument is a option which uses an argument.
416 * Thus, the value is the number of consecutive arguments that are part of
428 if (!strcmp(&arg[2], "ssert"))
446 if (!strcmp(&arg[2], "static"))
448 if (!strcmp(&arg[2], "dynamic"))
450 if (!strcmp(&arg[2], "forcedynamic"))
456 if (!strcmp(&arg[2], "text"))
458 if (!strcmp(&arg[2], "data"))
467 * Process the command arguments, setting up file_table with an entry for
468 * each input file, and setting variables according to the options.
472 decode_command(argc, argv)
477 register struct file_entry *p;
480 output_filename = "a.out";
483 * First compute number_of_files so we know how long to make
485 * Also process most options completely.
488 for (i = 1; i < argc; i++) {
489 register int code = classify_arg(argv[i]);
492 errx(1, "no argument following %s", argv[i]);
494 decode_option(argv[i], argv[i + 1]);
496 if (argv[i][1] == 'l' || argv[i][1] == 'A')
504 if (!number_of_files) {
507 errx(1, "No input files specified");
510 p = file_table = (struct file_entry *)
511 xmalloc(number_of_files * sizeof(struct file_entry));
512 bzero(p, number_of_files * sizeof(struct file_entry));
514 /* Now scan again and fill in file_table. */
515 /* All options except -A and -l are ignored here. */
517 for (i = 1; i < argc; i++) {
519 register int code = classify_arg(argv[i]);
522 p->filename = argv[i];
523 p->local_sym_name = argv[i];
528 string = argv[i + 1];
530 string = &argv[i][2];
532 if (argv[i][1] == 'B') {
533 if (strcmp(string, "static") == 0)
534 link_mode &= ~DYNAMIC;
535 else if (strcmp(string, "dynamic") == 0)
536 link_mode |= DYNAMIC;
537 else if (strcmp(string, "forcedynamic") == 0)
538 link_mode |= DYNAMIC|FORCEDYNAMIC;
539 else if (strcmp(string, "symbolic") == 0)
540 link_mode |= SYMBOLIC;
541 else if (strcmp(string, "forcearchive") == 0)
542 link_mode |= FORCEARCHIVE;
543 else if (strcmp(string, "shareable") == 0)
544 link_mode |= SHAREABLE;
546 else if (strcmp(string, "silly") == 0)
547 link_mode |= SILLYARCHIVE;
548 else if (strcmp(string, "~silly") == 0)
549 link_mode &= ~SILLYARCHIVE;
552 if (!strcmp(argv[i] + 1, "assert")) {
553 if (!strcmp(string, "pure-text"))
554 link_mode |= WARNRRSTEXT;
556 if (argv[i][1] == 'A') {
558 errx(1, "-A specified before an input file other than the first");
559 p->filename = string;
560 p->local_sym_name = string;
561 p->flags |= E_JUST_SYMS;
562 link_mode &= ~DYNAMIC;
565 if (argv[i][1] == 'l') {
566 p->filename = string;
567 p->local_sym_name = concat("-l", string, "");
568 p->flags |= E_SEARCH_DIRS;
569 if (link_mode & DYNAMIC && !relocatable_output)
570 p->flags |= E_SEARCH_DYNAMIC;
576 /* Now check some option settings for consistency. */
578 if (page_align_segments &&
579 (text_start - text_start_alignment) & (page_size - 1))
580 errx(1, "incorrect alignment of text start address");
582 /* Append the standard search directories to the user-specified ones. */
583 add_search_path(getenv("LD_LIBRARY_PATH"));
594 for (ptr = cmdline_references;
595 ptr < cmdline_references + cl_refs_allocated && *ptr;
598 if (ptr >= cmdline_references + cl_refs_allocated - 1) {
599 int diff = ptr - cmdline_references;
601 cl_refs_allocated *= 2;
602 cmdline_references = (symbol **)
603 xrealloc(cmdline_references,
604 cl_refs_allocated * sizeof(symbol *));
605 ptr = cmdline_references + diff;
612 set_element_prefixed_p(name)
615 struct string_list_element *p;
618 for (p = set_element_prefixes; p; p = p->next) {
620 for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++);
621 if (p->str[i] == '\0')
628 * Record an option and arrange to act on it later. ARG should be the
629 * following command argument, which may or may not be used by this option.
631 * The `l' and `A' options are ignored here since they actually specify input
636 decode_option(swt, arg)
637 register char *swt, *arg;
639 if (!strcmp(swt + 1, "Bstatic"))
641 if (!strcmp(swt + 1, "Bdynamic"))
643 if (!strcmp(swt + 1, "Bforcedynamic"))
645 if (!strcmp(swt + 1, "Bsymbolic"))
647 if (!strcmp(swt + 1, "Bforcearchive"))
649 if (!strcmp(swt + 1, "Bshareable"))
651 if (!strcmp(swt + 1, "assert"))
654 if (!strcmp(swt + 1, "Bsilly"))
657 if (!strcmp(swt + 1, "Ttext")) {
658 text_start = parse(arg, "%x", "invalid argument to -Ttext");
659 T_flag_specified = 1;
662 if (!strcmp(swt + 1, "Tdata")) {
663 rrs_data_start = parse(arg, "%x", "invalid argument to -Tdata");
664 Tdata_flag_specified = 1;
667 if (!strcmp(swt + 1, "noinhibit-exec")) {
668 force_executable = 1;
671 if (!strcmp(swt + 1, "nostdlib")) {
683 specified_data_size = parse(arg, "%x", "invalid argument to -D");
687 if (swt[2] == 0 || *arg == 'c')
688 force_common_definition = 1;
689 else if (*arg == 'p')
690 force_alias_definition = 1;
692 errx(1, "-d option takes 'c' or 'p' argument");
696 entry_symbol = getsym(arg);
697 if (!entry_symbol->defined &&
698 !(entry_symbol->flags & GS_REFERENCED))
699 undefined_global_sym_count++;
700 entry_symbol->flags |= GS_REFERENCED;
701 add_cmdline_ref(entry_symbol);
728 output_filename = xmalloc(strlen(arg)+5);
729 strcpy(output_filename, arg);
730 strcat(output_filename, ".tmp");
731 real_output_filename = arg;
735 output_filename = arg;
749 relocatable_output = 1;
755 rrs_search_paths = (rrs_search_paths == NULL)
757 : concat(rrs_search_paths, ":", arg);
761 strip_symbols = STRIP_DEBUGGER;
765 strip_symbols = STRIP_ALL;
769 text_start = parse(arg, "%x", "invalid argument to -T");
770 T_flag_specified = 1;
779 register symbol *sp = getsym(arg);
781 if (!sp->defined && !(sp->flags & GS_REFERENCED))
782 undefined_global_sym_count++;
783 sp->flags |= GS_REFERENCED;
790 soversion = parse(arg, "%d", "invalid argument to -V");
795 discard_locals = DISCARD_L;
799 discard_locals = DISCARD_ALL;
804 register symbol *sp = getsym(&swt[2]);
805 sp->flags |= GS_TRACE;
818 magic = oldmagic = ZMAGIC;
825 errx(1, "invalid command option `%s'", swt);
829 /* Convenient functions for operating on one or all files being loaded. */
832 * Call FUNCTION on each input file entry. Do not call for entries for
833 * libraries; instead, call once for each library member that is being
836 * FUNCTION receives two arguments: the entry, and ARG.
840 each_file(function, arg)
841 register void (*function)();
846 for (i = 0; i < number_of_files; i++) {
847 register struct file_entry *entry = &file_table[i];
848 register struct file_entry *subentry;
850 if (entry->flags & E_SCRAPPED)
853 if (!(entry->flags & E_IS_LIBRARY))
854 (*function)(entry, arg);
856 subentry = entry->subfiles;
857 for (; subentry; subentry = subentry->chain) {
858 if (subentry->flags & E_SCRAPPED)
860 (*function)(subentry, arg);
864 if (entry->silly_archive) {
866 if (!(entry->flags & E_DYNAMIC))
869 if (!(entry->silly_archive->flags & E_IS_LIBRARY))
872 subentry = entry->silly_archive->subfiles;
873 for (; subentry; subentry = subentry->chain) {
874 if (subentry->flags & E_SCRAPPED)
876 (*function)(subentry, arg);
884 * Call FUNCTION on each input file entry until it returns a non-zero value.
885 * Return this value. Do not call for entries for libraries; instead, call
886 * once for each library member that is being loaded.
888 * FUNCTION receives two arguments: the entry, and ARG. It must be a function
889 * returning unsigned long (though this can probably be fudged).
893 check_each_file(function, arg)
894 register unsigned long (*function)();
898 register unsigned long return_val;
900 for (i = 0; i < number_of_files; i++) {
901 register struct file_entry *entry = &file_table[i];
902 if (entry->flags & E_SCRAPPED)
904 if (entry->flags & E_IS_LIBRARY) {
905 register struct file_entry *subentry = entry->subfiles;
906 for (; subentry; subentry = subentry->chain) {
907 if (subentry->flags & E_SCRAPPED)
909 if ( (return_val = (*function)(subentry, arg)) )
912 } else if ( (return_val = (*function)(entry, arg)) )
918 /* Like `each_file' but ignore files that were just for symbol definitions. */
921 each_full_file(function, arg)
922 register void (*function)();
927 for (i = 0; i < number_of_files; i++) {
928 register struct file_entry *entry = &file_table[i];
929 register struct file_entry *subentry;
931 if (entry->flags & (E_SCRAPPED | E_JUST_SYMS))
935 if (entry->silly_archive) {
937 if (!(entry->flags & E_DYNAMIC))
940 if (!(entry->silly_archive->flags & E_IS_LIBRARY))
943 subentry = entry->silly_archive->subfiles;
944 for (; subentry; subentry = subentry->chain) {
945 if (subentry->flags & E_SCRAPPED)
947 (*function)(subentry, arg);
951 if (entry->flags & E_DYNAMIC)
954 if (!(entry->flags & E_IS_LIBRARY))
955 (*function)(entry, arg);
957 subentry = entry->subfiles;
958 for (; subentry; subentry = subentry->chain) {
959 if (subentry->flags & E_SCRAPPED)
961 (*function)(subentry, arg);
967 /* Close the input file that is now open. */
978 * Open the input file specified by 'entry', and return a descriptor. The
979 * open file is remembered; if the same file is opened twice in a row, a new
980 * open is not actually done.
984 register struct file_entry *entry;
988 if (entry->superfile && (entry->superfile->flags & E_IS_LIBRARY))
989 return file_open(entry->superfile);
991 if (entry == input_file)
997 if (entry->flags & E_SEARCH_DIRS) {
1000 fd = open(entry->filename, O_RDONLY, 0);
1008 if (entry->flags & E_SEARCH_DIRS)
1009 errx(1, "%s: no match", entry->local_sym_name);
1011 err(1, "%s", entry->filename);
1017 struct file_entry *entry;
1019 return entry->starting_offset + N_TXTOFF (entry->header);
1022 /*---------------------------------------------------------------------------*/
1025 * Read a file's header into the proper place in the file_entry. FD is the
1026 * descriptor on which the file is open. ENTRY is the file's entry.
1029 read_header(fd, entry)
1031 struct file_entry *entry;
1035 if (lseek(fd, entry->starting_offset, L_SET) !=
1036 entry->starting_offset)
1037 err(1, "%s: read_header: lseek", get_file_name(entry));
1039 len = read(fd, &entry->header, sizeof(struct exec));
1040 if (len != sizeof (struct exec))
1041 err(1, "%s: read_header: read", get_file_name(entry));
1043 md_swapin_exec_hdr(&entry->header);
1045 if (N_BADMAG (entry->header))
1046 errx(1, "%s: bad magic", get_file_name(entry));
1048 if (N_BADMID(entry->header))
1049 errx(1, "%s: non-native input file", get_file_name(entry));
1051 entry->flags |= E_HEADER_VALID;
1055 * Read the symbols of file ENTRY into core. Assume it is already open, on
1056 * descriptor FD. Also read the length of the string table, which follows
1057 * the symbol table, but don't read the contents of the string table.
1061 read_entry_symbols(fd, entry)
1062 struct file_entry *entry;
1069 if (!(entry->flags & E_HEADER_VALID))
1070 read_header(fd, entry);
1072 np = (struct nlist *)alloca(entry->header.a_syms);
1073 entry->nsymbols = entry->header.a_syms / sizeof(struct nlist);
1074 if (entry->nsymbols == 0)
1077 entry->symbols = (struct localsymbol *)
1078 xmalloc(entry->nsymbols * sizeof(struct localsymbol));
1080 if (lseek(fd, N_SYMOFF(entry->header) + entry->starting_offset, L_SET)
1081 != N_SYMOFF(entry->header) + entry->starting_offset)
1082 err(1, "%s: read_symbols: lseek(syms)", get_file_name(entry));
1084 if (entry->header.a_syms != read(fd, np, entry->header.a_syms))
1085 errx(1, "%s: read_symbols: premature end of file in symbols",
1086 get_file_name(entry));
1088 md_swapin_symbols(np, entry->header.a_syms / sizeof(struct nlist));
1090 for (i = 0; i < entry->nsymbols; i++) {
1091 entry->symbols[i].nzlist.nlist = *np++;
1092 entry->symbols[i].nzlist.nz_size = 0;
1093 entry->symbols[i].symbol = NULL;
1094 entry->symbols[i].next = NULL;
1095 entry->symbols[i].entry = entry;
1096 entry->symbols[i].gotslot_offset = -1;
1097 entry->symbols[i].flags = 0;
1100 entry->strings_offset = N_STROFF(entry->header) +
1101 entry->starting_offset;
1102 if (lseek(fd, entry->strings_offset, 0) == (off_t)-1)
1103 err(1, "%s: read_symbols: lseek(strings)",
1104 get_file_name(entry));
1105 if (sizeof str_size != read(fd, &str_size, sizeof str_size))
1106 errx(1, "%s: read_symbols: cannot read string table size",
1107 get_file_name(entry));
1109 entry->string_size = md_swap_long(str_size);
1113 * Read the string table of file ENTRY open on descriptor FD, into core.
1116 read_entry_strings(fd, entry)
1117 struct file_entry *entry;
1121 if (entry->string_size == 0)
1124 if (!(entry->flags & E_HEADER_VALID) || !entry->strings_offset)
1125 errx(1, "%s: read_strings: string table unavailable",
1126 get_file_name(entry));
1128 if (lseek(fd, entry->strings_offset, L_SET) !=
1129 entry->strings_offset)
1130 err(1, "%s: read_strings: lseek",
1131 get_file_name(entry));
1133 if (read(fd, entry->strings, entry->string_size) !=
1135 errx(1, "%s: read_strings: premature end of file in strings",
1136 get_file_name(entry));
1141 /* Read in the relocation sections of ENTRY if necessary */
1144 read_entry_relocation(fd, entry)
1146 struct file_entry *entry;
1148 register struct relocation_info *reloc;
1151 if (!entry->textrel) {
1153 reloc = (struct relocation_info *)
1154 xmalloc(entry->header.a_trsize);
1156 pos = text_offset(entry) +
1157 entry->header.a_text + entry->header.a_data;
1159 if (lseek(fd, pos, L_SET) != pos)
1160 err(1, "%s: read_reloc(text): lseek",
1161 get_file_name(entry));
1163 if (read(fd, reloc, entry->header.a_trsize) !=
1164 entry->header.a_trsize)
1165 errx(1, "%s: read_reloc(text): premature EOF",
1166 get_file_name(entry));
1168 md_swapin_reloc(reloc, entry->header.a_trsize / sizeof(*reloc));
1169 entry->textrel = reloc;
1170 entry->ntextrel = entry->header.a_trsize / sizeof(*reloc);
1174 if (!entry->datarel) {
1176 reloc = (struct relocation_info *)
1177 xmalloc(entry->header.a_drsize);
1179 pos = text_offset(entry) + entry->header.a_text +
1180 entry->header.a_data + entry->header.a_trsize;
1182 if (lseek(fd, pos, L_SET) != pos)
1183 err(1, "%s: read_reloc(data): lseek",
1184 get_file_name(entry));
1186 if (read(fd, reloc, entry->header.a_drsize) !=
1187 entry->header.a_drsize)
1188 errx(1, "%s: read_reloc(data): premature EOF",
1189 get_file_name(entry));
1191 md_swapin_reloc(reloc, entry->header.a_drsize / sizeof(*reloc));
1192 entry->datarel = reloc;
1193 entry->ndatarel = entry->header.a_drsize / sizeof(*reloc);
1198 /*---------------------------------------------------------------------------*/
1201 * Read in the symbols of all input files.
1209 fprintf(stderr, "Loading symbols:\n\n");
1211 for (i = 0; i < number_of_files; i++)
1212 read_file_symbols(&file_table[i]);
1215 fprintf(stderr, "\n");
1219 * If ENTRY is a rel file, read its symbol and string sections into core. If
1220 * it is a library, search it and load the appropriate members (which means
1221 * calling this function recursively on those members).
1225 read_file_symbols(entry)
1226 register struct file_entry *entry;
1232 fd = file_open(entry);
1234 len = read(fd, &hdr, sizeof hdr);
1235 if (len != sizeof hdr)
1236 errx(1, "%s: read_file_symbols(header): premature EOF",
1237 get_file_name(entry));
1239 md_swapin_exec_hdr(&hdr);
1241 if (!N_BADMAG (hdr)) {
1242 if (N_IS_DYNAMIC(hdr) && !(entry->flags & E_JUST_SYMS)) {
1243 if (relocatable_output) {
1245 "%s: -r and shared objects currently not supported",
1246 get_file_name(entry));
1249 #if notyet /* Compatibility */
1250 if (!(N_GETFLAG(hdr) & EX_PIC))
1251 warnx("%s: EX_PIC not set",
1252 get_file_name(entry));
1254 entry->flags |= E_DYNAMIC;
1255 if (entry->superfile || rrs_add_shobj(entry))
1256 read_shared_object(fd, entry);
1258 entry->flags |= E_SCRAPPED;
1260 if (N_GETFLAG(hdr) & EX_PIC)
1262 read_entry_symbols(fd, entry);
1263 entry->strings = (char *)alloca(entry->string_size);
1264 read_entry_strings(fd, entry);
1265 read_entry_relocation(fd, entry);
1266 enter_file_symbols(entry);
1273 if (SARMAG != read(fd, armag, SARMAG) ||
1274 strncmp (armag, ARMAG, SARMAG))
1276 "%s: malformed input file (not rel or archive)",
1277 get_file_name(entry));
1278 entry->flags |= E_IS_LIBRARY;
1279 search_library(fd, entry);
1287 * Enter the external symbol defs and refs of ENTRY in the hash table.
1291 enter_file_symbols(entry)
1292 struct file_entry *entry;
1294 struct localsymbol *lsp, *lspend;
1297 prline_file_name(entry, stderr);
1299 lspend = entry->symbols + entry->nsymbols;
1301 for (lsp = entry->symbols; lsp < lspend; lsp++) {
1302 register struct nlist *p = &lsp->nzlist.nlist;
1304 if (p->n_type == (N_SETV | N_EXT))
1308 * Turn magically prefixed symbols into set symbols of
1309 * a corresponding type.
1311 if (set_element_prefixes &&
1312 set_element_prefixed_p(entry->strings+lsp->nzlist.nz_strx))
1313 lsp->nzlist.nz_type += (N_SETA - N_ABS);
1315 if (SET_ELEMENT_P(p->n_type)) {
1317 if (!relocatable_output)
1318 enter_global_ref(lsp,
1319 p->n_un.n_strx + entry->strings, entry);
1320 } else if (p->n_type == N_WARNING) {
1321 char *msg = p->n_un.n_strx + entry->strings;
1323 /* Grab the next entry. */
1325 p = &lsp->nzlist.nlist;
1326 if (p->n_type != (N_UNDF | N_EXT)) {
1328 "%s: Warning symbol without external reference following.",
1329 get_file_name(entry));
1330 make_executable = 0;
1331 lsp--; /* Process normally. */
1334 char *name = p->n_un.n_strx + entry->strings;
1335 /* Deal with the warning symbol. */
1336 lsp->flags |= LS_WARNING;
1337 enter_global_ref(lsp, name, entry);
1339 if (sp->warning == NULL) {
1340 sp->warning = (char *)
1341 xmalloc(strlen(msg)+1);
1342 strcpy(sp->warning, msg);
1344 } else if (strcmp(sp->warning, msg))
1346 "%s: multiple definitions for warning symbol `%s'",
1347 get_file_name(entry), demangle(sp->name));
1349 } else if (p->n_type & N_EXT) {
1350 enter_global_ref(lsp,
1351 p->n_un.n_strx + entry->strings, entry);
1352 } else if (p->n_un.n_strx &&
1353 (p->n_un.n_strx + entry->strings)[0] == LPREFIX)
1354 lsp->flags |= LS_L_SYMBOL;
1360 * Enter one global symbol in the hash table. LSP points to the `struct
1361 * localsymbol' from the file that describes the global symbol. NAME is the
1362 * symbol's name. ENTRY is the file entry for the file the symbol comes from.
1364 * LSP is put on the chain of all such structs that refer to the same symbol.
1365 * This chain starts in the `refs' for symbols from relocatable objects. A
1366 * backpointer to the global symbol is kept in LSP.
1368 * Symbols from shared objects are linked through `soref'. For such symbols
1369 * that's all we do at this stage, with the exception of the case where the
1370 * symbol is a common. The `referenced' bit is only set for references from
1371 * relocatable objects.
1376 enter_global_ref(lsp, name, entry)
1377 struct localsymbol *lsp;
1379 struct file_entry *entry;
1381 register struct nzlist *nzp = &lsp->nzlist;
1382 register symbol *sp = getsym(name);
1383 register int type = nzp->nz_type;
1384 int oldref = (sp->flags & GS_REFERENCED);
1385 int olddef = sp->defined;
1386 int com = sp->defined && sp->common_size;
1388 if (type == (N_INDR | N_EXT) && !olddef) {
1389 sp->alias = getsym(entry->strings + (lsp + 1)->nzlist.nz_strx);
1390 if (sp == sp->alias) {
1391 warnx("%s: %s is alias for itself",
1392 get_file_name(entry), name);
1393 /* Rewrite symbol as global text symbol with value 0 */
1394 lsp->nzlist.nz_type = N_TEXT|N_EXT;
1395 lsp->nzlist.nz_value = 0;
1396 make_executable = 0;
1399 if (sp->flags & GS_REFERENCED)
1400 sp->alias->flags |= GS_REFERENCED;
1404 if (entry->flags & E_DYNAMIC) {
1405 lsp->next = sp->sorefs;
1410 * Handle commons from shared objects:
1411 * 1) If symbol hitherto undefined, turn it into a common.
1412 * 2) If symbol already common, update size if necessary.
1414 /*XXX - look at case where commons are only in shared objects */
1415 if (type == (N_UNDF | N_EXT) && nzp->nz_value) {
1418 undefined_global_sym_count--;
1419 common_defined_global_count++;
1420 sp->common_size = nzp->nz_value;
1421 sp->defined = N_UNDF | N_EXT;
1422 } else if (com && sp->common_size < nzp->nz_value) {
1423 sp->common_size = nzp->nz_value;
1425 } else if (type != (N_UNDF | N_EXT) && !oldref) {
1427 * This is an ex common...
1430 common_defined_global_count--;
1431 sp->common_size = 0;
1436 * Handle size information in shared objects.
1438 if (nzp->nz_size > sp->size)
1439 sp->size = nzp->nz_size;
1441 if ((lsp->flags & LS_WARNING) && (sp->flags & GS_REFERENCED))
1443 * Prevent warning symbols from getting
1444 * gratuitously referenced.
1446 list_warning_symbols = 1;
1450 lsp->next = sp->refs;
1454 if (lsp->flags & LS_WARNING) {
1456 * Prevent warning symbols from getting
1457 * gratuitously referenced.
1459 if (sp->flags & GS_REFERENCED)
1460 list_warning_symbols = 1;
1465 list_warning_symbols = 1;
1467 sp->flags |= GS_REFERENCED;
1469 if (sp == dynamic_symbol || sp == got_symbol) {
1470 if (type != (N_UNDF | N_EXT) && !(entry->flags & E_JUST_SYMS))
1471 errx(1,"Linker reserved symbol %s defined as type %x ",
1476 if (olddef && N_ISWEAK(&nzp->nlist) && !(sp->flags & GS_WEAK)) {
1478 printf("%s: not overridden by weak symbol from %s\n",
1479 demangle(sp->name), get_file_name(entry));
1484 if (type == (N_SIZE | N_EXT)) {
1486 if (relocatable_output && nzp->nz_value != 0 && sp->size == 0)
1488 if (sp->size < nzp->nz_value)
1489 sp->size = nzp->nz_value;
1491 } else if (type != (N_UNDF | N_EXT) || nzp->nz_value) {
1494 * Set `->defined' here, so commons and undefined globals
1495 * can be counted correctly.
1497 if (!sp->defined || sp->defined == (N_UNDF | N_EXT)) {
1501 if ((sp->flags & GS_WEAK) && !N_ISWEAK(&nzp->nlist)) {
1503 * Upgrade an existing weak definition.
1504 * We fake it by pretending the symbol is undefined;
1505 * must undo any common fiddling, however.
1508 errx(1, "internal error: enter_glob_ref: "
1509 "weak symbol not referenced");
1510 if (!olddef && !com)
1511 undefined_weak_sym_count--;
1512 undefined_global_sym_count++;
1514 sp->flags &= ~GS_WEAK;
1517 common_defined_global_count--;
1519 sp->common_size = 0;
1521 if (oldref && !olddef) {
1523 * It used to be undefined and we're defining it.
1525 undefined_global_sym_count--;
1526 if (sp->flags & GS_WEAK)
1527 /* Used to be a weak reference */
1528 undefined_weak_sym_count--;
1529 if (undefined_global_sym_count < 0 ||
1530 undefined_weak_sym_count < 0)
1531 errx(1, "internal error: enter_glob_ref: "
1532 "undefined_global_sym_count = %d, "
1533 "undefined_weak_sym_count = %d",
1534 undefined_global_sym_count,
1535 undefined_weak_sym_count);
1539 if (N_ISWEAK(&nzp->nlist))
1540 /* The definition is weak */
1541 sp->flags |= GS_WEAK;
1543 if (!olddef && type == (N_UNDF | N_EXT) && nzp->nz_value) {
1545 * First definition and it's common.
1547 common_defined_global_count++;
1548 sp->common_size = nzp->nz_value;
1549 } else if (com && type != (N_UNDF | N_EXT)) {
1551 * It used to be common and we're defining
1552 * it as something else.
1554 common_defined_global_count--;
1555 sp->common_size = 0;
1556 } else if (com && type == (N_UNDF | N_EXT) &&
1557 sp->common_size < nzp->nz_value)
1559 * It used to be common and this is a new common entry
1560 * to which we need to pay attention.
1562 sp->common_size = nzp->nz_value;
1564 if (SET_ELEMENT_P(type) && (!olddef || com))
1567 } else if (!oldref && !com) {
1569 * An unreferenced symbol can already be defined
1570 * as common by shared objects.
1572 undefined_global_sym_count++;
1573 if (N_ISWEAK(&nzp->nlist)) {
1574 /* The reference is weak */
1575 sp->flags |= GS_WEAK;
1576 undefined_weak_sym_count++;
1580 if (sp == end_symbol && (entry->flags & E_JUST_SYMS) &&
1582 text_start = nzp->nz_value;
1584 if (sp->flags & GS_TRACE) {
1585 register char *reftype;
1586 switch (type & N_TYPE) {
1588 reftype = nzp->nz_value
1589 ? "defined as common" : "referenced";
1593 reftype = "defined as absolute";
1597 reftype = "defined in text section";
1601 reftype = "defined in data section";
1605 reftype = "defined in BSS section";
1613 reftype = "size spec";
1617 reftype = "I don't know this type";
1621 fprintf(stderr, "symbol %s %s%s in ", demangle(sp->name),
1622 (N_ISWEAK(&nzp->nlist))?"weakly ":"", reftype);
1623 print_file_name (entry, stderr);
1624 fprintf(stderr, "\n");
1629 * This returns 0 if the given file entry's symbol table does *not* contain
1630 * the nlist point entry, and it returns the files entry pointer (cast to
1631 * unsigned long) if it does.
1635 contains_symbol(entry, np)
1636 struct file_entry *entry;
1637 register struct nlist *np;
1639 if (np >= &entry->symbols->nzlist.nlist &&
1640 np < &(entry->symbols + entry->nsymbols)->nzlist.nlist)
1641 return (unsigned long) entry;
1647 * Having entered all the global symbols and found the sizes of sections of
1648 * all files to be linked, make all appropriate deductions from this data.
1650 * We propagate global symbol values from definitions to references. We compute
1651 * the layout of the output file and where each input file's contents fit
1654 * This is now done in several stages.
1656 * 1) All global symbols are examined for definitions in relocatable (.o)
1657 * files. The symbols' type is set according to the definition found,
1658 * but its value can not yet be determined. In stead, we keep a pointer
1659 * to the file entry's localsymbol that bequeathed the global symbol with
1660 * its definition. Also, multiple (incompatible) definitions are checked
1661 * for in this pass. If no definition comes forward, the set of local
1662 * symbols originating from shared objects is searched for a definition.
1664 * 2) Then the relocation information of each relocatable file is examined
1665 * for possible contributions to the RRS section.
1667 * 3) When this is done, the sizes and start addresses are set of all segments
1668 * that will appear in the output file (including the RRS segment).
1670 * 4) Finally, all symbols are relocated according according to the start
1671 * of the entry they are part of. Then global symbols are assigned their
1672 * final values. Also, space for commons and imported data are allocated
1673 * during this pass, if the link mode in effect so demands.
1682 fprintf(stderr, "Digesting symbol information:\n\n");
1684 if (!relocatable_output) {
1686 * The set sector size is the number of set elements + a word
1687 * for each symbol for the length word at the beginning of
1688 * the vector, plus a word for each symbol for a zero at the
1689 * end of the vector (for incremental linking).
1691 set_sect_size = (set_symbol_count + 2 * set_vector_count) *
1692 sizeof (unsigned long);
1693 set_vectors = (long *)xmalloc (set_sect_size);
1694 setv_fill_count = 0;
1697 /* Pass 1: check and define symbols */
1698 defined_global_sym_count = 0;
1701 each_full_file(consider_relocation, (void *)0); /* Text */
1702 each_full_file(consider_relocation, (void *)1); /* Data */
1704 each_file(consider_local_symbols, (void *)0);
1707 * Compute total size of sections.
1708 * RRS data is the first output data section, RRS text is the last
1709 * text section. Thus, DATA_START is calculated from RRS_DATA_START
1710 * and RRS_DATA_SIZE, while RRS_TEXT_START is derived from TEXT_START
1713 consider_rrs_section_lengths();
1714 each_full_file(consider_file_section_lengths, 0);
1715 rrs_text_start = text_start + text_size;
1716 text_size += rrs_text_size;
1717 data_size += rrs_data_size;
1720 * If necessary, pad text section to full page in the file. Include
1721 * the padding in the text segment size.
1724 if (page_align_segments || page_align_data) {
1725 int text_end = text_size + N_TXTOFF(outheader);
1726 text_pad = PALIGN(text_end, page_size) - text_end;
1727 text_size += text_pad;
1729 outheader.a_text = text_size;
1732 * Make the data segment address start in memory on a suitable
1736 if (!Tdata_flag_specified)
1737 rrs_data_start = text_start +
1738 DATA_START(outheader) - TEXT_START(outheader);
1740 data_start = rrs_data_start + rrs_data_size;
1741 if (!relocatable_output) {
1742 set_sect_start = rrs_data_start + data_size;
1743 data_size += MALIGN(set_sect_size);
1745 bss_start = rrs_data_start + data_size;
1748 printf("textstart = %#x, textsize = %#x, rrs_text_start = %#x, rrs_text_size %#x\n",
1749 text_start, text_size, rrs_text_start, rrs_text_size);
1750 printf("datastart = %#x, datasize = %#x, rrs_data_start %#x, rrs_data_size %#x\n",
1751 data_start, data_size, rrs_data_start, rrs_data_size);
1752 printf("bssstart = %#x, bsssize = %#x\n",
1753 bss_start, bss_size);
1754 printf("set_sect_start = %#x, set_sect_size = %#x\n",
1755 set_sect_start, set_sect_size);
1758 /* Compute start addresses of each file's sections and symbols. */
1760 each_full_file(relocate_file_addresses, 0);
1761 relocate_rrs_addresses();
1763 /* Pass 2: assign values to symbols */
1766 if (end_symbol) { /* These are null if -r. */
1767 etext_symbol->value = text_start + text_size - text_pad;
1768 edata_symbol->value = rrs_data_start + data_size;
1769 end_symbol->value = rrs_data_start + data_size + bss_size;
1772 * Figure the data_pad now, so that it overlaps with the bss
1776 if (specified_data_size && specified_data_size > data_size)
1777 data_pad = specified_data_size - data_size;
1779 if (page_align_segments)
1780 data_pad = PALIGN(data_pad + data_size, page_size) - data_size;
1782 bss_size -= data_pad;
1786 data_size += data_pad;
1789 * Calculate total number of symbols that will go into
1790 * the output symbol table (barring DISCARD_* settings).
1792 global_sym_count = defined_global_sym_count +
1793 undefined_global_sym_count;
1795 if (dynamic_symbol->flags & GS_REFERENCED)
1798 if (got_symbol->flags & GS_REFERENCED)
1801 if (relocatable_output || building_shared_object) {
1802 /* For each alias we write out two struct nlists */
1803 global_sym_count += global_alias_count;
1804 /* Propagate warning symbols; costs two extra struct nlists */
1805 global_sym_count += 2 * warn_sym_count;
1808 if (relocatable_output)
1809 /* We write out the original N_SIZE symbols */
1810 global_sym_count += size_sym_count;
1814 "global symbols %d "
1815 "(defined %d, undefined %d, weak %d, aliases %d, warnings 2 * %d, "
1816 "size symbols %d)\ncommons %d, locals: %d, debug symbols: %d, set_symbols %d\n",
1818 defined_global_sym_count, undefined_global_sym_count,
1819 undefined_weak_sym_count,
1820 global_alias_count, warn_sym_count, size_sym_count,
1821 common_defined_global_count, local_sym_count,
1822 debugger_sym_count, set_symbol_count);
1827 * Determine the definition of each global symbol.
1834 * For each symbol, verify that it is defined globally at most
1835 * once within relocatable files (except when building a shared lib).
1836 * and set the `defined' field if there is a definition.
1838 * Then check the shared object symbol chain for any remaining
1839 * undefined symbols. Set the `so_defined' field for any
1840 * definition find this way.
1842 FOR_EACH_SYMBOL(i, sp) {
1844 struct localsymbol *lsp;
1847 if (!(sp->flags & GS_REFERENCED)) {
1849 /* Check for undefined symbols in shared objects */
1851 for (lsp = sp->sorefs; lsp; lsp = lsp->next) {
1852 type = lsp->nzlist.nlist.n_type;
1853 if ((type & N_EXT) && type != (N_UNDF | N_EXT))
1856 if ((type & N_EXT) && type == (N_UNDF | N_EXT))
1857 undefined_shobj_sym_count++;
1860 /* Superfluous symbol from shared object */
1864 /* Already examined; must have been an alias */
1867 if (sp == got_symbol || sp == dynamic_symbol)
1870 for (lsp = sp->refs; lsp; lsp = lsp->next) {
1871 register struct nlist *p = &lsp->nzlist.nlist;
1872 register int type = p->n_type;
1874 if (SET_ELEMENT_P(type)) {
1875 if (relocatable_output)
1877 "internal error: global ref to set el %s with -r",
1878 demangle(sp->name));
1880 sp->defined = N_SETV | N_EXT;
1882 setv_fill_count++ * sizeof(long);
1883 } else if ((sp->defined & N_TYPE) != N_SETV) {
1885 multiple_def_count++;
1887 /* Keep count and remember symbol */
1889 set_vectors[setv_fill_count++] = (long)p;
1890 if (building_shared_object) {
1891 struct relocation_info reloc;
1894 * Make sure to relocate the contents
1895 * of this set vector.
1897 bzero(&reloc, sizeof(reloc));
1898 RELOC_INIT_SEGMENT_RELOC(&reloc);
1899 RELOC_ADDRESS(&reloc) =
1900 setv_fill_count * sizeof(long);
1901 alloc_rrs_segment_reloc(NULL, &reloc);
1904 } else if ((type & N_EXT) && type != (N_UNDF | N_EXT)
1905 && (type & N_TYPE) != N_FN
1906 && (type & N_TYPE) != N_SIZE) {
1907 /* non-common definition */
1912 multiple_def_count++;
1913 } else if (!N_ISWEAK(p) ||
1914 (!sp->def_lsp && !sp->common_size)) {
1916 lsp->entry->flags |= E_SYMBOLS_USED;
1924 * If this symbol has acquired final definition, we're done.
1925 * Commons must be allowed to bind to shared object data
1929 (sp->common_size == 0 ||
1930 relocatable_output || building_shared_object)) {
1931 if ((sp->defined & N_TYPE) == N_SETV)
1932 /* Allocate zero entry in set vector */
1935 * At this stage, we do not know whether an alias
1936 * is going to be defined for real here, or whether
1937 * it refers to a shared object symbol. The decision
1938 * is deferred until digest_pass2().
1941 defined_global_sym_count++;
1945 if (relocatable_output)
1950 * Still undefined, search the shared object symbols for a
1951 * definition. This symbol must go into the RRS.
1953 if (building_shared_object) {
1954 /* Just punt for now */
1955 undefined_global_sym_count--;
1956 if (undefined_global_sym_count < 0)
1958 "internal error: digest_pass1,1: %s: undefined_global_sym_count = %d",
1959 demangle(sp->name), undefined_global_sym_count);
1965 for (lsp = sp->sorefs; lsp; lsp = lsp->next) {
1966 register struct nlist *p = &lsp->nzlist.nlist;
1967 register int type = p->n_type;
1969 if ((type & N_EXT) && type != (N_UNDF | N_EXT) &&
1970 (type & N_TYPE) != N_FN) {
1971 /* non-common definition */
1972 if (sp->common_size) {
1974 * This common has an so defn; switch
1975 * to it iff defn is: data, first-class
1978 if (N_AUX(p) != AUX_OBJECT ||
1980 (lsp->entry->flags & E_SECONDCLASS))
1984 * Change common to so ref. First,
1985 * downgrade common to undefined.
1987 sp->common_size = 0;
1989 common_defined_global_count--;
1990 undefined_global_sym_count++;
1993 sp->so_defined = type;
1995 if (lsp->entry->flags & E_SECONDCLASS)
1996 /* Keep looking for something better */
1999 /* Keep looking for something better */
2006 printf("pass1: SO definition for %s, type %x in %s at %#x\n",
2007 demangle(sp->name), sp->so_defined, get_file_name(sp->def_lsp->entry),
2008 sp->def_lsp->nzlist.nz_value);
2010 sp->def_lsp->entry->flags |= E_SYMBOLS_USED;
2011 if (sp->flags & GS_REFERENCED) {
2012 undefined_global_sym_count--;
2014 sp->flags |= GS_REFERENCED;
2015 if (undefined_global_sym_count < 0)
2016 errx(1, "internal error: digest_pass1,2: "
2017 "%s: undefined_global_sym_count = %d",
2018 demangle(sp->name), undefined_global_sym_count);
2020 !(sp->alias->flags & GS_REFERENCED)) {
2024 } else if (sp->defined) {
2025 if (sp->common_size == 0)
2026 errx(1, "internal error: digest_pass1,3: "
2027 "%s: not a common: %x",
2028 demangle(sp->name), sp->defined);
2030 * Common not bound to shared object data; treat
2031 * it now like other defined symbols were above.
2034 defined_global_sym_count++;
2039 if (setv_fill_count != set_sect_size/sizeof(long))
2040 errx(1, "internal error: allocated set symbol space (%d) "
2041 "doesn't match actual (%d)",
2042 set_sect_size/sizeof(long), setv_fill_count);
2047 * Scan relocation info in ENTRY for contributions to the RRS section
2048 * of the output file.
2051 consider_relocation(entry, dataseg)
2052 struct file_entry *entry;
2055 struct relocation_info *reloc, *end;
2056 struct localsymbol *lsp;
2060 /* Text relocations */
2061 reloc = entry->textrel;
2062 end = entry->textrel + entry->ntextrel;
2064 /* Data relocations */
2065 reloc = entry->datarel;
2066 end = entry->datarel + entry->ndatarel;
2069 for (; reloc < end; reloc++) {
2071 if (relocatable_output) {
2072 lsp = &entry->symbols[reloc->r_symbolnum];
2073 if (RELOC_BASEREL_P(reloc)) {
2074 pic_code_seen = 1; /* Compatibility */
2075 if (!RELOC_EXTERN_P(reloc))
2076 lsp->flags |= LS_RENAME;
2082 * First, do the PIC specific relocs.
2083 * r_relative and r_copy should not occur at this point
2084 * (we do output them). The others break down to these
2087 * jmptab: extern: needs jmp slot
2088 * !extern: "intersegment" jump/call,
2089 * should get resolved in output
2091 * baserel: extern: need GOT entry
2092 * !extern: may need GOT entry,
2095 * baserel's always refer to symbol through `r_symbolnum'
2096 * whether extern or not. Internal baserels refer to statics
2097 * that must be accessed either *through* the GOT table like
2098 * global data, or by means of an offset from the GOT table.
2099 * The macro RELOC_STATICS_THROUGH_GOT_P() determines which
2100 * applies, since this is a machine (compiler?) dependent
2104 if (RELOC_JMPTAB_P(reloc)) {
2106 if (!RELOC_EXTERN_P(reloc))
2109 lsp = &entry->symbols[reloc->r_symbolnum];
2113 if (sp->flags & GS_TRACE) {
2114 fprintf(stderr, "symbol %s has jmpslot in %s\n",
2115 demangle(sp->name), get_file_name(entry));
2117 alloc_rrs_jmpslot(entry, sp);
2119 } else if (RELOC_BASEREL_P(reloc)) {
2121 lsp = &entry->symbols[reloc->r_symbolnum];
2122 alloc_rrs_gotslot(entry, reloc, lsp);
2123 if (pic_type != PIC_TYPE_NONE &&
2124 RELOC_PIC_TYPE(reloc) != pic_type)
2125 errx(1, "%s: illegal reloc type mix",
2126 get_file_name(entry));
2127 pic_type = RELOC_PIC_TYPE(reloc);
2129 } else if (RELOC_EXTERN_P(reloc)) {
2132 * Non-PIC relocations.
2133 * If the definition comes from a shared object
2134 * we need a relocation entry in RRS.
2136 * If the .so definition is N_TEXT a jmpslot is
2139 * If it is N_DATA we allocate an address in BSS (?)
2140 * and arrange for the data to be copied at run-time.
2141 * The symbol is temporarily marked with N_SIZE in
2142 * the `defined' field, so we know what to do in
2143 * pass2() and during actual relocation. We convert
2144 * the type back to something real again when writing
2148 lsp = &entry->symbols[reloc->r_symbolnum];
2151 errx(1, "%s: bogus relocation record",
2152 get_file_name(entry));
2158 * Skip refs to _GLOBAL_OFFSET_TABLE_ and __DYNAMIC
2160 if (sp == got_symbol) {
2161 if (!CHECK_GOT_RELOC(reloc))
2163 "%s: Unexpected relocation type for GOT symbol",
2164 get_file_name(entry));
2169 * This symbol gives rise to a RRS entry
2172 if (building_shared_object) {
2173 if (sp->flags & GS_TRACE) {
2175 "symbol %s RRS entry in %s\n",
2176 demangle(sp->name), get_file_name(entry));
2178 alloc_rrs_reloc(entry, sp);
2182 if (force_alias_definition && sp->so_defined &&
2183 sp->aux == AUX_FUNC) {
2185 /* Call to shared library procedure */
2186 alloc_rrs_jmpslot(entry, sp);
2188 } else if (sp->size && sp->so_defined &&
2189 sp->aux == AUX_OBJECT) {
2191 /* Reference to shared library data */
2192 alloc_rrs_cpy_reloc(entry, sp);
2193 sp->defined = N_SIZE;
2195 } else if (!sp->defined && sp->common_size == 0 &&
2197 alloc_rrs_reloc(entry, sp);
2201 * Segment relocation.
2202 * Prepare an RRS relocation as these are load
2203 * address dependent.
2205 if (building_shared_object && !RELOC_PCREL_P(reloc)) {
2206 alloc_rrs_segment_reloc(entry, reloc);
2213 * Determine the disposition of each local symbol.
2216 consider_local_symbols(entry)
2217 register struct file_entry *entry;
2219 register struct localsymbol *lsp, *lspend;
2221 if (entry->flags & E_DYNAMIC)
2224 lspend = entry->symbols + entry->nsymbols;
2227 * For each symbol determine whether it should go
2228 * in the output symbol table.
2231 for (lsp = entry->symbols; lsp < lspend; lsp++) {
2232 register struct nlist *p = &lsp->nzlist.nlist;
2233 register int type = p->n_type;
2235 if (type == N_WARNING)
2238 if (SET_ELEMENT_P (type)) {
2240 * This occurs even if global. These types of
2241 * symbols are never written globally, though
2242 * they are stored globally.
2244 if (relocatable_output)
2245 lsp->flags |= LS_WRITE;
2247 } else if (!(type & (N_STAB | N_EXT))) {
2250 * Ordinary local symbol
2252 if ((lsp->flags & LS_RENAME) || (
2253 discard_locals != DISCARD_ALL &&
2254 !(discard_locals == DISCARD_L &&
2255 (lsp->flags & LS_L_SYMBOL))) ) {
2257 lsp->flags |= LS_WRITE;
2261 } else if (!(type & N_EXT)) {
2266 if (strip_symbols == STRIP_NONE) {
2267 lsp->flags |= LS_WRITE;
2268 debugger_sym_count++;
2274 * Count one for the local symbol that we generate,
2275 * whose name is the file's name (usually) and whose address
2276 * is the start of the file's text.
2278 if (discard_locals != DISCARD_ALL)
2283 * Accumulate the section sizes of input file ENTRY into the section sizes of
2287 consider_file_section_lengths(entry)
2288 register struct file_entry *entry;
2291 entry->text_start_address = text_size;
2292 /* If there were any vectors, we need to chop them off */
2293 text_size += entry->header.a_text;
2294 entry->data_start_address = data_size;
2295 data_size += entry->header.a_data;
2296 entry->bss_start_address = bss_size;
2297 bss_size += MALIGN(entry->header.a_bss);
2299 text_reloc_size += entry->header.a_trsize;
2300 data_reloc_size += entry->header.a_drsize;
2304 * Determine where the sections of ENTRY go into the output file,
2305 * whose total section sizes are already known.
2306 * Also relocate the addresses of the file's local and debugger symbols.
2309 relocate_file_addresses(entry)
2310 register struct file_entry *entry;
2312 register struct localsymbol *lsp, *lspend;
2314 entry->text_start_address += text_start;
2316 * Note that `data_start' and `data_size' have not yet been
2317 * adjusted for `data_pad'. If they had been, we would get the wrong
2320 entry->data_start_address += data_start;
2321 entry->bss_start_address += bss_start;
2323 printf("%s: datastart: %#x, bss %#x\n", get_file_name(entry),
2324 entry->data_start_address, entry->bss_start_address);
2327 lspend = entry->symbols + entry->nsymbols;
2329 for (lsp = entry->symbols; lsp < lspend; lsp++) {
2330 register struct nlist *p = &lsp->nzlist.nlist;
2331 register int type = p->n_type;
2334 * If this belongs to a section, update it
2335 * by the section's start address
2338 switch (type & N_TYPE) {
2341 p->n_value += entry->text_start_address;
2347 * A symbol whose value is in the data section is
2348 * present in the input file as if the data section
2349 * started at an address equal to the length of the
2352 p->n_value += entry->data_start_address -
2353 entry->header.a_text;
2357 /* likewise for symbols with value in BSS. */
2358 p->n_value += entry->bss_start_address -
2359 (entry->header.a_text +
2360 entry->header.a_data);
2369 * Assign a value to each global symbol.
2374 FOR_EACH_SYMBOL(i, sp) {
2376 int align = sizeof(int);
2378 if (!(sp->flags & GS_REFERENCED))
2382 (relocatable_output || building_shared_object ||
2383 (sp->alias->defined && !sp->alias->so_defined))) {
2385 * The alias points at a defined symbol, so it
2386 * must itself be counted as one too, in order to
2387 * compute the correct number of symbol table entries.
2391 * Change aliased symbol's definition too.
2392 * These things happen if shared object commons
2393 * or data is going into our symbol table.
2395 if (sp->so_defined != (N_INDR+N_EXT))
2396 warnx( "pass2: %s: alias isn't",
2397 demangle(sp->name));
2398 sp->defined = sp->so_defined;
2401 defined_global_sym_count++;
2405 * Count the aliases that will appear in the output.
2407 if (sp->alias && !sp->so_defined && !sp->alias->so_defined &&
2408 (sp->defined || relocatable_output ||
2409 !building_shared_object))
2410 global_alias_count++;
2412 if ((sp->defined & N_TYPE) == N_SETV) {
2414 * Set length word at front of vector and zero byte
2415 * at end. Reverse the vector itself to put it in
2418 unsigned long i, *p, *q;
2419 unsigned long length_word_index =
2420 sp->value / sizeof(long);
2422 /* Relocate symbol value */
2423 sp->value += set_sect_start;
2425 set_vectors[length_word_index] = sp->setv_count;
2428 * Relocate vector to final address.
2430 for (i = 0; i < sp->setv_count; i++) {
2431 struct nlist *p = (struct nlist *)
2432 set_vectors[1+i+length_word_index];
2434 set_vectors[1+i+length_word_index] = p->n_value;
2435 if (building_shared_object) {
2436 struct relocation_info reloc;
2438 bzero(&reloc, sizeof(reloc));
2439 RELOC_INIT_SEGMENT_RELOC(&reloc);
2440 RELOC_ADDRESS(&reloc) =
2441 (1 + i + length_word_index) *
2444 RELOC_TYPE(&reloc) =
2445 (p->n_type - (N_SETA - N_ABS)) & N_TYPE;
2446 claim_rrs_segment_reloc(NULL, &reloc);
2451 * Reverse the vector.
2453 p = &set_vectors[length_word_index + 1];
2454 q = &set_vectors[length_word_index + sp->setv_count];
2456 unsigned long tmp = *p;
2461 /* Clear terminating entry */
2462 set_vectors[length_word_index + sp->setv_count + 1] = 0;
2467 if (sp->defined && (sp->defined & ~N_EXT) != N_SETV)
2468 sp->value = sp->def_lsp->nzlist.nz_value;
2469 if (sp->so_defined &&
2470 (sp->def_lsp->entry->flags & E_SECONDCLASS))
2471 /* Flag second-hand definitions */
2472 undefined_global_sym_count++;
2473 if (sp->flags & GS_TRACE)
2474 printf("symbol %s assigned to location %#lx\n",
2475 demangle(sp->name), sp->value);
2479 * If not -r'ing, allocate common symbols in the BSS section.
2481 if (building_shared_object && !(link_mode & SYMBOLIC))
2482 /* No common allocation in shared objects */
2485 if ((size = sp->common_size) != 0) {
2489 if (sp->defined != (N_UNDF + N_EXT))
2490 errx(1, "%s: common isn't", demangle(sp->name));
2492 } else if ((size = sp->size) != 0 && sp->defined == N_SIZE) {
2494 * It's data from shared object with size info.
2496 if (!sp->so_defined)
2497 errx(1, "%s: Bogus N_SIZE item", demangle(sp->name));
2506 if (relocatable_output && !force_common_definition) {
2508 undefined_global_sym_count++;
2509 defined_global_sym_count--;
2514 * Round up to nearest sizeof (int). I don't know whether
2515 * this is necessary or not (given that alignment is taken
2516 * care of later), but it's traditional, so I'll leave it in.
2517 * Note that if this size alignment is ever removed, ALIGN
2518 * above will have to be initialized to 1 instead of sizeof
2522 size = PALIGN(size, sizeof(int));
2524 while (align < MAX_ALIGNMENT && !(size & align))
2527 bss_size = PALIGN(bss_size + data_size + rrs_data_start, align)
2528 - (data_size + rrs_data_start);
2530 sp->value = rrs_data_start + data_size + bss_size;
2531 if (sp->defined == (N_UNDF | N_EXT))
2532 sp->defined = N_BSS | N_EXT;
2535 defined_global_sym_count++;
2539 printf("Allocating %s %s: %x at %lx\n",
2540 sp->defined==(N_BSS|N_EXT)?"common":"data",
2541 demangle(sp->name), size, sp->value);
2547 /* -------------------------------------------------------------------*/
2549 /* Write the output file */
2553 struct stat statbuf;
2557 if (lstat(output_filename, &statbuf) == 0) {
2558 if (S_ISREG(statbuf.st_mode))
2559 (void)unlink(output_filename);
2563 (void)umask(u_mask);
2565 outstream = fopen(output_filename, "w");
2566 if (outstream == NULL)
2567 err(1, "fopen: %s", output_filename);
2569 if (atexit(cleanup))
2572 if (fstat(fileno(outstream), &statbuf) < 0)
2573 err(1, "fstat: %s", output_filename);
2575 filemode = statbuf.st_mode;
2577 if (S_ISREG(statbuf.st_mode) &&
2578 chmod(output_filename, filemode & ~0111) == -1)
2579 err(1, "chmod: %s", output_filename);
2581 /* Output the a.out header. */
2584 /* Output the text and data segments, relocating as we go. */
2588 /* Output the merged relocation info, if requested with `-r'. */
2589 if (relocatable_output)
2592 /* Output the symbol table (both globals and locals). */
2595 /* Output the RSS section */
2598 if (chmod (output_filename, filemode | (0111 & ~u_mask)) == -1)
2599 err(1, "chmod: %s", output_filename);
2602 /* Report I/O error such as disk full. */
2603 if (ferror(outstream) || fclose(outstream) != 0)
2604 err(1, "write_output: %s", output_filename);
2606 if (real_output_filename)
2607 if (rename(output_filename, real_output_filename))
2608 err(1, "rename output: %s to %s",
2609 output_filename, real_output_filename);
2612 /* Total number of symbols to be written in the output file. */
2620 if (link_mode & SHAREABLE)
2621 /* Output is shared object */
2622 flags = EX_DYNAMIC | EX_PIC;
2623 else if (relocatable_output && pic_code_seen)
2624 /* Output is relocatable and contains PIC code */
2626 else if (rrs_section_type == RRS_FULL)
2627 /* Output is a dynamic executable */
2631 * Output is a static executable
2632 * or a non-PIC relocatable object
2636 if (oldmagic && (flags & EX_DPMASK) && !(link_mode & FORCEDYNAMIC))
2637 warnx("Cannot set flag in old magic headers\n");
2639 N_SET_FLAG (outheader, flags);
2641 outheader.a_text = text_size;
2642 outheader.a_data = data_size;
2643 outheader.a_bss = bss_size;
2644 outheader.a_entry = (entry_symbol ? entry_symbol->value
2645 : text_start + entry_offset);
2647 if (strip_symbols == STRIP_ALL)
2650 nsyms = global_sym_count + local_sym_count + debugger_sym_count;
2652 if (relocatable_output)
2653 nsyms += set_symbol_count;
2655 outheader.a_syms = nsyms * sizeof (struct nlist);
2657 if (relocatable_output) {
2658 outheader.a_trsize = text_reloc_size;
2659 outheader.a_drsize = data_reloc_size;
2661 outheader.a_trsize = 0;
2662 outheader.a_drsize = 0;
2665 md_swapout_exec_hdr(&outheader);
2666 mywrite(&outheader, 1, sizeof (struct exec), outstream);
2667 md_swapin_exec_hdr(&outheader);
2670 * Output whatever padding is required in the executable file
2671 * between the header and the start of the text.
2674 #ifndef COFF_ENCAPSULATE
2675 padfile(N_TXTOFF(outheader) - sizeof outheader, outstream);
2680 * Relocate the text segment of each input file
2681 * and write to the output file.
2688 fprintf(stderr, "Copying and relocating text:\n\n");
2690 each_full_file(copy_text, 0);
2694 fprintf(stderr, "\n");
2696 padfile(text_pad, outstream);
2700 * Read the text segment contents of ENTRY, relocate them, and write the
2701 * result to the output file. If `-r', save the text relocation for later
2706 struct file_entry *entry;
2708 register char *bytes;
2712 prline_file_name(entry, stderr);
2714 fd = file_open(entry);
2716 /* Allocate space for the file's text section */
2717 bytes = (char *)alloca(entry->header.a_text);
2719 /* Deal with relocation information however is appropriate */
2720 if (entry->textrel == NULL)
2721 errx(1, "%s: no text relocation", get_file_name(entry));
2723 /* Read the text section into core. */
2724 if (lseek(fd, text_offset(entry), L_SET) == (off_t)-1)
2725 err(1, "%s: copy_text: lseek", get_file_name(entry));
2726 if (entry->header.a_text != read(fd, bytes, entry->header.a_text))
2727 errx(1, "%s: copy_text: premature EOF", get_file_name(entry));
2729 /* Relocate the text according to the text relocation. */
2730 perform_relocation (bytes, entry->header.a_text,
2731 entry->textrel, entry->ntextrel, entry, 0);
2733 /* Write the relocated text to the output file. */
2734 mywrite(bytes, entry->header.a_text, 1, outstream);
2738 * Relocate the data segment of each input file
2739 * and write to the output file.
2748 fprintf(stderr, "Copying and relocating data:\n\n");
2750 pos = N_DATOFF(outheader) + data_start - rrs_data_start;
2751 if (fseek(outstream, pos, SEEK_SET) != 0)
2752 errx(1, "write_data: fseek");
2754 each_full_file(copy_data, 0);
2758 * Write out the set element vectors. See digest symbols for
2759 * description of length of the set vector section.
2762 if (set_vector_count) {
2763 swap_longs(set_vectors, set_symbol_count + 2*set_vector_count);
2764 mywrite(set_vectors, set_symbol_count + 2*set_vector_count,
2765 sizeof (unsigned long), outstream);
2769 fprintf(stderr, "\n");
2771 padfile(data_pad, outstream);
2775 * Read the data segment contents of ENTRY, relocate them, and write the
2776 * result to the output file. If `-r', save the data relocation for later
2777 * reuse. See comments in `copy_text'.
2781 struct file_entry *entry;
2783 register char *bytes;
2787 prline_file_name (entry, stderr);
2789 fd = file_open(entry);
2791 bytes = (char *)alloca(entry->header.a_data);
2793 if (entry->datarel == NULL)
2794 errx(1, "%s: no data relocation", get_file_name(entry));
2796 if (lseek(fd, text_offset(entry) + entry->header.a_text, L_SET) ==
2798 err(1, "%s: copy_data: lseek", get_file_name(entry));
2799 if (entry->header.a_data != read(fd, bytes, entry->header.a_data))
2800 errx(1, "%s: copy_data: premature EOF", get_file_name(entry));
2802 perform_relocation(bytes, entry->header.a_data,
2803 entry->datarel, entry->ndatarel, entry, 1);
2805 mywrite(bytes, entry->header.a_data, 1, outstream);
2809 * Relocate ENTRY's text or data section contents. DATA is the address of the
2810 * contents, in core. DATA_SIZE is the length of the contents. PC_RELOCATION
2811 * is the difference between the address of the contents in the output file
2812 * and its address in the input file. RELOC is the address of the
2813 * relocation info, in core. NRELOC says how many there are.
2819 perform_relocation(data, data_size, reloc, nreloc, entry, dataseg)
2822 struct relocation_info *reloc;
2824 struct file_entry *entry;
2828 register struct relocation_info *r = reloc;
2829 struct relocation_info *end = reloc + nreloc;
2831 int text_relocation = entry->text_start_address;
2832 int data_relocation = entry->data_start_address - entry->header.a_text;
2833 int bss_relocation = entry->bss_start_address -
2834 entry->header.a_text - entry->header.a_data;
2835 pc_relocation = dataseg
2836 ? entry->data_start_address - entry->header.a_text
2837 : entry->text_start_address;
2839 for (; r < end; r++) {
2840 int addr = RELOC_ADDRESS(r);
2841 long addend = md_get_addend(r, data+addr);
2845 * Loop over the relocations again as we did in
2846 * consider_relocation(), claiming the reserved RRS
2850 if (addr >= data_size)
2851 errx(1, "%s: relocation address out of range",
2852 get_file_name(entry));
2854 if (RELOC_JMPTAB_P(r)) {
2856 int symindex = RELOC_SYMBOL(r);
2857 struct localsymbol *lsp = &entry->symbols[symindex];
2860 if (symindex >= entry->nsymbols)
2861 errx(1, "%s: relocation symbolnum out of range",
2862 get_file_name(entry));
2866 errx(1, "%s: bogus relocation record",
2867 get_file_name(entry));
2871 if (relocatable_output)
2872 relocation = addend;
2873 else if (!RELOC_EXTERN_P(r)) {
2874 relocation = addend +
2875 data_relocation - text_relocation;
2877 relocation = addend +
2878 claim_rrs_jmpslot(entry, r, sp, addend);
2880 } else if (RELOC_BASEREL_P(r)) {
2882 int symindex = RELOC_SYMBOL(r);
2883 struct localsymbol *lsp = &entry->symbols[symindex];
2885 if (symindex >= entry->nsymbols)
2886 errx(1, "%s: relocation symbolnum out of range",
2887 get_file_name(entry));
2889 if (relocatable_output)
2890 relocation = addend;
2891 else if (!RELOC_EXTERN_P(r))
2892 relocation = claim_rrs_internal_gotslot(
2893 entry, r, lsp, addend);
2895 relocation = claim_rrs_gotslot(
2896 entry, r, lsp, addend);
2898 } else if (RELOC_EXTERN_P(r)) {
2900 int symindex = RELOC_SYMBOL(r);
2903 if (symindex >= entry->nsymbols)
2904 errx(1, "%s: relocation symbolnum out of range",
2905 get_file_name(entry));
2907 sp = entry->symbols[symindex].symbol;
2909 errx(1, "%s: bogus relocation record",
2910 get_file_name(entry));
2914 if (relocatable_output) {
2915 relocation = addend;
2917 * In PIC code, we keep the reference to the
2918 * external symbol, even if defined now.
2921 relocation += sp->value;
2922 } else if (sp->defined) {
2923 if (sp->flags & GS_TRACE) {
2925 "symbol %s defined as %x in %s\n",
2926 demangle(sp->name), sp->defined,
2927 get_file_name(entry) );
2929 if (sp == got_symbol) {
2930 /* Handle _GOT_ refs */
2931 relocation = addend + sp->value
2933 } else if (building_shared_object) {
2935 * Normal (non-PIC) relocation needs
2936 * to be converted into an RRS reloc
2937 * when building a shared object.
2939 r->r_address += dataseg?
2940 entry->data_start_address:
2941 entry->text_start_address;
2942 relocation = addend;
2943 if (claim_rrs_reloc(
2944 entry, r, sp, &relocation))
2946 } else if (sp->defined == N_SIZE) {
2948 * If size is known, arrange a
2952 errx(1, "Copy item isn't: %s",
2953 demangle(sp->name));
2955 relocation = addend + sp->value;
2956 r->r_address = sp->value;
2957 claim_rrs_cpy_reloc(entry, r, sp);
2959 /* Plain old relocation */
2960 relocation = addend + sp->value;
2963 * If the symbol is undefined, we relocate it
2964 * in a way similar to -r case. We use an
2965 * RRS relocation to resolve the symbol at
2966 * run-time. The r_address field is updated
2967 * to reflect the changed position in the
2970 if (sp->flags & GS_TRACE) {
2972 "symbol %s claims RRS in %s%s\n",
2973 demangle(sp->name), get_file_name(entry),
2974 (sp->so_defined == (N_TEXT+N_EXT) &&
2975 sp->flags & GS_HASJMPSLOT)?
2978 if (sp->so_defined == (N_TEXT+N_EXT) &&
2979 sp->flags & GS_HASJMPSLOT) {
2981 * Claim a jmpslot if one was allocated.
2983 * At this point, a jmpslot can only
2984 * result from a shared object reference
2985 * while `force_alias' is in effect.
2987 relocation = addend +
2989 entry, r, sp, addend);
2991 r->r_address += dataseg?
2992 entry->data_start_address:
2993 entry->text_start_address;
2994 relocation = addend;
2995 if ((building_shared_object ||
2997 claim_rrs_reloc(entry, r, sp,
3005 switch (RELOC_TYPE(r)) {
3007 case N_TEXT | N_EXT:
3008 relocation = addend + text_relocation;
3012 case N_DATA | N_EXT:
3014 * A word that points to beginning of the the
3015 * data section initially contains not 0 but
3016 * rather the "address" of that section in
3017 * the input file, which is the length of the
3020 relocation = addend + data_relocation;
3026 * Similarly, an input word pointing to the
3027 * beginning of the bss initially contains
3028 * the length of text plus data of the file.
3030 relocation = addend + bss_relocation;
3036 * Don't know why this code would occur, but
3037 * apparently it does.
3042 errx(1, "%s: nonexternal relocation invalid",
3043 get_file_name(entry));
3047 * When building a shared object, these segment
3048 * relocations need a "load address relative"
3051 if (building_shared_object && !RELOC_PCREL_P(r)) {
3052 r->r_address += dataseg?
3053 entry->data_start_address:
3054 entry->text_start_address;
3055 claim_rrs_segment_reloc(entry, r);
3059 if (RELOC_PCREL_P(r))
3060 relocation -= pc_relocation;
3062 md_relocate(r, relocation, data+addr, relocatable_output);
3069 * For relocatable_output only: write out the relocation,
3070 * relocating the addresses-to-be-relocated.
3078 fprintf(stderr, "Writing text relocation:\n\n");
3081 * Assign each global symbol a sequence number, giving the order
3082 * in which `write_syms' will write it.
3083 * This is so we can store the proper symbolnum fields
3084 * in relocation entries we write.
3087 /* BLECH - Assign number 0 to __DYNAMIC (!! Sun compatibility) */
3089 if (dynamic_symbol->flags & GS_REFERENCED)
3090 dynamic_symbol->symbolnum = count++;
3091 FOR_EACH_SYMBOL(i, sp) {
3092 if (sp == dynamic_symbol)
3096 if (!(sp->flags & GS_REFERENCED))
3098 sp->symbolnum = count++;
3105 if (count != global_sym_count)
3106 errx(1, "internal error: write_rel: count = %d", count);
3108 each_full_file(assign_symbolnums, &count);
3110 /* Write out the relocations of all files, remembered from copy_text. */
3111 each_full_file(coptxtrel, 0);
3114 fprintf(stderr, "\nWriting data relocation:\n\n");
3116 each_full_file(copdatrel, 0);
3119 fprintf(stderr, "\n");
3124 * Assign symbol ordinal numbers to local symbols in each entry.
3127 assign_symbolnums(entry, countp)
3128 struct file_entry *entry;
3131 struct localsymbol *lsp, *lspend;
3134 lspend = entry->symbols + entry->nsymbols;
3136 if (discard_locals != DISCARD_ALL)
3137 /* Count the N_FN symbol for this entry */
3140 for (lsp = entry->symbols; lsp < lspend; lsp++) {
3141 if (lsp->flags & LS_WRITE)
3142 lsp->symbolnum = n++;
3149 struct file_entry *entry;
3151 register struct relocation_info *r, *end;
3152 register int reloc = entry->text_start_address;
3155 end = r + entry->ntextrel;
3157 for (; r < end; r++) {
3158 register int symindex;
3159 struct localsymbol *lsp;
3162 RELOC_ADDRESS(r) += reloc;
3164 symindex = RELOC_SYMBOL(r);
3165 lsp = &entry->symbols[symindex];
3167 if (!RELOC_EXTERN_P(r)) {
3170 if (RELOC_BASEREL_P(r))
3171 RELOC_SYMBOL(r) = lsp->symbolnum;
3175 if (symindex >= entry->nsymbols)
3176 errx(1, "%s: relocation symbolnum out of range",
3177 get_file_name(entry));
3182 /* Resolve indirection. */
3183 if ((sp->defined & ~N_EXT) == N_INDR) {
3184 if (sp->alias == NULL)
3185 errx(1, "internal error: alias in hyperspace");
3191 * If the symbol is now defined, change the external
3192 * relocation to an internal one.
3196 if (!pic_code_seen) {
3197 RELOC_EXTERN_P(r) = 0;
3198 RELOC_SYMBOL(r) = (sp->defined & N_TYPE);
3200 RELOC_SYMBOL(r) = sp->symbolnum;
3203 * Global symbols come first.
3205 RELOC_SYMBOL(r) = sp->symbolnum;
3207 md_swapout_reloc(entry->textrel, entry->ntextrel);
3208 mywrite(entry->textrel, entry->ntextrel,
3209 sizeof(struct relocation_info), outstream);
3214 struct file_entry *entry;
3216 register struct relocation_info *r, *end;
3218 * Relocate the address of the relocation. Old address is relative to
3219 * start of the input file's data section. New address is relative to
3220 * start of the output file's data section.
3222 register int reloc = entry->data_start_address - text_size;
3225 end = r + entry->ndatarel;
3227 for (; r < end; r++) {
3228 register int symindex;
3232 RELOC_ADDRESS(r) += reloc;
3234 if (!RELOC_EXTERN_P(r)) {
3235 if (RELOC_BASEREL_P(r))
3236 errx(1, "%s: Unsupported relocation type",
3237 get_file_name(entry));
3241 symindex = RELOC_SYMBOL(r);
3242 sp = entry->symbols[symindex].symbol;
3244 if (symindex >= entry->header.a_syms)
3245 errx(1, "%s: relocation symbolnum out of range",
3246 get_file_name(entry));
3249 /* Resolve indirection. */
3250 if ((sp->defined & ~N_EXT) == N_INDR) {
3251 if (sp->alias == NULL)
3252 errx(1, "internal error: alias in hyperspace");
3257 symtype = sp->defined & N_TYPE;
3259 if (!pic_code_seen && ( symtype == N_BSS ||
3260 symtype == N_DATA ||
3261 symtype == N_TEXT ||
3262 symtype == N_ABS)) {
3263 RELOC_EXTERN_P(r) = 0;
3264 RELOC_SYMBOL(r) = symtype;
3267 * Global symbols come first.
3270 entry->symbols[symindex].symbol->symbolnum;
3272 md_swapout_reloc(entry->datarel, entry->ndatarel);
3273 mywrite(entry->datarel, entry->ndatarel,
3274 sizeof(struct relocation_info), outstream);
3277 void write_file_syms __P((struct file_entry *, int *));
3278 void write_string_table __P((void));
3280 /* Offsets and current lengths of symbol and string tables in output file. */
3282 static int symtab_offset;
3283 static int symtab_len;
3285 /* Address in output file where string table starts. */
3286 static int strtab_offset;
3288 /* Offset within string table
3289 where the strings in `strtab_vector' should be written. */
3290 static int strtab_len;
3292 /* Total size of string table strings allocated so far,
3293 including strings in `strtab_vector'. */
3294 static int strtab_size;
3296 /* Vector whose elements are strings to be added to the string table. */
3297 static char **strtab_vector;
3299 /* Vector whose elements are the lengths of those strings. */
3300 static int *strtab_lens;
3302 /* Index in `strtab_vector' at which the next string will be stored. */
3303 static int strtab_index;
3306 * Add the string NAME to the output file string table. Record it in
3307 * `strtab_vector' to be output later. Return the index within the string
3308 * table that this string will have.
3312 assign_string_table_index(name)
3315 register int index = strtab_size;
3316 register int len = strlen(name) + 1;
3319 strtab_vector[strtab_index] = name;
3320 strtab_lens[strtab_index++] = len;
3326 * Write the contents of `strtab_vector' into the string table. This is done
3327 * once for each file's local&debugger symbols and once for the global
3331 write_string_table()
3335 if (fseek(outstream, strtab_offset + strtab_len, SEEK_SET) != 0)
3336 err(1, "write_string_table: %s: fseek", output_filename);
3338 for (i = 0; i < strtab_index; i++) {
3339 mywrite(strtab_vector[i], strtab_lens[i], 1, outstream);
3340 strtab_len += strtab_lens[i];
3344 /* Write the symbol table and string table of the output file. */
3349 /* Number of symbols written so far. */
3350 int syms_written = 0;
3354 * Buffer big enough for all the global symbols. One extra struct
3355 * for each indirect symbol to hold the extra reference following.
3357 struct nlist *buf = (struct nlist *)
3358 alloca(global_sym_count * sizeof(struct nlist));
3359 /* Pointer for storing into BUF. */
3360 register struct nlist *bufp = buf;
3362 /* Size of string table includes the bytes that store the size. */
3363 strtab_size = sizeof strtab_size;
3365 symtab_offset = N_SYMOFF(outheader);
3367 strtab_offset = N_STROFF(outheader);
3368 strtab_len = strtab_size;
3370 if (strip_symbols == STRIP_ALL)
3373 /* First, write out the global symbols. */
3376 * Allocate two vectors that record the data to generate the string
3377 * table from the global symbols written so far. This must include
3378 * extra space for the references following indirect outputs.
3381 strtab_vector = (char **)alloca((global_sym_count) * sizeof(char *));
3382 strtab_lens = (int *)alloca((global_sym_count) * sizeof(int));
3386 * __DYNAMIC symbol *must* be first for Sun compatibility, as Sun's
3387 * ld.so reads the shared object's first symbol. This means that
3388 * (Sun's) shared libraries cannot be stripped! (We only assume
3389 * that __DYNAMIC is the first item in the data segment)
3391 * If defined (ie. not relocatable_output), make it look
3392 * like an internal symbol.
3394 if (dynamic_symbol->flags & GS_REFERENCED) {
3397 nl.n_type = dynamic_symbol->defined;
3398 if (nl.n_type == N_UNDF)
3401 nl.n_type &= ~N_EXT;
3402 nl.n_value = dynamic_symbol->value;
3403 nl.n_un.n_strx = assign_string_table_index(dynamic_symbol->name);
3408 /* Scan the symbol hash table, bucket by bucket. */
3410 FOR_EACH_SYMBOL(i, sp) {
3412 if (sp == dynamic_symbol)
3413 /* Already dealt with above */
3417 * Propagate N_WARNING symbols.
3419 if ((relocatable_output || building_shared_object)
3421 nl.n_type = N_WARNING;
3422 nl.n_un.n_strx = assign_string_table_index(sp->warning);
3429 nl.n_type = N_UNDF + N_EXT;
3430 nl.n_un.n_strx = assign_string_table_index(sp->name);
3438 if (!(sp->flags & GS_REFERENCED))
3439 /* Came from shared object but was not used */
3442 if (sp->so_defined || (sp->alias && sp->alias->so_defined))
3444 * Definition came from shared object,
3445 * don't mention it here
3449 if (!sp->defined && !relocatable_output) {
3451 * We're building a shared object and there
3452 * are still undefined symbols. Don't output
3453 * these, symbol was discounted in digest_pass1()
3454 * (they are in the RRS symbol table).
3456 if (building_shared_object)
3458 if (!(sp->flags & GS_WEAK))
3459 warnx("symbol %s remains undefined", demangle(sp->name));
3462 if (syms_written >= global_sym_count)
3464 "internal error: number of symbols exceeds alloc'd %d",
3468 * Construct a `struct nlist' for the symbol.
3473 if (sp->defined > 1) {
3475 * defined with known type
3477 if (!relocatable_output && !building_shared_object &&
3478 sp->alias && sp->alias->defined > 1) {
3480 * If the target of an indirect symbol has
3481 * been defined and we are outputting an
3482 * executable, resolve the indirection; it's
3485 nl.n_type = sp->alias->defined;
3486 nl.n_value = sp->alias->value;
3487 nl.n_other = N_OTHER(0, sp->alias->aux);
3491 if (sp->defined == N_SIZE)
3492 nl.n_type = N_DATA | N_EXT;
3494 nl.n_type = sp->defined;
3495 if (nl.n_type == (N_INDR|N_EXT) &&
3497 errx(1, "%s: N_INDR has value %#lx",
3498 demangle(sp->name), sp->value);
3499 nl.n_value = sp->value;
3501 bind = N_BIND(&sp->def_lsp->nzlist.nlist);
3502 nl.n_other = N_OTHER(bind, sp->aux);
3505 } else if (sp->common_size) {
3507 * defined as common but not allocated,
3508 * happens only with -r and not -d, write out
3509 * a common definition.
3511 * common condition needs to be before undefined
3512 * condition because unallocated commons are set
3513 * undefined in digest_symbols.
3515 nl.n_type = N_UNDF | N_EXT;
3516 nl.n_value = sp->common_size;
3517 } else if (!sp->defined) {
3518 /* undefined -- legit only if -r */
3519 nl.n_type = N_UNDF | N_EXT;
3523 "internal error: %s defined in mysterious way",
3524 demangle(sp->name));
3527 * Allocate string table space for the symbol name.
3530 nl.n_un.n_strx = assign_string_table_index(sp->name);
3532 /* Output to the buffer and count it. */
3538 * Write second symbol of an alias pair.
3540 if (nl.n_type == N_INDR + N_EXT) {
3541 if (sp->alias == NULL)
3542 errx(1, "internal error: alias in hyperspace");
3543 nl.n_type = N_UNDF + N_EXT;
3545 assign_string_table_index(sp->alias->name);
3554 * Write N_SIZE symbol for a symbol with a known size.
3556 if (relocatable_output && sp->size) {
3557 nl.n_type = N_SIZE + N_EXT;
3558 nl.n_un.n_strx = assign_string_table_index(sp->name);
3559 nl.n_value = sp->size;
3567 printf("writesym(#%d): %s, type %x\n", syms_written, demangle(sp->name), sp->defined);
3571 if (syms_written != strtab_index || strtab_index != global_sym_count)
3572 errx(1, "internal error: wrong number (%d) of global symbols "
3573 "written into output file, should be %d",
3574 syms_written, global_sym_count);
3576 /* Output the buffer full of `struct nlist's. */
3578 if (fseek(outstream, symtab_offset + symtab_len, SEEK_SET) != 0)
3579 err(1, "write_syms: fseek");
3580 md_swapout_symbols(buf, bufp - buf);
3581 mywrite(buf, bufp - buf, sizeof(struct nlist), outstream);
3582 symtab_len += sizeof(struct nlist) * (bufp - buf);
3584 /* Write the strings for the global symbols. */
3585 write_string_table();
3587 /* Write the local symbols defined by the various files. */
3588 each_file(write_file_syms, (void *)&syms_written);
3591 if (syms_written != nsyms)
3592 errx(1, "internal error: wrong number of symbols (%d) "
3593 "written into output file, should be %d",
3594 syms_written, nsyms);
3596 if (symtab_offset + symtab_len != strtab_offset)
3598 "internal error: inconsistent symbol table length: %d vs %d",
3599 symtab_offset + symtab_len, strtab_offset);
3601 if (fseek(outstream, strtab_offset, SEEK_SET) != 0)
3602 err(1, "write_syms: fseek");
3603 strtab_size = md_swap_long(strtab_size);
3604 mywrite(&strtab_size, sizeof(int), 1, outstream);
3609 * Write the local and debugger symbols of file ENTRY. Increment
3610 * *SYMS_WRITTEN_ADDR for each symbol that is written.
3614 * Note that we do not combine identical names of local symbols. dbx or gdb
3615 * would be confused if we did that.
3618 write_file_syms(entry, syms_written_addr)
3619 struct file_entry *entry;
3620 int *syms_written_addr;
3622 struct localsymbol *lsp, *lspend;
3624 /* Upper bound on number of syms to be written here. */
3625 int max_syms = entry->nsymbols + 1;
3628 * Buffer to accumulate all the syms before writing them. It has one
3629 * extra slot for the local symbol we generate here.
3631 struct nlist *buf = (struct nlist *)
3632 alloca(max_syms * sizeof(struct nlist));
3634 register struct nlist *bufp = buf;
3636 if (entry->flags & E_DYNAMIC)
3640 * Make tables that record, for each symbol, its name and its name's
3641 * length. The elements are filled in by `assign_string_table_index'.
3644 strtab_vector = (char **)alloca(max_syms * sizeof(char *));
3645 strtab_lens = (int *)alloca(max_syms * sizeof(int));
3648 /* Generate a local symbol for the start of this file's text. */
3650 if (discard_locals != DISCARD_ALL) {
3653 nl.n_type = N_FN | N_EXT;
3655 assign_string_table_index(entry->local_sym_name);
3656 nl.n_value = entry->text_start_address;
3660 (*syms_written_addr)++;
3662 /* Read the file's string table. */
3664 entry->strings = (char *)alloca(entry->string_size);
3665 read_entry_strings(file_open(entry), entry);
3667 lspend = entry->symbols + entry->nsymbols;
3669 for (lsp = entry->symbols; lsp < lspend; lsp++) {
3670 register struct nlist *p = &lsp->nzlist.nlist;
3673 if (!(lsp->flags & LS_WRITE))
3676 if (discard_locals == DISCARD_ALL ||
3677 (discard_locals == DISCARD_L &&
3678 (lsp->flags & LS_L_SYMBOL))) {
3680 * The user wants to discard this symbol, but it
3681 * is referenced by a relocation. We can still
3682 * save some file space by suppressing the unique
3683 * renaming of the symbol.
3685 lsp->flags &= ~LS_RENAME;
3688 if (p->n_un.n_strx == 0)
3690 else if (!(lsp->flags & LS_RENAME))
3691 name = p->n_un.n_strx + entry->strings;
3693 char *cp = p->n_un.n_strx + entry->strings;
3694 name = (char *)alloca(
3695 strlen(entry->local_sym_name) +
3697 (void)sprintf(name, "%s.%s", entry->local_sym_name, cp);
3701 * If this symbol has a name, allocate space for it
3702 * in the output string table.
3706 p->n_un.n_strx = assign_string_table_index(name);
3708 /* Output this symbol to the buffer and count it. */
3711 (*syms_written_addr)++;
3714 /* All the symbols are now in BUF; write them. */
3716 if (fseek(outstream, symtab_offset + symtab_len, SEEK_SET) != 0)
3717 err(1, "write local symbols: fseek");
3718 md_swapout_symbols(buf, bufp - buf);
3719 mywrite(buf, bufp - buf, sizeof(struct nlist), outstream);
3720 symtab_len += sizeof(struct nlist) * (bufp - buf);
3723 * Write the string-table data for the symbols just written, using
3724 * the data in vectors `strtab_vector' and `strtab_lens'.
3727 write_string_table();
3728 entry->strings = 0; /* Since it will disappear anyway. */
3732 * Parse the string ARG using scanf format FORMAT, and return the result.
3733 * If it does not parse, report fatal error
3734 * generating the error message using format string ERROR and ARG as arg.
3738 parse(arg, format, error)
3739 char *arg, *format, *error;
3743 if (1 != sscanf(arg, format, &x))
3744 errx(1, error, arg);
3749 * Output COUNT*ELTSIZE bytes of data at BUF to the descriptor FD.
3752 mywrite(buf, count, eltsize, fd)
3759 if (fwrite(buf, eltsize, count, fd) != count)
3766 struct stat statbuf;
3771 if (fstat(fileno(outstream), &statbuf) == 0) {
3772 if (S_ISREG(statbuf.st_mode))
3773 (void)unlink(output_filename);
3778 * Output PADDING zero-bytes to descriptor FD.
3779 * PADDING may be negative; in that case, do nothing.
3782 padfile(padding, fd)
3790 buf = (char *)alloca(padding);
3791 bzero(buf, padding);
3792 mywrite(buf, padding, 1, fd);
3801 for (i = 0; i < number_of_files; i++) {
3802 register struct file_entry *entry = &file_table[i];
3805 if (entry->flags & E_SEARCH_DIRS)
3806 fd = findlib(entry);
3808 fd = open(entry->filename, O_RDONLY, 0);
3815 * Print the name even if the file doesn't exist except in
3816 * the -lfoo case. This allows `ld -f' to work as well as
3817 * possible when it is used to generate dependencies before
3818 * the libraries exist.
3820 if (fd >= 0 || !(entry->flags & E_SEARCH_DIRS))
3821 printf("%s\n", entry->filename);