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