1 /* Main program of GNU linker.
2 Copyright (C) 1991, 1993, 1994 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. */
40 /* Somewhere above, sys/stat.h got included . . . . */
41 #if !defined(S_ISDIR) && defined(S_IFDIR)
42 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
47 static char *get_emulation
PARAMS ((int, char **));
48 static void set_scripts_dir
PARAMS ((void));
53 const char *output_filename
= "a.out";
55 /* Name this program was invoked by. */
58 /* The file that we're creating */
61 /* Set by -G argument, for MIPS ECOFF target. */
62 int g_switch_value
= 8;
64 /* Nonzero means print names of input files as processed. */
67 /* Nonzero means same, but note open failures, too. */
68 boolean trace_file_tries
;
70 /* Nonzero means version number was printed, so exit successfully
71 instead of complaining if no input files are given. */
72 boolean version_printed
;
74 /* 1 => write load map. */
77 args_type command_line
;
79 ld_config_type config
;
81 static boolean check_for_scripts_dir
PARAMS ((char *dir
));
82 static boolean add_archive_element
PARAMS ((struct bfd_link_info
*, bfd
*,
84 static boolean multiple_definition
PARAMS ((struct bfd_link_info
*,
86 bfd
*, asection
*, bfd_vma
,
87 bfd
*, asection
*, bfd_vma
));
88 static boolean multiple_common
PARAMS ((struct bfd_link_info
*,
90 enum bfd_link_hash_type
, bfd_vma
,
91 bfd
*, enum bfd_link_hash_type
,
93 static boolean add_to_set
PARAMS ((struct bfd_link_info
*,
94 struct bfd_link_hash_entry
*,
96 bfd
*, asection
*, bfd_vma
));
97 static boolean constructor_callback
PARAMS ((struct bfd_link_info
*,
101 bfd
*, asection
*, bfd_vma
));
102 static boolean warning_callback
PARAMS ((struct bfd_link_info
*,
104 static boolean undefined_symbol
PARAMS ((struct bfd_link_info
*,
106 asection
*, bfd_vma
));
107 static boolean reloc_overflow
PARAMS ((struct bfd_link_info
*, const char *,
108 const char *, bfd_vma
,
109 bfd
*, asection
*, bfd_vma
));
110 static boolean reloc_dangerous
PARAMS ((struct bfd_link_info
*, const char *,
111 bfd
*, asection
*, bfd_vma
));
112 static boolean unattached_reloc
PARAMS ((struct bfd_link_info
*,
113 const char *, bfd
*, asection
*,
115 static boolean notice_ysym
PARAMS ((struct bfd_link_info
*, const char *,
116 bfd
*, asection
*, bfd_vma
));
118 static struct bfd_link_callbacks link_callbacks
=
124 constructor_callback
,
133 struct bfd_link_info link_info
;
135 extern int main
PARAMS ((int, char **));
143 long start_time
= get_run_time ();
145 program_name
= argv
[0];
149 /* Initialize the data about options. */
150 trace_files
= trace_file_tries
= version_printed
= false;
152 config
.build_constructors
= true;
153 command_line
.force_common_definition
= false;
155 link_info
.callbacks
= &link_callbacks
;
156 link_info
.relocateable
= false;
157 link_info
.strip
= strip_none
;
158 link_info
.discard
= discard_none
;
159 link_info
.lprefix_len
= 1;
160 link_info
.lprefix
= "L";
161 link_info
.keep_memory
= true;
162 link_info
.input_bfds
= NULL
;
163 link_info
.create_object_symbols_section
= NULL
;
164 link_info
.hash
= NULL
;
165 link_info
.keep_hash
= NULL
;
166 link_info
.notice_hash
= NULL
;
168 ldfile_add_arch ("");
170 config
.make_executable
= true;
171 force_make_executable
= false;
172 config
.magic_demand_paged
= true;
173 config
.text_read_only
= true;
174 config
.make_executable
= true;
176 emulation
= get_emulation (argc
, argv
);
177 ldemul_choose_mode (emulation
);
178 default_target
= ldemul_choose_target ();
180 ldemul_before_parse ();
181 lang_has_input_file
= false;
182 parse_args (argc
, argv
);
184 /* This essentially adds another -L directory so this must be done after
185 the -L's in argv have been processed. */
188 if (had_script
== false)
190 /* Read the emulation's appropriate default script. */
192 char *s
= ldemul_get_script (&isfile
);
196 /* sizeof counts the terminating NUL. */
197 size_t size
= strlen (s
) + sizeof ("-T ");
198 char *buf
= (char *) ldmalloc(size
);
199 sprintf (buf
, "-T %s", s
);
207 if (link_info
.relocateable
&& command_line
.relax
)
209 einfo ("%P%F: -relax and -r may not be used together\n");
213 if (lang_has_input_file
== false)
217 einfo ("%P%F: no input files\n");
222 info_msg ("%P: mode %s\n", emulation
);
225 ldemul_after_parse ();
228 if (config
.map_filename
)
230 if (strcmp (config
.map_filename
, "-") == 0)
232 config
.map_file
= stdout
;
236 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
237 if (config
.map_file
== (FILE *) NULL
)
239 einfo ("%P%F: cannot open map file %s: %E\n",
240 config
.map_filename
);
248 /* Print error messages for any missing symbols, for any warning
249 symbols, and possibly multiple definitions */
252 if (config
.text_read_only
)
254 /* Look for a text section and mark the readonly attribute in it */
255 asection
*found
= bfd_get_section_by_name (output_bfd
, ".text");
257 if (found
!= (asection
*) NULL
)
259 found
->flags
|= SEC_READONLY
;
263 if (link_info
.relocateable
)
264 output_bfd
->flags
&= ~EXEC_P
;
266 output_bfd
->flags
|= EXEC_P
;
270 /* Even if we're producing relocateable output, some non-fatal errors should
271 be reported in the exit status. (What non-fatal errors, if any, do we
272 want to ignore for relocateable output?) */
274 if (config
.make_executable
== false && force_make_executable
== false)
276 if (trace_files
== true)
278 einfo ("%P: link errors found, deleting executable `%s'\n",
282 if (output_bfd
->iostream
)
283 fclose ((FILE *) (output_bfd
->iostream
));
285 unlink (output_filename
);
290 bfd_close (output_bfd
);
295 extern char **environ
;
296 char *lim
= (char *) sbrk (0);
297 long run_time
= get_run_time () - start_time
;
299 fprintf (stderr
, "%s: total time in link: %d.%06d\n",
300 program_name
, run_time
/ 1000000, run_time
% 1000000);
301 fprintf (stderr
, "%s: data size %ld\n", program_name
,
302 (long) (lim
- (char *) &environ
));
309 /* We need to find any explicitly given emulation in order to initialize the
310 state that's needed by the lex&yacc argument parser (parse_args). */
313 get_emulation (argc
, argv
)
320 emulation
= (char *) getenv (EMULATION_ENVIRON
);
321 if (emulation
== NULL
)
322 emulation
= DEFAULT_EMULATION
;
324 for (i
= 1; i
< argc
; i
++)
326 if (!strncmp (argv
[i
], "-m", 2))
328 if (argv
[i
][2] == '\0')
333 emulation
= argv
[i
+ 1];
338 einfo("%P%F: missing argument to -m\n");
341 else if (strcmp (argv
[i
], "-mips1") == 0
342 || strcmp (argv
[i
], "-mips2") == 0
343 || strcmp (argv
[i
], "-mips3") == 0)
345 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
346 passed to the linker by some MIPS compilers. They
347 generally tell the linker to use a slightly different
348 library path. Perhaps someday these should be
349 implemented as emulations; until then, we just ignore
350 the arguments and hope that nobody ever creates
351 emulations named ips1, ips2 or ips3. */
356 emulation
= &argv
[i
][2];
364 /* If directory DIR contains an "ldscripts" subdirectory,
365 add DIR to the library search path and return true,
366 else return false. */
369 check_for_scripts_dir (dir
)
377 dirlen
= strlen (dir
);
378 /* sizeof counts the terminating NUL. */
379 buf
= (char *) ldmalloc (dirlen
+ sizeof("/ldscripts"));
380 sprintf (buf
, "%s/ldscripts", dir
);
382 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
385 ldfile_add_library_path (dir
);
389 /* Set the default directory for finding script files.
390 Libraries will be searched for here too, but that's ok.
391 We look for the "ldscripts" directory in:
393 SCRIPTDIR (passed from Makefile)
394 the dir where this program is (for using it from the build tree)
395 the dir where this program is/../lib (for installing the tool suite elsewhere) */
403 if (check_for_scripts_dir (SCRIPTDIR
))
404 return; /* We've been installed normally. */
406 /* Look for "ldscripts" in the dir where our binary is. */
407 end
= strrchr (program_name
, '/');
410 dirlen
= end
- program_name
;
411 /* Make a copy of program_name in dir.
412 Leave room for later "/../lib". */
413 dir
= (char *) ldmalloc (dirlen
+ 8);
414 strncpy (dir
, program_name
, dirlen
);
420 dir
= (char *) ldmalloc (dirlen
+ 8);
424 if (check_for_scripts_dir (dir
))
425 return; /* Don't free dir. */
427 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
428 strcpy (dir
+ dirlen
, "/../lib");
429 if (check_for_scripts_dir (dir
))
432 free (dir
); /* Well, we tried. */
439 if (link_info
.notice_hash
== (struct bfd_hash_table
*) NULL
)
441 link_info
.notice_hash
= ((struct bfd_hash_table
*)
442 ldmalloc (sizeof (struct bfd_hash_table
)));
443 if (! bfd_hash_table_init_n (link_info
.notice_hash
,
446 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
449 if (bfd_hash_lookup (link_info
.notice_hash
, name
, true, true)
450 == (struct bfd_hash_entry
*) NULL
)
451 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
454 /* Handle the -retain-symbols-file option. */
457 add_keepsyms_file (filename
)
458 const char *filename
;
465 if (link_info
.strip
== strip_some
)
466 einfo ("%X%P: error: duplicate retain-symbols-file\n");
468 file
= fopen (filename
, "r");
469 if (file
== (FILE *) NULL
)
471 bfd_error
= system_call_error
;
472 einfo ("%X%P: %s: %E", filename
);
476 link_info
.keep_hash
= ((struct bfd_hash_table
*)
477 ldmalloc (sizeof (struct bfd_hash_table
)));
478 if (! bfd_hash_table_init (link_info
.keep_hash
, bfd_hash_newfunc
))
479 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
482 buf
= (char *) ldmalloc (bufsize
);
494 while (! isspace (c
) && c
!= EOF
)
501 buf
= ldrealloc (buf
, bufsize
);
508 if (bfd_hash_lookup (link_info
.keep_hash
, buf
, true, true)
509 == (struct bfd_hash_entry
*) NULL
)
510 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
514 if (link_info
.strip
!= strip_none
)
515 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
517 link_info
.strip
= strip_some
;
520 /* Callbacks from the BFD linker routines. */
522 /* This is called when BFD has decided to include an archive member in
527 add_archive_element (info
, abfd
, name
)
528 struct bfd_link_info
*info
;
532 lang_input_statement_type
*input
;
534 input
= ((lang_input_statement_type
*)
535 ldmalloc ((bfd_size_type
) sizeof (lang_input_statement_type
)));
536 input
->filename
= abfd
->filename
;
537 input
->local_sym_name
= abfd
->filename
;
538 input
->the_bfd
= abfd
;
539 input
->asymbols
= NULL
;
540 input
->subfiles
= NULL
;
542 input
->just_syms_flag
= false;
543 input
->loaded
= false;
546 /* FIXME: This is wrong. It should point to an entry for the
547 archive itself. However, it doesn't seem to matter. */
548 input
->superfile
= NULL
;
550 /* FIXME: The following fields are not set: header.next,
551 header.type, closed, passive_position, symbol_count, total_size,
552 next_real_file, is_archive, search_dirs_flag, target, real,
553 common_section, common_output_section, complained. This bit of
554 code is from the old decode_library_subfile function. I don't
555 know whether any of those fields matters. */
557 ldlang_add_file (input
);
560 info_msg ("%s needed due to %T\n", abfd
->filename
, name
);
562 if (trace_files
|| trace_file_tries
)
563 info_msg ("%I\n", input
);
568 /* This is called when BFD has discovered a symbol which is defined
573 multiple_definition (info
, name
, obfd
, osec
, oval
, nbfd
, nsec
, nval
)
574 struct bfd_link_info
*info
;
583 einfo ("%X%C: multiple definition of `%T'\n",
584 nbfd
, nsec
, nval
, name
);
585 if (obfd
!= (bfd
*) NULL
)
586 einfo ("%C: first defined here\n", obfd
, osec
, oval
);
590 /* This is called when there is a definition of a common symbol, or
591 when a common symbol is found for a symbol that is already defined,
592 or when two common symbols are found. We only do something if
593 -warn-common was used. */
597 multiple_common (info
, name
, obfd
, otype
, osize
, nbfd
, ntype
, nsize
)
598 struct bfd_link_info
*info
;
601 enum bfd_link_hash_type otype
;
604 enum bfd_link_hash_type ntype
;
607 if (! config
.warn_common
)
610 if (ntype
== bfd_link_hash_defined
)
612 ASSERT (otype
== bfd_link_hash_common
);
613 einfo ("%B: warning: definition of `%T' overriding common\n",
615 einfo ("%B: warning: common is here\n", obfd
);
617 else if (otype
== bfd_link_hash_defined
)
619 ASSERT (ntype
== bfd_link_hash_common
);
620 einfo ("%B: warning: common of `%T' overridden by definition\n",
622 einfo ("%B: warning: defined here\n", obfd
);
626 ASSERT (otype
== bfd_link_hash_common
&& ntype
== bfd_link_hash_common
);
629 einfo ("%B: warning: common of `%T' overridden by larger common\n",
631 einfo ("%B: warning: larger common is here\n", obfd
);
633 else if (nsize
> osize
)
635 einfo ("%B: warning: common of `%T' overriding smaller common\n",
637 einfo ("%B: warning: smaller common is here\n", obfd
);
641 einfo ("%B: warning: multiple common of `%T'\n", nbfd
, name
);
642 einfo ("%B: warning: previous common is here\n", obfd
);
649 /* This is called when BFD has discovered a set element. H is the
650 entry in the linker hash table for the set. SECTION and VALUE
651 represent a value which should be added to the set. */
655 add_to_set (info
, h
, bitsize
, abfd
, section
, value
)
656 struct bfd_link_info
*info
;
657 struct bfd_link_hash_entry
*h
;
658 unsigned int bitsize
;
663 ldctor_add_set_entry (h
, bitsize
, section
, value
);
667 /* This is called when BFD has discovered a constructor. This is only
668 called for some object file formats--those which do not handle
669 constructors in some more clever fashion. This is similar to
670 adding an element to a set, but less general. */
673 constructor_callback (info
, constructor
, bitsize
, name
, abfd
, section
, value
)
674 struct bfd_link_info
*info
;
676 unsigned int bitsize
;
684 struct bfd_link_hash_entry
*h
;
686 if (! config
.build_constructors
)
689 set_name
= (char *) alloca (1 + sizeof "__CTOR_LIST__");
691 if (bfd_get_symbol_leading_char (abfd
) != '\0')
692 *s
++ = bfd_get_symbol_leading_char (abfd
);
694 strcpy (s
, "__CTOR_LIST__");
696 strcpy (s
, "__DTOR_LIST__");
699 info_msg ("Adding %s to constructor/destructor set %s\n", name
, set_name
);
701 h
= bfd_link_hash_lookup (info
->hash
, set_name
, true, true, true);
702 if (h
== (struct bfd_link_hash_entry
*) NULL
)
703 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
704 if (h
->type
== bfd_link_hash_new
)
706 h
->type
= bfd_link_hash_undefined
;
707 h
->u
.undef
.abfd
= abfd
;
708 /* We don't call bfd_link_add_undef to add this to the list of
709 undefined symbols because we are going to define it
713 ldctor_add_set_entry (h
, bitsize
, section
, value
);
717 /* This is called when there is a reference to a warning symbol. */
721 warning_callback (info
, warning
)
722 struct bfd_link_info
*info
;
725 einfo ("%P: %s\n", warning
);
729 /* This is called when an undefined symbol is found. */
733 undefined_symbol (info
, name
, abfd
, section
, address
)
734 struct bfd_link_info
*info
;
740 static char *error_name
;
741 static unsigned int error_count
;
743 #define MAX_ERRORS_IN_A_ROW 5
745 /* We never print more than a reasonable number of errors in a row
746 for a single symbol. */
747 if (error_name
!= (char *) NULL
748 && strcmp (name
, error_name
) == 0)
753 if (error_name
!= (char *) NULL
)
755 error_name
= buystring (name
);
758 if (error_count
< MAX_ERRORS_IN_A_ROW
)
759 einfo ("%X%C: undefined reference to `%T'\n",
760 abfd
, section
, address
, name
);
761 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
762 einfo ("%C: more undefined references to `%T' follow\n",
763 abfd
, section
, address
, name
);
768 /* This is called when a reloc overflows. */
772 reloc_overflow (info
, name
, reloc_name
, addend
, abfd
, section
, address
)
773 struct bfd_link_info
*info
;
775 const char *reloc_name
;
781 einfo ("%X%C: relocation truncated to fit: %s %T", abfd
, section
,
782 address
, reloc_name
, name
);
784 einfo ("+%v", addend
);
789 /* This is called when a dangerous relocation is made. */
793 reloc_dangerous (info
, message
, abfd
, section
, address
)
794 struct bfd_link_info
*info
;
800 einfo ("%X%C: dangerous relocation: %s\n", abfd
, section
, address
, message
);
804 /* This is called when a reloc is being generated attached to a symbol
805 that is not being output. */
809 unattached_reloc (info
, name
, abfd
, section
, address
)
810 struct bfd_link_info
*info
;
816 einfo ("%X%C: reloc refers to symbol `%T' which is not being output\n",
817 abfd
, section
, address
, name
);
821 /* This is called when a symbol in notice_hash is found. Symbols are
822 put in notice_hash using the -y option. */
826 notice_ysym (info
, name
, abfd
, section
, value
)
827 struct bfd_link_info
*info
;
833 einfo ("%B: %s %s\n", abfd
,
834 section
!= &bfd_und_section
? "definition of" : "reference to",