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