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;
166 link_info
.callbacks
= &link_callbacks
;
167 link_info
.relocateable
= false;
168 link_info
.strip
= strip_none
;
169 link_info
.discard
= discard_none
;
170 link_info
.lprefix_len
= 1;
171 link_info
.lprefix
= "L";
172 link_info
.keep_memory
= true;
173 link_info
.input_bfds
= NULL
;
174 link_info
.create_object_symbols_section
= NULL
;
175 link_info
.hash
= NULL
;
176 link_info
.keep_hash
= NULL
;
177 link_info
.notice_hash
= NULL
;
179 ldfile_add_arch ("");
181 config
.make_executable
= true;
182 force_make_executable
= false;
183 config
.magic_demand_paged
= true;
184 config
.text_read_only
= true;
185 config
.make_executable
= true;
187 emulation
= get_emulation (argc
, argv
);
188 ldemul_choose_mode (emulation
);
189 default_target
= ldemul_choose_target ();
191 ldemul_before_parse ();
192 lang_has_input_file
= false;
193 parse_args (argc
, argv
);
195 if (link_info
.relocateable
)
197 if (command_line
.relax
)
198 einfo ("%P%F: -relax and -r may not be used together\n");
199 if (config
.dynamic_link
)
200 einfo ("%P%F: -r and -call_shared may not be used together\n");
201 if (link_info
.strip
== strip_all
)
202 einfo ("%P%F: -r and -s may not be used together\n");
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
);
216 ldfile_open_command_file (s
);
219 parser_input
= input_script
;
225 if (lang_has_input_file
== false)
229 einfo ("%P%F: no input files\n");
234 info_msg ("%P: mode %s\n", emulation
);
237 ldemul_after_parse ();
240 if (config
.map_filename
)
242 if (strcmp (config
.map_filename
, "-") == 0)
244 config
.map_file
= stdout
;
248 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
249 if (config
.map_file
== (FILE *) NULL
)
251 einfo ("%P%F: cannot open map file %s: %E\n",
252 config
.map_filename
);
260 /* Print error messages for any missing symbols, for any warning
261 symbols, and possibly multiple definitions */
264 if (config
.text_read_only
)
266 /* Look for a text section and mark the readonly attribute in it */
267 asection
*found
= bfd_get_section_by_name (output_bfd
, ".text");
269 if (found
!= (asection
*) NULL
)
271 found
->flags
|= SEC_READONLY
;
275 if (link_info
.relocateable
)
276 output_bfd
->flags
&= ~EXEC_P
;
278 output_bfd
->flags
|= EXEC_P
;
282 /* Even if we're producing relocateable output, some non-fatal errors should
283 be reported in the exit status. (What non-fatal errors, if any, do we
284 want to ignore for relocateable output?) */
286 if (config
.make_executable
== false && force_make_executable
== false)
288 if (trace_files
== true)
290 einfo ("%P: link errors found, deleting executable `%s'\n",
294 if (output_bfd
->iostream
)
295 fclose ((FILE *) (output_bfd
->iostream
));
297 unlink (output_filename
);
302 if (! bfd_close (output_bfd
))
303 einfo ("%F%B: final close failed: %E\n", output_bfd
);
308 extern char **environ
;
309 char *lim
= (char *) sbrk (0);
310 long run_time
= get_run_time () - start_time
;
312 fprintf (stderr
, "%s: total time in link: %ld.%06ld\n",
313 program_name
, run_time
/ 1000000, run_time
% 1000000);
314 fprintf (stderr
, "%s: data size %ld\n", program_name
,
315 (long) (lim
- (char *) &environ
));
318 /* Prevent remove_output from doing anything, after a successful link. */
319 output_filename
= NULL
;
325 /* We need to find any explicitly given emulation in order to initialize the
326 state that's needed by the lex&yacc argument parser (parse_args). */
329 get_emulation (argc
, argv
)
336 emulation
= (char *) getenv (EMULATION_ENVIRON
);
337 if (emulation
== NULL
)
338 emulation
= DEFAULT_EMULATION
;
340 for (i
= 1; i
< argc
; i
++)
342 if (!strncmp (argv
[i
], "-m", 2))
344 if (argv
[i
][2] == '\0')
349 emulation
= argv
[i
+ 1];
354 einfo("%P%F: missing argument to -m\n");
357 else if (strcmp (argv
[i
], "-mips1") == 0
358 || strcmp (argv
[i
], "-mips2") == 0
359 || strcmp (argv
[i
], "-mips3") == 0)
361 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
362 passed to the linker by some MIPS compilers. They
363 generally tell the linker to use a slightly different
364 library path. Perhaps someday these should be
365 implemented as emulations; until then, we just ignore
366 the arguments and hope that nobody ever creates
367 emulations named ips1, ips2 or ips3. */
372 emulation
= &argv
[i
][2];
380 /* If directory DIR contains an "ldscripts" subdirectory,
381 add DIR to the library search path and return true,
382 else return false. */
385 check_for_scripts_dir (dir
)
393 dirlen
= strlen (dir
);
394 /* sizeof counts the terminating NUL. */
395 buf
= (char *) xmalloc (dirlen
+ sizeof("/ldscripts"));
396 sprintf (buf
, "%s/ldscripts", dir
);
398 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
401 ldfile_add_library_path (dir
, false);
405 /* Set the default directory for finding script files.
406 Libraries will be searched for here too, but that's ok.
407 We look for the "ldscripts" directory in:
409 SCRIPTDIR (passed from Makefile)
410 the dir where this program is (for using it from the build tree)
411 the dir where this program is/../lib (for installing the tool suite elsewhere) */
419 if (check_for_scripts_dir (SCRIPTDIR
))
420 return; /* We've been installed normally. */
422 /* Look for "ldscripts" in the dir where our binary is. */
423 end
= strrchr (program_name
, '/');
426 dirlen
= end
- program_name
;
427 /* Make a copy of program_name in dir.
428 Leave room for later "/../lib". */
429 dir
= (char *) xmalloc (dirlen
+ 8);
430 strncpy (dir
, program_name
, dirlen
);
436 dir
= (char *) xmalloc (dirlen
+ 8);
440 if (check_for_scripts_dir (dir
))
441 return; /* Don't free dir. */
443 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
444 strcpy (dir
+ dirlen
, "/../lib");
445 if (check_for_scripts_dir (dir
))
448 free (dir
); /* Well, we tried. */
455 if (link_info
.notice_hash
== (struct bfd_hash_table
*) NULL
)
457 link_info
.notice_hash
= ((struct bfd_hash_table
*)
458 xmalloc (sizeof (struct bfd_hash_table
)));
459 if (! bfd_hash_table_init_n (link_info
.notice_hash
,
462 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
465 if (bfd_hash_lookup (link_info
.notice_hash
, name
, true, true)
466 == (struct bfd_hash_entry
*) NULL
)
467 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
470 /* Handle the -retain-symbols-file option. */
473 add_keepsyms_file (filename
)
474 const char *filename
;
481 if (link_info
.strip
== strip_some
)
482 einfo ("%X%P: error: duplicate retain-symbols-file\n");
484 file
= fopen (filename
, "r");
485 if (file
== (FILE *) NULL
)
487 bfd_set_error (bfd_error_system_call
);
488 einfo ("%X%P: %s: %E", filename
);
492 link_info
.keep_hash
= ((struct bfd_hash_table
*)
493 xmalloc (sizeof (struct bfd_hash_table
)));
494 if (! bfd_hash_table_init (link_info
.keep_hash
, bfd_hash_newfunc
))
495 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
498 buf
= (char *) xmalloc (bufsize
);
510 while (! isspace (c
) && c
!= EOF
)
517 buf
= xrealloc (buf
, bufsize
);
524 if (bfd_hash_lookup (link_info
.keep_hash
, buf
, true, true)
525 == (struct bfd_hash_entry
*) NULL
)
526 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
530 if (link_info
.strip
!= strip_none
)
531 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
533 link_info
.strip
= strip_some
;
536 /* Callbacks from the BFD linker routines. */
538 /* This is called when BFD has decided to include an archive member in
543 add_archive_element (info
, abfd
, name
)
544 struct bfd_link_info
*info
;
548 lang_input_statement_type
*input
;
550 input
= ((lang_input_statement_type
*)
551 xmalloc ((bfd_size_type
) sizeof (lang_input_statement_type
)));
552 input
->filename
= abfd
->filename
;
553 input
->local_sym_name
= abfd
->filename
;
554 input
->the_bfd
= abfd
;
555 input
->asymbols
= NULL
;
557 input
->just_syms_flag
= false;
558 input
->loaded
= false;
559 input
->search_dirs_flag
= false;
561 /* FIXME: The following fields are not set: header.next,
562 header.type, closed, passive_position, symbol_count,
563 next_real_file, is_archive, target, real, common_section,
564 common_output_section, complained. This bit of code is from the
565 old decode_library_subfile function. I don't know whether any of
566 those fields matters. */
568 ldlang_add_file (input
);
570 if (config
.map_file
!= (FILE *) NULL
)
571 minfo ("%s needed due to %T\n", abfd
->filename
, name
);
573 if (trace_files
|| trace_file_tries
)
574 info_msg ("%I\n", input
);
579 /* This is called when BFD has discovered a symbol which is defined
584 multiple_definition (info
, name
, obfd
, osec
, oval
, nbfd
, nsec
, nval
)
585 struct bfd_link_info
*info
;
594 einfo ("%X%C: multiple definition of `%T'\n",
595 nbfd
, nsec
, nval
, name
);
596 if (obfd
!= (bfd
*) NULL
)
597 einfo ("%C: first defined here\n", obfd
, osec
, oval
);
601 /* This is called when there is a definition of a common symbol, or
602 when a common symbol is found for a symbol that is already defined,
603 or when two common symbols are found. We only do something if
604 -warn-common was used. */
608 multiple_common (info
, name
, obfd
, otype
, osize
, nbfd
, ntype
, nsize
)
609 struct bfd_link_info
*info
;
612 enum bfd_link_hash_type otype
;
615 enum bfd_link_hash_type ntype
;
618 if (! config
.warn_common
)
621 if (ntype
== bfd_link_hash_defined
)
623 ASSERT (otype
== bfd_link_hash_common
);
624 einfo ("%B: warning: definition of `%T' overriding common\n",
626 einfo ("%B: warning: common is here\n", obfd
);
628 else if (otype
== bfd_link_hash_defined
)
630 ASSERT (ntype
== bfd_link_hash_common
);
631 einfo ("%B: warning: common of `%T' overridden by definition\n",
633 einfo ("%B: warning: defined here\n", obfd
);
637 ASSERT (otype
== bfd_link_hash_common
&& ntype
== bfd_link_hash_common
);
640 einfo ("%B: warning: common of `%T' overridden by larger common\n",
642 einfo ("%B: warning: larger common is here\n", obfd
);
644 else if (nsize
> osize
)
646 einfo ("%B: warning: common of `%T' overriding smaller common\n",
648 einfo ("%B: warning: smaller common is here\n", obfd
);
652 einfo ("%B: warning: multiple common of `%T'\n", nbfd
, name
);
653 einfo ("%B: warning: previous common is here\n", obfd
);
660 /* This is called when BFD has discovered a set element. H is the
661 entry in the linker hash table for the set. SECTION and VALUE
662 represent a value which should be added to the set. */
666 add_to_set (info
, h
, reloc
, abfd
, section
, value
)
667 struct bfd_link_info
*info
;
668 struct bfd_link_hash_entry
*h
;
669 bfd_reloc_code_real_type reloc
;
674 if (! config
.build_constructors
)
677 ldctor_add_set_entry (h
, reloc
, section
, value
);
679 if (h
->type
== bfd_link_hash_new
)
681 h
->type
= bfd_link_hash_undefined
;
682 h
->u
.undef
.abfd
= abfd
;
683 /* We don't call bfd_link_add_undef to add this to the list of
684 undefined symbols because we are going to define it
691 /* This is called when BFD has discovered a constructor. This is only
692 called for some object file formats--those which do not handle
693 constructors in some more clever fashion. This is similar to
694 adding an element to a set, but less general. */
697 constructor_callback (info
, constructor
, name
, abfd
, section
, value
)
698 struct bfd_link_info
*info
;
707 struct bfd_link_hash_entry
*h
;
709 if (! config
.build_constructors
)
712 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
713 useful error message. */
714 if (bfd_reloc_type_lookup (output_bfd
, BFD_RELOC_CTOR
) == NULL
)
715 einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported");
717 set_name
= (char *) alloca (1 + sizeof "__CTOR_LIST__");
719 if (bfd_get_symbol_leading_char (abfd
) != '\0')
720 *s
++ = bfd_get_symbol_leading_char (abfd
);
722 strcpy (s
, "__CTOR_LIST__");
724 strcpy (s
, "__DTOR_LIST__");
726 if (config
.map_file
!= (FILE *) NULL
)
727 fprintf (config
.map_file
,
728 "Adding %s to constructor/destructor set %s\n", name
, set_name
);
730 h
= bfd_link_hash_lookup (info
->hash
, set_name
, true, true, true);
731 if (h
== (struct bfd_link_hash_entry
*) NULL
)
732 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
733 if (h
->type
== bfd_link_hash_new
)
735 h
->type
= bfd_link_hash_undefined
;
736 h
->u
.undef
.abfd
= abfd
;
737 /* We don't call bfd_link_add_undef to add this to the list of
738 undefined symbols because we are going to define it
742 ldctor_add_set_entry (h
, BFD_RELOC_CTOR
, section
, value
);
746 /* This is called when there is a reference to a warning symbol. */
750 warning_callback (info
, warning
)
751 struct bfd_link_info
*info
;
754 einfo ("%P: %s\n", warning
);
758 /* This is called when an undefined symbol is found. */
762 undefined_symbol (info
, name
, abfd
, section
, address
)
763 struct bfd_link_info
*info
;
769 static char *error_name
;
770 static unsigned int error_count
;
772 #define MAX_ERRORS_IN_A_ROW 5
774 /* We never print more than a reasonable number of errors in a row
775 for a single symbol. */
776 if (error_name
!= (char *) NULL
777 && strcmp (name
, error_name
) == 0)
782 if (error_name
!= (char *) NULL
)
784 error_name
= buystring (name
);
787 if (error_count
< MAX_ERRORS_IN_A_ROW
)
788 einfo ("%X%C: undefined reference to `%T'\n",
789 abfd
, section
, address
, name
);
790 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
791 einfo ("%C: more undefined references to `%T' follow\n",
792 abfd
, section
, address
, name
);
797 /* This is called when a reloc overflows. */
801 reloc_overflow (info
, name
, reloc_name
, addend
, abfd
, section
, address
)
802 struct bfd_link_info
*info
;
804 const char *reloc_name
;
810 if (abfd
== (bfd
*) NULL
)
811 einfo ("%P%X: generated");
813 einfo ("%X%C:", abfd
, section
, address
);
814 einfo (" relocation truncated to fit: %s %T", reloc_name
, name
);
816 einfo ("+%v", addend
);
821 /* This is called when a dangerous relocation is made. */
825 reloc_dangerous (info
, message
, abfd
, section
, address
)
826 struct bfd_link_info
*info
;
832 if (abfd
== (bfd
*) NULL
)
833 einfo ("%P%X: generated");
835 einfo ("%X%C:", abfd
, section
, address
);
836 einfo ("dangerous relocation: %s\n", message
);
840 /* This is called when a reloc is being generated attached to a symbol
841 that is not being output. */
845 unattached_reloc (info
, name
, abfd
, section
, address
)
846 struct bfd_link_info
*info
;
852 if (abfd
== (bfd
*) NULL
)
853 einfo ("%P%X: generated");
855 einfo ("%X%C:", abfd
, section
, address
);
856 einfo (" reloc refers to symbol `%T' which is not being output\n", name
);
860 /* This is called when a symbol in notice_hash is found. Symbols are
861 put in notice_hash using the -y option. */
865 notice_ysym (info
, name
, abfd
, section
, value
)
866 struct bfd_link_info
*info
;
872 einfo ("%B: %s %s\n", abfd
,
873 section
!= &bfd_und_section
? "definition of" : "reference to",