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 file that we're creating. */
78 /* Set by -G argument, for MIPS ECOFF target. */
79 int g_switch_value
= 8;
81 /* Nonzero means print names of input files as processed. */
82 bfd_boolean trace_files
;
84 /* Nonzero means same, but note open failures, too. */
85 bfd_boolean trace_file_tries
;
87 /* Nonzero means version number was printed, so exit successfully
88 instead of complaining if no input files are given. */
89 bfd_boolean version_printed
;
91 /* Nonzero means link in every member of an archive. */
92 bfd_boolean whole_archive
;
94 /* TRUE if we should demangle symbol names. */
95 bfd_boolean demangling
;
97 args_type command_line
;
99 ld_config_type config
;
101 static char *get_emulation
102 PARAMS ((int, char **));
103 static void set_scripts_dir
105 static void remove_output
107 static bfd_boolean check_for_scripts_dir
109 static bfd_boolean add_archive_element
110 PARAMS ((struct bfd_link_info
*, bfd
*, const char *));
111 static bfd_boolean multiple_definition
112 PARAMS ((struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
,
113 bfd
*, asection
*, bfd_vma
));
114 static bfd_boolean multiple_common
115 PARAMS ((struct bfd_link_info
*, const char *, bfd
*,
116 enum bfd_link_hash_type
, bfd_vma
, bfd
*, enum bfd_link_hash_type
,
118 static bfd_boolean add_to_set
119 PARAMS ((struct bfd_link_info
*, struct bfd_link_hash_entry
*,
120 bfd_reloc_code_real_type
, bfd
*, asection
*, bfd_vma
));
121 static bfd_boolean constructor_callback
122 PARAMS ((struct bfd_link_info
*, bfd_boolean
, const char *, bfd
*,
123 asection
*, bfd_vma
));
124 static bfd_boolean warning_callback
125 PARAMS ((struct bfd_link_info
*, const char *, const char *, bfd
*,
126 asection
*, bfd_vma
));
127 static void warning_find_reloc
128 PARAMS ((bfd
*, asection
*, PTR
));
129 static bfd_boolean undefined_symbol
130 PARAMS ((struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
,
132 static bfd_boolean reloc_overflow
133 PARAMS ((struct bfd_link_info
*, const char *, const char *, bfd_vma
,
134 bfd
*, asection
*, bfd_vma
));
135 static bfd_boolean reloc_dangerous
136 PARAMS ((struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
));
137 static bfd_boolean unattached_reloc
138 PARAMS ((struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
));
139 static bfd_boolean notice
140 PARAMS ((struct bfd_link_info
*, const char *, bfd
*, asection
*, bfd_vma
));
142 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
,
206 int res
= stat (ld_sysroot
, &s
) == 0 && S_ISDIR (s
.st_mode
);
217 ld_sysroot
= make_relative_prefix (program_name
, TOOLBINDIR
,
223 int res
= stat (ld_sysroot
, &s
) == 0 && S_ISDIR (s
.st_mode
);
235 ld_sysroot
= TARGET_SYSTEM_ROOT
;
237 /* Set the default BFD target based on the configured target. Doing
238 this permits the linker to be configured for a particular target,
239 and linked against a shared BFD library which was configured for
240 a different target. The macro TARGET is defined by Makefile. */
241 if (! bfd_set_default_target (TARGET
))
243 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET
);
254 /* Initialize the data about options. */
255 trace_files
= trace_file_tries
= version_printed
= FALSE
;
256 whole_archive
= FALSE
;
257 config
.build_constructors
= TRUE
;
258 config
.dynamic_link
= FALSE
;
259 config
.has_shared
= FALSE
;
260 config
.split_by_reloc
= (unsigned) -1;
261 config
.split_by_file
= (bfd_size_type
) -1;
262 command_line
.force_common_definition
= FALSE
;
263 command_line
.inhibit_common_definition
= FALSE
;
264 command_line
.interpreter
= NULL
;
265 command_line
.rpath
= NULL
;
266 command_line
.warn_mismatch
= TRUE
;
267 command_line
.check_section_addresses
= TRUE
;
268 command_line
.accept_unknown_input_arch
= FALSE
;
270 /* We initialize DEMANGLING based on the environment variable
271 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
272 output of the linker, unless COLLECT_NO_DEMANGLE is set in the
273 environment. Acting the same way here lets us provide the same
274 interface by default. */
275 demangling
= getenv ("COLLECT_NO_DEMANGLE") == NULL
;
277 link_info
.relocateable
= FALSE
;
278 link_info
.emitrelocations
= FALSE
;
279 link_info
.task_link
= FALSE
;
280 link_info
.shared
= FALSE
;
281 link_info
.symbolic
= FALSE
;
282 link_info
.export_dynamic
= FALSE
;
283 link_info
.static_link
= FALSE
;
284 link_info
.traditional_format
= FALSE
;
285 link_info
.optimize
= FALSE
;
286 link_info
.no_undefined
= FALSE
;
287 link_info
.allow_shlib_undefined
= TRUE
;
288 link_info
.allow_multiple_definition
= FALSE
;
289 link_info
.allow_undefined_version
= TRUE
;
290 link_info
.keep_memory
= TRUE
;
291 link_info
.notice_all
= FALSE
;
292 link_info
.nocopyreloc
= FALSE
;
293 link_info
.new_dtags
= FALSE
;
294 link_info
.combreloc
= TRUE
;
295 link_info
.eh_frame_hdr
= FALSE
;
296 link_info
.strip_discarded
= TRUE
;
297 link_info
.strip
= strip_none
;
298 link_info
.discard
= discard_sec_merge
;
299 link_info
.common_skip_ar_aymbols
= bfd_link_common_skip_none
;
300 link_info
.callbacks
= &link_callbacks
;
301 link_info
.hash
= NULL
;
302 link_info
.keep_hash
= NULL
;
303 link_info
.notice_hash
= NULL
;
304 link_info
.wrap_hash
= NULL
;
305 link_info
.input_bfds
= NULL
;
306 link_info
.create_object_symbols_section
= NULL
;
307 link_info
.gc_sym_list
= NULL
;
308 link_info
.base_file
= NULL
;
309 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
310 and _fini symbols. We are compatible. */
311 link_info
.init_function
= "_init";
312 link_info
.fini_function
= "_fini";
313 link_info
.mpc860c0
= 0;
314 link_info
.pei386_auto_import
= -1;
315 link_info
.pei386_runtime_pseudo_reloc
= FALSE
;
316 link_info
.spare_dynamic_tags
= 5;
317 link_info
.flags
= (bfd_vma
) 0;
318 link_info
.flags_1
= (bfd_vma
) 0;
320 ldfile_add_arch ("");
322 config
.make_executable
= TRUE
;
323 force_make_executable
= FALSE
;
324 config
.magic_demand_paged
= TRUE
;
325 config
.text_read_only
= TRUE
;
327 emulation
= get_emulation (argc
, argv
);
328 ldemul_choose_mode (emulation
);
329 default_target
= ldemul_choose_target (argc
, argv
);
331 ldemul_before_parse ();
332 lang_has_input_file
= FALSE
;
333 parse_args (argc
, argv
);
335 ldemul_set_symbols ();
337 if (link_info
.relocateable
)
339 if (command_line
.gc_sections
)
340 einfo ("%P%F: --gc-sections and -r may not be used together\n");
341 if (link_info
.mpc860c0
)
342 einfo (_("%P%F: -r and --mpc860c0 may not be used together\n"));
343 else if (command_line
.relax
)
344 einfo (_("%P%F: --relax and -r may not be used together\n"));
345 if (link_info
.shared
)
346 einfo (_("%P%F: -r and -shared may not be used together\n"));
349 if (! link_info
.shared
)
351 if (command_line
.filter_shlib
)
352 einfo (_("%P%F: -F may not be used without -shared\n"));
353 if (command_line
.auxiliary_filters
)
354 einfo (_("%P%F: -f may not be used without -shared\n"));
357 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
358 don't see how else this can be handled, since in this case we
359 must preserve all externally visible symbols. */
360 if (link_info
.relocateable
&& link_info
.strip
== strip_all
)
362 link_info
.strip
= strip_debugger
;
363 if (link_info
.discard
== discard_sec_merge
)
364 link_info
.discard
= discard_all
;
367 /* This essentially adds another -L directory so this must be done after
368 the -L's in argv have been processed. */
371 /* If we have not already opened and parsed a linker script
372 read the emulation's appropriate default script. */
373 if (saved_script_handle
== NULL
)
376 char *s
= ldemul_get_script (&isfile
);
379 ldfile_open_command_file (s
);
385 parser_input
= input_script
;
390 if (trace_file_tries
)
392 if (saved_script_handle
)
393 info_msg (_("using external linker script:"));
395 info_msg (_("using internal linker script:"));
396 info_msg ("\n==================================================\n");
398 if (saved_script_handle
)
400 static const int ld_bufsz
= 8193;
402 char *buf
= xmalloc (ld_bufsz
);
404 rewind (saved_script_handle
);
405 while ((n
= fread (buf
, 1, ld_bufsz
- 1, saved_script_handle
)) > 0)
410 rewind (saved_script_handle
);
417 info_msg (ldemul_get_script (&isfile
));
420 info_msg ("\n==================================================\n");
425 if (!lang_has_input_file
)
429 einfo (_("%P%F: no input files\n"));
433 info_msg (_("%P: mode %s\n"), emulation
);
435 ldemul_after_parse ();
437 if (config
.map_filename
)
439 if (strcmp (config
.map_filename
, "-") == 0)
441 config
.map_file
= stdout
;
445 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
446 if (config
.map_file
== (FILE *) NULL
)
448 bfd_set_error (bfd_error_system_call
);
449 einfo (_("%P%F: cannot open map file %s: %E\n"),
450 config
.map_filename
);
457 /* Print error messages for any missing symbols, for any warning
458 symbols, and possibly multiple definitions. */
459 if (link_info
.relocateable
)
460 output_bfd
->flags
&= ~EXEC_P
;
462 output_bfd
->flags
|= EXEC_P
;
466 if (config
.map_file
!= NULL
)
468 if (command_line
.cref
)
469 output_cref (config
.map_file
!= NULL
? config
.map_file
: stdout
);
470 if (nocrossref_list
!= NULL
)
471 check_nocrossrefs ();
473 /* Even if we're producing relocateable output, some non-fatal errors should
474 be reported in the exit status. (What non-fatal errors, if any, do we
475 want to ignore for relocateable output?) */
476 if (!config
.make_executable
&& !force_make_executable
)
479 einfo (_("%P: link errors found, deleting executable `%s'\n"),
482 /* The file will be removed by remove_output. */
487 if (! bfd_close (output_bfd
))
488 einfo (_("%F%B: final close failed: %E\n"), output_bfd
);
490 /* If the --force-exe-suffix is enabled, and we're making an
491 executable file and it doesn't end in .exe, copy it to one
493 if (! link_info
.relocateable
&& command_line
.force_exe_suffix
)
495 int len
= strlen (output_filename
);
498 || (strcasecmp (output_filename
+ len
- 4, ".exe") != 0
499 && strcasecmp (output_filename
+ len
- 4, ".dll") != 0))
503 const int bsize
= 4096;
504 char *buf
= xmalloc (bsize
);
506 char *dst_name
= xmalloc (len
+ 5);
508 strcpy (dst_name
, output_filename
);
509 strcat (dst_name
, ".exe");
510 src
= fopen (output_filename
, FOPEN_RB
);
511 dst
= fopen (dst_name
, FOPEN_WB
);
514 einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename
);
516 einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name
);
517 while ((l
= fread (buf
, 1, bsize
, src
)) > 0)
519 int done
= fwrite (buf
, 1, l
, dst
);
522 einfo (_("%P: Error writing file `%s'\n"), dst_name
);
526 if (fclose (dst
) == EOF
)
527 einfo (_("%P: Error closing file `%s'\n"), dst_name
);
534 END_PROGRESS (program_name
);
539 char *lim
= (char *) sbrk (0);
541 long run_time
= get_run_time () - start_time
;
543 fprintf (stderr
, _("%s: total time in link: %ld.%06ld\n"),
544 program_name
, run_time
/ 1000000, run_time
% 1000000);
546 fprintf (stderr
, _("%s: data size %ld\n"), program_name
,
547 (long) (lim
- (char *) &environ
));
551 /* Prevent remove_output from doing anything, after a successful link. */
552 output_filename
= NULL
;
558 /* We need to find any explicitly given emulation in order to initialize the
559 state that's needed by the lex&yacc argument parser (parse_args). */
562 get_emulation (argc
, argv
)
569 emulation
= getenv (EMULATION_ENVIRON
);
570 if (emulation
== NULL
)
571 emulation
= DEFAULT_EMULATION
;
573 for (i
= 1; i
< argc
; i
++)
575 if (!strncmp (argv
[i
], "-m", 2))
577 if (argv
[i
][2] == '\0')
582 emulation
= argv
[i
+ 1];
586 einfo (_("%P%F: missing argument to -m\n"));
588 else if (strcmp (argv
[i
], "-mips1") == 0
589 || strcmp (argv
[i
], "-mips2") == 0
590 || strcmp (argv
[i
], "-mips3") == 0
591 || strcmp (argv
[i
], "-mips4") == 0
592 || strcmp (argv
[i
], "-mips5") == 0
593 || strcmp (argv
[i
], "-mips32") == 0
594 || strcmp (argv
[i
], "-mips32r2") == 0
595 || strcmp (argv
[i
], "-mips64") == 0)
597 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
598 passed to the linker by some MIPS compilers. They
599 generally tell the linker to use a slightly different
600 library path. Perhaps someday these should be
601 implemented as emulations; until then, we just ignore
602 the arguments and hope that nobody ever creates
603 emulations named ips1, ips2 or ips3. */
605 else if (strcmp (argv
[i
], "-m486") == 0)
607 /* FIXME: The argument -m486 is passed to the linker on
608 some Linux systems. Hope that nobody creates an
609 emulation named 486. */
614 emulation
= &argv
[i
][2];
622 /* If directory DIR contains an "ldscripts" subdirectory,
623 add DIR to the library search path and return TRUE,
624 else return FALSE. */
627 check_for_scripts_dir (dir
)
635 dirlen
= strlen (dir
);
636 /* sizeof counts the terminating NUL. */
637 buf
= (char *) xmalloc (dirlen
+ sizeof ("/ldscripts"));
638 sprintf (buf
, "%s/ldscripts", dir
);
640 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
643 ldfile_add_library_path (dir
, FALSE
);
647 /* Set the default directory for finding script files.
648 Libraries will be searched for here too, but that's ok.
649 We look for the "ldscripts" directory in:
651 SCRIPTDIR (passed from Makefile)
652 (adjusted according to the current location of the binary)
653 SCRIPTDIR (passed from Makefile)
654 the dir where this program is (for using it from the build tree)
655 the dir where this program is/../lib
656 (for installing the tool suite elsewhere). */
664 dir
= make_relative_prefix (program_name
, BINDIR
, SCRIPTDIR
);
665 if (dir
&& check_for_scripts_dir (dir
))
666 /* Success. Don't free dir. */
672 if (check_for_scripts_dir (SCRIPTDIR
))
673 /* We've been installed normally. */
676 /* Look for "ldscripts" in the dir where our binary is. */
677 end
= strrchr (program_name
, '/');
678 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
680 /* We could have \foo\bar, or /foo\bar. */
681 char *bslash
= strrchr (program_name
, '\\');
683 if (end
== NULL
|| (bslash
!= NULL
&& bslash
> end
))
689 /* Don't look for ldscripts in the current directory. There is
690 too much potential for confusion. */
693 dirlen
= end
- program_name
;
694 /* Make a copy of program_name in dir.
695 Leave room for later "/../lib". */
696 dir
= (char *) xmalloc (dirlen
+ 8);
697 strncpy (dir
, program_name
, dirlen
);
700 if (check_for_scripts_dir (dir
))
701 /* Don't free dir. */
704 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
705 strcpy (dir
+ dirlen
, "/../lib");
706 if (check_for_scripts_dir (dir
))
709 /* Well, we tried. */
717 if (link_info
.notice_hash
== (struct bfd_hash_table
*) NULL
)
719 link_info
.notice_hash
= ((struct bfd_hash_table
*)
720 xmalloc (sizeof (struct bfd_hash_table
)));
721 if (! bfd_hash_table_init_n (link_info
.notice_hash
,
724 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
727 if (bfd_hash_lookup (link_info
.notice_hash
, name
, TRUE
, TRUE
)
728 == (struct bfd_hash_entry
*) NULL
)
729 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
732 /* Record a symbol to be wrapped, from the --wrap option. */
738 if (link_info
.wrap_hash
== NULL
)
740 link_info
.wrap_hash
= ((struct bfd_hash_table
*)
741 xmalloc (sizeof (struct bfd_hash_table
)));
742 if (! bfd_hash_table_init_n (link_info
.wrap_hash
,
745 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
748 if (bfd_hash_lookup (link_info
.wrap_hash
, name
, TRUE
, TRUE
) == NULL
)
749 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
752 /* Handle the -retain-symbols-file option. */
755 add_keepsyms_file (filename
)
756 const char *filename
;
763 if (link_info
.strip
== strip_some
)
764 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
766 file
= fopen (filename
, "r");
767 if (file
== (FILE *) NULL
)
769 bfd_set_error (bfd_error_system_call
);
770 einfo ("%X%P: %s: %E\n", filename
);
774 link_info
.keep_hash
= ((struct bfd_hash_table
*)
775 xmalloc (sizeof (struct bfd_hash_table
)));
776 if (! bfd_hash_table_init (link_info
.keep_hash
, bfd_hash_newfunc
))
777 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
780 buf
= (char *) xmalloc (bufsize
);
792 while (! ISSPACE (c
) && c
!= EOF
)
799 buf
= xrealloc (buf
, bufsize
);
806 if (bfd_hash_lookup (link_info
.keep_hash
, buf
, TRUE
, TRUE
)
807 == (struct bfd_hash_entry
*) NULL
)
808 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
812 if (link_info
.strip
!= strip_none
)
813 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
815 link_info
.strip
= strip_some
;
818 /* Callbacks from the BFD linker routines. */
820 /* This is called when BFD has decided to include an archive member in
824 add_archive_element (info
, abfd
, name
)
825 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
829 lang_input_statement_type
*input
;
831 input
= ((lang_input_statement_type
*)
832 xmalloc (sizeof (lang_input_statement_type
)));
833 input
->filename
= abfd
->filename
;
834 input
->local_sym_name
= abfd
->filename
;
835 input
->the_bfd
= abfd
;
836 input
->asymbols
= NULL
;
838 input
->just_syms_flag
= FALSE
;
839 input
->loaded
= FALSE
;
840 input
->search_dirs_flag
= FALSE
;
842 /* FIXME: The following fields are not set: header.next,
843 header.type, closed, passive_position, symbol_count,
844 next_real_file, is_archive, target, real. This bit of code is
845 from the old decode_library_subfile function. I don't know
846 whether any of those fields matters. */
848 ldlang_add_file (input
);
850 if (config
.map_file
!= (FILE *) NULL
)
852 static bfd_boolean header_printed
;
853 struct bfd_link_hash_entry
*h
;
857 h
= bfd_link_hash_lookup (link_info
.hash
, name
, FALSE
, FALSE
, TRUE
);
869 case bfd_link_hash_defined
:
870 case bfd_link_hash_defweak
:
871 from
= h
->u
.def
.section
->owner
;
874 case bfd_link_hash_undefined
:
875 case bfd_link_hash_undefweak
:
876 from
= h
->u
.undef
.abfd
;
879 case bfd_link_hash_common
:
880 from
= h
->u
.c
.p
->section
->owner
;
885 if (! header_printed
)
889 sprintf (buf
, _("Archive member included because of file (symbol)\n\n"));
891 header_printed
= TRUE
;
894 if (bfd_my_archive (abfd
) == NULL
)
896 minfo ("%s", bfd_get_filename (abfd
));
897 len
= strlen (bfd_get_filename (abfd
));
901 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd
)),
902 bfd_get_filename (abfd
));
903 len
= (strlen (bfd_get_filename (bfd_my_archive (abfd
)))
904 + strlen (bfd_get_filename (abfd
))
922 minfo ("(%T)\n", h
->root
.string
);
924 minfo ("(%s)\n", name
);
927 if (trace_files
|| trace_file_tries
)
928 info_msg ("%I\n", input
);
933 /* This is called when BFD has discovered a symbol which is defined
937 multiple_definition (info
, name
, obfd
, osec
, oval
, nbfd
, nsec
, nval
)
938 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
947 /* If either section has the output_section field set to
948 bfd_abs_section_ptr, it means that the section is being
949 discarded, and this is not really a multiple definition at all.
950 FIXME: It would be cleaner to somehow ignore symbols defined in
951 sections which are being discarded. */
952 if ((osec
->output_section
!= NULL
953 && ! bfd_is_abs_section (osec
)
954 && bfd_is_abs_section (osec
->output_section
))
955 || (nsec
->output_section
!= NULL
956 && ! bfd_is_abs_section (nsec
)
957 && bfd_is_abs_section (nsec
->output_section
)))
960 einfo (_("%X%C: multiple definition of `%T'\n"),
961 nbfd
, nsec
, nval
, name
);
962 if (obfd
!= (bfd
*) NULL
)
963 einfo (_("%D: first defined here\n"), obfd
, osec
, oval
);
965 if (command_line
.relax
)
967 einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
968 command_line
.relax
= 0;
974 /* This is called when there is a definition of a common symbol, or
975 when a common symbol is found for a symbol that is already defined,
976 or when two common symbols are found. We only do something if
977 -warn-common was used. */
980 multiple_common (info
, name
, obfd
, otype
, osize
, nbfd
, ntype
, nsize
)
981 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
984 enum bfd_link_hash_type otype
;
987 enum bfd_link_hash_type ntype
;
990 if (! config
.warn_common
)
993 if (ntype
== bfd_link_hash_defined
994 || ntype
== bfd_link_hash_defweak
995 || ntype
== bfd_link_hash_indirect
)
997 ASSERT (otype
== bfd_link_hash_common
);
998 einfo (_("%B: warning: definition of `%T' overriding common\n"),
1001 einfo (_("%B: warning: common is here\n"), obfd
);
1003 else if (otype
== bfd_link_hash_defined
1004 || otype
== bfd_link_hash_defweak
1005 || otype
== bfd_link_hash_indirect
)
1007 ASSERT (ntype
== bfd_link_hash_common
);
1008 einfo (_("%B: warning: common of `%T' overridden by definition\n"),
1011 einfo (_("%B: warning: defined here\n"), obfd
);
1015 ASSERT (otype
== bfd_link_hash_common
&& ntype
== bfd_link_hash_common
);
1018 einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
1021 einfo (_("%B: warning: larger common is here\n"), obfd
);
1023 else if (nsize
> osize
)
1025 einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
1028 einfo (_("%B: warning: smaller common is here\n"), obfd
);
1032 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd
, name
);
1034 einfo (_("%B: warning: previous common is here\n"), obfd
);
1041 /* This is called when BFD has discovered a set element. H is the
1042 entry in the linker hash table for the set. SECTION and VALUE
1043 represent a value which should be added to the set. */
1046 add_to_set (info
, h
, reloc
, abfd
, section
, value
)
1047 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1048 struct bfd_link_hash_entry
*h
;
1049 bfd_reloc_code_real_type reloc
;
1054 if (config
.warn_constructors
)
1055 einfo (_("%P: warning: global constructor %s used\n"),
1058 if (! config
.build_constructors
)
1061 ldctor_add_set_entry (h
, reloc
, (const char *) NULL
, section
, value
);
1063 if (h
->type
== bfd_link_hash_new
)
1065 h
->type
= bfd_link_hash_undefined
;
1066 h
->u
.undef
.abfd
= abfd
;
1067 /* We don't call bfd_link_add_undef to add this to the list of
1068 undefined symbols because we are going to define it
1075 /* This is called when BFD has discovered a constructor. This is only
1076 called for some object file formats--those which do not handle
1077 constructors in some more clever fashion. This is similar to
1078 adding an element to a set, but less general. */
1081 constructor_callback (info
, constructor
, name
, abfd
, section
, value
)
1082 struct bfd_link_info
*info
;
1083 bfd_boolean constructor
;
1090 struct bfd_link_hash_entry
*h
;
1091 char set_name
[1 + sizeof "__CTOR_LIST__"];
1093 if (config
.warn_constructors
)
1094 einfo (_("%P: warning: global constructor %s used\n"), name
);
1096 if (! config
.build_constructors
)
1099 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1100 useful error message. */
1101 if (bfd_reloc_type_lookup (output_bfd
, BFD_RELOC_CTOR
) == NULL
1102 && (link_info
.relocateable
1103 || bfd_reloc_type_lookup (abfd
, BFD_RELOC_CTOR
) == NULL
))
1104 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1107 if (bfd_get_symbol_leading_char (abfd
) != '\0')
1108 *s
++ = bfd_get_symbol_leading_char (abfd
);
1110 strcpy (s
, "__CTOR_LIST__");
1112 strcpy (s
, "__DTOR_LIST__");
1114 h
= bfd_link_hash_lookup (info
->hash
, set_name
, TRUE
, TRUE
, TRUE
);
1115 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1116 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1117 if (h
->type
== bfd_link_hash_new
)
1119 h
->type
= bfd_link_hash_undefined
;
1120 h
->u
.undef
.abfd
= abfd
;
1121 /* We don't call bfd_link_add_undef to add this to the list of
1122 undefined symbols because we are going to define it
1126 ldctor_add_set_entry (h
, BFD_RELOC_CTOR
, name
, section
, value
);
1130 /* A structure used by warning_callback to pass information through
1131 bfd_map_over_sections. */
1133 struct warning_callback_info
1136 const char *warning
;
1141 /* This is called when there is a reference to a warning symbol. */
1144 warning_callback (info
, warning
, symbol
, abfd
, section
, address
)
1145 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1146 const char *warning
;
1152 /* This is a hack to support warn_multiple_gp. FIXME: This should
1153 have a cleaner interface, but what? */
1154 if (! config
.warn_multiple_gp
1155 && strcmp (warning
, "using multiple gp values") == 0)
1158 if (section
!= NULL
)
1159 einfo ("%C: %s\n", abfd
, section
, address
, warning
);
1160 else if (abfd
== NULL
)
1161 einfo ("%P: %s\n", warning
);
1162 else if (symbol
== NULL
)
1163 einfo ("%B: %s\n", abfd
, warning
);
1166 lang_input_statement_type
*entry
;
1168 struct warning_callback_info info
;
1170 /* Look through the relocs to see if we can find a plausible
1172 entry
= (lang_input_statement_type
*) abfd
->usrdata
;
1173 if (entry
!= NULL
&& entry
->asymbols
!= NULL
)
1174 asymbols
= entry
->asymbols
;
1180 symsize
= bfd_get_symtab_upper_bound (abfd
);
1182 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
1183 asymbols
= (asymbol
**) xmalloc (symsize
);
1184 symbol_count
= bfd_canonicalize_symtab (abfd
, asymbols
);
1185 if (symbol_count
< 0)
1186 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
1189 entry
->asymbols
= asymbols
;
1190 entry
->symbol_count
= symbol_count
;
1195 info
.warning
= warning
;
1196 info
.symbol
= symbol
;
1197 info
.asymbols
= asymbols
;
1198 bfd_map_over_sections (abfd
, warning_find_reloc
, (PTR
) &info
);
1201 einfo ("%B: %s\n", abfd
, warning
);
1210 /* This is called by warning_callback for each section. It checks the
1211 relocs of the section to see if it can find a reference to the
1212 symbol which triggered the warning. If it can, it uses the reloc
1213 to give an error message with a file and line number. */
1216 warning_find_reloc (abfd
, sec
, iarg
)
1221 struct warning_callback_info
*info
= (struct warning_callback_info
*) iarg
;
1225 arelent
**p
, **pend
;
1230 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1232 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
1236 relpp
= (arelent
**) xmalloc (relsize
);
1237 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, info
->asymbols
);
1239 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
1242 pend
= p
+ relcount
;
1243 for (; p
< pend
&& *p
!= NULL
; p
++)
1247 if (q
->sym_ptr_ptr
!= NULL
1248 && *q
->sym_ptr_ptr
!= NULL
1249 && strcmp (bfd_asymbol_name (*q
->sym_ptr_ptr
), info
->symbol
) == 0)
1251 /* We found a reloc for the symbol we are looking for. */
1252 einfo ("%C: %s\n", abfd
, sec
, q
->address
, info
->warning
);
1261 /* This is called when an undefined symbol is found. */
1264 undefined_symbol (info
, name
, abfd
, section
, address
, fatal
)
1265 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1270 bfd_boolean fatal ATTRIBUTE_UNUSED
;
1272 static char *error_name
;
1273 static unsigned int error_count
;
1275 #define MAX_ERRORS_IN_A_ROW 5
1277 if (config
.warn_once
)
1279 static struct bfd_hash_table
*hash
;
1281 /* Only warn once about a particular undefined symbol. */
1284 hash
= ((struct bfd_hash_table
*)
1285 xmalloc (sizeof (struct bfd_hash_table
)));
1286 if (! bfd_hash_table_init (hash
, bfd_hash_newfunc
))
1287 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
1290 if (bfd_hash_lookup (hash
, name
, FALSE
, FALSE
) != NULL
)
1293 if (bfd_hash_lookup (hash
, name
, TRUE
, TRUE
) == NULL
)
1294 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
1297 /* We never print more than a reasonable number of errors in a row
1298 for a single symbol. */
1299 if (error_name
!= (char *) NULL
1300 && strcmp (name
, error_name
) == 0)
1305 if (error_name
!= (char *) NULL
)
1307 error_name
= xstrdup (name
);
1310 if (section
!= NULL
)
1312 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1314 einfo (_("%C: undefined reference to `%T'\n"),
1315 abfd
, section
, address
, name
);
1319 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1320 einfo (_("%D: more undefined references to `%T' follow\n"),
1321 abfd
, section
, address
, name
);
1325 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1327 einfo (_("%B: undefined reference to `%T'\n"),
1332 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1333 einfo (_("%B: more undefined references to `%T' follow\n"),
1340 /* This is called when a reloc overflows. */
1343 reloc_overflow (info
, name
, reloc_name
, addend
, abfd
, section
, address
)
1344 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1346 const char *reloc_name
;
1352 if (abfd
== (bfd
*) NULL
)
1353 einfo (_("%P%X: generated"));
1355 einfo ("%X%C:", abfd
, section
, address
);
1356 einfo (_(" relocation truncated to fit: %s %T"), reloc_name
, name
);
1358 einfo ("+%v", addend
);
1363 /* This is called when a dangerous relocation is made. */
1366 reloc_dangerous (info
, message
, abfd
, section
, address
)
1367 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1368 const char *message
;
1373 if (abfd
== (bfd
*) NULL
)
1374 einfo (_("%P%X: generated"));
1376 einfo ("%X%C:", abfd
, section
, address
);
1377 einfo (_("dangerous relocation: %s\n"), message
);
1381 /* This is called when a reloc is being generated attached to a symbol
1382 that is not being output. */
1385 unattached_reloc (info
, name
, abfd
, section
, address
)
1386 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1392 if (abfd
== (bfd
*) NULL
)
1393 einfo (_("%P%X: generated"));
1395 einfo ("%X%C:", abfd
, section
, address
);
1396 einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name
);
1400 /* This is called if link_info.notice_all is set, or when a symbol in
1401 link_info.notice_hash is found. Symbols are put in notice_hash
1402 using the -y option. */
1405 notice (info
, name
, abfd
, section
, value
)
1406 struct bfd_link_info
*info
;
1412 if (! info
->notice_all
1413 || (info
->notice_hash
!= NULL
1414 && bfd_hash_lookup (info
->notice_hash
, name
, FALSE
, FALSE
) != NULL
))
1416 if (bfd_is_und_section (section
))
1417 einfo ("%B: reference to %s\n", abfd
, name
);
1419 einfo ("%B: definition of %s\n", abfd
, name
);
1422 if (command_line
.cref
|| nocrossref_list
!= NULL
)
1423 add_cref (name
, abfd
, section
, value
);