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 /* 1 => write load map. */
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 *,
97 bfd *, asection *, bfd_vma));
98 static boolean constructor_callback PARAMS ((struct bfd_link_info *,
100 unsigned int bitsize,
102 bfd *, asection *, bfd_vma));
103 static boolean warning_callback PARAMS ((struct bfd_link_info *,
105 static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
107 asection *, bfd_vma));
108 static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
109 const char *, bfd_vma,
110 bfd *, asection *, bfd_vma));
111 static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
112 bfd *, asection *, bfd_vma));
113 static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
114 const char *, bfd *, asection *,
116 static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
117 bfd *, asection *, bfd_vma));
119 static struct bfd_link_callbacks link_callbacks =
125 constructor_callback,
134 struct bfd_link_info link_info;
141 if (output_bfd && output_bfd->iostream)
142 fclose((FILE *)(output_bfd->iostream));
143 if (delete_output_file_on_failure)
144 unlink (output_filename);
154 long start_time = get_run_time ();
156 program_name = argv[0];
157 xmalloc_set_program_name (program_name);
161 xatexit (remove_output);
163 /* Initialize the data about options. */
164 trace_files = trace_file_tries = version_printed = false;
166 config.build_constructors = true;
167 command_line.force_common_definition = false;
169 link_info.callbacks = &link_callbacks;
170 link_info.relocateable = false;
171 link_info.strip = strip_none;
172 link_info.discard = discard_none;
173 link_info.lprefix_len = 1;
174 link_info.lprefix = "L";
175 link_info.keep_memory = true;
176 link_info.input_bfds = NULL;
177 link_info.create_object_symbols_section = NULL;
178 link_info.hash = NULL;
179 link_info.keep_hash = NULL;
180 link_info.notice_hash = NULL;
182 ldfile_add_arch ("");
184 config.make_executable = true;
185 force_make_executable = false;
186 config.magic_demand_paged = true;
187 config.text_read_only = true;
188 config.make_executable = true;
190 emulation = get_emulation (argc, argv);
191 ldemul_choose_mode (emulation);
192 default_target = ldemul_choose_target ();
194 ldemul_before_parse ();
195 lang_has_input_file = false;
196 parse_args (argc, argv);
198 /* This essentially adds another -L directory so this must be done after
199 the -L's in argv have been processed. */
202 if (had_script == false)
204 /* Read the emulation's appropriate default script. */
206 char *s = ldemul_get_script (&isfile);
209 ldfile_open_command_file (s);
212 parser_input = input_script;
216 if (link_info.relocateable && command_line.relax)
218 einfo ("%P%F: -relax and -r may not be used together\n");
222 if (lang_has_input_file == false)
226 einfo ("%P%F: no input files\n");
231 info_msg ("%P: mode %s\n", emulation);
234 ldemul_after_parse ();
237 if (config.map_filename)
239 if (strcmp (config.map_filename, "-") == 0)
241 config.map_file = stdout;
245 config.map_file = fopen (config.map_filename, FOPEN_WT);
246 if (config.map_file == (FILE *) NULL)
248 einfo ("%P%F: cannot open map file %s: %E\n",
249 config.map_filename);
257 /* Print error messages for any missing symbols, for any warning
258 symbols, and possibly multiple definitions */
261 if (config.text_read_only)
263 /* Look for a text section and mark the readonly attribute in it */
264 asection *found = bfd_get_section_by_name (output_bfd, ".text");
266 if (found != (asection *) NULL)
268 found->flags |= SEC_READONLY;
272 if (link_info.relocateable)
273 output_bfd->flags &= ~EXEC_P;
275 output_bfd->flags |= EXEC_P;
279 /* Even if we're producing relocateable output, some non-fatal errors should
280 be reported in the exit status. (What non-fatal errors, if any, do we
281 want to ignore for relocateable output?) */
283 if (config.make_executable == false && force_make_executable == false)
285 if (trace_files == true)
287 einfo ("%P: link errors found, deleting executable `%s'\n",
291 if (output_bfd->iostream)
292 fclose ((FILE *) (output_bfd->iostream));
294 unlink (output_filename);
299 bfd_close (output_bfd);
304 extern char **environ;
305 char *lim = (char *) sbrk (0);
306 long run_time = get_run_time () - start_time;
308 fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
309 program_name, run_time / 1000000, run_time % 1000000);
310 fprintf (stderr, "%s: data size %ld\n", program_name,
311 (long) (lim - (char *) &environ));
314 /* Prevent remove_output from doing anything, after a successful link. */
315 output_filename = NULL;
321 /* We need to find any explicitly given emulation in order to initialize the
322 state that's needed by the lex&yacc argument parser (parse_args). */
325 get_emulation (argc, argv)
332 emulation = (char *) getenv (EMULATION_ENVIRON);
333 if (emulation == NULL)
334 emulation = DEFAULT_EMULATION;
336 for (i = 1; i < argc; i++)
338 if (!strncmp (argv[i], "-m", 2))
340 if (argv[i][2] == '\0')
345 emulation = argv[i + 1];
350 einfo("%P%F: missing argument to -m\n");
353 else if (strcmp (argv[i], "-mips1") == 0
354 || strcmp (argv[i], "-mips2") == 0
355 || strcmp (argv[i], "-mips3") == 0)
357 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
358 passed to the linker by some MIPS compilers. They
359 generally tell the linker to use a slightly different
360 library path. Perhaps someday these should be
361 implemented as emulations; until then, we just ignore
362 the arguments and hope that nobody ever creates
363 emulations named ips1, ips2 or ips3. */
368 emulation = &argv[i][2];
376 /* If directory DIR contains an "ldscripts" subdirectory,
377 add DIR to the library search path and return true,
378 else return false. */
381 check_for_scripts_dir (dir)
389 dirlen = strlen (dir);
390 /* sizeof counts the terminating NUL. */
391 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
392 sprintf (buf, "%s/ldscripts", dir);
394 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
397 ldfile_add_library_path (dir);
401 /* Set the default directory for finding script files.
402 Libraries will be searched for here too, but that's ok.
403 We look for the "ldscripts" directory in:
405 SCRIPTDIR (passed from Makefile)
406 the dir where this program is (for using it from the build tree)
407 the dir where this program is/../lib (for installing the tool suite elsewhere) */
415 if (check_for_scripts_dir (SCRIPTDIR))
416 return; /* We've been installed normally. */
418 /* Look for "ldscripts" in the dir where our binary is. */
419 end = strrchr (program_name, '/');
422 dirlen = end - program_name;
423 /* Make a copy of program_name in dir.
424 Leave room for later "/../lib". */
425 dir = (char *) xmalloc (dirlen + 8);
426 strncpy (dir, program_name, dirlen);
432 dir = (char *) xmalloc (dirlen + 8);
436 if (check_for_scripts_dir (dir))
437 return; /* Don't free dir. */
439 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
440 strcpy (dir + dirlen, "/../lib");
441 if (check_for_scripts_dir (dir))
444 free (dir); /* Well, we tried. */
451 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
453 link_info.notice_hash = ((struct bfd_hash_table *)
454 xmalloc (sizeof (struct bfd_hash_table)));
455 if (! bfd_hash_table_init_n (link_info.notice_hash,
458 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
461 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
462 == (struct bfd_hash_entry *) NULL)
463 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
466 /* Handle the -retain-symbols-file option. */
469 add_keepsyms_file (filename)
470 const char *filename;
477 if (link_info.strip == strip_some)
478 einfo ("%X%P: error: duplicate retain-symbols-file\n");
480 file = fopen (filename, "r");
481 if (file == (FILE *) NULL)
483 bfd_set_error (bfd_error_system_call);
484 einfo ("%X%P: %s: %E", filename);
488 link_info.keep_hash = ((struct bfd_hash_table *)
489 xmalloc (sizeof (struct bfd_hash_table)));
490 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
491 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
494 buf = (char *) xmalloc (bufsize);
506 while (! isspace (c) && c != EOF)
513 buf = xrealloc (buf, bufsize);
520 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
521 == (struct bfd_hash_entry *) NULL)
522 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
526 if (link_info.strip != strip_none)
527 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
529 link_info.strip = strip_some;
532 /* Callbacks from the BFD linker routines. */
534 /* This is called when BFD has decided to include an archive member in
539 add_archive_element (info, abfd, name)
540 struct bfd_link_info *info;
544 lang_input_statement_type *input;
546 input = ((lang_input_statement_type *)
547 xmalloc ((bfd_size_type) sizeof (lang_input_statement_type)));
548 input->filename = abfd->filename;
549 input->local_sym_name = abfd->filename;
550 input->the_bfd = abfd;
551 input->asymbols = NULL;
552 input->subfiles = NULL;
554 input->just_syms_flag = false;
555 input->loaded = false;
558 /* FIXME: This is wrong. It should point to an entry for the
559 archive itself. However, it doesn't seem to matter. */
560 input->superfile = NULL;
562 /* FIXME: The following fields are not set: header.next,
563 header.type, closed, passive_position, symbol_count, total_size,
564 next_real_file, is_archive, search_dirs_flag, target, real,
565 common_section, common_output_section, complained. This bit of
566 code is from the old decode_library_subfile function. I don't
567 know whether any of those fields matters. */
569 ldlang_add_file (input);
572 info_msg ("%s needed due to %T\n", abfd->filename, name);
574 if (trace_files || trace_file_tries)
575 info_msg ("%I\n", input);
580 /* This is called when BFD has discovered a symbol which is defined
585 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
586 struct bfd_link_info *info;
595 einfo ("%X%C: multiple definition of `%T'\n",
596 nbfd, nsec, nval, name);
597 if (obfd != (bfd *) NULL)
598 einfo ("%C: first defined here\n", obfd, osec, oval);
602 /* This is called when there is a definition of a common symbol, or
603 when a common symbol is found for a symbol that is already defined,
604 or when two common symbols are found. We only do something if
605 -warn-common was used. */
609 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
610 struct bfd_link_info *info;
613 enum bfd_link_hash_type otype;
616 enum bfd_link_hash_type ntype;
619 if (! config.warn_common)
622 if (ntype == bfd_link_hash_defined)
624 ASSERT (otype == bfd_link_hash_common);
625 einfo ("%B: warning: definition of `%T' overriding common\n",
627 einfo ("%B: warning: common is here\n", obfd);
629 else if (otype == bfd_link_hash_defined)
631 ASSERT (ntype == bfd_link_hash_common);
632 einfo ("%B: warning: common of `%T' overridden by definition\n",
634 einfo ("%B: warning: defined here\n", obfd);
638 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
641 einfo ("%B: warning: common of `%T' overridden by larger common\n",
643 einfo ("%B: warning: larger common is here\n", obfd);
645 else if (nsize > osize)
647 einfo ("%B: warning: common of `%T' overriding smaller common\n",
649 einfo ("%B: warning: smaller common is here\n", obfd);
653 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
654 einfo ("%B: warning: previous common is here\n", obfd);
661 /* This is called when BFD has discovered a set element. H is the
662 entry in the linker hash table for the set. SECTION and VALUE
663 represent a value which should be added to the set. */
667 add_to_set (info, h, bitsize, abfd, section, value)
668 struct bfd_link_info *info;
669 struct bfd_link_hash_entry *h;
670 unsigned int bitsize;
675 ldctor_add_set_entry (h, bitsize, section, value);
679 /* This is called when BFD has discovered a constructor. This is only
680 called for some object file formats--those which do not handle
681 constructors in some more clever fashion. This is similar to
682 adding an element to a set, but less general. */
685 constructor_callback (info, constructor, bitsize, name, abfd, section, value)
686 struct bfd_link_info *info;
688 unsigned int bitsize;
696 struct bfd_link_hash_entry *h;
698 if (! config.build_constructors)
701 set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__");
703 if (bfd_get_symbol_leading_char (abfd) != '\0')
704 *s++ = bfd_get_symbol_leading_char (abfd);
706 strcpy (s, "__CTOR_LIST__");
708 strcpy (s, "__DTOR_LIST__");
711 info_msg ("Adding %s to constructor/destructor set %s\n", name, set_name);
713 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
714 if (h == (struct bfd_link_hash_entry *) NULL)
715 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
716 if (h->type == bfd_link_hash_new)
718 h->type = bfd_link_hash_undefined;
719 h->u.undef.abfd = abfd;
720 /* We don't call bfd_link_add_undef to add this to the list of
721 undefined symbols because we are going to define it
725 ldctor_add_set_entry (h, bitsize, section, value);
729 /* This is called when there is a reference to a warning symbol. */
733 warning_callback (info, warning)
734 struct bfd_link_info *info;
737 einfo ("%P: %s\n", warning);
741 /* This is called when an undefined symbol is found. */
745 undefined_symbol (info, name, abfd, section, address)
746 struct bfd_link_info *info;
752 static char *error_name;
753 static unsigned int error_count;
755 #define MAX_ERRORS_IN_A_ROW 5
757 /* We never print more than a reasonable number of errors in a row
758 for a single symbol. */
759 if (error_name != (char *) NULL
760 && strcmp (name, error_name) == 0)
765 if (error_name != (char *) NULL)
767 error_name = buystring (name);
770 if (error_count < MAX_ERRORS_IN_A_ROW)
771 einfo ("%X%C: undefined reference to `%T'\n",
772 abfd, section, address, name);
773 else if (error_count == MAX_ERRORS_IN_A_ROW)
774 einfo ("%C: more undefined references to `%T' follow\n",
775 abfd, section, address, name);
780 /* This is called when a reloc overflows. */
784 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
785 struct bfd_link_info *info;
787 const char *reloc_name;
793 einfo ("%X%C: relocation truncated to fit: %s %T", abfd, section,
794 address, reloc_name, name);
796 einfo ("+%v", addend);
801 /* This is called when a dangerous relocation is made. */
805 reloc_dangerous (info, message, abfd, section, address)
806 struct bfd_link_info *info;
812 einfo ("%X%C: dangerous relocation: %s\n", abfd, section, address, message);
816 /* This is called when a reloc is being generated attached to a symbol
817 that is not being output. */
821 unattached_reloc (info, name, abfd, section, address)
822 struct bfd_link_info *info;
828 einfo ("%X%C: reloc refers to symbol `%T' which is not being output\n",
829 abfd, section, address, name);
833 /* This is called when a symbol in notice_hash is found. Symbols are
834 put in notice_hash using the -y option. */
838 notice_ysym (info, name, abfd, section, value)
839 struct bfd_link_info *info;
845 einfo ("%B: %s %s\n", abfd,
846 section != &bfd_und_section ? "definition of" : "reference to",