Add --section-ordering command line option to the bfd linker.
[binutils-gdb.git] / ld / ldmain.c
blob037099b9d37352437744c3507757df096250de24
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 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 einfo (_("%F%P: cannot open dependency file %s: %E\n"),
198 config.dependency_file);
201 fprintf (out, "%s:", output_filename);
203 for (dep = dependency_files; dep != NULL; dep = dep->next)
204 fprintf (out, " \\\n %s", dep->name);
206 fprintf (out, "\n");
207 for (dep = dependency_files; dep != NULL; dep = dep->next)
208 fprintf (out, "\n%s:\n", dep->name);
210 fclose (out);
213 static void
214 ld_cleanup (void)
216 bfd *ibfd, *inext;
217 if (link_info.output_bfd)
218 bfd_close_all_done (link_info.output_bfd);
219 for (ibfd = link_info.input_bfds; ibfd; ibfd = inext)
221 inext = ibfd->link.next;
222 bfd_close_all_done (ibfd);
224 #if BFD_SUPPORTS_PLUGINS
225 plugin_call_cleanup ();
226 #endif
227 if (output_filename && delete_output_file_on_failure)
228 unlink_if_ordinary (output_filename);
231 /* Hook to notice BFD assertions. */
233 static void
234 ld_bfd_assert_handler (const char *fmt, const char *bfdver,
235 const char *file, int line)
237 config.make_executable = false;
238 (*default_bfd_assert_handler) (fmt, bfdver, file, line);
241 /* Hook the bfd error/warning handler for --fatal-warnings. */
243 static void
244 ld_bfd_error_handler (const char *fmt, va_list ap)
246 if (config.fatal_warnings)
247 config.make_executable = false;
248 (*default_bfd_error_handler) (fmt, ap);
251 static void
252 display_external_script (void)
254 if (saved_script_handle == NULL)
255 return;
257 static const int ld_bufsz = 8193;
258 size_t n;
259 char *buf = (char *) xmalloc (ld_bufsz);
261 rewind (saved_script_handle);
262 while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
264 buf[n] = 0;
265 info_msg ("%s", buf);
267 rewind (saved_script_handle);
268 free (buf);
272 main (int argc, char **argv)
274 char *emulation;
275 long start_time = get_run_time ();
277 #ifdef HAVE_LC_MESSAGES
278 setlocale (LC_MESSAGES, "");
279 #endif
280 setlocale (LC_CTYPE, "");
281 bindtextdomain (PACKAGE, LOCALEDIR);
282 textdomain (PACKAGE);
284 program_name = argv[0];
285 xmalloc_set_program_name (program_name);
287 expandargv (&argc, &argv);
289 if (bfd_init () != BFD_INIT_MAGIC)
290 einfo (_("%F%P: fatal error: libbfd ABI mismatch\n"));
292 bfd_set_error_program_name (program_name);
294 /* We want to notice and fail on those nasty BFD assertions which are
295 likely to signal incorrect output being generated but otherwise may
296 leave no trace. */
297 default_bfd_assert_handler = bfd_set_assert_handler (ld_bfd_assert_handler);
299 /* Also hook the bfd error/warning handler for --fatal-warnings. */
300 default_bfd_error_handler = bfd_set_error_handler (ld_bfd_error_handler);
302 xatexit (ld_cleanup);
304 /* Set up the sysroot directory. */
305 ld_sysroot = get_sysroot (argc, argv);
306 if (*ld_sysroot)
307 ld_canon_sysroot = lrealpath (ld_sysroot);
308 if (ld_canon_sysroot)
310 ld_canon_sysroot_len = strlen (ld_canon_sysroot);
312 /* is_sysrooted_pathname() relies on no trailing dirsep. */
313 if (ld_canon_sysroot_len > 0
314 && IS_DIR_SEPARATOR (ld_canon_sysroot [ld_canon_sysroot_len - 1]))
315 ld_canon_sysroot [--ld_canon_sysroot_len] = '\0';
317 else
318 ld_canon_sysroot_len = -1;
320 /* Set the default BFD target based on the configured target. Doing
321 this permits the linker to be configured for a particular target,
322 and linked against a shared BFD library which was configured for
323 a different target. The macro TARGET is defined by Makefile. */
324 if (!bfd_set_default_target (TARGET))
326 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
327 xexit (1);
330 #if YYDEBUG
332 extern int yydebug;
333 yydebug = 1;
335 #endif
337 config.build_constructors = true;
338 config.rpath_separator = ':';
339 config.split_by_reloc = (unsigned) -1;
340 config.split_by_file = (bfd_size_type) -1;
341 config.make_executable = true;
342 config.magic_demand_paged = true;
343 config.text_read_only = true;
344 config.print_map_discarded = true;
345 link_info.disable_target_specific_optimizations = -1;
347 command_line.warn_mismatch = true;
348 command_line.warn_search_mismatch = true;
349 command_line.check_section_addresses = -1;
351 /* We initialize DEMANGLING based on the environment variable
352 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
353 output of the linker, unless COLLECT_NO_DEMANGLE is set in the
354 environment. Acting the same way here lets us provide the same
355 interface by default. */
356 demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
358 link_info.allow_undefined_version = true;
359 link_info.keep_memory = true;
360 link_info.max_cache_size = (bfd_size_type) -1;
361 link_info.combreloc = true;
362 link_info.strip_discarded = true;
363 link_info.prohibit_multiple_definition_absolute = false;
364 link_info.textrel_check = DEFAULT_LD_TEXTREL_CHECK;
365 link_info.emit_hash = DEFAULT_EMIT_SYSV_HASH;
366 link_info.emit_gnu_hash = DEFAULT_EMIT_GNU_HASH;
367 link_info.callbacks = &link_callbacks;
368 link_info.input_bfds_tail = &link_info.input_bfds;
369 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
370 and _fini symbols. We are compatible. */
371 link_info.init_function = "_init";
372 link_info.fini_function = "_fini";
373 link_info.relax_pass = 1;
374 link_info.extern_protected_data = -1;
375 link_info.dynamic_undefined_weak = -1;
376 link_info.indirect_extern_access = -1;
377 link_info.pei386_auto_import = -1;
378 link_info.spare_dynamic_tags = 5;
379 link_info.path_separator = ':';
380 #ifdef DEFAULT_FLAG_COMPRESS_DEBUG
381 config.compress_debug = DEFAULT_COMPRESSED_DEBUG_ALGORITHM;
382 #endif
383 #ifdef DEFAULT_NEW_DTAGS
384 link_info.new_dtags = DEFAULT_NEW_DTAGS;
385 #endif
386 link_info.start_stop_gc = false;
387 link_info.start_stop_visibility = STV_PROTECTED;
389 ldfile_add_arch ("");
390 emulation = get_emulation (argc, argv);
391 ldemul_choose_mode (emulation);
392 default_target = ldemul_choose_target (argc, argv);
393 lang_init ();
394 ldexp_init ();
395 ldemul_before_parse ();
396 lang_has_input_file = false;
397 parse_args (argc, argv);
399 if (config.hash_table_size != 0)
400 bfd_hash_set_default_size (config.hash_table_size);
402 #if BFD_SUPPORTS_PLUGINS
403 /* Now all the plugin arguments have been gathered, we can load them. */
404 plugin_load_plugins ();
405 #endif /* BFD_SUPPORTS_PLUGINS */
407 ldemul_set_symbols ();
409 /* If we have not already opened and parsed a linker script,
410 try the default script from command line first. */
411 if (saved_script_handle == NULL
412 && command_line.default_script != NULL)
414 ldfile_open_script_file (command_line.default_script);
415 parser_input = input_script;
416 yyparse ();
419 /* If we have not already opened and parsed a linker script
420 read the emulation's appropriate default script. */
421 if (saved_script_handle == NULL)
423 int isfile;
424 char *s = ldemul_get_script (&isfile);
426 if (isfile)
427 ldfile_open_default_command_file (s);
428 else
430 lex_string = s;
431 lex_redirect (s, _("built in linker script"), 1);
433 parser_input = input_script;
434 yyparse ();
435 lex_string = NULL;
438 if (verbose)
440 if (saved_script_handle)
441 info_msg (_("using external linker script: %s"), processed_scripts->name);
442 else
443 info_msg (_("using internal linker script:"));
444 info_msg ("\n==================================================\n");
446 if (saved_script_handle)
447 display_external_script ();
448 else
450 int isfile;
452 info_msg (ldemul_get_script (&isfile));
455 info_msg ("\n==================================================\n");
458 if (command_line.section_ordering_file)
460 FILE *hold_script_handle;
462 hold_script_handle = saved_script_handle;
463 ldfile_open_command_file (command_line.section_ordering_file);
464 if (verbose)
465 display_external_script ();
466 saved_script_handle = hold_script_handle;
467 in_section_ordering = true;
468 parser_input = input_section_ordering_script;
469 yyparse ();
470 in_section_ordering = false;
474 if (command_line.force_group_allocation
475 || !bfd_link_relocatable (&link_info))
476 link_info.resolve_section_groups = true;
477 else
478 link_info.resolve_section_groups = false;
480 if (command_line.print_output_format)
481 info_msg ("%s\n", lang_get_output_target ());
483 lang_final ();
485 /* If the only command line argument has been -v or --version or --verbose
486 then ignore any input files provided by linker scripts and exit now.
487 We do not want to create an output file when the linker is just invoked
488 to provide version information. */
489 if (argc == 2 && version_printed)
490 xexit (0);
492 if (link_info.inhibit_common_definition && !bfd_link_dll (&link_info))
493 einfo (_("%F%P: --no-define-common may not be used without -shared\n"));
495 if (!lang_has_input_file)
497 if (version_printed || command_line.print_output_format)
498 xexit (0);
499 output_unknown_cmdline_warnings ();
500 einfo (_("%F%P: no input files\n"));
503 if (verbose)
504 info_msg (_("%P: mode %s\n"), emulation);
506 ldemul_after_parse ();
508 output_unknown_cmdline_warnings ();
510 if (config.map_filename)
512 if (strcmp (config.map_filename, "-") == 0)
514 config.map_file = stdout;
516 else
518 config.map_file = fopen (config.map_filename, FOPEN_WT);
519 if (config.map_file == (FILE *) NULL)
521 bfd_set_error (bfd_error_system_call);
522 einfo (_("%F%P: cannot open map file %s: %E\n"),
523 config.map_filename);
526 link_info.has_map_file = true;
529 lang_process ();
531 /* Print error messages for any missing symbols, for any warning
532 symbols, and possibly multiple definitions. */
533 if (bfd_link_relocatable (&link_info))
534 link_info.output_bfd->flags &= ~EXEC_P;
535 else
536 link_info.output_bfd->flags |= EXEC_P;
538 flagword flags = 0;
539 switch (config.compress_debug)
541 case COMPRESS_DEBUG_GNU_ZLIB:
542 flags = BFD_COMPRESS;
543 break;
544 case COMPRESS_DEBUG_GABI_ZLIB:
545 flags = BFD_COMPRESS | BFD_COMPRESS_GABI;
546 break;
547 case COMPRESS_DEBUG_ZSTD:
548 flags = BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD;
549 break;
550 default:
551 break;
553 link_info.output_bfd->flags
554 |= flags & bfd_applicable_file_flags (link_info.output_bfd);
556 ldwrite ();
558 if (config.map_file != NULL)
559 lang_map ();
560 if (command_line.cref)
561 output_cref (config.map_file != NULL ? config.map_file : stdout);
562 if (nocrossref_list != NULL)
563 check_nocrossrefs ();
564 if (command_line.print_memory_usage)
565 lang_print_memory_usage ();
566 #if 0
568 struct bfd_link_hash_entry *h;
570 h = bfd_link_hash_lookup (link_info.hash, "__image_base__", 0,0,1);
571 fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1);
573 #endif
574 ldexp_finish ();
575 lang_finish ();
577 if (config.dependency_file != NULL)
578 write_dependency_file ();
580 /* Even if we're producing relocatable output, some non-fatal errors should
581 be reported in the exit status. (What non-fatal errors, if any, do we
582 want to ignore for relocatable output?) */
583 if (!config.make_executable && !force_make_executable)
585 if (verbose)
586 einfo (_("%P: link errors found, deleting executable `%s'\n"),
587 output_filename);
589 /* The file will be removed by ld_cleanup. */
590 xexit (1);
592 else
594 bfd *obfd = link_info.output_bfd;
595 link_info.output_bfd = NULL;
596 if (!bfd_close (obfd))
597 einfo (_("%F%P: %s: final close failed: %E\n"), output_filename);
599 /* If the --force-exe-suffix is enabled, and we're making an
600 executable file and it doesn't end in .exe, copy it to one
601 which does. */
602 if (!bfd_link_relocatable (&link_info)
603 && command_line.force_exe_suffix)
605 int len = strlen (output_filename);
607 if (len < 4
608 || (strcasecmp (output_filename + len - 4, ".exe") != 0
609 && strcasecmp (output_filename + len - 4, ".dll") != 0))
611 FILE *src;
612 FILE *dst;
613 const int bsize = 4096;
614 char *buf = (char *) xmalloc (bsize);
615 int l;
616 char *dst_name = (char *) xmalloc (len + 5);
618 strcpy (dst_name, output_filename);
619 strcat (dst_name, ".exe");
620 src = fopen (output_filename, FOPEN_RB);
621 dst = fopen (dst_name, FOPEN_WB);
623 if (!src)
624 einfo (_("%F%P: unable to open for source of copy `%s'\n"),
625 output_filename);
626 if (!dst)
627 einfo (_("%F%P: unable to open for destination of copy `%s'\n"),
628 dst_name);
629 while ((l = fread (buf, 1, bsize, src)) > 0)
631 int done = fwrite (buf, 1, l, dst);
633 if (done != l)
634 einfo (_("%P: error writing file `%s'\n"), dst_name);
637 fclose (src);
638 if (fclose (dst) == EOF)
639 einfo (_("%P: error closing file `%s'\n"), dst_name);
640 free (dst_name);
641 free (buf);
646 if (config.stats)
648 long run_time = get_run_time () - start_time;
650 fflush (stdout);
651 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
652 program_name, run_time / 1000000, run_time % 1000000);
653 fflush (stderr);
656 /* Prevent ld_cleanup from deleting the output file. */
657 output_filename = NULL;
659 xexit (0);
660 return 0;
663 /* If the configured sysroot is relocatable, try relocating it based on
664 default prefix FROM. Return the relocated directory if it exists,
665 otherwise return null. */
667 static char *
668 get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
670 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
671 char *path;
672 struct stat s;
674 path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
675 if (path)
677 if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
678 return path;
679 free (path);
681 #endif
682 return 0;
685 /* Return the sysroot directory. Return "" if no sysroot is being used. */
687 static const char *
688 get_sysroot (int argc, char **argv)
690 int i;
691 const char *path = NULL;
693 for (i = 1; i < argc; i++)
694 if (startswith (argv[i], "--sysroot="))
695 path = argv[i] + strlen ("--sysroot=");
697 if (!path)
698 path = get_relative_sysroot (BINDIR);
700 if (!path)
701 path = get_relative_sysroot (TOOLBINDIR);
703 if (!path)
704 path = TARGET_SYSTEM_ROOT;
706 if (IS_DIR_SEPARATOR (*path) && path[1] == 0)
707 path = "";
709 return path;
712 /* We need to find any explicitly given emulation in order to initialize the
713 state that's needed by the lex&yacc argument parser (parse_args). */
715 static char *
716 get_emulation (int argc, char **argv)
718 char *emulation;
719 int i;
721 emulation = getenv (EMULATION_ENVIRON);
722 if (emulation == NULL)
723 emulation = DEFAULT_EMULATION;
725 for (i = 1; i < argc; i++)
727 if (startswith (argv[i], "-m"))
729 if (argv[i][2] == '\0')
731 /* -m EMUL */
732 if (i < argc - 1)
734 emulation = argv[i + 1];
735 i++;
737 else
738 einfo (_("%F%P: missing argument to -m\n"));
740 else if (strcmp (argv[i], "-mips1") == 0
741 || strcmp (argv[i], "-mips2") == 0
742 || strcmp (argv[i], "-mips3") == 0
743 || strcmp (argv[i], "-mips4") == 0
744 || strcmp (argv[i], "-mips5") == 0
745 || strcmp (argv[i], "-mips32") == 0
746 || strcmp (argv[i], "-mips32r2") == 0
747 || strcmp (argv[i], "-mips32r3") == 0
748 || strcmp (argv[i], "-mips32r5") == 0
749 || strcmp (argv[i], "-mips32r6") == 0
750 || strcmp (argv[i], "-mips64") == 0
751 || strcmp (argv[i], "-mips64r2") == 0
752 || strcmp (argv[i], "-mips64r3") == 0
753 || strcmp (argv[i], "-mips64r5") == 0
754 || strcmp (argv[i], "-mips64r6") == 0)
756 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
757 passed to the linker by some MIPS compilers. They
758 generally tell the linker to use a slightly different
759 library path. Perhaps someday these should be
760 implemented as emulations; until then, we just ignore
761 the arguments and hope that nobody ever creates
762 emulations named ips1, ips2 or ips3. */
764 else if (strcmp (argv[i], "-m486") == 0)
766 /* FIXME: The argument -m486 is passed to the linker on
767 some Linux systems. Hope that nobody creates an
768 emulation named 486. */
770 else
772 /* -mEMUL */
773 emulation = &argv[i][2];
778 return emulation;
781 void
782 add_ysym (const char *name)
784 if (link_info.notice_hash == NULL)
786 link_info.notice_hash
787 = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
788 if (!bfd_hash_table_init_n (link_info.notice_hash,
789 bfd_hash_newfunc,
790 sizeof (struct bfd_hash_entry),
791 61))
792 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
795 if (bfd_hash_lookup (link_info.notice_hash, name, true, true) == NULL)
796 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
799 void
800 add_ignoresym (struct bfd_link_info *info, const char *name)
802 if (info->ignore_hash == NULL)
804 info->ignore_hash = xmalloc (sizeof (struct bfd_hash_table));
805 if (!bfd_hash_table_init_n (info->ignore_hash,
806 bfd_hash_newfunc,
807 sizeof (struct bfd_hash_entry),
808 61))
809 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
812 if (bfd_hash_lookup (info->ignore_hash, name, true, true) == NULL)
813 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
816 /* Record a symbol to be wrapped, from the --wrap option. */
818 void
819 add_wrap (const char *name)
821 if (link_info.wrap_hash == NULL)
823 link_info.wrap_hash
824 = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
825 if (!bfd_hash_table_init_n (link_info.wrap_hash,
826 bfd_hash_newfunc,
827 sizeof (struct bfd_hash_entry),
828 61))
829 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
832 if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
833 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
836 /* Handle the -retain-symbols-file option. */
838 void
839 add_keepsyms_file (const char *filename)
841 FILE *file;
842 char *buf;
843 size_t bufsize;
844 int c;
846 if (link_info.strip == strip_some)
847 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
849 file = fopen (filename, "r");
850 if (file == NULL)
852 bfd_set_error (bfd_error_system_call);
853 einfo ("%X%P: %s: %E\n", filename);
854 return;
857 link_info.keep_hash = (struct bfd_hash_table *)
858 xmalloc (sizeof (struct bfd_hash_table));
859 if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc,
860 sizeof (struct bfd_hash_entry)))
861 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
863 bufsize = 100;
864 buf = (char *) xmalloc (bufsize);
866 c = getc (file);
867 while (c != EOF)
869 while (ISSPACE (c))
870 c = getc (file);
872 if (c != EOF)
874 size_t len = 0;
876 while (!ISSPACE (c) && c != EOF)
878 buf[len] = c;
879 ++len;
880 if (len >= bufsize)
882 bufsize *= 2;
883 buf = (char *) xrealloc (buf, bufsize);
885 c = getc (file);
888 buf[len] = '\0';
890 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true) == NULL)
891 einfo (_("%F%P: bfd_hash_lookup for insertion failed: %E\n"));
895 if (link_info.strip != strip_none)
896 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
898 free (buf);
899 link_info.strip = strip_some;
900 fclose (file);
903 /* Callbacks from the BFD linker routines. */
905 /* This is called when BFD has decided to include an archive member in
906 a link. */
908 static bool
909 add_archive_element (struct bfd_link_info *info,
910 bfd *abfd,
911 const char *name,
912 bfd **subsbfd ATTRIBUTE_UNUSED)
914 lang_input_statement_type *input;
915 lang_input_statement_type *parent;
916 lang_input_statement_type orig_input;
918 input = (lang_input_statement_type *)
919 xcalloc (1, sizeof (lang_input_statement_type));
920 input->header.type = lang_input_statement_enum;
921 input->filename = bfd_get_filename (abfd);
922 input->local_sym_name = bfd_get_filename (abfd);
923 input->the_bfd = abfd;
925 /* Save the original data for trace files/tries below, as plugins
926 (if enabled) may possibly alter it to point to a replacement
927 BFD, but we still want to output the original BFD filename. */
928 orig_input = *input;
929 #if BFD_SUPPORTS_PLUGINS
930 /* Don't claim a fat IR object if no IR object should be claimed. */
931 if (link_info.lto_plugin_active
932 && (!no_more_claiming
933 || bfd_get_lto_type (abfd) != lto_fat_ir_object))
935 /* We must offer this archive member to the plugins to claim. */
936 plugin_maybe_claim (input);
937 if (input->flags.claimed)
939 if (no_more_claiming)
941 /* Don't claim new IR symbols after all IR symbols have
942 been claimed. */
943 if (verbose)
944 info_msg ("%pI: no new IR symbols to claim\n",
945 &orig_input);
946 input->flags.claimed = 0;
947 return false;
949 input->flags.claim_archive = true;
950 *subsbfd = input->the_bfd;
953 #endif /* BFD_SUPPORTS_PLUGINS */
955 if (link_info.input_bfds_tail == &input->the_bfd->link.next
956 || input->the_bfd->link.next != NULL)
958 /* We have already loaded this element, and are attempting to
959 load it again. This can happen when the archive map doesn't
960 match actual symbols defined by the element. */
961 free (input);
962 bfd_set_error (bfd_error_malformed_archive);
963 return false;
966 /* Set the file_chain pointer of archives to the last element loaded
967 from the archive. See ldlang.c:find_rescan_insertion. */
968 parent = bfd_usrdata (abfd->my_archive);
969 if (parent != NULL && !parent->flags.reload)
970 parent->next = input;
972 ldlang_add_file (input);
974 if (config.map_file != NULL)
976 static bool header_printed;
977 struct bfd_link_hash_entry *h;
978 bfd *from;
979 int len;
981 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
982 if (h == NULL
983 && info->pei386_auto_import
984 && startswith (name, "__imp_"))
985 h = bfd_link_hash_lookup (info->hash, name + 6, false, false, true);
987 if (h == NULL)
988 from = NULL;
989 else
991 switch (h->type)
993 default:
994 from = NULL;
995 break;
997 case bfd_link_hash_defined:
998 case bfd_link_hash_defweak:
999 from = h->u.def.section->owner;
1000 break;
1002 case bfd_link_hash_undefined:
1003 case bfd_link_hash_undefweak:
1004 from = h->u.undef.abfd;
1005 break;
1007 case bfd_link_hash_common:
1008 from = h->u.c.p->section->owner;
1009 break;
1013 if (!header_printed)
1015 minfo (_("Archive member included to satisfy reference by file (symbol)\n\n"));
1016 header_printed = true;
1019 if (abfd->my_archive == NULL
1020 || bfd_is_thin_archive (abfd->my_archive))
1022 minfo ("%s", bfd_get_filename (abfd));
1023 len = strlen (bfd_get_filename (abfd));
1025 else
1027 minfo ("%s(%s)", bfd_get_filename (abfd->my_archive),
1028 bfd_get_filename (abfd));
1029 len = (strlen (bfd_get_filename (abfd->my_archive))
1030 + strlen (bfd_get_filename (abfd))
1031 + 2);
1034 if (len >= 29)
1036 print_nl ();
1037 len = 0;
1039 print_spaces (30 - len);
1041 if (from != NULL)
1042 minfo ("%pB ", from);
1043 if (h != NULL)
1044 minfo ("(%pT)\n", h->root.string);
1045 else
1046 minfo ("(%s)\n", name);
1049 if (verbose
1050 || trace_files > 1
1051 || (trace_files && bfd_is_thin_archive (orig_input.the_bfd->my_archive)))
1052 info_msg ("%pI\n", &orig_input);
1053 return true;
1056 /* This is called when BFD has discovered a symbol which is defined
1057 multiple times. */
1059 static void
1060 multiple_definition (struct bfd_link_info *info,
1061 struct bfd_link_hash_entry *h,
1062 bfd *nbfd,
1063 asection *nsec,
1064 bfd_vma nval)
1066 const char *name;
1067 bfd *obfd;
1068 asection *osec;
1069 bfd_vma oval;
1071 if (info->allow_multiple_definition)
1072 return;
1074 switch (h->type)
1076 case bfd_link_hash_defined:
1077 osec = h->u.def.section;
1078 oval = h->u.def.value;
1079 obfd = h->u.def.section->owner;
1080 break;
1081 case bfd_link_hash_indirect:
1082 osec = bfd_ind_section_ptr;
1083 oval = 0;
1084 obfd = NULL;
1085 break;
1086 default:
1087 abort ();
1090 /* Ignore a redefinition of an absolute symbol to the
1091 same value; it's harmless. */
1092 if (h->type == bfd_link_hash_defined
1093 && bfd_is_abs_section (osec)
1094 && bfd_is_abs_section (nsec)
1095 && nval == oval)
1096 return;
1098 /* If either section has the output_section field set to
1099 bfd_abs_section_ptr, it means that the section is being
1100 discarded, and this is not really a multiple definition at all.
1101 FIXME: It would be cleaner to somehow ignore symbols defined in
1102 sections which are being discarded. */
1103 if (!info->prohibit_multiple_definition_absolute
1104 && ((osec->output_section != NULL
1105 && ! bfd_is_abs_section (osec)
1106 && bfd_is_abs_section (osec->output_section))
1107 || (nsec->output_section != NULL
1108 && !bfd_is_abs_section (nsec)
1109 && bfd_is_abs_section (nsec->output_section))))
1110 return;
1112 name = h->root.string;
1113 if (nbfd == NULL)
1115 nbfd = obfd;
1116 nsec = osec;
1117 nval = oval;
1118 obfd = NULL;
1120 if (info->warn_multiple_definition)
1121 einfo (_("%P: %C: warning: multiple definition of `%pT'"),
1122 nbfd, nsec, nval, name);
1123 else
1124 einfo (_("%X%P: %C: multiple definition of `%pT'"),
1125 nbfd, nsec, nval, name);
1126 if (obfd != NULL)
1127 einfo (_("; %D: first defined here"), obfd, osec, oval);
1128 einfo ("\n");
1130 if (RELAXATION_ENABLED_BY_USER)
1132 einfo (_("%P: disabling relaxation; it will not work with multiple definitions\n"));
1133 DISABLE_RELAXATION;
1137 /* This is called when there is a definition of a common symbol, or
1138 when a common symbol is found for a symbol that is already defined,
1139 or when two common symbols are found. We only do something if
1140 -warn-common was used. */
1142 static void
1143 multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1144 struct bfd_link_hash_entry *h,
1145 bfd *nbfd,
1146 enum bfd_link_hash_type ntype,
1147 bfd_vma nsize)
1149 const char *name;
1150 bfd *obfd;
1151 enum bfd_link_hash_type otype;
1152 bfd_vma osize;
1154 if (!config.warn_common)
1155 return;
1157 name = h->root.string;
1158 otype = h->type;
1159 if (otype == bfd_link_hash_common)
1161 obfd = h->u.c.p->section->owner;
1162 osize = h->u.c.size;
1164 else if (otype == bfd_link_hash_defined
1165 || otype == bfd_link_hash_defweak)
1167 obfd = h->u.def.section->owner;
1168 osize = 0;
1170 else
1172 /* FIXME: It would nice if we could report the BFD which defined
1173 an indirect symbol, but we don't have anywhere to store the
1174 information. */
1175 obfd = NULL;
1176 osize = 0;
1179 if (ntype == bfd_link_hash_defined
1180 || ntype == bfd_link_hash_defweak
1181 || ntype == bfd_link_hash_indirect)
1183 ASSERT (otype == bfd_link_hash_common);
1184 if (obfd != NULL)
1185 einfo (_("%P: %pB: warning: definition of `%pT' overriding common"
1186 " from %pB\n"),
1187 nbfd, name, obfd);
1188 else
1189 einfo (_("%P: %pB: warning: definition of `%pT' overriding common\n"),
1190 nbfd, name);
1192 else if (otype == bfd_link_hash_defined
1193 || otype == bfd_link_hash_defweak
1194 || otype == bfd_link_hash_indirect)
1196 ASSERT (ntype == bfd_link_hash_common);
1197 if (obfd != NULL)
1198 einfo (_("%P: %pB: warning: common of `%pT' overridden by definition"
1199 " from %pB\n"),
1200 nbfd, name, obfd);
1201 else
1202 einfo (_("%P: %pB: warning: common of `%pT' overridden by definition\n"),
1203 nbfd, name);
1205 else
1207 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
1208 if (osize > nsize)
1210 if (obfd != NULL)
1211 einfo (_("%P: %pB: warning: common of `%pT' overridden"
1212 " by larger common from %pB\n"),
1213 nbfd, name, obfd);
1214 else
1215 einfo (_("%P: %pB: warning: common of `%pT' overridden"
1216 " by larger common\n"),
1217 nbfd, name);
1219 else if (nsize > osize)
1221 if (obfd != NULL)
1222 einfo (_("%P: %pB: warning: common of `%pT' overriding"
1223 " smaller common from %pB\n"),
1224 nbfd, name, obfd);
1225 else
1226 einfo (_("%P: %pB: warning: common of `%pT' overriding"
1227 " smaller common\n"),
1228 nbfd, name);
1230 else
1232 if (obfd != NULL)
1233 einfo (_("%P: %pB and %pB: warning: multiple common of `%pT'\n"),
1234 nbfd, obfd, name);
1235 else
1236 einfo (_("%P: %pB: warning: multiple common of `%pT'\n"),
1237 nbfd, name);
1242 /* This is called when BFD has discovered a set element. H is the
1243 entry in the linker hash table for the set. SECTION and VALUE
1244 represent a value which should be added to the set. */
1246 static void
1247 add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1248 struct bfd_link_hash_entry *h,
1249 bfd_reloc_code_real_type reloc,
1250 bfd *abfd,
1251 asection *section,
1252 bfd_vma value)
1254 if (config.warn_constructors)
1255 einfo (_("%P: warning: global constructor %s used\n"),
1256 h->root.string);
1258 if (!config.build_constructors)
1259 return;
1261 ldctor_add_set_entry (h, reloc, NULL, section, value);
1263 if (h->type == bfd_link_hash_new)
1265 h->type = bfd_link_hash_undefined;
1266 h->u.undef.abfd = abfd;
1267 /* We don't call bfd_link_add_undef to add this to the list of
1268 undefined symbols because we are going to define it
1269 ourselves. */
1273 /* This is called when BFD has discovered a constructor. This is only
1274 called for some object file formats--those which do not handle
1275 constructors in some more clever fashion. This is similar to
1276 adding an element to a set, but less general. */
1278 static void
1279 constructor_callback (struct bfd_link_info *info,
1280 bool constructor,
1281 const char *name,
1282 bfd *abfd,
1283 asection *section,
1284 bfd_vma value)
1286 char *s;
1287 struct bfd_link_hash_entry *h;
1288 char set_name[1 + sizeof "__CTOR_LIST__"];
1290 if (config.warn_constructors)
1291 einfo (_("%P: warning: global constructor %s used\n"), name);
1293 if (!config.build_constructors)
1294 return;
1296 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1297 useful error message. */
1298 if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL
1299 && (bfd_link_relocatable (info)
1300 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
1301 einfo (_("%F%P: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1303 s = set_name;
1304 if (bfd_get_symbol_leading_char (abfd) != '\0')
1305 *s++ = bfd_get_symbol_leading_char (abfd);
1306 if (constructor)
1307 strcpy (s, "__CTOR_LIST__");
1308 else
1309 strcpy (s, "__DTOR_LIST__");
1311 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
1312 if (h == (struct bfd_link_hash_entry *) NULL)
1313 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1314 if (h->type == bfd_link_hash_new)
1316 h->type = bfd_link_hash_undefined;
1317 h->u.undef.abfd = abfd;
1318 /* We don't call bfd_link_add_undef to add this to the list of
1319 undefined symbols because we are going to define it
1320 ourselves. */
1323 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
1326 /* A structure used by warning_callback to pass information through
1327 bfd_map_over_sections. */
1329 struct warning_callback_info
1331 bool found;
1332 const char *warning;
1333 const char *symbol;
1334 asymbol **asymbols;
1337 /* Look through the relocs to see if we can find a plausible address
1338 for SYMBOL in ABFD. Return TRUE if found. Otherwise return FALSE. */
1340 static bool
1341 symbol_warning (const char *warning, const char *symbol, bfd *abfd)
1343 struct warning_callback_info cinfo;
1345 if (!bfd_generic_link_read_symbols (abfd))
1346 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd);
1348 cinfo.found = false;
1349 cinfo.warning = warning;
1350 cinfo.symbol = symbol;
1351 cinfo.asymbols = bfd_get_outsymbols (abfd);
1352 bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
1353 return cinfo.found;
1356 /* This is called when there is a reference to a warning symbol. */
1358 static void
1359 warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1360 const char *warning,
1361 const char *symbol,
1362 bfd *abfd,
1363 asection *section,
1364 bfd_vma address)
1366 /* This is a hack to support warn_multiple_gp. FIXME: This should
1367 have a cleaner interface, but what? */
1368 if (!config.warn_multiple_gp
1369 && strcmp (warning, "using multiple gp values") == 0)
1370 return;
1372 if (section != NULL)
1373 einfo ("%P: %C: %s%s\n", abfd, section, address, _("warning: "), warning);
1374 else if (abfd == NULL)
1375 einfo ("%P: %s%s\n", _("warning: "), warning);
1376 else if (symbol == NULL)
1377 einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
1378 else if (!symbol_warning (warning, symbol, abfd))
1380 bfd *b;
1381 /* Search all input files for a reference to SYMBOL. */
1382 for (b = info->input_bfds; b; b = b->link.next)
1383 if (b != abfd && symbol_warning (warning, symbol, b))
1384 return;
1385 einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
1389 /* This is called by warning_callback for each section. It checks the
1390 relocs of the section to see if it can find a reference to the
1391 symbol which triggered the warning. If it can, it uses the reloc
1392 to give an error message with a file and line number. */
1394 static void
1395 warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
1397 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
1398 long relsize;
1399 arelent **relpp;
1400 long relcount;
1401 arelent **p, **pend;
1403 if (info->found)
1404 return;
1406 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1407 if (relsize < 0)
1408 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
1409 if (relsize == 0)
1410 return;
1412 relpp = (arelent **) xmalloc (relsize);
1413 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1414 if (relcount < 0)
1415 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
1417 p = relpp;
1418 pend = p + relcount;
1419 for (; p < pend && *p != NULL; p++)
1421 arelent *q = *p;
1423 if (q->sym_ptr_ptr != NULL
1424 && *q->sym_ptr_ptr != NULL
1425 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1427 /* We found a reloc for the symbol we are looking for. */
1428 einfo ("%P: %H: %s%s\n", abfd, sec, q->address, _("warning: "),
1429 info->warning);
1430 info->found = true;
1431 break;
1435 free (relpp);
1438 #if SUPPORT_ERROR_HANDLING_SCRIPT
1439 char * error_handling_script = NULL;
1440 #endif
1442 /* This is called when an undefined symbol is found. */
1444 static void
1445 undefined_symbol (struct bfd_link_info *info,
1446 const char *name,
1447 bfd *abfd,
1448 asection *section,
1449 bfd_vma address,
1450 bool error)
1452 static char *error_name;
1453 static unsigned int error_count;
1455 #define MAX_ERRORS_IN_A_ROW 5
1457 if (info->ignore_hash != NULL
1458 && bfd_hash_lookup (info->ignore_hash, name, false, false) != NULL)
1459 return;
1461 if (config.warn_once)
1463 /* Only warn once about a particular undefined symbol. */
1464 add_ignoresym (info, name);
1467 /* We never print more than a reasonable number of errors in a row
1468 for a single symbol. */
1469 if (error_name != NULL
1470 && strcmp (name, error_name) == 0)
1471 ++error_count;
1472 else
1474 error_count = 0;
1475 free (error_name);
1476 error_name = xstrdup (name);
1479 #if SUPPORT_ERROR_HANDLING_SCRIPT
1480 if (error_handling_script != NULL
1481 && error_count < MAX_ERRORS_IN_A_ROW)
1483 char * argv[4];
1484 const char * res;
1485 int status, err;
1487 argv[0] = error_handling_script;
1488 argv[1] = "undefined-symbol";
1489 argv[2] = (char *) name;
1490 argv[3] = NULL;
1492 if (verbose)
1493 einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
1494 argv[0], argv[1], argv[2]);
1496 res = pex_one (PEX_SEARCH, error_handling_script, argv,
1497 N_("error handling script"),
1498 NULL /* Send stdout to random, temp file. */,
1499 NULL /* Write to stderr. */,
1500 &status, &err);
1501 if (res != NULL)
1503 einfo (_("%P: Failed to run error handling script '%s', reason: "),
1504 error_handling_script);
1505 /* FIXME: We assume here that errrno == err. */
1506 perror (res);
1508 /* We ignore the return status of the script and
1509 carry on to issue the normal error message. */
1511 #endif /* SUPPORT_ERROR_HANDLING_SCRIPT */
1513 if (section != NULL)
1515 if (error_count < MAX_ERRORS_IN_A_ROW)
1517 if (error)
1518 einfo (_("%X%P: %H: undefined reference to `%pT'\n"),
1519 abfd, section, address, name);
1520 else
1521 einfo (_("%P: %H: warning: undefined reference to `%pT'\n"),
1522 abfd, section, address, name);
1524 else if (error_count == MAX_ERRORS_IN_A_ROW)
1526 if (error)
1527 einfo (_("%X%P: %D: more undefined references to `%pT' follow\n"),
1528 abfd, section, address, name);
1529 else
1530 einfo (_("%P: %D: warning: more undefined references to `%pT' follow\n"),
1531 abfd, section, address, name);
1533 else if (error)
1534 einfo ("%X");
1536 else
1538 if (error_count < MAX_ERRORS_IN_A_ROW)
1540 if (error)
1541 einfo (_("%X%P: %pB: undefined reference to `%pT'\n"),
1542 abfd, name);
1543 else
1544 einfo (_("%P: %pB: warning: undefined reference to `%pT'\n"),
1545 abfd, name);
1547 else if (error_count == MAX_ERRORS_IN_A_ROW)
1549 if (error)
1550 einfo (_("%X%P: %pB: more undefined references to `%pT' follow\n"),
1551 abfd, name);
1552 else
1553 einfo (_("%P: %pB: warning: more undefined references to `%pT' follow\n"),
1554 abfd, name);
1556 else if (error)
1557 einfo ("%X");
1561 /* Counter to limit the number of relocation overflow error messages
1562 to print. Errors are printed as it is decremented. When it's
1563 called and the counter is zero, a final message is printed
1564 indicating more relocations were omitted. When it gets to -1, no
1565 such errors are printed. If it's initially set to a value less
1566 than -1, all such errors will be printed (--verbose does this). */
1568 int overflow_cutoff_limit = 10;
1570 /* This is called when a reloc overflows. */
1572 static void
1573 reloc_overflow (struct bfd_link_info *info,
1574 struct bfd_link_hash_entry *entry,
1575 const char *name,
1576 const char *reloc_name,
1577 bfd_vma addend,
1578 bfd *abfd,
1579 asection *section,
1580 bfd_vma address)
1582 if (overflow_cutoff_limit == -1)
1583 return;
1585 einfo ("%X%H:", abfd, section, address);
1587 if (overflow_cutoff_limit >= 0
1588 && overflow_cutoff_limit-- == 0)
1590 einfo (_(" additional relocation overflows omitted from the output\n"));
1591 return;
1594 if (entry)
1596 while (entry->type == bfd_link_hash_indirect
1597 || entry->type == bfd_link_hash_warning)
1598 entry = entry->u.i.link;
1599 switch (entry->type)
1601 case bfd_link_hash_undefined:
1602 case bfd_link_hash_undefweak:
1603 einfo (_(" relocation truncated to fit: "
1604 "%s against undefined symbol `%pT'"),
1605 reloc_name, entry->root.string);
1606 break;
1607 case bfd_link_hash_defined:
1608 case bfd_link_hash_defweak:
1609 einfo (_(" relocation truncated to fit: "
1610 "%s against symbol `%pT' defined in %pA section in %pB"),
1611 reloc_name, entry->root.string,
1612 entry->u.def.section,
1613 entry->u.def.section == bfd_abs_section_ptr
1614 ? info->output_bfd : entry->u.def.section->owner);
1615 break;
1616 default:
1617 abort ();
1618 break;
1621 else
1622 einfo (_(" relocation truncated to fit: %s against `%pT'"),
1623 reloc_name, name);
1624 if (addend != 0)
1625 einfo ("+%v", addend);
1626 einfo ("\n");
1629 /* This is called when a dangerous relocation is made. */
1631 static void
1632 reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1633 const char *message,
1634 bfd *abfd,
1635 asection *section,
1636 bfd_vma address)
1638 einfo (_("%X%H: dangerous relocation: %s\n"),
1639 abfd, section, address, message);
1642 /* This is called when a reloc is being generated attached to a symbol
1643 that is not being output. */
1645 static void
1646 unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1647 const char *name,
1648 bfd *abfd,
1649 asection *section,
1650 bfd_vma address)
1652 einfo (_("%X%H: reloc refers to symbol `%pT' which is not being output\n"),
1653 abfd, section, address, name);
1656 /* This is called if link_info.notice_all is set, or when a symbol in
1657 link_info.notice_hash is found. Symbols are put in notice_hash
1658 using the -y option, while notice_all is set if the --cref option
1659 has been supplied, or if there are any NOCROSSREFS sections in the
1660 linker script; and if plugins are active, since they need to monitor
1661 all references from non-IR files. */
1663 static bool
1664 notice (struct bfd_link_info *info,
1665 struct bfd_link_hash_entry *h,
1666 struct bfd_link_hash_entry *inh ATTRIBUTE_UNUSED,
1667 bfd *abfd,
1668 asection *section,
1669 bfd_vma value,
1670 flagword flags ATTRIBUTE_UNUSED)
1672 const char *name;
1674 if (h == NULL)
1676 if (command_line.cref || nocrossref_list != NULL)
1677 return handle_asneeded_cref (abfd, (enum notice_asneeded_action) value);
1678 return true;
1681 name = h->root.string;
1682 if (info->notice_hash != NULL
1683 && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL)
1685 if (bfd_is_und_section (section))
1686 einfo (_("%P: %pB: reference to %s\n"), abfd, name);
1687 else
1688 einfo (_("%P: %pB: definition of %s\n"), abfd, name);
1691 if (command_line.cref || nocrossref_list != NULL)
1692 add_cref (name, abfd, section, value);
1694 return true;