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";
140 emulation
= getenv(EMULATION_ENVIRON
);
142 /* Initialize the data about options. */
143 strip_symbols
= STRIP_NONE
;
145 discard_locals
= DISCARD_NONE
;
148 config
.relocateable_output
= false;
150 command_line
.force_common_definition
= false;
153 ldfile_add_library_path("./");
154 config
.make_executable
= true;
155 force_make_executable
= false;
158 /* Initialize the cumulative counts of symbols. */
159 undefined_global_sym_count
= 0;
161 multiple_def_count
= 0;
164 config
.magic_demand_paged
= true ;
165 config
.make_executable
= true;
167 if (emulation
== (char *)NULL
) {
168 emulation
= DEFAULT_EMULATION
;
172 ldemul_choose_mode(emulation
);
174 default_target
= ldemul_choose_target();
177 ldemul_before_parse();
178 lang_has_input_file
= false;
179 parse_args(argc
, argv
);
182 info("%P: mode %s\n", emulation
);
184 if (lang_has_input_file
== false) {
185 info("%P%F: No input files\n");
188 ldemul_after_parse();
195 /* Print error messages for any missing symbols, for any warning
196 symbols, and possibly multiple definitions */
198 /* Print a map, if requested. */
201 ldsym_print_symbol_table ();
206 if (config
.relocateable_output
) {
207 output_bfd
->flags
&= ~( D_PAGED
);
208 output_bfd
->flags
|= EXEC_P
;
210 bfd_close(output_bfd
);
213 output_bfd
->flags
|= EXEC_P
;
216 bfd_close(output_bfd
);
217 if (config
.make_executable
== false && force_make_executable
== false) {
218 unlink(output_filename
);
220 return (!config
.make_executable
);
228 Q_read_entry_symbols (desc
, entry
)
230 struct lang_input_statement_struct
*entry
;
232 if (entry
->asymbols
== (asymbol
**)NULL
) {
233 size_t table_size
= get_symtab_upper_bound(desc
);
234 entry
->asymbols
= (asymbol
**)ldmalloc(table_size
);
236 entry
->symbol_count
= bfd_canonicalize_symtab(desc
, entry
->asymbols
) ;
242 * turn this item into a reference
249 asymbol
*sym
= *nlist_p
;
251 sym
->flags
= BSF_UNDEFINED
;
252 sym
->section
= (asection
*)NULL
;
253 sym
->udata
=(void *)( sp
->srefs_chain
);
254 sp
->srefs_chain
= nlist_p
;
257 This function is called for each name which is seen which has a global
258 scope. It enters the name into the global symbol table in the correct
259 symbol on the correct chain. Remember that each ldsym_type has three
260 chains attatched, one of all definitions of a symbol, one of all
261 references of a symbol and one of all common definitions of a symbol.
263 When the function is over, the supplied is left connected to the bfd
264 to which is was born, with its udata field pointing to the next member
265 on the chain in which it has been inserted.
267 A certain amount of jigery pokery is necessary since commons come
268 along and upset things, we only keep one item in the common chain; the
269 one with the biggest size seen sofar. When another common comes along
270 it either bumps the previous definition into the ref chain, since it
271 is bigger, or gets turned into a ref on the spot since the one on the
272 common chain is already bigger. If a real definition comes along then
273 the common gets bumped off anyway.
275 Whilst all this is going on we keep a count of the number of multiple
276 definitions seen, undefined global symbols and pending commons.
281 Q_enter_global_ref (nlist_p
)
285 asymbol
*sym
= *nlist_p
;
286 char *name
= sym
->name
;
287 ldsym_type
*sp
= ldsym_get (name
);
289 flagword this_symbol_flags
= sym
->flags
;
292 ASSERT(sym
->udata
== 0);
294 /* Just place onto correct chain */
295 if (flag_is_common(this_symbol_flags
)) {
296 /* If we have a definition of this symbol already then
297 * this common turns into a reference. Also we only
298 * ever point to the largest common, so if we
299 * have a common, but it's bigger that the new symbol
300 * the turn this into a reference too.
304 /* This is a common symbol, but we already have a definition
305 for it, so just link it into the ref chain as if
310 else if (sp
->scoms_chain
) {
311 /* If we have a previous common, keep only the biggest */
312 if ( (*(sp
->scoms_chain
))->value
> sym
->value
) {
313 /* other common is bigger, throw this one away */
316 else if (sp
->scoms_chain
!= nlist_p
) {
317 /* other common is smaller, throw that away */
318 refize(sp
, sp
->scoms_chain
);
319 sp
->scoms_chain
= nlist_p
;
323 /* This is the first time we've seen a common, so
324 * remember it - if it was undefined before, we know it's defined now
327 undefined_global_sym_count
--;
330 sp
->scoms_chain
= nlist_p
;
334 else if (flag_is_defined(this_symbol_flags
)) {
335 /* This is the definition of a symbol, add to def chain */
336 if (sp
->sdefs_chain
&& (*(sp
->sdefs_chain
))->section
!= sym
->section
) {
337 /* Multiple definition */
338 asymbol
*sy
= *(sp
->sdefs_chain
);
339 lang_input_statement_type
*stat
= (lang_input_statement_type
*) sy
->the_bfd
->usrdata
;
340 lang_input_statement_type
*stat1
= (lang_input_statement_type
*) sym
->the_bfd
->usrdata
;
341 asymbol
** stat1_symbols
= stat1
? stat1
->asymbols
: 0;
342 asymbol
** stat_symbols
= stat
? stat
->asymbols
:0;
344 multiple_def_count
++;
345 info("%C: multiple definition of `%T'\n",
352 info("%C: first seen here\n",
359 sym
->udata
=(void *)( sp
->sdefs_chain
);
360 sp
->sdefs_chain
= nlist_p
;
362 /* A definition overrides a common symbol */
363 if (sp
->scoms_chain
) {
364 refize(sp
, sp
->scoms_chain
);
368 else if (sp
->srefs_chain
) {
369 /* If previously was undefined, then remember as defined */
370 undefined_global_sym_count
--;
374 if (sp
->scoms_chain
== (asymbol
**)NULL
375 && sp
->srefs_chain
== (asymbol
**)NULL
376 && sp
->sdefs_chain
== (asymbol
**)NULL
) {
377 /* And it's the first time we've seen it */
378 undefined_global_sym_count
++;
385 ASSERT(sp
->sdefs_chain
== 0 || sp
->scoms_chain
== 0);
386 ASSERT(sp
->scoms_chain
==0 || (*(sp
->scoms_chain
))->udata
== 0);
392 Q_enter_file_symbols (entry
)
393 lang_input_statement_type
*entry
;
396 entry
->common_section
=
397 bfd_make_section(entry
->the_bfd
, "COMMON");
399 ldlang_add_file(entry
);
402 if (trace_files
|| option_v
) {
406 total_symbols_seen
+= entry
->symbol_count
;
408 for (q
= entry
->asymbols
; *q
; q
++)
412 if (flag_is_undefined_or_global_or_common(p
->flags
))
414 Q_enter_global_ref(q
);
416 ASSERT(p
->flags
!= 0);
422 /* Searching libraries */
424 struct lang_input_statement_struct
*decode_library_subfile ();
425 void linear_library (), symdef_library ();
427 /* Search the library ENTRY, already open on descriptor DESC.
428 This means deciding which library members to load,
429 making a chain of `struct lang_input_statement_struct' for those members,
430 and entering their global symbols in the hash table. */
433 search_library (entry
)
434 struct lang_input_statement_struct
*entry
;
437 /* No need to load a library if no undefined symbols */
438 if (!undefined_global_sym_count
) return;
440 if (bfd_has_map(entry
->the_bfd
))
441 symdef_library (entry
);
443 linear_library (entry
);
449 ldmain_open_file_read_symbol (entry
)
450 struct lang_input_statement_struct
*entry
;
452 if (entry
->asymbols
== (asymbol
**)NULL
453 &&entry
->real
== true
454 && entry
->filename
!= (char *)NULL
)
456 ldfile_open_file (entry
);
458 if (bfd_check_format(entry
->the_bfd
, bfd_object
))
460 entry
->the_bfd
->usrdata
= (void*)entry
;
463 Q_read_entry_symbols (entry
->the_bfd
, entry
);
464 Q_enter_file_symbols (entry
);
466 else if (bfd_check_format(entry
->the_bfd
, bfd_archive
))
468 entry
->the_bfd
->usrdata
= (void *)entry
;
470 entry
->subfiles
= (lang_input_statement_type
*)NULL
;
471 search_library (entry
);
475 info("%F%I: malformed input file (not rel or archive) \n", entry
);
482 /* Construct and return a lang_input_statement_struct for a library member.
483 The library's lang_input_statement_struct is library_entry,
484 and the library is open on DESC.
485 SUBFILE_OFFSET is the byte index in the library of this member's header.
486 We store the length of the member into *LENGTH_LOC. */
488 lang_input_statement_type
*
489 decode_library_subfile (library_entry
, subfile_offset
)
490 struct lang_input_statement_struct
*library_entry
;
493 register struct lang_input_statement_struct
*subentry
;
494 subentry
= (struct lang_input_statement_struct
*) ldmalloc (sizeof (struct lang_input_statement_struct
));
495 subentry
->filename
= subfile_offset
-> filename
;
496 subentry
->local_sym_name
= subfile_offset
->filename
;
497 subentry
->asymbols
= 0;
498 subentry
->the_bfd
= subfile_offset
;
499 subentry
->subfiles
= 0;
501 subentry
->superfile
= library_entry
;
502 subentry
->is_archive
= false;
504 subentry
->just_syms_flag
= false;
505 subentry
->loaded
= false;
511 boolean
subfile_wanted_p ();
513 clear_syms(entry
, offset
)
514 struct lang_input_statement_struct
*entry
;
518 unsigned long indx
= bfd_get_next_mapent(entry
->the_bfd
,
521 while (indx
!= BFD_NO_MORE_SYMBOLS
) {
522 if (car
->file_offset
== offset
) {
525 indx
= bfd_get_next_mapent(entry
->the_bfd
, indx
, &car
);
530 /* Search a library that has a map
533 symdef_library (entry
)
534 struct lang_input_statement_struct
*entry
;
537 register struct lang_input_statement_struct
*prev
= 0;
539 boolean not_finished
= true;
542 while (not_finished
== true)
544 carsym
*exported_library_name
;
545 bfd
*prev_archive_member_bfd
= 0;
547 int idx
= bfd_get_next_mapent(entry
->the_bfd
,
549 &exported_library_name
);
551 not_finished
= false;
553 while (idx
!= BFD_NO_MORE_SYMBOLS
&& undefined_global_sym_count
)
556 if (exported_library_name
->name
)
559 ldsym_type
*sp
= ldsym_get_soft (exported_library_name
->name
);
561 /* If we find a symbol that appears to be needed, think carefully
562 about the archive member that the symbol is in. */
563 /* So - if it exists, and is referenced somewhere and is
565 if (sp
&& sp
->srefs_chain
&& !sp
->sdefs_chain
)
567 bfd
*archive_member_bfd
= bfd_get_elt_at_index(entry
->the_bfd
, idx
);
568 struct lang_input_statement_struct
*archive_member_lang_input_statement_struct
;
570 if (archive_member_bfd
&& bfd_check_format(archive_member_bfd
, bfd_object
))
573 /* Don't think carefully about any archive member
574 more than once in a given pass. */
575 if (prev_archive_member_bfd
!= archive_member_bfd
)
578 prev_archive_member_bfd
= archive_member_bfd
;
580 /* Read the symbol table of the archive member. */
582 if (archive_member_bfd
->usrdata
!= (void *)NULL
) {
584 archive_member_lang_input_statement_struct
=(lang_input_statement_type
*) archive_member_bfd
->usrdata
;
588 archive_member_lang_input_statement_struct
=
589 decode_library_subfile (entry
, archive_member_bfd
);
590 archive_member_bfd
->usrdata
= (void *) archive_member_lang_input_statement_struct
;
594 if (archive_member_lang_input_statement_struct
== 0) {
595 info ("%F%I contains invalid archive member %s\n",
600 if (archive_member_lang_input_statement_struct
->loaded
== false)
603 Q_read_entry_symbols (archive_member_bfd
, archive_member_lang_input_statement_struct
);
604 /* Now scan the symbol table and decide whether to load. */
607 if (subfile_wanted_p (archive_member_lang_input_statement_struct
) == true)
610 /* This member is needed; load it.
611 Since we are loading something on this pass,
612 we must make another pass through the symdef data. */
616 Q_enter_file_symbols (archive_member_lang_input_statement_struct
);
619 prev
->chain
= archive_member_lang_input_statement_struct
;
621 entry
->subfiles
= archive_member_lang_input_statement_struct
;
624 prev
= archive_member_lang_input_statement_struct
;
627 /* Clear out this member's symbols from the symdef data
628 so that following passes won't waste time on them. */
629 clear_syms(entry
, exported_library_name
->file_offset
);
630 archive_member_lang_input_statement_struct
->loaded
= true;
637 idx
= bfd_get_next_mapent(entry
->the_bfd
, idx
, &exported_library_name
);
643 linear_library (entry
)
644 struct lang_input_statement_struct
*entry
;
646 boolean more_to_do
= true;
647 register struct lang_input_statement_struct
*prev
= 0;
651 bfd
* archive
= bfd_openr_next_archived_file(entry
->the_bfd
,0);
655 if (bfd_check_format(archive
, bfd_object
))
657 register struct lang_input_statement_struct
*subentry
;
659 subentry
= decode_library_subfile (entry
,
662 archive
->usrdata
= (void *) subentry
;
663 if (!subentry
) return;
664 if (subentry
->loaded
== false) {
665 Q_read_entry_symbols (archive
, subentry
);
667 if (subfile_wanted_p (subentry
) == true)
669 Q_enter_file_symbols (subentry
);
672 prev
->chain
= subentry
;
674 entry
->subfiles
= subentry
;
678 subentry
->loaded
= true;
682 archive
= bfd_openr_next_archived_file(entry
->the_bfd
,archive
);
689 /* ENTRY is an entry for a library member.
690 Its symbols have been read into core, but not entered.
691 Return nonzero if we ought to load this member. */
694 subfile_wanted_p (entry
)
695 struct lang_input_statement_struct
*entry
;
699 for (q
= entry
->asymbols
; *q
; q
++)
703 /* If the symbol has an interesting definition, we could
704 potentially want it. */
706 if (p
->flags
& BSF_FORT_COMM
707 || p
->flags
& BSF_GLOBAL
)
709 register ldsym_type
*sp
= ldsym_get_soft (p
->name
);
712 /* If this symbol has not been hashed,
713 we can't be looking for it. */
714 if (sp
!= (ldsym_type
*)NULL
715 && sp
->sdefs_chain
== (asymbol
**)NULL
) {
716 if (sp
->srefs_chain
!= (asymbol
**)NULL
717 || sp
->scoms_chain
!= (asymbol
**)NULL
)
719 /* This is a symbol we are looking for. It is either
720 not yet defined or common. */
722 if (flag_is_common(p
->flags
))
724 /* This libary member has something to
725 say about this element. We should
726 remember if its a new size */
727 /* Move something from the ref list to the com list */
728 if(sp
->scoms_chain
) {
729 /* Already a common symbol, maybe update it */
730 if (p
->value
> (*(sp
->scoms_chain
))->value
) {
731 (*(sp
->scoms_chain
))->value
= p
->value
;
735 /* Take a value from the ref chain
736 Here we are moving a symbol from the owning bfd
737 to another bfd. We must set up the
738 common_section portion of the bfd thing */
742 sp
->scoms_chain
= sp
->srefs_chain
;
744 (asymbol
**)((*(sp
->srefs_chain
))->udata
);
745 (*(sp
->scoms_chain
))->udata
= (void*)NULL
;
747 (*( sp
->scoms_chain
))->flags
= BSF_FORT_COMM
;
748 /* Remember the size of this item */
749 sp
->scoms_chain
[0]->value
= p
->value
;
751 undefined_global_sym_count
--;
753 asymbol
*com
= *(sp
->scoms_chain
);
754 if (((lang_input_statement_type
*)
755 (com
->the_bfd
->usrdata
))->common_section
==
757 ((lang_input_statement_type
*)
758 (com
->the_bfd
->usrdata
))->common_section
=
759 bfd_make_section(com
->the_bfd
, "COMMON");
762 ASSERT(p
->udata
== 0);
768 info("%I needed due to %s\n",entry
, sp
->name
);