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