2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
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)
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
27 #include "libiberty.h"
30 #ifdef ANSI_PROTOTYPES
45 static void vfinfo PARAMS ((FILE *, const char *, va_list));
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
56 %X no object output, fail return
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
75 bfd_boolean fatal = FALSE;
79 while (*fmt != '%' && *fmt != '\0')
91 fprintf (fp, "%%%c", fmt[-1]);
100 /* no object output, fail return */
101 config.make_executable = FALSE;
107 bfd_vma value = va_arg (arg, bfd_vma);
108 fprintf_vma (fp, value);
113 /* hex bfd_vma, no leading zeros */
117 bfd_vma value = va_arg (arg, bfd_vma);
118 sprintf_vma (p, value);
128 /* hex bfd_vma with 0x with no leading zeroes taking up
136 value = va_arg (arg, bfd_vma);
137 sprintf_vma (buf, value);
138 for (p = buf; *p == '0'; ++p)
148 fprintf (fp, "0x%s", p);
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);
165 demangled = demangle (name);
166 fprintf (fp, "%s", demangled);
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,
180 fprintf (fp, "%s", abfd->filename);
185 /* Error is fatal. */
190 /* Print program name. */
191 fprintf (fp, "%s", program_name);
195 /* current bfd error or errno */
196 fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
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)
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);
216 /* Print script file and linenumber. */
218 fprintf (fp, "--defsym %s", lex_string);
219 else if (ldfile_input_filename != NULL)
220 fprintf (fp, "%s:%u", ldfile_input_filename, lineno);
222 fprintf (fp, _("built in linker script:%u"), lineno);
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,
233 relent->howto->name);
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;
249 lang_input_statement_type *entry;
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;
269 symsize = bfd_get_symtab_upper_bound (abfd);
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);
286 if (bfd_find_nearest_line (abfd, section, asymbols, offset,
287 &filename, &functionname,
290 bfd_boolean need_colon = TRUE;
292 if (functionname != NULL && fmt[-1] == 'C')
296 || last_function == NULL
299 && strcmp (last_file, filename) != 0)
300 || strcmp (last_function, functionname) != 0)
302 lfinfo (fp, _(": In function `%T':\n"),
307 if (last_file != NULL)
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)
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)
338 if (last_file != NULL)
343 if (last_function != NULL)
345 free (last_function);
346 last_function = NULL;
353 /* arbitrary string, like printf */
354 fprintf (fp, "%s", va_arg (arg, char *));
358 /* integer, like printf */
359 fprintf (fp, "%d", va_arg (arg, int));
363 /* unsigned integer, like printf */
364 fprintf (fp, "%u", va_arg (arg, unsigned int));
370 if (config.fatal_warnings)
371 config.make_executable = FALSE;
377 /* Wrapper around cplus_demangle. Strips leading underscores and
378 other such chars that would otherwise confuse the demangler. */
387 if (output_bfd != NULL
388 && bfd_get_symbol_leading_char (output_bfd) == name[0])
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. */
399 res = cplus_demangle (p, DMGL_ANSI | DMGL_PARAMS);
402 size_t dots = p - name;
404 /* Now put back any stripped dots. */
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);
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.) */
426 info_msg VPARAMS ((const char *fmt, ...))
429 VA_FIXEDARG (arg, const char *, fmt);
431 vfinfo (stdout, fmt, arg);
435 /* ('e' for error.) Format info message and print on stderr. */
438 einfo VPARAMS ((const char *fmt, ...))
441 VA_FIXEDARG (arg, const char *, fmt);
443 vfinfo (stderr, fmt, arg);
448 info_assert (file, line)
452 einfo (_("%F%P: internal error %s %d\n"), file, line);
455 /* ('m' for map) Format info message and print on map. */
458 minfo VPARAMS ((const char *fmt, ...))
461 VA_FIXEDARG (arg, const char *, fmt);
463 vfinfo (config.map_file, fmt, arg);
468 lfinfo VPARAMS ((FILE *file, const char *fmt, ...))
471 VA_FIXEDARG (arg, FILE *, file);
472 VA_FIXEDARG (arg, const char *, fmt);
474 vfinfo (file, fmt, arg);
478 /* Functions to print the link map. */
483 fprintf (config.map_file, " ");
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. */
496 ld_abort (file, line, fn)
502 einfo (_("%P: internal error: aborting at %s line %d in %s\n"),
505 einfo (_("%P: internal error: aborting at %s line %d\n"),
507 einfo (_("%P%F: please report this bug\n"));
512 error_handler VPARAMS ((int id, const char *fmt, ...))
515 VA_FIXEDARG (arg, const char *, fmt);
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;
540 if (id == -LD_DEFINITION_IN_DISCARDED_SECTION)
542 fatal = va_arg (arg, int);
546 name = va_arg (arg, const char *);
547 /* Only warn once about a particular undefined symbol. */
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)
559 if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL)
560 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
563 config.make_executable = FALSE;
567 vfinfo (stderr, fmt, arg);