Merges from Intel.
[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 "ld-emul.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;
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
105
106
107
108
109
110
111
112 /* Force the make_executable to be output, even if there are non-fatal
113 errors */
114 boolean force_make_executable;
115
116
117 /* A count of the total number of local symbols ever seen - by adding
118 the symbol_count field of each newly read afile.*/
119
120
121 unsigned int total_symbols_seen;
122
123 /* A count of the number of read files - the same as the number of elements
124 in file_chain
125 */
126 unsigned int total_files_seen;
127
128
129 /* IMPORTS */
130 args_type command_line;
131 ld_config_type config;
132 int
133 main (argc, argv)
134 char **argv;
135 int argc;
136 {
137 char *emulation;
138 program_name = argv[0];
139 output_filename = "a.out";
140
141 #ifdef GNU960
142 check_v960( argc, argv );
143 #endif
144
145 emulation = (char *) getenv(EMULATION_ENVIRON);
146
147 /* Initialize the data about options. */
148 strip_symbols = STRIP_NONE;
149 trace_files = false;
150 discard_locals = DISCARD_NONE;
151
152 write_map = false;
153 config.relocateable_output = false;
154 unix_relocate = 0;
155 command_line.force_common_definition = false;
156
157 ldfile_add_arch("");
158 ldfile_add_library_path("./");
159 config.make_executable = true;
160 force_make_executable = false;
161
162
163 /* Initialize the cumulative counts of symbols. */
164 undefined_global_sym_count = 0;
165 warning_count = 0;
166 multiple_def_count = 0;
167 commons_pending = 0;
168
169 config.magic_demand_paged = true ;
170 config.make_executable = true;
171
172 #ifdef GNU960
173 ldemul_choose_mode(LNK960_EMULATION_NAME);
174 #else
175 if (emulation == (char *)NULL) {
176 emulation= DEFAULT_EMULATION;
177 }
178
179
180 ldemul_choose_mode(emulation);
181 #endif
182
183 default_target = ldemul_choose_target();
184
185 lang_init();
186 ldemul_before_parse();
187 lang_has_input_file = false;
188 parse_args(argc, argv);
189 lang_final();
190 if (trace_files) {
191 info("%P: mode %s\n", emulation);
192 }
193 if (lang_has_input_file == false) {
194 info("%P%F: No input files\n");
195 }
196
197 ldemul_after_parse();
198
199 lang_process();
200
201
202
203
204 /* Print error messages for any missing symbols, for any warning
205 symbols, and possibly multiple definitions */
206
207 /* Print a map, if requested. */
208
209 if (write_map) {
210 ldsym_print_symbol_table ();
211 lang_map(stdout);
212 }
213
214
215 if (config.relocateable_output) {
216 output_bfd->flags &= ~( D_PAGED);
217 output_bfd->flags |= EXEC_P;
218 ldwrite();
219 bfd_close(output_bfd);
220 }
221 else {
222 output_bfd->flags |= EXEC_P;
223
224 ldwrite();
225 bfd_close(output_bfd);
226 if (config.make_executable == false && force_make_executable == false) {
227 unlink(output_filename);
228 }
229 return (!config.make_executable);
230 }
231
232 return(0);
233 } /* main() */
234
235
236 void
237 Q_read_entry_symbols (desc, entry)
238 bfd *desc;
239 struct lang_input_statement_struct *entry;
240 {
241 if (entry->asymbols == (asymbol **)NULL) {
242 size_t table_size = get_symtab_upper_bound(desc);
243 entry->asymbols = (asymbol **)ldmalloc(table_size);
244
245 entry->symbol_count = bfd_canonicalize_symtab(desc, entry->asymbols) ;
246 }
247 }
248
249
250 /*
251 * turn this item into a reference
252 */
253 static void
254 refize(sp, nlist_p)
255 ldsym_type *sp;
256 asymbol **nlist_p;
257 {
258 asymbol *sym = *nlist_p;
259 sym->value = 0;
260 sym->flags = BSF_UNDEFINED;
261 sym->section = (asection *)NULL;
262 sym->udata =(PTR)( sp->srefs_chain);
263 sp->srefs_chain = nlist_p;
264 }
265 /*
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.
271
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.
275
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.
283
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.
286 */
287
288
289 void
290 Q_enter_global_ref (nlist_p)
291 asymbol **nlist_p;
292
293 {
294 asymbol *sym = *nlist_p;
295 CONST char *name = sym->name;
296 ldsym_type *sp = ldsym_get (name);
297
298 flagword this_symbol_flags = sym->flags;
299
300
301 ASSERT(sym->udata == 0);
302
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.
310 */
311 if (sp->sdefs_chain)
312 {
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
315 it were a reference
316 */
317 refize(sp, nlist_p);
318 }
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 */
323 refize(sp, nlist_p);
324 }
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;
329 }
330 }
331 else {
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
334 */
335 if (sp->srefs_chain)
336 undefined_global_sym_count--;
337
338 commons_pending++;
339 sp->scoms_chain = nlist_p;
340 }
341 }
342
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;
352
353 multiple_def_count++;
354 info("%C: multiple definition of `%T'\n",
355 sym->the_bfd,
356 sym->section,
357 stat1_symbols,
358 sym->value,
359 sym);
360
361 info("%C: first seen here\n",
362 sy->the_bfd,
363 sy->section,
364 stat_symbols,
365 sy->value);
366 }
367 else {
368 sym->udata =(PTR)( sp->sdefs_chain);
369 sp->sdefs_chain = nlist_p;
370 }
371 /* A definition overrides a common symbol */
372 if (sp->scoms_chain) {
373 refize(sp, sp->scoms_chain);
374 sp->scoms_chain = 0;
375 commons_pending--;
376 }
377 else if (sp->srefs_chain) {
378 /* If previously was undefined, then remember as defined */
379 undefined_global_sym_count--;
380 }
381 }
382 else {
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++;
388
389 }
390
391 refize(sp, nlist_p);
392 }
393
394 ASSERT(sp->sdefs_chain == 0 || sp->scoms_chain == 0);
395 ASSERT(sp->scoms_chain ==0 || (*(sp->scoms_chain))->udata == 0);
396
397
398 }
399
400 static void
401 Q_enter_file_symbols (entry)
402 lang_input_statement_type *entry;
403 {
404 asymbol **q ;
405 entry->common_section =
406 bfd_make_section(entry->the_bfd, "COMMON");
407
408 ldlang_add_file(entry);
409
410
411 if (trace_files || option_v) {
412 info("%I\n", entry);
413 }
414
415 total_symbols_seen += entry->symbol_count;
416 total_files_seen ++;
417 for (q = entry->asymbols; *q; q++)
418 {
419 asymbol *p = *q;
420
421 if (flag_is_undefined_or_global_or_common(p->flags))
422 {
423 Q_enter_global_ref(q);
424 }
425 ASSERT(p->flags != 0);
426 }
427 }
428
429
430
431 /* Searching libraries */
432
433 struct lang_input_statement_struct *decode_library_subfile ();
434 void linear_library (), symdef_library ();
435
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. */
440
441 void
442 search_library (entry)
443 struct lang_input_statement_struct *entry;
444 {
445
446 /* No need to load a library if no undefined symbols */
447 if (!undefined_global_sym_count) return;
448
449 if (bfd_has_map(entry->the_bfd))
450 symdef_library (entry);
451 else
452 linear_library (entry);
453
454 }
455
456
457 #ifdef GNU960
458 static
459 boolean
460 gnu960_check_format (abfd, format)
461 bfd *abfd;
462 bfd_format format;
463 {
464 boolean retval;
465
466 if ((bfd_check_format(abfd,format) == true) && BFD_COFF_FILE_P(abfd)) {
467 return true;
468 }
469 return false;
470 }
471 #endif
472
473 void
474 ldmain_open_file_read_symbol (entry)
475 struct lang_input_statement_struct *entry;
476 {
477 if (entry->asymbols == (asymbol **)NULL
478 &&entry->real == true
479 && entry->filename != (char *)NULL)
480 {
481 ldfile_open_file (entry);
482
483 #ifdef GNU960
484 if (gnu960_check_format(entry->the_bfd, bfd_object))
485 #else
486 if (bfd_check_format(entry->the_bfd, bfd_object))
487 #endif
488 {
489 entry->the_bfd->usrdata = (PTR)entry;
490
491
492 Q_read_entry_symbols (entry->the_bfd, entry);
493 Q_enter_file_symbols (entry);
494 }
495 #ifdef GNU960
496 else if (gnu960_check_format(entry->the_bfd, bfd_archive))
497 #else
498 else if (bfd_check_format(entry->the_bfd, bfd_archive))
499 #endif
500 {
501 entry->the_bfd->usrdata = (PTR)entry;
502
503 entry->subfiles = (lang_input_statement_type *)NULL;
504 search_library (entry);
505 }
506 else
507 {
508 info("%F%B: malformed input file (not rel or archive) \n",
509 entry->the_bfd);
510 }
511 }
512
513 }
514
515
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. */
521
522 lang_input_statement_type *
523 decode_library_subfile (library_entry, subfile_offset)
524 struct lang_input_statement_struct *library_entry;
525 bfd *subfile_offset;
526 {
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;
534 subentry->next = 0;
535 subentry->superfile = library_entry;
536 subentry->is_archive = false;
537
538 subentry->just_syms_flag = false;
539 subentry->loaded = false;
540 subentry->chain = 0;
541
542 return subentry;
543 }
544
545 boolean subfile_wanted_p ();
546 void
547 clear_syms(entry, offset)
548 struct lang_input_statement_struct *entry;
549 file_ptr offset;
550 {
551 carsym *car;
552 unsigned long indx = bfd_get_next_mapent(entry->the_bfd,
553 BFD_NO_MORE_SYMBOLS,
554 &car);
555 while (indx != BFD_NO_MORE_SYMBOLS) {
556 if (car->file_offset == offset) {
557 car->name = 0;
558 }
559 indx = bfd_get_next_mapent(entry->the_bfd, indx, &car);
560 }
561
562 }
563
564 /* Search a library that has a map
565 */
566 void
567 symdef_library (entry)
568 struct lang_input_statement_struct *entry;
569
570 {
571 register struct lang_input_statement_struct *prev = 0;
572
573 boolean not_finished = true;
574
575
576 while (not_finished == true)
577 {
578 carsym *exported_library_name;
579 bfd *prev_archive_member_bfd = 0;
580
581 int idx = bfd_get_next_mapent(entry->the_bfd,
582 BFD_NO_MORE_SYMBOLS,
583 &exported_library_name);
584
585 not_finished = false;
586
587 while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count)
588 {
589
590 if (exported_library_name->name)
591 {
592
593 ldsym_type *sp = ldsym_get_soft (exported_library_name->name);
594
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
598 undefined or */
599 if (sp && sp->srefs_chain && !sp->sdefs_chain)
600 {
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;
603
604 #ifdef GNU960
605 if (archive_member_bfd && gnu960_check_format(archive_member_bfd, bfd_object))
606 #else
607 if (archive_member_bfd && bfd_check_format(archive_member_bfd, bfd_object))
608 #endif
609 {
610
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)
614 {
615
616 prev_archive_member_bfd = archive_member_bfd;
617
618 /* Read the symbol table of the archive member. */
619
620 if (archive_member_bfd->usrdata != (PTR)NULL) {
621
622 archive_member_lang_input_statement_struct =(lang_input_statement_type *) archive_member_bfd->usrdata;
623 }
624 else {
625
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;
629
630 }
631
632 if (archive_member_lang_input_statement_struct == 0) {
633 info ("%F%I contains invalid archive member %s\n",
634 entry,
635 sp->name);
636 }
637
638 if (archive_member_lang_input_statement_struct->loaded == false)
639 {
640
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. */
643
644
645 if (subfile_wanted_p (archive_member_lang_input_statement_struct) == true)
646
647 {
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. */
651
652 not_finished = true;
653
654 Q_enter_file_symbols (archive_member_lang_input_statement_struct);
655
656 if (prev)
657 prev->chain = archive_member_lang_input_statement_struct;
658 else
659 entry->subfiles = archive_member_lang_input_statement_struct;
660
661
662 prev = archive_member_lang_input_statement_struct;
663
664
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;
669 }
670 }
671 }
672 }
673 }
674 }
675 idx = bfd_get_next_mapent(entry->the_bfd, idx, &exported_library_name);
676 }
677 }
678 }
679
680 void
681 linear_library (entry)
682 struct lang_input_statement_struct *entry;
683 {
684 boolean more_to_do = true;
685 register struct lang_input_statement_struct *prev = 0;
686
687 while (more_to_do) {
688
689 bfd * archive = bfd_openr_next_archived_file(entry->the_bfd,0);
690
691 more_to_do = false;
692 while (archive) {
693 #ifdef GNU960
694 if (gnu960_check_format(archive, bfd_object))
695 #else
696 if (bfd_check_format(archive, bfd_object))
697 #endif
698 {
699 register struct lang_input_statement_struct *subentry;
700
701 subentry = decode_library_subfile (entry,
702 archive);
703
704 archive->usrdata = (PTR) subentry;
705 if (!subentry) return;
706 if (subentry->loaded == false) {
707 Q_read_entry_symbols (archive, subentry);
708
709 if (subfile_wanted_p (subentry) == true)
710 {
711 Q_enter_file_symbols (subentry);
712
713 if (prev)
714 prev->chain = subentry;
715 else
716 entry->subfiles = subentry;
717 prev = subentry;
718
719 more_to_do = true;
720 subentry->loaded = true;
721 }
722 }
723 }
724 archive = bfd_openr_next_archived_file(entry->the_bfd,archive);
725
726 }
727
728 }
729 }
730
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. */
734
735 boolean
736 subfile_wanted_p (entry)
737 struct lang_input_statement_struct *entry;
738 {
739 asymbol **q;
740
741 for (q = entry->asymbols; *q; q++)
742 {
743 asymbol *p = *q;
744
745 /* If the symbol has an interesting definition, we could
746 potentially want it. */
747
748 if (p->flags & BSF_FORT_COMM
749 || p->flags & BSF_GLOBAL)
750 {
751 register ldsym_type *sp = ldsym_get_soft (p->name);
752
753
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)
760 {
761 /* This is a symbol we are looking for. It is either
762 not yet defined or common. */
763
764 if (flag_is_common(p->flags))
765 {
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;
774 }
775 }
776 else {
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 */
781
782
783
784 sp->scoms_chain = sp->srefs_chain;
785 sp->srefs_chain =
786 (asymbol **)((*(sp->srefs_chain))->udata);
787 (*(sp->scoms_chain))->udata = (PTR)NULL;
788
789 (*( sp->scoms_chain))->flags = BSF_FORT_COMM;
790 /* Remember the size of this item */
791 sp->scoms_chain[0]->value = p->value;
792 commons_pending++;
793 undefined_global_sym_count--;
794 } {
795 asymbol *com = *(sp->scoms_chain);
796 if (((lang_input_statement_type *)
797 (com->the_bfd->usrdata))->common_section ==
798 (asection *)NULL) {
799 ((lang_input_statement_type *)
800 (com->the_bfd->usrdata))->common_section =
801 bfd_make_section(com->the_bfd, "COMMON");
802 }
803 }
804 ASSERT(p->udata == 0);
805 }
806
807 else {
808 if (write_map)
809 {
810 info("%I needed due to %s\n",entry, sp->name);
811 }
812 return true;
813 }
814 }
815 }
816 }
817 }
818
819 return false;
820 }
821