1 /* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
2 Written by Steve Chamberlain steve@cygnus.com
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 *, const char *,
103 const char *, bfd_vma,
104 bfd *, asection *, bfd_vma));
105 static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
106 bfd *, asection *, bfd_vma));
107 static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
108 const char *, bfd *, asection *,
110 static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
111 bfd *, asection *, bfd_vma));
113 static struct bfd_link_callbacks link_callbacks =
119 constructor_callback,
128 struct bfd_link_info link_info;
130 extern int main PARAMS ((int, char **));
139 program_name = argv[0];
143 /* Initialize the data about options. */
144 trace_files = trace_file_tries = false;
146 config.build_constructors = true;
147 command_line.force_common_definition = false;
149 link_info.callbacks = &link_callbacks;
150 link_info.relocateable = false;
151 link_info.strip = strip_none;
152 link_info.discard = discard_none;
153 link_info.lprefix_len = 1;
154 link_info.lprefix = "L";
155 link_info.keep_memory = true;
156 link_info.input_bfds = NULL;
157 link_info.create_object_symbols_section = NULL;
158 link_info.hash = NULL;
159 link_info.keep_hash = NULL;
160 link_info.notice_hash = NULL;
162 ldfile_add_arch ("");
164 config.make_executable = true;
165 force_make_executable = false;
166 config.magic_demand_paged = true;
167 config.text_read_only = true;
168 config.make_executable = true;
170 emulation = get_emulation (argc, argv);
171 ldemul_choose_mode (emulation);
172 default_target = ldemul_choose_target ();
174 ldemul_before_parse ();
175 lang_has_input_file = false;
176 parse_args (argc, argv);
178 /* This essentially adds another -L directory so this must be done after
179 the -L's in argv have been processed. */
182 if (had_script == false)
184 /* Read the emulation's appropriate default script. */
186 char *s = ldemul_get_script (&isfile);
190 /* sizeof counts the terminating NUL. */
191 size_t size = strlen (s) + sizeof ("-T ");
192 char *buf = (char *) ldmalloc(size);
193 sprintf (buf, "-T %s", s);
201 if (link_info.relocateable && command_line.relax)
203 einfo ("%P%F: -relax and -r may not be used together\n");
207 if (lang_has_input_file == false)
209 einfo ("%P%F: no input files\n");
214 info_msg ("%P: mode %s\n", emulation);
217 ldemul_after_parse ();
220 if (config.map_filename)
222 if (strcmp (config.map_filename, "-") == 0)
224 config.map_file = stdout;
228 config.map_file = fopen (config.map_filename, FOPEN_WT);
229 if (config.map_file == (FILE *) NULL)
231 einfo ("%P%F: cannot open map file %s: %E\n",
232 config.map_filename);
240 /* Print error messages for any missing symbols, for any warning
241 symbols, and possibly multiple definitions */
244 if (config.text_read_only)
246 /* Look for a text section and mark the readonly attribute in it */
247 asection *found = bfd_get_section_by_name (output_bfd, ".text");
249 if (found != (asection *) NULL)
251 found->flags |= SEC_READONLY;
255 if (link_info.relocateable)
256 output_bfd->flags &= ~EXEC_P;
258 output_bfd->flags |= EXEC_P;
262 /* Even if we're producing relocateable output, some non-fatal errors should
263 be reported in the exit status. (What non-fatal errors, if any, do we
264 want to ignore for relocateable output?) */
266 if (config.make_executable == false && force_make_executable == false)
268 if (trace_files == true)
270 einfo ("%P: link errors found, deleting executable `%s'\n",
274 if (output_bfd->iostream)
275 fclose ((FILE *) (output_bfd->iostream));
277 unlink (output_filename);
282 bfd_close (output_bfd);
287 extern char **environ;
288 char *lim = (char *) sbrk (0);
290 fprintf (stderr, "%s: data size %ld\n", program_name,
291 (long) (lim - (char *) &environ));
298 /* We need to find any explicitly given emulation in order to initialize the
299 state that's needed by the lex&yacc argument parser (parse_args). */
302 get_emulation (argc, argv)
309 emulation = (char *) getenv (EMULATION_ENVIRON);
310 if (emulation == NULL)
311 emulation = DEFAULT_EMULATION;
313 for (i = 1; i < argc; i++)
315 if (!strncmp (argv[i], "-m", 2))
317 if (argv[i][2] == '\0')
322 emulation = argv[i + 1];
327 einfo("%P%F: missing argument to -m\n");
330 else if (strcmp (argv[i], "-mips1") == 0
331 || strcmp (argv[i], "-mips2") == 0
332 || strcmp (argv[i], "-mips3") == 0)
334 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
335 passed to the linker by some MIPS compilers. They
336 generally tell the linker to use a slightly different
337 library path. Perhaps someday these should be
338 implemented as emulations; until then, we just ignore
339 the arguments and hope that nobody ever creates
340 emulations named ips1, ips2 or ips3. */
345 emulation = &argv[i][2];
353 /* If directory DIR contains an "ldscripts" subdirectory,
354 add DIR to the library search path and return true,
355 else return false. */
358 check_for_scripts_dir (dir)
366 dirlen = strlen (dir);
367 /* sizeof counts the terminating NUL. */
368 buf = (char *) ldmalloc (dirlen + sizeof("/ldscripts"));
369 sprintf (buf, "%s/ldscripts", dir);
371 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
374 ldfile_add_library_path (dir);
378 /* Set the default directory for finding script files.
379 Libraries will be searched for here too, but that's ok.
380 We look for the "ldscripts" directory in:
382 SCRIPTDIR (passed from Makefile)
383 the dir where this program is (for using it from the build tree)
384 the dir where this program is/../lib (for installing the tool suite elsewhere) */
392 if (check_for_scripts_dir (SCRIPTDIR))
393 return; /* We've been installed normally. */
395 /* Look for "ldscripts" in the dir where our binary is. */
396 end = strrchr (program_name, '/');
399 dirlen = end - program_name;
400 /* Make a copy of program_name in dir.
401 Leave room for later "/../lib". */
402 dir = (char *) ldmalloc (dirlen + 8);
403 strncpy (dir, program_name, dirlen);
409 dir = (char *) ldmalloc (dirlen + 8);
413 if (check_for_scripts_dir (dir))
414 return; /* Don't free dir. */
416 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
417 strcpy (dir + dirlen, "/../lib");
418 if (check_for_scripts_dir (dir))
421 free (dir); /* Well, we tried. */
428 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
430 link_info.notice_hash = ((struct bfd_hash_table *)
431 ldmalloc (sizeof (struct bfd_hash_table)));
432 if (! bfd_hash_table_init_n (link_info.notice_hash,
435 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
438 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
439 == (struct bfd_hash_entry *) NULL)
440 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
443 /* Handle the -retain-symbols-file option. */
446 add_keepsyms_file (filename)
447 const char *filename;
454 if (link_info.strip == strip_some)
455 einfo ("%X%P: error: duplicate retain-symbols-file\n");
457 file = fopen (filename, "r");
458 if (file == (FILE *) NULL)
460 bfd_error = system_call_error;
461 einfo ("%X%P: %s: %E", filename);
465 link_info.keep_hash = ((struct bfd_hash_table *)
466 ldmalloc (sizeof (struct bfd_hash_table)));
467 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
468 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
471 buf = (char *) ldmalloc (bufsize);
483 while (! isspace (c) && c != EOF)
490 buf = ldrealloc (buf, bufsize);
497 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
498 == (struct bfd_hash_entry *) NULL)
499 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
503 if (link_info.strip != strip_none)
504 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
506 link_info.strip = strip_some;
509 /* Callbacks from the BFD linker routines. */
511 /* This is called when BFD has decided to include an archive member in
516 add_archive_element (info, abfd, name)
517 struct bfd_link_info *info;
521 lang_input_statement_type *input;
523 input = ((lang_input_statement_type *)
524 ldmalloc ((bfd_size_type) sizeof (lang_input_statement_type)));
525 input->filename = abfd->filename;
526 input->local_sym_name = abfd->filename;
527 input->the_bfd = abfd;
528 input->asymbols = NULL;
529 input->subfiles = NULL;
531 input->just_syms_flag = false;
532 input->loaded = false;
535 /* FIXME: This is wrong. It should point to an entry for the
536 archive itself. However, it doesn't seem to matter. */
537 input->superfile = NULL;
539 /* FIXME: The following fields are not set: header.next,
540 header.type, closed, passive_position, symbol_count, total_size,
541 next_real_file, is_archive, search_dirs_flag, target, real,
542 common_section, common_output_section, complained. This bit of
543 code is from the old decode_library_subfile function. I don't
544 know whether any of those fields matters. */
546 ldlang_add_file (input);
549 info_msg ("%s needed due to %T\n", abfd->filename, name);
551 if (trace_files || trace_file_tries)
552 info_msg ("%I\n", input);
557 /* This is called when BFD has discovered a symbol which is defined
562 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
563 struct bfd_link_info *info;
572 einfo ("%X%C: multiple definition of `%T'\n",
573 nbfd, nsec, nval, name);
574 if (obfd != (bfd *) NULL)
575 einfo ("%C: first defined here\n", obfd, osec, oval);
579 /* This is called when there is a definition of a common symbol, or
580 when a common symbol is found for a symbol that is already defined,
581 or when two common symbols are found. We only do something if
582 -warn-common was used. */
586 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
587 struct bfd_link_info *info;
590 enum bfd_link_hash_type otype;
593 enum bfd_link_hash_type ntype;
596 if (! config.warn_common)
599 if (ntype == bfd_link_hash_defined)
601 ASSERT (otype == bfd_link_hash_common);
602 einfo ("%B: warning: definition of `%T' overriding common\n",
604 einfo ("%B: warning: common is here\n", obfd);
606 else if (otype == bfd_link_hash_defined)
608 ASSERT (ntype == bfd_link_hash_common);
609 einfo ("%B: warning: common of `%T' overridden by definition\n",
611 einfo ("%B: warning: defined here\n", obfd);
615 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
618 einfo ("%B: warning: common of `%T' overridden by larger common\n",
620 einfo ("%B: warning: larger common is here\n", obfd);
622 else if (nsize > osize)
624 einfo ("%B: warning: common of `%T' overriding smaller common\n",
626 einfo ("%B: warning: smaller common is here\n", obfd);
630 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
631 einfo ("%B: warning: previous common is here\n", obfd);
638 /* This is called when BFD has discovered a set element. H is the
639 entry in the linker hash table for the set. SECTION and VALUE
640 represent a value which should be added to the set. */
644 add_to_set (info, h, bitsize, abfd, section, value)
645 struct bfd_link_info *info;
646 struct bfd_link_hash_entry *h;
647 unsigned int bitsize;
652 ldctor_add_set_entry (h, bitsize, section, value);
656 /* This is called when BFD has discovered a constructor. This is only
657 called for some object file formats--those which do not handle
658 constructors in some more clever fashion. This is similar to
659 adding an element to a set, but less general. */
662 constructor_callback (info, constructor, bitsize, name, abfd, section, value)
663 struct bfd_link_info *info;
665 unsigned int bitsize;
673 struct bfd_link_hash_entry *h;
675 if (! config.build_constructors)
678 set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__");
680 if (bfd_get_symbol_leading_char (abfd) != '\0')
681 *s++ = bfd_get_symbol_leading_char (abfd);
683 strcpy (s, "__CTOR_LIST__");
685 strcpy (s, "__DTOR_LIST__");
688 info_msg ("Adding %s to constructor/destructor set %s\n", name, set_name);
690 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
691 if (h == (struct bfd_link_hash_entry *) NULL)
692 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
693 if (h->type == bfd_link_hash_new)
695 h->type = bfd_link_hash_undefined;
696 h->u.undef.abfd = abfd;
697 /* We don't call bfd_link_add_undef to add this to the list of
698 undefined symbols because we are going to define it
702 ldctor_add_set_entry (h, bitsize, section, value);
706 /* This is called when there is a reference to a warning symbol. */
710 warning_callback (info, warning)
711 struct bfd_link_info *info;
714 einfo ("%P: %s\n", warning);
718 /* This is called when an undefined symbol is found. */
722 undefined_symbol (info, name, abfd, section, address)
723 struct bfd_link_info *info;
729 static char *error_name;
730 static unsigned int error_count;
732 #define MAX_ERRORS_IN_A_ROW 5
734 /* We never print more than a reasonable number of errors in a row
735 for a single symbol. */
736 if (error_name != (char *) NULL
737 && strcmp (name, error_name) == 0)
742 if (error_name != (char *) NULL)
744 error_name = buystring (name);
747 if (error_count < MAX_ERRORS_IN_A_ROW)
748 einfo ("%X%C: undefined reference to `%T'\n",
749 abfd, section, address, name);
750 else if (error_count == MAX_ERRORS_IN_A_ROW)
751 einfo ("%C: more undefined references to `%T' follow\n",
752 abfd, section, address, name);
757 /* This is called when a reloc overflows. */
761 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
762 struct bfd_link_info *info;
764 const char *reloc_name;
770 einfo ("%X%C: relocation truncated to fit: %s %T", abfd, section,
771 address, reloc_name, name);
773 einfo ("+%v", addend);
778 /* This is called when a dangerous relocation is made. */
782 reloc_dangerous (info, message, abfd, section, address)
783 struct bfd_link_info *info;
789 einfo ("%X%C: dangerous relocation: %s\n", abfd, section, address, message);
793 /* This is called when a reloc is being generated attached to a symbol
794 that is not being output. */
798 unattached_reloc (info, name, abfd, section, address)
799 struct bfd_link_info *info;
805 einfo ("%X%C: reloc refers to symbol `%T' which is not being output\n",
806 abfd, section, address, name);
810 /* This is called when a symbol in notice_hash is found. Symbols are
811 put in notice_hash using the -y option. */
815 notice_ysym (info, name, abfd, section, value)
816 struct bfd_link_info *info;
822 einfo ("%B: %s %s\n", abfd,
823 section != &bfd_und_section ? "definition of" : "reference to",