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