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