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