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. */
40 /* Somewhere above, sys/stat.h got included . . . . */
41 #if !defined(S_ISDIR) && defined(S_IFDIR)
42 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
47 static char *get_emulation PARAMS ((int, char **));
48 static void set_scripts_dir PARAMS ((void));
53 const char *output_filename = "a.out";
55 /* Name this program was invoked by. */
58 /* The file that we're creating */
61 /* Set by -G argument, for MIPS ECOFF target. */
62 int g_switch_value = 8;
64 /* Nonzero means print names of input files as processed. */
67 /* Nonzero means same, but note open failures, too. */
68 boolean trace_file_tries;
70 /* Nonzero means version number was printed, so exit successfully
71 instead of complaining if no input files are given. */
72 boolean version_printed;
74 /* 1 => write load map. */
77 args_type command_line;
79 ld_config_type config;
81 static boolean check_for_scripts_dir PARAMS ((char *dir));
82 static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
84 static boolean multiple_definition PARAMS ((struct bfd_link_info *,
86 bfd *, asection *, bfd_vma,
87 bfd *, asection *, bfd_vma));
88 static boolean multiple_common PARAMS ((struct bfd_link_info *,
90 enum bfd_link_hash_type, bfd_vma,
91 bfd *, enum bfd_link_hash_type,
93 static boolean add_to_set PARAMS ((struct bfd_link_info *,
94 struct bfd_link_hash_entry *,
96 bfd *, asection *, bfd_vma));
97 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);
147 /* Prevent remove_output from doing anything.
148 Called after a successful link. */
153 output_filename = NULL;
162 long start_time = get_run_time ();
164 program_name = argv[0];
168 atexit (remove_output);
170 /* Initialize the data about options. */
171 trace_files = trace_file_tries = version_printed = false;
173 config.build_constructors = true;
174 command_line.force_common_definition = false;
176 link_info.callbacks = &link_callbacks;
177 link_info.relocateable = 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 /* This essentially adds another -L directory so this must be done after
206 the -L's in argv have been processed. */
209 if (had_script == false)
211 /* Read the emulation's appropriate default script. */
213 char *s = ldemul_get_script (&isfile);
217 /* sizeof counts the terminating NUL. */
218 size_t size = strlen (s) + sizeof ("-T ");
219 char *buf = (char *) xmalloc(size);
220 sprintf (buf, "-T %s", s);
228 if (link_info.relocateable && command_line.relax)
230 einfo ("%P%F: -relax and -r may not be used together\n");
234 if (lang_has_input_file == false)
238 einfo ("%P%F: no input files\n");
243 info_msg ("%P: mode %s\n", emulation);
246 ldemul_after_parse ();
249 if (config.map_filename)
251 if (strcmp (config.map_filename, "-") == 0)
253 config.map_file = stdout;
257 config.map_file = fopen (config.map_filename, FOPEN_WT);
258 if (config.map_file == (FILE *) NULL)
260 einfo ("%P%F: cannot open map file %s: %E\n",
261 config.map_filename);
269 /* Print error messages for any missing symbols, for any warning
270 symbols, and possibly multiple definitions */
273 if (config.text_read_only)
275 /* Look for a text section and mark the readonly attribute in it */
276 asection *found = bfd_get_section_by_name (output_bfd, ".text");
278 if (found != (asection *) NULL)
280 found->flags |= SEC_READONLY;
284 if (link_info.relocateable)
285 output_bfd->flags &= ~EXEC_P;
287 output_bfd->flags |= EXEC_P;
291 /* Even if we're producing relocateable output, some non-fatal errors should
292 be reported in the exit status. (What non-fatal errors, if any, do we
293 want to ignore for relocateable output?) */
295 if (config.make_executable == false && force_make_executable == false)
297 if (trace_files == true)
299 einfo ("%P: link errors found, deleting executable `%s'\n",
303 if (output_bfd->iostream)
304 fclose ((FILE *) (output_bfd->iostream));
306 unlink (output_filename);
311 bfd_close (output_bfd);
316 extern char **environ;
317 char *lim = (char *) sbrk (0);
318 long run_time = get_run_time () - start_time;
320 fprintf (stderr, "%s: total time in link: %d.%06d\n",
321 program_name, run_time / 1000000, run_time % 1000000);
322 fprintf (stderr, "%s: data size %ld\n", program_name,
323 (long) (lim - (char *) &environ));
326 atexit (preserve_output);
331 /* We need to find any explicitly given emulation in order to initialize the
332 state that's needed by the lex&yacc argument parser (parse_args). */
335 get_emulation (argc, argv)
342 emulation = (char *) getenv (EMULATION_ENVIRON);
343 if (emulation == NULL)
344 emulation = DEFAULT_EMULATION;
346 for (i = 1; i < argc; i++)
348 if (!strncmp (argv[i], "-m", 2))
350 if (argv[i][2] == '\0')
355 emulation = argv[i + 1];
360 einfo("%P%F: missing argument to -m\n");
363 else if (strcmp (argv[i], "-mips1") == 0
364 || strcmp (argv[i], "-mips2") == 0
365 || strcmp (argv[i], "-mips3") == 0)
367 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
368 passed to the linker by some MIPS compilers. They
369 generally tell the linker to use a slightly different
370 library path. Perhaps someday these should be
371 implemented as emulations; until then, we just ignore
372 the arguments and hope that nobody ever creates
373 emulations named ips1, ips2 or ips3. */
378 emulation = &argv[i][2];
386 /* If directory DIR contains an "ldscripts" subdirectory,
387 add DIR to the library search path and return true,
388 else return false. */
391 check_for_scripts_dir (dir)
399 dirlen = strlen (dir);
400 /* sizeof counts the terminating NUL. */
401 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
402 sprintf (buf, "%s/ldscripts", dir);
404 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
407 ldfile_add_library_path (dir);
411 /* Set the default directory for finding script files.
412 Libraries will be searched for here too, but that's ok.
413 We look for the "ldscripts" directory in:
415 SCRIPTDIR (passed from Makefile)
416 the dir where this program is (for using it from the build tree)
417 the dir where this program is/../lib (for installing the tool suite elsewhere) */
425 if (check_for_scripts_dir (SCRIPTDIR))
426 return; /* We've been installed normally. */
428 /* Look for "ldscripts" in the dir where our binary is. */
429 end = strrchr (program_name, '/');
432 dirlen = end - program_name;
433 /* Make a copy of program_name in dir.
434 Leave room for later "/../lib". */
435 dir = (char *) xmalloc (dirlen + 8);
436 strncpy (dir, program_name, dirlen);
442 dir = (char *) xmalloc (dirlen + 8);
446 if (check_for_scripts_dir (dir))
447 return; /* Don't free dir. */
449 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
450 strcpy (dir + dirlen, "/../lib");
451 if (check_for_scripts_dir (dir))
454 free (dir); /* Well, we tried. */
461 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
463 link_info.notice_hash = ((struct bfd_hash_table *)
464 xmalloc (sizeof (struct bfd_hash_table)));
465 if (! bfd_hash_table_init_n (link_info.notice_hash,
468 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
471 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
472 == (struct bfd_hash_entry *) NULL)
473 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
476 /* Handle the -retain-symbols-file option. */
479 add_keepsyms_file (filename)
480 const char *filename;
487 if (link_info.strip == strip_some)
488 einfo ("%X%P: error: duplicate retain-symbols-file\n");
490 file = fopen (filename, "r");
491 if (file == (FILE *) NULL)
493 bfd_error = system_call_error;
494 einfo ("%X%P: %s: %E", filename);
498 link_info.keep_hash = ((struct bfd_hash_table *)
499 xmalloc (sizeof (struct bfd_hash_table)));
500 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
501 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
504 buf = (char *) xmalloc (bufsize);
516 while (! isspace (c) && c != EOF)
523 buf = xrealloc (buf, bufsize);
530 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
531 == (struct bfd_hash_entry *) NULL)
532 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
536 if (link_info.strip != strip_none)
537 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
539 link_info.strip = strip_some;
542 /* Callbacks from the BFD linker routines. */
544 /* This is called when BFD has decided to include an archive member in
549 add_archive_element (info, abfd, name)
550 struct bfd_link_info *info;
554 lang_input_statement_type *input;
556 input = ((lang_input_statement_type *)
557 xmalloc ((bfd_size_type) sizeof (lang_input_statement_type)));
558 input->filename = abfd->filename;
559 input->local_sym_name = abfd->filename;
560 input->the_bfd = abfd;
561 input->asymbols = NULL;
562 input->subfiles = NULL;
564 input->just_syms_flag = false;
565 input->loaded = false;
568 /* FIXME: This is wrong. It should point to an entry for the
569 archive itself. However, it doesn't seem to matter. */
570 input->superfile = NULL;
572 /* FIXME: The following fields are not set: header.next,
573 header.type, closed, passive_position, symbol_count, total_size,
574 next_real_file, is_archive, search_dirs_flag, target, real,
575 common_section, common_output_section, complained. This bit of
576 code is from the old decode_library_subfile function. I don't
577 know whether any of those fields matters. */
579 ldlang_add_file (input);
582 info_msg ("%s needed due to %T\n", abfd->filename, name);
584 if (trace_files || trace_file_tries)
585 info_msg ("%I\n", input);
590 /* This is called when BFD has discovered a symbol which is defined
595 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
596 struct bfd_link_info *info;
605 einfo ("%X%C: multiple definition of `%T'\n",
606 nbfd, nsec, nval, name);
607 if (obfd != (bfd *) NULL)
608 einfo ("%C: first defined here\n", obfd, osec, oval);
612 /* This is called when there is a definition of a common symbol, or
613 when a common symbol is found for a symbol that is already defined,
614 or when two common symbols are found. We only do something if
615 -warn-common was used. */
619 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
620 struct bfd_link_info *info;
623 enum bfd_link_hash_type otype;
626 enum bfd_link_hash_type ntype;
629 if (! config.warn_common)
632 if (ntype == bfd_link_hash_defined)
634 ASSERT (otype == bfd_link_hash_common);
635 einfo ("%B: warning: definition of `%T' overriding common\n",
637 einfo ("%B: warning: common is here\n", obfd);
639 else if (otype == bfd_link_hash_defined)
641 ASSERT (ntype == bfd_link_hash_common);
642 einfo ("%B: warning: common of `%T' overridden by definition\n",
644 einfo ("%B: warning: defined here\n", obfd);
648 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
651 einfo ("%B: warning: common of `%T' overridden by larger common\n",
653 einfo ("%B: warning: larger common is here\n", obfd);
655 else if (nsize > osize)
657 einfo ("%B: warning: common of `%T' overriding smaller common\n",
659 einfo ("%B: warning: smaller common is here\n", obfd);
663 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
664 einfo ("%B: warning: previous common is here\n", obfd);
671 /* This is called when BFD has discovered a set element. H is the
672 entry in the linker hash table for the set. SECTION and VALUE
673 represent a value which should be added to the set. */
677 add_to_set (info, h, bitsize, abfd, section, value)
678 struct bfd_link_info *info;
679 struct bfd_link_hash_entry *h;
680 unsigned int bitsize;
685 ldctor_add_set_entry (h, bitsize, section, value);
689 /* This is called when BFD has discovered a constructor. This is only
690 called for some object file formats--those which do not handle
691 constructors in some more clever fashion. This is similar to
692 adding an element to a set, but less general. */
695 constructor_callback (info, constructor, bitsize, name, abfd, section, value)
696 struct bfd_link_info *info;
698 unsigned int bitsize;
706 struct bfd_link_hash_entry *h;
708 if (! config.build_constructors)
711 set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__");
713 if (bfd_get_symbol_leading_char (abfd) != '\0')
714 *s++ = bfd_get_symbol_leading_char (abfd);
716 strcpy (s, "__CTOR_LIST__");
718 strcpy (s, "__DTOR_LIST__");
721 info_msg ("Adding %s to constructor/destructor set %s\n", name, set_name);
723 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
724 if (h == (struct bfd_link_hash_entry *) NULL)
725 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
726 if (h->type == bfd_link_hash_new)
728 h->type = bfd_link_hash_undefined;
729 h->u.undef.abfd = abfd;
730 /* We don't call bfd_link_add_undef to add this to the list of
731 undefined symbols because we are going to define it
735 ldctor_add_set_entry (h, bitsize, section, value);
739 /* This is called when there is a reference to a warning symbol. */
743 warning_callback (info, warning)
744 struct bfd_link_info *info;
747 einfo ("%P: %s\n", warning);
751 /* This is called when an undefined symbol is found. */
755 undefined_symbol (info, name, abfd, section, address)
756 struct bfd_link_info *info;
762 static char *error_name;
763 static unsigned int error_count;
765 #define MAX_ERRORS_IN_A_ROW 5
767 /* We never print more than a reasonable number of errors in a row
768 for a single symbol. */
769 if (error_name != (char *) NULL
770 && strcmp (name, error_name) == 0)
775 if (error_name != (char *) NULL)
777 error_name = buystring (name);
780 if (error_count < MAX_ERRORS_IN_A_ROW)
781 einfo ("%X%C: undefined reference to `%T'\n",
782 abfd, section, address, name);
783 else if (error_count == MAX_ERRORS_IN_A_ROW)
784 einfo ("%C: more undefined references to `%T' follow\n",
785 abfd, section, address, name);
790 /* This is called when a reloc overflows. */
794 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
795 struct bfd_link_info *info;
797 const char *reloc_name;
803 einfo ("%X%C: relocation truncated to fit: %s %T", abfd, section,
804 address, reloc_name, name);
806 einfo ("+%v", addend);
811 /* This is called when a dangerous relocation is made. */
815 reloc_dangerous (info, message, abfd, section, address)
816 struct bfd_link_info *info;
822 einfo ("%X%C: dangerous relocation: %s\n", abfd, section, address, message);
826 /* This is called when a reloc is being generated attached to a symbol
827 that is not being output. */
831 unattached_reloc (info, name, abfd, section, address)
832 struct bfd_link_info *info;
838 einfo ("%X%C: reloc refers to symbol `%T' which is not being output\n",
839 abfd, section, address, name);
843 /* This is called when a symbol in notice_hash is found. Symbols are
844 put in notice_hash using the -y option. */
848 notice_ysym (info, name, abfd, section, value)
849 struct bfd_link_info *info;
855 einfo ("%B: %s %s\n", abfd,
856 section != &bfd_und_section ? "definition of" : "reference to",