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