1 /* Main program of GNU linker.
2 Copyright (C) 1991-2021 Free Software Foundation, Inc.
3 Written by Steve Chamberlain steve@cygnus.com
5 This file is part of the GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
24 #include "safe-ctype.h"
25 #include "libiberty.h"
29 #include "filenames.h"
30 #include "elf/common.h"
43 #if BFD_SUPPORTS_PLUGINS
45 #include "plugin-api.h"
46 #endif /* BFD_SUPPORTS_PLUGINS */
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)
55 #ifndef TARGET_SYSTEM_ROOT
56 #define TARGET_SYSTEM_ROOT ""
61 FILE *saved_script_handle
= NULL
;
62 FILE *previous_script_handle
= NULL
;
63 bool force_make_executable
= false;
66 const char *output_filename
= "a.out";
68 /* Name this program was invoked by. */
71 /* The prefix for system library directories. */
72 const char *ld_sysroot
;
74 /* The canonical representation of ld_sysroot. */
75 char *ld_canon_sysroot
;
76 int ld_canon_sysroot_len
;
78 /* Set by -G argument, for targets like MIPS ELF. */
79 int g_switch_value
= 8;
81 /* Nonzero means print names of input files as processed. */
82 unsigned int trace_files
;
84 /* Nonzero means report actions taken by the linker, and describe the linker script in use. */
87 /* Nonzero means version number was printed, so exit successfully
88 instead of complaining if no input files are given. */
91 /* TRUE if we should demangle symbol names. */
94 args_type command_line
;
96 ld_config_type config
;
98 sort_type sort_section
;
100 static const char *get_sysroot
102 static char *get_emulation
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
,
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
);
134 (struct bfd_link_info
*, struct bfd_link_hash_entry
*,
135 struct bfd_link_hash_entry
*, bfd
*, asection
*, bfd_vma
, flagword
);
137 static struct bfd_link_callbacks link_callbacks
=
143 constructor_callback
,
153 ldlang_override_segment_assignment
,
154 ldlang_ctf_acquire_strings
,
156 ldlang_ctf_new_dynsym
,
157 ldlang_write_ctf_late
160 static bfd_assert_handler_type default_bfd_assert_handler
;
161 static bfd_error_handler_type default_bfd_error_handler
;
163 struct bfd_link_info link_info
;
165 struct dependency_file
167 struct dependency_file
*next
;
171 static struct dependency_file
*dependency_files
, *dependency_files_tail
;
174 track_dependency_files (const char *filename
)
176 struct dependency_file
*dep
177 = (struct dependency_file
*) xmalloc (sizeof (*dep
));
178 dep
->name
= xstrdup (filename
);
180 if (dependency_files
== NULL
)
181 dependency_files
= dep
;
183 dependency_files_tail
->next
= dep
;
184 dependency_files_tail
= dep
;
188 write_dependency_file (void)
191 struct dependency_file
*dep
;
193 out
= fopen (config
.dependency_file
, FOPEN_WT
);
196 einfo (_("%F%P: cannot open dependency file %s: %E\n"),
197 config
.dependency_file
);
200 fprintf (out
, "%s:", output_filename
);
202 for (dep
= dependency_files
; dep
!= NULL
; dep
= dep
->next
)
203 fprintf (out
, " \\\n %s", dep
->name
);
206 for (dep
= dependency_files
; dep
!= NULL
; dep
= dep
->next
)
207 fprintf (out
, "\n%s:\n", dep
->name
);
215 bfd_cache_close_all ();
216 #if BFD_SUPPORTS_PLUGINS
217 plugin_call_cleanup ();
219 if (output_filename
&& delete_output_file_on_failure
)
220 unlink_if_ordinary (output_filename
);
223 /* Hook to notice BFD assertions. */
226 ld_bfd_assert_handler (const char *fmt
, const char *bfdver
,
227 const char *file
, int line
)
229 config
.make_executable
= false;
230 (*default_bfd_assert_handler
) (fmt
, bfdver
, file
, line
);
233 /* Hook the bfd error/warning handler for --fatal-warnings. */
236 ld_bfd_error_handler (const char *fmt
, va_list ap
)
238 if (config
.fatal_warnings
)
239 config
.make_executable
= false;
240 (*default_bfd_error_handler
) (fmt
, ap
);
244 main (int argc
, char **argv
)
247 long start_time
= get_run_time ();
249 #ifdef HAVE_LC_MESSAGES
250 setlocale (LC_MESSAGES
, "");
252 setlocale (LC_CTYPE
, "");
253 bindtextdomain (PACKAGE
, LOCALEDIR
);
254 textdomain (PACKAGE
);
256 program_name
= argv
[0];
257 xmalloc_set_program_name (program_name
);
259 START_PROGRESS (program_name
, 0);
261 expandargv (&argc
, &argv
);
263 if (bfd_init () != BFD_INIT_MAGIC
)
264 einfo (_("%F%P: fatal error: libbfd ABI mismatch\n"));
266 bfd_set_error_program_name (program_name
);
268 /* We want to notice and fail on those nasty BFD assertions which are
269 likely to signal incorrect output being generated but otherwise may
271 default_bfd_assert_handler
= bfd_set_assert_handler (ld_bfd_assert_handler
);
273 /* Also hook the bfd error/warning handler for --fatal-warnings. */
274 default_bfd_error_handler
= bfd_set_error_handler (ld_bfd_error_handler
);
276 xatexit (ld_cleanup
);
278 /* Set up the sysroot directory. */
279 ld_sysroot
= get_sysroot (argc
, argv
);
281 ld_canon_sysroot
= lrealpath (ld_sysroot
);
282 if (ld_canon_sysroot
)
284 ld_canon_sysroot_len
= strlen (ld_canon_sysroot
);
286 /* is_sysrooted_pathname() relies on no trailing dirsep. */
287 if (ld_canon_sysroot_len
> 0
288 && IS_DIR_SEPARATOR (ld_canon_sysroot
[ld_canon_sysroot_len
- 1]))
289 ld_canon_sysroot
[--ld_canon_sysroot_len
] = '\0';
292 ld_canon_sysroot_len
= -1;
294 /* Set the default BFD target based on the configured target. Doing
295 this permits the linker to be configured for a particular target,
296 and linked against a shared BFD library which was configured for
297 a different target. The macro TARGET is defined by Makefile. */
298 if (!bfd_set_default_target (TARGET
))
300 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET
);
311 config
.build_constructors
= true;
312 config
.rpath_separator
= ':';
313 config
.split_by_reloc
= (unsigned) -1;
314 config
.split_by_file
= (bfd_size_type
) -1;
315 config
.make_executable
= true;
316 config
.magic_demand_paged
= true;
317 config
.text_read_only
= true;
318 config
.print_map_discarded
= true;
319 link_info
.disable_target_specific_optimizations
= -1;
321 command_line
.warn_mismatch
= true;
322 command_line
.warn_search_mismatch
= true;
323 command_line
.check_section_addresses
= -1;
325 /* We initialize DEMANGLING based on the environment variable
326 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
327 output of the linker, unless COLLECT_NO_DEMANGLE is set in the
328 environment. Acting the same way here lets us provide the same
329 interface by default. */
330 demangling
= getenv ("COLLECT_NO_DEMANGLE") == NULL
;
332 link_info
.allow_undefined_version
= true;
333 link_info
.keep_memory
= true;
334 link_info
.combreloc
= true;
335 link_info
.strip_discarded
= true;
336 link_info
.prohibit_multiple_definition_absolute
= false;
337 link_info
.textrel_check
= DEFAULT_LD_TEXTREL_CHECK
;
338 link_info
.emit_hash
= DEFAULT_EMIT_SYSV_HASH
;
339 link_info
.emit_gnu_hash
= DEFAULT_EMIT_GNU_HASH
;
340 link_info
.callbacks
= &link_callbacks
;
341 link_info
.input_bfds_tail
= &link_info
.input_bfds
;
342 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
343 and _fini symbols. We are compatible. */
344 link_info
.init_function
= "_init";
345 link_info
.fini_function
= "_fini";
346 link_info
.relax_pass
= 1;
347 link_info
.extern_protected_data
= -1;
348 link_info
.dynamic_undefined_weak
= -1;
349 link_info
.pei386_auto_import
= -1;
350 link_info
.spare_dynamic_tags
= 5;
351 link_info
.path_separator
= ':';
352 #ifdef DEFAULT_FLAG_COMPRESS_DEBUG
353 link_info
.compress_debug
= COMPRESS_DEBUG_GABI_ZLIB
;
355 #ifdef DEFAULT_NEW_DTAGS
356 link_info
.new_dtags
= DEFAULT_NEW_DTAGS
;
358 link_info
.start_stop_gc
= false;
359 link_info
.start_stop_visibility
= STV_PROTECTED
;
361 ldfile_add_arch ("");
362 emulation
= get_emulation (argc
, argv
);
363 ldemul_choose_mode (emulation
);
364 default_target
= ldemul_choose_target (argc
, argv
);
367 ldemul_before_parse ();
368 lang_has_input_file
= false;
369 parse_args (argc
, argv
);
371 if (config
.hash_table_size
!= 0)
372 bfd_hash_set_default_size (config
.hash_table_size
);
374 #if BFD_SUPPORTS_PLUGINS
375 /* Now all the plugin arguments have been gathered, we can load them. */
376 plugin_load_plugins ();
377 #endif /* BFD_SUPPORTS_PLUGINS */
379 ldemul_set_symbols ();
381 /* If we have not already opened and parsed a linker script,
382 try the default script from command line first. */
383 if (saved_script_handle
== NULL
384 && command_line
.default_script
!= NULL
)
386 ldfile_open_script_file (command_line
.default_script
);
387 parser_input
= input_script
;
391 /* If we have not already opened and parsed a linker script
392 read the emulation's appropriate default script. */
393 if (saved_script_handle
== NULL
)
396 char *s
= ldemul_get_script (&isfile
);
399 ldfile_open_default_command_file (s
);
403 lex_redirect (s
, _("built in linker script"), 1);
405 parser_input
= input_script
;
412 if (saved_script_handle
)
413 info_msg (_("using external linker script:"));
415 info_msg (_("using internal linker script:"));
416 info_msg ("\n==================================================\n");
418 if (saved_script_handle
)
420 static const int ld_bufsz
= 8193;
422 char *buf
= (char *) xmalloc (ld_bufsz
);
424 rewind (saved_script_handle
);
425 while ((n
= fread (buf
, 1, ld_bufsz
- 1, saved_script_handle
)) > 0)
428 info_msg ("%s", buf
);
430 rewind (saved_script_handle
);
437 info_msg (ldemul_get_script (&isfile
));
440 info_msg ("\n==================================================\n");
443 if (command_line
.force_group_allocation
444 || !bfd_link_relocatable (&link_info
))
445 link_info
.resolve_section_groups
= true;
447 link_info
.resolve_section_groups
= false;
449 if (command_line
.print_output_format
)
450 info_msg ("%s\n", lang_get_output_target ());
454 /* If the only command line argument has been -v or --version or --verbose
455 then ignore any input files provided by linker scripts and exit now.
456 We do not want to create an output file when the linker is just invoked
457 to provide version information. */
458 if (argc
== 2 && version_printed
)
461 if (link_info
.inhibit_common_definition
&& !bfd_link_dll (&link_info
))
462 einfo (_("%F%P: --no-define-common may not be used without -shared\n"));
464 if (!lang_has_input_file
)
466 if (version_printed
|| command_line
.print_output_format
)
468 einfo (_("%F%P: no input files\n"));
472 info_msg (_("%P: mode %s\n"), emulation
);
474 ldemul_after_parse ();
476 if (config
.map_filename
)
478 if (strcmp (config
.map_filename
, "-") == 0)
480 config
.map_file
= stdout
;
484 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
485 if (config
.map_file
== (FILE *) NULL
)
487 bfd_set_error (bfd_error_system_call
);
488 einfo (_("%F%P: cannot open map file %s: %E\n"),
489 config
.map_filename
);
492 link_info
.has_map_file
= true;
497 /* Print error messages for any missing symbols, for any warning
498 symbols, and possibly multiple definitions. */
499 if (bfd_link_relocatable (&link_info
))
500 link_info
.output_bfd
->flags
&= ~EXEC_P
;
502 link_info
.output_bfd
->flags
|= EXEC_P
;
504 if ((link_info
.compress_debug
& COMPRESS_DEBUG
))
506 link_info
.output_bfd
->flags
|= BFD_COMPRESS
;
507 if (link_info
.compress_debug
== COMPRESS_DEBUG_GABI_ZLIB
)
508 link_info
.output_bfd
->flags
|= BFD_COMPRESS_GABI
;
513 if (config
.map_file
!= NULL
)
515 if (command_line
.cref
)
516 output_cref (config
.map_file
!= NULL
? config
.map_file
: stdout
);
517 if (nocrossref_list
!= NULL
)
518 check_nocrossrefs ();
519 if (command_line
.print_memory_usage
)
520 lang_print_memory_usage ();
523 struct bfd_link_hash_entry
*h
;
525 h
= bfd_link_hash_lookup (link_info
.hash
, "__image_base__", 0,0,1);
526 fprintf (stderr
, "lookup = %p val %lx\n", h
, h
? h
->u
.def
.value
: 1);
532 if (config
.dependency_file
!= NULL
)
533 write_dependency_file ();
535 /* Even if we're producing relocatable output, some non-fatal errors should
536 be reported in the exit status. (What non-fatal errors, if any, do we
537 want to ignore for relocatable output?) */
538 if (!config
.make_executable
&& !force_make_executable
)
541 einfo (_("%P: link errors found, deleting executable `%s'\n"),
544 /* The file will be removed by ld_cleanup. */
549 if (!bfd_close (link_info
.output_bfd
))
550 einfo (_("%F%P: %pB: final close failed: %E\n"), link_info
.output_bfd
);
552 /* If the --force-exe-suffix is enabled, and we're making an
553 executable file and it doesn't end in .exe, copy it to one
555 if (!bfd_link_relocatable (&link_info
)
556 && command_line
.force_exe_suffix
)
558 int len
= strlen (output_filename
);
561 || (strcasecmp (output_filename
+ len
- 4, ".exe") != 0
562 && strcasecmp (output_filename
+ len
- 4, ".dll") != 0))
566 const int bsize
= 4096;
567 char *buf
= (char *) xmalloc (bsize
);
569 char *dst_name
= (char *) xmalloc (len
+ 5);
571 strcpy (dst_name
, output_filename
);
572 strcat (dst_name
, ".exe");
573 src
= fopen (output_filename
, FOPEN_RB
);
574 dst
= fopen (dst_name
, FOPEN_WB
);
577 einfo (_("%F%P: unable to open for source of copy `%s'\n"),
580 einfo (_("%F%P: unable to open for destination of copy `%s'\n"),
582 while ((l
= fread (buf
, 1, bsize
, src
)) > 0)
584 int done
= fwrite (buf
, 1, l
, dst
);
587 einfo (_("%P: error writing file `%s'\n"), dst_name
);
591 if (fclose (dst
) == EOF
)
592 einfo (_("%P: error closing file `%s'\n"), dst_name
);
599 END_PROGRESS (program_name
);
603 long run_time
= get_run_time () - start_time
;
606 fprintf (stderr
, _("%s: total time in link: %ld.%06ld\n"),
607 program_name
, run_time
/ 1000000, run_time
% 1000000);
611 /* Prevent ld_cleanup from doing anything, after a successful link. */
612 output_filename
= NULL
;
618 /* If the configured sysroot is relocatable, try relocating it based on
619 default prefix FROM. Return the relocated directory if it exists,
620 otherwise return null. */
623 get_relative_sysroot (const char *from ATTRIBUTE_UNUSED
)
625 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
629 path
= make_relative_prefix (program_name
, from
, TARGET_SYSTEM_ROOT
);
632 if (stat (path
, &s
) == 0 && S_ISDIR (s
.st_mode
))
640 /* Return the sysroot directory. Return "" if no sysroot is being used. */
643 get_sysroot (int argc
, char **argv
)
646 const char *path
= NULL
;
648 for (i
= 1; i
< argc
; i
++)
649 if (startswith (argv
[i
], "--sysroot="))
650 path
= argv
[i
] + strlen ("--sysroot=");
653 path
= get_relative_sysroot (BINDIR
);
656 path
= get_relative_sysroot (TOOLBINDIR
);
659 path
= TARGET_SYSTEM_ROOT
;
661 if (IS_DIR_SEPARATOR (*path
) && path
[1] == 0)
667 /* We need to find any explicitly given emulation in order to initialize the
668 state that's needed by the lex&yacc argument parser (parse_args). */
671 get_emulation (int argc
, char **argv
)
676 emulation
= getenv (EMULATION_ENVIRON
);
677 if (emulation
== NULL
)
678 emulation
= DEFAULT_EMULATION
;
680 for (i
= 1; i
< argc
; i
++)
682 if (startswith (argv
[i
], "-m"))
684 if (argv
[i
][2] == '\0')
689 emulation
= argv
[i
+ 1];
693 einfo (_("%F%P: missing argument to -m\n"));
695 else if (strcmp (argv
[i
], "-mips1") == 0
696 || strcmp (argv
[i
], "-mips2") == 0
697 || strcmp (argv
[i
], "-mips3") == 0
698 || strcmp (argv
[i
], "-mips4") == 0
699 || strcmp (argv
[i
], "-mips5") == 0
700 || strcmp (argv
[i
], "-mips32") == 0
701 || strcmp (argv
[i
], "-mips32r2") == 0
702 || strcmp (argv
[i
], "-mips32r3") == 0
703 || strcmp (argv
[i
], "-mips32r5") == 0
704 || strcmp (argv
[i
], "-mips32r6") == 0
705 || strcmp (argv
[i
], "-mips64") == 0
706 || strcmp (argv
[i
], "-mips64r2") == 0
707 || strcmp (argv
[i
], "-mips64r3") == 0
708 || strcmp (argv
[i
], "-mips64r5") == 0
709 || strcmp (argv
[i
], "-mips64r6") == 0)
711 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
712 passed to the linker by some MIPS compilers. They
713 generally tell the linker to use a slightly different
714 library path. Perhaps someday these should be
715 implemented as emulations; until then, we just ignore
716 the arguments and hope that nobody ever creates
717 emulations named ips1, ips2 or ips3. */
719 else if (strcmp (argv
[i
], "-m486") == 0)
721 /* FIXME: The argument -m486 is passed to the linker on
722 some Linux systems. Hope that nobody creates an
723 emulation named 486. */
728 emulation
= &argv
[i
][2];
737 add_ysym (const char *name
)
739 if (link_info
.notice_hash
== NULL
)
741 link_info
.notice_hash
742 = (struct bfd_hash_table
*) xmalloc (sizeof (struct bfd_hash_table
));
743 if (!bfd_hash_table_init_n (link_info
.notice_hash
,
745 sizeof (struct bfd_hash_entry
),
747 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
750 if (bfd_hash_lookup (link_info
.notice_hash
, name
, true, true) == NULL
)
751 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
755 add_ignoresym (struct bfd_link_info
*info
, const char *name
)
757 if (info
->ignore_hash
== NULL
)
759 info
->ignore_hash
= xmalloc (sizeof (struct bfd_hash_table
));
760 if (!bfd_hash_table_init_n (info
->ignore_hash
,
762 sizeof (struct bfd_hash_entry
),
764 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
767 if (bfd_hash_lookup (info
->ignore_hash
, name
, true, true) == NULL
)
768 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
771 /* Record a symbol to be wrapped, from the --wrap option. */
774 add_wrap (const char *name
)
776 if (link_info
.wrap_hash
== NULL
)
779 = (struct bfd_hash_table
*) xmalloc (sizeof (struct bfd_hash_table
));
780 if (!bfd_hash_table_init_n (link_info
.wrap_hash
,
782 sizeof (struct bfd_hash_entry
),
784 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
787 if (bfd_hash_lookup (link_info
.wrap_hash
, name
, true, true) == NULL
)
788 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
791 /* Handle the -retain-symbols-file option. */
794 add_keepsyms_file (const char *filename
)
801 if (link_info
.strip
== strip_some
)
802 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
804 file
= fopen (filename
, "r");
807 bfd_set_error (bfd_error_system_call
);
808 einfo ("%X%P: %s: %E\n", filename
);
812 link_info
.keep_hash
= (struct bfd_hash_table
*)
813 xmalloc (sizeof (struct bfd_hash_table
));
814 if (!bfd_hash_table_init (link_info
.keep_hash
, bfd_hash_newfunc
,
815 sizeof (struct bfd_hash_entry
)))
816 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
819 buf
= (char *) xmalloc (bufsize
);
831 while (!ISSPACE (c
) && c
!= EOF
)
838 buf
= (char *) xrealloc (buf
, bufsize
);
845 if (bfd_hash_lookup (link_info
.keep_hash
, buf
, true, true) == NULL
)
846 einfo (_("%F%P: bfd_hash_lookup for insertion failed: %E\n"));
850 if (link_info
.strip
!= strip_none
)
851 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
854 link_info
.strip
= strip_some
;
858 /* Callbacks from the BFD linker routines. */
860 /* This is called when BFD has decided to include an archive member in
864 add_archive_element (struct bfd_link_info
*info
,
867 bfd
**subsbfd ATTRIBUTE_UNUSED
)
869 lang_input_statement_type
*input
;
870 lang_input_statement_type
*parent
;
871 lang_input_statement_type orig_input
;
873 input
= (lang_input_statement_type
*)
874 xcalloc (1, sizeof (lang_input_statement_type
));
875 input
->header
.type
= lang_input_statement_enum
;
876 input
->filename
= bfd_get_filename (abfd
);
877 input
->local_sym_name
= bfd_get_filename (abfd
);
878 input
->the_bfd
= abfd
;
880 /* Save the original data for trace files/tries below, as plugins
881 (if enabled) may possibly alter it to point to a replacement
882 BFD, but we still want to output the original BFD filename. */
884 #if BFD_SUPPORTS_PLUGINS
885 if (link_info
.lto_plugin_active
)
887 /* We must offer this archive member to the plugins to claim. */
888 plugin_maybe_claim (input
);
889 if (input
->flags
.claimed
)
891 if (no_more_claiming
)
893 /* Don't claim new IR symbols after all IR symbols have
896 info_msg ("%pI: no new IR symbols to claim\n",
898 input
->flags
.claimed
= 0;
901 input
->flags
.claim_archive
= true;
902 *subsbfd
= input
->the_bfd
;
905 #endif /* BFD_SUPPORTS_PLUGINS */
907 if (link_info
.input_bfds_tail
== &input
->the_bfd
->link
.next
908 || input
->the_bfd
->link
.next
!= NULL
)
910 /* We have already loaded this element, and are attempting to
911 load it again. This can happen when the archive map doesn't
912 match actual symbols defined by the element. */
914 bfd_set_error (bfd_error_malformed_archive
);
918 /* Set the file_chain pointer of archives to the last element loaded
919 from the archive. See ldlang.c:find_rescan_insertion. */
920 parent
= bfd_usrdata (abfd
->my_archive
);
921 if (parent
!= NULL
&& !parent
->flags
.reload
)
922 parent
->next
= input
;
924 ldlang_add_file (input
);
926 if (config
.map_file
!= NULL
)
928 static bool header_printed
;
929 struct bfd_link_hash_entry
*h
;
933 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
935 && info
->pei386_auto_import
936 && startswith (name
, "__imp_"))
937 h
= bfd_link_hash_lookup (info
->hash
, name
+ 6, false, false, true);
949 case bfd_link_hash_defined
:
950 case bfd_link_hash_defweak
:
951 from
= h
->u
.def
.section
->owner
;
954 case bfd_link_hash_undefined
:
955 case bfd_link_hash_undefweak
:
956 from
= h
->u
.undef
.abfd
;
959 case bfd_link_hash_common
:
960 from
= h
->u
.c
.p
->section
->owner
;
967 minfo (_("Archive member included to satisfy reference by file (symbol)\n\n"));
968 header_printed
= true;
971 if (abfd
->my_archive
== NULL
972 || bfd_is_thin_archive (abfd
->my_archive
))
974 minfo ("%s", bfd_get_filename (abfd
));
975 len
= strlen (bfd_get_filename (abfd
));
979 minfo ("%s(%s)", bfd_get_filename (abfd
->my_archive
),
980 bfd_get_filename (abfd
));
981 len
= (strlen (bfd_get_filename (abfd
->my_archive
))
982 + strlen (bfd_get_filename (abfd
))
998 minfo ("%pB ", from
);
1000 minfo ("(%pT)\n", h
->root
.string
);
1002 minfo ("(%s)\n", name
);
1007 || (trace_files
&& bfd_is_thin_archive (orig_input
.the_bfd
->my_archive
)))
1008 info_msg ("%pI\n", &orig_input
);
1012 /* This is called when BFD has discovered a symbol which is defined
1016 multiple_definition (struct bfd_link_info
*info
,
1017 struct bfd_link_hash_entry
*h
,
1027 if (info
->allow_multiple_definition
)
1032 case bfd_link_hash_defined
:
1033 osec
= h
->u
.def
.section
;
1034 oval
= h
->u
.def
.value
;
1035 obfd
= h
->u
.def
.section
->owner
;
1037 case bfd_link_hash_indirect
:
1038 osec
= bfd_ind_section_ptr
;
1046 /* Ignore a redefinition of an absolute symbol to the
1047 same value; it's harmless. */
1048 if (h
->type
== bfd_link_hash_defined
1049 && bfd_is_abs_section (osec
)
1050 && bfd_is_abs_section (nsec
)
1054 /* If either section has the output_section field set to
1055 bfd_abs_section_ptr, it means that the section is being
1056 discarded, and this is not really a multiple definition at all.
1057 FIXME: It would be cleaner to somehow ignore symbols defined in
1058 sections which are being discarded. */
1059 if (!info
->prohibit_multiple_definition_absolute
1060 && ((osec
->output_section
!= NULL
1061 && ! bfd_is_abs_section (osec
)
1062 && bfd_is_abs_section (osec
->output_section
))
1063 || (nsec
->output_section
!= NULL
1064 && !bfd_is_abs_section (nsec
)
1065 && bfd_is_abs_section (nsec
->output_section
))))
1068 name
= h
->root
.string
;
1076 if (info
->warn_multiple_definition
)
1077 einfo (_("%P: %C: warning: multiple definition of `%pT'"),
1078 nbfd
, nsec
, nval
, name
);
1080 einfo (_("%X%P: %C: multiple definition of `%pT'"),
1081 nbfd
, nsec
, nval
, name
);
1083 einfo (_("; %D: first defined here"), obfd
, osec
, oval
);
1086 if (RELAXATION_ENABLED_BY_USER
)
1088 einfo (_("%P: disabling relaxation; it will not work with multiple definitions\n"));
1093 /* This is called when there is a definition of a common symbol, or
1094 when a common symbol is found for a symbol that is already defined,
1095 or when two common symbols are found. We only do something if
1096 -warn-common was used. */
1099 multiple_common (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1100 struct bfd_link_hash_entry
*h
,
1102 enum bfd_link_hash_type ntype
,
1107 enum bfd_link_hash_type otype
;
1110 if (!config
.warn_common
)
1113 name
= h
->root
.string
;
1115 if (otype
== bfd_link_hash_common
)
1117 obfd
= h
->u
.c
.p
->section
->owner
;
1118 osize
= h
->u
.c
.size
;
1120 else if (otype
== bfd_link_hash_defined
1121 || otype
== bfd_link_hash_defweak
)
1123 obfd
= h
->u
.def
.section
->owner
;
1128 /* FIXME: It would nice if we could report the BFD which defined
1129 an indirect symbol, but we don't have anywhere to store the
1135 if (ntype
== bfd_link_hash_defined
1136 || ntype
== bfd_link_hash_defweak
1137 || ntype
== bfd_link_hash_indirect
)
1139 ASSERT (otype
== bfd_link_hash_common
);
1141 einfo (_("%P: %pB: warning: definition of `%pT' overriding common"
1145 einfo (_("%P: %pB: warning: definition of `%pT' overriding common\n"),
1148 else if (otype
== bfd_link_hash_defined
1149 || otype
== bfd_link_hash_defweak
1150 || otype
== bfd_link_hash_indirect
)
1152 ASSERT (ntype
== bfd_link_hash_common
);
1154 einfo (_("%P: %pB: warning: common of `%pT' overridden by definition"
1158 einfo (_("%P: %pB: warning: common of `%pT' overridden by definition\n"),
1163 ASSERT (otype
== bfd_link_hash_common
&& ntype
== bfd_link_hash_common
);
1167 einfo (_("%P: %pB: warning: common of `%pT' overridden"
1168 " by larger common from %pB\n"),
1171 einfo (_("%P: %pB: warning: common of `%pT' overridden"
1172 " by larger common\n"),
1175 else if (nsize
> osize
)
1178 einfo (_("%P: %pB: warning: common of `%pT' overriding"
1179 " smaller common from %pB\n"),
1182 einfo (_("%P: %pB: warning: common of `%pT' overriding"
1183 " smaller common\n"),
1189 einfo (_("%P: %pB and %pB: warning: multiple common of `%pT'\n"),
1192 einfo (_("%P: %pB: warning: multiple common of `%pT'\n"),
1198 /* This is called when BFD has discovered a set element. H is the
1199 entry in the linker hash table for the set. SECTION and VALUE
1200 represent a value which should be added to the set. */
1203 add_to_set (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1204 struct bfd_link_hash_entry
*h
,
1205 bfd_reloc_code_real_type reloc
,
1210 if (config
.warn_constructors
)
1211 einfo (_("%P: warning: global constructor %s used\n"),
1214 if (!config
.build_constructors
)
1217 ldctor_add_set_entry (h
, reloc
, NULL
, section
, value
);
1219 if (h
->type
== bfd_link_hash_new
)
1221 h
->type
= bfd_link_hash_undefined
;
1222 h
->u
.undef
.abfd
= abfd
;
1223 /* We don't call bfd_link_add_undef to add this to the list of
1224 undefined symbols because we are going to define it
1229 /* This is called when BFD has discovered a constructor. This is only
1230 called for some object file formats--those which do not handle
1231 constructors in some more clever fashion. This is similar to
1232 adding an element to a set, but less general. */
1235 constructor_callback (struct bfd_link_info
*info
,
1243 struct bfd_link_hash_entry
*h
;
1244 char set_name
[1 + sizeof "__CTOR_LIST__"];
1246 if (config
.warn_constructors
)
1247 einfo (_("%P: warning: global constructor %s used\n"), name
);
1249 if (!config
.build_constructors
)
1252 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1253 useful error message. */
1254 if (bfd_reloc_type_lookup (info
->output_bfd
, BFD_RELOC_CTOR
) == NULL
1255 && (bfd_link_relocatable (info
)
1256 || bfd_reloc_type_lookup (abfd
, BFD_RELOC_CTOR
) == NULL
))
1257 einfo (_("%F%P: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1260 if (bfd_get_symbol_leading_char (abfd
) != '\0')
1261 *s
++ = bfd_get_symbol_leading_char (abfd
);
1263 strcpy (s
, "__CTOR_LIST__");
1265 strcpy (s
, "__DTOR_LIST__");
1267 h
= bfd_link_hash_lookup (info
->hash
, set_name
, true, true, true);
1268 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1269 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1270 if (h
->type
== bfd_link_hash_new
)
1272 h
->type
= bfd_link_hash_undefined
;
1273 h
->u
.undef
.abfd
= abfd
;
1274 /* We don't call bfd_link_add_undef to add this to the list of
1275 undefined symbols because we are going to define it
1279 ldctor_add_set_entry (h
, BFD_RELOC_CTOR
, name
, section
, value
);
1282 /* A structure used by warning_callback to pass information through
1283 bfd_map_over_sections. */
1285 struct warning_callback_info
1288 const char *warning
;
1293 /* Look through the relocs to see if we can find a plausible address
1294 for SYMBOL in ABFD. Return TRUE if found. Otherwise return FALSE. */
1297 symbol_warning (const char *warning
, const char *symbol
, bfd
*abfd
)
1299 struct warning_callback_info cinfo
;
1301 if (!bfd_generic_link_read_symbols (abfd
))
1302 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd
);
1304 cinfo
.found
= false;
1305 cinfo
.warning
= warning
;
1306 cinfo
.symbol
= symbol
;
1307 cinfo
.asymbols
= bfd_get_outsymbols (abfd
);
1308 bfd_map_over_sections (abfd
, warning_find_reloc
, &cinfo
);
1312 /* This is called when there is a reference to a warning symbol. */
1315 warning_callback (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1316 const char *warning
,
1322 /* This is a hack to support warn_multiple_gp. FIXME: This should
1323 have a cleaner interface, but what? */
1324 if (!config
.warn_multiple_gp
1325 && strcmp (warning
, "using multiple gp values") == 0)
1328 if (section
!= NULL
)
1329 einfo ("%P: %C: %s%s\n", abfd
, section
, address
, _("warning: "), warning
);
1330 else if (abfd
== NULL
)
1331 einfo ("%P: %s%s\n", _("warning: "), warning
);
1332 else if (symbol
== NULL
)
1333 einfo ("%P: %pB: %s%s\n", abfd
, _("warning: "), warning
);
1334 else if (!symbol_warning (warning
, symbol
, abfd
))
1337 /* Search all input files for a reference to SYMBOL. */
1338 for (b
= info
->input_bfds
; b
; b
= b
->link
.next
)
1339 if (b
!= abfd
&& symbol_warning (warning
, symbol
, b
))
1341 einfo ("%P: %pB: %s%s\n", abfd
, _("warning: "), warning
);
1345 /* This is called by warning_callback for each section. It checks the
1346 relocs of the section to see if it can find a reference to the
1347 symbol which triggered the warning. If it can, it uses the reloc
1348 to give an error message with a file and line number. */
1351 warning_find_reloc (bfd
*abfd
, asection
*sec
, void *iarg
)
1353 struct warning_callback_info
*info
= (struct warning_callback_info
*) iarg
;
1357 arelent
**p
, **pend
;
1362 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1364 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd
);
1368 relpp
= (arelent
**) xmalloc (relsize
);
1369 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, info
->asymbols
);
1371 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd
);
1374 pend
= p
+ relcount
;
1375 for (; p
< pend
&& *p
!= NULL
; p
++)
1379 if (q
->sym_ptr_ptr
!= NULL
1380 && *q
->sym_ptr_ptr
!= NULL
1381 && strcmp (bfd_asymbol_name (*q
->sym_ptr_ptr
), info
->symbol
) == 0)
1383 /* We found a reloc for the symbol we are looking for. */
1384 einfo ("%P: %C: %s%s\n", abfd
, sec
, q
->address
, _("warning: "),
1394 #if SUPPORT_ERROR_HANDLING_SCRIPT
1395 char * error_handling_script
= NULL
;
1398 /* This is called when an undefined symbol is found. */
1401 undefined_symbol (struct bfd_link_info
*info
,
1408 static char *error_name
;
1409 static unsigned int error_count
;
1411 #define MAX_ERRORS_IN_A_ROW 5
1413 if (info
->ignore_hash
!= NULL
1414 && bfd_hash_lookup (info
->ignore_hash
, name
, false, false) != NULL
)
1417 if (config
.warn_once
)
1419 /* Only warn once about a particular undefined symbol. */
1420 add_ignoresym (info
, name
);
1423 /* We never print more than a reasonable number of errors in a row
1424 for a single symbol. */
1425 if (error_name
!= NULL
1426 && strcmp (name
, error_name
) == 0)
1432 error_name
= xstrdup (name
);
1435 #if SUPPORT_ERROR_HANDLING_SCRIPT
1436 if (error_handling_script
!= NULL
1437 && error_count
< MAX_ERRORS_IN_A_ROW
)
1443 argv
[0] = error_handling_script
;
1444 argv
[1] = "undefined-symbol";
1445 argv
[2] = (char *) name
;
1449 einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
1450 argv
[0], argv
[1], argv
[2]);
1452 res
= pex_one (PEX_SEARCH
, error_handling_script
, argv
,
1453 N_("error handling script"),
1454 NULL
/* Send stdout to random, temp file. */,
1455 NULL
/* Write to stderr. */,
1459 einfo (_("%P: Failed to run error handling script '%s', reason: "),
1460 error_handling_script
);
1461 /* FIXME: We assume here that errrno == err. */
1464 /* We ignore the return status of the script and
1465 carry on to issue the normal error message. */
1467 #endif /* SUPPORT_ERROR_HANDLING_SCRIPT */
1469 if (section
!= NULL
)
1471 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1474 einfo (_("%X%P: %C: undefined reference to `%pT'\n"),
1475 abfd
, section
, address
, name
);
1477 einfo (_("%P: %C: warning: undefined reference to `%pT'\n"),
1478 abfd
, section
, address
, name
);
1480 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1483 einfo (_("%X%P: %D: more undefined references to `%pT' follow\n"),
1484 abfd
, section
, address
, name
);
1486 einfo (_("%P: %D: warning: more undefined references to `%pT' follow\n"),
1487 abfd
, section
, address
, name
);
1494 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1497 einfo (_("%X%P: %pB: undefined reference to `%pT'\n"),
1500 einfo (_("%P: %pB: warning: undefined reference to `%pT'\n"),
1503 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1506 einfo (_("%X%P: %pB: more undefined references to `%pT' follow\n"),
1509 einfo (_("%P: %pB: warning: more undefined references to `%pT' follow\n"),
1517 /* Counter to limit the number of relocation overflow error messages
1518 to print. Errors are printed as it is decremented. When it's
1519 called and the counter is zero, a final message is printed
1520 indicating more relocations were omitted. When it gets to -1, no
1521 such errors are printed. If it's initially set to a value less
1522 than -1, all such errors will be printed (--verbose does this). */
1524 int overflow_cutoff_limit
= 10;
1526 /* This is called when a reloc overflows. */
1529 reloc_overflow (struct bfd_link_info
*info
,
1530 struct bfd_link_hash_entry
*entry
,
1532 const char *reloc_name
,
1538 if (overflow_cutoff_limit
== -1)
1541 einfo ("%X%H:", abfd
, section
, address
);
1543 if (overflow_cutoff_limit
>= 0
1544 && overflow_cutoff_limit
-- == 0)
1546 einfo (_(" additional relocation overflows omitted from the output\n"));
1552 while (entry
->type
== bfd_link_hash_indirect
1553 || entry
->type
== bfd_link_hash_warning
)
1554 entry
= entry
->u
.i
.link
;
1555 switch (entry
->type
)
1557 case bfd_link_hash_undefined
:
1558 case bfd_link_hash_undefweak
:
1559 einfo (_(" relocation truncated to fit: "
1560 "%s against undefined symbol `%pT'"),
1561 reloc_name
, entry
->root
.string
);
1563 case bfd_link_hash_defined
:
1564 case bfd_link_hash_defweak
:
1565 einfo (_(" relocation truncated to fit: "
1566 "%s against symbol `%pT' defined in %pA section in %pB"),
1567 reloc_name
, entry
->root
.string
,
1568 entry
->u
.def
.section
,
1569 entry
->u
.def
.section
== bfd_abs_section_ptr
1570 ? info
->output_bfd
: entry
->u
.def
.section
->owner
);
1578 einfo (_(" relocation truncated to fit: %s against `%pT'"),
1581 einfo ("+%v", addend
);
1585 /* This is called when a dangerous relocation is made. */
1588 reloc_dangerous (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1589 const char *message
,
1594 einfo (_("%X%H: dangerous relocation: %s\n"),
1595 abfd
, section
, address
, message
);
1598 /* This is called when a reloc is being generated attached to a symbol
1599 that is not being output. */
1602 unattached_reloc (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1608 einfo (_("%X%H: reloc refers to symbol `%pT' which is not being output\n"),
1609 abfd
, section
, address
, name
);
1612 /* This is called if link_info.notice_all is set, or when a symbol in
1613 link_info.notice_hash is found. Symbols are put in notice_hash
1614 using the -y option, while notice_all is set if the --cref option
1615 has been supplied, or if there are any NOCROSSREFS sections in the
1616 linker script; and if plugins are active, since they need to monitor
1617 all references from non-IR files. */
1620 notice (struct bfd_link_info
*info
,
1621 struct bfd_link_hash_entry
*h
,
1622 struct bfd_link_hash_entry
*inh ATTRIBUTE_UNUSED
,
1626 flagword flags ATTRIBUTE_UNUSED
)
1632 if (command_line
.cref
|| nocrossref_list
!= NULL
)
1633 return handle_asneeded_cref (abfd
, (enum notice_asneeded_action
) value
);
1637 name
= h
->root
.string
;
1638 if (info
->notice_hash
!= NULL
1639 && bfd_hash_lookup (info
->notice_hash
, name
, false, false) != NULL
)
1641 if (bfd_is_und_section (section
))
1642 einfo (_("%P: %pB: reference to %s\n"), abfd
, name
);
1644 einfo (_("%P: %pB: definition of %s\n"), abfd
, name
);
1647 if (command_line
.cref
|| nocrossref_list
!= NULL
)
1648 add_cref (name
, abfd
, section
, value
);