1 /* Main program of GNU linker.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 1997, 1998 Free Software Foundation, Inc.
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 setlocale (LC_MESSAGES, "");
166 bindtextdomain (PACKAGE, LOCALEDIR);
167 textdomain (PACKAGE);
169 program_name = argv[0];
170 xmalloc_set_program_name (program_name);
172 START_PROGRESS (program_name, 0);
176 bfd_set_error_program_name (program_name);
178 xatexit (remove_output);
180 /* Set the default BFD target based on the configured target. Doing
181 this permits the linker to be configured for a particular target,
182 and linked against a shared BFD library which was configured for
183 a different target. The macro TARGET is defined by Makefile. */
184 if (! bfd_set_default_target (TARGET))
186 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
190 /* Initialize the data about options. */
191 trace_files = trace_file_tries = version_printed = false;
192 whole_archive = false;
193 config.build_constructors = true;
194 config.dynamic_link = false;
195 command_line.force_common_definition = false;
196 command_line.interpreter = NULL;
197 command_line.rpath = NULL;
198 command_line.warn_mismatch = true;
200 link_info.callbacks = &link_callbacks;
201 link_info.relocateable = false;
202 link_info.shared = false;
203 link_info.symbolic = false;
204 link_info.static_link = false;
205 link_info.traditional_format = false;
206 link_info.strip = strip_none;
207 link_info.discard = discard_none;
208 link_info.keep_memory = true;
209 link_info.input_bfds = NULL;
210 link_info.create_object_symbols_section = NULL;
211 link_info.hash = NULL;
212 link_info.keep_hash = NULL;
213 link_info.notice_all = false;
214 link_info.notice_hash = NULL;
215 link_info.wrap_hash = NULL;
217 ldfile_add_arch ("");
219 config.make_executable = true;
220 force_make_executable = false;
221 config.magic_demand_paged = true;
222 config.text_read_only = true;
223 config.make_executable = true;
225 emulation = get_emulation (argc, argv);
226 ldemul_choose_mode (emulation);
227 default_target = ldemul_choose_target ();
229 ldemul_before_parse ();
230 lang_has_input_file = false;
231 parse_args (argc, argv);
233 ldemul_set_symbols ();
235 if (link_info.relocateable)
237 if (command_line.relax)
238 einfo (_("%P%F: -relax and -r may not be used together\n"));
239 if (link_info.shared)
240 einfo (_("%P%F: -r and -shared may not be used together\n"));
243 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
244 don't see how else this can be handled, since in this case we
245 must preserve all externally visible symbols. */
246 if (link_info.relocateable && link_info.strip == strip_all)
248 link_info.strip = strip_debugger;
249 if (link_info.discard == discard_none)
250 link_info.discard = discard_all;
253 /* This essentially adds another -L directory so this must be done after
254 the -L's in argv have been processed. */
257 if (had_script == false)
259 /* Read the emulation's appropriate default script. */
261 char *s = ldemul_get_script (&isfile);
264 ldfile_open_command_file (s);
267 if (trace_file_tries)
269 info_msg (_("using internal linker script:\n"));
270 info_msg ("==================================================\n");
272 info_msg ("\n==================================================\n");
277 parser_input = input_script;
284 if (lang_has_input_file == false)
288 einfo (_("%P%F: no input files\n"));
293 info_msg (_("%P: mode %s\n"), emulation);
296 ldemul_after_parse ();
299 if (config.map_filename)
301 if (strcmp (config.map_filename, "-") == 0)
303 config.map_file = stdout;
307 config.map_file = fopen (config.map_filename, FOPEN_WT);
308 if (config.map_file == (FILE *) NULL)
310 bfd_set_error (bfd_error_system_call);
311 einfo (_("%P%F: cannot open map file %s: %E\n"),
312 config.map_filename);
320 /* Print error messages for any missing symbols, for any warning
321 symbols, and possibly multiple definitions */
324 if (config.text_read_only)
326 /* Look for a text section and mark the readonly attribute in it */
327 asection *found = bfd_get_section_by_name (output_bfd, ".text");
329 if (found != (asection *) NULL)
331 found->flags |= SEC_READONLY;
335 if (link_info.relocateable)
336 output_bfd->flags &= ~EXEC_P;
338 output_bfd->flags |= EXEC_P;
342 if (config.map_file != NULL)
344 if (command_line.cref)
345 output_cref (config.map_file != NULL ? config.map_file : stdout);
346 if (nocrossref_list != NULL)
347 check_nocrossrefs ();
349 /* Even if we're producing relocateable output, some non-fatal errors should
350 be reported in the exit status. (What non-fatal errors, if any, do we
351 want to ignore for relocateable output?) */
353 if (config.make_executable == false && force_make_executable == false)
355 if (trace_files == true)
357 einfo (_("%P: link errors found, deleting executable `%s'\n"),
361 /* The file will be removed by remove_output. */
367 if (! bfd_close (output_bfd))
368 einfo (_("%F%B: final close failed: %E\n"), output_bfd);
370 /* If the --force-exe-suffix is enabled, and we're making an
371 executable file and it doesn't end in .exe, copy it to one which does. */
373 if (! link_info.relocateable && command_line.force_exe_suffix)
375 int len = strlen (output_filename);
377 || (strcasecmp (output_filename + len - 4, ".exe") != 0
378 && strcasecmp (output_filename + len - 4, ".dll") != 0))
382 const int bsize = 4096;
383 char *buf = xmalloc (bsize);
385 char *dst_name = xmalloc (len + 5);
386 strcpy (dst_name, output_filename);
387 strcat (dst_name, ".exe");
388 src = fopen (output_filename, FOPEN_RB);
389 dst = fopen (dst_name, FOPEN_WB);
392 einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename);
394 einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name);
395 while ((l = fread (buf, 1, bsize, src)) > 0)
397 int done = fwrite (buf, 1, l, dst);
400 einfo (_("%P: Error writing file `%s'\n"), dst_name);
404 if (fclose (dst) == EOF)
406 einfo (_("%P: Error closing file `%s'\n"), dst_name);
414 END_PROGRESS (program_name);
418 extern char **environ;
420 char *lim = (char *) sbrk (0);
422 long run_time = get_run_time () - start_time;
424 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
425 program_name, run_time / 1000000, run_time % 1000000);
427 fprintf (stderr, _("%s: data size %ld\n"), program_name,
428 (long) (lim - (char *) &environ));
432 /* Prevent remove_output from doing anything, after a successful link. */
433 output_filename = NULL;
439 /* We need to find any explicitly given emulation in order to initialize the
440 state that's needed by the lex&yacc argument parser (parse_args). */
443 get_emulation (argc, argv)
450 emulation = getenv (EMULATION_ENVIRON);
451 if (emulation == NULL)
452 emulation = DEFAULT_EMULATION;
454 for (i = 1; i < argc; i++)
456 if (!strncmp (argv[i], "-m", 2))
458 if (argv[i][2] == '\0')
463 emulation = argv[i + 1];
468 einfo(_("%P%F: missing argument to -m\n"));
471 else if (strcmp (argv[i], "-mips1") == 0
472 || strcmp (argv[i], "-mips2") == 0
473 || strcmp (argv[i], "-mips3") == 0
474 || strcmp (argv[i], "-mips4") == 0)
476 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
477 passed to the linker by some MIPS compilers. They
478 generally tell the linker to use a slightly different
479 library path. Perhaps someday these should be
480 implemented as emulations; until then, we just ignore
481 the arguments and hope that nobody ever creates
482 emulations named ips1, ips2 or ips3. */
484 else if (strcmp (argv[i], "-m486") == 0)
486 /* FIXME: The argument -m486 is passed to the linker on
487 some Linux systems. Hope that nobody creates an
488 emulation named 486. */
493 emulation = &argv[i][2];
501 /* If directory DIR contains an "ldscripts" subdirectory,
502 add DIR to the library search path and return true,
503 else return false. */
506 check_for_scripts_dir (dir)
514 dirlen = strlen (dir);
515 /* sizeof counts the terminating NUL. */
516 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
517 sprintf (buf, "%s/ldscripts", dir);
519 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
522 ldfile_add_library_path (dir, false);
526 /* Set the default directory for finding script files.
527 Libraries will be searched for here too, but that's ok.
528 We look for the "ldscripts" directory in:
530 SCRIPTDIR (passed from Makefile)
531 the dir where this program is (for using it from the build tree)
532 the dir where this program is/../lib (for installing the tool suite elsewhere) */
540 if (check_for_scripts_dir (SCRIPTDIR))
541 return; /* We've been installed normally. */
543 /* Look for "ldscripts" in the dir where our binary is. */
544 end = strrchr (program_name, '/');
548 /* Don't look for ldscripts in the current directory. There is
549 too much potential for confusion. */
553 dirlen = end - program_name;
554 /* Make a copy of program_name in dir.
555 Leave room for later "/../lib". */
556 dir = (char *) xmalloc (dirlen + 8);
557 strncpy (dir, program_name, dirlen);
560 if (check_for_scripts_dir (dir))
561 return; /* Don't free dir. */
563 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
564 strcpy (dir + dirlen, "/../lib");
565 if (check_for_scripts_dir (dir))
568 free (dir); /* Well, we tried. */
575 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
577 link_info.notice_hash = ((struct bfd_hash_table *)
578 xmalloc (sizeof (struct bfd_hash_table)));
579 if (! bfd_hash_table_init_n (link_info.notice_hash,
582 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
585 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
586 == (struct bfd_hash_entry *) NULL)
587 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
590 /* Record a symbol to be wrapped, from the --wrap option. */
596 if (link_info.wrap_hash == NULL)
598 link_info.wrap_hash = ((struct bfd_hash_table *)
599 xmalloc (sizeof (struct bfd_hash_table)));
600 if (! bfd_hash_table_init_n (link_info.wrap_hash,
603 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
605 if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
606 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
609 /* Handle the -retain-symbols-file option. */
612 add_keepsyms_file (filename)
613 const char *filename;
620 if (link_info.strip == strip_some)
621 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
623 file = fopen (filename, "r");
624 if (file == (FILE *) NULL)
626 bfd_set_error (bfd_error_system_call);
627 einfo ("%X%P: %s: %E\n", filename);
631 link_info.keep_hash = ((struct bfd_hash_table *)
632 xmalloc (sizeof (struct bfd_hash_table)));
633 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
634 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
637 buf = (char *) xmalloc (bufsize);
649 while (! isspace (c) && c != EOF)
656 buf = xrealloc (buf, bufsize);
663 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
664 == (struct bfd_hash_entry *) NULL)
665 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
669 if (link_info.strip != strip_none)
670 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
672 link_info.strip = strip_some;
675 /* Callbacks from the BFD linker routines. */
677 /* This is called when BFD has decided to include an archive member in
682 add_archive_element (info, abfd, name)
683 struct bfd_link_info *info;
687 lang_input_statement_type *input;
689 input = ((lang_input_statement_type *)
690 xmalloc (sizeof (lang_input_statement_type)));
691 input->filename = abfd->filename;
692 input->local_sym_name = abfd->filename;
693 input->the_bfd = abfd;
694 input->asymbols = NULL;
696 input->just_syms_flag = false;
697 input->loaded = false;
698 input->search_dirs_flag = false;
700 /* FIXME: The following fields are not set: header.next,
701 header.type, closed, passive_position, symbol_count,
702 next_real_file, is_archive, target, real. This bit of code is
703 from the old decode_library_subfile function. I don't know
704 whether any of those fields matters. */
706 ldlang_add_file (input);
708 if (config.map_file != (FILE *) NULL)
710 static boolean header_printed;
711 struct bfd_link_hash_entry *h;
715 h = bfd_link_hash_lookup (link_info.hash, name, false, false, true);
727 case bfd_link_hash_defined:
728 case bfd_link_hash_defweak:
729 from = h->u.def.section->owner;
732 case bfd_link_hash_undefined:
733 case bfd_link_hash_undefweak:
734 from = h->u.undef.abfd;
737 case bfd_link_hash_common:
738 from = h->u.c.p->section->owner;
743 if (! header_printed)
747 sprintf (buf, "%-29s %s\n\n", _("Archive member included"),
748 _("because of file (symbol)"));
750 header_printed = true;
753 if (bfd_my_archive (abfd) == NULL)
755 minfo ("%s", bfd_get_filename (abfd));
756 len = strlen (bfd_get_filename (abfd));
760 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)),
761 bfd_get_filename (abfd));
762 len = (strlen (bfd_get_filename (bfd_my_archive (abfd)))
763 + strlen (bfd_get_filename (abfd))
781 minfo ("(%T)\n", h->root.string);
783 minfo ("(%s)\n", name);
786 if (trace_files || trace_file_tries)
787 info_msg ("%I\n", input);
792 /* This is called when BFD has discovered a symbol which is defined
797 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
798 struct bfd_link_info *info;
807 /* If either section has the output_section field set to
808 bfd_abs_section_ptr, it means that the section is being
809 discarded, and this is not really a multiple definition at all.
810 FIXME: It would be cleaner to somehow ignore symbols defined in
811 sections which are being discarded. */
812 if ((osec->output_section != NULL
813 && ! bfd_is_abs_section (osec)
814 && bfd_is_abs_section (osec->output_section))
815 || (nsec->output_section != NULL
816 && ! bfd_is_abs_section (nsec)
817 && bfd_is_abs_section (nsec->output_section)))
820 einfo (_("%X%C: multiple definition of `%T'\n"),
821 nbfd, nsec, nval, name);
822 if (obfd != (bfd *) NULL)
823 einfo (_("%D: first defined here\n"), obfd, osec, oval);
827 /* This is called when there is a definition of a common symbol, or
828 when a common symbol is found for a symbol that is already defined,
829 or when two common symbols are found. We only do something if
830 -warn-common was used. */
834 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
835 struct bfd_link_info *info;
838 enum bfd_link_hash_type otype;
841 enum bfd_link_hash_type ntype;
844 if (! config.warn_common)
847 if (ntype == bfd_link_hash_defined
848 || ntype == bfd_link_hash_defweak
849 || ntype == bfd_link_hash_indirect)
851 ASSERT (otype == bfd_link_hash_common);
852 einfo (_("%B: warning: definition of `%T' overriding common\n"),
855 einfo (_("%B: warning: common is here\n"), obfd);
857 else if (otype == bfd_link_hash_defined
858 || otype == bfd_link_hash_defweak
859 || otype == bfd_link_hash_indirect)
861 ASSERT (ntype == bfd_link_hash_common);
862 einfo (_("%B: warning: common of `%T' overridden by definition\n"),
865 einfo (_("%B: warning: defined here\n"), obfd);
869 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
872 einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
875 einfo (_("%B: warning: larger common is here\n"), obfd);
877 else if (nsize > osize)
879 einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
882 einfo (_("%B: warning: smaller common is here\n"), obfd);
886 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name);
888 einfo (_("%B: warning: previous common is here\n"), obfd);
895 /* This is called when BFD has discovered a set element. H is the
896 entry in the linker hash table for the set. SECTION and VALUE
897 represent a value which should be added to the set. */
901 add_to_set (info, h, reloc, abfd, section, value)
902 struct bfd_link_info *info;
903 struct bfd_link_hash_entry *h;
904 bfd_reloc_code_real_type reloc;
909 if (config.warn_constructors)
910 einfo (_("%P: warning: global constructor %s used\n"),
913 if (! config.build_constructors)
916 ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value);
918 if (h->type == bfd_link_hash_new)
920 h->type = bfd_link_hash_undefined;
921 h->u.undef.abfd = abfd;
922 /* We don't call bfd_link_add_undef to add this to the list of
923 undefined symbols because we are going to define it
930 /* This is called when BFD has discovered a constructor. This is only
931 called for some object file formats--those which do not handle
932 constructors in some more clever fashion. This is similar to
933 adding an element to a set, but less general. */
936 constructor_callback (info, constructor, name, abfd, section, value)
937 struct bfd_link_info *info;
945 struct bfd_link_hash_entry *h;
946 char set_name[1 + sizeof "__CTOR_LIST__"];
948 if (config.warn_constructors)
949 einfo (_("%P: warning: global constructor %s used\n"), name);
951 if (! config.build_constructors)
954 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
955 useful error message. */
956 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL
957 && (link_info.relocateable
958 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
959 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
962 if (bfd_get_symbol_leading_char (abfd) != '\0')
963 *s++ = bfd_get_symbol_leading_char (abfd);
965 strcpy (s, "__CTOR_LIST__");
967 strcpy (s, "__DTOR_LIST__");
969 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
970 if (h == (struct bfd_link_hash_entry *) NULL)
971 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
972 if (h->type == bfd_link_hash_new)
974 h->type = bfd_link_hash_undefined;
975 h->u.undef.abfd = abfd;
976 /* We don't call bfd_link_add_undef to add this to the list of
977 undefined symbols because we are going to define it
981 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
985 /* A structure used by warning_callback to pass information through
986 bfd_map_over_sections. */
988 struct warning_callback_info
996 /* This is called when there is a reference to a warning symbol. */
1000 warning_callback (info, warning, symbol, abfd, section, address)
1001 struct bfd_link_info *info;
1002 const char *warning;
1008 /* This is a hack to support warn_multiple_gp. FIXME: This should
1009 have a cleaner interface, but what? */
1010 if (! config.warn_multiple_gp
1011 && strcmp (warning, "using multiple gp values") == 0)
1014 if (section != NULL)
1015 einfo ("%C: %s\n", abfd, section, address, warning);
1016 else if (abfd == NULL)
1017 einfo ("%P: %s\n", warning);
1018 else if (symbol == NULL)
1019 einfo ("%B: %s\n", abfd, warning);
1022 lang_input_statement_type *entry;
1024 struct warning_callback_info info;
1026 /* Look through the relocs to see if we can find a plausible
1029 entry = (lang_input_statement_type *) abfd->usrdata;
1030 if (entry != NULL && entry->asymbols != NULL)
1031 asymbols = entry->asymbols;
1037 symsize = bfd_get_symtab_upper_bound (abfd);
1039 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1040 asymbols = (asymbol **) xmalloc (symsize);
1041 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
1042 if (symbol_count < 0)
1043 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1046 entry->asymbols = asymbols;
1047 entry->symbol_count = symbol_count;
1052 info.warning = warning;
1053 info.symbol = symbol;
1054 info.asymbols = asymbols;
1055 bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info);
1058 einfo ("%B: %s\n", abfd, warning);
1067 /* This is called by warning_callback for each section. It checks the
1068 relocs of the section to see if it can find a reference to the
1069 symbol which triggered the warning. If it can, it uses the reloc
1070 to give an error message with a file and line number. */
1073 warning_find_reloc (abfd, sec, iarg)
1078 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
1082 arelent **p, **pend;
1087 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1089 einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1093 relpp = (arelent **) xmalloc (relsize);
1094 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1096 einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1099 pend = p + relcount;
1100 for (; p < pend && *p != NULL; p++)
1104 if (q->sym_ptr_ptr != NULL
1105 && *q->sym_ptr_ptr != NULL
1106 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1108 /* We found a reloc for the symbol we are looking for. */
1109 einfo ("%C: %s\n", abfd, sec, q->address, info->warning);
1118 /* This is called when an undefined symbol is found. */
1122 undefined_symbol (info, name, abfd, section, address)
1123 struct bfd_link_info *info;
1129 static char *error_name;
1130 static unsigned int error_count;
1132 #define MAX_ERRORS_IN_A_ROW 5
1134 if (config.warn_once)
1136 static struct bfd_hash_table *hash;
1138 /* Only warn once about a particular undefined symbol. */
1142 hash = ((struct bfd_hash_table *)
1143 xmalloc (sizeof (struct bfd_hash_table)));
1144 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
1145 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
1148 if (bfd_hash_lookup (hash, name, false, false) != NULL)
1151 if (bfd_hash_lookup (hash, name, true, true) == NULL)
1152 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
1155 /* We never print more than a reasonable number of errors in a row
1156 for a single symbol. */
1157 if (error_name != (char *) NULL
1158 && strcmp (name, error_name) == 0)
1163 if (error_name != (char *) NULL)
1165 error_name = buystring (name);
1168 if (section != NULL)
1170 if (error_count < MAX_ERRORS_IN_A_ROW)
1171 einfo (_("%X%C: undefined reference to `%T'\n"),
1172 abfd, section, address, name);
1173 else if (error_count == MAX_ERRORS_IN_A_ROW)
1174 einfo (_("%D: more undefined references to `%T' follow\n"),
1175 abfd, section, address, name);
1179 if (error_count < MAX_ERRORS_IN_A_ROW)
1180 einfo (_("%X%B: undefined reference to `%T'\n"),
1182 else if (error_count == MAX_ERRORS_IN_A_ROW)
1183 einfo (_("%B: more undefined references to `%T' follow\n"),
1190 /* This is called when a reloc overflows. */
1194 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
1195 struct bfd_link_info *info;
1197 const char *reloc_name;
1203 if (abfd == (bfd *) NULL)
1204 einfo (_("%P%X: generated"));
1206 einfo ("%X%C:", abfd, section, address);
1207 einfo (_(" relocation truncated to fit: %s %T"), reloc_name, name);
1209 einfo ("+%v", addend);
1214 /* This is called when a dangerous relocation is made. */
1218 reloc_dangerous (info, message, abfd, section, address)
1219 struct bfd_link_info *info;
1220 const char *message;
1225 if (abfd == (bfd *) NULL)
1226 einfo (_("%P%X: generated"));
1228 einfo ("%X%C:", abfd, section, address);
1229 einfo (_("dangerous relocation: %s\n"), message);
1233 /* This is called when a reloc is being generated attached to a symbol
1234 that is not being output. */
1238 unattached_reloc (info, name, abfd, section, address)
1239 struct bfd_link_info *info;
1245 if (abfd == (bfd *) NULL)
1246 einfo (_("%P%X: generated"));
1248 einfo ("%X%C:", abfd, section, address);
1249 einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name);
1253 /* This is called if link_info.notice_all is set, or when a symbol in
1254 link_info.notice_hash is found. Symbols are put in notice_hash
1255 using the -y option. */
1258 notice (info, name, abfd, section, value)
1259 struct bfd_link_info *info;
1265 if (! info->notice_all
1266 || (info->notice_hash != NULL
1267 && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL))
1269 if (bfd_is_und_section (section))
1270 einfo ("%B: reference to %s\n", abfd, name);
1272 einfo ("%B: definition of %s\n", abfd, name);
1275 if (command_line.cref || nocrossref_list != NULL)
1276 add_cref (name, abfd, section, value);