Commit | Line | Data |
---|---|---|
8594f568 | 1 | /* Main program of GNU linker. |
8bc8f4bc | 2 | Copyright (C) 1991, 92, 93, 94, 95, 96, 1997, 1998 Free Software Foundation, Inc. |
ce4d59e2 | 3 | Written by Steve Chamberlain steve@cygnus.com |
e47bfa63 | 4 | |
2fa0b342 DHW |
5 | This file is part of GLD, the Gnu Linker. |
6 | ||
7 | GLD is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
ce4d59e2 | 9 | the Free Software Foundation; either version 2, or (at your option) |
2fa0b342 DHW |
10 | any later version. |
11 | ||
12 | GLD is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
2ed9fe47 ILT |
18 | along with GLD; see the file COPYING. If not, write to the Free |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
2fa0b342 | 21 | |
2fa0b342 | 22 | #include "bfd.h" |
f177a611 | 23 | #include "sysdep.h" |
b9395be3 | 24 | #include <stdio.h> |
7d6439d9 | 25 | #include <ctype.h> |
d4e5e3c3 | 26 | #include "libiberty.h" |
a735edad | 27 | #include "progress.h" |
b9395be3 | 28 | #include "bfdlink.h" |
2fa0b342 | 29 | |
2fa0b342 DHW |
30 | #include "ld.h" |
31 | #include "ldmain.h" | |
32 | #include "ldmisc.h" | |
33 | #include "ldwrite.h" | |
34 | #include "ldgram.h" | |
fcf276c4 | 35 | #include "ldexp.h" |
2fa0b342 | 36 | #include "ldlang.h" |
8c514453 | 37 | #include "ldemul.h" |
2fa0b342 DHW |
38 | #include "ldlex.h" |
39 | #include "ldfile.h" | |
c611e285 | 40 | #include "ldctor.h" |
9d1fe8a4 | 41 | |
922018a1 | 42 | /* Somewhere above, sys/stat.h got included . . . . */ |
d723cd17 DM |
43 | #if !defined(S_ISDIR) && defined(S_IFDIR) |
44 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) | |
45 | #endif | |
46 | ||
47 | #include <string.h> | |
48 | ||
45408cd7 ILT |
49 | #ifdef HAVE_SBRK |
50 | #ifdef NEED_DECLARATION_SBRK | |
51 | extern PTR sbrk (); | |
52 | #endif | |
53 | #endif | |
54 | ||
fcf276c4 ILT |
55 | static char *get_emulation PARAMS ((int, char **)); |
56 | static void set_scripts_dir PARAMS ((void)); | |
e47bfa63 | 57 | |
2fa0b342 DHW |
58 | /* EXPORTS */ |
59 | ||
60 | char *default_target; | |
fcf276c4 | 61 | const char *output_filename = "a.out"; |
e47bfa63 | 62 | |
2fa0b342 DHW |
63 | /* Name this program was invoked by. */ |
64 | char *program_name; | |
65 | ||
66 | /* The file that we're creating */ | |
b6316534 | 67 | bfd *output_bfd = 0; |
2fa0b342 | 68 | |
173a0c3d DM |
69 | /* Set by -G argument, for MIPS ECOFF target. */ |
70 | int g_switch_value = 8; | |
71 | ||
2fa0b342 DHW |
72 | /* Nonzero means print names of input files as processed. */ |
73 | boolean trace_files; | |
74 | ||
bbd2521f DM |
75 | /* Nonzero means same, but note open failures, too. */ |
76 | boolean trace_file_tries; | |
77 | ||
7b40f2b1 DM |
78 | /* Nonzero means version number was printed, so exit successfully |
79 | instead of complaining if no input files are given. */ | |
80 | boolean version_printed; | |
81 | ||
f2f55b16 ILT |
82 | /* Nonzero means link in every member of an archive. */ |
83 | boolean whole_archive; | |
84 | ||
2fa0b342 | 85 | args_type command_line; |
173a0c3d | 86 | |
2fa0b342 | 87 | ld_config_type config; |
9f629407 | 88 | |
8bc8f4bc | 89 | static void remove_output PARAMS ((void)); |
9f629407 | 90 | static boolean check_for_scripts_dir PARAMS ((char *dir)); |
b9395be3 ILT |
91 | static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *, |
92 | const char *)); | |
93 | static boolean multiple_definition PARAMS ((struct bfd_link_info *, | |
94 | const char *, | |
95 | bfd *, asection *, bfd_vma, | |
96 | bfd *, asection *, bfd_vma)); | |
97 | static boolean multiple_common PARAMS ((struct bfd_link_info *, | |
98 | const char *, bfd *, | |
99 | enum bfd_link_hash_type, bfd_vma, | |
100 | bfd *, enum bfd_link_hash_type, | |
101 | bfd_vma)); | |
102 | static boolean add_to_set PARAMS ((struct bfd_link_info *, | |
103 | struct bfd_link_hash_entry *, | |
2a9fa50c | 104 | bfd_reloc_code_real_type, |
b9395be3 ILT |
105 | bfd *, asection *, bfd_vma)); |
106 | static boolean constructor_callback PARAMS ((struct bfd_link_info *, | |
107 | boolean constructor, | |
b9395be3 ILT |
108 | const char *name, |
109 | bfd *, asection *, bfd_vma)); | |
110 | static boolean warning_callback PARAMS ((struct bfd_link_info *, | |
7d6439d9 ILT |
111 | const char *, const char *, bfd *, |
112 | asection *, bfd_vma)); | |
113 | static void warning_find_reloc PARAMS ((bfd *, asection *, PTR)); | |
b9395be3 ILT |
114 | static boolean undefined_symbol PARAMS ((struct bfd_link_info *, |
115 | const char *, bfd *, | |
116 | asection *, bfd_vma)); | |
5dad4c97 ILT |
117 | static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *, |
118 | const char *, bfd_vma, | |
119 | bfd *, asection *, bfd_vma)); | |
b9395be3 ILT |
120 | static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *, |
121 | bfd *, asection *, bfd_vma)); | |
122 | static boolean unattached_reloc PARAMS ((struct bfd_link_info *, | |
123 | const char *, bfd *, asection *, | |
124 | bfd_vma)); | |
cd09553a ILT |
125 | static boolean notice PARAMS ((struct bfd_link_info *, const char *, |
126 | bfd *, asection *, bfd_vma)); | |
b9395be3 ILT |
127 | |
128 | static struct bfd_link_callbacks link_callbacks = | |
129 | { | |
130 | add_archive_element, | |
131 | multiple_definition, | |
132 | multiple_common, | |
133 | add_to_set, | |
134 | constructor_callback, | |
135 | warning_callback, | |
136 | undefined_symbol, | |
137 | reloc_overflow, | |
138 | reloc_dangerous, | |
139 | unattached_reloc, | |
cd09553a | 140 | notice |
b9395be3 ILT |
141 | }; |
142 | ||
143 | struct bfd_link_info link_info; | |
173a0c3d | 144 | \f |
0b2f8d2e DM |
145 | static void |
146 | remove_output () | |
147 | { | |
148 | if (output_filename) | |
149 | { | |
150 | if (output_bfd && output_bfd->iostream) | |
151 | fclose((FILE *)(output_bfd->iostream)); | |
152 | if (delete_output_file_on_failure) | |
153 | unlink (output_filename); | |
154 | } | |
155 | } | |
156 | ||
9f629407 | 157 | int |
2fa0b342 | 158 | main (argc, argv) |
2fa0b342 | 159 | int argc; |
9f629407 | 160 | char **argv; |
2fa0b342 DHW |
161 | { |
162 | char *emulation; | |
8594f568 | 163 | long start_time = get_run_time (); |
e47bfa63 | 164 | |
5864e097 | 165 | #ifdef HAVE_SETLOCALE |
8bc8f4bc | 166 | setlocale (LC_MESSAGES, ""); |
5864e097 | 167 | #endif |
8bc8f4bc TT |
168 | bindtextdomain (PACKAGE, LOCALEDIR); |
169 | textdomain (PACKAGE); | |
170 | ||
2fa0b342 | 171 | program_name = argv[0]; |
5bcb7f28 | 172 | xmalloc_set_program_name (program_name); |
99fe4553 | 173 | |
a735edad ILT |
174 | START_PROGRESS (program_name, 0); |
175 | ||
e47bfa63 | 176 | bfd_init (); |
bfbdc80f | 177 | |
28113e82 ILT |
178 | bfd_set_error_program_name (program_name); |
179 | ||
5bcb7f28 | 180 | xatexit (remove_output); |
0b2f8d2e | 181 | |
d7339c87 ILT |
182 | /* Set the default BFD target based on the configured target. Doing |
183 | this permits the linker to be configured for a particular target, | |
184 | and linked against a shared BFD library which was configured for | |
185 | a different target. The macro TARGET is defined by Makefile. */ | |
186 | if (! bfd_set_default_target (TARGET)) | |
187 | { | |
8bc8f4bc | 188 | einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET); |
d7339c87 ILT |
189 | xexit (1); |
190 | } | |
191 | ||
2fa0b342 | 192 | /* Initialize the data about options. */ |
7b40f2b1 | 193 | trace_files = trace_file_tries = version_printed = false; |
f2f55b16 | 194 | whole_archive = false; |
b9395be3 | 195 | config.build_constructors = true; |
2a9fa50c | 196 | config.dynamic_link = false; |
5864e097 | 197 | config.has_shared = false; |
2fa0b342 | 198 | command_line.force_common_definition = false; |
7fb9ca5f | 199 | command_line.interpreter = NULL; |
cc23cc69 | 200 | command_line.rpath = NULL; |
8bc8f4bc | 201 | command_line.warn_mismatch = true; |
2fa0b342 | 202 | |
b9395be3 ILT |
203 | link_info.callbacks = &link_callbacks; |
204 | link_info.relocateable = false; | |
64887de2 | 205 | link_info.shared = false; |
4551e108 | 206 | link_info.symbolic = false; |
7d6439d9 | 207 | link_info.static_link = false; |
6799c638 | 208 | link_info.traditional_format = false; |
5864e097 | 209 | link_info.optimize = false; |
b9395be3 ILT |
210 | link_info.strip = strip_none; |
211 | link_info.discard = discard_none; | |
b9395be3 ILT |
212 | link_info.keep_memory = true; |
213 | link_info.input_bfds = NULL; | |
214 | link_info.create_object_symbols_section = NULL; | |
215 | link_info.hash = NULL; | |
216 | link_info.keep_hash = NULL; | |
cd09553a | 217 | link_info.notice_all = false; |
b9395be3 | 218 | link_info.notice_hash = NULL; |
e3d73386 | 219 | link_info.wrap_hash = NULL; |
7d6439d9 | 220 | |
e47bfa63 | 221 | ldfile_add_arch (""); |
a72f4e5f | 222 | |
2fa0b342 DHW |
223 | config.make_executable = true; |
224 | force_make_executable = false; | |
ce4d59e2 SC |
225 | config.magic_demand_paged = true; |
226 | config.text_read_only = true; | |
2fa0b342 | 227 | config.make_executable = true; |
1418c83b | 228 | |
d723cd17 | 229 | emulation = get_emulation (argc, argv); |
e47bfa63 SC |
230 | ldemul_choose_mode (emulation); |
231 | default_target = ldemul_choose_target (); | |
232 | lang_init (); | |
233 | ldemul_before_parse (); | |
2fa0b342 | 234 | lang_has_input_file = false; |
e47bfa63 | 235 | parse_args (argc, argv); |
f3739bc3 | 236 | |
7d6439d9 ILT |
237 | ldemul_set_symbols (); |
238 | ||
2a9fa50c ILT |
239 | if (link_info.relocateable) |
240 | { | |
5864e097 UD |
241 | if (command_line.gc_sections) |
242 | einfo ("%P%F: --gc-sections and -r may not be used together\n"); | |
2a9fa50c | 243 | if (command_line.relax) |
5864e097 | 244 | einfo (_("%P%F: --relax and -r may not be used together\n")); |
64887de2 | 245 | if (link_info.shared) |
8bc8f4bc | 246 | einfo (_("%P%F: -r and -shared may not be used together\n")); |
2a9fa50c ILT |
247 | } |
248 | ||
5864e097 UD |
249 | if (command_line.gc_sections && config.dynamic_link) |
250 | einfo("%P%F: --gc-sections may only be performed for static links\n"); | |
251 | ||
8ed88239 ILT |
252 | /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I |
253 | don't see how else this can be handled, since in this case we | |
254 | must preserve all externally visible symbols. */ | |
255 | if (link_info.relocateable && link_info.strip == strip_all) | |
256 | { | |
257 | link_info.strip = strip_debugger; | |
258 | if (link_info.discard == discard_none) | |
259 | link_info.discard = discard_all; | |
260 | } | |
261 | ||
973e421e ILT |
262 | /* This essentially adds another -L directory so this must be done after |
263 | the -L's in argv have been processed. */ | |
264 | set_scripts_dir (); | |
265 | ||
bbd2521f DM |
266 | if (had_script == false) |
267 | { | |
268 | /* Read the emulation's appropriate default script. */ | |
2a28d8b0 DM |
269 | int isfile; |
270 | char *s = ldemul_get_script (&isfile); | |
271 | ||
272 | if (isfile) | |
d4e5e3c3 | 273 | ldfile_open_command_file (s); |
2a28d8b0 | 274 | else |
22c41f00 ILT |
275 | { |
276 | if (trace_file_tries) | |
277 | { | |
8bc8f4bc | 278 | info_msg (_("using internal linker script:\n")); |
22c41f00 ILT |
279 | info_msg ("==================================================\n"); |
280 | info_msg (s); | |
281 | info_msg ("\n==================================================\n"); | |
282 | } | |
7d6439d9 | 283 | lex_string = s; |
22c41f00 ILT |
284 | lex_redirect (s); |
285 | } | |
d4e5e3c3 DM |
286 | parser_input = input_script; |
287 | yyparse (); | |
7d6439d9 | 288 | lex_string = NULL; |
bbd2521f DM |
289 | } |
290 | ||
e47bfa63 | 291 | lang_final (); |
9d1fe8a4 | 292 | |
e47bfa63 SC |
293 | if (lang_has_input_file == false) |
294 | { | |
7b40f2b1 | 295 | if (version_printed) |
0b2f8d2e | 296 | xexit (0); |
8bc8f4bc | 297 | einfo (_("%P%F: no input files\n")); |
870f54b2 | 298 | } |
2fa0b342 | 299 | |
bbd2521f DM |
300 | if (trace_files) |
301 | { | |
8bc8f4bc | 302 | info_msg (_("%P: mode %s\n"), emulation); |
bbd2521f DM |
303 | } |
304 | ||
e47bfa63 | 305 | ldemul_after_parse (); |
870f54b2 | 306 | |
9d1fe8a4 | 307 | |
e47bfa63 | 308 | if (config.map_filename) |
870f54b2 | 309 | { |
e47bfa63 | 310 | if (strcmp (config.map_filename, "-") == 0) |
2e2bf962 | 311 | { |
e47bfa63 | 312 | config.map_file = stdout; |
2e2bf962 | 313 | } |
e47bfa63 SC |
314 | else |
315 | { | |
316 | config.map_file = fopen (config.map_filename, FOPEN_WT); | |
317 | if (config.map_file == (FILE *) NULL) | |
318 | { | |
de71eb77 | 319 | bfd_set_error (bfd_error_system_call); |
8bc8f4bc | 320 | einfo (_("%P%F: cannot open map file %s: %E\n"), |
e47bfa63 SC |
321 | config.map_filename); |
322 | } | |
323 | } | |
324 | } | |
870f54b2 | 325 | |
2e2bf962 | 326 | |
e47bfa63 | 327 | lang_process (); |
2fa0b342 | 328 | |
2fa0b342 DHW |
329 | /* Print error messages for any missing symbols, for any warning |
330 | symbols, and possibly multiple definitions */ | |
331 | ||
2fa0b342 | 332 | |
e47bfa63 SC |
333 | if (config.text_read_only) |
334 | { | |
335 | /* Look for a text section and mark the readonly attribute in it */ | |
336 | asection *found = bfd_get_section_by_name (output_bfd, ".text"); | |
337 | ||
338 | if (found != (asection *) NULL) | |
339 | { | |
340 | found->flags |= SEC_READONLY; | |
341 | } | |
3e4c643d | 342 | } |
2fa0b342 | 343 | |
a735edad | 344 | if (link_info.relocateable) |
f3739bc3 | 345 | output_bfd->flags &= ~EXEC_P; |
a72f4e5f | 346 | else |
f3739bc3 | 347 | output_bfd->flags |= EXEC_P; |
b257477f | 348 | |
f3739bc3 | 349 | ldwrite (); |
b257477f | 350 | |
0cacbcbe ILT |
351 | if (config.map_file != NULL) |
352 | lang_map (); | |
cd09553a ILT |
353 | if (command_line.cref) |
354 | output_cref (config.map_file != NULL ? config.map_file : stdout); | |
582dd77f ILT |
355 | if (nocrossref_list != NULL) |
356 | check_nocrossrefs (); | |
0cacbcbe | 357 | |
f3739bc3 SC |
358 | /* Even if we're producing relocateable output, some non-fatal errors should |
359 | be reported in the exit status. (What non-fatal errors, if any, do we | |
360 | want to ignore for relocateable output?) */ | |
a72f4e5f | 361 | |
f3739bc3 SC |
362 | if (config.make_executable == false && force_make_executable == false) |
363 | { | |
364 | if (trace_files == true) | |
e47bfa63 | 365 | { |
8bc8f4bc | 366 | einfo (_("%P: link errors found, deleting executable `%s'\n"), |
f3739bc3 | 367 | output_filename); |
e47bfa63 | 368 | } |
a72f4e5f | 369 | |
cd09553a | 370 | /* The file will be removed by remove_output. */ |
a72f4e5f | 371 | |
0b2f8d2e | 372 | xexit (1); |
f3739bc3 SC |
373 | } |
374 | else | |
375 | { | |
2a9fa50c | 376 | if (! bfd_close (output_bfd)) |
8bc8f4bc | 377 | einfo (_("%F%B: final close failed: %E\n"), output_bfd); |
cd09553a ILT |
378 | |
379 | /* If the --force-exe-suffix is enabled, and we're making an | |
380 | executable file and it doesn't end in .exe, copy it to one which does. */ | |
381 | ||
382 | if (! link_info.relocateable && command_line.force_exe_suffix) | |
383 | { | |
384 | int len = strlen (output_filename); | |
385 | if (len < 4 | |
386 | || (strcasecmp (output_filename + len - 4, ".exe") != 0 | |
387 | && strcasecmp (output_filename + len - 4, ".dll") != 0)) | |
388 | { | |
389 | FILE *src; | |
390 | FILE *dst; | |
391 | const int bsize = 4096; | |
392 | char *buf = xmalloc (bsize); | |
393 | int l; | |
394 | char *dst_name = xmalloc (len + 5); | |
395 | strcpy (dst_name, output_filename); | |
396 | strcat (dst_name, ".exe"); | |
397 | src = fopen (output_filename, FOPEN_RB); | |
398 | dst = fopen (dst_name, FOPEN_WB); | |
399 | ||
400 | if (!src) | |
8bc8f4bc | 401 | einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename); |
cd09553a | 402 | if (!dst) |
8bc8f4bc | 403 | einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name); |
cd09553a ILT |
404 | while ((l = fread (buf, 1, bsize, src)) > 0) |
405 | { | |
406 | int done = fwrite (buf, 1, l, dst); | |
407 | if (done != l) | |
408 | { | |
8bc8f4bc | 409 | einfo (_("%P: Error writing file `%s'\n"), dst_name); |
cd09553a ILT |
410 | } |
411 | } | |
412 | fclose (src); | |
8bc8f4bc | 413 | if (fclose (dst) == EOF) |
cd09553a | 414 | { |
8bc8f4bc | 415 | einfo (_("%P: Error closing file `%s'\n"), dst_name); |
cd09553a ILT |
416 | } |
417 | free (dst_name); | |
418 | free (buf); | |
419 | } | |
420 | } | |
f3739bc3 | 421 | } |
2fa0b342 | 422 | |
a735edad ILT |
423 | END_PROGRESS (program_name); |
424 | ||
b9395be3 ILT |
425 | if (config.stats) |
426 | { | |
427 | extern char **environ; | |
eac6290c | 428 | #ifdef HAVE_SBRK |
b9395be3 | 429 | char *lim = (char *) sbrk (0); |
de71eb77 | 430 | #endif |
8594f568 | 431 | long run_time = get_run_time () - start_time; |
b9395be3 | 432 | |
8bc8f4bc | 433 | fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"), |
8594f568 | 434 | program_name, run_time / 1000000, run_time % 1000000); |
eac6290c | 435 | #ifdef HAVE_SBRK |
8bc8f4bc | 436 | fprintf (stderr, _("%s: data size %ld\n"), program_name, |
b9395be3 | 437 | (long) (lim - (char *) &environ)); |
de71eb77 | 438 | #endif |
b9395be3 ILT |
439 | } |
440 | ||
52a8ebfe DM |
441 | /* Prevent remove_output from doing anything, after a successful link. */ |
442 | output_filename = NULL; | |
443 | ||
0b2f8d2e | 444 | xexit (0); |
9f629407 | 445 | return 0; |
d723cd17 DM |
446 | } |
447 | ||
448 | /* We need to find any explicitly given emulation in order to initialize the | |
449 | state that's needed by the lex&yacc argument parser (parse_args). */ | |
450 | ||
451 | static char * | |
452 | get_emulation (argc, argv) | |
453 | int argc; | |
454 | char **argv; | |
455 | { | |
456 | char *emulation; | |
457 | int i; | |
458 | ||
45408cd7 | 459 | emulation = getenv (EMULATION_ENVIRON); |
d723cd17 DM |
460 | if (emulation == NULL) |
461 | emulation = DEFAULT_EMULATION; | |
d723cd17 DM |
462 | |
463 | for (i = 1; i < argc; i++) | |
464 | { | |
465 | if (!strncmp (argv[i], "-m", 2)) | |
466 | { | |
467 | if (argv[i][2] == '\0') | |
468 | { | |
469 | /* -m EMUL */ | |
470 | if (i < argc - 1) | |
471 | { | |
472 | emulation = argv[i + 1]; | |
473 | i++; | |
474 | } | |
475 | else | |
476 | { | |
8bc8f4bc | 477 | einfo(_("%P%F: missing argument to -m\n")); |
d723cd17 DM |
478 | } |
479 | } | |
973e421e ILT |
480 | else if (strcmp (argv[i], "-mips1") == 0 |
481 | || strcmp (argv[i], "-mips2") == 0 | |
45408cd7 ILT |
482 | || strcmp (argv[i], "-mips3") == 0 |
483 | || strcmp (argv[i], "-mips4") == 0) | |
973e421e ILT |
484 | { |
485 | /* FIXME: The arguments -mips1, -mips2 and -mips3 are | |
486 | passed to the linker by some MIPS compilers. They | |
487 | generally tell the linker to use a slightly different | |
488 | library path. Perhaps someday these should be | |
489 | implemented as emulations; until then, we just ignore | |
490 | the arguments and hope that nobody ever creates | |
491 | emulations named ips1, ips2 or ips3. */ | |
492 | } | |
cbbf9608 ILT |
493 | else if (strcmp (argv[i], "-m486") == 0) |
494 | { | |
495 | /* FIXME: The argument -m486 is passed to the linker on | |
496 | some Linux systems. Hope that nobody creates an | |
497 | emulation named 486. */ | |
498 | } | |
d723cd17 DM |
499 | else |
500 | { | |
501 | /* -mEMUL */ | |
502 | emulation = &argv[i][2]; | |
503 | } | |
504 | } | |
505 | } | |
506 | ||
507 | return emulation; | |
508 | } | |
509 | ||
510 | /* If directory DIR contains an "ldscripts" subdirectory, | |
511 | add DIR to the library search path and return true, | |
512 | else return false. */ | |
513 | ||
514 | static boolean | |
515 | check_for_scripts_dir (dir) | |
516 | char *dir; | |
517 | { | |
518 | size_t dirlen; | |
519 | char *buf; | |
520 | struct stat s; | |
521 | boolean res; | |
522 | ||
523 | dirlen = strlen (dir); | |
524 | /* sizeof counts the terminating NUL. */ | |
0b2f8d2e | 525 | buf = (char *) xmalloc (dirlen + sizeof("/ldscripts")); |
d723cd17 DM |
526 | sprintf (buf, "%s/ldscripts", dir); |
527 | ||
528 | res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); | |
f4208462 | 529 | free (buf); |
d723cd17 | 530 | if (res) |
0cd82d00 | 531 | ldfile_add_library_path (dir, false); |
d723cd17 DM |
532 | return res; |
533 | } | |
534 | ||
535 | /* Set the default directory for finding script files. | |
f4208462 DM |
536 | Libraries will be searched for here too, but that's ok. |
537 | We look for the "ldscripts" directory in: | |
538 | ||
f4208462 | 539 | SCRIPTDIR (passed from Makefile) |
bbd2521f DM |
540 | the dir where this program is (for using it from the build tree) |
541 | the dir where this program is/../lib (for installing the tool suite elsewhere) */ | |
d723cd17 DM |
542 | |
543 | static void | |
544 | set_scripts_dir () | |
545 | { | |
f4208462 DM |
546 | char *end, *dir; |
547 | size_t dirlen; | |
548 | ||
d723cd17 | 549 | if (check_for_scripts_dir (SCRIPTDIR)) |
f4208462 | 550 | return; /* We've been installed normally. */ |
d723cd17 DM |
551 | |
552 | /* Look for "ldscripts" in the dir where our binary is. */ | |
553 | end = strrchr (program_name, '/'); | |
7d6439d9 ILT |
554 | |
555 | if (end == NULL) | |
bbd2521f | 556 | { |
7d6439d9 ILT |
557 | /* Don't look for ldscripts in the current directory. There is |
558 | too much potential for confusion. */ | |
559 | return; | |
bbd2521f | 560 | } |
f4208462 | 561 | |
7d6439d9 ILT |
562 | dirlen = end - program_name; |
563 | /* Make a copy of program_name in dir. | |
564 | Leave room for later "/../lib". */ | |
565 | dir = (char *) xmalloc (dirlen + 8); | |
566 | strncpy (dir, program_name, dirlen); | |
567 | dir[dirlen] = '\0'; | |
568 | ||
f4208462 DM |
569 | if (check_for_scripts_dir (dir)) |
570 | return; /* Don't free dir. */ | |
571 | ||
572 | /* Look for "ldscripts" in <the dir where our binary is>/../lib. */ | |
573 | strcpy (dir + dirlen, "/../lib"); | |
574 | if (check_for_scripts_dir (dir)) | |
575 | return; | |
576 | ||
577 | free (dir); /* Well, we tried. */ | |
d723cd17 | 578 | } |
2fa0b342 | 579 | |
e47bfa63 | 580 | void |
b9395be3 ILT |
581 | add_ysym (name) |
582 | const char *name; | |
2fa0b342 | 583 | { |
b9395be3 ILT |
584 | if (link_info.notice_hash == (struct bfd_hash_table *) NULL) |
585 | { | |
586 | link_info.notice_hash = ((struct bfd_hash_table *) | |
0b2f8d2e | 587 | xmalloc (sizeof (struct bfd_hash_table))); |
b9395be3 ILT |
588 | if (! bfd_hash_table_init_n (link_info.notice_hash, |
589 | bfd_hash_newfunc, | |
590 | 61)) | |
8bc8f4bc | 591 | einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); |
b9395be3 ILT |
592 | } |
593 | ||
594 | if (bfd_hash_lookup (link_info.notice_hash, name, true, true) | |
595 | == (struct bfd_hash_entry *) NULL) | |
8bc8f4bc | 596 | einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); |
2fa0b342 | 597 | } |
e47bfa63 | 598 | |
28113e82 ILT |
599 | /* Record a symbol to be wrapped, from the --wrap option. */ |
600 | ||
601 | void | |
602 | add_wrap (name) | |
603 | const char *name; | |
604 | { | |
605 | if (link_info.wrap_hash == NULL) | |
606 | { | |
607 | link_info.wrap_hash = ((struct bfd_hash_table *) | |
608 | xmalloc (sizeof (struct bfd_hash_table))); | |
609 | if (! bfd_hash_table_init_n (link_info.wrap_hash, | |
610 | bfd_hash_newfunc, | |
611 | 61)) | |
8bc8f4bc | 612 | einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); |
28113e82 ILT |
613 | } |
614 | if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL) | |
8bc8f4bc | 615 | einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); |
28113e82 ILT |
616 | } |
617 | ||
b9395be3 | 618 | /* Handle the -retain-symbols-file option. */ |
2fa0b342 | 619 | |
2fa0b342 | 620 | void |
b9395be3 ILT |
621 | add_keepsyms_file (filename) |
622 | const char *filename; | |
2fa0b342 | 623 | { |
b9395be3 ILT |
624 | FILE *file; |
625 | char *buf; | |
626 | size_t bufsize; | |
627 | int c; | |
2fa0b342 | 628 | |
b9395be3 | 629 | if (link_info.strip == strip_some) |
8bc8f4bc | 630 | einfo (_("%X%P: error: duplicate retain-symbols-file\n")); |
3e4c643d | 631 | |
b9395be3 ILT |
632 | file = fopen (filename, "r"); |
633 | if (file == (FILE *) NULL) | |
e47bfa63 | 634 | { |
5bcb7f28 | 635 | bfd_set_error (bfd_error_system_call); |
7d6439d9 | 636 | einfo ("%X%P: %s: %E\n", filename); |
b9395be3 | 637 | return; |
be1627d3 | 638 | } |
be1627d3 | 639 | |
b9395be3 | 640 | link_info.keep_hash = ((struct bfd_hash_table *) |
0b2f8d2e | 641 | xmalloc (sizeof (struct bfd_hash_table))); |
b9395be3 | 642 | if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc)) |
8bc8f4bc | 643 | einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); |
2fa0b342 | 644 | |
b9395be3 | 645 | bufsize = 100; |
0b2f8d2e | 646 | buf = (char *) xmalloc (bufsize); |
c611e285 | 647 | |
b9395be3 ILT |
648 | c = getc (file); |
649 | while (c != EOF) | |
29f33467 | 650 | { |
b9395be3 ILT |
651 | while (isspace (c)) |
652 | c = getc (file); | |
e47bfa63 | 653 | |
b9395be3 | 654 | if (c != EOF) |
29f33467 | 655 | { |
b9395be3 | 656 | size_t len = 0; |
8a045e50 | 657 | |
b9395be3 | 658 | while (! isspace (c) && c != EOF) |
29f33467 | 659 | { |
b9395be3 ILT |
660 | buf[len] = c; |
661 | ++len; | |
662 | if (len >= bufsize) | |
29f33467 | 663 | { |
b9395be3 | 664 | bufsize *= 2; |
0b2f8d2e | 665 | buf = xrealloc (buf, bufsize); |
29f33467 | 666 | } |
b9395be3 | 667 | c = getc (file); |
29f33467 | 668 | } |
81016051 | 669 | |
b9395be3 ILT |
670 | buf[len] = '\0'; |
671 | ||
672 | if (bfd_hash_lookup (link_info.keep_hash, buf, true, true) | |
673 | == (struct bfd_hash_entry *) NULL) | |
8bc8f4bc | 674 | einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n")); |
29f33467 | 675 | } |
e47bfa63 | 676 | } |
2fa0b342 | 677 | |
b9395be3 | 678 | if (link_info.strip != strip_none) |
8bc8f4bc | 679 | einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n")); |
8a045e50 | 680 | |
b9395be3 ILT |
681 | link_info.strip = strip_some; |
682 | } | |
683 | \f | |
684 | /* Callbacks from the BFD linker routines. */ | |
2fa0b342 | 685 | |
b9395be3 ILT |
686 | /* This is called when BFD has decided to include an archive member in |
687 | a link. */ | |
2fa0b342 | 688 | |
b9395be3 ILT |
689 | /*ARGSUSED*/ |
690 | static boolean | |
691 | add_archive_element (info, abfd, name) | |
692 | struct bfd_link_info *info; | |
693 | bfd *abfd; | |
694 | const char *name; | |
2fa0b342 | 695 | { |
b9395be3 ILT |
696 | lang_input_statement_type *input; |
697 | ||
698 | input = ((lang_input_statement_type *) | |
7d6439d9 | 699 | xmalloc (sizeof (lang_input_statement_type))); |
b9395be3 ILT |
700 | input->filename = abfd->filename; |
701 | input->local_sym_name = abfd->filename; | |
702 | input->the_bfd = abfd; | |
703 | input->asymbols = NULL; | |
b9395be3 ILT |
704 | input->next = NULL; |
705 | input->just_syms_flag = false; | |
706 | input->loaded = false; | |
210c52ac | 707 | input->search_dirs_flag = false; |
b9395be3 ILT |
708 | |
709 | /* FIXME: The following fields are not set: header.next, | |
210c52ac | 710 | header.type, closed, passive_position, symbol_count, |
cd09553a ILT |
711 | next_real_file, is_archive, target, real. This bit of code is |
712 | from the old decode_library_subfile function. I don't know | |
713 | whether any of those fields matters. */ | |
b9395be3 ILT |
714 | |
715 | ldlang_add_file (input); | |
716 | ||
2a9fa50c | 717 | if (config.map_file != (FILE *) NULL) |
efa6c497 ILT |
718 | { |
719 | static boolean header_printed; | |
720 | struct bfd_link_hash_entry *h; | |
721 | bfd *from; | |
722 | int len; | |
723 | ||
724 | h = bfd_link_hash_lookup (link_info.hash, name, false, false, true); | |
725 | ||
726 | if (h == NULL) | |
727 | from = NULL; | |
728 | else | |
729 | { | |
730 | switch (h->type) | |
731 | { | |
732 | default: | |
733 | from = NULL; | |
734 | break; | |
735 | ||
736 | case bfd_link_hash_defined: | |
737 | case bfd_link_hash_defweak: | |
738 | from = h->u.def.section->owner; | |
739 | break; | |
740 | ||
741 | case bfd_link_hash_undefined: | |
742 | case bfd_link_hash_undefweak: | |
743 | from = h->u.undef.abfd; | |
744 | break; | |
745 | ||
746 | case bfd_link_hash_common: | |
747 | from = h->u.c.p->section->owner; | |
748 | break; | |
749 | } | |
750 | } | |
751 | ||
752 | if (! header_printed) | |
753 | { | |
754 | char buf[100]; | |
755 | ||
8bc8f4bc TT |
756 | sprintf (buf, "%-29s %s\n\n", _("Archive member included"), |
757 | _("because of file (symbol)")); | |
efa6c497 ILT |
758 | minfo ("%s", buf); |
759 | header_printed = true; | |
760 | } | |
761 | ||
762 | if (bfd_my_archive (abfd) == NULL) | |
763 | { | |
764 | minfo ("%s", bfd_get_filename (abfd)); | |
765 | len = strlen (bfd_get_filename (abfd)); | |
766 | } | |
767 | else | |
768 | { | |
769 | minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)), | |
770 | bfd_get_filename (abfd)); | |
771 | len = (strlen (bfd_get_filename (bfd_my_archive (abfd))) | |
772 | + strlen (bfd_get_filename (abfd)) | |
773 | + 2); | |
774 | } | |
775 | ||
776 | if (len >= 29) | |
777 | { | |
778 | print_nl (); | |
779 | len = 0; | |
780 | } | |
781 | while (len < 30) | |
782 | { | |
783 | print_space (); | |
784 | ++len; | |
785 | } | |
786 | ||
787 | if (from != NULL) | |
788 | minfo ("%B ", from); | |
789 | if (h != NULL) | |
790 | minfo ("(%T)\n", h->root.string); | |
791 | else | |
792 | minfo ("(%s)\n", name); | |
793 | } | |
b9395be3 | 794 | |
5dad4c97 ILT |
795 | if (trace_files || trace_file_tries) |
796 | info_msg ("%I\n", input); | |
797 | ||
b9395be3 ILT |
798 | return true; |
799 | } | |
2fa0b342 | 800 | |
b9395be3 ILT |
801 | /* This is called when BFD has discovered a symbol which is defined |
802 | multiple times. */ | |
2fa0b342 | 803 | |
b9395be3 ILT |
804 | /*ARGSUSED*/ |
805 | static boolean | |
806 | multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval) | |
807 | struct bfd_link_info *info; | |
808 | const char *name; | |
809 | bfd *obfd; | |
810 | asection *osec; | |
811 | bfd_vma oval; | |
812 | bfd *nbfd; | |
813 | asection *nsec; | |
814 | bfd_vma nval; | |
815 | { | |
6799c638 ILT |
816 | /* If either section has the output_section field set to |
817 | bfd_abs_section_ptr, it means that the section is being | |
818 | discarded, and this is not really a multiple definition at all. | |
819 | FIXME: It would be cleaner to somehow ignore symbols defined in | |
820 | sections which are being discarded. */ | |
821 | if ((osec->output_section != NULL | |
d7339c87 | 822 | && ! bfd_is_abs_section (osec) |
6799c638 ILT |
823 | && bfd_is_abs_section (osec->output_section)) |
824 | || (nsec->output_section != NULL | |
d7339c87 | 825 | && ! bfd_is_abs_section (nsec) |
6799c638 ILT |
826 | && bfd_is_abs_section (nsec->output_section))) |
827 | return true; | |
828 | ||
8bc8f4bc | 829 | einfo (_("%X%C: multiple definition of `%T'\n"), |
b9395be3 ILT |
830 | nbfd, nsec, nval, name); |
831 | if (obfd != (bfd *) NULL) | |
8bc8f4bc | 832 | einfo (_("%D: first defined here\n"), obfd, osec, oval); |
b9395be3 | 833 | return true; |
2fa0b342 DHW |
834 | } |
835 | ||
b9395be3 ILT |
836 | /* This is called when there is a definition of a common symbol, or |
837 | when a common symbol is found for a symbol that is already defined, | |
838 | or when two common symbols are found. We only do something if | |
839 | -warn-common was used. */ | |
840 | ||
841 | /*ARGSUSED*/ | |
842 | static boolean | |
843 | multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize) | |
844 | struct bfd_link_info *info; | |
845 | const char *name; | |
846 | bfd *obfd; | |
847 | enum bfd_link_hash_type otype; | |
848 | bfd_vma osize; | |
849 | bfd *nbfd; | |
850 | enum bfd_link_hash_type ntype; | |
851 | bfd_vma nsize; | |
99fe4553 | 852 | { |
b9395be3 ILT |
853 | if (! config.warn_common) |
854 | return true; | |
99fe4553 | 855 | |
8ed88239 ILT |
856 | if (ntype == bfd_link_hash_defined |
857 | || ntype == bfd_link_hash_defweak | |
858 | || ntype == bfd_link_hash_indirect) | |
e47bfa63 | 859 | { |
b9395be3 | 860 | ASSERT (otype == bfd_link_hash_common); |
8bc8f4bc | 861 | einfo (_("%B: warning: definition of `%T' overriding common\n"), |
b9395be3 | 862 | nbfd, name); |
8ed88239 | 863 | if (obfd != NULL) |
8bc8f4bc | 864 | einfo (_("%B: warning: common is here\n"), obfd); |
e47bfa63 | 865 | } |
8ed88239 ILT |
866 | else if (otype == bfd_link_hash_defined |
867 | || otype == bfd_link_hash_defweak | |
868 | || otype == bfd_link_hash_indirect) | |
2fa0b342 | 869 | { |
b9395be3 | 870 | ASSERT (ntype == bfd_link_hash_common); |
8bc8f4bc | 871 | einfo (_("%B: warning: common of `%T' overridden by definition\n"), |
b9395be3 | 872 | nbfd, name); |
8ed88239 | 873 | if (obfd != NULL) |
8bc8f4bc | 874 | einfo (_("%B: warning: defined here\n"), obfd); |
b9395be3 ILT |
875 | } |
876 | else | |
877 | { | |
878 | ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common); | |
879 | if (osize > nsize) | |
2fa0b342 | 880 | { |
8bc8f4bc | 881 | einfo (_("%B: warning: common of `%T' overridden by larger common\n"), |
b9395be3 | 882 | nbfd, name); |
8ed88239 | 883 | if (obfd != NULL) |
8bc8f4bc | 884 | einfo (_("%B: warning: larger common is here\n"), obfd); |
2fa0b342 | 885 | } |
b9395be3 | 886 | else if (nsize > osize) |
2fa0b342 | 887 | { |
8bc8f4bc | 888 | einfo (_("%B: warning: common of `%T' overriding smaller common\n"), |
b9395be3 | 889 | nbfd, name); |
8ed88239 | 890 | if (obfd != NULL) |
8bc8f4bc | 891 | einfo (_("%B: warning: smaller common is here\n"), obfd); |
2fa0b342 | 892 | } |
e47bfa63 | 893 | else |
2fa0b342 | 894 | { |
8bc8f4bc | 895 | einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name); |
8ed88239 | 896 | if (obfd != NULL) |
8bc8f4bc | 897 | einfo (_("%B: warning: previous common is here\n"), obfd); |
2fa0b342 DHW |
898 | } |
899 | } | |
900 | ||
b9395be3 | 901 | return true; |
2fa0b342 DHW |
902 | } |
903 | ||
b9395be3 ILT |
904 | /* This is called when BFD has discovered a set element. H is the |
905 | entry in the linker hash table for the set. SECTION and VALUE | |
906 | represent a value which should be added to the set. */ | |
2fa0b342 | 907 | |
b9395be3 ILT |
908 | /*ARGSUSED*/ |
909 | static boolean | |
2a9fa50c | 910 | add_to_set (info, h, reloc, abfd, section, value) |
b9395be3 ILT |
911 | struct bfd_link_info *info; |
912 | struct bfd_link_hash_entry *h; | |
2a9fa50c | 913 | bfd_reloc_code_real_type reloc; |
b9395be3 ILT |
914 | bfd *abfd; |
915 | asection *section; | |
916 | bfd_vma value; | |
2fa0b342 | 917 | { |
7d6439d9 | 918 | if (config.warn_constructors) |
8bc8f4bc | 919 | einfo (_("%P: warning: global constructor %s used\n"), |
7d6439d9 ILT |
920 | h->root.string); |
921 | ||
2a9fa50c ILT |
922 | if (! config.build_constructors) |
923 | return true; | |
924 | ||
4b7d2399 | 925 | ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value); |
2a9fa50c ILT |
926 | |
927 | if (h->type == bfd_link_hash_new) | |
928 | { | |
929 | h->type = bfd_link_hash_undefined; | |
930 | h->u.undef.abfd = abfd; | |
931 | /* We don't call bfd_link_add_undef to add this to the list of | |
932 | undefined symbols because we are going to define it | |
933 | ourselves. */ | |
934 | } | |
935 | ||
b9395be3 ILT |
936 | return true; |
937 | } | |
1418c83b | 938 | |
b9395be3 ILT |
939 | /* This is called when BFD has discovered a constructor. This is only |
940 | called for some object file formats--those which do not handle | |
941 | constructors in some more clever fashion. This is similar to | |
942 | adding an element to a set, but less general. */ | |
2fa0b342 | 943 | |
b9395be3 | 944 | static boolean |
2a9fa50c | 945 | constructor_callback (info, constructor, name, abfd, section, value) |
b9395be3 ILT |
946 | struct bfd_link_info *info; |
947 | boolean constructor; | |
b9395be3 ILT |
948 | const char *name; |
949 | bfd *abfd; | |
950 | asection *section; | |
951 | bfd_vma value; | |
952 | { | |
b9395be3 ILT |
953 | char *s; |
954 | struct bfd_link_hash_entry *h; | |
7d6439d9 ILT |
955 | char set_name[1 + sizeof "__CTOR_LIST__"]; |
956 | ||
957 | if (config.warn_constructors) | |
8bc8f4bc | 958 | einfo (_("%P: warning: global constructor %s used\n"), name); |
b9395be3 ILT |
959 | |
960 | if (! config.build_constructors) | |
961 | return true; | |
962 | ||
2a9fa50c ILT |
963 | /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a |
964 | useful error message. */ | |
4b7d2399 ILT |
965 | if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL |
966 | && (link_info.relocateable | |
967 | || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL)) | |
8bc8f4bc | 968 | einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n")); |
2a9fa50c | 969 | |
b9395be3 ILT |
970 | s = set_name; |
971 | if (bfd_get_symbol_leading_char (abfd) != '\0') | |
972 | *s++ = bfd_get_symbol_leading_char (abfd); | |
973 | if (constructor) | |
974 | strcpy (s, "__CTOR_LIST__"); | |
29f33467 | 975 | else |
b9395be3 | 976 | strcpy (s, "__DTOR_LIST__"); |
2fa0b342 | 977 | |
b9395be3 ILT |
978 | h = bfd_link_hash_lookup (info->hash, set_name, true, true, true); |
979 | if (h == (struct bfd_link_hash_entry *) NULL) | |
8bc8f4bc | 980 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
b9395be3 | 981 | if (h->type == bfd_link_hash_new) |
e47bfa63 | 982 | { |
b9395be3 ILT |
983 | h->type = bfd_link_hash_undefined; |
984 | h->u.undef.abfd = abfd; | |
985 | /* We don't call bfd_link_add_undef to add this to the list of | |
986 | undefined symbols because we are going to define it | |
987 | ourselves. */ | |
2fa0b342 | 988 | } |
2fa0b342 | 989 | |
4b7d2399 | 990 | ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value); |
b9395be3 | 991 | return true; |
2fa0b342 DHW |
992 | } |
993 | ||
7d6439d9 ILT |
994 | /* A structure used by warning_callback to pass information through |
995 | bfd_map_over_sections. */ | |
996 | ||
997 | struct warning_callback_info | |
998 | { | |
999 | boolean found; | |
1000 | const char *warning; | |
1001 | const char *symbol; | |
1002 | asymbol **asymbols; | |
1003 | }; | |
1004 | ||
b9395be3 | 1005 | /* This is called when there is a reference to a warning symbol. */ |
2fa0b342 | 1006 | |
b9395be3 ILT |
1007 | /*ARGSUSED*/ |
1008 | static boolean | |
7d6439d9 | 1009 | warning_callback (info, warning, symbol, abfd, section, address) |
b9395be3 ILT |
1010 | struct bfd_link_info *info; |
1011 | const char *warning; | |
7d6439d9 ILT |
1012 | const char *symbol; |
1013 | bfd *abfd; | |
1014 | asection *section; | |
1015 | bfd_vma address; | |
2fa0b342 | 1016 | { |
4b7d2399 ILT |
1017 | /* This is a hack to support warn_multiple_gp. FIXME: This should |
1018 | have a cleaner interface, but what? */ | |
1019 | if (! config.warn_multiple_gp | |
1020 | && strcmp (warning, "using multiple gp values") == 0) | |
1021 | return true; | |
1022 | ||
7d6439d9 ILT |
1023 | if (section != NULL) |
1024 | einfo ("%C: %s\n", abfd, section, address, warning); | |
1025 | else if (abfd == NULL) | |
1026 | einfo ("%P: %s\n", warning); | |
1027 | else if (symbol == NULL) | |
1028 | einfo ("%B: %s\n", abfd, warning); | |
1029 | else | |
1030 | { | |
1031 | lang_input_statement_type *entry; | |
1032 | asymbol **asymbols; | |
1033 | struct warning_callback_info info; | |
1034 | ||
1035 | /* Look through the relocs to see if we can find a plausible | |
1036 | address. */ | |
1037 | ||
1038 | entry = (lang_input_statement_type *) abfd->usrdata; | |
1039 | if (entry != NULL && entry->asymbols != NULL) | |
1040 | asymbols = entry->asymbols; | |
1041 | else | |
1042 | { | |
1043 | long symsize; | |
1044 | long symbol_count; | |
1045 | ||
1046 | symsize = bfd_get_symtab_upper_bound (abfd); | |
1047 | if (symsize < 0) | |
8bc8f4bc | 1048 | einfo (_("%B%F: could not read symbols: %E\n"), abfd); |
7d6439d9 ILT |
1049 | asymbols = (asymbol **) xmalloc (symsize); |
1050 | symbol_count = bfd_canonicalize_symtab (abfd, asymbols); | |
1051 | if (symbol_count < 0) | |
8bc8f4bc | 1052 | einfo (_("%B%F: could not read symbols: %E\n"), abfd); |
7d6439d9 ILT |
1053 | if (entry != NULL) |
1054 | { | |
1055 | entry->asymbols = asymbols; | |
1056 | entry->symbol_count = symbol_count; | |
1057 | } | |
1058 | } | |
1059 | ||
1060 | info.found = false; | |
1061 | info.warning = warning; | |
1062 | info.symbol = symbol; | |
1063 | info.asymbols = asymbols; | |
1064 | bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info); | |
1065 | ||
1066 | if (! info.found) | |
1067 | einfo ("%B: %s\n", abfd, warning); | |
582dd77f ILT |
1068 | |
1069 | if (entry == NULL) | |
1070 | free (asymbols); | |
7d6439d9 ILT |
1071 | } |
1072 | ||
b9395be3 | 1073 | return true; |
2fa0b342 DHW |
1074 | } |
1075 | ||
7d6439d9 ILT |
1076 | /* This is called by warning_callback for each section. It checks the |
1077 | relocs of the section to see if it can find a reference to the | |
1078 | symbol which triggered the warning. If it can, it uses the reloc | |
1079 | to give an error message with a file and line number. */ | |
1080 | ||
1081 | static void | |
1082 | warning_find_reloc (abfd, sec, iarg) | |
1083 | bfd *abfd; | |
1084 | asection *sec; | |
1085 | PTR iarg; | |
1086 | { | |
1087 | struct warning_callback_info *info = (struct warning_callback_info *) iarg; | |
1088 | long relsize; | |
1089 | arelent **relpp; | |
1090 | long relcount; | |
1091 | arelent **p, **pend; | |
1092 | ||
1093 | if (info->found) | |
1094 | return; | |
1095 | ||
1096 | relsize = bfd_get_reloc_upper_bound (abfd, sec); | |
1097 | if (relsize < 0) | |
8bc8f4bc | 1098 | einfo (_("%B%F: could not read relocs: %E\n"), abfd); |
7d6439d9 ILT |
1099 | if (relsize == 0) |
1100 | return; | |
1101 | ||
1102 | relpp = (arelent **) xmalloc (relsize); | |
1103 | relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols); | |
1104 | if (relcount < 0) | |
8bc8f4bc | 1105 | einfo (_("%B%F: could not read relocs: %E\n"), abfd); |
7d6439d9 ILT |
1106 | |
1107 | p = relpp; | |
1108 | pend = p + relcount; | |
1109 | for (; p < pend && *p != NULL; p++) | |
1110 | { | |
1111 | arelent *q = *p; | |
1112 | ||
1113 | if (q->sym_ptr_ptr != NULL | |
1114 | && *q->sym_ptr_ptr != NULL | |
1115 | && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0) | |
1116 | { | |
1117 | /* We found a reloc for the symbol we are looking for. */ | |
1118 | einfo ("%C: %s\n", abfd, sec, q->address, info->warning); | |
1119 | info->found = true; | |
1120 | break; | |
1121 | } | |
1122 | } | |
1123 | ||
1124 | free (relpp); | |
1125 | } | |
1126 | ||
b9395be3 | 1127 | /* This is called when an undefined symbol is found. */ |
2fa0b342 | 1128 | |
b9395be3 ILT |
1129 | /*ARGSUSED*/ |
1130 | static boolean | |
1131 | undefined_symbol (info, name, abfd, section, address) | |
1132 | struct bfd_link_info *info; | |
1133 | const char *name; | |
1134 | bfd *abfd; | |
1135 | asection *section; | |
1136 | bfd_vma address; | |
1137 | { | |
1138 | static char *error_name; | |
1139 | static unsigned int error_count; | |
29f33467 | 1140 | |
b9395be3 | 1141 | #define MAX_ERRORS_IN_A_ROW 5 |
8a045e50 | 1142 | |
809ee7e0 ILT |
1143 | if (config.warn_once) |
1144 | { | |
1145 | static struct bfd_hash_table *hash; | |
1146 | ||
1147 | /* Only warn once about a particular undefined symbol. */ | |
1148 | ||
1149 | if (hash == NULL) | |
1150 | { | |
1151 | hash = ((struct bfd_hash_table *) | |
1152 | xmalloc (sizeof (struct bfd_hash_table))); | |
1153 | if (! bfd_hash_table_init (hash, bfd_hash_newfunc)) | |
8bc8f4bc | 1154 | einfo (_("%F%P: bfd_hash_table_init failed: %E\n")); |
809ee7e0 ILT |
1155 | } |
1156 | ||
1157 | if (bfd_hash_lookup (hash, name, false, false) != NULL) | |
1158 | return true; | |
1159 | ||
1160 | if (bfd_hash_lookup (hash, name, true, true) == NULL) | |
8bc8f4bc | 1161 | einfo (_("%F%P: bfd_hash_lookup failed: %E\n")); |
809ee7e0 ILT |
1162 | } |
1163 | ||
b9395be3 ILT |
1164 | /* We never print more than a reasonable number of errors in a row |
1165 | for a single symbol. */ | |
1166 | if (error_name != (char *) NULL | |
1167 | && strcmp (name, error_name) == 0) | |
1168 | ++error_count; | |
1169 | else | |
e47bfa63 | 1170 | { |
b9395be3 ILT |
1171 | error_count = 0; |
1172 | if (error_name != (char *) NULL) | |
1173 | free (error_name); | |
1174 | error_name = buystring (name); | |
1175 | } | |
2fa0b342 | 1176 | |
23244cd6 ILT |
1177 | if (section != NULL) |
1178 | { | |
1179 | if (error_count < MAX_ERRORS_IN_A_ROW) | |
8bc8f4bc | 1180 | einfo (_("%X%C: undefined reference to `%T'\n"), |
23244cd6 ILT |
1181 | abfd, section, address, name); |
1182 | else if (error_count == MAX_ERRORS_IN_A_ROW) | |
8bc8f4bc | 1183 | einfo (_("%D: more undefined references to `%T' follow\n"), |
23244cd6 ILT |
1184 | abfd, section, address, name); |
1185 | } | |
1186 | else | |
1187 | { | |
1188 | if (error_count < MAX_ERRORS_IN_A_ROW) | |
8bc8f4bc | 1189 | einfo (_("%X%B: undefined reference to `%T'\n"), |
23244cd6 ILT |
1190 | abfd, name); |
1191 | else if (error_count == MAX_ERRORS_IN_A_ROW) | |
8bc8f4bc | 1192 | einfo (_("%B: more undefined references to `%T' follow\n"), |
23244cd6 ILT |
1193 | abfd, name); |
1194 | } | |
2fa0b342 | 1195 | |
b9395be3 | 1196 | return true; |
2fa0b342 DHW |
1197 | } |
1198 | ||
b9395be3 | 1199 | /* This is called when a reloc overflows. */ |
2fa0b342 | 1200 | |
b9395be3 | 1201 | /*ARGSUSED*/ |
9f629407 | 1202 | static boolean |
5dad4c97 | 1203 | reloc_overflow (info, name, reloc_name, addend, abfd, section, address) |
b9395be3 | 1204 | struct bfd_link_info *info; |
5dad4c97 ILT |
1205 | const char *name; |
1206 | const char *reloc_name; | |
1207 | bfd_vma addend; | |
b9395be3 ILT |
1208 | bfd *abfd; |
1209 | asection *section; | |
1210 | bfd_vma address; | |
2fa0b342 | 1211 | { |
2a9fa50c | 1212 | if (abfd == (bfd *) NULL) |
8bc8f4bc | 1213 | einfo (_("%P%X: generated")); |
2a9fa50c ILT |
1214 | else |
1215 | einfo ("%X%C:", abfd, section, address); | |
8bc8f4bc | 1216 | einfo (_(" relocation truncated to fit: %s %T"), reloc_name, name); |
5dad4c97 ILT |
1217 | if (addend != 0) |
1218 | einfo ("+%v", addend); | |
1219 | einfo ("\n"); | |
b9395be3 ILT |
1220 | return true; |
1221 | } | |
2fa0b342 | 1222 | |
b9395be3 | 1223 | /* This is called when a dangerous relocation is made. */ |
3e4c643d | 1224 | |
b9395be3 ILT |
1225 | /*ARGSUSED*/ |
1226 | static boolean | |
1227 | reloc_dangerous (info, message, abfd, section, address) | |
1228 | struct bfd_link_info *info; | |
1229 | const char *message; | |
1230 | bfd *abfd; | |
1231 | asection *section; | |
1232 | bfd_vma address; | |
1233 | { | |
2a9fa50c | 1234 | if (abfd == (bfd *) NULL) |
8bc8f4bc | 1235 | einfo (_("%P%X: generated")); |
2a9fa50c ILT |
1236 | else |
1237 | einfo ("%X%C:", abfd, section, address); | |
8bc8f4bc | 1238 | einfo (_("dangerous relocation: %s\n"), message); |
b9395be3 ILT |
1239 | return true; |
1240 | } | |
973e421e | 1241 | |
b9395be3 ILT |
1242 | /* This is called when a reloc is being generated attached to a symbol |
1243 | that is not being output. */ | |
2fa0b342 | 1244 | |
b9395be3 ILT |
1245 | /*ARGSUSED*/ |
1246 | static boolean | |
1247 | unattached_reloc (info, name, abfd, section, address) | |
1248 | struct bfd_link_info *info; | |
1249 | const char *name; | |
1250 | bfd *abfd; | |
1251 | asection *section; | |
1252 | bfd_vma address; | |
1253 | { | |
2a9fa50c | 1254 | if (abfd == (bfd *) NULL) |
8bc8f4bc | 1255 | einfo (_("%P%X: generated")); |
2a9fa50c ILT |
1256 | else |
1257 | einfo ("%X%C:", abfd, section, address); | |
8bc8f4bc | 1258 | einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name); |
b9395be3 | 1259 | return true; |
2fa0b342 | 1260 | } |
8a045e50 | 1261 | |
cd09553a ILT |
1262 | /* This is called if link_info.notice_all is set, or when a symbol in |
1263 | link_info.notice_hash is found. Symbols are put in notice_hash | |
1264 | using the -y option. */ | |
b9395be3 | 1265 | |
b9395be3 | 1266 | static boolean |
cd09553a | 1267 | notice (info, name, abfd, section, value) |
b9395be3 ILT |
1268 | struct bfd_link_info *info; |
1269 | const char *name; | |
1270 | bfd *abfd; | |
1271 | asection *section; | |
1272 | bfd_vma value; | |
8a045e50 | 1273 | { |
cd09553a ILT |
1274 | if (! info->notice_all |
1275 | || (info->notice_hash != NULL | |
1276 | && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL)) | |
8bc8f4bc TT |
1277 | { |
1278 | if (bfd_is_und_section (section)) | |
1279 | einfo ("%B: reference to %s\n", abfd, name); | |
1280 | else | |
1281 | einfo ("%B: definition of %s\n", abfd, name); | |
1282 | } | |
cd09553a | 1283 | |
582dd77f | 1284 | if (command_line.cref || nocrossref_list != NULL) |
cd09553a ILT |
1285 | add_cref (name, abfd, section, value); |
1286 | ||
b9395be3 | 1287 | return true; |
8a045e50 | 1288 | } |