1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 Written by Steve Chamberlain steve@cygnus.com
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_EMULATION_NAME;
137 for ( i = 1; i < argc; i++ ){
138 if ( !strcmp(argv[i],"-Fcoff") ){
139 emulation = LNK960_EMULATION_NAME;
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();
193 if (config.map_filename)
195 config.map_file = fopen(config.map_filename, FOPEN_WT);
196 if (config.map_file == (FILE *)NULL)
198 einfo("%P%F: can't open map file %s\n",
199 config.map_filename);
202 else config.map_file = stdout;
206 /* Print error messages for any missing symbols, for any warning
207 symbols, and possibly multiple definitions */
210 if (config.text_read_only) {
211 /* Look for a text section and mark the readonly attribute in it */
212 asection *found = bfd_get_section_by_name(output_bfd, ".text");
213 if (found == (asection *)NULL) {
214 einfo("%P%F: text marked read only, but no text section present");
216 found->flags |= SEC_READONLY;
219 if (config.relocateable_output) {
220 output_bfd->flags &= ~EXEC_P;
223 bfd_close(output_bfd);
226 output_bfd->flags |= EXEC_P;
230 if (config.make_executable == false && force_make_executable ==false) {
232 unlink(output_filename);
234 else { bfd_close(output_bfd); };
235 return (!config.make_executable);
243 Q_read_entry_symbols (desc, entry)
245 struct lang_input_statement_struct *entry;
247 if (entry->asymbols == (asymbol **)NULL) {
248 bfd_size_type table_size = get_symtab_upper_bound(desc);
249 entry->asymbols = (asymbol **)ldmalloc(table_size);
250 entry->symbol_count = bfd_canonicalize_symtab(desc, entry->asymbols) ;
256 * turn this item into a reference
263 asymbol *sym = *nlist_p;
266 sym->section = &bfd_und_section;
267 sym->udata =(PTR)( sp->srefs_chain);
268 sp->srefs_chain = nlist_p;
271 This function is called for each name which is seen which has a global
272 scope. It enters the name into the global symbol table in the correct
273 symbol on the correct chain. Remember that each ldsym_type has three
274 chains attatched, one of all definitions of a symbol, one of all
275 references of a symbol and one of all common definitions of a symbol.
277 When the function is over, the supplied is left connected to the bfd
278 to which is was born, with its udata field pointing to the next member
279 on the chain in which it has been inserted.
281 A certain amount of jigery pokery is necessary since commons come
282 along and upset things, we only keep one item in the common chain; the
283 one with the biggest size seen sofar. When another common comes along
284 it either bumps the previous definition into the ref chain, since it
285 is bigger, or gets turned into a ref on the spot since the one on the
286 common chain is already bigger. If a real definition comes along then
287 the common gets bumped off anyway.
289 Whilst all this is going on we keep a count of the number of multiple
290 definitions seen, undefined global symbols and pending commons.
293 extern boolean relaxing;
296 Q_enter_global_ref (nlist_p)
300 asymbol *sym = *nlist_p;
301 CONST char *name = sym->name;
302 ldsym_type *sp = ldsym_get (name);
304 flagword this_symbol_flags = sym->flags;
307 ASSERT(sym->udata == 0);
310 if (flag_is_constructor(this_symbol_flags)) {
311 /* Add this constructor to the list we keep */
312 ldlang_add_constructor(sp);
313 /* Turn any commons into refs */
314 if (sp->scoms_chain != (asymbol **)NULL) {
315 refize(sp, sp->scoms_chain);
322 if (sym->section == &bfd_com_section) {
323 /* If we have a definition of this symbol already then
324 this common turns into a reference. Also we only
325 ever point to the largest common, so if we
326 have a common, but it's bigger that the new symbol
327 the turn this into a reference too. */
330 /* This is a common symbol, but we already have a definition
331 for it, so just link it into the ref chain as if
332 it were a reference */
335 else if (sp->scoms_chain) {
336 /* If we have a previous common, keep only the biggest */
337 if ( (*(sp->scoms_chain))->value > sym->value) {
338 /* other common is bigger, throw this one away */
341 else if (sp->scoms_chain != nlist_p) {
342 /* other common is smaller, throw that away */
343 refize(sp, sp->scoms_chain);
344 sp->scoms_chain = nlist_p;
348 /* This is the first time we've seen a common, so remember it
349 - if it was undefined before, we know it's defined now. If
350 the symbol has been marked as really being a constructor,
351 then treat this as a ref
353 if (sp->flags & SYM_CONSTRUCTOR) {
354 /* Turn this into a ref */
358 /* treat like a common */
360 undefined_global_sym_count--;
363 sp->scoms_chain = nlist_p;
368 else if (sym->section != &bfd_und_section) {
369 /* This is the definition of a symbol, add to def chain */
370 if (sp->sdefs_chain && (*(sp->sdefs_chain))->section != sym->section) {
371 /* Multiple definition */
372 asymbol *sy = *(sp->sdefs_chain);
373 lang_input_statement_type *stat = (lang_input_statement_type *) sy->the_bfd->usrdata;
374 lang_input_statement_type *stat1 = (lang_input_statement_type *) sym->the_bfd->usrdata;
375 asymbol ** stat1_symbols = stat1 ? stat1->asymbols: 0;
376 asymbol ** stat_symbols = stat ? stat->asymbols:0;
378 multiple_def_count++;
379 einfo("%C: multiple definition of `%T'\n",
380 sym->the_bfd, sym->section, stat1_symbols, sym->value, sym);
382 einfo("%C: first seen here\n",
383 sy->the_bfd, sy->section, stat_symbols, sy->value);
386 sym->udata =(PTR)( sp->sdefs_chain);
387 sp->sdefs_chain = nlist_p;
389 /* A definition overrides a common symbol */
390 if (sp->scoms_chain) {
391 refize(sp, sp->scoms_chain);
395 else if (sp->srefs_chain && relaxing == false) {
396 /* If previously was undefined, then remember as defined */
397 undefined_global_sym_count--;
401 if (sp->scoms_chain == (asymbol **)NULL
402 && sp->srefs_chain == (asymbol **)NULL
403 && sp->sdefs_chain == (asymbol **)NULL) {
404 /* And it's the first time we've seen it */
405 undefined_global_sym_count++;
413 ASSERT(sp->sdefs_chain == 0 || sp->scoms_chain == 0);
414 ASSERT(sp->scoms_chain ==0 || (*(sp->scoms_chain))->udata == 0);
420 Q_enter_file_symbols (entry)
421 lang_input_statement_type *entry;
425 entry->common_section =
426 bfd_make_section_old_way(entry->the_bfd, "COMMON");
428 ldlang_add_file(entry);
431 if (trace_files || option_v) {
435 total_symbols_seen += entry->symbol_count;
437 for (q = entry->asymbols; *q; q++)
441 if (p->section == &bfd_und_section
442 || (p->flags & BSF_GLOBAL)
443 || p->section == &bfd_com_section
444 || (p->flags & BSF_CONSTRUCTOR))
446 Q_enter_global_ref(q);
448 if (p->flags & BSF_INDIRECT) {
452 if (p->flags & BSF_WARNING) {
461 /* Searching libraries */
463 struct lang_input_statement_struct *decode_library_subfile ();
464 void linear_library (), symdef_library ();
466 /* Search the library ENTRY, already open on descriptor DESC.
467 This means deciding which library members to load,
468 making a chain of `struct lang_input_statement_struct' for those members,
469 and entering their global symbols in the hash table. */
472 search_library (entry)
473 struct lang_input_statement_struct *entry;
476 /* No need to load a library if no undefined symbols */
477 if (!undefined_global_sym_count) return;
479 if (bfd_has_map(entry->the_bfd))
480 symdef_library (entry);
482 linear_library (entry);
490 gnu960_check_format (abfd, format)
496 if ((bfd_check_format(abfd,format) == true)
497 && (abfd->xvec->flavour == output_flavor) ){
507 ldmain_open_file_read_symbol (entry)
508 struct lang_input_statement_struct *entry;
510 if (entry->asymbols == (asymbol **)NULL
511 &&entry->real == true
512 && entry->filename != (char *)NULL)
514 ldfile_open_file (entry);
518 if (gnu960_check_format(entry->the_bfd, bfd_object))
520 if (bfd_check_format(entry->the_bfd, bfd_object))
523 entry->the_bfd->usrdata = (PTR)entry;
526 Q_read_entry_symbols (entry->the_bfd, entry);
528 /* look through the sections in the file and see if any of them
530 ldlang_check_for_constructors (entry);
532 Q_enter_file_symbols (entry);
535 else if (gnu960_check_format(entry->the_bfd, bfd_archive))
537 else if (bfd_check_format(entry->the_bfd, bfd_archive))
540 entry->the_bfd->usrdata = (PTR)entry;
542 entry->subfiles = (lang_input_statement_type *)NULL;
543 search_library (entry);
547 einfo("%F%B: malformed input file (not rel or archive) \n",
555 /* Construct and return a lang_input_statement_struct for a library member.
556 The library's lang_input_statement_struct is library_entry,
557 and the library is open on DESC.
558 SUBFILE_OFFSET is the byte index in the library of this member's header.
559 We store the length of the member into *LENGTH_LOC. */
561 lang_input_statement_type *
562 decode_library_subfile (library_entry, subfile_offset)
563 struct lang_input_statement_struct *library_entry;
566 register struct lang_input_statement_struct *subentry;
567 subentry = (struct lang_input_statement_struct *) ldmalloc ((bfd_size_type)(sizeof (struct lang_input_statement_struct)));
568 subentry->filename = subfile_offset -> filename;
569 subentry->local_sym_name = subfile_offset->filename;
570 subentry->asymbols = 0;
571 subentry->the_bfd = subfile_offset;
572 subentry->subfiles = 0;
574 subentry->superfile = library_entry;
575 subentry->is_archive = false;
577 subentry->just_syms_flag = false;
578 subentry->loaded = false;
584 boolean subfile_wanted_p ();
586 clear_syms(entry, offset)
587 struct lang_input_statement_struct *entry;
591 unsigned long indx = bfd_get_next_mapent(entry->the_bfd,
594 while (indx != BFD_NO_MORE_SYMBOLS) {
595 if (car->file_offset == offset) {
598 indx = bfd_get_next_mapent(entry->the_bfd, indx, &car);
603 /* Search a library that has a map
606 symdef_library (entry)
607 struct lang_input_statement_struct *entry;
610 register struct lang_input_statement_struct *prev = 0;
612 boolean not_finished = true;
615 while (not_finished == true)
617 carsym *exported_library_name;
618 bfd *prev_archive_member_bfd = 0;
620 int idx = bfd_get_next_mapent(entry->the_bfd,
622 &exported_library_name);
624 not_finished = false;
626 while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count)
629 if (exported_library_name->name)
632 ldsym_type *sp = ldsym_get_soft (exported_library_name->name);
634 /* If we find a symbol that appears to be needed, think carefully
635 about the archive member that the symbol is in. */
636 /* So - if it exists, and is referenced somewhere and is
638 if (sp && sp->srefs_chain && !sp->sdefs_chain)
640 bfd *archive_member_bfd = bfd_get_elt_at_index(entry->the_bfd, idx);
641 struct lang_input_statement_struct *archive_member_lang_input_statement_struct;
644 if (archive_member_bfd && gnu960_check_format(archive_member_bfd, bfd_object))
646 if (archive_member_bfd && bfd_check_format(archive_member_bfd, bfd_object))
650 /* Don't think carefully about any archive member
651 more than once in a given pass. */
652 if (prev_archive_member_bfd != archive_member_bfd)
655 prev_archive_member_bfd = archive_member_bfd;
657 /* Read the symbol table of the archive member. */
659 if (archive_member_bfd->usrdata != (PTR)NULL) {
661 archive_member_lang_input_statement_struct =(lang_input_statement_type *) archive_member_bfd->usrdata;
665 archive_member_lang_input_statement_struct =
666 decode_library_subfile (entry, archive_member_bfd);
667 archive_member_bfd->usrdata = (PTR) archive_member_lang_input_statement_struct;
671 if (archive_member_lang_input_statement_struct == 0) {
672 einfo ("%F%I contains invalid archive member %s\n",
676 if (archive_member_lang_input_statement_struct->loaded == false)
679 Q_read_entry_symbols (archive_member_bfd, archive_member_lang_input_statement_struct);
680 /* Now scan the symbol table and decide whether to load. */
683 if (subfile_wanted_p (archive_member_lang_input_statement_struct) == true)
686 /* This member is needed; load it.
687 Since we are loading something on this pass,
688 we must make another pass through the symdef data. */
692 Q_enter_file_symbols (archive_member_lang_input_statement_struct);
695 prev->chain = archive_member_lang_input_statement_struct;
697 entry->subfiles = archive_member_lang_input_statement_struct;
700 prev = archive_member_lang_input_statement_struct;
703 /* Clear out this member's symbols from the symdef data
704 so that following passes won't waste time on them. */
705 clear_syms(entry, exported_library_name->file_offset);
706 archive_member_lang_input_statement_struct->loaded = true;
713 idx = bfd_get_next_mapent(entry->the_bfd, idx, &exported_library_name);
719 linear_library (entry)
720 struct lang_input_statement_struct *entry;
722 boolean more_to_do = true;
723 register struct lang_input_statement_struct *prev = 0;
727 bfd * archive = bfd_openr_next_archived_file(entry->the_bfd,0);
732 if (gnu960_check_format(archive, bfd_object))
734 if (bfd_check_format(archive, bfd_object))
737 register struct lang_input_statement_struct *subentry;
739 subentry = decode_library_subfile (entry,
742 archive->usrdata = (PTR) subentry;
743 if (!subentry) return;
744 if (subentry->loaded == false) {
745 Q_read_entry_symbols (archive, subentry);
747 if (subfile_wanted_p (subentry) == true)
749 Q_enter_file_symbols (subentry);
752 prev->chain = subentry;
754 entry->subfiles = subentry;
758 subentry->loaded = true;
762 archive = bfd_openr_next_archived_file(entry->the_bfd,archive);
769 /* ENTRY is an entry for a library member.
770 Its symbols have been read into core, but not entered.
771 Return nonzero if we ought to load this member. */
774 subfile_wanted_p (entry)
775 struct lang_input_statement_struct *entry;
779 for (q = entry->asymbols; *q; q++)
783 /* If the symbol has an interesting definition, we could
784 potentially want it. */
786 if (p->flags & BSF_INDIRECT) {
787 /* Grab out the name we've indirected to, and keep the insides
792 if (p->section == &bfd_com_section
793 || p->flags & BSF_GLOBAL)
795 register ldsym_type *sp = ldsym_get_soft (p->name);
798 /* If this symbol has not been hashed,
799 we can't be looking for it. */
800 if (sp != (ldsym_type *)NULL
801 && sp->sdefs_chain == (asymbol **)NULL) {
802 if (sp->srefs_chain != (asymbol **)NULL
803 || sp->scoms_chain != (asymbol **)NULL)
805 /* This is a symbol we are looking for. It is either
806 not yet defined or common. */
808 if (p->section == &bfd_com_section)
811 /* If the symbol in the table is a constructor, we won't to
812 anything fancy with it */
813 if ((sp->flags & SYM_CONSTRUCTOR) == 0) {
814 /* This libary member has something to
815 say about this element. We should
816 remember if its a new size */
817 /* Move something from the ref list to the com list */
818 if(sp->scoms_chain) {
819 /* Already a common symbol, maybe update it */
820 if (p->value > (*(sp->scoms_chain))->value) {
821 (*(sp->scoms_chain))->value = p->value;
825 /* Take a value from the ref chain
826 Here we are moving a symbol from the owning bfd
827 to another bfd. We must set up the
828 common_section portion of the bfd thing */
832 sp->scoms_chain = sp->srefs_chain;
834 (asymbol **)((*(sp->srefs_chain))->udata);
835 (*(sp->scoms_chain))->udata = (PTR)NULL;
837 (*( sp->scoms_chain))->section =
839 (*( sp->scoms_chain))->flags = 0;
840 /* Remember the size of this item */
841 sp->scoms_chain[0]->value = p->value;
843 undefined_global_sym_count--;
845 asymbol *com = *(sp->scoms_chain);
846 if (((lang_input_statement_type *)
847 (com->the_bfd->usrdata))->common_section ==
849 ((lang_input_statement_type *)
850 (com->the_bfd->usrdata))->common_section =
851 bfd_make_section_old_way(com->the_bfd, "COMMON");
855 ASSERT(p->udata == 0);
861 info("%I needed due to %s\n",entry, sp->name);