2 Copyright (C) 1991-2019 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 "safe-ctype.h"
27 #include "filenames.h"
41 %C clever filename:linenumber with function
42 %D like %C, but no function name
43 %E current bfd error or errno
45 %G like %D, but only function name
46 %H like %C but in addition emit section+offset
49 %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces
50 %X no object output, fail return
51 %d integer, like printf
53 %lu unsigned long, like printf
54 %p native (host) void* pointer, like printf
55 %pA section name from a section
56 %pB filename from a bfd
57 %pI filename from a lang_input_statement_type
58 %pR info about a relent
59 %pS print script file and linenumber from etree_type.
61 %s arbitrary string, like printf
62 %u integer, like printf
63 %v hex bfd_vma, no leading zeros
67 vfinfo (FILE *fp
, const char *fmt
, va_list ap
, bfd_boolean is_warning
)
69 bfd_boolean fatal
= FALSE
;
72 unsigned int arg_count
= 0;
96 for (arg_no
= 0; arg_no
< sizeof (args
) / sizeof (args
[0]); arg_no
++)
97 args
[arg_no
].type
= Bad
;
101 while (*scan
!= '\0')
103 while (*scan
!= '%' && *scan
!= '\0')
111 if (*scan
!= '0' && ISDIGIT (*scan
) && scan
[1] == '$')
113 arg_no
= *scan
- '1';
135 if (*scan
== 'A' || *scan
== 'B' || *scan
== 'I'
136 || *scan
== 'R' || *scan
== 'S' || *scan
== 'T')
154 if (*scan
== 'd' || *scan
== 'u')
166 if (arg_no
>= sizeof (args
) / sizeof (args
[0]))
168 args
[arg_no
].type
= arg_type
;
174 for (arg_no
= 0; arg_no
< arg_count
; arg_no
++)
176 switch (args
[arg_no
].type
)
179 args
[arg_no
].i
= va_arg (ap
, int);
182 args
[arg_no
].l
= va_arg (ap
, long);
185 args
[arg_no
].p
= va_arg (ap
, void *);
188 args
[arg_no
].v
= va_arg (ap
, bfd_vma
);
191 args
[arg_no
].reladdr
.abfd
= va_arg (ap
, bfd
*);
192 args
[arg_no
].reladdr
.sec
= va_arg (ap
, asection
*);
193 args
[arg_no
].reladdr
.off
= va_arg (ap
, bfd_vma
);
203 const char *str
= fmt
;
204 while (*fmt
!= '%' && *fmt
!= '\0')
207 if (fwrite (str
, 1, fmt
- str
, fp
))
217 if (*fmt
!= '0' && ISDIGIT (*fmt
) && fmt
[1] == '$')
235 /* no object output, fail return */
236 config
.make_executable
= FALSE
;
242 bfd_vma value
= args
[arg_no
].v
;
244 fprintf_vma (fp
, value
);
249 /* hex bfd_vma, no leading zeros */
253 bfd_vma value
= args
[arg_no
].v
;
255 sprintf_vma (p
, value
);
265 /* hex bfd_vma with 0x with no leading zeroes taking up
273 value
= args
[arg_no
].v
;
275 sprintf_vma (buf
, value
);
276 for (p
= buf
; *p
== '0'; ++p
)
286 fprintf (fp
, "0x%s", p
);
291 /* Error is fatal. */
296 /* Print program name. */
297 fprintf (fp
, "%s", program_name
);
301 /* current bfd error or errno */
302 fprintf (fp
, "%s", bfd_errmsg (bfd_get_error ()));
309 /* Clever filename:linenumber with function name if possible.
310 The arguments are a BFD, a section, and an offset. */
312 static bfd
*last_bfd
;
313 static char *last_file
;
314 static char *last_function
;
318 asymbol
**asymbols
= NULL
;
319 const char *filename
;
320 const char *functionname
;
321 unsigned int linenumber
;
322 bfd_boolean discard_last
;
325 abfd
= args
[arg_no
].reladdr
.abfd
;
326 section
= args
[arg_no
].reladdr
.sec
;
327 offset
= args
[arg_no
].reladdr
.off
;
332 if (!bfd_generic_link_read_symbols (abfd
))
333 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd
);
335 asymbols
= bfd_get_outsymbols (abfd
);
338 /* The GNU Coding Standard requires that error messages
341 source-file-name:lineno: message
343 We do not always have a line number available so if
344 we cannot find them we print out the section name and
348 && bfd_find_nearest_line (abfd
, section
, asymbols
, offset
,
349 &filename
, &functionname
,
352 if (functionname
!= NULL
353 && (fmt
[-1] == 'C' || fmt
[-1] == 'H'))
355 /* Detect the case where we are printing out a
356 message for the same function as the last
357 call to vinfo ("%C"). In this situation do
358 not print out the ABFD filename or the
359 function name again. Note - we do still
360 print out the source filename, as this will
361 allow programs that parse the linker's output
362 (eg emacs) to correctly locate multiple
363 errors in the same source file. */
365 || last_function
== NULL
367 || (last_file
== NULL
) != (filename
== NULL
)
369 && filename_cmp (last_file
, filename
) != 0)
370 || strcmp (last_function
, functionname
) != 0)
372 lfinfo (fp
, _("%pB: in function `%pT':\n"),
376 if (last_file
!= NULL
)
380 last_file
= xstrdup (filename
);
381 if (last_function
!= NULL
)
382 free (last_function
);
383 last_function
= xstrdup (functionname
);
385 discard_last
= FALSE
;
388 lfinfo (fp
, "%pB:", abfd
);
390 if (filename
!= NULL
)
391 fprintf (fp
, "%s:", filename
);
393 done
= fmt
[-1] != 'H';
394 if (functionname
!= NULL
&& fmt
[-1] == 'G')
395 lfinfo (fp
, "%pT", functionname
);
396 else if (filename
!= NULL
&& linenumber
!= 0)
397 fprintf (fp
, "%u%s", linenumber
, done
? "" : ":");
403 lfinfo (fp
, "%pB:", abfd
);
407 lfinfo (fp
, "(%pA+0x%v)", section
, offset
);
412 if (last_file
!= NULL
)
417 if (last_function
!= NULL
)
419 free (last_function
);
420 last_function
= NULL
;
429 /* section name from a section */
434 sec
= (asection
*) args
[arg_no
].p
;
436 fprintf (fp
, "%s", sec
->name
);
440 const char *group
= bfd_group_name (abfd
, sec
);
442 fprintf (fp
, "[%s]", group
);
445 else if (*fmt
== 'B')
447 /* filename from a bfd */
448 bfd
*abfd
= (bfd
*) args
[arg_no
].p
;
453 fprintf (fp
, "%s generated", program_name
);
454 else if (abfd
->my_archive
!= NULL
455 && !bfd_is_thin_archive (abfd
->my_archive
))
456 fprintf (fp
, "%s(%s)", abfd
->my_archive
->filename
,
459 fprintf (fp
, "%s", abfd
->filename
);
461 else if (*fmt
== 'I')
463 /* filename from a lang_input_statement_type */
464 lang_input_statement_type
*i
;
467 i
= (lang_input_statement_type
*) args
[arg_no
].p
;
469 if (i
->the_bfd
!= NULL
470 && i
->the_bfd
->my_archive
!= NULL
471 && !bfd_is_thin_archive (i
->the_bfd
->my_archive
))
472 fprintf (fp
, "(%s)%s", i
->the_bfd
->my_archive
->filename
,
475 fprintf (fp
, "%s", i
->filename
);
477 else if (*fmt
== 'R')
479 /* Print all that's interesting about a relent. */
480 arelent
*relent
= (arelent
*) args
[arg_no
].p
;
484 lfinfo (fp
, "%s+0x%v (type %s)",
485 (*(relent
->sym_ptr_ptr
))->name
,
487 relent
->howto
->name
);
489 else if (*fmt
== 'S')
491 /* Print script file and linenumber. */
493 etree_type
*tp
= (etree_type
*) args
[arg_no
].p
;
500 tp
->type
.filename
= ldlex_filename ();
501 tp
->type
.lineno
= lineno
;
503 if (tp
->type
.filename
!= NULL
)
504 fprintf (fp
, "%s:%u", tp
->type
.filename
, tp
->type
.lineno
);
506 else if (*fmt
== 'T')
509 const char *name
= (const char *) args
[arg_no
].p
;
513 if (name
== NULL
|| *name
== 0)
515 fprintf (fp
, _("no symbol"));
522 demangled
= bfd_demangle (link_info
.output_bfd
, name
,
523 DMGL_ANSI
| DMGL_PARAMS
);
524 if (demangled
!= NULL
)
526 fprintf (fp
, "%s", demangled
);
531 fprintf (fp
, "%s", name
);
535 /* native (host) void* pointer, like printf */
536 fprintf (fp
, "%p", args
[arg_no
].p
);
542 /* arbitrary string, like printf */
543 fprintf (fp
, "%s", (char *) args
[arg_no
].p
);
548 /* integer, like printf */
549 fprintf (fp
, "%d", args
[arg_no
].i
);
554 /* unsigned integer, like printf */
555 fprintf (fp
, "%u", args
[arg_no
].i
);
562 fprintf (fp
, "%ld", args
[arg_no
].l
);
567 else if (*fmt
== 'u')
569 fprintf (fp
, "%lu", args
[arg_no
].l
);
577 fprintf (fp
, "%%%c", fmt
[-1]);
583 if (is_warning
&& config
.fatal_warnings
)
584 config
.make_executable
= FALSE
;
590 /* Format info message and print on stdout. */
592 /* (You would think this should be called just "info", but then you
593 would be hosed by LynxOS, which defines that name in its libc.) */
596 info_msg (const char *fmt
, ...)
601 vfinfo (stdout
, fmt
, arg
, FALSE
);
605 /* ('e' for error.) Format info message and print on stderr. */
608 einfo (const char *fmt
, ...)
614 vfinfo (stderr
, fmt
, arg
, TRUE
);
620 info_assert (const char *file
, unsigned int line
)
622 einfo (_("%F%P: internal error %s %d\n"), file
, line
);
625 /* ('m' for map) Format info message and print on map. */
628 minfo (const char *fmt
, ...)
630 if (config
.map_file
!= NULL
)
635 if (fmt
[0] == '%' && fmt
[1] == '!' && fmt
[2] == 0)
637 /* Stash info about --as-needed shared libraries. Print
638 later so they don't appear intermingled with archive
640 struct asneeded_minfo
*m
= xmalloc (sizeof *m
);
643 m
->soname
= va_arg (arg
, const char *);
644 m
->ref
= va_arg (arg
, bfd
*);
645 m
->name
= va_arg (arg
, const char *);
646 *asneeded_list_tail
= m
;
647 asneeded_list_tail
= &m
->next
;
650 vfinfo (config
.map_file
, fmt
, arg
, FALSE
);
656 lfinfo (FILE *file
, const char *fmt
, ...)
661 vfinfo (file
, fmt
, arg
, FALSE
);
665 /* Functions to print the link map. */
670 fprintf (config
.map_file
, " ");
676 fprintf (config
.map_file
, "\n");
679 /* A more or less friendly abort message. In ld.h abort is defined to
680 call this function. */
683 ld_abort (const char *file
, int line
, const char *fn
)
686 einfo (_("%P: internal error: aborting at %s:%d in %s\n"),
689 einfo (_("%P: internal error: aborting at %s:%d\n"),
691 einfo (_("%F%P: please report this bug\n"));