1 /* Main program of GNU linker.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain steve@cygnus.com
7 This file is part of the GNU Binutils.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
26 #include "safe-ctype.h"
27 #include "libiberty.h"
30 #include "filenames.h"
45 #include "plugin-api.h"
47 #endif /* ENABLE_PLUGINS */
49 /* Somewhere above, sys/stat.h got included. */
50 #if !defined(S_ISDIR) && defined(S_IFDIR)
51 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
62 #ifndef TARGET_SYSTEM_ROOT
63 #define TARGET_SYSTEM_ROOT ""
68 FILE *saved_script_handle
= NULL
;
69 FILE *previous_script_handle
= NULL
;
70 bfd_boolean force_make_executable
= FALSE
;
73 const char *output_filename
= "a.out";
75 /* Name this program was invoked by. */
78 /* The prefix for system library directories. */
79 const char *ld_sysroot
;
81 /* The canonical representation of ld_sysroot. */
82 char * ld_canon_sysroot
;
83 int ld_canon_sysroot_len
;
85 /* Set by -G argument, for MIPS ECOFF target. */
86 int g_switch_value
= 8;
88 /* Nonzero means print names of input files as processed. */
89 bfd_boolean trace_files
;
91 /* Nonzero means same, but note open failures, too. */
92 bfd_boolean trace_file_tries
;
94 /* Nonzero means version number was printed, so exit successfully
95 instead of complaining if no input files are given. */
96 bfd_boolean version_printed
;
98 /* TRUE if we should demangle symbol names. */
99 bfd_boolean demangling
;
101 args_type command_line
;
103 ld_config_type config
;
105 sort_type sort_section
;
107 static const char *get_sysroot
109 static char *get_emulation
111 static bfd_boolean add_archive_element
112 (struct bfd_link_info
*, bfd
*, const char *, bfd
**);
113 static bfd_boolean multiple_definition
114 (struct bfd_link_info
*, struct bfd_link_hash_entry
*,
115 bfd
*, asection
*, bfd_vma
);
116 static bfd_boolean multiple_common
117 (struct bfd_link_info
*, struct bfd_link_hash_entry
*,
118 bfd
*, enum bfd_link_hash_type
, bfd_vma
);
119 static bfd_boolean add_to_set
120 (struct bfd_link_info
*, struct bfd_link_hash_entry
*,
121 bfd_reloc_code_real_type
, bfd
*, asection
*, bfd_vma
);
122 static bfd_boolean constructor_callback
123 (struct bfd_link_info
*, bfd_boolean
, const char *, bfd
*,
124 asection
*, bfd_vma
);
125 static bfd_boolean warning_callback
126 (struct bfd_link_info
*, const char *, const char *, bfd
*,
127 asection
*, bfd_vma
);
128 static void warning_find_reloc
129 (bfd
*, asection
*, void *);
130 static bfd_boolean undefined_symbol
131 (struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
,
133 static bfd_boolean reloc_overflow
134 (struct bfd_link_info
*, struct bfd_link_hash_entry
*, const char *,
135 const char *, bfd_vma
, bfd
*, asection
*, bfd_vma
);
136 static bfd_boolean reloc_dangerous
137 (struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
);
138 static bfd_boolean unattached_reloc
139 (struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
);
140 static bfd_boolean notice
141 (struct bfd_link_info
*, struct bfd_link_hash_entry
*,
142 bfd
*, asection
*, bfd_vma
, flagword
, const char *);
144 static struct bfd_link_callbacks link_callbacks
=
150 constructor_callback
,
160 ldlang_override_segment_assignment
163 struct bfd_link_info link_info
;
168 bfd_cache_close_all ();
169 #ifdef ENABLE_PLUGINS
170 plugin_call_cleanup ();
172 if (output_filename
&& delete_output_file_on_failure
)
173 unlink_if_ordinary (output_filename
);
177 main (int argc
, char **argv
)
180 long start_time
= get_run_time ();
182 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
183 setlocale (LC_MESSAGES
, "");
185 #if defined (HAVE_SETLOCALE)
186 setlocale (LC_CTYPE
, "");
188 bindtextdomain (PACKAGE
, LOCALEDIR
);
189 textdomain (PACKAGE
);
191 program_name
= argv
[0];
192 xmalloc_set_program_name (program_name
);
194 START_PROGRESS (program_name
, 0);
196 expandargv (&argc
, &argv
);
200 bfd_set_error_program_name (program_name
);
202 xatexit (ld_cleanup
);
204 /* Set up the sysroot directory. */
205 ld_sysroot
= get_sysroot (argc
, argv
);
208 if (*TARGET_SYSTEM_ROOT
== 0)
210 einfo ("%P%F: this linker was not configured to use sysroots\n");
214 ld_canon_sysroot
= lrealpath (ld_sysroot
);
216 if (ld_canon_sysroot
)
217 ld_canon_sysroot_len
= strlen (ld_canon_sysroot
);
219 ld_canon_sysroot_len
= -1;
221 /* Set the default BFD target based on the configured target. Doing
222 this permits the linker to be configured for a particular target,
223 and linked against a shared BFD library which was configured for
224 a different target. The macro TARGET is defined by Makefile. */
225 if (! bfd_set_default_target (TARGET
))
227 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET
);
238 config
.build_constructors
= TRUE
;
239 config
.rpath_separator
= ':';
240 config
.split_by_reloc
= (unsigned) -1;
241 config
.split_by_file
= (bfd_size_type
) -1;
242 config
.make_executable
= TRUE
;
243 config
.magic_demand_paged
= TRUE
;
244 config
.text_read_only
= TRUE
;
246 command_line
.warn_mismatch
= TRUE
;
247 command_line
.warn_search_mismatch
= TRUE
;
248 command_line
.check_section_addresses
= -1;
249 command_line
.disable_target_specific_optimizations
= -1;
251 /* We initialize DEMANGLING based on the environment variable
252 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
253 output of the linker, unless COLLECT_NO_DEMANGLE is set in the
254 environment. Acting the same way here lets us provide the same
255 interface by default. */
256 demangling
= getenv ("COLLECT_NO_DEMANGLE") == NULL
;
258 link_info
.allow_undefined_version
= TRUE
;
259 link_info
.keep_memory
= TRUE
;
260 link_info
.combreloc
= TRUE
;
261 link_info
.strip_discarded
= TRUE
;
262 link_info
.emit_hash
= TRUE
;
263 link_info
.callbacks
= &link_callbacks
;
264 link_info
.input_bfds_tail
= &link_info
.input_bfds
;
265 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
266 and _fini symbols. We are compatible. */
267 link_info
.init_function
= "_init";
268 link_info
.fini_function
= "_fini";
269 link_info
.relax_pass
= 1;
270 link_info
.pei386_auto_import
= -1;
271 link_info
.spare_dynamic_tags
= 5;
272 link_info
.path_separator
= ':';
274 ldfile_add_arch ("");
275 emulation
= get_emulation (argc
, argv
);
276 ldemul_choose_mode (emulation
);
277 default_target
= ldemul_choose_target (argc
, argv
);
278 config
.maxpagesize
= bfd_emul_get_maxpagesize (default_target
);
279 config
.commonpagesize
= bfd_emul_get_commonpagesize (default_target
);
281 ldemul_before_parse ();
282 lang_has_input_file
= FALSE
;
283 parse_args (argc
, argv
);
285 if (config
.hash_table_size
!= 0)
286 bfd_hash_set_default_size (config
.hash_table_size
);
288 #ifdef ENABLE_PLUGINS
289 /* Now all the plugin arguments have been gathered, we can load them. */
290 if (plugin_load_plugins ())
291 einfo (_("%P%F: %s: error loading plugin\n"), plugin_error_plugin ());
292 #endif /* ENABLE_PLUGINS */
294 ldemul_set_symbols ();
296 /* If we have not already opened and parsed a linker script,
297 try the default script from command line first. */
298 if (saved_script_handle
== NULL
299 && command_line
.default_script
!= NULL
)
301 ldfile_open_command_file (command_line
.default_script
);
302 parser_input
= input_script
;
306 /* If we have not already opened and parsed a linker script
307 read the emulation's appropriate default script. */
308 if (saved_script_handle
== NULL
)
311 char *s
= ldemul_get_script (&isfile
);
314 ldfile_open_default_command_file (s
);
318 lex_redirect (s
, _("built in linker script"), 1);
320 parser_input
= input_script
;
325 if (trace_file_tries
)
327 if (saved_script_handle
)
328 info_msg (_("using external linker script:"));
330 info_msg (_("using internal linker script:"));
331 info_msg ("\n==================================================\n");
333 if (saved_script_handle
)
335 static const int ld_bufsz
= 8193;
337 char *buf
= (char *) xmalloc (ld_bufsz
);
339 rewind (saved_script_handle
);
340 while ((n
= fread (buf
, 1, ld_bufsz
- 1, saved_script_handle
)) > 0)
345 rewind (saved_script_handle
);
352 info_msg (ldemul_get_script (&isfile
));
355 info_msg ("\n==================================================\n");
358 if (command_line
.print_output_format
)
359 info_msg ("%s\n", lang_get_output_target ());
363 if (!lang_has_input_file
)
365 if (version_printed
|| command_line
.print_output_format
)
367 einfo (_("%P%F: no input files\n"));
371 info_msg (_("%P: mode %s\n"), emulation
);
373 ldemul_after_parse ();
375 if (config
.map_filename
)
377 if (strcmp (config
.map_filename
, "-") == 0)
379 config
.map_file
= stdout
;
383 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
384 if (config
.map_file
== (FILE *) NULL
)
386 bfd_set_error (bfd_error_system_call
);
387 einfo (_("%P%F: cannot open map file %s: %E\n"),
388 config
.map_filename
);
395 /* Print error messages for any missing symbols, for any warning
396 symbols, and possibly multiple definitions. */
397 if (link_info
.relocatable
)
398 link_info
.output_bfd
->flags
&= ~EXEC_P
;
400 link_info
.output_bfd
->flags
|= EXEC_P
;
404 if (config
.map_file
!= NULL
)
406 if (command_line
.cref
)
407 output_cref (config
.map_file
!= NULL
? config
.map_file
: stdout
);
408 if (nocrossref_list
!= NULL
)
409 check_nocrossrefs ();
413 /* Even if we're producing relocatable output, some non-fatal errors should
414 be reported in the exit status. (What non-fatal errors, if any, do we
415 want to ignore for relocatable output?) */
416 if (!config
.make_executable
&& !force_make_executable
)
419 einfo (_("%P: link errors found, deleting executable `%s'\n"),
422 /* The file will be removed by ld_cleanup. */
427 if (! bfd_close (link_info
.output_bfd
))
428 einfo (_("%F%B: final close failed: %E\n"), link_info
.output_bfd
);
430 /* If the --force-exe-suffix is enabled, and we're making an
431 executable file and it doesn't end in .exe, copy it to one
433 if (! link_info
.relocatable
&& command_line
.force_exe_suffix
)
435 int len
= strlen (output_filename
);
438 || (strcasecmp (output_filename
+ len
- 4, ".exe") != 0
439 && strcasecmp (output_filename
+ len
- 4, ".dll") != 0))
443 const int bsize
= 4096;
444 char *buf
= (char *) xmalloc (bsize
);
446 char *dst_name
= (char *) xmalloc (len
+ 5);
448 strcpy (dst_name
, output_filename
);
449 strcat (dst_name
, ".exe");
450 src
= fopen (output_filename
, FOPEN_RB
);
451 dst
= fopen (dst_name
, FOPEN_WB
);
454 einfo (_("%X%P: unable to open for source of copy `%s'\n"),
457 einfo (_("%X%P: unable to open for destination of copy `%s'\n"),
459 while ((l
= fread (buf
, 1, bsize
, src
)) > 0)
461 int done
= fwrite (buf
, 1, l
, dst
);
464 einfo (_("%P: Error writing file `%s'\n"), dst_name
);
468 if (fclose (dst
) == EOF
)
469 einfo (_("%P: Error closing file `%s'\n"), dst_name
);
476 END_PROGRESS (program_name
);
481 char *lim
= (char *) sbrk (0);
483 long run_time
= get_run_time () - start_time
;
486 fprintf (stderr
, _("%s: total time in link: %ld.%06ld\n"),
487 program_name
, run_time
/ 1000000, run_time
% 1000000);
489 fprintf (stderr
, _("%s: data size %ld\n"), program_name
,
490 (long) (lim
- (char *) &environ
));
495 /* Prevent ld_cleanup from doing anything, after a successful link. */
496 output_filename
= NULL
;
502 /* If the configured sysroot is relocatable, try relocating it based on
503 default prefix FROM. Return the relocated directory if it exists,
504 otherwise return null. */
507 get_relative_sysroot (const char *from ATTRIBUTE_UNUSED
)
509 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
513 path
= make_relative_prefix (program_name
, from
, TARGET_SYSTEM_ROOT
);
516 if (stat (path
, &s
) == 0 && S_ISDIR (s
.st_mode
))
524 /* Return the sysroot directory. Return "" if no sysroot is being used. */
527 get_sysroot (int argc
, char **argv
)
532 for (i
= 1; i
< argc
; i
++)
533 if (CONST_STRNEQ (argv
[i
], "--sysroot="))
534 return argv
[i
] + strlen ("--sysroot=");
536 path
= get_relative_sysroot (BINDIR
);
540 path
= get_relative_sysroot (TOOLBINDIR
);
544 return TARGET_SYSTEM_ROOT
;
547 /* We need to find any explicitly given emulation in order to initialize the
548 state that's needed by the lex&yacc argument parser (parse_args). */
551 get_emulation (int argc
, char **argv
)
556 emulation
= getenv (EMULATION_ENVIRON
);
557 if (emulation
== NULL
)
558 emulation
= DEFAULT_EMULATION
;
560 for (i
= 1; i
< argc
; i
++)
562 if (CONST_STRNEQ (argv
[i
], "-m"))
564 if (argv
[i
][2] == '\0')
569 emulation
= argv
[i
+ 1];
573 einfo (_("%P%F: missing argument to -m\n"));
575 else if (strcmp (argv
[i
], "-mips1") == 0
576 || strcmp (argv
[i
], "-mips2") == 0
577 || strcmp (argv
[i
], "-mips3") == 0
578 || strcmp (argv
[i
], "-mips4") == 0
579 || strcmp (argv
[i
], "-mips5") == 0
580 || strcmp (argv
[i
], "-mips32") == 0
581 || strcmp (argv
[i
], "-mips32r2") == 0
582 || strcmp (argv
[i
], "-mips64") == 0
583 || strcmp (argv
[i
], "-mips64r2") == 0)
585 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
586 passed to the linker by some MIPS compilers. They
587 generally tell the linker to use a slightly different
588 library path. Perhaps someday these should be
589 implemented as emulations; until then, we just ignore
590 the arguments and hope that nobody ever creates
591 emulations named ips1, ips2 or ips3. */
593 else if (strcmp (argv
[i
], "-m486") == 0)
595 /* FIXME: The argument -m486 is passed to the linker on
596 some Linux systems. Hope that nobody creates an
597 emulation named 486. */
602 emulation
= &argv
[i
][2];
611 add_ysym (const char *name
)
613 if (link_info
.notice_hash
== NULL
)
615 link_info
.notice_hash
=
616 (struct bfd_hash_table
*) xmalloc (sizeof (struct bfd_hash_table
));
617 if (!bfd_hash_table_init_n (link_info
.notice_hash
,
619 sizeof (struct bfd_hash_entry
),
621 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
624 if (bfd_hash_lookup (link_info
.notice_hash
, name
, TRUE
, TRUE
) == NULL
)
625 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
628 /* Record a symbol to be wrapped, from the --wrap option. */
631 add_wrap (const char *name
)
633 if (link_info
.wrap_hash
== NULL
)
635 link_info
.wrap_hash
=
636 (struct bfd_hash_table
*) xmalloc (sizeof (struct bfd_hash_table
));
637 if (!bfd_hash_table_init_n (link_info
.wrap_hash
,
639 sizeof (struct bfd_hash_entry
),
641 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
644 if (bfd_hash_lookup (link_info
.wrap_hash
, name
, TRUE
, TRUE
) == NULL
)
645 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
648 /* Handle the -retain-symbols-file option. */
651 add_keepsyms_file (const char *filename
)
658 if (link_info
.strip
== strip_some
)
659 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
661 file
= fopen (filename
, "r");
664 bfd_set_error (bfd_error_system_call
);
665 einfo ("%X%P: %s: %E\n", filename
);
669 link_info
.keep_hash
= (struct bfd_hash_table
*)
670 xmalloc (sizeof (struct bfd_hash_table
));
671 if (!bfd_hash_table_init (link_info
.keep_hash
, bfd_hash_newfunc
,
672 sizeof (struct bfd_hash_entry
)))
673 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
676 buf
= (char *) xmalloc (bufsize
);
688 while (! ISSPACE (c
) && c
!= EOF
)
695 buf
= (char *) xrealloc (buf
, bufsize
);
702 if (bfd_hash_lookup (link_info
.keep_hash
, buf
, TRUE
, TRUE
) == NULL
)
703 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
707 if (link_info
.strip
!= strip_none
)
708 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
711 link_info
.strip
= strip_some
;
714 /* Callbacks from the BFD linker routines. */
716 /* This is called when BFD has decided to include an archive member in
720 add_archive_element (struct bfd_link_info
*info
,
723 bfd
**subsbfd ATTRIBUTE_UNUSED
)
725 lang_input_statement_type
*input
;
726 lang_input_statement_type orig_input
;
728 input
= (lang_input_statement_type
*)
729 xcalloc (1, sizeof (lang_input_statement_type
));
730 input
->filename
= abfd
->filename
;
731 input
->local_sym_name
= abfd
->filename
;
732 input
->the_bfd
= abfd
;
734 /* Save the original data for trace files/tries below, as plugins
735 (if enabled) may possibly alter it to point to a replacement
736 BFD, but we still want to output the original BFD filename. */
738 #ifdef ENABLE_PLUGINS
739 if (plugin_active_plugins_p () && !no_more_claiming
)
741 /* We must offer this archive member to the plugins to claim. */
742 const char *filename
= (bfd_my_archive (abfd
) != NULL
743 ? bfd_my_archive (abfd
)->filename
: abfd
->filename
);
744 int fd
= open (filename
, O_RDONLY
| O_BINARY
);
747 struct ld_plugin_input_file file
;
749 /* Offset and filesize must refer to the individual archive
750 member, not the whole file, and must exclude the header.
751 Fortunately for us, that is how the data is stored in the
752 origin field of the bfd and in the arelt_data. */
753 file
.name
= filename
;
754 file
.offset
= abfd
->origin
;
755 file
.filesize
= arelt_size (abfd
);
757 plugin_maybe_claim (&file
, input
);
758 if (input
->flags
.claimed
)
760 input
->flags
.claim_archive
= TRUE
;
761 *subsbfd
= input
->the_bfd
;
765 #endif /* ENABLE_PLUGINS */
767 ldlang_add_file (input
);
769 if (config
.map_file
!= NULL
)
771 static bfd_boolean header_printed
;
772 struct bfd_link_hash_entry
*h
;
776 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
788 case bfd_link_hash_defined
:
789 case bfd_link_hash_defweak
:
790 from
= h
->u
.def
.section
->owner
;
793 case bfd_link_hash_undefined
:
794 case bfd_link_hash_undefweak
:
795 from
= h
->u
.undef
.abfd
;
798 case bfd_link_hash_common
:
799 from
= h
->u
.c
.p
->section
->owner
;
804 if (! header_printed
)
808 sprintf (buf
, _("Archive member included because of file (symbol)\n\n"));
810 header_printed
= TRUE
;
813 if (bfd_my_archive (abfd
) == NULL
)
815 minfo ("%s", bfd_get_filename (abfd
));
816 len
= strlen (bfd_get_filename (abfd
));
820 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd
)),
821 bfd_get_filename (abfd
));
822 len
= (strlen (bfd_get_filename (bfd_my_archive (abfd
)))
823 + strlen (bfd_get_filename (abfd
))
841 minfo ("(%T)\n", h
->root
.string
);
843 minfo ("(%s)\n", name
);
846 if (trace_files
|| trace_file_tries
)
847 info_msg ("%I\n", &orig_input
);
851 /* This is called when BFD has discovered a symbol which is defined
855 multiple_definition (struct bfd_link_info
*info
,
856 struct bfd_link_hash_entry
*h
,
866 if (info
->allow_multiple_definition
)
871 case bfd_link_hash_defined
:
872 osec
= h
->u
.def
.section
;
873 oval
= h
->u
.def
.value
;
874 obfd
= h
->u
.def
.section
->owner
;
876 case bfd_link_hash_indirect
:
877 osec
= bfd_ind_section_ptr
;
885 /* Ignore a redefinition of an absolute symbol to the
886 same value; it's harmless. */
887 if (h
->type
== bfd_link_hash_defined
888 && bfd_is_abs_section (osec
)
889 && bfd_is_abs_section (nsec
)
893 /* If either section has the output_section field set to
894 bfd_abs_section_ptr, it means that the section is being
895 discarded, and this is not really a multiple definition at all.
896 FIXME: It would be cleaner to somehow ignore symbols defined in
897 sections which are being discarded. */
898 if ((osec
->output_section
!= NULL
899 && ! bfd_is_abs_section (osec
)
900 && bfd_is_abs_section (osec
->output_section
))
901 || (nsec
->output_section
!= NULL
902 && ! bfd_is_abs_section (nsec
)
903 && bfd_is_abs_section (nsec
->output_section
)))
906 name
= h
->root
.string
;
914 einfo (_("%X%C: multiple definition of `%T'\n"),
915 nbfd
, nsec
, nval
, name
);
917 einfo (_("%D: first defined here\n"), obfd
, osec
, oval
);
919 if (RELAXATION_ENABLED
)
921 einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
922 command_line
.disable_target_specific_optimizations
= -1;
928 /* This is called when there is a definition of a common symbol, or
929 when a common symbol is found for a symbol that is already defined,
930 or when two common symbols are found. We only do something if
931 -warn-common was used. */
934 multiple_common (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
935 struct bfd_link_hash_entry
*h
,
937 enum bfd_link_hash_type ntype
,
942 enum bfd_link_hash_type otype
;
945 if (!config
.warn_common
)
948 name
= h
->root
.string
;
950 if (otype
== bfd_link_hash_common
)
952 obfd
= h
->u
.c
.p
->section
->owner
;
955 else if (otype
== bfd_link_hash_defined
956 || otype
== bfd_link_hash_defweak
)
958 obfd
= h
->u
.def
.section
->owner
;
963 /* FIXME: It would nice if we could report the BFD which defined
964 an indirect symbol, but we don't have anywhere to store the
970 if (ntype
== bfd_link_hash_defined
971 || ntype
== bfd_link_hash_defweak
972 || ntype
== bfd_link_hash_indirect
)
974 ASSERT (otype
== bfd_link_hash_common
);
975 einfo (_("%B: warning: definition of `%T' overriding common\n"),
978 einfo (_("%B: warning: common is here\n"), obfd
);
980 else if (otype
== bfd_link_hash_defined
981 || otype
== bfd_link_hash_defweak
982 || otype
== bfd_link_hash_indirect
)
984 ASSERT (ntype
== bfd_link_hash_common
);
985 einfo (_("%B: warning: common of `%T' overridden by definition\n"),
988 einfo (_("%B: warning: defined here\n"), obfd
);
992 ASSERT (otype
== bfd_link_hash_common
&& ntype
== bfd_link_hash_common
);
995 einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
998 einfo (_("%B: warning: larger common is here\n"), obfd
);
1000 else if (nsize
> osize
)
1002 einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
1005 einfo (_("%B: warning: smaller common is here\n"), obfd
);
1009 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd
, name
);
1011 einfo (_("%B: warning: previous common is here\n"), obfd
);
1018 /* This is called when BFD has discovered a set element. H is the
1019 entry in the linker hash table for the set. SECTION and VALUE
1020 represent a value which should be added to the set. */
1023 add_to_set (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1024 struct bfd_link_hash_entry
*h
,
1025 bfd_reloc_code_real_type reloc
,
1030 if (config
.warn_constructors
)
1031 einfo (_("%P: warning: global constructor %s used\n"),
1034 if (! config
.build_constructors
)
1037 ldctor_add_set_entry (h
, reloc
, NULL
, section
, value
);
1039 if (h
->type
== bfd_link_hash_new
)
1041 h
->type
= bfd_link_hash_undefined
;
1042 h
->u
.undef
.abfd
= abfd
;
1043 /* We don't call bfd_link_add_undef to add this to the list of
1044 undefined symbols because we are going to define it
1051 /* This is called when BFD has discovered a constructor. This is only
1052 called for some object file formats--those which do not handle
1053 constructors in some more clever fashion. This is similar to
1054 adding an element to a set, but less general. */
1057 constructor_callback (struct bfd_link_info
*info
,
1058 bfd_boolean constructor
,
1065 struct bfd_link_hash_entry
*h
;
1066 char set_name
[1 + sizeof "__CTOR_LIST__"];
1068 if (config
.warn_constructors
)
1069 einfo (_("%P: warning: global constructor %s used\n"), name
);
1071 if (! config
.build_constructors
)
1074 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1075 useful error message. */
1076 if (bfd_reloc_type_lookup (link_info
.output_bfd
, BFD_RELOC_CTOR
) == NULL
1077 && (info
->relocatable
1078 || bfd_reloc_type_lookup (abfd
, BFD_RELOC_CTOR
) == NULL
))
1079 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1082 if (bfd_get_symbol_leading_char (abfd
) != '\0')
1083 *s
++ = bfd_get_symbol_leading_char (abfd
);
1085 strcpy (s
, "__CTOR_LIST__");
1087 strcpy (s
, "__DTOR_LIST__");
1089 h
= bfd_link_hash_lookup (info
->hash
, set_name
, TRUE
, TRUE
, TRUE
);
1090 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1091 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1092 if (h
->type
== bfd_link_hash_new
)
1094 h
->type
= bfd_link_hash_undefined
;
1095 h
->u
.undef
.abfd
= abfd
;
1096 /* We don't call bfd_link_add_undef to add this to the list of
1097 undefined symbols because we are going to define it
1101 ldctor_add_set_entry (h
, BFD_RELOC_CTOR
, name
, section
, value
);
1105 /* A structure used by warning_callback to pass information through
1106 bfd_map_over_sections. */
1108 struct warning_callback_info
1111 const char *warning
;
1116 /* This is called when there is a reference to a warning symbol. */
1119 warning_callback (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1120 const char *warning
,
1126 /* This is a hack to support warn_multiple_gp. FIXME: This should
1127 have a cleaner interface, but what? */
1128 if (! config
.warn_multiple_gp
1129 && strcmp (warning
, "using multiple gp values") == 0)
1132 if (section
!= NULL
)
1133 einfo ("%C: %s%s\n", abfd
, section
, address
, _("warning: "), warning
);
1134 else if (abfd
== NULL
)
1135 einfo ("%P: %s%s\n", _("warning: "), warning
);
1136 else if (symbol
== NULL
)
1137 einfo ("%B: %s%s\n", abfd
, _("warning: "), warning
);
1140 struct warning_callback_info cinfo
;
1142 /* Look through the relocs to see if we can find a plausible
1145 if (!bfd_generic_link_read_symbols (abfd
))
1146 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
1148 cinfo
.found
= FALSE
;
1149 cinfo
.warning
= warning
;
1150 cinfo
.symbol
= symbol
;
1151 cinfo
.asymbols
= bfd_get_outsymbols (abfd
);
1152 bfd_map_over_sections (abfd
, warning_find_reloc
, &cinfo
);
1155 einfo ("%B: %s%s\n", abfd
, _("warning: "), warning
);
1161 /* This is called by warning_callback for each section. It checks the
1162 relocs of the section to see if it can find a reference to the
1163 symbol which triggered the warning. If it can, it uses the reloc
1164 to give an error message with a file and line number. */
1167 warning_find_reloc (bfd
*abfd
, asection
*sec
, void *iarg
)
1169 struct warning_callback_info
*info
= (struct warning_callback_info
*) iarg
;
1173 arelent
**p
, **pend
;
1178 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1180 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
1184 relpp
= (arelent
**) xmalloc (relsize
);
1185 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, info
->asymbols
);
1187 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
1190 pend
= p
+ relcount
;
1191 for (; p
< pend
&& *p
!= NULL
; p
++)
1195 if (q
->sym_ptr_ptr
!= NULL
1196 && *q
->sym_ptr_ptr
!= NULL
1197 && strcmp (bfd_asymbol_name (*q
->sym_ptr_ptr
), info
->symbol
) == 0)
1199 /* We found a reloc for the symbol we are looking for. */
1200 einfo ("%C: %s%s\n", abfd
, sec
, q
->address
, _("warning: "),
1210 /* This is called when an undefined symbol is found. */
1213 undefined_symbol (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1220 static char *error_name
;
1221 static unsigned int error_count
;
1223 #define MAX_ERRORS_IN_A_ROW 5
1225 if (config
.warn_once
)
1227 static struct bfd_hash_table
*hash
;
1229 /* Only warn once about a particular undefined symbol. */
1232 hash
= (struct bfd_hash_table
*)
1233 xmalloc (sizeof (struct bfd_hash_table
));
1234 if (!bfd_hash_table_init (hash
, bfd_hash_newfunc
,
1235 sizeof (struct bfd_hash_entry
)))
1236 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
1239 if (bfd_hash_lookup (hash
, name
, FALSE
, FALSE
) != NULL
)
1242 if (bfd_hash_lookup (hash
, name
, TRUE
, TRUE
) == NULL
)
1243 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
1246 /* We never print more than a reasonable number of errors in a row
1247 for a single symbol. */
1248 if (error_name
!= NULL
1249 && strcmp (name
, error_name
) == 0)
1254 if (error_name
!= NULL
)
1256 error_name
= xstrdup (name
);
1259 if (section
!= NULL
)
1261 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1264 einfo (_("%X%C: undefined reference to `%T'\n"),
1265 abfd
, section
, address
, name
);
1267 einfo (_("%C: warning: undefined reference to `%T'\n"),
1268 abfd
, section
, address
, name
);
1270 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1273 einfo (_("%X%D: more undefined references to `%T' follow\n"),
1274 abfd
, section
, address
, name
);
1276 einfo (_("%D: warning: more undefined references to `%T' follow\n"),
1277 abfd
, section
, address
, name
);
1284 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1287 einfo (_("%X%B: undefined reference to `%T'\n"),
1290 einfo (_("%B: warning: undefined reference to `%T'\n"),
1293 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1296 einfo (_("%X%B: more undefined references to `%T' follow\n"),
1299 einfo (_("%B: warning: more undefined references to `%T' follow\n"),
1309 /* Counter to limit the number of relocation overflow error messages
1310 to print. Errors are printed as it is decremented. When it's
1311 called and the counter is zero, a final message is printed
1312 indicating more relocations were omitted. When it gets to -1, no
1313 such errors are printed. If it's initially set to a value less
1314 than -1, all such errors will be printed (--verbose does this). */
1316 int overflow_cutoff_limit
= 10;
1318 /* This is called when a reloc overflows. */
1321 reloc_overflow (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1322 struct bfd_link_hash_entry
*entry
,
1324 const char *reloc_name
,
1330 if (overflow_cutoff_limit
== -1)
1333 einfo ("%X%H:", abfd
, section
, address
);
1335 if (overflow_cutoff_limit
>= 0
1336 && overflow_cutoff_limit
-- == 0)
1338 einfo (_(" additional relocation overflows omitted from the output\n"));
1344 while (entry
->type
== bfd_link_hash_indirect
1345 || entry
->type
== bfd_link_hash_warning
)
1346 entry
= entry
->u
.i
.link
;
1347 switch (entry
->type
)
1349 case bfd_link_hash_undefined
:
1350 case bfd_link_hash_undefweak
:
1351 einfo (_(" relocation truncated to fit: %s against undefined symbol `%T'"),
1352 reloc_name
, entry
->root
.string
);
1354 case bfd_link_hash_defined
:
1355 case bfd_link_hash_defweak
:
1356 einfo (_(" relocation truncated to fit: %s against symbol `%T' defined in %A section in %B"),
1357 reloc_name
, entry
->root
.string
,
1358 entry
->u
.def
.section
,
1359 entry
->u
.def
.section
== bfd_abs_section_ptr
1360 ? link_info
.output_bfd
: entry
->u
.def
.section
->owner
);
1368 einfo (_(" relocation truncated to fit: %s against `%T'"),
1371 einfo ("+%v", addend
);
1376 /* This is called when a dangerous relocation is made. */
1379 reloc_dangerous (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1380 const char *message
,
1385 einfo (_("%X%H: dangerous relocation: %s\n"),
1386 abfd
, section
, address
, message
);
1390 /* This is called when a reloc is being generated attached to a symbol
1391 that is not being output. */
1394 unattached_reloc (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1400 einfo (_("%X%H: reloc refers to symbol `%T' which is not being output\n"),
1401 abfd
, section
, address
, name
);
1405 /* This is called if link_info.notice_all is set, or when a symbol in
1406 link_info.notice_hash is found. Symbols are put in notice_hash
1407 using the -y option, while notice_all is set if the --cref option
1408 has been supplied, or if there are any NOCROSSREFS sections in the
1409 linker script; and if plugins are active, since they need to monitor
1410 all references from non-IR files. */
1413 notice (struct bfd_link_info
*info
,
1414 struct bfd_link_hash_entry
*h
,
1418 flagword flags ATTRIBUTE_UNUSED
,
1419 const char *string ATTRIBUTE_UNUSED
)
1425 if (command_line
.cref
|| nocrossref_list
!= NULL
)
1426 return handle_asneeded_cref (abfd
, (enum notice_asneeded_action
) value
);
1430 name
= h
->root
.string
;
1431 if (info
->notice_hash
!= NULL
1432 && bfd_hash_lookup (info
->notice_hash
, name
, FALSE
, FALSE
) != NULL
)
1434 if (bfd_is_und_section (section
))
1435 einfo ("%B: reference to %s\n", abfd
, name
);
1437 einfo ("%B: definition of %s\n", abfd
, name
);
1440 if (command_line
.cref
|| nocrossref_list
!= NULL
)
1441 add_cref (name
, abfd
, section
, value
);