1 /* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
4 This file is part of GLD, the Gnu Linker.
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
39 /* Somewhere above, sys/stat.h got included . . . . */
40 #if !defined(S_ISDIR) && defined(S_IFDIR)
41 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
46 static char *get_emulation PARAMS ((int, char **));
47 static void set_scripts_dir PARAMS ((void));
52 const char *output_filename = "a.out";
54 /* Name this program was invoked by. */
57 /* The file that we're creating */
60 /* Set by -G argument, for MIPS ECOFF target. */
61 int g_switch_value = 8;
63 /* Nonzero means print names of input files as processed. */
66 /* Nonzero means same, but note open failures, too. */
67 boolean trace_file_tries;
69 /* 1 => write load map. */
72 args_type command_line;
74 ld_config_type config;
76 static boolean check_for_scripts_dir PARAMS ((char *dir));
77 static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
79 static boolean multiple_definition PARAMS ((struct bfd_link_info *,
81 bfd *, asection *, bfd_vma,
82 bfd *, asection *, bfd_vma));
83 static boolean multiple_common PARAMS ((struct bfd_link_info *,
85 enum bfd_link_hash_type, bfd_vma,
86 bfd *, enum bfd_link_hash_type,
88 static boolean add_to_set PARAMS ((struct bfd_link_info *,
89 struct bfd_link_hash_entry *,
91 bfd *, asection *, bfd_vma));
92 static boolean constructor_callback PARAMS ((struct bfd_link_info *,
96 bfd *, asection *, bfd_vma));
97 static boolean warning_callback PARAMS ((struct bfd_link_info *,
99 static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
101 asection *, bfd_vma));
102 static boolean reloc_overflow PARAMS ((struct bfd_link_info *, bfd *,
103 asection *, bfd_vma));
104 static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
105 bfd *, asection *, bfd_vma));
106 static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
107 const char *, bfd *, asection *,
109 static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
110 bfd *, asection *, bfd_vma));
112 static struct bfd_link_callbacks link_callbacks =
118 constructor_callback,
127 struct bfd_link_info link_info;
129 extern int main PARAMS ((int, char **));
138 program_name = argv[0];
142 /* Initialize the data about options. */
143 trace_files = trace_file_tries = false;
145 config.build_constructors = true;
146 command_line.force_common_definition = false;
148 link_info.callbacks = &link_callbacks;
149 link_info.relocateable = false;
150 link_info.strip = strip_none;
151 link_info.discard = discard_none;
152 link_info.lprefix_len = 1;
153 link_info.lprefix = "L";
154 link_info.keep_memory = true;
155 link_info.input_bfds = NULL;
156 link_info.create_object_symbols_section = NULL;
157 link_info.hash = NULL;
158 link_info.keep_hash = NULL;
159 link_info.notice_hash = NULL;
161 ldfile_add_arch ("");
163 config.make_executable = true;
164 force_make_executable = false;
165 config.magic_demand_paged = true;
166 config.text_read_only = true;
167 config.make_executable = true;
169 emulation = get_emulation (argc, argv);
170 ldemul_choose_mode (emulation);
171 default_target = ldemul_choose_target ();
173 ldemul_before_parse ();
174 lang_has_input_file = false;
175 parse_args (argc, argv);
177 /* This essentially adds another -L directory so this must be done after
178 the -L's in argv have been processed. */
181 if (had_script == false)
183 /* Read the emulation's appropriate default script. */
185 char *s = ldemul_get_script (&isfile);
189 /* sizeof counts the terminating NUL. */
190 size_t size = strlen (s) + sizeof ("-T ");
191 char *buf = (char *) ldmalloc(size);
192 sprintf (buf, "-T %s", s);
200 if (link_info.relocateable && command_line.relax)
202 einfo ("%P%F: -relax and -r may not be used together\n");
206 if (lang_has_input_file == false)
208 einfo ("%P%F: no input files\n");
213 info_msg ("%P: mode %s\n", emulation);
216 ldemul_after_parse ();
219 if (config.map_filename)
221 if (strcmp (config.map_filename, "-") == 0)
223 config.map_file = stdout;
227 config.map_file = fopen (config.map_filename, FOPEN_WT);
228 if (config.map_file == (FILE *) NULL)
230 einfo ("%P%F: cannot open map file %s: %E\n",
231 config.map_filename);
239 /* Print error messages for any missing symbols, for any warning
240 symbols, and possibly multiple definitions */
243 if (config.text_read_only)
245 /* Look for a text section and mark the readonly attribute in it */
246 asection *found = bfd_get_section_by_name (output_bfd, ".text");
248 if (found != (asection *) NULL)
250 found->flags |= SEC_READONLY;
254 if (link_info.relocateable)
255 output_bfd->flags &= ~EXEC_P;
257 output_bfd->flags |= EXEC_P;
261 /* Even if we're producing relocateable output, some non-fatal errors should
262 be reported in the exit status. (What non-fatal errors, if any, do we
263 want to ignore for relocateable output?) */
265 if (config.make_executable == false && force_make_executable == false)
267 if (trace_files == true)
269 einfo ("%P: link errors found, deleting executable `%s'\n",
273 if (output_bfd->iostream)
274 fclose ((FILE *) (output_bfd->iostream));
276 unlink (output_filename);
281 bfd_close (output_bfd);
286 extern char **environ;
287 char *lim = (char *) sbrk (0);
289 fprintf (stderr, "%s: data size %ld\n", program_name,
290 (long) (lim - (char *) &environ));
297 /* We need to find any explicitly given emulation in order to initialize the
298 state that's needed by the lex&yacc argument parser (parse_args). */
301 get_emulation (argc, argv)
308 emulation = (char *) getenv (EMULATION_ENVIRON);
309 if (emulation == NULL)
310 emulation = DEFAULT_EMULATION;
312 for (i = 1; i < argc; i++)
314 if (!strncmp (argv[i], "-m", 2))
316 if (argv[i][2] == '\0')
321 emulation = argv[i + 1];
326 einfo("%P%F: missing argument to -m\n");
329 else if (strcmp (argv[i], "-mips1") == 0
330 || strcmp (argv[i], "-mips2") == 0
331 || strcmp (argv[i], "-mips3") == 0)
333 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
334 passed to the linker by some MIPS compilers. They
335 generally tell the linker to use a slightly different
336 library path. Perhaps someday these should be
337 implemented as emulations; until then, we just ignore
338 the arguments and hope that nobody ever creates
339 emulations named ips1, ips2 or ips3. */
344 emulation = &argv[i][2];
352 /* If directory DIR contains an "ldscripts" subdirectory,
353 add DIR to the library search path and return true,
354 else return false. */
357 check_for_scripts_dir (dir)
365 dirlen = strlen (dir);
366 /* sizeof counts the terminating NUL. */
367 buf = (char *) ldmalloc (dirlen + sizeof("/ldscripts"));
368 sprintf (buf, "%s/ldscripts", dir);
370 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
373 ldfile_add_library_path (dir);
377 /* Set the default directory for finding script files.
378 Libraries will be searched for here too, but that's ok.
379 We look for the "ldscripts" directory in:
381 SCRIPTDIR (passed from Makefile)
382 the dir where this program is (for using it from the build tree)
383 the dir where this program is/../lib (for installing the tool suite elsewhere) */
391 if (check_for_scripts_dir (SCRIPTDIR))
392 return; /* We've been installed normally. */
394 /* Look for "ldscripts" in the dir where our binary is. */
395 end = strrchr (program_name, '/');
398 dirlen = end - program_name;
399 /* Make a copy of program_name in dir.
400 Leave room for later "/../lib". */
401 dir = (char *) ldmalloc (dirlen + 8);
402 strncpy (dir, program_name, dirlen);
408 dir = (char *) ldmalloc (dirlen + 8);
412 if (check_for_scripts_dir (dir))
413 return; /* Don't free dir. */
415 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
416 strcpy (dir + dirlen, "/../lib");
417 if (check_for_scripts_dir (dir))
420 free (dir); /* Well, we tried. */
427 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
429 link_info.notice_hash = ((struct bfd_hash_table *)
430 ldmalloc (sizeof (struct bfd_hash_table)));
431 if (! bfd_hash_table_init_n (link_info.notice_hash,
434 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
437 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
438 == (struct bfd_hash_entry *) NULL)
439 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
442 /* Handle the -retain-symbols-file option. */
445 add_keepsyms_file (filename)
446 const char *filename;
453 if (link_info.strip == strip_some)
454 einfo ("%X%P: error: duplicate retain-symbols-file\n");
456 file = fopen (filename, "r");
457 if (file == (FILE *) NULL)
459 bfd_error = system_call_error;
460 einfo ("%X%P: %s: %E", filename);
464 link_info.keep_hash = ((struct bfd_hash_table *)
465 ldmalloc (sizeof (struct bfd_hash_table)));
466 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
467 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
470 buf = (char *) ldmalloc (bufsize);
482 while (! isspace (c) && c != EOF)
489 buf = ldrealloc (buf, bufsize);
496 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
497 == (struct bfd_hash_entry *) NULL)
498 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
502 if (link_info.strip != strip_none)
503 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
505 link_info.strip = strip_some;
508 /* Callbacks from the BFD linker routines. */
510 /* This is called when BFD has decided to include an archive member in
515 add_archive_element (info, abfd, name)
516 struct bfd_link_info *info;
520 lang_input_statement_type *input;
522 input = ((lang_input_statement_type *)
523 ldmalloc ((bfd_size_type) sizeof (lang_input_statement_type)));
524 input->filename = abfd->filename;
525 input->local_sym_name = abfd->filename;
526 input->the_bfd = abfd;
527 input->asymbols = NULL;
528 input->subfiles = NULL;
530 input->just_syms_flag = false;
531 input->loaded = false;
534 /* FIXME: This is wrong. It should point to an entry for the
535 archive itself. However, it doesn't seem to matter. */
536 input->superfile = NULL;
538 /* FIXME: The following fields are not set: header.next,
539 header.type, closed, passive_position, symbol_count, total_size,
540 next_real_file, is_archive, search_dirs_flag, target, real,
541 common_section, common_output_section, complained. This bit of
542 code is from the old decode_library_subfile function. I don't
543 know whether any of those fields matters. */
545 ldlang_add_file (input);
548 info_msg ("%s needed due to %T\n", abfd->filename, name);
553 /* This is called when BFD has discovered a symbol which is defined
558 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
559 struct bfd_link_info *info;
568 einfo ("%X%C: multiple definition of `%T'\n",
569 nbfd, nsec, nval, name);
570 if (obfd != (bfd *) NULL)
571 einfo ("%C: first defined here\n", obfd, osec, oval);
575 /* This is called when there is a definition of a common symbol, or
576 when a common symbol is found for a symbol that is already defined,
577 or when two common symbols are found. We only do something if
578 -warn-common was used. */
582 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
583 struct bfd_link_info *info;
586 enum bfd_link_hash_type otype;
589 enum bfd_link_hash_type ntype;
592 if (! config.warn_common)
595 if (ntype == bfd_link_hash_defined)
597 ASSERT (otype == bfd_link_hash_common);
598 einfo ("%B: warning: definition of `%T' overriding common\n",
600 einfo ("%B: warning: common is here\n", obfd);
602 else if (otype == bfd_link_hash_defined)
604 ASSERT (ntype == bfd_link_hash_common);
605 einfo ("%B: warning: common of `%T' overridden by definition\n",
607 einfo ("%B: warning: defined here\n", obfd);
611 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
614 einfo ("%B: warning: common of `%T' overridden by larger common\n",
616 einfo ("%B: warning: larger common is here\n", obfd);
618 else if (nsize > osize)
620 einfo ("%B: warning: common of `%T' overriding smaller common\n",
622 einfo ("%B: warning: smaller common is here\n", obfd);
626 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
627 einfo ("%B: warning: previous common is here\n", obfd);
634 /* This is called when BFD has discovered a set element. H is the
635 entry in the linker hash table for the set. SECTION and VALUE
636 represent a value which should be added to the set. */
640 add_to_set (info, h, bitsize, abfd, section, value)
641 struct bfd_link_info *info;
642 struct bfd_link_hash_entry *h;
643 unsigned int bitsize;
648 ldctor_add_set_entry (h, bitsize, section, value);
652 /* This is called when BFD has discovered a constructor. This is only
653 called for some object file formats--those which do not handle
654 constructors in some more clever fashion. This is similar to
655 adding an element to a set, but less general. */
658 constructor_callback (info, constructor, bitsize, name, abfd, section, value)
659 struct bfd_link_info *info;
661 unsigned int bitsize;
669 struct bfd_link_hash_entry *h;
671 if (! config.build_constructors)
674 set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__");
676 if (bfd_get_symbol_leading_char (abfd) != '\0')
677 *s++ = bfd_get_symbol_leading_char (abfd);
679 strcpy (s, "__CTOR_LIST__");
681 strcpy (s, "__DTOR_LIST__");
684 info_msg ("Adding %s to constructor/destructor set %s\n", name, set_name);
686 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
687 if (h == (struct bfd_link_hash_entry *) NULL)
688 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
689 if (h->type == bfd_link_hash_new)
691 h->type = bfd_link_hash_undefined;
692 h->u.undef.abfd = abfd;
693 /* We don't call bfd_link_add_undef to add this to the list of
694 undefined symbols because we are going to define it
698 ldctor_add_set_entry (h, bitsize, section, value);
702 /* This is called when there is a reference to a warning symbol. */
706 warning_callback (info, warning)
707 struct bfd_link_info *info;
710 einfo ("%P: %s\n", warning);
714 /* This is called when an undefined symbol is found. */
718 undefined_symbol (info, name, abfd, section, address)
719 struct bfd_link_info *info;
725 static char *error_name;
726 static unsigned int error_count;
728 #define MAX_ERRORS_IN_A_ROW 5
730 /* We never print more than a reasonable number of errors in a row
731 for a single symbol. */
732 if (error_name != (char *) NULL
733 && strcmp (name, error_name) == 0)
738 if (error_name != (char *) NULL)
740 error_name = buystring (name);
743 if (error_count < MAX_ERRORS_IN_A_ROW)
744 einfo ("%X%C: undefined reference to `%T'\n",
745 abfd, section, address, name);
746 else if (error_count == MAX_ERRORS_IN_A_ROW)
747 einfo ("%C: more undefined references to `%T' follow\n",
748 abfd, section, address, name);
753 /* This is called when a reloc overflows. */
757 reloc_overflow (info, abfd, section, address)
758 struct bfd_link_info *info;
763 einfo ("%X%C: relocation truncated to fit\n", abfd, section, address);
767 /* This is called when a dangerous relocation is made. */
771 reloc_dangerous (info, message, abfd, section, address)
772 struct bfd_link_info *info;
778 einfo ("%X%C: dangerous relocation: %s\n", abfd, section, address, message);
782 /* This is called when a reloc is being generated attached to a symbol
783 that is not being output. */
787 unattached_reloc (info, name, abfd, section, address)
788 struct bfd_link_info *info;
794 einfo ("%X%C: reloc refers to symbol `%T' which is not being output\n",
795 abfd, section, address, name);
799 /* This is called when a symbol in notice_hash is found. Symbols are
800 put in notice_hash using the -y option. */
804 notice_ysym (info, name, abfd, section, value)
805 struct bfd_link_info *info;
811 einfo ("%B: %s %s\n", abfd,
812 section != &bfd_und_section ? "definition of" : "reference to",