* configure.in (mips*-sgi-irix5*): New target. Use mipsb-elf32.
[deliverable/binutils-gdb.git] / ld / ldmain.c
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 Written by Steve Chamberlain steve@cygnus.com
3
4 This file is part of GLD, the Gnu Linker.
5
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 #include "bfd.h"
22 #include "sysdep.h"
23
24 #include "config.h"
25 #include "ld.h"
26 #include "ldmain.h"
27 #include "ldmisc.h"
28 #include "ldwrite.h"
29 #include "ldgram.h"
30 #include "ldsym.h"
31 #include "ldlang.h"
32 #include "ldemul.h"
33 #include "ldlex.h"
34 #include "ldfile.h"
35 #include "ldindr.h"
36 #include "ldwarn.h"
37 #include "ldctor.h"
38 #include "lderror.h"
39
40 /* Somewhere above, sys/stat.h got included . . . . */
41 #if !defined(S_ISDIR) && defined(S_IFDIR)
42 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
43 #endif
44
45 #include <string.h>
46
47 static char *get_emulation ();
48 static void set_scripts_dir ();
49
50 /* IMPORTS */
51 extern boolean lang_has_input_file;
52 extern boolean force_make_executable;
53 extern boolean relaxing;
54 extern boolean had_script;
55
56 /* EXPORTS */
57
58 char *default_target;
59 char *output_filename = "a.out";
60
61 /* Name this program was invoked by. */
62 char *program_name;
63
64 /* The file that we're creating */
65 bfd *output_bfd = 0;
66
67 /* set if -y on the command line */
68 int had_y;
69
70 /* The local symbol prefix */
71 char lprefix = 'L';
72
73 /* Set by -G argument, for MIPS ECOFF target. */
74 int g_switch_value = 8;
75
76 /* Count the number of global symbols multiply defined. */
77 int multiple_def_count;
78
79 /* Count the number of symbols defined through common declarations.
80 This count is referenced in symdef_library, linear_library, and
81 modified by enter_global_ref.
82
83 It is incremented when a symbol is created as a common, and
84 decremented when the common declaration is overridden
85
86 Another way of thinking of it is that this is a count of
87 all ldsym_types with a ->scoms field */
88
89 unsigned int commons_pending;
90
91 /* Count the number of global symbols referenced and not defined.
92 common symbols are not included in this count. */
93
94 unsigned int undefined_global_sym_count;
95
96 /* Nonzero means print names of input files as processed. */
97 boolean trace_files;
98
99 /* Nonzero means same, but note open failures, too. */
100 boolean trace_file_tries;
101
102 /* 1 => write load map. */
103 boolean write_map;
104
105 #ifdef GNU960
106 /* Indicates whether output file will be b.out (default) or coff */
107 enum target_flavour output_flavor = BFD_BOUT_FORMAT;
108 #endif
109
110 /* A count of the total number of local symbols ever seen - by adding
111 the symbol_count field of each newly read afile.*/
112
113 unsigned int total_symbols_seen;
114
115 /* A count of the number of read files - the same as the number of elements
116 in file_chain
117 */
118 unsigned int total_files_seen;
119
120 args_type command_line;
121
122 ld_config_type config;
123 \f
124 void
125 main (argc, argv)
126 char **argv;
127 int argc;
128 {
129 char *emulation;
130
131 program_name = argv[0];
132
133 bfd_init ();
134
135 /* Initialize the data about options. */
136
137
138 trace_files = trace_file_tries = false;
139 write_map = false;
140 config.relocateable_output = false;
141 command_line.force_common_definition = false;
142
143 init_bfd_error_vector ();
144 ldsym_init ();
145 ldfile_add_arch ("");
146
147 config.make_executable = true;
148 force_make_executable = false;
149
150 /* Initialize the cumulative counts of symbols. */
151 undefined_global_sym_count = 0;
152 multiple_def_count = 0;
153 commons_pending = 0;
154
155 config.magic_demand_paged = true;
156 config.text_read_only = true;
157 config.make_executable = true;
158
159 emulation = get_emulation (argc, argv);
160 ldemul_choose_mode (emulation);
161 default_target = ldemul_choose_target ();
162 lang_init ();
163 ldemul_before_parse ();
164 lang_has_input_file = false;
165 parse_args (argc, argv);
166
167 /* This essentially adds another -L directory so this must be done after
168 the -L's in argv have been processed. */
169 set_scripts_dir ();
170
171 if (had_script == false)
172 {
173 /* Read the emulation's appropriate default script. */
174 int isfile;
175 char *s = ldemul_get_script (&isfile);
176
177 if (isfile)
178 {
179 /* sizeof counts the terminating NUL. */
180 size_t size = strlen (s) + sizeof ("-T ");
181 char *buf = (char *) ldmalloc(size);
182 sprintf (buf, "-T %s", s);
183 parse_line (buf, 0);
184 free (buf);
185 }
186 else
187 parse_line (s, 1);
188 }
189
190 if (config.relocateable_output && command_line.relax)
191 {
192 einfo ("%P%F: -relax and -r may not be used together\n");
193 }
194 lang_final ();
195
196 if (lang_has_input_file == false)
197 {
198 einfo ("%P%F: no input files\n");
199 }
200
201 if (trace_files)
202 {
203 info_msg ("%P: mode %s\n", emulation);
204 }
205
206 ldemul_after_parse ();
207
208
209 if (config.map_filename)
210 {
211 if (strcmp (config.map_filename, "-") == 0)
212 {
213 config.map_file = stdout;
214 }
215 else
216 {
217 config.map_file = fopen (config.map_filename, FOPEN_WT);
218 if (config.map_file == (FILE *) NULL)
219 {
220 einfo ("%P%F: cannot open map file %s: %E\n",
221 config.map_filename);
222 }
223 }
224 }
225
226
227 lang_process ();
228
229 /* Print error messages for any missing symbols, for any warning
230 symbols, and possibly multiple definitions */
231
232
233 if (config.text_read_only)
234 {
235 /* Look for a text section and mark the readonly attribute in it */
236 asection *found = bfd_get_section_by_name (output_bfd, ".text");
237
238 if (found != (asection *) NULL)
239 {
240 found->flags |= SEC_READONLY;
241 }
242 }
243
244 if (config.relocateable_output)
245 output_bfd->flags &= ~EXEC_P;
246 else
247 output_bfd->flags |= EXEC_P;
248
249 ldwrite ();
250
251 /* Even if we're producing relocateable output, some non-fatal errors should
252 be reported in the exit status. (What non-fatal errors, if any, do we
253 want to ignore for relocateable output?) */
254
255 if (config.make_executable == false && force_make_executable == false)
256 {
257 if (trace_files == true)
258 {
259 einfo ("%P: link errors found, deleting executable `%s'\n",
260 output_filename);
261 }
262
263 if (output_bfd->iostream)
264 fclose ((FILE *) (output_bfd->iostream));
265
266 unlink (output_filename);
267 exit (1);
268 }
269 else
270 {
271 bfd_close (output_bfd);
272 }
273
274 exit (0);
275 }
276
277 /* We need to find any explicitly given emulation in order to initialize the
278 state that's needed by the lex&yacc argument parser (parse_args). */
279
280 static char *
281 get_emulation (argc, argv)
282 int argc;
283 char **argv;
284 {
285 char *emulation;
286 int i;
287
288 #ifdef GNU960
289 check_v960 (argc, argv);
290 emulation = "gld960";
291 for (i = 1; i < argc; i++)
292 {
293 if (!strcmp (argv[i], "-Fcoff"))
294 {
295 emulation = "lnk960";
296 output_flavor = BFD_COFF_FORMAT;
297 break;
298 }
299 }
300 #else
301 emulation = (char *) getenv (EMULATION_ENVIRON);
302 if (emulation == NULL)
303 emulation = DEFAULT_EMULATION;
304 #endif
305
306 for (i = 1; i < argc; i++)
307 {
308 if (!strncmp (argv[i], "-m", 2))
309 {
310 if (argv[i][2] == '\0')
311 {
312 /* -m EMUL */
313 if (i < argc - 1)
314 {
315 emulation = argv[i + 1];
316 i++;
317 }
318 else
319 {
320 einfo("%P%F: missing argument to -m\n");
321 }
322 }
323 else if (strcmp (argv[i], "-mips1") == 0
324 || strcmp (argv[i], "-mips2") == 0
325 || strcmp (argv[i], "-mips3") == 0)
326 {
327 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
328 passed to the linker by some MIPS compilers. They
329 generally tell the linker to use a slightly different
330 library path. Perhaps someday these should be
331 implemented as emulations; until then, we just ignore
332 the arguments and hope that nobody ever creates
333 emulations named ips1, ips2 or ips3. */
334 }
335 else
336 {
337 /* -mEMUL */
338 emulation = &argv[i][2];
339 }
340 }
341 }
342
343 return emulation;
344 }
345
346 /* If directory DIR contains an "ldscripts" subdirectory,
347 add DIR to the library search path and return true,
348 else return false. */
349
350 static boolean
351 check_for_scripts_dir (dir)
352 char *dir;
353 {
354 size_t dirlen;
355 char *buf;
356 struct stat s;
357 boolean res;
358
359 dirlen = strlen (dir);
360 /* sizeof counts the terminating NUL. */
361 buf = (char *) ldmalloc (dirlen + sizeof("/ldscripts"));
362 sprintf (buf, "%s/ldscripts", dir);
363
364 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
365 free (buf);
366 if (res)
367 ldfile_add_library_path (dir);
368 return res;
369 }
370
371 /* Set the default directory for finding script files.
372 Libraries will be searched for here too, but that's ok.
373 We look for the "ldscripts" directory in:
374
375 SCRIPTDIR (passed from Makefile)
376 the dir where this program is (for using it from the build tree)
377 the dir where this program is/../lib (for installing the tool suite elsewhere) */
378
379 static void
380 set_scripts_dir ()
381 {
382 char *end, *dir;
383 size_t dirlen;
384
385 if (check_for_scripts_dir (SCRIPTDIR))
386 return; /* We've been installed normally. */
387
388 /* Look for "ldscripts" in the dir where our binary is. */
389 end = strrchr (program_name, '/');
390 if (end)
391 {
392 dirlen = end - program_name;
393 /* Make a copy of program_name in dir.
394 Leave room for later "/../lib". */
395 dir = (char *) ldmalloc (dirlen + 8);
396 strncpy (dir, program_name, dirlen);
397 dir[dirlen] = '\0';
398 }
399 else
400 {
401 dirlen = 1;
402 dir = (char *) ldmalloc (dirlen + 8);
403 strcpy (dir, ".");
404 }
405
406 if (check_for_scripts_dir (dir))
407 return; /* Don't free dir. */
408
409 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
410 strcpy (dir + dirlen, "/../lib");
411 if (check_for_scripts_dir (dir))
412 return;
413
414 free (dir); /* Well, we tried. */
415 }
416
417 void
418 read_entry_symbols (desc, entry)
419 bfd *desc;
420 struct lang_input_statement_struct *entry;
421 {
422 if (entry->asymbols == (asymbol **) NULL)
423 {
424 bfd_size_type table_size = get_symtab_upper_bound (desc);
425
426 entry->asymbols = (asymbol **) ldmalloc (table_size);
427 entry->symbol_count = bfd_canonicalize_symtab (desc, entry->asymbols);
428 }
429 }
430
431 /*
432 * turn this item into a reference
433 */
434 void
435 refize (sp, nlist_p)
436 ldsym_type *sp;
437 asymbol **nlist_p;
438 {
439 asymbol *sym = *nlist_p;
440
441 sym->value = 0;
442
443 /* FIXME: Why do we clear the flags here? This used to set the
444 flags to zero, but we must not clear BSF_WEAK. */
445 sym->flags &= BSF_WEAK;
446
447 sym->section = &bfd_und_section;
448 sym->udata = (PTR) (sp->srefs_chain);
449 sp->srefs_chain = nlist_p;
450 }
451
452 /*
453 This function is called for each name which is seen which has a global
454 scope. It enters the name into the global symbol table in the correct
455 symbol on the correct chain. Remember that each ldsym_type has three
456 chains attatched, one of all definitions of a symbol, one of all
457 references of a symbol and one of all common definitions of a symbol.
458
459 When the function is over, the supplied is left connected to the bfd
460 to which is was born, with its udata field pointing to the next member
461 on the chain in which it has been inserted.
462
463 A certain amount of jigery pokery is necessary since commons come
464 along and upset things, we only keep one item in the common chain; the
465 one with the biggest size seen sofar. When another common comes along
466 it either bumps the previous definition into the ref chain, since it
467 is bigger, or gets turned into a ref on the spot since the one on the
468 common chain is already bigger. If a real definition comes along then
469 the common gets bumped off anyway.
470
471 Whilst all this is going on we keep a count of the number of multiple
472 definitions seen, undefined global symbols and pending commons.
473 */
474
475 void
476 enter_global_ref (nlist_p, name)
477 asymbol ** nlist_p; /* pointer into symbol table from incoming bfd */
478 CONST char *name; /* name of symbol in linker table */
479 {
480 asymbol *sym = *nlist_p;
481 ldsym_type *sp;
482
483 /* Lookup the name from the incoming bfd's symbol table in the
484 linker's global symbol table */
485
486
487 flagword this_symbol_flags = sym->flags;
488
489 sp = ldsym_get (name);
490
491
492 /* If this symbol already has udata, it means that something strange
493 has happened.
494
495 The strange thing is that we've had an undefined symbol resolved by
496 an alias, but the thing the alias defined wasn't in the file. So
497 the symbol got a udata entry, but the file wasn't loaded. Then
498 later on the file was loaded, but we don't need to do this
499 processing again */
500
501
502 if (sym->udata)
503 return;
504
505
506 if (this_symbol_flags & BSF_CONSTRUCTOR)
507 {
508 /* Add this constructor to the list we keep */
509 ldlang_add_constructor (sp);
510 /* Turn any commons into refs */
511 if (sp->scoms_chain != (asymbol **) NULL)
512 {
513 refize (sp, sp->scoms_chain);
514 sp->scoms_chain = 0;
515 }
516 }
517 else if ((sym->flags & BSF_WEAK) != 0
518 && (sp->sdefs_chain != NULL || sp->scoms_chain != NULL))
519 {
520 /* SYM is a weak symbol for which we already have a definition.
521 Just ignore it. The UnixWare linker is order dependent: if a
522 global symbol follows a weak symbol it gives an error; if a
523 weak symbol follows a global symbol it does not. We are
524 compatible. */
525 }
526 else
527 {
528 if (bfd_is_com_section (sym->section))
529 {
530 /* If we have a definition of this symbol already then
531 this common turns into a reference. Also we only
532 ever point to the largest common, so if we
533 have a common, but it's bigger that the new symbol
534 the turn this into a reference too. */
535 if (sp->sdefs_chain)
536 {
537 /* This is a common symbol, but we already have a definition
538 for it, so just link it into the ref chain as if
539 it were a reference */
540 if (config.warn_common)
541 multiple_warn("%C: warning: common of `%s' overridden by definition\n",
542 sym,
543 "%C: warning: defined here\n",
544 *(sp->sdefs_chain));
545 refize (sp, nlist_p);
546 }
547 else if (sp->scoms_chain)
548 {
549 /* If we have a previous common, keep only the biggest */
550 if ((*(sp->scoms_chain))->value > sym->value)
551 {
552 /* other common is bigger, throw this one away */
553 if (config.warn_common)
554 multiple_warn("%C: warning: common of `%s' overridden by larger common\n",
555 sym,
556 "%C: warning: larger common is here\n",
557 *(sp->scoms_chain));
558 refize (sp, nlist_p);
559 }
560 else if (sp->scoms_chain != nlist_p)
561 {
562 /* other common is smaller, throw that away */
563 if (config.warn_common)
564 {
565 if ((*(sp->scoms_chain))->value < sym->value)
566 multiple_warn("%C: warning: common of `%s' overriding smaller common\n",
567 sym,
568 "%C: warning: smaller common is here\n",
569 *(sp->scoms_chain));
570 else
571 multiple_warn("%C: warning: multiple common of `%s'\n",
572 sym,
573 "%C: warning: previous common is here\n",
574 *(sp->scoms_chain));
575 }
576 refize (sp, sp->scoms_chain);
577 sp->scoms_chain = nlist_p;
578 }
579 }
580 else
581 {
582 /* This is the first time we've seen a common, so remember it
583 - if it was undefined before, we know it's defined now. If
584 the symbol has been marked as really being a constructor,
585 then treat this as a ref. */
586 if (sp->flags & SYM_CONSTRUCTOR)
587 {
588 /* Turn this into a ref */
589 refize (sp, nlist_p);
590 }
591 else
592 {
593 /* treat like a common */
594 if (sp->srefs_chain)
595 undefined_global_sym_count--;
596
597 commons_pending++;
598 sp->scoms_chain = nlist_p;
599 }
600 }
601 }
602 else if (sym->section != &bfd_und_section)
603 {
604 /* This is the definition of a symbol, add to def chain */
605 if (sp->sdefs_chain && (*(sp->sdefs_chain))->section != sym->section)
606 {
607 multiple_warn("%X%C: multiple definition of `%s'\n",
608 sym,
609 "%X%C: first defined here\n",
610 *(sp->sdefs_chain));
611 multiple_def_count++;
612 }
613 else
614 {
615 sym->udata = (PTR) (sp->sdefs_chain);
616 sp->sdefs_chain = nlist_p;
617 }
618
619 /* A definition overrides a common symbol */
620 if (sp->scoms_chain != NULL)
621 {
622 if (config.warn_common)
623 multiple_warn("%C: warning: definition of `%s' overriding common\n",
624 sym,
625 "%C: warning: common is here\n",
626 *(sp->scoms_chain));
627 refize (sp, sp->scoms_chain);
628 sp->scoms_chain = 0;
629 commons_pending--;
630 }
631 else if (sp->srefs_chain && relaxing == false)
632 {
633 /* If previously was undefined, then remember as defined */
634 undefined_global_sym_count--;
635 }
636 }
637 else
638 {
639 if (sp->scoms_chain == (asymbol **) NULL
640 && sp->srefs_chain == (asymbol **) NULL
641 && sp->sdefs_chain == (asymbol **) NULL)
642 {
643 /* And it's the first time we've seen it */
644 undefined_global_sym_count++;
645 }
646
647 refize (sp, nlist_p);
648 }
649 }
650
651 ASSERT (sp->sdefs_chain == 0 || sp->scoms_chain == 0);
652 ASSERT (sp->scoms_chain == 0 || (*(sp->scoms_chain))->udata == 0);
653 }
654
655 static void
656 enter_file_symbols (entry)
657 lang_input_statement_type *entry;
658 {
659 asymbol **q;
660
661 entry->common_section =
662 bfd_make_section_old_way (entry->the_bfd, "COMMON");
663 entry->common_section->flags = SEC_NEVER_LOAD;
664 ldlang_add_file (entry);
665
666
667 if (trace_files || trace_file_tries)
668 {
669 info_msg ("%I\n", entry);
670 }
671
672 total_symbols_seen += entry->symbol_count;
673 total_files_seen++;
674 if (entry->symbol_count)
675 {
676 for (q = entry->asymbols; *q; q++)
677 {
678 asymbol *p = *q;
679
680 if (had_y && p->name)
681 {
682 /* look up the symbol anyway to see if the trace bit was
683 set */
684 ldsym_type *s = ldsym_get (p->name);
685 if (s->flags & SYM_Y)
686 {
687 einfo ("%B: %s %T\n", entry->the_bfd,
688 p->section == &bfd_und_section ? "reference to" : "definition of ",
689 p);
690 }
691 }
692
693 if (p->section == &bfd_ind_section
694 || (p->flags & BSF_INDIRECT) != 0)
695 {
696 add_indirect (q);
697 }
698 else if (p->flags & BSF_WARNING)
699 {
700 add_warning (p);
701 }
702 else if (bfd_is_com_section (p->section))
703 {
704 enter_global_ref (q, p->name);
705
706 /* If this is not bfd_com_section, make sure we have a
707 section of this name in the bfd. We need this
708 because some targets that use multiple common
709 sections do not actually put the common section in
710 the BFD, but we need it there so that a wildcard
711 specification in the linker script (e.g.,
712 *(.scommon)) will find the section and attach it to
713 the right output section. When an section is chosed
714 for the common symbols (in lang_common) that section
715 must have been correctly given an output section.
716 For normal common symbols we just use
717 entry->common_section, initialized above. */
718 if (p->section != &bfd_com_section
719 && p->section->owner != entry->the_bfd)
720 bfd_make_section (entry->the_bfd,
721 bfd_get_section_name (p->section->owner,
722 p->section));
723 }
724 else if (p->section == &bfd_und_section
725 || (p->flags & BSF_GLOBAL)
726 || (p->flags & BSF_CONSTRUCTOR)
727 || (p->flags & BSF_WEAK))
728 {
729 enter_global_ref (q, p->name);
730 }
731 }
732 }
733 }
734
735
736 /* Searching libraries */
737
738 struct lang_input_statement_struct *decode_library_subfile ();
739 void linear_library (), symdef_library ();
740
741 /* Search the library ENTRY, already open on descriptor DESC.
742 This means deciding which library members to load,
743 making a chain of `struct lang_input_statement_struct' for those members,
744 and entering their global symbols in the hash table. */
745
746 void
747 search_library (entry)
748 struct lang_input_statement_struct *entry;
749 {
750
751 /* No need to load a library if no undefined symbols */
752 if (!undefined_global_sym_count)
753 return;
754
755 if (bfd_has_map (entry->the_bfd))
756 symdef_library (entry);
757 else
758 linear_library (entry);
759
760 }
761
762 #ifdef GNU960
763 static
764 boolean
765 gnu960_check_format (abfd, format)
766 bfd *abfd;
767 bfd_format format;
768 {
769 boolean retval;
770
771 if ((bfd_check_format (abfd, format) == true)
772 && (abfd->xvec->flavour == output_flavor))
773 {
774 return true;
775 }
776
777
778 return false;
779 }
780
781 #endif
782
783 void
784 ldmain_open_file_read_symbol (entry)
785 struct lang_input_statement_struct *entry;
786 {
787 if (entry->asymbols == (asymbol **) NULL
788 && entry->real == true
789 && entry->filename != (char *) NULL)
790 {
791 ldfile_open_file (entry);
792
793
794 #ifdef GNU960
795 if (gnu960_check_format (entry->the_bfd, bfd_object))
796 #else
797 if (bfd_check_format (entry->the_bfd, bfd_object))
798 #endif
799 {
800 entry->the_bfd->usrdata = (PTR) entry;
801
802
803 read_entry_symbols (entry->the_bfd, entry);
804
805 /* look through the sections in the file and see if any of them
806 are constructors */
807 ldlang_check_for_constructors (entry);
808
809 enter_file_symbols (entry);
810 }
811 #ifdef GNU960
812 else if (gnu960_check_format (entry->the_bfd, bfd_archive))
813 #else
814 else if (bfd_check_format (entry->the_bfd, bfd_archive))
815 #endif
816 {
817 entry->the_bfd->usrdata = (PTR) entry;
818
819 entry->subfiles = (lang_input_statement_type *) NULL;
820 search_library (entry);
821 }
822 else
823 {
824 einfo ("%F%B: malformed input file (not rel or archive) \n",
825 entry->the_bfd);
826 }
827 }
828
829 }
830
831 /* Construct and return a lang_input_statement_struct for a library member.
832 The library's lang_input_statement_struct is library_entry,
833 and the library is open on DESC.
834 SUBFILE_OFFSET is the byte index in the library of this member's header.
835 We store the length of the member into *LENGTH_LOC. */
836
837 lang_input_statement_type *
838 decode_library_subfile (library_entry, subfile_offset)
839 struct lang_input_statement_struct *library_entry;
840 bfd *subfile_offset;
841 {
842 register struct lang_input_statement_struct *subentry;
843
844
845 /* First, check if we already have a loaded
846 lang_input_statement_struct for this library subfile. If so,
847 just return it. Otherwise, allocate some space and build a new one. */
848
849 if (subfile_offset->usrdata
850 && ((struct lang_input_statement_struct *) subfile_offset->usrdata)->
851 loaded == true)
852 {
853 subentry = (struct lang_input_statement_struct *) subfile_offset->usrdata;
854 }
855 else
856 {
857 subentry =
858 (struct lang_input_statement_struct *)
859 ldmalloc ((bfd_size_type) (sizeof (struct lang_input_statement_struct)));
860
861 subentry->filename = subfile_offset->filename;
862 subentry->local_sym_name = subfile_offset->filename;
863 subentry->asymbols = 0;
864 subentry->the_bfd = subfile_offset;
865 subentry->subfiles = 0;
866 subentry->next = 0;
867 subentry->superfile = library_entry;
868 subentry->is_archive = false;
869
870 subentry->just_syms_flag = false;
871 subentry->loaded = false;
872 subentry->chain = 0;
873 }
874 return subentry;
875 }
876
877 boolean subfile_wanted_p ();
878 void
879 clear_syms (entry, offset)
880 struct lang_input_statement_struct *entry;
881 file_ptr offset;
882 {
883 carsym *car;
884 unsigned long indx = bfd_get_next_mapent (entry->the_bfd,
885 BFD_NO_MORE_SYMBOLS,
886 &car);
887
888 while (indx != BFD_NO_MORE_SYMBOLS)
889 {
890 if (car->file_offset == offset)
891 {
892 car->name = 0;
893 }
894 indx = bfd_get_next_mapent (entry->the_bfd, indx, &car);
895 }
896
897 }
898
899 /* Search a library that has a map
900 */
901 void
902 symdef_library (entry)
903 struct lang_input_statement_struct *entry;
904
905 {
906 register struct lang_input_statement_struct *prev = 0;
907
908 boolean not_finished = true;
909
910 while (not_finished == true)
911 {
912 carsym *exported_library_name;
913 bfd *prev_archive_member_bfd = 0;
914
915 int idx = bfd_get_next_mapent (entry->the_bfd,
916 BFD_NO_MORE_SYMBOLS,
917 &exported_library_name);
918
919 not_finished = false;
920
921 while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count)
922 {
923
924 if (exported_library_name->name)
925 {
926
927 ldsym_type *sp = ldsym_get_soft (exported_library_name->name);
928
929 /* If we find a symbol that appears to be needed, think carefully
930 about the archive member that the symbol is in. */
931 /* So - if it exists, and is referenced somewhere and is
932 undefined or */
933 if (sp && sp->srefs_chain && !sp->sdefs_chain)
934 {
935 bfd *archive_member_bfd = bfd_get_elt_at_index (entry->the_bfd, idx);
936 struct lang_input_statement_struct *archive_member_lang_input_statement_struct;
937
938 #ifdef GNU960
939 if (archive_member_bfd && gnu960_check_format (archive_member_bfd, bfd_object))
940 #else
941 if (archive_member_bfd && bfd_check_format (archive_member_bfd, bfd_object))
942 #endif
943 {
944
945 /* Don't think carefully about any archive member
946 more than once in a given pass. */
947 if (prev_archive_member_bfd != archive_member_bfd)
948 {
949
950 prev_archive_member_bfd = archive_member_bfd;
951
952 /* Read the symbol table of the archive member. */
953
954 if (archive_member_bfd->usrdata != (PTR) NULL)
955 {
956
957 archive_member_lang_input_statement_struct = (lang_input_statement_type *) archive_member_bfd->usrdata;
958 }
959 else
960 {
961
962 archive_member_lang_input_statement_struct =
963 decode_library_subfile (entry, archive_member_bfd);
964 archive_member_bfd->usrdata = (PTR) archive_member_lang_input_statement_struct;
965
966 }
967
968 if (archive_member_lang_input_statement_struct == 0)
969 {
970 einfo ("%F%I contains invalid archive member %s\n",
971 entry, sp->name);
972 }
973
974 if (archive_member_lang_input_statement_struct->loaded == false)
975 {
976
977 read_entry_symbols (archive_member_bfd, archive_member_lang_input_statement_struct);
978 /* Now scan the symbol table and decide whether to load. */
979
980
981 if (subfile_wanted_p (archive_member_lang_input_statement_struct) == true)
982
983 {
984 /* This member is needed; load it.
985 Since we are loading something on this pass,
986 we must make another pass through the symdef data. */
987
988 not_finished = true;
989
990 enter_file_symbols (archive_member_lang_input_statement_struct);
991
992 if (prev)
993 prev->chain = archive_member_lang_input_statement_struct;
994 else
995 entry->subfiles = archive_member_lang_input_statement_struct;
996
997
998 prev = archive_member_lang_input_statement_struct;
999
1000
1001 /* Clear out this member's symbols from the symdef data
1002 so that following passes won't waste time on them. */
1003 clear_syms (entry, exported_library_name->file_offset);
1004 archive_member_lang_input_statement_struct->loaded = true;
1005 }
1006 }
1007 }
1008 }
1009 }
1010 }
1011 idx = bfd_get_next_mapent (entry->the_bfd, idx, &exported_library_name);
1012 }
1013 }
1014 }
1015
1016 void
1017 linear_library (entry)
1018 struct lang_input_statement_struct *entry;
1019 {
1020 boolean more_to_do = true;
1021 register struct lang_input_statement_struct *prev = 0;
1022
1023 if (entry->complained == false)
1024 {
1025 if (entry->the_bfd->xvec->flavour != bfd_target_ieee_flavour)
1026
1027 {
1028 /* IEEE can use table of contents, so this message is bogus */
1029 einfo ("%P: library %s has bad table of contents, rerun ranlib\n",
1030 entry->the_bfd->filename);
1031 }
1032 entry->complained = true;
1033
1034 }
1035 while (more_to_do)
1036 {
1037
1038 bfd *archive = bfd_openr_next_archived_file (entry->the_bfd, 0);
1039
1040 more_to_do = false;
1041 while (archive)
1042 {
1043 /* Don't check this file if it's already been read in
1044 once */
1045
1046 if (!archive->usrdata ||
1047 !((lang_input_statement_type *) (archive->usrdata))->loaded)
1048 {
1049 #ifdef GNU960
1050 if (gnu960_check_format (archive, bfd_object))
1051 #else
1052 if (bfd_check_format (archive, bfd_object))
1053 #endif
1054 {
1055 register struct lang_input_statement_struct *subentry;
1056
1057 subentry = decode_library_subfile (entry,
1058 archive);
1059
1060 archive->usrdata = (PTR) subentry;
1061 if (!subentry)
1062 return;
1063 if (subentry->loaded == false)
1064 {
1065 read_entry_symbols (archive, subentry);
1066
1067 if (subfile_wanted_p (subentry) == true)
1068 {
1069 enter_file_symbols (subentry);
1070
1071 if (prev)
1072 prev->chain = subentry;
1073 else
1074 entry->subfiles = subentry;
1075 prev = subentry;
1076
1077 more_to_do = true;
1078 subentry->loaded = true;
1079 }
1080 }
1081 }
1082 }
1083 archive = bfd_openr_next_archived_file (entry->the_bfd, archive);
1084
1085 }
1086
1087 }
1088 }
1089
1090 /* ENTRY is an entry for a file inside an archive
1091 Its symbols have been read into core, but not entered into the
1092 linker ymbol table
1093 Return nonzero if we ought to load this file */
1094
1095 boolean
1096 subfile_wanted_p (entry)
1097 struct lang_input_statement_struct *entry;
1098 {
1099 asymbol **q;
1100
1101 if (entry->symbol_count == 0)
1102 return false;
1103
1104 for (q = entry->asymbols; *q; q++)
1105 {
1106 asymbol *p = *q;
1107
1108 /* If the symbol has an interesting definition, we could
1109 potentially want it. */
1110
1111 if (p->flags & BSF_INDIRECT)
1112 {
1113 /** add_indirect(q);*/
1114 }
1115
1116 if (bfd_is_com_section (p->section)
1117 || (p->flags & BSF_GLOBAL)
1118 || (p->flags & BSF_INDIRECT)
1119 || (p->flags & BSF_WEAK))
1120 {
1121 register ldsym_type *sp = ldsym_get_soft (p->name);
1122
1123 /* If this symbol has not been hashed,
1124 we can't be looking for it. */
1125 if (sp != (ldsym_type *) NULL
1126 && sp->sdefs_chain == (asymbol **) NULL)
1127 {
1128 int check;
1129
1130 /* A weak symbol is not considered to be a reference
1131 when pulling files out of an archive. An unresolved
1132 weak symbol winds up with a value of zero. See the
1133 SVR4 ABI, p. 4-27. */
1134 if (sp->scoms_chain != (asymbol **) NULL)
1135 check = 1;
1136 else if (sp->srefs_chain == (asymbol **) NULL)
1137 check = 0;
1138 else
1139 {
1140 asymbol **ptr;
1141
1142 check = 0;
1143 for (ptr = sp->srefs_chain;
1144 ptr != (asymbol **) NULL;
1145 ptr = (asymbol **) ((*ptr)->udata))
1146 {
1147 if (((*ptr)->flags & BSF_WEAK) == 0)
1148 {
1149 check = 1;
1150 break;
1151 }
1152 }
1153 }
1154
1155 if (check)
1156 {
1157 /* This is a symbol we are looking for. It is
1158 either common or not yet defined. If this is a
1159 common symbol, then if the symbol in the object
1160 file is common, we need to combine sizes. But if
1161 we already have a common symbol, and the symbol
1162 in the object file is not common, we don't want
1163 the object file: it is providing a definition for
1164 a symbol that we already have a definition for
1165 (this is the else condition below). */
1166 if (bfd_is_com_section (p->section))
1167 {
1168
1169 /* If the symbol in the table is a constructor, we won't to
1170 anything fancy with it */
1171 if ((sp->flags & SYM_CONSTRUCTOR) == 0)
1172 {
1173 /* This libary member has something to
1174 say about this element. We should
1175 remember if its a new size */
1176 /* Move something from the ref list to the com list */
1177 if (sp->scoms_chain)
1178 {
1179 /* Already a common symbol, maybe update it */
1180 if (p->value > (*(sp->scoms_chain))->value)
1181 {
1182 (*(sp->scoms_chain))->value = p->value;
1183 }
1184 }
1185 else
1186 {
1187 /* Take a value from the ref chain
1188 Here we are moving a symbol from the owning bfd
1189 to another bfd. We must set up the
1190 common_section portion of the bfd thing */
1191
1192
1193
1194 sp->scoms_chain = sp->srefs_chain;
1195 sp->srefs_chain =
1196 (asymbol **) ((*(sp->srefs_chain))->udata);
1197 (*(sp->scoms_chain))->udata = (PTR) NULL;
1198
1199 (*(sp->scoms_chain))->section = p->section;
1200 (*(sp->scoms_chain))->flags = 0;
1201 /* Remember the size of this item */
1202 sp->scoms_chain[0]->value = p->value;
1203 commons_pending++;
1204 undefined_global_sym_count--;
1205 }
1206 {
1207 asymbol *com = *(sp->scoms_chain);
1208
1209 if (((lang_input_statement_type *)
1210 (bfd_asymbol_bfd (com)->usrdata))->common_section ==
1211 (asection *) NULL)
1212 {
1213 ((lang_input_statement_type *)
1214 (bfd_asymbol_bfd (com)->usrdata))->common_section =
1215 bfd_make_section_old_way (bfd_asymbol_bfd (com), "COMMON");
1216 }
1217 }
1218 }
1219 ASSERT (p->udata == 0);
1220 }
1221 else if (sp->scoms_chain == (asymbol **) NULL)
1222 {
1223 if (write_map)
1224 {
1225 info_msg ("%I needed due to %s\n", entry, sp->name);
1226 }
1227 return true;
1228 }
1229 }
1230 }
1231 }
1232 }
1233
1234 return false;
1235 }
1236
1237 void
1238 add_ysym (text)
1239 char *text;
1240 {
1241 ldsym_type *lookup = ldsym_get (text);
1242 lookup->flags |= SYM_Y;
1243 had_y = 1;
1244 }
This page took 0.061511 seconds and 4 git commands to generate.