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
;
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
;
118 args_type command_line
;
119 ld_config_type config
;
126 program_name
= argv
[0];
127 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. */
154 config
.relocateable_output
= false;
156 command_line
.force_common_definition
= false;
158 init_bfd_error_vector();
162 config
.make_executable
= true;
163 force_make_executable
= false;
166 /* Initialize the cumulative counts of symbols. */
167 undefined_global_sym_count
= 0;
169 multiple_def_count
= 0;
172 config
.magic_demand_paged
= true;
173 config
.text_read_only
= true;
174 config
.make_executable
= true;
175 if (emulation
== (char *)NULL
) {
176 emulation
= DEFAULT_EMULATION
;
179 ldemul_choose_mode(emulation
);
180 default_target
= ldemul_choose_target();
182 ldemul_before_parse();
183 lang_has_input_file
= false;
184 parse_args(argc
, argv
);
189 info("%P: mode %s\n", emulation
);
192 if (lang_has_input_file
== false) {
193 einfo("%P%F: No input files\n");
196 ldemul_after_parse();
199 if (config
.map_filename
)
201 if (strcmp(config
.map_filename
, "-") == 0)
203 config
.map_file
= stdout
;
206 config
.map_file
= fopen(config
.map_filename
, FOPEN_WT
);
207 if (config
.map_file
== (FILE *)NULL
)
209 einfo("%P%F: can't open map file %s\n",
210 config
.map_filename
);
218 /* Print error messages for any missing symbols, for any warning
219 symbols, and possibly multiple definitions */
222 if (config
.text_read_only
)
224 /* Look for a text section and mark the readonly attribute in it */
225 asection
*found
= bfd_get_section_by_name(output_bfd
, ".text");
226 if (found
!= (asection
*)NULL
) {
227 found
->flags
|= SEC_READONLY
;
231 if (config
.relocateable_output
) {
232 output_bfd
->flags
&= ~EXEC_P
;
235 bfd_close(output_bfd
);
241 output_bfd
->flags
|= EXEC_P
;
246 if (config
.make_executable
== false && force_make_executable
==false)
249 if (trace_files
== true)
251 einfo("%P: Link errors found, deleting executable `%s'\n",
255 if (output_bfd
->iostream
)
256 fclose((FILE*)(output_bfd
->iostream
));
258 unlink(output_filename
);
262 bfd_close(output_bfd
);
273 Q_read_entry_symbols (desc
, entry
)
275 struct lang_input_statement_struct
*entry
;
277 if (entry
->asymbols
== (asymbol
**)NULL
) {
278 bfd_size_type table_size
= get_symtab_upper_bound(desc
);
279 entry
->asymbols
= (asymbol
**)ldmalloc(table_size
);
280 entry
->symbol_count
= bfd_canonicalize_symtab(desc
, entry
->asymbols
) ;
286 * turn this item into a reference
293 asymbol
*sym
= *nlist_p
;
296 sym
->section
= &bfd_und_section
;
297 sym
->udata
=(PTR
)( sp
->srefs_chain
);
298 sp
->srefs_chain
= nlist_p
;
301 This function is called for each name which is seen which has a global
302 scope. It enters the name into the global symbol table in the correct
303 symbol on the correct chain. Remember that each ldsym_type has three
304 chains attatched, one of all definitions of a symbol, one of all
305 references of a symbol and one of all common definitions of a symbol.
307 When the function is over, the supplied is left connected to the bfd
308 to which is was born, with its udata field pointing to the next member
309 on the chain in which it has been inserted.
311 A certain amount of jigery pokery is necessary since commons come
312 along and upset things, we only keep one item in the common chain; the
313 one with the biggest size seen sofar. When another common comes along
314 it either bumps the previous definition into the ref chain, since it
315 is bigger, or gets turned into a ref on the spot since the one on the
316 common chain is already bigger. If a real definition comes along then
317 the common gets bumped off anyway.
319 Whilst all this is going on we keep a count of the number of multiple
320 definitions seen, undefined global symbols and pending commons.
323 extern boolean relaxing
;
326 DEFUN(Q_enter_global_ref
,(nlist_p
, name
),
327 asymbol
**nlist_p AND
/* pointer into symbol table from incoming bfd */
328 CONST
char *name
/* name of symbol in linker table */ )
330 asymbol
*sym
= *nlist_p
;
333 /* Lookup the name from the incoming bfd's symbol table in the
334 linker's global symbol table */
337 flagword this_symbol_flags
= sym
->flags
;
338 sp
= ldsym_get (name
);
340 ASSERT(sym
->udata
== 0);
343 if (flag_is_constructor(this_symbol_flags
)) {
344 /* Add this constructor to the list we keep */
345 ldlang_add_constructor(sp
);
346 /* Turn any commons into refs */
347 if (sp
->scoms_chain
!= (asymbol
**)NULL
) {
348 refize(sp
, sp
->scoms_chain
);
355 if (sym
->section
== &bfd_com_section
) {
356 /* If we have a definition of this symbol already then
357 this common turns into a reference. Also we only
358 ever point to the largest common, so if we
359 have a common, but it's bigger that the new symbol
360 the turn this into a reference too. */
363 /* This is a common symbol, but we already have a definition
364 for it, so just link it into the ref chain as if
365 it were a reference */
368 else if (sp
->scoms_chain
) {
369 /* If we have a previous common, keep only the biggest */
370 if ( (*(sp
->scoms_chain
))->value
> sym
->value
) {
371 /* other common is bigger, throw this one away */
374 else if (sp
->scoms_chain
!= nlist_p
) {
375 /* other common is smaller, throw that away */
376 refize(sp
, sp
->scoms_chain
);
377 sp
->scoms_chain
= nlist_p
;
381 /* This is the first time we've seen a common, so remember it
382 - if it was undefined before, we know it's defined now. If
383 the symbol has been marked as really being a constructor,
384 then treat this as a ref
386 if (sp
->flags
& SYM_CONSTRUCTOR
) {
387 /* Turn this into a ref */
391 /* treat like a common */
393 undefined_global_sym_count
--;
396 sp
->scoms_chain
= nlist_p
;
401 else if (sym
->section
!= &bfd_und_section
) {
402 /* This is the definition of a symbol, add to def chain */
403 if (sp
->sdefs_chain
&& (*(sp
->sdefs_chain
))->section
!= sym
->section
) {
404 /* Multiple definition */
405 asymbol
*sy
= *(sp
->sdefs_chain
);
406 lang_input_statement_type
*stat
= (lang_input_statement_type
*) sy
->the_bfd
->usrdata
;
407 lang_input_statement_type
*stat1
= (lang_input_statement_type
*) sym
->the_bfd
->usrdata
;
408 asymbol
** stat1_symbols
= stat1
? stat1
->asymbols
: 0;
409 asymbol
** stat_symbols
= stat
? stat
->asymbols
:0;
411 multiple_def_count
++;
412 einfo("%X%C: multiple definition of `%T'\n",
413 sym
->the_bfd
, sym
->section
, stat1_symbols
, sym
->value
, sym
);
415 einfo("%X%C: first seen here\n",
416 sy
->the_bfd
, sy
->section
, stat_symbols
, sy
->value
);
419 sym
->udata
=(PTR
)( sp
->sdefs_chain
);
420 sp
->sdefs_chain
= nlist_p
;
422 /* A definition overrides a common symbol */
423 if (sp
->scoms_chain
) {
424 refize(sp
, sp
->scoms_chain
);
428 else if (sp
->srefs_chain
&& relaxing
== false) {
429 /* If previously was undefined, then remember as defined */
430 undefined_global_sym_count
--;
434 if (sp
->scoms_chain
== (asymbol
**)NULL
435 && sp
->srefs_chain
== (asymbol
**)NULL
436 && sp
->sdefs_chain
== (asymbol
**)NULL
) {
437 /* And it's the first time we've seen it */
438 undefined_global_sym_count
++;
446 ASSERT(sp
->sdefs_chain
== 0 || sp
->scoms_chain
== 0);
447 ASSERT(sp
->scoms_chain
==0 || (*(sp
->scoms_chain
))->udata
== 0);
453 Q_enter_file_symbols (entry
)
454 lang_input_statement_type
*entry
;
458 entry
->common_section
=
459 bfd_make_section_old_way(entry
->the_bfd
, "COMMON");
461 ldlang_add_file(entry
);
464 if (trace_files
|| option_v
) {
468 total_symbols_seen
+= entry
->symbol_count
;
470 if (entry
->symbol_count
)
471 for (q
= entry
->asymbols
; *q
; q
++)
475 if (p
->flags
& BSF_INDIRECT
)
479 else if (p
->flags
& BSF_WARNING
)
483 else if (p
->section
== &bfd_und_section
484 || (p
->flags
& BSF_GLOBAL
)
485 || p
->section
== &bfd_com_section
486 || (p
->flags
& BSF_CONSTRUCTOR
))
488 Q_enter_global_ref(q
, p
->name
);
497 /* Searching libraries */
499 struct lang_input_statement_struct
*decode_library_subfile ();
500 void linear_library (), symdef_library ();
502 /* Search the library ENTRY, already open on descriptor DESC.
503 This means deciding which library members to load,
504 making a chain of `struct lang_input_statement_struct' for those members,
505 and entering their global symbols in the hash table. */
508 search_library (entry
)
509 struct lang_input_statement_struct
*entry
;
512 /* No need to load a library if no undefined symbols */
513 if (!undefined_global_sym_count
) return;
515 if (bfd_has_map(entry
->the_bfd
))
516 symdef_library (entry
);
518 linear_library (entry
);
526 gnu960_check_format (abfd
, format
)
532 if ((bfd_check_format(abfd
,format
) == true)
533 && (abfd
->xvec
->flavour
== output_flavor
) ){
543 ldmain_open_file_read_symbol (entry
)
544 struct lang_input_statement_struct
*entry
;
546 if (entry
->asymbols
== (asymbol
**)NULL
547 &&entry
->real
== true
548 && entry
->filename
!= (char *)NULL
)
550 ldfile_open_file (entry
);
554 if (gnu960_check_format(entry
->the_bfd
, bfd_object
))
556 if (bfd_check_format(entry
->the_bfd
, bfd_object
))
559 entry
->the_bfd
->usrdata
= (PTR
)entry
;
562 Q_read_entry_symbols (entry
->the_bfd
, entry
);
564 /* look through the sections in the file and see if any of them
566 ldlang_check_for_constructors (entry
);
568 Q_enter_file_symbols (entry
);
571 else if (gnu960_check_format(entry
->the_bfd
, bfd_archive
))
573 else if (bfd_check_format(entry
->the_bfd
, bfd_archive
))
576 entry
->the_bfd
->usrdata
= (PTR
)entry
;
578 entry
->subfiles
= (lang_input_statement_type
*)NULL
;
579 search_library (entry
);
583 einfo("%F%B: malformed input file (not rel or archive) \n",
591 /* Construct and return a lang_input_statement_struct for a library member.
592 The library's lang_input_statement_struct is library_entry,
593 and the library is open on DESC.
594 SUBFILE_OFFSET is the byte index in the library of this member's header.
595 We store the length of the member into *LENGTH_LOC. */
597 lang_input_statement_type
*
598 decode_library_subfile (library_entry
, subfile_offset
)
599 struct lang_input_statement_struct
*library_entry
;
602 register struct lang_input_statement_struct
*subentry
;
603 subentry
= (struct lang_input_statement_struct
*) ldmalloc ((bfd_size_type
)(sizeof (struct lang_input_statement_struct
)));
604 subentry
->filename
= subfile_offset
-> filename
;
605 subentry
->local_sym_name
= subfile_offset
->filename
;
606 subentry
->asymbols
= 0;
607 subentry
->the_bfd
= subfile_offset
;
608 subentry
->subfiles
= 0;
610 subentry
->superfile
= library_entry
;
611 subentry
->is_archive
= false;
613 subentry
->just_syms_flag
= false;
614 subentry
->loaded
= false;
620 boolean
subfile_wanted_p ();
622 clear_syms(entry
, offset
)
623 struct lang_input_statement_struct
*entry
;
627 unsigned long indx
= bfd_get_next_mapent(entry
->the_bfd
,
630 while (indx
!= BFD_NO_MORE_SYMBOLS
) {
631 if (car
->file_offset
== offset
) {
634 indx
= bfd_get_next_mapent(entry
->the_bfd
, indx
, &car
);
639 /* Search a library that has a map
642 symdef_library (entry
)
643 struct lang_input_statement_struct
*entry
;
646 register struct lang_input_statement_struct
*prev
= 0;
648 boolean not_finished
= true;
651 while (not_finished
== true)
653 carsym
*exported_library_name
;
654 bfd
*prev_archive_member_bfd
= 0;
656 int idx
= bfd_get_next_mapent(entry
->the_bfd
,
658 &exported_library_name
);
660 not_finished
= false;
662 while (idx
!= BFD_NO_MORE_SYMBOLS
&& undefined_global_sym_count
)
665 if (exported_library_name
->name
)
668 ldsym_type
*sp
= ldsym_get_soft (exported_library_name
->name
);
670 /* If we find a symbol that appears to be needed, think carefully
671 about the archive member that the symbol is in. */
672 /* So - if it exists, and is referenced somewhere and is
674 if (sp
&& sp
->srefs_chain
&& !sp
->sdefs_chain
)
676 bfd
*archive_member_bfd
= bfd_get_elt_at_index(entry
->the_bfd
, idx
);
677 struct lang_input_statement_struct
*archive_member_lang_input_statement_struct
;
680 if (archive_member_bfd
&& gnu960_check_format(archive_member_bfd
, bfd_object
))
682 if (archive_member_bfd
&& bfd_check_format(archive_member_bfd
, bfd_object
))
686 /* Don't think carefully about any archive member
687 more than once in a given pass. */
688 if (prev_archive_member_bfd
!= archive_member_bfd
)
691 prev_archive_member_bfd
= archive_member_bfd
;
693 /* Read the symbol table of the archive member. */
695 if (archive_member_bfd
->usrdata
!= (PTR
)NULL
) {
697 archive_member_lang_input_statement_struct
=(lang_input_statement_type
*) archive_member_bfd
->usrdata
;
701 archive_member_lang_input_statement_struct
=
702 decode_library_subfile (entry
, archive_member_bfd
);
703 archive_member_bfd
->usrdata
= (PTR
) archive_member_lang_input_statement_struct
;
707 if (archive_member_lang_input_statement_struct
== 0) {
708 einfo ("%F%I contains invalid archive member %s\n",
712 if (archive_member_lang_input_statement_struct
->loaded
== false)
715 Q_read_entry_symbols (archive_member_bfd
, archive_member_lang_input_statement_struct
);
716 /* Now scan the symbol table and decide whether to load. */
719 if (subfile_wanted_p (archive_member_lang_input_statement_struct
) == true)
722 /* This member is needed; load it.
723 Since we are loading something on this pass,
724 we must make another pass through the symdef data. */
728 Q_enter_file_symbols (archive_member_lang_input_statement_struct
);
731 prev
->chain
= archive_member_lang_input_statement_struct
;
733 entry
->subfiles
= archive_member_lang_input_statement_struct
;
736 prev
= archive_member_lang_input_statement_struct
;
739 /* Clear out this member's symbols from the symdef data
740 so that following passes won't waste time on them. */
741 clear_syms(entry
, exported_library_name
->file_offset
);
742 archive_member_lang_input_statement_struct
->loaded
= true;
749 idx
= bfd_get_next_mapent(entry
->the_bfd
, idx
, &exported_library_name
);
755 linear_library (entry
)
756 struct lang_input_statement_struct
*entry
;
758 boolean more_to_do
= true;
759 register struct lang_input_statement_struct
*prev
= 0;
761 if (entry
->complained
== false) {
762 einfo("%P: library %s has bad table of contents, rerun ranlib\n",
763 entry
->the_bfd
->filename
);
764 entry
->complained
= true;
768 bfd
* archive
= bfd_openr_next_archived_file(entry
->the_bfd
,0);
773 if (gnu960_check_format(archive
, bfd_object
))
775 if (bfd_check_format(archive
, bfd_object
))
778 register struct lang_input_statement_struct
*subentry
;
780 subentry
= decode_library_subfile (entry
,
783 archive
->usrdata
= (PTR
) subentry
;
784 if (!subentry
) return;
785 if (subentry
->loaded
== false) {
786 Q_read_entry_symbols (archive
, subentry
);
788 if (subfile_wanted_p (subentry
) == true)
790 Q_enter_file_symbols (subentry
);
793 prev
->chain
= subentry
;
795 entry
->subfiles
= subentry
;
799 subentry
->loaded
= true;
803 archive
= bfd_openr_next_archived_file(entry
->the_bfd
,archive
);
810 /* ENTRY is an entry for a file inside an archive
811 Its symbols have been read into core, but not entered into the
813 Return nonzero if we ought to load this file */
816 subfile_wanted_p (entry
)
817 struct lang_input_statement_struct
*entry
;
821 for (q
= entry
->asymbols
; *q
; q
++)
825 /* If the symbol has an interesting definition, we could
826 potentially want it. */
828 if (p
->flags
& BSF_INDIRECT
)
830 /** add_indirect(q);*/
833 if (p
->section
== &bfd_com_section
834 || (p
->flags
& BSF_GLOBAL
)
835 || (p
->flags
& BSF_INDIRECT
))
837 register ldsym_type
*sp
= ldsym_get_soft (p
->name
);
840 /* If this symbol has not been hashed,
841 we can't be looking for it. */
842 if (sp
!= (ldsym_type
*)NULL
843 && sp
->sdefs_chain
== (asymbol
**)NULL
) {
844 if (sp
->srefs_chain
!= (asymbol
**)NULL
845 || sp
->scoms_chain
!= (asymbol
**)NULL
)
847 /* This is a symbol we are looking for. It is either
848 not yet defined or common. */
850 if (p
->section
== &bfd_com_section
)
853 /* If the symbol in the table is a constructor, we won't to
854 anything fancy with it */
855 if ((sp
->flags
& SYM_CONSTRUCTOR
) == 0) {
856 /* This libary member has something to
857 say about this element. We should
858 remember if its a new size */
859 /* Move something from the ref list to the com list */
860 if(sp
->scoms_chain
) {
861 /* Already a common symbol, maybe update it */
862 if (p
->value
> (*(sp
->scoms_chain
))->value
) {
863 (*(sp
->scoms_chain
))->value
= p
->value
;
867 /* Take a value from the ref chain
868 Here we are moving a symbol from the owning bfd
869 to another bfd. We must set up the
870 common_section portion of the bfd thing */
874 sp
->scoms_chain
= sp
->srefs_chain
;
876 (asymbol
**)((*(sp
->srefs_chain
))->udata
);
877 (*(sp
->scoms_chain
))->udata
= (PTR
)NULL
;
879 (*( sp
->scoms_chain
))->section
=
881 (*( sp
->scoms_chain
))->flags
= 0;
882 /* Remember the size of this item */
883 sp
->scoms_chain
[0]->value
= p
->value
;
885 undefined_global_sym_count
--;
887 asymbol
*com
= *(sp
->scoms_chain
);
888 if (((lang_input_statement_type
*)
889 (com
->the_bfd
->usrdata
))->common_section
==
891 ((lang_input_statement_type
*)
892 (com
->the_bfd
->usrdata
))->common_section
=
893 bfd_make_section_old_way(com
->the_bfd
, "COMMON");
897 ASSERT(p
->udata
== 0);
903 info("%I needed due to %s\n",entry
, sp
->name
);