1 /* Main program of GNU linker.
2 Copyright (C) 1991, 92, 93, 94 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. */
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 /* Use sbrk() except on specific OS types */
50 #if !defined(__amigados__) && !defined(WINDOWS_NT)
54 static char *get_emulation PARAMS ((int, char **));
55 static void set_scripts_dir PARAMS ((void));
60 const char *output_filename = "a.out";
62 /* Name this program was invoked by. */
65 /* The file that we're creating */
68 /* Set by -G argument, for MIPS ECOFF target. */
69 int g_switch_value = 8;
71 /* Nonzero means print names of input files as processed. */
74 /* Nonzero means same, but note open failures, too. */
75 boolean trace_file_tries;
77 /* Nonzero means version number was printed, so exit successfully
78 instead of complaining if no input files are given. */
79 boolean version_printed;
81 /* Nonzero means link in every member of an archive. */
82 boolean whole_archive;
84 args_type command_line;
86 ld_config_type config;
88 static boolean check_for_scripts_dir PARAMS ((char *dir));
89 static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
91 static boolean multiple_definition PARAMS ((struct bfd_link_info *,
93 bfd *, asection *, bfd_vma,
94 bfd *, asection *, bfd_vma));
95 static boolean multiple_common PARAMS ((struct bfd_link_info *,
97 enum bfd_link_hash_type, bfd_vma,
98 bfd *, enum bfd_link_hash_type,
100 static boolean add_to_set PARAMS ((struct bfd_link_info *,
101 struct bfd_link_hash_entry *,
102 bfd_reloc_code_real_type,
103 bfd *, asection *, bfd_vma));
104 static boolean constructor_callback PARAMS ((struct bfd_link_info *,
107 bfd *, asection *, bfd_vma));
108 static boolean warning_callback PARAMS ((struct bfd_link_info *,
110 static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
112 asection *, bfd_vma));
113 static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
114 const char *, bfd_vma,
115 bfd *, asection *, bfd_vma));
116 static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
117 bfd *, asection *, bfd_vma));
118 static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
119 const char *, bfd *, asection *,
121 static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
122 bfd *, asection *, bfd_vma));
124 static struct bfd_link_callbacks link_callbacks =
130 constructor_callback,
139 struct bfd_link_info link_info;
146 if (output_bfd && output_bfd->iostream)
147 fclose((FILE *)(output_bfd->iostream));
148 if (delete_output_file_on_failure)
149 unlink (output_filename);
159 long start_time = get_run_time ();
161 program_name = argv[0];
162 xmalloc_set_program_name (program_name);
164 START_PROGRESS (program_name, 0);
168 xatexit (remove_output);
170 /* Initialize the data about options. */
171 trace_files = trace_file_tries = version_printed = false;
172 whole_archive = false;
173 config.traditional_format = false;
174 config.build_constructors = true;
175 config.dynamic_link = false;
176 command_line.force_common_definition = false;
177 command_line.interpreter = NULL;
178 command_line.rpath = NULL;
180 link_info.callbacks = &link_callbacks;
181 link_info.relocateable = false;
182 link_info.shared = false;
183 link_info.symbolic = false;
184 link_info.strip = strip_none;
185 link_info.discard = discard_none;
186 link_info.lprefix_len = 1;
187 link_info.lprefix = "L";
188 link_info.keep_memory = true;
189 link_info.input_bfds = NULL;
190 link_info.create_object_symbols_section = NULL;
191 link_info.hash = NULL;
192 link_info.keep_hash = NULL;
193 link_info.notice_hash = NULL;
194 link_info.subsystem = console;
195 link_info.stack_heap_parameters.stack_defined = false;
196 link_info.stack_heap_parameters.heap_defined = false;
197 link_info.stack_heap_parameters.stack_reserve = 0;
198 link_info.stack_heap_parameters.stack_commit = 0;
199 link_info.stack_heap_parameters.heap_reserve = 0;
200 link_info.stack_heap_parameters.heap_commit = 0;
203 ldfile_add_arch ("");
205 config.make_executable = true;
206 force_make_executable = false;
207 config.magic_demand_paged = true;
208 config.text_read_only = true;
209 config.make_executable = true;
211 emulation = get_emulation (argc, argv);
212 ldemul_choose_mode (emulation);
213 default_target = ldemul_choose_target ();
215 ldemul_before_parse ();
216 lang_has_input_file = false;
217 parse_args (argc, argv);
219 if (link_info.relocateable)
221 if (command_line.relax)
222 einfo ("%P%F: -relax and -r may not be used together\n");
223 if (config.dynamic_link)
224 einfo ("%P%F: -r and -call_shared may not be used together\n");
225 if (link_info.shared)
226 einfo ("%P%F: -r and -shared may not be used together\n");
229 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
230 don't see how else this can be handled, since in this case we
231 must preserve all externally visible symbols. */
232 if (link_info.relocateable && link_info.strip == strip_all)
234 link_info.strip = strip_debugger;
235 if (link_info.discard == discard_none)
236 link_info.discard = discard_all;
239 /* This essentially adds another -L directory so this must be done after
240 the -L's in argv have been processed. */
243 if (had_script == false)
245 /* Read the emulation's appropriate default script. */
247 char *s = ldemul_get_script (&isfile);
250 ldfile_open_command_file (s);
253 if (trace_file_tries)
255 info_msg ("using internal linker script:\n");
256 info_msg ("==================================================\n");
258 info_msg ("\n==================================================\n");
262 parser_input = input_script;
268 if (lang_has_input_file == false)
272 einfo ("%P%F: no input files\n");
277 info_msg ("%P: mode %s\n", emulation);
280 ldemul_after_parse ();
283 if (config.map_filename)
285 if (strcmp (config.map_filename, "-") == 0)
287 config.map_file = stdout;
291 config.map_file = fopen (config.map_filename, FOPEN_WT);
292 if (config.map_file == (FILE *) NULL)
294 bfd_set_error (bfd_error_system_call);
295 einfo ("%P%F: cannot open map file %s: %E\n",
296 config.map_filename);
304 /* Print error messages for any missing symbols, for any warning
305 symbols, and possibly multiple definitions */
308 if (config.text_read_only)
310 /* Look for a text section and mark the readonly attribute in it */
311 asection *found = bfd_get_section_by_name (output_bfd, ".text");
313 if (found != (asection *) NULL)
315 found->flags |= SEC_READONLY;
319 if (link_info.relocateable)
320 output_bfd->flags &= ~EXEC_P;
322 output_bfd->flags |= EXEC_P;
326 /* Even if we're producing relocateable output, some non-fatal errors should
327 be reported in the exit status. (What non-fatal errors, if any, do we
328 want to ignore for relocateable output?) */
330 if (config.make_executable == false && force_make_executable == false)
332 if (trace_files == true)
334 einfo ("%P: link errors found, deleting executable `%s'\n",
338 if (output_bfd->iostream)
339 fclose ((FILE *) (output_bfd->iostream));
341 unlink (output_filename);
346 if (! bfd_close (output_bfd))
347 einfo ("%F%B: final close failed: %E\n", output_bfd);
350 END_PROGRESS (program_name);
354 extern char **environ;
356 char *lim = (char *) sbrk (0);
358 long run_time = get_run_time () - start_time;
360 fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
361 program_name, run_time / 1000000, run_time % 1000000);
363 fprintf (stderr, "%s: data size %ld\n", program_name,
364 (long) (lim - (char *) &environ));
368 /* Prevent remove_output from doing anything, after a successful link. */
369 output_filename = NULL;
375 /* We need to find any explicitly given emulation in order to initialize the
376 state that's needed by the lex&yacc argument parser (parse_args). */
379 get_emulation (argc, argv)
386 emulation = (char *) getenv (EMULATION_ENVIRON);
387 if (emulation == NULL)
388 emulation = DEFAULT_EMULATION;
390 for (i = 1; i < argc; i++)
392 if (!strncmp (argv[i], "-m", 2))
394 if (argv[i][2] == '\0')
399 emulation = argv[i + 1];
404 einfo("%P%F: missing argument to -m\n");
407 else if (strcmp (argv[i], "-mips1") == 0
408 || strcmp (argv[i], "-mips2") == 0
409 || strcmp (argv[i], "-mips3") == 0)
411 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
412 passed to the linker by some MIPS compilers. They
413 generally tell the linker to use a slightly different
414 library path. Perhaps someday these should be
415 implemented as emulations; until then, we just ignore
416 the arguments and hope that nobody ever creates
417 emulations named ips1, ips2 or ips3. */
419 else if (strcmp (argv[i], "-m486") == 0)
421 /* FIXME: The argument -m486 is passed to the linker on
422 some Linux systems. Hope that nobody creates an
423 emulation named 486. */
428 emulation = &argv[i][2];
436 /* If directory DIR contains an "ldscripts" subdirectory,
437 add DIR to the library search path and return true,
438 else return false. */
441 check_for_scripts_dir (dir)
449 dirlen = strlen (dir);
450 /* sizeof counts the terminating NUL. */
451 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
452 sprintf (buf, "%s/ldscripts", dir);
454 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
457 ldfile_add_library_path (dir, false);
461 /* Set the default directory for finding script files.
462 Libraries will be searched for here too, but that's ok.
463 We look for the "ldscripts" directory in:
465 SCRIPTDIR (passed from Makefile)
466 the dir where this program is (for using it from the build tree)
467 the dir where this program is/../lib (for installing the tool suite elsewhere) */
475 if (check_for_scripts_dir (SCRIPTDIR))
476 return; /* We've been installed normally. */
478 /* Look for "ldscripts" in the dir where our binary is. */
479 end = strrchr (program_name, '/');
482 dirlen = end - program_name;
483 /* Make a copy of program_name in dir.
484 Leave room for later "/../lib". */
485 dir = (char *) xmalloc (dirlen + 8);
486 strncpy (dir, program_name, dirlen);
492 dir = (char *) xmalloc (dirlen + 8);
496 if (check_for_scripts_dir (dir))
497 return; /* Don't free dir. */
499 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
500 strcpy (dir + dirlen, "/../lib");
501 if (check_for_scripts_dir (dir))
504 free (dir); /* Well, we tried. */
511 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
513 link_info.notice_hash = ((struct bfd_hash_table *)
514 xmalloc (sizeof (struct bfd_hash_table)));
515 if (! bfd_hash_table_init_n (link_info.notice_hash,
518 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
521 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
522 == (struct bfd_hash_entry *) NULL)
523 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
526 /* Handle the -retain-symbols-file option. */
529 add_keepsyms_file (filename)
530 const char *filename;
537 if (link_info.strip == strip_some)
538 einfo ("%X%P: error: duplicate retain-symbols-file\n");
540 file = fopen (filename, "r");
541 if (file == (FILE *) NULL)
543 bfd_set_error (bfd_error_system_call);
544 einfo ("%X%P: %s: %E", filename);
548 link_info.keep_hash = ((struct bfd_hash_table *)
549 xmalloc (sizeof (struct bfd_hash_table)));
550 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
551 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
554 buf = (char *) xmalloc (bufsize);
566 while (! isspace (c) && c != EOF)
573 buf = xrealloc (buf, bufsize);
580 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
581 == (struct bfd_hash_entry *) NULL)
582 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
586 if (link_info.strip != strip_none)
587 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
589 link_info.strip = strip_some;
592 /* Callbacks from the BFD linker routines. */
594 /* This is called when BFD has decided to include an archive member in
599 add_archive_element (info, abfd, name)
600 struct bfd_link_info *info;
604 lang_input_statement_type *input;
606 input = ((lang_input_statement_type *)
607 xmalloc ((bfd_size_type) sizeof (lang_input_statement_type)));
608 input->filename = abfd->filename;
609 input->local_sym_name = abfd->filename;
610 input->the_bfd = abfd;
611 input->asymbols = NULL;
613 input->just_syms_flag = false;
614 input->loaded = false;
615 input->search_dirs_flag = false;
617 /* FIXME: The following fields are not set: header.next,
618 header.type, closed, passive_position, symbol_count,
619 next_real_file, is_archive, target, real, common_section,
620 common_output_section, complained. This bit of code is from the
621 old decode_library_subfile function. I don't know whether any of
622 those fields matters. */
624 ldlang_add_file (input);
626 if (config.map_file != (FILE *) NULL)
627 minfo ("%s needed due to %T\n", abfd->filename, name);
629 if (trace_files || trace_file_tries)
630 info_msg ("%I\n", input);
635 /* This is called when BFD has discovered a symbol which is defined
640 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
641 struct bfd_link_info *info;
650 einfo ("%X%C: multiple definition of `%T'\n",
651 nbfd, nsec, nval, name);
652 if (obfd != (bfd *) NULL)
653 einfo ("%D: first defined here\n", obfd, osec, oval);
657 /* This is called when there is a definition of a common symbol, or
658 when a common symbol is found for a symbol that is already defined,
659 or when two common symbols are found. We only do something if
660 -warn-common was used. */
664 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
665 struct bfd_link_info *info;
668 enum bfd_link_hash_type otype;
671 enum bfd_link_hash_type ntype;
674 if (! config.warn_common)
677 if (ntype == bfd_link_hash_defined
678 || ntype == bfd_link_hash_defweak
679 || ntype == bfd_link_hash_indirect)
681 ASSERT (otype == bfd_link_hash_common);
682 einfo ("%B: warning: definition of `%T' overriding common\n",
685 einfo ("%B: warning: common is here\n", obfd);
687 else if (otype == bfd_link_hash_defined
688 || otype == bfd_link_hash_defweak
689 || otype == bfd_link_hash_indirect)
691 ASSERT (ntype == bfd_link_hash_common);
692 einfo ("%B: warning: common of `%T' overridden by definition\n",
695 einfo ("%B: warning: defined here\n", obfd);
699 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
702 einfo ("%B: warning: common of `%T' overridden by larger common\n",
705 einfo ("%B: warning: larger common is here\n", obfd);
707 else if (nsize > osize)
709 einfo ("%B: warning: common of `%T' overriding smaller common\n",
712 einfo ("%B: warning: smaller common is here\n", obfd);
716 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
718 einfo ("%B: warning: previous common is here\n", obfd);
725 /* This is called when BFD has discovered a set element. H is the
726 entry in the linker hash table for the set. SECTION and VALUE
727 represent a value which should be added to the set. */
731 add_to_set (info, h, reloc, abfd, section, value)
732 struct bfd_link_info *info;
733 struct bfd_link_hash_entry *h;
734 bfd_reloc_code_real_type reloc;
739 if (! config.build_constructors)
742 ldctor_add_set_entry (h, reloc, section, value);
744 if (h->type == bfd_link_hash_new)
746 h->type = bfd_link_hash_undefined;
747 h->u.undef.abfd = abfd;
748 /* We don't call bfd_link_add_undef to add this to the list of
749 undefined symbols because we are going to define it
756 /* This is called when BFD has discovered a constructor. This is only
757 called for some object file formats--those which do not handle
758 constructors in some more clever fashion. This is similar to
759 adding an element to a set, but less general. */
762 constructor_callback (info, constructor, name, abfd, section, value)
763 struct bfd_link_info *info;
772 struct bfd_link_hash_entry *h;
774 if (! config.build_constructors)
777 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
778 useful error message. */
779 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL)
780 einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported");
782 set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__");
784 if (bfd_get_symbol_leading_char (abfd) != '\0')
785 *s++ = bfd_get_symbol_leading_char (abfd);
787 strcpy (s, "__CTOR_LIST__");
789 strcpy (s, "__DTOR_LIST__");
791 if (config.map_file != (FILE *) NULL)
792 fprintf (config.map_file,
793 "Adding %s to constructor/destructor set %s\n", name, set_name);
795 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
796 if (h == (struct bfd_link_hash_entry *) NULL)
797 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
798 if (h->type == bfd_link_hash_new)
800 h->type = bfd_link_hash_undefined;
801 h->u.undef.abfd = abfd;
802 /* We don't call bfd_link_add_undef to add this to the list of
803 undefined symbols because we are going to define it
807 ldctor_add_set_entry (h, BFD_RELOC_CTOR, section, value);
811 /* This is called when there is a reference to a warning symbol. */
815 warning_callback (info, warning)
816 struct bfd_link_info *info;
819 einfo ("%P: %s\n", warning);
823 /* This is called when an undefined symbol is found. */
827 undefined_symbol (info, name, abfd, section, address)
828 struct bfd_link_info *info;
834 static char *error_name;
835 static unsigned int error_count;
837 #define MAX_ERRORS_IN_A_ROW 5
839 if (config.warn_once)
841 static struct bfd_hash_table *hash;
843 /* Only warn once about a particular undefined symbol. */
847 hash = ((struct bfd_hash_table *)
848 xmalloc (sizeof (struct bfd_hash_table)));
849 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
850 einfo ("%F%P: bfd_hash_table_init failed: %E\n");
853 if (bfd_hash_lookup (hash, name, false, false) != NULL)
856 if (bfd_hash_lookup (hash, name, true, true) == NULL)
857 einfo ("%F%P: bfd_hash_lookup failed: %E\n");
860 /* We never print more than a reasonable number of errors in a row
861 for a single symbol. */
862 if (error_name != (char *) NULL
863 && strcmp (name, error_name) == 0)
868 if (error_name != (char *) NULL)
870 error_name = buystring (name);
875 if (error_count < MAX_ERRORS_IN_A_ROW)
876 einfo ("%X%C: undefined reference to `%T'\n",
877 abfd, section, address, name);
878 else if (error_count == MAX_ERRORS_IN_A_ROW)
879 einfo ("%D: more undefined references to `%T' follow\n",
880 abfd, section, address, name);
884 if (error_count < MAX_ERRORS_IN_A_ROW)
885 einfo ("%X%B: undefined reference to `%T'\n",
887 else if (error_count == MAX_ERRORS_IN_A_ROW)
888 einfo ("%B: more undefined references to `%T' follow\n",
895 /* This is called when a reloc overflows. */
899 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
900 struct bfd_link_info *info;
902 const char *reloc_name;
908 if (abfd == (bfd *) NULL)
909 einfo ("%P%X: generated");
911 einfo ("%X%C:", abfd, section, address);
912 einfo (" relocation truncated to fit: %s %T", reloc_name, name);
914 einfo ("+%v", addend);
919 /* This is called when a dangerous relocation is made. */
923 reloc_dangerous (info, message, abfd, section, address)
924 struct bfd_link_info *info;
930 if (abfd == (bfd *) NULL)
931 einfo ("%P%X: generated");
933 einfo ("%X%C:", abfd, section, address);
934 einfo ("dangerous relocation: %s\n", message);
938 /* This is called when a reloc is being generated attached to a symbol
939 that is not being output. */
943 unattached_reloc (info, name, abfd, section, address)
944 struct bfd_link_info *info;
950 if (abfd == (bfd *) NULL)
951 einfo ("%P%X: generated");
953 einfo ("%X%C:", abfd, section, address);
954 einfo (" reloc refers to symbol `%T' which is not being output\n", name);
958 /* This is called when a symbol in notice_hash is found. Symbols are
959 put in notice_hash using the -y option. */
963 notice_ysym (info, name, abfd, section, value)
964 struct bfd_link_info *info;
970 einfo ("%B: %s %s\n", abfd,
971 bfd_is_und_section (section) ? "reference to" : "definition of",