2001-07-11 Jakub Jelinek <jakub@redhat.com>
[binutils.git] / ld / ldmain.c
blobed0abd6639a08df0539275beb5e552958950750b
1 /* Main program of GNU linker.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain steve@cygnus.com
6 This file is part of GLD, the Gnu Linker.
8 GLD is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GLD is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GLD; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include <stdio.h>
26 #include <ctype.h>
27 #include "libiberty.h"
28 #include "progress.h"
29 #include "bfdlink.h"
30 #include "filenames.h"
32 #include "ld.h"
33 #include "ldmain.h"
34 #include "ldmisc.h"
35 #include "ldwrite.h"
36 #include "ldgram.h"
37 #include "ldexp.h"
38 #include "ldlang.h"
39 #include "ldlex.h"
40 #include "ldfile.h"
41 #include "ldemul.h"
42 #include "ldctor.h"
44 /* Somewhere above, sys/stat.h got included . . . . */
45 #if !defined(S_ISDIR) && defined(S_IFDIR)
46 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
47 #endif
49 #include <string.h>
51 #ifdef HAVE_SBRK
52 #ifdef NEED_DECLARATION_SBRK
53 extern PTR sbrk ();
54 #endif
55 #endif
57 static char *get_emulation PARAMS ((int, char **));
58 static void set_scripts_dir PARAMS ((void));
60 /* EXPORTS */
62 char *default_target;
63 const char *output_filename = "a.out";
65 /* Name this program was invoked by. */
66 char *program_name;
68 /* The file that we're creating. */
69 bfd *output_bfd = 0;
71 /* Set by -G argument, for MIPS ECOFF target. */
72 int g_switch_value = 8;
74 /* Nonzero means print names of input files as processed. */
75 boolean trace_files;
77 /* Nonzero means same, but note open failures, too. */
78 boolean trace_file_tries;
80 /* Nonzero means version number was printed, so exit successfully
81 instead of complaining if no input files are given. */
82 boolean version_printed;
84 /* Nonzero means link in every member of an archive. */
85 boolean whole_archive;
87 /* True if we should demangle symbol names. */
88 boolean demangling;
90 args_type command_line;
92 ld_config_type config;
94 static void remove_output PARAMS ((void));
95 static boolean check_for_scripts_dir PARAMS ((char *dir));
96 static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
97 const char *));
98 static boolean multiple_definition PARAMS ((struct bfd_link_info *,
99 const char *,
100 bfd *, asection *, bfd_vma,
101 bfd *, asection *, bfd_vma));
102 static boolean multiple_common PARAMS ((struct bfd_link_info *,
103 const char *, bfd *,
104 enum bfd_link_hash_type, bfd_vma,
105 bfd *, enum bfd_link_hash_type,
106 bfd_vma));
107 static boolean add_to_set PARAMS ((struct bfd_link_info *,
108 struct bfd_link_hash_entry *,
109 bfd_reloc_code_real_type,
110 bfd *, asection *, bfd_vma));
111 static boolean constructor_callback PARAMS ((struct bfd_link_info *,
112 boolean constructor,
113 const char *name,
114 bfd *, asection *, bfd_vma));
115 static boolean warning_callback PARAMS ((struct bfd_link_info *,
116 const char *, const char *, bfd *,
117 asection *, bfd_vma));
118 static void warning_find_reloc PARAMS ((bfd *, asection *, PTR));
119 static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
120 const char *, bfd *,
121 asection *, bfd_vma, boolean));
122 static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
123 const char *, bfd_vma,
124 bfd *, asection *, bfd_vma));
125 static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
126 bfd *, asection *, bfd_vma));
127 static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
128 const char *, bfd *, asection *,
129 bfd_vma));
130 static boolean notice PARAMS ((struct bfd_link_info *, const char *,
131 bfd *, asection *, bfd_vma));
133 static struct bfd_link_callbacks link_callbacks = {
134 add_archive_element,
135 multiple_definition,
136 multiple_common,
137 add_to_set,
138 constructor_callback,
139 warning_callback,
140 undefined_symbol,
141 reloc_overflow,
142 reloc_dangerous,
143 unattached_reloc,
144 notice
147 struct bfd_link_info link_info;
149 static void
150 remove_output ()
152 if (output_filename)
154 if (output_bfd && output_bfd->iostream)
155 fclose ((FILE *) (output_bfd->iostream));
156 if (delete_output_file_on_failure)
157 unlink (output_filename);
162 main (argc, argv)
163 int argc;
164 char **argv;
166 char *emulation;
167 long start_time = get_run_time ();
169 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
170 setlocale (LC_MESSAGES, "");
171 #endif
172 bindtextdomain (PACKAGE, LOCALEDIR);
173 textdomain (PACKAGE);
175 program_name = argv[0];
176 xmalloc_set_program_name (program_name);
178 START_PROGRESS (program_name, 0);
180 bfd_init ();
182 bfd_set_error_program_name (program_name);
184 xatexit (remove_output);
186 /* Set the default BFD target based on the configured target. Doing
187 this permits the linker to be configured for a particular target,
188 and linked against a shared BFD library which was configured for
189 a different target. The macro TARGET is defined by Makefile. */
190 if (! bfd_set_default_target (TARGET))
192 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
193 xexit (1);
196 /* Initialize the data about options. */
197 trace_files = trace_file_tries = version_printed = false;
198 whole_archive = false;
199 config.build_constructors = true;
200 config.dynamic_link = false;
201 config.has_shared = false;
202 config.split_by_reloc = (unsigned) -1;
203 config.split_by_file = (bfd_size_type) -1;
204 command_line.force_common_definition = false;
205 command_line.interpreter = NULL;
206 command_line.rpath = NULL;
207 command_line.warn_mismatch = true;
208 command_line.check_section_addresses = true;
210 /* We initialize DEMANGLING based on the environment variable
211 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
212 output of the linker, unless COLLECT_NO_DEMANGLE is set in the
213 environment. Acting the same way here lets us provide the same
214 interface by default. */
215 demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
217 link_info.callbacks = &link_callbacks;
218 link_info.relocateable = false;
219 link_info.emitrelocations = false;
220 link_info.shared = false;
221 link_info.symbolic = false;
222 link_info.export_dynamic = false;
223 link_info.static_link = false;
224 link_info.traditional_format = false;
225 link_info.optimize = false;
226 link_info.no_undefined = false;
227 link_info.allow_shlib_undefined = false;
228 link_info.strip = strip_none;
229 link_info.discard = discard_sec_merge;
230 link_info.keep_memory = true;
231 link_info.input_bfds = NULL;
232 link_info.create_object_symbols_section = NULL;
233 link_info.hash = NULL;
234 link_info.keep_hash = NULL;
235 link_info.notice_all = false;
236 link_info.notice_hash = NULL;
237 link_info.wrap_hash = NULL;
238 link_info.mpc860c0 = 0;
239 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
240 and _fini symbols. We are compatible. */
241 link_info.init_function = "_init";
242 link_info.fini_function = "_fini";
243 link_info.new_dtags = false;
244 link_info.flags = (bfd_vma) 0;
245 link_info.flags_1 = (bfd_vma) 0;
247 ldfile_add_arch ("");
249 config.make_executable = true;
250 force_make_executable = false;
251 config.magic_demand_paged = true;
252 config.text_read_only = true;
254 emulation = get_emulation (argc, argv);
255 ldemul_choose_mode (emulation);
256 default_target = ldemul_choose_target ();
257 lang_init ();
258 ldemul_before_parse ();
259 lang_has_input_file = false;
260 parse_args (argc, argv);
262 ldemul_set_symbols ();
264 if (link_info.relocateable)
266 if (command_line.gc_sections)
267 einfo ("%P%F: --gc-sections and -r may not be used together\n");
268 if (link_info.mpc860c0)
269 einfo (_("%P%F: -r and --mpc860c0 may not be used together\n"));
270 else if (command_line.relax)
271 einfo (_("%P%F: --relax and -r may not be used together\n"));
272 if (link_info.shared)
273 einfo (_("%P%F: -r and -shared may not be used together\n"));
276 if (! link_info.shared)
278 if (command_line.filter_shlib)
279 einfo ("%P%F: -F may not be used without -shared\n"));
280 if (command_line.auxiliary_filters)
281 einfo ("%P%F: -f may not be used without -shared\n"));
284 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
285 don't see how else this can be handled, since in this case we
286 must preserve all externally visible symbols. */
287 if (link_info.relocateable && link_info.strip == strip_all)
289 link_info.strip = strip_debugger;
290 if (link_info.discard == discard_sec_merge)
291 link_info.discard = discard_all;
294 /* This essentially adds another -L directory so this must be done after
295 the -L's in argv have been processed. */
296 set_scripts_dir ();
298 if (had_script == false)
300 /* Read the emulation's appropriate default script. */
301 int isfile;
302 char *s = ldemul_get_script (&isfile);
304 if (isfile)
305 ldfile_open_command_file (s);
306 else
308 if (trace_file_tries)
310 info_msg (_("using internal linker script:\n"));
311 info_msg ("==================================================\n");
312 info_msg (s);
313 info_msg ("\n==================================================\n");
315 lex_string = s;
316 lex_redirect (s);
318 parser_input = input_script;
319 yyparse ();
320 lex_string = NULL;
323 lang_final ();
325 if (lang_has_input_file == false)
327 if (version_printed)
328 xexit (0);
329 einfo (_("%P%F: no input files\n"));
332 if (trace_files)
334 info_msg (_("%P: mode %s\n"), emulation);
337 ldemul_after_parse ();
339 if (config.map_filename)
341 if (strcmp (config.map_filename, "-") == 0)
343 config.map_file = stdout;
345 else
347 config.map_file = fopen (config.map_filename, FOPEN_WT);
348 if (config.map_file == (FILE *) NULL)
350 bfd_set_error (bfd_error_system_call);
351 einfo (_("%P%F: cannot open map file %s: %E\n"),
352 config.map_filename);
357 lang_process ();
359 /* Print error messages for any missing symbols, for any warning
360 symbols, and possibly multiple definitions. */
362 if (! link_info.relocateable)
364 /* Look for a text section and switch the readonly attribute in it. */
365 asection *found = bfd_get_section_by_name (output_bfd, ".text");
367 if (found != (asection *) NULL)
369 if (config.text_read_only)
370 found->flags |= SEC_READONLY;
371 else
372 found->flags &= ~SEC_READONLY;
376 if (link_info.relocateable)
377 output_bfd->flags &= ~EXEC_P;
378 else
379 output_bfd->flags |= EXEC_P;
381 ldwrite ();
383 if (config.map_file != NULL)
384 lang_map ();
385 if (command_line.cref)
386 output_cref (config.map_file != NULL ? config.map_file : stdout);
387 if (nocrossref_list != NULL)
388 check_nocrossrefs ();
390 /* Even if we're producing relocateable output, some non-fatal errors should
391 be reported in the exit status. (What non-fatal errors, if any, do we
392 want to ignore for relocateable output?) */
394 if (config.make_executable == false && force_make_executable == false)
396 if (trace_files == true)
398 einfo (_("%P: link errors found, deleting executable `%s'\n"),
399 output_filename);
402 /* The file will be removed by remove_output. */
404 xexit (1);
406 else
408 if (! bfd_close (output_bfd))
409 einfo (_("%F%B: final close failed: %E\n"), output_bfd);
411 /* If the --force-exe-suffix is enabled, and we're making an
412 executable file and it doesn't end in .exe, copy it to one
413 which does. */
414 if (! link_info.relocateable && command_line.force_exe_suffix)
416 int len = strlen (output_filename);
417 if (len < 4
418 || (strcasecmp (output_filename + len - 4, ".exe") != 0
419 && strcasecmp (output_filename + len - 4, ".dll") != 0))
421 FILE *src;
422 FILE *dst;
423 const int bsize = 4096;
424 char *buf = xmalloc (bsize);
425 int l;
426 char *dst_name = xmalloc (len + 5);
427 strcpy (dst_name, output_filename);
428 strcat (dst_name, ".exe");
429 src = fopen (output_filename, FOPEN_RB);
430 dst = fopen (dst_name, FOPEN_WB);
432 if (!src)
433 einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename);
434 if (!dst)
435 einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name);
436 while ((l = fread (buf, 1, bsize, src)) > 0)
438 int done = fwrite (buf, 1, l, dst);
439 if (done != l)
441 einfo (_("%P: Error writing file `%s'\n"), dst_name);
444 fclose (src);
445 if (fclose (dst) == EOF)
447 einfo (_("%P: Error closing file `%s'\n"), dst_name);
449 free (dst_name);
450 free (buf);
455 END_PROGRESS (program_name);
457 if (config.stats)
459 #ifdef HAVE_SBRK
460 char *lim = (char *) sbrk (0);
461 #endif
462 long run_time = get_run_time () - start_time;
464 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
465 program_name, run_time / 1000000, run_time % 1000000);
466 #ifdef HAVE_SBRK
467 fprintf (stderr, _("%s: data size %ld\n"), program_name,
468 (long) (lim - (char *) &environ));
469 #endif
472 /* Prevent remove_output from doing anything, after a successful link. */
473 output_filename = NULL;
475 xexit (0);
476 return 0;
479 /* We need to find any explicitly given emulation in order to initialize the
480 state that's needed by the lex&yacc argument parser (parse_args). */
482 static char *
483 get_emulation (argc, argv)
484 int argc;
485 char **argv;
487 char *emulation;
488 int i;
490 emulation = getenv (EMULATION_ENVIRON);
491 if (emulation == NULL)
492 emulation = DEFAULT_EMULATION;
494 for (i = 1; i < argc; i++)
496 if (!strncmp (argv[i], "-m", 2))
498 if (argv[i][2] == '\0')
500 /* -m EMUL */
501 if (i < argc - 1)
503 emulation = argv[i + 1];
504 i++;
506 else
508 einfo (_("%P%F: missing argument to -m\n"));
511 else if (strcmp (argv[i], "-mips1") == 0
512 || strcmp (argv[i], "-mips2") == 0
513 || strcmp (argv[i], "-mips3") == 0
514 || strcmp (argv[i], "-mips4") == 0
515 || strcmp (argv[i], "-mips5") == 0)
517 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
518 passed to the linker by some MIPS compilers. They
519 generally tell the linker to use a slightly different
520 library path. Perhaps someday these should be
521 implemented as emulations; until then, we just ignore
522 the arguments and hope that nobody ever creates
523 emulations named ips1, ips2 or ips3. */
525 else if (strcmp (argv[i], "-m486") == 0)
527 /* FIXME: The argument -m486 is passed to the linker on
528 some Linux systems. Hope that nobody creates an
529 emulation named 486. */
531 else
533 /* -mEMUL */
534 emulation = &argv[i][2];
539 return emulation;
542 /* If directory DIR contains an "ldscripts" subdirectory,
543 add DIR to the library search path and return true,
544 else return false. */
546 static boolean
547 check_for_scripts_dir (dir)
548 char *dir;
550 size_t dirlen;
551 char *buf;
552 struct stat s;
553 boolean res;
555 dirlen = strlen (dir);
556 /* sizeof counts the terminating NUL. */
557 buf = (char *) xmalloc (dirlen + sizeof ("/ldscripts"));
558 sprintf (buf, "%s/ldscripts", dir);
560 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
561 free (buf);
562 if (res)
563 ldfile_add_library_path (dir, false);
564 return res;
567 /* Set the default directory for finding script files.
568 Libraries will be searched for here too, but that's ok.
569 We look for the "ldscripts" directory in:
571 SCRIPTDIR (passed from Makefile)
572 the dir where this program is (for using it from the build tree)
573 the dir where this program is/../lib (for installing the tool suite elsewhere) */
575 static void
576 set_scripts_dir ()
578 char *end, *dir;
579 size_t dirlen;
581 if (check_for_scripts_dir (SCRIPTDIR))
582 /* We've been installed normally. */
583 return;
585 /* Look for "ldscripts" in the dir where our binary is. */
586 end = strrchr (program_name, '/');
587 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
589 /* We could have \foo\bar, or /foo\bar. */
590 char *bslash = strrchr (program_name, '\\');
591 if (end == NULL || (bslash != NULL && bslash > end))
592 end = bslash;
594 #endif
596 if (end == NULL)
598 /* Don't look for ldscripts in the current directory. There is
599 too much potential for confusion. */
600 return;
603 dirlen = end - program_name;
604 /* Make a copy of program_name in dir.
605 Leave room for later "/../lib". */
606 dir = (char *) xmalloc (dirlen + 8);
607 strncpy (dir, program_name, dirlen);
608 dir[dirlen] = '\0';
610 if (check_for_scripts_dir (dir))
611 /* Don't free dir. */
612 return;
614 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
615 strcpy (dir + dirlen, "/../lib");
616 if (check_for_scripts_dir (dir))
617 return;
619 /* Well, we tried. */
620 free (dir);
623 void
624 add_ysym (name)
625 const char *name;
627 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
629 link_info.notice_hash = ((struct bfd_hash_table *)
630 xmalloc (sizeof (struct bfd_hash_table)));
631 if (! bfd_hash_table_init_n (link_info.notice_hash,
632 bfd_hash_newfunc,
633 61))
634 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
637 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
638 == (struct bfd_hash_entry *) NULL)
639 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
642 /* Record a symbol to be wrapped, from the --wrap option. */
644 void
645 add_wrap (name)
646 const char *name;
648 if (link_info.wrap_hash == NULL)
650 link_info.wrap_hash = ((struct bfd_hash_table *)
651 xmalloc (sizeof (struct bfd_hash_table)));
652 if (! bfd_hash_table_init_n (link_info.wrap_hash,
653 bfd_hash_newfunc,
654 61))
655 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
657 if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
658 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
661 /* Handle the -retain-symbols-file option. */
663 void
664 add_keepsyms_file (filename)
665 const char *filename;
667 FILE *file;
668 char *buf;
669 size_t bufsize;
670 int c;
672 if (link_info.strip == strip_some)
673 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
675 file = fopen (filename, "r");
676 if (file == (FILE *) NULL)
678 bfd_set_error (bfd_error_system_call);
679 einfo ("%X%P: %s: %E\n", filename);
680 return;
683 link_info.keep_hash = ((struct bfd_hash_table *)
684 xmalloc (sizeof (struct bfd_hash_table)));
685 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
686 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
688 bufsize = 100;
689 buf = (char *) xmalloc (bufsize);
691 c = getc (file);
692 while (c != EOF)
694 while (isspace (c))
695 c = getc (file);
697 if (c != EOF)
699 size_t len = 0;
701 while (! isspace (c) && c != EOF)
703 buf[len] = c;
704 ++len;
705 if (len >= bufsize)
707 bufsize *= 2;
708 buf = xrealloc (buf, bufsize);
710 c = getc (file);
713 buf[len] = '\0';
715 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
716 == (struct bfd_hash_entry *) NULL)
717 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
721 if (link_info.strip != strip_none)
722 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
724 link_info.strip = strip_some;
727 /* Callbacks from the BFD linker routines. */
729 /* This is called when BFD has decided to include an archive member in
730 a link. */
732 static boolean
733 add_archive_element (info, abfd, name)
734 struct bfd_link_info *info ATTRIBUTE_UNUSED;
735 bfd *abfd;
736 const char *name;
738 lang_input_statement_type *input;
740 input = ((lang_input_statement_type *)
741 xmalloc (sizeof (lang_input_statement_type)));
742 input->filename = abfd->filename;
743 input->local_sym_name = abfd->filename;
744 input->the_bfd = abfd;
745 input->asymbols = NULL;
746 input->next = NULL;
747 input->just_syms_flag = false;
748 input->loaded = false;
749 input->search_dirs_flag = false;
751 /* FIXME: The following fields are not set: header.next,
752 header.type, closed, passive_position, symbol_count,
753 next_real_file, is_archive, target, real. This bit of code is
754 from the old decode_library_subfile function. I don't know
755 whether any of those fields matters. */
757 ldlang_add_file (input);
759 if (config.map_file != (FILE *) NULL)
761 static boolean header_printed;
762 struct bfd_link_hash_entry *h;
763 bfd *from;
764 int len;
766 h = bfd_link_hash_lookup (link_info.hash, name, false, false, true);
768 if (h == NULL)
769 from = NULL;
770 else
772 switch (h->type)
774 default:
775 from = NULL;
776 break;
778 case bfd_link_hash_defined:
779 case bfd_link_hash_defweak:
780 from = h->u.def.section->owner;
781 break;
783 case bfd_link_hash_undefined:
784 case bfd_link_hash_undefweak:
785 from = h->u.undef.abfd;
786 break;
788 case bfd_link_hash_common:
789 from = h->u.c.p->section->owner;
790 break;
794 if (! header_printed)
796 char buf[100];
798 sprintf (buf, "%-29s %s\n\n", _("Archive member included"),
799 _("because of file (symbol)"));
800 minfo ("%s", buf);
801 header_printed = true;
804 if (bfd_my_archive (abfd) == NULL)
806 minfo ("%s", bfd_get_filename (abfd));
807 len = strlen (bfd_get_filename (abfd));
809 else
811 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)),
812 bfd_get_filename (abfd));
813 len = (strlen (bfd_get_filename (bfd_my_archive (abfd)))
814 + strlen (bfd_get_filename (abfd))
815 + 2);
818 if (len >= 29)
820 print_nl ();
821 len = 0;
823 while (len < 30)
825 print_space ();
826 ++len;
829 if (from != NULL)
830 minfo ("%B ", from);
831 if (h != NULL)
832 minfo ("(%T)\n", h->root.string);
833 else
834 minfo ("(%s)\n", name);
837 if (trace_files || trace_file_tries)
838 info_msg ("%I\n", input);
840 return true;
843 /* This is called when BFD has discovered a symbol which is defined
844 multiple times. */
846 static boolean
847 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
848 struct bfd_link_info *info ATTRIBUTE_UNUSED;
849 const char *name;
850 bfd *obfd;
851 asection *osec;
852 bfd_vma oval;
853 bfd *nbfd;
854 asection *nsec;
855 bfd_vma nval;
857 /* If either section has the output_section field set to
858 bfd_abs_section_ptr, it means that the section is being
859 discarded, and this is not really a multiple definition at all.
860 FIXME: It would be cleaner to somehow ignore symbols defined in
861 sections which are being discarded. */
862 if ((osec->output_section != NULL
863 && ! bfd_is_abs_section (osec)
864 && bfd_is_abs_section (osec->output_section))
865 || (nsec->output_section != NULL
866 && ! bfd_is_abs_section (nsec)
867 && bfd_is_abs_section (nsec->output_section)))
868 return true;
870 einfo (_("%X%C: multiple definition of `%T'\n"),
871 nbfd, nsec, nval, name);
872 if (obfd != (bfd *) NULL)
873 einfo (_("%D: first defined here\n"), obfd, osec, oval);
875 if (command_line.relax)
877 einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
878 command_line.relax = 0;
881 return true;
884 /* This is called when there is a definition of a common symbol, or
885 when a common symbol is found for a symbol that is already defined,
886 or when two common symbols are found. We only do something if
887 -warn-common was used. */
889 static boolean
890 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
891 struct bfd_link_info *info ATTRIBUTE_UNUSED;
892 const char *name;
893 bfd *obfd;
894 enum bfd_link_hash_type otype;
895 bfd_vma osize;
896 bfd *nbfd;
897 enum bfd_link_hash_type ntype;
898 bfd_vma nsize;
900 if (! config.warn_common)
901 return true;
903 if (ntype == bfd_link_hash_defined
904 || ntype == bfd_link_hash_defweak
905 || ntype == bfd_link_hash_indirect)
907 ASSERT (otype == bfd_link_hash_common);
908 einfo (_("%B: warning: definition of `%T' overriding common\n"),
909 nbfd, name);
910 if (obfd != NULL)
911 einfo (_("%B: warning: common is here\n"), obfd);
913 else if (otype == bfd_link_hash_defined
914 || otype == bfd_link_hash_defweak
915 || otype == bfd_link_hash_indirect)
917 ASSERT (ntype == bfd_link_hash_common);
918 einfo (_("%B: warning: common of `%T' overridden by definition\n"),
919 nbfd, name);
920 if (obfd != NULL)
921 einfo (_("%B: warning: defined here\n"), obfd);
923 else
925 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
926 if (osize > nsize)
928 einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
929 nbfd, name);
930 if (obfd != NULL)
931 einfo (_("%B: warning: larger common is here\n"), obfd);
933 else if (nsize > osize)
935 einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
936 nbfd, name);
937 if (obfd != NULL)
938 einfo (_("%B: warning: smaller common is here\n"), obfd);
940 else
942 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name);
943 if (obfd != NULL)
944 einfo (_("%B: warning: previous common is here\n"), obfd);
948 return true;
951 /* This is called when BFD has discovered a set element. H is the
952 entry in the linker hash table for the set. SECTION and VALUE
953 represent a value which should be added to the set. */
955 static boolean
956 add_to_set (info, h, reloc, abfd, section, value)
957 struct bfd_link_info *info ATTRIBUTE_UNUSED;
958 struct bfd_link_hash_entry *h;
959 bfd_reloc_code_real_type reloc;
960 bfd *abfd;
961 asection *section;
962 bfd_vma value;
964 if (config.warn_constructors)
965 einfo (_("%P: warning: global constructor %s used\n"),
966 h->root.string);
968 if (! config.build_constructors)
969 return true;
971 ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value);
973 if (h->type == bfd_link_hash_new)
975 h->type = bfd_link_hash_undefined;
976 h->u.undef.abfd = abfd;
977 /* We don't call bfd_link_add_undef to add this to the list of
978 undefined symbols because we are going to define it
979 ourselves. */
982 return true;
985 /* This is called when BFD has discovered a constructor. This is only
986 called for some object file formats--those which do not handle
987 constructors in some more clever fashion. This is similar to
988 adding an element to a set, but less general. */
990 static boolean
991 constructor_callback (info, constructor, name, abfd, section, value)
992 struct bfd_link_info *info;
993 boolean constructor;
994 const char *name;
995 bfd *abfd;
996 asection *section;
997 bfd_vma value;
999 char *s;
1000 struct bfd_link_hash_entry *h;
1001 char set_name[1 + sizeof "__CTOR_LIST__"];
1003 if (config.warn_constructors)
1004 einfo (_("%P: warning: global constructor %s used\n"), name);
1006 if (! config.build_constructors)
1007 return true;
1009 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1010 useful error message. */
1011 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL
1012 && (link_info.relocateable
1013 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
1014 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1016 s = set_name;
1017 if (bfd_get_symbol_leading_char (abfd) != '\0')
1018 *s++ = bfd_get_symbol_leading_char (abfd);
1019 if (constructor)
1020 strcpy (s, "__CTOR_LIST__");
1021 else
1022 strcpy (s, "__DTOR_LIST__");
1024 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
1025 if (h == (struct bfd_link_hash_entry *) NULL)
1026 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1027 if (h->type == bfd_link_hash_new)
1029 h->type = bfd_link_hash_undefined;
1030 h->u.undef.abfd = abfd;
1031 /* We don't call bfd_link_add_undef to add this to the list of
1032 undefined symbols because we are going to define it
1033 ourselves. */
1036 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
1037 return true;
1040 /* A structure used by warning_callback to pass information through
1041 bfd_map_over_sections. */
1043 struct warning_callback_info {
1044 boolean found;
1045 const char *warning;
1046 const char *symbol;
1047 asymbol **asymbols;
1050 /* This is called when there is a reference to a warning symbol. */
1052 static boolean
1053 warning_callback (info, warning, symbol, abfd, section, address)
1054 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1055 const char *warning;
1056 const char *symbol;
1057 bfd *abfd;
1058 asection *section;
1059 bfd_vma address;
1061 /* This is a hack to support warn_multiple_gp. FIXME: This should
1062 have a cleaner interface, but what? */
1063 if (! config.warn_multiple_gp
1064 && strcmp (warning, "using multiple gp values") == 0)
1065 return true;
1067 if (section != NULL)
1068 einfo ("%C: %s\n", abfd, section, address, warning);
1069 else if (abfd == NULL)
1070 einfo ("%P: %s\n", warning);
1071 else if (symbol == NULL)
1072 einfo ("%B: %s\n", abfd, warning);
1073 else
1075 lang_input_statement_type *entry;
1076 asymbol **asymbols;
1077 struct warning_callback_info info;
1079 /* Look through the relocs to see if we can find a plausible
1080 address. */
1082 entry = (lang_input_statement_type *) abfd->usrdata;
1083 if (entry != NULL && entry->asymbols != NULL)
1084 asymbols = entry->asymbols;
1085 else
1087 long symsize;
1088 long symbol_count;
1090 symsize = bfd_get_symtab_upper_bound (abfd);
1091 if (symsize < 0)
1092 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1093 asymbols = (asymbol **) xmalloc (symsize);
1094 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
1095 if (symbol_count < 0)
1096 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1097 if (entry != NULL)
1099 entry->asymbols = asymbols;
1100 entry->symbol_count = symbol_count;
1104 info.found = false;
1105 info.warning = warning;
1106 info.symbol = symbol;
1107 info.asymbols = asymbols;
1108 bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info);
1110 if (! info.found)
1111 einfo ("%B: %s\n", abfd, warning);
1113 if (entry == NULL)
1114 free (asymbols);
1117 return true;
1120 /* This is called by warning_callback for each section. It checks the
1121 relocs of the section to see if it can find a reference to the
1122 symbol which triggered the warning. If it can, it uses the reloc
1123 to give an error message with a file and line number. */
1125 static void
1126 warning_find_reloc (abfd, sec, iarg)
1127 bfd *abfd;
1128 asection *sec;
1129 PTR iarg;
1131 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
1132 long relsize;
1133 arelent **relpp;
1134 long relcount;
1135 arelent **p, **pend;
1137 if (info->found)
1138 return;
1140 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1141 if (relsize < 0)
1142 einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1143 if (relsize == 0)
1144 return;
1146 relpp = (arelent **) xmalloc (relsize);
1147 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1148 if (relcount < 0)
1149 einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1151 p = relpp;
1152 pend = p + relcount;
1153 for (; p < pend && *p != NULL; p++)
1155 arelent *q = *p;
1157 if (q->sym_ptr_ptr != NULL
1158 && *q->sym_ptr_ptr != NULL
1159 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1161 /* We found a reloc for the symbol we are looking for. */
1162 einfo ("%C: %s\n", abfd, sec, q->address, info->warning);
1163 info->found = true;
1164 break;
1168 free (relpp);
1171 /* This is called when an undefined symbol is found. */
1173 static boolean
1174 undefined_symbol (info, name, abfd, section, address, fatal)
1175 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1176 const char *name;
1177 bfd *abfd;
1178 asection *section;
1179 bfd_vma address;
1180 boolean fatal ATTRIBUTE_UNUSED;
1182 static char *error_name;
1183 static unsigned int error_count;
1185 #define MAX_ERRORS_IN_A_ROW 5
1187 if (config.warn_once)
1189 static struct bfd_hash_table *hash;
1191 /* Only warn once about a particular undefined symbol. */
1193 if (hash == NULL)
1195 hash = ((struct bfd_hash_table *)
1196 xmalloc (sizeof (struct bfd_hash_table)));
1197 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
1198 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
1201 if (bfd_hash_lookup (hash, name, false, false) != NULL)
1202 return true;
1204 if (bfd_hash_lookup (hash, name, true, true) == NULL)
1205 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
1208 /* We never print more than a reasonable number of errors in a row
1209 for a single symbol. */
1210 if (error_name != (char *) NULL
1211 && strcmp (name, error_name) == 0)
1212 ++error_count;
1213 else
1215 error_count = 0;
1216 if (error_name != (char *) NULL)
1217 free (error_name);
1218 error_name = xstrdup (name);
1221 if (section != NULL)
1223 if (error_count < MAX_ERRORS_IN_A_ROW)
1225 einfo (_("%C: undefined reference to `%T'\n"),
1226 abfd, section, address, name);
1227 if (fatal)
1228 einfo ("%X");
1230 else if (error_count == MAX_ERRORS_IN_A_ROW)
1231 einfo (_("%D: more undefined references to `%T' follow\n"),
1232 abfd, section, address, name);
1234 else
1236 if (error_count < MAX_ERRORS_IN_A_ROW)
1238 einfo (_("%B: undefined reference to `%T'\n"),
1239 abfd, name);
1240 if (fatal)
1241 einfo ("%X");
1243 else if (error_count == MAX_ERRORS_IN_A_ROW)
1244 einfo (_("%B: more undefined references to `%T' follow\n"),
1245 abfd, name);
1248 return true;
1251 /* This is called when a reloc overflows. */
1253 static boolean
1254 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
1255 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1256 const char *name;
1257 const char *reloc_name;
1258 bfd_vma addend;
1259 bfd *abfd;
1260 asection *section;
1261 bfd_vma address;
1263 if (abfd == (bfd *) NULL)
1264 einfo (_("%P%X: generated"));
1265 else
1266 einfo ("%X%C:", abfd, section, address);
1267 einfo (_(" relocation truncated to fit: %s %T"), reloc_name, name);
1268 if (addend != 0)
1269 einfo ("+%v", addend);
1270 einfo ("\n");
1271 return true;
1274 /* This is called when a dangerous relocation is made. */
1276 static boolean
1277 reloc_dangerous (info, message, abfd, section, address)
1278 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1279 const char *message;
1280 bfd *abfd;
1281 asection *section;
1282 bfd_vma address;
1284 if (abfd == (bfd *) NULL)
1285 einfo (_("%P%X: generated"));
1286 else
1287 einfo ("%X%C:", abfd, section, address);
1288 einfo (_("dangerous relocation: %s\n"), message);
1289 return true;
1292 /* This is called when a reloc is being generated attached to a symbol
1293 that is not being output. */
1295 static boolean
1296 unattached_reloc (info, name, abfd, section, address)
1297 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1298 const char *name;
1299 bfd *abfd;
1300 asection *section;
1301 bfd_vma address;
1303 if (abfd == (bfd *) NULL)
1304 einfo (_("%P%X: generated"));
1305 else
1306 einfo ("%X%C:", abfd, section, address);
1307 einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name);
1308 return true;
1311 /* This is called if link_info.notice_all is set, or when a symbol in
1312 link_info.notice_hash is found. Symbols are put in notice_hash
1313 using the -y option. */
1315 static boolean
1316 notice (info, name, abfd, section, value)
1317 struct bfd_link_info *info;
1318 const char *name;
1319 bfd *abfd;
1320 asection *section;
1321 bfd_vma value;
1323 if (! info->notice_all
1324 || (info->notice_hash != NULL
1325 && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL))
1327 if (bfd_is_und_section (section))
1328 einfo ("%B: reference to %s\n", abfd, name);
1329 else
1330 einfo ("%B: definition of %s\n", abfd, name);
1333 if (command_line.cref || nocrossref_list != NULL)
1334 add_cref (name, abfd, section, value);
1336 return true;