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