readelf: Add -j/--display-section command line option.
[binutils-gdb.git] / ld / ldmain.c
blobfe389681bd3c3cad0b0919a82ee2b1430eb60eb6
1 /* Main program of GNU linker.
2 Copyright (C) 1991-2024 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 "bfdlink.h"
27 #include "ctf-api.h"
28 #include "filenames.h"
29 #include "elf/common.h"
31 #include "ld.h"
32 #include "ldmain.h"
33 #include "ldmisc.h"
34 #include "ldwrite.h"
35 #include "ldexp.h"
36 #include "ldlang.h"
37 #include <ldgram.h>
38 #include "ldlex.h"
39 #include "ldfile.h"
40 #include "ldemul.h"
41 #include "ldctor.h"
42 #if BFD_SUPPORTS_PLUGINS
43 #include "plugin.h"
44 #include "plugin-api.h"
45 #endif /* BFD_SUPPORTS_PLUGINS */
47 /* Somewhere above, sys/stat.h got included. */
48 #if !defined(S_ISDIR) && defined(S_IFDIR)
49 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
50 #endif
52 #include <string.h>
54 #ifndef TARGET_SYSTEM_ROOT
55 #define TARGET_SYSTEM_ROOT ""
56 #endif
58 /* EXPORTS */
60 FILE *saved_script_handle = NULL;
61 FILE *previous_script_handle = NULL;
62 bool force_make_executable = false;
64 char *default_target;
65 const char *output_filename = "a.out";
67 /* Name this program was invoked by. */
68 char *program_name;
70 /* The prefix for system library directories. */
71 const char *ld_sysroot;
73 /* The canonical representation of ld_sysroot. */
74 char *ld_canon_sysroot;
75 int ld_canon_sysroot_len;
77 /* Set by -G argument, for targets like MIPS ELF. */
78 int g_switch_value = 8;
80 /* Nonzero means print names of input files as processed. */
81 unsigned int trace_files;
83 /* Nonzero means report actions taken by the linker, and describe the linker script in use. */
84 bool verbose;
86 /* Nonzero means version number was printed, so exit successfully
87 instead of complaining if no input files are given. */
88 bool version_printed;
90 /* TRUE if we should demangle symbol names. */
91 bool demangling;
93 args_type command_line;
95 ld_config_type config;
97 sort_type sort_section;
99 static const char *get_sysroot
100 (int, char **);
101 static char *get_emulation
102 (int, char **);
103 static bool add_archive_element
104 (struct bfd_link_info *, bfd *, const char *, bfd **);
105 static void multiple_definition
106 (struct bfd_link_info *, struct bfd_link_hash_entry *,
107 bfd *, asection *, bfd_vma);
108 static void multiple_common
109 (struct bfd_link_info *, struct bfd_link_hash_entry *,
110 bfd *, enum bfd_link_hash_type, bfd_vma);
111 static void add_to_set
112 (struct bfd_link_info *, struct bfd_link_hash_entry *,
113 bfd_reloc_code_real_type, bfd *, asection *, bfd_vma);
114 static void constructor_callback
115 (struct bfd_link_info *, bool, const char *, bfd *,
116 asection *, bfd_vma);
117 static void warning_callback
118 (struct bfd_link_info *, const char *, const char *, bfd *,
119 asection *, bfd_vma);
120 static void warning_find_reloc
121 (bfd *, asection *, void *);
122 static void undefined_symbol
123 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
124 bool);
125 static void reloc_overflow
126 (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
127 const char *, bfd_vma, bfd *, asection *, bfd_vma);
128 static void reloc_dangerous
129 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
130 static void unattached_reloc
131 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
132 static bool notice
133 (struct bfd_link_info *, struct bfd_link_hash_entry *,
134 struct bfd_link_hash_entry *, bfd *, asection *, bfd_vma, flagword);
136 static struct bfd_link_callbacks link_callbacks =
138 add_archive_element,
139 multiple_definition,
140 multiple_common,
141 add_to_set,
142 constructor_callback,
143 warning_callback,
144 undefined_symbol,
145 reloc_overflow,
146 reloc_dangerous,
147 unattached_reloc,
148 notice,
149 einfo,
150 info_msg,
151 minfo,
152 ldlang_override_segment_assignment,
153 ldlang_ctf_acquire_strings,
154 NULL,
155 ldlang_ctf_new_dynsym,
156 ldlang_write_ctf_late
159 static bfd_assert_handler_type default_bfd_assert_handler;
160 static bfd_error_handler_type default_bfd_error_handler;
162 struct bfd_link_info link_info;
164 struct dependency_file
166 struct dependency_file *next;
167 char *name;
170 static struct dependency_file *dependency_files, *dependency_files_tail;
172 void
173 track_dependency_files (const char *filename)
175 struct dependency_file *dep
176 = (struct dependency_file *) xmalloc (sizeof (*dep));
177 dep->name = xstrdup (filename);
178 dep->next = NULL;
179 if (dependency_files == NULL)
180 dependency_files = dep;
181 else
182 dependency_files_tail->next = dep;
183 dependency_files_tail = dep;
186 static void
187 write_dependency_file (void)
189 FILE *out;
190 struct dependency_file *dep;
192 out = fopen (config.dependency_file, FOPEN_WT);
193 if (out == NULL)
195 einfo (_("%F%P: cannot open dependency file %s: %E\n"),
196 config.dependency_file);
199 fprintf (out, "%s:", output_filename);
201 for (dep = dependency_files; dep != NULL; dep = dep->next)
202 fprintf (out, " \\\n %s", dep->name);
204 fprintf (out, "\n");
205 for (dep = dependency_files; dep != NULL; dep = dep->next)
206 fprintf (out, "\n%s:\n", dep->name);
208 fclose (out);
211 static void
212 ld_cleanup (void)
214 bfd *ibfd, *inext;
215 if (link_info.output_bfd)
216 bfd_close_all_done (link_info.output_bfd);
217 for (ibfd = link_info.input_bfds; ibfd; ibfd = inext)
219 inext = ibfd->link.next;
220 bfd_close_all_done (ibfd);
222 #if BFD_SUPPORTS_PLUGINS
223 plugin_call_cleanup ();
224 #endif
225 if (output_filename && delete_output_file_on_failure)
226 unlink_if_ordinary (output_filename);
229 /* Hook to notice BFD assertions. */
231 static void
232 ld_bfd_assert_handler (const char *fmt, const char *bfdver,
233 const char *file, int line)
235 config.make_executable = false;
236 (*default_bfd_assert_handler) (fmt, bfdver, file, line);
239 /* Hook the bfd error/warning handler for --fatal-warnings. */
241 static void
242 ld_bfd_error_handler (const char *fmt, va_list ap)
244 if (config.fatal_warnings)
245 config.make_executable = false;
246 (*default_bfd_error_handler) (fmt, ap);
250 main (int argc, char **argv)
252 char *emulation;
253 long start_time = get_run_time ();
255 #ifdef HAVE_LC_MESSAGES
256 setlocale (LC_MESSAGES, "");
257 #endif
258 setlocale (LC_CTYPE, "");
259 bindtextdomain (PACKAGE, LOCALEDIR);
260 textdomain (PACKAGE);
262 program_name = argv[0];
263 xmalloc_set_program_name (program_name);
265 expandargv (&argc, &argv);
267 if (bfd_init () != BFD_INIT_MAGIC)
268 einfo (_("%F%P: fatal error: libbfd ABI mismatch\n"));
270 bfd_set_error_program_name (program_name);
272 /* We want to notice and fail on those nasty BFD assertions which are
273 likely to signal incorrect output being generated but otherwise may
274 leave no trace. */
275 default_bfd_assert_handler = bfd_set_assert_handler (ld_bfd_assert_handler);
277 /* Also hook the bfd error/warning handler for --fatal-warnings. */
278 default_bfd_error_handler = bfd_set_error_handler (ld_bfd_error_handler);
280 xatexit (ld_cleanup);
282 /* Set up the sysroot directory. */
283 ld_sysroot = get_sysroot (argc, argv);
284 if (*ld_sysroot)
285 ld_canon_sysroot = lrealpath (ld_sysroot);
286 if (ld_canon_sysroot)
288 ld_canon_sysroot_len = strlen (ld_canon_sysroot);
290 /* is_sysrooted_pathname() relies on no trailing dirsep. */
291 if (ld_canon_sysroot_len > 0
292 && IS_DIR_SEPARATOR (ld_canon_sysroot [ld_canon_sysroot_len - 1]))
293 ld_canon_sysroot [--ld_canon_sysroot_len] = '\0';
295 else
296 ld_canon_sysroot_len = -1;
298 /* Set the default BFD target based on the configured target. Doing
299 this permits the linker to be configured for a particular target,
300 and linked against a shared BFD library which was configured for
301 a different target. The macro TARGET is defined by Makefile. */
302 if (!bfd_set_default_target (TARGET))
304 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
305 xexit (1);
308 #if YYDEBUG
310 extern int yydebug;
311 yydebug = 1;
313 #endif
315 config.build_constructors = true;
316 config.rpath_separator = ':';
317 config.split_by_reloc = (unsigned) -1;
318 config.split_by_file = (bfd_size_type) -1;
319 config.make_executable = true;
320 config.magic_demand_paged = true;
321 config.text_read_only = true;
322 config.print_map_discarded = true;
323 link_info.disable_target_specific_optimizations = -1;
325 command_line.warn_mismatch = true;
326 command_line.warn_search_mismatch = true;
327 command_line.check_section_addresses = -1;
329 /* We initialize DEMANGLING based on the environment variable
330 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
331 output of the linker, unless COLLECT_NO_DEMANGLE is set in the
332 environment. Acting the same way here lets us provide the same
333 interface by default. */
334 demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
336 link_info.allow_undefined_version = true;
337 link_info.keep_memory = true;
338 link_info.max_cache_size = (bfd_size_type) -1;
339 link_info.combreloc = true;
340 link_info.strip_discarded = true;
341 link_info.prohibit_multiple_definition_absolute = false;
342 link_info.textrel_check = DEFAULT_LD_TEXTREL_CHECK;
343 link_info.emit_hash = DEFAULT_EMIT_SYSV_HASH;
344 link_info.emit_gnu_hash = DEFAULT_EMIT_GNU_HASH;
345 link_info.callbacks = &link_callbacks;
346 link_info.input_bfds_tail = &link_info.input_bfds;
347 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
348 and _fini symbols. We are compatible. */
349 link_info.init_function = "_init";
350 link_info.fini_function = "_fini";
351 link_info.relax_pass = 1;
352 link_info.extern_protected_data = -1;
353 link_info.dynamic_undefined_weak = -1;
354 link_info.indirect_extern_access = -1;
355 link_info.pei386_auto_import = -1;
356 link_info.spare_dynamic_tags = 5;
357 link_info.path_separator = ':';
358 #ifdef DEFAULT_FLAG_COMPRESS_DEBUG
359 config.compress_debug = DEFAULT_COMPRESSED_DEBUG_ALGORITHM;
360 #endif
361 #ifdef DEFAULT_NEW_DTAGS
362 link_info.new_dtags = DEFAULT_NEW_DTAGS;
363 #endif
364 link_info.start_stop_gc = false;
365 link_info.start_stop_visibility = STV_PROTECTED;
367 ldfile_add_arch ("");
368 emulation = get_emulation (argc, argv);
369 ldemul_choose_mode (emulation);
370 default_target = ldemul_choose_target (argc, argv);
371 lang_init ();
372 ldexp_init ();
373 ldemul_before_parse ();
374 lang_has_input_file = false;
375 parse_args (argc, argv);
377 if (config.hash_table_size != 0)
378 bfd_hash_set_default_size (config.hash_table_size);
380 #if BFD_SUPPORTS_PLUGINS
381 /* Now all the plugin arguments have been gathered, we can load them. */
382 plugin_load_plugins ();
383 #endif /* BFD_SUPPORTS_PLUGINS */
385 ldemul_set_symbols ();
387 /* If we have not already opened and parsed a linker script,
388 try the default script from command line first. */
389 if (saved_script_handle == NULL
390 && command_line.default_script != NULL)
392 ldfile_open_script_file (command_line.default_script);
393 parser_input = input_script;
394 yyparse ();
397 /* If we have not already opened and parsed a linker script
398 read the emulation's appropriate default script. */
399 if (saved_script_handle == NULL)
401 int isfile;
402 char *s = ldemul_get_script (&isfile);
404 if (isfile)
405 ldfile_open_default_command_file (s);
406 else
408 lex_string = s;
409 lex_redirect (s, _("built in linker script"), 1);
411 parser_input = input_script;
412 yyparse ();
413 lex_string = NULL;
416 if (verbose)
418 if (saved_script_handle)
419 info_msg (_("using external linker script:"));
420 else
421 info_msg (_("using internal linker script:"));
422 info_msg ("\n==================================================\n");
424 if (saved_script_handle)
426 static const int ld_bufsz = 8193;
427 size_t n;
428 char *buf = (char *) xmalloc (ld_bufsz);
430 rewind (saved_script_handle);
431 while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
433 buf[n] = 0;
434 info_msg ("%s", buf);
436 rewind (saved_script_handle);
437 free (buf);
439 else
441 int isfile;
443 info_msg (ldemul_get_script (&isfile));
446 info_msg ("\n==================================================\n");
449 if (command_line.force_group_allocation
450 || !bfd_link_relocatable (&link_info))
451 link_info.resolve_section_groups = true;
452 else
453 link_info.resolve_section_groups = false;
455 if (command_line.print_output_format)
456 info_msg ("%s\n", lang_get_output_target ());
458 lang_final ();
460 /* If the only command line argument has been -v or --version or --verbose
461 then ignore any input files provided by linker scripts and exit now.
462 We do not want to create an output file when the linker is just invoked
463 to provide version information. */
464 if (argc == 2 && version_printed)
465 xexit (0);
467 if (link_info.inhibit_common_definition && !bfd_link_dll (&link_info))
468 einfo (_("%F%P: --no-define-common may not be used without -shared\n"));
470 if (!lang_has_input_file)
472 if (version_printed || command_line.print_output_format)
473 xexit (0);
474 output_unknown_cmdline_warnings ();
475 einfo (_("%F%P: no input files\n"));
478 if (verbose)
479 info_msg (_("%P: mode %s\n"), emulation);
481 ldemul_after_parse ();
483 output_unknown_cmdline_warnings ();
485 if (config.map_filename)
487 if (strcmp (config.map_filename, "-") == 0)
489 config.map_file = stdout;
491 else
493 config.map_file = fopen (config.map_filename, FOPEN_WT);
494 if (config.map_file == (FILE *) NULL)
496 bfd_set_error (bfd_error_system_call);
497 einfo (_("%F%P: cannot open map file %s: %E\n"),
498 config.map_filename);
501 link_info.has_map_file = true;
504 lang_process ();
506 /* Print error messages for any missing symbols, for any warning
507 symbols, and possibly multiple definitions. */
508 if (bfd_link_relocatable (&link_info))
509 link_info.output_bfd->flags &= ~EXEC_P;
510 else
511 link_info.output_bfd->flags |= EXEC_P;
513 flagword flags = 0;
514 switch (config.compress_debug)
516 case COMPRESS_DEBUG_GNU_ZLIB:
517 flags = BFD_COMPRESS;
518 break;
519 case COMPRESS_DEBUG_GABI_ZLIB:
520 flags = BFD_COMPRESS | BFD_COMPRESS_GABI;
521 break;
522 case COMPRESS_DEBUG_ZSTD:
523 flags = BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD;
524 break;
525 default:
526 break;
528 link_info.output_bfd->flags
529 |= flags & bfd_applicable_file_flags (link_info.output_bfd);
531 ldwrite ();
533 if (config.map_file != NULL)
534 lang_map ();
535 if (command_line.cref)
536 output_cref (config.map_file != NULL ? config.map_file : stdout);
537 if (nocrossref_list != NULL)
538 check_nocrossrefs ();
539 if (command_line.print_memory_usage)
540 lang_print_memory_usage ();
541 #if 0
543 struct bfd_link_hash_entry *h;
545 h = bfd_link_hash_lookup (link_info.hash, "__image_base__", 0,0,1);
546 fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1);
548 #endif
549 ldexp_finish ();
550 lang_finish ();
552 if (config.dependency_file != NULL)
553 write_dependency_file ();
555 /* Even if we're producing relocatable output, some non-fatal errors should
556 be reported in the exit status. (What non-fatal errors, if any, do we
557 want to ignore for relocatable output?) */
558 if (!config.make_executable && !force_make_executable)
560 if (verbose)
561 einfo (_("%P: link errors found, deleting executable `%s'\n"),
562 output_filename);
564 /* The file will be removed by ld_cleanup. */
565 xexit (1);
567 else
569 bfd *obfd = link_info.output_bfd;
570 link_info.output_bfd = NULL;
571 if (!bfd_close (obfd))
572 einfo (_("%F%P: %s: final close failed: %E\n"), output_filename);
574 /* If the --force-exe-suffix is enabled, and we're making an
575 executable file and it doesn't end in .exe, copy it to one
576 which does. */
577 if (!bfd_link_relocatable (&link_info)
578 && command_line.force_exe_suffix)
580 int len = strlen (output_filename);
582 if (len < 4
583 || (strcasecmp (output_filename + len - 4, ".exe") != 0
584 && strcasecmp (output_filename + len - 4, ".dll") != 0))
586 FILE *src;
587 FILE *dst;
588 const int bsize = 4096;
589 char *buf = (char *) xmalloc (bsize);
590 int l;
591 char *dst_name = (char *) xmalloc (len + 5);
593 strcpy (dst_name, output_filename);
594 strcat (dst_name, ".exe");
595 src = fopen (output_filename, FOPEN_RB);
596 dst = fopen (dst_name, FOPEN_WB);
598 if (!src)
599 einfo (_("%F%P: unable to open for source of copy `%s'\n"),
600 output_filename);
601 if (!dst)
602 einfo (_("%F%P: unable to open for destination of copy `%s'\n"),
603 dst_name);
604 while ((l = fread (buf, 1, bsize, src)) > 0)
606 int done = fwrite (buf, 1, l, dst);
608 if (done != l)
609 einfo (_("%P: error writing file `%s'\n"), dst_name);
612 fclose (src);
613 if (fclose (dst) == EOF)
614 einfo (_("%P: error closing file `%s'\n"), dst_name);
615 free (dst_name);
616 free (buf);
621 if (config.stats)
623 long run_time = get_run_time () - start_time;
625 fflush (stdout);
626 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
627 program_name, run_time / 1000000, run_time % 1000000);
628 fflush (stderr);
631 /* Prevent ld_cleanup from deleting the output file. */
632 output_filename = NULL;
634 xexit (0);
635 return 0;
638 /* If the configured sysroot is relocatable, try relocating it based on
639 default prefix FROM. Return the relocated directory if it exists,
640 otherwise return null. */
642 static char *
643 get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
645 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
646 char *path;
647 struct stat s;
649 path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
650 if (path)
652 if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
653 return path;
654 free (path);
656 #endif
657 return 0;
660 /* Return the sysroot directory. Return "" if no sysroot is being used. */
662 static const char *
663 get_sysroot (int argc, char **argv)
665 int i;
666 const char *path = NULL;
668 for (i = 1; i < argc; i++)
669 if (startswith (argv[i], "--sysroot="))
670 path = argv[i] + strlen ("--sysroot=");
672 if (!path)
673 path = get_relative_sysroot (BINDIR);
675 if (!path)
676 path = get_relative_sysroot (TOOLBINDIR);
678 if (!path)
679 path = TARGET_SYSTEM_ROOT;
681 if (IS_DIR_SEPARATOR (*path) && path[1] == 0)
682 path = "";
684 return path;
687 /* We need to find any explicitly given emulation in order to initialize the
688 state that's needed by the lex&yacc argument parser (parse_args). */
690 static char *
691 get_emulation (int argc, char **argv)
693 char *emulation;
694 int i;
696 emulation = getenv (EMULATION_ENVIRON);
697 if (emulation == NULL)
698 emulation = DEFAULT_EMULATION;
700 for (i = 1; i < argc; i++)
702 if (startswith (argv[i], "-m"))
704 if (argv[i][2] == '\0')
706 /* -m EMUL */
707 if (i < argc - 1)
709 emulation = argv[i + 1];
710 i++;
712 else
713 einfo (_("%F%P: missing argument to -m\n"));
715 else if (strcmp (argv[i], "-mips1") == 0
716 || strcmp (argv[i], "-mips2") == 0
717 || strcmp (argv[i], "-mips3") == 0
718 || strcmp (argv[i], "-mips4") == 0
719 || strcmp (argv[i], "-mips5") == 0
720 || strcmp (argv[i], "-mips32") == 0
721 || strcmp (argv[i], "-mips32r2") == 0
722 || strcmp (argv[i], "-mips32r3") == 0
723 || strcmp (argv[i], "-mips32r5") == 0
724 || strcmp (argv[i], "-mips32r6") == 0
725 || strcmp (argv[i], "-mips64") == 0
726 || strcmp (argv[i], "-mips64r2") == 0
727 || strcmp (argv[i], "-mips64r3") == 0
728 || strcmp (argv[i], "-mips64r5") == 0
729 || strcmp (argv[i], "-mips64r6") == 0)
731 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
732 passed to the linker by some MIPS compilers. They
733 generally tell the linker to use a slightly different
734 library path. Perhaps someday these should be
735 implemented as emulations; until then, we just ignore
736 the arguments and hope that nobody ever creates
737 emulations named ips1, ips2 or ips3. */
739 else if (strcmp (argv[i], "-m486") == 0)
741 /* FIXME: The argument -m486 is passed to the linker on
742 some Linux systems. Hope that nobody creates an
743 emulation named 486. */
745 else
747 /* -mEMUL */
748 emulation = &argv[i][2];
753 return emulation;
756 void
757 add_ysym (const char *name)
759 if (link_info.notice_hash == NULL)
761 link_info.notice_hash
762 = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
763 if (!bfd_hash_table_init_n (link_info.notice_hash,
764 bfd_hash_newfunc,
765 sizeof (struct bfd_hash_entry),
766 61))
767 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
770 if (bfd_hash_lookup (link_info.notice_hash, name, true, true) == NULL)
771 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
774 void
775 add_ignoresym (struct bfd_link_info *info, const char *name)
777 if (info->ignore_hash == NULL)
779 info->ignore_hash = xmalloc (sizeof (struct bfd_hash_table));
780 if (!bfd_hash_table_init_n (info->ignore_hash,
781 bfd_hash_newfunc,
782 sizeof (struct bfd_hash_entry),
783 61))
784 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
787 if (bfd_hash_lookup (info->ignore_hash, name, true, true) == NULL)
788 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
791 /* Record a symbol to be wrapped, from the --wrap option. */
793 void
794 add_wrap (const char *name)
796 if (link_info.wrap_hash == NULL)
798 link_info.wrap_hash
799 = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
800 if (!bfd_hash_table_init_n (link_info.wrap_hash,
801 bfd_hash_newfunc,
802 sizeof (struct bfd_hash_entry),
803 61))
804 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
807 if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
808 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
811 /* Handle the -retain-symbols-file option. */
813 void
814 add_keepsyms_file (const char *filename)
816 FILE *file;
817 char *buf;
818 size_t bufsize;
819 int c;
821 if (link_info.strip == strip_some)
822 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
824 file = fopen (filename, "r");
825 if (file == NULL)
827 bfd_set_error (bfd_error_system_call);
828 einfo ("%X%P: %s: %E\n", filename);
829 return;
832 link_info.keep_hash = (struct bfd_hash_table *)
833 xmalloc (sizeof (struct bfd_hash_table));
834 if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc,
835 sizeof (struct bfd_hash_entry)))
836 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
838 bufsize = 100;
839 buf = (char *) xmalloc (bufsize);
841 c = getc (file);
842 while (c != EOF)
844 while (ISSPACE (c))
845 c = getc (file);
847 if (c != EOF)
849 size_t len = 0;
851 while (!ISSPACE (c) && c != EOF)
853 buf[len] = c;
854 ++len;
855 if (len >= bufsize)
857 bufsize *= 2;
858 buf = (char *) xrealloc (buf, bufsize);
860 c = getc (file);
863 buf[len] = '\0';
865 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true) == NULL)
866 einfo (_("%F%P: bfd_hash_lookup for insertion failed: %E\n"));
870 if (link_info.strip != strip_none)
871 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
873 free (buf);
874 link_info.strip = strip_some;
875 fclose (file);
878 /* Callbacks from the BFD linker routines. */
880 /* This is called when BFD has decided to include an archive member in
881 a link. */
883 static bool
884 add_archive_element (struct bfd_link_info *info,
885 bfd *abfd,
886 const char *name,
887 bfd **subsbfd ATTRIBUTE_UNUSED)
889 lang_input_statement_type *input;
890 lang_input_statement_type *parent;
891 lang_input_statement_type orig_input;
893 input = (lang_input_statement_type *)
894 xcalloc (1, sizeof (lang_input_statement_type));
895 input->header.type = lang_input_statement_enum;
896 input->filename = bfd_get_filename (abfd);
897 input->local_sym_name = bfd_get_filename (abfd);
898 input->the_bfd = abfd;
900 /* Save the original data for trace files/tries below, as plugins
901 (if enabled) may possibly alter it to point to a replacement
902 BFD, but we still want to output the original BFD filename. */
903 orig_input = *input;
904 #if BFD_SUPPORTS_PLUGINS
905 /* Don't claim a fat IR object if no IR object should be claimed. */
906 if (link_info.lto_plugin_active
907 && (!no_more_claiming
908 || bfd_get_lto_type (abfd) != lto_fat_ir_object))
910 /* We must offer this archive member to the plugins to claim. */
911 plugin_maybe_claim (input);
912 if (input->flags.claimed)
914 if (no_more_claiming)
916 /* Don't claim new IR symbols after all IR symbols have
917 been claimed. */
918 if (verbose)
919 info_msg ("%pI: no new IR symbols to claim\n",
920 &orig_input);
921 input->flags.claimed = 0;
922 return false;
924 input->flags.claim_archive = true;
925 *subsbfd = input->the_bfd;
928 #endif /* BFD_SUPPORTS_PLUGINS */
930 if (link_info.input_bfds_tail == &input->the_bfd->link.next
931 || input->the_bfd->link.next != NULL)
933 /* We have already loaded this element, and are attempting to
934 load it again. This can happen when the archive map doesn't
935 match actual symbols defined by the element. */
936 free (input);
937 bfd_set_error (bfd_error_malformed_archive);
938 return false;
941 /* Set the file_chain pointer of archives to the last element loaded
942 from the archive. See ldlang.c:find_rescan_insertion. */
943 parent = bfd_usrdata (abfd->my_archive);
944 if (parent != NULL && !parent->flags.reload)
945 parent->next = input;
947 ldlang_add_file (input);
949 if (config.map_file != NULL)
951 static bool header_printed;
952 struct bfd_link_hash_entry *h;
953 bfd *from;
954 int len;
956 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
957 if (h == NULL
958 && info->pei386_auto_import
959 && startswith (name, "__imp_"))
960 h = bfd_link_hash_lookup (info->hash, name + 6, false, false, true);
962 if (h == NULL)
963 from = NULL;
964 else
966 switch (h->type)
968 default:
969 from = NULL;
970 break;
972 case bfd_link_hash_defined:
973 case bfd_link_hash_defweak:
974 from = h->u.def.section->owner;
975 break;
977 case bfd_link_hash_undefined:
978 case bfd_link_hash_undefweak:
979 from = h->u.undef.abfd;
980 break;
982 case bfd_link_hash_common:
983 from = h->u.c.p->section->owner;
984 break;
988 if (!header_printed)
990 minfo (_("Archive member included to satisfy reference by file (symbol)\n\n"));
991 header_printed = true;
994 if (abfd->my_archive == NULL
995 || bfd_is_thin_archive (abfd->my_archive))
997 minfo ("%s", bfd_get_filename (abfd));
998 len = strlen (bfd_get_filename (abfd));
1000 else
1002 minfo ("%s(%s)", bfd_get_filename (abfd->my_archive),
1003 bfd_get_filename (abfd));
1004 len = (strlen (bfd_get_filename (abfd->my_archive))
1005 + strlen (bfd_get_filename (abfd))
1006 + 2);
1009 if (len >= 29)
1011 print_nl ();
1012 len = 0;
1014 print_spaces (30 - len);
1016 if (from != NULL)
1017 minfo ("%pB ", from);
1018 if (h != NULL)
1019 minfo ("(%pT)\n", h->root.string);
1020 else
1021 minfo ("(%s)\n", name);
1024 if (verbose
1025 || trace_files > 1
1026 || (trace_files && bfd_is_thin_archive (orig_input.the_bfd->my_archive)))
1027 info_msg ("%pI\n", &orig_input);
1028 return true;
1031 /* This is called when BFD has discovered a symbol which is defined
1032 multiple times. */
1034 static void
1035 multiple_definition (struct bfd_link_info *info,
1036 struct bfd_link_hash_entry *h,
1037 bfd *nbfd,
1038 asection *nsec,
1039 bfd_vma nval)
1041 const char *name;
1042 bfd *obfd;
1043 asection *osec;
1044 bfd_vma oval;
1046 if (info->allow_multiple_definition)
1047 return;
1049 switch (h->type)
1051 case bfd_link_hash_defined:
1052 osec = h->u.def.section;
1053 oval = h->u.def.value;
1054 obfd = h->u.def.section->owner;
1055 break;
1056 case bfd_link_hash_indirect:
1057 osec = bfd_ind_section_ptr;
1058 oval = 0;
1059 obfd = NULL;
1060 break;
1061 default:
1062 abort ();
1065 /* Ignore a redefinition of an absolute symbol to the
1066 same value; it's harmless. */
1067 if (h->type == bfd_link_hash_defined
1068 && bfd_is_abs_section (osec)
1069 && bfd_is_abs_section (nsec)
1070 && nval == oval)
1071 return;
1073 /* If either section has the output_section field set to
1074 bfd_abs_section_ptr, it means that the section is being
1075 discarded, and this is not really a multiple definition at all.
1076 FIXME: It would be cleaner to somehow ignore symbols defined in
1077 sections which are being discarded. */
1078 if (!info->prohibit_multiple_definition_absolute
1079 && ((osec->output_section != NULL
1080 && ! bfd_is_abs_section (osec)
1081 && bfd_is_abs_section (osec->output_section))
1082 || (nsec->output_section != NULL
1083 && !bfd_is_abs_section (nsec)
1084 && bfd_is_abs_section (nsec->output_section))))
1085 return;
1087 name = h->root.string;
1088 if (nbfd == NULL)
1090 nbfd = obfd;
1091 nsec = osec;
1092 nval = oval;
1093 obfd = NULL;
1095 if (info->warn_multiple_definition)
1096 einfo (_("%P: %C: warning: multiple definition of `%pT'"),
1097 nbfd, nsec, nval, name);
1098 else
1099 einfo (_("%X%P: %C: multiple definition of `%pT'"),
1100 nbfd, nsec, nval, name);
1101 if (obfd != NULL)
1102 einfo (_("; %D: first defined here"), obfd, osec, oval);
1103 einfo ("\n");
1105 if (RELAXATION_ENABLED_BY_USER)
1107 einfo (_("%P: disabling relaxation; it will not work with multiple definitions\n"));
1108 DISABLE_RELAXATION;
1112 /* This is called when there is a definition of a common symbol, or
1113 when a common symbol is found for a symbol that is already defined,
1114 or when two common symbols are found. We only do something if
1115 -warn-common was used. */
1117 static void
1118 multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1119 struct bfd_link_hash_entry *h,
1120 bfd *nbfd,
1121 enum bfd_link_hash_type ntype,
1122 bfd_vma nsize)
1124 const char *name;
1125 bfd *obfd;
1126 enum bfd_link_hash_type otype;
1127 bfd_vma osize;
1129 if (!config.warn_common)
1130 return;
1132 name = h->root.string;
1133 otype = h->type;
1134 if (otype == bfd_link_hash_common)
1136 obfd = h->u.c.p->section->owner;
1137 osize = h->u.c.size;
1139 else if (otype == bfd_link_hash_defined
1140 || otype == bfd_link_hash_defweak)
1142 obfd = h->u.def.section->owner;
1143 osize = 0;
1145 else
1147 /* FIXME: It would nice if we could report the BFD which defined
1148 an indirect symbol, but we don't have anywhere to store the
1149 information. */
1150 obfd = NULL;
1151 osize = 0;
1154 if (ntype == bfd_link_hash_defined
1155 || ntype == bfd_link_hash_defweak
1156 || ntype == bfd_link_hash_indirect)
1158 ASSERT (otype == bfd_link_hash_common);
1159 if (obfd != NULL)
1160 einfo (_("%P: %pB: warning: definition of `%pT' overriding common"
1161 " from %pB\n"),
1162 nbfd, name, obfd);
1163 else
1164 einfo (_("%P: %pB: warning: definition of `%pT' overriding common\n"),
1165 nbfd, name);
1167 else if (otype == bfd_link_hash_defined
1168 || otype == bfd_link_hash_defweak
1169 || otype == bfd_link_hash_indirect)
1171 ASSERT (ntype == bfd_link_hash_common);
1172 if (obfd != NULL)
1173 einfo (_("%P: %pB: warning: common of `%pT' overridden by definition"
1174 " from %pB\n"),
1175 nbfd, name, obfd);
1176 else
1177 einfo (_("%P: %pB: warning: common of `%pT' overridden by definition\n"),
1178 nbfd, name);
1180 else
1182 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
1183 if (osize > nsize)
1185 if (obfd != NULL)
1186 einfo (_("%P: %pB: warning: common of `%pT' overridden"
1187 " by larger common from %pB\n"),
1188 nbfd, name, obfd);
1189 else
1190 einfo (_("%P: %pB: warning: common of `%pT' overridden"
1191 " by larger common\n"),
1192 nbfd, name);
1194 else if (nsize > osize)
1196 if (obfd != NULL)
1197 einfo (_("%P: %pB: warning: common of `%pT' overriding"
1198 " smaller common from %pB\n"),
1199 nbfd, name, obfd);
1200 else
1201 einfo (_("%P: %pB: warning: common of `%pT' overriding"
1202 " smaller common\n"),
1203 nbfd, name);
1205 else
1207 if (obfd != NULL)
1208 einfo (_("%P: %pB and %pB: warning: multiple common of `%pT'\n"),
1209 nbfd, obfd, name);
1210 else
1211 einfo (_("%P: %pB: warning: multiple common of `%pT'\n"),
1212 nbfd, name);
1217 /* This is called when BFD has discovered a set element. H is the
1218 entry in the linker hash table for the set. SECTION and VALUE
1219 represent a value which should be added to the set. */
1221 static void
1222 add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1223 struct bfd_link_hash_entry *h,
1224 bfd_reloc_code_real_type reloc,
1225 bfd *abfd,
1226 asection *section,
1227 bfd_vma value)
1229 if (config.warn_constructors)
1230 einfo (_("%P: warning: global constructor %s used\n"),
1231 h->root.string);
1233 if (!config.build_constructors)
1234 return;
1236 ldctor_add_set_entry (h, reloc, NULL, section, value);
1238 if (h->type == bfd_link_hash_new)
1240 h->type = bfd_link_hash_undefined;
1241 h->u.undef.abfd = abfd;
1242 /* We don't call bfd_link_add_undef to add this to the list of
1243 undefined symbols because we are going to define it
1244 ourselves. */
1248 /* This is called when BFD has discovered a constructor. This is only
1249 called for some object file formats--those which do not handle
1250 constructors in some more clever fashion. This is similar to
1251 adding an element to a set, but less general. */
1253 static void
1254 constructor_callback (struct bfd_link_info *info,
1255 bool constructor,
1256 const char *name,
1257 bfd *abfd,
1258 asection *section,
1259 bfd_vma value)
1261 char *s;
1262 struct bfd_link_hash_entry *h;
1263 char set_name[1 + sizeof "__CTOR_LIST__"];
1265 if (config.warn_constructors)
1266 einfo (_("%P: warning: global constructor %s used\n"), name);
1268 if (!config.build_constructors)
1269 return;
1271 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1272 useful error message. */
1273 if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL
1274 && (bfd_link_relocatable (info)
1275 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
1276 einfo (_("%F%P: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1278 s = set_name;
1279 if (bfd_get_symbol_leading_char (abfd) != '\0')
1280 *s++ = bfd_get_symbol_leading_char (abfd);
1281 if (constructor)
1282 strcpy (s, "__CTOR_LIST__");
1283 else
1284 strcpy (s, "__DTOR_LIST__");
1286 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
1287 if (h == (struct bfd_link_hash_entry *) NULL)
1288 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1289 if (h->type == bfd_link_hash_new)
1291 h->type = bfd_link_hash_undefined;
1292 h->u.undef.abfd = abfd;
1293 /* We don't call bfd_link_add_undef to add this to the list of
1294 undefined symbols because we are going to define it
1295 ourselves. */
1298 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
1301 /* A structure used by warning_callback to pass information through
1302 bfd_map_over_sections. */
1304 struct warning_callback_info
1306 bool found;
1307 const char *warning;
1308 const char *symbol;
1309 asymbol **asymbols;
1312 /* Look through the relocs to see if we can find a plausible address
1313 for SYMBOL in ABFD. Return TRUE if found. Otherwise return FALSE. */
1315 static bool
1316 symbol_warning (const char *warning, const char *symbol, bfd *abfd)
1318 struct warning_callback_info cinfo;
1320 if (!bfd_generic_link_read_symbols (abfd))
1321 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd);
1323 cinfo.found = false;
1324 cinfo.warning = warning;
1325 cinfo.symbol = symbol;
1326 cinfo.asymbols = bfd_get_outsymbols (abfd);
1327 bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
1328 return cinfo.found;
1331 /* This is called when there is a reference to a warning symbol. */
1333 static void
1334 warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1335 const char *warning,
1336 const char *symbol,
1337 bfd *abfd,
1338 asection *section,
1339 bfd_vma address)
1341 /* This is a hack to support warn_multiple_gp. FIXME: This should
1342 have a cleaner interface, but what? */
1343 if (!config.warn_multiple_gp
1344 && strcmp (warning, "using multiple gp values") == 0)
1345 return;
1347 if (section != NULL)
1348 einfo ("%P: %C: %s%s\n", abfd, section, address, _("warning: "), warning);
1349 else if (abfd == NULL)
1350 einfo ("%P: %s%s\n", _("warning: "), warning);
1351 else if (symbol == NULL)
1352 einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
1353 else if (!symbol_warning (warning, symbol, abfd))
1355 bfd *b;
1356 /* Search all input files for a reference to SYMBOL. */
1357 for (b = info->input_bfds; b; b = b->link.next)
1358 if (b != abfd && symbol_warning (warning, symbol, b))
1359 return;
1360 einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
1364 /* This is called by warning_callback for each section. It checks the
1365 relocs of the section to see if it can find a reference to the
1366 symbol which triggered the warning. If it can, it uses the reloc
1367 to give an error message with a file and line number. */
1369 static void
1370 warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
1372 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
1373 long relsize;
1374 arelent **relpp;
1375 long relcount;
1376 arelent **p, **pend;
1378 if (info->found)
1379 return;
1381 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1382 if (relsize < 0)
1383 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
1384 if (relsize == 0)
1385 return;
1387 relpp = (arelent **) xmalloc (relsize);
1388 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1389 if (relcount < 0)
1390 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
1392 p = relpp;
1393 pend = p + relcount;
1394 for (; p < pend && *p != NULL; p++)
1396 arelent *q = *p;
1398 if (q->sym_ptr_ptr != NULL
1399 && *q->sym_ptr_ptr != NULL
1400 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1402 /* We found a reloc for the symbol we are looking for. */
1403 einfo ("%P: %H: %s%s\n", abfd, sec, q->address, _("warning: "),
1404 info->warning);
1405 info->found = true;
1406 break;
1410 free (relpp);
1413 #if SUPPORT_ERROR_HANDLING_SCRIPT
1414 char * error_handling_script = NULL;
1415 #endif
1417 /* This is called when an undefined symbol is found. */
1419 static void
1420 undefined_symbol (struct bfd_link_info *info,
1421 const char *name,
1422 bfd *abfd,
1423 asection *section,
1424 bfd_vma address,
1425 bool error)
1427 static char *error_name;
1428 static unsigned int error_count;
1430 #define MAX_ERRORS_IN_A_ROW 5
1432 if (info->ignore_hash != NULL
1433 && bfd_hash_lookup (info->ignore_hash, name, false, false) != NULL)
1434 return;
1436 if (config.warn_once)
1438 /* Only warn once about a particular undefined symbol. */
1439 add_ignoresym (info, name);
1442 /* We never print more than a reasonable number of errors in a row
1443 for a single symbol. */
1444 if (error_name != NULL
1445 && strcmp (name, error_name) == 0)
1446 ++error_count;
1447 else
1449 error_count = 0;
1450 free (error_name);
1451 error_name = xstrdup (name);
1454 #if SUPPORT_ERROR_HANDLING_SCRIPT
1455 if (error_handling_script != NULL
1456 && error_count < MAX_ERRORS_IN_A_ROW)
1458 char * argv[4];
1459 const char * res;
1460 int status, err;
1462 argv[0] = error_handling_script;
1463 argv[1] = "undefined-symbol";
1464 argv[2] = (char *) name;
1465 argv[3] = NULL;
1467 if (verbose)
1468 einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
1469 argv[0], argv[1], argv[2]);
1471 res = pex_one (PEX_SEARCH, error_handling_script, argv,
1472 N_("error handling script"),
1473 NULL /* Send stdout to random, temp file. */,
1474 NULL /* Write to stderr. */,
1475 &status, &err);
1476 if (res != NULL)
1478 einfo (_("%P: Failed to run error handling script '%s', reason: "),
1479 error_handling_script);
1480 /* FIXME: We assume here that errrno == err. */
1481 perror (res);
1483 /* We ignore the return status of the script and
1484 carry on to issue the normal error message. */
1486 #endif /* SUPPORT_ERROR_HANDLING_SCRIPT */
1488 if (section != NULL)
1490 if (error_count < MAX_ERRORS_IN_A_ROW)
1492 if (error)
1493 einfo (_("%X%P: %H: undefined reference to `%pT'\n"),
1494 abfd, section, address, name);
1495 else
1496 einfo (_("%P: %H: warning: undefined reference to `%pT'\n"),
1497 abfd, section, address, name);
1499 else if (error_count == MAX_ERRORS_IN_A_ROW)
1501 if (error)
1502 einfo (_("%X%P: %D: more undefined references to `%pT' follow\n"),
1503 abfd, section, address, name);
1504 else
1505 einfo (_("%P: %D: warning: more undefined references to `%pT' follow\n"),
1506 abfd, section, address, name);
1508 else if (error)
1509 einfo ("%X");
1511 else
1513 if (error_count < MAX_ERRORS_IN_A_ROW)
1515 if (error)
1516 einfo (_("%X%P: %pB: undefined reference to `%pT'\n"),
1517 abfd, name);
1518 else
1519 einfo (_("%P: %pB: warning: undefined reference to `%pT'\n"),
1520 abfd, name);
1522 else if (error_count == MAX_ERRORS_IN_A_ROW)
1524 if (error)
1525 einfo (_("%X%P: %pB: more undefined references to `%pT' follow\n"),
1526 abfd, name);
1527 else
1528 einfo (_("%P: %pB: warning: more undefined references to `%pT' follow\n"),
1529 abfd, name);
1531 else if (error)
1532 einfo ("%X");
1536 /* Counter to limit the number of relocation overflow error messages
1537 to print. Errors are printed as it is decremented. When it's
1538 called and the counter is zero, a final message is printed
1539 indicating more relocations were omitted. When it gets to -1, no
1540 such errors are printed. If it's initially set to a value less
1541 than -1, all such errors will be printed (--verbose does this). */
1543 int overflow_cutoff_limit = 10;
1545 /* This is called when a reloc overflows. */
1547 static void
1548 reloc_overflow (struct bfd_link_info *info,
1549 struct bfd_link_hash_entry *entry,
1550 const char *name,
1551 const char *reloc_name,
1552 bfd_vma addend,
1553 bfd *abfd,
1554 asection *section,
1555 bfd_vma address)
1557 if (overflow_cutoff_limit == -1)
1558 return;
1560 einfo ("%X%H:", abfd, section, address);
1562 if (overflow_cutoff_limit >= 0
1563 && overflow_cutoff_limit-- == 0)
1565 einfo (_(" additional relocation overflows omitted from the output\n"));
1566 return;
1569 if (entry)
1571 while (entry->type == bfd_link_hash_indirect
1572 || entry->type == bfd_link_hash_warning)
1573 entry = entry->u.i.link;
1574 switch (entry->type)
1576 case bfd_link_hash_undefined:
1577 case bfd_link_hash_undefweak:
1578 einfo (_(" relocation truncated to fit: "
1579 "%s against undefined symbol `%pT'"),
1580 reloc_name, entry->root.string);
1581 break;
1582 case bfd_link_hash_defined:
1583 case bfd_link_hash_defweak:
1584 einfo (_(" relocation truncated to fit: "
1585 "%s against symbol `%pT' defined in %pA section in %pB"),
1586 reloc_name, entry->root.string,
1587 entry->u.def.section,
1588 entry->u.def.section == bfd_abs_section_ptr
1589 ? info->output_bfd : entry->u.def.section->owner);
1590 break;
1591 default:
1592 abort ();
1593 break;
1596 else
1597 einfo (_(" relocation truncated to fit: %s against `%pT'"),
1598 reloc_name, name);
1599 if (addend != 0)
1600 einfo ("+%v", addend);
1601 einfo ("\n");
1604 /* This is called when a dangerous relocation is made. */
1606 static void
1607 reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1608 const char *message,
1609 bfd *abfd,
1610 asection *section,
1611 bfd_vma address)
1613 einfo (_("%X%H: dangerous relocation: %s\n"),
1614 abfd, section, address, message);
1617 /* This is called when a reloc is being generated attached to a symbol
1618 that is not being output. */
1620 static void
1621 unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1622 const char *name,
1623 bfd *abfd,
1624 asection *section,
1625 bfd_vma address)
1627 einfo (_("%X%H: reloc refers to symbol `%pT' which is not being output\n"),
1628 abfd, section, address, name);
1631 /* This is called if link_info.notice_all is set, or when a symbol in
1632 link_info.notice_hash is found. Symbols are put in notice_hash
1633 using the -y option, while notice_all is set if the --cref option
1634 has been supplied, or if there are any NOCROSSREFS sections in the
1635 linker script; and if plugins are active, since they need to monitor
1636 all references from non-IR files. */
1638 static bool
1639 notice (struct bfd_link_info *info,
1640 struct bfd_link_hash_entry *h,
1641 struct bfd_link_hash_entry *inh ATTRIBUTE_UNUSED,
1642 bfd *abfd,
1643 asection *section,
1644 bfd_vma value,
1645 flagword flags ATTRIBUTE_UNUSED)
1647 const char *name;
1649 if (h == NULL)
1651 if (command_line.cref || nocrossref_list != NULL)
1652 return handle_asneeded_cref (abfd, (enum notice_asneeded_action) value);
1653 return true;
1656 name = h->root.string;
1657 if (info->notice_hash != NULL
1658 && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL)
1660 if (bfd_is_und_section (section))
1661 einfo (_("%P: %pB: reference to %s\n"), abfd, name);
1662 else
1663 einfo (_("%P: %pB: definition of %s\n"), abfd, name);
1666 if (command_line.cref || nocrossref_list != NULL)
1667 add_cref (name, abfd, section, value);
1669 return true;