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