* ldlang.c (lang_process): Ifdef out final call to
[deliverable/binutils-gdb.git] / ld / ldmain.c
CommitLineData
2fa0b342 1/* Copyright (C) 1991 Free Software Foundation, Inc.
ce4d59e2 2 Written by Steve Chamberlain steve@cygnus.com
e47bfa63 3
2fa0b342
DHW
4This file is part of GLD, the Gnu Linker.
5
6GLD is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
ce4d59e2 8the Free Software Foundation; either version 2, or (at your option)
2fa0b342
DHW
9any later version.
10
11GLD is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GLD; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
2fa0b342 20
2fa0b342 21#include "bfd.h"
f177a611 22#include "sysdep.h"
2fa0b342
DHW
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"
8c514453 32#include "ldemul.h"
2fa0b342
DHW
33#include "ldlex.h"
34#include "ldfile.h"
c611e285
SC
35#include "ldindr.h"
36#include "ldwarn.h"
37#include "ldctor.h"
9d1fe8a4
SC
38#include "lderror.h"
39
922018a1 40/* Somewhere above, sys/stat.h got included . . . . */
d723cd17
DM
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
47static char *get_emulation ();
48static void set_scripts_dir ();
49
2fa0b342
DHW
50/* IMPORTS */
51extern boolean lang_has_input_file;
bbd2521f
DM
52extern boolean force_make_executable;
53extern boolean relaxing;
54extern boolean had_script;
e47bfa63 55
2fa0b342
DHW
56/* EXPORTS */
57
58char *default_target;
59char *output_filename = "a.out";
e47bfa63 60
2fa0b342
DHW
61/* Name this program was invoked by. */
62char *program_name;
63
64/* The file that we're creating */
b6316534 65bfd *output_bfd = 0;
2fa0b342 66
8a045e50
ILT
67/* set if -y on the command line */
68int had_y;
69
2fa0b342
DHW
70/* The local symbol prefix */
71char lprefix = 'L';
72
173a0c3d
DM
73/* Set by -G argument, for MIPS ECOFF target. */
74int g_switch_value = 8;
75
2fa0b342
DHW
76/* Count the number of global symbols multiply defined. */
77int multiple_def_count;
78
2fa0b342
DHW
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
3e4c643d 87 all ldsym_types with a ->scoms field */
2fa0b342 88
3e4c643d 89unsigned int commons_pending;
2fa0b342 90
e47bfa63 91/* Count the number of global symbols referenced and not defined.
3e4c643d 92 common symbols are not included in this count. */
2fa0b342
DHW
93
94unsigned int undefined_global_sym_count;
95
2fa0b342
DHW
96/* Nonzero means print names of input files as processed. */
97boolean trace_files;
98
bbd2521f
DM
99/* Nonzero means same, but note open failures, too. */
100boolean trace_file_tries;
101
2fa0b342
DHW
102/* 1 => write load map. */
103boolean write_map;
104
81016051
SC
105#ifdef GNU960
106/* Indicates whether output file will be b.out (default) or coff */
f177a611 107enum target_flavour output_flavor = BFD_BOUT_FORMAT;
81016051 108#endif
2fa0b342 109
2fa0b342
DHW
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
2fa0b342
DHW
113unsigned 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 */
118unsigned int total_files_seen;
119
2fa0b342 120args_type command_line;
173a0c3d 121
2fa0b342 122ld_config_type config;
173a0c3d 123\f
9d1fe8a4 124void
2fa0b342
DHW
125main (argc, argv)
126 char **argv;
127 int argc;
128{
129 char *emulation;
e47bfa63 130
2fa0b342 131 program_name = argv[0];
99fe4553 132
e47bfa63 133 bfd_init ();
bfbdc80f 134
2fa0b342 135 /* Initialize the data about options. */
76855794 136
9d1fe8a4 137
bbd2521f 138 trace_files = trace_file_tries = false;
2fa0b342
DHW
139 write_map = false;
140 config.relocateable_output = false;
2fa0b342
DHW
141 command_line.force_common_definition = false;
142
e47bfa63
SC
143 init_bfd_error_vector ();
144 ldsym_init ();
145 ldfile_add_arch ("");
a72f4e5f 146
2fa0b342
DHW
147 config.make_executable = true;
148 force_make_executable = false;
149
2fa0b342
DHW
150 /* Initialize the cumulative counts of symbols. */
151 undefined_global_sym_count = 0;
2fa0b342
DHW
152 multiple_def_count = 0;
153 commons_pending = 0;
154
ce4d59e2
SC
155 config.magic_demand_paged = true;
156 config.text_read_only = true;
2fa0b342 157 config.make_executable = true;
1418c83b 158
d723cd17 159 emulation = get_emulation (argc, argv);
e47bfa63
SC
160 ldemul_choose_mode (emulation);
161 default_target = ldemul_choose_target ();
162 lang_init ();
163 ldemul_before_parse ();
2fa0b342 164 lang_has_input_file = false;
e47bfa63 165 parse_args (argc, argv);
f3739bc3 166
973e421e
ILT
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
bbd2521f
DM
171 if (had_script == false)
172 {
173 /* Read the emulation's appropriate default script. */
2a28d8b0
DM
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);
bbd2521f
DM
188 }
189
29f33467
SC
190 if (config.relocateable_output && command_line.relax)
191 {
f3739bc3 192 einfo ("%P%F: -relax and -r may not be used together\n");
29f33467 193 }
e47bfa63 194 lang_final ();
9d1fe8a4 195
e47bfa63
SC
196 if (lang_has_input_file == false)
197 {
973e421e 198 einfo ("%P%F: no input files\n");
870f54b2 199 }
2fa0b342 200
bbd2521f
DM
201 if (trace_files)
202 {
973e421e 203 info_msg ("%P: mode %s\n", emulation);
bbd2521f
DM
204 }
205
e47bfa63 206 ldemul_after_parse ();
870f54b2 207
9d1fe8a4 208
e47bfa63 209 if (config.map_filename)
870f54b2 210 {
e47bfa63 211 if (strcmp (config.map_filename, "-") == 0)
2e2bf962 212 {
e47bfa63 213 config.map_file = stdout;
2e2bf962 214 }
e47bfa63
SC
215 else
216 {
217 config.map_file = fopen (config.map_filename, FOPEN_WT);
218 if (config.map_file == (FILE *) NULL)
219 {
bbd2521f 220 einfo ("%P%F: cannot open map file %s: %E\n",
e47bfa63
SC
221 config.map_filename);
222 }
223 }
224 }
870f54b2 225
2e2bf962 226
e47bfa63 227 lang_process ();
2fa0b342 228
2fa0b342
DHW
229 /* Print error messages for any missing symbols, for any warning
230 symbols, and possibly multiple definitions */
231
2fa0b342 232
e47bfa63
SC
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 }
3e4c643d 242 }
2fa0b342 243
e47bfa63 244 if (config.relocateable_output)
f3739bc3 245 output_bfd->flags &= ~EXEC_P;
a72f4e5f 246 else
f3739bc3 247 output_bfd->flags |= EXEC_P;
b257477f 248
f3739bc3 249 ldwrite ();
b257477f 250
f3739bc3
SC
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?) */
a72f4e5f 254
f3739bc3
SC
255 if (config.make_executable == false && force_make_executable == false)
256 {
257 if (trace_files == true)
e47bfa63 258 {
973e421e 259 einfo ("%P: link errors found, deleting executable `%s'\n",
f3739bc3 260 output_filename);
e47bfa63 261 }
a72f4e5f 262
f3739bc3
SC
263 if (output_bfd->iostream)
264 fclose ((FILE *) (output_bfd->iostream));
a72f4e5f 265
f3739bc3
SC
266 unlink (output_filename);
267 exit (1);
268 }
269 else
270 {
271 bfd_close (output_bfd);
272 }
2fa0b342 273
e47bfa63 274 exit (0);
d723cd17
DM
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
280static char *
281get_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 {
973e421e 320 einfo("%P%F: missing argument to -m\n");
d723cd17
DM
321 }
322 }
973e421e
ILT
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 }
d723cd17
DM
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
350static boolean
351check_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);
f4208462 365 free (buf);
d723cd17 366 if (res)
f4208462 367 ldfile_add_library_path (dir);
d723cd17
DM
368 return res;
369}
370
371/* Set the default directory for finding script files.
f4208462
DM
372 Libraries will be searched for here too, but that's ok.
373 We look for the "ldscripts" directory in:
374
f4208462 375 SCRIPTDIR (passed from Makefile)
bbd2521f
DM
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) */
d723cd17
DM
378
379static void
380set_scripts_dir ()
381{
f4208462
DM
382 char *end, *dir;
383 size_t dirlen;
384
d723cd17 385 if (check_for_scripts_dir (SCRIPTDIR))
f4208462 386 return; /* We've been installed normally. */
d723cd17
DM
387
388 /* Look for "ldscripts" in the dir where our binary is. */
389 end = strrchr (program_name, '/');
bbd2521f
DM
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 }
f4208462 405
f4208462
DM
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. */
d723cd17 415}
2fa0b342 416
2fa0b342 417void
bbd2521f 418read_entry_symbols (desc, entry)
2fa0b342
DHW
419 bfd *desc;
420 struct lang_input_statement_struct *entry;
421{
e47bfa63
SC
422 if (entry->asymbols == (asymbol **) NULL)
423 {
424 bfd_size_type table_size = get_symtab_upper_bound (desc);
2fa0b342 425
e47bfa63
SC
426 entry->asymbols = (asymbol **) ldmalloc (table_size);
427 entry->symbol_count = bfd_canonicalize_symtab (desc, entry->asymbols);
428 }
429}
2fa0b342
DHW
430
431/*
e47bfa63 432 * turn this item into a reference
2fa0b342 433 */
e47bfa63
SC
434void
435refize (sp, nlist_p)
436 ldsym_type *sp;
437 asymbol **nlist_p;
2fa0b342
DHW
438{
439 asymbol *sym = *nlist_p;
e47bfa63 440
2fa0b342 441 sym->value = 0;
973e421e
ILT
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
c611e285 447 sym->section = &bfd_und_section;
e47bfa63 448 sym->udata = (PTR) (sp->srefs_chain);
2fa0b342
DHW
449 sp->srefs_chain = nlist_p;
450}
e47bfa63 451
2fa0b342
DHW
452/*
453This function is called for each name which is seen which has a global
454scope. It enters the name into the global symbol table in the correct
455symbol on the correct chain. Remember that each ldsym_type has three
456chains attatched, one of all definitions of a symbol, one of all
457references of a symbol and one of all common definitions of a symbol.
458
459When the function is over, the supplied is left connected to the bfd
460to which is was born, with its udata field pointing to the next member
461on the chain in which it has been inserted.
462
463A certain amount of jigery pokery is necessary since commons come
464along and upset things, we only keep one item in the common chain; the
465one with the biggest size seen sofar. When another common comes along
466it either bumps the previous definition into the ref chain, since it
467is bigger, or gets turned into a ref on the spot since the one on the
468common chain is already bigger. If a real definition comes along then
469the common gets bumped off anyway.
470
471Whilst all this is going on we keep a count of the number of multiple
472definitions seen, undefined global symbols and pending commons.
473*/
474
2fa0b342 475void
bbd2521f 476enter_global_ref (nlist_p, name)
173a0c3d
DM
477 asymbol ** nlist_p; /* pointer into symbol table from incoming bfd */
478 CONST char *name; /* name of symbol in linker table */
2fa0b342
DHW
479{
480 asymbol *sym = *nlist_p;
e47bfa63 481 ldsym_type *sp;
b257477f
SC
482
483 /* Lookup the name from the incoming bfd's symbol table in the
484 linker's global symbol table */
2fa0b342 485
2fa0b342 486
b257477f 487 flagword this_symbol_flags = sym->flags;
e47bfa63 488
b257477f 489 sp = ldsym_get (name);
8a045e50
ILT
490
491
29f33467 492 /* If this symbol already has udata, it means that something strange
be1627d3 493 has happened.
29f33467 494
be1627d3
SC
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 */
2fa0b342 500
be1627d3
SC
501
502 if (sym->udata)
29f33467 503 return;
2fa0b342 504
3e4c643d 505
973e421e 506 if (this_symbol_flags & BSF_CONSTRUCTOR)
e47bfa63 507 {
29f33467
SC
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 }
29f33467 516 }
973e421e
ILT
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 }
e47bfa63
SC
526 else
527 {
29f33467
SC
528 if (bfd_is_com_section (sym->section))
529 {
530 /* If we have a definition of this symbol already then
2a28d8b0
DM
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. */
29f33467
SC
535 if (sp->sdefs_chain)
536 {
537 /* This is a common symbol, but we already have a definition
2a28d8b0
DM
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));
29f33467
SC
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 */
2a28d8b0
DM
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));
29f33467
SC
558 refize (sp, nlist_p);
559 }
560 else if (sp->scoms_chain != nlist_p)
561 {
562 /* other common is smaller, throw that away */
2a28d8b0
DM
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 }
29f33467
SC
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
2a28d8b0
DM
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. */
29f33467
SC
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--;
be1627d3 596
29f33467
SC
597 commons_pending++;
598 sp->scoms_chain = nlist_p;
599 }
600 }
be1627d3 601 }
29f33467
SC
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 {
2a28d8b0
DM
607 multiple_warn("%X%C: multiple definition of `%s'\n",
608 sym,
609 "%X%C: first defined here\n",
610 *(sp->sdefs_chain));
29f33467 611 multiple_def_count++;
29f33467
SC
612 }
613 else
614 {
615 sym->udata = (PTR) (sp->sdefs_chain);
616 sp->sdefs_chain = nlist_p;
617 }
973e421e 618
29f33467 619 /* A definition overrides a common symbol */
973e421e 620 if (sp->scoms_chain != NULL)
29f33467 621 {
2a28d8b0
DM
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));
29f33467
SC
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 }
be1627d3 637 else
29f33467
SC
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++;
29f33467 645 }
be1627d3 646
29f33467
SC
647 refize (sp, nlist_p);
648 }
be1627d3 649 }
be1627d3 650
e47bfa63
SC
651 ASSERT (sp->sdefs_chain == 0 || sp->scoms_chain == 0);
652 ASSERT (sp->scoms_chain == 0 || (*(sp->scoms_chain))->udata == 0);
2fa0b342
DHW
653}
654
655static void
bbd2521f 656enter_file_symbols (entry)
e47bfa63 657 lang_input_statement_type *entry;
2fa0b342 658{
e47bfa63 659 asymbol **q;
c611e285 660
2fa0b342 661 entry->common_section =
29f33467
SC
662 bfd_make_section_old_way (entry->the_bfd, "COMMON");
663 entry->common_section->flags = SEC_NEVER_LOAD;
e47bfa63 664 ldlang_add_file (entry);
2fa0b342 665
fe563ffe 666
bbd2521f 667 if (trace_files || trace_file_tries)
29f33467 668 {
973e421e 669 info_msg ("%I\n", entry);
29f33467 670 }
e47bfa63
SC
671
672 total_symbols_seen += entry->symbol_count;
673 total_files_seen++;
674 if (entry->symbol_count)
fe563ffe 675 {
29f33467
SC
676 for (q = entry->asymbols; *q; q++)
677 {
678 asymbol *p = *q;
8a045e50 679
29f33467
SC
680 if (had_y && p->name)
681 {
682 /* look up the symbol anyway to see if the trace bit was
973e421e 683 set */
29f33467
SC
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 }
81016051 692
973e421e
ILT
693 if (p->section == &bfd_ind_section
694 || (p->flags & BSF_INDIRECT) != 0)
29f33467
SC
695 {
696 add_indirect (q);
697 }
698 else if (p->flags & BSF_WARNING)
699 {
700 add_warning (p);
701 }
973e421e
ILT
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 }
29f33467
SC
724 else if (p->section == &bfd_und_section
725 || (p->flags & BSF_GLOBAL)
973e421e
ILT
726 || (p->flags & BSF_CONSTRUCTOR)
727 || (p->flags & BSF_WEAK))
29f33467 728 {
973e421e 729 enter_global_ref (q, p->name);
29f33467 730 }
29f33467 731 }
e47bfa63
SC
732 }
733}
2fa0b342 734
8a045e50 735
2fa0b342
DHW
736/* Searching libraries */
737
738struct lang_input_statement_struct *decode_library_subfile ();
739void 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
746void
747search_library (entry)
748 struct lang_input_statement_struct *entry;
749{
750
751 /* No need to load a library if no undefined symbols */
e47bfa63
SC
752 if (!undefined_global_sym_count)
753 return;
2fa0b342 754
e47bfa63 755 if (bfd_has_map (entry->the_bfd))
2fa0b342
DHW
756 symdef_library (entry);
757 else
758 linear_library (entry);
759
760}
761
99fe4553
SC
762#ifdef GNU960
763static
29f33467 764 boolean
99fe4553 765gnu960_check_format (abfd, format)
e47bfa63
SC
766 bfd *abfd;
767 bfd_format format;
99fe4553
SC
768{
769 boolean retval;
770
e47bfa63
SC
771 if ((bfd_check_format (abfd, format) == true)
772 && (abfd->xvec->flavour == output_flavor))
773 {
774 return true;
775 }
81016051
SC
776
777
99fe4553
SC
778 return false;
779}
e47bfa63 780
99fe4553
SC
781#endif
782
2fa0b342 783void
1418c83b 784ldmain_open_file_read_symbol (entry)
e47bfa63 785 struct lang_input_statement_struct *entry;
2fa0b342 786{
e47bfa63
SC
787 if (entry->asymbols == (asymbol **) NULL
788 && entry->real == true
789 && entry->filename != (char *) NULL)
2fa0b342
DHW
790 {
791 ldfile_open_file (entry);
792
ce4d59e2 793
99fe4553 794#ifdef GNU960
e47bfa63 795 if (gnu960_check_format (entry->the_bfd, bfd_object))
99fe4553 796#else
e47bfa63 797 if (bfd_check_format (entry->the_bfd, bfd_object))
99fe4553 798#endif
2fa0b342 799 {
e47bfa63 800 entry->the_bfd->usrdata = (PTR) entry;
2fa0b342
DHW
801
802
bbd2521f 803 read_entry_symbols (entry->the_bfd, entry);
ce4d59e2
SC
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
bbd2521f 809 enter_file_symbols (entry);
2fa0b342 810 }
99fe4553 811#ifdef GNU960
e47bfa63 812 else if (gnu960_check_format (entry->the_bfd, bfd_archive))
99fe4553 813#else
e47bfa63 814 else if (bfd_check_format (entry->the_bfd, bfd_archive))
99fe4553 815#endif
2fa0b342 816 {
e47bfa63 817 entry->the_bfd->usrdata = (PTR) entry;
2fa0b342 818
e47bfa63 819 entry->subfiles = (lang_input_statement_type *) NULL;
2fa0b342
DHW
820 search_library (entry);
821 }
e47bfa63 822 else
2fa0b342 823 {
e47bfa63
SC
824 einfo ("%F%B: malformed input file (not rel or archive) \n",
825 entry->the_bfd);
2fa0b342
DHW
826 }
827 }
828
829}
830
2fa0b342
DHW
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
837lang_input_statement_type *
838decode_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;
e47bfa63 843
1418c83b 844
8a045e50
ILT
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. */
2fa0b342 848
29f33467
SC
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 }
2fa0b342
DHW
874 return subentry;
875}
876
e47bfa63 877boolean subfile_wanted_p ();
2fa0b342 878void
e47bfa63
SC
879clear_syms (entry, offset)
880 struct lang_input_statement_struct *entry;
881 file_ptr offset;
2fa0b342
DHW
882{
883 carsym *car;
e47bfa63
SC
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);
2fa0b342 895 }
2fa0b342
DHW
896
897}
898
899/* Search a library that has a map
900 */
901void
902symdef_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
2fa0b342 910 while (not_finished == true)
e47bfa63
SC
911 {
912 carsym *exported_library_name;
913 bfd *prev_archive_member_bfd = 0;
2fa0b342 914
e47bfa63
SC
915 int idx = bfd_get_next_mapent (entry->the_bfd,
916 BFD_NO_MORE_SYMBOLS,
917 &exported_library_name);
2fa0b342 918
e47bfa63 919 not_finished = false;
2fa0b342 920
e47bfa63
SC
921 while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count)
922 {
2fa0b342 923
e47bfa63
SC
924 if (exported_library_name->name)
925 {
2fa0b342 926
e47bfa63 927 ldsym_type *sp = ldsym_get_soft (exported_library_name->name);
3e4c643d 928
e47bfa63
SC
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;
2fa0b342 937
99fe4553 938#ifdef GNU960
e47bfa63 939 if (archive_member_bfd && gnu960_check_format (archive_member_bfd, bfd_object))
99fe4553 940#else
e47bfa63 941 if (archive_member_bfd && bfd_check_format (archive_member_bfd, bfd_object))
99fe4553 942#endif
e47bfa63 943 {
2fa0b342 944
e47bfa63
SC
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 {
2fa0b342 949
e47bfa63 950 prev_archive_member_bfd = archive_member_bfd;
2fa0b342 951
e47bfa63 952 /* Read the symbol table of the archive member. */
2fa0b342 953
e47bfa63
SC
954 if (archive_member_bfd->usrdata != (PTR) NULL)
955 {
2fa0b342 956
e47bfa63
SC
957 archive_member_lang_input_statement_struct = (lang_input_statement_type *) archive_member_bfd->usrdata;
958 }
959 else
960 {
2fa0b342 961
e47bfa63
SC
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;
2fa0b342 965
e47bfa63 966 }
2fa0b342 967
e47bfa63
SC
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 }
2fa0b342 973
e47bfa63
SC
974 if (archive_member_lang_input_statement_struct->loaded == false)
975 {
2fa0b342 976
bbd2521f 977 read_entry_symbols (archive_member_bfd, archive_member_lang_input_statement_struct);
e47bfa63 978 /* Now scan the symbol table and decide whether to load. */
2fa0b342
DHW
979
980
e47bfa63 981 if (subfile_wanted_p (archive_member_lang_input_statement_struct) == true)
2fa0b342 982
e47bfa63
SC
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. */
2fa0b342 987
e47bfa63 988 not_finished = true;
2fa0b342 989
bbd2521f 990 enter_file_symbols (archive_member_lang_input_statement_struct);
2fa0b342 991
e47bfa63
SC
992 if (prev)
993 prev->chain = archive_member_lang_input_statement_struct;
994 else
995 entry->subfiles = archive_member_lang_input_statement_struct;
2fa0b342
DHW
996
997
e47bfa63 998 prev = archive_member_lang_input_statement_struct;
2fa0b342
DHW
999
1000
e47bfa63
SC
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;
2fa0b342 1005 }
e47bfa63 1006 }
2fa0b342 1007 }
e47bfa63
SC
1008 }
1009 }
2fa0b342 1010 }
e47bfa63
SC
1011 idx = bfd_get_next_mapent (entry->the_bfd, idx, &exported_library_name);
1012 }
1013 }
2fa0b342
DHW
1014}
1015
1016void
1017linear_library (entry)
e47bfa63 1018 struct lang_input_statement_struct *entry;
2fa0b342
DHW
1019{
1020 boolean more_to_do = true;
1021 register struct lang_input_statement_struct *prev = 0;
1022
e47bfa63
SC
1023 if (entry->complained == false)
1024 {
29f33467
SC
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 }
e47bfa63 1032 entry->complained = true;
8a045e50 1033
e47bfa63
SC
1034 }
1035 while (more_to_do)
1036 {
2fa0b342 1037
e47bfa63 1038 bfd *archive = bfd_openr_next_archived_file (entry->the_bfd, 0);
2fa0b342 1039
e47bfa63
SC
1040 more_to_do = false;
1041 while (archive)
1042 {
f3739bc3
SC
1043 /* Don't check this file if it's already been read in
1044 once */
1045
1046 if (!archive->usrdata ||
29f33467
SC
1047 !((lang_input_statement_type *) (archive->usrdata))->loaded)
1048 {
99fe4553 1049#ifdef GNU960
29f33467 1050 if (gnu960_check_format (archive, bfd_object))
99fe4553 1051#else
29f33467 1052 if (bfd_check_format (archive, bfd_object))
99fe4553 1053#endif
29f33467
SC
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 {
bbd2521f 1065 read_entry_symbols (archive, subentry);
29f33467
SC
1066
1067 if (subfile_wanted_p (subentry) == true)
1068 {
bbd2521f 1069 enter_file_symbols (subentry);
29f33467
SC
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 }
e47bfa63 1083 archive = bfd_openr_next_archived_file (entry->the_bfd, archive);
2fa0b342 1084
2fa0b342 1085 }
2fa0b342 1086
e47bfa63 1087 }
2fa0b342
DHW
1088}
1089
29f33467 1090/* ENTRY is an entry for a file inside an archive
e47bfa63
SC
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 */
2fa0b342
DHW
1094
1095boolean
1096subfile_wanted_p (entry)
e47bfa63 1097 struct lang_input_statement_struct *entry;
2fa0b342
DHW
1098{
1099 asymbol **q;
1100
173a0c3d
DM
1101 if (entry->symbol_count == 0)
1102 return false;
1103
2fa0b342
DHW
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
e47bfa63
SC
1111 if (p->flags & BSF_INDIRECT)
1112 {
29f33467 1113 /** add_indirect(q);*/
e47bfa63 1114 }
3e4c643d 1115
8a045e50 1116 if (bfd_is_com_section (p->section)
9d1fe8a4 1117 || (p->flags & BSF_GLOBAL)
973e421e
ILT
1118 || (p->flags & BSF_INDIRECT)
1119 || (p->flags & BSF_WEAK))
2fa0b342
DHW
1120 {
1121 register ldsym_type *sp = ldsym_get_soft (p->name);
1122
2fa0b342
DHW
1123 /* If this symbol has not been hashed,
1124 we can't be looking for it. */
e47bfa63
SC
1125 if (sp != (ldsym_type *) NULL
1126 && sp->sdefs_chain == (asymbol **) NULL)
1127 {
973e421e
ILT
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)
e47bfa63 1156 {
f31cb329 1157 /* This is a symbol we are looking for. It is
973e421e 1158 either common or not yet defined. If this is a
f31cb329
ILT
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). */
8a045e50 1166 if (bfd_is_com_section (p->section))
e47bfa63
SC
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
29f33467 1199 (*(sp->scoms_chain))->section = p->section;
e47bfa63
SC
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 *)
29f33467 1210 (bfd_asymbol_bfd (com)->usrdata))->common_section ==
e47bfa63
SC
1211 (asection *) NULL)
1212 {
1213 ((lang_input_statement_type *)
29f33467
SC
1214 (bfd_asymbol_bfd (com)->usrdata))->common_section =
1215 bfd_make_section_old_way (bfd_asymbol_bfd (com), "COMMON");
e47bfa63
SC
1216 }
1217 }
1218 }
1219 ASSERT (p->udata == 0);
2fa0b342 1220 }
f31cb329 1221 else if (sp->scoms_chain == (asymbol **) NULL)
2fa0b342 1222 {
e47bfa63
SC
1223 if (write_map)
1224 {
973e421e 1225 info_msg ("%I needed due to %s\n", entry, sp->name);
e47bfa63
SC
1226 }
1227 return true;
2fa0b342 1228 }
2fa0b342 1229 }
e47bfa63 1230 }
2fa0b342
DHW
1231 }
1232 }
1233
1234 return false;
1235}
8a045e50
ILT
1236
1237void
29f33467
SC
1238add_ysym (text)
1239 char *text;
8a045e50 1240{
29f33467 1241 ldsym_type *lookup = ldsym_get (text);
8a045e50
ILT
1242 lookup->flags |= SYM_Y;
1243 had_y = 1;
1244}
This page took 0.156651 seconds and 4 git commands to generate.