1 /* Main program of GNU linker.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain steve@cygnus.com
7 This file is part of GLD, the Gnu Linker.
9 GLD 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 2, or (at your option)
14 GLD 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 GLD; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 #include "safe-ctype.h"
28 #include "libiberty.h"
31 #include "filenames.h"
45 /* Somewhere above, sys/stat.h got included . . . . */
46 #if !defined(S_ISDIR) && defined(S_IFDIR)
47 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
53 #ifdef NEED_DECLARATION_SBRK
58 #ifndef TARGET_SYSTEM_ROOT
59 #define TARGET_SYSTEM_ROOT ""
62 int main
PARAMS ((int, char **));
64 static char *get_emulation
PARAMS ((int, char **));
65 static void set_scripts_dir
PARAMS ((void));
70 const char *output_filename
= "a.out";
72 /* Name this program was invoked by. */
75 /* The prefix for system library directories. */
78 /* The file that we're creating. */
81 /* Set by -G argument, for MIPS ECOFF target. */
82 int g_switch_value
= 8;
84 /* Nonzero means print names of input files as processed. */
85 bfd_boolean trace_files
;
87 /* Nonzero means same, but note open failures, too. */
88 bfd_boolean trace_file_tries
;
90 /* Nonzero means version number was printed, so exit successfully
91 instead of complaining if no input files are given. */
92 bfd_boolean version_printed
;
94 /* Nonzero means link in every member of an archive. */
95 bfd_boolean whole_archive
;
97 /* TRUE if we should demangle symbol names. */
98 bfd_boolean demangling
;
100 args_type command_line
;
102 ld_config_type config
;
104 static void remove_output
PARAMS ((void));
105 static bfd_boolean check_for_scripts_dir
PARAMS ((char *dir
));
106 static bfd_boolean add_archive_element
PARAMS ((struct bfd_link_info
*, bfd
*,
108 static bfd_boolean multiple_definition
PARAMS ((struct bfd_link_info
*,
110 bfd
*, asection
*, bfd_vma
,
111 bfd
*, asection
*, bfd_vma
));
112 static bfd_boolean multiple_common
PARAMS ((struct bfd_link_info
*,
114 enum bfd_link_hash_type
, bfd_vma
,
115 bfd
*, enum bfd_link_hash_type
,
117 static bfd_boolean add_to_set
PARAMS ((struct bfd_link_info
*,
118 struct bfd_link_hash_entry
*,
119 bfd_reloc_code_real_type
,
120 bfd
*, asection
*, bfd_vma
));
121 static bfd_boolean constructor_callback
PARAMS ((struct bfd_link_info
*,
122 bfd_boolean constructor
,
124 bfd
*, asection
*, bfd_vma
));
125 static bfd_boolean warning_callback
PARAMS ((struct bfd_link_info
*,
126 const char *, const char *, bfd
*,
127 asection
*, bfd_vma
));
128 static void warning_find_reloc
PARAMS ((bfd
*, asection
*, PTR
));
129 static bfd_boolean undefined_symbol
PARAMS ((struct bfd_link_info
*,
131 asection
*, bfd_vma
, bfd_boolean
));
132 static bfd_boolean reloc_overflow
PARAMS ((struct bfd_link_info
*, const char *,
133 const char *, bfd_vma
,
134 bfd
*, asection
*, bfd_vma
));
135 static bfd_boolean reloc_dangerous
PARAMS ((struct bfd_link_info
*, const char *,
136 bfd
*, asection
*, bfd_vma
));
137 static bfd_boolean unattached_reloc
PARAMS ((struct bfd_link_info
*,
138 const char *, bfd
*, asection
*,
140 static bfd_boolean notice
PARAMS ((struct bfd_link_info
*, const char *,
141 bfd
*, asection
*, bfd_vma
));
143 static struct bfd_link_callbacks link_callbacks
= {
148 constructor_callback
,
157 struct bfd_link_info link_info
;
164 if (output_bfd
&& output_bfd
->iostream
)
165 fclose ((FILE *) (output_bfd
->iostream
));
166 if (delete_output_file_on_failure
)
167 unlink (output_filename
);
177 long start_time
= get_run_time ();
179 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
180 setlocale (LC_MESSAGES
, "");
182 #if defined (HAVE_SETLOCALE)
183 setlocale (LC_CTYPE
, "");
185 bindtextdomain (PACKAGE
, LOCALEDIR
);
186 textdomain (PACKAGE
);
188 program_name
= argv
[0];
189 xmalloc_set_program_name (program_name
);
191 START_PROGRESS (program_name
, 0);
195 bfd_set_error_program_name (program_name
);
197 xatexit (remove_output
);
199 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
200 ld_sysroot
= make_relative_prefix (program_name
, BINDIR
,
205 int res
= stat (ld_sysroot
, &s
) == 0 && S_ISDIR (s
.st_mode
);
209 ld_sysroot
= TARGET_SYSTEM_ROOT
;
213 ld_sysroot
= TARGET_SYSTEM_ROOT
;
215 ld_sysroot
= TARGET_SYSTEM_ROOT
;
218 /* Set the default BFD target based on the configured target. Doing
219 this permits the linker to be configured for a particular target,
220 and linked against a shared BFD library which was configured for
221 a different target. The macro TARGET is defined by Makefile. */
222 if (! bfd_set_default_target (TARGET
))
224 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET
);
235 /* Initialize the data about options. */
236 trace_files
= trace_file_tries
= version_printed
= FALSE
;
237 whole_archive
= FALSE
;
238 config
.build_constructors
= TRUE
;
239 config
.dynamic_link
= FALSE
;
240 config
.has_shared
= FALSE
;
241 config
.split_by_reloc
= (unsigned) -1;
242 config
.split_by_file
= (bfd_size_type
) -1;
243 command_line
.force_common_definition
= FALSE
;
244 command_line
.inhibit_common_definition
= FALSE
;
245 command_line
.interpreter
= NULL
;
246 command_line
.rpath
= NULL
;
247 command_line
.warn_mismatch
= TRUE
;
248 command_line
.check_section_addresses
= TRUE
;
249 command_line
.accept_unknown_input_arch
= FALSE
;
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
.relocateable
= FALSE
;
259 link_info
.emitrelocations
= FALSE
;
260 link_info
.task_link
= FALSE
;
261 link_info
.shared
= FALSE
;
262 link_info
.symbolic
= FALSE
;
263 link_info
.export_dynamic
= FALSE
;
264 link_info
.static_link
= FALSE
;
265 link_info
.traditional_format
= FALSE
;
266 link_info
.optimize
= FALSE
;
267 link_info
.no_undefined
= FALSE
;
268 link_info
.allow_shlib_undefined
= FALSE
;
269 link_info
.allow_multiple_definition
= FALSE
;
270 link_info
.allow_undefined_version
= TRUE
;
271 link_info
.keep_memory
= TRUE
;
272 link_info
.notice_all
= FALSE
;
273 link_info
.nocopyreloc
= FALSE
;
274 link_info
.new_dtags
= FALSE
;
275 link_info
.combreloc
= TRUE
;
276 link_info
.eh_frame_hdr
= FALSE
;
277 link_info
.strip_discarded
= TRUE
;
278 link_info
.strip
= strip_none
;
279 link_info
.discard
= discard_sec_merge
;
280 link_info
.common_skip_ar_aymbols
= bfd_link_common_skip_none
;
281 link_info
.callbacks
= &link_callbacks
;
282 link_info
.hash
= NULL
;
283 link_info
.keep_hash
= NULL
;
284 link_info
.notice_hash
= NULL
;
285 link_info
.wrap_hash
= NULL
;
286 link_info
.input_bfds
= NULL
;
287 link_info
.create_object_symbols_section
= NULL
;
288 link_info
.gc_sym_list
= NULL
;
289 link_info
.base_file
= NULL
;
290 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
291 and _fini symbols. We are compatible. */
292 link_info
.init_function
= "_init";
293 link_info
.fini_function
= "_fini";
294 link_info
.mpc860c0
= 0;
295 link_info
.pei386_auto_import
= -1;
296 link_info
.pei386_runtime_pseudo_reloc
= FALSE
;
297 link_info
.spare_dynamic_tags
= 5;
298 link_info
.flags
= (bfd_vma
) 0;
299 link_info
.flags_1
= (bfd_vma
) 0;
301 ldfile_add_arch ("");
303 config
.make_executable
= TRUE
;
304 force_make_executable
= FALSE
;
305 config
.magic_demand_paged
= TRUE
;
306 config
.text_read_only
= TRUE
;
308 emulation
= get_emulation (argc
, argv
);
309 ldemul_choose_mode (emulation
);
310 default_target
= ldemul_choose_target (argc
, argv
);
312 ldemul_before_parse ();
313 lang_has_input_file
= FALSE
;
314 parse_args (argc
, argv
);
316 ldemul_set_symbols ();
318 if (link_info
.relocateable
)
320 if (command_line
.gc_sections
)
321 einfo ("%P%F: --gc-sections and -r may not be used together\n");
322 if (link_info
.mpc860c0
)
323 einfo (_("%P%F: -r and --mpc860c0 may not be used together\n"));
324 else if (command_line
.relax
)
325 einfo (_("%P%F: --relax and -r may not be used together\n"));
326 if (link_info
.shared
)
327 einfo (_("%P%F: -r and -shared may not be used together\n"));
330 if (! link_info
.shared
)
332 if (command_line
.filter_shlib
)
333 einfo (_("%P%F: -F may not be used without -shared\n"));
334 if (command_line
.auxiliary_filters
)
335 einfo (_("%P%F: -f may not be used without -shared\n"));
338 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
339 don't see how else this can be handled, since in this case we
340 must preserve all externally visible symbols. */
341 if (link_info
.relocateable
&& link_info
.strip
== strip_all
)
343 link_info
.strip
= strip_debugger
;
344 if (link_info
.discard
== discard_sec_merge
)
345 link_info
.discard
= discard_all
;
348 /* This essentially adds another -L directory so this must be done after
349 the -L's in argv have been processed. */
352 /* If we have not already opened and parsed a linker script
353 read the emulation's appropriate default script. */
354 if (saved_script_handle
== NULL
)
357 char *s
= ldemul_get_script (&isfile
);
360 ldfile_open_command_file (s
);
366 parser_input
= input_script
;
371 if (trace_file_tries
)
373 if (saved_script_handle
)
374 info_msg (_("using external linker script:"));
376 info_msg (_("using internal linker script:"));
377 info_msg ("\n==================================================\n");
379 if (saved_script_handle
)
381 static const int ld_bufsz
= 8193;
383 char *buf
= xmalloc (ld_bufsz
);
385 rewind (saved_script_handle
);
386 while ((n
= fread (buf
, 1, ld_bufsz
- 1, saved_script_handle
)) > 0)
391 rewind (saved_script_handle
);
398 info_msg (ldemul_get_script (&isfile
));
401 info_msg ("\n==================================================\n");
406 if (!lang_has_input_file
)
410 einfo (_("%P%F: no input files\n"));
415 info_msg (_("%P: mode %s\n"), emulation
);
418 ldemul_after_parse ();
420 if (config
.map_filename
)
422 if (strcmp (config
.map_filename
, "-") == 0)
424 config
.map_file
= stdout
;
428 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
429 if (config
.map_file
== (FILE *) NULL
)
431 bfd_set_error (bfd_error_system_call
);
432 einfo (_("%P%F: cannot open map file %s: %E\n"),
433 config
.map_filename
);
440 /* Print error messages for any missing symbols, for any warning
441 symbols, and possibly multiple definitions. */
443 if (link_info
.relocateable
)
444 output_bfd
->flags
&= ~EXEC_P
;
446 output_bfd
->flags
|= EXEC_P
;
450 if (config
.map_file
!= NULL
)
452 if (command_line
.cref
)
453 output_cref (config
.map_file
!= NULL
? config
.map_file
: stdout
);
454 if (nocrossref_list
!= NULL
)
455 check_nocrossrefs ();
457 /* Even if we're producing relocateable output, some non-fatal errors should
458 be reported in the exit status. (What non-fatal errors, if any, do we
459 want to ignore for relocateable output?) */
461 if (!config
.make_executable
&& !force_make_executable
)
465 einfo (_("%P: link errors found, deleting executable `%s'\n"),
469 /* The file will be removed by remove_output. */
475 if (! bfd_close (output_bfd
))
476 einfo (_("%F%B: final close failed: %E\n"), output_bfd
);
478 /* If the --force-exe-suffix is enabled, and we're making an
479 executable file and it doesn't end in .exe, copy it to one
481 if (! link_info
.relocateable
&& command_line
.force_exe_suffix
)
483 int len
= strlen (output_filename
);
485 || (strcasecmp (output_filename
+ len
- 4, ".exe") != 0
486 && strcasecmp (output_filename
+ len
- 4, ".dll") != 0))
490 const int bsize
= 4096;
491 char *buf
= xmalloc (bsize
);
493 char *dst_name
= xmalloc (len
+ 5);
494 strcpy (dst_name
, output_filename
);
495 strcat (dst_name
, ".exe");
496 src
= fopen (output_filename
, FOPEN_RB
);
497 dst
= fopen (dst_name
, FOPEN_WB
);
500 einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename
);
502 einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name
);
503 while ((l
= fread (buf
, 1, bsize
, src
)) > 0)
505 int done
= fwrite (buf
, 1, l
, dst
);
508 einfo (_("%P: Error writing file `%s'\n"), dst_name
);
512 if (fclose (dst
) == EOF
)
514 einfo (_("%P: Error closing file `%s'\n"), dst_name
);
522 END_PROGRESS (program_name
);
527 char *lim
= (char *) sbrk (0);
529 long run_time
= get_run_time () - start_time
;
531 fprintf (stderr
, _("%s: total time in link: %ld.%06ld\n"),
532 program_name
, run_time
/ 1000000, run_time
% 1000000);
534 fprintf (stderr
, _("%s: data size %ld\n"), program_name
,
535 (long) (lim
- (char *) &environ
));
539 /* Prevent remove_output from doing anything, after a successful link. */
540 output_filename
= NULL
;
546 /* We need to find any explicitly given emulation in order to initialize the
547 state that's needed by the lex&yacc argument parser (parse_args). */
550 get_emulation (argc
, argv
)
557 emulation
= getenv (EMULATION_ENVIRON
);
558 if (emulation
== NULL
)
559 emulation
= DEFAULT_EMULATION
;
561 for (i
= 1; i
< argc
; i
++)
563 if (!strncmp (argv
[i
], "-m", 2))
565 if (argv
[i
][2] == '\0')
570 emulation
= argv
[i
+ 1];
575 einfo (_("%P%F: missing argument to -m\n"));
578 else if (strcmp (argv
[i
], "-mips1") == 0
579 || strcmp (argv
[i
], "-mips2") == 0
580 || strcmp (argv
[i
], "-mips3") == 0
581 || strcmp (argv
[i
], "-mips32") == 0
582 || strcmp (argv
[i
], "-mips64") == 0
583 || strcmp (argv
[i
], "-mips4") == 0
584 || strcmp (argv
[i
], "-mips5") == 0)
586 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
587 passed to the linker by some MIPS compilers. They
588 generally tell the linker to use a slightly different
589 library path. Perhaps someday these should be
590 implemented as emulations; until then, we just ignore
591 the arguments and hope that nobody ever creates
592 emulations named ips1, ips2 or ips3. */
594 else if (strcmp (argv
[i
], "-m486") == 0)
596 /* FIXME: The argument -m486 is passed to the linker on
597 some Linux systems. Hope that nobody creates an
598 emulation named 486. */
603 emulation
= &argv
[i
][2];
611 /* If directory DIR contains an "ldscripts" subdirectory,
612 add DIR to the library search path and return TRUE,
613 else return FALSE. */
616 check_for_scripts_dir (dir
)
624 dirlen
= strlen (dir
);
625 /* sizeof counts the terminating NUL. */
626 buf
= (char *) xmalloc (dirlen
+ sizeof ("/ldscripts"));
627 sprintf (buf
, "%s/ldscripts", dir
);
629 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
632 ldfile_add_library_path (dir
, FALSE
);
636 /* Set the default directory for finding script files.
637 Libraries will be searched for here too, but that's ok.
638 We look for the "ldscripts" directory in:
640 SCRIPTDIR (passed from Makefile)
641 (adjusted according to the current location of the binary)
642 SCRIPTDIR (passed from Makefile)
643 the dir where this program is (for using it from the build tree)
644 the dir where this program is/../lib
645 (for installing the tool suite elsewhere) */
653 dir
= make_relative_prefix (program_name
, BINDIR
, SCRIPTDIR
);
654 if (dir
&& check_for_scripts_dir (dir
))
655 /* Success. Don't free dir. */
661 if (check_for_scripts_dir (SCRIPTDIR
))
662 /* We've been installed normally. */
665 /* Look for "ldscripts" in the dir where our binary is. */
666 end
= strrchr (program_name
, '/');
667 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
669 /* We could have \foo\bar, or /foo\bar. */
670 char *bslash
= strrchr (program_name
, '\\');
671 if (end
== NULL
|| (bslash
!= NULL
&& bslash
> end
))
678 /* Don't look for ldscripts in the current directory. There is
679 too much potential for confusion. */
683 dirlen
= end
- program_name
;
684 /* Make a copy of program_name in dir.
685 Leave room for later "/../lib". */
686 dir
= (char *) xmalloc (dirlen
+ 8);
687 strncpy (dir
, program_name
, dirlen
);
690 if (check_for_scripts_dir (dir
))
691 /* Don't free dir. */
694 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
695 strcpy (dir
+ dirlen
, "/../lib");
696 if (check_for_scripts_dir (dir
))
699 /* Well, we tried. */
707 if (link_info
.notice_hash
== (struct bfd_hash_table
*) NULL
)
709 link_info
.notice_hash
= ((struct bfd_hash_table
*)
710 xmalloc (sizeof (struct bfd_hash_table
)));
711 if (! bfd_hash_table_init_n (link_info
.notice_hash
,
714 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
717 if (bfd_hash_lookup (link_info
.notice_hash
, name
, TRUE
, TRUE
)
718 == (struct bfd_hash_entry
*) NULL
)
719 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
722 /* Record a symbol to be wrapped, from the --wrap option. */
728 if (link_info
.wrap_hash
== NULL
)
730 link_info
.wrap_hash
= ((struct bfd_hash_table
*)
731 xmalloc (sizeof (struct bfd_hash_table
)));
732 if (! bfd_hash_table_init_n (link_info
.wrap_hash
,
735 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
737 if (bfd_hash_lookup (link_info
.wrap_hash
, name
, TRUE
, TRUE
) == NULL
)
738 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
741 /* Handle the -retain-symbols-file option. */
744 add_keepsyms_file (filename
)
745 const char *filename
;
752 if (link_info
.strip
== strip_some
)
753 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
755 file
= fopen (filename
, "r");
756 if (file
== (FILE *) NULL
)
758 bfd_set_error (bfd_error_system_call
);
759 einfo ("%X%P: %s: %E\n", filename
);
763 link_info
.keep_hash
= ((struct bfd_hash_table
*)
764 xmalloc (sizeof (struct bfd_hash_table
)));
765 if (! bfd_hash_table_init (link_info
.keep_hash
, bfd_hash_newfunc
))
766 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
769 buf
= (char *) xmalloc (bufsize
);
781 while (! ISSPACE (c
) && c
!= EOF
)
788 buf
= xrealloc (buf
, bufsize
);
795 if (bfd_hash_lookup (link_info
.keep_hash
, buf
, TRUE
, TRUE
)
796 == (struct bfd_hash_entry
*) NULL
)
797 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
801 if (link_info
.strip
!= strip_none
)
802 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
804 link_info
.strip
= strip_some
;
807 /* Callbacks from the BFD linker routines. */
809 /* This is called when BFD has decided to include an archive member in
813 add_archive_element (info
, abfd
, name
)
814 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
818 lang_input_statement_type
*input
;
820 input
= ((lang_input_statement_type
*)
821 xmalloc (sizeof (lang_input_statement_type
)));
822 input
->filename
= abfd
->filename
;
823 input
->local_sym_name
= abfd
->filename
;
824 input
->the_bfd
= abfd
;
825 input
->asymbols
= NULL
;
827 input
->just_syms_flag
= FALSE
;
828 input
->loaded
= FALSE
;
829 input
->search_dirs_flag
= FALSE
;
831 /* FIXME: The following fields are not set: header.next,
832 header.type, closed, passive_position, symbol_count,
833 next_real_file, is_archive, target, real. This bit of code is
834 from the old decode_library_subfile function. I don't know
835 whether any of those fields matters. */
837 ldlang_add_file (input
);
839 if (config
.map_file
!= (FILE *) NULL
)
841 static bfd_boolean header_printed
;
842 struct bfd_link_hash_entry
*h
;
846 h
= bfd_link_hash_lookup (link_info
.hash
, name
, FALSE
, FALSE
, TRUE
);
858 case bfd_link_hash_defined
:
859 case bfd_link_hash_defweak
:
860 from
= h
->u
.def
.section
->owner
;
863 case bfd_link_hash_undefined
:
864 case bfd_link_hash_undefweak
:
865 from
= h
->u
.undef
.abfd
;
868 case bfd_link_hash_common
:
869 from
= h
->u
.c
.p
->section
->owner
;
874 if (! header_printed
)
878 sprintf (buf
, _("Archive member included because of file (symbol)\n\n"));
880 header_printed
= TRUE
;
883 if (bfd_my_archive (abfd
) == NULL
)
885 minfo ("%s", bfd_get_filename (abfd
));
886 len
= strlen (bfd_get_filename (abfd
));
890 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd
)),
891 bfd_get_filename (abfd
));
892 len
= (strlen (bfd_get_filename (bfd_my_archive (abfd
)))
893 + strlen (bfd_get_filename (abfd
))
911 minfo ("(%T)\n", h
->root
.string
);
913 minfo ("(%s)\n", name
);
916 if (trace_files
|| trace_file_tries
)
917 info_msg ("%I\n", input
);
922 /* This is called when BFD has discovered a symbol which is defined
926 multiple_definition (info
, name
, obfd
, osec
, oval
, nbfd
, nsec
, nval
)
927 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
936 /* If either section has the output_section field set to
937 bfd_abs_section_ptr, it means that the section is being
938 discarded, and this is not really a multiple definition at all.
939 FIXME: It would be cleaner to somehow ignore symbols defined in
940 sections which are being discarded. */
941 if ((osec
->output_section
!= NULL
942 && ! bfd_is_abs_section (osec
)
943 && bfd_is_abs_section (osec
->output_section
))
944 || (nsec
->output_section
!= NULL
945 && ! bfd_is_abs_section (nsec
)
946 && bfd_is_abs_section (nsec
->output_section
)))
949 einfo (_("%X%C: multiple definition of `%T'\n"),
950 nbfd
, nsec
, nval
, name
);
951 if (obfd
!= (bfd
*) NULL
)
952 einfo (_("%D: first defined here\n"), obfd
, osec
, oval
);
954 if (command_line
.relax
)
956 einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
957 command_line
.relax
= 0;
963 /* This is called when there is a definition of a common symbol, or
964 when a common symbol is found for a symbol that is already defined,
965 or when two common symbols are found. We only do something if
966 -warn-common was used. */
969 multiple_common (info
, name
, obfd
, otype
, osize
, nbfd
, ntype
, nsize
)
970 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
973 enum bfd_link_hash_type otype
;
976 enum bfd_link_hash_type ntype
;
979 if (! config
.warn_common
)
982 if (ntype
== bfd_link_hash_defined
983 || ntype
== bfd_link_hash_defweak
984 || ntype
== bfd_link_hash_indirect
)
986 ASSERT (otype
== bfd_link_hash_common
);
987 einfo (_("%B: warning: definition of `%T' overriding common\n"),
990 einfo (_("%B: warning: common is here\n"), obfd
);
992 else if (otype
== bfd_link_hash_defined
993 || otype
== bfd_link_hash_defweak
994 || otype
== bfd_link_hash_indirect
)
996 ASSERT (ntype
== bfd_link_hash_common
);
997 einfo (_("%B: warning: common of `%T' overridden by definition\n"),
1000 einfo (_("%B: warning: defined here\n"), obfd
);
1004 ASSERT (otype
== bfd_link_hash_common
&& ntype
== bfd_link_hash_common
);
1007 einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
1010 einfo (_("%B: warning: larger common is here\n"), obfd
);
1012 else if (nsize
> osize
)
1014 einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
1017 einfo (_("%B: warning: smaller common is here\n"), obfd
);
1021 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd
, name
);
1023 einfo (_("%B: warning: previous common is here\n"), obfd
);
1030 /* This is called when BFD has discovered a set element. H is the
1031 entry in the linker hash table for the set. SECTION and VALUE
1032 represent a value which should be added to the set. */
1035 add_to_set (info
, h
, reloc
, abfd
, section
, value
)
1036 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1037 struct bfd_link_hash_entry
*h
;
1038 bfd_reloc_code_real_type reloc
;
1043 if (config
.warn_constructors
)
1044 einfo (_("%P: warning: global constructor %s used\n"),
1047 if (! config
.build_constructors
)
1050 ldctor_add_set_entry (h
, reloc
, (const char *) NULL
, section
, value
);
1052 if (h
->type
== bfd_link_hash_new
)
1054 h
->type
= bfd_link_hash_undefined
;
1055 h
->u
.undef
.abfd
= abfd
;
1056 /* We don't call bfd_link_add_undef to add this to the list of
1057 undefined symbols because we are going to define it
1064 /* This is called when BFD has discovered a constructor. This is only
1065 called for some object file formats--those which do not handle
1066 constructors in some more clever fashion. This is similar to
1067 adding an element to a set, but less general. */
1070 constructor_callback (info
, constructor
, name
, abfd
, section
, value
)
1071 struct bfd_link_info
*info
;
1072 bfd_boolean constructor
;
1079 struct bfd_link_hash_entry
*h
;
1080 char set_name
[1 + sizeof "__CTOR_LIST__"];
1082 if (config
.warn_constructors
)
1083 einfo (_("%P: warning: global constructor %s used\n"), name
);
1085 if (! config
.build_constructors
)
1088 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1089 useful error message. */
1090 if (bfd_reloc_type_lookup (output_bfd
, BFD_RELOC_CTOR
) == NULL
1091 && (link_info
.relocateable
1092 || bfd_reloc_type_lookup (abfd
, BFD_RELOC_CTOR
) == NULL
))
1093 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1096 if (bfd_get_symbol_leading_char (abfd
) != '\0')
1097 *s
++ = bfd_get_symbol_leading_char (abfd
);
1099 strcpy (s
, "__CTOR_LIST__");
1101 strcpy (s
, "__DTOR_LIST__");
1103 h
= bfd_link_hash_lookup (info
->hash
, set_name
, TRUE
, TRUE
, TRUE
);
1104 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1105 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1106 if (h
->type
== bfd_link_hash_new
)
1108 h
->type
= bfd_link_hash_undefined
;
1109 h
->u
.undef
.abfd
= abfd
;
1110 /* We don't call bfd_link_add_undef to add this to the list of
1111 undefined symbols because we are going to define it
1115 ldctor_add_set_entry (h
, BFD_RELOC_CTOR
, name
, section
, value
);
1119 /* A structure used by warning_callback to pass information through
1120 bfd_map_over_sections. */
1122 struct warning_callback_info
{
1124 const char *warning
;
1129 /* This is called when there is a reference to a warning symbol. */
1132 warning_callback (info
, warning
, symbol
, abfd
, section
, address
)
1133 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1134 const char *warning
;
1140 /* This is a hack to support warn_multiple_gp. FIXME: This should
1141 have a cleaner interface, but what? */
1142 if (! config
.warn_multiple_gp
1143 && strcmp (warning
, "using multiple gp values") == 0)
1146 if (section
!= NULL
)
1147 einfo ("%C: %s\n", abfd
, section
, address
, warning
);
1148 else if (abfd
== NULL
)
1149 einfo ("%P: %s\n", warning
);
1150 else if (symbol
== NULL
)
1151 einfo ("%B: %s\n", abfd
, warning
);
1154 lang_input_statement_type
*entry
;
1156 struct warning_callback_info info
;
1158 /* Look through the relocs to see if we can find a plausible
1161 entry
= (lang_input_statement_type
*) abfd
->usrdata
;
1162 if (entry
!= NULL
&& entry
->asymbols
!= NULL
)
1163 asymbols
= entry
->asymbols
;
1169 symsize
= bfd_get_symtab_upper_bound (abfd
);
1171 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
1172 asymbols
= (asymbol
**) xmalloc (symsize
);
1173 symbol_count
= bfd_canonicalize_symtab (abfd
, asymbols
);
1174 if (symbol_count
< 0)
1175 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
1178 entry
->asymbols
= asymbols
;
1179 entry
->symbol_count
= symbol_count
;
1184 info
.warning
= warning
;
1185 info
.symbol
= symbol
;
1186 info
.asymbols
= asymbols
;
1187 bfd_map_over_sections (abfd
, warning_find_reloc
, (PTR
) &info
);
1190 einfo ("%B: %s\n", abfd
, warning
);
1199 /* This is called by warning_callback for each section. It checks the
1200 relocs of the section to see if it can find a reference to the
1201 symbol which triggered the warning. If it can, it uses the reloc
1202 to give an error message with a file and line number. */
1205 warning_find_reloc (abfd
, sec
, iarg
)
1210 struct warning_callback_info
*info
= (struct warning_callback_info
*) iarg
;
1214 arelent
**p
, **pend
;
1219 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1221 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
1225 relpp
= (arelent
**) xmalloc (relsize
);
1226 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, info
->asymbols
);
1228 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
1231 pend
= p
+ relcount
;
1232 for (; p
< pend
&& *p
!= NULL
; p
++)
1236 if (q
->sym_ptr_ptr
!= NULL
1237 && *q
->sym_ptr_ptr
!= NULL
1238 && strcmp (bfd_asymbol_name (*q
->sym_ptr_ptr
), info
->symbol
) == 0)
1240 /* We found a reloc for the symbol we are looking for. */
1241 einfo ("%C: %s\n", abfd
, sec
, q
->address
, info
->warning
);
1250 /* This is called when an undefined symbol is found. */
1253 undefined_symbol (info
, name
, abfd
, section
, address
, fatal
)
1254 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1259 bfd_boolean fatal ATTRIBUTE_UNUSED
;
1261 static char *error_name
;
1262 static unsigned int error_count
;
1264 #define MAX_ERRORS_IN_A_ROW 5
1266 if (config
.warn_once
)
1268 static struct bfd_hash_table
*hash
;
1270 /* Only warn once about a particular undefined symbol. */
1274 hash
= ((struct bfd_hash_table
*)
1275 xmalloc (sizeof (struct bfd_hash_table
)));
1276 if (! bfd_hash_table_init (hash
, bfd_hash_newfunc
))
1277 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
1280 if (bfd_hash_lookup (hash
, name
, FALSE
, FALSE
) != NULL
)
1283 if (bfd_hash_lookup (hash
, name
, TRUE
, TRUE
) == NULL
)
1284 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
1287 /* We never print more than a reasonable number of errors in a row
1288 for a single symbol. */
1289 if (error_name
!= (char *) NULL
1290 && strcmp (name
, error_name
) == 0)
1295 if (error_name
!= (char *) NULL
)
1297 error_name
= xstrdup (name
);
1300 if (section
!= NULL
)
1302 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1304 einfo (_("%C: undefined reference to `%T'\n"),
1305 abfd
, section
, address
, name
);
1309 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1310 einfo (_("%D: more undefined references to `%T' follow\n"),
1311 abfd
, section
, address
, name
);
1315 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1317 einfo (_("%B: undefined reference to `%T'\n"),
1322 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1323 einfo (_("%B: more undefined references to `%T' follow\n"),
1330 /* This is called when a reloc overflows. */
1333 reloc_overflow (info
, name
, reloc_name
, addend
, abfd
, section
, address
)
1334 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1336 const char *reloc_name
;
1342 if (abfd
== (bfd
*) NULL
)
1343 einfo (_("%P%X: generated"));
1345 einfo ("%X%C:", abfd
, section
, address
);
1346 einfo (_(" relocation truncated to fit: %s %T"), reloc_name
, name
);
1348 einfo ("+%v", addend
);
1353 /* This is called when a dangerous relocation is made. */
1356 reloc_dangerous (info
, message
, abfd
, section
, address
)
1357 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1358 const char *message
;
1363 if (abfd
== (bfd
*) NULL
)
1364 einfo (_("%P%X: generated"));
1366 einfo ("%X%C:", abfd
, section
, address
);
1367 einfo (_("dangerous relocation: %s\n"), message
);
1371 /* This is called when a reloc is being generated attached to a symbol
1372 that is not being output. */
1375 unattached_reloc (info
, name
, abfd
, section
, address
)
1376 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1382 if (abfd
== (bfd
*) NULL
)
1383 einfo (_("%P%X: generated"));
1385 einfo ("%X%C:", abfd
, section
, address
);
1386 einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name
);
1390 /* This is called if link_info.notice_all is set, or when a symbol in
1391 link_info.notice_hash is found. Symbols are put in notice_hash
1392 using the -y option. */
1395 notice (info
, name
, abfd
, section
, value
)
1396 struct bfd_link_info
*info
;
1402 if (! info
->notice_all
1403 || (info
->notice_hash
!= NULL
1404 && bfd_hash_lookup (info
->notice_hash
, name
, FALSE
, FALSE
) != NULL
))
1406 if (bfd_is_und_section (section
))
1407 einfo ("%B: reference to %s\n", abfd
, name
);
1409 einfo ("%B: definition of %s\n", abfd
, name
);
1412 if (command_line
.cref
|| nocrossref_list
!= NULL
)
1413 add_cref (name
, abfd
, section
, value
);