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, 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 (link_info.shared)
224 einfo ("%P%F: -r and -shared may not be used together\n");
227 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
228 don't see how else this can be handled, since in this case we
229 must preserve all externally visible symbols. */
230 if (link_info.relocateable && link_info.strip == strip_all)
232 link_info.strip = strip_debugger;
233 if (link_info.discard == discard_none)
234 link_info.discard = discard_all;
237 /* This essentially adds another -L directory so this must be done after
238 the -L's in argv have been processed. */
241 if (had_script == false)
243 /* Read the emulation's appropriate default script. */
245 char *s = ldemul_get_script (&isfile);
248 ldfile_open_command_file (s);
251 if (trace_file_tries)
253 info_msg ("using internal linker script:\n");
254 info_msg ("==================================================\n");
256 info_msg ("\n==================================================\n");
260 parser_input = input_script;
266 if (lang_has_input_file == false)
270 einfo ("%P%F: no input files\n");
275 info_msg ("%P: mode %s\n", emulation);
278 ldemul_after_parse ();
281 if (config.map_filename)
283 if (strcmp (config.map_filename, "-") == 0)
285 config.map_file = stdout;
289 config.map_file = fopen (config.map_filename, FOPEN_WT);
290 if (config.map_file == (FILE *) NULL)
292 bfd_set_error (bfd_error_system_call);
293 einfo ("%P%F: cannot open map file %s: %E\n",
294 config.map_filename);
302 /* Print error messages for any missing symbols, for any warning
303 symbols, and possibly multiple definitions */
306 if (config.text_read_only)
308 /* Look for a text section and mark the readonly attribute in it */
309 asection *found = bfd_get_section_by_name (output_bfd, ".text");
311 if (found != (asection *) NULL)
313 found->flags |= SEC_READONLY;
317 if (link_info.relocateable)
318 output_bfd->flags &= ~EXEC_P;
320 output_bfd->flags |= EXEC_P;
324 /* Even if we're producing relocateable output, some non-fatal errors should
325 be reported in the exit status. (What non-fatal errors, if any, do we
326 want to ignore for relocateable output?) */
328 if (config.make_executable == false && force_make_executable == false)
330 if (trace_files == true)
332 einfo ("%P: link errors found, deleting executable `%s'\n",
336 if (output_bfd->iostream)
337 fclose ((FILE *) (output_bfd->iostream));
339 unlink (output_filename);
344 if (! bfd_close (output_bfd))
345 einfo ("%F%B: final close failed: %E\n", output_bfd);
348 END_PROGRESS (program_name);
352 extern char **environ;
354 char *lim = (char *) sbrk (0);
356 long run_time = get_run_time () - start_time;
358 fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
359 program_name, run_time / 1000000, run_time % 1000000);
361 fprintf (stderr, "%s: data size %ld\n", program_name,
362 (long) (lim - (char *) &environ));
366 /* Prevent remove_output from doing anything, after a successful link. */
367 output_filename = NULL;
373 /* We need to find any explicitly given emulation in order to initialize the
374 state that's needed by the lex&yacc argument parser (parse_args). */
377 get_emulation (argc, argv)
384 emulation = (char *) getenv (EMULATION_ENVIRON);
385 if (emulation == NULL)
386 emulation = DEFAULT_EMULATION;
388 for (i = 1; i < argc; i++)
390 if (!strncmp (argv[i], "-m", 2))
392 if (argv[i][2] == '\0')
397 emulation = argv[i + 1];
402 einfo("%P%F: missing argument to -m\n");
405 else if (strcmp (argv[i], "-mips1") == 0
406 || strcmp (argv[i], "-mips2") == 0
407 || strcmp (argv[i], "-mips3") == 0)
409 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
410 passed to the linker by some MIPS compilers. They
411 generally tell the linker to use a slightly different
412 library path. Perhaps someday these should be
413 implemented as emulations; until then, we just ignore
414 the arguments and hope that nobody ever creates
415 emulations named ips1, ips2 or ips3. */
417 else if (strcmp (argv[i], "-m486") == 0)
419 /* FIXME: The argument -m486 is passed to the linker on
420 some Linux systems. Hope that nobody creates an
421 emulation named 486. */
426 emulation = &argv[i][2];
434 /* If directory DIR contains an "ldscripts" subdirectory,
435 add DIR to the library search path and return true,
436 else return false. */
439 check_for_scripts_dir (dir)
447 dirlen = strlen (dir);
448 /* sizeof counts the terminating NUL. */
449 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
450 sprintf (buf, "%s/ldscripts", dir);
452 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
455 ldfile_add_library_path (dir, false);
459 /* Set the default directory for finding script files.
460 Libraries will be searched for here too, but that's ok.
461 We look for the "ldscripts" directory in:
463 SCRIPTDIR (passed from Makefile)
464 the dir where this program is (for using it from the build tree)
465 the dir where this program is/../lib (for installing the tool suite elsewhere) */
473 if (check_for_scripts_dir (SCRIPTDIR))
474 return; /* We've been installed normally. */
476 /* Look for "ldscripts" in the dir where our binary is. */
477 end = strrchr (program_name, '/');
480 dirlen = end - program_name;
481 /* Make a copy of program_name in dir.
482 Leave room for later "/../lib". */
483 dir = (char *) xmalloc (dirlen + 8);
484 strncpy (dir, program_name, dirlen);
490 dir = (char *) xmalloc (dirlen + 8);
494 if (check_for_scripts_dir (dir))
495 return; /* Don't free dir. */
497 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
498 strcpy (dir + dirlen, "/../lib");
499 if (check_for_scripts_dir (dir))
502 free (dir); /* Well, we tried. */
509 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
511 link_info.notice_hash = ((struct bfd_hash_table *)
512 xmalloc (sizeof (struct bfd_hash_table)));
513 if (! bfd_hash_table_init_n (link_info.notice_hash,
516 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
519 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
520 == (struct bfd_hash_entry *) NULL)
521 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
524 /* Handle the -retain-symbols-file option. */
527 add_keepsyms_file (filename)
528 const char *filename;
535 if (link_info.strip == strip_some)
536 einfo ("%X%P: error: duplicate retain-symbols-file\n");
538 file = fopen (filename, "r");
539 if (file == (FILE *) NULL)
541 bfd_set_error (bfd_error_system_call);
542 einfo ("%X%P: %s: %E", filename);
546 link_info.keep_hash = ((struct bfd_hash_table *)
547 xmalloc (sizeof (struct bfd_hash_table)));
548 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
549 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
552 buf = (char *) xmalloc (bufsize);
564 while (! isspace (c) && c != EOF)
571 buf = xrealloc (buf, bufsize);
578 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
579 == (struct bfd_hash_entry *) NULL)
580 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
584 if (link_info.strip != strip_none)
585 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
587 link_info.strip = strip_some;
590 /* Callbacks from the BFD linker routines. */
592 /* This is called when BFD has decided to include an archive member in
597 add_archive_element (info, abfd, name)
598 struct bfd_link_info *info;
602 lang_input_statement_type *input;
604 input = ((lang_input_statement_type *)
605 xmalloc ((bfd_size_type) sizeof (lang_input_statement_type)));
606 input->filename = abfd->filename;
607 input->local_sym_name = abfd->filename;
608 input->the_bfd = abfd;
609 input->asymbols = NULL;
611 input->just_syms_flag = false;
612 input->loaded = false;
613 input->search_dirs_flag = false;
615 /* FIXME: The following fields are not set: header.next,
616 header.type, closed, passive_position, symbol_count,
617 next_real_file, is_archive, target, real, common_section,
618 common_output_section, complained. This bit of code is from the
619 old decode_library_subfile function. I don't know whether any of
620 those fields matters. */
622 ldlang_add_file (input);
624 if (config.map_file != (FILE *) NULL)
625 minfo ("%s needed due to %T\n", abfd->filename, name);
627 if (trace_files || trace_file_tries)
628 info_msg ("%I\n", input);
633 /* This is called when BFD has discovered a symbol which is defined
638 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
639 struct bfd_link_info *info;
648 einfo ("%X%C: multiple definition of `%T'\n",
649 nbfd, nsec, nval, name);
650 if (obfd != (bfd *) NULL)
651 einfo ("%D: first defined here\n", obfd, osec, oval);
655 /* This is called when there is a definition of a common symbol, or
656 when a common symbol is found for a symbol that is already defined,
657 or when two common symbols are found. We only do something if
658 -warn-common was used. */
662 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
663 struct bfd_link_info *info;
666 enum bfd_link_hash_type otype;
669 enum bfd_link_hash_type ntype;
672 if (! config.warn_common)
675 if (ntype == bfd_link_hash_defined
676 || ntype == bfd_link_hash_defweak
677 || ntype == bfd_link_hash_indirect)
679 ASSERT (otype == bfd_link_hash_common);
680 einfo ("%B: warning: definition of `%T' overriding common\n",
683 einfo ("%B: warning: common is here\n", obfd);
685 else if (otype == bfd_link_hash_defined
686 || otype == bfd_link_hash_defweak
687 || otype == bfd_link_hash_indirect)
689 ASSERT (ntype == bfd_link_hash_common);
690 einfo ("%B: warning: common of `%T' overridden by definition\n",
693 einfo ("%B: warning: defined here\n", obfd);
697 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
700 einfo ("%B: warning: common of `%T' overridden by larger common\n",
703 einfo ("%B: warning: larger common is here\n", obfd);
705 else if (nsize > osize)
707 einfo ("%B: warning: common of `%T' overriding smaller common\n",
710 einfo ("%B: warning: smaller common is here\n", obfd);
714 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
716 einfo ("%B: warning: previous common is here\n", obfd);
723 /* This is called when BFD has discovered a set element. H is the
724 entry in the linker hash table for the set. SECTION and VALUE
725 represent a value which should be added to the set. */
729 add_to_set (info, h, reloc, abfd, section, value)
730 struct bfd_link_info *info;
731 struct bfd_link_hash_entry *h;
732 bfd_reloc_code_real_type reloc;
737 if (! config.build_constructors)
740 ldctor_add_set_entry (h, reloc, section, value);
742 if (h->type == bfd_link_hash_new)
744 h->type = bfd_link_hash_undefined;
745 h->u.undef.abfd = abfd;
746 /* We don't call bfd_link_add_undef to add this to the list of
747 undefined symbols because we are going to define it
754 /* This is called when BFD has discovered a constructor. This is only
755 called for some object file formats--those which do not handle
756 constructors in some more clever fashion. This is similar to
757 adding an element to a set, but less general. */
760 constructor_callback (info, constructor, name, abfd, section, value)
761 struct bfd_link_info *info;
770 struct bfd_link_hash_entry *h;
772 if (! config.build_constructors)
775 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
776 useful error message. */
777 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL)
778 einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported");
780 set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__");
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");
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 /* This is called when there is a reference to a warning symbol. */
813 warning_callback (info, warning)
814 struct bfd_link_info *info;
817 einfo ("%P: %s\n", warning);
821 /* This is called when an undefined symbol is found. */
825 undefined_symbol (info, name, abfd, section, address)
826 struct bfd_link_info *info;
832 static char *error_name;
833 static unsigned int error_count;
835 #define MAX_ERRORS_IN_A_ROW 5
837 if (config.warn_once)
839 static struct bfd_hash_table *hash;
841 /* Only warn once about a particular undefined symbol. */
845 hash = ((struct bfd_hash_table *)
846 xmalloc (sizeof (struct bfd_hash_table)));
847 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
848 einfo ("%F%P: bfd_hash_table_init failed: %E\n");
851 if (bfd_hash_lookup (hash, name, false, false) != NULL)
854 if (bfd_hash_lookup (hash, name, true, true) == NULL)
855 einfo ("%F%P: bfd_hash_lookup failed: %E\n");
858 /* We never print more than a reasonable number of errors in a row
859 for a single symbol. */
860 if (error_name != (char *) NULL
861 && strcmp (name, error_name) == 0)
866 if (error_name != (char *) NULL)
868 error_name = buystring (name);
873 if (error_count < MAX_ERRORS_IN_A_ROW)
874 einfo ("%X%C: undefined reference to `%T'\n",
875 abfd, section, address, name);
876 else if (error_count == MAX_ERRORS_IN_A_ROW)
877 einfo ("%D: more undefined references to `%T' follow\n",
878 abfd, section, address, name);
882 if (error_count < MAX_ERRORS_IN_A_ROW)
883 einfo ("%X%B: undefined reference to `%T'\n",
885 else if (error_count == MAX_ERRORS_IN_A_ROW)
886 einfo ("%B: more undefined references to `%T' follow\n",
893 /* This is called when a reloc overflows. */
897 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
898 struct bfd_link_info *info;
900 const char *reloc_name;
906 if (abfd == (bfd *) NULL)
907 einfo ("%P%X: generated");
909 einfo ("%X%C:", abfd, section, address);
910 einfo (" relocation truncated to fit: %s %T", reloc_name, name);
912 einfo ("+%v", addend);
917 /* This is called when a dangerous relocation is made. */
921 reloc_dangerous (info, message, abfd, section, address)
922 struct bfd_link_info *info;
928 if (abfd == (bfd *) NULL)
929 einfo ("%P%X: generated");
931 einfo ("%X%C:", abfd, section, address);
932 einfo ("dangerous relocation: %s\n", message);
936 /* This is called when a reloc is being generated attached to a symbol
937 that is not being output. */
941 unattached_reloc (info, name, abfd, section, address)
942 struct bfd_link_info *info;
948 if (abfd == (bfd *) NULL)
949 einfo ("%P%X: generated");
951 einfo ("%X%C:", abfd, section, address);
952 einfo (" reloc refers to symbol `%T' which is not being output\n", name);
956 /* This is called when a symbol in notice_hash is found. Symbols are
957 put in notice_hash using the -y option. */
961 notice_ysym (info, name, abfd, section, value)
962 struct bfd_link_info *info;
968 einfo ("%B: %s %s\n", abfd,
969 bfd_is_und_section (section) ? "reference to" : "definition of",