Updated Malay translation for the bfd sub-directory
[binutils-gdb.git] / ld / ldmain.c
blob64c4cce837158605d5273e874d2ae0d665754a3d
1 /* Main program of GNU linker.
2 Copyright (C) 1991-2025 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 bool in_section_ordering;
95 args_type command_line;
97 ld_config_type config;
99 sort_type sort_section;
101 static const char *get_sysroot
102 (int, char **);
103 static char *get_emulation
104 (int, char **);
105 static bool add_archive_element
106 (struct bfd_link_info *, bfd *, const char *, bfd **);
107 static void multiple_definition
108 (struct bfd_link_info *, struct bfd_link_hash_entry *,
109 bfd *, asection *, bfd_vma);
110 static void multiple_common
111 (struct bfd_link_info *, struct bfd_link_hash_entry *,
112 bfd *, enum bfd_link_hash_type, bfd_vma);
113 static void add_to_set
114 (struct bfd_link_info *, struct bfd_link_hash_entry *,
115 bfd_reloc_code_real_type, bfd *, asection *, bfd_vma);
116 static void constructor_callback
117 (struct bfd_link_info *, bool, const char *, bfd *,
118 asection *, bfd_vma);
119 static void warning_callback
120 (struct bfd_link_info *, const char *, const char *, bfd *,
121 asection *, bfd_vma);
122 static void warning_find_reloc
123 (bfd *, asection *, void *);
124 static void undefined_symbol
125 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
126 bool);
127 static void reloc_overflow
128 (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
129 const char *, bfd_vma, bfd *, asection *, bfd_vma);
130 static void reloc_dangerous
131 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
132 static void unattached_reloc
133 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
134 static bool notice
135 (struct bfd_link_info *, struct bfd_link_hash_entry *,
136 struct bfd_link_hash_entry *, bfd *, asection *, bfd_vma, flagword);
138 static struct bfd_link_callbacks link_callbacks =
140 add_archive_element,
141 multiple_definition,
142 multiple_common,
143 add_to_set,
144 constructor_callback,
145 warning_callback,
146 undefined_symbol,
147 reloc_overflow,
148 reloc_dangerous,
149 unattached_reloc,
150 notice,
151 einfo,
152 info_msg,
153 minfo,
154 ldlang_override_segment_assignment,
155 ldlang_ctf_acquire_strings,
156 NULL,
157 ldlang_ctf_new_dynsym,
158 ldlang_write_ctf_late
161 static bfd_assert_handler_type default_bfd_assert_handler;
162 static bfd_error_handler_type default_bfd_error_handler;
164 struct bfd_link_info link_info;
166 struct dependency_file
168 struct dependency_file *next;
169 char *name;
172 static struct dependency_file *dependency_files, *dependency_files_tail;
174 void
175 track_dependency_files (const char *filename)
177 struct dependency_file *dep
178 = (struct dependency_file *) xmalloc (sizeof (*dep));
179 dep->name = xstrdup (filename);
180 dep->next = NULL;
181 if (dependency_files == NULL)
182 dependency_files = dep;
183 else
184 dependency_files_tail->next = dep;
185 dependency_files_tail = dep;
188 static void
189 write_dependency_file (void)
191 FILE *out;
192 struct dependency_file *dep;
194 out = fopen (config.dependency_file, FOPEN_WT);
195 if (out == NULL)
197 bfd_set_error (bfd_error_system_call);
198 einfo (_("%F%P: cannot open dependency file %s: %E\n"),
199 config.dependency_file);
202 fprintf (out, "%s:", output_filename);
204 for (dep = dependency_files; dep != NULL; dep = dep->next)
205 fprintf (out, " \\\n %s", dep->name);
207 fprintf (out, "\n");
208 for (dep = dependency_files; dep != NULL; dep = dep->next)
209 fprintf (out, "\n%s:\n", dep->name);
211 fclose (out);
214 static void
215 ld_cleanup (void)
217 bfd *ibfd, *inext;
218 if (link_info.output_bfd)
219 bfd_close_all_done (link_info.output_bfd);
220 for (ibfd = link_info.input_bfds; ibfd; ibfd = inext)
222 inext = ibfd->link.next;
223 bfd_close_all_done (ibfd);
225 #if BFD_SUPPORTS_PLUGINS
226 plugin_call_cleanup ();
227 #endif
228 if (output_filename && delete_output_file_on_failure)
229 unlink_if_ordinary (output_filename);
232 /* Hook to notice BFD assertions. */
234 static void
235 ld_bfd_assert_handler (const char *fmt, const char *bfdver,
236 const char *file, int line)
238 config.make_executable = false;
239 (*default_bfd_assert_handler) (fmt, bfdver, file, line);
242 /* Hook the bfd error/warning handler for --fatal-warnings. */
244 static void
245 ld_bfd_error_handler (const char *fmt, va_list ap)
247 if (config.fatal_warnings)
248 config.make_executable = false;
249 (*default_bfd_error_handler) (fmt, ap);
252 static void
253 display_external_script (void)
255 if (saved_script_handle == NULL)
256 return;
258 static const int ld_bufsz = 8193;
259 size_t n;
260 char *buf = (char *) xmalloc (ld_bufsz);
262 rewind (saved_script_handle);
263 while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
265 buf[n] = 0;
266 info_msg ("%s", buf);
268 rewind (saved_script_handle);
269 free (buf);
273 main (int argc, char **argv)
275 char *emulation;
276 long start_time = get_run_time ();
278 #ifdef HAVE_LC_MESSAGES
279 setlocale (LC_MESSAGES, "");
280 #endif
281 setlocale (LC_CTYPE, "");
282 bindtextdomain (PACKAGE, LOCALEDIR);
283 textdomain (PACKAGE);
285 program_name = argv[0];
286 xmalloc_set_program_name (program_name);
288 expandargv (&argc, &argv);
290 if (bfd_init () != BFD_INIT_MAGIC)
291 einfo (_("%F%P: fatal error: libbfd ABI mismatch\n"));
293 bfd_set_error_program_name (program_name);
295 /* We want to notice and fail on those nasty BFD assertions which are
296 likely to signal incorrect output being generated but otherwise may
297 leave no trace. */
298 default_bfd_assert_handler = bfd_set_assert_handler (ld_bfd_assert_handler);
300 /* Also hook the bfd error/warning handler for --fatal-warnings. */
301 default_bfd_error_handler = bfd_set_error_handler (ld_bfd_error_handler);
303 xatexit (ld_cleanup);
305 /* Set up the sysroot directory. */
306 ld_sysroot = get_sysroot (argc, argv);
307 if (*ld_sysroot)
308 ld_canon_sysroot = lrealpath (ld_sysroot);
309 if (ld_canon_sysroot)
311 ld_canon_sysroot_len = strlen (ld_canon_sysroot);
313 /* is_sysrooted_pathname() relies on no trailing dirsep. */
314 if (ld_canon_sysroot_len > 0
315 && IS_DIR_SEPARATOR (ld_canon_sysroot [ld_canon_sysroot_len - 1]))
316 ld_canon_sysroot [--ld_canon_sysroot_len] = '\0';
318 else
319 ld_canon_sysroot_len = -1;
321 /* Set the default BFD target based on the configured target. Doing
322 this permits the linker to be configured for a particular target,
323 and linked against a shared BFD library which was configured for
324 a different target. The macro TARGET is defined by Makefile. */
325 if (!bfd_set_default_target (TARGET))
327 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
328 xexit (1);
331 #if YYDEBUG
333 extern int yydebug;
334 yydebug = 1;
336 #endif
338 config.build_constructors = true;
339 config.rpath_separator = ':';
340 config.split_by_reloc = (unsigned) -1;
341 config.split_by_file = (bfd_size_type) -1;
342 config.make_executable = true;
343 config.magic_demand_paged = true;
344 config.text_read_only = true;
345 config.print_map_discarded = true;
346 link_info.disable_target_specific_optimizations = -1;
348 command_line.warn_mismatch = true;
349 command_line.warn_search_mismatch = true;
350 command_line.check_section_addresses = -1;
352 /* We initialize DEMANGLING based on the environment variable
353 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
354 output of the linker, unless COLLECT_NO_DEMANGLE is set in the
355 environment. Acting the same way here lets us provide the same
356 interface by default. */
357 demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
359 link_info.allow_undefined_version = true;
360 link_info.keep_memory = true;
361 link_info.max_cache_size = (bfd_size_type) -1;
362 link_info.combreloc = true;
363 link_info.strip_discarded = true;
364 link_info.prohibit_multiple_definition_absolute = false;
365 link_info.textrel_check = DEFAULT_LD_TEXTREL_CHECK;
366 link_info.emit_hash = DEFAULT_EMIT_SYSV_HASH;
367 link_info.emit_gnu_hash = DEFAULT_EMIT_GNU_HASH;
368 link_info.callbacks = &link_callbacks;
369 link_info.input_bfds_tail = &link_info.input_bfds;
370 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
371 and _fini symbols. We are compatible. */
372 link_info.init_function = "_init";
373 link_info.fini_function = "_fini";
374 link_info.relax_pass = 1;
375 link_info.extern_protected_data = -1;
376 link_info.dynamic_undefined_weak = -1;
377 link_info.indirect_extern_access = -1;
378 link_info.pei386_auto_import = -1;
379 link_info.spare_dynamic_tags = 5;
380 link_info.path_separator = ':';
381 #ifdef DEFAULT_FLAG_COMPRESS_DEBUG
382 config.compress_debug = DEFAULT_COMPRESSED_DEBUG_ALGORITHM;
383 #endif
384 #ifdef DEFAULT_NEW_DTAGS
385 link_info.new_dtags = DEFAULT_NEW_DTAGS;
386 #endif
387 link_info.start_stop_gc = false;
388 link_info.start_stop_visibility = STV_PROTECTED;
390 ldfile_add_arch ("");
391 emulation = get_emulation (argc, argv);
392 ldemul_choose_mode (emulation);
393 default_target = ldemul_choose_target (argc, argv);
394 lang_init ();
395 ldexp_init ();
396 ldemul_before_parse ();
397 lang_has_input_file = false;
398 parse_args (argc, argv);
400 if (config.hash_table_size != 0)
401 bfd_hash_set_default_size (config.hash_table_size);
403 #if BFD_SUPPORTS_PLUGINS
404 /* Now all the plugin arguments have been gathered, we can load them. */
405 plugin_load_plugins ();
406 #endif /* BFD_SUPPORTS_PLUGINS */
408 ldemul_set_symbols ();
410 /* If we have not already opened and parsed a linker script,
411 try the default script from command line first. */
412 if (saved_script_handle == NULL
413 && command_line.default_script != NULL)
415 ldfile_open_script_file (command_line.default_script);
416 parser_input = input_script;
417 yyparse ();
420 /* If we have not already opened and parsed a linker script
421 read the emulation's appropriate default script. */
422 if (saved_script_handle == NULL)
424 int isfile;
425 char *s = ldemul_get_script (&isfile);
427 if (isfile)
428 ldfile_open_default_command_file (s);
429 else
431 lex_string = s;
432 lex_redirect (s, _("built in linker script"), 1);
434 parser_input = input_script;
435 yyparse ();
436 lex_string = NULL;
439 if (verbose)
441 if (saved_script_handle)
442 info_msg (_("using external linker script: %s"), processed_scripts->name);
443 else
444 info_msg (_("using internal linker script:"));
445 info_msg ("\n==================================================\n");
447 if (saved_script_handle)
448 display_external_script ();
449 else
451 int isfile;
453 info_msg (ldemul_get_script (&isfile));
456 info_msg ("\n==================================================\n");
459 if (command_line.section_ordering_file)
461 FILE *hold_script_handle;
463 hold_script_handle = saved_script_handle;
464 ldfile_open_command_file (command_line.section_ordering_file);
465 if (verbose)
466 display_external_script ();
467 saved_script_handle = hold_script_handle;
468 in_section_ordering = true;
469 parser_input = input_section_ordering_script;
470 yyparse ();
471 in_section_ordering = false;
475 if (command_line.force_group_allocation
476 || !bfd_link_relocatable (&link_info))
477 link_info.resolve_section_groups = true;
478 else
479 link_info.resolve_section_groups = false;
481 if (command_line.print_output_format)
482 info_msg ("%s\n", lang_get_output_target ());
484 lang_final ();
486 /* If the only command line argument has been -v or --version or --verbose
487 then ignore any input files provided by linker scripts and exit now.
488 We do not want to create an output file when the linker is just invoked
489 to provide version information. */
490 if (argc == 2 && version_printed)
491 xexit (0);
493 if (link_info.inhibit_common_definition && !bfd_link_dll (&link_info))
494 einfo (_("%F%P: --no-define-common may not be used without -shared\n"));
496 if (!lang_has_input_file)
498 if (version_printed || command_line.print_output_format)
499 xexit (0);
500 output_unknown_cmdline_warnings ();
501 einfo (_("%F%P: no input files\n"));
504 if (verbose)
505 info_msg (_("%P: mode %s\n"), emulation);
507 ldemul_after_parse ();
509 output_unknown_cmdline_warnings ();
511 if (config.map_filename)
513 if (strcmp (config.map_filename, "-") == 0)
515 config.map_file = stdout;
517 else
519 config.map_file = fopen (config.map_filename, FOPEN_WT);
520 if (config.map_file == (FILE *) NULL)
522 bfd_set_error (bfd_error_system_call);
523 einfo (_("%F%P: cannot open map file %s: %E\n"),
524 config.map_filename);
527 link_info.has_map_file = true;
530 lang_process ();
532 /* Print error messages for any missing symbols, for any warning
533 symbols, and possibly multiple definitions. */
534 if (bfd_link_relocatable (&link_info))
535 link_info.output_bfd->flags &= ~EXEC_P;
536 else
537 link_info.output_bfd->flags |= EXEC_P;
539 flagword flags = 0;
540 switch (config.compress_debug)
542 case COMPRESS_DEBUG_GNU_ZLIB:
543 flags = BFD_COMPRESS;
544 break;
545 case COMPRESS_DEBUG_GABI_ZLIB:
546 flags = BFD_COMPRESS | BFD_COMPRESS_GABI;
547 break;
548 case COMPRESS_DEBUG_ZSTD:
549 flags = BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD;
550 break;
551 default:
552 break;
554 link_info.output_bfd->flags
555 |= flags & bfd_applicable_file_flags (link_info.output_bfd);
557 ldwrite ();
559 if (config.map_file != NULL)
560 lang_map ();
561 if (command_line.cref)
562 output_cref (config.map_file != NULL ? config.map_file : stdout);
563 if (nocrossref_list != NULL)
564 check_nocrossrefs ();
565 if (command_line.print_memory_usage)
566 lang_print_memory_usage ();
567 #if 0
569 struct bfd_link_hash_entry *h;
571 h = bfd_link_hash_lookup (link_info.hash, "__image_base__", 0,0,1);
572 fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1);
574 #endif
575 ldexp_finish ();
576 lang_finish ();
578 if (config.dependency_file != NULL)
579 write_dependency_file ();
581 /* Even if we're producing relocatable output, some non-fatal errors should
582 be reported in the exit status. (What non-fatal errors, if any, do we
583 want to ignore for relocatable output?) */
584 if (!config.make_executable && !force_make_executable)
586 if (verbose)
587 einfo (_("%P: link errors found, deleting executable `%s'\n"),
588 output_filename);
590 /* The file will be removed by ld_cleanup. */
591 xexit (1);
593 else
595 bfd *obfd = link_info.output_bfd;
596 link_info.output_bfd = NULL;
597 if (!bfd_close (obfd))
598 einfo (_("%F%P: %s: final close failed: %E\n"), output_filename);
600 /* If the --force-exe-suffix is enabled, and we're making an
601 executable file and it doesn't end in .exe, copy it to one
602 which does. */
603 if (!bfd_link_relocatable (&link_info)
604 && command_line.force_exe_suffix)
606 int len = strlen (output_filename);
608 if (len < 4
609 || (strcasecmp (output_filename + len - 4, ".exe") != 0
610 && strcasecmp (output_filename + len - 4, ".dll") != 0))
612 FILE *src;
613 FILE *dst;
614 const int bsize = 4096;
615 char *buf = (char *) xmalloc (bsize);
616 int l;
617 char *dst_name = (char *) xmalloc (len + 5);
619 strcpy (dst_name, output_filename);
620 strcat (dst_name, ".exe");
621 src = fopen (output_filename, FOPEN_RB);
622 dst = fopen (dst_name, FOPEN_WB);
624 if (!src)
625 einfo (_("%F%P: unable to open for source of copy `%s'\n"),
626 output_filename);
627 if (!dst)
628 einfo (_("%F%P: unable to open for destination of copy `%s'\n"),
629 dst_name);
630 while ((l = fread (buf, 1, bsize, src)) > 0)
632 int done = fwrite (buf, 1, l, dst);
634 if (done != l)
635 einfo (_("%P: error writing file `%s'\n"), dst_name);
638 fclose (src);
639 if (fclose (dst) == EOF)
640 einfo (_("%P: error closing file `%s'\n"), dst_name);
641 free (dst_name);
642 free (buf);
647 if (config.stats)
649 long run_time = get_run_time () - start_time;
651 fflush (stdout);
652 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
653 program_name, run_time / 1000000, run_time % 1000000);
654 fflush (stderr);
657 /* Prevent ld_cleanup from deleting the output file. */
658 output_filename = NULL;
660 xexit (0);
661 return 0;
664 /* If the configured sysroot is relocatable, try relocating it based on
665 default prefix FROM. Return the relocated directory if it exists,
666 otherwise return null. */
668 static char *
669 get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
671 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
672 char *path;
673 struct stat s;
675 path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
676 if (path)
678 if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
679 return path;
680 free (path);
682 #endif
683 return 0;
686 /* Return the sysroot directory. Return "" if no sysroot is being used. */
688 static const char *
689 get_sysroot (int argc, char **argv)
691 int i;
692 const char *path = NULL;
694 for (i = 1; i < argc; i++)
695 if (startswith (argv[i], "--sysroot="))
696 path = argv[i] + strlen ("--sysroot=");
698 if (!path)
699 path = get_relative_sysroot (BINDIR);
701 if (!path)
702 path = get_relative_sysroot (TOOLBINDIR);
704 if (!path)
705 path = TARGET_SYSTEM_ROOT;
707 if (IS_DIR_SEPARATOR (*path) && path[1] == 0)
708 path = "";
710 return path;
713 /* We need to find any explicitly given emulation in order to initialize the
714 state that's needed by the lex&yacc argument parser (parse_args). */
716 static char *
717 get_emulation (int argc, char **argv)
719 char *emulation;
720 int i;
722 emulation = getenv (EMULATION_ENVIRON);
723 if (emulation == NULL)
724 emulation = DEFAULT_EMULATION;
726 for (i = 1; i < argc; i++)
728 if (startswith (argv[i], "-m"))
730 if (argv[i][2] == '\0')
732 /* -m EMUL */
733 if (i < argc - 1)
735 emulation = argv[i + 1];
736 i++;
738 else
739 einfo (_("%F%P: missing argument to -m\n"));
741 else if (strcmp (argv[i], "-mips1") == 0
742 || strcmp (argv[i], "-mips2") == 0
743 || strcmp (argv[i], "-mips3") == 0
744 || strcmp (argv[i], "-mips4") == 0
745 || strcmp (argv[i], "-mips5") == 0
746 || strcmp (argv[i], "-mips32") == 0
747 || strcmp (argv[i], "-mips32r2") == 0
748 || strcmp (argv[i], "-mips32r3") == 0
749 || strcmp (argv[i], "-mips32r5") == 0
750 || strcmp (argv[i], "-mips32r6") == 0
751 || strcmp (argv[i], "-mips64") == 0
752 || strcmp (argv[i], "-mips64r2") == 0
753 || strcmp (argv[i], "-mips64r3") == 0
754 || strcmp (argv[i], "-mips64r5") == 0
755 || strcmp (argv[i], "-mips64r6") == 0)
757 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
758 passed to the linker by some MIPS compilers. They
759 generally tell the linker to use a slightly different
760 library path. Perhaps someday these should be
761 implemented as emulations; until then, we just ignore
762 the arguments and hope that nobody ever creates
763 emulations named ips1, ips2 or ips3. */
765 else if (strcmp (argv[i], "-m486") == 0)
767 /* FIXME: The argument -m486 is passed to the linker on
768 some Linux systems. Hope that nobody creates an
769 emulation named 486. */
771 else
773 /* -mEMUL */
774 emulation = &argv[i][2];
779 return emulation;
782 void
783 add_ysym (const char *name)
785 if (link_info.notice_hash == NULL)
787 link_info.notice_hash
788 = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
789 if (!bfd_hash_table_init_n (link_info.notice_hash,
790 bfd_hash_newfunc,
791 sizeof (struct bfd_hash_entry),
792 61))
793 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
796 if (bfd_hash_lookup (link_info.notice_hash, name, true, true) == NULL)
797 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
800 void
801 add_ignoresym (struct bfd_link_info *info, const char *name)
803 if (info->ignore_hash == NULL)
805 info->ignore_hash = xmalloc (sizeof (struct bfd_hash_table));
806 if (!bfd_hash_table_init_n (info->ignore_hash,
807 bfd_hash_newfunc,
808 sizeof (struct bfd_hash_entry),
809 61))
810 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
813 if (bfd_hash_lookup (info->ignore_hash, name, true, true) == NULL)
814 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
817 /* Record a symbol to be wrapped, from the --wrap option. */
819 void
820 add_wrap (const char *name)
822 if (link_info.wrap_hash == NULL)
824 link_info.wrap_hash
825 = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
826 if (!bfd_hash_table_init_n (link_info.wrap_hash,
827 bfd_hash_newfunc,
828 sizeof (struct bfd_hash_entry),
829 61))
830 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
833 if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
834 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
837 /* Handle the -retain-symbols-file option. */
839 void
840 add_keepsyms_file (const char *filename)
842 FILE *file;
843 char *buf;
844 size_t bufsize;
845 int c;
847 if (link_info.strip == strip_some)
848 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
850 file = fopen (filename, "r");
851 if (file == NULL)
853 bfd_set_error (bfd_error_system_call);
854 einfo ("%X%P: %s: %E\n", filename);
855 return;
858 link_info.keep_hash = (struct bfd_hash_table *)
859 xmalloc (sizeof (struct bfd_hash_table));
860 if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc,
861 sizeof (struct bfd_hash_entry)))
862 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
864 bufsize = 100;
865 buf = (char *) xmalloc (bufsize);
867 c = getc (file);
868 while (c != EOF)
870 while (ISSPACE (c))
871 c = getc (file);
873 if (c != EOF)
875 size_t len = 0;
877 while (!ISSPACE (c) && c != EOF)
879 buf[len] = c;
880 ++len;
881 if (len >= bufsize)
883 bufsize *= 2;
884 buf = (char *) xrealloc (buf, bufsize);
886 c = getc (file);
889 buf[len] = '\0';
891 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true) == NULL)
892 einfo (_("%F%P: bfd_hash_lookup for insertion failed: %E\n"));
896 if (link_info.strip != strip_none)
897 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
899 free (buf);
900 link_info.strip = strip_some;
901 fclose (file);
904 /* Callbacks from the BFD linker routines. */
906 /* This is called when BFD has decided to include an archive member in
907 a link. */
909 static bool
910 add_archive_element (struct bfd_link_info *info,
911 bfd *abfd,
912 const char *name,
913 bfd **subsbfd ATTRIBUTE_UNUSED)
915 lang_input_statement_type *input;
916 lang_input_statement_type *parent;
917 lang_input_statement_type orig_input;
919 input = (lang_input_statement_type *)
920 xcalloc (1, sizeof (lang_input_statement_type));
921 input->header.type = lang_input_statement_enum;
922 input->filename = bfd_get_filename (abfd);
923 input->local_sym_name = bfd_get_filename (abfd);
924 input->the_bfd = abfd;
926 /* Save the original data for trace files/tries below, as plugins
927 (if enabled) may possibly alter it to point to a replacement
928 BFD, but we still want to output the original BFD filename. */
929 orig_input = *input;
930 #if BFD_SUPPORTS_PLUGINS
931 /* Don't claim a fat IR object if no IR object should be claimed. */
932 if (link_info.lto_plugin_active
933 && (!no_more_claiming
934 || bfd_get_lto_type (abfd) != lto_fat_ir_object))
936 /* We must offer this archive member to the plugins to claim. */
937 plugin_maybe_claim (input);
938 if (input->flags.claimed)
940 if (no_more_claiming)
942 /* Don't claim new IR symbols after all IR symbols have
943 been claimed. */
944 if (verbose)
945 info_msg ("%pI: no new IR symbols to claim\n",
946 &orig_input);
947 input->flags.claimed = 0;
948 return false;
950 input->flags.claim_archive = true;
951 *subsbfd = input->the_bfd;
954 #endif /* BFD_SUPPORTS_PLUGINS */
956 if (link_info.input_bfds_tail == &input->the_bfd->link.next
957 || input->the_bfd->link.next != NULL)
959 /* We have already loaded this element, and are attempting to
960 load it again. This can happen when the archive map doesn't
961 match actual symbols defined by the element. */
962 free (input);
963 bfd_set_error (bfd_error_malformed_archive);
964 return false;
967 /* Set the file_chain pointer of archives to the last element loaded
968 from the archive. See ldlang.c:find_rescan_insertion. */
969 parent = bfd_usrdata (abfd->my_archive);
970 if (parent != NULL && !parent->flags.reload)
971 parent->next = input;
973 ldlang_add_file (input);
975 if (config.map_file != NULL)
977 static bool header_printed;
978 struct bfd_link_hash_entry *h;
979 bfd *from;
980 int len;
982 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
983 if (h == NULL
984 && info->pei386_auto_import
985 && startswith (name, "__imp_"))
986 h = bfd_link_hash_lookup (info->hash, name + 6, false, false, true);
988 if (h == NULL)
989 from = NULL;
990 else
992 switch (h->type)
994 default:
995 from = NULL;
996 break;
998 case bfd_link_hash_defined:
999 case bfd_link_hash_defweak:
1000 from = h->u.def.section->owner;
1001 break;
1003 case bfd_link_hash_undefined:
1004 case bfd_link_hash_undefweak:
1005 from = h->u.undef.abfd;
1006 break;
1008 case bfd_link_hash_common:
1009 from = h->u.c.p->section->owner;
1010 break;
1014 if (!header_printed)
1016 minfo (_("Archive member included to satisfy reference by file (symbol)\n\n"));
1017 header_printed = true;
1020 if (abfd->my_archive == NULL
1021 || bfd_is_thin_archive (abfd->my_archive))
1023 minfo ("%s", bfd_get_filename (abfd));
1024 len = strlen (bfd_get_filename (abfd));
1026 else
1028 minfo ("%s(%s)", bfd_get_filename (abfd->my_archive),
1029 bfd_get_filename (abfd));
1030 len = (strlen (bfd_get_filename (abfd->my_archive))
1031 + strlen (bfd_get_filename (abfd))
1032 + 2);
1035 if (len >= 29)
1037 print_nl ();
1038 len = 0;
1040 print_spaces (30 - len);
1042 if (from != NULL)
1043 minfo ("%pB ", from);
1044 if (h != NULL)
1045 minfo ("(%pT)\n", h->root.string);
1046 else
1047 minfo ("(%s)\n", name);
1050 if (verbose
1051 || trace_files > 1
1052 || (trace_files && bfd_is_thin_archive (orig_input.the_bfd->my_archive)))
1053 info_msg ("%pI\n", &orig_input);
1054 return true;
1057 /* This is called when BFD has discovered a symbol which is defined
1058 multiple times. */
1060 static void
1061 multiple_definition (struct bfd_link_info *info,
1062 struct bfd_link_hash_entry *h,
1063 bfd *nbfd,
1064 asection *nsec,
1065 bfd_vma nval)
1067 const char *name;
1068 bfd *obfd;
1069 asection *osec;
1070 bfd_vma oval;
1072 if (info->allow_multiple_definition)
1073 return;
1075 switch (h->type)
1077 case bfd_link_hash_defined:
1078 osec = h->u.def.section;
1079 oval = h->u.def.value;
1080 obfd = h->u.def.section->owner;
1081 break;
1082 case bfd_link_hash_indirect:
1083 osec = bfd_ind_section_ptr;
1084 oval = 0;
1085 obfd = NULL;
1086 break;
1087 default:
1088 abort ();
1091 /* Ignore a redefinition of an absolute symbol to the
1092 same value; it's harmless. */
1093 if (h->type == bfd_link_hash_defined
1094 && bfd_is_abs_section (osec)
1095 && bfd_is_abs_section (nsec)
1096 && nval == oval)
1097 return;
1099 /* If either section has the output_section field set to
1100 bfd_abs_section_ptr, it means that the section is being
1101 discarded, and this is not really a multiple definition at all.
1102 FIXME: It would be cleaner to somehow ignore symbols defined in
1103 sections which are being discarded. */
1104 if (!info->prohibit_multiple_definition_absolute
1105 && ((osec->output_section != NULL
1106 && ! bfd_is_abs_section (osec)
1107 && bfd_is_abs_section (osec->output_section))
1108 || (nsec->output_section != NULL
1109 && !bfd_is_abs_section (nsec)
1110 && bfd_is_abs_section (nsec->output_section))))
1111 return;
1113 name = h->root.string;
1114 if (nbfd == NULL)
1116 nbfd = obfd;
1117 nsec = osec;
1118 nval = oval;
1119 obfd = NULL;
1121 if (info->warn_multiple_definition)
1122 einfo (_("%P: %C: warning: multiple definition of `%pT'"),
1123 nbfd, nsec, nval, name);
1124 else
1125 einfo (_("%X%P: %C: multiple definition of `%pT'"),
1126 nbfd, nsec, nval, name);
1127 if (obfd != NULL)
1128 einfo (_("; %D: first defined here"), obfd, osec, oval);
1129 einfo ("\n");
1131 if (RELAXATION_ENABLED_BY_USER)
1133 einfo (_("%P: disabling relaxation; it will not work with multiple definitions\n"));
1134 DISABLE_RELAXATION;
1138 /* This is called when there is a definition of a common symbol, or
1139 when a common symbol is found for a symbol that is already defined,
1140 or when two common symbols are found. We only do something if
1141 -warn-common was used. */
1143 static void
1144 multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1145 struct bfd_link_hash_entry *h,
1146 bfd *nbfd,
1147 enum bfd_link_hash_type ntype,
1148 bfd_vma nsize)
1150 const char *name;
1151 bfd *obfd;
1152 enum bfd_link_hash_type otype;
1153 bfd_vma osize;
1155 if (!config.warn_common)
1156 return;
1158 name = h->root.string;
1159 otype = h->type;
1160 if (otype == bfd_link_hash_common)
1162 obfd = h->u.c.p->section->owner;
1163 osize = h->u.c.size;
1165 else if (otype == bfd_link_hash_defined
1166 || otype == bfd_link_hash_defweak)
1168 obfd = h->u.def.section->owner;
1169 osize = 0;
1171 else
1173 /* FIXME: It would nice if we could report the BFD which defined
1174 an indirect symbol, but we don't have anywhere to store the
1175 information. */
1176 obfd = NULL;
1177 osize = 0;
1180 if (ntype == bfd_link_hash_defined
1181 || ntype == bfd_link_hash_defweak
1182 || ntype == bfd_link_hash_indirect)
1184 ASSERT (otype == bfd_link_hash_common);
1185 if (obfd != NULL)
1186 einfo (_("%P: %pB: warning: definition of `%pT' overriding common"
1187 " from %pB\n"),
1188 nbfd, name, obfd);
1189 else
1190 einfo (_("%P: %pB: warning: definition of `%pT' overriding common\n"),
1191 nbfd, name);
1193 else if (otype == bfd_link_hash_defined
1194 || otype == bfd_link_hash_defweak
1195 || otype == bfd_link_hash_indirect)
1197 ASSERT (ntype == bfd_link_hash_common);
1198 if (obfd != NULL)
1199 einfo (_("%P: %pB: warning: common of `%pT' overridden by definition"
1200 " from %pB\n"),
1201 nbfd, name, obfd);
1202 else
1203 einfo (_("%P: %pB: warning: common of `%pT' overridden by definition\n"),
1204 nbfd, name);
1206 else
1208 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
1209 if (osize > nsize)
1211 if (obfd != NULL)
1212 einfo (_("%P: %pB: warning: common of `%pT' overridden"
1213 " by larger common from %pB\n"),
1214 nbfd, name, obfd);
1215 else
1216 einfo (_("%P: %pB: warning: common of `%pT' overridden"
1217 " by larger common\n"),
1218 nbfd, name);
1220 else if (nsize > osize)
1222 if (obfd != NULL)
1223 einfo (_("%P: %pB: warning: common of `%pT' overriding"
1224 " smaller common from %pB\n"),
1225 nbfd, name, obfd);
1226 else
1227 einfo (_("%P: %pB: warning: common of `%pT' overriding"
1228 " smaller common\n"),
1229 nbfd, name);
1231 else
1233 if (obfd != NULL)
1234 einfo (_("%P: %pB and %pB: warning: multiple common of `%pT'\n"),
1235 nbfd, obfd, name);
1236 else
1237 einfo (_("%P: %pB: warning: multiple common of `%pT'\n"),
1238 nbfd, name);
1243 /* This is called when BFD has discovered a set element. H is the
1244 entry in the linker hash table for the set. SECTION and VALUE
1245 represent a value which should be added to the set. */
1247 static void
1248 add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1249 struct bfd_link_hash_entry *h,
1250 bfd_reloc_code_real_type reloc,
1251 bfd *abfd,
1252 asection *section,
1253 bfd_vma value)
1255 if (config.warn_constructors)
1256 einfo (_("%P: warning: global constructor %s used\n"),
1257 h->root.string);
1259 if (!config.build_constructors)
1260 return;
1262 ldctor_add_set_entry (h, reloc, NULL, section, value);
1264 if (h->type == bfd_link_hash_new)
1266 h->type = bfd_link_hash_undefined;
1267 h->u.undef.abfd = abfd;
1268 /* We don't call bfd_link_add_undef to add this to the list of
1269 undefined symbols because we are going to define it
1270 ourselves. */
1274 /* This is called when BFD has discovered a constructor. This is only
1275 called for some object file formats--those which do not handle
1276 constructors in some more clever fashion. This is similar to
1277 adding an element to a set, but less general. */
1279 static void
1280 constructor_callback (struct bfd_link_info *info,
1281 bool constructor,
1282 const char *name,
1283 bfd *abfd,
1284 asection *section,
1285 bfd_vma value)
1287 char *s;
1288 struct bfd_link_hash_entry *h;
1289 char set_name[1 + sizeof "__CTOR_LIST__"];
1291 if (config.warn_constructors)
1292 einfo (_("%P: warning: global constructor %s used\n"), name);
1294 if (!config.build_constructors)
1295 return;
1297 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1298 useful error message. */
1299 if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL
1300 && (bfd_link_relocatable (info)
1301 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
1302 einfo (_("%F%P: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1304 s = set_name;
1305 if (bfd_get_symbol_leading_char (abfd) != '\0')
1306 *s++ = bfd_get_symbol_leading_char (abfd);
1307 if (constructor)
1308 strcpy (s, "__CTOR_LIST__");
1309 else
1310 strcpy (s, "__DTOR_LIST__");
1312 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
1313 if (h == (struct bfd_link_hash_entry *) NULL)
1314 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1315 if (h->type == bfd_link_hash_new)
1317 h->type = bfd_link_hash_undefined;
1318 h->u.undef.abfd = abfd;
1319 /* We don't call bfd_link_add_undef to add this to the list of
1320 undefined symbols because we are going to define it
1321 ourselves. */
1324 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
1327 /* A structure used by warning_callback to pass information through
1328 bfd_map_over_sections. */
1330 struct warning_callback_info
1332 bool found;
1333 const char *warning;
1334 const char *symbol;
1335 asymbol **asymbols;
1338 /* Look through the relocs to see if we can find a plausible address
1339 for SYMBOL in ABFD. Return TRUE if found. Otherwise return FALSE. */
1341 static bool
1342 symbol_warning (const char *warning, const char *symbol, bfd *abfd)
1344 struct warning_callback_info cinfo;
1346 if (!bfd_generic_link_read_symbols (abfd))
1347 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd);
1349 cinfo.found = false;
1350 cinfo.warning = warning;
1351 cinfo.symbol = symbol;
1352 cinfo.asymbols = bfd_get_outsymbols (abfd);
1353 bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
1354 return cinfo.found;
1357 /* This is called when there is a reference to a warning symbol. */
1359 static void
1360 warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1361 const char *warning,
1362 const char *symbol,
1363 bfd *abfd,
1364 asection *section,
1365 bfd_vma address)
1367 /* This is a hack to support warn_multiple_gp. FIXME: This should
1368 have a cleaner interface, but what? */
1369 if (!config.warn_multiple_gp
1370 && strcmp (warning, "using multiple gp values") == 0)
1371 return;
1373 if (section != NULL)
1374 einfo ("%P: %C: %s%s\n", abfd, section, address, _("warning: "), warning);
1375 else if (abfd == NULL)
1376 einfo ("%P: %s%s\n", _("warning: "), warning);
1377 else if (symbol == NULL)
1378 einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
1379 else if (!symbol_warning (warning, symbol, abfd))
1381 bfd *b;
1382 /* Search all input files for a reference to SYMBOL. */
1383 for (b = info->input_bfds; b; b = b->link.next)
1384 if (b != abfd && symbol_warning (warning, symbol, b))
1385 return;
1386 einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
1390 /* This is called by warning_callback for each section. It checks the
1391 relocs of the section to see if it can find a reference to the
1392 symbol which triggered the warning. If it can, it uses the reloc
1393 to give an error message with a file and line number. */
1395 static void
1396 warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
1398 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
1399 long relsize;
1400 arelent **relpp;
1401 long relcount;
1402 arelent **p, **pend;
1404 if (info->found)
1405 return;
1407 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1408 if (relsize < 0)
1409 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
1410 if (relsize == 0)
1411 return;
1413 relpp = (arelent **) xmalloc (relsize);
1414 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1415 if (relcount < 0)
1416 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
1418 p = relpp;
1419 pend = p + relcount;
1420 for (; p < pend && *p != NULL; p++)
1422 arelent *q = *p;
1424 if (q->sym_ptr_ptr != NULL
1425 && *q->sym_ptr_ptr != NULL
1426 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1428 /* We found a reloc for the symbol we are looking for. */
1429 einfo ("%P: %H: %s%s\n", abfd, sec, q->address, _("warning: "),
1430 info->warning);
1431 info->found = true;
1432 break;
1436 free (relpp);
1439 #if SUPPORT_ERROR_HANDLING_SCRIPT
1440 char * error_handling_script = NULL;
1441 #endif
1443 /* This is called when an undefined symbol is found. */
1445 static void
1446 undefined_symbol (struct bfd_link_info *info,
1447 const char *name,
1448 bfd *abfd,
1449 asection *section,
1450 bfd_vma address,
1451 bool error)
1453 static char *error_name;
1454 static unsigned int error_count;
1456 #define MAX_ERRORS_IN_A_ROW 5
1458 if (info->ignore_hash != NULL
1459 && bfd_hash_lookup (info->ignore_hash, name, false, false) != NULL)
1460 return;
1462 if (config.warn_once)
1464 /* Only warn once about a particular undefined symbol. */
1465 add_ignoresym (info, name);
1468 /* We never print more than a reasonable number of errors in a row
1469 for a single symbol. */
1470 if (error_name != NULL
1471 && strcmp (name, error_name) == 0)
1472 ++error_count;
1473 else
1475 error_count = 0;
1476 free (error_name);
1477 error_name = xstrdup (name);
1480 #if SUPPORT_ERROR_HANDLING_SCRIPT
1481 if (error_handling_script != NULL
1482 && error_count < MAX_ERRORS_IN_A_ROW)
1484 char * argv[4];
1485 const char * res;
1486 int status, err;
1488 argv[0] = error_handling_script;
1489 argv[1] = "undefined-symbol";
1490 argv[2] = (char *) name;
1491 argv[3] = NULL;
1493 if (verbose)
1494 einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
1495 argv[0], argv[1], argv[2]);
1497 res = pex_one (PEX_SEARCH, error_handling_script, argv,
1498 N_("error handling script"),
1499 NULL /* Send stdout to random, temp file. */,
1500 NULL /* Write to stderr. */,
1501 &status, &err);
1502 if (res != NULL)
1504 einfo (_("%P: Failed to run error handling script '%s', reason: "),
1505 error_handling_script);
1506 /* FIXME: We assume here that errrno == err. */
1507 perror (res);
1509 /* We ignore the return status of the script and
1510 carry on to issue the normal error message. */
1512 #endif /* SUPPORT_ERROR_HANDLING_SCRIPT */
1514 if (section != NULL)
1516 if (error_count < MAX_ERRORS_IN_A_ROW)
1518 if (error)
1519 einfo (_("%X%P: %H: undefined reference to `%pT'\n"),
1520 abfd, section, address, name);
1521 else
1522 einfo (_("%P: %H: warning: undefined reference to `%pT'\n"),
1523 abfd, section, address, name);
1525 else if (error_count == MAX_ERRORS_IN_A_ROW)
1527 if (error)
1528 einfo (_("%X%P: %D: more undefined references to `%pT' follow\n"),
1529 abfd, section, address, name);
1530 else
1531 einfo (_("%P: %D: warning: more undefined references to `%pT' follow\n"),
1532 abfd, section, address, name);
1534 else if (error)
1535 einfo ("%X");
1537 else
1539 if (error_count < MAX_ERRORS_IN_A_ROW)
1541 if (error)
1542 einfo (_("%X%P: %pB: undefined reference to `%pT'\n"),
1543 abfd, name);
1544 else
1545 einfo (_("%P: %pB: warning: undefined reference to `%pT'\n"),
1546 abfd, name);
1548 else if (error_count == MAX_ERRORS_IN_A_ROW)
1550 if (error)
1551 einfo (_("%X%P: %pB: more undefined references to `%pT' follow\n"),
1552 abfd, name);
1553 else
1554 einfo (_("%P: %pB: warning: more undefined references to `%pT' follow\n"),
1555 abfd, name);
1557 else if (error)
1558 einfo ("%X");
1562 /* Counter to limit the number of relocation overflow error messages
1563 to print. Errors are printed as it is decremented. When it's
1564 called and the counter is zero, a final message is printed
1565 indicating more relocations were omitted. When it gets to -1, no
1566 such errors are printed. If it's initially set to a value less
1567 than -1, all such errors will be printed (--verbose does this). */
1569 int overflow_cutoff_limit = 10;
1571 /* This is called when a reloc overflows. */
1573 static void
1574 reloc_overflow (struct bfd_link_info *info,
1575 struct bfd_link_hash_entry *entry,
1576 const char *name,
1577 const char *reloc_name,
1578 bfd_vma addend,
1579 bfd *abfd,
1580 asection *section,
1581 bfd_vma address)
1583 if (overflow_cutoff_limit == -1)
1584 return;
1586 einfo ("%X%H:", abfd, section, address);
1588 if (overflow_cutoff_limit >= 0
1589 && overflow_cutoff_limit-- == 0)
1591 einfo (_(" additional relocation overflows omitted from the output\n"));
1592 return;
1595 if (entry)
1597 while (entry->type == bfd_link_hash_indirect
1598 || entry->type == bfd_link_hash_warning)
1599 entry = entry->u.i.link;
1600 switch (entry->type)
1602 case bfd_link_hash_undefined:
1603 case bfd_link_hash_undefweak:
1604 einfo (_(" relocation truncated to fit: "
1605 "%s against undefined symbol `%pT'"),
1606 reloc_name, entry->root.string);
1607 break;
1608 case bfd_link_hash_defined:
1609 case bfd_link_hash_defweak:
1610 einfo (_(" relocation truncated to fit: "
1611 "%s against symbol `%pT' defined in %pA section in %pB"),
1612 reloc_name, entry->root.string,
1613 entry->u.def.section,
1614 entry->u.def.section == bfd_abs_section_ptr
1615 ? info->output_bfd : entry->u.def.section->owner);
1616 break;
1617 default:
1618 abort ();
1619 break;
1622 else
1623 einfo (_(" relocation truncated to fit: %s against `%pT'"),
1624 reloc_name, name);
1625 if (addend != 0)
1626 einfo ("+%v", addend);
1627 einfo ("\n");
1630 /* This is called when a dangerous relocation is made. */
1632 static void
1633 reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1634 const char *message,
1635 bfd *abfd,
1636 asection *section,
1637 bfd_vma address)
1639 einfo (_("%X%H: dangerous relocation: %s\n"),
1640 abfd, section, address, message);
1643 /* This is called when a reloc is being generated attached to a symbol
1644 that is not being output. */
1646 static void
1647 unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1648 const char *name,
1649 bfd *abfd,
1650 asection *section,
1651 bfd_vma address)
1653 einfo (_("%X%H: reloc refers to symbol `%pT' which is not being output\n"),
1654 abfd, section, address, name);
1657 /* This is called if link_info.notice_all is set, or when a symbol in
1658 link_info.notice_hash is found. Symbols are put in notice_hash
1659 using the -y option, while notice_all is set if the --cref option
1660 has been supplied, or if there are any NOCROSSREFS sections in the
1661 linker script; and if plugins are active, since they need to monitor
1662 all references from non-IR files. */
1664 static bool
1665 notice (struct bfd_link_info *info,
1666 struct bfd_link_hash_entry *h,
1667 struct bfd_link_hash_entry *inh ATTRIBUTE_UNUSED,
1668 bfd *abfd,
1669 asection *section,
1670 bfd_vma value,
1671 flagword flags ATTRIBUTE_UNUSED)
1673 const char *name;
1675 if (h == NULL)
1677 if (command_line.cref || nocrossref_list != NULL)
1678 return handle_asneeded_cref (abfd, (enum notice_asneeded_action) value);
1679 return true;
1682 name = h->root.string;
1683 if (info->notice_hash != NULL
1684 && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL)
1686 if (bfd_is_und_section (section))
1687 einfo (_("%P: %pB: reference to %s\n"), abfd, name);
1688 else
1689 einfo (_("%P: %pB: definition of %s\n"), abfd, name);
1692 if (command_line.cref || nocrossref_list != NULL)
1693 add_cref (name, abfd, section, value);
1695 return true;