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