1 /* Copyright (C) 1991 Free Software Foundation, Inc.
4 This file is part of GLD, the Gnu Linker.
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
41 extern boolean lang_has_input_file;
42 extern boolean trace_files;
47 char *output_filename = "a.out";
49 /* Name this program was invoked by. */
52 /* The file that we're creating */
55 extern boolean option_v;
57 /* set if -y on the command line */
60 /* The local symbol prefix */
63 /* Count the number of global symbols multiply defined. */
64 int multiple_def_count;
66 /* Count the number of symbols defined through common declarations.
67 This count is referenced in symdef_library, linear_library, and
68 modified by enter_global_ref.
70 It is incremented when a symbol is created as a common, and
71 decremented when the common declaration is overridden
73 Another way of thinking of it is that this is a count of
74 all ldsym_types with a ->scoms field */
76 unsigned int commons_pending;
78 /* Count the number of global symbols referenced and not defined.
79 common symbols are not included in this count. */
81 unsigned int undefined_global_sym_count;
83 /* Count the number of warning symbols encountered. */
86 /* have we had a load script ? */
87 extern boolean had_script;
89 /* Nonzero means print names of input files as processed. */
92 /* 1 => write load map. */
99 /* Indicates whether output file will be b.out (default) or coff */
100 enum target_flavour output_flavor = BFD_BOUT_FORMAT;
104 /* Force the make_executable to be output, even if there are non-fatal
106 boolean force_make_executable;
108 /* A count of the total number of local symbols ever seen - by adding
109 the symbol_count field of each newly read afile.*/
111 unsigned int total_symbols_seen;
113 /* A count of the number of read files - the same as the number of elements
116 unsigned int total_files_seen;
119 args_type command_line;
120 ld_config_type config;
128 program_name = argv[0];
129 output_filename = "a.out";
137 check_v960 (argc, argv);
138 emulation = "gld960";
139 for (i = 1; i < argc; i++)
141 if (!strcmp (argv[i], "-Fcoff"))
143 emulation = "lnk960";
144 output_flavor = BFD_COFF_FORMAT;
150 emulation = (char *) getenv (EMULATION_ENVIRON);
153 /* Initialize the data about options. */
158 config.relocateable_output = false;
160 command_line.force_common_definition = false;
162 init_bfd_error_vector ();
164 ldfile_add_arch ("");
166 config.make_executable = true;
167 force_make_executable = false;
170 /* Initialize the cumulative counts of symbols. */
171 undefined_global_sym_count = 0;
173 multiple_def_count = 0;
176 config.magic_demand_paged = true;
177 config.text_read_only = true;
178 config.make_executable = true;
179 if (emulation == (char *) NULL)
181 emulation = DEFAULT_EMULATION;
184 ldemul_choose_mode (emulation);
185 default_target = ldemul_choose_target ();
187 ldemul_before_parse ();
188 lang_has_input_file = false;
189 parse_args (argc, argv);
191 if (config.relocateable_output && command_line.relax)
193 einfo ("%P%F: -relax and -r may not be used together\n");
199 info ("%P: mode %s\n", emulation);
201 if (lang_has_input_file == false)
203 einfo ("%P%F: No input files\n");
206 ldemul_after_parse ();
209 if (config.map_filename)
211 if (strcmp (config.map_filename, "-") == 0)
213 config.map_file = stdout;
217 config.map_file = fopen (config.map_filename, FOPEN_WT);
218 if (config.map_file == (FILE *) NULL)
220 einfo ("%P%F: can't open map file %s\n",
221 config.map_filename);
229 /* Print error messages for any missing symbols, for any warning
230 symbols, and possibly multiple definitions */
233 if (config.text_read_only)
235 /* Look for a text section and mark the readonly attribute in it */
236 asection *found = bfd_get_section_by_name (output_bfd, ".text");
238 if (found != (asection *) NULL)
240 found->flags |= SEC_READONLY;
244 if (config.relocateable_output)
245 output_bfd->flags &= ~EXEC_P;
247 output_bfd->flags |= EXEC_P;
251 /* Even if we're producing relocateable output, some non-fatal errors should
252 be reported in the exit status. (What non-fatal errors, if any, do we
253 want to ignore for relocateable output?) */
255 if (config.make_executable == false && force_make_executable == false)
257 if (trace_files == true)
259 einfo ("%P: Link errors found, deleting executable `%s'\n",
263 if (output_bfd->iostream)
264 fclose ((FILE *) (output_bfd->iostream));
266 unlink (output_filename);
271 bfd_close (output_bfd);
278 Q_read_entry_symbols (desc, entry)
280 struct lang_input_statement_struct *entry;
282 if (entry->asymbols == (asymbol **) NULL)
284 bfd_size_type table_size = get_symtab_upper_bound (desc);
286 entry->asymbols = (asymbol **) ldmalloc (table_size);
287 entry->symbol_count = bfd_canonicalize_symtab (desc, entry->asymbols);
292 * turn this item into a reference
299 asymbol *sym = *nlist_p;
303 sym->section = &bfd_und_section;
304 sym->udata = (PTR) (sp->srefs_chain);
305 sp->srefs_chain = nlist_p;
309 This function is called for each name which is seen which has a global
310 scope. It enters the name into the global symbol table in the correct
311 symbol on the correct chain. Remember that each ldsym_type has three
312 chains attatched, one of all definitions of a symbol, one of all
313 references of a symbol and one of all common definitions of a symbol.
315 When the function is over, the supplied is left connected to the bfd
316 to which is was born, with its udata field pointing to the next member
317 on the chain in which it has been inserted.
319 A certain amount of jigery pokery is necessary since commons come
320 along and upset things, we only keep one item in the common chain; the
321 one with the biggest size seen sofar. When another common comes along
322 it either bumps the previous definition into the ref chain, since it
323 is bigger, or gets turned into a ref on the spot since the one on the
324 common chain is already bigger. If a real definition comes along then
325 the common gets bumped off anyway.
327 Whilst all this is going on we keep a count of the number of multiple
328 definitions seen, undefined global symbols and pending commons.
331 extern boolean relaxing;
334 DEFUN (Q_enter_global_ref, (nlist_p, name),
335 asymbol ** nlist_p AND /* pointer into symbol table from incoming bfd */
336 CONST char *name /* name of symbol in linker table */ )
338 asymbol *sym = *nlist_p;
341 /* Lookup the name from the incoming bfd's symbol table in the
342 linker's global symbol table */
345 flagword this_symbol_flags = sym->flags;
347 sp = ldsym_get (name);
350 /* If this symbol already has udata, it means that something strange
353 The strange thing is that we've had an undefined symbol resolved by
354 an alias, but the thing the alias defined wasn't in the file. So
355 the symbol got a udata entry, but the file wasn't loaded. Then
356 later on the file was loaded, but we don't need to do this
364 if (flag_is_constructor (this_symbol_flags))
366 /* Add this constructor to the list we keep */
367 ldlang_add_constructor (sp);
368 /* Turn any commons into refs */
369 if (sp->scoms_chain != (asymbol **) NULL)
371 refize (sp, sp->scoms_chain);
379 if (bfd_is_com_section (sym->section))
381 /* If we have a definition of this symbol already then
382 this common turns into a reference. Also we only
383 ever point to the largest common, so if we
384 have a common, but it's bigger that the new symbol
385 the turn this into a reference too. */
388 /* This is a common symbol, but we already have a definition
389 for it, so just link it into the ref chain as if
390 it were a reference */
391 refize (sp, nlist_p);
393 else if (sp->scoms_chain)
395 /* If we have a previous common, keep only the biggest */
396 if ((*(sp->scoms_chain))->value > sym->value)
398 /* other common is bigger, throw this one away */
399 refize (sp, nlist_p);
401 else if (sp->scoms_chain != nlist_p)
403 /* other common is smaller, throw that away */
404 refize (sp, sp->scoms_chain);
405 sp->scoms_chain = nlist_p;
410 /* This is the first time we've seen a common, so remember it
411 - if it was undefined before, we know it's defined now. If
412 the symbol has been marked as really being a constructor,
413 then treat this as a ref
415 if (sp->flags & SYM_CONSTRUCTOR)
417 /* Turn this into a ref */
418 refize (sp, nlist_p);
422 /* treat like a common */
424 undefined_global_sym_count--;
427 sp->scoms_chain = nlist_p;
432 else if (sym->section != &bfd_und_section)
434 /* This is the definition of a symbol, add to def chain */
435 if (sp->sdefs_chain && (*(sp->sdefs_chain))->section != sym->section)
437 /* Multiple definition */
438 asymbol *sy = *(sp->sdefs_chain);
439 lang_input_statement_type *stat =
440 (lang_input_statement_type *) bfd_asymbol_bfd(sy)->usrdata;
441 lang_input_statement_type *stat1 =
442 (lang_input_statement_type *) bfd_asymbol_bfd(sym)->usrdata;
443 asymbol **stat1_symbols = stat1 ? stat1->asymbols : 0;
444 asymbol **stat_symbols = stat ? stat->asymbols : 0;
446 multiple_def_count++;
447 einfo ("%X%C: multiple definition of `%T'\n",
448 bfd_asymbol_bfd(sym), sym->section, stat1_symbols, sym->value, sym);
450 einfo ("%X%C: first seen here\n",
451 bfd_asymbol_bfd(sy), sy->section, stat_symbols, sy->value);
455 sym->udata = (PTR) (sp->sdefs_chain);
456 sp->sdefs_chain = nlist_p;
458 /* A definition overrides a common symbol */
461 refize (sp, sp->scoms_chain);
465 else if (sp->srefs_chain && relaxing == false)
467 /* If previously was undefined, then remember as defined */
468 undefined_global_sym_count--;
473 if (sp->scoms_chain == (asymbol **) NULL
474 && sp->srefs_chain == (asymbol **) NULL
475 && sp->sdefs_chain == (asymbol **) NULL)
477 /* And it's the first time we've seen it */
478 undefined_global_sym_count++;
482 refize (sp, nlist_p);
486 ASSERT (sp->sdefs_chain == 0 || sp->scoms_chain == 0);
487 ASSERT (sp->scoms_chain == 0 || (*(sp->scoms_chain))->udata == 0);
493 Q_enter_file_symbols (entry)
494 lang_input_statement_type *entry;
498 entry->common_section =
499 bfd_make_section_old_way (entry->the_bfd, "COMMON");
501 ldlang_add_file (entry);
504 if (trace_files || option_v)
506 info ("%I\n", entry);
509 total_symbols_seen += entry->symbol_count;
511 if (entry->symbol_count)
513 for (q = entry->asymbols; *q; q++)
517 if (had_y && p->name)
519 /* look up the symbol anyway to see if the trace bit was
521 ldsym_type *s = ldsym_get(p->name);
522 if (s->flags & SYM_Y)
524 einfo("%B: %s %T\n", entry->the_bfd,
525 p->section == &bfd_und_section ? "reference to" : "definition of ",
530 if (p->flags & BSF_INDIRECT)
534 else if (p->flags & BSF_WARNING)
538 else if (p->section == &bfd_und_section
539 || (p->flags & BSF_GLOBAL)
540 || bfd_is_com_section (p->section)
541 || (p->flags & BSF_CONSTRUCTOR))
547 if (p->flags & BSF_INDIRECT)
551 else if (p->flags & BSF_WARNING)
555 else if (p->section == &bfd_und_section
556 || (p->flags & BSF_GLOBAL)
557 || bfd_is_com_section (p->section)
558 || (p->flags & BSF_CONSTRUCTOR))
560 Q_enter_global_ref (q, p->name);
570 /* Searching libraries */
572 struct lang_input_statement_struct *decode_library_subfile ();
573 void linear_library (), symdef_library ();
575 /* Search the library ENTRY, already open on descriptor DESC.
576 This means deciding which library members to load,
577 making a chain of `struct lang_input_statement_struct' for those members,
578 and entering their global symbols in the hash table. */
581 search_library (entry)
582 struct lang_input_statement_struct *entry;
585 /* No need to load a library if no undefined symbols */
586 if (!undefined_global_sym_count)
589 if (bfd_has_map (entry->the_bfd))
590 symdef_library (entry);
592 linear_library (entry);
599 gnu960_check_format (abfd, format)
605 if ((bfd_check_format (abfd, format) == true)
606 && (abfd->xvec->flavour == output_flavor))
618 ldmain_open_file_read_symbol (entry)
619 struct lang_input_statement_struct *entry;
621 if (entry->asymbols == (asymbol **) NULL
622 && entry->real == true
623 && entry->filename != (char *) NULL)
625 ldfile_open_file (entry);
629 if (gnu960_check_format (entry->the_bfd, bfd_object))
631 if (bfd_check_format (entry->the_bfd, bfd_object))
634 entry->the_bfd->usrdata = (PTR) entry;
637 Q_read_entry_symbols (entry->the_bfd, entry);
639 /* look through the sections in the file and see if any of them
641 ldlang_check_for_constructors (entry);
643 Q_enter_file_symbols (entry);
646 else if (gnu960_check_format (entry->the_bfd, bfd_archive))
648 else if (bfd_check_format (entry->the_bfd, bfd_archive))
651 entry->the_bfd->usrdata = (PTR) entry;
653 entry->subfiles = (lang_input_statement_type *) NULL;
654 search_library (entry);
658 einfo ("%F%B: malformed input file (not rel or archive) \n",
665 /* Construct and return a lang_input_statement_struct for a library member.
666 The library's lang_input_statement_struct is library_entry,
667 and the library is open on DESC.
668 SUBFILE_OFFSET is the byte index in the library of this member's header.
669 We store the length of the member into *LENGTH_LOC. */
671 lang_input_statement_type *
672 decode_library_subfile (library_entry, subfile_offset)
673 struct lang_input_statement_struct *library_entry;
676 register struct lang_input_statement_struct *subentry;
679 /* First, check if we already have a loaded
680 lang_input_statement_struct for this library subfile. If so,
681 just return it. Otherwise, allocate some space and build a new one. */
683 if ( subfile_offset->usrdata
684 && ((struct lang_input_statement_struct *)subfile_offset->usrdata)->
687 subentry = (struct lang_input_statement_struct *)subfile_offset->usrdata;
692 (struct lang_input_statement_struct *)
693 ldmalloc ((bfd_size_type) (sizeof (struct lang_input_statement_struct)));
695 subentry->filename = subfile_offset->filename;
696 subentry->local_sym_name = subfile_offset->filename;
697 subentry->asymbols = 0;
698 subentry->the_bfd = subfile_offset;
699 subentry->subfiles = 0;
701 subentry->superfile = library_entry;
702 subentry->is_archive = false;
704 subentry->just_syms_flag = false;
705 subentry->loaded = false;
711 boolean subfile_wanted_p ();
713 clear_syms (entry, offset)
714 struct lang_input_statement_struct *entry;
718 unsigned long indx = bfd_get_next_mapent (entry->the_bfd,
722 while (indx != BFD_NO_MORE_SYMBOLS)
724 if (car->file_offset == offset)
728 indx = bfd_get_next_mapent (entry->the_bfd, indx, &car);
733 /* Search a library that has a map
736 symdef_library (entry)
737 struct lang_input_statement_struct *entry;
740 register struct lang_input_statement_struct *prev = 0;
742 boolean not_finished = true;
744 while (not_finished == true)
746 carsym *exported_library_name;
747 bfd *prev_archive_member_bfd = 0;
749 int idx = bfd_get_next_mapent (entry->the_bfd,
751 &exported_library_name);
753 not_finished = false;
755 while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count)
758 if (exported_library_name->name)
761 ldsym_type *sp = ldsym_get_soft (exported_library_name->name);
763 /* If we find a symbol that appears to be needed, think carefully
764 about the archive member that the symbol is in. */
765 /* So - if it exists, and is referenced somewhere and is
767 if (sp && sp->srefs_chain && !sp->sdefs_chain)
769 bfd *archive_member_bfd = bfd_get_elt_at_index (entry->the_bfd, idx);
770 struct lang_input_statement_struct *archive_member_lang_input_statement_struct;
773 if (archive_member_bfd && gnu960_check_format (archive_member_bfd, bfd_object))
775 if (archive_member_bfd && bfd_check_format (archive_member_bfd, bfd_object))
779 /* Don't think carefully about any archive member
780 more than once in a given pass. */
781 if (prev_archive_member_bfd != archive_member_bfd)
784 prev_archive_member_bfd = archive_member_bfd;
786 /* Read the symbol table of the archive member. */
788 if (archive_member_bfd->usrdata != (PTR) NULL)
791 archive_member_lang_input_statement_struct = (lang_input_statement_type *) archive_member_bfd->usrdata;
796 archive_member_lang_input_statement_struct =
797 decode_library_subfile (entry, archive_member_bfd);
798 archive_member_bfd->usrdata = (PTR) archive_member_lang_input_statement_struct;
802 if (archive_member_lang_input_statement_struct == 0)
804 einfo ("%F%I contains invalid archive member %s\n",
808 if (archive_member_lang_input_statement_struct->loaded == false)
811 Q_read_entry_symbols (archive_member_bfd, archive_member_lang_input_statement_struct);
812 /* Now scan the symbol table and decide whether to load. */
815 if (subfile_wanted_p (archive_member_lang_input_statement_struct) == true)
818 /* This member is needed; load it.
819 Since we are loading something on this pass,
820 we must make another pass through the symdef data. */
824 Q_enter_file_symbols (archive_member_lang_input_statement_struct);
827 prev->chain = archive_member_lang_input_statement_struct;
829 entry->subfiles = archive_member_lang_input_statement_struct;
832 prev = archive_member_lang_input_statement_struct;
835 /* Clear out this member's symbols from the symdef data
836 so that following passes won't waste time on them. */
837 clear_syms (entry, exported_library_name->file_offset);
838 archive_member_lang_input_statement_struct->loaded = true;
845 idx = bfd_get_next_mapent (entry->the_bfd, idx, &exported_library_name);
851 linear_library (entry)
852 struct lang_input_statement_struct *entry;
854 boolean more_to_do = true;
855 register struct lang_input_statement_struct *prev = 0;
857 if (entry->complained == false)
859 if (entry->the_bfd->xvec->flavour != bfd_target_ieee_flavour)
862 /* IEEE can use table of contents, so this message is bogus */
863 einfo ("%P: library %s has bad table of contents, rerun ranlib\n",
864 entry->the_bfd->filename);
866 entry->complained = true;
872 bfd *archive = bfd_openr_next_archived_file (entry->the_bfd, 0);
877 /* Don't check this file if it's already been read in
880 if (!archive->usrdata ||
881 ! ((lang_input_statement_type *)(archive->usrdata))->loaded)
884 if (gnu960_check_format (archive, bfd_object))
886 if (bfd_check_format (archive, bfd_object))
889 register struct lang_input_statement_struct *subentry;
891 subentry = decode_library_subfile (entry,
894 archive->usrdata = (PTR) subentry;
897 if (subentry->loaded == false)
899 Q_read_entry_symbols (archive, subentry);
901 if (subfile_wanted_p (subentry) == true)
903 Q_enter_file_symbols (subentry);
906 prev->chain = subentry;
908 entry->subfiles = subentry;
912 subentry->loaded = true;
917 archive = bfd_openr_next_archived_file (entry->the_bfd, archive);
924 /* ENTRY is an entry for a file inside an archive
925 Its symbols have been read into core, but not entered into the
927 Return nonzero if we ought to load this file */
930 subfile_wanted_p (entry)
931 struct lang_input_statement_struct *entry;
935 for (q = entry->asymbols; *q; q++)
939 /* If the symbol has an interesting definition, we could
940 potentially want it. */
942 if (p->flags & BSF_INDIRECT)
944 /** add_indirect(q);*/
947 if (bfd_is_com_section (p->section)
948 || (p->flags & BSF_GLOBAL)
949 || (p->flags & BSF_INDIRECT))
951 register ldsym_type *sp = ldsym_get_soft (p->name);
953 /* If this symbol has not been hashed,
954 we can't be looking for it. */
955 if (sp != (ldsym_type *) NULL
956 && sp->sdefs_chain == (asymbol **) NULL)
958 if (sp->srefs_chain != (asymbol **) NULL
959 || sp->scoms_chain != (asymbol **) NULL)
961 /* This is a symbol we are looking for. It is
962 either not yet defined or common. If this is a
963 common symbol, then if the symbol in the object
964 file is common, we need to combine sizes. But if
965 we already have a common symbol, and the symbol
966 in the object file is not common, we don't want
967 the object file: it is providing a definition for
968 a symbol that we already have a definition for
969 (this is the else condition below). */
970 if (bfd_is_com_section (p->section))
973 /* If the symbol in the table is a constructor, we won't to
974 anything fancy with it */
975 if ((sp->flags & SYM_CONSTRUCTOR) == 0)
977 /* This libary member has something to
978 say about this element. We should
979 remember if its a new size */
980 /* Move something from the ref list to the com list */
983 /* Already a common symbol, maybe update it */
984 if (p->value > (*(sp->scoms_chain))->value)
986 (*(sp->scoms_chain))->value = p->value;
991 /* Take a value from the ref chain
992 Here we are moving a symbol from the owning bfd
993 to another bfd. We must set up the
994 common_section portion of the bfd thing */
998 sp->scoms_chain = sp->srefs_chain;
1000 (asymbol **) ((*(sp->srefs_chain))->udata);
1001 (*(sp->scoms_chain))->udata = (PTR) NULL;
1003 (*(sp->scoms_chain))->section =
1005 (*(sp->scoms_chain))->flags = 0;
1006 /* Remember the size of this item */
1007 sp->scoms_chain[0]->value = p->value;
1009 undefined_global_sym_count--;
1012 asymbol *com = *(sp->scoms_chain);
1014 if (((lang_input_statement_type *)
1015 (bfd_asymbol_bfd(com)->usrdata))->common_section ==
1018 ((lang_input_statement_type *)
1019 (bfd_asymbol_bfd(com)->usrdata))->common_section =
1020 bfd_make_section_old_way (bfd_asymbol_bfd(com), "COMMON");
1024 ASSERT (p->udata == 0);
1026 else if (sp->scoms_chain == (asymbol **) NULL)
1030 info ("%I needed due to %s\n", entry, sp->name);
1046 ldsym_type *lookup = ldsym_get(text);
1047 lookup->flags |= SYM_Y;