1 /* Main program of GNU linker.
2 Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
3 Written by Steve Chamberlain steve@cygnus.com
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
.traditional_format
= false;
162 config
.build_constructors
= true;
163 config
.dynamic_link
= false;
164 command_line
.force_common_definition
= false;
165 command_line
.interpreter
= NULL
;
167 link_info
.callbacks
= &link_callbacks
;
168 link_info
.relocateable
= false;
169 link_info
.strip
= strip_none
;
170 link_info
.discard
= discard_none
;
171 link_info
.lprefix_len
= 1;
172 link_info
.lprefix
= "L";
173 link_info
.keep_memory
= true;
174 link_info
.input_bfds
= NULL
;
175 link_info
.create_object_symbols_section
= NULL
;
176 link_info
.hash
= NULL
;
177 link_info
.keep_hash
= NULL
;
178 link_info
.notice_hash
= NULL
;
180 ldfile_add_arch ("");
182 config
.make_executable
= true;
183 force_make_executable
= false;
184 config
.magic_demand_paged
= true;
185 config
.text_read_only
= true;
186 config
.make_executable
= true;
188 emulation
= get_emulation (argc
, argv
);
189 ldemul_choose_mode (emulation
);
190 default_target
= ldemul_choose_target ();
192 ldemul_before_parse ();
193 lang_has_input_file
= false;
194 parse_args (argc
, argv
);
196 if (link_info
.relocateable
)
198 if (command_line
.relax
)
199 einfo ("%P%F: -relax and -r may not be used together\n");
200 if (config
.dynamic_link
)
201 einfo ("%P%F: -r and -call_shared may not be used together\n");
202 if (link_info
.strip
== strip_all
)
203 einfo ("%P%F: -r and -s may not be used together\n");
206 /* This essentially adds another -L directory so this must be done after
207 the -L's in argv have been processed. */
210 if (had_script
== false)
212 /* Read the emulation's appropriate default script. */
214 char *s
= ldemul_get_script (&isfile
);
217 ldfile_open_command_file (s
);
220 parser_input
= input_script
;
226 if (lang_has_input_file
== false)
230 einfo ("%P%F: no input files\n");
235 info_msg ("%P: mode %s\n", emulation
);
238 ldemul_after_parse ();
241 if (config
.map_filename
)
243 if (strcmp (config
.map_filename
, "-") == 0)
245 config
.map_file
= stdout
;
249 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
250 if (config
.map_file
== (FILE *) NULL
)
252 einfo ("%P%F: cannot open map file %s: %E\n",
253 config
.map_filename
);
261 /* Print error messages for any missing symbols, for any warning
262 symbols, and possibly multiple definitions */
265 if (config
.text_read_only
)
267 /* Look for a text section and mark the readonly attribute in it */
268 asection
*found
= bfd_get_section_by_name (output_bfd
, ".text");
270 if (found
!= (asection
*) NULL
)
272 found
->flags
|= SEC_READONLY
;
276 if (link_info
.relocateable
)
277 output_bfd
->flags
&= ~EXEC_P
;
279 output_bfd
->flags
|= EXEC_P
;
283 /* Even if we're producing relocateable output, some non-fatal errors should
284 be reported in the exit status. (What non-fatal errors, if any, do we
285 want to ignore for relocateable output?) */
287 if (config
.make_executable
== false && force_make_executable
== false)
289 if (trace_files
== true)
291 einfo ("%P: link errors found, deleting executable `%s'\n",
295 if (output_bfd
->iostream
)
296 fclose ((FILE *) (output_bfd
->iostream
));
298 unlink (output_filename
);
303 if (! bfd_close (output_bfd
))
304 einfo ("%F%B: final close failed: %E\n", output_bfd
);
309 extern char **environ
;
310 char *lim
= (char *) sbrk (0);
311 long run_time
= get_run_time () - start_time
;
313 fprintf (stderr
, "%s: total time in link: %ld.%06ld\n",
314 program_name
, run_time
/ 1000000, run_time
% 1000000);
315 fprintf (stderr
, "%s: data size %ld\n", program_name
,
316 (long) (lim
- (char *) &environ
));
319 /* Prevent remove_output from doing anything, after a successful link. */
320 output_filename
= NULL
;
326 /* We need to find any explicitly given emulation in order to initialize the
327 state that's needed by the lex&yacc argument parser (parse_args). */
330 get_emulation (argc
, argv
)
337 emulation
= (char *) getenv (EMULATION_ENVIRON
);
338 if (emulation
== NULL
)
339 emulation
= DEFAULT_EMULATION
;
341 for (i
= 1; i
< argc
; i
++)
343 if (!strncmp (argv
[i
], "-m", 2))
345 if (argv
[i
][2] == '\0')
350 emulation
= argv
[i
+ 1];
355 einfo("%P%F: missing argument to -m\n");
358 else if (strcmp (argv
[i
], "-mips1") == 0
359 || strcmp (argv
[i
], "-mips2") == 0
360 || strcmp (argv
[i
], "-mips3") == 0)
362 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
363 passed to the linker by some MIPS compilers. They
364 generally tell the linker to use a slightly different
365 library path. Perhaps someday these should be
366 implemented as emulations; until then, we just ignore
367 the arguments and hope that nobody ever creates
368 emulations named ips1, ips2 or ips3. */
373 emulation
= &argv
[i
][2];
381 /* If directory DIR contains an "ldscripts" subdirectory,
382 add DIR to the library search path and return true,
383 else return false. */
386 check_for_scripts_dir (dir
)
394 dirlen
= strlen (dir
);
395 /* sizeof counts the terminating NUL. */
396 buf
= (char *) xmalloc (dirlen
+ sizeof("/ldscripts"));
397 sprintf (buf
, "%s/ldscripts", dir
);
399 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
402 ldfile_add_library_path (dir
, false);
406 /* Set the default directory for finding script files.
407 Libraries will be searched for here too, but that's ok.
408 We look for the "ldscripts" directory in:
410 SCRIPTDIR (passed from Makefile)
411 the dir where this program is (for using it from the build tree)
412 the dir where this program is/../lib (for installing the tool suite elsewhere) */
420 if (check_for_scripts_dir (SCRIPTDIR
))
421 return; /* We've been installed normally. */
423 /* Look for "ldscripts" in the dir where our binary is. */
424 end
= strrchr (program_name
, '/');
427 dirlen
= end
- program_name
;
428 /* Make a copy of program_name in dir.
429 Leave room for later "/../lib". */
430 dir
= (char *) xmalloc (dirlen
+ 8);
431 strncpy (dir
, program_name
, dirlen
);
437 dir
= (char *) xmalloc (dirlen
+ 8);
441 if (check_for_scripts_dir (dir
))
442 return; /* Don't free dir. */
444 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
445 strcpy (dir
+ dirlen
, "/../lib");
446 if (check_for_scripts_dir (dir
))
449 free (dir
); /* Well, we tried. */
456 if (link_info
.notice_hash
== (struct bfd_hash_table
*) NULL
)
458 link_info
.notice_hash
= ((struct bfd_hash_table
*)
459 xmalloc (sizeof (struct bfd_hash_table
)));
460 if (! bfd_hash_table_init_n (link_info
.notice_hash
,
463 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
466 if (bfd_hash_lookup (link_info
.notice_hash
, name
, true, true)
467 == (struct bfd_hash_entry
*) NULL
)
468 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
471 /* Handle the -retain-symbols-file option. */
474 add_keepsyms_file (filename
)
475 const char *filename
;
482 if (link_info
.strip
== strip_some
)
483 einfo ("%X%P: error: duplicate retain-symbols-file\n");
485 file
= fopen (filename
, "r");
486 if (file
== (FILE *) NULL
)
488 bfd_set_error (bfd_error_system_call
);
489 einfo ("%X%P: %s: %E", filename
);
493 link_info
.keep_hash
= ((struct bfd_hash_table
*)
494 xmalloc (sizeof (struct bfd_hash_table
)));
495 if (! bfd_hash_table_init (link_info
.keep_hash
, bfd_hash_newfunc
))
496 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
499 buf
= (char *) xmalloc (bufsize
);
511 while (! isspace (c
) && c
!= EOF
)
518 buf
= xrealloc (buf
, bufsize
);
525 if (bfd_hash_lookup (link_info
.keep_hash
, buf
, true, true)
526 == (struct bfd_hash_entry
*) NULL
)
527 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
531 if (link_info
.strip
!= strip_none
)
532 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
534 link_info
.strip
= strip_some
;
537 /* Callbacks from the BFD linker routines. */
539 /* This is called when BFD has decided to include an archive member in
544 add_archive_element (info
, abfd
, name
)
545 struct bfd_link_info
*info
;
549 lang_input_statement_type
*input
;
551 input
= ((lang_input_statement_type
*)
552 xmalloc ((bfd_size_type
) sizeof (lang_input_statement_type
)));
553 input
->filename
= abfd
->filename
;
554 input
->local_sym_name
= abfd
->filename
;
555 input
->the_bfd
= abfd
;
556 input
->asymbols
= NULL
;
558 input
->just_syms_flag
= false;
559 input
->loaded
= false;
560 input
->search_dirs_flag
= false;
562 /* FIXME: The following fields are not set: header.next,
563 header.type, closed, passive_position, symbol_count,
564 next_real_file, is_archive, target, real, common_section,
565 common_output_section, complained. This bit of code is from the
566 old decode_library_subfile function. I don't know whether any of
567 those fields matters. */
569 ldlang_add_file (input
);
571 if (config
.map_file
!= (FILE *) NULL
)
572 minfo ("%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
, reloc
, abfd
, section
, value
)
668 struct bfd_link_info
*info
;
669 struct bfd_link_hash_entry
*h
;
670 bfd_reloc_code_real_type reloc
;
675 if (! config
.build_constructors
)
678 ldctor_add_set_entry (h
, reloc
, section
, value
);
680 if (h
->type
== bfd_link_hash_new
)
682 h
->type
= bfd_link_hash_undefined
;
683 h
->u
.undef
.abfd
= abfd
;
684 /* We don't call bfd_link_add_undef to add this to the list of
685 undefined symbols because we are going to define it
692 /* This is called when BFD has discovered a constructor. This is only
693 called for some object file formats--those which do not handle
694 constructors in some more clever fashion. This is similar to
695 adding an element to a set, but less general. */
698 constructor_callback (info
, constructor
, name
, abfd
, section
, value
)
699 struct bfd_link_info
*info
;
708 struct bfd_link_hash_entry
*h
;
710 if (! config
.build_constructors
)
713 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
714 useful error message. */
715 if (bfd_reloc_type_lookup (output_bfd
, BFD_RELOC_CTOR
) == NULL
)
716 einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported");
718 set_name
= (char *) alloca (1 + sizeof "__CTOR_LIST__");
720 if (bfd_get_symbol_leading_char (abfd
) != '\0')
721 *s
++ = bfd_get_symbol_leading_char (abfd
);
723 strcpy (s
, "__CTOR_LIST__");
725 strcpy (s
, "__DTOR_LIST__");
727 if (config
.map_file
!= (FILE *) NULL
)
728 fprintf (config
.map_file
,
729 "Adding %s to constructor/destructor set %s\n", name
, set_name
);
731 h
= bfd_link_hash_lookup (info
->hash
, set_name
, true, true, true);
732 if (h
== (struct bfd_link_hash_entry
*) NULL
)
733 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
734 if (h
->type
== bfd_link_hash_new
)
736 h
->type
= bfd_link_hash_undefined
;
737 h
->u
.undef
.abfd
= abfd
;
738 /* We don't call bfd_link_add_undef to add this to the list of
739 undefined symbols because we are going to define it
743 ldctor_add_set_entry (h
, BFD_RELOC_CTOR
, section
, value
);
747 /* This is called when there is a reference to a warning symbol. */
751 warning_callback (info
, warning
)
752 struct bfd_link_info
*info
;
755 einfo ("%P: %s\n", warning
);
759 /* This is called when an undefined symbol is found. */
763 undefined_symbol (info
, name
, abfd
, section
, address
)
764 struct bfd_link_info
*info
;
770 static char *error_name
;
771 static unsigned int error_count
;
773 #define MAX_ERRORS_IN_A_ROW 5
775 /* We never print more than a reasonable number of errors in a row
776 for a single symbol. */
777 if (error_name
!= (char *) NULL
778 && strcmp (name
, error_name
) == 0)
783 if (error_name
!= (char *) NULL
)
785 error_name
= buystring (name
);
788 if (error_count
< MAX_ERRORS_IN_A_ROW
)
789 einfo ("%X%C: undefined reference to `%T'\n",
790 abfd
, section
, address
, name
);
791 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
792 einfo ("%C: more undefined references to `%T' follow\n",
793 abfd
, section
, address
, name
);
798 /* This is called when a reloc overflows. */
802 reloc_overflow (info
, name
, reloc_name
, addend
, abfd
, section
, address
)
803 struct bfd_link_info
*info
;
805 const char *reloc_name
;
811 if (abfd
== (bfd
*) NULL
)
812 einfo ("%P%X: generated");
814 einfo ("%X%C:", abfd
, section
, address
);
815 einfo (" relocation truncated to fit: %s %T", reloc_name
, name
);
817 einfo ("+%v", addend
);
822 /* This is called when a dangerous relocation is made. */
826 reloc_dangerous (info
, message
, abfd
, section
, address
)
827 struct bfd_link_info
*info
;
833 if (abfd
== (bfd
*) NULL
)
834 einfo ("%P%X: generated");
836 einfo ("%X%C:", abfd
, section
, address
);
837 einfo ("dangerous relocation: %s\n", message
);
841 /* This is called when a reloc is being generated attached to a symbol
842 that is not being output. */
846 unattached_reloc (info
, name
, abfd
, section
, address
)
847 struct bfd_link_info
*info
;
853 if (abfd
== (bfd
*) NULL
)
854 einfo ("%P%X: generated");
856 einfo ("%X%C:", abfd
, section
, address
);
857 einfo (" reloc refers to symbol `%T' which is not being output\n", name
);
861 /* This is called when a symbol in notice_hash is found. Symbols are
862 put in notice_hash using the -y option. */
866 notice_ysym (info
, name
, abfd
, section
, value
)
867 struct bfd_link_info
*info
;
873 einfo ("%B: %s %s\n", abfd
,
874 section
!= &bfd_und_section
? "definition of" : "reference to",