xref: /dragonfly/contrib/binutils-2.27/ld/ldmain.c (revision a9fa9459)
1*a9fa9459Szrj /* Main program of GNU linker.
2*a9fa9459Szrj    Copyright (C) 1991-2016 Free Software Foundation, Inc.
3*a9fa9459Szrj    Written by Steve Chamberlain steve@cygnus.com
4*a9fa9459Szrj 
5*a9fa9459Szrj    This file is part of the GNU Binutils.
6*a9fa9459Szrj 
7*a9fa9459Szrj    This program is free software; you can redistribute it and/or modify
8*a9fa9459Szrj    it under the terms of the GNU General Public License as published by
9*a9fa9459Szrj    the Free Software Foundation; either version 3 of the License, or
10*a9fa9459Szrj    (at your option) any later version.
11*a9fa9459Szrj 
12*a9fa9459Szrj    This program is distributed in the hope that it will be useful,
13*a9fa9459Szrj    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*a9fa9459Szrj    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*a9fa9459Szrj    GNU General Public License for more details.
16*a9fa9459Szrj 
17*a9fa9459Szrj    You should have received a copy of the GNU General Public License
18*a9fa9459Szrj    along with this program; if not, write to the Free Software
19*a9fa9459Szrj    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20*a9fa9459Szrj    MA 02110-1301, USA.  */
21*a9fa9459Szrj 
22*a9fa9459Szrj #include "sysdep.h"
23*a9fa9459Szrj #include "bfd.h"
24*a9fa9459Szrj #include "safe-ctype.h"
25*a9fa9459Szrj #include "libiberty.h"
26*a9fa9459Szrj #include "progress.h"
27*a9fa9459Szrj #include "bfdlink.h"
28*a9fa9459Szrj #include "filenames.h"
29*a9fa9459Szrj 
30*a9fa9459Szrj #include "ld.h"
31*a9fa9459Szrj #include "ldmain.h"
32*a9fa9459Szrj #include "ldmisc.h"
33*a9fa9459Szrj #include "ldwrite.h"
34*a9fa9459Szrj #include "ldexp.h"
35*a9fa9459Szrj #include "ldlang.h"
36*a9fa9459Szrj #include <ldgram.h>
37*a9fa9459Szrj #include "ldlex.h"
38*a9fa9459Szrj #include "ldfile.h"
39*a9fa9459Szrj #include "ldemul.h"
40*a9fa9459Szrj #include "ldctor.h"
41*a9fa9459Szrj #ifdef ENABLE_PLUGINS
42*a9fa9459Szrj #include "plugin.h"
43*a9fa9459Szrj #include "plugin-api.h"
44*a9fa9459Szrj #endif /* ENABLE_PLUGINS */
45*a9fa9459Szrj 
46*a9fa9459Szrj /* Somewhere above, sys/stat.h got included.  */
47*a9fa9459Szrj #if !defined(S_ISDIR) && defined(S_IFDIR)
48*a9fa9459Szrj #define	S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
49*a9fa9459Szrj #endif
50*a9fa9459Szrj 
51*a9fa9459Szrj #include <string.h>
52*a9fa9459Szrj 
53*a9fa9459Szrj #ifdef HAVE_SBRK
54*a9fa9459Szrj #if !HAVE_DECL_SBRK
55*a9fa9459Szrj extern void *sbrk ();
56*a9fa9459Szrj #endif
57*a9fa9459Szrj #endif
58*a9fa9459Szrj 
59*a9fa9459Szrj #ifndef TARGET_SYSTEM_ROOT
60*a9fa9459Szrj #define TARGET_SYSTEM_ROOT ""
61*a9fa9459Szrj #endif
62*a9fa9459Szrj 
63*a9fa9459Szrj /* EXPORTS */
64*a9fa9459Szrj 
65*a9fa9459Szrj FILE *saved_script_handle = NULL;
66*a9fa9459Szrj FILE *previous_script_handle = NULL;
67*a9fa9459Szrj bfd_boolean force_make_executable = FALSE;
68*a9fa9459Szrj 
69*a9fa9459Szrj char *default_target;
70*a9fa9459Szrj const char *output_filename = "a.out";
71*a9fa9459Szrj 
72*a9fa9459Szrj /* Name this program was invoked by.  */
73*a9fa9459Szrj char *program_name;
74*a9fa9459Szrj 
75*a9fa9459Szrj /* The prefix for system library directories.  */
76*a9fa9459Szrj const char *ld_sysroot;
77*a9fa9459Szrj 
78*a9fa9459Szrj /* The canonical representation of ld_sysroot.  */
79*a9fa9459Szrj char *ld_canon_sysroot;
80*a9fa9459Szrj int ld_canon_sysroot_len;
81*a9fa9459Szrj 
82*a9fa9459Szrj /* Set by -G argument, for targets like MIPS ELF.  */
83*a9fa9459Szrj int g_switch_value = 8;
84*a9fa9459Szrj 
85*a9fa9459Szrj /* Nonzero means print names of input files as processed.  */
86*a9fa9459Szrj bfd_boolean trace_files;
87*a9fa9459Szrj 
88*a9fa9459Szrj /* Nonzero means report actions taken by the linker, and describe the linker script in use.  */
89*a9fa9459Szrj bfd_boolean verbose;
90*a9fa9459Szrj 
91*a9fa9459Szrj /* Nonzero means version number was printed, so exit successfully
92*a9fa9459Szrj    instead of complaining if no input files are given.  */
93*a9fa9459Szrj bfd_boolean version_printed;
94*a9fa9459Szrj 
95*a9fa9459Szrj /* TRUE if we should demangle symbol names.  */
96*a9fa9459Szrj bfd_boolean demangling;
97*a9fa9459Szrj 
98*a9fa9459Szrj args_type command_line;
99*a9fa9459Szrj 
100*a9fa9459Szrj ld_config_type config;
101*a9fa9459Szrj 
102*a9fa9459Szrj sort_type sort_section;
103*a9fa9459Szrj 
104*a9fa9459Szrj static const char *get_sysroot
105*a9fa9459Szrj   (int, char **);
106*a9fa9459Szrj static char *get_emulation
107*a9fa9459Szrj   (int, char **);
108*a9fa9459Szrj static bfd_boolean add_archive_element
109*a9fa9459Szrj   (struct bfd_link_info *, bfd *, const char *, bfd **);
110*a9fa9459Szrj static void multiple_definition
111*a9fa9459Szrj   (struct bfd_link_info *, struct bfd_link_hash_entry *,
112*a9fa9459Szrj    bfd *, asection *, bfd_vma);
113*a9fa9459Szrj static void multiple_common
114*a9fa9459Szrj   (struct bfd_link_info *, struct bfd_link_hash_entry *,
115*a9fa9459Szrj    bfd *, enum bfd_link_hash_type, bfd_vma);
116*a9fa9459Szrj static void add_to_set
117*a9fa9459Szrj   (struct bfd_link_info *, struct bfd_link_hash_entry *,
118*a9fa9459Szrj    bfd_reloc_code_real_type, bfd *, asection *, bfd_vma);
119*a9fa9459Szrj static void constructor_callback
120*a9fa9459Szrj   (struct bfd_link_info *, bfd_boolean, const char *, bfd *,
121*a9fa9459Szrj    asection *, bfd_vma);
122*a9fa9459Szrj static void warning_callback
123*a9fa9459Szrj   (struct bfd_link_info *, const char *, const char *, bfd *,
124*a9fa9459Szrj    asection *, bfd_vma);
125*a9fa9459Szrj static void warning_find_reloc
126*a9fa9459Szrj   (bfd *, asection *, void *);
127*a9fa9459Szrj static void undefined_symbol
128*a9fa9459Szrj   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
129*a9fa9459Szrj    bfd_boolean);
130*a9fa9459Szrj static void reloc_overflow
131*a9fa9459Szrj   (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
132*a9fa9459Szrj    const char *, bfd_vma, bfd *, asection *, bfd_vma);
133*a9fa9459Szrj static void reloc_dangerous
134*a9fa9459Szrj   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
135*a9fa9459Szrj static void unattached_reloc
136*a9fa9459Szrj   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
137*a9fa9459Szrj static bfd_boolean notice
138*a9fa9459Szrj   (struct bfd_link_info *, struct bfd_link_hash_entry *,
139*a9fa9459Szrj    struct bfd_link_hash_entry *, bfd *, asection *, bfd_vma, flagword);
140*a9fa9459Szrj 
141*a9fa9459Szrj static struct bfd_link_callbacks link_callbacks =
142*a9fa9459Szrj {
143*a9fa9459Szrj   add_archive_element,
144*a9fa9459Szrj   multiple_definition,
145*a9fa9459Szrj   multiple_common,
146*a9fa9459Szrj   add_to_set,
147*a9fa9459Szrj   constructor_callback,
148*a9fa9459Szrj   warning_callback,
149*a9fa9459Szrj   undefined_symbol,
150*a9fa9459Szrj   reloc_overflow,
151*a9fa9459Szrj   reloc_dangerous,
152*a9fa9459Szrj   unattached_reloc,
153*a9fa9459Szrj   notice,
154*a9fa9459Szrj   einfo,
155*a9fa9459Szrj   info_msg,
156*a9fa9459Szrj   minfo,
157*a9fa9459Szrj   ldlang_override_segment_assignment
158*a9fa9459Szrj };
159*a9fa9459Szrj 
160*a9fa9459Szrj static bfd_assert_handler_type default_bfd_assert_handler;
161*a9fa9459Szrj 
162*a9fa9459Szrj struct bfd_link_info link_info;
163*a9fa9459Szrj 
164*a9fa9459Szrj static void
ld_cleanup(void)165*a9fa9459Szrj ld_cleanup (void)
166*a9fa9459Szrj {
167*a9fa9459Szrj   bfd_cache_close_all ();
168*a9fa9459Szrj #ifdef ENABLE_PLUGINS
169*a9fa9459Szrj   plugin_call_cleanup ();
170*a9fa9459Szrj #endif
171*a9fa9459Szrj   if (output_filename && delete_output_file_on_failure)
172*a9fa9459Szrj     unlink_if_ordinary (output_filename);
173*a9fa9459Szrj }
174*a9fa9459Szrj 
175*a9fa9459Szrj /* If there's a BFD assertion, we'll notice and exit with an error
176*a9fa9459Szrj    unless otherwise instructed.  */
177*a9fa9459Szrj 
178*a9fa9459Szrj static void
ld_bfd_assert_handler(const char * fmt,const char * bfdver,const char * file,int line)179*a9fa9459Szrj ld_bfd_assert_handler (const char *fmt, const char *bfdver,
180*a9fa9459Szrj 		       const char *file, int line)
181*a9fa9459Szrj {
182*a9fa9459Szrj   (*default_bfd_assert_handler) (fmt, bfdver, file, line);
183*a9fa9459Szrj   config.make_executable = FALSE;
184*a9fa9459Szrj }
185*a9fa9459Szrj 
186*a9fa9459Szrj int
main(int argc,char ** argv)187*a9fa9459Szrj main (int argc, char **argv)
188*a9fa9459Szrj {
189*a9fa9459Szrj   char *emulation;
190*a9fa9459Szrj   long start_time = get_run_time ();
191*a9fa9459Szrj #ifdef HAVE_SBRK
192*a9fa9459Szrj   char *start_sbrk = (char *) sbrk (0);
193*a9fa9459Szrj #endif
194*a9fa9459Szrj 
195*a9fa9459Szrj #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
196*a9fa9459Szrj   setlocale (LC_MESSAGES, "");
197*a9fa9459Szrj #endif
198*a9fa9459Szrj #if defined (HAVE_SETLOCALE)
199*a9fa9459Szrj   setlocale (LC_CTYPE, "");
200*a9fa9459Szrj #endif
201*a9fa9459Szrj   bindtextdomain (PACKAGE, LOCALEDIR);
202*a9fa9459Szrj   textdomain (PACKAGE);
203*a9fa9459Szrj 
204*a9fa9459Szrj   program_name = argv[0];
205*a9fa9459Szrj   xmalloc_set_program_name (program_name);
206*a9fa9459Szrj 
207*a9fa9459Szrj   START_PROGRESS (program_name, 0);
208*a9fa9459Szrj 
209*a9fa9459Szrj   expandargv (&argc, &argv);
210*a9fa9459Szrj 
211*a9fa9459Szrj   bfd_init ();
212*a9fa9459Szrj 
213*a9fa9459Szrj   bfd_set_error_program_name (program_name);
214*a9fa9459Szrj 
215*a9fa9459Szrj   /* We want to notice and fail on those nasty BFD assertions which are
216*a9fa9459Szrj      likely to signal incorrect output being generated but otherwise may
217*a9fa9459Szrj      leave no trace.  */
218*a9fa9459Szrj   default_bfd_assert_handler = bfd_set_assert_handler (ld_bfd_assert_handler);
219*a9fa9459Szrj 
220*a9fa9459Szrj   xatexit (ld_cleanup);
221*a9fa9459Szrj 
222*a9fa9459Szrj   /* Set up the sysroot directory.  */
223*a9fa9459Szrj   ld_sysroot = get_sysroot (argc, argv);
224*a9fa9459Szrj   if (*ld_sysroot)
225*a9fa9459Szrj     ld_canon_sysroot = lrealpath (ld_sysroot);
226*a9fa9459Szrj   if (ld_canon_sysroot)
227*a9fa9459Szrj     ld_canon_sysroot_len = strlen (ld_canon_sysroot);
228*a9fa9459Szrj   else
229*a9fa9459Szrj     ld_canon_sysroot_len = -1;
230*a9fa9459Szrj 
231*a9fa9459Szrj   /* Set the default BFD target based on the configured target.  Doing
232*a9fa9459Szrj      this permits the linker to be configured for a particular target,
233*a9fa9459Szrj      and linked against a shared BFD library which was configured for
234*a9fa9459Szrj      a different target.  The macro TARGET is defined by Makefile.  */
235*a9fa9459Szrj   if (!bfd_set_default_target (TARGET))
236*a9fa9459Szrj     {
237*a9fa9459Szrj       einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
238*a9fa9459Szrj       xexit (1);
239*a9fa9459Szrj     }
240*a9fa9459Szrj 
241*a9fa9459Szrj #if YYDEBUG
242*a9fa9459Szrj   {
243*a9fa9459Szrj     extern int yydebug;
244*a9fa9459Szrj     yydebug = 1;
245*a9fa9459Szrj   }
246*a9fa9459Szrj #endif
247*a9fa9459Szrj 
248*a9fa9459Szrj   config.build_constructors = TRUE;
249*a9fa9459Szrj   config.rpath_separator = ':';
250*a9fa9459Szrj   config.split_by_reloc = (unsigned) -1;
251*a9fa9459Szrj   config.split_by_file = (bfd_size_type) -1;
252*a9fa9459Szrj   config.make_executable = TRUE;
253*a9fa9459Szrj   config.magic_demand_paged = TRUE;
254*a9fa9459Szrj   config.text_read_only = TRUE;
255*a9fa9459Szrj   link_info.disable_target_specific_optimizations = -1;
256*a9fa9459Szrj 
257*a9fa9459Szrj   command_line.warn_mismatch = TRUE;
258*a9fa9459Szrj   command_line.warn_search_mismatch = TRUE;
259*a9fa9459Szrj   command_line.check_section_addresses = -1;
260*a9fa9459Szrj 
261*a9fa9459Szrj   /* We initialize DEMANGLING based on the environment variable
262*a9fa9459Szrj      COLLECT_NO_DEMANGLE.  The gcc collect2 program will demangle the
263*a9fa9459Szrj      output of the linker, unless COLLECT_NO_DEMANGLE is set in the
264*a9fa9459Szrj      environment.  Acting the same way here lets us provide the same
265*a9fa9459Szrj      interface by default.  */
266*a9fa9459Szrj   demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
267*a9fa9459Szrj 
268*a9fa9459Szrj   link_info.allow_undefined_version = TRUE;
269*a9fa9459Szrj   link_info.keep_memory = TRUE;
270*a9fa9459Szrj   link_info.combreloc = TRUE;
271*a9fa9459Szrj   link_info.strip_discarded = TRUE;
272*a9fa9459Szrj   link_info.emit_hash = TRUE;
273*a9fa9459Szrj   link_info.callbacks = &link_callbacks;
274*a9fa9459Szrj   link_info.input_bfds_tail = &link_info.input_bfds;
275*a9fa9459Szrj   /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
276*a9fa9459Szrj      and _fini symbols.  We are compatible.  */
277*a9fa9459Szrj   link_info.init_function = "_init";
278*a9fa9459Szrj   link_info.fini_function = "_fini";
279*a9fa9459Szrj   link_info.relax_pass = 1;
280*a9fa9459Szrj   link_info.extern_protected_data = -1;
281*a9fa9459Szrj   link_info.dynamic_undefined_weak = -1;
282*a9fa9459Szrj   link_info.pei386_auto_import = -1;
283*a9fa9459Szrj   link_info.spare_dynamic_tags = 5;
284*a9fa9459Szrj   link_info.path_separator = ':';
285*a9fa9459Szrj #ifdef DEFAULT_FLAG_COMPRESS_DEBUG
286*a9fa9459Szrj   link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
287*a9fa9459Szrj #endif
288*a9fa9459Szrj 
289*a9fa9459Szrj   ldfile_add_arch ("");
290*a9fa9459Szrj   emulation = get_emulation (argc, argv);
291*a9fa9459Szrj   ldemul_choose_mode (emulation);
292*a9fa9459Szrj   default_target = ldemul_choose_target (argc, argv);
293*a9fa9459Szrj   config.maxpagesize = bfd_emul_get_maxpagesize (default_target);
294*a9fa9459Szrj   config.commonpagesize = bfd_emul_get_commonpagesize (default_target);
295*a9fa9459Szrj   lang_init ();
296*a9fa9459Szrj   ldexp_init ();
297*a9fa9459Szrj   ldemul_before_parse ();
298*a9fa9459Szrj   lang_has_input_file = FALSE;
299*a9fa9459Szrj   parse_args (argc, argv);
300*a9fa9459Szrj 
301*a9fa9459Szrj   if (config.hash_table_size != 0)
302*a9fa9459Szrj     bfd_hash_set_default_size (config.hash_table_size);
303*a9fa9459Szrj 
304*a9fa9459Szrj #ifdef ENABLE_PLUGINS
305*a9fa9459Szrj   /* Now all the plugin arguments have been gathered, we can load them.  */
306*a9fa9459Szrj   plugin_load_plugins ();
307*a9fa9459Szrj #endif /* ENABLE_PLUGINS */
308*a9fa9459Szrj 
309*a9fa9459Szrj   ldemul_set_symbols ();
310*a9fa9459Szrj 
311*a9fa9459Szrj   /* If we have not already opened and parsed a linker script,
312*a9fa9459Szrj      try the default script from command line first.  */
313*a9fa9459Szrj   if (saved_script_handle == NULL
314*a9fa9459Szrj       && command_line.default_script != NULL)
315*a9fa9459Szrj     {
316*a9fa9459Szrj       ldfile_open_command_file (command_line.default_script);
317*a9fa9459Szrj       parser_input = input_script;
318*a9fa9459Szrj       yyparse ();
319*a9fa9459Szrj     }
320*a9fa9459Szrj 
321*a9fa9459Szrj   /* If we have not already opened and parsed a linker script
322*a9fa9459Szrj      read the emulation's appropriate default script.  */
323*a9fa9459Szrj   if (saved_script_handle == NULL)
324*a9fa9459Szrj     {
325*a9fa9459Szrj       int isfile;
326*a9fa9459Szrj       char *s = ldemul_get_script (&isfile);
327*a9fa9459Szrj 
328*a9fa9459Szrj       if (isfile)
329*a9fa9459Szrj 	ldfile_open_default_command_file (s);
330*a9fa9459Szrj       else
331*a9fa9459Szrj 	{
332*a9fa9459Szrj 	  lex_string = s;
333*a9fa9459Szrj 	  lex_redirect (s, _("built in linker script"), 1);
334*a9fa9459Szrj 	}
335*a9fa9459Szrj       parser_input = input_script;
336*a9fa9459Szrj       yyparse ();
337*a9fa9459Szrj       lex_string = NULL;
338*a9fa9459Szrj     }
339*a9fa9459Szrj 
340*a9fa9459Szrj   if (verbose)
341*a9fa9459Szrj     {
342*a9fa9459Szrj       if (saved_script_handle)
343*a9fa9459Szrj 	info_msg (_("using external linker script:"));
344*a9fa9459Szrj       else
345*a9fa9459Szrj 	info_msg (_("using internal linker script:"));
346*a9fa9459Szrj       info_msg ("\n==================================================\n");
347*a9fa9459Szrj 
348*a9fa9459Szrj       if (saved_script_handle)
349*a9fa9459Szrj 	{
350*a9fa9459Szrj 	  static const int ld_bufsz = 8193;
351*a9fa9459Szrj 	  size_t n;
352*a9fa9459Szrj 	  char *buf = (char *) xmalloc (ld_bufsz);
353*a9fa9459Szrj 
354*a9fa9459Szrj 	  rewind (saved_script_handle);
355*a9fa9459Szrj 	  while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
356*a9fa9459Szrj 	    {
357*a9fa9459Szrj 	      buf[n] = 0;
358*a9fa9459Szrj 	      info_msg (buf);
359*a9fa9459Szrj 	    }
360*a9fa9459Szrj 	  rewind (saved_script_handle);
361*a9fa9459Szrj 	  free (buf);
362*a9fa9459Szrj 	}
363*a9fa9459Szrj       else
364*a9fa9459Szrj 	{
365*a9fa9459Szrj 	  int isfile;
366*a9fa9459Szrj 
367*a9fa9459Szrj 	  info_msg (ldemul_get_script (&isfile));
368*a9fa9459Szrj 	}
369*a9fa9459Szrj 
370*a9fa9459Szrj       info_msg ("\n==================================================\n");
371*a9fa9459Szrj     }
372*a9fa9459Szrj 
373*a9fa9459Szrj   if (command_line.print_output_format)
374*a9fa9459Szrj     info_msg ("%s\n", lang_get_output_target ());
375*a9fa9459Szrj 
376*a9fa9459Szrj   lang_final ();
377*a9fa9459Szrj 
378*a9fa9459Szrj   /* If the only command line argument has been -v or --version or --verbose
379*a9fa9459Szrj      then ignore any input files provided by linker scripts and exit now.
380*a9fa9459Szrj      We do not want to create an output file when the linker is just invoked
381*a9fa9459Szrj      to provide version information.  */
382*a9fa9459Szrj   if (argc == 2 && version_printed)
383*a9fa9459Szrj     xexit (0);
384*a9fa9459Szrj 
385*a9fa9459Szrj   if (!lang_has_input_file)
386*a9fa9459Szrj     {
387*a9fa9459Szrj       if (version_printed || command_line.print_output_format)
388*a9fa9459Szrj 	xexit (0);
389*a9fa9459Szrj       einfo (_("%P%F: no input files\n"));
390*a9fa9459Szrj     }
391*a9fa9459Szrj 
392*a9fa9459Szrj   if (trace_files)
393*a9fa9459Szrj     info_msg (_("%P: mode %s\n"), emulation);
394*a9fa9459Szrj 
395*a9fa9459Szrj   ldemul_after_parse ();
396*a9fa9459Szrj 
397*a9fa9459Szrj   if (config.map_filename)
398*a9fa9459Szrj     {
399*a9fa9459Szrj       if (strcmp (config.map_filename, "-") == 0)
400*a9fa9459Szrj 	{
401*a9fa9459Szrj 	  config.map_file = stdout;
402*a9fa9459Szrj 	}
403*a9fa9459Szrj       else
404*a9fa9459Szrj 	{
405*a9fa9459Szrj 	  config.map_file = fopen (config.map_filename, FOPEN_WT);
406*a9fa9459Szrj 	  if (config.map_file == (FILE *) NULL)
407*a9fa9459Szrj 	    {
408*a9fa9459Szrj 	      bfd_set_error (bfd_error_system_call);
409*a9fa9459Szrj 	      einfo (_("%P%F: cannot open map file %s: %E\n"),
410*a9fa9459Szrj 		     config.map_filename);
411*a9fa9459Szrj 	    }
412*a9fa9459Szrj 	}
413*a9fa9459Szrj     }
414*a9fa9459Szrj 
415*a9fa9459Szrj   lang_process ();
416*a9fa9459Szrj 
417*a9fa9459Szrj   /* Print error messages for any missing symbols, for any warning
418*a9fa9459Szrj      symbols, and possibly multiple definitions.  */
419*a9fa9459Szrj   if (bfd_link_relocatable (&link_info))
420*a9fa9459Szrj     link_info.output_bfd->flags &= ~EXEC_P;
421*a9fa9459Szrj   else
422*a9fa9459Szrj     link_info.output_bfd->flags |= EXEC_P;
423*a9fa9459Szrj 
424*a9fa9459Szrj   if ((link_info.compress_debug & COMPRESS_DEBUG))
425*a9fa9459Szrj     {
426*a9fa9459Szrj       link_info.output_bfd->flags |= BFD_COMPRESS;
427*a9fa9459Szrj       if (link_info.compress_debug == COMPRESS_DEBUG_GABI_ZLIB)
428*a9fa9459Szrj 	link_info.output_bfd->flags |= BFD_COMPRESS_GABI;
429*a9fa9459Szrj     }
430*a9fa9459Szrj 
431*a9fa9459Szrj   ldwrite ();
432*a9fa9459Szrj 
433*a9fa9459Szrj   if (config.map_file != NULL)
434*a9fa9459Szrj     lang_map ();
435*a9fa9459Szrj   if (command_line.cref)
436*a9fa9459Szrj     output_cref (config.map_file != NULL ? config.map_file : stdout);
437*a9fa9459Szrj   if (nocrossref_list != NULL)
438*a9fa9459Szrj     check_nocrossrefs ();
439*a9fa9459Szrj   if (command_line.print_memory_usage)
440*a9fa9459Szrj     lang_print_memory_usage ();
441*a9fa9459Szrj #if 0
442*a9fa9459Szrj   {
443*a9fa9459Szrj     struct bfd_link_hash_entry *h;
444*a9fa9459Szrj 
445*a9fa9459Szrj     h = bfd_link_hash_lookup (link_info.hash, "__image_base__", 0,0,1);
446*a9fa9459Szrj     fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1);
447*a9fa9459Szrj   }
448*a9fa9459Szrj #endif
449*a9fa9459Szrj   ldexp_finish ();
450*a9fa9459Szrj   lang_finish ();
451*a9fa9459Szrj 
452*a9fa9459Szrj   /* Even if we're producing relocatable output, some non-fatal errors should
453*a9fa9459Szrj      be reported in the exit status.  (What non-fatal errors, if any, do we
454*a9fa9459Szrj      want to ignore for relocatable output?)  */
455*a9fa9459Szrj   if (!config.make_executable && !force_make_executable)
456*a9fa9459Szrj     {
457*a9fa9459Szrj       if (trace_files)
458*a9fa9459Szrj 	einfo (_("%P: link errors found, deleting executable `%s'\n"),
459*a9fa9459Szrj 	       output_filename);
460*a9fa9459Szrj 
461*a9fa9459Szrj       /* The file will be removed by ld_cleanup.  */
462*a9fa9459Szrj       xexit (1);
463*a9fa9459Szrj     }
464*a9fa9459Szrj   else
465*a9fa9459Szrj     {
466*a9fa9459Szrj       if (!bfd_close (link_info.output_bfd))
467*a9fa9459Szrj 	einfo (_("%F%B: final close failed: %E\n"), link_info.output_bfd);
468*a9fa9459Szrj 
469*a9fa9459Szrj       /* If the --force-exe-suffix is enabled, and we're making an
470*a9fa9459Szrj 	 executable file and it doesn't end in .exe, copy it to one
471*a9fa9459Szrj 	 which does.  */
472*a9fa9459Szrj       if (!bfd_link_relocatable (&link_info)
473*a9fa9459Szrj 	  && command_line.force_exe_suffix)
474*a9fa9459Szrj 	{
475*a9fa9459Szrj 	  int len = strlen (output_filename);
476*a9fa9459Szrj 
477*a9fa9459Szrj 	  if (len < 4
478*a9fa9459Szrj 	      || (strcasecmp (output_filename + len - 4, ".exe") != 0
479*a9fa9459Szrj 		  && strcasecmp (output_filename + len - 4, ".dll") != 0))
480*a9fa9459Szrj 	    {
481*a9fa9459Szrj 	      FILE *src;
482*a9fa9459Szrj 	      FILE *dst;
483*a9fa9459Szrj 	      const int bsize = 4096;
484*a9fa9459Szrj 	      char *buf = (char *) xmalloc (bsize);
485*a9fa9459Szrj 	      int l;
486*a9fa9459Szrj 	      char *dst_name = (char *) xmalloc (len + 5);
487*a9fa9459Szrj 
488*a9fa9459Szrj 	      strcpy (dst_name, output_filename);
489*a9fa9459Szrj 	      strcat (dst_name, ".exe");
490*a9fa9459Szrj 	      src = fopen (output_filename, FOPEN_RB);
491*a9fa9459Szrj 	      dst = fopen (dst_name, FOPEN_WB);
492*a9fa9459Szrj 
493*a9fa9459Szrj 	      if (!src)
494*a9fa9459Szrj 		einfo (_("%P%F: unable to open for source of copy `%s'\n"),
495*a9fa9459Szrj 		       output_filename);
496*a9fa9459Szrj 	      if (!dst)
497*a9fa9459Szrj 		einfo (_("%P%F: unable to open for destination of copy `%s'\n"),
498*a9fa9459Szrj 		       dst_name);
499*a9fa9459Szrj 	      while ((l = fread (buf, 1, bsize, src)) > 0)
500*a9fa9459Szrj 		{
501*a9fa9459Szrj 		  int done = fwrite (buf, 1, l, dst);
502*a9fa9459Szrj 
503*a9fa9459Szrj 		  if (done != l)
504*a9fa9459Szrj 		    einfo (_("%P: Error writing file `%s'\n"), dst_name);
505*a9fa9459Szrj 		}
506*a9fa9459Szrj 
507*a9fa9459Szrj 	      fclose (src);
508*a9fa9459Szrj 	      if (fclose (dst) == EOF)
509*a9fa9459Szrj 		einfo (_("%P: Error closing file `%s'\n"), dst_name);
510*a9fa9459Szrj 	      free (dst_name);
511*a9fa9459Szrj 	      free (buf);
512*a9fa9459Szrj 	    }
513*a9fa9459Szrj 	}
514*a9fa9459Szrj     }
515*a9fa9459Szrj 
516*a9fa9459Szrj   END_PROGRESS (program_name);
517*a9fa9459Szrj 
518*a9fa9459Szrj   if (config.stats)
519*a9fa9459Szrj     {
520*a9fa9459Szrj #ifdef HAVE_SBRK
521*a9fa9459Szrj       char *lim = (char *) sbrk (0);
522*a9fa9459Szrj #endif
523*a9fa9459Szrj       long run_time = get_run_time () - start_time;
524*a9fa9459Szrj 
525*a9fa9459Szrj       fflush (stdout);
526*a9fa9459Szrj       fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
527*a9fa9459Szrj 	       program_name, run_time / 1000000, run_time % 1000000);
528*a9fa9459Szrj #ifdef HAVE_SBRK
529*a9fa9459Szrj       fprintf (stderr, _("%s: data size %ld\n"), program_name,
530*a9fa9459Szrj 	       (long) (lim - start_sbrk));
531*a9fa9459Szrj #endif
532*a9fa9459Szrj       fflush (stderr);
533*a9fa9459Szrj     }
534*a9fa9459Szrj 
535*a9fa9459Szrj   /* Prevent ld_cleanup from doing anything, after a successful link.  */
536*a9fa9459Szrj   output_filename = NULL;
537*a9fa9459Szrj 
538*a9fa9459Szrj   xexit (0);
539*a9fa9459Szrj   return 0;
540*a9fa9459Szrj }
541*a9fa9459Szrj 
542*a9fa9459Szrj /* If the configured sysroot is relocatable, try relocating it based on
543*a9fa9459Szrj    default prefix FROM.  Return the relocated directory if it exists,
544*a9fa9459Szrj    otherwise return null.  */
545*a9fa9459Szrj 
546*a9fa9459Szrj static char *
get_relative_sysroot(const char * from ATTRIBUTE_UNUSED)547*a9fa9459Szrj get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
548*a9fa9459Szrj {
549*a9fa9459Szrj #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
550*a9fa9459Szrj   char *path;
551*a9fa9459Szrj   struct stat s;
552*a9fa9459Szrj 
553*a9fa9459Szrj   path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
554*a9fa9459Szrj   if (path)
555*a9fa9459Szrj     {
556*a9fa9459Szrj       if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
557*a9fa9459Szrj 	return path;
558*a9fa9459Szrj       free (path);
559*a9fa9459Szrj     }
560*a9fa9459Szrj #endif
561*a9fa9459Szrj   return 0;
562*a9fa9459Szrj }
563*a9fa9459Szrj 
564*a9fa9459Szrj /* Return the sysroot directory.  Return "" if no sysroot is being used.  */
565*a9fa9459Szrj 
566*a9fa9459Szrj static const char *
get_sysroot(int argc,char ** argv)567*a9fa9459Szrj get_sysroot (int argc, char **argv)
568*a9fa9459Szrj {
569*a9fa9459Szrj   int i;
570*a9fa9459Szrj   const char *path;
571*a9fa9459Szrj 
572*a9fa9459Szrj   for (i = 1; i < argc; i++)
573*a9fa9459Szrj     if (CONST_STRNEQ (argv[i], "--sysroot="))
574*a9fa9459Szrj       return argv[i] + strlen ("--sysroot=");
575*a9fa9459Szrj 
576*a9fa9459Szrj   path = get_relative_sysroot (BINDIR);
577*a9fa9459Szrj   if (path)
578*a9fa9459Szrj     return path;
579*a9fa9459Szrj 
580*a9fa9459Szrj   path = get_relative_sysroot (TOOLBINDIR);
581*a9fa9459Szrj   if (path)
582*a9fa9459Szrj     return path;
583*a9fa9459Szrj 
584*a9fa9459Szrj   return TARGET_SYSTEM_ROOT;
585*a9fa9459Szrj }
586*a9fa9459Szrj 
587*a9fa9459Szrj /* We need to find any explicitly given emulation in order to initialize the
588*a9fa9459Szrj    state that's needed by the lex&yacc argument parser (parse_args).  */
589*a9fa9459Szrj 
590*a9fa9459Szrj static char *
get_emulation(int argc,char ** argv)591*a9fa9459Szrj get_emulation (int argc, char **argv)
592*a9fa9459Szrj {
593*a9fa9459Szrj   char *emulation;
594*a9fa9459Szrj   int i;
595*a9fa9459Szrj 
596*a9fa9459Szrj   emulation = getenv (EMULATION_ENVIRON);
597*a9fa9459Szrj   if (emulation == NULL)
598*a9fa9459Szrj     emulation = DEFAULT_EMULATION;
599*a9fa9459Szrj 
600*a9fa9459Szrj   for (i = 1; i < argc; i++)
601*a9fa9459Szrj     {
602*a9fa9459Szrj       if (CONST_STRNEQ (argv[i], "-m"))
603*a9fa9459Szrj 	{
604*a9fa9459Szrj 	  if (argv[i][2] == '\0')
605*a9fa9459Szrj 	    {
606*a9fa9459Szrj 	      /* -m EMUL */
607*a9fa9459Szrj 	      if (i < argc - 1)
608*a9fa9459Szrj 		{
609*a9fa9459Szrj 		  emulation = argv[i + 1];
610*a9fa9459Szrj 		  i++;
611*a9fa9459Szrj 		}
612*a9fa9459Szrj 	      else
613*a9fa9459Szrj 		einfo (_("%P%F: missing argument to -m\n"));
614*a9fa9459Szrj 	    }
615*a9fa9459Szrj 	  else if (strcmp (argv[i], "-mips1") == 0
616*a9fa9459Szrj 		   || strcmp (argv[i], "-mips2") == 0
617*a9fa9459Szrj 		   || strcmp (argv[i], "-mips3") == 0
618*a9fa9459Szrj 		   || strcmp (argv[i], "-mips4") == 0
619*a9fa9459Szrj 		   || strcmp (argv[i], "-mips5") == 0
620*a9fa9459Szrj 		   || strcmp (argv[i], "-mips32") == 0
621*a9fa9459Szrj 		   || strcmp (argv[i], "-mips32r2") == 0
622*a9fa9459Szrj 		   || strcmp (argv[i], "-mips32r6") == 0
623*a9fa9459Szrj 		   || strcmp (argv[i], "-mips64") == 0
624*a9fa9459Szrj 		   || strcmp (argv[i], "-mips64r2") == 0
625*a9fa9459Szrj 		   || strcmp (argv[i], "-mips64r6") == 0)
626*a9fa9459Szrj 	    {
627*a9fa9459Szrj 	      /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
628*a9fa9459Szrj 		 passed to the linker by some MIPS compilers.  They
629*a9fa9459Szrj 		 generally tell the linker to use a slightly different
630*a9fa9459Szrj 		 library path.  Perhaps someday these should be
631*a9fa9459Szrj 		 implemented as emulations; until then, we just ignore
632*a9fa9459Szrj 		 the arguments and hope that nobody ever creates
633*a9fa9459Szrj 		 emulations named ips1, ips2 or ips3.  */
634*a9fa9459Szrj 	    }
635*a9fa9459Szrj 	  else if (strcmp (argv[i], "-m486") == 0)
636*a9fa9459Szrj 	    {
637*a9fa9459Szrj 	      /* FIXME: The argument -m486 is passed to the linker on
638*a9fa9459Szrj 		 some Linux systems.  Hope that nobody creates an
639*a9fa9459Szrj 		 emulation named 486.  */
640*a9fa9459Szrj 	    }
641*a9fa9459Szrj 	  else
642*a9fa9459Szrj 	    {
643*a9fa9459Szrj 	      /* -mEMUL */
644*a9fa9459Szrj 	      emulation = &argv[i][2];
645*a9fa9459Szrj 	    }
646*a9fa9459Szrj 	}
647*a9fa9459Szrj     }
648*a9fa9459Szrj 
649*a9fa9459Szrj   return emulation;
650*a9fa9459Szrj }
651*a9fa9459Szrj 
652*a9fa9459Szrj void
add_ysym(const char * name)653*a9fa9459Szrj add_ysym (const char *name)
654*a9fa9459Szrj {
655*a9fa9459Szrj   if (link_info.notice_hash == NULL)
656*a9fa9459Szrj     {
657*a9fa9459Szrj       link_info.notice_hash
658*a9fa9459Szrj 	= (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
659*a9fa9459Szrj       if (!bfd_hash_table_init_n (link_info.notice_hash,
660*a9fa9459Szrj 				  bfd_hash_newfunc,
661*a9fa9459Szrj 				  sizeof (struct bfd_hash_entry),
662*a9fa9459Szrj 				  61))
663*a9fa9459Szrj 	einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
664*a9fa9459Szrj     }
665*a9fa9459Szrj 
666*a9fa9459Szrj   if (bfd_hash_lookup (link_info.notice_hash, name, TRUE, TRUE) == NULL)
667*a9fa9459Szrj     einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
668*a9fa9459Szrj }
669*a9fa9459Szrj 
670*a9fa9459Szrj void
add_ignoresym(struct bfd_link_info * info,const char * name)671*a9fa9459Szrj add_ignoresym (struct bfd_link_info *info, const char *name)
672*a9fa9459Szrj {
673*a9fa9459Szrj   if (info->ignore_hash == NULL)
674*a9fa9459Szrj     {
675*a9fa9459Szrj       info->ignore_hash = xmalloc (sizeof (struct bfd_hash_table));
676*a9fa9459Szrj       if (!bfd_hash_table_init_n (info->ignore_hash,
677*a9fa9459Szrj 				  bfd_hash_newfunc,
678*a9fa9459Szrj 				  sizeof (struct bfd_hash_entry),
679*a9fa9459Szrj 				  61))
680*a9fa9459Szrj 	einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
681*a9fa9459Szrj     }
682*a9fa9459Szrj 
683*a9fa9459Szrj   if (bfd_hash_lookup (info->ignore_hash, name, TRUE, TRUE) == NULL)
684*a9fa9459Szrj     einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
685*a9fa9459Szrj }
686*a9fa9459Szrj 
687*a9fa9459Szrj /* Record a symbol to be wrapped, from the --wrap option.  */
688*a9fa9459Szrj 
689*a9fa9459Szrj void
add_wrap(const char * name)690*a9fa9459Szrj add_wrap (const char *name)
691*a9fa9459Szrj {
692*a9fa9459Szrj   if (link_info.wrap_hash == NULL)
693*a9fa9459Szrj     {
694*a9fa9459Szrj       link_info.wrap_hash
695*a9fa9459Szrj 	= (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
696*a9fa9459Szrj       if (!bfd_hash_table_init_n (link_info.wrap_hash,
697*a9fa9459Szrj 				  bfd_hash_newfunc,
698*a9fa9459Szrj 				  sizeof (struct bfd_hash_entry),
699*a9fa9459Szrj 				  61))
700*a9fa9459Szrj 	einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
701*a9fa9459Szrj     }
702*a9fa9459Szrj 
703*a9fa9459Szrj   if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL)
704*a9fa9459Szrj     einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
705*a9fa9459Szrj }
706*a9fa9459Szrj 
707*a9fa9459Szrj /* Handle the -retain-symbols-file option.  */
708*a9fa9459Szrj 
709*a9fa9459Szrj void
add_keepsyms_file(const char * filename)710*a9fa9459Szrj add_keepsyms_file (const char *filename)
711*a9fa9459Szrj {
712*a9fa9459Szrj   FILE *file;
713*a9fa9459Szrj   char *buf;
714*a9fa9459Szrj   size_t bufsize;
715*a9fa9459Szrj   int c;
716*a9fa9459Szrj 
717*a9fa9459Szrj   if (link_info.strip == strip_some)
718*a9fa9459Szrj     einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
719*a9fa9459Szrj 
720*a9fa9459Szrj   file = fopen (filename, "r");
721*a9fa9459Szrj   if (file == NULL)
722*a9fa9459Szrj     {
723*a9fa9459Szrj       bfd_set_error (bfd_error_system_call);
724*a9fa9459Szrj       einfo ("%X%P: %s: %E\n", filename);
725*a9fa9459Szrj       return;
726*a9fa9459Szrj     }
727*a9fa9459Szrj 
728*a9fa9459Szrj   link_info.keep_hash = (struct bfd_hash_table *)
729*a9fa9459Szrj       xmalloc (sizeof (struct bfd_hash_table));
730*a9fa9459Szrj   if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc,
731*a9fa9459Szrj 			    sizeof (struct bfd_hash_entry)))
732*a9fa9459Szrj     einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
733*a9fa9459Szrj 
734*a9fa9459Szrj   bufsize = 100;
735*a9fa9459Szrj   buf = (char *) xmalloc (bufsize);
736*a9fa9459Szrj 
737*a9fa9459Szrj   c = getc (file);
738*a9fa9459Szrj   while (c != EOF)
739*a9fa9459Szrj     {
740*a9fa9459Szrj       while (ISSPACE (c))
741*a9fa9459Szrj 	c = getc (file);
742*a9fa9459Szrj 
743*a9fa9459Szrj       if (c != EOF)
744*a9fa9459Szrj 	{
745*a9fa9459Szrj 	  size_t len = 0;
746*a9fa9459Szrj 
747*a9fa9459Szrj 	  while (!ISSPACE (c) && c != EOF)
748*a9fa9459Szrj 	    {
749*a9fa9459Szrj 	      buf[len] = c;
750*a9fa9459Szrj 	      ++len;
751*a9fa9459Szrj 	      if (len >= bufsize)
752*a9fa9459Szrj 		{
753*a9fa9459Szrj 		  bufsize *= 2;
754*a9fa9459Szrj 		  buf = (char *) xrealloc (buf, bufsize);
755*a9fa9459Szrj 		}
756*a9fa9459Szrj 	      c = getc (file);
757*a9fa9459Szrj 	    }
758*a9fa9459Szrj 
759*a9fa9459Szrj 	  buf[len] = '\0';
760*a9fa9459Szrj 
761*a9fa9459Szrj 	  if (bfd_hash_lookup (link_info.keep_hash, buf, TRUE, TRUE) == NULL)
762*a9fa9459Szrj 	    einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
763*a9fa9459Szrj 	}
764*a9fa9459Szrj     }
765*a9fa9459Szrj 
766*a9fa9459Szrj   if (link_info.strip != strip_none)
767*a9fa9459Szrj     einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
768*a9fa9459Szrj 
769*a9fa9459Szrj   free (buf);
770*a9fa9459Szrj   link_info.strip = strip_some;
771*a9fa9459Szrj   fclose (file);
772*a9fa9459Szrj }
773*a9fa9459Szrj 
774*a9fa9459Szrj /* Callbacks from the BFD linker routines.  */
775*a9fa9459Szrj 
776*a9fa9459Szrj /* This is called when BFD has decided to include an archive member in
777*a9fa9459Szrj    a link.  */
778*a9fa9459Szrj 
779*a9fa9459Szrj static bfd_boolean
add_archive_element(struct bfd_link_info * info,bfd * abfd,const char * name,bfd ** subsbfd ATTRIBUTE_UNUSED)780*a9fa9459Szrj add_archive_element (struct bfd_link_info *info,
781*a9fa9459Szrj 		     bfd *abfd,
782*a9fa9459Szrj 		     const char *name,
783*a9fa9459Szrj 		     bfd **subsbfd ATTRIBUTE_UNUSED)
784*a9fa9459Szrj {
785*a9fa9459Szrj   lang_input_statement_type *input;
786*a9fa9459Szrj   lang_input_statement_type orig_input;
787*a9fa9459Szrj 
788*a9fa9459Szrj   input = (lang_input_statement_type *)
789*a9fa9459Szrj       xcalloc (1, sizeof (lang_input_statement_type));
790*a9fa9459Szrj   input->filename = abfd->filename;
791*a9fa9459Szrj   input->local_sym_name = abfd->filename;
792*a9fa9459Szrj   input->the_bfd = abfd;
793*a9fa9459Szrj 
794*a9fa9459Szrj   /* Save the original data for trace files/tries below, as plugins
795*a9fa9459Szrj      (if enabled) may possibly alter it to point to a replacement
796*a9fa9459Szrj      BFD, but we still want to output the original BFD filename.  */
797*a9fa9459Szrj   orig_input = *input;
798*a9fa9459Szrj #ifdef ENABLE_PLUGINS
799*a9fa9459Szrj   if (link_info.lto_plugin_active)
800*a9fa9459Szrj     {
801*a9fa9459Szrj       /* We must offer this archive member to the plugins to claim.  */
802*a9fa9459Szrj       plugin_maybe_claim (input);
803*a9fa9459Szrj       if (input->flags.claimed)
804*a9fa9459Szrj 	{
805*a9fa9459Szrj 	  if (no_more_claiming)
806*a9fa9459Szrj 	    {
807*a9fa9459Szrj 	      /* Don't claim new IR symbols after all IR symbols have
808*a9fa9459Szrj 		 been claimed.  */
809*a9fa9459Szrj 	      if (trace_files || verbose)
810*a9fa9459Szrj 		info_msg ("%I: no new IR symbols to claimi\n",
811*a9fa9459Szrj 			  &orig_input);
812*a9fa9459Szrj 	      input->flags.claimed = 0;
813*a9fa9459Szrj 	      return FALSE;
814*a9fa9459Szrj 	    }
815*a9fa9459Szrj 	  input->flags.claim_archive = TRUE;
816*a9fa9459Szrj 	  *subsbfd = input->the_bfd;
817*a9fa9459Szrj 	}
818*a9fa9459Szrj     }
819*a9fa9459Szrj #endif /* ENABLE_PLUGINS */
820*a9fa9459Szrj 
821*a9fa9459Szrj   ldlang_add_file (input);
822*a9fa9459Szrj 
823*a9fa9459Szrj   if (config.map_file != NULL)
824*a9fa9459Szrj     {
825*a9fa9459Szrj       static bfd_boolean header_printed;
826*a9fa9459Szrj       struct bfd_link_hash_entry *h;
827*a9fa9459Szrj       bfd *from;
828*a9fa9459Szrj       int len;
829*a9fa9459Szrj 
830*a9fa9459Szrj       h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
831*a9fa9459Szrj 
832*a9fa9459Szrj       if (h == NULL)
833*a9fa9459Szrj 	from = NULL;
834*a9fa9459Szrj       else
835*a9fa9459Szrj 	{
836*a9fa9459Szrj 	  switch (h->type)
837*a9fa9459Szrj 	    {
838*a9fa9459Szrj 	    default:
839*a9fa9459Szrj 	      from = NULL;
840*a9fa9459Szrj 	      break;
841*a9fa9459Szrj 
842*a9fa9459Szrj 	    case bfd_link_hash_defined:
843*a9fa9459Szrj 	    case bfd_link_hash_defweak:
844*a9fa9459Szrj 	      from = h->u.def.section->owner;
845*a9fa9459Szrj 	      break;
846*a9fa9459Szrj 
847*a9fa9459Szrj 	    case bfd_link_hash_undefined:
848*a9fa9459Szrj 	    case bfd_link_hash_undefweak:
849*a9fa9459Szrj 	      from = h->u.undef.abfd;
850*a9fa9459Szrj 	      break;
851*a9fa9459Szrj 
852*a9fa9459Szrj 	    case bfd_link_hash_common:
853*a9fa9459Szrj 	      from = h->u.c.p->section->owner;
854*a9fa9459Szrj 	      break;
855*a9fa9459Szrj 	    }
856*a9fa9459Szrj 	}
857*a9fa9459Szrj 
858*a9fa9459Szrj       if (!header_printed)
859*a9fa9459Szrj 	{
860*a9fa9459Szrj 	  char buf[100];
861*a9fa9459Szrj 
862*a9fa9459Szrj 	  sprintf (buf, _("Archive member included "
863*a9fa9459Szrj 			  "to satisfy reference by file (symbol)\n\n"));
864*a9fa9459Szrj 	  minfo ("%s", buf);
865*a9fa9459Szrj 	  header_printed = TRUE;
866*a9fa9459Szrj 	}
867*a9fa9459Szrj 
868*a9fa9459Szrj       if (abfd->my_archive == NULL
869*a9fa9459Szrj 	  || bfd_is_thin_archive (abfd->my_archive))
870*a9fa9459Szrj 	{
871*a9fa9459Szrj 	  minfo ("%s", bfd_get_filename (abfd));
872*a9fa9459Szrj 	  len = strlen (bfd_get_filename (abfd));
873*a9fa9459Szrj 	}
874*a9fa9459Szrj       else
875*a9fa9459Szrj 	{
876*a9fa9459Szrj 	  minfo ("%s(%s)", bfd_get_filename (abfd->my_archive),
877*a9fa9459Szrj 		 bfd_get_filename (abfd));
878*a9fa9459Szrj 	  len = (strlen (bfd_get_filename (abfd->my_archive))
879*a9fa9459Szrj 		 + strlen (bfd_get_filename (abfd))
880*a9fa9459Szrj 		 + 2);
881*a9fa9459Szrj 	}
882*a9fa9459Szrj 
883*a9fa9459Szrj       if (len >= 29)
884*a9fa9459Szrj 	{
885*a9fa9459Szrj 	  print_nl ();
886*a9fa9459Szrj 	  len = 0;
887*a9fa9459Szrj 	}
888*a9fa9459Szrj       while (len < 30)
889*a9fa9459Szrj 	{
890*a9fa9459Szrj 	  print_space ();
891*a9fa9459Szrj 	  ++len;
892*a9fa9459Szrj 	}
893*a9fa9459Szrj 
894*a9fa9459Szrj       if (from != NULL)
895*a9fa9459Szrj 	minfo ("%B ", from);
896*a9fa9459Szrj       if (h != NULL)
897*a9fa9459Szrj 	minfo ("(%T)\n", h->root.string);
898*a9fa9459Szrj       else
899*a9fa9459Szrj 	minfo ("(%s)\n", name);
900*a9fa9459Szrj     }
901*a9fa9459Szrj 
902*a9fa9459Szrj   if (trace_files || verbose)
903*a9fa9459Szrj     info_msg ("%I\n", &orig_input);
904*a9fa9459Szrj   return TRUE;
905*a9fa9459Szrj }
906*a9fa9459Szrj 
907*a9fa9459Szrj /* This is called when BFD has discovered a symbol which is defined
908*a9fa9459Szrj    multiple times.  */
909*a9fa9459Szrj 
910*a9fa9459Szrj static void
multiple_definition(struct bfd_link_info * info,struct bfd_link_hash_entry * h,bfd * nbfd,asection * nsec,bfd_vma nval)911*a9fa9459Szrj multiple_definition (struct bfd_link_info *info,
912*a9fa9459Szrj 		     struct bfd_link_hash_entry *h,
913*a9fa9459Szrj 		     bfd *nbfd,
914*a9fa9459Szrj 		     asection *nsec,
915*a9fa9459Szrj 		     bfd_vma nval)
916*a9fa9459Szrj {
917*a9fa9459Szrj   const char *name;
918*a9fa9459Szrj   bfd *obfd;
919*a9fa9459Szrj   asection *osec;
920*a9fa9459Szrj   bfd_vma oval;
921*a9fa9459Szrj 
922*a9fa9459Szrj   if (info->allow_multiple_definition)
923*a9fa9459Szrj     return;
924*a9fa9459Szrj 
925*a9fa9459Szrj   switch (h->type)
926*a9fa9459Szrj     {
927*a9fa9459Szrj     case bfd_link_hash_defined:
928*a9fa9459Szrj       osec = h->u.def.section;
929*a9fa9459Szrj       oval = h->u.def.value;
930*a9fa9459Szrj       obfd = h->u.def.section->owner;
931*a9fa9459Szrj       break;
932*a9fa9459Szrj     case bfd_link_hash_indirect:
933*a9fa9459Szrj       osec = bfd_ind_section_ptr;
934*a9fa9459Szrj       oval = 0;
935*a9fa9459Szrj       obfd = NULL;
936*a9fa9459Szrj       break;
937*a9fa9459Szrj     default:
938*a9fa9459Szrj       abort ();
939*a9fa9459Szrj     }
940*a9fa9459Szrj 
941*a9fa9459Szrj   /* Ignore a redefinition of an absolute symbol to the
942*a9fa9459Szrj      same value; it's harmless.  */
943*a9fa9459Szrj   if (h->type == bfd_link_hash_defined
944*a9fa9459Szrj       && bfd_is_abs_section (osec)
945*a9fa9459Szrj       && bfd_is_abs_section (nsec)
946*a9fa9459Szrj       && nval == oval)
947*a9fa9459Szrj     return;
948*a9fa9459Szrj 
949*a9fa9459Szrj   /* If either section has the output_section field set to
950*a9fa9459Szrj      bfd_abs_section_ptr, it means that the section is being
951*a9fa9459Szrj      discarded, and this is not really a multiple definition at all.
952*a9fa9459Szrj      FIXME: It would be cleaner to somehow ignore symbols defined in
953*a9fa9459Szrj      sections which are being discarded.  */
954*a9fa9459Szrj   if ((osec->output_section != NULL
955*a9fa9459Szrj        && !bfd_is_abs_section (osec)
956*a9fa9459Szrj        && bfd_is_abs_section (osec->output_section))
957*a9fa9459Szrj       || (nsec->output_section != NULL
958*a9fa9459Szrj 	  && !bfd_is_abs_section (nsec)
959*a9fa9459Szrj 	  && bfd_is_abs_section (nsec->output_section)))
960*a9fa9459Szrj     return;
961*a9fa9459Szrj 
962*a9fa9459Szrj   name = h->root.string;
963*a9fa9459Szrj   if (nbfd == NULL)
964*a9fa9459Szrj     {
965*a9fa9459Szrj       nbfd = obfd;
966*a9fa9459Szrj       nsec = osec;
967*a9fa9459Szrj       nval = oval;
968*a9fa9459Szrj       obfd = NULL;
969*a9fa9459Szrj     }
970*a9fa9459Szrj   einfo (_("%X%C: multiple definition of `%T'\n"),
971*a9fa9459Szrj 	 nbfd, nsec, nval, name);
972*a9fa9459Szrj   if (obfd != NULL)
973*a9fa9459Szrj     einfo (_("%D: first defined here\n"), obfd, osec, oval);
974*a9fa9459Szrj 
975*a9fa9459Szrj   if (RELAXATION_ENABLED_BY_USER)
976*a9fa9459Szrj     {
977*a9fa9459Szrj       einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
978*a9fa9459Szrj       DISABLE_RELAXATION;
979*a9fa9459Szrj     }
980*a9fa9459Szrj }
981*a9fa9459Szrj 
982*a9fa9459Szrj /* This is called when there is a definition of a common symbol, or
983*a9fa9459Szrj    when a common symbol is found for a symbol that is already defined,
984*a9fa9459Szrj    or when two common symbols are found.  We only do something if
985*a9fa9459Szrj    -warn-common was used.  */
986*a9fa9459Szrj 
987*a9fa9459Szrj static void
multiple_common(struct bfd_link_info * info ATTRIBUTE_UNUSED,struct bfd_link_hash_entry * h,bfd * nbfd,enum bfd_link_hash_type ntype,bfd_vma nsize)988*a9fa9459Szrj multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
989*a9fa9459Szrj 		 struct bfd_link_hash_entry *h,
990*a9fa9459Szrj 		 bfd *nbfd,
991*a9fa9459Szrj 		 enum bfd_link_hash_type ntype,
992*a9fa9459Szrj 		 bfd_vma nsize)
993*a9fa9459Szrj {
994*a9fa9459Szrj   const char *name;
995*a9fa9459Szrj   bfd *obfd;
996*a9fa9459Szrj   enum bfd_link_hash_type otype;
997*a9fa9459Szrj   bfd_vma osize;
998*a9fa9459Szrj 
999*a9fa9459Szrj   if (!config.warn_common)
1000*a9fa9459Szrj     return;
1001*a9fa9459Szrj 
1002*a9fa9459Szrj   name = h->root.string;
1003*a9fa9459Szrj   otype = h->type;
1004*a9fa9459Szrj   if (otype == bfd_link_hash_common)
1005*a9fa9459Szrj     {
1006*a9fa9459Szrj       obfd = h->u.c.p->section->owner;
1007*a9fa9459Szrj       osize = h->u.c.size;
1008*a9fa9459Szrj     }
1009*a9fa9459Szrj   else if (otype == bfd_link_hash_defined
1010*a9fa9459Szrj 	   || otype == bfd_link_hash_defweak)
1011*a9fa9459Szrj     {
1012*a9fa9459Szrj       obfd = h->u.def.section->owner;
1013*a9fa9459Szrj       osize = 0;
1014*a9fa9459Szrj     }
1015*a9fa9459Szrj   else
1016*a9fa9459Szrj     {
1017*a9fa9459Szrj       /* FIXME: It would nice if we could report the BFD which defined
1018*a9fa9459Szrj 	 an indirect symbol, but we don't have anywhere to store the
1019*a9fa9459Szrj 	 information.  */
1020*a9fa9459Szrj       obfd = NULL;
1021*a9fa9459Szrj       osize = 0;
1022*a9fa9459Szrj     }
1023*a9fa9459Szrj 
1024*a9fa9459Szrj   if (ntype == bfd_link_hash_defined
1025*a9fa9459Szrj       || ntype == bfd_link_hash_defweak
1026*a9fa9459Szrj       || ntype == bfd_link_hash_indirect)
1027*a9fa9459Szrj     {
1028*a9fa9459Szrj       ASSERT (otype == bfd_link_hash_common);
1029*a9fa9459Szrj       einfo (_("%B: warning: definition of `%T' overriding common\n"),
1030*a9fa9459Szrj 	     nbfd, name);
1031*a9fa9459Szrj       if (obfd != NULL)
1032*a9fa9459Szrj 	einfo (_("%B: warning: common is here\n"), obfd);
1033*a9fa9459Szrj     }
1034*a9fa9459Szrj   else if (otype == bfd_link_hash_defined
1035*a9fa9459Szrj 	   || otype == bfd_link_hash_defweak
1036*a9fa9459Szrj 	   || otype == bfd_link_hash_indirect)
1037*a9fa9459Szrj     {
1038*a9fa9459Szrj       ASSERT (ntype == bfd_link_hash_common);
1039*a9fa9459Szrj       einfo (_("%B: warning: common of `%T' overridden by definition\n"),
1040*a9fa9459Szrj 	     nbfd, name);
1041*a9fa9459Szrj       if (obfd != NULL)
1042*a9fa9459Szrj 	einfo (_("%B: warning: defined here\n"), obfd);
1043*a9fa9459Szrj     }
1044*a9fa9459Szrj   else
1045*a9fa9459Szrj     {
1046*a9fa9459Szrj       ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
1047*a9fa9459Szrj       if (osize > nsize)
1048*a9fa9459Szrj 	{
1049*a9fa9459Szrj 	  einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
1050*a9fa9459Szrj 		 nbfd, name);
1051*a9fa9459Szrj 	  if (obfd != NULL)
1052*a9fa9459Szrj 	    einfo (_("%B: warning: larger common is here\n"), obfd);
1053*a9fa9459Szrj 	}
1054*a9fa9459Szrj       else if (nsize > osize)
1055*a9fa9459Szrj 	{
1056*a9fa9459Szrj 	  einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
1057*a9fa9459Szrj 		 nbfd, name);
1058*a9fa9459Szrj 	  if (obfd != NULL)
1059*a9fa9459Szrj 	    einfo (_("%B: warning: smaller common is here\n"), obfd);
1060*a9fa9459Szrj 	}
1061*a9fa9459Szrj       else
1062*a9fa9459Szrj 	{
1063*a9fa9459Szrj 	  einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name);
1064*a9fa9459Szrj 	  if (obfd != NULL)
1065*a9fa9459Szrj 	    einfo (_("%B: warning: previous common is here\n"), obfd);
1066*a9fa9459Szrj 	}
1067*a9fa9459Szrj     }
1068*a9fa9459Szrj }
1069*a9fa9459Szrj 
1070*a9fa9459Szrj /* This is called when BFD has discovered a set element.  H is the
1071*a9fa9459Szrj    entry in the linker hash table for the set.  SECTION and VALUE
1072*a9fa9459Szrj    represent a value which should be added to the set.  */
1073*a9fa9459Szrj 
1074*a9fa9459Szrj static void
add_to_set(struct bfd_link_info * info ATTRIBUTE_UNUSED,struct bfd_link_hash_entry * h,bfd_reloc_code_real_type reloc,bfd * abfd,asection * section,bfd_vma value)1075*a9fa9459Szrj add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1076*a9fa9459Szrj 	    struct bfd_link_hash_entry *h,
1077*a9fa9459Szrj 	    bfd_reloc_code_real_type reloc,
1078*a9fa9459Szrj 	    bfd *abfd,
1079*a9fa9459Szrj 	    asection *section,
1080*a9fa9459Szrj 	    bfd_vma value)
1081*a9fa9459Szrj {
1082*a9fa9459Szrj   if (config.warn_constructors)
1083*a9fa9459Szrj     einfo (_("%P: warning: global constructor %s used\n"),
1084*a9fa9459Szrj 	   h->root.string);
1085*a9fa9459Szrj 
1086*a9fa9459Szrj   if (!config.build_constructors)
1087*a9fa9459Szrj     return;
1088*a9fa9459Szrj 
1089*a9fa9459Szrj   ldctor_add_set_entry (h, reloc, NULL, section, value);
1090*a9fa9459Szrj 
1091*a9fa9459Szrj   if (h->type == bfd_link_hash_new)
1092*a9fa9459Szrj     {
1093*a9fa9459Szrj       h->type = bfd_link_hash_undefined;
1094*a9fa9459Szrj       h->u.undef.abfd = abfd;
1095*a9fa9459Szrj       /* We don't call bfd_link_add_undef to add this to the list of
1096*a9fa9459Szrj 	 undefined symbols because we are going to define it
1097*a9fa9459Szrj 	 ourselves.  */
1098*a9fa9459Szrj     }
1099*a9fa9459Szrj }
1100*a9fa9459Szrj 
1101*a9fa9459Szrj /* This is called when BFD has discovered a constructor.  This is only
1102*a9fa9459Szrj    called for some object file formats--those which do not handle
1103*a9fa9459Szrj    constructors in some more clever fashion.  This is similar to
1104*a9fa9459Szrj    adding an element to a set, but less general.  */
1105*a9fa9459Szrj 
1106*a9fa9459Szrj static void
constructor_callback(struct bfd_link_info * info,bfd_boolean constructor,const char * name,bfd * abfd,asection * section,bfd_vma value)1107*a9fa9459Szrj constructor_callback (struct bfd_link_info *info,
1108*a9fa9459Szrj 		      bfd_boolean constructor,
1109*a9fa9459Szrj 		      const char *name,
1110*a9fa9459Szrj 		      bfd *abfd,
1111*a9fa9459Szrj 		      asection *section,
1112*a9fa9459Szrj 		      bfd_vma value)
1113*a9fa9459Szrj {
1114*a9fa9459Szrj   char *s;
1115*a9fa9459Szrj   struct bfd_link_hash_entry *h;
1116*a9fa9459Szrj   char set_name[1 + sizeof "__CTOR_LIST__"];
1117*a9fa9459Szrj 
1118*a9fa9459Szrj   if (config.warn_constructors)
1119*a9fa9459Szrj     einfo (_("%P: warning: global constructor %s used\n"), name);
1120*a9fa9459Szrj 
1121*a9fa9459Szrj   if (!config.build_constructors)
1122*a9fa9459Szrj     return;
1123*a9fa9459Szrj 
1124*a9fa9459Szrj   /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1125*a9fa9459Szrj      useful error message.  */
1126*a9fa9459Szrj   if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL
1127*a9fa9459Szrj       && (bfd_link_relocatable (info)
1128*a9fa9459Szrj 	  || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
1129*a9fa9459Szrj     einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1130*a9fa9459Szrj 
1131*a9fa9459Szrj   s = set_name;
1132*a9fa9459Szrj   if (bfd_get_symbol_leading_char (abfd) != '\0')
1133*a9fa9459Szrj     *s++ = bfd_get_symbol_leading_char (abfd);
1134*a9fa9459Szrj   if (constructor)
1135*a9fa9459Szrj     strcpy (s, "__CTOR_LIST__");
1136*a9fa9459Szrj   else
1137*a9fa9459Szrj     strcpy (s, "__DTOR_LIST__");
1138*a9fa9459Szrj 
1139*a9fa9459Szrj   h = bfd_link_hash_lookup (info->hash, set_name, TRUE, TRUE, TRUE);
1140*a9fa9459Szrj   if (h == (struct bfd_link_hash_entry *) NULL)
1141*a9fa9459Szrj     einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1142*a9fa9459Szrj   if (h->type == bfd_link_hash_new)
1143*a9fa9459Szrj     {
1144*a9fa9459Szrj       h->type = bfd_link_hash_undefined;
1145*a9fa9459Szrj       h->u.undef.abfd = abfd;
1146*a9fa9459Szrj       /* We don't call bfd_link_add_undef to add this to the list of
1147*a9fa9459Szrj 	 undefined symbols because we are going to define it
1148*a9fa9459Szrj 	 ourselves.  */
1149*a9fa9459Szrj     }
1150*a9fa9459Szrj 
1151*a9fa9459Szrj   ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
1152*a9fa9459Szrj }
1153*a9fa9459Szrj 
1154*a9fa9459Szrj /* A structure used by warning_callback to pass information through
1155*a9fa9459Szrj    bfd_map_over_sections.  */
1156*a9fa9459Szrj 
1157*a9fa9459Szrj struct warning_callback_info
1158*a9fa9459Szrj {
1159*a9fa9459Szrj   bfd_boolean found;
1160*a9fa9459Szrj   const char *warning;
1161*a9fa9459Szrj   const char *symbol;
1162*a9fa9459Szrj   asymbol **asymbols;
1163*a9fa9459Szrj };
1164*a9fa9459Szrj 
1165*a9fa9459Szrj /* Look through the relocs to see if we can find a plausible address
1166*a9fa9459Szrj    for SYMBOL in ABFD.  Return TRUE if found.  Otherwise return FALSE.  */
1167*a9fa9459Szrj 
1168*a9fa9459Szrj static bfd_boolean
symbol_warning(const char * warning,const char * symbol,bfd * abfd)1169*a9fa9459Szrj symbol_warning (const char *warning, const char *symbol, bfd *abfd)
1170*a9fa9459Szrj {
1171*a9fa9459Szrj   struct warning_callback_info cinfo;
1172*a9fa9459Szrj 
1173*a9fa9459Szrj   if (!bfd_generic_link_read_symbols (abfd))
1174*a9fa9459Szrj     einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1175*a9fa9459Szrj 
1176*a9fa9459Szrj   cinfo.found = FALSE;
1177*a9fa9459Szrj   cinfo.warning = warning;
1178*a9fa9459Szrj   cinfo.symbol = symbol;
1179*a9fa9459Szrj   cinfo.asymbols = bfd_get_outsymbols (abfd);
1180*a9fa9459Szrj   bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
1181*a9fa9459Szrj   return cinfo.found;
1182*a9fa9459Szrj }
1183*a9fa9459Szrj 
1184*a9fa9459Szrj /* This is called when there is a reference to a warning symbol.  */
1185*a9fa9459Szrj 
1186*a9fa9459Szrj static void
warning_callback(struct bfd_link_info * info ATTRIBUTE_UNUSED,const char * warning,const char * symbol,bfd * abfd,asection * section,bfd_vma address)1187*a9fa9459Szrj warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1188*a9fa9459Szrj 		  const char *warning,
1189*a9fa9459Szrj 		  const char *symbol,
1190*a9fa9459Szrj 		  bfd *abfd,
1191*a9fa9459Szrj 		  asection *section,
1192*a9fa9459Szrj 		  bfd_vma address)
1193*a9fa9459Szrj {
1194*a9fa9459Szrj   /* This is a hack to support warn_multiple_gp.  FIXME: This should
1195*a9fa9459Szrj      have a cleaner interface, but what?  */
1196*a9fa9459Szrj   if (!config.warn_multiple_gp
1197*a9fa9459Szrj       && strcmp (warning, "using multiple gp values") == 0)
1198*a9fa9459Szrj     return;
1199*a9fa9459Szrj 
1200*a9fa9459Szrj   if (section != NULL)
1201*a9fa9459Szrj     einfo ("%C: %s%s\n", abfd, section, address, _("warning: "), warning);
1202*a9fa9459Szrj   else if (abfd == NULL)
1203*a9fa9459Szrj     einfo ("%P: %s%s\n", _("warning: "), warning);
1204*a9fa9459Szrj   else if (symbol == NULL)
1205*a9fa9459Szrj     einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
1206*a9fa9459Szrj   else if (!symbol_warning (warning, symbol, abfd))
1207*a9fa9459Szrj     {
1208*a9fa9459Szrj       bfd *b;
1209*a9fa9459Szrj       /* Search all input files for a reference to SYMBOL.  */
1210*a9fa9459Szrj       for (b = info->input_bfds; b; b = b->link.next)
1211*a9fa9459Szrj 	if (b != abfd && symbol_warning (warning, symbol, b))
1212*a9fa9459Szrj 	  return;
1213*a9fa9459Szrj       einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
1214*a9fa9459Szrj     }
1215*a9fa9459Szrj }
1216*a9fa9459Szrj 
1217*a9fa9459Szrj /* This is called by warning_callback for each section.  It checks the
1218*a9fa9459Szrj    relocs of the section to see if it can find a reference to the
1219*a9fa9459Szrj    symbol which triggered the warning.  If it can, it uses the reloc
1220*a9fa9459Szrj    to give an error message with a file and line number.  */
1221*a9fa9459Szrj 
1222*a9fa9459Szrj static void
warning_find_reloc(bfd * abfd,asection * sec,void * iarg)1223*a9fa9459Szrj warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
1224*a9fa9459Szrj {
1225*a9fa9459Szrj   struct warning_callback_info *info = (struct warning_callback_info *) iarg;
1226*a9fa9459Szrj   long relsize;
1227*a9fa9459Szrj   arelent **relpp;
1228*a9fa9459Szrj   long relcount;
1229*a9fa9459Szrj   arelent **p, **pend;
1230*a9fa9459Szrj 
1231*a9fa9459Szrj   if (info->found)
1232*a9fa9459Szrj     return;
1233*a9fa9459Szrj 
1234*a9fa9459Szrj   relsize = bfd_get_reloc_upper_bound (abfd, sec);
1235*a9fa9459Szrj   if (relsize < 0)
1236*a9fa9459Szrj     einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1237*a9fa9459Szrj   if (relsize == 0)
1238*a9fa9459Szrj     return;
1239*a9fa9459Szrj 
1240*a9fa9459Szrj   relpp = (arelent **) xmalloc (relsize);
1241*a9fa9459Szrj   relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1242*a9fa9459Szrj   if (relcount < 0)
1243*a9fa9459Szrj     einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1244*a9fa9459Szrj 
1245*a9fa9459Szrj   p = relpp;
1246*a9fa9459Szrj   pend = p + relcount;
1247*a9fa9459Szrj   for (; p < pend && *p != NULL; p++)
1248*a9fa9459Szrj     {
1249*a9fa9459Szrj       arelent *q = *p;
1250*a9fa9459Szrj 
1251*a9fa9459Szrj       if (q->sym_ptr_ptr != NULL
1252*a9fa9459Szrj 	  && *q->sym_ptr_ptr != NULL
1253*a9fa9459Szrj 	  && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1254*a9fa9459Szrj 	{
1255*a9fa9459Szrj 	  /* We found a reloc for the symbol we are looking for.  */
1256*a9fa9459Szrj 	  einfo ("%C: %s%s\n", abfd, sec, q->address, _("warning: "),
1257*a9fa9459Szrj 		 info->warning);
1258*a9fa9459Szrj 	  info->found = TRUE;
1259*a9fa9459Szrj 	  break;
1260*a9fa9459Szrj 	}
1261*a9fa9459Szrj     }
1262*a9fa9459Szrj 
1263*a9fa9459Szrj   free (relpp);
1264*a9fa9459Szrj }
1265*a9fa9459Szrj 
1266*a9fa9459Szrj /* This is called when an undefined symbol is found.  */
1267*a9fa9459Szrj 
1268*a9fa9459Szrj static void
undefined_symbol(struct bfd_link_info * info,const char * name,bfd * abfd,asection * section,bfd_vma address,bfd_boolean error)1269*a9fa9459Szrj undefined_symbol (struct bfd_link_info *info,
1270*a9fa9459Szrj 		  const char *name,
1271*a9fa9459Szrj 		  bfd *abfd,
1272*a9fa9459Szrj 		  asection *section,
1273*a9fa9459Szrj 		  bfd_vma address,
1274*a9fa9459Szrj 		  bfd_boolean error)
1275*a9fa9459Szrj {
1276*a9fa9459Szrj   static char *error_name;
1277*a9fa9459Szrj   static unsigned int error_count;
1278*a9fa9459Szrj 
1279*a9fa9459Szrj #define MAX_ERRORS_IN_A_ROW 5
1280*a9fa9459Szrj 
1281*a9fa9459Szrj   if (info->ignore_hash != NULL
1282*a9fa9459Szrj       && bfd_hash_lookup (info->ignore_hash, name, FALSE, FALSE) != NULL)
1283*a9fa9459Szrj     return;
1284*a9fa9459Szrj 
1285*a9fa9459Szrj   if (config.warn_once)
1286*a9fa9459Szrj     {
1287*a9fa9459Szrj       /* Only warn once about a particular undefined symbol.  */
1288*a9fa9459Szrj       add_ignoresym (info, name);
1289*a9fa9459Szrj     }
1290*a9fa9459Szrj 
1291*a9fa9459Szrj   /* We never print more than a reasonable number of errors in a row
1292*a9fa9459Szrj      for a single symbol.  */
1293*a9fa9459Szrj   if (error_name != NULL
1294*a9fa9459Szrj       && strcmp (name, error_name) == 0)
1295*a9fa9459Szrj     ++error_count;
1296*a9fa9459Szrj   else
1297*a9fa9459Szrj     {
1298*a9fa9459Szrj       error_count = 0;
1299*a9fa9459Szrj       if (error_name != NULL)
1300*a9fa9459Szrj 	free (error_name);
1301*a9fa9459Szrj       error_name = xstrdup (name);
1302*a9fa9459Szrj     }
1303*a9fa9459Szrj 
1304*a9fa9459Szrj   if (section != NULL)
1305*a9fa9459Szrj     {
1306*a9fa9459Szrj       if (error_count < MAX_ERRORS_IN_A_ROW)
1307*a9fa9459Szrj 	{
1308*a9fa9459Szrj 	  if (error)
1309*a9fa9459Szrj 	    einfo (_("%X%C: undefined reference to `%T'\n"),
1310*a9fa9459Szrj 		   abfd, section, address, name);
1311*a9fa9459Szrj 	  else
1312*a9fa9459Szrj 	    einfo (_("%C: warning: undefined reference to `%T'\n"),
1313*a9fa9459Szrj 		   abfd, section, address, name);
1314*a9fa9459Szrj 	}
1315*a9fa9459Szrj       else if (error_count == MAX_ERRORS_IN_A_ROW)
1316*a9fa9459Szrj 	{
1317*a9fa9459Szrj 	  if (error)
1318*a9fa9459Szrj 	    einfo (_("%X%D: more undefined references to `%T' follow\n"),
1319*a9fa9459Szrj 		   abfd, section, address, name);
1320*a9fa9459Szrj 	  else
1321*a9fa9459Szrj 	    einfo (_("%D: warning: more undefined references to `%T' follow\n"),
1322*a9fa9459Szrj 		   abfd, section, address, name);
1323*a9fa9459Szrj 	}
1324*a9fa9459Szrj       else if (error)
1325*a9fa9459Szrj 	einfo ("%X");
1326*a9fa9459Szrj     }
1327*a9fa9459Szrj   else
1328*a9fa9459Szrj     {
1329*a9fa9459Szrj       if (error_count < MAX_ERRORS_IN_A_ROW)
1330*a9fa9459Szrj 	{
1331*a9fa9459Szrj 	  if (error)
1332*a9fa9459Szrj 	    einfo (_("%X%B: undefined reference to `%T'\n"),
1333*a9fa9459Szrj 		   abfd, name);
1334*a9fa9459Szrj 	  else
1335*a9fa9459Szrj 	    einfo (_("%B: warning: undefined reference to `%T'\n"),
1336*a9fa9459Szrj 		   abfd, name);
1337*a9fa9459Szrj 	}
1338*a9fa9459Szrj       else if (error_count == MAX_ERRORS_IN_A_ROW)
1339*a9fa9459Szrj 	{
1340*a9fa9459Szrj 	  if (error)
1341*a9fa9459Szrj 	    einfo (_("%X%B: more undefined references to `%T' follow\n"),
1342*a9fa9459Szrj 		   abfd, name);
1343*a9fa9459Szrj 	  else
1344*a9fa9459Szrj 	    einfo (_("%B: warning: more undefined references to `%T' follow\n"),
1345*a9fa9459Szrj 		   abfd, name);
1346*a9fa9459Szrj 	}
1347*a9fa9459Szrj       else if (error)
1348*a9fa9459Szrj 	einfo ("%X");
1349*a9fa9459Szrj     }
1350*a9fa9459Szrj }
1351*a9fa9459Szrj 
1352*a9fa9459Szrj /* Counter to limit the number of relocation overflow error messages
1353*a9fa9459Szrj    to print.  Errors are printed as it is decremented.  When it's
1354*a9fa9459Szrj    called and the counter is zero, a final message is printed
1355*a9fa9459Szrj    indicating more relocations were omitted.  When it gets to -1, no
1356*a9fa9459Szrj    such errors are printed.  If it's initially set to a value less
1357*a9fa9459Szrj    than -1, all such errors will be printed (--verbose does this).  */
1358*a9fa9459Szrj 
1359*a9fa9459Szrj int overflow_cutoff_limit = 10;
1360*a9fa9459Szrj 
1361*a9fa9459Szrj /* This is called when a reloc overflows.  */
1362*a9fa9459Szrj 
1363*a9fa9459Szrj static void
reloc_overflow(struct bfd_link_info * info,struct bfd_link_hash_entry * entry,const char * name,const char * reloc_name,bfd_vma addend,bfd * abfd,asection * section,bfd_vma address)1364*a9fa9459Szrj reloc_overflow (struct bfd_link_info *info,
1365*a9fa9459Szrj 		struct bfd_link_hash_entry *entry,
1366*a9fa9459Szrj 		const char *name,
1367*a9fa9459Szrj 		const char *reloc_name,
1368*a9fa9459Szrj 		bfd_vma addend,
1369*a9fa9459Szrj 		bfd *abfd,
1370*a9fa9459Szrj 		asection *section,
1371*a9fa9459Szrj 		bfd_vma address)
1372*a9fa9459Szrj {
1373*a9fa9459Szrj   if (overflow_cutoff_limit == -1)
1374*a9fa9459Szrj     return;
1375*a9fa9459Szrj 
1376*a9fa9459Szrj   einfo ("%X%H:", abfd, section, address);
1377*a9fa9459Szrj 
1378*a9fa9459Szrj   if (overflow_cutoff_limit >= 0
1379*a9fa9459Szrj       && overflow_cutoff_limit-- == 0)
1380*a9fa9459Szrj     {
1381*a9fa9459Szrj       einfo (_(" additional relocation overflows omitted from the output\n"));
1382*a9fa9459Szrj       return;
1383*a9fa9459Szrj     }
1384*a9fa9459Szrj 
1385*a9fa9459Szrj   if (entry)
1386*a9fa9459Szrj     {
1387*a9fa9459Szrj       while (entry->type == bfd_link_hash_indirect
1388*a9fa9459Szrj 	     || entry->type == bfd_link_hash_warning)
1389*a9fa9459Szrj 	entry = entry->u.i.link;
1390*a9fa9459Szrj       switch (entry->type)
1391*a9fa9459Szrj 	{
1392*a9fa9459Szrj 	case bfd_link_hash_undefined:
1393*a9fa9459Szrj 	case bfd_link_hash_undefweak:
1394*a9fa9459Szrj 	  einfo (_(" relocation truncated to fit: "
1395*a9fa9459Szrj 		   "%s against undefined symbol `%T'"),
1396*a9fa9459Szrj 		 reloc_name, entry->root.string);
1397*a9fa9459Szrj 	  break;
1398*a9fa9459Szrj 	case bfd_link_hash_defined:
1399*a9fa9459Szrj 	case bfd_link_hash_defweak:
1400*a9fa9459Szrj 	  einfo (_(" relocation truncated to fit: "
1401*a9fa9459Szrj 		   "%s against symbol `%T' defined in %A section in %B"),
1402*a9fa9459Szrj 		 reloc_name, entry->root.string,
1403*a9fa9459Szrj 		 entry->u.def.section,
1404*a9fa9459Szrj 		 entry->u.def.section == bfd_abs_section_ptr
1405*a9fa9459Szrj 		 ? info->output_bfd : entry->u.def.section->owner);
1406*a9fa9459Szrj 	  break;
1407*a9fa9459Szrj 	default:
1408*a9fa9459Szrj 	  abort ();
1409*a9fa9459Szrj 	  break;
1410*a9fa9459Szrj 	}
1411*a9fa9459Szrj     }
1412*a9fa9459Szrj   else
1413*a9fa9459Szrj     einfo (_(" relocation truncated to fit: %s against `%T'"),
1414*a9fa9459Szrj 	   reloc_name, name);
1415*a9fa9459Szrj   if (addend != 0)
1416*a9fa9459Szrj     einfo ("+%v", addend);
1417*a9fa9459Szrj   einfo ("\n");
1418*a9fa9459Szrj }
1419*a9fa9459Szrj 
1420*a9fa9459Szrj /* This is called when a dangerous relocation is made.  */
1421*a9fa9459Szrj 
1422*a9fa9459Szrj static void
reloc_dangerous(struct bfd_link_info * info ATTRIBUTE_UNUSED,const char * message,bfd * abfd,asection * section,bfd_vma address)1423*a9fa9459Szrj reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1424*a9fa9459Szrj 		 const char *message,
1425*a9fa9459Szrj 		 bfd *abfd,
1426*a9fa9459Szrj 		 asection *section,
1427*a9fa9459Szrj 		 bfd_vma address)
1428*a9fa9459Szrj {
1429*a9fa9459Szrj   einfo (_("%X%H: dangerous relocation: %s\n"),
1430*a9fa9459Szrj 	 abfd, section, address, message);
1431*a9fa9459Szrj }
1432*a9fa9459Szrj 
1433*a9fa9459Szrj /* This is called when a reloc is being generated attached to a symbol
1434*a9fa9459Szrj    that is not being output.  */
1435*a9fa9459Szrj 
1436*a9fa9459Szrj static void
unattached_reloc(struct bfd_link_info * info ATTRIBUTE_UNUSED,const char * name,bfd * abfd,asection * section,bfd_vma address)1437*a9fa9459Szrj unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1438*a9fa9459Szrj 		  const char *name,
1439*a9fa9459Szrj 		  bfd *abfd,
1440*a9fa9459Szrj 		  asection *section,
1441*a9fa9459Szrj 		  bfd_vma address)
1442*a9fa9459Szrj {
1443*a9fa9459Szrj   einfo (_("%X%H: reloc refers to symbol `%T' which is not being output\n"),
1444*a9fa9459Szrj 	 abfd, section, address, name);
1445*a9fa9459Szrj }
1446*a9fa9459Szrj 
1447*a9fa9459Szrj /* This is called if link_info.notice_all is set, or when a symbol in
1448*a9fa9459Szrj    link_info.notice_hash is found.  Symbols are put in notice_hash
1449*a9fa9459Szrj    using the -y option, while notice_all is set if the --cref option
1450*a9fa9459Szrj    has been supplied, or if there are any NOCROSSREFS sections in the
1451*a9fa9459Szrj    linker script; and if plugins are active, since they need to monitor
1452*a9fa9459Szrj    all references from non-IR files.  */
1453*a9fa9459Szrj 
1454*a9fa9459Szrj static bfd_boolean
notice(struct bfd_link_info * info,struct bfd_link_hash_entry * h,struct bfd_link_hash_entry * inh ATTRIBUTE_UNUSED,bfd * abfd,asection * section,bfd_vma value,flagword flags ATTRIBUTE_UNUSED)1455*a9fa9459Szrj notice (struct bfd_link_info *info,
1456*a9fa9459Szrj 	struct bfd_link_hash_entry *h,
1457*a9fa9459Szrj 	struct bfd_link_hash_entry *inh ATTRIBUTE_UNUSED,
1458*a9fa9459Szrj 	bfd *abfd,
1459*a9fa9459Szrj 	asection *section,
1460*a9fa9459Szrj 	bfd_vma value,
1461*a9fa9459Szrj 	flagword flags ATTRIBUTE_UNUSED)
1462*a9fa9459Szrj {
1463*a9fa9459Szrj   const char *name;
1464*a9fa9459Szrj 
1465*a9fa9459Szrj   if (h == NULL)
1466*a9fa9459Szrj     {
1467*a9fa9459Szrj       if (command_line.cref || nocrossref_list != NULL)
1468*a9fa9459Szrj 	return handle_asneeded_cref (abfd, (enum notice_asneeded_action) value);
1469*a9fa9459Szrj       return TRUE;
1470*a9fa9459Szrj     }
1471*a9fa9459Szrj 
1472*a9fa9459Szrj   name = h->root.string;
1473*a9fa9459Szrj   if (info->notice_hash != NULL
1474*a9fa9459Szrj       && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL)
1475*a9fa9459Szrj     {
1476*a9fa9459Szrj       if (bfd_is_und_section (section))
1477*a9fa9459Szrj 	einfo ("%B: reference to %s\n", abfd, name);
1478*a9fa9459Szrj       else
1479*a9fa9459Szrj 	einfo ("%B: definition of %s\n", abfd, name);
1480*a9fa9459Szrj     }
1481*a9fa9459Szrj 
1482*a9fa9459Szrj   if (command_line.cref || nocrossref_list != NULL)
1483*a9fa9459Szrj     add_cref (name, abfd, section, value);
1484*a9fa9459Szrj 
1485*a9fa9459Szrj   return TRUE;
1486*a9fa9459Szrj }
1487