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