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