1 /* Main program of GNU linker.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 1997 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 boolean check_for_scripts_dir
PARAMS ((char *dir
));
90 static boolean add_archive_element
PARAMS ((struct bfd_link_info
*, bfd
*,
92 static boolean multiple_definition
PARAMS ((struct bfd_link_info
*,
94 bfd
*, asection
*, bfd_vma
,
95 bfd
*, asection
*, bfd_vma
));
96 static boolean multiple_common
PARAMS ((struct bfd_link_info
*,
98 enum bfd_link_hash_type
, bfd_vma
,
99 bfd
*, enum bfd_link_hash_type
,
101 static boolean add_to_set
PARAMS ((struct bfd_link_info
*,
102 struct bfd_link_hash_entry
*,
103 bfd_reloc_code_real_type
,
104 bfd
*, asection
*, bfd_vma
));
105 static boolean constructor_callback
PARAMS ((struct bfd_link_info
*,
108 bfd
*, asection
*, bfd_vma
));
109 static boolean warning_callback
PARAMS ((struct bfd_link_info
*,
110 const char *, const char *, bfd
*,
111 asection
*, bfd_vma
));
112 static void warning_find_reloc
PARAMS ((bfd
*, asection
*, PTR
));
113 static boolean undefined_symbol
PARAMS ((struct bfd_link_info
*,
115 asection
*, bfd_vma
));
116 static boolean reloc_overflow
PARAMS ((struct bfd_link_info
*, const char *,
117 const char *, bfd_vma
,
118 bfd
*, asection
*, bfd_vma
));
119 static boolean reloc_dangerous
PARAMS ((struct bfd_link_info
*, const char *,
120 bfd
*, asection
*, bfd_vma
));
121 static boolean unattached_reloc
PARAMS ((struct bfd_link_info
*,
122 const char *, bfd
*, asection
*,
124 static boolean notice
PARAMS ((struct bfd_link_info
*, const char *,
125 bfd
*, asection
*, bfd_vma
));
127 static struct bfd_link_callbacks link_callbacks
=
133 constructor_callback
,
142 struct bfd_link_info link_info
;
149 if (output_bfd
&& output_bfd
->iostream
)
150 fclose((FILE *)(output_bfd
->iostream
));
151 if (delete_output_file_on_failure
)
152 unlink (output_filename
);
162 long start_time
= get_run_time ();
164 program_name
= argv
[0];
165 xmalloc_set_program_name (program_name
);
167 START_PROGRESS (program_name
, 0);
171 bfd_set_error_program_name (program_name
);
173 xatexit (remove_output
);
175 /* Initialize the data about options. */
176 trace_files
= trace_file_tries
= version_printed
= false;
177 whole_archive
= false;
178 config
.build_constructors
= true;
179 config
.dynamic_link
= false;
180 command_line
.force_common_definition
= false;
181 command_line
.interpreter
= NULL
;
182 command_line
.rpath
= NULL
;
184 link_info
.callbacks
= &link_callbacks
;
185 link_info
.relocateable
= false;
186 link_info
.shared
= false;
187 link_info
.symbolic
= false;
188 link_info
.static_link
= false;
189 link_info
.traditional_format
= false;
190 link_info
.strip
= strip_none
;
191 link_info
.discard
= discard_none
;
192 link_info
.keep_memory
= true;
193 link_info
.input_bfds
= NULL
;
194 link_info
.create_object_symbols_section
= NULL
;
195 link_info
.hash
= NULL
;
196 link_info
.keep_hash
= NULL
;
197 link_info
.notice_all
= false;
198 link_info
.notice_hash
= NULL
;
199 link_info
.wrap_hash
= NULL
;
201 ldfile_add_arch ("");
203 config
.make_executable
= true;
204 force_make_executable
= false;
205 config
.magic_demand_paged
= true;
206 config
.text_read_only
= true;
207 config
.make_executable
= true;
209 emulation
= get_emulation (argc
, argv
);
210 ldemul_choose_mode (emulation
);
211 default_target
= ldemul_choose_target ();
213 ldemul_before_parse ();
214 lang_has_input_file
= false;
215 parse_args (argc
, argv
);
217 ldemul_set_symbols ();
219 if (link_info
.relocateable
)
221 if (command_line
.relax
)
222 einfo ("%P%F: -relax and -r may not be used together\n");
223 if (link_info
.shared
)
224 einfo ("%P%F: -r and -shared may not be used together\n");
227 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
228 don't see how else this can be handled, since in this case we
229 must preserve all externally visible symbols. */
230 if (link_info
.relocateable
&& link_info
.strip
== strip_all
)
232 link_info
.strip
= strip_debugger
;
233 if (link_info
.discard
== discard_none
)
234 link_info
.discard
= discard_all
;
237 /* This essentially adds another -L directory so this must be done after
238 the -L's in argv have been processed. */
241 if (had_script
== false)
243 /* Read the emulation's appropriate default script. */
245 char *s
= ldemul_get_script (&isfile
);
248 ldfile_open_command_file (s
);
251 if (trace_file_tries
)
253 info_msg ("using internal linker script:\n");
254 info_msg ("==================================================\n");
256 info_msg ("\n==================================================\n");
261 parser_input
= input_script
;
268 if (lang_has_input_file
== false)
272 einfo ("%P%F: no input files\n");
277 info_msg ("%P: mode %s\n", emulation
);
280 ldemul_after_parse ();
283 if (config
.map_filename
)
285 if (strcmp (config
.map_filename
, "-") == 0)
287 config
.map_file
= stdout
;
291 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
292 if (config
.map_file
== (FILE *) NULL
)
294 bfd_set_error (bfd_error_system_call
);
295 einfo ("%P%F: cannot open map file %s: %E\n",
296 config
.map_filename
);
304 /* Print error messages for any missing symbols, for any warning
305 symbols, and possibly multiple definitions */
308 if (config
.text_read_only
)
310 /* Look for a text section and mark the readonly attribute in it */
311 asection
*found
= bfd_get_section_by_name (output_bfd
, ".text");
313 if (found
!= (asection
*) NULL
)
315 found
->flags
|= SEC_READONLY
;
319 if (link_info
.relocateable
)
320 output_bfd
->flags
&= ~EXEC_P
;
322 output_bfd
->flags
|= EXEC_P
;
326 if (config
.map_file
!= NULL
)
328 if (command_line
.cref
)
329 output_cref (config
.map_file
!= NULL
? config
.map_file
: stdout
);
330 if (nocrossref_list
!= NULL
)
331 check_nocrossrefs ();
333 /* Even if we're producing relocateable output, some non-fatal errors should
334 be reported in the exit status. (What non-fatal errors, if any, do we
335 want to ignore for relocateable output?) */
337 if (config
.make_executable
== false && force_make_executable
== false)
339 if (trace_files
== true)
341 einfo ("%P: link errors found, deleting executable `%s'\n",
345 /* The file will be removed by remove_output. */
351 if (! bfd_close (output_bfd
))
352 einfo ("%F%B: final close failed: %E\n", output_bfd
);
354 /* If the --force-exe-suffix is enabled, and we're making an
355 executable file and it doesn't end in .exe, copy it to one which does. */
357 if (! link_info
.relocateable
&& command_line
.force_exe_suffix
)
359 int len
= strlen (output_filename
);
361 || (strcasecmp (output_filename
+ len
- 4, ".exe") != 0
362 && strcasecmp (output_filename
+ len
- 4, ".dll") != 0))
366 const int bsize
= 4096;
367 char *buf
= xmalloc (bsize
);
369 char *dst_name
= xmalloc (len
+ 5);
370 strcpy (dst_name
, output_filename
);
371 strcat (dst_name
, ".exe");
372 src
= fopen (output_filename
, FOPEN_RB
);
373 dst
= fopen (dst_name
, FOPEN_WB
);
376 einfo ("%X%P: unable to open for source of copy `%s'\n", output_filename
);
378 einfo ("%X%P: unable to open for destination of copy `%s'\n", dst_name
);
379 while ((l
= fread (buf
, 1, bsize
, src
)) > 0)
381 int done
= fwrite (buf
, 1, l
, dst
);
384 einfo ("%P: Error writing file `%s'\n", dst_name
);
390 einfo ("%P: Error closing file `%s'\n", dst_name
);
398 END_PROGRESS (program_name
);
402 extern char **environ
;
404 char *lim
= (char *) sbrk (0);
406 long run_time
= get_run_time () - start_time
;
408 fprintf (stderr
, "%s: total time in link: %ld.%06ld\n",
409 program_name
, run_time
/ 1000000, run_time
% 1000000);
411 fprintf (stderr
, "%s: data size %ld\n", program_name
,
412 (long) (lim
- (char *) &environ
));
416 /* Prevent remove_output from doing anything, after a successful link. */
417 output_filename
= NULL
;
423 /* We need to find any explicitly given emulation in order to initialize the
424 state that's needed by the lex&yacc argument parser (parse_args). */
427 get_emulation (argc
, argv
)
434 emulation
= getenv (EMULATION_ENVIRON
);
435 if (emulation
== NULL
)
436 emulation
= DEFAULT_EMULATION
;
438 for (i
= 1; i
< argc
; i
++)
440 if (!strncmp (argv
[i
], "-m", 2))
442 if (argv
[i
][2] == '\0')
447 emulation
= argv
[i
+ 1];
452 einfo("%P%F: missing argument to -m\n");
455 else if (strcmp (argv
[i
], "-mips1") == 0
456 || strcmp (argv
[i
], "-mips2") == 0
457 || strcmp (argv
[i
], "-mips3") == 0
458 || strcmp (argv
[i
], "-mips4") == 0)
460 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
461 passed to the linker by some MIPS compilers. They
462 generally tell the linker to use a slightly different
463 library path. Perhaps someday these should be
464 implemented as emulations; until then, we just ignore
465 the arguments and hope that nobody ever creates
466 emulations named ips1, ips2 or ips3. */
468 else if (strcmp (argv
[i
], "-m486") == 0)
470 /* FIXME: The argument -m486 is passed to the linker on
471 some Linux systems. Hope that nobody creates an
472 emulation named 486. */
477 emulation
= &argv
[i
][2];
485 /* If directory DIR contains an "ldscripts" subdirectory,
486 add DIR to the library search path and return true,
487 else return false. */
490 check_for_scripts_dir (dir
)
498 dirlen
= strlen (dir
);
499 /* sizeof counts the terminating NUL. */
500 buf
= (char *) xmalloc (dirlen
+ sizeof("/ldscripts"));
501 sprintf (buf
, "%s/ldscripts", dir
);
503 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
506 ldfile_add_library_path (dir
, false);
510 /* Set the default directory for finding script files.
511 Libraries will be searched for here too, but that's ok.
512 We look for the "ldscripts" directory in:
514 SCRIPTDIR (passed from Makefile)
515 the dir where this program is (for using it from the build tree)
516 the dir where this program is/../lib (for installing the tool suite elsewhere) */
524 if (check_for_scripts_dir (SCRIPTDIR
))
525 return; /* We've been installed normally. */
527 /* Look for "ldscripts" in the dir where our binary is. */
528 end
= strrchr (program_name
, '/');
532 /* Don't look for ldscripts in the current directory. There is
533 too much potential for confusion. */
537 dirlen
= end
- program_name
;
538 /* Make a copy of program_name in dir.
539 Leave room for later "/../lib". */
540 dir
= (char *) xmalloc (dirlen
+ 8);
541 strncpy (dir
, program_name
, dirlen
);
544 if (check_for_scripts_dir (dir
))
545 return; /* Don't free dir. */
547 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
548 strcpy (dir
+ dirlen
, "/../lib");
549 if (check_for_scripts_dir (dir
))
552 free (dir
); /* Well, we tried. */
559 if (link_info
.notice_hash
== (struct bfd_hash_table
*) NULL
)
561 link_info
.notice_hash
= ((struct bfd_hash_table
*)
562 xmalloc (sizeof (struct bfd_hash_table
)));
563 if (! bfd_hash_table_init_n (link_info
.notice_hash
,
566 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
569 if (bfd_hash_lookup (link_info
.notice_hash
, name
, true, true)
570 == (struct bfd_hash_entry
*) NULL
)
571 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
574 /* Record a symbol to be wrapped, from the --wrap option. */
580 if (link_info
.wrap_hash
== NULL
)
582 link_info
.wrap_hash
= ((struct bfd_hash_table
*)
583 xmalloc (sizeof (struct bfd_hash_table
)));
584 if (! bfd_hash_table_init_n (link_info
.wrap_hash
,
587 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
589 if (bfd_hash_lookup (link_info
.wrap_hash
, name
, true, true) == NULL
)
590 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
593 /* Handle the -retain-symbols-file option. */
596 add_keepsyms_file (filename
)
597 const char *filename
;
604 if (link_info
.strip
== strip_some
)
605 einfo ("%X%P: error: duplicate retain-symbols-file\n");
607 file
= fopen (filename
, "r");
608 if (file
== (FILE *) NULL
)
610 bfd_set_error (bfd_error_system_call
);
611 einfo ("%X%P: %s: %E\n", filename
);
615 link_info
.keep_hash
= ((struct bfd_hash_table
*)
616 xmalloc (sizeof (struct bfd_hash_table
)));
617 if (! bfd_hash_table_init (link_info
.keep_hash
, bfd_hash_newfunc
))
618 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
621 buf
= (char *) xmalloc (bufsize
);
633 while (! isspace (c
) && c
!= EOF
)
640 buf
= xrealloc (buf
, bufsize
);
647 if (bfd_hash_lookup (link_info
.keep_hash
, buf
, true, true)
648 == (struct bfd_hash_entry
*) NULL
)
649 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E\n");
653 if (link_info
.strip
!= strip_none
)
654 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
656 link_info
.strip
= strip_some
;
659 /* Callbacks from the BFD linker routines. */
661 /* This is called when BFD has decided to include an archive member in
666 add_archive_element (info
, abfd
, name
)
667 struct bfd_link_info
*info
;
671 lang_input_statement_type
*input
;
673 input
= ((lang_input_statement_type
*)
674 xmalloc (sizeof (lang_input_statement_type
)));
675 input
->filename
= abfd
->filename
;
676 input
->local_sym_name
= abfd
->filename
;
677 input
->the_bfd
= abfd
;
678 input
->asymbols
= NULL
;
680 input
->just_syms_flag
= false;
681 input
->loaded
= false;
682 input
->search_dirs_flag
= false;
684 /* FIXME: The following fields are not set: header.next,
685 header.type, closed, passive_position, symbol_count,
686 next_real_file, is_archive, target, real. This bit of code is
687 from the old decode_library_subfile function. I don't know
688 whether any of those fields matters. */
690 ldlang_add_file (input
);
692 if (config
.map_file
!= (FILE *) NULL
)
694 static boolean header_printed
;
695 struct bfd_link_hash_entry
*h
;
699 h
= bfd_link_hash_lookup (link_info
.hash
, name
, false, false, true);
711 case bfd_link_hash_defined
:
712 case bfd_link_hash_defweak
:
713 from
= h
->u
.def
.section
->owner
;
716 case bfd_link_hash_undefined
:
717 case bfd_link_hash_undefweak
:
718 from
= h
->u
.undef
.abfd
;
721 case bfd_link_hash_common
:
722 from
= h
->u
.c
.p
->section
->owner
;
727 if (! header_printed
)
731 sprintf (buf
, "%-29s %s\n\n", "Archive member included",
732 "because of file (symbol)");
734 header_printed
= true;
737 if (bfd_my_archive (abfd
) == NULL
)
739 minfo ("%s", bfd_get_filename (abfd
));
740 len
= strlen (bfd_get_filename (abfd
));
744 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd
)),
745 bfd_get_filename (abfd
));
746 len
= (strlen (bfd_get_filename (bfd_my_archive (abfd
)))
747 + strlen (bfd_get_filename (abfd
))
765 minfo ("(%T)\n", h
->root
.string
);
767 minfo ("(%s)\n", name
);
770 if (trace_files
|| trace_file_tries
)
771 info_msg ("%I\n", input
);
776 /* This is called when BFD has discovered a symbol which is defined
781 multiple_definition (info
, name
, obfd
, osec
, oval
, nbfd
, nsec
, nval
)
782 struct bfd_link_info
*info
;
791 /* If either section has the output_section field set to
792 bfd_abs_section_ptr, it means that the section is being
793 discarded, and this is not really a multiple definition at all.
794 FIXME: It would be cleaner to somehow ignore symbols defined in
795 sections which are being discarded. */
796 if ((osec
->output_section
!= NULL
797 && bfd_is_abs_section (osec
->output_section
))
798 || (nsec
->output_section
!= NULL
799 && bfd_is_abs_section (nsec
->output_section
)))
802 einfo ("%X%C: multiple definition of `%T'\n",
803 nbfd
, nsec
, nval
, name
);
804 if (obfd
!= (bfd
*) NULL
)
805 einfo ("%D: first defined here\n", obfd
, osec
, oval
);
809 /* This is called when there is a definition of a common symbol, or
810 when a common symbol is found for a symbol that is already defined,
811 or when two common symbols are found. We only do something if
812 -warn-common was used. */
816 multiple_common (info
, name
, obfd
, otype
, osize
, nbfd
, ntype
, nsize
)
817 struct bfd_link_info
*info
;
820 enum bfd_link_hash_type otype
;
823 enum bfd_link_hash_type ntype
;
826 if (! config
.warn_common
)
829 if (ntype
== bfd_link_hash_defined
830 || ntype
== bfd_link_hash_defweak
831 || ntype
== bfd_link_hash_indirect
)
833 ASSERT (otype
== bfd_link_hash_common
);
834 einfo ("%B: warning: definition of `%T' overriding common\n",
837 einfo ("%B: warning: common is here\n", obfd
);
839 else if (otype
== bfd_link_hash_defined
840 || otype
== bfd_link_hash_defweak
841 || otype
== bfd_link_hash_indirect
)
843 ASSERT (ntype
== bfd_link_hash_common
);
844 einfo ("%B: warning: common of `%T' overridden by definition\n",
847 einfo ("%B: warning: defined here\n", obfd
);
851 ASSERT (otype
== bfd_link_hash_common
&& ntype
== bfd_link_hash_common
);
854 einfo ("%B: warning: common of `%T' overridden by larger common\n",
857 einfo ("%B: warning: larger common is here\n", obfd
);
859 else if (nsize
> osize
)
861 einfo ("%B: warning: common of `%T' overriding smaller common\n",
864 einfo ("%B: warning: smaller common is here\n", obfd
);
868 einfo ("%B: warning: multiple common of `%T'\n", nbfd
, name
);
870 einfo ("%B: warning: previous common is here\n", obfd
);
877 /* This is called when BFD has discovered a set element. H is the
878 entry in the linker hash table for the set. SECTION and VALUE
879 represent a value which should be added to the set. */
883 add_to_set (info
, h
, reloc
, abfd
, section
, value
)
884 struct bfd_link_info
*info
;
885 struct bfd_link_hash_entry
*h
;
886 bfd_reloc_code_real_type reloc
;
891 if (config
.warn_constructors
)
892 einfo ("%P: warning: global constructor %s used\n",
895 if (! config
.build_constructors
)
898 ldctor_add_set_entry (h
, reloc
, (const char *) NULL
, section
, value
);
900 if (h
->type
== bfd_link_hash_new
)
902 h
->type
= bfd_link_hash_undefined
;
903 h
->u
.undef
.abfd
= abfd
;
904 /* We don't call bfd_link_add_undef to add this to the list of
905 undefined symbols because we are going to define it
912 /* This is called when BFD has discovered a constructor. This is only
913 called for some object file formats--those which do not handle
914 constructors in some more clever fashion. This is similar to
915 adding an element to a set, but less general. */
918 constructor_callback (info
, constructor
, name
, abfd
, section
, value
)
919 struct bfd_link_info
*info
;
927 struct bfd_link_hash_entry
*h
;
928 char set_name
[1 + sizeof "__CTOR_LIST__"];
930 if (config
.warn_constructors
)
931 einfo ("%P: warning: global constructor %s used\n", name
);
933 if (! config
.build_constructors
)
936 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
937 useful error message. */
938 if (bfd_reloc_type_lookup (output_bfd
, BFD_RELOC_CTOR
) == NULL
939 && (link_info
.relocateable
940 || bfd_reloc_type_lookup (abfd
, BFD_RELOC_CTOR
) == NULL
))
941 einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n");
944 if (bfd_get_symbol_leading_char (abfd
) != '\0')
945 *s
++ = bfd_get_symbol_leading_char (abfd
);
947 strcpy (s
, "__CTOR_LIST__");
949 strcpy (s
, "__DTOR_LIST__");
951 h
= bfd_link_hash_lookup (info
->hash
, set_name
, true, true, true);
952 if (h
== (struct bfd_link_hash_entry
*) NULL
)
953 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
954 if (h
->type
== bfd_link_hash_new
)
956 h
->type
= bfd_link_hash_undefined
;
957 h
->u
.undef
.abfd
= abfd
;
958 /* We don't call bfd_link_add_undef to add this to the list of
959 undefined symbols because we are going to define it
963 ldctor_add_set_entry (h
, BFD_RELOC_CTOR
, name
, section
, value
);
967 /* A structure used by warning_callback to pass information through
968 bfd_map_over_sections. */
970 struct warning_callback_info
978 /* This is called when there is a reference to a warning symbol. */
982 warning_callback (info
, warning
, symbol
, abfd
, section
, address
)
983 struct bfd_link_info
*info
;
990 /* This is a hack to support warn_multiple_gp. FIXME: This should
991 have a cleaner interface, but what? */
992 if (! config
.warn_multiple_gp
993 && strcmp (warning
, "using multiple gp values") == 0)
997 einfo ("%C: %s\n", abfd
, section
, address
, warning
);
998 else if (abfd
== NULL
)
999 einfo ("%P: %s\n", warning
);
1000 else if (symbol
== NULL
)
1001 einfo ("%B: %s\n", abfd
, warning
);
1004 lang_input_statement_type
*entry
;
1006 struct warning_callback_info info
;
1008 /* Look through the relocs to see if we can find a plausible
1011 entry
= (lang_input_statement_type
*) abfd
->usrdata
;
1012 if (entry
!= NULL
&& entry
->asymbols
!= NULL
)
1013 asymbols
= entry
->asymbols
;
1019 symsize
= bfd_get_symtab_upper_bound (abfd
);
1021 einfo ("%B%F: could not read symbols: %E\n", abfd
);
1022 asymbols
= (asymbol
**) xmalloc (symsize
);
1023 symbol_count
= bfd_canonicalize_symtab (abfd
, asymbols
);
1024 if (symbol_count
< 0)
1025 einfo ("%B%F: could not read symbols: %E\n", abfd
);
1028 entry
->asymbols
= asymbols
;
1029 entry
->symbol_count
= symbol_count
;
1034 info
.warning
= warning
;
1035 info
.symbol
= symbol
;
1036 info
.asymbols
= asymbols
;
1037 bfd_map_over_sections (abfd
, warning_find_reloc
, (PTR
) &info
);
1040 einfo ("%B: %s\n", abfd
, warning
);
1049 /* This is called by warning_callback for each section. It checks the
1050 relocs of the section to see if it can find a reference to the
1051 symbol which triggered the warning. If it can, it uses the reloc
1052 to give an error message with a file and line number. */
1055 warning_find_reloc (abfd
, sec
, iarg
)
1060 struct warning_callback_info
*info
= (struct warning_callback_info
*) iarg
;
1064 arelent
**p
, **pend
;
1069 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1071 einfo ("%B%F: could not read relocs: %E\n", abfd
);
1075 relpp
= (arelent
**) xmalloc (relsize
);
1076 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, info
->asymbols
);
1078 einfo ("%B%F: could not read relocs: %E\n", abfd
);
1081 pend
= p
+ relcount
;
1082 for (; p
< pend
&& *p
!= NULL
; p
++)
1086 if (q
->sym_ptr_ptr
!= NULL
1087 && *q
->sym_ptr_ptr
!= NULL
1088 && strcmp (bfd_asymbol_name (*q
->sym_ptr_ptr
), info
->symbol
) == 0)
1090 /* We found a reloc for the symbol we are looking for. */
1091 einfo ("%C: %s\n", abfd
, sec
, q
->address
, info
->warning
);
1100 /* This is called when an undefined symbol is found. */
1104 undefined_symbol (info
, name
, abfd
, section
, address
)
1105 struct bfd_link_info
*info
;
1111 static char *error_name
;
1112 static unsigned int error_count
;
1114 #define MAX_ERRORS_IN_A_ROW 5
1116 if (config
.warn_once
)
1118 static struct bfd_hash_table
*hash
;
1120 /* Only warn once about a particular undefined symbol. */
1124 hash
= ((struct bfd_hash_table
*)
1125 xmalloc (sizeof (struct bfd_hash_table
)));
1126 if (! bfd_hash_table_init (hash
, bfd_hash_newfunc
))
1127 einfo ("%F%P: bfd_hash_table_init failed: %E\n");
1130 if (bfd_hash_lookup (hash
, name
, false, false) != NULL
)
1133 if (bfd_hash_lookup (hash
, name
, true, true) == NULL
)
1134 einfo ("%F%P: bfd_hash_lookup failed: %E\n");
1137 /* We never print more than a reasonable number of errors in a row
1138 for a single symbol. */
1139 if (error_name
!= (char *) NULL
1140 && strcmp (name
, error_name
) == 0)
1145 if (error_name
!= (char *) NULL
)
1147 error_name
= buystring (name
);
1150 if (section
!= NULL
)
1152 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1153 einfo ("%X%C: undefined reference to `%T'\n",
1154 abfd
, section
, address
, name
);
1155 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1156 einfo ("%D: more undefined references to `%T' follow\n",
1157 abfd
, section
, address
, name
);
1161 if (error_count
< MAX_ERRORS_IN_A_ROW
)
1162 einfo ("%X%B: undefined reference to `%T'\n",
1164 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
1165 einfo ("%B: more undefined references to `%T' follow\n",
1172 /* This is called when a reloc overflows. */
1176 reloc_overflow (info
, name
, reloc_name
, addend
, abfd
, section
, address
)
1177 struct bfd_link_info
*info
;
1179 const char *reloc_name
;
1185 if (abfd
== (bfd
*) NULL
)
1186 einfo ("%P%X: generated");
1188 einfo ("%X%C:", abfd
, section
, address
);
1189 einfo (" relocation truncated to fit: %s %T", reloc_name
, name
);
1191 einfo ("+%v", addend
);
1196 /* This is called when a dangerous relocation is made. */
1200 reloc_dangerous (info
, message
, abfd
, section
, address
)
1201 struct bfd_link_info
*info
;
1202 const char *message
;
1207 if (abfd
== (bfd
*) NULL
)
1208 einfo ("%P%X: generated");
1210 einfo ("%X%C:", abfd
, section
, address
);
1211 einfo ("dangerous relocation: %s\n", message
);
1215 /* This is called when a reloc is being generated attached to a symbol
1216 that is not being output. */
1220 unattached_reloc (info
, name
, abfd
, section
, address
)
1221 struct bfd_link_info
*info
;
1227 if (abfd
== (bfd
*) NULL
)
1228 einfo ("%P%X: generated");
1230 einfo ("%X%C:", abfd
, section
, address
);
1231 einfo (" reloc refers to symbol `%T' which is not being output\n", name
);
1235 /* This is called if link_info.notice_all is set, or when a symbol in
1236 link_info.notice_hash is found. Symbols are put in notice_hash
1237 using the -y option. */
1240 notice (info
, name
, abfd
, section
, value
)
1241 struct bfd_link_info
*info
;
1247 if (! info
->notice_all
1248 || (info
->notice_hash
!= NULL
1249 && bfd_hash_lookup (info
->notice_hash
, name
, false, false) != NULL
))
1250 einfo ("%B: %s %s\n", abfd
,
1251 bfd_is_und_section (section
) ? "reference to" : "definition of",
1254 if (command_line
.cref
|| nocrossref_list
!= NULL
)
1255 add_cref (name
, abfd
, section
, value
);