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 args_type command_line;
77 ld_config_type config;
79 static boolean check_for_scripts_dir PARAMS ((char *dir));
80 static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
82 static boolean multiple_definition PARAMS ((struct bfd_link_info *,
84 bfd *, asection *, bfd_vma,
85 bfd *, asection *, bfd_vma));
86 static boolean multiple_common PARAMS ((struct bfd_link_info *,
88 enum bfd_link_hash_type, bfd_vma,
89 bfd *, enum bfd_link_hash_type,
91 static boolean add_to_set PARAMS ((struct bfd_link_info *,
92 struct bfd_link_hash_entry *,
93 bfd_reloc_code_real_type,
94 bfd *, asection *, bfd_vma));
95 static boolean constructor_callback PARAMS ((struct bfd_link_info *,
98 bfd *, asection *, bfd_vma));
99 static boolean warning_callback PARAMS ((struct bfd_link_info *,
101 static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
103 asection *, bfd_vma));
104 static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
105 const char *, bfd_vma,
106 bfd *, asection *, bfd_vma));
107 static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
108 bfd *, asection *, bfd_vma));
109 static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
110 const char *, bfd *, asection *,
112 static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
113 bfd *, asection *, bfd_vma));
115 static struct bfd_link_callbacks link_callbacks =
121 constructor_callback,
130 struct bfd_link_info link_info;
137 if (output_bfd && output_bfd->iostream)
138 fclose((FILE *)(output_bfd->iostream));
139 if (delete_output_file_on_failure)
140 unlink (output_filename);
150 long start_time = get_run_time ();
152 program_name = argv[0];
153 xmalloc_set_program_name (program_name);
157 xatexit (remove_output);
159 /* Initialize the data about options. */
160 trace_files = trace_file_tries = version_printed = false;
161 config.build_constructors = true;
162 config.dynamic_link = false;
163 command_line.force_common_definition = false;
165 link_info.callbacks = &link_callbacks;
166 link_info.relocateable = false;
167 link_info.strip = strip_none;
168 link_info.discard = discard_none;
169 link_info.lprefix_len = 1;
170 link_info.lprefix = "L";
171 link_info.keep_memory = true;
172 link_info.input_bfds = NULL;
173 link_info.create_object_symbols_section = NULL;
174 link_info.hash = NULL;
175 link_info.keep_hash = NULL;
176 link_info.notice_hash = NULL;
178 ldfile_add_arch ("");
180 config.make_executable = true;
181 force_make_executable = false;
182 config.magic_demand_paged = true;
183 config.text_read_only = true;
184 config.make_executable = true;
186 emulation = get_emulation (argc, argv);
187 ldemul_choose_mode (emulation);
188 default_target = ldemul_choose_target ();
190 ldemul_before_parse ();
191 lang_has_input_file = false;
192 parse_args (argc, argv);
194 if (link_info.relocateable)
196 if (command_line.relax)
197 einfo ("%P%F: -relax and -r may not be used together\n");
198 if (config.dynamic_link)
199 einfo ("%P%F: -r and -call_shared may not be used together\n");
200 if (link_info.strip == strip_all)
201 einfo ("%P%F: -r and -s may not be used together\n");
204 /* This essentially adds another -L directory so this must be done after
205 the -L's in argv have been processed. */
208 if (had_script == false)
210 /* Read the emulation's appropriate default script. */
212 char *s = ldemul_get_script (&isfile);
215 ldfile_open_command_file (s);
218 parser_input = input_script;
224 if (lang_has_input_file == false)
228 einfo ("%P%F: no input files\n");
233 info_msg ("%P: mode %s\n", emulation);
236 ldemul_after_parse ();
239 if (config.map_filename)
241 if (strcmp (config.map_filename, "-") == 0)
243 config.map_file = stdout;
247 config.map_file = fopen (config.map_filename, FOPEN_WT);
248 if (config.map_file == (FILE *) NULL)
250 einfo ("%P%F: cannot open map file %s: %E\n",
251 config.map_filename);
259 /* Print error messages for any missing symbols, for any warning
260 symbols, and possibly multiple definitions */
263 if (config.text_read_only)
265 /* Look for a text section and mark the readonly attribute in it */
266 asection *found = bfd_get_section_by_name (output_bfd, ".text");
268 if (found != (asection *) NULL)
270 found->flags |= SEC_READONLY;
274 if (link_info.relocateable)
275 output_bfd->flags &= ~EXEC_P;
277 output_bfd->flags |= EXEC_P;
281 /* Even if we're producing relocateable output, some non-fatal errors should
282 be reported in the exit status. (What non-fatal errors, if any, do we
283 want to ignore for relocateable output?) */
285 if (config.make_executable == false && force_make_executable == false)
287 if (trace_files == true)
289 einfo ("%P: link errors found, deleting executable `%s'\n",
293 if (output_bfd->iostream)
294 fclose ((FILE *) (output_bfd->iostream));
296 unlink (output_filename);
301 if (! bfd_close (output_bfd))
302 einfo ("%F%B: final close failed: %E\n", output_bfd);
307 extern char **environ;
308 char *lim = (char *) sbrk (0);
309 long run_time = get_run_time () - start_time;
311 fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
312 program_name, run_time / 1000000, run_time % 1000000);
313 fprintf (stderr, "%s: data size %ld\n", program_name,
314 (long) (lim - (char *) &environ));
317 /* Prevent remove_output from doing anything, after a successful link. */
318 output_filename = NULL;
324 /* We need to find any explicitly given emulation in order to initialize the
325 state that's needed by the lex&yacc argument parser (parse_args). */
328 get_emulation (argc, argv)
335 emulation = (char *) getenv (EMULATION_ENVIRON);
336 if (emulation == NULL)
337 emulation = DEFAULT_EMULATION;
339 for (i = 1; i < argc; i++)
341 if (!strncmp (argv[i], "-m", 2))
343 if (argv[i][2] == '\0')
348 emulation = argv[i + 1];
353 einfo("%P%F: missing argument to -m\n");
356 else if (strcmp (argv[i], "-mips1") == 0
357 || strcmp (argv[i], "-mips2") == 0
358 || strcmp (argv[i], "-mips3") == 0)
360 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
361 passed to the linker by some MIPS compilers. They
362 generally tell the linker to use a slightly different
363 library path. Perhaps someday these should be
364 implemented as emulations; until then, we just ignore
365 the arguments and hope that nobody ever creates
366 emulations named ips1, ips2 or ips3. */
371 emulation = &argv[i][2];
379 /* If directory DIR contains an "ldscripts" subdirectory,
380 add DIR to the library search path and return true,
381 else return false. */
384 check_for_scripts_dir (dir)
392 dirlen = strlen (dir);
393 /* sizeof counts the terminating NUL. */
394 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
395 sprintf (buf, "%s/ldscripts", dir);
397 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
400 ldfile_add_library_path (dir);
404 /* Set the default directory for finding script files.
405 Libraries will be searched for here too, but that's ok.
406 We look for the "ldscripts" directory in:
408 SCRIPTDIR (passed from Makefile)
409 the dir where this program is (for using it from the build tree)
410 the dir where this program is/../lib (for installing the tool suite elsewhere) */
418 if (check_for_scripts_dir (SCRIPTDIR))
419 return; /* We've been installed normally. */
421 /* Look for "ldscripts" in the dir where our binary is. */
422 end = strrchr (program_name, '/');
425 dirlen = end - program_name;
426 /* Make a copy of program_name in dir.
427 Leave room for later "/../lib". */
428 dir = (char *) xmalloc (dirlen + 8);
429 strncpy (dir, program_name, dirlen);
435 dir = (char *) xmalloc (dirlen + 8);
439 if (check_for_scripts_dir (dir))
440 return; /* Don't free dir. */
442 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
443 strcpy (dir + dirlen, "/../lib");
444 if (check_for_scripts_dir (dir))
447 free (dir); /* Well, we tried. */
454 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
456 link_info.notice_hash = ((struct bfd_hash_table *)
457 xmalloc (sizeof (struct bfd_hash_table)));
458 if (! bfd_hash_table_init_n (link_info.notice_hash,
461 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
464 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
465 == (struct bfd_hash_entry *) NULL)
466 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
469 /* Handle the -retain-symbols-file option. */
472 add_keepsyms_file (filename)
473 const char *filename;
480 if (link_info.strip == strip_some)
481 einfo ("%X%P: error: duplicate retain-symbols-file\n");
483 file = fopen (filename, "r");
484 if (file == (FILE *) NULL)
486 bfd_set_error (bfd_error_system_call);
487 einfo ("%X%P: %s: %E", filename);
491 link_info.keep_hash = ((struct bfd_hash_table *)
492 xmalloc (sizeof (struct bfd_hash_table)));
493 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
494 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
497 buf = (char *) xmalloc (bufsize);
509 while (! isspace (c) && c != EOF)
516 buf = xrealloc (buf, bufsize);
523 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
524 == (struct bfd_hash_entry *) NULL)
525 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
529 if (link_info.strip != strip_none)
530 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
532 link_info.strip = strip_some;
535 /* Callbacks from the BFD linker routines. */
537 /* This is called when BFD has decided to include an archive member in
542 add_archive_element (info, abfd, name)
543 struct bfd_link_info *info;
547 lang_input_statement_type *input;
549 input = ((lang_input_statement_type *)
550 xmalloc ((bfd_size_type) sizeof (lang_input_statement_type)));
551 input->filename = abfd->filename;
552 input->local_sym_name = abfd->filename;
553 input->the_bfd = abfd;
554 input->asymbols = NULL;
555 input->subfiles = NULL;
557 input->just_syms_flag = false;
558 input->loaded = false;
561 /* FIXME: This is wrong. It should point to an entry for the
562 archive itself. However, it doesn't seem to matter. */
563 input->superfile = NULL;
565 /* FIXME: The following fields are not set: header.next,
566 header.type, closed, passive_position, symbol_count, total_size,
567 next_real_file, is_archive, search_dirs_flag, target, real,
568 common_section, common_output_section, complained. This bit of
569 code is from the old decode_library_subfile function. I don't
570 know whether any of those fields matters. */
572 ldlang_add_file (input);
574 if (config.map_file != (FILE *) NULL)
575 minfo ("%s needed due to %T\n", abfd->filename, name);
577 if (trace_files || trace_file_tries)
578 info_msg ("%I\n", input);
583 /* This is called when BFD has discovered a symbol which is defined
588 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
589 struct bfd_link_info *info;
598 einfo ("%X%C: multiple definition of `%T'\n",
599 nbfd, nsec, nval, name);
600 if (obfd != (bfd *) NULL)
601 einfo ("%C: first defined here\n", obfd, osec, oval);
605 /* This is called when there is a definition of a common symbol, or
606 when a common symbol is found for a symbol that is already defined,
607 or when two common symbols are found. We only do something if
608 -warn-common was used. */
612 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
613 struct bfd_link_info *info;
616 enum bfd_link_hash_type otype;
619 enum bfd_link_hash_type ntype;
622 if (! config.warn_common)
625 if (ntype == bfd_link_hash_defined)
627 ASSERT (otype == bfd_link_hash_common);
628 einfo ("%B: warning: definition of `%T' overriding common\n",
630 einfo ("%B: warning: common is here\n", obfd);
632 else if (otype == bfd_link_hash_defined)
634 ASSERT (ntype == bfd_link_hash_common);
635 einfo ("%B: warning: common of `%T' overridden by definition\n",
637 einfo ("%B: warning: defined here\n", obfd);
641 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
644 einfo ("%B: warning: common of `%T' overridden by larger common\n",
646 einfo ("%B: warning: larger common is here\n", obfd);
648 else if (nsize > osize)
650 einfo ("%B: warning: common of `%T' overriding smaller common\n",
652 einfo ("%B: warning: smaller common is here\n", obfd);
656 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
657 einfo ("%B: warning: previous common is here\n", obfd);
664 /* This is called when BFD has discovered a set element. H is the
665 entry in the linker hash table for the set. SECTION and VALUE
666 represent a value which should be added to the set. */
670 add_to_set (info, h, reloc, abfd, section, value)
671 struct bfd_link_info *info;
672 struct bfd_link_hash_entry *h;
673 bfd_reloc_code_real_type reloc;
678 if (! config.build_constructors)
681 ldctor_add_set_entry (h, reloc, section, value);
683 if (h->type == bfd_link_hash_new)
685 h->type = bfd_link_hash_undefined;
686 h->u.undef.abfd = abfd;
687 /* We don't call bfd_link_add_undef to add this to the list of
688 undefined symbols because we are going to define it
695 /* This is called when BFD has discovered a constructor. This is only
696 called for some object file formats--those which do not handle
697 constructors in some more clever fashion. This is similar to
698 adding an element to a set, but less general. */
701 constructor_callback (info, constructor, name, abfd, section, value)
702 struct bfd_link_info *info;
711 struct bfd_link_hash_entry *h;
713 if (! config.build_constructors)
716 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
717 useful error message. */
718 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL)
719 einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported");
721 set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__");
723 if (bfd_get_symbol_leading_char (abfd) != '\0')
724 *s++ = bfd_get_symbol_leading_char (abfd);
726 strcpy (s, "__CTOR_LIST__");
728 strcpy (s, "__DTOR_LIST__");
730 if (config.map_file != (FILE *) NULL)
731 fprintf (config.map_file,
732 "Adding %s to constructor/destructor set %s\n", name, set_name);
734 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
735 if (h == (struct bfd_link_hash_entry *) NULL)
736 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
737 if (h->type == bfd_link_hash_new)
739 h->type = bfd_link_hash_undefined;
740 h->u.undef.abfd = abfd;
741 /* We don't call bfd_link_add_undef to add this to the list of
742 undefined symbols because we are going to define it
746 ldctor_add_set_entry (h, BFD_RELOC_CTOR, section, value);
750 /* This is called when there is a reference to a warning symbol. */
754 warning_callback (info, warning)
755 struct bfd_link_info *info;
758 einfo ("%P: %s\n", warning);
762 /* This is called when an undefined symbol is found. */
766 undefined_symbol (info, name, abfd, section, address)
767 struct bfd_link_info *info;
773 static char *error_name;
774 static unsigned int error_count;
776 #define MAX_ERRORS_IN_A_ROW 5
778 /* We never print more than a reasonable number of errors in a row
779 for a single symbol. */
780 if (error_name != (char *) NULL
781 && strcmp (name, error_name) == 0)
786 if (error_name != (char *) NULL)
788 error_name = buystring (name);
791 if (error_count < MAX_ERRORS_IN_A_ROW)
792 einfo ("%X%C: undefined reference to `%T'\n",
793 abfd, section, address, name);
794 else if (error_count == MAX_ERRORS_IN_A_ROW)
795 einfo ("%C: more undefined references to `%T' follow\n",
796 abfd, section, address, name);
801 /* This is called when a reloc overflows. */
805 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
806 struct bfd_link_info *info;
808 const char *reloc_name;
814 if (abfd == (bfd *) NULL)
815 einfo ("%P%X: generated");
817 einfo ("%X%C:", abfd, section, address);
818 einfo (" relocation truncated to fit: %s %T", reloc_name, name);
820 einfo ("+%v", addend);
825 /* This is called when a dangerous relocation is made. */
829 reloc_dangerous (info, message, abfd, section, address)
830 struct bfd_link_info *info;
836 if (abfd == (bfd *) NULL)
837 einfo ("%P%X: generated");
839 einfo ("%X%C:", abfd, section, address);
840 einfo ("dangerous relocation: %s\n", message);
844 /* This is called when a reloc is being generated attached to a symbol
845 that is not being output. */
849 unattached_reloc (info, name, abfd, section, address)
850 struct bfd_link_info *info;
856 if (abfd == (bfd *) NULL)
857 einfo ("%P%X: generated");
859 einfo ("%X%C:", abfd, section, address);
860 einfo (" reloc refers to symbol `%T' which is not being output\n", name);
864 /* This is called when a symbol in notice_hash is found. Symbols are
865 put in notice_hash using the -y option. */
869 notice_ysym (info, name, abfd, section, value)
870 struct bfd_link_info *info;
876 einfo ("%B: %s %s\n", abfd,
877 section != &bfd_und_section ? "definition of" : "reference to",