168a7364fa4b7c87c50f9f7f21116b6ffdcc454c
[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 emulation = getenv(EMULATION_ENVIRON);
141
142 /* Initialize the data about options. */
143 strip_symbols = STRIP_NONE;
144 trace_files = false;
145 discard_locals = DISCARD_NONE;
146
147 write_map = false;
148 config.relocateable_output = false;
149 unix_relocate = 0;
150 command_line.force_common_definition = false;
151
152 ldfile_add_arch("");
153 ldfile_add_library_path("./");
154 config.make_executable = true;
155 force_make_executable = false;
156
157
158 /* Initialize the cumulative counts of symbols. */
159 undefined_global_sym_count = 0;
160 warning_count = 0;
161 multiple_def_count = 0;
162 commons_pending = 0;
163
164 config.magic_demand_paged = true ;
165 config.make_executable = true;
166
167 if (emulation == (char *)NULL) {
168 emulation= DEFAULT_EMULATION;
169 }
170
171
172 ldemul_choose_mode(emulation);
173
174 default_target = ldemul_choose_target();
175
176 lang_init();
177 ldemul_before_parse();
178 lang_has_input_file = false;
179 parse_args(argc, argv);
180 lang_final();
181 if (trace_files) {
182 info("%P: mode %s\n", emulation);
183 }
184 if (lang_has_input_file == false) {
185 info("%P%F: No input files\n");
186 }
187
188 ldemul_after_parse();
189
190 lang_process();
191
192
193
194
195 /* Print error messages for any missing symbols, for any warning
196 symbols, and possibly multiple definitions */
197
198 /* Print a map, if requested. */
199
200 if (write_map) {
201 ldsym_print_symbol_table ();
202 lang_map(stdout);
203 }
204
205
206 if (config.relocateable_output) {
207 output_bfd->flags &= ~( D_PAGED);
208 output_bfd->flags |= EXEC_P;
209 ldwrite();
210 bfd_close(output_bfd);
211 }
212 else {
213 output_bfd->flags |= EXEC_P;
214
215 ldwrite();
216 bfd_close(output_bfd);
217 if (config.make_executable == false && force_make_executable == false) {
218 unlink(output_filename);
219 }
220 return (!config.make_executable);
221 }
222
223 return(0);
224 } /* main() */
225
226
227 void
228 Q_read_entry_symbols (desc, entry)
229 bfd *desc;
230 struct lang_input_statement_struct *entry;
231 {
232 if (entry->asymbols == (asymbol **)NULL) {
233 size_t table_size = get_symtab_upper_bound(desc);
234 entry->asymbols = (asymbol **)ldmalloc(table_size);
235
236 entry->symbol_count = bfd_canonicalize_symtab(desc, entry->asymbols) ;
237 }
238 }
239
240
241 /*
242 * turn this item into a reference
243 */
244 static void
245 refize(sp, nlist_p)
246 ldsym_type *sp;
247 asymbol **nlist_p;
248 {
249 asymbol *sym = *nlist_p;
250 sym->value = 0;
251 sym->flags = BSF_UNDEFINED;
252 sym->section = (asection *)NULL;
253 sym->udata =(void *)( sp->srefs_chain);
254 sp->srefs_chain = nlist_p;
255 }
256 /*
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.
262
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.
266
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.
274
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.
277 */
278
279
280 void
281 Q_enter_global_ref (nlist_p)
282 asymbol **nlist_p;
283
284 {
285 asymbol *sym = *nlist_p;
286 char *name = sym->name;
287 ldsym_type *sp = ldsym_get (name);
288
289 flagword this_symbol_flags = sym->flags;
290
291
292 ASSERT(sym->udata == 0);
293
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.
301 */
302 if (sp->sdefs_chain)
303 {
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
306 it were a reference
307 */
308 refize(sp, nlist_p);
309 }
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 */
314 refize(sp, nlist_p);
315 }
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;
320 }
321 }
322 else {
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
325 */
326 if (sp->srefs_chain)
327 undefined_global_sym_count--;
328
329 commons_pending++;
330 sp->scoms_chain = nlist_p;
331 }
332 }
333
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;
343
344 multiple_def_count++;
345 info("%C: multiple definition of `%T'\n",
346 sym->the_bfd,
347 sym->section,
348 stat1_symbols,
349 sym->value,
350 sym);
351
352 info("%C: first seen here\n",
353 sy->the_bfd,
354 sy->section,
355 stat_symbols,
356 sy->value);
357 }
358 else {
359 sym->udata =(void *)( sp->sdefs_chain);
360 sp->sdefs_chain = nlist_p;
361 }
362 /* A definition overrides a common symbol */
363 if (sp->scoms_chain) {
364 refize(sp, sp->scoms_chain);
365 sp->scoms_chain = 0;
366 commons_pending--;
367 }
368 else if (sp->srefs_chain) {
369 /* If previously was undefined, then remember as defined */
370 undefined_global_sym_count--;
371 }
372 }
373 else {
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++;
379
380 }
381
382 refize(sp, nlist_p);
383 }
384
385 ASSERT(sp->sdefs_chain == 0 || sp->scoms_chain == 0);
386 ASSERT(sp->scoms_chain ==0 || (*(sp->scoms_chain))->udata == 0);
387
388
389 }
390
391 static void
392 Q_enter_file_symbols (entry)
393 lang_input_statement_type *entry;
394 {
395 asymbol **q ;
396 entry->common_section =
397 bfd_make_section(entry->the_bfd, "COMMON");
398
399 ldlang_add_file(entry);
400
401
402 if (trace_files || option_v) {
403 info("%I\n", entry);
404 }
405
406 total_symbols_seen += entry->symbol_count;
407 total_files_seen ++;
408 for (q = entry->asymbols; *q; q++)
409 {
410 asymbol *p = *q;
411
412 if (flag_is_undefined_or_global_or_common(p->flags))
413 {
414 Q_enter_global_ref(q);
415 }
416 ASSERT(p->flags != 0);
417 }
418 }
419
420
421
422 /* Searching libraries */
423
424 struct lang_input_statement_struct *decode_library_subfile ();
425 void linear_library (), symdef_library ();
426
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. */
431
432 void
433 search_library (entry)
434 struct lang_input_statement_struct *entry;
435 {
436
437 /* No need to load a library if no undefined symbols */
438 if (!undefined_global_sym_count) return;
439
440 if (bfd_has_map(entry->the_bfd))
441 symdef_library (entry);
442 else
443 linear_library (entry);
444
445 }
446
447
448 void
449 ldmain_open_file_read_symbol (entry)
450 struct lang_input_statement_struct *entry;
451 {
452 if (entry->asymbols == (asymbol **)NULL
453 &&entry->real == true
454 && entry->filename != (char *)NULL)
455 {
456 ldfile_open_file (entry);
457
458 if (bfd_check_format(entry->the_bfd, bfd_object))
459 {
460 entry->the_bfd->usrdata = (void*)entry;
461
462
463 Q_read_entry_symbols (entry->the_bfd, entry);
464 Q_enter_file_symbols (entry);
465 }
466 else if (bfd_check_format(entry->the_bfd, bfd_archive))
467 {
468 entry->the_bfd->usrdata = (void *)entry;
469
470 entry->subfiles = (lang_input_statement_type *)NULL;
471 search_library (entry);
472 }
473 else
474 {
475 info("%F%I: malformed input file (not rel or archive) \n", entry);
476 }
477 }
478
479 }
480
481
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. */
487
488 lang_input_statement_type *
489 decode_library_subfile (library_entry, subfile_offset)
490 struct lang_input_statement_struct *library_entry;
491 bfd *subfile_offset;
492 {
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;
500 subentry->next = 0;
501 subentry->superfile = library_entry;
502 subentry->is_archive = false;
503
504 subentry->just_syms_flag = false;
505 subentry->loaded = false;
506 subentry->chain = 0;
507
508 return subentry;
509 }
510
511 boolean subfile_wanted_p ();
512 void
513 clear_syms(entry, offset)
514 struct lang_input_statement_struct *entry;
515 file_ptr offset;
516 {
517 carsym *car;
518 unsigned long indx = bfd_get_next_mapent(entry->the_bfd,
519 BFD_NO_MORE_SYMBOLS,
520 &car);
521 while (indx != BFD_NO_MORE_SYMBOLS) {
522 if (car->file_offset == offset) {
523 car->name = 0;
524 }
525 indx = bfd_get_next_mapent(entry->the_bfd, indx, &car);
526 }
527
528 }
529
530 /* Search a library that has a map
531 */
532 void
533 symdef_library (entry)
534 struct lang_input_statement_struct *entry;
535
536 {
537 register struct lang_input_statement_struct *prev = 0;
538
539 boolean not_finished = true;
540
541
542 while (not_finished == true)
543 {
544 carsym *exported_library_name;
545 bfd *prev_archive_member_bfd = 0;
546
547 int idx = bfd_get_next_mapent(entry->the_bfd,
548 BFD_NO_MORE_SYMBOLS,
549 &exported_library_name);
550
551 not_finished = false;
552
553 while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count)
554 {
555
556 if (exported_library_name->name)
557 {
558
559 ldsym_type *sp = ldsym_get_soft (exported_library_name->name);
560
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
564 undefined or */
565 if (sp && sp->srefs_chain && !sp->sdefs_chain)
566 {
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;
569
570 if (archive_member_bfd && bfd_check_format(archive_member_bfd, bfd_object))
571 {
572
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)
576 {
577
578 prev_archive_member_bfd = archive_member_bfd;
579
580 /* Read the symbol table of the archive member. */
581
582 if (archive_member_bfd->usrdata != (void *)NULL) {
583
584 archive_member_lang_input_statement_struct =(lang_input_statement_type *) archive_member_bfd->usrdata;
585 }
586 else {
587
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;
591
592 }
593
594 if (archive_member_lang_input_statement_struct == 0) {
595 info ("%F%I contains invalid archive member %s\n",
596 entry,
597 sp->name);
598 }
599
600 if (archive_member_lang_input_statement_struct->loaded == false)
601 {
602
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. */
605
606
607 if (subfile_wanted_p (archive_member_lang_input_statement_struct) == true)
608
609 {
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. */
613
614 not_finished = true;
615
616 Q_enter_file_symbols (archive_member_lang_input_statement_struct);
617
618 if (prev)
619 prev->chain = archive_member_lang_input_statement_struct;
620 else
621 entry->subfiles = archive_member_lang_input_statement_struct;
622
623
624 prev = archive_member_lang_input_statement_struct;
625
626
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;
631 }
632 }
633 }
634 }
635 }
636 }
637 idx = bfd_get_next_mapent(entry->the_bfd, idx, &exported_library_name);
638 }
639 }
640 }
641
642 void
643 linear_library (entry)
644 struct lang_input_statement_struct *entry;
645 {
646 boolean more_to_do = true;
647 register struct lang_input_statement_struct *prev = 0;
648
649 while (more_to_do) {
650
651 bfd * archive = bfd_openr_next_archived_file(entry->the_bfd,0);
652
653 more_to_do = false;
654 while (archive) {
655 if (bfd_check_format(archive, bfd_object))
656 {
657 register struct lang_input_statement_struct *subentry;
658
659 subentry = decode_library_subfile (entry,
660 archive);
661
662 archive->usrdata = (void *) subentry;
663 if (!subentry) return;
664 if (subentry->loaded == false) {
665 Q_read_entry_symbols (archive, subentry);
666
667 if (subfile_wanted_p (subentry) == true)
668 {
669 Q_enter_file_symbols (subentry);
670
671 if (prev)
672 prev->chain = subentry;
673 else
674 entry->subfiles = subentry;
675 prev = subentry;
676
677 more_to_do = true;
678 subentry->loaded = true;
679 }
680 }
681 }
682 archive = bfd_openr_next_archived_file(entry->the_bfd,archive);
683
684 }
685
686 }
687 }
688
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. */
692
693 boolean
694 subfile_wanted_p (entry)
695 struct lang_input_statement_struct *entry;
696 {
697 asymbol **q;
698
699 for (q = entry->asymbols; *q; q++)
700 {
701 asymbol *p = *q;
702
703 /* If the symbol has an interesting definition, we could
704 potentially want it. */
705
706 if (p->flags & BSF_FORT_COMM
707 || p->flags & BSF_GLOBAL)
708 {
709 register ldsym_type *sp = ldsym_get_soft (p->name);
710
711
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)
718 {
719 /* This is a symbol we are looking for. It is either
720 not yet defined or common. */
721
722 if (flag_is_common(p->flags))
723 {
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;
732 }
733 }
734 else {
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 */
739
740
741
742 sp->scoms_chain = sp->srefs_chain;
743 sp->srefs_chain =
744 (asymbol **)((*(sp->srefs_chain))->udata);
745 (*(sp->scoms_chain))->udata = (void*)NULL;
746
747 (*( sp->scoms_chain))->flags = BSF_FORT_COMM;
748 /* Remember the size of this item */
749 sp->scoms_chain[0]->value = p->value;
750 commons_pending++;
751 undefined_global_sym_count--;
752 } {
753 asymbol *com = *(sp->scoms_chain);
754 if (((lang_input_statement_type *)
755 (com->the_bfd->usrdata))->common_section ==
756 (asection *)NULL) {
757 ((lang_input_statement_type *)
758 (com->the_bfd->usrdata))->common_section =
759 bfd_make_section(com->the_bfd, "COMMON");
760 }
761 }
762 ASSERT(p->udata == 0);
763 }
764
765 else {
766 if (write_map)
767 {
768 info("%I needed due to %s\n",entry, sp->name);
769 }
770 return true;
771 }
772 }
773 }
774 }
775 }
776
777 return false;
778 }
779