Add i386-netbsd-gnu target.
[binutils.git] / ld / ldmisc.c
blob461d697274d5c9e9bb0f06f7cbb07523b16865dd
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.
7 This file is part of GLD, the Gnu Linker.
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.
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.
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. */
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "sysdep.h"
27 #include "libiberty.h"
28 #include "demangle.h"
30 #ifdef ANSI_PROTOTYPES
31 #include <stdarg.h>
32 #else
33 #include <varargs.h>
34 #endif
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"
45 static void vfinfo PARAMS ((FILE *, const char *, va_list));
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
69 static void
70 vfinfo (fp, fmt, arg)
71 FILE *fp;
72 const char *fmt;
73 va_list arg;
75 bfd_boolean fatal = FALSE;
77 while (*fmt != '\0')
79 while (*fmt != '%' && *fmt != '\0')
81 putc (*fmt, fp);
82 fmt++;
85 if (*fmt == '%')
87 fmt++;
88 switch (*fmt++)
90 default:
91 fprintf (fp, "%%%c", fmt[-1]);
92 break;
94 case '%':
95 /* literal % */
96 putc ('%', fp);
97 break;
99 case 'X':
100 /* no object output, fail return */
101 config.make_executable = FALSE;
102 break;
104 case 'V':
105 /* hex bfd_vma */
107 bfd_vma value = va_arg (arg, bfd_vma);
108 fprintf_vma (fp, value);
110 break;
112 case 'v':
113 /* hex bfd_vma, no leading zeros */
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);
125 break;
127 case 'W':
128 /* hex bfd_vma with 0x with no leading zeroes taking up
129 8 spaces. */
131 char buf[100];
132 bfd_vma value;
133 char *p;
134 int len;
136 value = va_arg (arg, bfd_vma);
137 sprintf_vma (buf, value);
138 for (p = buf; *p == '0'; ++p)
140 if (*p == '\0')
141 --p;
142 len = strlen (p);
143 while (len < 8)
145 putc (' ', fp);
146 ++len;
148 fprintf (fp, "0x%s", p);
150 break;
152 case 'T':
153 /* Symbol name. */
155 const char *name = va_arg (arg, const char *);
157 if (name == (const char *) NULL || *name == 0)
158 fprintf (fp, _("no symbol"));
159 else if (! demangling)
160 fprintf (fp, "%s", name);
161 else
163 char *demangled;
165 demangled = demangle (name);
166 fprintf (fp, "%s", demangled);
167 free (demangled);
170 break;
172 case 'B':
173 /* filename from a bfd */
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);
182 break;
184 case 'F':
185 /* Error is fatal. */
186 fatal = TRUE;
187 break;
189 case 'P':
190 /* Print program name. */
191 fprintf (fp, "%s", program_name);
192 break;
194 case 'E':
195 /* current bfd error or errno */
196 fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
197 break;
199 case 'I':
200 /* filename from a lang_input_statement_type */
202 lang_input_statement_type *i;
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);
213 break;
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;
225 case 'R':
226 /* Print all that's interesting about a relent. */
228 arelent *relent = va_arg (arg, arelent *);
230 lfinfo (fp, "%s+0x%v (type %s)",
231 (*(relent->sym_ptr_ptr))->name,
232 relent->addend,
233 relent->howto->name);
235 break;
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. */
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;
256 abfd = va_arg (arg, bfd *);
257 section = va_arg (arg, asection *);
258 offset = va_arg (arg, bfd_vma);
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
266 long symsize;
267 long symbol_count;
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)
278 entry->asymbols = asymbols;
279 entry->symbol_count = symbol_count;
283 lfinfo (fp, "%B(%s+0x%v)", abfd, section->name, offset);
285 discard_last = TRUE;
286 if (bfd_find_nearest_line (abfd, section, asymbols, offset,
287 &filename, &functionname,
288 &linenumber))
290 bfd_boolean need_colon = TRUE;
292 if (functionname != NULL && fmt[-1] == 'C')
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)
302 lfinfo (fp, _(": In function `%T':\n"),
303 functionname);
304 need_colon = FALSE;
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);
316 discard_last = FALSE;
319 if (filename != NULL)
321 if (need_colon)
322 putc (':', fp);
323 fputs (filename, fp);
326 if (functionname != NULL && fmt[-1] == 'G')
327 lfinfo (fp, ":%T", functionname);
328 else if (filename != NULL && linenumber != 0)
329 fprintf (fp, ":%u", linenumber);
332 if (asymbols != NULL && entry == NULL)
333 free (asymbols);
335 if (discard_last)
337 last_bfd = NULL;
338 if (last_file != NULL)
340 free (last_file);
341 last_file = NULL;
343 if (last_function != NULL)
345 free (last_function);
346 last_function = NULL;
350 break;
352 case 's':
353 /* arbitrary string, like printf */
354 fprintf (fp, "%s", va_arg (arg, char *));
355 break;
357 case 'd':
358 /* integer, like printf */
359 fprintf (fp, "%d", va_arg (arg, int));
360 break;
362 case 'u':
363 /* unsigned integer, like printf */
364 fprintf (fp, "%u", va_arg (arg, unsigned int));
365 break;
370 if (config.fatal_warnings)
371 config.make_executable = FALSE;
373 if (fatal)
374 xexit (1);
377 /* Wrapper around cplus_demangle. Strips leading underscores and
378 other such chars that would otherwise confuse the demangler. */
380 char *
381 demangle (name)
382 const char *name;
384 char *res;
385 const char *p;
387 if (output_bfd != NULL
388 && bfd_get_symbol_leading_char (output_bfd) == name[0])
389 ++name;
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;
399 res = cplus_demangle (p, DMGL_ANSI | DMGL_PARAMS);
400 if (res)
402 size_t dots = p - name;
404 /* Now put back any stripped dots. */
405 if (dots != 0)
407 size_t len = strlen (res) + 1;
408 char *add_dots = xmalloc (len + dots);
410 memcpy (add_dots, name, dots);
411 memcpy (add_dots + dots, res, len);
412 free (res);
413 res = add_dots;
415 return res;
417 return xstrdup (name);
420 /* Format info message and print on stdout. */
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.) */
425 void
426 info_msg VPARAMS ((const char *fmt, ...))
428 VA_OPEN (arg, fmt);
429 VA_FIXEDARG (arg, const char *, fmt);
431 vfinfo (stdout, fmt, arg);
432 VA_CLOSE (arg);
435 /* ('e' for error.) Format info message and print on stderr. */
437 void
438 einfo VPARAMS ((const char *fmt, ...))
440 VA_OPEN (arg, fmt);
441 VA_FIXEDARG (arg, const char *, fmt);
443 vfinfo (stderr, fmt, arg);
444 VA_CLOSE (arg);
447 void
448 info_assert (file, line)
449 const char *file;
450 unsigned int line;
452 einfo (_("%F%P: internal error %s %d\n"), file, line);
455 /* ('m' for map) Format info message and print on map. */
457 void
458 minfo VPARAMS ((const char *fmt, ...))
460 VA_OPEN (arg, fmt);
461 VA_FIXEDARG (arg, const char *, fmt);
463 vfinfo (config.map_file, fmt, arg);
464 VA_CLOSE (arg);
467 void
468 lfinfo VPARAMS ((FILE *file, const char *fmt, ...))
470 VA_OPEN (arg, fmt);
471 VA_FIXEDARG (arg, FILE *, file);
472 VA_FIXEDARG (arg, const char *, fmt);
474 vfinfo (file, fmt, arg);
475 VA_CLOSE (arg);
478 /* Functions to print the link map. */
480 void
481 print_space ()
483 fprintf (config.map_file, " ");
486 void
487 print_nl ()
489 fprintf (config.map_file, "\n");
492 /* A more or less friendly abort message. In ld.h abort is defined to
493 call this function. */
495 void
496 ld_abort (file, line, fn)
497 const char *file;
498 int line;
499 const char *fn;
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);
511 bfd_boolean
512 error_handler VPARAMS ((int id, const char *fmt, ...))
514 VA_OPEN (arg, fmt);
515 VA_FIXEDARG (arg, const char *, fmt);
517 va_start (arg, fmt);
519 switch (id)
521 default:
522 break;
524 /* We can be called with
526 error_handler (-LD_DEFINITION_IN_DISCARDED_SECTION, "", 0);
528 to make this error non-fatal and
530 error_handler (-LD_DEFINITION_IN_DISCARDED_SECTION, "", 1);
532 to make this error fatal. */
533 case -LD_DEFINITION_IN_DISCARDED_SECTION:
534 case LD_DEFINITION_IN_DISCARDED_SECTION:
536 static struct bfd_hash_table *hash;
537 static int fatal = 1;
538 const char *name;
540 if (id == -LD_DEFINITION_IN_DISCARDED_SECTION)
542 fatal = va_arg (arg, int);
543 goto out;
546 name = va_arg (arg, const char *);
547 /* Only warn once about a particular undefined symbol. */
548 if (hash == NULL)
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"));
556 if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL)
557 goto out;
559 if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL)
560 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
562 if (fatal)
563 config.make_executable = FALSE;
565 break;
567 vfinfo (stderr, fmt, arg);
568 out:
569 VA_CLOSE (arg);
570 return TRUE;