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. */
42 extern boolean lang_has_input_file;
43 extern boolean trace_files;
47 char *output_filename = "a.out";
48 /* Name this program was invoked by. */
51 /* The file that we're creating */
54 extern boolean option_v;
56 /* The local symbol prefix */
59 /* Count the number of global symbols multiply defined. */
60 int multiple_def_count;
63 /* Count the number of symbols defined through common declarations.
64 This count is referenced in symdef_library, linear_library, and
65 modified by enter_global_ref.
67 It is incremented when a symbol is created as a common, and
68 decremented when the common declaration is overridden
70 Another way of thinking of it is that this is a count of
71 all ldsym_types with a ->scoms field */
73 unsigned int commons_pending;
75 /* Count the number of global symbols referenced and not defined.
76 common symbols are not included in this count. */
78 unsigned int undefined_global_sym_count;
82 /* Count the number of warning symbols encountered. */
85 /* have we had a load script ? */
86 extern boolean had_script;
88 /* Nonzero means print names of input files as processed. */
93 /* 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;
103 /* Force the make_executable to be output, even if there are non-fatal
105 boolean force_make_executable;
107 /* A count of the total number of local symbols ever seen - by adding
108 the symbol_count field of each newly read afile.*/
110 unsigned int total_symbols_seen;
112 /* A count of the number of read files - the same as the number of elements
115 unsigned int total_files_seen;
119 args_type command_line;
120 ld_config_type config;
127 program_name = argv[0];
128 output_filename = "a.out";
135 check_v960( argc, argv );
136 emulation = "gld960";
137 for ( i = 1; i < argc; i++ ){
138 if ( !strcmp(argv[i],"-Fcoff") ){
139 emulation = "lnk960";
140 output_flavor = BFD_COFF_FORMAT;
146 emulation = (char *) getenv(EMULATION_ENVIRON);
149 /* Initialize the data about options. */
153 config.relocateable_output = false;
155 command_line.force_common_definition = false;
157 init_bfd_error_vector();
160 ldfile_add_library_path("./");
161 config.make_executable = true;
162 force_make_executable = false;
165 /* Initialize the cumulative counts of symbols. */
166 undefined_global_sym_count = 0;
168 multiple_def_count = 0;
171 config.magic_demand_paged = true;
172 config.text_read_only = true;
173 config.make_executable = true;
174 if (emulation == (char *)NULL) {
175 emulation= DEFAULT_EMULATION;
178 ldemul_choose_mode(emulation);
179 default_target = ldemul_choose_target();
181 ldemul_before_parse();
182 lang_has_input_file = false;
183 parse_args(argc, argv);
186 info("%P: mode %s\n", emulation);
188 if (lang_has_input_file == false) {
189 einfo("%P%F: No input files\n");
192 ldemul_after_parse();
194 if (config.map_filename)
196 if (strcmp(config.map_filename, "-") == 0)
198 config.map_file = stdout;
201 config.map_file = fopen(config.map_filename, FOPEN_WT);
202 if (config.map_file == (FILE *)NULL)
204 einfo("%P%F: can't open map file %s\n",
205 config.map_filename);
213 /* Print error messages for any missing symbols, for any warning
214 symbols, and possibly multiple definitions */
217 if (config.text_read_only) {
218 /* Look for a text section and mark the readonly attribute in it */
219 asection *found = bfd_get_section_by_name(output_bfd, ".text");
220 if (found == (asection *)NULL) {
221 einfo("%P%F: text marked read only, but no text section present");
223 found->flags |= SEC_READONLY;
226 if (config.relocateable_output) {
227 output_bfd->flags &= ~EXEC_P;
230 bfd_close(output_bfd);
233 output_bfd->flags |= EXEC_P;
237 if (config.make_executable == false && force_make_executable ==false) {
239 unlink(output_filename);
241 else { bfd_close(output_bfd); };
242 exit (!config.make_executable);
250 Q_read_entry_symbols (desc, entry)
252 struct lang_input_statement_struct *entry;
254 if (entry->asymbols == (asymbol **)NULL) {
255 bfd_size_type table_size = get_symtab_upper_bound(desc);
256 entry->asymbols = (asymbol **)ldmalloc(table_size);
257 entry->symbol_count = bfd_canonicalize_symtab(desc, entry->asymbols) ;
263 * turn this item into a reference
270 asymbol *sym = *nlist_p;
273 sym->section = &bfd_und_section;
274 sym->udata =(PTR)( sp->srefs_chain);
275 sp->srefs_chain = nlist_p;
278 This function is called for each name which is seen which has a global
279 scope. It enters the name into the global symbol table in the correct
280 symbol on the correct chain. Remember that each ldsym_type has three
281 chains attatched, one of all definitions of a symbol, one of all
282 references of a symbol and one of all common definitions of a symbol.
284 When the function is over, the supplied is left connected to the bfd
285 to which is was born, with its udata field pointing to the next member
286 on the chain in which it has been inserted.
288 A certain amount of jigery pokery is necessary since commons come
289 along and upset things, we only keep one item in the common chain; the
290 one with the biggest size seen sofar. When another common comes along
291 it either bumps the previous definition into the ref chain, since it
292 is bigger, or gets turned into a ref on the spot since the one on the
293 common chain is already bigger. If a real definition comes along then
294 the common gets bumped off anyway.
296 Whilst all this is going on we keep a count of the number of multiple
297 definitions seen, undefined global symbols and pending commons.
300 extern boolean relaxing;
303 Q_enter_global_ref (nlist_p)
307 asymbol *sym = *nlist_p;
308 CONST char *name = sym->name;
309 ldsym_type *sp = ldsym_get (name);
311 flagword this_symbol_flags = sym->flags;
314 ASSERT(sym->udata == 0);
317 if (flag_is_constructor(this_symbol_flags)) {
318 /* Add this constructor to the list we keep */
319 ldlang_add_constructor(sp);
320 /* Turn any commons into refs */
321 if (sp->scoms_chain != (asymbol **)NULL) {
322 refize(sp, sp->scoms_chain);
329 if (sym->section == &bfd_com_section) {
330 /* If we have a definition of this symbol already then
331 this common turns into a reference. Also we only
332 ever point to the largest common, so if we
333 have a common, but it's bigger that the new symbol
334 the turn this into a reference too. */
337 /* This is a common symbol, but we already have a definition
338 for it, so just link it into the ref chain as if
339 it were a reference */
342 else if (sp->scoms_chain) {
343 /* If we have a previous common, keep only the biggest */
344 if ( (*(sp->scoms_chain))->value > sym->value) {
345 /* other common is bigger, throw this one away */
348 else if (sp->scoms_chain != nlist_p) {
349 /* other common is smaller, throw that away */
350 refize(sp, sp->scoms_chain);
351 sp->scoms_chain = nlist_p;
355 /* This is the first time we've seen a common, so remember it
356 - if it was undefined before, we know it's defined now. If
357 the symbol has been marked as really being a constructor,
358 then treat this as a ref
360 if (sp->flags & SYM_CONSTRUCTOR) {
361 /* Turn this into a ref */
365 /* treat like a common */
367 undefined_global_sym_count--;
370 sp->scoms_chain = nlist_p;
375 else if (sym->section != &bfd_und_section) {
376 /* This is the definition of a symbol, add to def chain */
377 if (sp->sdefs_chain && (*(sp->sdefs_chain))->section != sym->section) {
378 /* Multiple definition */
379 asymbol *sy = *(sp->sdefs_chain);
380 lang_input_statement_type *stat = (lang_input_statement_type *) sy->the_bfd->usrdata;
381 lang_input_statement_type *stat1 = (lang_input_statement_type *) sym->the_bfd->usrdata;
382 asymbol ** stat1_symbols = stat1 ? stat1->asymbols: 0;
383 asymbol ** stat_symbols = stat ? stat->asymbols:0;
385 multiple_def_count++;
386 einfo("%C: multiple definition of `%T'\n",
387 sym->the_bfd, sym->section, stat1_symbols, sym->value, sym);
389 einfo("%C: first seen here\n",
390 sy->the_bfd, sy->section, stat_symbols, sy->value);
393 sym->udata =(PTR)( sp->sdefs_chain);
394 sp->sdefs_chain = nlist_p;
396 /* A definition overrides a common symbol */
397 if (sp->scoms_chain) {
398 refize(sp, sp->scoms_chain);
402 else if (sp->srefs_chain && relaxing == false) {
403 /* If previously was undefined, then remember as defined */
404 undefined_global_sym_count--;
408 if (sp->scoms_chain == (asymbol **)NULL
409 && sp->srefs_chain == (asymbol **)NULL
410 && sp->sdefs_chain == (asymbol **)NULL) {
411 /* And it's the first time we've seen it */
412 undefined_global_sym_count++;
420 ASSERT(sp->sdefs_chain == 0 || sp->scoms_chain == 0);
421 ASSERT(sp->scoms_chain ==0 || (*(sp->scoms_chain))->udata == 0);
427 Q_enter_file_symbols (entry)
428 lang_input_statement_type *entry;
432 entry->common_section =
433 bfd_make_section_old_way(entry->the_bfd, "COMMON");
435 ldlang_add_file(entry);
438 if (trace_files || option_v) {
442 total_symbols_seen += entry->symbol_count;
444 for (q = entry->asymbols; *q; q++)
448 if (p->flags & BSF_INDIRECT)
452 else if (p->flags & BSF_WARNING)
457 else if (p->section == &bfd_und_section
458 || (p->flags & BSF_GLOBAL)
459 || p->section == &bfd_com_section
460 || (p->flags & BSF_CONSTRUCTOR))
462 Q_enter_global_ref(q);
471 /* Searching libraries */
473 struct lang_input_statement_struct *decode_library_subfile ();
474 void linear_library (), symdef_library ();
476 /* Search the library ENTRY, already open on descriptor DESC.
477 This means deciding which library members to load,
478 making a chain of `struct lang_input_statement_struct' for those members,
479 and entering their global symbols in the hash table. */
482 search_library (entry)
483 struct lang_input_statement_struct *entry;
486 /* No need to load a library if no undefined symbols */
487 if (!undefined_global_sym_count) return;
489 if (bfd_has_map(entry->the_bfd))
490 symdef_library (entry);
492 linear_library (entry);
500 gnu960_check_format (abfd, format)
506 if ((bfd_check_format(abfd,format) == true)
507 && (abfd->xvec->flavour == output_flavor) ){
517 ldmain_open_file_read_symbol (entry)
518 struct lang_input_statement_struct *entry;
520 if (entry->asymbols == (asymbol **)NULL
521 &&entry->real == true
522 && entry->filename != (char *)NULL)
524 ldfile_open_file (entry);
528 if (gnu960_check_format(entry->the_bfd, bfd_object))
530 if (bfd_check_format(entry->the_bfd, bfd_object))
533 entry->the_bfd->usrdata = (PTR)entry;
536 Q_read_entry_symbols (entry->the_bfd, entry);
538 /* look through the sections in the file and see if any of them
540 ldlang_check_for_constructors (entry);
542 Q_enter_file_symbols (entry);
545 else if (gnu960_check_format(entry->the_bfd, bfd_archive))
547 else if (bfd_check_format(entry->the_bfd, bfd_archive))
550 entry->the_bfd->usrdata = (PTR)entry;
552 entry->subfiles = (lang_input_statement_type *)NULL;
553 search_library (entry);
557 einfo("%F%B: malformed input file (not rel or archive) \n",
565 /* Construct and return a lang_input_statement_struct for a library member.
566 The library's lang_input_statement_struct is library_entry,
567 and the library is open on DESC.
568 SUBFILE_OFFSET is the byte index in the library of this member's header.
569 We store the length of the member into *LENGTH_LOC. */
571 lang_input_statement_type *
572 decode_library_subfile (library_entry, subfile_offset)
573 struct lang_input_statement_struct *library_entry;
576 register struct lang_input_statement_struct *subentry;
577 subentry = (struct lang_input_statement_struct *) ldmalloc ((bfd_size_type)(sizeof (struct lang_input_statement_struct)));
578 subentry->filename = subfile_offset -> filename;
579 subentry->local_sym_name = subfile_offset->filename;
580 subentry->asymbols = 0;
581 subentry->the_bfd = subfile_offset;
582 subentry->subfiles = 0;
584 subentry->superfile = library_entry;
585 subentry->is_archive = false;
587 subentry->just_syms_flag = false;
588 subentry->loaded = false;
594 boolean subfile_wanted_p ();
596 clear_syms(entry, offset)
597 struct lang_input_statement_struct *entry;
601 unsigned long indx = bfd_get_next_mapent(entry->the_bfd,
604 while (indx != BFD_NO_MORE_SYMBOLS) {
605 if (car->file_offset == offset) {
608 indx = bfd_get_next_mapent(entry->the_bfd, indx, &car);
613 /* Search a library that has a map
616 symdef_library (entry)
617 struct lang_input_statement_struct *entry;
620 register struct lang_input_statement_struct *prev = 0;
622 boolean not_finished = true;
625 while (not_finished == true)
627 carsym *exported_library_name;
628 bfd *prev_archive_member_bfd = 0;
630 int idx = bfd_get_next_mapent(entry->the_bfd,
632 &exported_library_name);
634 not_finished = false;
636 while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count)
639 if (exported_library_name->name)
642 ldsym_type *sp = ldsym_get_soft (exported_library_name->name);
644 /* If we find a symbol that appears to be needed, think carefully
645 about the archive member that the symbol is in. */
646 /* So - if it exists, and is referenced somewhere and is
648 if (sp && sp->srefs_chain && !sp->sdefs_chain)
650 bfd *archive_member_bfd = bfd_get_elt_at_index(entry->the_bfd, idx);
651 struct lang_input_statement_struct *archive_member_lang_input_statement_struct;
654 if (archive_member_bfd && gnu960_check_format(archive_member_bfd, bfd_object))
656 if (archive_member_bfd && bfd_check_format(archive_member_bfd, bfd_object))
660 /* Don't think carefully about any archive member
661 more than once in a given pass. */
662 if (prev_archive_member_bfd != archive_member_bfd)
665 prev_archive_member_bfd = archive_member_bfd;
667 /* Read the symbol table of the archive member. */
669 if (archive_member_bfd->usrdata != (PTR)NULL) {
671 archive_member_lang_input_statement_struct =(lang_input_statement_type *) archive_member_bfd->usrdata;
675 archive_member_lang_input_statement_struct =
676 decode_library_subfile (entry, archive_member_bfd);
677 archive_member_bfd->usrdata = (PTR) archive_member_lang_input_statement_struct;
681 if (archive_member_lang_input_statement_struct == 0) {
682 einfo ("%F%I contains invalid archive member %s\n",
686 if (archive_member_lang_input_statement_struct->loaded == false)
689 Q_read_entry_symbols (archive_member_bfd, archive_member_lang_input_statement_struct);
690 /* Now scan the symbol table and decide whether to load. */
693 if (subfile_wanted_p (archive_member_lang_input_statement_struct) == true)
696 /* This member is needed; load it.
697 Since we are loading something on this pass,
698 we must make another pass through the symdef data. */
702 Q_enter_file_symbols (archive_member_lang_input_statement_struct);
705 prev->chain = archive_member_lang_input_statement_struct;
707 entry->subfiles = archive_member_lang_input_statement_struct;
710 prev = archive_member_lang_input_statement_struct;
713 /* Clear out this member's symbols from the symdef data
714 so that following passes won't waste time on them. */
715 clear_syms(entry, exported_library_name->file_offset);
716 archive_member_lang_input_statement_struct->loaded = true;
723 idx = bfd_get_next_mapent(entry->the_bfd, idx, &exported_library_name);
729 linear_library (entry)
730 struct lang_input_statement_struct *entry;
732 boolean more_to_do = true;
733 register struct lang_input_statement_struct *prev = 0;
737 bfd * archive = bfd_openr_next_archived_file(entry->the_bfd,0);
742 if (gnu960_check_format(archive, bfd_object))
744 if (bfd_check_format(archive, bfd_object))
747 register struct lang_input_statement_struct *subentry;
749 subentry = decode_library_subfile (entry,
752 archive->usrdata = (PTR) subentry;
753 if (!subentry) return;
754 if (subentry->loaded == false) {
755 Q_read_entry_symbols (archive, subentry);
757 if (subfile_wanted_p (subentry) == true)
759 Q_enter_file_symbols (subentry);
762 prev->chain = subentry;
764 entry->subfiles = subentry;
768 subentry->loaded = true;
772 archive = bfd_openr_next_archived_file(entry->the_bfd,archive);
779 /* ENTRY is an entry for a library member.
780 Its symbols have been read into core, but not entered.
781 Return nonzero if we ought to load this member. */
784 subfile_wanted_p (entry)
785 struct lang_input_statement_struct *entry;
789 for (q = entry->asymbols; *q; q++)
793 /* If the symbol has an interesting definition, we could
794 potentially want it. */
796 if (p->flags & BSF_INDIRECT) {
797 /* Grab out the name we've indirected to, and keep the insides
802 if (p->section == &bfd_com_section
803 || p->flags & BSF_GLOBAL)
805 register ldsym_type *sp = ldsym_get_soft (p->name);
808 /* If this symbol has not been hashed,
809 we can't be looking for it. */
810 if (sp != (ldsym_type *)NULL
811 && sp->sdefs_chain == (asymbol **)NULL) {
812 if (sp->srefs_chain != (asymbol **)NULL
813 || sp->scoms_chain != (asymbol **)NULL)
815 /* This is a symbol we are looking for. It is either
816 not yet defined or common. */
818 if (p->section == &bfd_com_section)
821 /* If the symbol in the table is a constructor, we won't to
822 anything fancy with it */
823 if ((sp->flags & SYM_CONSTRUCTOR) == 0) {
824 /* This libary member has something to
825 say about this element. We should
826 remember if its a new size */
827 /* Move something from the ref list to the com list */
828 if(sp->scoms_chain) {
829 /* Already a common symbol, maybe update it */
830 if (p->value > (*(sp->scoms_chain))->value) {
831 (*(sp->scoms_chain))->value = p->value;
835 /* Take a value from the ref chain
836 Here we are moving a symbol from the owning bfd
837 to another bfd. We must set up the
838 common_section portion of the bfd thing */
842 sp->scoms_chain = sp->srefs_chain;
844 (asymbol **)((*(sp->srefs_chain))->udata);
845 (*(sp->scoms_chain))->udata = (PTR)NULL;
847 (*( sp->scoms_chain))->section =
849 (*( sp->scoms_chain))->flags = 0;
850 /* Remember the size of this item */
851 sp->scoms_chain[0]->value = p->value;
853 undefined_global_sym_count--;
855 asymbol *com = *(sp->scoms_chain);
856 if (((lang_input_statement_type *)
857 (com->the_bfd->usrdata))->common_section ==
859 ((lang_input_statement_type *)
860 (com->the_bfd->usrdata))->common_section =
861 bfd_make_section_old_way(com->the_bfd, "COMMON");
865 ASSERT(p->udata == 0);
871 info("%I needed due to %s\n",entry, sp->name);