Add support for dwarf2 debug information.
[binutils.git] / ld / ldmain.c
blobd8ee1b23e7f578476f343a265341f174c810d2e0
1 /* Main program of GNU linker.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain steve@cygnus.com
7 This file is part of GLD, the Gnu Linker.
9 GLD 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 2, or (at your option)
12 any later version.
14 GLD 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 GLD; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include <stdio.h>
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "progress.h"
30 #include "bfdlink.h"
31 #include "filenames.h"
33 #include "ld.h"
34 #include "ldmain.h"
35 #include "ldmisc.h"
36 #include "ldwrite.h"
37 #include "ldexp.h"
38 #include "ldlang.h"
39 #include <ldgram.h>
40 #include "ldlex.h"
41 #include "ldfile.h"
42 #include "ldemul.h"
43 #include "ldctor.h"
45 /* Somewhere above, sys/stat.h got included. */
46 #if !defined(S_ISDIR) && defined(S_IFDIR)
47 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
48 #endif
50 #include <string.h>
52 #ifdef HAVE_SBRK
53 #ifdef NEED_DECLARATION_SBRK
54 extern PTR sbrk ();
55 #endif
56 #endif
58 #ifndef TARGET_SYSTEM_ROOT
59 #define TARGET_SYSTEM_ROOT ""
60 #endif
62 int main PARAMS ((int, char **));
64 /* EXPORTS */
66 char *default_target;
67 const char *output_filename = "a.out";
69 /* Name this program was invoked by. */
70 char *program_name;
72 /* The prefix for system library directories. */
73 char *ld_sysroot;
75 /* The canonical representation of ld_sysroot. */
76 char * ld_canon_sysroot;
77 int ld_canon_sysroot_len;
79 /* The file that we're creating. */
80 bfd *output_bfd = 0;
82 /* Set by -G argument, for MIPS ECOFF target. */
83 int g_switch_value = 8;
85 /* Nonzero means print names of input files as processed. */
86 bfd_boolean trace_files;
88 /* Nonzero means same, but note open failures, too. */
89 bfd_boolean trace_file_tries;
91 /* Nonzero means version number was printed, so exit successfully
92 instead of complaining if no input files are given. */
93 bfd_boolean version_printed;
95 /* Nonzero means link in every member of an archive. */
96 bfd_boolean whole_archive;
98 /* TRUE if we should demangle symbol names. */
99 bfd_boolean demangling;
101 args_type command_line;
103 ld_config_type config;
105 static char *get_emulation
106 PARAMS ((int, char **));
107 static void set_scripts_dir
108 PARAMS ((void));
109 static void remove_output
110 PARAMS ((void));
111 static bfd_boolean check_for_scripts_dir
112 PARAMS ((char *));
113 static bfd_boolean add_archive_element
114 PARAMS ((struct bfd_link_info *, bfd *, const char *));
115 static bfd_boolean multiple_definition
116 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
117 bfd *, asection *, bfd_vma));
118 static bfd_boolean multiple_common
119 PARAMS ((struct bfd_link_info *, const char *, bfd *,
120 enum bfd_link_hash_type, bfd_vma, bfd *, enum bfd_link_hash_type,
121 bfd_vma));
122 static bfd_boolean add_to_set
123 PARAMS ((struct bfd_link_info *, struct bfd_link_hash_entry *,
124 bfd_reloc_code_real_type, bfd *, asection *, bfd_vma));
125 static bfd_boolean constructor_callback
126 PARAMS ((struct bfd_link_info *, bfd_boolean, const char *, bfd *,
127 asection *, bfd_vma));
128 static bfd_boolean warning_callback
129 PARAMS ((struct bfd_link_info *, const char *, const char *, bfd *,
130 asection *, bfd_vma));
131 static void warning_find_reloc
132 PARAMS ((bfd *, asection *, PTR));
133 static bfd_boolean undefined_symbol
134 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
135 bfd_boolean));
136 static bfd_boolean reloc_overflow
137 PARAMS ((struct bfd_link_info *, const char *, const char *, bfd_vma,
138 bfd *, asection *, bfd_vma));
139 static bfd_boolean reloc_dangerous
140 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
141 static bfd_boolean unattached_reloc
142 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
143 static bfd_boolean notice
144 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
146 static struct bfd_link_callbacks link_callbacks =
148 add_archive_element,
149 multiple_definition,
150 multiple_common,
151 add_to_set,
152 constructor_callback,
153 warning_callback,
154 undefined_symbol,
155 reloc_overflow,
156 reloc_dangerous,
157 unattached_reloc,
158 notice,
159 error_handler
162 struct bfd_link_info link_info;
164 static void
165 remove_output ()
167 if (output_filename)
169 if (output_bfd && output_bfd->iostream)
170 fclose ((FILE *) (output_bfd->iostream));
171 if (delete_output_file_on_failure)
172 unlink (output_filename);
177 main (argc, argv)
178 int argc;
179 char **argv;
181 char *emulation;
182 long start_time = get_run_time ();
184 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
185 setlocale (LC_MESSAGES, "");
186 #endif
187 #if defined (HAVE_SETLOCALE)
188 setlocale (LC_CTYPE, "");
189 #endif
190 bindtextdomain (PACKAGE, LOCALEDIR);
191 textdomain (PACKAGE);
193 program_name = argv[0];
194 xmalloc_set_program_name (program_name);
196 START_PROGRESS (program_name, 0);
198 bfd_init ();
200 bfd_set_error_program_name (program_name);
202 xatexit (remove_output);
204 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
205 ld_sysroot = make_relative_prefix (program_name, BINDIR,
206 TARGET_SYSTEM_ROOT);
208 if (ld_sysroot)
210 struct stat s;
211 int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode);
213 if (!res)
215 free (ld_sysroot);
216 ld_sysroot = NULL;
220 if (! ld_sysroot)
222 ld_sysroot = make_relative_prefix (program_name, TOOLBINDIR,
223 TARGET_SYSTEM_ROOT);
225 if (ld_sysroot)
227 struct stat s;
228 int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode);
230 if (!res)
232 free (ld_sysroot);
233 ld_sysroot = NULL;
238 if (! ld_sysroot)
239 #endif
240 ld_sysroot = TARGET_SYSTEM_ROOT;
242 if (ld_sysroot && *ld_sysroot)
243 ld_canon_sysroot = lrealpath (ld_sysroot);
245 if (ld_canon_sysroot)
246 ld_canon_sysroot_len = strlen (ld_canon_sysroot);
247 else
248 ld_canon_sysroot_len = -1;
250 /* Set the default BFD target based on the configured target. Doing
251 this permits the linker to be configured for a particular target,
252 and linked against a shared BFD library which was configured for
253 a different target. The macro TARGET is defined by Makefile. */
254 if (! bfd_set_default_target (TARGET))
256 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
257 xexit (1);
260 #if YYDEBUG
262 extern int yydebug;
263 yydebug = 1;
265 #endif
267 /* Initialize the data about options. */
268 trace_files = trace_file_tries = version_printed = FALSE;
269 whole_archive = FALSE;
270 config.build_constructors = TRUE;
271 config.dynamic_link = FALSE;
272 config.has_shared = FALSE;
273 config.split_by_reloc = (unsigned) -1;
274 config.split_by_file = (bfd_size_type) -1;
275 command_line.force_common_definition = FALSE;
276 command_line.inhibit_common_definition = FALSE;
277 command_line.interpreter = NULL;
278 command_line.rpath = NULL;
279 command_line.warn_mismatch = TRUE;
280 command_line.check_section_addresses = TRUE;
281 command_line.accept_unknown_input_arch = FALSE;
283 /* We initialize DEMANGLING based on the environment variable
284 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
285 output of the linker, unless COLLECT_NO_DEMANGLE is set in the
286 environment. Acting the same way here lets us provide the same
287 interface by default. */
288 demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
290 link_info.relocateable = FALSE;
291 link_info.emitrelocations = FALSE;
292 link_info.task_link = FALSE;
293 link_info.shared = FALSE;
294 link_info.symbolic = FALSE;
295 link_info.export_dynamic = FALSE;
296 link_info.static_link = FALSE;
297 link_info.traditional_format = FALSE;
298 link_info.optimize = FALSE;
299 link_info.no_undefined = FALSE;
300 link_info.allow_shlib_undefined = TRUE;
301 link_info.allow_multiple_definition = FALSE;
302 link_info.allow_undefined_version = TRUE;
303 link_info.keep_memory = TRUE;
304 link_info.notice_all = FALSE;
305 link_info.nocopyreloc = FALSE;
306 link_info.new_dtags = FALSE;
307 link_info.combreloc = TRUE;
308 link_info.eh_frame_hdr = FALSE;
309 link_info.strip_discarded = TRUE;
310 link_info.strip = strip_none;
311 link_info.discard = discard_sec_merge;
312 link_info.common_skip_ar_aymbols = bfd_link_common_skip_none;
313 link_info.callbacks = &link_callbacks;
314 link_info.hash = NULL;
315 link_info.keep_hash = NULL;
316 link_info.notice_hash = NULL;
317 link_info.wrap_hash = NULL;
318 link_info.input_bfds = NULL;
319 link_info.create_object_symbols_section = NULL;
320 link_info.gc_sym_list = NULL;
321 link_info.base_file = NULL;
322 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
323 and _fini symbols. We are compatible. */
324 link_info.init_function = "_init";
325 link_info.fini_function = "_fini";
326 link_info.mpc860c0 = 0;
327 link_info.pei386_auto_import = -1;
328 link_info.pei386_runtime_pseudo_reloc = FALSE;
329 link_info.spare_dynamic_tags = 5;
330 link_info.flags = (bfd_vma) 0;
331 link_info.flags_1 = (bfd_vma) 0;
333 ldfile_add_arch ("");
335 config.make_executable = TRUE;
336 force_make_executable = FALSE;
337 config.magic_demand_paged = TRUE;
338 config.text_read_only = TRUE;
340 emulation = get_emulation (argc, argv);
341 ldemul_choose_mode (emulation);
342 default_target = ldemul_choose_target (argc, argv);
343 lang_init ();
344 ldemul_before_parse ();
345 lang_has_input_file = FALSE;
346 parse_args (argc, argv);
348 ldemul_set_symbols ();
350 if (link_info.relocateable)
352 if (command_line.gc_sections)
353 einfo ("%P%F: --gc-sections and -r may not be used together\n");
354 if (link_info.mpc860c0)
355 einfo (_("%P%F: -r and --mpc860c0 may not be used together\n"));
356 else if (command_line.relax)
357 einfo (_("%P%F: --relax and -r may not be used together\n"));
358 if (link_info.shared)
359 einfo (_("%P%F: -r and -shared may not be used together\n"));
362 if (! link_info.shared)
364 if (command_line.filter_shlib)
365 einfo (_("%P%F: -F may not be used without -shared\n"));
366 if (command_line.auxiliary_filters)
367 einfo (_("%P%F: -f may not be used without -shared\n"));
370 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
371 don't see how else this can be handled, since in this case we
372 must preserve all externally visible symbols. */
373 if (link_info.relocateable && link_info.strip == strip_all)
375 link_info.strip = strip_debugger;
376 if (link_info.discard == discard_sec_merge)
377 link_info.discard = discard_all;
380 /* This essentially adds another -L directory so this must be done after
381 the -L's in argv have been processed. */
382 set_scripts_dir ();
384 /* If we have not already opened and parsed a linker script
385 read the emulation's appropriate default script. */
386 if (saved_script_handle == NULL)
388 int isfile;
389 char *s = ldemul_get_script (&isfile);
391 if (isfile)
392 ldfile_open_command_file (s);
393 else
395 lex_string = s;
396 lex_redirect (s);
398 parser_input = input_script;
399 yyparse ();
400 lex_string = NULL;
403 if (trace_file_tries)
405 if (saved_script_handle)
406 info_msg (_("using external linker script:"));
407 else
408 info_msg (_("using internal linker script:"));
409 info_msg ("\n==================================================\n");
411 if (saved_script_handle)
413 static const int ld_bufsz = 8193;
414 size_t n;
415 char *buf = xmalloc (ld_bufsz);
417 rewind (saved_script_handle);
418 while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
420 buf[n] = 0;
421 info_msg (buf);
423 rewind (saved_script_handle);
424 free (buf);
426 else
428 int isfile;
430 info_msg (ldemul_get_script (&isfile));
433 info_msg ("\n==================================================\n");
436 lang_final ();
438 if (!lang_has_input_file)
440 if (version_printed)
441 xexit (0);
442 einfo (_("%P%F: no input files\n"));
445 if (trace_files)
446 info_msg (_("%P: mode %s\n"), emulation);
448 ldemul_after_parse ();
450 if (config.map_filename)
452 if (strcmp (config.map_filename, "-") == 0)
454 config.map_file = stdout;
456 else
458 config.map_file = fopen (config.map_filename, FOPEN_WT);
459 if (config.map_file == (FILE *) NULL)
461 bfd_set_error (bfd_error_system_call);
462 einfo (_("%P%F: cannot open map file %s: %E\n"),
463 config.map_filename);
468 lang_process ();
470 /* Print error messages for any missing symbols, for any warning
471 symbols, and possibly multiple definitions. */
472 if (link_info.relocateable)
473 output_bfd->flags &= ~EXEC_P;
474 else
475 output_bfd->flags |= EXEC_P;
477 ldwrite ();
479 if (config.map_file != NULL)
480 lang_map ();
481 if (command_line.cref)
482 output_cref (config.map_file != NULL ? config.map_file : stdout);
483 if (nocrossref_list != NULL)
484 check_nocrossrefs ();
486 /* Even if we're producing relocateable output, some non-fatal errors should
487 be reported in the exit status. (What non-fatal errors, if any, do we
488 want to ignore for relocateable output?) */
489 if (!config.make_executable && !force_make_executable)
491 if (trace_files)
492 einfo (_("%P: link errors found, deleting executable `%s'\n"),
493 output_filename);
495 /* The file will be removed by remove_output. */
496 xexit (1);
498 else
500 if (! bfd_close (output_bfd))
501 einfo (_("%F%B: final close failed: %E\n"), output_bfd);
503 /* If the --force-exe-suffix is enabled, and we're making an
504 executable file and it doesn't end in .exe, copy it to one
505 which does. */
506 if (! link_info.relocateable && command_line.force_exe_suffix)
508 int len = strlen (output_filename);
510 if (len < 4
511 || (strcasecmp (output_filename + len - 4, ".exe") != 0
512 && strcasecmp (output_filename + len - 4, ".dll") != 0))
514 FILE *src;
515 FILE *dst;
516 const int bsize = 4096;
517 char *buf = xmalloc (bsize);
518 int l;
519 char *dst_name = xmalloc (len + 5);
521 strcpy (dst_name, output_filename);
522 strcat (dst_name, ".exe");
523 src = fopen (output_filename, FOPEN_RB);
524 dst = fopen (dst_name, FOPEN_WB);
526 if (!src)
527 einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename);
528 if (!dst)
529 einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name);
530 while ((l = fread (buf, 1, bsize, src)) > 0)
532 int done = fwrite (buf, 1, l, dst);
534 if (done != l)
535 einfo (_("%P: Error writing file `%s'\n"), dst_name);
538 fclose (src);
539 if (fclose (dst) == EOF)
540 einfo (_("%P: Error closing file `%s'\n"), dst_name);
541 free (dst_name);
542 free (buf);
547 END_PROGRESS (program_name);
549 if (config.stats)
551 #ifdef HAVE_SBRK
552 char *lim = (char *) sbrk (0);
553 #endif
554 long run_time = get_run_time () - start_time;
556 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
557 program_name, run_time / 1000000, run_time % 1000000);
558 #ifdef HAVE_SBRK
559 fprintf (stderr, _("%s: data size %ld\n"), program_name,
560 (long) (lim - (char *) &environ));
561 #endif
564 /* Prevent remove_output from doing anything, after a successful link. */
565 output_filename = NULL;
567 xexit (0);
568 return 0;
571 /* We need to find any explicitly given emulation in order to initialize the
572 state that's needed by the lex&yacc argument parser (parse_args). */
574 static char *
575 get_emulation (argc, argv)
576 int argc;
577 char **argv;
579 char *emulation;
580 int i;
582 emulation = getenv (EMULATION_ENVIRON);
583 if (emulation == NULL)
584 emulation = DEFAULT_EMULATION;
586 for (i = 1; i < argc; i++)
588 if (!strncmp (argv[i], "-m", 2))
590 if (argv[i][2] == '\0')
592 /* -m EMUL */
593 if (i < argc - 1)
595 emulation = argv[i + 1];
596 i++;
598 else
599 einfo (_("%P%F: missing argument to -m\n"));
601 else if (strcmp (argv[i], "-mips1") == 0
602 || strcmp (argv[i], "-mips2") == 0
603 || strcmp (argv[i], "-mips3") == 0
604 || strcmp (argv[i], "-mips4") == 0
605 || strcmp (argv[i], "-mips5") == 0
606 || strcmp (argv[i], "-mips32") == 0
607 || strcmp (argv[i], "-mips32r2") == 0
608 || strcmp (argv[i], "-mips64") == 0)
610 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
611 passed to the linker by some MIPS compilers. They
612 generally tell the linker to use a slightly different
613 library path. Perhaps someday these should be
614 implemented as emulations; until then, we just ignore
615 the arguments and hope that nobody ever creates
616 emulations named ips1, ips2 or ips3. */
618 else if (strcmp (argv[i], "-m486") == 0)
620 /* FIXME: The argument -m486 is passed to the linker on
621 some Linux systems. Hope that nobody creates an
622 emulation named 486. */
624 else
626 /* -mEMUL */
627 emulation = &argv[i][2];
632 return emulation;
635 /* If directory DIR contains an "ldscripts" subdirectory,
636 add DIR to the library search path and return TRUE,
637 else return FALSE. */
639 static bfd_boolean
640 check_for_scripts_dir (dir)
641 char *dir;
643 size_t dirlen;
644 char *buf;
645 struct stat s;
646 bfd_boolean res;
648 dirlen = strlen (dir);
649 /* sizeof counts the terminating NUL. */
650 buf = (char *) xmalloc (dirlen + sizeof ("/ldscripts"));
651 sprintf (buf, "%s/ldscripts", dir);
653 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
654 free (buf);
655 if (res)
656 ldfile_add_library_path (dir, FALSE);
657 return res;
660 /* Set the default directory for finding script files.
661 Libraries will be searched for here too, but that's ok.
662 We look for the "ldscripts" directory in:
664 SCRIPTDIR (passed from Makefile)
665 (adjusted according to the current location of the binary)
666 SCRIPTDIR (passed from Makefile)
667 the dir where this program is (for using it from the build tree)
668 the dir where this program is/../lib
669 (for installing the tool suite elsewhere). */
671 static void
672 set_scripts_dir ()
674 char *end, *dir;
675 size_t dirlen;
676 bfd_boolean found;
678 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
679 if (dir)
681 found = check_for_scripts_dir (dir);
682 free (dir);
683 if (found)
684 return;
687 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
688 if (dir)
690 found = check_for_scripts_dir (dir);
691 free (dir);
692 if (found)
693 return;
696 if (check_for_scripts_dir (SCRIPTDIR))
697 /* We've been installed normally. */
698 return;
700 /* Look for "ldscripts" in the dir where our binary is. */
701 end = strrchr (program_name, '/');
702 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
704 /* We could have \foo\bar, or /foo\bar. */
705 char *bslash = strrchr (program_name, '\\');
707 if (end == NULL || (bslash != NULL && bslash > end))
708 end = bslash;
710 #endif
712 if (end == NULL)
713 /* Don't look for ldscripts in the current directory. There is
714 too much potential for confusion. */
715 return;
717 dirlen = end - program_name;
718 /* Make a copy of program_name in dir.
719 Leave room for later "/../lib". */
720 dir = (char *) xmalloc (dirlen + 8);
721 strncpy (dir, program_name, dirlen);
722 dir[dirlen] = '\0';
724 if (check_for_scripts_dir (dir))
726 free (dir);
727 return;
730 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
731 strcpy (dir + dirlen, "/../lib");
732 check_for_scripts_dir (dir);
733 free (dir);
736 void
737 add_ysym (name)
738 const char *name;
740 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
742 link_info.notice_hash = ((struct bfd_hash_table *)
743 xmalloc (sizeof (struct bfd_hash_table)));
744 if (! bfd_hash_table_init_n (link_info.notice_hash,
745 bfd_hash_newfunc,
746 61))
747 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
750 if (bfd_hash_lookup (link_info.notice_hash, name, TRUE, TRUE)
751 == (struct bfd_hash_entry *) NULL)
752 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
755 /* Record a symbol to be wrapped, from the --wrap option. */
757 void
758 add_wrap (name)
759 const char *name;
761 if (link_info.wrap_hash == NULL)
763 link_info.wrap_hash = ((struct bfd_hash_table *)
764 xmalloc (sizeof (struct bfd_hash_table)));
765 if (! bfd_hash_table_init_n (link_info.wrap_hash,
766 bfd_hash_newfunc,
767 61))
768 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
771 if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL)
772 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
775 /* Handle the -retain-symbols-file option. */
777 void
778 add_keepsyms_file (filename)
779 const char *filename;
781 FILE *file;
782 char *buf;
783 size_t bufsize;
784 int c;
786 if (link_info.strip == strip_some)
787 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
789 file = fopen (filename, "r");
790 if (file == (FILE *) NULL)
792 bfd_set_error (bfd_error_system_call);
793 einfo ("%X%P: %s: %E\n", filename);
794 return;
797 link_info.keep_hash = ((struct bfd_hash_table *)
798 xmalloc (sizeof (struct bfd_hash_table)));
799 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
800 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
802 bufsize = 100;
803 buf = (char *) xmalloc (bufsize);
805 c = getc (file);
806 while (c != EOF)
808 while (ISSPACE (c))
809 c = getc (file);
811 if (c != EOF)
813 size_t len = 0;
815 while (! ISSPACE (c) && c != EOF)
817 buf[len] = c;
818 ++len;
819 if (len >= bufsize)
821 bufsize *= 2;
822 buf = xrealloc (buf, bufsize);
824 c = getc (file);
827 buf[len] = '\0';
829 if (bfd_hash_lookup (link_info.keep_hash, buf, TRUE, TRUE)
830 == (struct bfd_hash_entry *) NULL)
831 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
835 if (link_info.strip != strip_none)
836 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
838 free (buf);
839 link_info.strip = strip_some;
842 /* Callbacks from the BFD linker routines. */
844 /* This is called when BFD has decided to include an archive member in
845 a link. */
847 static bfd_boolean
848 add_archive_element (info, abfd, name)
849 struct bfd_link_info *info ATTRIBUTE_UNUSED;
850 bfd *abfd;
851 const char *name;
853 lang_input_statement_type *input;
855 input = ((lang_input_statement_type *)
856 xmalloc (sizeof (lang_input_statement_type)));
857 input->filename = abfd->filename;
858 input->local_sym_name = abfd->filename;
859 input->the_bfd = abfd;
860 input->asymbols = NULL;
861 input->next = NULL;
862 input->just_syms_flag = FALSE;
863 input->loaded = FALSE;
864 input->search_dirs_flag = FALSE;
866 /* FIXME: The following fields are not set: header.next,
867 header.type, closed, passive_position, symbol_count,
868 next_real_file, is_archive, target, real. This bit of code is
869 from the old decode_library_subfile function. I don't know
870 whether any of those fields matters. */
872 ldlang_add_file (input);
874 if (config.map_file != (FILE *) NULL)
876 static bfd_boolean header_printed;
877 struct bfd_link_hash_entry *h;
878 bfd *from;
879 int len;
881 h = bfd_link_hash_lookup (link_info.hash, name, FALSE, FALSE, TRUE);
883 if (h == NULL)
884 from = NULL;
885 else
887 switch (h->type)
889 default:
890 from = NULL;
891 break;
893 case bfd_link_hash_defined:
894 case bfd_link_hash_defweak:
895 from = h->u.def.section->owner;
896 break;
898 case bfd_link_hash_undefined:
899 case bfd_link_hash_undefweak:
900 from = h->u.undef.abfd;
901 break;
903 case bfd_link_hash_common:
904 from = h->u.c.p->section->owner;
905 break;
909 if (! header_printed)
911 char buf[100];
913 sprintf (buf, _("Archive member included because of file (symbol)\n\n"));
914 minfo ("%s", buf);
915 header_printed = TRUE;
918 if (bfd_my_archive (abfd) == NULL)
920 minfo ("%s", bfd_get_filename (abfd));
921 len = strlen (bfd_get_filename (abfd));
923 else
925 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)),
926 bfd_get_filename (abfd));
927 len = (strlen (bfd_get_filename (bfd_my_archive (abfd)))
928 + strlen (bfd_get_filename (abfd))
929 + 2);
932 if (len >= 29)
934 print_nl ();
935 len = 0;
937 while (len < 30)
939 print_space ();
940 ++len;
943 if (from != NULL)
944 minfo ("%B ", from);
945 if (h != NULL)
946 minfo ("(%T)\n", h->root.string);
947 else
948 minfo ("(%s)\n", name);
951 if (trace_files || trace_file_tries)
952 info_msg ("%I\n", input);
954 return TRUE;
957 /* This is called when BFD has discovered a symbol which is defined
958 multiple times. */
960 static bfd_boolean
961 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
962 struct bfd_link_info *info ATTRIBUTE_UNUSED;
963 const char *name;
964 bfd *obfd;
965 asection *osec;
966 bfd_vma oval;
967 bfd *nbfd;
968 asection *nsec;
969 bfd_vma nval;
971 /* If either section has the output_section field set to
972 bfd_abs_section_ptr, it means that the section is being
973 discarded, and this is not really a multiple definition at all.
974 FIXME: It would be cleaner to somehow ignore symbols defined in
975 sections which are being discarded. */
976 if ((osec->output_section != NULL
977 && ! bfd_is_abs_section (osec)
978 && bfd_is_abs_section (osec->output_section))
979 || (nsec->output_section != NULL
980 && ! bfd_is_abs_section (nsec)
981 && bfd_is_abs_section (nsec->output_section)))
982 return TRUE;
984 einfo (_("%X%C: multiple definition of `%T'\n"),
985 nbfd, nsec, nval, name);
986 if (obfd != (bfd *) NULL)
987 einfo (_("%D: first defined here\n"), obfd, osec, oval);
989 if (command_line.relax)
991 einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
992 command_line.relax = 0;
995 return TRUE;
998 /* This is called when there is a definition of a common symbol, or
999 when a common symbol is found for a symbol that is already defined,
1000 or when two common symbols are found. We only do something if
1001 -warn-common was used. */
1003 static bfd_boolean
1004 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
1005 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1006 const char *name;
1007 bfd *obfd;
1008 enum bfd_link_hash_type otype;
1009 bfd_vma osize;
1010 bfd *nbfd;
1011 enum bfd_link_hash_type ntype;
1012 bfd_vma nsize;
1014 if (! config.warn_common)
1015 return TRUE;
1017 if (ntype == bfd_link_hash_defined
1018 || ntype == bfd_link_hash_defweak
1019 || ntype == bfd_link_hash_indirect)
1021 ASSERT (otype == bfd_link_hash_common);
1022 einfo (_("%B: warning: definition of `%T' overriding common\n"),
1023 nbfd, name);
1024 if (obfd != NULL)
1025 einfo (_("%B: warning: common is here\n"), obfd);
1027 else if (otype == bfd_link_hash_defined
1028 || otype == bfd_link_hash_defweak
1029 || otype == bfd_link_hash_indirect)
1031 ASSERT (ntype == bfd_link_hash_common);
1032 einfo (_("%B: warning: common of `%T' overridden by definition\n"),
1033 nbfd, name);
1034 if (obfd != NULL)
1035 einfo (_("%B: warning: defined here\n"), obfd);
1037 else
1039 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
1040 if (osize > nsize)
1042 einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
1043 nbfd, name);
1044 if (obfd != NULL)
1045 einfo (_("%B: warning: larger common is here\n"), obfd);
1047 else if (nsize > osize)
1049 einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
1050 nbfd, name);
1051 if (obfd != NULL)
1052 einfo (_("%B: warning: smaller common is here\n"), obfd);
1054 else
1056 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name);
1057 if (obfd != NULL)
1058 einfo (_("%B: warning: previous common is here\n"), obfd);
1062 return TRUE;
1065 /* This is called when BFD has discovered a set element. H is the
1066 entry in the linker hash table for the set. SECTION and VALUE
1067 represent a value which should be added to the set. */
1069 static bfd_boolean
1070 add_to_set (info, h, reloc, abfd, section, value)
1071 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1072 struct bfd_link_hash_entry *h;
1073 bfd_reloc_code_real_type reloc;
1074 bfd *abfd;
1075 asection *section;
1076 bfd_vma value;
1078 if (config.warn_constructors)
1079 einfo (_("%P: warning: global constructor %s used\n"),
1080 h->root.string);
1082 if (! config.build_constructors)
1083 return TRUE;
1085 ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value);
1087 if (h->type == bfd_link_hash_new)
1089 h->type = bfd_link_hash_undefined;
1090 h->u.undef.abfd = abfd;
1091 /* We don't call bfd_link_add_undef to add this to the list of
1092 undefined symbols because we are going to define it
1093 ourselves. */
1096 return TRUE;
1099 /* This is called when BFD has discovered a constructor. This is only
1100 called for some object file formats--those which do not handle
1101 constructors in some more clever fashion. This is similar to
1102 adding an element to a set, but less general. */
1104 static bfd_boolean
1105 constructor_callback (info, constructor, name, abfd, section, value)
1106 struct bfd_link_info *info;
1107 bfd_boolean constructor;
1108 const char *name;
1109 bfd *abfd;
1110 asection *section;
1111 bfd_vma value;
1113 char *s;
1114 struct bfd_link_hash_entry *h;
1115 char set_name[1 + sizeof "__CTOR_LIST__"];
1117 if (config.warn_constructors)
1118 einfo (_("%P: warning: global constructor %s used\n"), name);
1120 if (! config.build_constructors)
1121 return TRUE;
1123 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1124 useful error message. */
1125 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL
1126 && (link_info.relocateable
1127 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
1128 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1130 s = set_name;
1131 if (bfd_get_symbol_leading_char (abfd) != '\0')
1132 *s++ = bfd_get_symbol_leading_char (abfd);
1133 if (constructor)
1134 strcpy (s, "__CTOR_LIST__");
1135 else
1136 strcpy (s, "__DTOR_LIST__");
1138 h = bfd_link_hash_lookup (info->hash, set_name, TRUE, TRUE, TRUE);
1139 if (h == (struct bfd_link_hash_entry *) NULL)
1140 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1141 if (h->type == bfd_link_hash_new)
1143 h->type = bfd_link_hash_undefined;
1144 h->u.undef.abfd = abfd;
1145 /* We don't call bfd_link_add_undef to add this to the list of
1146 undefined symbols because we are going to define it
1147 ourselves. */
1150 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
1151 return TRUE;
1154 /* A structure used by warning_callback to pass information through
1155 bfd_map_over_sections. */
1157 struct warning_callback_info
1159 bfd_boolean found;
1160 const char *warning;
1161 const char *symbol;
1162 asymbol **asymbols;
1165 /* This is called when there is a reference to a warning symbol. */
1167 static bfd_boolean
1168 warning_callback (info, warning, symbol, abfd, section, address)
1169 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1170 const char *warning;
1171 const char *symbol;
1172 bfd *abfd;
1173 asection *section;
1174 bfd_vma address;
1176 /* This is a hack to support warn_multiple_gp. FIXME: This should
1177 have a cleaner interface, but what? */
1178 if (! config.warn_multiple_gp
1179 && strcmp (warning, "using multiple gp values") == 0)
1180 return TRUE;
1182 if (section != NULL)
1183 einfo ("%C: %s\n", abfd, section, address, warning);
1184 else if (abfd == NULL)
1185 einfo ("%P: %s\n", warning);
1186 else if (symbol == NULL)
1187 einfo ("%B: %s\n", abfd, warning);
1188 else
1190 lang_input_statement_type *entry;
1191 asymbol **asymbols;
1192 struct warning_callback_info info;
1194 /* Look through the relocs to see if we can find a plausible
1195 address. */
1196 entry = (lang_input_statement_type *) abfd->usrdata;
1197 if (entry != NULL && entry->asymbols != NULL)
1198 asymbols = entry->asymbols;
1199 else
1201 long symsize;
1202 long symbol_count;
1204 symsize = bfd_get_symtab_upper_bound (abfd);
1205 if (symsize < 0)
1206 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1207 asymbols = (asymbol **) xmalloc (symsize);
1208 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
1209 if (symbol_count < 0)
1210 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1211 if (entry != NULL)
1213 entry->asymbols = asymbols;
1214 entry->symbol_count = symbol_count;
1218 info.found = FALSE;
1219 info.warning = warning;
1220 info.symbol = symbol;
1221 info.asymbols = asymbols;
1222 bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info);
1224 if (! info.found)
1225 einfo ("%B: %s\n", abfd, warning);
1227 if (entry == NULL)
1228 free (asymbols);
1231 return TRUE;
1234 /* This is called by warning_callback for each section. It checks the
1235 relocs of the section to see if it can find a reference to the
1236 symbol which triggered the warning. If it can, it uses the reloc
1237 to give an error message with a file and line number. */
1239 static void
1240 warning_find_reloc (abfd, sec, iarg)
1241 bfd *abfd;
1242 asection *sec;
1243 PTR iarg;
1245 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
1246 long relsize;
1247 arelent **relpp;
1248 long relcount;
1249 arelent **p, **pend;
1251 if (info->found)
1252 return;
1254 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1255 if (relsize < 0)
1256 einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1257 if (relsize == 0)
1258 return;
1260 relpp = (arelent **) xmalloc (relsize);
1261 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1262 if (relcount < 0)
1263 einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1265 p = relpp;
1266 pend = p + relcount;
1267 for (; p < pend && *p != NULL; p++)
1269 arelent *q = *p;
1271 if (q->sym_ptr_ptr != NULL
1272 && *q->sym_ptr_ptr != NULL
1273 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1275 /* We found a reloc for the symbol we are looking for. */
1276 einfo ("%C: %s\n", abfd, sec, q->address, info->warning);
1277 info->found = TRUE;
1278 break;
1282 free (relpp);
1285 /* This is called when an undefined symbol is found. */
1287 static bfd_boolean
1288 undefined_symbol (info, name, abfd, section, address, fatal)
1289 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1290 const char *name;
1291 bfd *abfd;
1292 asection *section;
1293 bfd_vma address;
1294 bfd_boolean fatal ATTRIBUTE_UNUSED;
1296 static char *error_name;
1297 static unsigned int error_count;
1299 #define MAX_ERRORS_IN_A_ROW 5
1301 if (config.warn_once)
1303 static struct bfd_hash_table *hash;
1305 /* Only warn once about a particular undefined symbol. */
1306 if (hash == NULL)
1308 hash = ((struct bfd_hash_table *)
1309 xmalloc (sizeof (struct bfd_hash_table)));
1310 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
1311 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
1314 if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL)
1315 return TRUE;
1317 if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL)
1318 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
1321 /* We never print more than a reasonable number of errors in a row
1322 for a single symbol. */
1323 if (error_name != (char *) NULL
1324 && strcmp (name, error_name) == 0)
1325 ++error_count;
1326 else
1328 error_count = 0;
1329 if (error_name != (char *) NULL)
1330 free (error_name);
1331 error_name = xstrdup (name);
1334 if (section != NULL)
1336 if (error_count < MAX_ERRORS_IN_A_ROW)
1338 einfo (_("%C: undefined reference to `%T'\n"),
1339 abfd, section, address, name);
1340 if (fatal)
1341 einfo ("%X");
1343 else if (error_count == MAX_ERRORS_IN_A_ROW)
1344 einfo (_("%D: more undefined references to `%T' follow\n"),
1345 abfd, section, address, name);
1347 else
1349 if (error_count < MAX_ERRORS_IN_A_ROW)
1351 einfo (_("%B: undefined reference to `%T'\n"),
1352 abfd, name);
1353 if (fatal)
1354 einfo ("%X");
1356 else if (error_count == MAX_ERRORS_IN_A_ROW)
1357 einfo (_("%B: more undefined references to `%T' follow\n"),
1358 abfd, name);
1361 return TRUE;
1364 /* This is called when a reloc overflows. */
1366 static bfd_boolean
1367 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
1368 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1369 const char *name;
1370 const char *reloc_name;
1371 bfd_vma addend;
1372 bfd *abfd;
1373 asection *section;
1374 bfd_vma address;
1376 if (abfd == (bfd *) NULL)
1377 einfo (_("%P%X: generated"));
1378 else
1379 einfo ("%X%C:", abfd, section, address);
1380 einfo (_(" relocation truncated to fit: %s %T"), reloc_name, name);
1381 if (addend != 0)
1382 einfo ("+%v", addend);
1383 einfo ("\n");
1384 return TRUE;
1387 /* This is called when a dangerous relocation is made. */
1389 static bfd_boolean
1390 reloc_dangerous (info, message, abfd, section, address)
1391 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1392 const char *message;
1393 bfd *abfd;
1394 asection *section;
1395 bfd_vma address;
1397 if (abfd == (bfd *) NULL)
1398 einfo (_("%P%X: generated"));
1399 else
1400 einfo ("%X%C:", abfd, section, address);
1401 einfo (_("dangerous relocation: %s\n"), message);
1402 return TRUE;
1405 /* This is called when a reloc is being generated attached to a symbol
1406 that is not being output. */
1408 static bfd_boolean
1409 unattached_reloc (info, name, abfd, section, address)
1410 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1411 const char *name;
1412 bfd *abfd;
1413 asection *section;
1414 bfd_vma address;
1416 if (abfd == (bfd *) NULL)
1417 einfo (_("%P%X: generated"));
1418 else
1419 einfo ("%X%C:", abfd, section, address);
1420 einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name);
1421 return TRUE;
1424 /* This is called if link_info.notice_all is set, or when a symbol in
1425 link_info.notice_hash is found. Symbols are put in notice_hash
1426 using the -y option. */
1428 static bfd_boolean
1429 notice (info, name, abfd, section, value)
1430 struct bfd_link_info *info;
1431 const char *name;
1432 bfd *abfd;
1433 asection *section;
1434 bfd_vma value;
1436 if (! info->notice_all
1437 || (info->notice_hash != NULL
1438 && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
1440 if (bfd_is_und_section (section))
1441 einfo ("%B: reference to %s\n", abfd, name);
1442 else
1443 einfo ("%B: definition of %s\n", abfd, name);
1446 if (command_line.cref || nocrossref_list != NULL)
1447 add_cref (name, abfd, section, value);
1449 return TRUE;