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 /* 1 => write load map. */
73 args_type command_line
;
75 ld_config_type config
;
77 static boolean check_for_scripts_dir
PARAMS ((char *dir
));
78 static boolean add_archive_element
PARAMS ((struct bfd_link_info
*, bfd
*,
80 static boolean multiple_definition
PARAMS ((struct bfd_link_info
*,
82 bfd
*, asection
*, bfd_vma
,
83 bfd
*, asection
*, bfd_vma
));
84 static boolean multiple_common
PARAMS ((struct bfd_link_info
*,
86 enum bfd_link_hash_type
, bfd_vma
,
87 bfd
*, enum bfd_link_hash_type
,
89 static boolean add_to_set
PARAMS ((struct bfd_link_info
*,
90 struct bfd_link_hash_entry
*,
92 bfd
*, asection
*, bfd_vma
));
93 static boolean constructor_callback
PARAMS ((struct bfd_link_info
*,
97 bfd
*, asection
*, bfd_vma
));
98 static boolean warning_callback
PARAMS ((struct bfd_link_info
*,
100 static boolean undefined_symbol
PARAMS ((struct bfd_link_info
*,
102 asection
*, bfd_vma
));
103 static boolean reloc_overflow
PARAMS ((struct bfd_link_info
*, const char *,
104 const char *, bfd_vma
,
105 bfd
*, asection
*, bfd_vma
));
106 static boolean reloc_dangerous
PARAMS ((struct bfd_link_info
*, const char *,
107 bfd
*, asection
*, bfd_vma
));
108 static boolean unattached_reloc
PARAMS ((struct bfd_link_info
*,
109 const char *, bfd
*, asection
*,
111 static boolean notice_ysym
PARAMS ((struct bfd_link_info
*, const char *,
112 bfd
*, asection
*, bfd_vma
));
114 static struct bfd_link_callbacks link_callbacks
=
120 constructor_callback
,
129 struct bfd_link_info link_info
;
131 extern int main
PARAMS ((int, char **));
139 long start_time
= get_run_time ();
141 program_name
= argv
[0];
145 /* Initialize the data about options. */
146 trace_files
= trace_file_tries
= false;
148 config
.build_constructors
= true;
149 command_line
.force_common_definition
= false;
151 link_info
.callbacks
= &link_callbacks
;
152 link_info
.relocateable
= false;
153 link_info
.strip
= strip_none
;
154 link_info
.discard
= discard_none
;
155 link_info
.lprefix_len
= 1;
156 link_info
.lprefix
= "L";
157 link_info
.keep_memory
= true;
158 link_info
.input_bfds
= NULL
;
159 link_info
.create_object_symbols_section
= NULL
;
160 link_info
.hash
= NULL
;
161 link_info
.keep_hash
= NULL
;
162 link_info
.notice_hash
= NULL
;
164 ldfile_add_arch ("");
166 config
.make_executable
= true;
167 force_make_executable
= false;
168 config
.magic_demand_paged
= true;
169 config
.text_read_only
= true;
170 config
.make_executable
= true;
172 emulation
= get_emulation (argc
, argv
);
173 ldemul_choose_mode (emulation
);
174 default_target
= ldemul_choose_target ();
176 ldemul_before_parse ();
177 lang_has_input_file
= false;
178 parse_args (argc
, argv
);
180 /* This essentially adds another -L directory so this must be done after
181 the -L's in argv have been processed. */
184 if (had_script
== false)
186 /* Read the emulation's appropriate default script. */
188 char *s
= ldemul_get_script (&isfile
);
192 /* sizeof counts the terminating NUL. */
193 size_t size
= strlen (s
) + sizeof ("-T ");
194 char *buf
= (char *) ldmalloc(size
);
195 sprintf (buf
, "-T %s", s
);
203 if (link_info
.relocateable
&& command_line
.relax
)
205 einfo ("%P%F: -relax and -r may not be used together\n");
209 if (lang_has_input_file
== false)
211 einfo ("%P%F: no input files\n");
216 info_msg ("%P: mode %s\n", emulation
);
219 ldemul_after_parse ();
222 if (config
.map_filename
)
224 if (strcmp (config
.map_filename
, "-") == 0)
226 config
.map_file
= stdout
;
230 config
.map_file
= fopen (config
.map_filename
, FOPEN_WT
);
231 if (config
.map_file
== (FILE *) NULL
)
233 einfo ("%P%F: cannot open map file %s: %E\n",
234 config
.map_filename
);
242 /* Print error messages for any missing symbols, for any warning
243 symbols, and possibly multiple definitions */
246 if (config
.text_read_only
)
248 /* Look for a text section and mark the readonly attribute in it */
249 asection
*found
= bfd_get_section_by_name (output_bfd
, ".text");
251 if (found
!= (asection
*) NULL
)
253 found
->flags
|= SEC_READONLY
;
257 if (link_info
.relocateable
)
258 output_bfd
->flags
&= ~EXEC_P
;
260 output_bfd
->flags
|= EXEC_P
;
264 /* Even if we're producing relocateable output, some non-fatal errors should
265 be reported in the exit status. (What non-fatal errors, if any, do we
266 want to ignore for relocateable output?) */
268 if (config
.make_executable
== false && force_make_executable
== false)
270 if (trace_files
== true)
272 einfo ("%P: link errors found, deleting executable `%s'\n",
276 if (output_bfd
->iostream
)
277 fclose ((FILE *) (output_bfd
->iostream
));
279 unlink (output_filename
);
284 bfd_close (output_bfd
);
289 extern char **environ
;
290 char *lim
= (char *) sbrk (0);
291 long run_time
= get_run_time () - start_time
;
293 fprintf (stderr
, "%s: total time in link: %d.%06d\n",
294 program_name
, run_time
/ 1000000, run_time
% 1000000);
295 fprintf (stderr
, "%s: data size %ld\n", program_name
,
296 (long) (lim
- (char *) &environ
));
303 /* We need to find any explicitly given emulation in order to initialize the
304 state that's needed by the lex&yacc argument parser (parse_args). */
307 get_emulation (argc
, argv
)
314 emulation
= (char *) getenv (EMULATION_ENVIRON
);
315 if (emulation
== NULL
)
316 emulation
= DEFAULT_EMULATION
;
318 for (i
= 1; i
< argc
; i
++)
320 if (!strncmp (argv
[i
], "-m", 2))
322 if (argv
[i
][2] == '\0')
327 emulation
= argv
[i
+ 1];
332 einfo("%P%F: missing argument to -m\n");
335 else if (strcmp (argv
[i
], "-mips1") == 0
336 || strcmp (argv
[i
], "-mips2") == 0
337 || strcmp (argv
[i
], "-mips3") == 0)
339 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
340 passed to the linker by some MIPS compilers. They
341 generally tell the linker to use a slightly different
342 library path. Perhaps someday these should be
343 implemented as emulations; until then, we just ignore
344 the arguments and hope that nobody ever creates
345 emulations named ips1, ips2 or ips3. */
350 emulation
= &argv
[i
][2];
358 /* If directory DIR contains an "ldscripts" subdirectory,
359 add DIR to the library search path and return true,
360 else return false. */
363 check_for_scripts_dir (dir
)
371 dirlen
= strlen (dir
);
372 /* sizeof counts the terminating NUL. */
373 buf
= (char *) ldmalloc (dirlen
+ sizeof("/ldscripts"));
374 sprintf (buf
, "%s/ldscripts", dir
);
376 res
= stat (buf
, &s
) == 0 && S_ISDIR (s
.st_mode
);
379 ldfile_add_library_path (dir
);
383 /* Set the default directory for finding script files.
384 Libraries will be searched for here too, but that's ok.
385 We look for the "ldscripts" directory in:
387 SCRIPTDIR (passed from Makefile)
388 the dir where this program is (for using it from the build tree)
389 the dir where this program is/../lib (for installing the tool suite elsewhere) */
397 if (check_for_scripts_dir (SCRIPTDIR
))
398 return; /* We've been installed normally. */
400 /* Look for "ldscripts" in the dir where our binary is. */
401 end
= strrchr (program_name
, '/');
404 dirlen
= end
- program_name
;
405 /* Make a copy of program_name in dir.
406 Leave room for later "/../lib". */
407 dir
= (char *) ldmalloc (dirlen
+ 8);
408 strncpy (dir
, program_name
, dirlen
);
414 dir
= (char *) ldmalloc (dirlen
+ 8);
418 if (check_for_scripts_dir (dir
))
419 return; /* Don't free dir. */
421 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
422 strcpy (dir
+ dirlen
, "/../lib");
423 if (check_for_scripts_dir (dir
))
426 free (dir
); /* Well, we tried. */
433 if (link_info
.notice_hash
== (struct bfd_hash_table
*) NULL
)
435 link_info
.notice_hash
= ((struct bfd_hash_table
*)
436 ldmalloc (sizeof (struct bfd_hash_table
)));
437 if (! bfd_hash_table_init_n (link_info
.notice_hash
,
440 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
443 if (bfd_hash_lookup (link_info
.notice_hash
, name
, true, true)
444 == (struct bfd_hash_entry
*) NULL
)
445 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
448 /* Handle the -retain-symbols-file option. */
451 add_keepsyms_file (filename
)
452 const char *filename
;
459 if (link_info
.strip
== strip_some
)
460 einfo ("%X%P: error: duplicate retain-symbols-file\n");
462 file
= fopen (filename
, "r");
463 if (file
== (FILE *) NULL
)
465 bfd_error
= system_call_error
;
466 einfo ("%X%P: %s: %E", filename
);
470 link_info
.keep_hash
= ((struct bfd_hash_table
*)
471 ldmalloc (sizeof (struct bfd_hash_table
)));
472 if (! bfd_hash_table_init (link_info
.keep_hash
, bfd_hash_newfunc
))
473 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
476 buf
= (char *) ldmalloc (bufsize
);
488 while (! isspace (c
) && c
!= EOF
)
495 buf
= ldrealloc (buf
, bufsize
);
502 if (bfd_hash_lookup (link_info
.keep_hash
, buf
, true, true)
503 == (struct bfd_hash_entry
*) NULL
)
504 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
508 if (link_info
.strip
!= strip_none
)
509 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
511 link_info
.strip
= strip_some
;
514 /* Callbacks from the BFD linker routines. */
516 /* This is called when BFD has decided to include an archive member in
521 add_archive_element (info
, abfd
, name
)
522 struct bfd_link_info
*info
;
526 lang_input_statement_type
*input
;
528 input
= ((lang_input_statement_type
*)
529 ldmalloc ((bfd_size_type
) sizeof (lang_input_statement_type
)));
530 input
->filename
= abfd
->filename
;
531 input
->local_sym_name
= abfd
->filename
;
532 input
->the_bfd
= abfd
;
533 input
->asymbols
= NULL
;
534 input
->subfiles
= NULL
;
536 input
->just_syms_flag
= false;
537 input
->loaded
= false;
540 /* FIXME: This is wrong. It should point to an entry for the
541 archive itself. However, it doesn't seem to matter. */
542 input
->superfile
= NULL
;
544 /* FIXME: The following fields are not set: header.next,
545 header.type, closed, passive_position, symbol_count, total_size,
546 next_real_file, is_archive, search_dirs_flag, target, real,
547 common_section, common_output_section, complained. This bit of
548 code is from the old decode_library_subfile function. I don't
549 know whether any of those fields matters. */
551 ldlang_add_file (input
);
554 info_msg ("%s needed due to %T\n", abfd
->filename
, name
);
556 if (trace_files
|| trace_file_tries
)
557 info_msg ("%I\n", input
);
562 /* This is called when BFD has discovered a symbol which is defined
567 multiple_definition (info
, name
, obfd
, osec
, oval
, nbfd
, nsec
, nval
)
568 struct bfd_link_info
*info
;
577 einfo ("%X%C: multiple definition of `%T'\n",
578 nbfd
, nsec
, nval
, name
);
579 if (obfd
!= (bfd
*) NULL
)
580 einfo ("%C: first defined here\n", obfd
, osec
, oval
);
584 /* This is called when there is a definition of a common symbol, or
585 when a common symbol is found for a symbol that is already defined,
586 or when two common symbols are found. We only do something if
587 -warn-common was used. */
591 multiple_common (info
, name
, obfd
, otype
, osize
, nbfd
, ntype
, nsize
)
592 struct bfd_link_info
*info
;
595 enum bfd_link_hash_type otype
;
598 enum bfd_link_hash_type ntype
;
601 if (! config
.warn_common
)
604 if (ntype
== bfd_link_hash_defined
)
606 ASSERT (otype
== bfd_link_hash_common
);
607 einfo ("%B: warning: definition of `%T' overriding common\n",
609 einfo ("%B: warning: common is here\n", obfd
);
611 else if (otype
== bfd_link_hash_defined
)
613 ASSERT (ntype
== bfd_link_hash_common
);
614 einfo ("%B: warning: common of `%T' overridden by definition\n",
616 einfo ("%B: warning: defined here\n", obfd
);
620 ASSERT (otype
== bfd_link_hash_common
&& ntype
== bfd_link_hash_common
);
623 einfo ("%B: warning: common of `%T' overridden by larger common\n",
625 einfo ("%B: warning: larger common is here\n", obfd
);
627 else if (nsize
> osize
)
629 einfo ("%B: warning: common of `%T' overriding smaller common\n",
631 einfo ("%B: warning: smaller common is here\n", obfd
);
635 einfo ("%B: warning: multiple common of `%T'\n", nbfd
, name
);
636 einfo ("%B: warning: previous common is here\n", obfd
);
643 /* This is called when BFD has discovered a set element. H is the
644 entry in the linker hash table for the set. SECTION and VALUE
645 represent a value which should be added to the set. */
649 add_to_set (info
, h
, bitsize
, abfd
, section
, value
)
650 struct bfd_link_info
*info
;
651 struct bfd_link_hash_entry
*h
;
652 unsigned int bitsize
;
657 ldctor_add_set_entry (h
, bitsize
, section
, value
);
661 /* This is called when BFD has discovered a constructor. This is only
662 called for some object file formats--those which do not handle
663 constructors in some more clever fashion. This is similar to
664 adding an element to a set, but less general. */
667 constructor_callback (info
, constructor
, bitsize
, name
, abfd
, section
, value
)
668 struct bfd_link_info
*info
;
670 unsigned int bitsize
;
678 struct bfd_link_hash_entry
*h
;
680 if (! config
.build_constructors
)
683 set_name
= (char *) alloca (1 + sizeof "__CTOR_LIST__");
685 if (bfd_get_symbol_leading_char (abfd
) != '\0')
686 *s
++ = bfd_get_symbol_leading_char (abfd
);
688 strcpy (s
, "__CTOR_LIST__");
690 strcpy (s
, "__DTOR_LIST__");
693 info_msg ("Adding %s to constructor/destructor set %s\n", name
, set_name
);
695 h
= bfd_link_hash_lookup (info
->hash
, set_name
, true, true, true);
696 if (h
== (struct bfd_link_hash_entry
*) NULL
)
697 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
698 if (h
->type
== bfd_link_hash_new
)
700 h
->type
= bfd_link_hash_undefined
;
701 h
->u
.undef
.abfd
= abfd
;
702 /* We don't call bfd_link_add_undef to add this to the list of
703 undefined symbols because we are going to define it
707 ldctor_add_set_entry (h
, bitsize
, section
, value
);
711 /* This is called when there is a reference to a warning symbol. */
715 warning_callback (info
, warning
)
716 struct bfd_link_info
*info
;
719 einfo ("%P: %s\n", warning
);
723 /* This is called when an undefined symbol is found. */
727 undefined_symbol (info
, name
, abfd
, section
, address
)
728 struct bfd_link_info
*info
;
734 static char *error_name
;
735 static unsigned int error_count
;
737 #define MAX_ERRORS_IN_A_ROW 5
739 /* We never print more than a reasonable number of errors in a row
740 for a single symbol. */
741 if (error_name
!= (char *) NULL
742 && strcmp (name
, error_name
) == 0)
747 if (error_name
!= (char *) NULL
)
749 error_name
= buystring (name
);
752 if (error_count
< MAX_ERRORS_IN_A_ROW
)
753 einfo ("%X%C: undefined reference to `%T'\n",
754 abfd
, section
, address
, name
);
755 else if (error_count
== MAX_ERRORS_IN_A_ROW
)
756 einfo ("%C: more undefined references to `%T' follow\n",
757 abfd
, section
, address
, name
);
762 /* This is called when a reloc overflows. */
766 reloc_overflow (info
, name
, reloc_name
, addend
, abfd
, section
, address
)
767 struct bfd_link_info
*info
;
769 const char *reloc_name
;
775 einfo ("%X%C: relocation truncated to fit: %s %T", abfd
, section
,
776 address
, reloc_name
, name
);
778 einfo ("+%v", addend
);
783 /* This is called when a dangerous relocation is made. */
787 reloc_dangerous (info
, message
, abfd
, section
, address
)
788 struct bfd_link_info
*info
;
794 einfo ("%X%C: dangerous relocation: %s\n", abfd
, section
, address
, message
);
798 /* This is called when a reloc is being generated attached to a symbol
799 that is not being output. */
803 unattached_reloc (info
, name
, abfd
, section
, address
)
804 struct bfd_link_info
*info
;
810 einfo ("%X%C: reloc refers to symbol `%T' which is not being output\n",
811 abfd
, section
, address
, name
);
815 /* This is called when a symbol in notice_hash is found. Symbols are
816 put in notice_hash using the -y option. */
820 notice_ysym (info
, name
, abfd
, section
, value
)
821 struct bfd_link_info
*info
;
827 einfo ("%B: %s %s\n", abfd
,
828 section
!= &bfd_und_section
? "definition of" : "reference to",