2 Copyright (C) 1991-2018 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"
43 %C clever filename:linenumber with function
44 %D like %C, but no function name
45 %E current bfd error or errno
47 %G like %D, but only function name
48 %H like %C but in addition emit section+offset
51 %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces
52 %X no object output, fail return
53 %d integer, like printf
55 %lu unsigned long, like printf
56 %p native (host) void* pointer, like printf
57 %pA section name from a section
58 %pB filename from a bfd
59 %pI filename from a lang_input_statement_type
60 %pR info about a relent
61 %pS print script file and linenumber from etree_type.
63 %s arbitrary string, like printf
64 %u integer, like printf
65 %v hex bfd_vma, no leading zeros
69 vfinfo (FILE *fp
, const char *fmt
, va_list ap
, bfd_boolean is_warning
)
71 bfd_boolean fatal
= FALSE
;
74 unsigned int arg_count
= 0;
98 for (arg_no
= 0; arg_no
< sizeof (args
) / sizeof (args
[0]); arg_no
++)
99 args
[arg_no
].type
= Bad
;
103 while (*scan
!= '\0')
105 while (*scan
!= '%' && *scan
!= '\0')
113 if (*scan
!= '0' && ISDIGIT (*scan
) && scan
[1] == '$')
115 arg_no
= *scan
- '1';
137 if (*scan
== 'A' || *scan
== 'B' || *scan
== 'I'
138 || *scan
== 'R' || *scan
== 'S' || *scan
== 'T')
156 if (*scan
== 'd' || *scan
== 'u')
168 if (arg_no
>= sizeof (args
) / sizeof (args
[0]))
170 args
[arg_no
].type
= arg_type
;
176 for (arg_no
= 0; arg_no
< arg_count
; arg_no
++)
178 switch (args
[arg_no
].type
)
181 args
[arg_no
].i
= va_arg (ap
, int);
184 args
[arg_no
].l
= va_arg (ap
, long);
187 args
[arg_no
].p
= va_arg (ap
, void *);
190 args
[arg_no
].v
= va_arg (ap
, bfd_vma
);
193 args
[arg_no
].reladdr
.abfd
= va_arg (ap
, bfd
*);
194 args
[arg_no
].reladdr
.sec
= va_arg (ap
, asection
*);
195 args
[arg_no
].reladdr
.off
= va_arg (ap
, bfd_vma
);
205 const char *str
= fmt
;
206 while (*fmt
!= '%' && *fmt
!= '\0')
209 if (fwrite (str
, 1, fmt
- str
, fp
))
219 if (*fmt
!= '0' && ISDIGIT (*fmt
) && fmt
[1] == '$')
237 /* no object output, fail return */
238 config
.make_executable
= FALSE
;
244 bfd_vma value
= args
[arg_no
].v
;
246 fprintf_vma (fp
, value
);
251 /* hex bfd_vma, no leading zeros */
255 bfd_vma value
= args
[arg_no
].v
;
257 sprintf_vma (p
, value
);
267 /* hex bfd_vma with 0x with no leading zeroes taking up
275 value
= args
[arg_no
].v
;
277 sprintf_vma (buf
, value
);
278 for (p
= buf
; *p
== '0'; ++p
)
288 fprintf (fp
, "0x%s", p
);
293 /* Error is fatal. */
298 /* Print program name. */
299 fprintf (fp
, "%s", program_name
);
303 /* current bfd error or errno */
304 fprintf (fp
, "%s", bfd_errmsg (bfd_get_error ()));
311 /* Clever filename:linenumber with function name if possible.
312 The arguments are a BFD, a section, and an offset. */
314 static bfd
*last_bfd
;
315 static char *last_file
;
316 static char *last_function
;
320 asymbol
**asymbols
= NULL
;
321 const char *filename
;
322 const char *functionname
;
323 unsigned int linenumber
;
324 bfd_boolean discard_last
;
327 abfd
= args
[arg_no
].reladdr
.abfd
;
328 section
= args
[arg_no
].reladdr
.sec
;
329 offset
= args
[arg_no
].reladdr
.off
;
334 if (!bfd_generic_link_read_symbols (abfd
))
335 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd
);
337 asymbols
= bfd_get_outsymbols (abfd
);
340 /* The GNU Coding Standard requires that error messages
343 source-file-name:lineno: message
345 We do not always have a line number available so if
346 we cannot find them we print out the section name and
350 && bfd_find_nearest_line (abfd
, section
, asymbols
, offset
,
351 &filename
, &functionname
,
354 if (functionname
!= NULL
355 && (fmt
[-1] == 'C' || fmt
[-1] == 'H'))
357 /* Detect the case where we are printing out a
358 message for the same function as the last
359 call to vinfo ("%C"). In this situation do
360 not print out the ABFD filename or the
361 function name again. Note - we do still
362 print out the source filename, as this will
363 allow programs that parse the linker's output
364 (eg emacs) to correctly locate multiple
365 errors in the same source file. */
367 || last_function
== NULL
369 || (last_file
== NULL
) != (filename
== NULL
)
371 && filename_cmp (last_file
, filename
) != 0)
372 || strcmp (last_function
, functionname
) != 0)
374 lfinfo (fp
, _("%pB: in function `%pT':\n"),
378 if (last_file
!= NULL
)
382 last_file
= xstrdup (filename
);
383 if (last_function
!= NULL
)
384 free (last_function
);
385 last_function
= xstrdup (functionname
);
387 discard_last
= FALSE
;
390 lfinfo (fp
, "%pB:", abfd
);
392 if (filename
!= NULL
)
393 fprintf (fp
, "%s:", filename
);
395 done
= fmt
[-1] != 'H';
396 if (functionname
!= NULL
&& fmt
[-1] == 'G')
397 lfinfo (fp
, "%pT", functionname
);
398 else if (filename
!= NULL
&& linenumber
!= 0)
399 fprintf (fp
, "%u%s", linenumber
, done
? "" : ":");
405 lfinfo (fp
, "%pB:", abfd
);
409 lfinfo (fp
, "(%pA+0x%v)", section
, offset
);
414 if (last_file
!= NULL
)
419 if (last_function
!= NULL
)
421 free (last_function
);
422 last_function
= NULL
;
431 /* section name from a section */
434 const char *group
= NULL
;
435 struct coff_comdat_info
*ci
;
438 sec
= (asection
*) args
[arg_no
].p
;
441 fprintf (fp
, "%s", sec
->name
);
443 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
444 && elf_next_in_group (sec
) != NULL
445 && (sec
->flags
& SEC_GROUP
) == 0)
446 group
= elf_group_name (sec
);
447 else if (abfd
!= NULL
448 && bfd_get_flavour (abfd
) == bfd_target_coff_flavour
449 && (ci
= bfd_coff_get_comdat_section (sec
->owner
,
453 fprintf (fp
, "[%s]", group
);
455 else if (*fmt
== 'B')
457 /* filename from a bfd */
458 bfd
*abfd
= (bfd
*) args
[arg_no
].p
;
463 fprintf (fp
, "%s generated", program_name
);
464 else if (abfd
->my_archive
!= NULL
465 && !bfd_is_thin_archive (abfd
->my_archive
))
466 fprintf (fp
, "%s(%s)", abfd
->my_archive
->filename
,
469 fprintf (fp
, "%s", abfd
->filename
);
471 else if (*fmt
== 'I')
473 /* filename from a lang_input_statement_type */
474 lang_input_statement_type
*i
;
477 i
= (lang_input_statement_type
*) args
[arg_no
].p
;
479 if (i
->the_bfd
->my_archive
!= NULL
480 && !bfd_is_thin_archive (i
->the_bfd
->my_archive
))
482 bfd_get_filename (i
->the_bfd
->my_archive
));
483 fprintf (fp
, "%s", i
->local_sym_name
);
484 if ((i
->the_bfd
->my_archive
== NULL
485 || bfd_is_thin_archive (i
->the_bfd
->my_archive
))
486 && filename_cmp (i
->local_sym_name
, i
->filename
) != 0)
487 fprintf (fp
, " (%s)", i
->filename
);
489 else if (*fmt
== 'R')
491 /* Print all that's interesting about a relent. */
492 arelent
*relent
= (arelent
*) args
[arg_no
].p
;
496 lfinfo (fp
, "%s+0x%v (type %s)",
497 (*(relent
->sym_ptr_ptr
))->name
,
499 relent
->howto
->name
);
501 else if (*fmt
== 'S')
503 /* Print script file and linenumber. */
505 etree_type
*tp
= (etree_type
*) args
[arg_no
].p
;
512 tp
->type
.filename
= ldlex_filename ();
513 tp
->type
.lineno
= lineno
;
515 if (tp
->type
.filename
!= NULL
)
516 fprintf (fp
, "%s:%u", tp
->type
.filename
, tp
->type
.lineno
);
518 else if (*fmt
== 'T')
521 const char *name
= (const char *) args
[arg_no
].p
;
525 if (name
== NULL
|| *name
== 0)
527 fprintf (fp
, _("no symbol"));
534 demangled
= bfd_demangle (link_info
.output_bfd
, name
,
535 DMGL_ANSI
| DMGL_PARAMS
);
536 if (demangled
!= NULL
)
538 fprintf (fp
, "%s", demangled
);
543 fprintf (fp
, "%s", name
);
547 /* native (host) void* pointer, like printf */
548 fprintf (fp
, "%p", args
[arg_no
].p
);
554 /* arbitrary string, like printf */
555 fprintf (fp
, "%s", (char *) args
[arg_no
].p
);
560 /* integer, like printf */
561 fprintf (fp
, "%d", args
[arg_no
].i
);
566 /* unsigned integer, like printf */
567 fprintf (fp
, "%u", args
[arg_no
].i
);
574 fprintf (fp
, "%ld", args
[arg_no
].l
);
579 else if (*fmt
== 'u')
581 fprintf (fp
, "%lu", args
[arg_no
].l
);
589 fprintf (fp
, "%%%c", fmt
[-1]);
595 if (is_warning
&& config
.fatal_warnings
)
596 config
.make_executable
= FALSE
;
602 /* Format info message and print on stdout. */
604 /* (You would think this should be called just "info", but then you
605 would be hosed by LynxOS, which defines that name in its libc.) */
608 info_msg (const char *fmt
, ...)
613 vfinfo (stdout
, fmt
, arg
, FALSE
);
617 /* ('e' for error.) Format info message and print on stderr. */
620 einfo (const char *fmt
, ...)
626 vfinfo (stderr
, fmt
, arg
, TRUE
);
632 info_assert (const char *file
, unsigned int line
)
634 einfo (_("%F%P: internal error %s %d\n"), file
, line
);
637 /* ('m' for map) Format info message and print on map. */
640 minfo (const char *fmt
, ...)
642 if (config
.map_file
!= NULL
)
647 if (fmt
[0] == '%' && fmt
[1] == '!' && fmt
[2] == 0)
649 /* Stash info about --as-needed shared libraries. Print
650 later so they don't appear intermingled with archive
652 struct asneeded_minfo
*m
= xmalloc (sizeof *m
);
655 m
->soname
= va_arg (arg
, const char *);
656 m
->ref
= va_arg (arg
, bfd
*);
657 m
->name
= va_arg (arg
, const char *);
658 *asneeded_list_tail
= m
;
659 asneeded_list_tail
= &m
->next
;
662 vfinfo (config
.map_file
, fmt
, arg
, FALSE
);
668 lfinfo (FILE *file
, const char *fmt
, ...)
673 vfinfo (file
, fmt
, arg
, FALSE
);
677 /* Functions to print the link map. */
682 fprintf (config
.map_file
, " ");
688 fprintf (config
.map_file
, "\n");
691 /* A more or less friendly abort message. In ld.h abort is defined to
692 call this function. */
695 ld_abort (const char *file
, int line
, const char *fn
)
698 einfo (_("%P: internal error: aborting at %s:%d in %s\n"),
701 einfo (_("%P: internal error: aborting at %s:%d\n"),
703 einfo (_("%F%P: please report this bug\n"));