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
44 extern boolean lang_has_input_file
;
45 extern boolean trace_files
;
49 char *output_filename
= "a.out";
50 /* Name this program was invoked by. */
53 /* The file that we're creating */
56 extern boolean option_v
;
58 /* The local symbol prefix */
61 /* Count the number of global symbols multiply defined. */
62 int multiple_def_count
;
65 /* Count the number of symbols defined through common declarations.
66 This count is referenced in symdef_library, linear_library, and
67 modified by enter_global_ref.
69 It is incremented when a symbol is created as a common, and
70 decremented when the common declaration is overridden
72 Another way of thinking of it is that this is a count of
73 all ldsym_types with a ->scoms field
75 unsigned int commons_pending
;
78 /* Count the number of global symbols referenced and not defined.
79 common symbols are not included in this count.
82 unsigned int undefined_global_sym_count
;
86 /* Count the number of warning symbols encountered. */
89 /* have we had a load script ? */
90 extern boolean had_script
;
94 /* Nonzero means print names of input files as processed. */
99 /* 1 => write load map. */
112 /* Force the make_executable to be output, even if there are non-fatal
114 boolean force_make_executable
;
117 /* A count of the total number of local symbols ever seen - by adding
118 the symbol_count field of each newly read afile.*/
121 unsigned int total_symbols_seen
;
123 /* A count of the number of read files - the same as the number of elements
126 unsigned int total_files_seen
;
130 args_type command_line
;
131 ld_config_type config
;
138 program_name
= argv
[0];
139 output_filename
= "a.out";
142 check_v960( argc
, argv
);
145 emulation
= (char *) getenv(EMULATION_ENVIRON
);
147 /* Initialize the data about options. */
153 config
.relocateable_output
= false;
155 command_line
.force_common_definition
= false;
158 ldfile_add_library_path("./");
159 config
.make_executable
= true;
160 force_make_executable
= false;
163 /* Initialize the cumulative counts of symbols. */
164 undefined_global_sym_count
= 0;
166 multiple_def_count
= 0;
169 config
.magic_demand_paged
= true ;
170 config
.make_executable
= true;
173 ldemul_choose_mode(LNK960_EMULATION_NAME
);
175 if (emulation
== (char *)NULL
) {
176 emulation
= DEFAULT_EMULATION
;
180 ldemul_choose_mode(emulation
);
183 default_target
= ldemul_choose_target();
186 ldemul_before_parse();
187 lang_has_input_file
= false;
188 parse_args(argc
, argv
);
191 info("%P: mode %s\n", emulation
);
193 if (lang_has_input_file
== false) {
194 info("%P%F: No input files\n");
197 ldemul_after_parse();
204 /* Print error messages for any missing symbols, for any warning
205 symbols, and possibly multiple definitions */
207 /* Print a map, if requested. */
210 ldsym_print_symbol_table ();
215 if (config
.relocateable_output
) {
216 output_bfd
->flags
&= ~( D_PAGED
);
217 output_bfd
->flags
|= EXEC_P
;
219 bfd_close(output_bfd
);
222 output_bfd
->flags
|= EXEC_P
;
225 bfd_close(output_bfd
);
226 if (config
.make_executable
== false && force_make_executable
== false) {
227 unlink(output_filename
);
229 return (!config
.make_executable
);
237 Q_read_entry_symbols (desc
, entry
)
239 struct lang_input_statement_struct
*entry
;
241 if (entry
->asymbols
== (asymbol
**)NULL
) {
242 size_t table_size
= get_symtab_upper_bound(desc
);
243 entry
->asymbols
= (asymbol
**)ldmalloc(table_size
);
245 entry
->symbol_count
= bfd_canonicalize_symtab(desc
, entry
->asymbols
) ;
251 * turn this item into a reference
258 asymbol
*sym
= *nlist_p
;
260 sym
->flags
= BSF_UNDEFINED
;
261 sym
->section
= (asection
*)NULL
;
262 sym
->udata
=(PTR
)( sp
->srefs_chain
);
263 sp
->srefs_chain
= nlist_p
;
266 This function is called for each name which is seen which has a global
267 scope. It enters the name into the global symbol table in the correct
268 symbol on the correct chain. Remember that each ldsym_type has three
269 chains attatched, one of all definitions of a symbol, one of all
270 references of a symbol and one of all common definitions of a symbol.
272 When the function is over, the supplied is left connected to the bfd
273 to which is was born, with its udata field pointing to the next member
274 on the chain in which it has been inserted.
276 A certain amount of jigery pokery is necessary since commons come
277 along and upset things, we only keep one item in the common chain; the
278 one with the biggest size seen sofar. When another common comes along
279 it either bumps the previous definition into the ref chain, since it
280 is bigger, or gets turned into a ref on the spot since the one on the
281 common chain is already bigger. If a real definition comes along then
282 the common gets bumped off anyway.
284 Whilst all this is going on we keep a count of the number of multiple
285 definitions seen, undefined global symbols and pending commons.
290 Q_enter_global_ref (nlist_p
)
294 asymbol
*sym
= *nlist_p
;
295 CONST
char *name
= sym
->name
;
296 ldsym_type
*sp
= ldsym_get (name
);
298 flagword this_symbol_flags
= sym
->flags
;
301 ASSERT(sym
->udata
== 0);
303 /* Just place onto correct chain */
304 if (flag_is_common(this_symbol_flags
)) {
305 /* If we have a definition of this symbol already then
306 * this common turns into a reference. Also we only
307 * ever point to the largest common, so if we
308 * have a common, but it's bigger that the new symbol
309 * the turn this into a reference too.
313 /* This is a common symbol, but we already have a definition
314 for it, so just link it into the ref chain as if
319 else if (sp
->scoms_chain
) {
320 /* If we have a previous common, keep only the biggest */
321 if ( (*(sp
->scoms_chain
))->value
> sym
->value
) {
322 /* other common is bigger, throw this one away */
325 else if (sp
->scoms_chain
!= nlist_p
) {
326 /* other common is smaller, throw that away */
327 refize(sp
, sp
->scoms_chain
);
328 sp
->scoms_chain
= nlist_p
;
332 /* This is the first time we've seen a common, so
333 * remember it - if it was undefined before, we know it's defined now
336 undefined_global_sym_count
--;
339 sp
->scoms_chain
= nlist_p
;
343 else if (flag_is_defined(this_symbol_flags
)) {
344 /* This is the definition of a symbol, add to def chain */
345 if (sp
->sdefs_chain
&& (*(sp
->sdefs_chain
))->section
!= sym
->section
) {
346 /* Multiple definition */
347 asymbol
*sy
= *(sp
->sdefs_chain
);
348 lang_input_statement_type
*stat
= (lang_input_statement_type
*) sy
->the_bfd
->usrdata
;
349 lang_input_statement_type
*stat1
= (lang_input_statement_type
*) sym
->the_bfd
->usrdata
;
350 asymbol
** stat1_symbols
= stat1
? stat1
->asymbols
: 0;
351 asymbol
** stat_symbols
= stat
? stat
->asymbols
:0;
353 multiple_def_count
++;
354 info("%C: multiple definition of `%T'\n",
361 info("%C: first seen here\n",
368 sym
->udata
=(PTR
)( sp
->sdefs_chain
);
369 sp
->sdefs_chain
= nlist_p
;
371 /* A definition overrides a common symbol */
372 if (sp
->scoms_chain
) {
373 refize(sp
, sp
->scoms_chain
);
377 else if (sp
->srefs_chain
) {
378 /* If previously was undefined, then remember as defined */
379 undefined_global_sym_count
--;
383 if (sp
->scoms_chain
== (asymbol
**)NULL
384 && sp
->srefs_chain
== (asymbol
**)NULL
385 && sp
->sdefs_chain
== (asymbol
**)NULL
) {
386 /* And it's the first time we've seen it */
387 undefined_global_sym_count
++;
394 ASSERT(sp
->sdefs_chain
== 0 || sp
->scoms_chain
== 0);
395 ASSERT(sp
->scoms_chain
==0 || (*(sp
->scoms_chain
))->udata
== 0);
401 Q_enter_file_symbols (entry
)
402 lang_input_statement_type
*entry
;
405 entry
->common_section
=
406 bfd_make_section(entry
->the_bfd
, "COMMON");
408 ldlang_add_file(entry
);
411 if (trace_files
|| option_v
) {
415 total_symbols_seen
+= entry
->symbol_count
;
417 for (q
= entry
->asymbols
; *q
; q
++)
421 if (flag_is_undefined_or_global_or_common(p
->flags
))
423 Q_enter_global_ref(q
);
425 ASSERT(p
->flags
!= 0);
431 /* Searching libraries */
433 struct lang_input_statement_struct
*decode_library_subfile ();
434 void linear_library (), symdef_library ();
436 /* Search the library ENTRY, already open on descriptor DESC.
437 This means deciding which library members to load,
438 making a chain of `struct lang_input_statement_struct' for those members,
439 and entering their global symbols in the hash table. */
442 search_library (entry
)
443 struct lang_input_statement_struct
*entry
;
446 /* No need to load a library if no undefined symbols */
447 if (!undefined_global_sym_count
) return;
449 if (bfd_has_map(entry
->the_bfd
))
450 symdef_library (entry
);
452 linear_library (entry
);
460 gnu960_check_format (abfd
, format
)
466 if ((bfd_check_format(abfd
,format
) == true) && BFD_COFF_FILE_P(abfd
)) {
474 ldmain_open_file_read_symbol (entry
)
475 struct lang_input_statement_struct
*entry
;
477 if (entry
->asymbols
== (asymbol
**)NULL
478 &&entry
->real
== true
479 && entry
->filename
!= (char *)NULL
)
481 ldfile_open_file (entry
);
484 if (gnu960_check_format(entry
->the_bfd
, bfd_object
))
486 if (bfd_check_format(entry
->the_bfd
, bfd_object
))
489 entry
->the_bfd
->usrdata
= (PTR
)entry
;
492 Q_read_entry_symbols (entry
->the_bfd
, entry
);
493 Q_enter_file_symbols (entry
);
496 else if (gnu960_check_format(entry
->the_bfd
, bfd_archive
))
498 else if (bfd_check_format(entry
->the_bfd
, bfd_archive
))
501 entry
->the_bfd
->usrdata
= (PTR
)entry
;
503 entry
->subfiles
= (lang_input_statement_type
*)NULL
;
504 search_library (entry
);
508 info("%F%B: malformed input file (not rel or archive) \n",
516 /* Construct and return a lang_input_statement_struct for a library member.
517 The library's lang_input_statement_struct is library_entry,
518 and the library is open on DESC.
519 SUBFILE_OFFSET is the byte index in the library of this member's header.
520 We store the length of the member into *LENGTH_LOC. */
522 lang_input_statement_type
*
523 decode_library_subfile (library_entry
, subfile_offset
)
524 struct lang_input_statement_struct
*library_entry
;
527 register struct lang_input_statement_struct
*subentry
;
528 subentry
= (struct lang_input_statement_struct
*) ldmalloc (sizeof (struct lang_input_statement_struct
));
529 subentry
->filename
= subfile_offset
-> filename
;
530 subentry
->local_sym_name
= subfile_offset
->filename
;
531 subentry
->asymbols
= 0;
532 subentry
->the_bfd
= subfile_offset
;
533 subentry
->subfiles
= 0;
535 subentry
->superfile
= library_entry
;
536 subentry
->is_archive
= false;
538 subentry
->just_syms_flag
= false;
539 subentry
->loaded
= false;
545 boolean
subfile_wanted_p ();
547 clear_syms(entry
, offset
)
548 struct lang_input_statement_struct
*entry
;
552 unsigned long indx
= bfd_get_next_mapent(entry
->the_bfd
,
555 while (indx
!= BFD_NO_MORE_SYMBOLS
) {
556 if (car
->file_offset
== offset
) {
559 indx
= bfd_get_next_mapent(entry
->the_bfd
, indx
, &car
);
564 /* Search a library that has a map
567 symdef_library (entry
)
568 struct lang_input_statement_struct
*entry
;
571 register struct lang_input_statement_struct
*prev
= 0;
573 boolean not_finished
= true;
576 while (not_finished
== true)
578 carsym
*exported_library_name
;
579 bfd
*prev_archive_member_bfd
= 0;
581 int idx
= bfd_get_next_mapent(entry
->the_bfd
,
583 &exported_library_name
);
585 not_finished
= false;
587 while (idx
!= BFD_NO_MORE_SYMBOLS
&& undefined_global_sym_count
)
590 if (exported_library_name
->name
)
593 ldsym_type
*sp
= ldsym_get_soft (exported_library_name
->name
);
595 /* If we find a symbol that appears to be needed, think carefully
596 about the archive member that the symbol is in. */
597 /* So - if it exists, and is referenced somewhere and is
599 if (sp
&& sp
->srefs_chain
&& !sp
->sdefs_chain
)
601 bfd
*archive_member_bfd
= bfd_get_elt_at_index(entry
->the_bfd
, idx
);
602 struct lang_input_statement_struct
*archive_member_lang_input_statement_struct
;
605 if (archive_member_bfd
&& gnu960_check_format(archive_member_bfd
, bfd_object
))
607 if (archive_member_bfd
&& bfd_check_format(archive_member_bfd
, bfd_object
))
611 /* Don't think carefully about any archive member
612 more than once in a given pass. */
613 if (prev_archive_member_bfd
!= archive_member_bfd
)
616 prev_archive_member_bfd
= archive_member_bfd
;
618 /* Read the symbol table of the archive member. */
620 if (archive_member_bfd
->usrdata
!= (PTR
)NULL
) {
622 archive_member_lang_input_statement_struct
=(lang_input_statement_type
*) archive_member_bfd
->usrdata
;
626 archive_member_lang_input_statement_struct
=
627 decode_library_subfile (entry
, archive_member_bfd
);
628 archive_member_bfd
->usrdata
= (PTR
) archive_member_lang_input_statement_struct
;
632 if (archive_member_lang_input_statement_struct
== 0) {
633 info ("%F%I contains invalid archive member %s\n",
638 if (archive_member_lang_input_statement_struct
->loaded
== false)
641 Q_read_entry_symbols (archive_member_bfd
, archive_member_lang_input_statement_struct
);
642 /* Now scan the symbol table and decide whether to load. */
645 if (subfile_wanted_p (archive_member_lang_input_statement_struct
) == true)
648 /* This member is needed; load it.
649 Since we are loading something on this pass,
650 we must make another pass through the symdef data. */
654 Q_enter_file_symbols (archive_member_lang_input_statement_struct
);
657 prev
->chain
= archive_member_lang_input_statement_struct
;
659 entry
->subfiles
= archive_member_lang_input_statement_struct
;
662 prev
= archive_member_lang_input_statement_struct
;
665 /* Clear out this member's symbols from the symdef data
666 so that following passes won't waste time on them. */
667 clear_syms(entry
, exported_library_name
->file_offset
);
668 archive_member_lang_input_statement_struct
->loaded
= true;
675 idx
= bfd_get_next_mapent(entry
->the_bfd
, idx
, &exported_library_name
);
681 linear_library (entry
)
682 struct lang_input_statement_struct
*entry
;
684 boolean more_to_do
= true;
685 register struct lang_input_statement_struct
*prev
= 0;
689 bfd
* archive
= bfd_openr_next_archived_file(entry
->the_bfd
,0);
694 if (gnu960_check_format(archive
, bfd_object
))
696 if (bfd_check_format(archive
, bfd_object
))
699 register struct lang_input_statement_struct
*subentry
;
701 subentry
= decode_library_subfile (entry
,
704 archive
->usrdata
= (PTR
) subentry
;
705 if (!subentry
) return;
706 if (subentry
->loaded
== false) {
707 Q_read_entry_symbols (archive
, subentry
);
709 if (subfile_wanted_p (subentry
) == true)
711 Q_enter_file_symbols (subentry
);
714 prev
->chain
= subentry
;
716 entry
->subfiles
= subentry
;
720 subentry
->loaded
= true;
724 archive
= bfd_openr_next_archived_file(entry
->the_bfd
,archive
);
731 /* ENTRY is an entry for a library member.
732 Its symbols have been read into core, but not entered.
733 Return nonzero if we ought to load this member. */
736 subfile_wanted_p (entry
)
737 struct lang_input_statement_struct
*entry
;
741 for (q
= entry
->asymbols
; *q
; q
++)
745 /* If the symbol has an interesting definition, we could
746 potentially want it. */
748 if (p
->flags
& BSF_FORT_COMM
749 || p
->flags
& BSF_GLOBAL
)
751 register ldsym_type
*sp
= ldsym_get_soft (p
->name
);
754 /* If this symbol has not been hashed,
755 we can't be looking for it. */
756 if (sp
!= (ldsym_type
*)NULL
757 && sp
->sdefs_chain
== (asymbol
**)NULL
) {
758 if (sp
->srefs_chain
!= (asymbol
**)NULL
759 || sp
->scoms_chain
!= (asymbol
**)NULL
)
761 /* This is a symbol we are looking for. It is either
762 not yet defined or common. */
764 if (flag_is_common(p
->flags
))
766 /* This libary member has something to
767 say about this element. We should
768 remember if its a new size */
769 /* Move something from the ref list to the com list */
770 if(sp
->scoms_chain
) {
771 /* Already a common symbol, maybe update it */
772 if (p
->value
> (*(sp
->scoms_chain
))->value
) {
773 (*(sp
->scoms_chain
))->value
= p
->value
;
777 /* Take a value from the ref chain
778 Here we are moving a symbol from the owning bfd
779 to another bfd. We must set up the
780 common_section portion of the bfd thing */
784 sp
->scoms_chain
= sp
->srefs_chain
;
786 (asymbol
**)((*(sp
->srefs_chain
))->udata
);
787 (*(sp
->scoms_chain
))->udata
= (PTR
)NULL
;
789 (*( sp
->scoms_chain
))->flags
= BSF_FORT_COMM
;
790 /* Remember the size of this item */
791 sp
->scoms_chain
[0]->value
= p
->value
;
793 undefined_global_sym_count
--;
795 asymbol
*com
= *(sp
->scoms_chain
);
796 if (((lang_input_statement_type
*)
797 (com
->the_bfd
->usrdata
))->common_section
==
799 ((lang_input_statement_type
*)
800 (com
->the_bfd
->usrdata
))->common_section
=
801 bfd_make_section(com
->the_bfd
, "COMMON");
804 ASSERT(p
->udata
== 0);
810 info("%I needed due to %s\n",entry
, sp
->name
);