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