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