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;
203 link_info
.callbacks
= &link_callbacks
;
204 link_info
.relocateable
= false;
205 link_info
.shared
= false;
206 link_info
.symbolic
= false;
207 link_info
.static_link
= false;
208 link_info
.traditional_format
= false;
209 link_info
.optimize
= false;
210 link_info
.strip
= strip_none
;
211 link_info
.discard
= discard_none
;
212 link_info
.keep_memory
= true;
213 link_info
.input_bfds
= NULL
;
214 link_info
.create_object_symbols_section
= NULL
;
215 link_info
.hash
= NULL
;
216 link_info
.keep_hash
= NULL
;
217 link_info
.notice_all
= false;
218 link_info
.notice_hash
= NULL
;
219 link_info
.wrap_hash
= NULL
;
221 ldfile_add_arch ("");
223 config
.make_executable
= true;
224 force_make_executable
= false;
225 config
.magic_demand_paged
= true;
226 config
.text_read_only
= true;
227 config
.make_executable
= true;
229 emulation
= get_emulation (argc
, argv
);
230 ldemul_choose_mode (emulation
);
231 default_target
= ldemul_choose_target ();
233 ldemul_before_parse ();
234 lang_has_input_file
= false;
235 parse_args (argc
, argv
);
237 ldemul_set_symbols ();
239 if (link_info
.relocateable
)
241 if (command_line
.gc_sections
)
242 einfo ("%P%F: --gc-sections and -r may not be used together\n");
243 if (command_line
.relax
)
244 einfo (_("%P%F: --relax and -r may not be used together\n"));
245 if (link_info
.shared
)
246 einfo (_("%P%F: -r and -shared may not be used together\n"));
249 if (command_line
.gc_sections
&& config
.dynamic_link
)
250 einfo("%P%F: --gc-sections may only be performed for static links\n");
252 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
253 don't see how else this can be handled, since in this case we
254 must preserve all externally visible symbols. */
255 if (link_info
.relocateable
&& link_info
.strip
== strip_all
)
257 link_info
.strip
= strip_debugger
;
258 if (link_info
.discard
== discard_none
)
259 link_info
.discard
= discard_all
;
262 /* This essentially adds another -L directory so this must be done after
263 the -L's in argv have been processed. */
266 if (had_script
== false)
268 /* Read the emulation's appropriate default script. */
270 char *s
= ldemul_get_script (&isfile
);
273 ldfile_open_command_file (s
);
276 if (trace_file_tries
)
278 info_msg (_("using internal linker script:\n"));
279 info_msg ("==================================================\n");
281 info_msg ("\n==================================================\n");
286 parser_input
= input_script
;
293 if (lang_has_input_file
== false)
297 einfo (_("%P%F: no input files\n"));
302 info_msg (_("%P: mode %s\n"), emulation
);
305 ldemul_after_parse ();
308 if (config
.map_filename
)
310 if (strcmp (config
.map_filename
, "-") == 0)
312 config
.map_file
= stdout
;
316 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
317 if (config
.map_file
== (FILE *) NULL
)
319 bfd_set_error (bfd_error_system_call
);
320 einfo (_("%P%F: cannot open map file %s: %E\n"),
321 config
.map_filename
);
329 /* Print error messages for any missing symbols, for any warning
330 symbols, and possibly multiple definitions */
333 if (config
.text_read_only
)
335 /* Look for a text section and mark the readonly attribute in it */
336 asection
*found
= bfd_get_section_by_name (output_bfd
, ".text");
338 if (found
!= (asection
*) NULL
)
340 found
->flags
|= SEC_READONLY
;
344 if (link_info
.relocateable
)
345 output_bfd
->flags
&= ~EXEC_P
;
347 output_bfd
->flags
|= EXEC_P
;
351 if (config
.map_file
!= NULL
)
353 if (command_line
.cref
)
354 output_cref (config
.map_file
!= NULL
? config
.map_file
: stdout
);
355 if (nocrossref_list
!= NULL
)
356 check_nocrossrefs ();
358 /* Even if we're producing relocateable output, some non-fatal errors should
359 be reported in the exit status. (What non-fatal errors, if any, do we
360 want to ignore for relocateable output?) */
362 if (config
.make_executable
== false && force_make_executable
== false)
364 if (trace_files
== true)
366 einfo (_("%P: link errors found, deleting executable `%s'\n"),
370 /* The file will be removed by remove_output. */
376 if (! bfd_close (output_bfd
))
377 einfo (_("%F%B: final close failed: %E\n"), output_bfd
);
379 /* If the --force-exe-suffix is enabled, and we're making an
380 executable file and it doesn't end in .exe, copy it to one which does. */
382 if (! link_info
.relocateable
&& command_line
.force_exe_suffix
)
384 int len
= strlen (output_filename
);
386 || (strcasecmp (output_filename
+ len
- 4, ".exe") != 0
387 && strcasecmp (output_filename
+ len
- 4, ".dll") != 0))
391 const int bsize
= 4096;
392 char *buf
= xmalloc (bsize
);
394 char *dst_name
= xmalloc (len
+ 5);
395 strcpy (dst_name
, output_filename
);
396 strcat (dst_name
, ".exe");
397 src
= fopen (output_filename
, FOPEN_RB
);
398 dst
= fopen (dst_name
, FOPEN_WB
);
401 einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename
);
403 einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name
);
404 while ((l
= fread (buf
, 1, bsize
, src
)) > 0)
406 int done
= fwrite (buf
, 1, l
, dst
);
409 einfo (_("%P: Error writing file `%s'\n"), dst_name
);
413 if (fclose (dst
) == EOF
)
415 einfo (_("%P: Error closing file `%s'\n"), dst_name
);
423 END_PROGRESS (program_name
);
427 extern char **environ
;
429 char *lim
= (char *) sbrk (0);
431 long run_time
= get_run_time () - start_time
;
433 fprintf (stderr
, _("%s: total time in link: %ld.%06ld\n"),
434 program_name
, run_time
/ 1000000, run_time
% 1000000);
436 fprintf (stderr
, _("%s: data size %ld\n"), program_name
,
437 (long) (lim
- (char *) &environ
));
441 /* Prevent remove_output from doing anything, after a successful link. */
442 output_filename
= NULL
;
448 /* We need to find any explicitly given emulation in order to initialize the
449 state that's needed by the lex&yacc argument parser (parse_args). */
452 get_emulation (argc
, argv
)
459 emulation
= getenv (EMULATION_ENVIRON
);
460 if (emulation
== NULL
)
461 emulation
= DEFAULT_EMULATION
;
463 for (i
= 1; i
< argc
; i
++)
465 if (!strncmp (argv
[i
], "-m", 2))
467 if (argv
[i
][2] == '\0')
472 emulation
= argv
[i
+ 1];
477 einfo(_("%P%F: missing argument to -m\n"));
480 else if (strcmp (argv
[i
], "-mips1") == 0
481 || strcmp (argv
[i
], "-mips2") == 0
482 || strcmp (argv
[i
], "-mips3") == 0
483 || strcmp (argv
[i
], "-mips4") == 0)
485 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
486 passed to the linker by some MIPS compilers. They
487 generally tell the linker to use a slightly different
488 library path. Perhaps someday these should be
489 implemented as emulations; until then, we just ignore
490 the arguments and hope that nobody ever creates
491 emulations named ips1, ips2 or ips3. */
493 else if (strcmp (argv
[i
], "-m486") == 0)
495 /* FIXME: The argument -m486 is passed to the linker on
496 some Linux systems. Hope that nobody creates an
497 emulation named 486. */
502 emulation
= &argv
[i
][2];
510 /* If directory DIR contains an "ldscripts" subdirectory,
511 add DIR to the library search path and return true,
512 else return false. */
515 check_for_scripts_dir (dir
)
523 dirlen
= strlen (dir
);
524 /* sizeof counts the terminating NUL. */
525 buf
= (char *) xmalloc (dirlen
+ sizeof("/ldscripts"));
526 sprintf (buf
, "%s/ldscripts", dir
);
528 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
531 ldfile_add_library_path (dir
, false);
535 /* Set the default directory for finding script files.
536 Libraries will be searched for here too, but that's ok.
537 We look for the "ldscripts" directory in:
539 SCRIPTDIR (passed from Makefile)
540 the dir where this program is (for using it from the build tree)
541 the dir where this program is/../lib (for installing the tool suite elsewhere) */
549 if (check_for_scripts_dir (SCRIPTDIR
))
550 return; /* We've been installed normally. */
552 /* Look for "ldscripts" in the dir where our binary is. */
553 end
= strrchr (program_name
, '/');
557 /* Don't look for ldscripts in the current directory. There is
558 too much potential for confusion. */
562 dirlen
= end
- program_name
;
563 /* Make a copy of program_name in dir.
564 Leave room for later "/../lib". */
565 dir
= (char *) xmalloc (dirlen
+ 8);
566 strncpy (dir
, program_name
, dirlen
);
569 if (check_for_scripts_dir (dir
))
570 return; /* Don't free dir. */
572 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
573 strcpy (dir
+ dirlen
, "/../lib");
574 if (check_for_scripts_dir (dir
))
577 free (dir
); /* Well, we tried. */
584 if (link_info
.notice_hash
== (struct bfd_hash_table
*) NULL
)
586 link_info
.notice_hash
= ((struct bfd_hash_table
*)
587 xmalloc (sizeof (struct bfd_hash_table
)));
588 if (! bfd_hash_table_init_n (link_info
.notice_hash
,
591 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
594 if (bfd_hash_lookup (link_info
.notice_hash
, name
, true, true)
595 == (struct bfd_hash_entry
*) NULL
)
596 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
599 /* Record a symbol to be wrapped, from the --wrap option. */
605 if (link_info
.wrap_hash
== NULL
)
607 link_info
.wrap_hash
= ((struct bfd_hash_table
*)
608 xmalloc (sizeof (struct bfd_hash_table
)));
609 if (! bfd_hash_table_init_n (link_info
.wrap_hash
,
612 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
614 if (bfd_hash_lookup (link_info
.wrap_hash
, name
, true, true) == NULL
)
615 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
618 /* Handle the -retain-symbols-file option. */
621 add_keepsyms_file (filename
)
622 const char *filename
;
629 if (link_info
.strip
== strip_some
)
630 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
632 file
= fopen (filename
, "r");
633 if (file
== (FILE *) NULL
)
635 bfd_set_error (bfd_error_system_call
);
636 einfo ("%X%P: %s: %E\n", filename
);
640 link_info
.keep_hash
= ((struct bfd_hash_table
*)
641 xmalloc (sizeof (struct bfd_hash_table
)));
642 if (! bfd_hash_table_init (link_info
.keep_hash
, bfd_hash_newfunc
))
643 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
646 buf
= (char *) xmalloc (bufsize
);
658 while (! isspace (c
) && c
!= EOF
)
665 buf
= xrealloc (buf
, bufsize
);
672 if (bfd_hash_lookup (link_info
.keep_hash
, buf
, true, true)
673 == (struct bfd_hash_entry
*) NULL
)
674 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
678 if (link_info
.strip
!= strip_none
)
679 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
681 link_info
.strip
= strip_some
;
684 /* Callbacks from the BFD linker routines. */
686 /* This is called when BFD has decided to include an archive member in
691 add_archive_element (info
, abfd
, name
)
692 struct bfd_link_info
*info
;
696 lang_input_statement_type
*input
;
698 input
= ((lang_input_statement_type
*)
699 xmalloc (sizeof (lang_input_statement_type
)));
700 input
->filename
= abfd
->filename
;
701 input
->local_sym_name
= abfd
->filename
;
702 input
->the_bfd
= abfd
;
703 input
->asymbols
= NULL
;
705 input
->just_syms_flag
= false;
706 input
->loaded
= false;
707 input
->search_dirs_flag
= false;
709 /* FIXME: The following fields are not set: header.next,
710 header.type, closed, passive_position, symbol_count,
711 next_real_file, is_archive, target, real. This bit of code is
712 from the old decode_library_subfile function. I don't know
713 whether any of those fields matters. */
715 ldlang_add_file (input
);
717 if (config
.map_file
!= (FILE *) NULL
)
719 static boolean header_printed
;
720 struct bfd_link_hash_entry
*h
;
724 h
= bfd_link_hash_lookup (link_info
.hash
, name
, false, false, true);
736 case bfd_link_hash_defined
:
737 case bfd_link_hash_defweak
:
738 from
= h
->u
.def
.section
->owner
;
741 case bfd_link_hash_undefined
:
742 case bfd_link_hash_undefweak
:
743 from
= h
->u
.undef
.abfd
;
746 case bfd_link_hash_common
:
747 from
= h
->u
.c
.p
->section
->owner
;
752 if (! header_printed
)
756 sprintf (buf
, "%-29s %s\n\n", _("Archive member included"),
757 _("because of file (symbol)"));
759 header_printed
= true;
762 if (bfd_my_archive (abfd
) == NULL
)
764 minfo ("%s", bfd_get_filename (abfd
));
765 len
= strlen (bfd_get_filename (abfd
));
769 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd
)),
770 bfd_get_filename (abfd
));
771 len
= (strlen (bfd_get_filename (bfd_my_archive (abfd
)))
772 + strlen (bfd_get_filename (abfd
))
790 minfo ("(%T)\n", h
->root
.string
);
792 minfo ("(%s)\n", name
);
795 if (trace_files
|| trace_file_tries
)
796 info_msg ("%I\n", input
);
801 /* This is called when BFD has discovered a symbol which is defined
806 multiple_definition (info
, name
, obfd
, osec
, oval
, nbfd
, nsec
, nval
)
807 struct bfd_link_info
*info
;
816 /* If either section has the output_section field set to
817 bfd_abs_section_ptr, it means that the section is being
818 discarded, and this is not really a multiple definition at all.
819 FIXME: It would be cleaner to somehow ignore symbols defined in
820 sections which are being discarded. */
821 if ((osec
->output_section
!= NULL
822 && ! bfd_is_abs_section (osec
)
823 && bfd_is_abs_section (osec
->output_section
))
824 || (nsec
->output_section
!= NULL
825 && ! bfd_is_abs_section (nsec
)
826 && bfd_is_abs_section (nsec
->output_section
)))
829 einfo (_("%X%C: multiple definition of `%T'\n"),
830 nbfd
, nsec
, nval
, name
);
831 if (obfd
!= (bfd
*) NULL
)
832 einfo (_("%D: first defined here\n"), obfd
, osec
, oval
);
836 /* This is called when there is a definition of a common symbol, or
837 when a common symbol is found for a symbol that is already defined,
838 or when two common symbols are found. We only do something if
839 -warn-common was used. */
843 multiple_common (info
, name
, obfd
, otype
, osize
, nbfd
, ntype
, nsize
)
844 struct bfd_link_info
*info
;
847 enum bfd_link_hash_type otype
;
850 enum bfd_link_hash_type ntype
;
853 if (! config
.warn_common
)
856 if (ntype
== bfd_link_hash_defined
857 || ntype
== bfd_link_hash_defweak
858 || ntype
== bfd_link_hash_indirect
)
860 ASSERT (otype
== bfd_link_hash_common
);
861 einfo (_("%B: warning: definition of `%T' overriding common\n"),
864 einfo (_("%B: warning: common is here\n"), obfd
);
866 else if (otype
== bfd_link_hash_defined
867 || otype
== bfd_link_hash_defweak
868 || otype
== bfd_link_hash_indirect
)
870 ASSERT (ntype
== bfd_link_hash_common
);
871 einfo (_("%B: warning: common of `%T' overridden by definition\n"),
874 einfo (_("%B: warning: defined here\n"), obfd
);
878 ASSERT (otype
== bfd_link_hash_common
&& ntype
== bfd_link_hash_common
);
881 einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
884 einfo (_("%B: warning: larger common is here\n"), obfd
);
886 else if (nsize
> osize
)
888 einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
891 einfo (_("%B: warning: smaller common is here\n"), obfd
);
895 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd
, name
);
897 einfo (_("%B: warning: previous common is here\n"), obfd
);
904 /* This is called when BFD has discovered a set element. H is the
905 entry in the linker hash table for the set. SECTION and VALUE
906 represent a value which should be added to the set. */
910 add_to_set (info
, h
, reloc
, abfd
, section
, value
)
911 struct bfd_link_info
*info
;
912 struct bfd_link_hash_entry
*h
;
913 bfd_reloc_code_real_type reloc
;
918 if (config
.warn_constructors
)
919 einfo (_("%P: warning: global constructor %s used\n"),
922 if (! config
.build_constructors
)
925 ldctor_add_set_entry (h
, reloc
, (const char *) NULL
, section
, value
);
927 if (h
->type
== bfd_link_hash_new
)
929 h
->type
= bfd_link_hash_undefined
;
930 h
->u
.undef
.abfd
= abfd
;
931 /* We don't call bfd_link_add_undef to add this to the list of
932 undefined symbols because we are going to define it
939 /* This is called when BFD has discovered a constructor. This is only
940 called for some object file formats--those which do not handle
941 constructors in some more clever fashion. This is similar to
942 adding an element to a set, but less general. */
945 constructor_callback (info
, constructor
, name
, abfd
, section
, value
)
946 struct bfd_link_info
*info
;
954 struct bfd_link_hash_entry
*h
;
955 char set_name
[1 + sizeof "__CTOR_LIST__"];
957 if (config
.warn_constructors
)
958 einfo (_("%P: warning: global constructor %s used\n"), name
);
960 if (! config
.build_constructors
)
963 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
964 useful error message. */
965 if (bfd_reloc_type_lookup (output_bfd
, BFD_RELOC_CTOR
) == NULL
966 && (link_info
.relocateable
967 || bfd_reloc_type_lookup (abfd
, BFD_RELOC_CTOR
) == NULL
))
968 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
971 if (bfd_get_symbol_leading_char (abfd
) != '\0')
972 *s
++ = bfd_get_symbol_leading_char (abfd
);
974 strcpy (s
, "__CTOR_LIST__");
976 strcpy (s
, "__DTOR_LIST__");
978 h
= bfd_link_hash_lookup (info
->hash
, set_name
, true, true, true);
979 if (h
== (struct bfd_link_hash_entry
*) NULL
)
980 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
981 if (h
->type
== bfd_link_hash_new
)
983 h
->type
= bfd_link_hash_undefined
;
984 h
->u
.undef
.abfd
= abfd
;
985 /* We don't call bfd_link_add_undef to add this to the list of
986 undefined symbols because we are going to define it
990 ldctor_add_set_entry (h
, BFD_RELOC_CTOR
, name
, section
, value
);
994 /* A structure used by warning_callback to pass information through
995 bfd_map_over_sections. */
997 struct warning_callback_info
1000 const char *warning
;
1005 /* This is called when there is a reference to a warning symbol. */
1009 warning_callback (info
, warning
, symbol
, abfd
, section
, address
)
1010 struct bfd_link_info
*info
;
1011 const char *warning
;
1017 /* This is a hack to support warn_multiple_gp. FIXME: This should
1018 have a cleaner interface, but what? */
1019 if (! config
.warn_multiple_gp
1020 && strcmp (warning
, "using multiple gp values") == 0)
1023 if (section
!= NULL
)
1024 einfo ("%C: %s\n", abfd
, section
, address
, warning
);
1025 else if (abfd
== NULL
)
1026 einfo ("%P: %s\n", warning
);
1027 else if (symbol
== NULL
)
1028 einfo ("%B: %s\n", abfd
, warning
);
1031 lang_input_statement_type
*entry
;
1033 struct warning_callback_info info
;
1035 /* Look through the relocs to see if we can find a plausible
1038 entry
= (lang_input_statement_type
*) abfd
->usrdata
;
1039 if (entry
!= NULL
&& entry
->asymbols
!= NULL
)
1040 asymbols
= entry
->asymbols
;
1046 symsize
= bfd_get_symtab_upper_bound (abfd
);
1048 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
1049 asymbols
= (asymbol
**) xmalloc (symsize
);
1050 symbol_count
= bfd_canonicalize_symtab (abfd
, asymbols
);
1051 if (symbol_count
< 0)
1052 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
1055 entry
->asymbols
= asymbols
;
1056 entry
->symbol_count
= symbol_count
;
1061 info
.warning
= warning
;
1062 info
.symbol
= symbol
;
1063 info
.asymbols
= asymbols
;
1064 bfd_map_over_sections (abfd
, warning_find_reloc
, (PTR
) &info
);
1067 einfo ("%B: %s\n", abfd
, warning
);
1076 /* This is called by warning_callback for each section. It checks the
1077 relocs of the section to see if it can find a reference to the
1078 symbol which triggered the warning. If it can, it uses the reloc
1079 to give an error message with a file and line number. */
1082 warning_find_reloc (abfd
, sec
, iarg
)
1087 struct warning_callback_info
*info
= (struct warning_callback_info
*) iarg
;
1091 arelent
**p
, **pend
;
1096 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1098 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
1102 relpp
= (arelent
**) xmalloc (relsize
);
1103 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, info
->asymbols
);
1105 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
1108 pend
= p
+ relcount
;
1109 for (; p
< pend
&& *p
!= NULL
; p
++)
1113 if (q
->sym_ptr_ptr
!= NULL
1114 && *q
->sym_ptr_ptr
!= NULL
1115 && strcmp (bfd_asymbol_name (*q
->sym_ptr_ptr
), info
->symbol
) == 0)
1117 /* We found a reloc for the symbol we are looking for. */
1118 einfo ("%C: %s\n", abfd
, sec
, q
->address
, info
->warning
);
1127 /* This is called when an undefined symbol is found. */
1131 undefined_symbol (info
, name
, abfd
, section
, address
)
1132 struct bfd_link_info
*info
;
1138 static char *error_name
;
1139 static unsigned int error_count
;
1141 #define MAX_ERRORS_IN_A_ROW 5
1143 if (config
.warn_once
)
1145 static struct bfd_hash_table
*hash
;
1147 /* Only warn once about a particular undefined symbol. */
1151 hash
= ((struct bfd_hash_table
*)
1152 xmalloc (sizeof (struct bfd_hash_table
)));
1153 if (! bfd_hash_table_init (hash
, bfd_hash_newfunc
))
1154 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
1157 if (bfd_hash_lookup (hash
, name
, false, false) != NULL
)
1160 if (bfd_hash_lookup (hash
, name
, true, true) == NULL
)
1161 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
1164 /* We never print more than a reasonable number of errors in a row
1165 for a single symbol. */
1166 if (error_name
!= (char *) NULL
1167 && strcmp (name
, error_name
) == 0)
1172 if (error_name
!= (char *) NULL
)
1174 error_name
= buystring (name
);
1177 if (section
!= NULL
)
1179 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1180 einfo (_("%X%C: undefined reference to `%T'\n"),
1181 abfd
, section
, address
, name
);
1182 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1183 einfo (_("%D: more undefined references to `%T' follow\n"),
1184 abfd
, section
, address
, name
);
1188 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1189 einfo (_("%X%B: undefined reference to `%T'\n"),
1191 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1192 einfo (_("%B: more undefined references to `%T' follow\n"),
1199 /* This is called when a reloc overflows. */
1203 reloc_overflow (info
, name
, reloc_name
, addend
, abfd
, section
, address
)
1204 struct bfd_link_info
*info
;
1206 const char *reloc_name
;
1212 if (abfd
== (bfd
*) NULL
)
1213 einfo (_("%P%X: generated"));
1215 einfo ("%X%C:", abfd
, section
, address
);
1216 einfo (_(" relocation truncated to fit: %s %T"), reloc_name
, name
);
1218 einfo ("+%v", addend
);
1223 /* This is called when a dangerous relocation is made. */
1227 reloc_dangerous (info
, message
, abfd
, section
, address
)
1228 struct bfd_link_info
*info
;
1229 const char *message
;
1234 if (abfd
== (bfd
*) NULL
)
1235 einfo (_("%P%X: generated"));
1237 einfo ("%X%C:", abfd
, section
, address
);
1238 einfo (_("dangerous relocation: %s\n"), message
);
1242 /* This is called when a reloc is being generated attached to a symbol
1243 that is not being output. */
1247 unattached_reloc (info
, name
, abfd
, section
, address
)
1248 struct bfd_link_info
*info
;
1254 if (abfd
== (bfd
*) NULL
)
1255 einfo (_("%P%X: generated"));
1257 einfo ("%X%C:", abfd
, section
, address
);
1258 einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name
);
1262 /* This is called if link_info.notice_all is set, or when a symbol in
1263 link_info.notice_hash is found. Symbols are put in notice_hash
1264 using the -y option. */
1267 notice (info
, name
, abfd
, section
, value
)
1268 struct bfd_link_info
*info
;
1274 if (! info
->notice_all
1275 || (info
->notice_hash
!= NULL
1276 && bfd_hash_lookup (info
->notice_hash
, name
, false, false) != NULL
))
1278 if (bfd_is_und_section (section
))
1279 einfo ("%B: reference to %s\n", abfd
, name
);
1281 einfo ("%B: definition of %s\n", abfd
, name
);
1284 if (command_line
.cref
|| nocrossref_list
!= NULL
)
1285 add_cref (name
, abfd
, section
, value
);