Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ldmisc.c |
6d39955e | 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
5ed6aba4 | 3 | 2000, 2002, 2003 |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | Written by Steve Chamberlain of Cygnus Support. | |
6 | ||
5ed6aba4 | 7 | This file is part of GLD, the Gnu Linker. |
252b5132 | 8 | |
5ed6aba4 NC |
9 | GLD is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2, or (at your option) | |
12 | any later version. | |
252b5132 | 13 | |
5ed6aba4 NC |
14 | GLD is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
252b5132 | 18 | |
5ed6aba4 NC |
19 | You should have received a copy of the GNU General Public License |
20 | along with GLD; see the file COPYING. If not, write to the Free | |
21 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
22 | 02111-1307, USA. */ | |
252b5132 RH |
23 | |
24 | #include "bfd.h" | |
6f6f27f8 | 25 | #include "bfdlink.h" |
252b5132 RH |
26 | #include "sysdep.h" |
27 | #include "libiberty.h" | |
28 | #include "demangle.h" | |
252b5132 | 29 | #include <stdarg.h> |
252b5132 RH |
30 | #include "ld.h" |
31 | #include "ldmisc.h" | |
32 | #include "ldexp.h" | |
33 | #include "ldlang.h" | |
df2a7313 | 34 | #include <ldgram.h> |
252b5132 RH |
35 | #include "ldlex.h" |
36 | #include "ldmain.h" | |
37 | #include "ldfile.h" | |
38 | ||
252b5132 RH |
39 | /* |
40 | %% literal % | |
41 | %F error is fatal | |
42 | %P print program name | |
43 | %S print script file and linenumber | |
44 | %E current bfd error or errno | |
45 | %I filename from a lang_input_statement_type | |
46 | %B filename from a bfd | |
47 | %T symbol name | |
48 | %X no object output, fail return | |
49 | %V hex bfd_vma | |
50 | %v hex bfd_vma, no leading zeros | |
51 | %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces | |
52 | %C clever filename:linenumber with function | |
53 | %D like %C, but no function name | |
54 | %G like %D, but only function name | |
55 | %R info about a relent | |
56 | %s arbitrary string, like printf | |
57 | %d integer, like printf | |
58 | %u integer, like printf | |
59 | */ | |
60 | ||
252b5132 | 61 | static void |
1579bae1 | 62 | vfinfo (FILE *fp, const char *fmt, va_list arg) |
252b5132 | 63 | { |
b34976b6 | 64 | bfd_boolean fatal = FALSE; |
252b5132 RH |
65 | |
66 | while (*fmt != '\0') | |
67 | { | |
6d5e62f8 | 68 | while (*fmt != '%' && *fmt != '\0') |
252b5132 RH |
69 | { |
70 | putc (*fmt, fp); | |
71 | fmt++; | |
72 | } | |
73 | ||
6d5e62f8 | 74 | if (*fmt == '%') |
252b5132 | 75 | { |
6d5e62f8 KH |
76 | fmt++; |
77 | switch (*fmt++) | |
252b5132 RH |
78 | { |
79 | default: | |
6d5e62f8 | 80 | fprintf (fp, "%%%c", fmt[-1]); |
252b5132 RH |
81 | break; |
82 | ||
83 | case '%': | |
84 | /* literal % */ | |
85 | putc ('%', fp); | |
86 | break; | |
87 | ||
88 | case 'X': | |
89 | /* no object output, fail return */ | |
b34976b6 | 90 | config.make_executable = FALSE; |
252b5132 RH |
91 | break; |
92 | ||
93 | case 'V': | |
94 | /* hex bfd_vma */ | |
95 | { | |
96 | bfd_vma value = va_arg (arg, bfd_vma); | |
97 | fprintf_vma (fp, value); | |
98 | } | |
99 | break; | |
100 | ||
101 | case 'v': | |
102 | /* hex bfd_vma, no leading zeros */ | |
103 | { | |
104 | char buf[100]; | |
105 | char *p = buf; | |
106 | bfd_vma value = va_arg (arg, bfd_vma); | |
107 | sprintf_vma (p, value); | |
108 | while (*p == '0') | |
109 | p++; | |
110 | if (!*p) | |
111 | p--; | |
112 | fputs (p, fp); | |
113 | } | |
114 | break; | |
115 | ||
116 | case 'W': | |
117 | /* hex bfd_vma with 0x with no leading zeroes taking up | |
1579bae1 | 118 | 8 spaces. */ |
252b5132 RH |
119 | { |
120 | char buf[100]; | |
121 | bfd_vma value; | |
122 | char *p; | |
123 | int len; | |
124 | ||
125 | value = va_arg (arg, bfd_vma); | |
126 | sprintf_vma (buf, value); | |
127 | for (p = buf; *p == '0'; ++p) | |
128 | ; | |
129 | if (*p == '\0') | |
130 | --p; | |
131 | len = strlen (p); | |
132 | while (len < 8) | |
133 | { | |
134 | putc (' ', fp); | |
135 | ++len; | |
136 | } | |
137 | fprintf (fp, "0x%s", p); | |
138 | } | |
139 | break; | |
140 | ||
141 | case 'T': | |
142 | /* Symbol name. */ | |
143 | { | |
144 | const char *name = va_arg (arg, const char *); | |
145 | ||
1579bae1 | 146 | if (name == NULL || *name == 0) |
252b5132 RH |
147 | fprintf (fp, _("no symbol")); |
148 | else if (! demangling) | |
149 | fprintf (fp, "%s", name); | |
150 | else | |
151 | { | |
152 | char *demangled; | |
153 | ||
154 | demangled = demangle (name); | |
155 | fprintf (fp, "%s", demangled); | |
156 | free (demangled); | |
157 | } | |
158 | } | |
159 | break; | |
160 | ||
161 | case 'B': | |
162 | /* filename from a bfd */ | |
6d5e62f8 | 163 | { |
252b5132 RH |
164 | bfd *abfd = va_arg (arg, bfd *); |
165 | if (abfd->my_archive) | |
166 | fprintf (fp, "%s(%s)", abfd->my_archive->filename, | |
167 | abfd->filename); | |
168 | else | |
169 | fprintf (fp, "%s", abfd->filename); | |
170 | } | |
171 | break; | |
172 | ||
173 | case 'F': | |
6d5e62f8 | 174 | /* Error is fatal. */ |
b34976b6 | 175 | fatal = TRUE; |
252b5132 RH |
176 | break; |
177 | ||
178 | case 'P': | |
6d5e62f8 | 179 | /* Print program name. */ |
252b5132 RH |
180 | fprintf (fp, "%s", program_name); |
181 | break; | |
182 | ||
183 | case 'E': | |
184 | /* current bfd error or errno */ | |
305c7206 | 185 | fprintf (fp, "%s", bfd_errmsg (bfd_get_error ())); |
252b5132 RH |
186 | break; |
187 | ||
188 | case 'I': | |
189 | /* filename from a lang_input_statement_type */ | |
190 | { | |
191 | lang_input_statement_type *i; | |
192 | ||
193 | i = va_arg (arg, lang_input_statement_type *); | |
194 | if (bfd_my_archive (i->the_bfd) != NULL) | |
195 | fprintf (fp, "(%s)", | |
196 | bfd_get_filename (bfd_my_archive (i->the_bfd))); | |
197 | fprintf (fp, "%s", i->local_sym_name); | |
198 | if (bfd_my_archive (i->the_bfd) == NULL | |
199 | && strcmp (i->local_sym_name, i->filename) != 0) | |
200 | fprintf (fp, " (%s)", i->filename); | |
201 | } | |
202 | break; | |
203 | ||
204 | case 'S': | |
6d5e62f8 | 205 | /* Print script file and linenumber. */ |
252b5132 RH |
206 | if (parsing_defsym) |
207 | fprintf (fp, "--defsym %s", lex_string); | |
208 | else if (ldfile_input_filename != NULL) | |
209 | fprintf (fp, "%s:%u", ldfile_input_filename, lineno); | |
210 | else | |
211 | fprintf (fp, _("built in linker script:%u"), lineno); | |
212 | break; | |
213 | ||
214 | case 'R': | |
6d5e62f8 | 215 | /* Print all that's interesting about a relent. */ |
252b5132 RH |
216 | { |
217 | arelent *relent = va_arg (arg, arelent *); | |
6d5e62f8 | 218 | |
252b5132 RH |
219 | lfinfo (fp, "%s+0x%v (type %s)", |
220 | (*(relent->sym_ptr_ptr))->name, | |
221 | relent->addend, | |
222 | relent->howto->name); | |
223 | } | |
224 | break; | |
6d5e62f8 | 225 | |
252b5132 RH |
226 | case 'C': |
227 | case 'D': | |
228 | case 'G': | |
5cfb2bb2 AM |
229 | /* Clever filename:linenumber with function name if possible. |
230 | The arguments are a BFD, a section, and an offset. */ | |
252b5132 RH |
231 | { |
232 | static bfd *last_bfd; | |
233 | static char *last_file = NULL; | |
234 | static char *last_function = NULL; | |
235 | bfd *abfd; | |
236 | asection *section; | |
237 | bfd_vma offset; | |
238 | lang_input_statement_type *entry; | |
239 | asymbol **asymbols; | |
240 | const char *filename; | |
241 | const char *functionname; | |
242 | unsigned int linenumber; | |
b34976b6 | 243 | bfd_boolean discard_last; |
252b5132 RH |
244 | |
245 | abfd = va_arg (arg, bfd *); | |
246 | section = va_arg (arg, asection *); | |
247 | offset = va_arg (arg, bfd_vma); | |
248 | ||
249 | entry = (lang_input_statement_type *) abfd->usrdata; | |
250 | if (entry != (lang_input_statement_type *) NULL | |
251 | && entry->asymbols != (asymbol **) NULL) | |
252 | asymbols = entry->asymbols; | |
253 | else | |
254 | { | |
255 | long symsize; | |
256 | long symbol_count; | |
257 | ||
258 | symsize = bfd_get_symtab_upper_bound (abfd); | |
259 | if (symsize < 0) | |
260 | einfo (_("%B%F: could not read symbols\n"), abfd); | |
1579bae1 | 261 | asymbols = xmalloc (symsize); |
252b5132 RH |
262 | symbol_count = bfd_canonicalize_symtab (abfd, asymbols); |
263 | if (symbol_count < 0) | |
264 | einfo (_("%B%F: could not read symbols\n"), abfd); | |
265 | if (entry != (lang_input_statement_type *) NULL) | |
266 | { | |
267 | entry->asymbols = asymbols; | |
268 | entry->symbol_count = symbol_count; | |
269 | } | |
270 | } | |
271 | ||
5cfb2bb2 AM |
272 | lfinfo (fp, "%B(%s+0x%v)", abfd, section->name, offset); |
273 | ||
b34976b6 | 274 | discard_last = TRUE; |
252b5132 RH |
275 | if (bfd_find_nearest_line (abfd, section, asymbols, offset, |
276 | &filename, &functionname, | |
277 | &linenumber)) | |
278 | { | |
b34976b6 | 279 | bfd_boolean need_colon = TRUE; |
252b5132 | 280 | |
5cfb2bb2 AM |
281 | if (functionname != NULL && fmt[-1] == 'C') |
282 | { | |
252b5132 RH |
283 | if (last_bfd == NULL |
284 | || last_file == NULL | |
285 | || last_function == NULL | |
286 | || last_bfd != abfd | |
5cfb2bb2 AM |
287 | || (filename != NULL |
288 | && strcmp (last_file, filename) != 0) | |
252b5132 RH |
289 | || strcmp (last_function, functionname) != 0) |
290 | { | |
5cfb2bb2 AM |
291 | lfinfo (fp, _(": In function `%T':\n"), |
292 | functionname); | |
b34976b6 | 293 | need_colon = FALSE; |
252b5132 RH |
294 | |
295 | last_bfd = abfd; | |
296 | if (last_file != NULL) | |
297 | free (last_file); | |
5cfb2bb2 AM |
298 | last_file = NULL; |
299 | if (filename) | |
300 | last_file = xstrdup (filename); | |
252b5132 RH |
301 | if (last_function != NULL) |
302 | free (last_function); | |
d1b2b2dc | 303 | last_function = xstrdup (functionname); |
252b5132 | 304 | } |
b34976b6 | 305 | discard_last = FALSE; |
252b5132 | 306 | } |
5cfb2bb2 AM |
307 | |
308 | if (filename != NULL) | |
252b5132 | 309 | { |
5cfb2bb2 AM |
310 | if (need_colon) |
311 | putc (':', fp); | |
312 | fputs (filename, fp); | |
252b5132 | 313 | } |
5cfb2bb2 AM |
314 | |
315 | if (functionname != NULL && fmt[-1] == 'G') | |
316 | lfinfo (fp, ":%T", functionname); | |
317 | else if (filename != NULL && linenumber != 0) | |
318 | fprintf (fp, ":%u", linenumber); | |
252b5132 | 319 | } |
252b5132 | 320 | |
5ed6aba4 NC |
321 | if (asymbols != NULL && entry == NULL) |
322 | free (asymbols); | |
323 | ||
252b5132 RH |
324 | if (discard_last) |
325 | { | |
326 | last_bfd = NULL; | |
327 | if (last_file != NULL) | |
328 | { | |
329 | free (last_file); | |
330 | last_file = NULL; | |
331 | } | |
332 | if (last_function != NULL) | |
333 | { | |
334 | free (last_function); | |
335 | last_function = NULL; | |
336 | } | |
337 | } | |
338 | } | |
339 | break; | |
6d5e62f8 | 340 | |
252b5132 RH |
341 | case 's': |
342 | /* arbitrary string, like printf */ | |
343 | fprintf (fp, "%s", va_arg (arg, char *)); | |
344 | break; | |
345 | ||
346 | case 'd': | |
347 | /* integer, like printf */ | |
348 | fprintf (fp, "%d", va_arg (arg, int)); | |
349 | break; | |
350 | ||
351 | case 'u': | |
352 | /* unsigned integer, like printf */ | |
353 | fprintf (fp, "%u", va_arg (arg, unsigned int)); | |
354 | break; | |
355 | } | |
356 | } | |
357 | } | |
358 | ||
7ce691ae | 359 | if (config.fatal_warnings) |
b34976b6 | 360 | config.make_executable = FALSE; |
7ce691ae | 361 | |
b34976b6 | 362 | if (fatal) |
6d5e62f8 | 363 | xexit (1); |
252b5132 RH |
364 | } |
365 | ||
b7b482a3 AM |
366 | /* Wrapper around cplus_demangle. Strips leading underscores and |
367 | other such chars that would otherwise confuse the demangler. */ | |
368 | ||
369 | char * | |
1579bae1 | 370 | demangle (const char *name) |
b7b482a3 AM |
371 | { |
372 | char *res; | |
373 | const char *p; | |
374 | ||
375 | if (output_bfd != NULL | |
376 | && bfd_get_symbol_leading_char (output_bfd) == name[0]) | |
377 | ++name; | |
378 | ||
379 | /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF | |
380 | or the MS PE format. These formats have a number of leading '.'s | |
381 | on at least some symbols, so we remove all dots to avoid | |
382 | confusing the demangler. */ | |
383 | p = name; | |
384 | while (*p == '.') | |
385 | ++p; | |
386 | ||
387 | res = cplus_demangle (p, DMGL_ANSI | DMGL_PARAMS); | |
388 | if (res) | |
389 | { | |
390 | size_t dots = p - name; | |
391 | ||
392 | /* Now put back any stripped dots. */ | |
393 | if (dots != 0) | |
394 | { | |
395 | size_t len = strlen (res) + 1; | |
396 | char *add_dots = xmalloc (len + dots); | |
397 | ||
398 | memcpy (add_dots, name, dots); | |
399 | memcpy (add_dots + dots, res, len); | |
400 | free (res); | |
401 | res = add_dots; | |
402 | } | |
403 | return res; | |
404 | } | |
405 | return xstrdup (name); | |
406 | } | |
407 | ||
6d5e62f8 | 408 | /* Format info message and print on stdout. */ |
252b5132 RH |
409 | |
410 | /* (You would think this should be called just "info", but then you | |
1579bae1 | 411 | would be hosed by LynxOS, which defines that name in its libc.) */ |
252b5132 RH |
412 | |
413 | void | |
1579bae1 | 414 | info_msg (const char *fmt, ...) |
252b5132 | 415 | { |
1579bae1 | 416 | va_list arg; |
252b5132 | 417 | |
1579bae1 | 418 | va_start (arg, fmt); |
252b5132 | 419 | vfinfo (stdout, fmt, arg); |
1579bae1 | 420 | va_end (arg); |
252b5132 RH |
421 | } |
422 | ||
6d5e62f8 | 423 | /* ('e' for error.) Format info message and print on stderr. */ |
252b5132 RH |
424 | |
425 | void | |
1579bae1 | 426 | einfo (const char *fmt, ...) |
252b5132 | 427 | { |
1579bae1 | 428 | va_list arg; |
252b5132 | 429 | |
1579bae1 | 430 | va_start (arg, fmt); |
252b5132 | 431 | vfinfo (stderr, fmt, arg); |
1579bae1 | 432 | va_end (arg); |
252b5132 RH |
433 | } |
434 | ||
6d5e62f8 | 435 | void |
1579bae1 | 436 | info_assert (const char *file, unsigned int line) |
252b5132 RH |
437 | { |
438 | einfo (_("%F%P: internal error %s %d\n"), file, line); | |
439 | } | |
440 | ||
6d5e62f8 | 441 | /* ('m' for map) Format info message and print on map. */ |
252b5132 RH |
442 | |
443 | void | |
1579bae1 | 444 | minfo (const char *fmt, ...) |
252b5132 | 445 | { |
1579bae1 | 446 | va_list arg; |
252b5132 | 447 | |
1579bae1 | 448 | va_start (arg, fmt); |
252b5132 | 449 | vfinfo (config.map_file, fmt, arg); |
1579bae1 | 450 | va_end (arg); |
252b5132 RH |
451 | } |
452 | ||
453 | void | |
1579bae1 | 454 | lfinfo (FILE *file, const char *fmt, ...) |
252b5132 | 455 | { |
1579bae1 | 456 | va_list arg; |
252b5132 | 457 | |
1579bae1 | 458 | va_start (arg, fmt); |
252b5132 | 459 | vfinfo (file, fmt, arg); |
1579bae1 | 460 | va_end (arg); |
252b5132 RH |
461 | } |
462 | \f | |
463 | /* Functions to print the link map. */ | |
464 | ||
6d5e62f8 | 465 | void |
1579bae1 | 466 | print_space (void) |
252b5132 RH |
467 | { |
468 | fprintf (config.map_file, " "); | |
469 | } | |
470 | ||
6d5e62f8 | 471 | void |
1579bae1 | 472 | print_nl (void) |
252b5132 RH |
473 | { |
474 | fprintf (config.map_file, "\n"); | |
475 | } | |
45455cdd ILT |
476 | |
477 | /* A more or less friendly abort message. In ld.h abort is defined to | |
478 | call this function. */ | |
479 | ||
480 | void | |
1579bae1 | 481 | ld_abort (const char *file, int line, const char *fn) |
45455cdd ILT |
482 | { |
483 | if (fn != NULL) | |
484 | einfo (_("%P: internal error: aborting at %s line %d in %s\n"), | |
485 | file, line, fn); | |
486 | else | |
487 | einfo (_("%P: internal error: aborting at %s line %d\n"), | |
488 | file, line); | |
489 | einfo (_("%P%F: please report this bug\n")); | |
490 | xexit (1); | |
491 | } | |
0f0569c4 L |
492 | |
493 | bfd_boolean | |
1579bae1 | 494 | error_handler (int id, const char *fmt, ...) |
0f0569c4 | 495 | { |
1579bae1 | 496 | va_list arg; |
0f0569c4 | 497 | |
6f6f27f8 L |
498 | va_start (arg, fmt); |
499 | ||
500 | switch (id) | |
501 | { | |
502 | default: | |
503 | break; | |
504 | ||
505 | /* We can be called with | |
1579bae1 | 506 | |
6f6f27f8 | 507 | error_handler (-LD_DEFINITION_IN_DISCARDED_SECTION, "", 0); |
1579bae1 | 508 | |
6f6f27f8 L |
509 | to make this error non-fatal and |
510 | ||
511 | error_handler (-LD_DEFINITION_IN_DISCARDED_SECTION, "", 1); | |
512 | ||
513 | to make this error fatal. */ | |
514 | case -LD_DEFINITION_IN_DISCARDED_SECTION: | |
515 | case LD_DEFINITION_IN_DISCARDED_SECTION: | |
516 | { | |
517 | static struct bfd_hash_table *hash; | |
518 | static int fatal = 1; | |
519 | const char *name; | |
520 | ||
521 | if (id == -LD_DEFINITION_IN_DISCARDED_SECTION) | |
522 | { | |
523 | fatal = va_arg (arg, int); | |
524 | goto out; | |
525 | } | |
526 | ||
527 | name = va_arg (arg, const char *); | |
528 | /* Only warn once about a particular undefined symbol. */ | |
529 | if (hash == NULL) | |
530 | { | |
1579bae1 | 531 | hash = xmalloc (sizeof (struct bfd_hash_table)); |
6f6f27f8 L |
532 | if (! bfd_hash_table_init (hash, bfd_hash_newfunc)) |
533 | einfo (_("%F%P: bfd_hash_table_init failed: %E\n")); | |
534 | } | |
535 | ||
536 | if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL) | |
537 | goto out; | |
538 | ||
539 | if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL) | |
540 | einfo (_("%F%P: bfd_hash_lookup failed: %E\n")); | |
541 | ||
542 | if (fatal) | |
543 | config.make_executable = FALSE; | |
544 | } | |
545 | break; | |
546 | } | |
0f0569c4 | 547 | vfinfo (stderr, fmt, arg); |
1579bae1 | 548 | |
6f6f27f8 | 549 | out: |
1579bae1 | 550 | va_end (arg); |
0f0569c4 L |
551 | return TRUE; |
552 | } |