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