Avoid 'v' flag on archive updates, since it just wastes space in the log.
[deliverable/binutils-gdb.git] / ld / ldmain.c
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2
3 This file is part of GLD, the Gnu Linker.
4
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)
8 any later version.
9
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.
14
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. */
18
19 /*
20 * Written by Steve Chamberlain steve@cygnus.com
21 *
22 * $Id$
23 *
24 *
25 */
26
27
28 #include "sysdep.h"
29 #include "bfd.h"
30
31 #include "config.h"
32 #include "ld.h"
33 #include "ldmain.h"
34 #include "ldmisc.h"
35 #include "ldwrite.h"
36 #include "ldgram.h"
37 #include "ldsym.h"
38 #include "ldlang.h"
39 #include "ldemul.h"
40 #include "ldlex.h"
41 #include "ldfile.h"
42
43 /* IMPORTS */
44 extern boolean lang_has_input_file;
45 extern boolean trace_files;
46 /* EXPORTS */
47
48 char *default_target;
49 char *output_filename = "a.out";
50 /* Name this program was invoked by. */
51 char *program_name;
52
53 /* The file that we're creating */
54 bfd *output_bfd = 0;
55
56 extern boolean option_v;
57
58 /* The local symbol prefix */
59 char lprefix = 'L';
60
61 /* Count the number of global symbols multiply defined. */
62 int multiple_def_count;
63
64
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.
68
69 It is incremented when a symbol is created as a common, and
70 decremented when the common declaration is overridden
71
72 Another way of thinking of it is that this is a count of
73 all ldsym_types with a ->scoms field
74 */
75 unsigned int commons_pending;
76
77
78 /* Count the number of global symbols referenced and not defined.
79 common symbols are not included in this count.
80 */
81
82 unsigned int undefined_global_sym_count;
83
84
85
86 /* Count the number of warning symbols encountered. */
87 int warning_count;
88
89 /* have we had a load script ? */
90 extern boolean had_script;
91
92
93
94 /* Nonzero means print names of input files as processed. */
95 boolean trace_files;
96
97
98
99 /* 1 => write load map. */
100 boolean write_map;
101
102
103 int unix_relocate;
104 #ifdef GNU960
105 /* Indicates whether output file will be b.out (default) or coff */
106 enum target_flavour_enum output_flavor = BFD_BOUT_FORMAT;
107 #endif
108
109
110
111
112
113
114
115
116 /* Force the make_executable to be output, even if there are non-fatal
117 errors */
118 boolean force_make_executable;
119
120
121 /* A count of the total number of local symbols ever seen - by adding
122 the symbol_count field of each newly read afile.*/
123
124
125 unsigned int total_symbols_seen;
126
127 /* A count of the number of read files - the same as the number of elements
128 in file_chain
129 */
130 unsigned int total_files_seen;
131
132
133 /* IMPORTS */
134 args_type command_line;
135 ld_config_type config;
136 int
137 main (argc, argv)
138 char **argv;
139 int argc;
140 {
141 char *emulation;
142 program_name = argv[0];
143 output_filename = "a.out";
144
145 #ifdef GNU960
146 {
147 int i;
148
149 check_v960( argc, argv );
150 emulation = GLD960_EMULATION_NAME;
151 for ( i = 1; i < argc; i++ ){
152 if ( !strcmp(argv[i],"-Fcoff") ){
153 emulation = LNK960_EMULATION_NAME;
154 output_flavor = BFD_COFF_FORMAT;
155 break;
156 }
157 }
158 }
159 #else
160 emulation = (char *) getenv(EMULATION_ENVIRON);
161 #endif
162
163 /* Initialize the data about options. */
164
165 trace_files = false;
166
167
168 write_map = false;
169 config.relocateable_output = false;
170 unix_relocate = 0;
171 command_line.force_common_definition = false;
172
173 ldfile_add_arch("");
174 ldfile_add_library_path("./");
175 config.make_executable = true;
176 force_make_executable = false;
177
178
179 /* Initialize the cumulative counts of symbols. */
180 undefined_global_sym_count = 0;
181 warning_count = 0;
182 multiple_def_count = 0;
183 commons_pending = 0;
184
185 config.magic_demand_paged = true ;
186 config.make_executable = true;
187 if (emulation == (char *)NULL) {
188 emulation= DEFAULT_EMULATION;
189 }
190
191
192 ldemul_choose_mode(emulation);
193
194
195 default_target = ldemul_choose_target();
196
197 lang_init();
198 ldemul_before_parse();
199 lang_has_input_file = false;
200 parse_args(argc, argv);
201 lang_final();
202 if (trace_files) {
203 info("%P: mode %s\n", emulation);
204 }
205 if (lang_has_input_file == false) {
206 info("%P%F: No input files\n");
207 }
208
209 ldemul_after_parse();
210
211 lang_process();
212
213
214
215
216 /* Print error messages for any missing symbols, for any warning
217 symbols, and possibly multiple definitions */
218
219 /* Print a map, if requested. */
220
221 if (write_map) {
222 ldsym_print_symbol_table ();
223 lang_map(stdout);
224 }
225
226
227 if (config.relocateable_output) {
228 output_bfd->flags &= ~( D_PAGED);
229 output_bfd->flags |= EXEC_P;
230 ldwrite();
231 bfd_close(output_bfd);
232 }
233 else {
234 output_bfd->flags |= EXEC_P;
235
236 ldwrite();
237 bfd_close(output_bfd);
238 if (config.make_executable == false && force_make_executable == false) {
239 unlink(output_filename);
240 }
241 return (!config.make_executable);
242 }
243
244 return(0);
245 } /* main() */
246
247
248 void
249 Q_read_entry_symbols (desc, entry)
250 bfd *desc;
251 struct lang_input_statement_struct *entry;
252 {
253 if (entry->asymbols == (asymbol **)NULL) {
254 bfd_size_type table_size = get_symtab_upper_bound(desc);
255 entry->asymbols = (asymbol **)ldmalloc(table_size);
256
257 entry->symbol_count = bfd_canonicalize_symtab(desc, entry->asymbols) ;
258 }
259 }
260
261
262 /*
263 * turn this item into a reference
264 */
265 static void
266 refize(sp, nlist_p)
267 ldsym_type *sp;
268 asymbol **nlist_p;
269 {
270 asymbol *sym = *nlist_p;
271 sym->value = 0;
272 sym->flags = BSF_UNDEFINED;
273 sym->section = (asection *)NULL;
274 sym->udata =(PTR)( sp->srefs_chain);
275 sp->srefs_chain = nlist_p;
276 }
277 /*
278 This function is called for each name which is seen which has a global
279 scope. It enters the name into the global symbol table in the correct
280 symbol on the correct chain. Remember that each ldsym_type has three
281 chains attatched, one of all definitions of a symbol, one of all
282 references of a symbol and one of all common definitions of a symbol.
283
284 When the function is over, the supplied is left connected to the bfd
285 to which is was born, with its udata field pointing to the next member
286 on the chain in which it has been inserted.
287
288 A certain amount of jigery pokery is necessary since commons come
289 along and upset things, we only keep one item in the common chain; the
290 one with the biggest size seen sofar. When another common comes along
291 it either bumps the previous definition into the ref chain, since it
292 is bigger, or gets turned into a ref on the spot since the one on the
293 common chain is already bigger. If a real definition comes along then
294 the common gets bumped off anyway.
295
296 Whilst all this is going on we keep a count of the number of multiple
297 definitions seen, undefined global symbols and pending commons.
298 */
299
300
301 void
302 Q_enter_global_ref (nlist_p)
303 asymbol **nlist_p;
304
305 {
306 asymbol *sym = *nlist_p;
307 CONST char *name = sym->name;
308 ldsym_type *sp = ldsym_get (name);
309
310 flagword this_symbol_flags = sym->flags;
311
312
313 ASSERT(sym->udata == 0);
314
315 if (flag_is_constructor(this_symbol_flags)) {
316 /* Just remeber the name, do it once per name by placing it as if
317 it were a zero sized common. The next ref */
318 ldlang_add_constructor(sp);
319
320 }
321 else {
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.
328 */
329 if (sp->sdefs_chain)
330 {
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
333 it were a reference
334 */
335 refize(sp, nlist_p);
336 }
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 */
341 refize(sp, nlist_p);
342 }
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;
347 }
348 }
349 else {
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
352 */
353 if (sp->srefs_chain)
354 undefined_global_sym_count--;
355
356 commons_pending++;
357 sp->scoms_chain = nlist_p;
358 }
359 }
360
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;
370
371 multiple_def_count++;
372 info("%C: multiple definition of `%T'\n",
373 sym->the_bfd,
374 sym->section,
375 stat1_symbols,
376 sym->value,
377 sym);
378
379 info("%C: first seen here\n",
380 sy->the_bfd,
381 sy->section,
382 stat_symbols,
383 sy->value);
384 }
385 else {
386 sym->udata =(PTR)( sp->sdefs_chain);
387 sp->sdefs_chain = nlist_p;
388 }
389 /* A definition overrides a common symbol */
390 if (sp->scoms_chain) {
391 refize(sp, sp->scoms_chain);
392 sp->scoms_chain = 0;
393 commons_pending--;
394 }
395 else if (sp->srefs_chain) {
396 /* If previously was undefined, then remember as defined */
397 undefined_global_sym_count--;
398 }
399 }
400 else {
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++;
406
407 }
408
409 refize(sp, nlist_p);
410 }
411 }
412
413 ASSERT(sp->sdefs_chain == 0 || sp->scoms_chain == 0);
414 ASSERT(sp->scoms_chain ==0 || (*(sp->scoms_chain))->udata == 0);
415
416
417 }
418
419 static void
420 Q_enter_file_symbols (entry)
421 lang_input_statement_type *entry;
422 {
423 asymbol **q ;
424 entry->common_section =
425 bfd_make_section(entry->the_bfd, "COMMON");
426
427 ldlang_add_file(entry);
428
429
430 if (trace_files || option_v) {
431 info("%I\n", entry);
432 }
433
434 total_symbols_seen += entry->symbol_count;
435 total_files_seen ++;
436 for (q = entry->asymbols; *q; q++)
437 {
438 asymbol *p = *q;
439
440 if (flag_is_undefined_or_global_or_common_or_constructor(p->flags))
441 {
442 Q_enter_global_ref(q);
443 }
444 if (p->flags & BSF_INDIRECT) {
445 add_indirect(q);
446 }
447
448 if (p->flags & BSF_WARNING) {
449 add_warning(p);
450 }
451 ASSERT(p->flags != 0);
452 }
453 }
454
455
456
457 /* Searching libraries */
458
459 struct lang_input_statement_struct *decode_library_subfile ();
460 void linear_library (), symdef_library ();
461
462 /* Search the library ENTRY, already open on descriptor DESC.
463 This means deciding which library members to load,
464 making a chain of `struct lang_input_statement_struct' for those members,
465 and entering their global symbols in the hash table. */
466
467 void
468 search_library (entry)
469 struct lang_input_statement_struct *entry;
470 {
471
472 /* No need to load a library if no undefined symbols */
473 if (!undefined_global_sym_count) return;
474
475 if (bfd_has_map(entry->the_bfd))
476 symdef_library (entry);
477 else
478 linear_library (entry);
479
480 }
481
482
483 #ifdef GNU960
484 static
485 boolean
486 gnu960_check_format (abfd, format)
487 bfd *abfd;
488 bfd_format format;
489 {
490 boolean retval;
491
492 if ((bfd_check_format(abfd,format) == true)
493 && (abfd->xvec->flavour == output_flavor) ){
494 return true;
495 }
496
497
498 return false;
499 }
500 #endif
501
502 void
503 ldmain_open_file_read_symbol (entry)
504 struct lang_input_statement_struct *entry;
505 {
506 if (entry->asymbols == (asymbol **)NULL
507 &&entry->real == true
508 && entry->filename != (char *)NULL)
509 {
510 ldfile_open_file (entry);
511
512 #ifdef GNU960
513 if (gnu960_check_format(entry->the_bfd, bfd_object))
514 #else
515 if (bfd_check_format(entry->the_bfd, bfd_object))
516 #endif
517 {
518 entry->the_bfd->usrdata = (PTR)entry;
519
520
521 Q_read_entry_symbols (entry->the_bfd, entry);
522 Q_enter_file_symbols (entry);
523 }
524 #ifdef GNU960
525 else if (gnu960_check_format(entry->the_bfd, bfd_archive))
526 #else
527 else if (bfd_check_format(entry->the_bfd, bfd_archive))
528 #endif
529 {
530 entry->the_bfd->usrdata = (PTR)entry;
531
532 entry->subfiles = (lang_input_statement_type *)NULL;
533 search_library (entry);
534 }
535 else
536 {
537 info("%F%B: malformed input file (not rel or archive) \n",
538 entry->the_bfd);
539 }
540 }
541
542 }
543
544
545 /* Construct and return a lang_input_statement_struct for a library member.
546 The library's lang_input_statement_struct is library_entry,
547 and the library is open on DESC.
548 SUBFILE_OFFSET is the byte index in the library of this member's header.
549 We store the length of the member into *LENGTH_LOC. */
550
551 lang_input_statement_type *
552 decode_library_subfile (library_entry, subfile_offset)
553 struct lang_input_statement_struct *library_entry;
554 bfd *subfile_offset;
555 {
556 register struct lang_input_statement_struct *subentry;
557 subentry = (struct lang_input_statement_struct *) ldmalloc ((bfd_size_type)(sizeof (struct lang_input_statement_struct)));
558 subentry->filename = subfile_offset -> filename;
559 subentry->local_sym_name = subfile_offset->filename;
560 subentry->asymbols = 0;
561 subentry->the_bfd = subfile_offset;
562 subentry->subfiles = 0;
563 subentry->next = 0;
564 subentry->superfile = library_entry;
565 subentry->is_archive = false;
566
567 subentry->just_syms_flag = false;
568 subentry->loaded = false;
569 subentry->chain = 0;
570
571 return subentry;
572 }
573
574 boolean subfile_wanted_p ();
575 void
576 clear_syms(entry, offset)
577 struct lang_input_statement_struct *entry;
578 file_ptr offset;
579 {
580 carsym *car;
581 unsigned long indx = bfd_get_next_mapent(entry->the_bfd,
582 BFD_NO_MORE_SYMBOLS,
583 &car);
584 while (indx != BFD_NO_MORE_SYMBOLS) {
585 if (car->file_offset == offset) {
586 car->name = 0;
587 }
588 indx = bfd_get_next_mapent(entry->the_bfd, indx, &car);
589 }
590
591 }
592
593 /* Search a library that has a map
594 */
595 void
596 symdef_library (entry)
597 struct lang_input_statement_struct *entry;
598
599 {
600 register struct lang_input_statement_struct *prev = 0;
601
602 boolean not_finished = true;
603
604
605 while (not_finished == true)
606 {
607 carsym *exported_library_name;
608 bfd *prev_archive_member_bfd = 0;
609
610 int idx = bfd_get_next_mapent(entry->the_bfd,
611 BFD_NO_MORE_SYMBOLS,
612 &exported_library_name);
613
614 not_finished = false;
615
616 while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count)
617 {
618
619 if (exported_library_name->name)
620 {
621
622 ldsym_type *sp = ldsym_get_soft (exported_library_name->name);
623
624 /* If we find a symbol that appears to be needed, think carefully
625 about the archive member that the symbol is in. */
626 /* So - if it exists, and is referenced somewhere and is
627 undefined or */
628 if (sp && sp->srefs_chain && !sp->sdefs_chain)
629 {
630 bfd *archive_member_bfd = bfd_get_elt_at_index(entry->the_bfd, idx);
631 struct lang_input_statement_struct *archive_member_lang_input_statement_struct;
632
633 #ifdef GNU960
634 if (archive_member_bfd && gnu960_check_format(archive_member_bfd, bfd_object))
635 #else
636 if (archive_member_bfd && bfd_check_format(archive_member_bfd, bfd_object))
637 #endif
638 {
639
640 /* Don't think carefully about any archive member
641 more than once in a given pass. */
642 if (prev_archive_member_bfd != archive_member_bfd)
643 {
644
645 prev_archive_member_bfd = archive_member_bfd;
646
647 /* Read the symbol table of the archive member. */
648
649 if (archive_member_bfd->usrdata != (PTR)NULL) {
650
651 archive_member_lang_input_statement_struct =(lang_input_statement_type *) archive_member_bfd->usrdata;
652 }
653 else {
654
655 archive_member_lang_input_statement_struct =
656 decode_library_subfile (entry, archive_member_bfd);
657 archive_member_bfd->usrdata = (PTR) archive_member_lang_input_statement_struct;
658
659 }
660
661 if (archive_member_lang_input_statement_struct == 0) {
662 info ("%F%I contains invalid archive member %s\n",
663 entry,
664 sp->name);
665 }
666
667 if (archive_member_lang_input_statement_struct->loaded == false)
668 {
669
670 Q_read_entry_symbols (archive_member_bfd, archive_member_lang_input_statement_struct);
671 /* Now scan the symbol table and decide whether to load. */
672
673
674 if (subfile_wanted_p (archive_member_lang_input_statement_struct) == true)
675
676 {
677 /* This member is needed; load it.
678 Since we are loading something on this pass,
679 we must make another pass through the symdef data. */
680
681 not_finished = true;
682
683 Q_enter_file_symbols (archive_member_lang_input_statement_struct);
684
685 if (prev)
686 prev->chain = archive_member_lang_input_statement_struct;
687 else
688 entry->subfiles = archive_member_lang_input_statement_struct;
689
690
691 prev = archive_member_lang_input_statement_struct;
692
693
694 /* Clear out this member's symbols from the symdef data
695 so that following passes won't waste time on them. */
696 clear_syms(entry, exported_library_name->file_offset);
697 archive_member_lang_input_statement_struct->loaded = true;
698 }
699 }
700 }
701 }
702 }
703 }
704 idx = bfd_get_next_mapent(entry->the_bfd, idx, &exported_library_name);
705 }
706 }
707 }
708
709 void
710 linear_library (entry)
711 struct lang_input_statement_struct *entry;
712 {
713 boolean more_to_do = true;
714 register struct lang_input_statement_struct *prev = 0;
715
716 while (more_to_do) {
717
718 bfd * archive = bfd_openr_next_archived_file(entry->the_bfd,0);
719
720 more_to_do = false;
721 while (archive) {
722 #ifdef GNU960
723 if (gnu960_check_format(archive, bfd_object))
724 #else
725 if (bfd_check_format(archive, bfd_object))
726 #endif
727 {
728 register struct lang_input_statement_struct *subentry;
729
730 subentry = decode_library_subfile (entry,
731 archive);
732
733 archive->usrdata = (PTR) subentry;
734 if (!subentry) return;
735 if (subentry->loaded == false) {
736 Q_read_entry_symbols (archive, subentry);
737
738 if (subfile_wanted_p (subentry) == true)
739 {
740 Q_enter_file_symbols (subentry);
741
742 if (prev)
743 prev->chain = subentry;
744 else
745 entry->subfiles = subentry;
746 prev = subentry;
747
748 more_to_do = true;
749 subentry->loaded = true;
750 }
751 }
752 }
753 archive = bfd_openr_next_archived_file(entry->the_bfd,archive);
754
755 }
756
757 }
758 }
759
760 /* ENTRY is an entry for a library member.
761 Its symbols have been read into core, but not entered.
762 Return nonzero if we ought to load this member. */
763
764 boolean
765 subfile_wanted_p (entry)
766 struct lang_input_statement_struct *entry;
767 {
768 asymbol **q;
769
770 for (q = entry->asymbols; *q; q++)
771 {
772 asymbol *p = *q;
773
774 /* If the symbol has an interesting definition, we could
775 potentially want it. */
776
777 if (p->flags & BSF_FORT_COMM
778 || p->flags & BSF_GLOBAL)
779 {
780 register ldsym_type *sp = ldsym_get_soft (p->name);
781
782
783 /* If this symbol has not been hashed,
784 we can't be looking for it. */
785 if (sp != (ldsym_type *)NULL
786 && sp->sdefs_chain == (asymbol **)NULL) {
787 if (sp->srefs_chain != (asymbol **)NULL
788 || sp->scoms_chain != (asymbol **)NULL)
789 {
790 /* This is a symbol we are looking for. It is either
791 not yet defined or common. */
792
793 if (flag_is_common(p->flags))
794 {
795 /* This libary member has something to
796 say about this element. We should
797 remember if its a new size */
798 /* Move something from the ref list to the com list */
799 if(sp->scoms_chain) {
800 /* Already a common symbol, maybe update it */
801 if (p->value > (*(sp->scoms_chain))->value) {
802 (*(sp->scoms_chain))->value = p->value;
803 }
804 }
805 else {
806 /* Take a value from the ref chain
807 Here we are moving a symbol from the owning bfd
808 to another bfd. We must set up the
809 common_section portion of the bfd thing */
810
811
812
813 sp->scoms_chain = sp->srefs_chain;
814 sp->srefs_chain =
815 (asymbol **)((*(sp->srefs_chain))->udata);
816 (*(sp->scoms_chain))->udata = (PTR)NULL;
817
818 (*( sp->scoms_chain))->flags = BSF_FORT_COMM;
819 /* Remember the size of this item */
820 sp->scoms_chain[0]->value = p->value;
821 commons_pending++;
822 undefined_global_sym_count--;
823 } {
824 asymbol *com = *(sp->scoms_chain);
825 if (((lang_input_statement_type *)
826 (com->the_bfd->usrdata))->common_section ==
827 (asection *)NULL) {
828 ((lang_input_statement_type *)
829 (com->the_bfd->usrdata))->common_section =
830 bfd_make_section(com->the_bfd, "COMMON");
831 }
832 }
833 ASSERT(p->udata == 0);
834 }
835
836 else {
837 if (write_map)
838 {
839 info("%I needed due to %s\n",entry, sp->name);
840 }
841 return true;
842 }
843 }
844 }
845 }
846 }
847
848 return false;
849 }
850
This page took 0.048471 seconds and 4 git commands to generate.