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 **));
67 const char *output_filename
= "a.out";
69 /* Name this program was invoked by. */
72 /* The prefix for system library directories. */
75 /* The canonical representation of ld_sysroot. */
76 char * ld_canon_sysroot
;
77 int ld_canon_sysroot_len
;
79 /* The file that we're creating. */
82 /* Set by -G argument, for MIPS ECOFF target. */
83 int g_switch_value
= 8;
85 /* Nonzero means print names of input files as processed. */
86 bfd_boolean trace_files
;
88 /* Nonzero means same, but note open failures, too. */
89 bfd_boolean trace_file_tries
;
91 /* Nonzero means version number was printed, so exit successfully
92 instead of complaining if no input files are given. */
93 bfd_boolean version_printed
;
95 /* Nonzero means link in every member of an archive. */
96 bfd_boolean whole_archive
;
98 /* TRUE if we should demangle symbol names. */
99 bfd_boolean demangling
;
101 args_type command_line
;
103 ld_config_type config
;
105 static char *get_emulation
106 PARAMS ((int, char **));
107 static void set_scripts_dir
109 static void remove_output
111 static bfd_boolean check_for_scripts_dir
113 static bfd_boolean add_archive_element
114 PARAMS ((struct bfd_link_info
*, bfd
*, const char *));
115 static bfd_boolean multiple_definition
116 PARAMS ((struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
,
117 bfd
*, asection
*, bfd_vma
));
118 static bfd_boolean multiple_common
119 PARAMS ((struct bfd_link_info
*, const char *, bfd
*,
120 enum bfd_link_hash_type
, bfd_vma
, bfd
*, enum bfd_link_hash_type
,
122 static bfd_boolean add_to_set
123 PARAMS ((struct bfd_link_info
*, struct bfd_link_hash_entry
*,
124 bfd_reloc_code_real_type
, bfd
*, asection
*, bfd_vma
));
125 static bfd_boolean constructor_callback
126 PARAMS ((struct bfd_link_info
*, bfd_boolean
, const char *, bfd
*,
127 asection
*, bfd_vma
));
128 static bfd_boolean warning_callback
129 PARAMS ((struct bfd_link_info
*, const char *, const char *, bfd
*,
130 asection
*, bfd_vma
));
131 static void warning_find_reloc
132 PARAMS ((bfd
*, asection
*, PTR
));
133 static bfd_boolean undefined_symbol
134 PARAMS ((struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
,
136 static bfd_boolean reloc_overflow
137 PARAMS ((struct bfd_link_info
*, const char *, const char *, bfd_vma
,
138 bfd
*, asection
*, bfd_vma
));
139 static bfd_boolean reloc_dangerous
140 PARAMS ((struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
));
141 static bfd_boolean unattached_reloc
142 PARAMS ((struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
));
143 static bfd_boolean notice
144 PARAMS ((struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
));
146 static struct bfd_link_callbacks link_callbacks
=
152 constructor_callback
,
162 struct bfd_link_info link_info
;
169 if (output_bfd
&& output_bfd
->iostream
)
170 fclose ((FILE *) (output_bfd
->iostream
));
171 if (delete_output_file_on_failure
)
172 unlink (output_filename
);
182 long start_time
= get_run_time ();
184 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
185 setlocale (LC_MESSAGES
, "");
187 #if defined (HAVE_SETLOCALE)
188 setlocale (LC_CTYPE
, "");
190 bindtextdomain (PACKAGE
, LOCALEDIR
);
191 textdomain (PACKAGE
);
193 program_name
= argv
[0];
194 xmalloc_set_program_name (program_name
);
196 START_PROGRESS (program_name
, 0);
200 bfd_set_error_program_name (program_name
);
202 xatexit (remove_output
);
204 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
205 ld_sysroot
= make_relative_prefix (program_name
, BINDIR
,
211 int res
= stat (ld_sysroot
, &s
) == 0 && S_ISDIR (s
.st_mode
);
222 ld_sysroot
= make_relative_prefix (program_name
, TOOLBINDIR
,
228 int res
= stat (ld_sysroot
, &s
) == 0 && S_ISDIR (s
.st_mode
);
240 ld_sysroot
= TARGET_SYSTEM_ROOT
;
242 if (ld_sysroot
&& *ld_sysroot
)
243 ld_canon_sysroot
= lrealpath (ld_sysroot
);
245 if (ld_canon_sysroot
)
246 ld_canon_sysroot_len
= strlen (ld_canon_sysroot
);
248 ld_canon_sysroot_len
= -1;
250 /* Set the default BFD target based on the configured target. Doing
251 this permits the linker to be configured for a particular target,
252 and linked against a shared BFD library which was configured for
253 a different target. The macro TARGET is defined by Makefile. */
254 if (! bfd_set_default_target (TARGET
))
256 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET
);
267 /* Initialize the data about options. */
268 trace_files
= trace_file_tries
= version_printed
= FALSE
;
269 whole_archive
= FALSE
;
270 config
.build_constructors
= TRUE
;
271 config
.dynamic_link
= FALSE
;
272 config
.has_shared
= FALSE
;
273 config
.split_by_reloc
= (unsigned) -1;
274 config
.split_by_file
= (bfd_size_type
) -1;
275 command_line
.force_common_definition
= FALSE
;
276 command_line
.inhibit_common_definition
= FALSE
;
277 command_line
.interpreter
= NULL
;
278 command_line
.rpath
= NULL
;
279 command_line
.warn_mismatch
= TRUE
;
280 command_line
.check_section_addresses
= TRUE
;
281 command_line
.accept_unknown_input_arch
= FALSE
;
283 /* We initialize DEMANGLING based on the environment variable
284 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
285 output of the linker, unless COLLECT_NO_DEMANGLE is set in the
286 environment. Acting the same way here lets us provide the same
287 interface by default. */
288 demangling
= getenv ("COLLECT_NO_DEMANGLE") == NULL
;
290 link_info
.relocateable
= FALSE
;
291 link_info
.emitrelocations
= FALSE
;
292 link_info
.task_link
= FALSE
;
293 link_info
.shared
= FALSE
;
294 link_info
.symbolic
= FALSE
;
295 link_info
.export_dynamic
= FALSE
;
296 link_info
.static_link
= FALSE
;
297 link_info
.traditional_format
= FALSE
;
298 link_info
.optimize
= FALSE
;
299 link_info
.no_undefined
= FALSE
;
300 link_info
.allow_shlib_undefined
= TRUE
;
301 link_info
.allow_multiple_definition
= FALSE
;
302 link_info
.allow_undefined_version
= TRUE
;
303 link_info
.keep_memory
= TRUE
;
304 link_info
.notice_all
= FALSE
;
305 link_info
.nocopyreloc
= FALSE
;
306 link_info
.new_dtags
= FALSE
;
307 link_info
.combreloc
= TRUE
;
308 link_info
.eh_frame_hdr
= FALSE
;
309 link_info
.strip_discarded
= TRUE
;
310 link_info
.strip
= strip_none
;
311 link_info
.discard
= discard_sec_merge
;
312 link_info
.common_skip_ar_aymbols
= bfd_link_common_skip_none
;
313 link_info
.callbacks
= &link_callbacks
;
314 link_info
.hash
= NULL
;
315 link_info
.keep_hash
= NULL
;
316 link_info
.notice_hash
= NULL
;
317 link_info
.wrap_hash
= NULL
;
318 link_info
.input_bfds
= NULL
;
319 link_info
.create_object_symbols_section
= NULL
;
320 link_info
.gc_sym_list
= NULL
;
321 link_info
.base_file
= NULL
;
322 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
323 and _fini symbols. We are compatible. */
324 link_info
.init_function
= "_init";
325 link_info
.fini_function
= "_fini";
326 link_info
.mpc860c0
= 0;
327 link_info
.pei386_auto_import
= -1;
328 link_info
.pei386_runtime_pseudo_reloc
= FALSE
;
329 link_info
.spare_dynamic_tags
= 5;
330 link_info
.flags
= (bfd_vma
) 0;
331 link_info
.flags_1
= (bfd_vma
) 0;
332 link_info
.relax_finalizing
= FALSE
;
334 ldfile_add_arch ("");
336 config
.make_executable
= TRUE
;
337 force_make_executable
= FALSE
;
338 config
.magic_demand_paged
= TRUE
;
339 config
.text_read_only
= TRUE
;
341 emulation
= get_emulation (argc
, argv
);
342 ldemul_choose_mode (emulation
);
343 default_target
= ldemul_choose_target (argc
, argv
);
345 ldemul_before_parse ();
346 lang_has_input_file
= FALSE
;
347 parse_args (argc
, argv
);
349 ldemul_set_symbols ();
351 if (link_info
.relocateable
)
353 if (command_line
.gc_sections
)
354 einfo ("%P%F: --gc-sections and -r may not be used together\n");
355 if (link_info
.mpc860c0
)
356 einfo (_("%P%F: -r and --mpc860c0 may not be used together\n"));
357 else if (command_line
.relax
)
358 einfo (_("%P%F: --relax and -r may not be used together\n"));
359 if (link_info
.shared
)
360 einfo (_("%P%F: -r and -shared may not be used together\n"));
363 if (! link_info
.shared
)
365 if (command_line
.filter_shlib
)
366 einfo (_("%P%F: -F may not be used without -shared\n"));
367 if (command_line
.auxiliary_filters
)
368 einfo (_("%P%F: -f may not be used without -shared\n"));
371 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
372 don't see how else this can be handled, since in this case we
373 must preserve all externally visible symbols. */
374 if (link_info
.relocateable
&& link_info
.strip
== strip_all
)
376 link_info
.strip
= strip_debugger
;
377 if (link_info
.discard
== discard_sec_merge
)
378 link_info
.discard
= discard_all
;
381 /* This essentially adds another -L directory so this must be done after
382 the -L's in argv have been processed. */
385 /* If we have not already opened and parsed a linker script
386 read the emulation's appropriate default script. */
387 if (saved_script_handle
== NULL
)
390 char *s
= ldemul_get_script (&isfile
);
393 ldfile_open_command_file (s
);
399 parser_input
= input_script
;
404 if (trace_file_tries
)
406 if (saved_script_handle
)
407 info_msg (_("using external linker script:"));
409 info_msg (_("using internal linker script:"));
410 info_msg ("\n==================================================\n");
412 if (saved_script_handle
)
414 static const int ld_bufsz
= 8193;
416 char *buf
= xmalloc (ld_bufsz
);
418 rewind (saved_script_handle
);
419 while ((n
= fread (buf
, 1, ld_bufsz
- 1, saved_script_handle
)) > 0)
424 rewind (saved_script_handle
);
431 info_msg (ldemul_get_script (&isfile
));
434 info_msg ("\n==================================================\n");
439 if (!lang_has_input_file
)
443 einfo (_("%P%F: no input files\n"));
447 info_msg (_("%P: mode %s\n"), emulation
);
449 ldemul_after_parse ();
451 if (config
.map_filename
)
453 if (strcmp (config
.map_filename
, "-") == 0)
455 config
.map_file
= stdout
;
459 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
460 if (config
.map_file
== (FILE *) NULL
)
462 bfd_set_error (bfd_error_system_call
);
463 einfo (_("%P%F: cannot open map file %s: %E\n"),
464 config
.map_filename
);
471 /* Print error messages for any missing symbols, for any warning
472 symbols, and possibly multiple definitions. */
473 if (link_info
.relocateable
)
474 output_bfd
->flags
&= ~EXEC_P
;
476 output_bfd
->flags
|= EXEC_P
;
480 if (config
.map_file
!= NULL
)
482 if (command_line
.cref
)
483 output_cref (config
.map_file
!= NULL
? config
.map_file
: stdout
);
484 if (nocrossref_list
!= NULL
)
485 check_nocrossrefs ();
487 /* Even if we're producing relocateable output, some non-fatal errors should
488 be reported in the exit status. (What non-fatal errors, if any, do we
489 want to ignore for relocateable output?) */
490 if (!config
.make_executable
&& !force_make_executable
)
493 einfo (_("%P: link errors found, deleting executable `%s'\n"),
496 /* The file will be removed by remove_output. */
501 if (! bfd_close (output_bfd
))
502 einfo (_("%F%B: final close failed: %E\n"), output_bfd
);
504 /* If the --force-exe-suffix is enabled, and we're making an
505 executable file and it doesn't end in .exe, copy it to one
507 if (! link_info
.relocateable
&& command_line
.force_exe_suffix
)
509 int len
= strlen (output_filename
);
512 || (strcasecmp (output_filename
+ len
- 4, ".exe") != 0
513 && strcasecmp (output_filename
+ len
- 4, ".dll") != 0))
517 const int bsize
= 4096;
518 char *buf
= xmalloc (bsize
);
520 char *dst_name
= xmalloc (len
+ 5);
522 strcpy (dst_name
, output_filename
);
523 strcat (dst_name
, ".exe");
524 src
= fopen (output_filename
, FOPEN_RB
);
525 dst
= fopen (dst_name
, FOPEN_WB
);
528 einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename
);
530 einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name
);
531 while ((l
= fread (buf
, 1, bsize
, src
)) > 0)
533 int done
= fwrite (buf
, 1, l
, dst
);
536 einfo (_("%P: Error writing file `%s'\n"), dst_name
);
540 if (fclose (dst
) == EOF
)
541 einfo (_("%P: Error closing file `%s'\n"), dst_name
);
548 END_PROGRESS (program_name
);
553 char *lim
= (char *) sbrk (0);
555 long run_time
= get_run_time () - start_time
;
557 fprintf (stderr
, _("%s: total time in link: %ld.%06ld\n"),
558 program_name
, run_time
/ 1000000, run_time
% 1000000);
560 fprintf (stderr
, _("%s: data size %ld\n"), program_name
,
561 (long) (lim
- (char *) &environ
));
565 /* Prevent remove_output from doing anything, after a successful link. */
566 output_filename
= NULL
;
572 /* We need to find any explicitly given emulation in order to initialize the
573 state that's needed by the lex&yacc argument parser (parse_args). */
576 get_emulation (argc
, argv
)
583 emulation
= getenv (EMULATION_ENVIRON
);
584 if (emulation
== NULL
)
585 emulation
= DEFAULT_EMULATION
;
587 for (i
= 1; i
< argc
; i
++)
589 if (!strncmp (argv
[i
], "-m", 2))
591 if (argv
[i
][2] == '\0')
596 emulation
= argv
[i
+ 1];
600 einfo (_("%P%F: missing argument to -m\n"));
602 else if (strcmp (argv
[i
], "-mips1") == 0
603 || strcmp (argv
[i
], "-mips2") == 0
604 || strcmp (argv
[i
], "-mips3") == 0
605 || strcmp (argv
[i
], "-mips4") == 0
606 || strcmp (argv
[i
], "-mips5") == 0
607 || strcmp (argv
[i
], "-mips32") == 0
608 || strcmp (argv
[i
], "-mips32r2") == 0
609 || strcmp (argv
[i
], "-mips64") == 0)
611 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
612 passed to the linker by some MIPS compilers. They
613 generally tell the linker to use a slightly different
614 library path. Perhaps someday these should be
615 implemented as emulations; until then, we just ignore
616 the arguments and hope that nobody ever creates
617 emulations named ips1, ips2 or ips3. */
619 else if (strcmp (argv
[i
], "-m486") == 0)
621 /* FIXME: The argument -m486 is passed to the linker on
622 some Linux systems. Hope that nobody creates an
623 emulation named 486. */
628 emulation
= &argv
[i
][2];
636 /* If directory DIR contains an "ldscripts" subdirectory,
637 add DIR to the library search path and return TRUE,
638 else return FALSE. */
641 check_for_scripts_dir (dir
)
649 dirlen
= strlen (dir
);
650 /* sizeof counts the terminating NUL. */
651 buf
= (char *) xmalloc (dirlen
+ sizeof ("/ldscripts"));
652 sprintf (buf
, "%s/ldscripts", dir
);
654 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
657 ldfile_add_library_path (dir
, FALSE
);
661 /* Set the default directory for finding script files.
662 Libraries will be searched for here too, but that's ok.
663 We look for the "ldscripts" directory in:
665 SCRIPTDIR (passed from Makefile)
666 (adjusted according to the current location of the binary)
667 SCRIPTDIR (passed from Makefile)
668 the dir where this program is (for using it from the build tree)
669 the dir where this program is/../lib
670 (for installing the tool suite elsewhere). */
679 dir
= make_relative_prefix (program_name
, BINDIR
, SCRIPTDIR
);
682 found
= check_for_scripts_dir (dir
);
688 dir
= make_relative_prefix (program_name
, TOOLBINDIR
, SCRIPTDIR
);
691 found
= check_for_scripts_dir (dir
);
697 if (check_for_scripts_dir (SCRIPTDIR
))
698 /* We've been installed normally. */
701 /* Look for "ldscripts" in the dir where our binary is. */
702 end
= strrchr (program_name
, '/');
703 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
705 /* We could have \foo\bar, or /foo\bar. */
706 char *bslash
= strrchr (program_name
, '\\');
708 if (end
== NULL
|| (bslash
!= NULL
&& bslash
> end
))
714 /* Don't look for ldscripts in the current directory. There is
715 too much potential for confusion. */
718 dirlen
= end
- program_name
;
719 /* Make a copy of program_name in dir.
720 Leave room for later "/../lib". */
721 dir
= (char *) xmalloc (dirlen
+ 8);
722 strncpy (dir
, program_name
, dirlen
);
725 if (check_for_scripts_dir (dir
))
731 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
732 strcpy (dir
+ dirlen
, "/../lib");
733 check_for_scripts_dir (dir
);
741 if (link_info
.notice_hash
== (struct bfd_hash_table
*) NULL
)
743 link_info
.notice_hash
= ((struct bfd_hash_table
*)
744 xmalloc (sizeof (struct bfd_hash_table
)));
745 if (! bfd_hash_table_init_n (link_info
.notice_hash
,
748 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
751 if (bfd_hash_lookup (link_info
.notice_hash
, name
, TRUE
, TRUE
)
752 == (struct bfd_hash_entry
*) NULL
)
753 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
756 /* Record a symbol to be wrapped, from the --wrap option. */
762 if (link_info
.wrap_hash
== NULL
)
764 link_info
.wrap_hash
= ((struct bfd_hash_table
*)
765 xmalloc (sizeof (struct bfd_hash_table
)));
766 if (! bfd_hash_table_init_n (link_info
.wrap_hash
,
769 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
772 if (bfd_hash_lookup (link_info
.wrap_hash
, name
, TRUE
, TRUE
) == NULL
)
773 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
776 /* Handle the -retain-symbols-file option. */
779 add_keepsyms_file (filename
)
780 const char *filename
;
787 if (link_info
.strip
== strip_some
)
788 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
790 file
= fopen (filename
, "r");
791 if (file
== (FILE *) NULL
)
793 bfd_set_error (bfd_error_system_call
);
794 einfo ("%X%P: %s: %E\n", filename
);
798 link_info
.keep_hash
= ((struct bfd_hash_table
*)
799 xmalloc (sizeof (struct bfd_hash_table
)));
800 if (! bfd_hash_table_init (link_info
.keep_hash
, bfd_hash_newfunc
))
801 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
804 buf
= (char *) xmalloc (bufsize
);
816 while (! ISSPACE (c
) && c
!= EOF
)
823 buf
= xrealloc (buf
, bufsize
);
830 if (bfd_hash_lookup (link_info
.keep_hash
, buf
, TRUE
, TRUE
)
831 == (struct bfd_hash_entry
*) NULL
)
832 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
836 if (link_info
.strip
!= strip_none
)
837 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
840 link_info
.strip
= strip_some
;
843 /* Callbacks from the BFD linker routines. */
845 /* This is called when BFD has decided to include an archive member in
849 add_archive_element (info
, abfd
, name
)
850 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
854 lang_input_statement_type
*input
;
856 input
= ((lang_input_statement_type
*)
857 xmalloc (sizeof (lang_input_statement_type
)));
858 input
->filename
= abfd
->filename
;
859 input
->local_sym_name
= abfd
->filename
;
860 input
->the_bfd
= abfd
;
861 input
->asymbols
= NULL
;
863 input
->just_syms_flag
= FALSE
;
864 input
->loaded
= FALSE
;
865 input
->search_dirs_flag
= FALSE
;
867 /* FIXME: The following fields are not set: header.next,
868 header.type, closed, passive_position, symbol_count,
869 next_real_file, is_archive, target, real. This bit of code is
870 from the old decode_library_subfile function. I don't know
871 whether any of those fields matters. */
873 ldlang_add_file (input
);
875 if (config
.map_file
!= (FILE *) NULL
)
877 static bfd_boolean header_printed
;
878 struct bfd_link_hash_entry
*h
;
882 h
= bfd_link_hash_lookup (link_info
.hash
, name
, FALSE
, FALSE
, TRUE
);
894 case bfd_link_hash_defined
:
895 case bfd_link_hash_defweak
:
896 from
= h
->u
.def
.section
->owner
;
899 case bfd_link_hash_undefined
:
900 case bfd_link_hash_undefweak
:
901 from
= h
->u
.undef
.abfd
;
904 case bfd_link_hash_common
:
905 from
= h
->u
.c
.p
->section
->owner
;
910 if (! header_printed
)
914 sprintf (buf
, _("Archive member included because of file (symbol)\n\n"));
916 header_printed
= TRUE
;
919 if (bfd_my_archive (abfd
) == NULL
)
921 minfo ("%s", bfd_get_filename (abfd
));
922 len
= strlen (bfd_get_filename (abfd
));
926 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd
)),
927 bfd_get_filename (abfd
));
928 len
= (strlen (bfd_get_filename (bfd_my_archive (abfd
)))
929 + strlen (bfd_get_filename (abfd
))
947 minfo ("(%T)\n", h
->root
.string
);
949 minfo ("(%s)\n", name
);
952 if (trace_files
|| trace_file_tries
)
953 info_msg ("%I\n", input
);
958 /* This is called when BFD has discovered a symbol which is defined
962 multiple_definition (info
, name
, obfd
, osec
, oval
, nbfd
, nsec
, nval
)
963 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
972 /* If either section has the output_section field set to
973 bfd_abs_section_ptr, it means that the section is being
974 discarded, and this is not really a multiple definition at all.
975 FIXME: It would be cleaner to somehow ignore symbols defined in
976 sections which are being discarded. */
977 if ((osec
->output_section
!= NULL
978 && ! bfd_is_abs_section (osec
)
979 && bfd_is_abs_section (osec
->output_section
))
980 || (nsec
->output_section
!= NULL
981 && ! bfd_is_abs_section (nsec
)
982 && bfd_is_abs_section (nsec
->output_section
)))
985 einfo (_("%X%C: multiple definition of `%T'\n"),
986 nbfd
, nsec
, nval
, name
);
987 if (obfd
!= (bfd
*) NULL
)
988 einfo (_("%D: first defined here\n"), obfd
, osec
, oval
);
990 if (command_line
.relax
)
992 einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
993 command_line
.relax
= 0;
999 /* This is called when there is a definition of a common symbol, or
1000 when a common symbol is found for a symbol that is already defined,
1001 or when two common symbols are found. We only do something if
1002 -warn-common was used. */
1005 multiple_common (info
, name
, obfd
, otype
, osize
, nbfd
, ntype
, nsize
)
1006 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1009 enum bfd_link_hash_type otype
;
1012 enum bfd_link_hash_type ntype
;
1015 if (! config
.warn_common
)
1018 if (ntype
== bfd_link_hash_defined
1019 || ntype
== bfd_link_hash_defweak
1020 || ntype
== bfd_link_hash_indirect
)
1022 ASSERT (otype
== bfd_link_hash_common
);
1023 einfo (_("%B: warning: definition of `%T' overriding common\n"),
1026 einfo (_("%B: warning: common is here\n"), obfd
);
1028 else if (otype
== bfd_link_hash_defined
1029 || otype
== bfd_link_hash_defweak
1030 || otype
== bfd_link_hash_indirect
)
1032 ASSERT (ntype
== bfd_link_hash_common
);
1033 einfo (_("%B: warning: common of `%T' overridden by definition\n"),
1036 einfo (_("%B: warning: defined here\n"), obfd
);
1040 ASSERT (otype
== bfd_link_hash_common
&& ntype
== bfd_link_hash_common
);
1043 einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
1046 einfo (_("%B: warning: larger common is here\n"), obfd
);
1048 else if (nsize
> osize
)
1050 einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
1053 einfo (_("%B: warning: smaller common is here\n"), obfd
);
1057 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd
, name
);
1059 einfo (_("%B: warning: previous common is here\n"), obfd
);
1066 /* This is called when BFD has discovered a set element. H is the
1067 entry in the linker hash table for the set. SECTION and VALUE
1068 represent a value which should be added to the set. */
1071 add_to_set (info
, h
, reloc
, abfd
, section
, value
)
1072 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1073 struct bfd_link_hash_entry
*h
;
1074 bfd_reloc_code_real_type reloc
;
1079 if (config
.warn_constructors
)
1080 einfo (_("%P: warning: global constructor %s used\n"),
1083 if (! config
.build_constructors
)
1086 ldctor_add_set_entry (h
, reloc
, (const char *) NULL
, section
, value
);
1088 if (h
->type
== bfd_link_hash_new
)
1090 h
->type
= bfd_link_hash_undefined
;
1091 h
->u
.undef
.abfd
= abfd
;
1092 /* We don't call bfd_link_add_undef to add this to the list of
1093 undefined symbols because we are going to define it
1100 /* This is called when BFD has discovered a constructor. This is only
1101 called for some object file formats--those which do not handle
1102 constructors in some more clever fashion. This is similar to
1103 adding an element to a set, but less general. */
1106 constructor_callback (info
, constructor
, name
, abfd
, section
, value
)
1107 struct bfd_link_info
*info
;
1108 bfd_boolean constructor
;
1115 struct bfd_link_hash_entry
*h
;
1116 char set_name
[1 + sizeof "__CTOR_LIST__"];
1118 if (config
.warn_constructors
)
1119 einfo (_("%P: warning: global constructor %s used\n"), name
);
1121 if (! config
.build_constructors
)
1124 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1125 useful error message. */
1126 if (bfd_reloc_type_lookup (output_bfd
, BFD_RELOC_CTOR
) == NULL
1127 && (link_info
.relocateable
1128 || bfd_reloc_type_lookup (abfd
, BFD_RELOC_CTOR
) == NULL
))
1129 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1132 if (bfd_get_symbol_leading_char (abfd
) != '\0')
1133 *s
++ = bfd_get_symbol_leading_char (abfd
);
1135 strcpy (s
, "__CTOR_LIST__");
1137 strcpy (s
, "__DTOR_LIST__");
1139 h
= bfd_link_hash_lookup (info
->hash
, set_name
, TRUE
, TRUE
, TRUE
);
1140 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1141 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1142 if (h
->type
== bfd_link_hash_new
)
1144 h
->type
= bfd_link_hash_undefined
;
1145 h
->u
.undef
.abfd
= abfd
;
1146 /* We don't call bfd_link_add_undef to add this to the list of
1147 undefined symbols because we are going to define it
1151 ldctor_add_set_entry (h
, BFD_RELOC_CTOR
, name
, section
, value
);
1155 /* A structure used by warning_callback to pass information through
1156 bfd_map_over_sections. */
1158 struct warning_callback_info
1161 const char *warning
;
1166 /* This is called when there is a reference to a warning symbol. */
1169 warning_callback (info
, warning
, symbol
, abfd
, section
, address
)
1170 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1171 const char *warning
;
1177 /* This is a hack to support warn_multiple_gp. FIXME: This should
1178 have a cleaner interface, but what? */
1179 if (! config
.warn_multiple_gp
1180 && strcmp (warning
, "using multiple gp values") == 0)
1183 if (section
!= NULL
)
1184 einfo ("%C: %s\n", abfd
, section
, address
, warning
);
1185 else if (abfd
== NULL
)
1186 einfo ("%P: %s\n", warning
);
1187 else if (symbol
== NULL
)
1188 einfo ("%B: %s\n", abfd
, warning
);
1191 lang_input_statement_type
*entry
;
1193 struct warning_callback_info info
;
1195 /* Look through the relocs to see if we can find a plausible
1197 entry
= (lang_input_statement_type
*) abfd
->usrdata
;
1198 if (entry
!= NULL
&& entry
->asymbols
!= NULL
)
1199 asymbols
= entry
->asymbols
;
1205 symsize
= bfd_get_symtab_upper_bound (abfd
);
1207 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
1208 asymbols
= (asymbol
**) xmalloc (symsize
);
1209 symbol_count
= bfd_canonicalize_symtab (abfd
, asymbols
);
1210 if (symbol_count
< 0)
1211 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
1214 entry
->asymbols
= asymbols
;
1215 entry
->symbol_count
= symbol_count
;
1220 info
.warning
= warning
;
1221 info
.symbol
= symbol
;
1222 info
.asymbols
= asymbols
;
1223 bfd_map_over_sections (abfd
, warning_find_reloc
, (PTR
) &info
);
1226 einfo ("%B: %s\n", abfd
, warning
);
1235 /* This is called by warning_callback for each section. It checks the
1236 relocs of the section to see if it can find a reference to the
1237 symbol which triggered the warning. If it can, it uses the reloc
1238 to give an error message with a file and line number. */
1241 warning_find_reloc (abfd
, sec
, iarg
)
1246 struct warning_callback_info
*info
= (struct warning_callback_info
*) iarg
;
1250 arelent
**p
, **pend
;
1255 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1257 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
1261 relpp
= (arelent
**) xmalloc (relsize
);
1262 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, info
->asymbols
);
1264 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
1267 pend
= p
+ relcount
;
1268 for (; p
< pend
&& *p
!= NULL
; p
++)
1272 if (q
->sym_ptr_ptr
!= NULL
1273 && *q
->sym_ptr_ptr
!= NULL
1274 && strcmp (bfd_asymbol_name (*q
->sym_ptr_ptr
), info
->symbol
) == 0)
1276 /* We found a reloc for the symbol we are looking for. */
1277 einfo ("%C: %s\n", abfd
, sec
, q
->address
, info
->warning
);
1286 /* This is called when an undefined symbol is found. */
1289 undefined_symbol (info
, name
, abfd
, section
, address
, fatal
)
1290 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1295 bfd_boolean fatal ATTRIBUTE_UNUSED
;
1297 static char *error_name
;
1298 static unsigned int error_count
;
1300 #define MAX_ERRORS_IN_A_ROW 5
1302 if (config
.warn_once
)
1304 static struct bfd_hash_table
*hash
;
1306 /* Only warn once about a particular undefined symbol. */
1309 hash
= ((struct bfd_hash_table
*)
1310 xmalloc (sizeof (struct bfd_hash_table
)));
1311 if (! bfd_hash_table_init (hash
, bfd_hash_newfunc
))
1312 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
1315 if (bfd_hash_lookup (hash
, name
, FALSE
, FALSE
) != NULL
)
1318 if (bfd_hash_lookup (hash
, name
, TRUE
, TRUE
) == NULL
)
1319 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
1322 /* We never print more than a reasonable number of errors in a row
1323 for a single symbol. */
1324 if (error_name
!= (char *) NULL
1325 && strcmp (name
, error_name
) == 0)
1330 if (error_name
!= (char *) NULL
)
1332 error_name
= xstrdup (name
);
1335 if (section
!= NULL
)
1337 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1339 einfo (_("%C: undefined reference to `%T'\n"),
1340 abfd
, section
, address
, name
);
1344 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1345 einfo (_("%D: more undefined references to `%T' follow\n"),
1346 abfd
, section
, address
, name
);
1350 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1352 einfo (_("%B: undefined reference to `%T'\n"),
1357 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1358 einfo (_("%B: more undefined references to `%T' follow\n"),
1365 /* Counter to limit the number of relocation overflow error messages
1366 to print. Errors are printed as it is decremented. When it's
1367 called and the counter is zero, a final message is printed
1368 indicating more relocations were omitted. When it gets to -1, no
1369 such errors are printed. If it's initially set to a value less
1370 than -1, all such errors will be printed (--verbose does this). */
1372 int overflow_cutoff_limit
= 10;
1374 /* This is called when a reloc overflows. */
1377 reloc_overflow (info
, name
, reloc_name
, addend
, abfd
, section
, address
)
1378 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1380 const char *reloc_name
;
1386 if (overflow_cutoff_limit
== -1)
1389 if (abfd
== (bfd
*) NULL
)
1390 einfo (_("%P%X: generated"));
1392 einfo ("%X%C:", abfd
, section
, address
);
1394 if (overflow_cutoff_limit
>= 0
1395 && overflow_cutoff_limit
-- == 0)
1397 einfo (_(" additional relocation overflows omitted from the output\n"));
1401 einfo (_(" relocation truncated to fit: %s %T"), reloc_name
, name
);
1403 einfo ("+%v", addend
);
1408 /* This is called when a dangerous relocation is made. */
1411 reloc_dangerous (info
, message
, abfd
, section
, address
)
1412 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1413 const char *message
;
1418 if (abfd
== (bfd
*) NULL
)
1419 einfo (_("%P%X: generated"));
1421 einfo ("%X%C:", abfd
, section
, address
);
1422 einfo (_("dangerous relocation: %s\n"), message
);
1426 /* This is called when a reloc is being generated attached to a symbol
1427 that is not being output. */
1430 unattached_reloc (info
, name
, abfd
, section
, address
)
1431 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1437 if (abfd
== (bfd
*) NULL
)
1438 einfo (_("%P%X: generated"));
1440 einfo ("%X%C:", abfd
, section
, address
);
1441 einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name
);
1445 /* This is called if link_info.notice_all is set, or when a symbol in
1446 link_info.notice_hash is found. Symbols are put in notice_hash
1447 using the -y option. */
1450 notice (info
, name
, abfd
, section
, value
)
1451 struct bfd_link_info
*info
;
1457 if (! info
->notice_all
1458 || (info
->notice_hash
!= NULL
1459 && bfd_hash_lookup (info
->notice_hash
, name
, FALSE
, FALSE
) != NULL
))
1461 if (bfd_is_und_section (section
))
1462 einfo ("%B: reference to %s\n", abfd
, name
);
1464 einfo ("%B: definition of %s\n", abfd
, name
);
1467 if (command_line
.cref
|| nocrossref_list
!= NULL
)
1468 add_cref (name
, abfd
, section
, value
);