1 /* Main program of GNU linker.
2 Copyright (C) 1991, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Written by Steve Chamberlain steve@cygnus.com
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "libiberty.h"
42 /* Somewhere above, sys/stat.h got included . . . . */
43 #if !defined(S_ISDIR) && defined(S_IFDIR)
44 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
49 static char *get_emulation PARAMS ((int, char **));
50 static void set_scripts_dir PARAMS ((void));
55 const char *output_filename = "a.out";
57 /* Name this program was invoked by. */
60 /* The file that we're creating */
63 /* Set by -G argument, for MIPS ECOFF target. */
64 int g_switch_value = 8;
66 /* Nonzero means print names of input files as processed. */
69 /* Nonzero means same, but note open failures, too. */
70 boolean trace_file_tries;
72 /* Nonzero means version number was printed, so exit successfully
73 instead of complaining if no input files are given. */
74 boolean version_printed;
76 /* Nonzero means link in every member of an archive. */
77 boolean whole_archive;
79 args_type command_line;
81 ld_config_type config;
83 static boolean check_for_scripts_dir PARAMS ((char *dir));
84 static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
86 static boolean multiple_definition PARAMS ((struct bfd_link_info *,
88 bfd *, asection *, bfd_vma,
89 bfd *, asection *, bfd_vma));
90 static boolean multiple_common PARAMS ((struct bfd_link_info *,
92 enum bfd_link_hash_type, bfd_vma,
93 bfd *, enum bfd_link_hash_type,
95 static boolean add_to_set PARAMS ((struct bfd_link_info *,
96 struct bfd_link_hash_entry *,
97 bfd_reloc_code_real_type,
98 bfd *, asection *, bfd_vma));
99 static boolean constructor_callback PARAMS ((struct bfd_link_info *,
102 bfd *, asection *, bfd_vma));
103 static boolean warning_callback PARAMS ((struct bfd_link_info *,
104 const char *, const char *, bfd *,
105 asection *, bfd_vma));
106 static void warning_find_reloc PARAMS ((bfd *, asection *, PTR));
107 static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
109 asection *, bfd_vma));
110 static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
111 const char *, bfd_vma,
112 bfd *, asection *, bfd_vma));
113 static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
114 bfd *, asection *, bfd_vma));
115 static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
116 const char *, bfd *, asection *,
118 static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
119 bfd *, asection *, bfd_vma));
121 static struct bfd_link_callbacks link_callbacks =
127 constructor_callback,
136 struct bfd_link_info link_info;
143 if (output_bfd && output_bfd->iostream)
144 fclose((FILE *)(output_bfd->iostream));
145 if (delete_output_file_on_failure)
146 unlink (output_filename);
156 long start_time = get_run_time ();
158 program_name = argv[0];
159 xmalloc_set_program_name (program_name);
161 START_PROGRESS (program_name, 0);
165 xatexit (remove_output);
167 /* Initialize the data about options. */
168 trace_files = trace_file_tries = version_printed = false;
169 whole_archive = false;
170 config.traditional_format = false;
171 config.build_constructors = true;
172 config.dynamic_link = false;
173 command_line.force_common_definition = false;
174 command_line.interpreter = NULL;
175 command_line.rpath = NULL;
177 link_info.callbacks = &link_callbacks;
178 link_info.relocateable = false;
179 link_info.shared = false;
180 link_info.symbolic = false;
181 link_info.static_link = false;
182 link_info.strip = strip_none;
183 link_info.discard = discard_none;
184 link_info.lprefix_len = 1;
185 link_info.lprefix = "L";
186 link_info.keep_memory = true;
187 link_info.input_bfds = NULL;
188 link_info.create_object_symbols_section = NULL;
189 link_info.hash = NULL;
190 link_info.keep_hash = NULL;
191 link_info.notice_hash = NULL;
194 ldfile_add_arch ("");
196 config.make_executable = true;
197 force_make_executable = false;
198 config.magic_demand_paged = true;
199 config.text_read_only = true;
200 config.make_executable = true;
202 emulation = get_emulation (argc, argv);
203 ldemul_choose_mode (emulation);
204 default_target = ldemul_choose_target ();
206 ldemul_before_parse ();
207 lang_has_input_file = false;
208 parse_args (argc, argv);
210 ldemul_set_symbols ();
212 if (link_info.relocateable)
214 if (command_line.relax)
215 einfo ("%P%F: -relax and -r may not be used together\n");
216 if (link_info.shared)
217 einfo ("%P%F: -r and -shared may not be used together\n");
220 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
221 don't see how else this can be handled, since in this case we
222 must preserve all externally visible symbols. */
223 if (link_info.relocateable && link_info.strip == strip_all)
225 link_info.strip = strip_debugger;
226 if (link_info.discard == discard_none)
227 link_info.discard = discard_all;
230 /* This essentially adds another -L directory so this must be done after
231 the -L's in argv have been processed. */
234 if (had_script == false)
236 /* Read the emulation's appropriate default script. */
238 char *s = ldemul_get_script (&isfile);
241 ldfile_open_command_file (s);
244 if (trace_file_tries)
246 info_msg ("using internal linker script:\n");
247 info_msg ("==================================================\n");
249 info_msg ("\n==================================================\n");
254 parser_input = input_script;
261 if (lang_has_input_file == false)
265 einfo ("%P%F: no input files\n");
270 info_msg ("%P: mode %s\n", emulation);
273 ldemul_after_parse ();
276 if (config.map_filename)
278 if (strcmp (config.map_filename, "-") == 0)
280 config.map_file = stdout;
284 config.map_file = fopen (config.map_filename, FOPEN_WT);
285 if (config.map_file == (FILE *) NULL)
287 bfd_set_error (bfd_error_system_call);
288 einfo ("%P%F: cannot open map file %s: %E\n",
289 config.map_filename);
297 /* Print error messages for any missing symbols, for any warning
298 symbols, and possibly multiple definitions */
301 if (config.text_read_only)
303 /* Look for a text section and mark the readonly attribute in it */
304 asection *found = bfd_get_section_by_name (output_bfd, ".text");
306 if (found != (asection *) NULL)
308 found->flags |= SEC_READONLY;
312 if (link_info.relocateable)
313 output_bfd->flags &= ~EXEC_P;
315 output_bfd->flags |= EXEC_P;
319 /* Even if we're producing relocateable output, some non-fatal errors should
320 be reported in the exit status. (What non-fatal errors, if any, do we
321 want to ignore for relocateable output?) */
323 if (config.make_executable == false && force_make_executable == false)
325 if (trace_files == true)
327 einfo ("%P: link errors found, deleting executable `%s'\n",
331 if (output_bfd->iostream)
332 fclose ((FILE *) (output_bfd->iostream));
334 unlink (output_filename);
339 if (! bfd_close (output_bfd))
340 einfo ("%F%B: final close failed: %E\n", output_bfd);
343 END_PROGRESS (program_name);
347 extern char **environ;
349 char *lim = (char *) sbrk (0);
351 long run_time = get_run_time () - start_time;
353 fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
354 program_name, run_time / 1000000, run_time % 1000000);
356 fprintf (stderr, "%s: data size %ld\n", program_name,
357 (long) (lim - (char *) &environ));
361 /* Prevent remove_output from doing anything, after a successful link. */
362 output_filename = NULL;
368 /* We need to find any explicitly given emulation in order to initialize the
369 state that's needed by the lex&yacc argument parser (parse_args). */
372 get_emulation (argc, argv)
379 emulation = (char *) getenv (EMULATION_ENVIRON);
380 if (emulation == NULL)
381 emulation = DEFAULT_EMULATION;
383 for (i = 1; i < argc; i++)
385 if (!strncmp (argv[i], "-m", 2))
387 if (argv[i][2] == '\0')
392 emulation = argv[i + 1];
397 einfo("%P%F: missing argument to -m\n");
400 else if (strcmp (argv[i], "-mips1") == 0
401 || strcmp (argv[i], "-mips2") == 0
402 || strcmp (argv[i], "-mips3") == 0)
404 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
405 passed to the linker by some MIPS compilers. They
406 generally tell the linker to use a slightly different
407 library path. Perhaps someday these should be
408 implemented as emulations; until then, we just ignore
409 the arguments and hope that nobody ever creates
410 emulations named ips1, ips2 or ips3. */
412 else if (strcmp (argv[i], "-m486") == 0)
414 /* FIXME: The argument -m486 is passed to the linker on
415 some Linux systems. Hope that nobody creates an
416 emulation named 486. */
421 emulation = &argv[i][2];
429 /* If directory DIR contains an "ldscripts" subdirectory,
430 add DIR to the library search path and return true,
431 else return false. */
434 check_for_scripts_dir (dir)
442 dirlen = strlen (dir);
443 /* sizeof counts the terminating NUL. */
444 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
445 sprintf (buf, "%s/ldscripts", dir);
447 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
450 ldfile_add_library_path (dir, false);
454 /* Set the default directory for finding script files.
455 Libraries will be searched for here too, but that's ok.
456 We look for the "ldscripts" directory in:
458 SCRIPTDIR (passed from Makefile)
459 the dir where this program is (for using it from the build tree)
460 the dir where this program is/../lib (for installing the tool suite elsewhere) */
468 if (check_for_scripts_dir (SCRIPTDIR))
469 return; /* We've been installed normally. */
471 /* Look for "ldscripts" in the dir where our binary is. */
472 end = strrchr (program_name, '/');
476 /* Don't look for ldscripts in the current directory. There is
477 too much potential for confusion. */
481 dirlen = end - program_name;
482 /* Make a copy of program_name in dir.
483 Leave room for later "/../lib". */
484 dir = (char *) xmalloc (dirlen + 8);
485 strncpy (dir, program_name, dirlen);
488 if (check_for_scripts_dir (dir))
489 return; /* Don't free dir. */
491 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
492 strcpy (dir + dirlen, "/../lib");
493 if (check_for_scripts_dir (dir))
496 free (dir); /* Well, we tried. */
503 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
505 link_info.notice_hash = ((struct bfd_hash_table *)
506 xmalloc (sizeof (struct bfd_hash_table)));
507 if (! bfd_hash_table_init_n (link_info.notice_hash,
510 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
513 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
514 == (struct bfd_hash_entry *) NULL)
515 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
518 /* Handle the -retain-symbols-file option. */
521 add_keepsyms_file (filename)
522 const char *filename;
529 if (link_info.strip == strip_some)
530 einfo ("%X%P: error: duplicate retain-symbols-file\n");
532 file = fopen (filename, "r");
533 if (file == (FILE *) NULL)
535 bfd_set_error (bfd_error_system_call);
536 einfo ("%X%P: %s: %E\n", filename);
540 link_info.keep_hash = ((struct bfd_hash_table *)
541 xmalloc (sizeof (struct bfd_hash_table)));
542 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
543 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
546 buf = (char *) xmalloc (bufsize);
558 while (! isspace (c) && c != EOF)
565 buf = xrealloc (buf, bufsize);
572 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
573 == (struct bfd_hash_entry *) NULL)
574 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E\n");
578 if (link_info.strip != strip_none)
579 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
581 link_info.strip = strip_some;
584 /* Callbacks from the BFD linker routines. */
586 /* This is called when BFD has decided to include an archive member in
591 add_archive_element (info, abfd, name)
592 struct bfd_link_info *info;
596 lang_input_statement_type *input;
598 input = ((lang_input_statement_type *)
599 xmalloc (sizeof (lang_input_statement_type)));
600 input->filename = abfd->filename;
601 input->local_sym_name = abfd->filename;
602 input->the_bfd = abfd;
603 input->asymbols = NULL;
605 input->just_syms_flag = false;
606 input->loaded = false;
607 input->search_dirs_flag = false;
609 /* FIXME: The following fields are not set: header.next,
610 header.type, closed, passive_position, symbol_count,
611 next_real_file, is_archive, target, real, common_section,
612 common_output_section, complained. This bit of code is from the
613 old decode_library_subfile function. I don't know whether any of
614 those fields matters. */
616 ldlang_add_file (input);
618 if (config.map_file != (FILE *) NULL)
619 minfo ("%s needed due to %T\n", abfd->filename, name);
621 if (trace_files || trace_file_tries)
622 info_msg ("%I\n", input);
627 /* This is called when BFD has discovered a symbol which is defined
632 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
633 struct bfd_link_info *info;
642 einfo ("%X%C: multiple definition of `%T'\n",
643 nbfd, nsec, nval, name);
644 if (obfd != (bfd *) NULL)
645 einfo ("%D: first defined here\n", obfd, osec, oval);
649 /* This is called when there is a definition of a common symbol, or
650 when a common symbol is found for a symbol that is already defined,
651 or when two common symbols are found. We only do something if
652 -warn-common was used. */
656 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
657 struct bfd_link_info *info;
660 enum bfd_link_hash_type otype;
663 enum bfd_link_hash_type ntype;
666 if (! config.warn_common)
669 if (ntype == bfd_link_hash_defined
670 || ntype == bfd_link_hash_defweak
671 || ntype == bfd_link_hash_indirect)
673 ASSERT (otype == bfd_link_hash_common);
674 einfo ("%B: warning: definition of `%T' overriding common\n",
677 einfo ("%B: warning: common is here\n", obfd);
679 else if (otype == bfd_link_hash_defined
680 || otype == bfd_link_hash_defweak
681 || otype == bfd_link_hash_indirect)
683 ASSERT (ntype == bfd_link_hash_common);
684 einfo ("%B: warning: common of `%T' overridden by definition\n",
687 einfo ("%B: warning: defined here\n", obfd);
691 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
694 einfo ("%B: warning: common of `%T' overridden by larger common\n",
697 einfo ("%B: warning: larger common is here\n", obfd);
699 else if (nsize > osize)
701 einfo ("%B: warning: common of `%T' overriding smaller common\n",
704 einfo ("%B: warning: smaller common is here\n", obfd);
708 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
710 einfo ("%B: warning: previous common is here\n", obfd);
717 /* This is called when BFD has discovered a set element. H is the
718 entry in the linker hash table for the set. SECTION and VALUE
719 represent a value which should be added to the set. */
723 add_to_set (info, h, reloc, abfd, section, value)
724 struct bfd_link_info *info;
725 struct bfd_link_hash_entry *h;
726 bfd_reloc_code_real_type reloc;
731 if (config.warn_constructors)
732 einfo ("%P: warning: global constructor %s used\n",
735 if (! config.build_constructors)
738 ldctor_add_set_entry (h, reloc, section, value);
740 if (h->type == bfd_link_hash_new)
742 h->type = bfd_link_hash_undefined;
743 h->u.undef.abfd = abfd;
744 /* We don't call bfd_link_add_undef to add this to the list of
745 undefined symbols because we are going to define it
752 /* This is called when BFD has discovered a constructor. This is only
753 called for some object file formats--those which do not handle
754 constructors in some more clever fashion. This is similar to
755 adding an element to a set, but less general. */
758 constructor_callback (info, constructor, name, abfd, section, value)
759 struct bfd_link_info *info;
767 struct bfd_link_hash_entry *h;
768 char set_name[1 + sizeof "__CTOR_LIST__"];
770 if (config.warn_constructors)
771 einfo ("%P: warning: global constructor %s used\n", name);
773 if (! config.build_constructors)
776 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
777 useful error message. */
778 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL)
779 einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n");
782 if (bfd_get_symbol_leading_char (abfd) != '\0')
783 *s++ = bfd_get_symbol_leading_char (abfd);
785 strcpy (s, "__CTOR_LIST__");
787 strcpy (s, "__DTOR_LIST__");
789 if (config.map_file != (FILE *) NULL)
790 fprintf (config.map_file,
791 "Adding %s to constructor/destructor set %s\n", name, set_name);
793 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
794 if (h == (struct bfd_link_hash_entry *) NULL)
795 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
796 if (h->type == bfd_link_hash_new)
798 h->type = bfd_link_hash_undefined;
799 h->u.undef.abfd = abfd;
800 /* We don't call bfd_link_add_undef to add this to the list of
801 undefined symbols because we are going to define it
805 ldctor_add_set_entry (h, BFD_RELOC_CTOR, section, value);
809 /* A structure used by warning_callback to pass information through
810 bfd_map_over_sections. */
812 struct warning_callback_info
820 /* This is called when there is a reference to a warning symbol. */
824 warning_callback (info, warning, symbol, abfd, section, address)
825 struct bfd_link_info *info;
833 einfo ("%C: %s\n", abfd, section, address, warning);
834 else if (abfd == NULL)
835 einfo ("%P: %s\n", warning);
836 else if (symbol == NULL)
837 einfo ("%B: %s\n", abfd, warning);
840 lang_input_statement_type *entry;
842 struct warning_callback_info info;
844 /* Look through the relocs to see if we can find a plausible
847 entry = (lang_input_statement_type *) abfd->usrdata;
848 if (entry != NULL && entry->asymbols != NULL)
849 asymbols = entry->asymbols;
855 symsize = bfd_get_symtab_upper_bound (abfd);
857 einfo ("%B%F: could not read symbols: %E\n", abfd);
858 asymbols = (asymbol **) xmalloc (symsize);
859 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
860 if (symbol_count < 0)
861 einfo ("%B%F: could not read symbols: %E\n", abfd);
864 entry->asymbols = asymbols;
865 entry->symbol_count = symbol_count;
870 info.warning = warning;
871 info.symbol = symbol;
872 info.asymbols = asymbols;
873 bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info);
876 einfo ("%B: %s\n", abfd, warning);
882 /* This is called by warning_callback for each section. It checks the
883 relocs of the section to see if it can find a reference to the
884 symbol which triggered the warning. If it can, it uses the reloc
885 to give an error message with a file and line number. */
888 warning_find_reloc (abfd, sec, iarg)
893 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
902 relsize = bfd_get_reloc_upper_bound (abfd, sec);
904 einfo ("%B%F: could not read relocs: %E\n", abfd);
908 relpp = (arelent **) xmalloc (relsize);
909 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
911 einfo ("%B%F: could not read relocs: %E\n", abfd);
915 for (; p < pend && *p != NULL; p++)
919 if (q->sym_ptr_ptr != NULL
920 && *q->sym_ptr_ptr != NULL
921 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
923 /* We found a reloc for the symbol we are looking for. */
924 einfo ("%C: %s\n", abfd, sec, q->address, info->warning);
933 /* This is called when an undefined symbol is found. */
937 undefined_symbol (info, name, abfd, section, address)
938 struct bfd_link_info *info;
944 static char *error_name;
945 static unsigned int error_count;
947 #define MAX_ERRORS_IN_A_ROW 5
949 if (config.warn_once)
951 static struct bfd_hash_table *hash;
953 /* Only warn once about a particular undefined symbol. */
957 hash = ((struct bfd_hash_table *)
958 xmalloc (sizeof (struct bfd_hash_table)));
959 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
960 einfo ("%F%P: bfd_hash_table_init failed: %E\n");
963 if (bfd_hash_lookup (hash, name, false, false) != NULL)
966 if (bfd_hash_lookup (hash, name, true, true) == NULL)
967 einfo ("%F%P: bfd_hash_lookup failed: %E\n");
970 /* We never print more than a reasonable number of errors in a row
971 for a single symbol. */
972 if (error_name != (char *) NULL
973 && strcmp (name, error_name) == 0)
978 if (error_name != (char *) NULL)
980 error_name = buystring (name);
985 if (error_count < MAX_ERRORS_IN_A_ROW)
986 einfo ("%X%C: undefined reference to `%T'\n",
987 abfd, section, address, name);
988 else if (error_count == MAX_ERRORS_IN_A_ROW)
989 einfo ("%D: more undefined references to `%T' follow\n",
990 abfd, section, address, name);
994 if (error_count < MAX_ERRORS_IN_A_ROW)
995 einfo ("%X%B: undefined reference to `%T'\n",
997 else if (error_count == MAX_ERRORS_IN_A_ROW)
998 einfo ("%B: more undefined references to `%T' follow\n",
1005 /* This is called when a reloc overflows. */
1009 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
1010 struct bfd_link_info *info;
1012 const char *reloc_name;
1018 if (abfd == (bfd *) NULL)
1019 einfo ("%P%X: generated");
1021 einfo ("%X%C:", abfd, section, address);
1022 einfo (" relocation truncated to fit: %s %T", reloc_name, name);
1024 einfo ("+%v", addend);
1029 /* This is called when a dangerous relocation is made. */
1033 reloc_dangerous (info, message, abfd, section, address)
1034 struct bfd_link_info *info;
1035 const char *message;
1040 if (abfd == (bfd *) NULL)
1041 einfo ("%P%X: generated");
1043 einfo ("%X%C:", abfd, section, address);
1044 einfo ("dangerous relocation: %s\n", message);
1048 /* This is called when a reloc is being generated attached to a symbol
1049 that is not being output. */
1053 unattached_reloc (info, name, abfd, section, address)
1054 struct bfd_link_info *info;
1060 if (abfd == (bfd *) NULL)
1061 einfo ("%P%X: generated");
1063 einfo ("%X%C:", abfd, section, address);
1064 einfo (" reloc refers to symbol `%T' which is not being output\n", name);
1068 /* This is called when a symbol in notice_hash is found. Symbols are
1069 put in notice_hash using the -y option. */
1073 notice_ysym (info, name, abfd, section, value)
1074 struct bfd_link_info *info;
1080 einfo ("%B: %s %s\n", abfd,
1081 bfd_is_und_section (section) ? "reference to" : "definition of",