powerpc common-page-size
[deliverable/binutils-gdb.git] / ld / ldmain.c
CommitLineData
252b5132 1/* Main program of GNU linker.
219d1afa 2 Copyright (C) 1991-2018 Free Software Foundation, Inc.
252b5132
RH
3 Written by Steve Chamberlain steve@cygnus.com
4
f96b4a7b 5 This file is part of the GNU Binutils.
252b5132 6
f96b4a7b 7 This program is free software; you can redistribute it and/or modify
2fa9fc65 8 it under the terms of the GNU General Public License as published by
f96b4a7b
NC
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
252b5132 11
f96b4a7b 12 This program is distributed in the hope that it will be useful,
2fa9fc65
NC
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
2fa9fc65 17 You should have received a copy of the GNU General Public License
f96b4a7b
NC
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132 21
252b5132 22#include "sysdep.h"
3db64b00 23#include "bfd.h"
3882b010 24#include "safe-ctype.h"
252b5132
RH
25#include "libiberty.h"
26#include "progress.h"
27#include "bfdlink.h"
5af11cab 28#include "filenames.h"
252b5132
RH
29
30#include "ld.h"
31#include "ldmain.h"
32#include "ldmisc.h"
33#include "ldwrite.h"
252b5132
RH
34#include "ldexp.h"
35#include "ldlang.h"
df2a7313 36#include <ldgram.h>
252b5132
RH
37#include "ldlex.h"
38#include "ldfile.h"
b71e2778 39#include "ldemul.h"
252b5132 40#include "ldctor.h"
5d3236ee
DK
41#ifdef ENABLE_PLUGINS
42#include "plugin.h"
43#include "plugin-api.h"
5d3236ee 44#endif /* ENABLE_PLUGINS */
252b5132 45
0125cdf2 46/* Somewhere above, sys/stat.h got included. */
252b5132
RH
47#if !defined(S_ISDIR) && defined(S_IFDIR)
48#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
49#endif
50
51#include <string.h>
52
9c8ebd6a
DJ
53#ifndef TARGET_SYSTEM_ROOT
54#define TARGET_SYSTEM_ROOT ""
55#endif
56
252b5132
RH
57/* EXPORTS */
58
53d25da6
AM
59FILE *saved_script_handle = NULL;
60FILE *previous_script_handle = NULL;
61bfd_boolean force_make_executable = FALSE;
62
252b5132
RH
63char *default_target;
64const char *output_filename = "a.out";
65
66/* Name this program was invoked by. */
67char *program_name;
68
9c8ebd6a 69/* The prefix for system library directories. */
e2243057 70const char *ld_sysroot;
9c8ebd6a 71
3fe38064 72/* The canonical representation of ld_sysroot. */
0aa7f586 73char *ld_canon_sysroot;
3fe38064
NC
74int ld_canon_sysroot_len;
75
e8044f35 76/* Set by -G argument, for targets like MIPS ELF. */
252b5132
RH
77int g_switch_value = 8;
78
79/* Nonzero means print names of input files as processed. */
b34976b6 80bfd_boolean trace_files;
252b5132 81
cd6f1cf3
NC
82/* Nonzero means report actions taken by the linker, and describe the linker script in use. */
83bfd_boolean verbose;
252b5132
RH
84
85/* Nonzero means version number was printed, so exit successfully
86 instead of complaining if no input files are given. */
b34976b6 87bfd_boolean version_printed;
252b5132 88
b34976b6
AM
89/* TRUE if we should demangle symbol names. */
90bfd_boolean demangling;
252b5132
RH
91
92args_type command_line;
93
94ld_config_type config;
95
bcaa7b3e
L
96sort_type sort_section;
97
e2243057
RS
98static const char *get_sysroot
99 (int, char **);
0125cdf2 100static char *get_emulation
1579bae1 101 (int, char **);
0125cdf2 102static bfd_boolean add_archive_element
5d3236ee 103 (struct bfd_link_info *, bfd *, const char *, bfd **);
1a72702b 104static void multiple_definition
24f58f47 105 (struct bfd_link_info *, struct bfd_link_hash_entry *,
1579bae1 106 bfd *, asection *, bfd_vma);
1a72702b 107static void multiple_common
24f58f47
AM
108 (struct bfd_link_info *, struct bfd_link_hash_entry *,
109 bfd *, enum bfd_link_hash_type, bfd_vma);
1a72702b 110static void add_to_set
1579bae1
AM
111 (struct bfd_link_info *, struct bfd_link_hash_entry *,
112 bfd_reloc_code_real_type, bfd *, asection *, bfd_vma);
1a72702b 113static void constructor_callback
1579bae1
AM
114 (struct bfd_link_info *, bfd_boolean, const char *, bfd *,
115 asection *, bfd_vma);
1a72702b 116static void warning_callback
1579bae1
AM
117 (struct bfd_link_info *, const char *, const char *, bfd *,
118 asection *, bfd_vma);
0125cdf2 119static void warning_find_reloc
1579bae1 120 (bfd *, asection *, void *);
1a72702b 121static void undefined_symbol
1579bae1
AM
122 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
123 bfd_boolean);
1a72702b 124static void reloc_overflow
dfeffb9f
L
125 (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
126 const char *, bfd_vma, bfd *, asection *, bfd_vma);
1a72702b 127static void reloc_dangerous
1579bae1 128 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
1a72702b 129static void unattached_reloc
1579bae1 130 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
0125cdf2 131static bfd_boolean notice
35ed3f94 132 (struct bfd_link_info *, struct bfd_link_hash_entry *,
46135103 133 struct bfd_link_hash_entry *, bfd *, asection *, bfd_vma, flagword);
0125cdf2
NC
134
135static struct bfd_link_callbacks link_callbacks =
136{
252b5132
RH
137 add_archive_element,
138 multiple_definition,
139 multiple_common,
140 add_to_set,
141 constructor_callback,
142 warning_callback,
143 undefined_symbol,
144 reloc_overflow,
145 reloc_dangerous,
146 unattached_reloc,
e1fffbe6 147 notice,
2889e75b 148 einfo,
49fa1e15
AM
149 info_msg,
150 minfo,
2889e75b 151 ldlang_override_segment_assignment
252b5132
RH
152};
153
3b66a141 154static bfd_assert_handler_type default_bfd_assert_handler;
4519d071 155static bfd_error_handler_type default_bfd_error_handler;
3b66a141 156
252b5132
RH
157struct bfd_link_info link_info;
158\f
159static void
498cd2a0 160ld_cleanup (void)
252b5132 161{
498cd2a0
AM
162 bfd_cache_close_all ();
163#ifdef ENABLE_PLUGINS
164 plugin_call_cleanup ();
165#endif
166 if (output_filename && delete_output_file_on_failure)
167 unlink_if_ordinary (output_filename);
252b5132
RH
168}
169
4519d071 170/* Hook to notice BFD assertions. */
3b66a141
HPN
171
172static void
173ld_bfd_assert_handler (const char *fmt, const char *bfdver,
174 const char *file, int line)
175{
3b66a141 176 config.make_executable = FALSE;
4519d071
AM
177 (*default_bfd_assert_handler) (fmt, bfdver, file, line);
178}
179
180/* Hook the bfd error/warning handler for --fatal-warnings. */
181
182static void
183ld_bfd_error_handler (const char *fmt, va_list ap)
184{
185 if (config.fatal_warnings)
186 config.make_executable = FALSE;
187 (*default_bfd_error_handler) (fmt, ap);
3b66a141
HPN
188}
189
252b5132 190int
1579bae1 191main (int argc, char **argv)
252b5132
RH
192{
193 char *emulation;
194 long start_time = get_run_time ();
195
196#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
197 setlocale (LC_MESSAGES, "");
3882b010
L
198#endif
199#if defined (HAVE_SETLOCALE)
200 setlocale (LC_CTYPE, "");
252b5132
RH
201#endif
202 bindtextdomain (PACKAGE, LOCALEDIR);
203 textdomain (PACKAGE);
204
205 program_name = argv[0];
206 xmalloc_set_program_name (program_name);
207
208 START_PROGRESS (program_name, 0);
209
869b9d07
MM
210 expandargv (&argc, &argv);
211
252b5132
RH
212 bfd_init ();
213
214 bfd_set_error_program_name (program_name);
215
3b66a141
HPN
216 /* We want to notice and fail on those nasty BFD assertions which are
217 likely to signal incorrect output being generated but otherwise may
218 leave no trace. */
219 default_bfd_assert_handler = bfd_set_assert_handler (ld_bfd_assert_handler);
220
4519d071
AM
221 /* Also hook the bfd error/warning handler for --fatal-warnings. */
222 default_bfd_error_handler = bfd_set_error_handler (ld_bfd_error_handler);
223
498cd2a0 224 xatexit (ld_cleanup);
252b5132 225
e2243057
RS
226 /* Set up the sysroot directory. */
227 ld_sysroot = get_sysroot (argc, argv);
228 if (*ld_sysroot)
3074964f 229 ld_canon_sysroot = lrealpath (ld_sysroot);
3fe38064 230 if (ld_canon_sysroot)
889be5db
DR
231 {
232 ld_canon_sysroot_len = strlen (ld_canon_sysroot);
233
234 /* is_sysrooted_pathname() relies on no trailing dirsep. */
235 if (ld_canon_sysroot_len > 0
236 && IS_DIR_SEPARATOR (ld_canon_sysroot [ld_canon_sysroot_len - 1]))
237 ld_canon_sysroot [--ld_canon_sysroot_len] = '\0';
238 }
3fe38064
NC
239 else
240 ld_canon_sysroot_len = -1;
241
252b5132
RH
242 /* Set the default BFD target based on the configured target. Doing
243 this permits the linker to be configured for a particular target,
244 and linked against a shared BFD library which was configured for
245 a different target. The macro TARGET is defined by Makefile. */
0aa7f586 246 if (!bfd_set_default_target (TARGET))
252b5132
RH
247 {
248 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
249 xexit (1);
250 }
251
b47c4208
AM
252#if YYDEBUG
253 {
254 extern int yydebug;
255 yydebug = 1;
256 }
257#endif
258
b34976b6 259 config.build_constructors = TRUE;
c76308d2 260 config.rpath_separator = ':';
a854a4a7
AM
261 config.split_by_reloc = (unsigned) -1;
262 config.split_by_file = (bfd_size_type) -1;
3b95049e
AM
263 config.make_executable = TRUE;
264 config.magic_demand_paged = TRUE;
265 config.text_read_only = TRUE;
4f9d22a0 266 link_info.disable_target_specific_optimizations = -1;
3b95049e 267
b34976b6 268 command_line.warn_mismatch = TRUE;
fe7929ce 269 command_line.warn_search_mismatch = TRUE;
02b0b1aa 270 command_line.check_section_addresses = -1;
bcaa7b3e 271
252b5132
RH
272 /* We initialize DEMANGLING based on the environment variable
273 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
274 output of the linker, unless COLLECT_NO_DEMANGLE is set in the
275 environment. Acting the same way here lets us provide the same
276 interface by default. */
277 demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
278
b34976b6 279 link_info.allow_undefined_version = TRUE;
b5067602 280 link_info.keep_memory = TRUE;
b5067602 281 link_info.combreloc = TRUE;
d5cd3933 282 link_info.strip_discarded = TRUE;
2760f24c
RG
283 link_info.emit_hash = DEFAULT_EMIT_SYSV_HASH;
284 link_info.emit_gnu_hash = DEFAULT_EMIT_GNU_HASH;
a9a4c53e
AM
285 link_info.callbacks = &link_callbacks;
286 link_info.input_bfds_tail = &link_info.input_bfds;
3dbf70a2
MM
287 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
288 and _fini symbols. We are compatible. */
289 link_info.init_function = "_init";
290 link_info.fini_function = "_fini";
a9a4c53e 291 link_info.relax_pass = 1;
889c2a67 292 link_info.extern_protected_data = -1;
aec6b87e 293 link_info.dynamic_undefined_weak = -1;
e0076ab3 294 link_info.pei386_auto_import = -1;
db6751f2 295 link_info.spare_dynamic_tags = 5;
967928e9 296 link_info.path_separator = ':';
6c3bc0f8
NC
297#ifdef DEFAULT_FLAG_COMPRESS_DEBUG
298 link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
299#endif
6734f10a 300#ifdef DEFAULT_NEW_DTAGS
91001320 301 link_info.new_dtags = DEFAULT_NEW_DTAGS;
6734f10a 302#endif
24718e3b 303
252b5132 304 ldfile_add_arch ("");
252b5132
RH
305 emulation = get_emulation (argc, argv);
306 ldemul_choose_mode (emulation);
742aeb63 307 default_target = ldemul_choose_target (argc, argv);
252b5132 308 lang_init ();
18d6a79d 309 ldexp_init ();
252b5132 310 ldemul_before_parse ();
b34976b6 311 lang_has_input_file = FALSE;
252b5132
RH
312 parse_args (argc, argv);
313
2d643429
NC
314 if (config.hash_table_size != 0)
315 bfd_hash_set_default_size (config.hash_table_size);
316
9bbc1a67
AM
317#ifdef ENABLE_PLUGINS
318 /* Now all the plugin arguments have been gathered, we can load them. */
d82184d7 319 plugin_load_plugins ();
9bbc1a67 320#endif /* ENABLE_PLUGINS */
36af4a4e 321
9bbc1a67 322 ldemul_set_symbols ();
252b5132 323
14be8564
L
324 /* If we have not already opened and parsed a linker script,
325 try the default script from command line first. */
326 if (saved_script_handle == NULL
327 && command_line.default_script != NULL)
328 {
329 ldfile_open_command_file (command_line.default_script);
330 parser_input = input_script;
331 yyparse ();
332 }
333
b9a8de1e
NC
334 /* If we have not already opened and parsed a linker script
335 read the emulation's appropriate default script. */
b90e1c6f 336 if (saved_script_handle == NULL)
252b5132 337 {
252b5132 338 int isfile;
b7a26f91 339 char *s = ldemul_get_script (&isfile);
252b5132
RH
340
341 if (isfile)
7d24f02c 342 ldfile_open_default_command_file (s);
252b5132 343 else
b7a26f91 344 {
252b5132 345 lex_string = s;
dab69f68 346 lex_redirect (s, _("built in linker script"), 1);
252b5132
RH
347 }
348 parser_input = input_script;
349 yyparse ();
350 lex_string = NULL;
351 }
352
cd6f1cf3 353 if (verbose)
b9a8de1e 354 {
9cb61eab
AM
355 if (saved_script_handle)
356 info_msg (_("using external linker script:"));
357 else
358 info_msg (_("using internal linker script:"));
359 info_msg ("\n==================================================\n");
b9a8de1e
NC
360
361 if (saved_script_handle)
362 {
74699268 363 static const int ld_bufsz = 8193;
b9a8de1e 364 size_t n;
1e9cc1c2 365 char *buf = (char *) xmalloc (ld_bufsz);
b9a8de1e
NC
366
367 rewind (saved_script_handle);
74699268 368 while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
b9a8de1e 369 {
b7a26f91 370 buf[n] = 0;
f5ac6ab3 371 info_msg ("%s", buf);
b9a8de1e
NC
372 }
373 rewind (saved_script_handle);
374 free (buf);
375 }
376 else
377 {
378 int isfile;
379
b7a26f91 380 info_msg (ldemul_get_script (&isfile));
b9a8de1e 381 }
b7a26f91 382
b9a8de1e
NC
383 info_msg ("\n==================================================\n");
384 }
385
7bdf4127
AB
386 if (command_line.force_group_allocation
387 || !bfd_link_relocatable (&link_info))
388 link_info.resolve_section_groups = TRUE;
389 else
390 link_info.resolve_section_groups = FALSE;
391
30824704
RM
392 if (command_line.print_output_format)
393 info_msg ("%s\n", lang_get_output_target ());
394
252b5132
RH
395 lang_final ();
396
61e2488c
JT
397 /* If the only command line argument has been -v or --version or --verbose
398 then ignore any input files provided by linker scripts and exit now.
399 We do not want to create an output file when the linker is just invoked
400 to provide version information. */
401 if (argc == 2 && version_printed)
402 xexit (0);
403
a4819f54 404 if (link_info.inhibit_common_definition && !bfd_link_dll (&link_info))
df5f2391 405 einfo (_("%F%P: --no-define-common may not be used without -shared\n"));
a4819f54 406
b34976b6 407 if (!lang_has_input_file)
252b5132 408 {
30824704 409 if (version_printed || command_line.print_output_format)
252b5132 410 xexit (0);
df5f2391 411 einfo (_("%F%P: no input files\n"));
252b5132
RH
412 }
413
414 if (trace_files)
0125cdf2 415 info_msg (_("%P: mode %s\n"), emulation);
252b5132
RH
416
417 ldemul_after_parse ();
418
252b5132
RH
419 if (config.map_filename)
420 {
421 if (strcmp (config.map_filename, "-") == 0)
422 {
423 config.map_file = stdout;
424 }
425 else
426 {
427 config.map_file = fopen (config.map_filename, FOPEN_WT);
428 if (config.map_file == (FILE *) NULL)
429 {
430 bfd_set_error (bfd_error_system_call);
df5f2391 431 einfo (_("%F%P: cannot open map file %s: %E\n"),
252b5132
RH
432 config.map_filename);
433 }
434 }
435 }
436
252b5132
RH
437 lang_process ();
438
439 /* Print error messages for any missing symbols, for any warning
6d5e62f8 440 symbols, and possibly multiple definitions. */
0e1862bb 441 if (bfd_link_relocatable (&link_info))
f13a99db 442 link_info.output_bfd->flags &= ~EXEC_P;
252b5132 443 else
f13a99db 444 link_info.output_bfd->flags |= EXEC_P;
252b5132 445
0ce398f1
L
446 if ((link_info.compress_debug & COMPRESS_DEBUG))
447 {
448 link_info.output_bfd->flags |= BFD_COMPRESS;
449 if (link_info.compress_debug == COMPRESS_DEBUG_GABI_ZLIB)
450 link_info.output_bfd->flags |= BFD_COMPRESS_GABI;
451 }
452
252b5132
RH
453 ldwrite ();
454
455 if (config.map_file != NULL)
456 lang_map ();
457 if (command_line.cref)
458 output_cref (config.map_file != NULL ? config.map_file : stdout);
459 if (nocrossref_list != NULL)
460 check_nocrossrefs ();
3604cb1f
TG
461 if (command_line.print_memory_usage)
462 lang_print_memory_usage ();
edf84efc
NC
463#if 0
464 {
0aa7f586 465 struct bfd_link_hash_entry *h;
252b5132 466
edf84efc
NC
467 h = bfd_link_hash_lookup (link_info.hash, "__image_base__", 0,0,1);
468 fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1);
469 }
470#endif
18d6a79d 471 ldexp_finish ();
750877ba
L
472 lang_finish ();
473
1049f94e 474 /* Even if we're producing relocatable output, some non-fatal errors should
252b5132 475 be reported in the exit status. (What non-fatal errors, if any, do we
1049f94e 476 want to ignore for relocatable output?) */
b34976b6 477 if (!config.make_executable && !force_make_executable)
252b5132 478 {
b34976b6 479 if (trace_files)
0125cdf2
NC
480 einfo (_("%P: link errors found, deleting executable `%s'\n"),
481 output_filename);
252b5132 482
4309cbf2 483 /* The file will be removed by ld_cleanup. */
252b5132
RH
484 xexit (1);
485 }
486 else
487 {
0aa7f586 488 if (!bfd_close (link_info.output_bfd))
df5f2391 489 einfo (_("%F%P: %pB: final close failed: %E\n"), link_info.output_bfd);
252b5132
RH
490
491 /* If the --force-exe-suffix is enabled, and we're making an
6d5e62f8
KH
492 executable file and it doesn't end in .exe, copy it to one
493 which does. */
0e1862bb
L
494 if (!bfd_link_relocatable (&link_info)
495 && command_line.force_exe_suffix)
252b5132
RH
496 {
497 int len = strlen (output_filename);
0125cdf2 498
6d5e62f8 499 if (len < 4
252b5132
RH
500 || (strcasecmp (output_filename + len - 4, ".exe") != 0
501 && strcasecmp (output_filename + len - 4, ".dll") != 0))
502 {
503 FILE *src;
504 FILE *dst;
505 const int bsize = 4096;
1e9cc1c2 506 char *buf = (char *) xmalloc (bsize);
252b5132 507 int l;
1e9cc1c2 508 char *dst_name = (char *) xmalloc (len + 5);
0125cdf2 509
252b5132
RH
510 strcpy (dst_name, output_filename);
511 strcat (dst_name, ".exe");
512 src = fopen (output_filename, FOPEN_RB);
513 dst = fopen (dst_name, FOPEN_WB);
514
515 if (!src)
df5f2391 516 einfo (_("%F%P: unable to open for source of copy `%s'\n"),
1579bae1 517 output_filename);
252b5132 518 if (!dst)
df5f2391 519 einfo (_("%F%P: unable to open for destination of copy `%s'\n"),
1579bae1 520 dst_name);
252b5132
RH
521 while ((l = fread (buf, 1, bsize, src)) > 0)
522 {
523 int done = fwrite (buf, 1, l, dst);
0125cdf2 524
252b5132 525 if (done != l)
df5f2391 526 einfo (_("%P: error writing file `%s'\n"), dst_name);
252b5132 527 }
0125cdf2 528
252b5132
RH
529 fclose (src);
530 if (fclose (dst) == EOF)
df5f2391 531 einfo (_("%P: error closing file `%s'\n"), dst_name);
252b5132
RH
532 free (dst_name);
533 free (buf);
534 }
535 }
536 }
537
538 END_PROGRESS (program_name);
539
540 if (config.stats)
541 {
252b5132
RH
542 long run_time = get_run_time () - start_time;
543
e922bcab 544 fflush (stdout);
252b5132
RH
545 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
546 program_name, run_time / 1000000, run_time % 1000000);
e922bcab 547 fflush (stderr);
252b5132
RH
548 }
549
4309cbf2 550 /* Prevent ld_cleanup from doing anything, after a successful link. */
252b5132
RH
551 output_filename = NULL;
552
553 xexit (0);
554 return 0;
555}
556
e2243057
RS
557/* If the configured sysroot is relocatable, try relocating it based on
558 default prefix FROM. Return the relocated directory if it exists,
559 otherwise return null. */
560
561static char *
562get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
563{
564#ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
565 char *path;
566 struct stat s;
567
568 path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
569 if (path)
570 {
571 if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
572 return path;
573 free (path);
574 }
575#endif
576 return 0;
577}
578
579/* Return the sysroot directory. Return "" if no sysroot is being used. */
580
581static const char *
582get_sysroot (int argc, char **argv)
583{
584 int i;
585 const char *path;
586
587 for (i = 1; i < argc; i++)
0112cd26 588 if (CONST_STRNEQ (argv[i], "--sysroot="))
e2243057
RS
589 return argv[i] + strlen ("--sysroot=");
590
591 path = get_relative_sysroot (BINDIR);
592 if (path)
593 return path;
594
595 path = get_relative_sysroot (TOOLBINDIR);
596 if (path)
597 return path;
598
599 return TARGET_SYSTEM_ROOT;
600}
601
252b5132
RH
602/* We need to find any explicitly given emulation in order to initialize the
603 state that's needed by the lex&yacc argument parser (parse_args). */
604
605static char *
1579bae1 606get_emulation (int argc, char **argv)
252b5132
RH
607{
608 char *emulation;
609 int i;
610
611 emulation = getenv (EMULATION_ENVIRON);
612 if (emulation == NULL)
613 emulation = DEFAULT_EMULATION;
614
615 for (i = 1; i < argc; i++)
616 {
0112cd26 617 if (CONST_STRNEQ (argv[i], "-m"))
252b5132
RH
618 {
619 if (argv[i][2] == '\0')
620 {
621 /* -m EMUL */
622 if (i < argc - 1)
623 {
624 emulation = argv[i + 1];
625 i++;
626 }
627 else
df5f2391 628 einfo (_("%F%P: missing argument to -m\n"));
252b5132
RH
629 }
630 else if (strcmp (argv[i], "-mips1") == 0
631 || strcmp (argv[i], "-mips2") == 0
632 || strcmp (argv[i], "-mips3") == 0
d1cf510e 633 || strcmp (argv[i], "-mips4") == 0
6f33421c
CD
634 || strcmp (argv[i], "-mips5") == 0
635 || strcmp (argv[i], "-mips32") == 0
636 || strcmp (argv[i], "-mips32r2") == 0
7361da2c 637 || strcmp (argv[i], "-mips32r6") == 0
5f74bc13 638 || strcmp (argv[i], "-mips64") == 0
7361da2c
AB
639 || strcmp (argv[i], "-mips64r2") == 0
640 || strcmp (argv[i], "-mips64r6") == 0)
252b5132 641 {
31d677f9 642 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
252b5132
RH
643 passed to the linker by some MIPS compilers. They
644 generally tell the linker to use a slightly different
645 library path. Perhaps someday these should be
646 implemented as emulations; until then, we just ignore
647 the arguments and hope that nobody ever creates
648 emulations named ips1, ips2 or ips3. */
649 }
650 else if (strcmp (argv[i], "-m486") == 0)
651 {
652 /* FIXME: The argument -m486 is passed to the linker on
653 some Linux systems. Hope that nobody creates an
654 emulation named 486. */
655 }
656 else
657 {
658 /* -mEMUL */
659 emulation = &argv[i][2];
660 }
661 }
662 }
663
664 return emulation;
665}
666
252b5132 667void
1579bae1 668add_ysym (const char *name)
252b5132 669{
1579bae1 670 if (link_info.notice_hash == NULL)
252b5132 671 {
0aa7f586
AM
672 link_info.notice_hash
673 = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
66eb6687
AM
674 if (!bfd_hash_table_init_n (link_info.notice_hash,
675 bfd_hash_newfunc,
676 sizeof (struct bfd_hash_entry),
677 61))
df5f2391 678 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
6d5e62f8 679 }
252b5132 680
1579bae1 681 if (bfd_hash_lookup (link_info.notice_hash, name, TRUE, TRUE) == NULL)
df5f2391 682 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
252b5132
RH
683}
684
0e86e20e
AM
685void
686add_ignoresym (struct bfd_link_info *info, const char *name)
687{
688 if (info->ignore_hash == NULL)
689 {
690 info->ignore_hash = xmalloc (sizeof (struct bfd_hash_table));
0aa7f586
AM
691 if (!bfd_hash_table_init_n (info->ignore_hash,
692 bfd_hash_newfunc,
693 sizeof (struct bfd_hash_entry),
694 61))
df5f2391 695 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
0e86e20e
AM
696 }
697
698 if (bfd_hash_lookup (info->ignore_hash, name, TRUE, TRUE) == NULL)
df5f2391 699 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
0e86e20e
AM
700}
701
252b5132
RH
702/* Record a symbol to be wrapped, from the --wrap option. */
703
704void
1579bae1 705add_wrap (const char *name)
252b5132
RH
706{
707 if (link_info.wrap_hash == NULL)
708 {
0aa7f586
AM
709 link_info.wrap_hash
710 = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
66eb6687
AM
711 if (!bfd_hash_table_init_n (link_info.wrap_hash,
712 bfd_hash_newfunc,
713 sizeof (struct bfd_hash_entry),
714 61))
df5f2391 715 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
252b5132 716 }
0125cdf2 717
b34976b6 718 if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL)
df5f2391 719 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
252b5132
RH
720}
721
722/* Handle the -retain-symbols-file option. */
723
724void
1579bae1 725add_keepsyms_file (const char *filename)
252b5132
RH
726{
727 FILE *file;
728 char *buf;
729 size_t bufsize;
730 int c;
731
732 if (link_info.strip == strip_some)
733 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
734
735 file = fopen (filename, "r");
1579bae1 736 if (file == NULL)
252b5132
RH
737 {
738 bfd_set_error (bfd_error_system_call);
739 einfo ("%X%P: %s: %E\n", filename);
740 return;
741 }
742
1e9cc1c2
NC
743 link_info.keep_hash = (struct bfd_hash_table *)
744 xmalloc (sizeof (struct bfd_hash_table));
66eb6687
AM
745 if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc,
746 sizeof (struct bfd_hash_entry)))
df5f2391 747 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
252b5132
RH
748
749 bufsize = 100;
1e9cc1c2 750 buf = (char *) xmalloc (bufsize);
252b5132
RH
751
752 c = getc (file);
753 while (c != EOF)
754 {
3882b010 755 while (ISSPACE (c))
252b5132
RH
756 c = getc (file);
757
758 if (c != EOF)
759 {
760 size_t len = 0;
761
0aa7f586 762 while (!ISSPACE (c) && c != EOF)
252b5132
RH
763 {
764 buf[len] = c;
765 ++len;
766 if (len >= bufsize)
767 {
768 bufsize *= 2;
1e9cc1c2 769 buf = (char *) xrealloc (buf, bufsize);
252b5132
RH
770 }
771 c = getc (file);
772 }
773
774 buf[len] = '\0';
775
1579bae1 776 if (bfd_hash_lookup (link_info.keep_hash, buf, TRUE, TRUE) == NULL)
df5f2391 777 einfo (_("%F%P: bfd_hash_lookup for insertion failed: %E\n"));
252b5132
RH
778 }
779 }
780
781 if (link_info.strip != strip_none)
782 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
783
5ed6aba4 784 free (buf);
252b5132 785 link_info.strip = strip_some;
d9313f4f 786 fclose (file);
252b5132
RH
787}
788\f
789/* Callbacks from the BFD linker routines. */
790
791/* This is called when BFD has decided to include an archive member in
792 a link. */
793
b34976b6 794static bfd_boolean
e1fffbe6 795add_archive_element (struct bfd_link_info *info,
1579bae1 796 bfd *abfd,
5d3236ee
DK
797 const char *name,
798 bfd **subsbfd ATTRIBUTE_UNUSED)
252b5132
RH
799{
800 lang_input_statement_type *input;
1fa4ec6a 801 lang_input_statement_type *parent;
5d3236ee 802 lang_input_statement_type orig_input;
252b5132 803
1e9cc1c2
NC
804 input = (lang_input_statement_type *)
805 xcalloc (1, sizeof (lang_input_statement_type));
fb47deda 806 input->header.type = lang_input_statement_enum;
252b5132
RH
807 input->filename = abfd->filename;
808 input->local_sym_name = abfd->filename;
809 input->the_bfd = abfd;
252b5132 810
1fa4ec6a
AM
811 parent = abfd->my_archive->usrdata;
812 if (parent != NULL && !parent->flags.reload)
813 parent->next = (lang_statement_union_type *) input;
814
5d3236ee
DK
815 /* Save the original data for trace files/tries below, as plugins
816 (if enabled) may possibly alter it to point to a replacement
817 BFD, but we still want to output the original BFD filename. */
818 orig_input = *input;
819#ifdef ENABLE_PLUGINS
b95a0a31 820 if (link_info.lto_plugin_active)
5d3236ee
DK
821 {
822 /* We must offer this archive member to the plugins to claim. */
35a1e5f3
L
823 plugin_maybe_claim (input);
824 if (input->flags.claimed)
5d3236ee 825 {
b95a0a31
L
826 if (no_more_claiming)
827 {
828 /* Don't claim new IR symbols after all IR symbols have
829 been claimed. */
830 if (trace_files || verbose)
c1c8c1ef 831 info_msg ("%pI: no new IR symbols to claimi\n",
b95a0a31
L
832 &orig_input);
833 input->flags.claimed = 0;
834 return FALSE;
835 }
35a1e5f3
L
836 input->flags.claim_archive = TRUE;
837 *subsbfd = input->the_bfd;
5d3236ee
DK
838 }
839 }
840#endif /* ENABLE_PLUGINS */
841
252b5132
RH
842 ldlang_add_file (input);
843
1579bae1 844 if (config.map_file != NULL)
252b5132 845 {
b34976b6 846 static bfd_boolean header_printed;
252b5132
RH
847 struct bfd_link_hash_entry *h;
848 bfd *from;
849 int len;
850
e1fffbe6 851 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
252b5132
RH
852
853 if (h == NULL)
854 from = NULL;
855 else
856 {
857 switch (h->type)
858 {
859 default:
860 from = NULL;
861 break;
862
863 case bfd_link_hash_defined:
864 case bfd_link_hash_defweak:
865 from = h->u.def.section->owner;
866 break;
867
868 case bfd_link_hash_undefined:
869 case bfd_link_hash_undefweak:
870 from = h->u.undef.abfd;
871 break;
872
873 case bfd_link_hash_common:
874 from = h->u.c.p->section->owner;
875 break;
876 }
877 }
878
0aa7f586 879 if (!header_printed)
252b5132 880 {
1273da04 881 minfo (_("Archive member included to satisfy reference by file (symbol)\n\n"));
b34976b6 882 header_printed = TRUE;
252b5132
RH
883 }
884
3860d2b4
AM
885 if (abfd->my_archive == NULL
886 || bfd_is_thin_archive (abfd->my_archive))
252b5132
RH
887 {
888 minfo ("%s", bfd_get_filename (abfd));
889 len = strlen (bfd_get_filename (abfd));
890 }
891 else
892 {
3860d2b4 893 minfo ("%s(%s)", bfd_get_filename (abfd->my_archive),
252b5132 894 bfd_get_filename (abfd));
3860d2b4 895 len = (strlen (bfd_get_filename (abfd->my_archive))
252b5132
RH
896 + strlen (bfd_get_filename (abfd))
897 + 2);
898 }
899
900 if (len >= 29)
901 {
902 print_nl ();
903 len = 0;
904 }
905 while (len < 30)
906 {
907 print_space ();
908 ++len;
909 }
910
911 if (from != NULL)
871b3ab2 912 minfo ("%pB ", from);
252b5132 913 if (h != NULL)
c1c8c1ef 914 minfo ("(%pT)\n", h->root.string);
252b5132
RH
915 else
916 minfo ("(%s)\n", name);
917 }
918
cd6f1cf3 919 if (trace_files || verbose)
c1c8c1ef 920 info_msg ("%pI\n", &orig_input);
b34976b6 921 return TRUE;
252b5132
RH
922}
923
924/* This is called when BFD has discovered a symbol which is defined
925 multiple times. */
926
1a72702b 927static void
24f58f47
AM
928multiple_definition (struct bfd_link_info *info,
929 struct bfd_link_hash_entry *h,
1579bae1
AM
930 bfd *nbfd,
931 asection *nsec,
932 bfd_vma nval)
252b5132 933{
24f58f47
AM
934 const char *name;
935 bfd *obfd;
936 asection *osec;
937 bfd_vma oval;
938
939 if (info->allow_multiple_definition)
1a72702b 940 return;
24f58f47
AM
941
942 switch (h->type)
943 {
944 case bfd_link_hash_defined:
945 osec = h->u.def.section;
946 oval = h->u.def.value;
947 obfd = h->u.def.section->owner;
948 break;
949 case bfd_link_hash_indirect:
950 osec = bfd_ind_section_ptr;
951 oval = 0;
952 obfd = NULL;
953 break;
954 default:
955 abort ();
956 }
957
958 /* Ignore a redefinition of an absolute symbol to the
959 same value; it's harmless. */
960 if (h->type == bfd_link_hash_defined
961 && bfd_is_abs_section (osec)
962 && bfd_is_abs_section (nsec)
963 && nval == oval)
1a72702b 964 return;
24f58f47 965
252b5132
RH
966 /* If either section has the output_section field set to
967 bfd_abs_section_ptr, it means that the section is being
968 discarded, and this is not really a multiple definition at all.
9e2278f5
AM
969 FIXME: It would be cleaner to somehow ignore symbols defined in
970 sections which are being discarded. */
252b5132 971 if ((osec->output_section != NULL
0aa7f586 972 && !bfd_is_abs_section (osec)
252b5132
RH
973 && bfd_is_abs_section (osec->output_section))
974 || (nsec->output_section != NULL
0aa7f586 975 && !bfd_is_abs_section (nsec)
252b5132 976 && bfd_is_abs_section (nsec->output_section)))
1a72702b 977 return;
252b5132 978
24f58f47
AM
979 name = h->root.string;
980 if (nbfd == NULL)
981 {
982 nbfd = obfd;
983 nsec = osec;
984 nval = oval;
985 obfd = NULL;
986 }
df5f2391 987 einfo (_("%X%P: %C: multiple definition of `%pT'"),
252b5132 988 nbfd, nsec, nval, name);
1579bae1 989 if (obfd != NULL)
df5f2391
AM
990 einfo (_("; %D: first defined here"), obfd, osec, oval);
991 einfo ("\n");
9b14b192 992
13761a11 993 if (RELAXATION_ENABLED_BY_USER)
9b14b192 994 {
df5f2391 995 einfo (_("%P: disabling relaxation; it will not work with multiple definitions\n"));
13761a11 996 DISABLE_RELAXATION;
9b14b192 997 }
252b5132
RH
998}
999
1000/* This is called when there is a definition of a common symbol, or
1001 when a common symbol is found for a symbol that is already defined,
1002 or when two common symbols are found. We only do something if
1003 -warn-common was used. */
1004
1a72702b 1005static void
1579bae1 1006multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
24f58f47 1007 struct bfd_link_hash_entry *h,
1579bae1
AM
1008 bfd *nbfd,
1009 enum bfd_link_hash_type ntype,
1010 bfd_vma nsize)
252b5132 1011{
24f58f47
AM
1012 const char *name;
1013 bfd *obfd;
1014 enum bfd_link_hash_type otype;
1015 bfd_vma osize;
1016
1017 if (!config.warn_common)
1a72702b 1018 return;
252b5132 1019
24f58f47
AM
1020 name = h->root.string;
1021 otype = h->type;
1022 if (otype == bfd_link_hash_common)
1023 {
1024 obfd = h->u.c.p->section->owner;
1025 osize = h->u.c.size;
1026 }
1027 else if (otype == bfd_link_hash_defined
1028 || otype == bfd_link_hash_defweak)
1029 {
1030 obfd = h->u.def.section->owner;
1031 osize = 0;
1032 }
1033 else
1034 {
1035 /* FIXME: It would nice if we could report the BFD which defined
1036 an indirect symbol, but we don't have anywhere to store the
1037 information. */
1038 obfd = NULL;
1039 osize = 0;
1040 }
1041
252b5132
RH
1042 if (ntype == bfd_link_hash_defined
1043 || ntype == bfd_link_hash_defweak
1044 || ntype == bfd_link_hash_indirect)
1045 {
1046 ASSERT (otype == bfd_link_hash_common);
252b5132 1047 if (obfd != NULL)
df5f2391
AM
1048 einfo (_("%P: %pB: warning: definition of `%pT' overriding common"
1049 " from %pB\n"),
1050 nbfd, name, obfd);
1051 else
1052 einfo (_("%P: %pB: warning: definition of `%pT' overriding common\n"),
1053 nbfd, name);
252b5132
RH
1054 }
1055 else if (otype == bfd_link_hash_defined
1056 || otype == bfd_link_hash_defweak
1057 || otype == bfd_link_hash_indirect)
1058 {
1059 ASSERT (ntype == bfd_link_hash_common);
252b5132 1060 if (obfd != NULL)
df5f2391
AM
1061 einfo (_("%P: %pB: warning: common of `%pT' overridden by definition"
1062 " from %pB\n"),
1063 nbfd, name, obfd);
1064 else
1065 einfo (_("%P: %pB: warning: common of `%pT' overridden by definition\n"),
1066 nbfd, name);
252b5132
RH
1067 }
1068 else
1069 {
1070 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
1071 if (osize > nsize)
1072 {
252b5132 1073 if (obfd != NULL)
df5f2391
AM
1074 einfo (_("%P: %pB: warning: common of `%pT' overridden"
1075 " by larger common from %pB\n"),
1076 nbfd, name, obfd);
1077 else
1078 einfo (_("%P: %pB: warning: common of `%pT' overridden"
1079 " by larger common\n"),
1080 nbfd, name);
252b5132
RH
1081 }
1082 else if (nsize > osize)
1083 {
252b5132 1084 if (obfd != NULL)
df5f2391
AM
1085 einfo (_("%P: %pB: warning: common of `%pT' overriding"
1086 " smaller common from %pB\n"),
1087 nbfd, name, obfd);
1088 else
1089 einfo (_("%P: %pB: warning: common of `%pT' overriding"
1090 " smaller common\n"),
1091 nbfd, name);
252b5132
RH
1092 }
1093 else
1094 {
252b5132 1095 if (obfd != NULL)
df5f2391
AM
1096 einfo (_("%P: %pB and %pB: warning: multiple common of `%pT'\n"),
1097 nbfd, obfd, name);
1098 else
1099 einfo (_("%P: %pB: warning: multiple common of `%pT'\n"),
1100 nbfd, name);
252b5132
RH
1101 }
1102 }
252b5132
RH
1103}
1104
1105/* This is called when BFD has discovered a set element. H is the
1106 entry in the linker hash table for the set. SECTION and VALUE
1107 represent a value which should be added to the set. */
1108
1a72702b 1109static void
1579bae1
AM
1110add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1111 struct bfd_link_hash_entry *h,
1112 bfd_reloc_code_real_type reloc,
1113 bfd *abfd,
1114 asection *section,
1115 bfd_vma value)
252b5132
RH
1116{
1117 if (config.warn_constructors)
1118 einfo (_("%P: warning: global constructor %s used\n"),
1119 h->root.string);
1120
0aa7f586 1121 if (!config.build_constructors)
1a72702b 1122 return;
252b5132 1123
1579bae1 1124 ldctor_add_set_entry (h, reloc, NULL, section, value);
252b5132
RH
1125
1126 if (h->type == bfd_link_hash_new)
1127 {
1128 h->type = bfd_link_hash_undefined;
1129 h->u.undef.abfd = abfd;
1130 /* We don't call bfd_link_add_undef to add this to the list of
1131 undefined symbols because we are going to define it
1132 ourselves. */
1133 }
252b5132
RH
1134}
1135
1136/* This is called when BFD has discovered a constructor. This is only
1137 called for some object file formats--those which do not handle
1138 constructors in some more clever fashion. This is similar to
1139 adding an element to a set, but less general. */
1140
1a72702b 1141static void
1579bae1
AM
1142constructor_callback (struct bfd_link_info *info,
1143 bfd_boolean constructor,
1144 const char *name,
1145 bfd *abfd,
1146 asection *section,
1147 bfd_vma value)
252b5132
RH
1148{
1149 char *s;
1150 struct bfd_link_hash_entry *h;
1151 char set_name[1 + sizeof "__CTOR_LIST__"];
1152
1153 if (config.warn_constructors)
1154 einfo (_("%P: warning: global constructor %s used\n"), name);
1155
0aa7f586 1156 if (!config.build_constructors)
1a72702b 1157 return;
252b5132
RH
1158
1159 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1160 useful error message. */
0e86e20e 1161 if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL
0e1862bb 1162 && (bfd_link_relocatable (info)
252b5132 1163 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
df5f2391 1164 einfo (_("%F%P: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
252b5132
RH
1165
1166 s = set_name;
1167 if (bfd_get_symbol_leading_char (abfd) != '\0')
1168 *s++ = bfd_get_symbol_leading_char (abfd);
1169 if (constructor)
1170 strcpy (s, "__CTOR_LIST__");
1171 else
1172 strcpy (s, "__DTOR_LIST__");
1173
b34976b6 1174 h = bfd_link_hash_lookup (info->hash, set_name, TRUE, TRUE, TRUE);
252b5132 1175 if (h == (struct bfd_link_hash_entry *) NULL)
df5f2391 1176 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
252b5132
RH
1177 if (h->type == bfd_link_hash_new)
1178 {
1179 h->type = bfd_link_hash_undefined;
1180 h->u.undef.abfd = abfd;
1181 /* We don't call bfd_link_add_undef to add this to the list of
1182 undefined symbols because we are going to define it
1183 ourselves. */
1184 }
1185
1186 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
252b5132
RH
1187}
1188
1189/* A structure used by warning_callback to pass information through
1190 bfd_map_over_sections. */
1191
0125cdf2
NC
1192struct warning_callback_info
1193{
b34976b6 1194 bfd_boolean found;
252b5132
RH
1195 const char *warning;
1196 const char *symbol;
1197 asymbol **asymbols;
1198};
1199
4c6d802e
L
1200/* Look through the relocs to see if we can find a plausible address
1201 for SYMBOL in ABFD. Return TRUE if found. Otherwise return FALSE. */
1202
1203static bfd_boolean
1204symbol_warning (const char *warning, const char *symbol, bfd *abfd)
1205{
1206 struct warning_callback_info cinfo;
1207
1208 if (!bfd_generic_link_read_symbols (abfd))
df5f2391 1209 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd);
4c6d802e
L
1210
1211 cinfo.found = FALSE;
1212 cinfo.warning = warning;
1213 cinfo.symbol = symbol;
1214 cinfo.asymbols = bfd_get_outsymbols (abfd);
1215 bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
1216 return cinfo.found;
1217}
1218
252b5132
RH
1219/* This is called when there is a reference to a warning symbol. */
1220
1a72702b 1221static void
1579bae1
AM
1222warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1223 const char *warning,
1224 const char *symbol,
1225 bfd *abfd,
1226 asection *section,
1227 bfd_vma address)
252b5132
RH
1228{
1229 /* This is a hack to support warn_multiple_gp. FIXME: This should
1230 have a cleaner interface, but what? */
0aa7f586 1231 if (!config.warn_multiple_gp
252b5132 1232 && strcmp (warning, "using multiple gp values") == 0)
1a72702b 1233 return;
252b5132
RH
1234
1235 if (section != NULL)
df5f2391 1236 einfo ("%P: %C: %s%s\n", abfd, section, address, _("warning: "), warning);
252b5132 1237 else if (abfd == NULL)
300475fe 1238 einfo ("%P: %s%s\n", _("warning: "), warning);
252b5132 1239 else if (symbol == NULL)
df5f2391 1240 einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
0aa7f586 1241 else if (!symbol_warning (warning, symbol, abfd))
252b5132 1242 {
4c6d802e
L
1243 bfd *b;
1244 /* Search all input files for a reference to SYMBOL. */
c72f2fb2 1245 for (b = info->input_bfds; b; b = b->link.next)
4c6d802e 1246 if (b != abfd && symbol_warning (warning, symbol, b))
1a72702b 1247 return;
df5f2391 1248 einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
252b5132 1249 }
252b5132
RH
1250}
1251
1252/* This is called by warning_callback for each section. It checks the
1253 relocs of the section to see if it can find a reference to the
1254 symbol which triggered the warning. If it can, it uses the reloc
1255 to give an error message with a file and line number. */
1256
1257static void
1579bae1 1258warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
252b5132 1259{
1e9cc1c2 1260 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
252b5132
RH
1261 long relsize;
1262 arelent **relpp;
1263 long relcount;
1264 arelent **p, **pend;
1265
1266 if (info->found)
1267 return;
1268
1269 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1270 if (relsize < 0)
df5f2391 1271 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
252b5132
RH
1272 if (relsize == 0)
1273 return;
1274
1e9cc1c2 1275 relpp = (arelent **) xmalloc (relsize);
252b5132
RH
1276 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1277 if (relcount < 0)
df5f2391 1278 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
252b5132
RH
1279
1280 p = relpp;
1281 pend = p + relcount;
1282 for (; p < pend && *p != NULL; p++)
1283 {
1284 arelent *q = *p;
1285
1286 if (q->sym_ptr_ptr != NULL
1287 && *q->sym_ptr_ptr != NULL
1288 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1289 {
1290 /* We found a reloc for the symbol we are looking for. */
df5f2391 1291 einfo ("%P: %C: %s%s\n", abfd, sec, q->address, _("warning: "),
300475fe 1292 info->warning);
b34976b6 1293 info->found = TRUE;
252b5132
RH
1294 break;
1295 }
1296 }
1297
1298 free (relpp);
1299}
1300
1301/* This is called when an undefined symbol is found. */
1302
1a72702b 1303static void
0e86e20e 1304undefined_symbol (struct bfd_link_info *info,
1579bae1
AM
1305 const char *name,
1306 bfd *abfd,
1307 asection *section,
1308 bfd_vma address,
0b28295d 1309 bfd_boolean error)
252b5132
RH
1310{
1311 static char *error_name;
1312 static unsigned int error_count;
1313
1314#define MAX_ERRORS_IN_A_ROW 5
1315
0e86e20e
AM
1316 if (info->ignore_hash != NULL
1317 && bfd_hash_lookup (info->ignore_hash, name, FALSE, FALSE) != NULL)
1a72702b 1318 return;
0e86e20e 1319
252b5132
RH
1320 if (config.warn_once)
1321 {
252b5132 1322 /* Only warn once about a particular undefined symbol. */
0e86e20e 1323 add_ignoresym (info, name);
252b5132
RH
1324 }
1325
1326 /* We never print more than a reasonable number of errors in a row
1327 for a single symbol. */
1579bae1 1328 if (error_name != NULL
252b5132
RH
1329 && strcmp (name, error_name) == 0)
1330 ++error_count;
1331 else
1332 {
1333 error_count = 0;
1579bae1 1334 if (error_name != NULL)
252b5132 1335 free (error_name);
d1b2b2dc 1336 error_name = xstrdup (name);
252b5132
RH
1337 }
1338
1339 if (section != NULL)
1340 {
1341 if (error_count < MAX_ERRORS_IN_A_ROW)
b6f29aaa 1342 {
0b28295d 1343 if (error)
df5f2391 1344 einfo (_("%X%P: %C: undefined reference to `%pT'\n"),
0b28295d
ILT
1345 abfd, section, address, name);
1346 else
df5f2391 1347 einfo (_("%P: %C: warning: undefined reference to `%pT'\n"),
0b28295d 1348 abfd, section, address, name);
b6f29aaa 1349 }
252b5132 1350 else if (error_count == MAX_ERRORS_IN_A_ROW)
0b28295d
ILT
1351 {
1352 if (error)
df5f2391 1353 einfo (_("%X%P: %D: more undefined references to `%pT' follow\n"),
0b28295d
ILT
1354 abfd, section, address, name);
1355 else
df5f2391 1356 einfo (_("%P: %D: warning: more undefined references to `%pT' follow\n"),
0b28295d
ILT
1357 abfd, section, address, name);
1358 }
1359 else if (error)
1360 einfo ("%X");
252b5132
RH
1361 }
1362 else
1363 {
1364 if (error_count < MAX_ERRORS_IN_A_ROW)
b6f29aaa 1365 {
0b28295d 1366 if (error)
df5f2391 1367 einfo (_("%X%P: %pB: undefined reference to `%pT'\n"),
0b28295d
ILT
1368 abfd, name);
1369 else
df5f2391 1370 einfo (_("%P: %pB: warning: undefined reference to `%pT'\n"),
0b28295d 1371 abfd, name);
b6f29aaa 1372 }
252b5132 1373 else if (error_count == MAX_ERRORS_IN_A_ROW)
0b28295d
ILT
1374 {
1375 if (error)
df5f2391 1376 einfo (_("%X%P: %pB: more undefined references to `%pT' follow\n"),
0b28295d
ILT
1377 abfd, name);
1378 else
df5f2391 1379 einfo (_("%P: %pB: warning: more undefined references to `%pT' follow\n"),
0b28295d
ILT
1380 abfd, name);
1381 }
1382 else if (error)
1383 einfo ("%X");
252b5132 1384 }
252b5132
RH
1385}
1386
8aae64e6
AO
1387/* Counter to limit the number of relocation overflow error messages
1388 to print. Errors are printed as it is decremented. When it's
1389 called and the counter is zero, a final message is printed
1390 indicating more relocations were omitted. When it gets to -1, no
1391 such errors are printed. If it's initially set to a value less
1392 than -1, all such errors will be printed (--verbose does this). */
1393
1394int overflow_cutoff_limit = 10;
1395
252b5132
RH
1396/* This is called when a reloc overflows. */
1397
1a72702b 1398static void
0e86e20e 1399reloc_overflow (struct bfd_link_info *info,
dfeffb9f 1400 struct bfd_link_hash_entry *entry,
1579bae1
AM
1401 const char *name,
1402 const char *reloc_name,
1403 bfd_vma addend,
1404 bfd *abfd,
1405 asection *section,
1406 bfd_vma address)
252b5132 1407{
8aae64e6 1408 if (overflow_cutoff_limit == -1)
1a72702b 1409 return;
8aae64e6 1410
df5f2391 1411 einfo ("%X%P: %H:", abfd, section, address);
8aae64e6
AO
1412
1413 if (overflow_cutoff_limit >= 0
1414 && overflow_cutoff_limit-- == 0)
1415 {
1416 einfo (_(" additional relocation overflows omitted from the output\n"));
1a72702b 1417 return;
8aae64e6
AO
1418 }
1419
dfeffb9f
L
1420 if (entry)
1421 {
1422 while (entry->type == bfd_link_hash_indirect
1423 || entry->type == bfd_link_hash_warning)
1424 entry = entry->u.i.link;
1425 switch (entry->type)
1426 {
1427 case bfd_link_hash_undefined:
1428 case bfd_link_hash_undefweak:
0aa7f586 1429 einfo (_(" relocation truncated to fit: "
c1c8c1ef 1430 "%s against undefined symbol `%pT'"),
dfeffb9f
L
1431 reloc_name, entry->root.string);
1432 break;
1433 case bfd_link_hash_defined:
1434 case bfd_link_hash_defweak:
0aa7f586 1435 einfo (_(" relocation truncated to fit: "
c1c8c1ef 1436 "%s against symbol `%pT' defined in %pA section in %pB"),
dfeffb9f 1437 reloc_name, entry->root.string,
a6e1b90f
L
1438 entry->u.def.section,
1439 entry->u.def.section == bfd_abs_section_ptr
0e86e20e 1440 ? info->output_bfd : entry->u.def.section->owner);
dfeffb9f
L
1441 break;
1442 default:
1443 abort ();
1444 break;
1445 }
1446 }
1447 else
c1c8c1ef 1448 einfo (_(" relocation truncated to fit: %s against `%pT'"),
dfeffb9f 1449 reloc_name, name);
252b5132
RH
1450 if (addend != 0)
1451 einfo ("+%v", addend);
1452 einfo ("\n");
252b5132
RH
1453}
1454
1455/* This is called when a dangerous relocation is made. */
1456
1a72702b 1457static void
1579bae1
AM
1458reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1459 const char *message,
1460 bfd *abfd,
1461 asection *section,
1462 bfd_vma address)
252b5132 1463{
df5f2391 1464 einfo (_("%X%P: %H: dangerous relocation: %s\n"),
e1fffbe6 1465 abfd, section, address, message);
252b5132
RH
1466}
1467
1468/* This is called when a reloc is being generated attached to a symbol
1469 that is not being output. */
1470
1a72702b 1471static void
1579bae1
AM
1472unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1473 const char *name,
1474 bfd *abfd,
1475 asection *section,
1476 bfd_vma address)
252b5132 1477{
df5f2391 1478 einfo (_("%X%P: %H: reloc refers to symbol `%pT' which is not being output\n"),
e1fffbe6 1479 abfd, section, address, name);
252b5132
RH
1480}
1481
1482/* This is called if link_info.notice_all is set, or when a symbol in
1483 link_info.notice_hash is found. Symbols are put in notice_hash
5d3236ee
DK
1484 using the -y option, while notice_all is set if the --cref option
1485 has been supplied, or if there are any NOCROSSREFS sections in the
1486 linker script; and if plugins are active, since they need to monitor
1487 all references from non-IR files. */
252b5132 1488
b34976b6 1489static bfd_boolean
1579bae1 1490notice (struct bfd_link_info *info,
35ed3f94 1491 struct bfd_link_hash_entry *h,
46135103 1492 struct bfd_link_hash_entry *inh ATTRIBUTE_UNUSED,
1579bae1
AM
1493 bfd *abfd,
1494 asection *section,
16d96b5b 1495 bfd_vma value,
46135103 1496 flagword flags ATTRIBUTE_UNUSED)
252b5132 1497{
35ed3f94
AM
1498 const char *name;
1499
1500 if (h == NULL)
5061a885
AM
1501 {
1502 if (command_line.cref || nocrossref_list != NULL)
1e9cc1c2 1503 return handle_asneeded_cref (abfd, (enum notice_asneeded_action) value);
5061a885
AM
1504 return TRUE;
1505 }
1506
35ed3f94 1507 name = h->root.string;
5d3236ee 1508 if (info->notice_hash != NULL
9e2278f5 1509 && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL)
252b5132
RH
1510 {
1511 if (bfd_is_und_section (section))
df5f2391 1512 einfo (_("%P: %pB: reference to %s\n"), abfd, name);
252b5132 1513 else
df5f2391 1514 einfo (_("%P: %pB: definition of %s\n"), abfd, name);
252b5132
RH
1515 }
1516
1517 if (command_line.cref || nocrossref_list != NULL)
1518 add_cref (name, abfd, section, value);
1519
b34976b6 1520 return TRUE;
252b5132 1521}
This page took 0.982551 seconds and 4 git commands to generate.