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