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