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
25 * Revision 1.1 1991/03/21 21:28:52 gumby
28 * Revision 1.1 1991/03/13 00:48:27 chrisb
31 * Revision 1.7 1991/03/10 19:15:45 sac
32 * Fixed a prototype problem
34 * Revision 1.6 1991/03/10 09:31:32 rich
36 * Makefile config.h ld-emul.c ld-emul.h ld-gld.c ld-gld960.c
37 * ld-lnk960.c ld.h lddigest.c ldexp.c ldexp.h ldfile.c ldfile.h
38 * ldgram.y ldinfo.h ldlang.c ldlang.h ldlex.h ldlex.l ldmain.c
39 * ldmain.h ldmisc.c ldmisc.h ldsym.c ldsym.h ldversion.c
40 * ldversion.h ldwarn.h ldwrite.c ldwrite.h y.tab.h
42 * As of this round of changes, ld now builds on all hosts of (Intel960)
43 * interest and copy passes my copy test on big endian hosts again.
45 * Revision 1.5 1991/03/09 03:31:02 sac
46 * After a fatal info message, the output file is deleted.
48 * Revision 1.4 1991/03/06 02:28:31 sac
49 * Fixed partial linking and error messages.
51 * Revision 1.3 1991/02/22 17:15:02 sac
52 * Added RCS keywords and copyrights
74 extern boolean lang_has_input_file
;
79 char *output_filename
= "a.out";
80 /* Name this program was invoked by. */
83 /* The file that we're creating */
86 extern boolean option_v
;
88 /* The local symbol prefix */
91 /* Count the number of global symbols multiply defined. */
92 int multiple_def_count
;
95 /* Count the number of symbols defined through common declarations.
96 This count is referenced in symdef_library, linear_library, and
97 modified by enter_global_ref.
99 It is incremented when a symbol is created as a common, and
100 decremented when the common declaration is overridden
102 Another way of thinking of it is that this is a count of
103 all ldsym_types with a ->scoms field
105 unsigned int commons_pending
;
108 /* Count the number of global symbols referenced and not defined.
109 common symbols are not included in this count.
112 unsigned int undefined_global_sym_count
;
116 /* Count the number of warning symbols encountered. */
119 /* have we had a load script ? */
120 extern boolean had_script
;
124 /* Nonzero means print names of input files as processed. */
129 /* 1 => write load map. */
142 /* Force the make_executable to be output, even if there are non-fatal
144 boolean force_make_executable
;
147 /* A count of the total number of local symbols ever seen - by adding
148 the symbol_count field of each newly read afile.*/
151 unsigned int total_symbols_seen
;
153 /* A count of the number of read files - the same as the number of elements
156 unsigned int total_files_seen
;
160 args_type command_line
;
161 ld_config_type config
;
168 program_name
= argv
[0];
169 output_filename
= "a.out";
171 emulation
= getenv(EMULATION_ENVIRON
);
173 /* Initialize the data about options. */
174 strip_symbols
= STRIP_NONE
;
176 discard_locals
= DISCARD_NONE
;
179 config
.relocateable_output
= false;
181 command_line
.force_common_definition
= false;
185 config
.make_executable
= true;
186 force_make_executable
= false;
189 /* Initialize the cumulative counts of symbols. */
190 undefined_global_sym_count
= 0;
192 multiple_def_count
= 0;
195 config
.magic_demand_paged
= true ;
196 config
.make_executable
= true;
198 if (emulation
== (char *)NULL
) {
199 emulation
= DEFAULT_EMULATION
;
201 ldemul_choose_mode(emulation
);
203 default_target
= ldemul_choose_target();
206 ldemul_before_parse();
208 lang_has_input_file
= false;
209 parse_args(argc
, argv
);
211 if (lang_has_input_file
== false) {
212 info("%P%F: No input files\n");
215 ldemul_after_parse();
222 /* Print error messages for any missing symbols, for any warning
223 symbols, and possibly multiple definitions */
225 /* Print a map, if requested. */
228 ldsym_print_symbol_table ();
233 if (config
.relocateable_output
) {
234 output_bfd
->flags
&= ~( D_PAGED
);
235 output_bfd
->flags
|= EXEC_P
;
237 bfd_close(output_bfd
);
240 output_bfd
->flags
|= EXEC_P
;
243 bfd_close(output_bfd
);
244 if (config
.make_executable
== false && force_make_executable
== false) {
245 unlink(output_filename
);
247 return (!config
.make_executable
);
255 Q_read_entry_symbols (desc
, entry
)
257 struct lang_input_statement_struct
*entry
;
259 if (entry
->asymbols
== (asymbol
**)NULL
) {
260 size_t table_size
= get_symtab_upper_bound(desc
);
261 entry
->asymbols
= (asymbol
**)ldmalloc(table_size
);
263 entry
->symbol_count
= bfd_canonicalize_symtab(desc
, entry
->asymbols
) ;
269 * turn this item into a reference
276 asymbol
*sym
= *nlist_p
;
278 sym
->flags
= BSF_UNDEFINED
;
279 sym
->section
= (asection
*)NULL
;
280 sym
->udata
=(void *)( sp
->srefs_chain
);
281 sp
->srefs_chain
= nlist_p
;
284 This function is called for each name which is seen which has a global
285 scope. It enters the name into the global symbol table in the correct
286 symbol on the correct chain. Remember that each ldsym_type has three
287 chains attatched, one of all definitions of a symbol, one of all
288 references of a symbol and one of all common definitions of a symbol.
290 When the function is over, the supplied is left connected to the bfd
291 to which is was born, with its udata field pointing to the next member
292 on the chain in which it has been inserted.
294 A certain amount of jigery pokery is necessary since commons come
295 along and upset things, we only keep one item in the common chain; the
296 one with the biggest size seen sofar. When another common comes along
297 it either bumps the previous definition into the ref chain, since it
298 is bigger, or gets turned into a ref on the spot since the one on the
299 common chain is already bigger. If a real definition comes along then
300 the common gets bumped off anyway.
302 Whilst all this is going on we keep a count of the number of multiple
303 definitions seen, undefined global symbols and pending commons.
308 Q_enter_global_ref (nlist_p
)
312 asymbol
*sym
= *nlist_p
;
313 char *name
= sym
->name
;
314 ldsym_type
*sp
= ldsym_get (name
);
316 flagword this_symbol_flags
= sym
->flags
;
319 ASSERT(sym
->udata
== 0);
321 /* Just place onto correct chain */
322 if (flag_is_common(this_symbol_flags
)) {
323 /* If we have a definition of this symbol already then
324 * this common turns into a reference. Also we only
325 * ever point to the largest common, so if we
326 * have a common, but it's bigger that the new symbol
327 * the turn this into a reference too.
331 /* This is a common symbol, but we already have a definition
332 for it, so just link it into the ref chain as if
337 else if (sp
->scoms_chain
) {
338 /* If we have a previous common, keep only the biggest */
339 if ( (*(sp
->scoms_chain
))->value
> sym
->value
) {
340 /* other common is bigger, throw this one away */
343 else if (sp
->scoms_chain
!= nlist_p
) {
344 /* other common is smaller, throw that away */
345 refize(sp
, sp
->scoms_chain
);
346 sp
->scoms_chain
= nlist_p
;
350 /* This is the first time we've seen a common, so
351 * remember it - if it was undefined before, we know it's defined now
354 undefined_global_sym_count
--;
357 sp
->scoms_chain
= nlist_p
;
361 else if (flag_is_defined(this_symbol_flags
)) {
362 /* This is the definition of a symbol, add to def chain */
363 if (sp
->sdefs_chain
&& (*(sp
->sdefs_chain
))->section
!= sym
->section
) {
364 /* Multiple definition */
365 asymbol
*sy
= *(sp
->sdefs_chain
);
366 lang_input_statement_type
*stat
= (lang_input_statement_type
*) sy
->the_bfd
->usrdata
;
367 lang_input_statement_type
*stat1
= (lang_input_statement_type
*) sym
->the_bfd
->usrdata
;
368 asymbol
** stat1_symbols
= stat1
? stat1
->asymbols
: 0;
369 asymbol
** stat_symbols
= stat
? stat
->asymbols
:0;
371 multiple_def_count
++;
372 info("%C: multiple definition of `%T'\n",
379 info("%C: first seen here\n",
386 sym
->udata
=(void *)( sp
->sdefs_chain
);
387 sp
->sdefs_chain
= nlist_p
;
389 /* A definition overrides a common symbol */
390 if (sp
->scoms_chain
) {
391 refize(sp
, sp
->scoms_chain
);
395 else if (sp
->srefs_chain
) {
396 /* If previously was undefined, then remember as defined */
397 undefined_global_sym_count
--;
401 if (sp
->scoms_chain
== (asymbol
**)NULL
402 && sp
->srefs_chain
== (asymbol
**)NULL
403 && sp
->sdefs_chain
== (asymbol
**)NULL
) {
404 /* And it's the first time we've seen it */
405 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(p
->flags
))
442 Q_enter_global_ref(q
);
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
);
477 Q_read_file_symbols (entry
)
478 struct lang_input_statement_struct
*entry
;
480 if (entry
->asymbols
== (asymbol
**)NULL
481 &&entry
->real
== true
482 && entry
->filename
!= (char *)NULL
)
484 ldfile_open_file (entry
);
486 if (bfd_check_format(entry
->the_bfd
, bfd_object
))
488 entry
->the_bfd
->usrdata
= (void*)entry
;
491 Q_read_entry_symbols (entry
->the_bfd
, entry
);
492 Q_enter_file_symbols (entry
);
494 else if (bfd_check_format(entry
->the_bfd
, bfd_archive
))
496 entry
->the_bfd
->usrdata
= (void *)entry
;
498 entry
->subfiles
= (lang_input_statement_type
*)NULL
;
499 search_library (entry
);
503 info("%F%I: malformed input file (not rel or archive) \n", entry
);
510 /* Construct and return a lang_input_statement_struct for a library member.
511 The library's lang_input_statement_struct is library_entry,
512 and the library is open on DESC.
513 SUBFILE_OFFSET is the byte index in the library of this member's header.
514 We store the length of the member into *LENGTH_LOC. */
516 lang_input_statement_type
*
517 decode_library_subfile (library_entry
, subfile_offset
)
518 struct lang_input_statement_struct
*library_entry
;
521 register struct lang_input_statement_struct
*subentry
;
522 subentry
= (struct lang_input_statement_struct
*) ldmalloc (sizeof (struct lang_input_statement_struct
));
523 subentry
->filename
= subfile_offset
-> filename
;
524 subentry
->local_sym_name
= subfile_offset
->filename
;
525 subentry
->asymbols
= 0;
526 subentry
->the_bfd
= subfile_offset
;
527 subentry
->subfiles
= 0;
529 subentry
->superfile
= library_entry
;
530 subentry
->is_archive
= false;
531 subentry
->header_read_flag
= false;
532 subentry
->just_syms_flag
= false;
533 subentry
->loaded
= false;
539 boolean
subfile_wanted_p ();
541 clear_syms(entry
, offset
)
542 struct lang_input_statement_struct
*entry
;
546 unsigned long indx
= bfd_get_next_mapent(entry
->the_bfd
,
549 while (indx
!= BFD_NO_MORE_SYMBOLS
) {
550 if (car
->file_offset
== offset
) {
553 indx
= bfd_get_next_mapent(entry
->the_bfd
, indx
, &car
);
558 /* Search a library that has a map
561 symdef_library (entry
)
562 struct lang_input_statement_struct
*entry
;
565 register struct lang_input_statement_struct
*prev
= 0;
567 boolean not_finished
= true;
570 while (not_finished
== true)
572 carsym
*exported_library_name
;
573 bfd
*prev_archive_member_bfd
= 0;
575 int idx
= bfd_get_next_mapent(entry
->the_bfd
,
577 &exported_library_name
);
579 not_finished
= false;
581 while (idx
!= BFD_NO_MORE_SYMBOLS
&& undefined_global_sym_count
)
584 if (exported_library_name
->name
)
587 ldsym_type
*sp
= ldsym_get_soft (exported_library_name
->name
);
589 /* If we find a symbol that appears to be needed, think carefully
590 about the archive member that the symbol is in. */
591 /* So - if it exists, and is referenced somewhere and is
593 if (sp
&& sp
->srefs_chain
&& !sp
->sdefs_chain
)
595 bfd
*archive_member_bfd
= bfd_get_elt_at_index(entry
->the_bfd
, idx
);
596 struct lang_input_statement_struct
*archive_member_lang_input_statement_struct
;
598 if (archive_member_bfd
&& bfd_check_format(archive_member_bfd
, bfd_object
))
601 /* Don't think carefully about any archive member
602 more than once in a given pass. */
603 if (prev_archive_member_bfd
!= archive_member_bfd
)
606 prev_archive_member_bfd
= archive_member_bfd
;
608 /* Read the symbol table of the archive member. */
610 if (archive_member_bfd
->usrdata
!= (void *)NULL
) {
612 archive_member_lang_input_statement_struct
=(lang_input_statement_type
*) archive_member_bfd
->usrdata
;
616 archive_member_lang_input_statement_struct
=
617 decode_library_subfile (entry
, archive_member_bfd
);
618 archive_member_bfd
->usrdata
= (void *) archive_member_lang_input_statement_struct
;
622 if (archive_member_lang_input_statement_struct
== 0) {
623 info ("%F%I contains invalid archive member %s\n",
628 if (archive_member_lang_input_statement_struct
->loaded
== false)
631 Q_read_entry_symbols (archive_member_bfd
, archive_member_lang_input_statement_struct
);
632 /* Now scan the symbol table and decide whether to load. */
635 if (subfile_wanted_p (archive_member_lang_input_statement_struct
) == true)
638 /* This member is needed; load it.
639 Since we are loading something on this pass,
640 we must make another pass through the symdef data. */
644 Q_enter_file_symbols (archive_member_lang_input_statement_struct
);
647 prev
->chain
= archive_member_lang_input_statement_struct
;
649 entry
->subfiles
= archive_member_lang_input_statement_struct
;
652 prev
= archive_member_lang_input_statement_struct
;
655 /* Clear out this member's symbols from the symdef data
656 so that following passes won't waste time on them. */
657 clear_syms(entry
, exported_library_name
->file_offset
);
658 archive_member_lang_input_statement_struct
->loaded
= true;
665 idx
= bfd_get_next_mapent(entry
->the_bfd
, idx
, &exported_library_name
);
671 linear_library (entry
)
672 struct lang_input_statement_struct
*entry
;
674 boolean more_to_do
= true;
675 register struct lang_input_statement_struct
*prev
= 0;
679 bfd
* archive
= bfd_openr_next_archived_file(entry
->the_bfd
,0);
683 if (bfd_check_format(archive
, bfd_object
))
685 register struct lang_input_statement_struct
*subentry
;
687 subentry
= decode_library_subfile (entry
,
690 archive
->usrdata
= (void *) subentry
;
691 if (!subentry
) return;
692 if (subentry
->loaded
== false) {
693 Q_read_entry_symbols (archive
, subentry
);
695 if (subfile_wanted_p (subentry
) == true)
697 Q_enter_file_symbols (subentry
);
700 prev
->chain
= subentry
;
702 entry
->subfiles
= subentry
;
706 subentry
->loaded
= true;
710 archive
= bfd_openr_next_archived_file(entry
->the_bfd
,archive
);
717 /* ENTRY is an entry for a library member.
718 Its symbols have been read into core, but not entered.
719 Return nonzero if we ought to load this member. */
722 subfile_wanted_p (entry
)
723 struct lang_input_statement_struct
*entry
;
727 for (q
= entry
->asymbols
; *q
; q
++)
731 /* If the symbol has an interesting definition, we could
732 potentially want it. */
734 if (p
->flags
& BSF_FORT_COMM
735 || p
->flags
& BSF_GLOBAL
)
737 register ldsym_type
*sp
= ldsym_get_soft (p
->name
);
740 /* If this symbol has not been hashed,
741 we can't be looking for it. */
742 if (sp
!= (ldsym_type
*)NULL
743 && sp
->sdefs_chain
== (asymbol
**)NULL
) {
744 if (sp
->srefs_chain
!= (asymbol
**)NULL
745 || sp
->scoms_chain
!= (asymbol
**)NULL
)
747 /* This is a symbol we are looking for. It is either
748 not yet defined or common. */
750 if (flag_is_common(p
->flags
))
752 /* This libary member has something to
753 say about this element. We should
754 remember if its a new size */
755 /* Move something from the ref list to the com list */
756 if(sp
->scoms_chain
) {
757 /* Already a common symbol, maybe update it */
758 if (p
->value
> (*(sp
->scoms_chain
))->value
) {
759 (*(sp
->scoms_chain
))->value
= p
->value
;
763 /* Take a value from the ref chain
764 Here we are moving a symbol from the owning bfd
765 to another bfd. We must set up the
766 common_section portion of the bfd thing */
770 sp
->scoms_chain
= sp
->srefs_chain
;
772 (asymbol
**)((*(sp
->srefs_chain
))->udata
);
773 (*(sp
->scoms_chain
))->udata
= (void*)NULL
;
775 (*( sp
->scoms_chain
))->flags
= BSF_FORT_COMM
;
777 undefined_global_sym_count
--;
779 asymbol
*com
= *(sp
->scoms_chain
);
780 if (((lang_input_statement_type
*)
781 (com
->the_bfd
->usrdata
))->common_section
==
783 ((lang_input_statement_type
*)
784 (com
->the_bfd
->usrdata
))->common_section
=
785 bfd_make_section(com
->the_bfd
, "COMMON");
788 ASSERT(p
->udata
== 0);
794 info("%I needed due to %s\n",entry
, sp
->name
);