1 /* Main program of GNU linker.
2 Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #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 *,
105 static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
107 asection *, bfd_vma));
108 static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
109 const char *, bfd_vma,
110 bfd *, asection *, bfd_vma));
111 static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
112 bfd *, asection *, bfd_vma));
113 static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
114 const char *, bfd *, asection *,
116 static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
117 bfd *, asection *, bfd_vma));
119 static struct bfd_link_callbacks link_callbacks =
125 constructor_callback,
134 struct bfd_link_info link_info;
141 if (output_bfd && output_bfd->iostream)
142 fclose((FILE *)(output_bfd->iostream));
143 if (delete_output_file_on_failure)
144 unlink (output_filename);
154 long start_time = get_run_time ();
156 program_name = argv[0];
157 xmalloc_set_program_name (program_name);
159 START_PROGRESS (program_name, 0);
163 xatexit (remove_output);
165 /* Initialize the data about options. */
166 trace_files = trace_file_tries = version_printed = false;
167 whole_archive = false;
168 config.traditional_format = false;
169 config.build_constructors = true;
170 config.dynamic_link = false;
171 command_line.force_common_definition = false;
172 command_line.interpreter = NULL;
173 command_line.rpath = NULL;
175 link_info.callbacks = &link_callbacks;
176 link_info.relocateable = false;
177 link_info.shared = false;
178 link_info.strip = strip_none;
179 link_info.discard = discard_none;
180 link_info.lprefix_len = 1;
181 link_info.lprefix = "L";
182 link_info.keep_memory = true;
183 link_info.input_bfds = NULL;
184 link_info.create_object_symbols_section = NULL;
185 link_info.hash = NULL;
186 link_info.keep_hash = NULL;
187 link_info.notice_hash = NULL;
189 ldfile_add_arch ("");
191 config.make_executable = true;
192 force_make_executable = false;
193 config.magic_demand_paged = true;
194 config.text_read_only = true;
195 config.make_executable = true;
197 emulation = get_emulation (argc, argv);
198 ldemul_choose_mode (emulation);
199 default_target = ldemul_choose_target ();
201 ldemul_before_parse ();
202 lang_has_input_file = false;
203 parse_args (argc, argv);
205 if (link_info.relocateable)
207 if (command_line.relax)
208 einfo ("%P%F: -relax and -r may not be used together\n");
209 if (config.dynamic_link)
210 einfo ("%P%F: -r and -call_shared may not be used together\n");
211 if (link_info.shared)
212 einfo ("%P%F: -r and -shared may not be used together\n");
215 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
216 don't see how else this can be handled, since in this case we
217 must preserve all externally visible symbols. */
218 if (link_info.relocateable && link_info.strip == strip_all)
220 link_info.strip = strip_debugger;
221 if (link_info.discard == discard_none)
222 link_info.discard = discard_all;
225 /* This essentially adds another -L directory so this must be done after
226 the -L's in argv have been processed. */
229 if (had_script == false)
231 /* Read the emulation's appropriate default script. */
233 char *s = ldemul_get_script (&isfile);
236 ldfile_open_command_file (s);
239 if (trace_file_tries)
241 info_msg ("using internal linker script:\n");
242 info_msg ("==================================================\n");
244 info_msg ("\n==================================================\n");
248 parser_input = input_script;
254 if (lang_has_input_file == false)
258 einfo ("%P%F: no input files\n");
263 info_msg ("%P: mode %s\n", emulation);
266 ldemul_after_parse ();
269 if (config.map_filename)
271 if (strcmp (config.map_filename, "-") == 0)
273 config.map_file = stdout;
277 config.map_file = fopen (config.map_filename, FOPEN_WT);
278 if (config.map_file == (FILE *) NULL)
280 einfo ("%P%F: cannot open map file %s: %E\n",
281 config.map_filename);
289 /* Print error messages for any missing symbols, for any warning
290 symbols, and possibly multiple definitions */
293 if (config.text_read_only)
295 /* Look for a text section and mark the readonly attribute in it */
296 asection *found = bfd_get_section_by_name (output_bfd, ".text");
298 if (found != (asection *) NULL)
300 found->flags |= SEC_READONLY;
304 if (link_info.relocateable)
305 output_bfd->flags &= ~EXEC_P;
307 output_bfd->flags |= EXEC_P;
311 /* Even if we're producing relocateable output, some non-fatal errors should
312 be reported in the exit status. (What non-fatal errors, if any, do we
313 want to ignore for relocateable output?) */
315 if (config.make_executable == false && force_make_executable == false)
317 if (trace_files == true)
319 einfo ("%P: link errors found, deleting executable `%s'\n",
323 if (output_bfd->iostream)
324 fclose ((FILE *) (output_bfd->iostream));
326 unlink (output_filename);
331 if (! bfd_close (output_bfd))
332 einfo ("%F%B: final close failed: %E\n", output_bfd);
335 END_PROGRESS (program_name);
339 extern char **environ;
340 char *lim = (char *) sbrk (0);
341 long run_time = get_run_time () - start_time;
343 fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
344 program_name, run_time / 1000000, run_time % 1000000);
345 fprintf (stderr, "%s: data size %ld\n", program_name,
346 (long) (lim - (char *) &environ));
349 /* Prevent remove_output from doing anything, after a successful link. */
350 output_filename = NULL;
356 /* We need to find any explicitly given emulation in order to initialize the
357 state that's needed by the lex&yacc argument parser (parse_args). */
360 get_emulation (argc, argv)
367 emulation = (char *) getenv (EMULATION_ENVIRON);
368 if (emulation == NULL)
369 emulation = DEFAULT_EMULATION;
371 for (i = 1; i < argc; i++)
373 if (!strncmp (argv[i], "-m", 2))
375 if (argv[i][2] == '\0')
380 emulation = argv[i + 1];
385 einfo("%P%F: missing argument to -m\n");
388 else if (strcmp (argv[i], "-mips1") == 0
389 || strcmp (argv[i], "-mips2") == 0
390 || strcmp (argv[i], "-mips3") == 0)
392 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
393 passed to the linker by some MIPS compilers. They
394 generally tell the linker to use a slightly different
395 library path. Perhaps someday these should be
396 implemented as emulations; until then, we just ignore
397 the arguments and hope that nobody ever creates
398 emulations named ips1, ips2 or ips3. */
400 else if (strcmp (argv[i], "-m486") == 0)
402 /* FIXME: The argument -m486 is passed to the linker on
403 some Linux systems. Hope that nobody creates an
404 emulation named 486. */
409 emulation = &argv[i][2];
417 /* If directory DIR contains an "ldscripts" subdirectory,
418 add DIR to the library search path and return true,
419 else return false. */
422 check_for_scripts_dir (dir)
430 dirlen = strlen (dir);
431 /* sizeof counts the terminating NUL. */
432 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
433 sprintf (buf, "%s/ldscripts", dir);
435 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
438 ldfile_add_library_path (dir, false);
442 /* Set the default directory for finding script files.
443 Libraries will be searched for here too, but that's ok.
444 We look for the "ldscripts" directory in:
446 SCRIPTDIR (passed from Makefile)
447 the dir where this program is (for using it from the build tree)
448 the dir where this program is/../lib (for installing the tool suite elsewhere) */
456 if (check_for_scripts_dir (SCRIPTDIR))
457 return; /* We've been installed normally. */
459 /* Look for "ldscripts" in the dir where our binary is. */
460 end = strrchr (program_name, '/');
463 dirlen = end - program_name;
464 /* Make a copy of program_name in dir.
465 Leave room for later "/../lib". */
466 dir = (char *) xmalloc (dirlen + 8);
467 strncpy (dir, program_name, dirlen);
473 dir = (char *) xmalloc (dirlen + 8);
477 if (check_for_scripts_dir (dir))
478 return; /* Don't free dir. */
480 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
481 strcpy (dir + dirlen, "/../lib");
482 if (check_for_scripts_dir (dir))
485 free (dir); /* Well, we tried. */
492 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
494 link_info.notice_hash = ((struct bfd_hash_table *)
495 xmalloc (sizeof (struct bfd_hash_table)));
496 if (! bfd_hash_table_init_n (link_info.notice_hash,
499 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
502 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
503 == (struct bfd_hash_entry *) NULL)
504 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
507 /* Handle the -retain-symbols-file option. */
510 add_keepsyms_file (filename)
511 const char *filename;
518 if (link_info.strip == strip_some)
519 einfo ("%X%P: error: duplicate retain-symbols-file\n");
521 file = fopen (filename, "r");
522 if (file == (FILE *) NULL)
524 bfd_set_error (bfd_error_system_call);
525 einfo ("%X%P: %s: %E", filename);
529 link_info.keep_hash = ((struct bfd_hash_table *)
530 xmalloc (sizeof (struct bfd_hash_table)));
531 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
532 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
535 buf = (char *) xmalloc (bufsize);
547 while (! isspace (c) && c != EOF)
554 buf = xrealloc (buf, bufsize);
561 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
562 == (struct bfd_hash_entry *) NULL)
563 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
567 if (link_info.strip != strip_none)
568 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
570 link_info.strip = strip_some;
573 /* Callbacks from the BFD linker routines. */
575 /* This is called when BFD has decided to include an archive member in
580 add_archive_element (info, abfd, name)
581 struct bfd_link_info *info;
585 lang_input_statement_type *input;
587 input = ((lang_input_statement_type *)
588 xmalloc ((bfd_size_type) sizeof (lang_input_statement_type)));
589 input->filename = abfd->filename;
590 input->local_sym_name = abfd->filename;
591 input->the_bfd = abfd;
592 input->asymbols = NULL;
594 input->just_syms_flag = false;
595 input->loaded = false;
596 input->search_dirs_flag = false;
598 /* FIXME: The following fields are not set: header.next,
599 header.type, closed, passive_position, symbol_count,
600 next_real_file, is_archive, target, real, common_section,
601 common_output_section, complained. This bit of code is from the
602 old decode_library_subfile function. I don't know whether any of
603 those fields matters. */
605 ldlang_add_file (input);
607 if (config.map_file != (FILE *) NULL)
608 minfo ("%s needed due to %T\n", abfd->filename, name);
610 if (trace_files || trace_file_tries)
611 info_msg ("%I\n", input);
616 /* This is called when BFD has discovered a symbol which is defined
621 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
622 struct bfd_link_info *info;
631 einfo ("%X%C: multiple definition of `%T'\n",
632 nbfd, nsec, nval, name);
633 if (obfd != (bfd *) NULL)
634 einfo ("%D: first defined here\n", obfd, osec, oval);
638 /* This is called when there is a definition of a common symbol, or
639 when a common symbol is found for a symbol that is already defined,
640 or when two common symbols are found. We only do something if
641 -warn-common was used. */
645 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
646 struct bfd_link_info *info;
649 enum bfd_link_hash_type otype;
652 enum bfd_link_hash_type ntype;
655 if (! config.warn_common)
658 if (ntype == bfd_link_hash_defined
659 || ntype == bfd_link_hash_defweak
660 || ntype == bfd_link_hash_indirect)
662 ASSERT (otype == bfd_link_hash_common);
663 einfo ("%B: warning: definition of `%T' overriding common\n",
666 einfo ("%B: warning: common is here\n", obfd);
668 else if (otype == bfd_link_hash_defined
669 || otype == bfd_link_hash_defweak
670 || otype == bfd_link_hash_indirect)
672 ASSERT (ntype == bfd_link_hash_common);
673 einfo ("%B: warning: common of `%T' overridden by definition\n",
676 einfo ("%B: warning: defined here\n", obfd);
680 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
683 einfo ("%B: warning: common of `%T' overridden by larger common\n",
686 einfo ("%B: warning: larger common is here\n", obfd);
688 else if (nsize > osize)
690 einfo ("%B: warning: common of `%T' overriding smaller common\n",
693 einfo ("%B: warning: smaller common is here\n", obfd);
697 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
699 einfo ("%B: warning: previous common is here\n", obfd);
706 /* This is called when BFD has discovered a set element. H is the
707 entry in the linker hash table for the set. SECTION and VALUE
708 represent a value which should be added to the set. */
712 add_to_set (info, h, reloc, abfd, section, value)
713 struct bfd_link_info *info;
714 struct bfd_link_hash_entry *h;
715 bfd_reloc_code_real_type reloc;
720 if (! config.build_constructors)
723 ldctor_add_set_entry (h, reloc, section, value);
725 if (h->type == bfd_link_hash_new)
727 h->type = bfd_link_hash_undefined;
728 h->u.undef.abfd = abfd;
729 /* We don't call bfd_link_add_undef to add this to the list of
730 undefined symbols because we are going to define it
737 /* This is called when BFD has discovered a constructor. This is only
738 called for some object file formats--those which do not handle
739 constructors in some more clever fashion. This is similar to
740 adding an element to a set, but less general. */
743 constructor_callback (info, constructor, name, abfd, section, value)
744 struct bfd_link_info *info;
753 struct bfd_link_hash_entry *h;
755 if (! config.build_constructors)
758 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
759 useful error message. */
760 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL)
761 einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported");
763 set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__");
765 if (bfd_get_symbol_leading_char (abfd) != '\0')
766 *s++ = bfd_get_symbol_leading_char (abfd);
768 strcpy (s, "__CTOR_LIST__");
770 strcpy (s, "__DTOR_LIST__");
772 if (config.map_file != (FILE *) NULL)
773 fprintf (config.map_file,
774 "Adding %s to constructor/destructor set %s\n", name, set_name);
776 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
777 if (h == (struct bfd_link_hash_entry *) NULL)
778 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
779 if (h->type == bfd_link_hash_new)
781 h->type = bfd_link_hash_undefined;
782 h->u.undef.abfd = abfd;
783 /* We don't call bfd_link_add_undef to add this to the list of
784 undefined symbols because we are going to define it
788 ldctor_add_set_entry (h, BFD_RELOC_CTOR, section, value);
792 /* This is called when there is a reference to a warning symbol. */
796 warning_callback (info, warning)
797 struct bfd_link_info *info;
800 einfo ("%P: %s\n", warning);
804 /* This is called when an undefined symbol is found. */
808 undefined_symbol (info, name, abfd, section, address)
809 struct bfd_link_info *info;
815 static char *error_name;
816 static unsigned int error_count;
818 #define MAX_ERRORS_IN_A_ROW 5
820 if (config.warn_once)
822 static struct bfd_hash_table *hash;
824 /* Only warn once about a particular undefined symbol. */
828 hash = ((struct bfd_hash_table *)
829 xmalloc (sizeof (struct bfd_hash_table)));
830 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
831 einfo ("%F%P: bfd_hash_table_init failed: %E\n");
834 if (bfd_hash_lookup (hash, name, false, false) != NULL)
837 if (bfd_hash_lookup (hash, name, true, true) == NULL)
838 einfo ("%F%P: bfd_hash_lookup failed: %E\n");
841 /* We never print more than a reasonable number of errors in a row
842 for a single symbol. */
843 if (error_name != (char *) NULL
844 && strcmp (name, error_name) == 0)
849 if (error_name != (char *) NULL)
851 error_name = buystring (name);
856 if (error_count < MAX_ERRORS_IN_A_ROW)
857 einfo ("%X%C: undefined reference to `%T'\n",
858 abfd, section, address, name);
859 else if (error_count == MAX_ERRORS_IN_A_ROW)
860 einfo ("%D: more undefined references to `%T' follow\n",
861 abfd, section, address, name);
865 if (error_count < MAX_ERRORS_IN_A_ROW)
866 einfo ("%X%B: undefined reference to `%T'\n",
868 else if (error_count == MAX_ERRORS_IN_A_ROW)
869 einfo ("%B: more undefined references to `%T' follow\n",
876 /* This is called when a reloc overflows. */
880 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
881 struct bfd_link_info *info;
883 const char *reloc_name;
889 if (abfd == (bfd *) NULL)
890 einfo ("%P%X: generated");
892 einfo ("%X%C:", abfd, section, address);
893 einfo (" relocation truncated to fit: %s %T", reloc_name, name);
895 einfo ("+%v", addend);
900 /* This is called when a dangerous relocation is made. */
904 reloc_dangerous (info, message, abfd, section, address)
905 struct bfd_link_info *info;
911 if (abfd == (bfd *) NULL)
912 einfo ("%P%X: generated");
914 einfo ("%X%C:", abfd, section, address);
915 einfo ("dangerous relocation: %s\n", message);
919 /* This is called when a reloc is being generated attached to a symbol
920 that is not being output. */
924 unattached_reloc (info, name, abfd, section, address)
925 struct bfd_link_info *info;
931 if (abfd == (bfd *) NULL)
932 einfo ("%P%X: generated");
934 einfo ("%X%C:", abfd, section, address);
935 einfo (" reloc refers to symbol `%T' which is not being output\n", name);
939 /* This is called when a symbol in notice_hash is found. Symbols are
940 put in notice_hash using the -y option. */
944 notice_ysym (info, name, abfd, section, value)
945 struct bfd_link_info *info;
951 einfo ("%B: %s %s\n", abfd,
952 bfd_is_und_section (section) ? "reference to" : "definition of",