* Makefile.in: (ALLOCA, MALLOC): Remove variables.
[deliverable/binutils-gdb.git] / ld / ldmain.c
CommitLineData
8594f568 1/* Main program of GNU linker.
7d6439d9 2 Copyright (C) 1991, 92, 93, 94, 1995 Free Software Foundation, Inc.
ce4d59e2 3 Written by Steve Chamberlain steve@cygnus.com
e47bfa63 4
2fa0b342
DHW
5This file is part of GLD, the Gnu Linker.
6
7GLD is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
ce4d59e2 9the Free Software Foundation; either version 2, or (at your option)
2fa0b342
DHW
10any later version.
11
12GLD is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GLD; see the file COPYING. If not, write to
943fbd5b 19the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
2fa0b342 20
2fa0b342 21
2fa0b342 22#include "bfd.h"
f177a611 23#include "sysdep.h"
b9395be3 24#include <stdio.h>
7d6439d9 25#include <ctype.h>
d4e5e3c3 26#include "libiberty.h"
a735edad 27#include "progress.h"
b9395be3 28#include "bfdlink.h"
2fa0b342 29
2fa0b342
DHW
30#include "ld.h"
31#include "ldmain.h"
32#include "ldmisc.h"
33#include "ldwrite.h"
34#include "ldgram.h"
fcf276c4 35#include "ldexp.h"
2fa0b342 36#include "ldlang.h"
8c514453 37#include "ldemul.h"
2fa0b342
DHW
38#include "ldlex.h"
39#include "ldfile.h"
c611e285 40#include "ldctor.h"
9d1fe8a4 41
922018a1 42/* Somewhere above, sys/stat.h got included . . . . */
d723cd17
DM
43#if !defined(S_ISDIR) && defined(S_IFDIR)
44#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
45#endif
46
47#include <string.h>
48
fcf276c4
ILT
49static char *get_emulation PARAMS ((int, char **));
50static void set_scripts_dir PARAMS ((void));
e47bfa63 51
2fa0b342
DHW
52/* EXPORTS */
53
54char *default_target;
fcf276c4 55const char *output_filename = "a.out";
e47bfa63 56
2fa0b342
DHW
57/* Name this program was invoked by. */
58char *program_name;
59
60/* The file that we're creating */
b6316534 61bfd *output_bfd = 0;
2fa0b342 62
173a0c3d
DM
63/* Set by -G argument, for MIPS ECOFF target. */
64int g_switch_value = 8;
65
2fa0b342
DHW
66/* Nonzero means print names of input files as processed. */
67boolean trace_files;
68
bbd2521f
DM
69/* Nonzero means same, but note open failures, too. */
70boolean trace_file_tries;
71
7b40f2b1
DM
72/* Nonzero means version number was printed, so exit successfully
73 instead of complaining if no input files are given. */
74boolean version_printed;
75
f2f55b16
ILT
76/* Nonzero means link in every member of an archive. */
77boolean whole_archive;
78
2fa0b342 79args_type command_line;
173a0c3d 80
2fa0b342 81ld_config_type config;
9f629407
ILT
82
83static boolean check_for_scripts_dir PARAMS ((char *dir));
b9395be3
ILT
84static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
85 const char *));
86static boolean multiple_definition PARAMS ((struct bfd_link_info *,
87 const char *,
88 bfd *, asection *, bfd_vma,
89 bfd *, asection *, bfd_vma));
90static boolean multiple_common PARAMS ((struct bfd_link_info *,
91 const char *, bfd *,
92 enum bfd_link_hash_type, bfd_vma,
93 bfd *, enum bfd_link_hash_type,
94 bfd_vma));
95static boolean add_to_set PARAMS ((struct bfd_link_info *,
96 struct bfd_link_hash_entry *,
2a9fa50c 97 bfd_reloc_code_real_type,
b9395be3
ILT
98 bfd *, asection *, bfd_vma));
99static boolean constructor_callback PARAMS ((struct bfd_link_info *,
100 boolean constructor,
b9395be3
ILT
101 const char *name,
102 bfd *, asection *, bfd_vma));
103static boolean warning_callback PARAMS ((struct bfd_link_info *,
7d6439d9
ILT
104 const char *, const char *, bfd *,
105 asection *, bfd_vma));
106static void warning_find_reloc PARAMS ((bfd *, asection *, PTR));
b9395be3
ILT
107static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
108 const char *, bfd *,
109 asection *, bfd_vma));
5dad4c97
ILT
110static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
111 const char *, bfd_vma,
112 bfd *, asection *, bfd_vma));
b9395be3
ILT
113static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
114 bfd *, asection *, bfd_vma));
115static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
116 const char *, bfd *, asection *,
117 bfd_vma));
118static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
119 bfd *, asection *, bfd_vma));
120
121static struct bfd_link_callbacks link_callbacks =
122{
123 add_archive_element,
124 multiple_definition,
125 multiple_common,
126 add_to_set,
127 constructor_callback,
128 warning_callback,
129 undefined_symbol,
130 reloc_overflow,
131 reloc_dangerous,
132 unattached_reloc,
133 notice_ysym
134};
135
136struct bfd_link_info link_info;
173a0c3d 137\f
0b2f8d2e
DM
138static void
139remove_output ()
140{
141 if (output_filename)
142 {
143 if (output_bfd && output_bfd->iostream)
144 fclose((FILE *)(output_bfd->iostream));
145 if (delete_output_file_on_failure)
146 unlink (output_filename);
147 }
148}
149
9f629407 150int
2fa0b342 151main (argc, argv)
2fa0b342 152 int argc;
9f629407 153 char **argv;
2fa0b342
DHW
154{
155 char *emulation;
8594f568 156 long start_time = get_run_time ();
e47bfa63 157
2fa0b342 158 program_name = argv[0];
5bcb7f28 159 xmalloc_set_program_name (program_name);
99fe4553 160
a735edad
ILT
161 START_PROGRESS (program_name, 0);
162
e47bfa63 163 bfd_init ();
bfbdc80f 164
5bcb7f28 165 xatexit (remove_output);
0b2f8d2e 166
2fa0b342 167 /* Initialize the data about options. */
7b40f2b1 168 trace_files = trace_file_tries = version_printed = false;
f2f55b16 169 whole_archive = false;
c96386c4 170 config.traditional_format = false;
b9395be3 171 config.build_constructors = true;
2a9fa50c 172 config.dynamic_link = false;
2fa0b342 173 command_line.force_common_definition = false;
7fb9ca5f 174 command_line.interpreter = NULL;
cc23cc69 175 command_line.rpath = NULL;
2fa0b342 176
b9395be3
ILT
177 link_info.callbacks = &link_callbacks;
178 link_info.relocateable = false;
64887de2 179 link_info.shared = false;
4551e108 180 link_info.symbolic = false;
7d6439d9 181 link_info.static_link = false;
b9395be3
ILT
182 link_info.strip = strip_none;
183 link_info.discard = discard_none;
184 link_info.lprefix_len = 1;
185 link_info.lprefix = "L";
186 link_info.keep_memory = true;
187 link_info.input_bfds = NULL;
188 link_info.create_object_symbols_section = NULL;
189 link_info.hash = NULL;
190 link_info.keep_hash = NULL;
191 link_info.notice_hash = NULL;
192
7d6439d9 193
e47bfa63 194 ldfile_add_arch ("");
a72f4e5f 195
2fa0b342
DHW
196 config.make_executable = true;
197 force_make_executable = false;
ce4d59e2
SC
198 config.magic_demand_paged = true;
199 config.text_read_only = true;
2fa0b342 200 config.make_executable = true;
1418c83b 201
d723cd17 202 emulation = get_emulation (argc, argv);
e47bfa63
SC
203 ldemul_choose_mode (emulation);
204 default_target = ldemul_choose_target ();
205 lang_init ();
206 ldemul_before_parse ();
2fa0b342 207 lang_has_input_file = false;
e47bfa63 208 parse_args (argc, argv);
f3739bc3 209
7d6439d9
ILT
210 ldemul_set_symbols ();
211
2a9fa50c
ILT
212 if (link_info.relocateable)
213 {
214 if (command_line.relax)
215 einfo ("%P%F: -relax and -r may not be used together\n");
64887de2
ILT
216 if (link_info.shared)
217 einfo ("%P%F: -r and -shared may not be used together\n");
2a9fa50c
ILT
218 }
219
8ed88239
ILT
220 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
221 don't see how else this can be handled, since in this case we
222 must preserve all externally visible symbols. */
223 if (link_info.relocateable && link_info.strip == strip_all)
224 {
225 link_info.strip = strip_debugger;
226 if (link_info.discard == discard_none)
227 link_info.discard = discard_all;
228 }
229
973e421e
ILT
230 /* This essentially adds another -L directory so this must be done after
231 the -L's in argv have been processed. */
232 set_scripts_dir ();
233
bbd2521f
DM
234 if (had_script == false)
235 {
236 /* Read the emulation's appropriate default script. */
2a28d8b0
DM
237 int isfile;
238 char *s = ldemul_get_script (&isfile);
239
240 if (isfile)
d4e5e3c3 241 ldfile_open_command_file (s);
2a28d8b0 242 else
22c41f00
ILT
243 {
244 if (trace_file_tries)
245 {
246 info_msg ("using internal linker script:\n");
247 info_msg ("==================================================\n");
248 info_msg (s);
249 info_msg ("\n==================================================\n");
250 }
7d6439d9 251 lex_string = s;
22c41f00
ILT
252 lex_redirect (s);
253 }
d4e5e3c3
DM
254 parser_input = input_script;
255 yyparse ();
7d6439d9 256 lex_string = NULL;
bbd2521f
DM
257 }
258
e47bfa63 259 lang_final ();
9d1fe8a4 260
e47bfa63
SC
261 if (lang_has_input_file == false)
262 {
7b40f2b1 263 if (version_printed)
0b2f8d2e 264 xexit (0);
973e421e 265 einfo ("%P%F: no input files\n");
870f54b2 266 }
2fa0b342 267
bbd2521f
DM
268 if (trace_files)
269 {
973e421e 270 info_msg ("%P: mode %s\n", emulation);
bbd2521f
DM
271 }
272
e47bfa63 273 ldemul_after_parse ();
870f54b2 274
9d1fe8a4 275
e47bfa63 276 if (config.map_filename)
870f54b2 277 {
e47bfa63 278 if (strcmp (config.map_filename, "-") == 0)
2e2bf962 279 {
e47bfa63 280 config.map_file = stdout;
2e2bf962 281 }
e47bfa63
SC
282 else
283 {
284 config.map_file = fopen (config.map_filename, FOPEN_WT);
285 if (config.map_file == (FILE *) NULL)
286 {
de71eb77 287 bfd_set_error (bfd_error_system_call);
bbd2521f 288 einfo ("%P%F: cannot open map file %s: %E\n",
e47bfa63
SC
289 config.map_filename);
290 }
291 }
292 }
870f54b2 293
2e2bf962 294
e47bfa63 295 lang_process ();
2fa0b342 296
2fa0b342
DHW
297 /* Print error messages for any missing symbols, for any warning
298 symbols, and possibly multiple definitions */
299
2fa0b342 300
e47bfa63
SC
301 if (config.text_read_only)
302 {
303 /* Look for a text section and mark the readonly attribute in it */
304 asection *found = bfd_get_section_by_name (output_bfd, ".text");
305
306 if (found != (asection *) NULL)
307 {
308 found->flags |= SEC_READONLY;
309 }
3e4c643d 310 }
2fa0b342 311
a735edad 312 if (link_info.relocateable)
f3739bc3 313 output_bfd->flags &= ~EXEC_P;
a72f4e5f 314 else
f3739bc3 315 output_bfd->flags |= EXEC_P;
b257477f 316
f3739bc3 317 ldwrite ();
b257477f 318
f3739bc3
SC
319 /* Even if we're producing relocateable output, some non-fatal errors should
320 be reported in the exit status. (What non-fatal errors, if any, do we
321 want to ignore for relocateable output?) */
a72f4e5f 322
f3739bc3
SC
323 if (config.make_executable == false && force_make_executable == false)
324 {
325 if (trace_files == true)
e47bfa63 326 {
973e421e 327 einfo ("%P: link errors found, deleting executable `%s'\n",
f3739bc3 328 output_filename);
e47bfa63 329 }
a72f4e5f 330
f3739bc3
SC
331 if (output_bfd->iostream)
332 fclose ((FILE *) (output_bfd->iostream));
a72f4e5f 333
f3739bc3 334 unlink (output_filename);
0b2f8d2e 335 xexit (1);
f3739bc3
SC
336 }
337 else
338 {
2a9fa50c
ILT
339 if (! bfd_close (output_bfd))
340 einfo ("%F%B: final close failed: %E\n", output_bfd);
f3739bc3 341 }
2fa0b342 342
a735edad
ILT
343 END_PROGRESS (program_name);
344
b9395be3
ILT
345 if (config.stats)
346 {
347 extern char **environ;
eac6290c 348#ifdef HAVE_SBRK
b9395be3 349 char *lim = (char *) sbrk (0);
de71eb77 350#endif
8594f568 351 long run_time = get_run_time () - start_time;
b9395be3 352
5bcb7f28 353 fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
8594f568 354 program_name, run_time / 1000000, run_time % 1000000);
eac6290c 355#ifdef HAVE_SBRK
b9395be3
ILT
356 fprintf (stderr, "%s: data size %ld\n", program_name,
357 (long) (lim - (char *) &environ));
de71eb77 358#endif
b9395be3
ILT
359 }
360
52a8ebfe
DM
361 /* Prevent remove_output from doing anything, after a successful link. */
362 output_filename = NULL;
363
0b2f8d2e 364 xexit (0);
9f629407 365 return 0;
d723cd17
DM
366}
367
368/* We need to find any explicitly given emulation in order to initialize the
369 state that's needed by the lex&yacc argument parser (parse_args). */
370
371static char *
372get_emulation (argc, argv)
373 int argc;
374 char **argv;
375{
376 char *emulation;
377 int i;
378
d723cd17
DM
379 emulation = (char *) getenv (EMULATION_ENVIRON);
380 if (emulation == NULL)
381 emulation = DEFAULT_EMULATION;
d723cd17
DM
382
383 for (i = 1; i < argc; i++)
384 {
385 if (!strncmp (argv[i], "-m", 2))
386 {
387 if (argv[i][2] == '\0')
388 {
389 /* -m EMUL */
390 if (i < argc - 1)
391 {
392 emulation = argv[i + 1];
393 i++;
394 }
395 else
396 {
973e421e 397 einfo("%P%F: missing argument to -m\n");
d723cd17
DM
398 }
399 }
973e421e
ILT
400 else if (strcmp (argv[i], "-mips1") == 0
401 || strcmp (argv[i], "-mips2") == 0
402 || strcmp (argv[i], "-mips3") == 0)
403 {
404 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
405 passed to the linker by some MIPS compilers. They
406 generally tell the linker to use a slightly different
407 library path. Perhaps someday these should be
408 implemented as emulations; until then, we just ignore
409 the arguments and hope that nobody ever creates
410 emulations named ips1, ips2 or ips3. */
411 }
cbbf9608
ILT
412 else if (strcmp (argv[i], "-m486") == 0)
413 {
414 /* FIXME: The argument -m486 is passed to the linker on
415 some Linux systems. Hope that nobody creates an
416 emulation named 486. */
417 }
d723cd17
DM
418 else
419 {
420 /* -mEMUL */
421 emulation = &argv[i][2];
422 }
423 }
424 }
425
426 return emulation;
427}
428
429/* If directory DIR contains an "ldscripts" subdirectory,
430 add DIR to the library search path and return true,
431 else return false. */
432
433static boolean
434check_for_scripts_dir (dir)
435 char *dir;
436{
437 size_t dirlen;
438 char *buf;
439 struct stat s;
440 boolean res;
441
442 dirlen = strlen (dir);
443 /* sizeof counts the terminating NUL. */
0b2f8d2e 444 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
d723cd17
DM
445 sprintf (buf, "%s/ldscripts", dir);
446
447 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
f4208462 448 free (buf);
d723cd17 449 if (res)
0cd82d00 450 ldfile_add_library_path (dir, false);
d723cd17
DM
451 return res;
452}
453
454/* Set the default directory for finding script files.
f4208462
DM
455 Libraries will be searched for here too, but that's ok.
456 We look for the "ldscripts" directory in:
457
f4208462 458 SCRIPTDIR (passed from Makefile)
bbd2521f
DM
459 the dir where this program is (for using it from the build tree)
460 the dir where this program is/../lib (for installing the tool suite elsewhere) */
d723cd17
DM
461
462static void
463set_scripts_dir ()
464{
f4208462
DM
465 char *end, *dir;
466 size_t dirlen;
467
d723cd17 468 if (check_for_scripts_dir (SCRIPTDIR))
f4208462 469 return; /* We've been installed normally. */
d723cd17
DM
470
471 /* Look for "ldscripts" in the dir where our binary is. */
472 end = strrchr (program_name, '/');
7d6439d9
ILT
473
474 if (end == NULL)
bbd2521f 475 {
7d6439d9
ILT
476 /* Don't look for ldscripts in the current directory. There is
477 too much potential for confusion. */
478 return;
bbd2521f 479 }
f4208462 480
7d6439d9
ILT
481 dirlen = end - program_name;
482 /* Make a copy of program_name in dir.
483 Leave room for later "/../lib". */
484 dir = (char *) xmalloc (dirlen + 8);
485 strncpy (dir, program_name, dirlen);
486 dir[dirlen] = '\0';
487
f4208462
DM
488 if (check_for_scripts_dir (dir))
489 return; /* Don't free dir. */
490
491 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
492 strcpy (dir + dirlen, "/../lib");
493 if (check_for_scripts_dir (dir))
494 return;
495
496 free (dir); /* Well, we tried. */
d723cd17 497}
2fa0b342 498
e47bfa63 499void
b9395be3
ILT
500add_ysym (name)
501 const char *name;
2fa0b342 502{
b9395be3
ILT
503 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
504 {
505 link_info.notice_hash = ((struct bfd_hash_table *)
0b2f8d2e 506 xmalloc (sizeof (struct bfd_hash_table)));
b9395be3
ILT
507 if (! bfd_hash_table_init_n (link_info.notice_hash,
508 bfd_hash_newfunc,
509 61))
510 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
511 }
512
513 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
514 == (struct bfd_hash_entry *) NULL)
515 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
2fa0b342 516}
e47bfa63 517
b9395be3 518/* Handle the -retain-symbols-file option. */
2fa0b342 519
2fa0b342 520void
b9395be3
ILT
521add_keepsyms_file (filename)
522 const char *filename;
2fa0b342 523{
b9395be3
ILT
524 FILE *file;
525 char *buf;
526 size_t bufsize;
527 int c;
2fa0b342 528
b9395be3
ILT
529 if (link_info.strip == strip_some)
530 einfo ("%X%P: error: duplicate retain-symbols-file\n");
3e4c643d 531
b9395be3
ILT
532 file = fopen (filename, "r");
533 if (file == (FILE *) NULL)
e47bfa63 534 {
5bcb7f28 535 bfd_set_error (bfd_error_system_call);
7d6439d9 536 einfo ("%X%P: %s: %E\n", filename);
b9395be3 537 return;
be1627d3 538 }
be1627d3 539
b9395be3 540 link_info.keep_hash = ((struct bfd_hash_table *)
0b2f8d2e 541 xmalloc (sizeof (struct bfd_hash_table)));
b9395be3
ILT
542 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
543 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
2fa0b342 544
b9395be3 545 bufsize = 100;
0b2f8d2e 546 buf = (char *) xmalloc (bufsize);
c611e285 547
b9395be3
ILT
548 c = getc (file);
549 while (c != EOF)
29f33467 550 {
b9395be3
ILT
551 while (isspace (c))
552 c = getc (file);
e47bfa63 553
b9395be3 554 if (c != EOF)
29f33467 555 {
b9395be3 556 size_t len = 0;
8a045e50 557
b9395be3 558 while (! isspace (c) && c != EOF)
29f33467 559 {
b9395be3
ILT
560 buf[len] = c;
561 ++len;
562 if (len >= bufsize)
29f33467 563 {
b9395be3 564 bufsize *= 2;
0b2f8d2e 565 buf = xrealloc (buf, bufsize);
29f33467 566 }
b9395be3 567 c = getc (file);
29f33467 568 }
81016051 569
b9395be3
ILT
570 buf[len] = '\0';
571
572 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
573 == (struct bfd_hash_entry *) NULL)
7d6439d9 574 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E\n");
29f33467 575 }
e47bfa63 576 }
2fa0b342 577
b9395be3
ILT
578 if (link_info.strip != strip_none)
579 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
8a045e50 580
b9395be3
ILT
581 link_info.strip = strip_some;
582}
583\f
584/* Callbacks from the BFD linker routines. */
2fa0b342 585
b9395be3
ILT
586/* This is called when BFD has decided to include an archive member in
587 a link. */
2fa0b342 588
b9395be3
ILT
589/*ARGSUSED*/
590static boolean
591add_archive_element (info, abfd, name)
592 struct bfd_link_info *info;
593 bfd *abfd;
594 const char *name;
2fa0b342 595{
b9395be3
ILT
596 lang_input_statement_type *input;
597
598 input = ((lang_input_statement_type *)
7d6439d9 599 xmalloc (sizeof (lang_input_statement_type)));
b9395be3
ILT
600 input->filename = abfd->filename;
601 input->local_sym_name = abfd->filename;
602 input->the_bfd = abfd;
603 input->asymbols = NULL;
b9395be3
ILT
604 input->next = NULL;
605 input->just_syms_flag = false;
606 input->loaded = false;
210c52ac 607 input->search_dirs_flag = false;
b9395be3
ILT
608
609 /* FIXME: The following fields are not set: header.next,
210c52ac
ILT
610 header.type, closed, passive_position, symbol_count,
611 next_real_file, is_archive, target, real, common_section,
612 common_output_section, complained. This bit of code is from the
613 old decode_library_subfile function. I don't know whether any of
614 those fields matters. */
b9395be3
ILT
615
616 ldlang_add_file (input);
617
2a9fa50c
ILT
618 if (config.map_file != (FILE *) NULL)
619 minfo ("%s needed due to %T\n", abfd->filename, name);
b9395be3 620
5dad4c97
ILT
621 if (trace_files || trace_file_tries)
622 info_msg ("%I\n", input);
623
b9395be3
ILT
624 return true;
625}
2fa0b342 626
b9395be3
ILT
627/* This is called when BFD has discovered a symbol which is defined
628 multiple times. */
2fa0b342 629
b9395be3
ILT
630/*ARGSUSED*/
631static boolean
632multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
633 struct bfd_link_info *info;
634 const char *name;
635 bfd *obfd;
636 asection *osec;
637 bfd_vma oval;
638 bfd *nbfd;
639 asection *nsec;
640 bfd_vma nval;
641{
642 einfo ("%X%C: multiple definition of `%T'\n",
643 nbfd, nsec, nval, name);
644 if (obfd != (bfd *) NULL)
cc23cc69 645 einfo ("%D: first defined here\n", obfd, osec, oval);
b9395be3 646 return true;
2fa0b342
DHW
647}
648
b9395be3
ILT
649/* This is called when there is a definition of a common symbol, or
650 when a common symbol is found for a symbol that is already defined,
651 or when two common symbols are found. We only do something if
652 -warn-common was used. */
653
654/*ARGSUSED*/
655static boolean
656multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
657 struct bfd_link_info *info;
658 const char *name;
659 bfd *obfd;
660 enum bfd_link_hash_type otype;
661 bfd_vma osize;
662 bfd *nbfd;
663 enum bfd_link_hash_type ntype;
664 bfd_vma nsize;
99fe4553 665{
b9395be3
ILT
666 if (! config.warn_common)
667 return true;
99fe4553 668
8ed88239
ILT
669 if (ntype == bfd_link_hash_defined
670 || ntype == bfd_link_hash_defweak
671 || ntype == bfd_link_hash_indirect)
e47bfa63 672 {
b9395be3
ILT
673 ASSERT (otype == bfd_link_hash_common);
674 einfo ("%B: warning: definition of `%T' overriding common\n",
675 nbfd, name);
8ed88239
ILT
676 if (obfd != NULL)
677 einfo ("%B: warning: common is here\n", obfd);
e47bfa63 678 }
8ed88239
ILT
679 else if (otype == bfd_link_hash_defined
680 || otype == bfd_link_hash_defweak
681 || otype == bfd_link_hash_indirect)
2fa0b342 682 {
b9395be3
ILT
683 ASSERT (ntype == bfd_link_hash_common);
684 einfo ("%B: warning: common of `%T' overridden by definition\n",
685 nbfd, name);
8ed88239
ILT
686 if (obfd != NULL)
687 einfo ("%B: warning: defined here\n", obfd);
b9395be3
ILT
688 }
689 else
690 {
691 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
692 if (osize > nsize)
2fa0b342 693 {
b9395be3
ILT
694 einfo ("%B: warning: common of `%T' overridden by larger common\n",
695 nbfd, name);
8ed88239
ILT
696 if (obfd != NULL)
697 einfo ("%B: warning: larger common is here\n", obfd);
2fa0b342 698 }
b9395be3 699 else if (nsize > osize)
2fa0b342 700 {
b9395be3
ILT
701 einfo ("%B: warning: common of `%T' overriding smaller common\n",
702 nbfd, name);
8ed88239
ILT
703 if (obfd != NULL)
704 einfo ("%B: warning: smaller common is here\n", obfd);
2fa0b342 705 }
e47bfa63 706 else
2fa0b342 707 {
b9395be3 708 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
8ed88239
ILT
709 if (obfd != NULL)
710 einfo ("%B: warning: previous common is here\n", obfd);
2fa0b342
DHW
711 }
712 }
713
b9395be3 714 return true;
2fa0b342
DHW
715}
716
b9395be3
ILT
717/* This is called when BFD has discovered a set element. H is the
718 entry in the linker hash table for the set. SECTION and VALUE
719 represent a value which should be added to the set. */
2fa0b342 720
b9395be3
ILT
721/*ARGSUSED*/
722static boolean
2a9fa50c 723add_to_set (info, h, reloc, abfd, section, value)
b9395be3
ILT
724 struct bfd_link_info *info;
725 struct bfd_link_hash_entry *h;
2a9fa50c 726 bfd_reloc_code_real_type reloc;
b9395be3
ILT
727 bfd *abfd;
728 asection *section;
729 bfd_vma value;
2fa0b342 730{
7d6439d9
ILT
731 if (config.warn_constructors)
732 einfo ("%P: warning: global constructor %s used\n",
733 h->root.string);
734
2a9fa50c
ILT
735 if (! config.build_constructors)
736 return true;
737
738 ldctor_add_set_entry (h, reloc, section, value);
739
740 if (h->type == bfd_link_hash_new)
741 {
742 h->type = bfd_link_hash_undefined;
743 h->u.undef.abfd = abfd;
744 /* We don't call bfd_link_add_undef to add this to the list of
745 undefined symbols because we are going to define it
746 ourselves. */
747 }
748
b9395be3
ILT
749 return true;
750}
1418c83b 751
b9395be3
ILT
752/* This is called when BFD has discovered a constructor. This is only
753 called for some object file formats--those which do not handle
754 constructors in some more clever fashion. This is similar to
755 adding an element to a set, but less general. */
2fa0b342 756
b9395be3 757static boolean
2a9fa50c 758constructor_callback (info, constructor, name, abfd, section, value)
b9395be3
ILT
759 struct bfd_link_info *info;
760 boolean constructor;
b9395be3
ILT
761 const char *name;
762 bfd *abfd;
763 asection *section;
764 bfd_vma value;
765{
b9395be3
ILT
766 char *s;
767 struct bfd_link_hash_entry *h;
7d6439d9
ILT
768 char set_name[1 + sizeof "__CTOR_LIST__"];
769
770 if (config.warn_constructors)
771 einfo ("%P: warning: global constructor %s used\n", name);
b9395be3
ILT
772
773 if (! config.build_constructors)
774 return true;
775
2a9fa50c
ILT
776 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
777 useful error message. */
778 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL)
7d6439d9 779 einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n");
2a9fa50c 780
b9395be3
ILT
781 s = set_name;
782 if (bfd_get_symbol_leading_char (abfd) != '\0')
783 *s++ = bfd_get_symbol_leading_char (abfd);
784 if (constructor)
785 strcpy (s, "__CTOR_LIST__");
29f33467 786 else
b9395be3 787 strcpy (s, "__DTOR_LIST__");
2fa0b342 788
2a9fa50c
ILT
789 if (config.map_file != (FILE *) NULL)
790 fprintf (config.map_file,
791 "Adding %s to constructor/destructor set %s\n", name, set_name);
e47bfa63 792
b9395be3
ILT
793 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
794 if (h == (struct bfd_link_hash_entry *) NULL)
7d6439d9 795 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
b9395be3 796 if (h->type == bfd_link_hash_new)
e47bfa63 797 {
b9395be3
ILT
798 h->type = bfd_link_hash_undefined;
799 h->u.undef.abfd = abfd;
800 /* We don't call bfd_link_add_undef to add this to the list of
801 undefined symbols because we are going to define it
802 ourselves. */
2fa0b342 803 }
2fa0b342 804
2a9fa50c 805 ldctor_add_set_entry (h, BFD_RELOC_CTOR, section, value);
b9395be3 806 return true;
2fa0b342
DHW
807}
808
7d6439d9
ILT
809/* A structure used by warning_callback to pass information through
810 bfd_map_over_sections. */
811
812struct warning_callback_info
813{
814 boolean found;
815 const char *warning;
816 const char *symbol;
817 asymbol **asymbols;
818};
819
b9395be3 820/* This is called when there is a reference to a warning symbol. */
2fa0b342 821
b9395be3
ILT
822/*ARGSUSED*/
823static boolean
7d6439d9 824warning_callback (info, warning, symbol, abfd, section, address)
b9395be3
ILT
825 struct bfd_link_info *info;
826 const char *warning;
7d6439d9
ILT
827 const char *symbol;
828 bfd *abfd;
829 asection *section;
830 bfd_vma address;
2fa0b342 831{
7d6439d9
ILT
832 if (section != NULL)
833 einfo ("%C: %s\n", abfd, section, address, warning);
834 else if (abfd == NULL)
835 einfo ("%P: %s\n", warning);
836 else if (symbol == NULL)
837 einfo ("%B: %s\n", abfd, warning);
838 else
839 {
840 lang_input_statement_type *entry;
841 asymbol **asymbols;
842 struct warning_callback_info info;
843
844 /* Look through the relocs to see if we can find a plausible
845 address. */
846
847 entry = (lang_input_statement_type *) abfd->usrdata;
848 if (entry != NULL && entry->asymbols != NULL)
849 asymbols = entry->asymbols;
850 else
851 {
852 long symsize;
853 long symbol_count;
854
855 symsize = bfd_get_symtab_upper_bound (abfd);
856 if (symsize < 0)
857 einfo ("%B%F: could not read symbols: %E\n", abfd);
858 asymbols = (asymbol **) xmalloc (symsize);
859 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
860 if (symbol_count < 0)
861 einfo ("%B%F: could not read symbols: %E\n", abfd);
862 if (entry != NULL)
863 {
864 entry->asymbols = asymbols;
865 entry->symbol_count = symbol_count;
866 }
867 }
868
869 info.found = false;
870 info.warning = warning;
871 info.symbol = symbol;
872 info.asymbols = asymbols;
873 bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info);
874
875 if (! info.found)
876 einfo ("%B: %s\n", abfd, warning);
877 }
878
b9395be3 879 return true;
2fa0b342
DHW
880}
881
7d6439d9
ILT
882/* This is called by warning_callback for each section. It checks the
883 relocs of the section to see if it can find a reference to the
884 symbol which triggered the warning. If it can, it uses the reloc
885 to give an error message with a file and line number. */
886
887static void
888warning_find_reloc (abfd, sec, iarg)
889 bfd *abfd;
890 asection *sec;
891 PTR iarg;
892{
893 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
894 long relsize;
895 arelent **relpp;
896 long relcount;
897 arelent **p, **pend;
898
899 if (info->found)
900 return;
901
902 relsize = bfd_get_reloc_upper_bound (abfd, sec);
903 if (relsize < 0)
904 einfo ("%B%F: could not read relocs: %E\n", abfd);
905 if (relsize == 0)
906 return;
907
908 relpp = (arelent **) xmalloc (relsize);
909 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
910 if (relcount < 0)
911 einfo ("%B%F: could not read relocs: %E\n", abfd);
912
913 p = relpp;
914 pend = p + relcount;
915 for (; p < pend && *p != NULL; p++)
916 {
917 arelent *q = *p;
918
919 if (q->sym_ptr_ptr != NULL
920 && *q->sym_ptr_ptr != NULL
921 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
922 {
923 /* We found a reloc for the symbol we are looking for. */
924 einfo ("%C: %s\n", abfd, sec, q->address, info->warning);
925 info->found = true;
926 break;
927 }
928 }
929
930 free (relpp);
931}
932
b9395be3 933/* This is called when an undefined symbol is found. */
2fa0b342 934
b9395be3
ILT
935/*ARGSUSED*/
936static boolean
937undefined_symbol (info, name, abfd, section, address)
938 struct bfd_link_info *info;
939 const char *name;
940 bfd *abfd;
941 asection *section;
942 bfd_vma address;
943{
944 static char *error_name;
945 static unsigned int error_count;
29f33467 946
b9395be3 947#define MAX_ERRORS_IN_A_ROW 5
8a045e50 948
809ee7e0
ILT
949 if (config.warn_once)
950 {
951 static struct bfd_hash_table *hash;
952
953 /* Only warn once about a particular undefined symbol. */
954
955 if (hash == NULL)
956 {
957 hash = ((struct bfd_hash_table *)
958 xmalloc (sizeof (struct bfd_hash_table)));
959 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
960 einfo ("%F%P: bfd_hash_table_init failed: %E\n");
961 }
962
963 if (bfd_hash_lookup (hash, name, false, false) != NULL)
964 return true;
965
966 if (bfd_hash_lookup (hash, name, true, true) == NULL)
967 einfo ("%F%P: bfd_hash_lookup failed: %E\n");
968 }
969
b9395be3
ILT
970 /* We never print more than a reasonable number of errors in a row
971 for a single symbol. */
972 if (error_name != (char *) NULL
973 && strcmp (name, error_name) == 0)
974 ++error_count;
975 else
e47bfa63 976 {
b9395be3
ILT
977 error_count = 0;
978 if (error_name != (char *) NULL)
979 free (error_name);
980 error_name = buystring (name);
981 }
2fa0b342 982
23244cd6
ILT
983 if (section != NULL)
984 {
985 if (error_count < MAX_ERRORS_IN_A_ROW)
986 einfo ("%X%C: undefined reference to `%T'\n",
987 abfd, section, address, name);
988 else if (error_count == MAX_ERRORS_IN_A_ROW)
989 einfo ("%D: more undefined references to `%T' follow\n",
990 abfd, section, address, name);
991 }
992 else
993 {
994 if (error_count < MAX_ERRORS_IN_A_ROW)
995 einfo ("%X%B: undefined reference to `%T'\n",
996 abfd, name);
997 else if (error_count == MAX_ERRORS_IN_A_ROW)
998 einfo ("%B: more undefined references to `%T' follow\n",
999 abfd, name);
1000 }
2fa0b342 1001
b9395be3 1002 return true;
2fa0b342
DHW
1003}
1004
b9395be3 1005/* This is called when a reloc overflows. */
2fa0b342 1006
b9395be3 1007/*ARGSUSED*/
9f629407 1008static boolean
5dad4c97 1009reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
b9395be3 1010 struct bfd_link_info *info;
5dad4c97
ILT
1011 const char *name;
1012 const char *reloc_name;
1013 bfd_vma addend;
b9395be3
ILT
1014 bfd *abfd;
1015 asection *section;
1016 bfd_vma address;
2fa0b342 1017{
2a9fa50c
ILT
1018 if (abfd == (bfd *) NULL)
1019 einfo ("%P%X: generated");
1020 else
1021 einfo ("%X%C:", abfd, section, address);
1022 einfo (" relocation truncated to fit: %s %T", reloc_name, name);
5dad4c97
ILT
1023 if (addend != 0)
1024 einfo ("+%v", addend);
1025 einfo ("\n");
b9395be3
ILT
1026 return true;
1027}
2fa0b342 1028
b9395be3 1029/* This is called when a dangerous relocation is made. */
3e4c643d 1030
b9395be3
ILT
1031/*ARGSUSED*/
1032static boolean
1033reloc_dangerous (info, message, abfd, section, address)
1034 struct bfd_link_info *info;
1035 const char *message;
1036 bfd *abfd;
1037 asection *section;
1038 bfd_vma address;
1039{
2a9fa50c
ILT
1040 if (abfd == (bfd *) NULL)
1041 einfo ("%P%X: generated");
1042 else
1043 einfo ("%X%C:", abfd, section, address);
1044 einfo ("dangerous relocation: %s\n", message);
b9395be3
ILT
1045 return true;
1046}
973e421e 1047
b9395be3
ILT
1048/* This is called when a reloc is being generated attached to a symbol
1049 that is not being output. */
2fa0b342 1050
b9395be3
ILT
1051/*ARGSUSED*/
1052static boolean
1053unattached_reloc (info, name, abfd, section, address)
1054 struct bfd_link_info *info;
1055 const char *name;
1056 bfd *abfd;
1057 asection *section;
1058 bfd_vma address;
1059{
2a9fa50c
ILT
1060 if (abfd == (bfd *) NULL)
1061 einfo ("%P%X: generated");
1062 else
1063 einfo ("%X%C:", abfd, section, address);
1064 einfo (" reloc refers to symbol `%T' which is not being output\n", name);
b9395be3 1065 return true;
2fa0b342 1066}
8a045e50 1067
b9395be3
ILT
1068/* This is called when a symbol in notice_hash is found. Symbols are
1069 put in notice_hash using the -y option. */
1070
1071/*ARGSUSED*/
1072static boolean
1073notice_ysym (info, name, abfd, section, value)
1074 struct bfd_link_info *info;
1075 const char *name;
1076 bfd *abfd;
1077 asection *section;
1078 bfd_vma value;
8a045e50 1079{
b9395be3 1080 einfo ("%B: %s %s\n", abfd,
cc23cc69 1081 bfd_is_und_section (section) ? "reference to" : "definition of",
b9395be3
ILT
1082 name);
1083 return true;
8a045e50 1084}
This page took 0.206676 seconds and 4 git commands to generate.