1 /* Copyright (C) 1991 Free Software Foundation, Inc.
3 This file is part of GLD, the Gnu Linker.
5 GLD is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
10 GLD is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GLD; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 * Written by Steve Chamberlain steve@cygnus.com
41 extern boolean lang_has_input_file;
42 extern boolean trace_files;
46 char *output_filename = "a.out";
47 /* Name this program was invoked by. */
50 /* The file that we're creating */
53 extern boolean option_v;
55 /* The local symbol prefix */
58 /* Count the number of global symbols multiply defined. */
59 int multiple_def_count;
62 /* Count the number of symbols defined through common declarations.
63 This count is referenced in symdef_library, linear_library, and
64 modified by enter_global_ref.
66 It is incremented when a symbol is created as a common, and
67 decremented when the common declaration is overridden
69 Another way of thinking of it is that this is a count of
70 all ldsym_types with a ->scoms field */
72 unsigned int commons_pending;
74 /* Count the number of global symbols referenced and not defined.
75 common symbols are not included in this count. */
77 unsigned int undefined_global_sym_count;
81 /* Count the number of warning symbols encountered. */
84 /* have we had a load script ? */
85 extern boolean had_script;
87 /* Nonzero means print names of input files as processed. */
92 /* 1 => write load map. */
98 /* Indicates whether output file will be b.out (default) or coff */
99 enum target_flavour output_flavor = BFD_BOUT_FORMAT;
102 /* Force the make_executable to be output, even if there are non-fatal
104 boolean force_make_executable;
106 /* A count of the total number of local symbols ever seen - by adding
107 the symbol_count field of each newly read afile.*/
109 unsigned int total_symbols_seen;
111 /* A count of the number of read files - the same as the number of elements
114 unsigned int total_files_seen;
117 args_type command_line;
118 ld_config_type config;
125 program_name = argv[0];
126 output_filename = "a.out";
133 check_v960( argc, argv );
134 emulation = GLD960_EMULATION_NAME;
135 for ( i = 1; i < argc; i++ ){
136 if ( !strcmp(argv[i],"-Fcoff") ){
137 emulation = LNK960_EMULATION_NAME;
138 output_flavor = BFD_COFF_FORMAT;
144 emulation = (char *) getenv(EMULATION_ENVIRON);
147 /* Initialize the data about options. */
151 config.relocateable_output = false;
153 command_line.force_common_definition = false;
156 ldfile_add_library_path("./");
157 config.make_executable = true;
158 force_make_executable = false;
161 /* Initialize the cumulative counts of symbols. */
162 undefined_global_sym_count = 0;
164 multiple_def_count = 0;
167 config.magic_demand_paged = 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;
207 output_bfd->flags |= WP_TEXT;
210 output_bfd->flags |= WP_TEXT;
214 if (config.relocateable_output) {
215 output_bfd->flags &= ~( D_PAGED);
216 output_bfd->flags |= EXEC_P;
218 bfd_close(output_bfd);
221 output_bfd->flags |= EXEC_P;
224 bfd_close(output_bfd);
225 if (config.make_executable == false && force_make_executable == false) {
226 unlink(output_filename);
228 return (!config.make_executable);
236 Q_read_entry_symbols (desc, entry)
238 struct lang_input_statement_struct *entry;
240 if (entry->asymbols == (asymbol **)NULL) {
241 bfd_size_type table_size = get_symtab_upper_bound(desc);
242 entry->asymbols = (asymbol **)ldmalloc(table_size);
243 entry->symbol_count = bfd_canonicalize_symtab(desc, entry->asymbols) ;
249 * turn this item into a reference
256 asymbol *sym = *nlist_p;
258 sym->flags = BSF_UNDEFINED;
259 sym->section = (asection *)NULL;
260 sym->udata =(PTR)( sp->srefs_chain);
261 sp->srefs_chain = nlist_p;
264 This function is called for each name which is seen which has a global
265 scope. It enters the name into the global symbol table in the correct
266 symbol on the correct chain. Remember that each ldsym_type has three
267 chains attatched, one of all definitions of a symbol, one of all
268 references of a symbol and one of all common definitions of a symbol.
270 When the function is over, the supplied is left connected to the bfd
271 to which is was born, with its udata field pointing to the next member
272 on the chain in which it has been inserted.
274 A certain amount of jigery pokery is necessary since commons come
275 along and upset things, we only keep one item in the common chain; the
276 one with the biggest size seen sofar. When another common comes along
277 it either bumps the previous definition into the ref chain, since it
278 is bigger, or gets turned into a ref on the spot since the one on the
279 common chain is already bigger. If a real definition comes along then
280 the common gets bumped off anyway.
282 Whilst all this is going on we keep a count of the number of multiple
283 definitions seen, undefined global symbols and pending commons.
288 Q_enter_global_ref (nlist_p)
292 asymbol *sym = *nlist_p;
293 CONST char *name = sym->name;
294 ldsym_type *sp = ldsym_get (name);
296 flagword this_symbol_flags = sym->flags;
299 ASSERT(sym->udata == 0);
302 if (flag_is_constructor(this_symbol_flags)) {
303 /* Add this constructor to the list we keep */
304 ldlang_add_constructor(sp);
305 /* Turn any commons into refs */
306 if (sp->scoms_chain != (asymbol **)NULL) {
307 refize(sp, sp->scoms_chain);
314 if (flag_is_common(this_symbol_flags)) {
315 /* If we have a definition of this symbol already then
316 this common turns into a reference. Also we only
317 ever point to the largest common, so if we
318 have a common, but it's bigger that the new symbol
319 the turn this into a reference too. */
322 /* This is a common symbol, but we already have a definition
323 for it, so just link it into the ref chain as if
324 it were a reference */
327 else if (sp->scoms_chain) {
328 /* If we have a previous common, keep only the biggest */
329 if ( (*(sp->scoms_chain))->value > sym->value) {
330 /* other common is bigger, throw this one away */
333 else if (sp->scoms_chain != nlist_p) {
334 /* other common is smaller, throw that away */
335 refize(sp, sp->scoms_chain);
336 sp->scoms_chain = nlist_p;
340 /* This is the first time we've seen a common, so remember it
341 - if it was undefined before, we know it's defined now. If
342 the symbol has been marked as really being a constructor,
343 then treat this as a ref
345 if (sp->flags & SYM_CONSTRUCTOR) {
346 /* Turn this into a ref */
350 /* treat like a common */
352 undefined_global_sym_count--;
355 sp->scoms_chain = nlist_p;
360 else if (flag_is_defined(this_symbol_flags)) {
361 /* This is the definition of a symbol, add to def chain */
362 if (sp->sdefs_chain && (*(sp->sdefs_chain))->section != sym->section) {
363 /* Multiple definition */
364 asymbol *sy = *(sp->sdefs_chain);
365 lang_input_statement_type *stat = (lang_input_statement_type *) sy->the_bfd->usrdata;
366 lang_input_statement_type *stat1 = (lang_input_statement_type *) sym->the_bfd->usrdata;
367 asymbol ** stat1_symbols = stat1 ? stat1->asymbols: 0;
368 asymbol ** stat_symbols = stat ? stat->asymbols:0;
370 multiple_def_count++;
371 info("%C: multiple definition of `%T'\n",
378 info("%C: first seen here\n",
385 sym->udata =(PTR)( sp->sdefs_chain);
386 sp->sdefs_chain = nlist_p;
388 /* A definition overrides a common symbol */
389 if (sp->scoms_chain) {
390 refize(sp, sp->scoms_chain);
394 else if (sp->srefs_chain) {
395 /* If previously was undefined, then remember as defined */
396 undefined_global_sym_count--;
400 if (sp->scoms_chain == (asymbol **)NULL
401 && sp->srefs_chain == (asymbol **)NULL
402 && sp->sdefs_chain == (asymbol **)NULL) {
403 /* And it's the first time we've seen it */
404 undefined_global_sym_count++;
412 ASSERT(sp->sdefs_chain == 0 || sp->scoms_chain == 0);
413 ASSERT(sp->scoms_chain ==0 || (*(sp->scoms_chain))->udata == 0);
419 Q_enter_file_symbols (entry)
420 lang_input_statement_type *entry;
423 entry->common_section =
424 bfd_make_section(entry->the_bfd, "COMMON");
426 ldlang_add_file(entry);
429 if (trace_files || option_v) {
433 total_symbols_seen += entry->symbol_count;
435 for (q = entry->asymbols; *q; q++)
439 if (flag_is_undefined_or_global_or_common_or_constructor(p->flags))
441 Q_enter_global_ref(q);
443 if (p->flags & BSF_INDIRECT) {
447 if (p->flags & BSF_WARNING) {
450 ASSERT(p->flags != 0);
456 /* Searching libraries */
458 struct lang_input_statement_struct *decode_library_subfile ();
459 void linear_library (), symdef_library ();
461 /* Search the library ENTRY, already open on descriptor DESC.
462 This means deciding which library members to load,
463 making a chain of `struct lang_input_statement_struct' for those members,
464 and entering their global symbols in the hash table. */
467 search_library (entry)
468 struct lang_input_statement_struct *entry;
471 /* No need to load a library if no undefined symbols */
472 if (!undefined_global_sym_count) return;
474 if (bfd_has_map(entry->the_bfd))
475 symdef_library (entry);
477 linear_library (entry);
485 gnu960_check_format (abfd, format)
491 if ((bfd_check_format(abfd,format) == true)
492 && (abfd->xvec->flavour == output_flavor) ){
502 ldmain_open_file_read_symbol (entry)
503 struct lang_input_statement_struct *entry;
505 if (entry->asymbols == (asymbol **)NULL
506 &&entry->real == true
507 && entry->filename != (char *)NULL)
509 ldfile_open_file (entry);
512 if (gnu960_check_format(entry->the_bfd, bfd_object))
514 if (bfd_check_format(entry->the_bfd, bfd_object))
517 entry->the_bfd->usrdata = (PTR)entry;
520 Q_read_entry_symbols (entry->the_bfd, 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);