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