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