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
49 %I filename from a lang_input_statement_type
51 %R info about a relent
52 %S print script file and linenumber from etree_type.
55 %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces
56 %X no object output, fail return
57 %d integer, like printf
59 %lu unsigned long, like printf
60 %p native (host) void* pointer, like printf
61 %pA section name from a section
62 %pB filename from a bfd
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';
141 if (*scan
== 'A' || *scan
== 'B')
159 if (*scan
== 'd' || *scan
== 'u')
171 if (arg_no
>= sizeof (args
) / sizeof (args
[0]))
173 args
[arg_no
].type
= arg_type
;
179 for (arg_no
= 0; arg_no
< arg_count
; arg_no
++)
181 switch (args
[arg_no
].type
)
184 args
[arg_no
].i
= va_arg (ap
, int);
187 args
[arg_no
].l
= va_arg (ap
, long);
190 args
[arg_no
].p
= va_arg (ap
, void *);
193 args
[arg_no
].v
= va_arg (ap
, bfd_vma
);
196 args
[arg_no
].reladdr
.abfd
= va_arg (ap
, bfd
*);
197 args
[arg_no
].reladdr
.sec
= va_arg (ap
, asection
*);
198 args
[arg_no
].reladdr
.off
= va_arg (ap
, bfd_vma
);
208 const char *str
= fmt
;
209 while (*fmt
!= '%' && *fmt
!= '\0')
212 if (fwrite (str
, 1, fmt
- str
, fp
))
222 if (*fmt
!= '0' && ISDIGIT (*fmt
) && fmt
[1] == '$')
240 /* no object output, fail return */
241 config
.make_executable
= FALSE
;
247 bfd_vma value
= args
[arg_no
].v
;
249 fprintf_vma (fp
, value
);
254 /* hex bfd_vma, no leading zeros */
258 bfd_vma value
= args
[arg_no
].v
;
260 sprintf_vma (p
, value
);
270 /* hex bfd_vma with 0x with no leading zeroes taking up
278 value
= args
[arg_no
].v
;
280 sprintf_vma (buf
, value
);
281 for (p
= buf
; *p
== '0'; ++p
)
291 fprintf (fp
, "0x%s", p
);
298 const char *name
= (const char *) args
[arg_no
].p
;
300 if (name
== NULL
|| *name
== 0)
302 fprintf (fp
, _("no symbol"));
309 demangled
= bfd_demangle (link_info
.output_bfd
, name
,
310 DMGL_ANSI
| DMGL_PARAMS
);
311 if (demangled
!= NULL
)
313 fprintf (fp
, "%s", demangled
);
318 fprintf (fp
, "%s", name
);
323 /* Error is fatal. */
328 /* Print program name. */
329 fprintf (fp
, "%s", program_name
);
333 /* current bfd error or errno */
334 fprintf (fp
, "%s", bfd_errmsg (bfd_get_error ()));
338 /* filename from a lang_input_statement_type */
340 lang_input_statement_type
*i
;
342 i
= (lang_input_statement_type
*) args
[arg_no
].p
;
344 if (i
->the_bfd
->my_archive
!= NULL
345 && !bfd_is_thin_archive (i
->the_bfd
->my_archive
))
347 bfd_get_filename (i
->the_bfd
->my_archive
));
348 fprintf (fp
, "%s", i
->local_sym_name
);
349 if ((i
->the_bfd
->my_archive
== NULL
350 || bfd_is_thin_archive (i
->the_bfd
->my_archive
))
351 && filename_cmp (i
->local_sym_name
, i
->filename
) != 0)
352 fprintf (fp
, " (%s)", i
->filename
);
357 /* Print script file and linenumber. */
360 etree_type
*tp
= (etree_type
*) args
[arg_no
].p
;
365 tp
->type
.filename
= ldlex_filename ();
366 tp
->type
.lineno
= lineno
;
368 if (tp
->type
.filename
!= NULL
)
369 fprintf (fp
, "%s:%u", tp
->type
.filename
, tp
->type
.lineno
);
374 /* Print all that's interesting about a relent. */
376 arelent
*relent
= (arelent
*) args
[arg_no
].p
;
378 lfinfo (fp
, "%s+0x%v (type %s)",
379 (*(relent
->sym_ptr_ptr
))->name
,
381 relent
->howto
->name
);
389 /* Clever filename:linenumber with function name if possible.
390 The arguments are a BFD, a section, and an offset. */
392 static bfd
*last_bfd
;
393 static char *last_file
;
394 static char *last_function
;
398 asymbol
**asymbols
= NULL
;
399 const char *filename
;
400 const char *functionname
;
401 unsigned int linenumber
;
402 bfd_boolean discard_last
;
405 abfd
= args
[arg_no
].reladdr
.abfd
;
406 section
= args
[arg_no
].reladdr
.sec
;
407 offset
= args
[arg_no
].reladdr
.off
;
412 if (!bfd_generic_link_read_symbols (abfd
))
413 einfo (_("%pB%F: could not read symbols: %E\n"), abfd
);
415 asymbols
= bfd_get_outsymbols (abfd
);
418 /* The GNU Coding Standard requires that error messages
421 source-file-name:lineno: message
423 We do not always have a line number available so if
424 we cannot find them we print out the section name and
428 && bfd_find_nearest_line (abfd
, section
, asymbols
, offset
,
429 &filename
, &functionname
,
432 if (functionname
!= NULL
433 && (fmt
[-1] == 'C' || fmt
[-1] == 'H'))
435 /* Detect the case where we are printing out a
436 message for the same function as the last
437 call to vinfo ("%C"). In this situation do
438 not print out the ABFD filename or the
439 function name again. Note - we do still
440 print out the source filename, as this will
441 allow programs that parse the linker's output
442 (eg emacs) to correctly locate multiple
443 errors in the same source file. */
445 || last_function
== NULL
447 || (last_file
== NULL
) != (filename
== NULL
)
449 && filename_cmp (last_file
, filename
) != 0)
450 || strcmp (last_function
, functionname
) != 0)
452 lfinfo (fp
, _("%pB: In function `%T':\n"),
456 if (last_file
!= NULL
)
460 last_file
= xstrdup (filename
);
461 if (last_function
!= NULL
)
462 free (last_function
);
463 last_function
= xstrdup (functionname
);
465 discard_last
= FALSE
;
468 lfinfo (fp
, "%pB:", abfd
);
470 if (filename
!= NULL
)
471 fprintf (fp
, "%s:", filename
);
473 done
= fmt
[-1] != 'H';
474 if (functionname
!= NULL
&& fmt
[-1] == 'G')
475 lfinfo (fp
, "%T", functionname
);
476 else if (filename
!= NULL
&& linenumber
!= 0)
477 fprintf (fp
, "%u%s", linenumber
, done
? "" : ":");
483 lfinfo (fp
, "%pB:", abfd
);
487 lfinfo (fp
, "(%pA+0x%v)", section
, offset
);
492 if (last_file
!= NULL
)
497 if (last_function
!= NULL
)
499 free (last_function
);
500 last_function
= NULL
;
509 /* section name from a section */
512 const char *group
= NULL
;
513 struct coff_comdat_info
*ci
;
516 sec
= (asection
*) args
[arg_no
].p
;
519 fprintf (fp
, "%s", sec
->name
);
521 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
522 && elf_next_in_group (sec
) != NULL
523 && (sec
->flags
& SEC_GROUP
) == 0)
524 group
= elf_group_name (sec
);
525 else if (abfd
!= NULL
526 && bfd_get_flavour (abfd
) == bfd_target_coff_flavour
527 && (ci
= bfd_coff_get_comdat_section (sec
->owner
,
531 fprintf (fp
, "[%s]", group
);
533 else if (*fmt
== 'B')
535 /* filename from a bfd */
536 bfd
*abfd
= (bfd
*) args
[arg_no
].p
;
541 fprintf (fp
, "%s generated", program_name
);
542 else if (abfd
->my_archive
!= NULL
543 && !bfd_is_thin_archive (abfd
->my_archive
))
544 fprintf (fp
, "%s(%s)", abfd
->my_archive
->filename
,
547 fprintf (fp
, "%s", abfd
->filename
);
551 /* native (host) void* pointer, like printf */
552 fprintf (fp
, "%p", args
[arg_no
].p
);
558 /* arbitrary string, like printf */
559 fprintf (fp
, "%s", (char *) args
[arg_no
].p
);
564 /* integer, like printf */
565 fprintf (fp
, "%d", args
[arg_no
].i
);
570 /* unsigned integer, like printf */
571 fprintf (fp
, "%u", args
[arg_no
].i
);
578 fprintf (fp
, "%ld", args
[arg_no
].l
);
583 else if (*fmt
== 'u')
585 fprintf (fp
, "%lu", args
[arg_no
].l
);
593 fprintf (fp
, "%%%c", fmt
[-1]);
599 if (is_warning
&& config
.fatal_warnings
)
600 config
.make_executable
= FALSE
;
606 /* Format info message and print on stdout. */
608 /* (You would think this should be called just "info", but then you
609 would be hosed by LynxOS, which defines that name in its libc.) */
612 info_msg (const char *fmt
, ...)
617 vfinfo (stdout
, fmt
, arg
, FALSE
);
621 /* ('e' for error.) Format info message and print on stderr. */
624 einfo (const char *fmt
, ...)
630 vfinfo (stderr
, fmt
, arg
, TRUE
);
636 info_assert (const char *file
, unsigned int line
)
638 einfo (_("%F%P: internal error %s %d\n"), file
, line
);
641 /* ('m' for map) Format info message and print on map. */
644 minfo (const char *fmt
, ...)
646 if (config
.map_file
!= NULL
)
651 if (fmt
[0] == '%' && fmt
[1] == '!' && fmt
[2] == 0)
653 /* Stash info about --as-needed shared libraries. Print
654 later so they don't appear intermingled with archive
656 struct asneeded_minfo
*m
= xmalloc (sizeof *m
);
659 m
->soname
= va_arg (arg
, const char *);
660 m
->ref
= va_arg (arg
, bfd
*);
661 m
->name
= va_arg (arg
, const char *);
662 *asneeded_list_tail
= m
;
663 asneeded_list_tail
= &m
->next
;
666 vfinfo (config
.map_file
, fmt
, arg
, FALSE
);
672 lfinfo (FILE *file
, const char *fmt
, ...)
677 vfinfo (file
, fmt
, arg
, FALSE
);
681 /* Functions to print the link map. */
686 fprintf (config
.map_file
, " ");
692 fprintf (config
.map_file
, "\n");
695 /* A more or less friendly abort message. In ld.h abort is defined to
696 call this function. */
699 ld_abort (const char *file
, int line
, const char *fn
)
702 einfo (_("%P: internal error: aborting at %s:%d in %s\n"),
705 einfo (_("%P: internal error: aborting at %s:%d\n"),
707 einfo (_("%P%F: please report this bug\n"));