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"
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
48 %X no object output, fail return
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
62 vfinfo (FILE *fp
, const char *fmt
, va_list arg
)
64 bfd_boolean fatal
= FALSE
;
68 while (*fmt
!= '%' && *fmt
!= '\0')
80 fprintf (fp
, "%%%c", fmt
[-1]);
89 /* no object output, fail return */
90 config
.make_executable
= FALSE
;
96 bfd_vma value
= va_arg (arg
, bfd_vma
);
97 fprintf_vma (fp
, value
);
102 /* hex bfd_vma, no leading zeros */
106 bfd_vma value
= va_arg (arg
, bfd_vma
);
107 sprintf_vma (p
, value
);
117 /* hex bfd_vma with 0x with no leading zeroes taking up
125 value
= va_arg (arg
, bfd_vma
);
126 sprintf_vma (buf
, value
);
127 for (p
= buf
; *p
== '0'; ++p
)
137 fprintf (fp
, "0x%s", p
);
144 const char *name
= va_arg (arg
, const char *);
146 if (name
== NULL
|| *name
== 0)
147 fprintf (fp
, _("no symbol"));
148 else if (! demangling
)
149 fprintf (fp
, "%s", name
);
154 demangled
= demangle (name
);
155 fprintf (fp
, "%s", demangled
);
162 /* filename from a bfd */
164 bfd
*abfd
= va_arg (arg
, bfd
*);
165 if (abfd
->my_archive
)
166 fprintf (fp
, "%s(%s)", abfd
->my_archive
->filename
,
169 fprintf (fp
, "%s", abfd
->filename
);
174 /* Error is fatal. */
179 /* Print program name. */
180 fprintf (fp
, "%s", program_name
);
184 /* current bfd error or errno */
185 fprintf (fp
, "%s", bfd_errmsg (bfd_get_error ()));
189 /* filename from a lang_input_statement_type */
191 lang_input_statement_type
*i
;
193 i
= va_arg (arg
, lang_input_statement_type
*);
194 if (bfd_my_archive (i
->the_bfd
) != NULL
)
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
);
205 /* Print script file and linenumber. */
207 fprintf (fp
, "--defsym %s", lex_string
);
208 else if (ldfile_input_filename
!= NULL
)
209 fprintf (fp
, "%s:%u", ldfile_input_filename
, lineno
);
211 fprintf (fp
, _("built in linker script:%u"), lineno
);
215 /* Print all that's interesting about a relent. */
217 arelent
*relent
= va_arg (arg
, arelent
*);
219 lfinfo (fp
, "%s+0x%v (type %s)",
220 (*(relent
->sym_ptr_ptr
))->name
,
222 relent
->howto
->name
);
229 /* Clever filename:linenumber with function name if possible.
230 The arguments are a BFD, a section, and an offset. */
232 static bfd
*last_bfd
;
233 static char *last_file
= NULL
;
234 static char *last_function
= NULL
;
238 lang_input_statement_type
*entry
;
240 const char *filename
;
241 const char *functionname
;
242 unsigned int linenumber
;
243 bfd_boolean discard_last
;
245 abfd
= va_arg (arg
, bfd
*);
246 section
= va_arg (arg
, asection
*);
247 offset
= va_arg (arg
, bfd_vma
);
249 entry
= (lang_input_statement_type
*) abfd
->usrdata
;
250 if (entry
!= (lang_input_statement_type
*) NULL
251 && entry
->asymbols
!= (asymbol
**) NULL
)
252 asymbols
= entry
->asymbols
;
258 symsize
= bfd_get_symtab_upper_bound (abfd
);
260 einfo (_("%B%F: could not read symbols\n"), abfd
);
261 asymbols
= xmalloc (symsize
);
262 symbol_count
= bfd_canonicalize_symtab (abfd
, asymbols
);
263 if (symbol_count
< 0)
264 einfo (_("%B%F: could not read symbols\n"), abfd
);
265 if (entry
!= (lang_input_statement_type
*) NULL
)
267 entry
->asymbols
= asymbols
;
268 entry
->symbol_count
= symbol_count
;
272 lfinfo (fp
, "%B(%s+0x%v)", abfd
, section
->name
, offset
);
275 if (bfd_find_nearest_line (abfd
, section
, asymbols
, offset
,
276 &filename
, &functionname
,
279 bfd_boolean need_colon
= TRUE
;
281 if (functionname
!= NULL
&& fmt
[-1] == 'C')
285 || last_function
== NULL
288 && strcmp (last_file
, filename
) != 0)
289 || strcmp (last_function
, functionname
) != 0)
291 lfinfo (fp
, _(": In function `%T':\n"),
296 if (last_file
!= NULL
)
300 last_file
= xstrdup (filename
);
301 if (last_function
!= NULL
)
302 free (last_function
);
303 last_function
= xstrdup (functionname
);
305 discard_last
= FALSE
;
308 if (filename
!= NULL
)
312 fputs (filename
, fp
);
315 if (functionname
!= NULL
&& fmt
[-1] == 'G')
316 lfinfo (fp
, ":%T", functionname
);
317 else if (filename
!= NULL
&& linenumber
!= 0)
318 fprintf (fp
, ":%u", linenumber
);
321 if (asymbols
!= NULL
&& entry
== NULL
)
327 if (last_file
!= NULL
)
332 if (last_function
!= NULL
)
334 free (last_function
);
335 last_function
= NULL
;
342 /* arbitrary string, like printf */
343 fprintf (fp
, "%s", va_arg (arg
, char *));
347 /* integer, like printf */
348 fprintf (fp
, "%d", va_arg (arg
, int));
352 /* unsigned integer, like printf */
353 fprintf (fp
, "%u", va_arg (arg
, unsigned int));
359 if (config
.fatal_warnings
)
360 config
.make_executable
= FALSE
;
366 /* Wrapper around cplus_demangle. Strips leading underscores and
367 other such chars that would otherwise confuse the demangler. */
370 demangle (const char *name
)
375 if (output_bfd
!= NULL
376 && bfd_get_symbol_leading_char (output_bfd
) == name
[0])
379 /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
380 or the MS PE format. These formats have a number of leading '.'s
381 on at least some symbols, so we remove all dots to avoid
382 confusing the demangler. */
387 res
= cplus_demangle (p
, DMGL_ANSI
| DMGL_PARAMS
);
390 size_t dots
= p
- name
;
392 /* Now put back any stripped dots. */
395 size_t len
= strlen (res
) + 1;
396 char *add_dots
= xmalloc (len
+ dots
);
398 memcpy (add_dots
, name
, dots
);
399 memcpy (add_dots
+ dots
, res
, len
);
405 return xstrdup (name
);
408 /* Format info message and print on stdout. */
410 /* (You would think this should be called just "info", but then you
411 would be hosed by LynxOS, which defines that name in its libc.) */
414 info_msg (const char *fmt
, ...)
419 vfinfo (stdout
, fmt
, arg
);
423 /* ('e' for error.) Format info message and print on stderr. */
426 einfo (const char *fmt
, ...)
431 vfinfo (stderr
, fmt
, arg
);
436 info_assert (const char *file
, unsigned int line
)
438 einfo (_("%F%P: internal error %s %d\n"), file
, line
);
441 /* ('m' for map) Format info message and print on map. */
444 minfo (const char *fmt
, ...)
449 vfinfo (config
.map_file
, fmt
, arg
);
454 lfinfo (FILE *file
, const char *fmt
, ...)
459 vfinfo (file
, fmt
, arg
);
463 /* Functions to print the link map. */
468 fprintf (config
.map_file
, " ");
474 fprintf (config
.map_file
, "\n");
477 /* A more or less friendly abort message. In ld.h abort is defined to
478 call this function. */
481 ld_abort (const char *file
, int line
, const char *fn
)
484 einfo (_("%P: internal error: aborting at %s line %d in %s\n"),
487 einfo (_("%P: internal error: aborting at %s line %d\n"),
489 einfo (_("%P%F: please report this bug\n"));
494 error_handler (int id
, const char *fmt
, ...)
505 /* We can be called with
507 error_handler (-LD_DEFINITION_IN_DISCARDED_SECTION, "", 0);
509 to make this error non-fatal and
511 error_handler (-LD_DEFINITION_IN_DISCARDED_SECTION, "", 1);
513 to make this error fatal. */
514 case -LD_DEFINITION_IN_DISCARDED_SECTION
:
515 case LD_DEFINITION_IN_DISCARDED_SECTION
:
517 static struct bfd_hash_table
*hash
;
518 static int fatal
= 1;
521 if (id
== -LD_DEFINITION_IN_DISCARDED_SECTION
)
523 fatal
= va_arg (arg
, int);
527 name
= va_arg (arg
, const char *);
528 /* Only warn once about a particular undefined symbol. */
531 hash
= xmalloc (sizeof (struct bfd_hash_table
));
532 if (! bfd_hash_table_init (hash
, bfd_hash_newfunc
))
533 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
536 if (bfd_hash_lookup (hash
, name
, FALSE
, FALSE
) != NULL
)
539 if (bfd_hash_lookup (hash
, name
, TRUE
, TRUE
) == NULL
)
540 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
543 config
.make_executable
= FALSE
;
547 vfinfo (stderr
, fmt
, arg
);