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. */
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 /* The local symbol prefix */
60 /* Count the number of global symbols multiply defined. */
61 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
;
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. */
89 /* 1 => write load map. */
96 /* Indicates whether output file will be b.out (default) or coff */
97 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
;
125 program_name
= argv
[0];
126 output_filename
= "a.out";
134 check_v960 (argc
, argv
);
135 emulation
= "gld960";
136 for (i
= 1; i
< argc
; i
++)
138 if (!strcmp (argv
[i
], "-Fcoff"))
140 emulation
= "lnk960";
141 output_flavor
= BFD_COFF_FORMAT
;
147 emulation
= (char *) getenv (EMULATION_ENVIRON
);
150 /* Initialize the data about options. */
155 config
.relocateable_output
= false;
157 command_line
.force_common_definition
= false;
159 init_bfd_error_vector ();
161 ldfile_add_arch ("");
163 config
.make_executable
= true;
164 force_make_executable
= false;
167 /* Initialize the cumulative counts of symbols. */
168 undefined_global_sym_count
= 0;
170 multiple_def_count
= 0;
173 config
.magic_demand_paged
= true;
174 config
.text_read_only
= true;
175 config
.make_executable
= true;
176 if (emulation
== (char *) NULL
)
178 emulation
= DEFAULT_EMULATION
;
181 ldemul_choose_mode (emulation
);
182 default_target
= ldemul_choose_target ();
184 ldemul_before_parse ();
185 lang_has_input_file
= false;
186 parse_args (argc
, argv
);
192 info ("%P: mode %s\n", emulation
);
195 if (lang_has_input_file
== false)
197 einfo ("%P%F: No input files\n");
200 ldemul_after_parse ();
203 if (config
.map_filename
)
205 if (strcmp (config
.map_filename
, "-") == 0)
207 config
.map_file
= stdout
;
211 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
212 if (config
.map_file
== (FILE *) NULL
)
214 einfo ("%P%F: can't open map file %s\n",
215 config
.map_filename
);
223 /* Print error messages for any missing symbols, for any warning
224 symbols, and possibly multiple definitions */
227 if (config
.text_read_only
)
229 /* Look for a text section and mark the readonly attribute in it */
230 asection
*found
= bfd_get_section_by_name (output_bfd
, ".text");
232 if (found
!= (asection
*) NULL
)
234 found
->flags
|= SEC_READONLY
;
238 if (config
.relocateable_output
)
240 output_bfd
->flags
&= ~EXEC_P
;
243 bfd_close (output_bfd
);
249 output_bfd
->flags
|= EXEC_P
;
254 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
);
281 Q_read_entry_symbols (desc
, entry
)
283 struct lang_input_statement_struct
*entry
;
285 if (entry
->asymbols
== (asymbol
**) NULL
)
287 bfd_size_type table_size
= get_symtab_upper_bound (desc
);
289 entry
->asymbols
= (asymbol
**) ldmalloc (table_size
);
290 entry
->symbol_count
= bfd_canonicalize_symtab (desc
, entry
->asymbols
);
295 * turn this item into a reference
302 asymbol
*sym
= *nlist_p
;
306 sym
->section
= &bfd_und_section
;
307 sym
->udata
= (PTR
) (sp
->srefs_chain
);
308 sp
->srefs_chain
= nlist_p
;
312 This function is called for each name which is seen which has a global
313 scope. It enters the name into the global symbol table in the correct
314 symbol on the correct chain. Remember that each ldsym_type has three
315 chains attatched, one of all definitions of a symbol, one of all
316 references of a symbol and one of all common definitions of a symbol.
318 When the function is over, the supplied is left connected to the bfd
319 to which is was born, with its udata field pointing to the next member
320 on the chain in which it has been inserted.
322 A certain amount of jigery pokery is necessary since commons come
323 along and upset things, we only keep one item in the common chain; the
324 one with the biggest size seen sofar. When another common comes along
325 it either bumps the previous definition into the ref chain, since it
326 is bigger, or gets turned into a ref on the spot since the one on the
327 common chain is already bigger. If a real definition comes along then
328 the common gets bumped off anyway.
330 Whilst all this is going on we keep a count of the number of multiple
331 definitions seen, undefined global symbols and pending commons.
334 extern boolean relaxing
;
337 DEFUN (Q_enter_global_ref
, (nlist_p
, name
),
338 asymbol
** nlist_p AND
/* pointer into symbol table from incoming bfd */
339 CONST
char *name
/* name of symbol in linker table */ )
341 asymbol
*sym
= *nlist_p
;
344 /* Lookup the name from the incoming bfd's symbol table in the
345 linker's global symbol table */
348 flagword this_symbol_flags
= sym
->flags
;
350 sp
= ldsym_get (name
);
352 ASSERT (sym
->udata
== 0);
355 if (flag_is_constructor (this_symbol_flags
))
357 /* Add this constructor to the list we keep */
358 ldlang_add_constructor (sp
);
359 /* Turn any commons into refs */
360 if (sp
->scoms_chain
!= (asymbol
**) NULL
)
362 refize (sp
, sp
->scoms_chain
);
370 if (sym
->section
== &bfd_com_section
)
372 /* If we have a definition of this symbol already then
373 this common turns into a reference. Also we only
374 ever point to the largest common, so if we
375 have a common, but it's bigger that the new symbol
376 the turn this into a reference too. */
379 /* This is a common symbol, but we already have a definition
380 for it, so just link it into the ref chain as if
381 it were a reference */
382 refize (sp
, nlist_p
);
384 else if (sp
->scoms_chain
)
386 /* If we have a previous common, keep only the biggest */
387 if ((*(sp
->scoms_chain
))->value
> sym
->value
)
389 /* other common is bigger, throw this one away */
390 refize (sp
, nlist_p
);
392 else if (sp
->scoms_chain
!= nlist_p
)
394 /* other common is smaller, throw that away */
395 refize (sp
, sp
->scoms_chain
);
396 sp
->scoms_chain
= nlist_p
;
401 /* This is the first time we've seen a common, so remember it
402 - if it was undefined before, we know it's defined now. If
403 the symbol has been marked as really being a constructor,
404 then treat this as a ref
406 if (sp
->flags
& SYM_CONSTRUCTOR
)
408 /* Turn this into a ref */
409 refize (sp
, nlist_p
);
413 /* treat like a common */
415 undefined_global_sym_count
--;
418 sp
->scoms_chain
= nlist_p
;
423 else if (sym
->section
!= &bfd_und_section
)
425 /* This is the definition of a symbol, add to def chain */
426 if (sp
->sdefs_chain
&& (*(sp
->sdefs_chain
))->section
!= sym
->section
)
428 /* Multiple definition */
429 asymbol
*sy
= *(sp
->sdefs_chain
);
430 lang_input_statement_type
*stat
= (lang_input_statement_type
*) sy
->the_bfd
->usrdata
;
431 lang_input_statement_type
*stat1
= (lang_input_statement_type
*) sym
->the_bfd
->usrdata
;
432 asymbol
**stat1_symbols
= stat1
? stat1
->asymbols
: 0;
433 asymbol
**stat_symbols
= stat
? stat
->asymbols
: 0;
435 multiple_def_count
++;
436 einfo ("%X%C: multiple definition of `%T'\n",
437 sym
->the_bfd
, sym
->section
, stat1_symbols
, sym
->value
, sym
);
439 einfo ("%X%C: first seen here\n",
440 sy
->the_bfd
, sy
->section
, stat_symbols
, sy
->value
);
444 sym
->udata
= (PTR
) (sp
->sdefs_chain
);
445 sp
->sdefs_chain
= nlist_p
;
447 /* A definition overrides a common symbol */
450 refize (sp
, sp
->scoms_chain
);
454 else if (sp
->srefs_chain
&& relaxing
== false)
456 /* If previously was undefined, then remember as defined */
457 undefined_global_sym_count
--;
462 if (sp
->scoms_chain
== (asymbol
**) NULL
463 && sp
->srefs_chain
== (asymbol
**) NULL
464 && sp
->sdefs_chain
== (asymbol
**) NULL
)
466 /* And it's the first time we've seen it */
467 undefined_global_sym_count
++;
471 refize (sp
, nlist_p
);
475 ASSERT (sp
->sdefs_chain
== 0 || sp
->scoms_chain
== 0);
476 ASSERT (sp
->scoms_chain
== 0 || (*(sp
->scoms_chain
))->udata
== 0);
482 Q_enter_file_symbols (entry
)
483 lang_input_statement_type
*entry
;
487 entry
->common_section
=
488 bfd_make_section_old_way (entry
->the_bfd
, "COMMON");
490 ldlang_add_file (entry
);
493 if (trace_files
|| option_v
)
495 info ("%I\n", entry
);
498 total_symbols_seen
+= entry
->symbol_count
;
500 if (entry
->symbol_count
)
502 for (q
= entry
->asymbols
; *q
; q
++)
506 if (p
->flags
& BSF_INDIRECT
)
510 else if (p
->flags
& BSF_WARNING
)
514 else if (p
->section
== &bfd_und_section
515 || (p
->flags
& BSF_GLOBAL
)
516 || p
->section
== &bfd_com_section
517 || (p
->flags
& BSF_CONSTRUCTOR
))
523 if (p
->flags
& BSF_INDIRECT
)
527 else if (p
->flags
& BSF_WARNING
)
531 else if (p
->section
== &bfd_und_section
532 || (p
->flags
& BSF_GLOBAL
)
533 || p
->section
== &bfd_com_section
534 || (p
->flags
& BSF_CONSTRUCTOR
))
536 Q_enter_global_ref (q
, p
->name
);
544 /* Searching libraries */
546 struct lang_input_statement_struct
*decode_library_subfile ();
547 void linear_library (), symdef_library ();
549 /* Search the library ENTRY, already open on descriptor DESC.
550 This means deciding which library members to load,
551 making a chain of `struct lang_input_statement_struct' for those members,
552 and entering their global symbols in the hash table. */
555 search_library (entry
)
556 struct lang_input_statement_struct
*entry
;
559 /* No need to load a library if no undefined symbols */
560 if (!undefined_global_sym_count
)
563 if (bfd_has_map (entry
->the_bfd
))
564 symdef_library (entry
);
566 linear_library (entry
);
573 gnu960_check_format (abfd
, format
)
579 if ((bfd_check_format (abfd
, format
) == true)
580 && (abfd
->xvec
->flavour
== output_flavor
))
592 ldmain_open_file_read_symbol (entry
)
593 struct lang_input_statement_struct
*entry
;
595 if (entry
->asymbols
== (asymbol
**) NULL
596 && entry
->real
== true
597 && entry
->filename
!= (char *) NULL
)
599 ldfile_open_file (entry
);
603 if (gnu960_check_format (entry
->the_bfd
, bfd_object
))
605 if (bfd_check_format (entry
->the_bfd
, bfd_object
))
608 entry
->the_bfd
->usrdata
= (PTR
) entry
;
611 Q_read_entry_symbols (entry
->the_bfd
, entry
);
613 /* look through the sections in the file and see if any of them
615 ldlang_check_for_constructors (entry
);
617 Q_enter_file_symbols (entry
);
620 else if (gnu960_check_format (entry
->the_bfd
, bfd_archive
))
622 else if (bfd_check_format (entry
->the_bfd
, bfd_archive
))
625 entry
->the_bfd
->usrdata
= (PTR
) entry
;
627 entry
->subfiles
= (lang_input_statement_type
*) NULL
;
628 search_library (entry
);
632 einfo ("%F%B: malformed input file (not rel or archive) \n",
639 /* Construct and return a lang_input_statement_struct for a library member.
640 The library's lang_input_statement_struct is library_entry,
641 and the library is open on DESC.
642 SUBFILE_OFFSET is the byte index in the library of this member's header.
643 We store the length of the member into *LENGTH_LOC. */
645 lang_input_statement_type
*
646 decode_library_subfile (library_entry
, subfile_offset
)
647 struct lang_input_statement_struct
*library_entry
;
650 register struct lang_input_statement_struct
*subentry
;
651 subentry
= (struct lang_input_statement_struct
*) ldmalloc ((bfd_size_type
) (sizeof (struct lang_input_statement_struct
)));
653 subentry
->filename
= subfile_offset
->filename
;
654 subentry
->local_sym_name
= subfile_offset
->filename
;
655 subentry
->asymbols
= 0;
656 subentry
->the_bfd
= subfile_offset
;
657 subentry
->subfiles
= 0;
659 subentry
->superfile
= library_entry
;
660 subentry
->is_archive
= false;
662 subentry
->just_syms_flag
= false;
663 subentry
->loaded
= false;
669 boolean
subfile_wanted_p ();
671 clear_syms (entry
, offset
)
672 struct lang_input_statement_struct
*entry
;
676 unsigned long indx
= bfd_get_next_mapent (entry
->the_bfd
,
680 while (indx
!= BFD_NO_MORE_SYMBOLS
)
682 if (car
->file_offset
== offset
)
686 indx
= bfd_get_next_mapent (entry
->the_bfd
, indx
, &car
);
691 /* Search a library that has a map
694 symdef_library (entry
)
695 struct lang_input_statement_struct
*entry
;
698 register struct lang_input_statement_struct
*prev
= 0;
700 boolean not_finished
= true;
702 while (not_finished
== true)
704 carsym
*exported_library_name
;
705 bfd
*prev_archive_member_bfd
= 0;
707 int idx
= bfd_get_next_mapent (entry
->the_bfd
,
709 &exported_library_name
);
711 not_finished
= false;
713 while (idx
!= BFD_NO_MORE_SYMBOLS
&& undefined_global_sym_count
)
716 if (exported_library_name
->name
)
719 ldsym_type
*sp
= ldsym_get_soft (exported_library_name
->name
);
721 /* If we find a symbol that appears to be needed, think carefully
722 about the archive member that the symbol is in. */
723 /* So - if it exists, and is referenced somewhere and is
725 if (sp
&& sp
->srefs_chain
&& !sp
->sdefs_chain
)
727 bfd
*archive_member_bfd
= bfd_get_elt_at_index (entry
->the_bfd
, idx
);
728 struct lang_input_statement_struct
*archive_member_lang_input_statement_struct
;
731 if (archive_member_bfd
&& gnu960_check_format (archive_member_bfd
, bfd_object
))
733 if (archive_member_bfd
&& bfd_check_format (archive_member_bfd
, bfd_object
))
737 /* Don't think carefully about any archive member
738 more than once in a given pass. */
739 if (prev_archive_member_bfd
!= archive_member_bfd
)
742 prev_archive_member_bfd
= archive_member_bfd
;
744 /* Read the symbol table of the archive member. */
746 if (archive_member_bfd
->usrdata
!= (PTR
) NULL
)
749 archive_member_lang_input_statement_struct
= (lang_input_statement_type
*) archive_member_bfd
->usrdata
;
754 archive_member_lang_input_statement_struct
=
755 decode_library_subfile (entry
, archive_member_bfd
);
756 archive_member_bfd
->usrdata
= (PTR
) archive_member_lang_input_statement_struct
;
760 if (archive_member_lang_input_statement_struct
== 0)
762 einfo ("%F%I contains invalid archive member %s\n",
766 if (archive_member_lang_input_statement_struct
->loaded
== false)
769 Q_read_entry_symbols (archive_member_bfd
, archive_member_lang_input_statement_struct
);
770 /* Now scan the symbol table and decide whether to load. */
773 if (subfile_wanted_p (archive_member_lang_input_statement_struct
) == true)
776 /* This member is needed; load it.
777 Since we are loading something on this pass,
778 we must make another pass through the symdef data. */
782 Q_enter_file_symbols (archive_member_lang_input_statement_struct
);
785 prev
->chain
= archive_member_lang_input_statement_struct
;
787 entry
->subfiles
= archive_member_lang_input_statement_struct
;
790 prev
= archive_member_lang_input_statement_struct
;
793 /* Clear out this member's symbols from the symdef data
794 so that following passes won't waste time on them. */
795 clear_syms (entry
, exported_library_name
->file_offset
);
796 archive_member_lang_input_statement_struct
->loaded
= true;
803 idx
= bfd_get_next_mapent (entry
->the_bfd
, idx
, &exported_library_name
);
809 linear_library (entry
)
810 struct lang_input_statement_struct
*entry
;
812 boolean more_to_do
= true;
813 register struct lang_input_statement_struct
*prev
= 0;
815 if (entry
->complained
== false)
817 einfo ("%P: library %s has bad table of contents, rerun ranlib\n",
818 entry
->the_bfd
->filename
);
819 entry
->complained
= true;
824 bfd
*archive
= bfd_openr_next_archived_file (entry
->the_bfd
, 0);
830 if (gnu960_check_format (archive
, bfd_object
))
832 if (bfd_check_format (archive
, bfd_object
))
835 register struct lang_input_statement_struct
*subentry
;
837 subentry
= decode_library_subfile (entry
,
840 archive
->usrdata
= (PTR
) subentry
;
843 if (subentry
->loaded
== false)
845 Q_read_entry_symbols (archive
, subentry
);
847 if (subfile_wanted_p (subentry
) == true)
849 Q_enter_file_symbols (subentry
);
852 prev
->chain
= subentry
;
854 entry
->subfiles
= subentry
;
858 subentry
->loaded
= true;
862 archive
= bfd_openr_next_archived_file (entry
->the_bfd
, archive
);
869 /* ENTRY is an entry for a file inside an archive
870 Its symbols have been read into core, but not entered into the
872 Return nonzero if we ought to load this file */
875 subfile_wanted_p (entry
)
876 struct lang_input_statement_struct
*entry
;
880 for (q
= entry
->asymbols
; *q
; q
++)
884 /* If the symbol has an interesting definition, we could
885 potentially want it. */
887 if (p
->flags
& BSF_INDIRECT
)
889 /** add_indirect(q);*/
892 if (p
->section
== &bfd_com_section
893 || (p
->flags
& BSF_GLOBAL
)
894 || (p
->flags
& BSF_INDIRECT
))
896 register ldsym_type
*sp
= ldsym_get_soft (p
->name
);
898 /* If this symbol has not been hashed,
899 we can't be looking for it. */
900 if (sp
!= (ldsym_type
*) NULL
901 && sp
->sdefs_chain
== (asymbol
**) NULL
)
903 if (sp
->srefs_chain
!= (asymbol
**) NULL
904 || sp
->scoms_chain
!= (asymbol
**) NULL
)
906 /* This is a symbol we are looking for. It is either
907 not yet defined or common. */
909 if (p
->section
== &bfd_com_section
)
912 /* If the symbol in the table is a constructor, we won't to
913 anything fancy with it */
914 if ((sp
->flags
& SYM_CONSTRUCTOR
) == 0)
916 /* This libary member has something to
917 say about this element. We should
918 remember if its a new size */
919 /* Move something from the ref list to the com list */
922 /* Already a common symbol, maybe update it */
923 if (p
->value
> (*(sp
->scoms_chain
))->value
)
925 (*(sp
->scoms_chain
))->value
= p
->value
;
930 /* Take a value from the ref chain
931 Here we are moving a symbol from the owning bfd
932 to another bfd. We must set up the
933 common_section portion of the bfd thing */
937 sp
->scoms_chain
= sp
->srefs_chain
;
939 (asymbol
**) ((*(sp
->srefs_chain
))->udata
);
940 (*(sp
->scoms_chain
))->udata
= (PTR
) NULL
;
942 (*(sp
->scoms_chain
))->section
=
944 (*(sp
->scoms_chain
))->flags
= 0;
945 /* Remember the size of this item */
946 sp
->scoms_chain
[0]->value
= p
->value
;
948 undefined_global_sym_count
--;
951 asymbol
*com
= *(sp
->scoms_chain
);
953 if (((lang_input_statement_type
*)
954 (com
->the_bfd
->usrdata
))->common_section
==
957 ((lang_input_statement_type
*)
958 (com
->the_bfd
->usrdata
))->common_section
=
959 bfd_make_section_old_way (com
->the_bfd
, "COMMON");
963 ASSERT (p
->udata
== 0);
970 info ("%I needed due to %s\n", entry
, sp
->name
);