New file used for rebuilding dependencies.
[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"
fcf276c4 26#include "ldsym.h"
2fa0b342
DHW
27#include "ldmain.h"
28#include "ldmisc.h"
29#include "ldwrite.h"
30#include "ldgram.h"
fcf276c4 31#include "ldexp.h"
2fa0b342 32#include "ldlang.h"
8c514453 33#include "ldemul.h"
2fa0b342
DHW
34#include "ldlex.h"
35#include "ldfile.h"
c611e285
SC
36#include "ldindr.h"
37#include "ldwarn.h"
38#include "ldctor.h"
9d1fe8a4
SC
39#include "lderror.h"
40
922018a1 41/* Somewhere above, sys/stat.h got included . . . . */
d723cd17
DM
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
fcf276c4
ILT
48static char *get_emulation PARAMS ((int, char **));
49static void set_scripts_dir PARAMS ((void));
e47bfa63 50
2fa0b342
DHW
51/* EXPORTS */
52
53char *default_target;
fcf276c4 54const char *output_filename = "a.out";
e47bfa63 55
2fa0b342
DHW
56/* Name this program was invoked by. */
57char *program_name;
58
59/* The file that we're creating */
b6316534 60bfd *output_bfd = 0;
2fa0b342 61
8a045e50
ILT
62/* set if -y on the command line */
63int had_y;
64
2fa0b342
DHW
65/* The local symbol prefix */
66char lprefix = 'L';
67
173a0c3d
DM
68/* Set by -G argument, for MIPS ECOFF target. */
69int g_switch_value = 8;
70
2fa0b342
DHW
71/* Count the number of global symbols multiply defined. */
72int multiple_def_count;
73
2fa0b342
DHW
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
3e4c643d 82 all ldsym_types with a ->scoms field */
2fa0b342 83
3e4c643d 84unsigned int commons_pending;
2fa0b342 85
e47bfa63 86/* Count the number of global symbols referenced and not defined.
3e4c643d 87 common symbols are not included in this count. */
2fa0b342
DHW
88
89unsigned int undefined_global_sym_count;
90
2fa0b342
DHW
91/* Nonzero means print names of input files as processed. */
92boolean trace_files;
93
bbd2521f
DM
94/* Nonzero means same, but note open failures, too. */
95boolean trace_file_tries;
96
2fa0b342
DHW
97/* 1 => write load map. */
98boolean write_map;
99
81016051
SC
100#ifdef GNU960
101/* Indicates whether output file will be b.out (default) or coff */
f177a611 102enum target_flavour output_flavor = BFD_BOUT_FORMAT;
81016051 103#endif
2fa0b342 104
2fa0b342
DHW
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
2fa0b342
DHW
108unsigned 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 */
113unsigned int total_files_seen;
114
2fa0b342 115args_type command_line;
173a0c3d 116
2fa0b342 117ld_config_type config;
9f629407
ILT
118
119static boolean check_for_scripts_dir PARAMS ((char *dir));
120static void read_entry_symbols
121 PARAMS ((bfd *desc, struct lang_input_statement_struct *entry));
122static void enter_file_symbols PARAMS ((lang_input_statement_type *entry));
123static void search_library PARAMS ((struct lang_input_statement_struct *));
124static lang_input_statement_type *decode_library_subfile
125 PARAMS ((struct lang_input_statement_struct *library_entry,
126 bfd *subfile_offset));
127static void linear_library PARAMS ((struct lang_input_statement_struct *));
128static void symdef_library PARAMS ((struct lang_input_statement_struct *));
129static void clear_syms PARAMS ((struct lang_input_statement_struct *entry,
130 file_ptr offset));
131static boolean subfile_wanted_p
132 PARAMS ((struct lang_input_statement_struct *));
173a0c3d 133\f
9f629407
ILT
134extern int main PARAMS ((int, char **));
135
136int
2fa0b342 137main (argc, argv)
2fa0b342 138 int argc;
9f629407 139 char **argv;
2fa0b342
DHW
140{
141 char *emulation;
e47bfa63 142
2fa0b342 143 program_name = argv[0];
99fe4553 144
e47bfa63 145 bfd_init ();
bfbdc80f 146
2fa0b342 147 /* Initialize the data about options. */
76855794 148
9d1fe8a4 149
bbd2521f 150 trace_files = trace_file_tries = false;
2fa0b342
DHW
151 write_map = false;
152 config.relocateable_output = false;
2fa0b342
DHW
153 command_line.force_common_definition = false;
154
e47bfa63
SC
155 init_bfd_error_vector ();
156 ldsym_init ();
157 ldfile_add_arch ("");
a72f4e5f 158
2fa0b342
DHW
159 config.make_executable = true;
160 force_make_executable = false;
161
2fa0b342
DHW
162 /* Initialize the cumulative counts of symbols. */
163 undefined_global_sym_count = 0;
2fa0b342
DHW
164 multiple_def_count = 0;
165 commons_pending = 0;
166
ce4d59e2
SC
167 config.magic_demand_paged = true;
168 config.text_read_only = true;
2fa0b342 169 config.make_executable = true;
1418c83b 170
d723cd17 171 emulation = get_emulation (argc, argv);
e47bfa63
SC
172 ldemul_choose_mode (emulation);
173 default_target = ldemul_choose_target ();
174 lang_init ();
175 ldemul_before_parse ();
2fa0b342 176 lang_has_input_file = false;
e47bfa63 177 parse_args (argc, argv);
f3739bc3 178
973e421e
ILT
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
bbd2521f
DM
183 if (had_script == false)
184 {
185 /* Read the emulation's appropriate default script. */
2a28d8b0
DM
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);
bbd2521f
DM
200 }
201
29f33467
SC
202 if (config.relocateable_output && command_line.relax)
203 {
f3739bc3 204 einfo ("%P%F: -relax and -r may not be used together\n");
29f33467 205 }
e47bfa63 206 lang_final ();
9d1fe8a4 207
e47bfa63
SC
208 if (lang_has_input_file == false)
209 {
973e421e 210 einfo ("%P%F: no input files\n");
870f54b2 211 }
2fa0b342 212
bbd2521f
DM
213 if (trace_files)
214 {
973e421e 215 info_msg ("%P: mode %s\n", emulation);
bbd2521f
DM
216 }
217
e47bfa63 218 ldemul_after_parse ();
870f54b2 219
9d1fe8a4 220
e47bfa63 221 if (config.map_filename)
870f54b2 222 {
e47bfa63 223 if (strcmp (config.map_filename, "-") == 0)
2e2bf962 224 {
e47bfa63 225 config.map_file = stdout;
2e2bf962 226 }
e47bfa63
SC
227 else
228 {
229 config.map_file = fopen (config.map_filename, FOPEN_WT);
230 if (config.map_file == (FILE *) NULL)
231 {
bbd2521f 232 einfo ("%P%F: cannot open map file %s: %E\n",
e47bfa63
SC
233 config.map_filename);
234 }
235 }
236 }
870f54b2 237
2e2bf962 238
e47bfa63 239 lang_process ();
2fa0b342 240
2fa0b342
DHW
241 /* Print error messages for any missing symbols, for any warning
242 symbols, and possibly multiple definitions */
243
2fa0b342 244
e47bfa63
SC
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 }
3e4c643d 254 }
2fa0b342 255
e47bfa63 256 if (config.relocateable_output)
f3739bc3 257 output_bfd->flags &= ~EXEC_P;
a72f4e5f 258 else
f3739bc3 259 output_bfd->flags |= EXEC_P;
b257477f 260
f3739bc3 261 ldwrite ();
b257477f 262
f3739bc3
SC
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?) */
a72f4e5f 266
f3739bc3
SC
267 if (config.make_executable == false && force_make_executable == false)
268 {
269 if (trace_files == true)
e47bfa63 270 {
973e421e 271 einfo ("%P: link errors found, deleting executable `%s'\n",
f3739bc3 272 output_filename);
e47bfa63 273 }
a72f4e5f 274
f3739bc3
SC
275 if (output_bfd->iostream)
276 fclose ((FILE *) (output_bfd->iostream));
a72f4e5f 277
f3739bc3
SC
278 unlink (output_filename);
279 exit (1);
280 }
281 else
282 {
283 bfd_close (output_bfd);
284 }
2fa0b342 285
e47bfa63 286 exit (0);
9f629407 287 return 0;
d723cd17
DM
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
293static char *
294get_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 {
973e421e 333 einfo("%P%F: missing argument to -m\n");
d723cd17
DM
334 }
335 }
973e421e
ILT
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 }
d723cd17
DM
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
363static boolean
364check_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);
f4208462 378 free (buf);
d723cd17 379 if (res)
f4208462 380 ldfile_add_library_path (dir);
d723cd17
DM
381 return res;
382}
383
384/* Set the default directory for finding script files.
f4208462
DM
385 Libraries will be searched for here too, but that's ok.
386 We look for the "ldscripts" directory in:
387
f4208462 388 SCRIPTDIR (passed from Makefile)
bbd2521f
DM
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) */
d723cd17
DM
391
392static void
393set_scripts_dir ()
394{
f4208462
DM
395 char *end, *dir;
396 size_t dirlen;
397
d723cd17 398 if (check_for_scripts_dir (SCRIPTDIR))
f4208462 399 return; /* We've been installed normally. */
d723cd17
DM
400
401 /* Look for "ldscripts" in the dir where our binary is. */
402 end = strrchr (program_name, '/');
bbd2521f
DM
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 }
f4208462 418
f4208462
DM
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. */
d723cd17 428}
2fa0b342 429
9f629407 430static void
bbd2521f 431read_entry_symbols (desc, entry)
2fa0b342
DHW
432 bfd *desc;
433 struct lang_input_statement_struct *entry;
434{
e47bfa63
SC
435 if (entry->asymbols == (asymbol **) NULL)
436 {
437 bfd_size_type table_size = get_symtab_upper_bound (desc);
2fa0b342 438
e47bfa63
SC
439 entry->asymbols = (asymbol **) ldmalloc (table_size);
440 entry->symbol_count = bfd_canonicalize_symtab (desc, entry->asymbols);
441 }
442}
2fa0b342
DHW
443
444/*
e47bfa63 445 * turn this item into a reference
2fa0b342 446 */
e47bfa63
SC
447void
448refize (sp, nlist_p)
449 ldsym_type *sp;
450 asymbol **nlist_p;
2fa0b342
DHW
451{
452 asymbol *sym = *nlist_p;
e47bfa63 453
2fa0b342 454 sym->value = 0;
973e421e
ILT
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
c611e285 460 sym->section = &bfd_und_section;
e47bfa63 461 sym->udata = (PTR) (sp->srefs_chain);
2fa0b342
DHW
462 sp->srefs_chain = nlist_p;
463}
e47bfa63 464
2fa0b342
DHW
465/*
466This function is called for each name which is seen which has a global
467scope. It enters the name into the global symbol table in the correct
468symbol on the correct chain. Remember that each ldsym_type has three
469chains attatched, one of all definitions of a symbol, one of all
470references of a symbol and one of all common definitions of a symbol.
471
472When the function is over, the supplied is left connected to the bfd
473to which is was born, with its udata field pointing to the next member
474on the chain in which it has been inserted.
475
476A certain amount of jigery pokery is necessary since commons come
477along and upset things, we only keep one item in the common chain; the
478one with the biggest size seen sofar. When another common comes along
479it either bumps the previous definition into the ref chain, since it
480is bigger, or gets turned into a ref on the spot since the one on the
481common chain is already bigger. If a real definition comes along then
482the common gets bumped off anyway.
483
484Whilst all this is going on we keep a count of the number of multiple
485definitions seen, undefined global symbols and pending commons.
486*/
487
2fa0b342 488void
bbd2521f 489enter_global_ref (nlist_p, name)
173a0c3d
DM
490 asymbol ** nlist_p; /* pointer into symbol table from incoming bfd */
491 CONST char *name; /* name of symbol in linker table */
2fa0b342
DHW
492{
493 asymbol *sym = *nlist_p;
e47bfa63 494 ldsym_type *sp;
b257477f
SC
495
496 /* Lookup the name from the incoming bfd's symbol table in the
497 linker's global symbol table */
2fa0b342 498
2fa0b342 499
b257477f 500 flagword this_symbol_flags = sym->flags;
e47bfa63 501
b257477f 502 sp = ldsym_get (name);
8a045e50
ILT
503
504
29f33467 505 /* If this symbol already has udata, it means that something strange
be1627d3 506 has happened.
29f33467 507
be1627d3
SC
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 */
2fa0b342 513
be1627d3
SC
514
515 if (sym->udata)
29f33467 516 return;
2fa0b342 517
3e4c643d 518
973e421e 519 if (this_symbol_flags & BSF_CONSTRUCTOR)
e47bfa63 520 {
29f33467
SC
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 }
29f33467 529 }
973e421e
ILT
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 }
e47bfa63
SC
539 else
540 {
29f33467
SC
541 if (bfd_is_com_section (sym->section))
542 {
543 /* If we have a definition of this symbol already then
2a28d8b0
DM
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. */
29f33467
SC
548 if (sp->sdefs_chain)
549 {
550 /* This is a common symbol, but we already have a definition
2a28d8b0
DM
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));
29f33467
SC
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 */
2a28d8b0
DM
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));
29f33467
SC
571 refize (sp, nlist_p);
572 }
573 else if (sp->scoms_chain != nlist_p)
574 {
575 /* other common is smaller, throw that away */
2a28d8b0
DM
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 }
29f33467
SC
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
2a28d8b0
DM
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. */
29f33467
SC
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--;
be1627d3 609
29f33467
SC
610 commons_pending++;
611 sp->scoms_chain = nlist_p;
612 }
613 }
be1627d3 614 }
29f33467
SC
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 {
2a28d8b0
DM
620 multiple_warn("%X%C: multiple definition of `%s'\n",
621 sym,
622 "%X%C: first defined here\n",
623 *(sp->sdefs_chain));
29f33467 624 multiple_def_count++;
29f33467
SC
625 }
626 else
627 {
628 sym->udata = (PTR) (sp->sdefs_chain);
629 sp->sdefs_chain = nlist_p;
630 }
973e421e 631
29f33467 632 /* A definition overrides a common symbol */
973e421e 633 if (sp->scoms_chain != NULL)
29f33467 634 {
2a28d8b0
DM
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));
29f33467
SC
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 }
be1627d3 650 else
29f33467
SC
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++;
29f33467 658 }
be1627d3 659
29f33467
SC
660 refize (sp, nlist_p);
661 }
be1627d3 662 }
be1627d3 663
e47bfa63
SC
664 ASSERT (sp->sdefs_chain == 0 || sp->scoms_chain == 0);
665 ASSERT (sp->scoms_chain == 0 || (*(sp->scoms_chain))->udata == 0);
2fa0b342
DHW
666}
667
668static void
bbd2521f 669enter_file_symbols (entry)
e47bfa63 670 lang_input_statement_type *entry;
2fa0b342 671{
e47bfa63 672 asymbol **q;
c611e285 673
2fa0b342 674 entry->common_section =
29f33467
SC
675 bfd_make_section_old_way (entry->the_bfd, "COMMON");
676 entry->common_section->flags = SEC_NEVER_LOAD;
e47bfa63 677 ldlang_add_file (entry);
2fa0b342 678
fe563ffe 679
bbd2521f 680 if (trace_files || trace_file_tries)
29f33467 681 {
973e421e 682 info_msg ("%I\n", entry);
29f33467 683 }
e47bfa63
SC
684
685 total_symbols_seen += entry->symbol_count;
686 total_files_seen++;
687 if (entry->symbol_count)
fe563ffe 688 {
29f33467
SC
689 for (q = entry->asymbols; *q; q++)
690 {
691 asymbol *p = *q;
8a045e50 692
29f33467
SC
693 if (had_y && p->name)
694 {
695 /* look up the symbol anyway to see if the trace bit was
973e421e 696 set */
29f33467
SC
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 }
81016051 705
973e421e
ILT
706 if (p->section == &bfd_ind_section
707 || (p->flags & BSF_INDIRECT) != 0)
29f33467
SC
708 {
709 add_indirect (q);
710 }
711 else if (p->flags & BSF_WARNING)
712 {
713 add_warning (p);
714 }
973e421e
ILT
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 }
29f33467
SC
737 else if (p->section == &bfd_und_section
738 || (p->flags & BSF_GLOBAL)
973e421e
ILT
739 || (p->flags & BSF_CONSTRUCTOR)
740 || (p->flags & BSF_WEAK))
29f33467 741 {
973e421e 742 enter_global_ref (q, p->name);
29f33467 743 }
29f33467 744 }
e47bfa63
SC
745 }
746}
2fa0b342 747
8a045e50 748
2fa0b342
DHW
749/* Searching libraries */
750
2fa0b342
DHW
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
9f629407 756static void
2fa0b342
DHW
757search_library (entry)
758 struct lang_input_statement_struct *entry;
759{
760
761 /* No need to load a library if no undefined symbols */
e47bfa63
SC
762 if (!undefined_global_sym_count)
763 return;
2fa0b342 764
e47bfa63 765 if (bfd_has_map (entry->the_bfd))
2fa0b342
DHW
766 symdef_library (entry);
767 else
768 linear_library (entry);
769
770}
771
99fe4553
SC
772#ifdef GNU960
773static
29f33467 774 boolean
99fe4553 775gnu960_check_format (abfd, format)
e47bfa63
SC
776 bfd *abfd;
777 bfd_format format;
99fe4553
SC
778{
779 boolean retval;
780
e47bfa63
SC
781 if ((bfd_check_format (abfd, format) == true)
782 && (abfd->xvec->flavour == output_flavor))
783 {
784 return true;
785 }
81016051
SC
786
787
99fe4553
SC
788 return false;
789}
e47bfa63 790
99fe4553
SC
791#endif
792
2fa0b342 793void
1418c83b 794ldmain_open_file_read_symbol (entry)
e47bfa63 795 struct lang_input_statement_struct *entry;
2fa0b342 796{
e47bfa63
SC
797 if (entry->asymbols == (asymbol **) NULL
798 && entry->real == true
799 && entry->filename != (char *) NULL)
2fa0b342
DHW
800 {
801 ldfile_open_file (entry);
802
ce4d59e2 803
99fe4553 804#ifdef GNU960
e47bfa63 805 if (gnu960_check_format (entry->the_bfd, bfd_object))
99fe4553 806#else
e47bfa63 807 if (bfd_check_format (entry->the_bfd, bfd_object))
99fe4553 808#endif
2fa0b342 809 {
e47bfa63 810 entry->the_bfd->usrdata = (PTR) entry;
2fa0b342
DHW
811
812
bbd2521f 813 read_entry_symbols (entry->the_bfd, entry);
ce4d59e2
SC
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
bbd2521f 819 enter_file_symbols (entry);
2fa0b342 820 }
99fe4553 821#ifdef GNU960
e47bfa63 822 else if (gnu960_check_format (entry->the_bfd, bfd_archive))
99fe4553 823#else
e47bfa63 824 else if (bfd_check_format (entry->the_bfd, bfd_archive))
99fe4553 825#endif
2fa0b342 826 {
e47bfa63 827 entry->the_bfd->usrdata = (PTR) entry;
2fa0b342 828
e47bfa63 829 entry->subfiles = (lang_input_statement_type *) NULL;
2fa0b342
DHW
830 search_library (entry);
831 }
e47bfa63 832 else
2fa0b342 833 {
e47bfa63
SC
834 einfo ("%F%B: malformed input file (not rel or archive) \n",
835 entry->the_bfd);
2fa0b342
DHW
836 }
837 }
838
839}
840
2fa0b342
DHW
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
9f629407 847static lang_input_statement_type *
2fa0b342
DHW
848decode_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;
e47bfa63 853
1418c83b 854
8a045e50
ILT
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. */
2fa0b342 858
29f33467
SC
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 }
2fa0b342
DHW
884 return subentry;
885}
886
9f629407 887static void
e47bfa63
SC
888clear_syms (entry, offset)
889 struct lang_input_statement_struct *entry;
890 file_ptr offset;
2fa0b342
DHW
891{
892 carsym *car;
9f629407
ILT
893 symindex indx = bfd_get_next_mapent (entry->the_bfd,
894 BFD_NO_MORE_SYMBOLS,
895 &car);
e47bfa63
SC
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);
2fa0b342 904 }
2fa0b342
DHW
905
906}
907
908/* Search a library that has a map
909 */
9f629407 910static void
2fa0b342
DHW
911symdef_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
2fa0b342 919 while (not_finished == true)
e47bfa63
SC
920 {
921 carsym *exported_library_name;
922 bfd *prev_archive_member_bfd = 0;
2fa0b342 923
9f629407
ILT
924 symindex idx = bfd_get_next_mapent (entry->the_bfd,
925 BFD_NO_MORE_SYMBOLS,
926 &exported_library_name);
2fa0b342 927
e47bfa63 928 not_finished = false;
2fa0b342 929
e47bfa63
SC
930 while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count)
931 {
e47bfa63
SC
932 if (exported_library_name->name)
933 {
e47bfa63 934 ldsym_type *sp = ldsym_get_soft (exported_library_name->name);
3e4c643d 935
e47bfa63 936 /* If we find a symbol that appears to be needed, think carefully
9f629407 937 about the archive member that the symbol is in. */
e47bfa63 938 /* So - if it exists, and is referenced somewhere and is
9f629407 939 undefined or */
e47bfa63
SC
940 if (sp && sp->srefs_chain && !sp->sdefs_chain)
941 {
9f629407
ILT
942 bfd *archive_member_bfd =
943 bfd_get_elt_at_index (entry->the_bfd, (int) idx);
e47bfa63 944 struct lang_input_statement_struct *archive_member_lang_input_statement_struct;
2fa0b342 945
99fe4553 946#ifdef GNU960
9f629407
ILT
947 if (archive_member_bfd
948 && gnu960_check_format (archive_member_bfd, bfd_object))
99fe4553 949#else
9f629407
ILT
950 if (archive_member_bfd
951 && bfd_check_format (archive_member_bfd, bfd_object))
99fe4553 952#endif
e47bfa63 953 {
e47bfa63 954 /* Don't think carefully about any archive member
9f629407 955 more than once in a given pass. */
e47bfa63
SC
956 if (prev_archive_member_bfd != archive_member_bfd)
957 {
2fa0b342 958
e47bfa63 959 prev_archive_member_bfd = archive_member_bfd;
2fa0b342 960
e47bfa63 961 /* Read the symbol table of the archive member. */
2fa0b342 962
e47bfa63
SC
963 if (archive_member_bfd->usrdata != (PTR) NULL)
964 {
2fa0b342 965
9f629407
ILT
966 archive_member_lang_input_statement_struct =
967 ((lang_input_statement_type *)
968 archive_member_bfd->usrdata);
e47bfa63
SC
969 }
970 else
971 {
e47bfa63 972 archive_member_lang_input_statement_struct =
9f629407
ILT
973 decode_library_subfile (entry,
974 archive_member_bfd);
975 archive_member_bfd->usrdata =
976 (PTR) archive_member_lang_input_statement_struct;
e47bfa63 977 }
2fa0b342 978
e47bfa63
SC
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 }
2fa0b342 984
e47bfa63
SC
985 if (archive_member_lang_input_statement_struct->loaded == false)
986 {
9f629407
ILT
987 read_entry_symbols (archive_member_bfd,
988 archive_member_lang_input_statement_struct);
2fa0b342 989
9f629407
ILT
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)
2fa0b342 994
e47bfa63
SC
995 {
996 /* This member is needed; load it.
9f629407
ILT
997 Since we are loading something on
998 this pass, we must make another
999 pass through the symdef data. */
e47bfa63 1000 not_finished = true;
2fa0b342 1001
bbd2521f 1002 enter_file_symbols (archive_member_lang_input_statement_struct);
2fa0b342 1003
e47bfa63
SC
1004 if (prev)
1005 prev->chain = archive_member_lang_input_statement_struct;
1006 else
1007 entry->subfiles = archive_member_lang_input_statement_struct;
2fa0b342
DHW
1008
1009
e47bfa63 1010 prev = archive_member_lang_input_statement_struct;
2fa0b342
DHW
1011
1012
9f629407
ILT
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);
e47bfa63 1019 archive_member_lang_input_statement_struct->loaded = true;
2fa0b342 1020 }
e47bfa63 1021 }
2fa0b342 1022 }
e47bfa63
SC
1023 }
1024 }
2fa0b342 1025 }
9f629407
ILT
1026 idx = bfd_get_next_mapent (entry->the_bfd, idx,
1027 &exported_library_name);
e47bfa63
SC
1028 }
1029 }
2fa0b342
DHW
1030}
1031
9f629407 1032static void
2fa0b342 1033linear_library (entry)
e47bfa63 1034 struct lang_input_statement_struct *entry;
2fa0b342
DHW
1035{
1036 boolean more_to_do = true;
1037 register struct lang_input_statement_struct *prev = 0;
1038
e47bfa63
SC
1039 if (entry->complained == false)
1040 {
29f33467
SC
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 }
e47bfa63 1048 entry->complained = true;
8a045e50 1049
e47bfa63
SC
1050 }
1051 while (more_to_do)
1052 {
2fa0b342 1053
e47bfa63 1054 bfd *archive = bfd_openr_next_archived_file (entry->the_bfd, 0);
2fa0b342 1055
e47bfa63
SC
1056 more_to_do = false;
1057 while (archive)
1058 {
f3739bc3
SC
1059 /* Don't check this file if it's already been read in
1060 once */
1061
1062 if (!archive->usrdata ||
29f33467
SC
1063 !((lang_input_statement_type *) (archive->usrdata))->loaded)
1064 {
99fe4553 1065#ifdef GNU960
29f33467 1066 if (gnu960_check_format (archive, bfd_object))
99fe4553 1067#else
29f33467 1068 if (bfd_check_format (archive, bfd_object))
99fe4553 1069#endif
29f33467
SC
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 {
bbd2521f 1081 read_entry_symbols (archive, subentry);
29f33467
SC
1082
1083 if (subfile_wanted_p (subentry) == true)
1084 {
bbd2521f 1085 enter_file_symbols (subentry);
29f33467
SC
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 }
e47bfa63 1099 archive = bfd_openr_next_archived_file (entry->the_bfd, archive);
2fa0b342 1100
2fa0b342 1101 }
2fa0b342 1102
e47bfa63 1103 }
2fa0b342
DHW
1104}
1105
29f33467 1106/* ENTRY is an entry for a file inside an archive
e47bfa63
SC
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 */
2fa0b342 1110
9f629407 1111static boolean
2fa0b342 1112subfile_wanted_p (entry)
e47bfa63 1113 struct lang_input_statement_struct *entry;
2fa0b342
DHW
1114{
1115 asymbol **q;
1116
173a0c3d
DM
1117 if (entry->symbol_count == 0)
1118 return false;
1119
2fa0b342
DHW
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
e47bfa63
SC
1127 if (p->flags & BSF_INDIRECT)
1128 {
29f33467 1129 /** add_indirect(q);*/
e47bfa63 1130 }
3e4c643d 1131
8a045e50 1132 if (bfd_is_com_section (p->section)
9d1fe8a4 1133 || (p->flags & BSF_GLOBAL)
973e421e
ILT
1134 || (p->flags & BSF_INDIRECT)
1135 || (p->flags & BSF_WEAK))
2fa0b342
DHW
1136 {
1137 register ldsym_type *sp = ldsym_get_soft (p->name);
1138
2fa0b342
DHW
1139 /* If this symbol has not been hashed,
1140 we can't be looking for it. */
e47bfa63
SC
1141 if (sp != (ldsym_type *) NULL
1142 && sp->sdefs_chain == (asymbol **) NULL)
1143 {
973e421e
ILT
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)
e47bfa63 1172 {
f31cb329 1173 /* This is a symbol we are looking for. It is
973e421e 1174 either common or not yet defined. If this is a
f31cb329
ILT
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). */
8a045e50 1182 if (bfd_is_com_section (p->section))
e47bfa63
SC
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
29f33467 1215 (*(sp->scoms_chain))->section = p->section;
e47bfa63
SC
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 *)
29f33467 1226 (bfd_asymbol_bfd (com)->usrdata))->common_section ==
e47bfa63
SC
1227 (asection *) NULL)
1228 {
1229 ((lang_input_statement_type *)
29f33467
SC
1230 (bfd_asymbol_bfd (com)->usrdata))->common_section =
1231 bfd_make_section_old_way (bfd_asymbol_bfd (com), "COMMON");
e47bfa63 1232 }
fcf276c4
ILT
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));
e47bfa63
SC
1244 }
1245 }
1246 ASSERT (p->udata == 0);
2fa0b342 1247 }
f31cb329 1248 else if (sp->scoms_chain == (asymbol **) NULL)
2fa0b342 1249 {
e47bfa63
SC
1250 if (write_map)
1251 {
973e421e 1252 info_msg ("%I needed due to %s\n", entry, sp->name);
e47bfa63
SC
1253 }
1254 return true;
2fa0b342 1255 }
2fa0b342 1256 }
e47bfa63 1257 }
2fa0b342
DHW
1258 }
1259 }
1260
1261 return false;
1262}
8a045e50
ILT
1263
1264void
29f33467
SC
1265add_ysym (text)
1266 char *text;
8a045e50 1267{
29f33467 1268 ldsym_type *lookup = ldsym_get (text);
8a045e50
ILT
1269 lookup->flags |= SYM_Y;
1270 had_y = 1;
1271}
This page took 0.163245 seconds and 4 git commands to generate.