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