2008-01-30 Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
[binutils.git] / ld / ldmain.c
bloba9af255a395c3dd713ee6a6fa8eb483015944b75
1 /* Main program of GNU linker.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain steve@cygnus.com
7 This file is part of the GNU Binutils.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "safe-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 "ldexp.h"
37 #include "ldlang.h"
38 #include <ldgram.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 #if !HAVE_DECL_SBRK
53 extern void *sbrk ();
54 #endif
55 #endif
57 #ifndef TARGET_SYSTEM_ROOT
58 #define TARGET_SYSTEM_ROOT ""
59 #endif
61 /* EXPORTS */
63 FILE *saved_script_handle = NULL;
64 FILE *previous_script_handle = NULL;
65 bfd_boolean force_make_executable = FALSE;
67 char *default_target;
68 const char *output_filename = "a.out";
70 /* Name this program was invoked by. */
71 char *program_name;
73 /* The prefix for system library directories. */
74 const char *ld_sysroot;
76 /* The canonical representation of ld_sysroot. */
77 char * ld_canon_sysroot;
78 int ld_canon_sysroot_len;
80 /* The file that we're creating. */
81 bfd *output_bfd = 0;
83 /* Set by -G argument, for MIPS ECOFF target. */
84 int g_switch_value = 8;
86 /* Nonzero means print names of input files as processed. */
87 bfd_boolean trace_files;
89 /* Nonzero means same, but note open failures, too. */
90 bfd_boolean trace_file_tries;
92 /* Nonzero means version number was printed, so exit successfully
93 instead of complaining if no input files are given. */
94 bfd_boolean version_printed;
96 /* Nonzero means link in every member of an archive. */
97 bfd_boolean whole_archive;
99 /* Nonzero means create DT_NEEDED entries only if a dynamic library
100 actually satisfies some reference in a regular object. */
101 bfd_boolean as_needed;
103 /* Nonzero means never create DT_NEEDED entries for dynamic libraries
104 in DT_NEEDED tags. */
105 bfd_boolean add_needed = TRUE;
107 /* TRUE if we should demangle symbol names. */
108 bfd_boolean demangling;
110 args_type command_line;
112 ld_config_type config;
114 sort_type sort_section;
116 static const char *get_sysroot
117 (int, char **);
118 static char *get_emulation
119 (int, char **);
120 static void set_scripts_dir
121 (void);
122 static bfd_boolean add_archive_element
123 (struct bfd_link_info *, bfd *, const char *);
124 static bfd_boolean multiple_definition
125 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
126 bfd *, asection *, bfd_vma);
127 static bfd_boolean multiple_common
128 (struct bfd_link_info *, const char *, bfd *, enum bfd_link_hash_type,
129 bfd_vma, bfd *, enum bfd_link_hash_type, bfd_vma);
130 static bfd_boolean add_to_set
131 (struct bfd_link_info *, struct bfd_link_hash_entry *,
132 bfd_reloc_code_real_type, bfd *, asection *, bfd_vma);
133 static bfd_boolean constructor_callback
134 (struct bfd_link_info *, bfd_boolean, const char *, bfd *,
135 asection *, bfd_vma);
136 static bfd_boolean warning_callback
137 (struct bfd_link_info *, const char *, const char *, bfd *,
138 asection *, bfd_vma);
139 static void warning_find_reloc
140 (bfd *, asection *, void *);
141 static bfd_boolean undefined_symbol
142 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
143 bfd_boolean);
144 static bfd_boolean reloc_overflow
145 (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
146 const char *, bfd_vma, bfd *, asection *, bfd_vma);
147 static bfd_boolean reloc_dangerous
148 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
149 static bfd_boolean unattached_reloc
150 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
151 static bfd_boolean notice
152 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
154 static struct bfd_link_callbacks link_callbacks =
156 add_archive_element,
157 multiple_definition,
158 multiple_common,
159 add_to_set,
160 constructor_callback,
161 warning_callback,
162 undefined_symbol,
163 reloc_overflow,
164 reloc_dangerous,
165 unattached_reloc,
166 notice,
167 einfo,
168 info_msg,
169 minfo,
170 ldlang_override_segment_assignment
173 struct bfd_link_info link_info;
175 static void
176 remove_output (void)
178 if (output_filename)
180 if (output_bfd)
181 bfd_cache_close (output_bfd);
182 if (delete_output_file_on_failure)
183 unlink_if_ordinary (output_filename);
188 main (int argc, char **argv)
190 char *emulation;
191 long start_time = get_run_time ();
193 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
194 setlocale (LC_MESSAGES, "");
195 #endif
196 #if defined (HAVE_SETLOCALE)
197 setlocale (LC_CTYPE, "");
198 #endif
199 bindtextdomain (PACKAGE, LOCALEDIR);
200 textdomain (PACKAGE);
202 program_name = argv[0];
203 xmalloc_set_program_name (program_name);
205 START_PROGRESS (program_name, 0);
207 expandargv (&argc, &argv);
209 bfd_init ();
211 bfd_set_error_program_name (program_name);
213 xatexit (remove_output);
215 /* Set up the sysroot directory. */
216 ld_sysroot = get_sysroot (argc, argv);
217 if (*ld_sysroot)
219 if (*TARGET_SYSTEM_ROOT == 0)
221 einfo ("%P%F: this linker was not configured to use sysroots\n");
222 ld_sysroot = "";
224 else
225 ld_canon_sysroot = lrealpath (ld_sysroot);
227 if (ld_canon_sysroot)
228 ld_canon_sysroot_len = strlen (ld_canon_sysroot);
229 else
230 ld_canon_sysroot_len = -1;
232 /* Set the default BFD target based on the configured target. Doing
233 this permits the linker to be configured for a particular target,
234 and linked against a shared BFD library which was configured for
235 a different target. The macro TARGET is defined by Makefile. */
236 if (! bfd_set_default_target (TARGET))
238 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
239 xexit (1);
242 #if YYDEBUG
244 extern int yydebug;
245 yydebug = 1;
247 #endif
249 config.build_constructors = TRUE;
250 config.rpath_separator = ':';
251 config.split_by_reloc = (unsigned) -1;
252 config.split_by_file = (bfd_size_type) -1;
253 config.make_executable = TRUE;
254 config.magic_demand_paged = TRUE;
255 config.text_read_only = TRUE;
257 command_line.warn_mismatch = TRUE;
258 command_line.warn_search_mismatch = TRUE;
259 command_line.check_section_addresses = TRUE;
261 /* We initialize DEMANGLING based on the environment variable
262 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
263 output of the linker, unless COLLECT_NO_DEMANGLE is set in the
264 environment. Acting the same way here lets us provide the same
265 interface by default. */
266 demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
268 link_info.allow_undefined_version = TRUE;
269 link_info.keep_memory = TRUE;
270 link_info.combreloc = TRUE;
271 link_info.strip_discarded = TRUE;
272 link_info.emit_hash = TRUE;
273 link_info.callbacks = &link_callbacks;
274 link_info.input_bfds_tail = &link_info.input_bfds;
275 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
276 and _fini symbols. We are compatible. */
277 link_info.init_function = "_init";
278 link_info.fini_function = "_fini";
279 link_info.relax_pass = 1;
280 link_info.pei386_auto_import = -1;
281 link_info.spare_dynamic_tags = 5;
283 ldfile_add_arch ("");
284 emulation = get_emulation (argc, argv);
285 ldemul_choose_mode (emulation);
286 default_target = ldemul_choose_target (argc, argv);
287 lang_init ();
288 ldemul_before_parse ();
289 lang_has_input_file = FALSE;
290 parse_args (argc, argv);
292 if (config.hash_table_size != 0)
293 bfd_hash_set_default_size (config.hash_table_size);
295 ldemul_set_symbols ();
297 if (link_info.relocatable)
299 if (command_line.relax)
300 einfo (_("%P%F: --relax and -r may not be used together\n"));
301 if (link_info.shared)
302 einfo (_("%P%F: -r and -shared may not be used together\n"));
305 /* We may have -Bsymbolic, -Bsymbolic-functions, --dynamic-list-data,
306 --dynamic-list-cpp-new, --dynamic-list-cpp-typeinfo and
307 --dynamic-list FILE. -Bsymbolic and -Bsymbolic-functions are
308 for shared libraries. -Bsymbolic overrides all others and vice
309 versa. */
310 switch (command_line.symbolic)
312 case symbolic_unset:
313 break;
314 case symbolic:
315 /* -Bsymbolic is for shared library only. */
316 if (link_info.shared)
318 link_info.symbolic = TRUE;
319 /* Should we free the unused memory? */
320 link_info.dynamic_list = NULL;
321 command_line.dynamic_list = dynamic_list_unset;
323 break;
324 case symbolic_functions:
325 /* -Bsymbolic-functions is for shared library only. */
326 if (link_info.shared)
327 command_line.dynamic_list = dynamic_list_data;
328 break;
331 switch (command_line.dynamic_list)
333 case dynamic_list_unset:
334 break;
335 case dynamic_list_data:
336 link_info.dynamic_data = TRUE;
337 case dynamic_list:
338 link_info.dynamic = TRUE;
339 break;
342 if (! link_info.shared)
344 if (command_line.filter_shlib)
345 einfo (_("%P%F: -F may not be used without -shared\n"));
346 if (command_line.auxiliary_filters)
347 einfo (_("%P%F: -f may not be used without -shared\n"));
350 if (! link_info.shared || link_info.pie)
351 link_info.executable = TRUE;
353 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
354 don't see how else this can be handled, since in this case we
355 must preserve all externally visible symbols. */
356 if (link_info.relocatable && link_info.strip == strip_all)
358 link_info.strip = strip_debugger;
359 if (link_info.discard == discard_sec_merge)
360 link_info.discard = discard_all;
363 /* This essentially adds another -L directory so this must be done after
364 the -L's in argv have been processed. */
365 set_scripts_dir ();
367 /* If we have not already opened and parsed a linker script,
368 try the default script from command line first. */
369 if (saved_script_handle == NULL
370 && command_line.default_script != NULL)
372 ldfile_open_command_file (command_line.default_script);
373 parser_input = input_script;
374 yyparse ();
377 /* If we have not already opened and parsed a linker script
378 read the emulation's appropriate default script. */
379 if (saved_script_handle == NULL)
381 int isfile;
382 char *s = ldemul_get_script (&isfile);
384 if (isfile)
385 ldfile_open_command_file (s);
386 else
388 lex_string = s;
389 lex_redirect (s);
391 parser_input = input_script;
392 yyparse ();
393 lex_string = NULL;
396 if (trace_file_tries)
398 if (saved_script_handle)
399 info_msg (_("using external linker script:"));
400 else
401 info_msg (_("using internal linker script:"));
402 info_msg ("\n==================================================\n");
404 if (saved_script_handle)
406 static const int ld_bufsz = 8193;
407 size_t n;
408 char *buf = xmalloc (ld_bufsz);
410 rewind (saved_script_handle);
411 while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
413 buf[n] = 0;
414 info_msg (buf);
416 rewind (saved_script_handle);
417 free (buf);
419 else
421 int isfile;
423 info_msg (ldemul_get_script (&isfile));
426 info_msg ("\n==================================================\n");
429 lang_final ();
431 if (!lang_has_input_file)
433 if (version_printed)
434 xexit (0);
435 einfo (_("%P%F: no input files\n"));
438 if (trace_files)
439 info_msg (_("%P: mode %s\n"), emulation);
441 ldemul_after_parse ();
443 if (config.map_filename)
445 if (strcmp (config.map_filename, "-") == 0)
447 config.map_file = stdout;
449 else
451 config.map_file = fopen (config.map_filename, FOPEN_WT);
452 if (config.map_file == (FILE *) NULL)
454 bfd_set_error (bfd_error_system_call);
455 einfo (_("%P%F: cannot open map file %s: %E\n"),
456 config.map_filename);
461 lang_process ();
463 /* Print error messages for any missing symbols, for any warning
464 symbols, and possibly multiple definitions. */
465 if (link_info.relocatable)
466 output_bfd->flags &= ~EXEC_P;
467 else
468 output_bfd->flags |= EXEC_P;
470 ldwrite ();
472 if (config.map_file != NULL)
473 lang_map ();
474 if (command_line.cref)
475 output_cref (config.map_file != NULL ? config.map_file : stdout);
476 if (nocrossref_list != NULL)
477 check_nocrossrefs ();
479 lang_finish ();
481 /* Even if we're producing relocatable output, some non-fatal errors should
482 be reported in the exit status. (What non-fatal errors, if any, do we
483 want to ignore for relocatable output?) */
484 if (!config.make_executable && !force_make_executable)
486 if (trace_files)
487 einfo (_("%P: link errors found, deleting executable `%s'\n"),
488 output_filename);
490 /* The file will be removed by remove_output. */
491 xexit (1);
493 else
495 if (! bfd_close (output_bfd))
496 einfo (_("%F%B: final close failed: %E\n"), output_bfd);
498 /* If the --force-exe-suffix is enabled, and we're making an
499 executable file and it doesn't end in .exe, copy it to one
500 which does. */
501 if (! link_info.relocatable && command_line.force_exe_suffix)
503 int len = strlen (output_filename);
505 if (len < 4
506 || (strcasecmp (output_filename + len - 4, ".exe") != 0
507 && strcasecmp (output_filename + len - 4, ".dll") != 0))
509 FILE *src;
510 FILE *dst;
511 const int bsize = 4096;
512 char *buf = xmalloc (bsize);
513 int l;
514 char *dst_name = xmalloc (len + 5);
516 strcpy (dst_name, output_filename);
517 strcat (dst_name, ".exe");
518 src = fopen (output_filename, FOPEN_RB);
519 dst = fopen (dst_name, FOPEN_WB);
521 if (!src)
522 einfo (_("%X%P: unable to open for source of copy `%s'\n"),
523 output_filename);
524 if (!dst)
525 einfo (_("%X%P: unable to open for destination of copy `%s'\n"),
526 dst_name);
527 while ((l = fread (buf, 1, bsize, src)) > 0)
529 int done = fwrite (buf, 1, l, dst);
531 if (done != l)
532 einfo (_("%P: Error writing file `%s'\n"), dst_name);
535 fclose (src);
536 if (fclose (dst) == EOF)
537 einfo (_("%P: Error closing file `%s'\n"), dst_name);
538 free (dst_name);
539 free (buf);
544 END_PROGRESS (program_name);
546 if (config.stats)
548 #ifdef HAVE_SBRK
549 char *lim = sbrk (0);
550 #endif
551 long run_time = get_run_time () - start_time;
553 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
554 program_name, run_time / 1000000, run_time % 1000000);
555 #ifdef HAVE_SBRK
556 fprintf (stderr, _("%s: data size %ld\n"), program_name,
557 (long) (lim - (char *) &environ));
558 #endif
561 /* Prevent remove_output from doing anything, after a successful link. */
562 output_filename = NULL;
564 xexit (0);
565 return 0;
568 /* If the configured sysroot is relocatable, try relocating it based on
569 default prefix FROM. Return the relocated directory if it exists,
570 otherwise return null. */
572 static char *
573 get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
575 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
576 char *path;
577 struct stat s;
579 path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
580 if (path)
582 if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
583 return path;
584 free (path);
586 #endif
587 return 0;
590 /* Return the sysroot directory. Return "" if no sysroot is being used. */
592 static const char *
593 get_sysroot (int argc, char **argv)
595 int i;
596 const char *path;
598 for (i = 1; i < argc; i++)
599 if (CONST_STRNEQ (argv[i], "--sysroot="))
600 return argv[i] + strlen ("--sysroot=");
602 path = get_relative_sysroot (BINDIR);
603 if (path)
604 return path;
606 path = get_relative_sysroot (TOOLBINDIR);
607 if (path)
608 return path;
610 return TARGET_SYSTEM_ROOT;
613 /* We need to find any explicitly given emulation in order to initialize the
614 state that's needed by the lex&yacc argument parser (parse_args). */
616 static char *
617 get_emulation (int argc, char **argv)
619 char *emulation;
620 int i;
622 emulation = getenv (EMULATION_ENVIRON);
623 if (emulation == NULL)
624 emulation = DEFAULT_EMULATION;
626 for (i = 1; i < argc; i++)
628 if (CONST_STRNEQ (argv[i], "-m"))
630 if (argv[i][2] == '\0')
632 /* -m EMUL */
633 if (i < argc - 1)
635 emulation = argv[i + 1];
636 i++;
638 else
639 einfo (_("%P%F: missing argument to -m\n"));
641 else if (strcmp (argv[i], "-mips1") == 0
642 || strcmp (argv[i], "-mips2") == 0
643 || strcmp (argv[i], "-mips3") == 0
644 || strcmp (argv[i], "-mips4") == 0
645 || strcmp (argv[i], "-mips5") == 0
646 || strcmp (argv[i], "-mips32") == 0
647 || strcmp (argv[i], "-mips32r2") == 0
648 || strcmp (argv[i], "-mips64") == 0
649 || strcmp (argv[i], "-mips64r2") == 0)
651 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
652 passed to the linker by some MIPS compilers. They
653 generally tell the linker to use a slightly different
654 library path. Perhaps someday these should be
655 implemented as emulations; until then, we just ignore
656 the arguments and hope that nobody ever creates
657 emulations named ips1, ips2 or ips3. */
659 else if (strcmp (argv[i], "-m486") == 0)
661 /* FIXME: The argument -m486 is passed to the linker on
662 some Linux systems. Hope that nobody creates an
663 emulation named 486. */
665 else
667 /* -mEMUL */
668 emulation = &argv[i][2];
673 return emulation;
676 /* If directory DIR contains an "ldscripts" subdirectory,
677 add DIR to the library search path and return TRUE,
678 else return FALSE. */
680 static bfd_boolean
681 check_for_scripts_dir (char *dir)
683 size_t dirlen;
684 char *buf;
685 struct stat s;
686 bfd_boolean res;
688 dirlen = strlen (dir);
689 /* sizeof counts the terminating NUL. */
690 buf = xmalloc (dirlen + sizeof ("/ldscripts"));
691 sprintf (buf, "%s/ldscripts", dir);
693 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
694 free (buf);
695 if (res)
696 ldfile_add_library_path (dir, FALSE);
697 return res;
700 /* Set the default directory for finding script files.
701 Libraries will be searched for here too, but that's ok.
702 We look for the "ldscripts" directory in:
704 SCRIPTDIR (passed from Makefile)
705 (adjusted according to the current location of the binary)
706 SCRIPTDIR (passed from Makefile)
707 the dir where this program is (for using it from the build tree)
708 the dir where this program is/../lib
709 (for installing the tool suite elsewhere). */
711 static void
712 set_scripts_dir (void)
714 char *end, *dir;
715 size_t dirlen;
716 bfd_boolean found;
718 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
719 if (dir)
721 found = check_for_scripts_dir (dir);
722 free (dir);
723 if (found)
724 return;
727 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
728 if (dir)
730 found = check_for_scripts_dir (dir);
731 free (dir);
732 if (found)
733 return;
736 if (check_for_scripts_dir (SCRIPTDIR))
737 /* We've been installed normally. */
738 return;
740 /* Look for "ldscripts" in the dir where our binary is. */
741 end = strrchr (program_name, '/');
742 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
744 /* We could have \foo\bar, or /foo\bar. */
745 char *bslash = strrchr (program_name, '\\');
747 if (end == NULL || (bslash != NULL && bslash > end))
748 end = bslash;
750 #endif
752 if (end == NULL)
753 /* Don't look for ldscripts in the current directory. There is
754 too much potential for confusion. */
755 return;
757 dirlen = end - program_name;
758 /* Make a copy of program_name in dir.
759 Leave room for later "/../lib". */
760 dir = xmalloc (dirlen + 8);
761 strncpy (dir, program_name, dirlen);
762 dir[dirlen] = '\0';
764 if (check_for_scripts_dir (dir))
766 free (dir);
767 return;
770 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
771 strcpy (dir + dirlen, "/../lib");
772 check_for_scripts_dir (dir);
773 free (dir);
776 void
777 add_ysym (const char *name)
779 if (link_info.notice_hash == NULL)
781 link_info.notice_hash = xmalloc (sizeof (struct bfd_hash_table));
782 if (!bfd_hash_table_init_n (link_info.notice_hash,
783 bfd_hash_newfunc,
784 sizeof (struct bfd_hash_entry),
785 61))
786 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
789 if (bfd_hash_lookup (link_info.notice_hash, name, TRUE, TRUE) == NULL)
790 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
793 /* Record a symbol to be wrapped, from the --wrap option. */
795 void
796 add_wrap (const char *name)
798 if (link_info.wrap_hash == NULL)
800 link_info.wrap_hash = xmalloc (sizeof (struct bfd_hash_table));
801 if (!bfd_hash_table_init_n (link_info.wrap_hash,
802 bfd_hash_newfunc,
803 sizeof (struct bfd_hash_entry),
804 61))
805 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
808 if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL)
809 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
812 /* Handle the -retain-symbols-file option. */
814 void
815 add_keepsyms_file (const char *filename)
817 FILE *file;
818 char *buf;
819 size_t bufsize;
820 int c;
822 if (link_info.strip == strip_some)
823 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
825 file = fopen (filename, "r");
826 if (file == NULL)
828 bfd_set_error (bfd_error_system_call);
829 einfo ("%X%P: %s: %E\n", filename);
830 return;
833 link_info.keep_hash = xmalloc (sizeof (struct bfd_hash_table));
834 if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc,
835 sizeof (struct bfd_hash_entry)))
836 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
838 bufsize = 100;
839 buf = xmalloc (bufsize);
841 c = getc (file);
842 while (c != EOF)
844 while (ISSPACE (c))
845 c = getc (file);
847 if (c != EOF)
849 size_t len = 0;
851 while (! ISSPACE (c) && c != EOF)
853 buf[len] = c;
854 ++len;
855 if (len >= bufsize)
857 bufsize *= 2;
858 buf = xrealloc (buf, bufsize);
860 c = getc (file);
863 buf[len] = '\0';
865 if (bfd_hash_lookup (link_info.keep_hash, buf, TRUE, TRUE) == NULL)
866 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
870 if (link_info.strip != strip_none)
871 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
873 free (buf);
874 link_info.strip = strip_some;
877 /* Callbacks from the BFD linker routines. */
879 /* This is called when BFD has decided to include an archive member in
880 a link. */
882 static bfd_boolean
883 add_archive_element (struct bfd_link_info *info,
884 bfd *abfd,
885 const char *name)
887 lang_input_statement_type *input;
889 input = xmalloc (sizeof (lang_input_statement_type));
890 input->filename = abfd->filename;
891 input->local_sym_name = abfd->filename;
892 input->the_bfd = abfd;
893 input->asymbols = NULL;
894 input->next = NULL;
895 input->just_syms_flag = FALSE;
896 input->loaded = FALSE;
897 input->search_dirs_flag = FALSE;
899 /* FIXME: The following fields are not set: header.next,
900 header.type, closed, passive_position, symbol_count,
901 next_real_file, is_archive, target, real. This bit of code is
902 from the old decode_library_subfile function. I don't know
903 whether any of those fields matters. */
905 ldlang_add_file (input);
907 if (config.map_file != NULL)
909 static bfd_boolean header_printed;
910 struct bfd_link_hash_entry *h;
911 bfd *from;
912 int len;
914 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
916 if (h == NULL)
917 from = NULL;
918 else
920 switch (h->type)
922 default:
923 from = NULL;
924 break;
926 case bfd_link_hash_defined:
927 case bfd_link_hash_defweak:
928 from = h->u.def.section->owner;
929 break;
931 case bfd_link_hash_undefined:
932 case bfd_link_hash_undefweak:
933 from = h->u.undef.abfd;
934 break;
936 case bfd_link_hash_common:
937 from = h->u.c.p->section->owner;
938 break;
942 if (! header_printed)
944 char buf[100];
946 sprintf (buf, _("Archive member included because of file (symbol)\n\n"));
947 minfo ("%s", buf);
948 header_printed = TRUE;
951 if (bfd_my_archive (abfd) == NULL)
953 minfo ("%s", bfd_get_filename (abfd));
954 len = strlen (bfd_get_filename (abfd));
956 else
958 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)),
959 bfd_get_filename (abfd));
960 len = (strlen (bfd_get_filename (bfd_my_archive (abfd)))
961 + strlen (bfd_get_filename (abfd))
962 + 2);
965 if (len >= 29)
967 print_nl ();
968 len = 0;
970 while (len < 30)
972 print_space ();
973 ++len;
976 if (from != NULL)
977 minfo ("%B ", from);
978 if (h != NULL)
979 minfo ("(%T)\n", h->root.string);
980 else
981 minfo ("(%s)\n", name);
984 if (trace_files || trace_file_tries)
985 info_msg ("%I\n", input);
987 return TRUE;
990 /* This is called when BFD has discovered a symbol which is defined
991 multiple times. */
993 static bfd_boolean
994 multiple_definition (struct bfd_link_info *info ATTRIBUTE_UNUSED,
995 const char *name,
996 bfd *obfd,
997 asection *osec,
998 bfd_vma oval,
999 bfd *nbfd,
1000 asection *nsec,
1001 bfd_vma nval)
1003 /* If either section has the output_section field set to
1004 bfd_abs_section_ptr, it means that the section is being
1005 discarded, and this is not really a multiple definition at all.
1006 FIXME: It would be cleaner to somehow ignore symbols defined in
1007 sections which are being discarded. */
1008 if ((osec->output_section != NULL
1009 && ! bfd_is_abs_section (osec)
1010 && bfd_is_abs_section (osec->output_section))
1011 || (nsec->output_section != NULL
1012 && ! bfd_is_abs_section (nsec)
1013 && bfd_is_abs_section (nsec->output_section)))
1014 return TRUE;
1016 einfo (_("%X%C: multiple definition of `%T'\n"),
1017 nbfd, nsec, nval, name);
1018 if (obfd != NULL)
1019 einfo (_("%D: first defined here\n"), obfd, osec, oval);
1021 if (command_line.relax)
1023 einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
1024 command_line.relax = 0;
1027 return TRUE;
1030 /* This is called when there is a definition of a common symbol, or
1031 when a common symbol is found for a symbol that is already defined,
1032 or when two common symbols are found. We only do something if
1033 -warn-common was used. */
1035 static bfd_boolean
1036 multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1037 const char *name,
1038 bfd *obfd,
1039 enum bfd_link_hash_type otype,
1040 bfd_vma osize,
1041 bfd *nbfd,
1042 enum bfd_link_hash_type ntype,
1043 bfd_vma nsize)
1045 if (! config.warn_common)
1046 return TRUE;
1048 if (ntype == bfd_link_hash_defined
1049 || ntype == bfd_link_hash_defweak
1050 || ntype == bfd_link_hash_indirect)
1052 ASSERT (otype == bfd_link_hash_common);
1053 einfo (_("%B: warning: definition of `%T' overriding common\n"),
1054 nbfd, name);
1055 if (obfd != NULL)
1056 einfo (_("%B: warning: common is here\n"), obfd);
1058 else if (otype == bfd_link_hash_defined
1059 || otype == bfd_link_hash_defweak
1060 || otype == bfd_link_hash_indirect)
1062 ASSERT (ntype == bfd_link_hash_common);
1063 einfo (_("%B: warning: common of `%T' overridden by definition\n"),
1064 nbfd, name);
1065 if (obfd != NULL)
1066 einfo (_("%B: warning: defined here\n"), obfd);
1068 else
1070 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
1071 if (osize > nsize)
1073 einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
1074 nbfd, name);
1075 if (obfd != NULL)
1076 einfo (_("%B: warning: larger common is here\n"), obfd);
1078 else if (nsize > osize)
1080 einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
1081 nbfd, name);
1082 if (obfd != NULL)
1083 einfo (_("%B: warning: smaller common is here\n"), obfd);
1085 else
1087 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name);
1088 if (obfd != NULL)
1089 einfo (_("%B: warning: previous common is here\n"), obfd);
1093 return TRUE;
1096 /* This is called when BFD has discovered a set element. H is the
1097 entry in the linker hash table for the set. SECTION and VALUE
1098 represent a value which should be added to the set. */
1100 static bfd_boolean
1101 add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1102 struct bfd_link_hash_entry *h,
1103 bfd_reloc_code_real_type reloc,
1104 bfd *abfd,
1105 asection *section,
1106 bfd_vma value)
1108 if (config.warn_constructors)
1109 einfo (_("%P: warning: global constructor %s used\n"),
1110 h->root.string);
1112 if (! config.build_constructors)
1113 return TRUE;
1115 ldctor_add_set_entry (h, reloc, NULL, section, value);
1117 if (h->type == bfd_link_hash_new)
1119 h->type = bfd_link_hash_undefined;
1120 h->u.undef.abfd = abfd;
1121 /* We don't call bfd_link_add_undef to add this to the list of
1122 undefined symbols because we are going to define it
1123 ourselves. */
1126 return TRUE;
1129 /* This is called when BFD has discovered a constructor. This is only
1130 called for some object file formats--those which do not handle
1131 constructors in some more clever fashion. This is similar to
1132 adding an element to a set, but less general. */
1134 static bfd_boolean
1135 constructor_callback (struct bfd_link_info *info,
1136 bfd_boolean constructor,
1137 const char *name,
1138 bfd *abfd,
1139 asection *section,
1140 bfd_vma value)
1142 char *s;
1143 struct bfd_link_hash_entry *h;
1144 char set_name[1 + sizeof "__CTOR_LIST__"];
1146 if (config.warn_constructors)
1147 einfo (_("%P: warning: global constructor %s used\n"), name);
1149 if (! config.build_constructors)
1150 return TRUE;
1152 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1153 useful error message. */
1154 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL
1155 && (info->relocatable
1156 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
1157 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1159 s = set_name;
1160 if (bfd_get_symbol_leading_char (abfd) != '\0')
1161 *s++ = bfd_get_symbol_leading_char (abfd);
1162 if (constructor)
1163 strcpy (s, "__CTOR_LIST__");
1164 else
1165 strcpy (s, "__DTOR_LIST__");
1167 h = bfd_link_hash_lookup (info->hash, set_name, TRUE, TRUE, TRUE);
1168 if (h == (struct bfd_link_hash_entry *) NULL)
1169 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1170 if (h->type == bfd_link_hash_new)
1172 h->type = bfd_link_hash_undefined;
1173 h->u.undef.abfd = abfd;
1174 /* We don't call bfd_link_add_undef to add this to the list of
1175 undefined symbols because we are going to define it
1176 ourselves. */
1179 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
1180 return TRUE;
1183 /* A structure used by warning_callback to pass information through
1184 bfd_map_over_sections. */
1186 struct warning_callback_info
1188 bfd_boolean found;
1189 const char *warning;
1190 const char *symbol;
1191 asymbol **asymbols;
1194 /* This is called when there is a reference to a warning symbol. */
1196 static bfd_boolean
1197 warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1198 const char *warning,
1199 const char *symbol,
1200 bfd *abfd,
1201 asection *section,
1202 bfd_vma address)
1204 /* This is a hack to support warn_multiple_gp. FIXME: This should
1205 have a cleaner interface, but what? */
1206 if (! config.warn_multiple_gp
1207 && strcmp (warning, "using multiple gp values") == 0)
1208 return TRUE;
1210 if (section != NULL)
1211 einfo ("%C: %s%s\n", abfd, section, address, _("warning: "), warning);
1212 else if (abfd == NULL)
1213 einfo ("%P: %s%s\n", _("warning: "), warning);
1214 else if (symbol == NULL)
1215 einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
1216 else
1218 lang_input_statement_type *entry;
1219 asymbol **asymbols;
1220 struct warning_callback_info info;
1222 /* Look through the relocs to see if we can find a plausible
1223 address. */
1224 entry = (lang_input_statement_type *) abfd->usrdata;
1225 if (entry != NULL && entry->asymbols != NULL)
1226 asymbols = entry->asymbols;
1227 else
1229 long symsize;
1230 long symbol_count;
1232 symsize = bfd_get_symtab_upper_bound (abfd);
1233 if (symsize < 0)
1234 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1235 asymbols = xmalloc (symsize);
1236 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
1237 if (symbol_count < 0)
1238 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1239 if (entry != NULL)
1241 entry->asymbols = asymbols;
1242 entry->symbol_count = symbol_count;
1246 info.found = FALSE;
1247 info.warning = warning;
1248 info.symbol = symbol;
1249 info.asymbols = asymbols;
1250 bfd_map_over_sections (abfd, warning_find_reloc, &info);
1252 if (! info.found)
1253 einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
1255 if (entry == NULL)
1256 free (asymbols);
1259 return TRUE;
1262 /* This is called by warning_callback for each section. It checks the
1263 relocs of the section to see if it can find a reference to the
1264 symbol which triggered the warning. If it can, it uses the reloc
1265 to give an error message with a file and line number. */
1267 static void
1268 warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
1270 struct warning_callback_info *info = iarg;
1271 long relsize;
1272 arelent **relpp;
1273 long relcount;
1274 arelent **p, **pend;
1276 if (info->found)
1277 return;
1279 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1280 if (relsize < 0)
1281 einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1282 if (relsize == 0)
1283 return;
1285 relpp = xmalloc (relsize);
1286 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1287 if (relcount < 0)
1288 einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1290 p = relpp;
1291 pend = p + relcount;
1292 for (; p < pend && *p != NULL; p++)
1294 arelent *q = *p;
1296 if (q->sym_ptr_ptr != NULL
1297 && *q->sym_ptr_ptr != NULL
1298 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1300 /* We found a reloc for the symbol we are looking for. */
1301 einfo ("%C: %s%s\n", abfd, sec, q->address, _("warning: "),
1302 info->warning);
1303 info->found = TRUE;
1304 break;
1308 free (relpp);
1311 /* This is called when an undefined symbol is found. */
1313 static bfd_boolean
1314 undefined_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1315 const char *name,
1316 bfd *abfd,
1317 asection *section,
1318 bfd_vma address,
1319 bfd_boolean error)
1321 static char *error_name;
1322 static unsigned int error_count;
1324 #define MAX_ERRORS_IN_A_ROW 5
1326 if (config.warn_once)
1328 static struct bfd_hash_table *hash;
1330 /* Only warn once about a particular undefined symbol. */
1331 if (hash == NULL)
1333 hash = xmalloc (sizeof (struct bfd_hash_table));
1334 if (!bfd_hash_table_init (hash, bfd_hash_newfunc,
1335 sizeof (struct bfd_hash_entry)))
1336 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
1339 if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL)
1340 return TRUE;
1342 if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL)
1343 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
1346 /* We never print more than a reasonable number of errors in a row
1347 for a single symbol. */
1348 if (error_name != NULL
1349 && strcmp (name, error_name) == 0)
1350 ++error_count;
1351 else
1353 error_count = 0;
1354 if (error_name != NULL)
1355 free (error_name);
1356 error_name = xstrdup (name);
1359 if (section != NULL)
1361 if (error_count < MAX_ERRORS_IN_A_ROW)
1363 if (error)
1364 einfo (_("%X%C: undefined reference to `%T'\n"),
1365 abfd, section, address, name);
1366 else
1367 einfo (_("%C: warning: undefined reference to `%T'\n"),
1368 abfd, section, address, name);
1370 else if (error_count == MAX_ERRORS_IN_A_ROW)
1372 if (error)
1373 einfo (_("%X%D: more undefined references to `%T' follow\n"),
1374 abfd, section, address, name);
1375 else
1376 einfo (_("%D: warning: more undefined references to `%T' follow\n"),
1377 abfd, section, address, name);
1379 else if (error)
1380 einfo ("%X");
1382 else
1384 if (error_count < MAX_ERRORS_IN_A_ROW)
1386 if (error)
1387 einfo (_("%X%B: undefined reference to `%T'\n"),
1388 abfd, name);
1389 else
1390 einfo (_("%B: warning: undefined reference to `%T'\n"),
1391 abfd, name);
1393 else if (error_count == MAX_ERRORS_IN_A_ROW)
1395 if (error)
1396 einfo (_("%X%B: more undefined references to `%T' follow\n"),
1397 abfd, name);
1398 else
1399 einfo (_("%B: warning: more undefined references to `%T' follow\n"),
1400 abfd, name);
1402 else if (error)
1403 einfo ("%X");
1406 return TRUE;
1409 /* Counter to limit the number of relocation overflow error messages
1410 to print. Errors are printed as it is decremented. When it's
1411 called and the counter is zero, a final message is printed
1412 indicating more relocations were omitted. When it gets to -1, no
1413 such errors are printed. If it's initially set to a value less
1414 than -1, all such errors will be printed (--verbose does this). */
1416 int overflow_cutoff_limit = 10;
1418 /* This is called when a reloc overflows. */
1420 static bfd_boolean
1421 reloc_overflow (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1422 struct bfd_link_hash_entry *entry,
1423 const char *name,
1424 const char *reloc_name,
1425 bfd_vma addend,
1426 bfd *abfd,
1427 asection *section,
1428 bfd_vma address)
1430 if (overflow_cutoff_limit == -1)
1431 return TRUE;
1433 einfo ("%X%C:", abfd, section, address);
1435 if (overflow_cutoff_limit >= 0
1436 && overflow_cutoff_limit-- == 0)
1438 einfo (_(" additional relocation overflows omitted from the output\n"));
1439 return TRUE;
1442 if (entry)
1444 while (entry->type == bfd_link_hash_indirect
1445 || entry->type == bfd_link_hash_warning)
1446 entry = entry->u.i.link;
1447 switch (entry->type)
1449 case bfd_link_hash_undefined:
1450 case bfd_link_hash_undefweak:
1451 einfo (_(" relocation truncated to fit: %s against undefined symbol `%T'"),
1452 reloc_name, entry->root.string);
1453 break;
1454 case bfd_link_hash_defined:
1455 case bfd_link_hash_defweak:
1456 einfo (_(" relocation truncated to fit: %s against symbol `%T' defined in %A section in %B"),
1457 reloc_name, entry->root.string,
1458 entry->u.def.section,
1459 entry->u.def.section == bfd_abs_section_ptr
1460 ? output_bfd : entry->u.def.section->owner);
1461 break;
1462 default:
1463 abort ();
1464 break;
1467 else
1468 einfo (_(" relocation truncated to fit: %s against `%T'"),
1469 reloc_name, name);
1470 if (addend != 0)
1471 einfo ("+%v", addend);
1472 einfo ("\n");
1473 return TRUE;
1476 /* This is called when a dangerous relocation is made. */
1478 static bfd_boolean
1479 reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1480 const char *message,
1481 bfd *abfd,
1482 asection *section,
1483 bfd_vma address)
1485 einfo (_("%X%C: dangerous relocation: %s\n"),
1486 abfd, section, address, message);
1487 return TRUE;
1490 /* This is called when a reloc is being generated attached to a symbol
1491 that is not being output. */
1493 static bfd_boolean
1494 unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1495 const char *name,
1496 bfd *abfd,
1497 asection *section,
1498 bfd_vma address)
1500 einfo (_("%X%C: reloc refers to symbol `%T' which is not being output\n"),
1501 abfd, section, address, name);
1502 return TRUE;
1505 /* This is called if link_info.notice_all is set, or when a symbol in
1506 link_info.notice_hash is found. Symbols are put in notice_hash
1507 using the -y option. */
1509 static bfd_boolean
1510 notice (struct bfd_link_info *info,
1511 const char *name,
1512 bfd *abfd,
1513 asection *section,
1514 bfd_vma value)
1516 if (name == NULL)
1518 if (command_line.cref || nocrossref_list != NULL)
1519 return handle_asneeded_cref (abfd, value);
1520 return TRUE;
1523 if (! info->notice_all
1524 || (info->notice_hash != NULL
1525 && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
1527 if (bfd_is_und_section (section))
1528 einfo ("%B: reference to %s\n", abfd, name);
1529 else
1530 einfo ("%B: definition of %s\n", abfd, name);
1533 if (command_line.cref || nocrossref_list != NULL)
1534 add_cref (name, abfd, section, value);
1536 return TRUE;