Ensure that libbacktrace/allocfail.sh is not deleted when creating release tarballs.
[binutils-gdb.git] / ld / ldmain.c
blob9290a189b0d26fbd7bbcc97624ee45fbcd188584
1 /* Main program of GNU linker.
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
3 Written by Steve Chamberlain steve@cygnus.com
5 This file is part of the GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
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.
17 You should have received a copy of the GNU General Public License
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. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "safe-ctype.h"
25 #include "libiberty.h"
26 #include "progress.h"
27 #include "bfdlink.h"
28 #include "ctf-api.h"
29 #include "filenames.h"
30 #include "elf/common.h"
32 #include "ld.h"
33 #include "ldmain.h"
34 #include "ldmisc.h"
35 #include "ldwrite.h"
36 #include "ldexp.h"
37 #include "ldlang.h"
38 #include <ldgram.h>
39 #include "ldlex.h"
40 #include "ldfile.h"
41 #include "ldemul.h"
42 #include "ldctor.h"
43 #if BFD_SUPPORTS_PLUGINS
44 #include "plugin.h"
45 #include "plugin-api.h"
46 #endif /* BFD_SUPPORTS_PLUGINS */
48 /* Somewhere above, sys/stat.h got included. */
49 #if !defined(S_ISDIR) && defined(S_IFDIR)
50 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
51 #endif
53 #include <string.h>
55 #ifndef TARGET_SYSTEM_ROOT
56 #define TARGET_SYSTEM_ROOT ""
57 #endif
59 /* EXPORTS */
61 FILE *saved_script_handle = NULL;
62 FILE *previous_script_handle = NULL;
63 bool force_make_executable = false;
65 char *default_target;
66 const char *output_filename = "a.out";
68 /* Name this program was invoked by. */
69 char *program_name;
71 /* The prefix for system library directories. */
72 const char *ld_sysroot;
74 /* The canonical representation of ld_sysroot. */
75 char *ld_canon_sysroot;
76 int ld_canon_sysroot_len;
78 /* Set by -G argument, for targets like MIPS ELF. */
79 int g_switch_value = 8;
81 /* Nonzero means print names of input files as processed. */
82 unsigned int trace_files;
84 /* Nonzero means report actions taken by the linker, and describe the linker script in use. */
85 bool verbose;
87 /* Nonzero means version number was printed, so exit successfully
88 instead of complaining if no input files are given. */
89 bool version_printed;
91 /* TRUE if we should demangle symbol names. */
92 bool demangling;
94 args_type command_line;
96 ld_config_type config;
98 sort_type sort_section;
100 static const char *get_sysroot
101 (int, char **);
102 static char *get_emulation
103 (int, char **);
104 static bool add_archive_element
105 (struct bfd_link_info *, bfd *, const char *, bfd **);
106 static void multiple_definition
107 (struct bfd_link_info *, struct bfd_link_hash_entry *,
108 bfd *, asection *, bfd_vma);
109 static void multiple_common
110 (struct bfd_link_info *, struct bfd_link_hash_entry *,
111 bfd *, enum bfd_link_hash_type, bfd_vma);
112 static void add_to_set
113 (struct bfd_link_info *, struct bfd_link_hash_entry *,
114 bfd_reloc_code_real_type, bfd *, asection *, bfd_vma);
115 static void constructor_callback
116 (struct bfd_link_info *, bool, const char *, bfd *,
117 asection *, bfd_vma);
118 static void warning_callback
119 (struct bfd_link_info *, const char *, const char *, bfd *,
120 asection *, bfd_vma);
121 static void warning_find_reloc
122 (bfd *, asection *, void *);
123 static void undefined_symbol
124 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
125 bool);
126 static void reloc_overflow
127 (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
128 const char *, bfd_vma, bfd *, asection *, bfd_vma);
129 static void reloc_dangerous
130 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
131 static void unattached_reloc
132 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
133 static bool notice
134 (struct bfd_link_info *, struct bfd_link_hash_entry *,
135 struct bfd_link_hash_entry *, bfd *, asection *, bfd_vma, flagword);
137 static struct bfd_link_callbacks link_callbacks =
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,
149 notice,
150 einfo,
151 info_msg,
152 minfo,
153 ldlang_override_segment_assignment,
154 ldlang_ctf_acquire_strings,
155 NULL,
156 ldlang_ctf_new_dynsym,
157 ldlang_write_ctf_late
160 static bfd_assert_handler_type default_bfd_assert_handler;
161 static bfd_error_handler_type default_bfd_error_handler;
163 struct bfd_link_info link_info;
165 struct dependency_file
167 struct dependency_file *next;
168 char *name;
171 static struct dependency_file *dependency_files, *dependency_files_tail;
173 void
174 track_dependency_files (const char *filename)
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;
187 static void
188 write_dependency_file (void)
190 FILE *out;
191 struct dependency_file *dep;
193 out = fopen (config.dependency_file, FOPEN_WT);
194 if (out == NULL)
196 einfo (_("%F%P: cannot open dependency file %s: %E\n"),
197 config.dependency_file);
200 fprintf (out, "%s:", output_filename);
202 for (dep = dependency_files; dep != NULL; dep = dep->next)
203 fprintf (out, " \\\n %s", dep->name);
205 fprintf (out, "\n");
206 for (dep = dependency_files; dep != NULL; dep = dep->next)
207 fprintf (out, "\n%s:\n", dep->name);
209 fclose (out);
212 static void
213 ld_cleanup (void)
215 bfd_cache_close_all ();
216 #if BFD_SUPPORTS_PLUGINS
217 plugin_call_cleanup ();
218 #endif
219 if (output_filename && delete_output_file_on_failure)
220 unlink_if_ordinary (output_filename);
223 /* Hook to notice BFD assertions. */
225 static void
226 ld_bfd_assert_handler (const char *fmt, const char *bfdver,
227 const char *file, int line)
229 config.make_executable = false;
230 (*default_bfd_assert_handler) (fmt, bfdver, file, line);
233 /* Hook the bfd error/warning handler for --fatal-warnings. */
235 static void
236 ld_bfd_error_handler (const char *fmt, va_list ap)
238 if (config.fatal_warnings)
239 config.make_executable = false;
240 (*default_bfd_error_handler) (fmt, ap);
244 main (int argc, char **argv)
246 char *emulation;
247 long start_time = get_run_time ();
249 #ifdef HAVE_LC_MESSAGES
250 setlocale (LC_MESSAGES, "");
251 #endif
252 setlocale (LC_CTYPE, "");
253 bindtextdomain (PACKAGE, LOCALEDIR);
254 textdomain (PACKAGE);
256 program_name = argv[0];
257 xmalloc_set_program_name (program_name);
259 START_PROGRESS (program_name, 0);
261 expandargv (&argc, &argv);
263 if (bfd_init () != BFD_INIT_MAGIC)
264 einfo (_("%F%P: fatal error: libbfd ABI mismatch\n"));
266 bfd_set_error_program_name (program_name);
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);
273 /* Also hook the bfd error/warning handler for --fatal-warnings. */
274 default_bfd_error_handler = bfd_set_error_handler (ld_bfd_error_handler);
276 xatexit (ld_cleanup);
278 /* Set up the sysroot directory. */
279 ld_sysroot = get_sysroot (argc, argv);
280 if (*ld_sysroot)
281 ld_canon_sysroot = lrealpath (ld_sysroot);
282 if (ld_canon_sysroot)
284 ld_canon_sysroot_len = strlen (ld_canon_sysroot);
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]))
289 ld_canon_sysroot [--ld_canon_sysroot_len] = '\0';
291 else
292 ld_canon_sysroot_len = -1;
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. */
298 if (!bfd_set_default_target (TARGET))
300 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
301 xexit (1);
304 #if YYDEBUG
306 extern int yydebug;
307 yydebug = 1;
309 #endif
311 config.build_constructors = true;
312 config.rpath_separator = ':';
313 config.split_by_reloc = (unsigned) -1;
314 config.split_by_file = (bfd_size_type) -1;
315 config.make_executable = true;
316 config.magic_demand_paged = true;
317 config.text_read_only = true;
318 config.print_map_discarded = true;
319 link_info.disable_target_specific_optimizations = -1;
321 command_line.warn_mismatch = true;
322 command_line.warn_search_mismatch = true;
323 command_line.check_section_addresses = -1;
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;
332 link_info.allow_undefined_version = true;
333 link_info.keep_memory = true;
334 link_info.max_cache_size = (bfd_size_type) -1;
335 link_info.combreloc = true;
336 link_info.strip_discarded = true;
337 link_info.prohibit_multiple_definition_absolute = false;
338 link_info.textrel_check = DEFAULT_LD_TEXTREL_CHECK;
339 link_info.emit_hash = DEFAULT_EMIT_SYSV_HASH;
340 link_info.emit_gnu_hash = DEFAULT_EMIT_GNU_HASH;
341 link_info.callbacks = &link_callbacks;
342 link_info.input_bfds_tail = &link_info.input_bfds;
343 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
344 and _fini symbols. We are compatible. */
345 link_info.init_function = "_init";
346 link_info.fini_function = "_fini";
347 link_info.relax_pass = 1;
348 link_info.extern_protected_data = -1;
349 link_info.dynamic_undefined_weak = -1;
350 link_info.indirect_extern_access = -1;
351 link_info.pei386_auto_import = -1;
352 link_info.spare_dynamic_tags = 5;
353 link_info.path_separator = ':';
354 #ifdef DEFAULT_FLAG_COMPRESS_DEBUG
355 config.compress_debug = DEFAULT_COMPRESSED_DEBUG_ALGORITHM;
356 #endif
357 #ifdef DEFAULT_NEW_DTAGS
358 link_info.new_dtags = DEFAULT_NEW_DTAGS;
359 #endif
360 link_info.start_stop_gc = false;
361 link_info.start_stop_visibility = STV_PROTECTED;
363 ldfile_add_arch ("");
364 emulation = get_emulation (argc, argv);
365 ldemul_choose_mode (emulation);
366 default_target = ldemul_choose_target (argc, argv);
367 lang_init ();
368 ldexp_init ();
369 ldemul_before_parse ();
370 lang_has_input_file = false;
371 parse_args (argc, argv);
373 if (config.hash_table_size != 0)
374 bfd_hash_set_default_size (config.hash_table_size);
376 #if BFD_SUPPORTS_PLUGINS
377 /* Now all the plugin arguments have been gathered, we can load them. */
378 plugin_load_plugins ();
379 #endif /* BFD_SUPPORTS_PLUGINS */
381 ldemul_set_symbols ();
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)
388 ldfile_open_script_file (command_line.default_script);
389 parser_input = input_script;
390 yyparse ();
393 /* If we have not already opened and parsed a linker script
394 read the emulation's appropriate default script. */
395 if (saved_script_handle == NULL)
397 int isfile;
398 char *s = ldemul_get_script (&isfile);
400 if (isfile)
401 ldfile_open_default_command_file (s);
402 else
404 lex_string = s;
405 lex_redirect (s, _("built in linker script"), 1);
407 parser_input = input_script;
408 yyparse ();
409 lex_string = NULL;
412 if (verbose)
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");
420 if (saved_script_handle)
422 static const int ld_bufsz = 8193;
423 size_t n;
424 char *buf = (char *) xmalloc (ld_bufsz);
426 rewind (saved_script_handle);
427 while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
429 buf[n] = 0;
430 info_msg ("%s", buf);
432 rewind (saved_script_handle);
433 free (buf);
435 else
437 int isfile;
439 info_msg (ldemul_get_script (&isfile));
442 info_msg ("\n==================================================\n");
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;
451 if (command_line.print_output_format)
452 info_msg ("%s\n", lang_get_output_target ());
454 lang_final ();
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);
463 if (link_info.inhibit_common_definition && !bfd_link_dll (&link_info))
464 einfo (_("%F%P: --no-define-common may not be used without -shared\n"));
466 if (!lang_has_input_file)
468 if (version_printed || command_line.print_output_format)
469 xexit (0);
470 einfo (_("%F%P: no input files\n"));
473 if (verbose)
474 info_msg (_("%P: mode %s\n"), emulation);
476 ldemul_after_parse ();
478 if (config.map_filename)
480 if (strcmp (config.map_filename, "-") == 0)
482 config.map_file = stdout;
484 else
486 config.map_file = fopen (config.map_filename, FOPEN_WT);
487 if (config.map_file == (FILE *) NULL)
489 bfd_set_error (bfd_error_system_call);
490 einfo (_("%F%P: cannot open map file %s: %E\n"),
491 config.map_filename);
494 link_info.has_map_file = true;
497 lang_process ();
499 /* Print error messages for any missing symbols, for any warning
500 symbols, and possibly multiple definitions. */
501 if (bfd_link_relocatable (&link_info))
502 link_info.output_bfd->flags &= ~EXEC_P;
503 else
504 link_info.output_bfd->flags |= EXEC_P;
506 flagword flags = 0;
507 switch (config.compress_debug)
509 case COMPRESS_DEBUG_GNU_ZLIB:
510 flags = BFD_COMPRESS;
511 break;
512 case COMPRESS_DEBUG_GABI_ZLIB:
513 flags = BFD_COMPRESS | BFD_COMPRESS_GABI;
514 break;
515 case COMPRESS_DEBUG_ZSTD:
516 flags = BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD;
517 break;
518 default:
519 break;
521 link_info.output_bfd->flags
522 |= flags & bfd_applicable_file_flags (link_info.output_bfd);
524 ldwrite ();
526 if (config.map_file != NULL)
527 lang_map ();
528 if (command_line.cref)
529 output_cref (config.map_file != NULL ? config.map_file : stdout);
530 if (nocrossref_list != NULL)
531 check_nocrossrefs ();
532 if (command_line.print_memory_usage)
533 lang_print_memory_usage ();
534 #if 0
536 struct bfd_link_hash_entry *h;
538 h = bfd_link_hash_lookup (link_info.hash, "__image_base__", 0,0,1);
539 fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1);
541 #endif
542 ldexp_finish ();
543 lang_finish ();
545 if (config.dependency_file != NULL)
546 write_dependency_file ();
548 /* Even if we're producing relocatable output, some non-fatal errors should
549 be reported in the exit status. (What non-fatal errors, if any, do we
550 want to ignore for relocatable output?) */
551 if (!config.make_executable && !force_make_executable)
553 if (verbose)
554 einfo (_("%P: link errors found, deleting executable `%s'\n"),
555 output_filename);
557 /* The file will be removed by ld_cleanup. */
558 xexit (1);
560 else
562 if (!bfd_close (link_info.output_bfd))
563 einfo (_("%F%P: %s: final close failed: %E\n"), output_filename);
565 /* If the --force-exe-suffix is enabled, and we're making an
566 executable file and it doesn't end in .exe, copy it to one
567 which does. */
568 if (!bfd_link_relocatable (&link_info)
569 && command_line.force_exe_suffix)
571 int len = strlen (output_filename);
573 if (len < 4
574 || (strcasecmp (output_filename + len - 4, ".exe") != 0
575 && strcasecmp (output_filename + len - 4, ".dll") != 0))
577 FILE *src;
578 FILE *dst;
579 const int bsize = 4096;
580 char *buf = (char *) xmalloc (bsize);
581 int l;
582 char *dst_name = (char *) xmalloc (len + 5);
584 strcpy (dst_name, output_filename);
585 strcat (dst_name, ".exe");
586 src = fopen (output_filename, FOPEN_RB);
587 dst = fopen (dst_name, FOPEN_WB);
589 if (!src)
590 einfo (_("%F%P: unable to open for source of copy `%s'\n"),
591 output_filename);
592 if (!dst)
593 einfo (_("%F%P: unable to open for destination of copy `%s'\n"),
594 dst_name);
595 while ((l = fread (buf, 1, bsize, src)) > 0)
597 int done = fwrite (buf, 1, l, dst);
599 if (done != l)
600 einfo (_("%P: error writing file `%s'\n"), dst_name);
603 fclose (src);
604 if (fclose (dst) == EOF)
605 einfo (_("%P: error closing file `%s'\n"), dst_name);
606 free (dst_name);
607 free (buf);
612 END_PROGRESS (program_name);
614 if (config.stats)
616 long run_time = get_run_time () - start_time;
618 fflush (stdout);
619 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
620 program_name, run_time / 1000000, run_time % 1000000);
621 fflush (stderr);
624 /* Prevent ld_cleanup from doing anything, after a successful link. */
625 output_filename = NULL;
627 xexit (0);
628 return 0;
631 /* If the configured sysroot is relocatable, try relocating it based on
632 default prefix FROM. Return the relocated directory if it exists,
633 otherwise return null. */
635 static char *
636 get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
638 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
639 char *path;
640 struct stat s;
642 path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
643 if (path)
645 if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
646 return path;
647 free (path);
649 #endif
650 return 0;
653 /* Return the sysroot directory. Return "" if no sysroot is being used. */
655 static const char *
656 get_sysroot (int argc, char **argv)
658 int i;
659 const char *path = NULL;
661 for (i = 1; i < argc; i++)
662 if (startswith (argv[i], "--sysroot="))
663 path = argv[i] + strlen ("--sysroot=");
665 if (!path)
666 path = get_relative_sysroot (BINDIR);
668 if (!path)
669 path = get_relative_sysroot (TOOLBINDIR);
671 if (!path)
672 path = TARGET_SYSTEM_ROOT;
674 if (IS_DIR_SEPARATOR (*path) && path[1] == 0)
675 path = "";
677 return path;
680 /* We need to find any explicitly given emulation in order to initialize the
681 state that's needed by the lex&yacc argument parser (parse_args). */
683 static char *
684 get_emulation (int argc, char **argv)
686 char *emulation;
687 int i;
689 emulation = getenv (EMULATION_ENVIRON);
690 if (emulation == NULL)
691 emulation = DEFAULT_EMULATION;
693 for (i = 1; i < argc; i++)
695 if (startswith (argv[i], "-m"))
697 if (argv[i][2] == '\0')
699 /* -m EMUL */
700 if (i < argc - 1)
702 emulation = argv[i + 1];
703 i++;
705 else
706 einfo (_("%F%P: missing argument to -m\n"));
708 else if (strcmp (argv[i], "-mips1") == 0
709 || strcmp (argv[i], "-mips2") == 0
710 || strcmp (argv[i], "-mips3") == 0
711 || strcmp (argv[i], "-mips4") == 0
712 || strcmp (argv[i], "-mips5") == 0
713 || strcmp (argv[i], "-mips32") == 0
714 || strcmp (argv[i], "-mips32r2") == 0
715 || strcmp (argv[i], "-mips32r3") == 0
716 || strcmp (argv[i], "-mips32r5") == 0
717 || strcmp (argv[i], "-mips32r6") == 0
718 || strcmp (argv[i], "-mips64") == 0
719 || strcmp (argv[i], "-mips64r2") == 0
720 || strcmp (argv[i], "-mips64r3") == 0
721 || strcmp (argv[i], "-mips64r5") == 0
722 || strcmp (argv[i], "-mips64r6") == 0)
724 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
725 passed to the linker by some MIPS compilers. They
726 generally tell the linker to use a slightly different
727 library path. Perhaps someday these should be
728 implemented as emulations; until then, we just ignore
729 the arguments and hope that nobody ever creates
730 emulations named ips1, ips2 or ips3. */
732 else if (strcmp (argv[i], "-m486") == 0)
734 /* FIXME: The argument -m486 is passed to the linker on
735 some Linux systems. Hope that nobody creates an
736 emulation named 486. */
738 else
740 /* -mEMUL */
741 emulation = &argv[i][2];
746 return emulation;
749 void
750 add_ysym (const char *name)
752 if (link_info.notice_hash == NULL)
754 link_info.notice_hash
755 = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
756 if (!bfd_hash_table_init_n (link_info.notice_hash,
757 bfd_hash_newfunc,
758 sizeof (struct bfd_hash_entry),
759 61))
760 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
763 if (bfd_hash_lookup (link_info.notice_hash, name, true, true) == NULL)
764 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
767 void
768 add_ignoresym (struct bfd_link_info *info, const char *name)
770 if (info->ignore_hash == NULL)
772 info->ignore_hash = xmalloc (sizeof (struct bfd_hash_table));
773 if (!bfd_hash_table_init_n (info->ignore_hash,
774 bfd_hash_newfunc,
775 sizeof (struct bfd_hash_entry),
776 61))
777 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
780 if (bfd_hash_lookup (info->ignore_hash, name, true, true) == NULL)
781 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
784 /* Record a symbol to be wrapped, from the --wrap option. */
786 void
787 add_wrap (const char *name)
789 if (link_info.wrap_hash == NULL)
791 link_info.wrap_hash
792 = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
793 if (!bfd_hash_table_init_n (link_info.wrap_hash,
794 bfd_hash_newfunc,
795 sizeof (struct bfd_hash_entry),
796 61))
797 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
800 if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
801 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
804 /* Handle the -retain-symbols-file option. */
806 void
807 add_keepsyms_file (const char *filename)
809 FILE *file;
810 char *buf;
811 size_t bufsize;
812 int c;
814 if (link_info.strip == strip_some)
815 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
817 file = fopen (filename, "r");
818 if (file == NULL)
820 bfd_set_error (bfd_error_system_call);
821 einfo ("%X%P: %s: %E\n", filename);
822 return;
825 link_info.keep_hash = (struct bfd_hash_table *)
826 xmalloc (sizeof (struct bfd_hash_table));
827 if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc,
828 sizeof (struct bfd_hash_entry)))
829 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
831 bufsize = 100;
832 buf = (char *) xmalloc (bufsize);
834 c = getc (file);
835 while (c != EOF)
837 while (ISSPACE (c))
838 c = getc (file);
840 if (c != EOF)
842 size_t len = 0;
844 while (!ISSPACE (c) && c != EOF)
846 buf[len] = c;
847 ++len;
848 if (len >= bufsize)
850 bufsize *= 2;
851 buf = (char *) xrealloc (buf, bufsize);
853 c = getc (file);
856 buf[len] = '\0';
858 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true) == NULL)
859 einfo (_("%F%P: bfd_hash_lookup for insertion failed: %E\n"));
863 if (link_info.strip != strip_none)
864 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
866 free (buf);
867 link_info.strip = strip_some;
868 fclose (file);
871 /* Callbacks from the BFD linker routines. */
873 /* This is called when BFD has decided to include an archive member in
874 a link. */
876 static bool
877 add_archive_element (struct bfd_link_info *info,
878 bfd *abfd,
879 const char *name,
880 bfd **subsbfd ATTRIBUTE_UNUSED)
882 lang_input_statement_type *input;
883 lang_input_statement_type *parent;
884 lang_input_statement_type orig_input;
886 input = (lang_input_statement_type *)
887 xcalloc (1, sizeof (lang_input_statement_type));
888 input->header.type = lang_input_statement_enum;
889 input->filename = bfd_get_filename (abfd);
890 input->local_sym_name = bfd_get_filename (abfd);
891 input->the_bfd = abfd;
893 /* Save the original data for trace files/tries below, as plugins
894 (if enabled) may possibly alter it to point to a replacement
895 BFD, but we still want to output the original BFD filename. */
896 orig_input = *input;
897 #if BFD_SUPPORTS_PLUGINS
898 if (link_info.lto_plugin_active)
900 /* We must offer this archive member to the plugins to claim. */
901 plugin_maybe_claim (input);
902 if (input->flags.claimed)
904 if (no_more_claiming)
906 /* Don't claim new IR symbols after all IR symbols have
907 been claimed. */
908 if (verbose)
909 info_msg ("%pI: no new IR symbols to claim\n",
910 &orig_input);
911 input->flags.claimed = 0;
912 return false;
914 input->flags.claim_archive = true;
915 *subsbfd = input->the_bfd;
918 #endif /* BFD_SUPPORTS_PLUGINS */
920 if (link_info.input_bfds_tail == &input->the_bfd->link.next
921 || input->the_bfd->link.next != NULL)
923 /* We have already loaded this element, and are attempting to
924 load it again. This can happen when the archive map doesn't
925 match actual symbols defined by the element. */
926 free (input);
927 bfd_set_error (bfd_error_malformed_archive);
928 return false;
931 /* Set the file_chain pointer of archives to the last element loaded
932 from the archive. See ldlang.c:find_rescan_insertion. */
933 parent = bfd_usrdata (abfd->my_archive);
934 if (parent != NULL && !parent->flags.reload)
935 parent->next = input;
937 ldlang_add_file (input);
939 if (config.map_file != NULL)
941 static bool header_printed;
942 struct bfd_link_hash_entry *h;
943 bfd *from;
944 int len;
946 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
947 if (h == NULL
948 && info->pei386_auto_import
949 && startswith (name, "__imp_"))
950 h = bfd_link_hash_lookup (info->hash, name + 6, false, false, true);
952 if (h == NULL)
953 from = NULL;
954 else
956 switch (h->type)
958 default:
959 from = NULL;
960 break;
962 case bfd_link_hash_defined:
963 case bfd_link_hash_defweak:
964 from = h->u.def.section->owner;
965 break;
967 case bfd_link_hash_undefined:
968 case bfd_link_hash_undefweak:
969 from = h->u.undef.abfd;
970 break;
972 case bfd_link_hash_common:
973 from = h->u.c.p->section->owner;
974 break;
978 if (!header_printed)
980 minfo (_("Archive member included to satisfy reference by file (symbol)\n\n"));
981 header_printed = true;
984 if (abfd->my_archive == NULL
985 || bfd_is_thin_archive (abfd->my_archive))
987 minfo ("%s", bfd_get_filename (abfd));
988 len = strlen (bfd_get_filename (abfd));
990 else
992 minfo ("%s(%s)", bfd_get_filename (abfd->my_archive),
993 bfd_get_filename (abfd));
994 len = (strlen (bfd_get_filename (abfd->my_archive))
995 + strlen (bfd_get_filename (abfd))
996 + 2);
999 if (len >= 29)
1001 print_nl ();
1002 len = 0;
1004 print_spaces (30 - len);
1006 if (from != NULL)
1007 minfo ("%pB ", from);
1008 if (h != NULL)
1009 minfo ("(%pT)\n", h->root.string);
1010 else
1011 minfo ("(%s)\n", name);
1014 if (verbose
1015 || trace_files > 1
1016 || (trace_files && bfd_is_thin_archive (orig_input.the_bfd->my_archive)))
1017 info_msg ("%pI\n", &orig_input);
1018 return true;
1021 /* This is called when BFD has discovered a symbol which is defined
1022 multiple times. */
1024 static void
1025 multiple_definition (struct bfd_link_info *info,
1026 struct bfd_link_hash_entry *h,
1027 bfd *nbfd,
1028 asection *nsec,
1029 bfd_vma nval)
1031 const char *name;
1032 bfd *obfd;
1033 asection *osec;
1034 bfd_vma oval;
1036 if (info->allow_multiple_definition)
1037 return;
1039 switch (h->type)
1041 case bfd_link_hash_defined:
1042 osec = h->u.def.section;
1043 oval = h->u.def.value;
1044 obfd = h->u.def.section->owner;
1045 break;
1046 case bfd_link_hash_indirect:
1047 osec = bfd_ind_section_ptr;
1048 oval = 0;
1049 obfd = NULL;
1050 break;
1051 default:
1052 abort ();
1055 /* Ignore a redefinition of an absolute symbol to the
1056 same value; it's harmless. */
1057 if (h->type == bfd_link_hash_defined
1058 && bfd_is_abs_section (osec)
1059 && bfd_is_abs_section (nsec)
1060 && nval == oval)
1061 return;
1063 /* If either section has the output_section field set to
1064 bfd_abs_section_ptr, it means that the section is being
1065 discarded, and this is not really a multiple definition at all.
1066 FIXME: It would be cleaner to somehow ignore symbols defined in
1067 sections which are being discarded. */
1068 if (!info->prohibit_multiple_definition_absolute
1069 && ((osec->output_section != NULL
1070 && ! bfd_is_abs_section (osec)
1071 && bfd_is_abs_section (osec->output_section))
1072 || (nsec->output_section != NULL
1073 && !bfd_is_abs_section (nsec)
1074 && bfd_is_abs_section (nsec->output_section))))
1075 return;
1077 name = h->root.string;
1078 if (nbfd == NULL)
1080 nbfd = obfd;
1081 nsec = osec;
1082 nval = oval;
1083 obfd = NULL;
1085 if (info->warn_multiple_definition)
1086 einfo (_("%P: %C: warning: multiple definition of `%pT'"),
1087 nbfd, nsec, nval, name);
1088 else
1089 einfo (_("%X%P: %C: multiple definition of `%pT'"),
1090 nbfd, nsec, nval, name);
1091 if (obfd != NULL)
1092 einfo (_("; %D: first defined here"), obfd, osec, oval);
1093 einfo ("\n");
1095 if (RELAXATION_ENABLED_BY_USER)
1097 einfo (_("%P: disabling relaxation; it will not work with multiple definitions\n"));
1098 DISABLE_RELAXATION;
1102 /* This is called when there is a definition of a common symbol, or
1103 when a common symbol is found for a symbol that is already defined,
1104 or when two common symbols are found. We only do something if
1105 -warn-common was used. */
1107 static void
1108 multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1109 struct bfd_link_hash_entry *h,
1110 bfd *nbfd,
1111 enum bfd_link_hash_type ntype,
1112 bfd_vma nsize)
1114 const char *name;
1115 bfd *obfd;
1116 enum bfd_link_hash_type otype;
1117 bfd_vma osize;
1119 if (!config.warn_common)
1120 return;
1122 name = h->root.string;
1123 otype = h->type;
1124 if (otype == bfd_link_hash_common)
1126 obfd = h->u.c.p->section->owner;
1127 osize = h->u.c.size;
1129 else if (otype == bfd_link_hash_defined
1130 || otype == bfd_link_hash_defweak)
1132 obfd = h->u.def.section->owner;
1133 osize = 0;
1135 else
1137 /* FIXME: It would nice if we could report the BFD which defined
1138 an indirect symbol, but we don't have anywhere to store the
1139 information. */
1140 obfd = NULL;
1141 osize = 0;
1144 if (ntype == bfd_link_hash_defined
1145 || ntype == bfd_link_hash_defweak
1146 || ntype == bfd_link_hash_indirect)
1148 ASSERT (otype == bfd_link_hash_common);
1149 if (obfd != NULL)
1150 einfo (_("%P: %pB: warning: definition of `%pT' overriding common"
1151 " from %pB\n"),
1152 nbfd, name, obfd);
1153 else
1154 einfo (_("%P: %pB: warning: definition of `%pT' overriding common\n"),
1155 nbfd, name);
1157 else if (otype == bfd_link_hash_defined
1158 || otype == bfd_link_hash_defweak
1159 || otype == bfd_link_hash_indirect)
1161 ASSERT (ntype == bfd_link_hash_common);
1162 if (obfd != NULL)
1163 einfo (_("%P: %pB: warning: common of `%pT' overridden by definition"
1164 " from %pB\n"),
1165 nbfd, name, obfd);
1166 else
1167 einfo (_("%P: %pB: warning: common of `%pT' overridden by definition\n"),
1168 nbfd, name);
1170 else
1172 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
1173 if (osize > nsize)
1175 if (obfd != NULL)
1176 einfo (_("%P: %pB: warning: common of `%pT' overridden"
1177 " by larger common from %pB\n"),
1178 nbfd, name, obfd);
1179 else
1180 einfo (_("%P: %pB: warning: common of `%pT' overridden"
1181 " by larger common\n"),
1182 nbfd, name);
1184 else if (nsize > osize)
1186 if (obfd != NULL)
1187 einfo (_("%P: %pB: warning: common of `%pT' overriding"
1188 " smaller common from %pB\n"),
1189 nbfd, name, obfd);
1190 else
1191 einfo (_("%P: %pB: warning: common of `%pT' overriding"
1192 " smaller common\n"),
1193 nbfd, name);
1195 else
1197 if (obfd != NULL)
1198 einfo (_("%P: %pB and %pB: warning: multiple common of `%pT'\n"),
1199 nbfd, obfd, name);
1200 else
1201 einfo (_("%P: %pB: warning: multiple common of `%pT'\n"),
1202 nbfd, name);
1207 /* This is called when BFD has discovered a set element. H is the
1208 entry in the linker hash table for the set. SECTION and VALUE
1209 represent a value which should be added to the set. */
1211 static void
1212 add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1213 struct bfd_link_hash_entry *h,
1214 bfd_reloc_code_real_type reloc,
1215 bfd *abfd,
1216 asection *section,
1217 bfd_vma value)
1219 if (config.warn_constructors)
1220 einfo (_("%P: warning: global constructor %s used\n"),
1221 h->root.string);
1223 if (!config.build_constructors)
1224 return;
1226 ldctor_add_set_entry (h, reloc, NULL, section, value);
1228 if (h->type == bfd_link_hash_new)
1230 h->type = bfd_link_hash_undefined;
1231 h->u.undef.abfd = abfd;
1232 /* We don't call bfd_link_add_undef to add this to the list of
1233 undefined symbols because we are going to define it
1234 ourselves. */
1238 /* This is called when BFD has discovered a constructor. This is only
1239 called for some object file formats--those which do not handle
1240 constructors in some more clever fashion. This is similar to
1241 adding an element to a set, but less general. */
1243 static void
1244 constructor_callback (struct bfd_link_info *info,
1245 bool constructor,
1246 const char *name,
1247 bfd *abfd,
1248 asection *section,
1249 bfd_vma value)
1251 char *s;
1252 struct bfd_link_hash_entry *h;
1253 char set_name[1 + sizeof "__CTOR_LIST__"];
1255 if (config.warn_constructors)
1256 einfo (_("%P: warning: global constructor %s used\n"), name);
1258 if (!config.build_constructors)
1259 return;
1261 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1262 useful error message. */
1263 if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL
1264 && (bfd_link_relocatable (info)
1265 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
1266 einfo (_("%F%P: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1268 s = set_name;
1269 if (bfd_get_symbol_leading_char (abfd) != '\0')
1270 *s++ = bfd_get_symbol_leading_char (abfd);
1271 if (constructor)
1272 strcpy (s, "__CTOR_LIST__");
1273 else
1274 strcpy (s, "__DTOR_LIST__");
1276 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
1277 if (h == (struct bfd_link_hash_entry *) NULL)
1278 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1279 if (h->type == bfd_link_hash_new)
1281 h->type = bfd_link_hash_undefined;
1282 h->u.undef.abfd = abfd;
1283 /* We don't call bfd_link_add_undef to add this to the list of
1284 undefined symbols because we are going to define it
1285 ourselves. */
1288 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
1291 /* A structure used by warning_callback to pass information through
1292 bfd_map_over_sections. */
1294 struct warning_callback_info
1296 bool found;
1297 const char *warning;
1298 const char *symbol;
1299 asymbol **asymbols;
1302 /* Look through the relocs to see if we can find a plausible address
1303 for SYMBOL in ABFD. Return TRUE if found. Otherwise return FALSE. */
1305 static bool
1306 symbol_warning (const char *warning, const char *symbol, bfd *abfd)
1308 struct warning_callback_info cinfo;
1310 if (!bfd_generic_link_read_symbols (abfd))
1311 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd);
1313 cinfo.found = false;
1314 cinfo.warning = warning;
1315 cinfo.symbol = symbol;
1316 cinfo.asymbols = bfd_get_outsymbols (abfd);
1317 bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
1318 return cinfo.found;
1321 /* This is called when there is a reference to a warning symbol. */
1323 static void
1324 warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1325 const char *warning,
1326 const char *symbol,
1327 bfd *abfd,
1328 asection *section,
1329 bfd_vma address)
1331 /* This is a hack to support warn_multiple_gp. FIXME: This should
1332 have a cleaner interface, but what? */
1333 if (!config.warn_multiple_gp
1334 && strcmp (warning, "using multiple gp values") == 0)
1335 return;
1337 if (section != NULL)
1338 einfo ("%P: %C: %s%s\n", abfd, section, address, _("warning: "), warning);
1339 else if (abfd == NULL)
1340 einfo ("%P: %s%s\n", _("warning: "), warning);
1341 else if (symbol == NULL)
1342 einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
1343 else if (!symbol_warning (warning, symbol, abfd))
1345 bfd *b;
1346 /* Search all input files for a reference to SYMBOL. */
1347 for (b = info->input_bfds; b; b = b->link.next)
1348 if (b != abfd && symbol_warning (warning, symbol, b))
1349 return;
1350 einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
1354 /* This is called by warning_callback for each section. It checks the
1355 relocs of the section to see if it can find a reference to the
1356 symbol which triggered the warning. If it can, it uses the reloc
1357 to give an error message with a file and line number. */
1359 static void
1360 warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
1362 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
1363 long relsize;
1364 arelent **relpp;
1365 long relcount;
1366 arelent **p, **pend;
1368 if (info->found)
1369 return;
1371 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1372 if (relsize < 0)
1373 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
1374 if (relsize == 0)
1375 return;
1377 relpp = (arelent **) xmalloc (relsize);
1378 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1379 if (relcount < 0)
1380 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
1382 p = relpp;
1383 pend = p + relcount;
1384 for (; p < pend && *p != NULL; p++)
1386 arelent *q = *p;
1388 if (q->sym_ptr_ptr != NULL
1389 && *q->sym_ptr_ptr != NULL
1390 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1392 /* We found a reloc for the symbol we are looking for. */
1393 einfo ("%P: %C: %s%s\n", abfd, sec, q->address, _("warning: "),
1394 info->warning);
1395 info->found = true;
1396 break;
1400 free (relpp);
1403 #if SUPPORT_ERROR_HANDLING_SCRIPT
1404 char * error_handling_script = NULL;
1405 #endif
1407 /* This is called when an undefined symbol is found. */
1409 static void
1410 undefined_symbol (struct bfd_link_info *info,
1411 const char *name,
1412 bfd *abfd,
1413 asection *section,
1414 bfd_vma address,
1415 bool error)
1417 static char *error_name;
1418 static unsigned int error_count;
1420 #define MAX_ERRORS_IN_A_ROW 5
1422 if (info->ignore_hash != NULL
1423 && bfd_hash_lookup (info->ignore_hash, name, false, false) != NULL)
1424 return;
1426 if (config.warn_once)
1428 /* Only warn once about a particular undefined symbol. */
1429 add_ignoresym (info, name);
1432 /* We never print more than a reasonable number of errors in a row
1433 for a single symbol. */
1434 if (error_name != NULL
1435 && strcmp (name, error_name) == 0)
1436 ++error_count;
1437 else
1439 error_count = 0;
1440 free (error_name);
1441 error_name = xstrdup (name);
1444 #if SUPPORT_ERROR_HANDLING_SCRIPT
1445 if (error_handling_script != NULL
1446 && error_count < MAX_ERRORS_IN_A_ROW)
1448 char * argv[4];
1449 const char * res;
1450 int status, err;
1452 argv[0] = error_handling_script;
1453 argv[1] = "undefined-symbol";
1454 argv[2] = (char *) name;
1455 argv[3] = NULL;
1457 if (verbose)
1458 einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
1459 argv[0], argv[1], argv[2]);
1461 res = pex_one (PEX_SEARCH, error_handling_script, argv,
1462 N_("error handling script"),
1463 NULL /* Send stdout to random, temp file. */,
1464 NULL /* Write to stderr. */,
1465 &status, &err);
1466 if (res != NULL)
1468 einfo (_("%P: Failed to run error handling script '%s', reason: "),
1469 error_handling_script);
1470 /* FIXME: We assume here that errrno == err. */
1471 perror (res);
1473 /* We ignore the return status of the script and
1474 carry on to issue the normal error message. */
1476 #endif /* SUPPORT_ERROR_HANDLING_SCRIPT */
1478 if (section != NULL)
1480 if (error_count < MAX_ERRORS_IN_A_ROW)
1482 if (error)
1483 einfo (_("%X%P: %C: undefined reference to `%pT'\n"),
1484 abfd, section, address, name);
1485 else
1486 einfo (_("%P: %C: warning: undefined reference to `%pT'\n"),
1487 abfd, section, address, name);
1489 else if (error_count == MAX_ERRORS_IN_A_ROW)
1491 if (error)
1492 einfo (_("%X%P: %D: more undefined references to `%pT' follow\n"),
1493 abfd, section, address, name);
1494 else
1495 einfo (_("%P: %D: warning: more undefined references to `%pT' follow\n"),
1496 abfd, section, address, name);
1498 else if (error)
1499 einfo ("%X");
1501 else
1503 if (error_count < MAX_ERRORS_IN_A_ROW)
1505 if (error)
1506 einfo (_("%X%P: %pB: undefined reference to `%pT'\n"),
1507 abfd, name);
1508 else
1509 einfo (_("%P: %pB: warning: undefined reference to `%pT'\n"),
1510 abfd, name);
1512 else if (error_count == MAX_ERRORS_IN_A_ROW)
1514 if (error)
1515 einfo (_("%X%P: %pB: more undefined references to `%pT' follow\n"),
1516 abfd, name);
1517 else
1518 einfo (_("%P: %pB: warning: more undefined references to `%pT' follow\n"),
1519 abfd, name);
1521 else if (error)
1522 einfo ("%X");
1526 /* Counter to limit the number of relocation overflow error messages
1527 to print. Errors are printed as it is decremented. When it's
1528 called and the counter is zero, a final message is printed
1529 indicating more relocations were omitted. When it gets to -1, no
1530 such errors are printed. If it's initially set to a value less
1531 than -1, all such errors will be printed (--verbose does this). */
1533 int overflow_cutoff_limit = 10;
1535 /* This is called when a reloc overflows. */
1537 static void
1538 reloc_overflow (struct bfd_link_info *info,
1539 struct bfd_link_hash_entry *entry,
1540 const char *name,
1541 const char *reloc_name,
1542 bfd_vma addend,
1543 bfd *abfd,
1544 asection *section,
1545 bfd_vma address)
1547 if (overflow_cutoff_limit == -1)
1548 return;
1550 einfo ("%X%H:", abfd, section, address);
1552 if (overflow_cutoff_limit >= 0
1553 && overflow_cutoff_limit-- == 0)
1555 einfo (_(" additional relocation overflows omitted from the output\n"));
1556 return;
1559 if (entry)
1561 while (entry->type == bfd_link_hash_indirect
1562 || entry->type == bfd_link_hash_warning)
1563 entry = entry->u.i.link;
1564 switch (entry->type)
1566 case bfd_link_hash_undefined:
1567 case bfd_link_hash_undefweak:
1568 einfo (_(" relocation truncated to fit: "
1569 "%s against undefined symbol `%pT'"),
1570 reloc_name, entry->root.string);
1571 break;
1572 case bfd_link_hash_defined:
1573 case bfd_link_hash_defweak:
1574 einfo (_(" relocation truncated to fit: "
1575 "%s against symbol `%pT' defined in %pA section in %pB"),
1576 reloc_name, entry->root.string,
1577 entry->u.def.section,
1578 entry->u.def.section == bfd_abs_section_ptr
1579 ? info->output_bfd : entry->u.def.section->owner);
1580 break;
1581 default:
1582 abort ();
1583 break;
1586 else
1587 einfo (_(" relocation truncated to fit: %s against `%pT'"),
1588 reloc_name, name);
1589 if (addend != 0)
1590 einfo ("+%v", addend);
1591 einfo ("\n");
1594 /* This is called when a dangerous relocation is made. */
1596 static void
1597 reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1598 const char *message,
1599 bfd *abfd,
1600 asection *section,
1601 bfd_vma address)
1603 einfo (_("%X%H: dangerous relocation: %s\n"),
1604 abfd, section, address, message);
1607 /* This is called when a reloc is being generated attached to a symbol
1608 that is not being output. */
1610 static void
1611 unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1612 const char *name,
1613 bfd *abfd,
1614 asection *section,
1615 bfd_vma address)
1617 einfo (_("%X%H: reloc refers to symbol `%pT' which is not being output\n"),
1618 abfd, section, address, name);
1621 /* This is called if link_info.notice_all is set, or when a symbol in
1622 link_info.notice_hash is found. Symbols are put in notice_hash
1623 using the -y option, while notice_all is set if the --cref option
1624 has been supplied, or if there are any NOCROSSREFS sections in the
1625 linker script; and if plugins are active, since they need to monitor
1626 all references from non-IR files. */
1628 static bool
1629 notice (struct bfd_link_info *info,
1630 struct bfd_link_hash_entry *h,
1631 struct bfd_link_hash_entry *inh ATTRIBUTE_UNUSED,
1632 bfd *abfd,
1633 asection *section,
1634 bfd_vma value,
1635 flagword flags ATTRIBUTE_UNUSED)
1637 const char *name;
1639 if (h == NULL)
1641 if (command_line.cref || nocrossref_list != NULL)
1642 return handle_asneeded_cref (abfd, (enum notice_asneeded_action) value);
1643 return true;
1646 name = h->root.string;
1647 if (info->notice_hash != NULL
1648 && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL)
1650 if (bfd_is_und_section (section))
1651 einfo (_("%P: %pB: reference to %s\n"), abfd, name);
1652 else
1653 einfo (_("%P: %pB: definition of %s\n"), abfd, name);
1656 if (command_line.cref || nocrossref_list != NULL)
1657 add_cref (name, abfd, section, value);
1659 return true;