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