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