1 /* Main program of GNU linker.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 1997, 1998 Free Software Foundation, Inc.
3 Written by Steve Chamberlain steve@cygnus.com
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include "libiberty.h"
42 /* Somewhere above, sys/stat.h got included . . . . */
43 #if !defined(S_ISDIR) && defined(S_IFDIR)
44 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
50 #ifdef NEED_DECLARATION_SBRK
55 static char *get_emulation
PARAMS ((int, char **));
56 static void set_scripts_dir
PARAMS ((void));
61 const char *output_filename
= "a.out";
63 /* Name this program was invoked by. */
66 /* The file that we're creating */
69 /* Set by -G argument, for MIPS ECOFF target. */
70 int g_switch_value
= 8;
72 /* Nonzero means print names of input files as processed. */
75 /* Nonzero means same, but note open failures, too. */
76 boolean trace_file_tries
;
78 /* Nonzero means version number was printed, so exit successfully
79 instead of complaining if no input files are given. */
80 boolean version_printed
;
82 /* Nonzero means link in every member of an archive. */
83 boolean whole_archive
;
85 args_type command_line
;
87 ld_config_type config
;
89 static void remove_output
PARAMS ((void));
90 static boolean check_for_scripts_dir
PARAMS ((char *dir
));
91 static boolean add_archive_element
PARAMS ((struct bfd_link_info
*, bfd
*,
93 static boolean multiple_definition
PARAMS ((struct bfd_link_info
*,
95 bfd
*, asection
*, bfd_vma
,
96 bfd
*, asection
*, bfd_vma
));
97 static boolean multiple_common
PARAMS ((struct bfd_link_info
*,
99 enum bfd_link_hash_type
, bfd_vma
,
100 bfd
*, enum bfd_link_hash_type
,
102 static boolean add_to_set
PARAMS ((struct bfd_link_info
*,
103 struct bfd_link_hash_entry
*,
104 bfd_reloc_code_real_type
,
105 bfd
*, asection
*, bfd_vma
));
106 static boolean constructor_callback
PARAMS ((struct bfd_link_info
*,
109 bfd
*, asection
*, bfd_vma
));
110 static boolean warning_callback
PARAMS ((struct bfd_link_info
*,
111 const char *, const char *, bfd
*,
112 asection
*, bfd_vma
));
113 static void warning_find_reloc
PARAMS ((bfd
*, asection
*, PTR
));
114 static boolean undefined_symbol
PARAMS ((struct bfd_link_info
*,
116 asection
*, bfd_vma
));
117 static boolean reloc_overflow
PARAMS ((struct bfd_link_info
*, const char *,
118 const char *, bfd_vma
,
119 bfd
*, asection
*, bfd_vma
));
120 static boolean reloc_dangerous
PARAMS ((struct bfd_link_info
*, const char *,
121 bfd
*, asection
*, bfd_vma
));
122 static boolean unattached_reloc
PARAMS ((struct bfd_link_info
*,
123 const char *, bfd
*, asection
*,
125 static boolean notice
PARAMS ((struct bfd_link_info
*, const char *,
126 bfd
*, asection
*, bfd_vma
));
128 static struct bfd_link_callbacks link_callbacks
=
134 constructor_callback
,
143 struct bfd_link_info link_info
;
150 if (output_bfd
&& output_bfd
->iostream
)
151 fclose((FILE *)(output_bfd
->iostream
));
152 if (delete_output_file_on_failure
)
153 unlink (output_filename
);
163 long start_time
= get_run_time ();
165 #ifdef HAVE_SETLOCALE
166 setlocale (LC_MESSAGES
, "");
168 bindtextdomain (PACKAGE
, LOCALEDIR
);
169 textdomain (PACKAGE
);
171 program_name
= argv
[0];
172 xmalloc_set_program_name (program_name
);
174 START_PROGRESS (program_name
, 0);
178 bfd_set_error_program_name (program_name
);
180 xatexit (remove_output
);
182 /* Set the default BFD target based on the configured target. Doing
183 this permits the linker to be configured for a particular target,
184 and linked against a shared BFD library which was configured for
185 a different target. The macro TARGET is defined by Makefile. */
186 if (! bfd_set_default_target (TARGET
))
188 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET
);
192 /* Initialize the data about options. */
193 trace_files
= trace_file_tries
= version_printed
= false;
194 whole_archive
= false;
195 config
.build_constructors
= true;
196 config
.dynamic_link
= false;
197 config
.has_shared
= false;
198 command_line
.force_common_definition
= false;
199 command_line
.interpreter
= NULL
;
200 command_line
.rpath
= NULL
;
201 command_line
.warn_mismatch
= true;
202 command_line
.check_section_addresses
= true;
204 link_info
.callbacks
= &link_callbacks
;
205 link_info
.relocateable
= false;
206 link_info
.shared
= false;
207 link_info
.symbolic
= false;
208 link_info
.static_link
= false;
209 link_info
.traditional_format
= false;
210 link_info
.optimize
= false;
211 link_info
.strip
= strip_none
;
212 link_info
.discard
= discard_none
;
213 link_info
.keep_memory
= true;
214 link_info
.input_bfds
= NULL
;
215 link_info
.create_object_symbols_section
= NULL
;
216 link_info
.hash
= NULL
;
217 link_info
.keep_hash
= NULL
;
218 link_info
.notice_all
= false;
219 link_info
.notice_hash
= NULL
;
220 link_info
.wrap_hash
= NULL
;
222 ldfile_add_arch ("");
224 config
.make_executable
= true;
225 force_make_executable
= false;
226 config
.magic_demand_paged
= true;
227 config
.text_read_only
= true;
228 config
.make_executable
= true;
230 emulation
= get_emulation (argc
, argv
);
231 ldemul_choose_mode (emulation
);
232 default_target
= ldemul_choose_target ();
234 ldemul_before_parse ();
235 lang_has_input_file
= false;
236 parse_args (argc
, argv
);
238 ldemul_set_symbols ();
240 if (link_info
.relocateable
)
242 if (command_line
.gc_sections
)
243 einfo ("%P%F: --gc-sections and -r may not be used together\n");
244 if (command_line
.relax
)
245 einfo (_("%P%F: --relax and -r may not be used together\n"));
246 if (link_info
.shared
)
247 einfo (_("%P%F: -r and -shared may not be used together\n"));
250 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
251 don't see how else this can be handled, since in this case we
252 must preserve all externally visible symbols. */
253 if (link_info
.relocateable
&& link_info
.strip
== strip_all
)
255 link_info
.strip
= strip_debugger
;
256 if (link_info
.discard
== discard_none
)
257 link_info
.discard
= discard_all
;
260 /* This essentially adds another -L directory so this must be done after
261 the -L's in argv have been processed. */
264 if (had_script
== false)
266 /* Read the emulation's appropriate default script. */
268 char *s
= ldemul_get_script (&isfile
);
271 ldfile_open_command_file (s
);
274 if (trace_file_tries
)
276 info_msg (_("using internal linker script:\n"));
277 info_msg ("==================================================\n");
279 info_msg ("\n==================================================\n");
284 parser_input
= input_script
;
291 if (lang_has_input_file
== false)
295 einfo (_("%P%F: no input files\n"));
300 info_msg (_("%P: mode %s\n"), emulation
);
303 ldemul_after_parse ();
306 if (config
.map_filename
)
308 if (strcmp (config
.map_filename
, "-") == 0)
310 config
.map_file
= stdout
;
314 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
315 if (config
.map_file
== (FILE *) NULL
)
317 bfd_set_error (bfd_error_system_call
);
318 einfo (_("%P%F: cannot open map file %s: %E\n"),
319 config
.map_filename
);
327 /* Print error messages for any missing symbols, for any warning
328 symbols, and possibly multiple definitions */
331 if (config
.text_read_only
)
333 /* Look for a text section and mark the readonly attribute in it */
334 asection
*found
= bfd_get_section_by_name (output_bfd
, ".text");
336 if (found
!= (asection
*) NULL
)
338 found
->flags
|= SEC_READONLY
;
342 if (link_info
.relocateable
)
343 output_bfd
->flags
&= ~EXEC_P
;
345 output_bfd
->flags
|= EXEC_P
;
349 if (config
.map_file
!= NULL
)
351 if (command_line
.cref
)
352 output_cref (config
.map_file
!= NULL
? config
.map_file
: stdout
);
353 if (nocrossref_list
!= NULL
)
354 check_nocrossrefs ();
356 /* Even if we're producing relocateable output, some non-fatal errors should
357 be reported in the exit status. (What non-fatal errors, if any, do we
358 want to ignore for relocateable output?) */
360 if (config
.make_executable
== false && force_make_executable
== false)
362 if (trace_files
== true)
364 einfo (_("%P: link errors found, deleting executable `%s'\n"),
368 /* The file will be removed by remove_output. */
374 if (! bfd_close (output_bfd
))
375 einfo (_("%F%B: final close failed: %E\n"), output_bfd
);
377 /* If the --force-exe-suffix is enabled, and we're making an
378 executable file and it doesn't end in .exe, copy it to one which does. */
380 if (! link_info
.relocateable
&& command_line
.force_exe_suffix
)
382 int len
= strlen (output_filename
);
384 || (strcasecmp (output_filename
+ len
- 4, ".exe") != 0
385 && strcasecmp (output_filename
+ len
- 4, ".dll") != 0))
389 const int bsize
= 4096;
390 char *buf
= xmalloc (bsize
);
392 char *dst_name
= xmalloc (len
+ 5);
393 strcpy (dst_name
, output_filename
);
394 strcat (dst_name
, ".exe");
395 src
= fopen (output_filename
, FOPEN_RB
);
396 dst
= fopen (dst_name
, FOPEN_WB
);
399 einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename
);
401 einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name
);
402 while ((l
= fread (buf
, 1, bsize
, src
)) > 0)
404 int done
= fwrite (buf
, 1, l
, dst
);
407 einfo (_("%P: Error writing file `%s'\n"), dst_name
);
411 if (fclose (dst
) == EOF
)
413 einfo (_("%P: Error closing file `%s'\n"), dst_name
);
421 END_PROGRESS (program_name
);
425 extern char **environ
;
427 char *lim
= (char *) sbrk (0);
429 long run_time
= get_run_time () - start_time
;
431 fprintf (stderr
, _("%s: total time in link: %ld.%06ld\n"),
432 program_name
, run_time
/ 1000000, run_time
% 1000000);
434 fprintf (stderr
, _("%s: data size %ld\n"), program_name
,
435 (long) (lim
- (char *) &environ
));
439 /* Prevent remove_output from doing anything, after a successful link. */
440 output_filename
= NULL
;
446 /* We need to find any explicitly given emulation in order to initialize the
447 state that's needed by the lex&yacc argument parser (parse_args). */
450 get_emulation (argc
, argv
)
457 emulation
= getenv (EMULATION_ENVIRON
);
458 if (emulation
== NULL
)
459 emulation
= DEFAULT_EMULATION
;
461 for (i
= 1; i
< argc
; i
++)
463 if (!strncmp (argv
[i
], "-m", 2))
465 if (argv
[i
][2] == '\0')
470 emulation
= argv
[i
+ 1];
475 einfo(_("%P%F: missing argument to -m\n"));
478 else if (strcmp (argv
[i
], "-mips1") == 0
479 || strcmp (argv
[i
], "-mips2") == 0
480 || strcmp (argv
[i
], "-mips3") == 0
481 || strcmp (argv
[i
], "-mips4") == 0)
483 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
484 passed to the linker by some MIPS compilers. They
485 generally tell the linker to use a slightly different
486 library path. Perhaps someday these should be
487 implemented as emulations; until then, we just ignore
488 the arguments and hope that nobody ever creates
489 emulations named ips1, ips2 or ips3. */
491 else if (strcmp (argv
[i
], "-m486") == 0)
493 /* FIXME: The argument -m486 is passed to the linker on
494 some Linux systems. Hope that nobody creates an
495 emulation named 486. */
500 emulation
= &argv
[i
][2];
508 /* If directory DIR contains an "ldscripts" subdirectory,
509 add DIR to the library search path and return true,
510 else return false. */
513 check_for_scripts_dir (dir
)
521 dirlen
= strlen (dir
);
522 /* sizeof counts the terminating NUL. */
523 buf
= (char *) xmalloc (dirlen
+ sizeof("/ldscripts"));
524 sprintf (buf
, "%s/ldscripts", dir
);
526 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
529 ldfile_add_library_path (dir
, false);
533 /* Set the default directory for finding script files.
534 Libraries will be searched for here too, but that's ok.
535 We look for the "ldscripts" directory in:
537 SCRIPTDIR (passed from Makefile)
538 the dir where this program is (for using it from the build tree)
539 the dir where this program is/../lib (for installing the tool suite elsewhere) */
547 if (check_for_scripts_dir (SCRIPTDIR
))
548 return; /* We've been installed normally. */
550 /* Look for "ldscripts" in the dir where our binary is. */
551 end
= strrchr (program_name
, '/');
555 /* Don't look for ldscripts in the current directory. There is
556 too much potential for confusion. */
560 dirlen
= end
- program_name
;
561 /* Make a copy of program_name in dir.
562 Leave room for later "/../lib". */
563 dir
= (char *) xmalloc (dirlen
+ 8);
564 strncpy (dir
, program_name
, dirlen
);
567 if (check_for_scripts_dir (dir
))
568 return; /* Don't free dir. */
570 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
571 strcpy (dir
+ dirlen
, "/../lib");
572 if (check_for_scripts_dir (dir
))
575 free (dir
); /* Well, we tried. */
582 if (link_info
.notice_hash
== (struct bfd_hash_table
*) NULL
)
584 link_info
.notice_hash
= ((struct bfd_hash_table
*)
585 xmalloc (sizeof (struct bfd_hash_table
)));
586 if (! bfd_hash_table_init_n (link_info
.notice_hash
,
589 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
592 if (bfd_hash_lookup (link_info
.notice_hash
, name
, true, true)
593 == (struct bfd_hash_entry
*) NULL
)
594 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
597 /* Record a symbol to be wrapped, from the --wrap option. */
603 if (link_info
.wrap_hash
== NULL
)
605 link_info
.wrap_hash
= ((struct bfd_hash_table
*)
606 xmalloc (sizeof (struct bfd_hash_table
)));
607 if (! bfd_hash_table_init_n (link_info
.wrap_hash
,
610 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
612 if (bfd_hash_lookup (link_info
.wrap_hash
, name
, true, true) == NULL
)
613 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
616 /* Handle the -retain-symbols-file option. */
619 add_keepsyms_file (filename
)
620 const char *filename
;
627 if (link_info
.strip
== strip_some
)
628 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
630 file
= fopen (filename
, "r");
631 if (file
== (FILE *) NULL
)
633 bfd_set_error (bfd_error_system_call
);
634 einfo ("%X%P: %s: %E\n", filename
);
638 link_info
.keep_hash
= ((struct bfd_hash_table
*)
639 xmalloc (sizeof (struct bfd_hash_table
)));
640 if (! bfd_hash_table_init (link_info
.keep_hash
, bfd_hash_newfunc
))
641 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
644 buf
= (char *) xmalloc (bufsize
);
656 while (! isspace (c
) && c
!= EOF
)
663 buf
= xrealloc (buf
, bufsize
);
670 if (bfd_hash_lookup (link_info
.keep_hash
, buf
, true, true)
671 == (struct bfd_hash_entry
*) NULL
)
672 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
676 if (link_info
.strip
!= strip_none
)
677 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
679 link_info
.strip
= strip_some
;
682 /* Callbacks from the BFD linker routines. */
684 /* This is called when BFD has decided to include an archive member in
689 add_archive_element (info
, abfd
, name
)
690 struct bfd_link_info
*info
;
694 lang_input_statement_type
*input
;
696 input
= ((lang_input_statement_type
*)
697 xmalloc (sizeof (lang_input_statement_type
)));
698 input
->filename
= abfd
->filename
;
699 input
->local_sym_name
= abfd
->filename
;
700 input
->the_bfd
= abfd
;
701 input
->asymbols
= NULL
;
703 input
->just_syms_flag
= false;
704 input
->loaded
= false;
705 input
->search_dirs_flag
= false;
707 /* FIXME: The following fields are not set: header.next,
708 header.type, closed, passive_position, symbol_count,
709 next_real_file, is_archive, target, real. This bit of code is
710 from the old decode_library_subfile function. I don't know
711 whether any of those fields matters. */
713 ldlang_add_file (input
);
715 if (config
.map_file
!= (FILE *) NULL
)
717 static boolean header_printed
;
718 struct bfd_link_hash_entry
*h
;
722 h
= bfd_link_hash_lookup (link_info
.hash
, name
, false, false, true);
734 case bfd_link_hash_defined
:
735 case bfd_link_hash_defweak
:
736 from
= h
->u
.def
.section
->owner
;
739 case bfd_link_hash_undefined
:
740 case bfd_link_hash_undefweak
:
741 from
= h
->u
.undef
.abfd
;
744 case bfd_link_hash_common
:
745 from
= h
->u
.c
.p
->section
->owner
;
750 if (! header_printed
)
754 sprintf (buf
, "%-29s %s\n\n", _("Archive member included"),
755 _("because of file (symbol)"));
757 header_printed
= true;
760 if (bfd_my_archive (abfd
) == NULL
)
762 minfo ("%s", bfd_get_filename (abfd
));
763 len
= strlen (bfd_get_filename (abfd
));
767 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd
)),
768 bfd_get_filename (abfd
));
769 len
= (strlen (bfd_get_filename (bfd_my_archive (abfd
)))
770 + strlen (bfd_get_filename (abfd
))
788 minfo ("(%T)\n", h
->root
.string
);
790 minfo ("(%s)\n", name
);
793 if (trace_files
|| trace_file_tries
)
794 info_msg ("%I\n", input
);
799 /* This is called when BFD has discovered a symbol which is defined
804 multiple_definition (info
, name
, obfd
, osec
, oval
, nbfd
, nsec
, nval
)
805 struct bfd_link_info
*info
;
814 /* If either section has the output_section field set to
815 bfd_abs_section_ptr, it means that the section is being
816 discarded, and this is not really a multiple definition at all.
817 FIXME: It would be cleaner to somehow ignore symbols defined in
818 sections which are being discarded. */
819 if ((osec
->output_section
!= NULL
820 && ! bfd_is_abs_section (osec
)
821 && bfd_is_abs_section (osec
->output_section
))
822 || (nsec
->output_section
!= NULL
823 && ! bfd_is_abs_section (nsec
)
824 && bfd_is_abs_section (nsec
->output_section
)))
827 einfo (_("%X%C: multiple definition of `%T'\n"),
828 nbfd
, nsec
, nval
, name
);
829 if (obfd
!= (bfd
*) NULL
)
830 einfo (_("%D: first defined here\n"), obfd
, osec
, oval
);
834 /* This is called when there is a definition of a common symbol, or
835 when a common symbol is found for a symbol that is already defined,
836 or when two common symbols are found. We only do something if
837 -warn-common was used. */
841 multiple_common (info
, name
, obfd
, otype
, osize
, nbfd
, ntype
, nsize
)
842 struct bfd_link_info
*info
;
845 enum bfd_link_hash_type otype
;
848 enum bfd_link_hash_type ntype
;
851 if (! config
.warn_common
)
854 if (ntype
== bfd_link_hash_defined
855 || ntype
== bfd_link_hash_defweak
856 || ntype
== bfd_link_hash_indirect
)
858 ASSERT (otype
== bfd_link_hash_common
);
859 einfo (_("%B: warning: definition of `%T' overriding common\n"),
862 einfo (_("%B: warning: common is here\n"), obfd
);
864 else if (otype
== bfd_link_hash_defined
865 || otype
== bfd_link_hash_defweak
866 || otype
== bfd_link_hash_indirect
)
868 ASSERT (ntype
== bfd_link_hash_common
);
869 einfo (_("%B: warning: common of `%T' overridden by definition\n"),
872 einfo (_("%B: warning: defined here\n"), obfd
);
876 ASSERT (otype
== bfd_link_hash_common
&& ntype
== bfd_link_hash_common
);
879 einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
882 einfo (_("%B: warning: larger common is here\n"), obfd
);
884 else if (nsize
> osize
)
886 einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
889 einfo (_("%B: warning: smaller common is here\n"), obfd
);
893 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd
, name
);
895 einfo (_("%B: warning: previous common is here\n"), obfd
);
902 /* This is called when BFD has discovered a set element. H is the
903 entry in the linker hash table for the set. SECTION and VALUE
904 represent a value which should be added to the set. */
908 add_to_set (info
, h
, reloc
, abfd
, section
, value
)
909 struct bfd_link_info
*info
;
910 struct bfd_link_hash_entry
*h
;
911 bfd_reloc_code_real_type reloc
;
916 if (config
.warn_constructors
)
917 einfo (_("%P: warning: global constructor %s used\n"),
920 if (! config
.build_constructors
)
923 ldctor_add_set_entry (h
, reloc
, (const char *) NULL
, section
, value
);
925 if (h
->type
== bfd_link_hash_new
)
927 h
->type
= bfd_link_hash_undefined
;
928 h
->u
.undef
.abfd
= abfd
;
929 /* We don't call bfd_link_add_undef to add this to the list of
930 undefined symbols because we are going to define it
937 /* This is called when BFD has discovered a constructor. This is only
938 called for some object file formats--those which do not handle
939 constructors in some more clever fashion. This is similar to
940 adding an element to a set, but less general. */
943 constructor_callback (info
, constructor
, name
, abfd
, section
, value
)
944 struct bfd_link_info
*info
;
952 struct bfd_link_hash_entry
*h
;
953 char set_name
[1 + sizeof "__CTOR_LIST__"];
955 if (config
.warn_constructors
)
956 einfo (_("%P: warning: global constructor %s used\n"), name
);
958 if (! config
.build_constructors
)
961 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
962 useful error message. */
963 if (bfd_reloc_type_lookup (output_bfd
, BFD_RELOC_CTOR
) == NULL
964 && (link_info
.relocateable
965 || bfd_reloc_type_lookup (abfd
, BFD_RELOC_CTOR
) == NULL
))
966 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
969 if (bfd_get_symbol_leading_char (abfd
) != '\0')
970 *s
++ = bfd_get_symbol_leading_char (abfd
);
972 strcpy (s
, "__CTOR_LIST__");
974 strcpy (s
, "__DTOR_LIST__");
976 h
= bfd_link_hash_lookup (info
->hash
, set_name
, true, true, true);
977 if (h
== (struct bfd_link_hash_entry
*) NULL
)
978 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
979 if (h
->type
== bfd_link_hash_new
)
981 h
->type
= bfd_link_hash_undefined
;
982 h
->u
.undef
.abfd
= abfd
;
983 /* We don't call bfd_link_add_undef to add this to the list of
984 undefined symbols because we are going to define it
988 ldctor_add_set_entry (h
, BFD_RELOC_CTOR
, name
, section
, value
);
992 /* A structure used by warning_callback to pass information through
993 bfd_map_over_sections. */
995 struct warning_callback_info
1003 /* This is called when there is a reference to a warning symbol. */
1007 warning_callback (info
, warning
, symbol
, abfd
, section
, address
)
1008 struct bfd_link_info
*info
;
1009 const char *warning
;
1015 /* This is a hack to support warn_multiple_gp. FIXME: This should
1016 have a cleaner interface, but what? */
1017 if (! config
.warn_multiple_gp
1018 && strcmp (warning
, "using multiple gp values") == 0)
1021 if (section
!= NULL
)
1022 einfo ("%C: %s\n", abfd
, section
, address
, warning
);
1023 else if (abfd
== NULL
)
1024 einfo ("%P: %s\n", warning
);
1025 else if (symbol
== NULL
)
1026 einfo ("%B: %s\n", abfd
, warning
);
1029 lang_input_statement_type
*entry
;
1031 struct warning_callback_info info
;
1033 /* Look through the relocs to see if we can find a plausible
1036 entry
= (lang_input_statement_type
*) abfd
->usrdata
;
1037 if (entry
!= NULL
&& entry
->asymbols
!= NULL
)
1038 asymbols
= entry
->asymbols
;
1044 symsize
= bfd_get_symtab_upper_bound (abfd
);
1046 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
1047 asymbols
= (asymbol
**) xmalloc (symsize
);
1048 symbol_count
= bfd_canonicalize_symtab (abfd
, asymbols
);
1049 if (symbol_count
< 0)
1050 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
1053 entry
->asymbols
= asymbols
;
1054 entry
->symbol_count
= symbol_count
;
1059 info
.warning
= warning
;
1060 info
.symbol
= symbol
;
1061 info
.asymbols
= asymbols
;
1062 bfd_map_over_sections (abfd
, warning_find_reloc
, (PTR
) &info
);
1065 einfo ("%B: %s\n", abfd
, warning
);
1074 /* This is called by warning_callback for each section. It checks the
1075 relocs of the section to see if it can find a reference to the
1076 symbol which triggered the warning. If it can, it uses the reloc
1077 to give an error message with a file and line number. */
1080 warning_find_reloc (abfd
, sec
, iarg
)
1085 struct warning_callback_info
*info
= (struct warning_callback_info
*) iarg
;
1089 arelent
**p
, **pend
;
1094 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1096 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
1100 relpp
= (arelent
**) xmalloc (relsize
);
1101 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, info
->asymbols
);
1103 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
1106 pend
= p
+ relcount
;
1107 for (; p
< pend
&& *p
!= NULL
; p
++)
1111 if (q
->sym_ptr_ptr
!= NULL
1112 && *q
->sym_ptr_ptr
!= NULL
1113 && strcmp (bfd_asymbol_name (*q
->sym_ptr_ptr
), info
->symbol
) == 0)
1115 /* We found a reloc for the symbol we are looking for. */
1116 einfo ("%C: %s\n", abfd
, sec
, q
->address
, info
->warning
);
1125 /* This is called when an undefined symbol is found. */
1129 undefined_symbol (info
, name
, abfd
, section
, address
)
1130 struct bfd_link_info
*info
;
1136 static char *error_name
;
1137 static unsigned int error_count
;
1139 #define MAX_ERRORS_IN_A_ROW 5
1141 if (config
.warn_once
)
1143 static struct bfd_hash_table
*hash
;
1145 /* Only warn once about a particular undefined symbol. */
1149 hash
= ((struct bfd_hash_table
*)
1150 xmalloc (sizeof (struct bfd_hash_table
)));
1151 if (! bfd_hash_table_init (hash
, bfd_hash_newfunc
))
1152 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
1155 if (bfd_hash_lookup (hash
, name
, false, false) != NULL
)
1158 if (bfd_hash_lookup (hash
, name
, true, true) == NULL
)
1159 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
1162 /* We never print more than a reasonable number of errors in a row
1163 for a single symbol. */
1164 if (error_name
!= (char *) NULL
1165 && strcmp (name
, error_name
) == 0)
1170 if (error_name
!= (char *) NULL
)
1172 error_name
= buystring (name
);
1175 if (section
!= NULL
)
1177 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1178 einfo (_("%X%C: undefined reference to `%T'\n"),
1179 abfd
, section
, address
, name
);
1180 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1181 einfo (_("%D: more undefined references to `%T' follow\n"),
1182 abfd
, section
, address
, name
);
1186 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1187 einfo (_("%X%B: undefined reference to `%T'\n"),
1189 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1190 einfo (_("%B: more undefined references to `%T' follow\n"),
1197 /* This is called when a reloc overflows. */
1201 reloc_overflow (info
, name
, reloc_name
, addend
, abfd
, section
, address
)
1202 struct bfd_link_info
*info
;
1204 const char *reloc_name
;
1210 if (abfd
== (bfd
*) NULL
)
1211 einfo (_("%P%X: generated"));
1213 einfo ("%X%C:", abfd
, section
, address
);
1214 einfo (_(" relocation truncated to fit: %s %T"), reloc_name
, name
);
1216 einfo ("+%v", addend
);
1221 /* This is called when a dangerous relocation is made. */
1225 reloc_dangerous (info
, message
, abfd
, section
, address
)
1226 struct bfd_link_info
*info
;
1227 const char *message
;
1232 if (abfd
== (bfd
*) NULL
)
1233 einfo (_("%P%X: generated"));
1235 einfo ("%X%C:", abfd
, section
, address
);
1236 einfo (_("dangerous relocation: %s\n"), message
);
1240 /* This is called when a reloc is being generated attached to a symbol
1241 that is not being output. */
1245 unattached_reloc (info
, name
, abfd
, section
, address
)
1246 struct bfd_link_info
*info
;
1252 if (abfd
== (bfd
*) NULL
)
1253 einfo (_("%P%X: generated"));
1255 einfo ("%X%C:", abfd
, section
, address
);
1256 einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name
);
1260 /* This is called if link_info.notice_all is set, or when a symbol in
1261 link_info.notice_hash is found. Symbols are put in notice_hash
1262 using the -y option. */
1265 notice (info
, name
, abfd
, section
, value
)
1266 struct bfd_link_info
*info
;
1272 if (! info
->notice_all
1273 || (info
->notice_hash
!= NULL
1274 && bfd_hash_lookup (info
->notice_hash
, name
, false, false) != NULL
))
1276 if (bfd_is_und_section (section
))
1277 einfo ("%B: reference to %s\n", abfd
, name
);
1279 einfo ("%B: definition of %s\n", abfd
, name
);
1282 if (command_line
.cref
|| nocrossref_list
!= NULL
)
1283 add_cref (name
, abfd
, section
, value
);