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