2002-02-17 Daniel Jacobowitz <drow@mvista.com>
[deliverable/binutils-gdb.git] / ld / ldmisc.c
1 /* ldmisc.c
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2002
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>
31 #else
32 #include <varargs.h>
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;
73 const char *p;
74
75 if (output_bfd != NULL
76 && bfd_get_symbol_leading_char (output_bfd) == string[0])
77 ++string;
78
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;
85
86 res = cplus_demangle (p, DMGL_ANSI | DMGL_PARAMS);
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 {
100 while (*fmt != '%' && *fmt != '\0')
101 {
102 putc (*fmt, fp);
103 fmt++;
104 }
105
106 if (*fmt == '%')
107 {
108 fmt++;
109 switch (*fmt++)
110 {
111 default:
112 fprintf (fp, "%%%c", fmt[-1]);
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 */
195 {
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':
206 /* Error is fatal. */
207 fatal = true;
208 break;
209
210 case 'P':
211 /* Print program name. */
212 fprintf (fp, "%s", program_name);
213 break;
214
215 case 'E':
216 /* current bfd error or errno */
217 fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
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':
237 /* Print script file and linenumber. */
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':
247 /* Print all that's interesting about a relent. */
248 {
249 arelent *relent = va_arg (arg, arelent *);
250
251 lfinfo (fp, "%s+0x%v (type %s)",
252 (*(relent->sym_ptr_ptr))->name,
253 relent->addend,
254 relent->howto->name);
255 }
256 break;
257
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);
339 last_file = xstrdup (filename);
340 if (last_function != NULL)
341 free (last_function);
342 last_function = xstrdup (functionname);
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 }
359 else if (linenumber != 0)
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;
384
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
403 if (config.fatal_warnings)
404 config.make_executable = false;
405
406 if (fatal == true)
407 xexit (1);
408 }
409
410 /* Format info message and print on stdout. */
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
416 info_msg VPARAMS ((const char *fmt, ...))
417 {
418 VA_OPEN (arg, fmt);
419 VA_FIXEDARG (arg, const char *, fmt);
420
421 vfinfo (stdout, fmt, arg);
422 VA_CLOSE (arg);
423 }
424
425 /* ('e' for error.) Format info message and print on stderr. */
426
427 void
428 einfo VPARAMS ((const char *fmt, ...))
429 {
430 VA_OPEN (arg, fmt);
431 VA_FIXEDARG (arg, const char *, fmt);
432
433 vfinfo (stderr, fmt, arg);
434 VA_CLOSE (arg);
435 }
436
437 void
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
445 /* ('m' for map) Format info message and print on map. */
446
447 void
448 minfo VPARAMS ((const char *fmt, ...))
449 {
450 VA_OPEN (arg, fmt);
451 VA_FIXEDARG (arg, const char *, fmt);
452
453 vfinfo (config.map_file, fmt, arg);
454 VA_CLOSE (arg);
455 }
456
457 void
458 lfinfo VPARAMS ((FILE *file, const char *fmt, ...))
459 {
460 VA_OPEN (arg, fmt);
461 VA_FIXEDARG (arg, FILE *, file);
462 VA_FIXEDARG (arg, const char *, fmt);
463
464 vfinfo (file, fmt, arg);
465 VA_CLOSE (arg);
466 }
467 \f
468 /* Functions to print the link map. */
469
470 void
471 print_space ()
472 {
473 fprintf (config.map_file, " ");
474 }
475
476 void
477 print_nl ()
478 {
479 fprintf (config.map_file, "\n");
480 }
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 }
This page took 0.038718 seconds and 4 git commands to generate.