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. */
40 extern boolean lang_has_input_file;
41 extern boolean trace_files;
45 char *output_filename = "a.out";
46 /* Name this program was invoked by. */
49 /* The file that we're creating */
52 extern boolean option_v;
54 /* The local symbol prefix */
57 /* Count the number of global symbols multiply defined. */
58 int multiple_def_count;
61 /* Count the number of symbols defined through common declarations.
62 This count is referenced in symdef_library, linear_library, and
63 modified by enter_global_ref.
65 It is incremented when a symbol is created as a common, and
66 decremented when the common declaration is overridden
68 Another way of thinking of it is that this is a count of
69 all ldsym_types with a ->scoms field */
71 unsigned int commons_pending;
73 /* Count the number of global symbols referenced and not defined.
74 common symbols are not included in this count. */
76 unsigned int undefined_global_sym_count;
80 /* Count the number of warning symbols encountered. */
83 /* have we had a load script ? */
84 extern boolean had_script;
86 /* Nonzero means print names of input files as processed. */
91 /* 1 => write load map. */
97 /* Indicates whether output file will be b.out (default) or coff */
98 enum target_flavour output_flavor = BFD_BOUT_FORMAT;
101 /* Force the make_executable to be output, even if there are non-fatal
103 boolean force_make_executable;
105 /* A count of the total number of local symbols ever seen - by adding
106 the symbol_count field of each newly read afile.*/
108 unsigned int total_symbols_seen;
110 /* A count of the number of read files - the same as the number of elements
113 unsigned int total_files_seen;
116 args_type command_line;
117 ld_config_type config;
124 program_name = argv[0];
125 output_filename = "a.out";
132 check_v960( argc, argv );
133 emulation = GLD960_EMULATION_NAME;
134 for ( i = 1; i < argc; i++ ){
135 if ( !strcmp(argv[i],"-Fcoff") ){
136 emulation = LNK960_EMULATION_NAME;
137 output_flavor = BFD_COFF_FORMAT;
143 emulation = (char *) getenv(EMULATION_ENVIRON);
146 /* Initialize the data about options. */
150 config.relocateable_output = false;
152 command_line.force_common_definition = false;
155 ldfile_add_library_path("./");
156 config.make_executable = true;
157 force_make_executable = false;
160 /* Initialize the cumulative counts of symbols. */
161 undefined_global_sym_count = 0;
163 multiple_def_count = 0;
166 config.magic_demand_paged = true;
167 config.text_read_only = true;
168 config.make_executable = true;
169 if (emulation == (char *)NULL) {
170 emulation= DEFAULT_EMULATION;
173 ldemul_choose_mode(emulation);
174 default_target = ldemul_choose_target();
176 ldemul_before_parse();
177 lang_has_input_file = false;
178 parse_args(argc, argv);
181 info("%P: mode %s\n", emulation);
183 if (lang_has_input_file == false) {
184 info("%P%F: No input files\n");
187 ldemul_after_parse();
190 /* Print error messages for any missing symbols, for any warning
191 symbols, and possibly multiple definitions */
193 /* Print a map, if requested. */
196 ldsym_print_symbol_table ();
200 if (config.text_read_only) {
201 /* Look for a text section and mark the readonly attribute in it */
202 asection *found = bfd_get_section_by_name(output_bfd, ".text");
203 if (found == (asection *)NULL) {
204 info("%P%F: text marked read only, but no text section present");
206 found->flags |= SEC_READONLY;
209 if (config.relocateable_output) {
210 output_bfd->flags &= ~EXEC_P;
212 bfd_close(output_bfd);
215 output_bfd->flags |= EXEC_P;
218 bfd_close(output_bfd);
219 if (config.make_executable == false && force_make_executable == false) {
220 unlink(output_filename);
222 return (!config.make_executable);
230 Q_read_entry_symbols (desc, entry)
232 struct lang_input_statement_struct *entry;
234 if (entry->asymbols == (asymbol **)NULL) {
235 bfd_size_type table_size = get_symtab_upper_bound(desc);
236 entry->asymbols = (asymbol **)ldmalloc(table_size);
237 entry->symbol_count = bfd_canonicalize_symtab(desc, entry->asymbols) ;
243 * turn this item into a reference
250 asymbol *sym = *nlist_p;
252 sym->flags = BSF_UNDEFINED;
253 sym->section = (asection *)NULL;
254 sym->udata =(PTR)( sp->srefs_chain);
255 sp->srefs_chain = nlist_p;
258 This function is called for each name which is seen which has a global
259 scope. It enters the name into the global symbol table in the correct
260 symbol on the correct chain. Remember that each ldsym_type has three
261 chains attatched, one of all definitions of a symbol, one of all
262 references of a symbol and one of all common definitions of a symbol.
264 When the function is over, the supplied is left connected to the bfd
265 to which is was born, with its udata field pointing to the next member
266 on the chain in which it has been inserted.
268 A certain amount of jigery pokery is necessary since commons come
269 along and upset things, we only keep one item in the common chain; the
270 one with the biggest size seen sofar. When another common comes along
271 it either bumps the previous definition into the ref chain, since it
272 is bigger, or gets turned into a ref on the spot since the one on the
273 common chain is already bigger. If a real definition comes along then
274 the common gets bumped off anyway.
276 Whilst all this is going on we keep a count of the number of multiple
277 definitions seen, undefined global symbols and pending commons.
282 Q_enter_global_ref (nlist_p)
286 asymbol *sym = *nlist_p;
287 CONST char *name = sym->name;
288 ldsym_type *sp = ldsym_get (name);
290 flagword this_symbol_flags = sym->flags;
293 ASSERT(sym->udata == 0);
296 if (flag_is_constructor(this_symbol_flags)) {
297 /* Add this constructor to the list we keep */
298 ldlang_add_constructor(sp);
299 /* Turn any commons into refs */
300 if (sp->scoms_chain != (asymbol **)NULL) {
301 refize(sp, sp->scoms_chain);
308 if (flag_is_common(this_symbol_flags)) {
309 /* If we have a definition of this symbol already then
310 this common turns into a reference. Also we only
311 ever point to the largest common, so if we
312 have a common, but it's bigger that the new symbol
313 the turn this into a reference too. */
316 /* This is a common symbol, but we already have a definition
317 for it, so just link it into the ref chain as if
318 it were a reference */
321 else if (sp->scoms_chain) {
322 /* If we have a previous common, keep only the biggest */
323 if ( (*(sp->scoms_chain))->value > sym->value) {
324 /* other common is bigger, throw this one away */
327 else if (sp->scoms_chain != nlist_p) {
328 /* other common is smaller, throw that away */
329 refize(sp, sp->scoms_chain);
330 sp->scoms_chain = nlist_p;
334 /* This is the first time we've seen a common, so remember it
335 - if it was undefined before, we know it's defined now. If
336 the symbol has been marked as really being a constructor,
337 then treat this as a ref
339 if (sp->flags & SYM_CONSTRUCTOR) {
340 /* Turn this into a ref */
344 /* treat like a common */
346 undefined_global_sym_count--;
349 sp->scoms_chain = nlist_p;
354 else if (flag_is_defined(this_symbol_flags)) {
355 /* This is the definition of a symbol, add to def chain */
356 if (sp->sdefs_chain && (*(sp->sdefs_chain))->section != sym->section) {
357 /* Multiple definition */
358 asymbol *sy = *(sp->sdefs_chain);
359 lang_input_statement_type *stat = (lang_input_statement_type *) sy->the_bfd->usrdata;
360 lang_input_statement_type *stat1 = (lang_input_statement_type *) sym->the_bfd->usrdata;
361 asymbol ** stat1_symbols = stat1 ? stat1->asymbols: 0;
362 asymbol ** stat_symbols = stat ? stat->asymbols:0;
364 multiple_def_count++;
365 info("%C: multiple definition of `%T'\n",
372 info("%C: first seen here\n",
379 sym->udata =(PTR)( sp->sdefs_chain);
380 sp->sdefs_chain = nlist_p;
382 /* A definition overrides a common symbol */
383 if (sp->scoms_chain) {
384 refize(sp, sp->scoms_chain);
388 else if (sp->srefs_chain) {
389 /* If previously was undefined, then remember as defined */
390 undefined_global_sym_count--;
394 if (sp->scoms_chain == (asymbol **)NULL
395 && sp->srefs_chain == (asymbol **)NULL
396 && sp->sdefs_chain == (asymbol **)NULL) {
397 /* And it's the first time we've seen it */
398 undefined_global_sym_count++;
406 ASSERT(sp->sdefs_chain == 0 || sp->scoms_chain == 0);
407 ASSERT(sp->scoms_chain ==0 || (*(sp->scoms_chain))->udata == 0);
413 Q_enter_file_symbols (entry)
414 lang_input_statement_type *entry;
417 entry->common_section =
418 bfd_make_section(entry->the_bfd, "COMMON");
420 ldlang_add_file(entry);
423 if (trace_files || option_v) {
427 total_symbols_seen += entry->symbol_count;
429 for (q = entry->asymbols; *q; q++)
433 if (flag_is_undefined_or_global_or_common_or_constructor(p->flags))
435 Q_enter_global_ref(q);
437 if (p->flags & BSF_INDIRECT) {
441 if (p->flags & BSF_WARNING) {
444 ASSERT(p->flags != 0);
450 /* Searching libraries */
452 struct lang_input_statement_struct *decode_library_subfile ();
453 void linear_library (), symdef_library ();
455 /* Search the library ENTRY, already open on descriptor DESC.
456 This means deciding which library members to load,
457 making a chain of `struct lang_input_statement_struct' for those members,
458 and entering their global symbols in the hash table. */
461 search_library (entry)
462 struct lang_input_statement_struct *entry;
465 /* No need to load a library if no undefined symbols */
466 if (!undefined_global_sym_count) return;
468 if (bfd_has_map(entry->the_bfd))
469 symdef_library (entry);
471 linear_library (entry);
479 gnu960_check_format (abfd, format)
485 if ((bfd_check_format(abfd,format) == true)
486 && (abfd->xvec->flavour == output_flavor) ){
496 ldmain_open_file_read_symbol (entry)
497 struct lang_input_statement_struct *entry;
499 if (entry->asymbols == (asymbol **)NULL
500 &&entry->real == true
501 && entry->filename != (char *)NULL)
503 ldfile_open_file (entry);
507 if (gnu960_check_format(entry->the_bfd, bfd_object))
509 if (bfd_check_format(entry->the_bfd, bfd_object))
512 entry->the_bfd->usrdata = (PTR)entry;
515 Q_read_entry_symbols (entry->the_bfd, entry);
517 /* look through the sections in the file and see if any of them
519 ldlang_check_for_constructors (entry);
521 Q_enter_file_symbols (entry);
524 else if (gnu960_check_format(entry->the_bfd, bfd_archive))
526 else if (bfd_check_format(entry->the_bfd, bfd_archive))
529 entry->the_bfd->usrdata = (PTR)entry;
531 entry->subfiles = (lang_input_statement_type *)NULL;
532 search_library (entry);
536 info("%F%B: malformed input file (not rel or archive) \n",
544 /* Construct and return a lang_input_statement_struct for a library member.
545 The library's lang_input_statement_struct is library_entry,
546 and the library is open on DESC.
547 SUBFILE_OFFSET is the byte index in the library of this member's header.
548 We store the length of the member into *LENGTH_LOC. */
550 lang_input_statement_type *
551 decode_library_subfile (library_entry, subfile_offset)
552 struct lang_input_statement_struct *library_entry;
555 register struct lang_input_statement_struct *subentry;
556 subentry = (struct lang_input_statement_struct *) ldmalloc ((bfd_size_type)(sizeof (struct lang_input_statement_struct)));
557 subentry->filename = subfile_offset -> filename;
558 subentry->local_sym_name = subfile_offset->filename;
559 subentry->asymbols = 0;
560 subentry->the_bfd = subfile_offset;
561 subentry->subfiles = 0;
563 subentry->superfile = library_entry;
564 subentry->is_archive = false;
566 subentry->just_syms_flag = false;
567 subentry->loaded = false;
573 boolean subfile_wanted_p ();
575 clear_syms(entry, offset)
576 struct lang_input_statement_struct *entry;
580 unsigned long indx = bfd_get_next_mapent(entry->the_bfd,
583 while (indx != BFD_NO_MORE_SYMBOLS) {
584 if (car->file_offset == offset) {
587 indx = bfd_get_next_mapent(entry->the_bfd, indx, &car);
592 /* Search a library that has a map
595 symdef_library (entry)
596 struct lang_input_statement_struct *entry;
599 register struct lang_input_statement_struct *prev = 0;
601 boolean not_finished = true;
604 while (not_finished == true)
606 carsym *exported_library_name;
607 bfd *prev_archive_member_bfd = 0;
609 int idx = bfd_get_next_mapent(entry->the_bfd,
611 &exported_library_name);
613 not_finished = false;
615 while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count)
618 if (exported_library_name->name)
621 ldsym_type *sp = ldsym_get_soft (exported_library_name->name);
623 /* If we find a symbol that appears to be needed, think carefully
624 about the archive member that the symbol is in. */
625 /* So - if it exists, and is referenced somewhere and is
627 if (sp && sp->srefs_chain && !sp->sdefs_chain)
629 bfd *archive_member_bfd = bfd_get_elt_at_index(entry->the_bfd, idx);
630 struct lang_input_statement_struct *archive_member_lang_input_statement_struct;
633 if (archive_member_bfd && gnu960_check_format(archive_member_bfd, bfd_object))
635 if (archive_member_bfd && bfd_check_format(archive_member_bfd, bfd_object))
639 /* Don't think carefully about any archive member
640 more than once in a given pass. */
641 if (prev_archive_member_bfd != archive_member_bfd)
644 prev_archive_member_bfd = archive_member_bfd;
646 /* Read the symbol table of the archive member. */
648 if (archive_member_bfd->usrdata != (PTR)NULL) {
650 archive_member_lang_input_statement_struct =(lang_input_statement_type *) archive_member_bfd->usrdata;
654 archive_member_lang_input_statement_struct =
655 decode_library_subfile (entry, archive_member_bfd);
656 archive_member_bfd->usrdata = (PTR) archive_member_lang_input_statement_struct;
660 if (archive_member_lang_input_statement_struct == 0) {
661 info ("%F%I contains invalid archive member %s\n",
666 if (archive_member_lang_input_statement_struct->loaded == false)
669 Q_read_entry_symbols (archive_member_bfd, archive_member_lang_input_statement_struct);
670 /* Now scan the symbol table and decide whether to load. */
673 if (subfile_wanted_p (archive_member_lang_input_statement_struct) == true)
676 /* This member is needed; load it.
677 Since we are loading something on this pass,
678 we must make another pass through the symdef data. */
682 Q_enter_file_symbols (archive_member_lang_input_statement_struct);
685 prev->chain = archive_member_lang_input_statement_struct;
687 entry->subfiles = archive_member_lang_input_statement_struct;
690 prev = archive_member_lang_input_statement_struct;
693 /* Clear out this member's symbols from the symdef data
694 so that following passes won't waste time on them. */
695 clear_syms(entry, exported_library_name->file_offset);
696 archive_member_lang_input_statement_struct->loaded = true;
703 idx = bfd_get_next_mapent(entry->the_bfd, idx, &exported_library_name);
709 linear_library (entry)
710 struct lang_input_statement_struct *entry;
712 boolean more_to_do = true;
713 register struct lang_input_statement_struct *prev = 0;
717 bfd * archive = bfd_openr_next_archived_file(entry->the_bfd,0);
722 if (gnu960_check_format(archive, bfd_object))
724 if (bfd_check_format(archive, bfd_object))
727 register struct lang_input_statement_struct *subentry;
729 subentry = decode_library_subfile (entry,
732 archive->usrdata = (PTR) subentry;
733 if (!subentry) return;
734 if (subentry->loaded == false) {
735 Q_read_entry_symbols (archive, subentry);
737 if (subfile_wanted_p (subentry) == true)
739 Q_enter_file_symbols (subentry);
742 prev->chain = subentry;
744 entry->subfiles = subentry;
748 subentry->loaded = true;
752 archive = bfd_openr_next_archived_file(entry->the_bfd,archive);
759 /* ENTRY is an entry for a library member.
760 Its symbols have been read into core, but not entered.
761 Return nonzero if we ought to load this member. */
764 subfile_wanted_p (entry)
765 struct lang_input_statement_struct *entry;
769 for (q = entry->asymbols; *q; q++)
773 /* If the symbol has an interesting definition, we could
774 potentially want it. */
776 if (p->flags & BSF_INDIRECT) {
777 /* Grab out the name we've indirected to, and keep the insides
782 if (p->flags & BSF_FORT_COMM
783 || p->flags & BSF_GLOBAL)
785 register ldsym_type *sp = ldsym_get_soft (p->name);
788 /* If this symbol has not been hashed,
789 we can't be looking for it. */
790 if (sp != (ldsym_type *)NULL
791 && sp->sdefs_chain == (asymbol **)NULL) {
792 if (sp->srefs_chain != (asymbol **)NULL
793 || sp->scoms_chain != (asymbol **)NULL)
795 /* This is a symbol we are looking for. It is either
796 not yet defined or common. */
798 if (flag_is_common(p->flags))
801 /* If the symbol in the table is a constructor, we won't to
802 anything fancy with it */
803 if ((sp->flags & SYM_CONSTRUCTOR) == 0) {
804 /* This libary member has something to
805 say about this element. We should
806 remember if its a new size */
807 /* Move something from the ref list to the com list */
808 if(sp->scoms_chain) {
809 /* Already a common symbol, maybe update it */
810 if (p->value > (*(sp->scoms_chain))->value) {
811 (*(sp->scoms_chain))->value = p->value;
815 /* Take a value from the ref chain
816 Here we are moving a symbol from the owning bfd
817 to another bfd. We must set up the
818 common_section portion of the bfd thing */
822 sp->scoms_chain = sp->srefs_chain;
824 (asymbol **)((*(sp->srefs_chain))->udata);
825 (*(sp->scoms_chain))->udata = (PTR)NULL;
827 (*( sp->scoms_chain))->flags = BSF_FORT_COMM;
828 /* Remember the size of this item */
829 sp->scoms_chain[0]->value = p->value;
831 undefined_global_sym_count--;
833 asymbol *com = *(sp->scoms_chain);
834 if (((lang_input_statement_type *)
835 (com->the_bfd->usrdata))->common_section ==
837 ((lang_input_statement_type *)
838 (com->the_bfd->usrdata))->common_section =
839 bfd_make_section(com->the_bfd, "COMMON");
843 ASSERT(p->udata == 0);
849 info("%I needed due to %s\n",entry, sp->name);