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"
41 /* Somewhere above, sys/stat.h got included . . . . */
42 #if !defined(S_ISDIR) && defined(S_IFDIR)
43 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
48 static char *get_emulation PARAMS ((int, char **));
49 static void set_scripts_dir PARAMS ((void));
54 const char *output_filename = "a.out";
56 /* Name this program was invoked by. */
59 /* The file that we're creating */
62 /* Set by -G argument, for MIPS ECOFF target. */
63 int g_switch_value = 8;
65 /* Nonzero means print names of input files as processed. */
68 /* Nonzero means same, but note open failures, too. */
69 boolean trace_file_tries;
71 /* Nonzero means version number was printed, so exit successfully
72 instead of complaining if no input files are given. */
73 boolean version_printed;
75 /* Nonzero means link in every member of an archive. */
76 boolean whole_archive;
78 args_type command_line;
80 ld_config_type config;
82 static boolean check_for_scripts_dir PARAMS ((char *dir));
83 static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
85 static boolean multiple_definition PARAMS ((struct bfd_link_info *,
87 bfd *, asection *, bfd_vma,
88 bfd *, asection *, bfd_vma));
89 static boolean multiple_common PARAMS ((struct bfd_link_info *,
91 enum bfd_link_hash_type, bfd_vma,
92 bfd *, enum bfd_link_hash_type,
94 static boolean add_to_set PARAMS ((struct bfd_link_info *,
95 struct bfd_link_hash_entry *,
96 bfd_reloc_code_real_type,
97 bfd *, asection *, bfd_vma));
98 static boolean constructor_callback PARAMS ((struct bfd_link_info *,
101 bfd *, asection *, bfd_vma));
102 static boolean warning_callback PARAMS ((struct bfd_link_info *,
104 static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
106 asection *, bfd_vma));
107 static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
108 const char *, bfd_vma,
109 bfd *, asection *, bfd_vma));
110 static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
111 bfd *, asection *, bfd_vma));
112 static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
113 const char *, bfd *, asection *,
115 static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
116 bfd *, asection *, bfd_vma));
118 static struct bfd_link_callbacks link_callbacks =
124 constructor_callback,
133 struct bfd_link_info link_info;
140 if (output_bfd && output_bfd->iostream)
141 fclose((FILE *)(output_bfd->iostream));
142 if (delete_output_file_on_failure)
143 unlink (output_filename);
153 long start_time = get_run_time ();
155 program_name = argv[0];
156 xmalloc_set_program_name (program_name);
160 xatexit (remove_output);
162 /* Initialize the data about options. */
163 trace_files = trace_file_tries = version_printed = false;
164 whole_archive = false;
165 config.traditional_format = false;
166 config.build_constructors = true;
167 config.dynamic_link = false;
168 command_line.force_common_definition = false;
169 command_line.interpreter = NULL;
170 command_line.rpath = NULL;
172 link_info.callbacks = &link_callbacks;
173 link_info.relocateable = false;
174 link_info.shared = false;
175 link_info.strip = strip_none;
176 link_info.discard = discard_none;
177 link_info.lprefix_len = 1;
178 link_info.lprefix = "L";
179 link_info.keep_memory = true;
180 link_info.input_bfds = NULL;
181 link_info.create_object_symbols_section = NULL;
182 link_info.hash = NULL;
183 link_info.keep_hash = NULL;
184 link_info.notice_hash = NULL;
186 ldfile_add_arch ("");
188 config.make_executable = true;
189 force_make_executable = false;
190 config.magic_demand_paged = true;
191 config.text_read_only = true;
192 config.make_executable = true;
194 emulation = get_emulation (argc, argv);
195 ldemul_choose_mode (emulation);
196 default_target = ldemul_choose_target ();
198 ldemul_before_parse ();
199 lang_has_input_file = false;
200 parse_args (argc, argv);
202 if (link_info.relocateable)
204 if (command_line.relax)
205 einfo ("%P%F: -relax and -r may not be used together\n");
206 if (config.dynamic_link)
207 einfo ("%P%F: -r and -call_shared may not be used together\n");
208 if (link_info.shared)
209 einfo ("%P%F: -r and -shared may not be used together\n");
212 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
213 don't see how else this can be handled, since in this case we
214 must preserve all externally visible symbols. */
215 if (link_info.relocateable && link_info.strip == strip_all)
217 link_info.strip = strip_debugger;
218 if (link_info.discard == discard_none)
219 link_info.discard = discard_all;
222 /* This essentially adds another -L directory so this must be done after
223 the -L's in argv have been processed. */
226 if (had_script == false)
228 /* Read the emulation's appropriate default script. */
230 char *s = ldemul_get_script (&isfile);
233 ldfile_open_command_file (s);
236 if (trace_file_tries)
238 info_msg ("using internal linker script:\n");
239 info_msg ("==================================================\n");
241 info_msg ("\n==================================================\n");
245 parser_input = input_script;
251 if (lang_has_input_file == false)
255 einfo ("%P%F: no input files\n");
260 info_msg ("%P: mode %s\n", emulation);
263 ldemul_after_parse ();
266 if (config.map_filename)
268 if (strcmp (config.map_filename, "-") == 0)
270 config.map_file = stdout;
274 config.map_file = fopen (config.map_filename, FOPEN_WT);
275 if (config.map_file == (FILE *) NULL)
277 einfo ("%P%F: cannot open map file %s: %E\n",
278 config.map_filename);
286 /* Print error messages for any missing symbols, for any warning
287 symbols, and possibly multiple definitions */
290 if (config.text_read_only)
292 /* Look for a text section and mark the readonly attribute in it */
293 asection *found = bfd_get_section_by_name (output_bfd, ".text");
295 if (found != (asection *) NULL)
297 found->flags |= SEC_READONLY;
301 if (link_info.relocateable || link_info.shared)
302 output_bfd->flags &= ~EXEC_P;
304 output_bfd->flags |= EXEC_P;
308 /* Even if we're producing relocateable output, some non-fatal errors should
309 be reported in the exit status. (What non-fatal errors, if any, do we
310 want to ignore for relocateable output?) */
312 if (config.make_executable == false && force_make_executable == false)
314 if (trace_files == true)
316 einfo ("%P: link errors found, deleting executable `%s'\n",
320 if (output_bfd->iostream)
321 fclose ((FILE *) (output_bfd->iostream));
323 unlink (output_filename);
328 if (! bfd_close (output_bfd))
329 einfo ("%F%B: final close failed: %E\n", output_bfd);
334 extern char **environ;
335 char *lim = (char *) sbrk (0);
336 long run_time = get_run_time () - start_time;
338 fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
339 program_name, run_time / 1000000, run_time % 1000000);
340 fprintf (stderr, "%s: data size %ld\n", program_name,
341 (long) (lim - (char *) &environ));
344 /* Prevent remove_output from doing anything, after a successful link. */
345 output_filename = NULL;
351 /* We need to find any explicitly given emulation in order to initialize the
352 state that's needed by the lex&yacc argument parser (parse_args). */
355 get_emulation (argc, argv)
362 emulation = (char *) getenv (EMULATION_ENVIRON);
363 if (emulation == NULL)
364 emulation = DEFAULT_EMULATION;
366 for (i = 1; i < argc; i++)
368 if (!strncmp (argv[i], "-m", 2))
370 if (argv[i][2] == '\0')
375 emulation = argv[i + 1];
380 einfo("%P%F: missing argument to -m\n");
383 else if (strcmp (argv[i], "-mips1") == 0
384 || strcmp (argv[i], "-mips2") == 0
385 || strcmp (argv[i], "-mips3") == 0)
387 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
388 passed to the linker by some MIPS compilers. They
389 generally tell the linker to use a slightly different
390 library path. Perhaps someday these should be
391 implemented as emulations; until then, we just ignore
392 the arguments and hope that nobody ever creates
393 emulations named ips1, ips2 or ips3. */
395 else if (strcmp (argv[i], "-m486") == 0)
397 /* FIXME: The argument -m486 is passed to the linker on
398 some Linux systems. Hope that nobody creates an
399 emulation named 486. */
404 emulation = &argv[i][2];
412 /* If directory DIR contains an "ldscripts" subdirectory,
413 add DIR to the library search path and return true,
414 else return false. */
417 check_for_scripts_dir (dir)
425 dirlen = strlen (dir);
426 /* sizeof counts the terminating NUL. */
427 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
428 sprintf (buf, "%s/ldscripts", dir);
430 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
433 ldfile_add_library_path (dir, false);
437 /* Set the default directory for finding script files.
438 Libraries will be searched for here too, but that's ok.
439 We look for the "ldscripts" directory in:
441 SCRIPTDIR (passed from Makefile)
442 the dir where this program is (for using it from the build tree)
443 the dir where this program is/../lib (for installing the tool suite elsewhere) */
451 if (check_for_scripts_dir (SCRIPTDIR))
452 return; /* We've been installed normally. */
454 /* Look for "ldscripts" in the dir where our binary is. */
455 end = strrchr (program_name, '/');
458 dirlen = end - program_name;
459 /* Make a copy of program_name in dir.
460 Leave room for later "/../lib". */
461 dir = (char *) xmalloc (dirlen + 8);
462 strncpy (dir, program_name, dirlen);
468 dir = (char *) xmalloc (dirlen + 8);
472 if (check_for_scripts_dir (dir))
473 return; /* Don't free dir. */
475 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
476 strcpy (dir + dirlen, "/../lib");
477 if (check_for_scripts_dir (dir))
480 free (dir); /* Well, we tried. */
487 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
489 link_info.notice_hash = ((struct bfd_hash_table *)
490 xmalloc (sizeof (struct bfd_hash_table)));
491 if (! bfd_hash_table_init_n (link_info.notice_hash,
494 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
497 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
498 == (struct bfd_hash_entry *) NULL)
499 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
502 /* Handle the -retain-symbols-file option. */
505 add_keepsyms_file (filename)
506 const char *filename;
513 if (link_info.strip == strip_some)
514 einfo ("%X%P: error: duplicate retain-symbols-file\n");
516 file = fopen (filename, "r");
517 if (file == (FILE *) NULL)
519 bfd_set_error (bfd_error_system_call);
520 einfo ("%X%P: %s: %E", filename);
524 link_info.keep_hash = ((struct bfd_hash_table *)
525 xmalloc (sizeof (struct bfd_hash_table)));
526 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
527 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
530 buf = (char *) xmalloc (bufsize);
542 while (! isspace (c) && c != EOF)
549 buf = xrealloc (buf, bufsize);
556 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
557 == (struct bfd_hash_entry *) NULL)
558 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
562 if (link_info.strip != strip_none)
563 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
565 link_info.strip = strip_some;
568 /* Callbacks from the BFD linker routines. */
570 /* This is called when BFD has decided to include an archive member in
575 add_archive_element (info, abfd, name)
576 struct bfd_link_info *info;
580 lang_input_statement_type *input;
582 input = ((lang_input_statement_type *)
583 xmalloc ((bfd_size_type) sizeof (lang_input_statement_type)));
584 input->filename = abfd->filename;
585 input->local_sym_name = abfd->filename;
586 input->the_bfd = abfd;
587 input->asymbols = NULL;
589 input->just_syms_flag = false;
590 input->loaded = false;
591 input->search_dirs_flag = false;
593 /* FIXME: The following fields are not set: header.next,
594 header.type, closed, passive_position, symbol_count,
595 next_real_file, is_archive, target, real, common_section,
596 common_output_section, complained. This bit of code is from the
597 old decode_library_subfile function. I don't know whether any of
598 those fields matters. */
600 ldlang_add_file (input);
602 if (config.map_file != (FILE *) NULL)
603 minfo ("%s needed due to %T\n", abfd->filename, name);
605 if (trace_files || trace_file_tries)
606 info_msg ("%I\n", input);
611 /* This is called when BFD has discovered a symbol which is defined
616 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
617 struct bfd_link_info *info;
626 einfo ("%X%C: multiple definition of `%T'\n",
627 nbfd, nsec, nval, name);
628 if (obfd != (bfd *) NULL)
629 einfo ("%D: first defined here\n", obfd, osec, oval);
633 /* This is called when there is a definition of a common symbol, or
634 when a common symbol is found for a symbol that is already defined,
635 or when two common symbols are found. We only do something if
636 -warn-common was used. */
640 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
641 struct bfd_link_info *info;
644 enum bfd_link_hash_type otype;
647 enum bfd_link_hash_type ntype;
650 if (! config.warn_common)
653 if (ntype == bfd_link_hash_defined
654 || ntype == bfd_link_hash_defweak
655 || ntype == bfd_link_hash_indirect)
657 ASSERT (otype == bfd_link_hash_common);
658 einfo ("%B: warning: definition of `%T' overriding common\n",
661 einfo ("%B: warning: common is here\n", obfd);
663 else if (otype == bfd_link_hash_defined
664 || otype == bfd_link_hash_defweak
665 || otype == bfd_link_hash_indirect)
667 ASSERT (ntype == bfd_link_hash_common);
668 einfo ("%B: warning: common of `%T' overridden by definition\n",
671 einfo ("%B: warning: defined here\n", obfd);
675 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
678 einfo ("%B: warning: common of `%T' overridden by larger common\n",
681 einfo ("%B: warning: larger common is here\n", obfd);
683 else if (nsize > osize)
685 einfo ("%B: warning: common of `%T' overriding smaller common\n",
688 einfo ("%B: warning: smaller common is here\n", obfd);
692 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
694 einfo ("%B: warning: previous common is here\n", obfd);
701 /* This is called when BFD has discovered a set element. H is the
702 entry in the linker hash table for the set. SECTION and VALUE
703 represent a value which should be added to the set. */
707 add_to_set (info, h, reloc, abfd, section, value)
708 struct bfd_link_info *info;
709 struct bfd_link_hash_entry *h;
710 bfd_reloc_code_real_type reloc;
715 if (! config.build_constructors)
718 ldctor_add_set_entry (h, reloc, section, value);
720 if (h->type == bfd_link_hash_new)
722 h->type = bfd_link_hash_undefined;
723 h->u.undef.abfd = abfd;
724 /* We don't call bfd_link_add_undef to add this to the list of
725 undefined symbols because we are going to define it
732 /* This is called when BFD has discovered a constructor. This is only
733 called for some object file formats--those which do not handle
734 constructors in some more clever fashion. This is similar to
735 adding an element to a set, but less general. */
738 constructor_callback (info, constructor, name, abfd, section, value)
739 struct bfd_link_info *info;
748 struct bfd_link_hash_entry *h;
750 if (! config.build_constructors)
753 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
754 useful error message. */
755 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL)
756 einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported");
758 set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__");
760 if (bfd_get_symbol_leading_char (abfd) != '\0')
761 *s++ = bfd_get_symbol_leading_char (abfd);
763 strcpy (s, "__CTOR_LIST__");
765 strcpy (s, "__DTOR_LIST__");
767 if (config.map_file != (FILE *) NULL)
768 fprintf (config.map_file,
769 "Adding %s to constructor/destructor set %s\n", name, set_name);
771 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
772 if (h == (struct bfd_link_hash_entry *) NULL)
773 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
774 if (h->type == bfd_link_hash_new)
776 h->type = bfd_link_hash_undefined;
777 h->u.undef.abfd = abfd;
778 /* We don't call bfd_link_add_undef to add this to the list of
779 undefined symbols because we are going to define it
783 ldctor_add_set_entry (h, BFD_RELOC_CTOR, section, value);
787 /* This is called when there is a reference to a warning symbol. */
791 warning_callback (info, warning)
792 struct bfd_link_info *info;
795 einfo ("%P: %s\n", warning);
799 /* This is called when an undefined symbol is found. */
803 undefined_symbol (info, name, abfd, section, address)
804 struct bfd_link_info *info;
810 static char *error_name;
811 static unsigned int error_count;
813 #define MAX_ERRORS_IN_A_ROW 5
815 if (config.warn_once)
817 static struct bfd_hash_table *hash;
819 /* Only warn once about a particular undefined symbol. */
823 hash = ((struct bfd_hash_table *)
824 xmalloc (sizeof (struct bfd_hash_table)));
825 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
826 einfo ("%F%P: bfd_hash_table_init failed: %E\n");
829 if (bfd_hash_lookup (hash, name, false, false) != NULL)
832 if (bfd_hash_lookup (hash, name, true, true) == NULL)
833 einfo ("%F%P: bfd_hash_lookup failed: %E\n");
836 /* We never print more than a reasonable number of errors in a row
837 for a single symbol. */
838 if (error_name != (char *) NULL
839 && strcmp (name, error_name) == 0)
844 if (error_name != (char *) NULL)
846 error_name = buystring (name);
851 if (error_count < MAX_ERRORS_IN_A_ROW)
852 einfo ("%X%C: undefined reference to `%T'\n",
853 abfd, section, address, name);
854 else if (error_count == MAX_ERRORS_IN_A_ROW)
855 einfo ("%D: more undefined references to `%T' follow\n",
856 abfd, section, address, name);
860 if (error_count < MAX_ERRORS_IN_A_ROW)
861 einfo ("%X%B: undefined reference to `%T'\n",
863 else if (error_count == MAX_ERRORS_IN_A_ROW)
864 einfo ("%B: more undefined references to `%T' follow\n",
871 /* This is called when a reloc overflows. */
875 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
876 struct bfd_link_info *info;
878 const char *reloc_name;
884 if (abfd == (bfd *) NULL)
885 einfo ("%P%X: generated");
887 einfo ("%X%C:", abfd, section, address);
888 einfo (" relocation truncated to fit: %s %T", reloc_name, name);
890 einfo ("+%v", addend);
895 /* This is called when a dangerous relocation is made. */
899 reloc_dangerous (info, message, abfd, section, address)
900 struct bfd_link_info *info;
906 if (abfd == (bfd *) NULL)
907 einfo ("%P%X: generated");
909 einfo ("%X%C:", abfd, section, address);
910 einfo ("dangerous relocation: %s\n", message);
914 /* This is called when a reloc is being generated attached to a symbol
915 that is not being output. */
919 unattached_reloc (info, name, abfd, section, address)
920 struct bfd_link_info *info;
926 if (abfd == (bfd *) NULL)
927 einfo ("%P%X: generated");
929 einfo ("%X%C:", abfd, section, address);
930 einfo (" reloc refers to symbol `%T' which is not being output\n", name);
934 /* This is called when a symbol in notice_hash is found. Symbols are
935 put in notice_hash using the -y option. */
939 notice_ysym (info, name, abfd, section, value)
940 struct bfd_link_info *info;
946 einfo ("%B: %s %s\n", abfd,
947 bfd_is_und_section (section) ? "reference to" : "definition of",