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