2 Copyright (C) 1991-2016 Free Software Foundation, Inc.
3 Written by Steve Chamberlain of Cygnus Support.
5 This file is part of the GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
25 #include "libiberty.h"
26 #include "filenames.h"
42 %A section name from a section
43 %B filename from a bfd
44 %C clever filename:linenumber with function
45 %D like %C, but no function name
46 %E current bfd error or errno
48 %G like %D, but only function name
49 %H like %C but in addition emit section+offset
50 %I filename from a lang_input_statement_type
52 %R info about a relent
53 %S print script file and linenumber from etree_type.
56 %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces
57 %X no object output, fail return
58 %d integer, like printf
60 %lu unsigned long, like printf
61 %p native (host) void* pointer, like printf
62 %s arbitrary string, like printf
63 %u integer, like printf
64 %v hex bfd_vma, no leading zeros
68 vfinfo (FILE *fp
, const char *fmt
, va_list arg
, bfd_boolean is_warning
)
70 bfd_boolean fatal
= FALSE
;
74 const char *str
= fmt
;
75 while (*fmt
!= '%' && *fmt
!= '\0')
78 if (fwrite (str
, 1, fmt
- str
, fp
))
94 /* no object output, fail return */
95 config
.make_executable
= FALSE
;
101 bfd_vma value
= va_arg (arg
, bfd_vma
);
102 fprintf_vma (fp
, value
);
107 /* hex bfd_vma, no leading zeros */
111 bfd_vma value
= va_arg (arg
, bfd_vma
);
112 sprintf_vma (p
, value
);
122 /* hex bfd_vma with 0x with no leading zeroes taking up
130 value
= va_arg (arg
, bfd_vma
);
131 sprintf_vma (buf
, value
);
132 for (p
= buf
; *p
== '0'; ++p
)
142 fprintf (fp
, "0x%s", p
);
149 const char *name
= va_arg (arg
, const char *);
151 if (name
== NULL
|| *name
== 0)
153 fprintf (fp
, _("no symbol"));
160 demangled
= bfd_demangle (link_info
.output_bfd
, name
,
161 DMGL_ANSI
| DMGL_PARAMS
);
162 if (demangled
!= NULL
)
164 fprintf (fp
, "%s", demangled
);
169 fprintf (fp
, "%s", name
);
174 /* section name from a section */
176 asection
*sec
= va_arg (arg
, asection
*);
177 bfd
*abfd
= sec
->owner
;
178 const char *group
= NULL
;
179 struct coff_comdat_info
*ci
;
181 fprintf (fp
, "%s", sec
->name
);
183 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
184 && elf_next_in_group (sec
) != NULL
185 && (sec
->flags
& SEC_GROUP
) == 0)
186 group
= elf_group_name (sec
);
187 else if (abfd
!= NULL
188 && bfd_get_flavour (abfd
) == bfd_target_coff_flavour
189 && (ci
= bfd_coff_get_comdat_section (sec
->owner
,
193 fprintf (fp
, "[%s]", group
);
198 /* filename from a bfd */
200 bfd
*abfd
= va_arg (arg
, bfd
*);
203 fprintf (fp
, "%s generated", program_name
);
204 else if (abfd
->my_archive
!= NULL
205 && !bfd_is_thin_archive (abfd
->my_archive
))
206 fprintf (fp
, "%s(%s)", abfd
->my_archive
->filename
,
209 fprintf (fp
, "%s", abfd
->filename
);
214 /* Error is fatal. */
219 /* Print program name. */
220 fprintf (fp
, "%s", program_name
);
224 /* current bfd error or errno */
225 fprintf (fp
, "%s", bfd_errmsg (bfd_get_error ()));
229 /* filename from a lang_input_statement_type */
231 lang_input_statement_type
*i
;
233 i
= va_arg (arg
, lang_input_statement_type
*);
234 if (i
->the_bfd
->my_archive
!= NULL
235 && !bfd_is_thin_archive (i
->the_bfd
->my_archive
))
237 bfd_get_filename (i
->the_bfd
->my_archive
));
238 fprintf (fp
, "%s", i
->local_sym_name
);
239 if ((i
->the_bfd
->my_archive
== NULL
240 || bfd_is_thin_archive (i
->the_bfd
->my_archive
))
241 && filename_cmp (i
->local_sym_name
, i
->filename
) != 0)
242 fprintf (fp
, " (%s)", i
->filename
);
247 /* Print script file and linenumber. */
250 etree_type
*tp
= va_arg (arg
, etree_type
*);
255 tp
->type
.filename
= ldlex_filename ();
256 tp
->type
.lineno
= lineno
;
258 if (tp
->type
.filename
!= NULL
)
259 fprintf (fp
, "%s:%u", tp
->type
.filename
, tp
->type
.lineno
);
264 /* Print all that's interesting about a relent. */
266 arelent
*relent
= va_arg (arg
, arelent
*);
268 lfinfo (fp
, "%s+0x%v (type %s)",
269 (*(relent
->sym_ptr_ptr
))->name
,
271 relent
->howto
->name
);
279 /* Clever filename:linenumber with function name if possible.
280 The arguments are a BFD, a section, and an offset. */
282 static bfd
*last_bfd
;
283 static char *last_file
= NULL
;
284 static char *last_function
= NULL
;
288 asymbol
**asymbols
= NULL
;
289 const char *filename
;
290 const char *functionname
;
291 unsigned int linenumber
;
292 bfd_boolean discard_last
;
295 abfd
= va_arg (arg
, bfd
*);
296 section
= va_arg (arg
, asection
*);
297 offset
= va_arg (arg
, bfd_vma
);
301 if (!bfd_generic_link_read_symbols (abfd
))
302 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
304 asymbols
= bfd_get_outsymbols (abfd
);
307 /* The GNU Coding Standard requires that error messages
310 source-file-name:lineno: message
312 We do not always have a line number available so if
313 we cannot find them we print out the section name and
317 && bfd_find_nearest_line (abfd
, section
, asymbols
, offset
,
318 &filename
, &functionname
,
321 if (functionname
!= NULL
322 && (fmt
[-1] == 'C' || fmt
[-1] == 'H'))
324 /* Detect the case where we are printing out a
325 message for the same function as the last
326 call to vinfo ("%C"). In this situation do
327 not print out the ABFD filename or the
328 function name again. Note - we do still
329 print out the source filename, as this will
330 allow programs that parse the linker's output
331 (eg emacs) to correctly locate multiple
332 errors in the same source file. */
335 || last_function
== NULL
338 && filename_cmp (last_file
, filename
) != 0)
339 || strcmp (last_function
, functionname
) != 0)
341 lfinfo (fp
, _("%B: In function `%T':\n"),
345 if (last_file
!= NULL
)
349 last_file
= xstrdup (filename
);
350 if (last_function
!= NULL
)
351 free (last_function
);
352 last_function
= xstrdup (functionname
);
354 discard_last
= FALSE
;
357 lfinfo (fp
, "%B:", abfd
);
359 if (filename
!= NULL
)
360 fprintf (fp
, "%s:", filename
);
362 done
= fmt
[-1] != 'H';
363 if (functionname
!= NULL
&& fmt
[-1] == 'G')
364 lfinfo (fp
, "%T", functionname
);
365 else if (filename
!= NULL
&& linenumber
!= 0)
366 fprintf (fp
, "%u%s", linenumber
, done
? "" : ":");
372 lfinfo (fp
, "%B:", abfd
);
376 lfinfo (fp
, "(%A+0x%v)", section
, offset
);
381 if (last_file
!= NULL
)
386 if (last_function
!= NULL
)
388 free (last_function
);
389 last_function
= NULL
;
396 /* native (host) void* pointer, like printf */
397 fprintf (fp
, "%p", va_arg (arg
, void *));
401 /* arbitrary string, like printf */
402 fprintf (fp
, "%s", va_arg (arg
, char *));
406 /* integer, like printf */
407 fprintf (fp
, "%d", va_arg (arg
, int));
411 /* unsigned integer, like printf */
412 fprintf (fp
, "%u", va_arg (arg
, unsigned int));
418 fprintf (fp
, "%ld", va_arg (arg
, long));
422 else if (*fmt
== 'u')
424 fprintf (fp
, "%lu", va_arg (arg
, unsigned long));
431 fprintf (fp
, "%%%c", fmt
[-1]);
437 if (is_warning
&& config
.fatal_warnings
)
438 config
.make_executable
= FALSE
;
444 /* Format info message and print on stdout. */
446 /* (You would think this should be called just "info", but then you
447 would be hosed by LynxOS, which defines that name in its libc.) */
450 info_msg (const char *fmt
, ...)
455 vfinfo (stdout
, fmt
, arg
, FALSE
);
459 /* ('e' for error.) Format info message and print on stderr. */
462 einfo (const char *fmt
, ...)
468 vfinfo (stderr
, fmt
, arg
, TRUE
);
474 info_assert (const char *file
, unsigned int line
)
476 einfo (_("%F%P: internal error %s %d\n"), file
, line
);
479 /* ('m' for map) Format info message and print on map. */
482 minfo (const char *fmt
, ...)
484 if (config
.map_file
!= NULL
)
489 if (fmt
[0] == '%' && fmt
[1] == '!' && fmt
[2] == 0)
491 /* Stash info about --as-needed shared libraries. Print
492 later so they don't appear intermingled with archive
494 struct asneeded_minfo
*m
= xmalloc (sizeof *m
);
497 m
->soname
= va_arg (arg
, const char *);
498 m
->ref
= va_arg (arg
, bfd
*);
499 m
->name
= va_arg (arg
, const char *);
500 *asneeded_list_tail
= m
;
501 asneeded_list_tail
= &m
->next
;
504 vfinfo (config
.map_file
, fmt
, arg
, FALSE
);
510 lfinfo (FILE *file
, const char *fmt
, ...)
515 vfinfo (file
, fmt
, arg
, FALSE
);
519 /* Functions to print the link map. */
524 fprintf (config
.map_file
, " ");
530 fprintf (config
.map_file
, "\n");
533 /* A more or less friendly abort message. In ld.h abort is defined to
534 call this function. */
537 ld_abort (const char *file
, int line
, const char *fn
)
540 einfo (_("%P: internal error: aborting at %s:%d in %s\n"),
543 einfo (_("%P: internal error: aborting at %s:%d\n"),
545 einfo (_("%P%F: please report this bug\n"));