2 Copyright (C) 1991-2023 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"
27 #include "safe-ctype.h"
28 #include "filenames.h"
42 %C clever filename:linenumber with function
43 %D like %C, but no function name
44 %E current bfd error or errno
46 %G like %D, but only function name
47 %H like %C but in addition emit section+offset
50 %W hex bfd_vma with 0x with no leading zeros taking up 10 spaces
51 %X no object output, fail return
52 %d integer, like printf
54 %lu unsigned long, like printf
55 %lx 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 %pU print script file without linenumber from etree_type.
64 %s arbitrary string, like printf
65 %u integer, like printf
66 %v hex bfd_vma, no leading zeros
67 %x integer, like printf
71 vfinfo (FILE *fp
, const char *fmt
, va_list ap
, bool is_warning
)
76 unsigned int arg_count
= 0;
100 if (is_warning
&& config
.no_warnings
)
103 for (arg_no
= 0; arg_no
< sizeof (args
) / sizeof (args
[0]); arg_no
++)
104 args
[arg_no
].type
= Bad
;
108 while (*scan
!= '\0')
110 while (*scan
!= '%' && *scan
!= '\0')
118 if (*scan
!= '0' && ISDIGIT (*scan
) && scan
[1] == '$')
120 arg_no
= *scan
- '1';
142 if (*scan
== 'A' || *scan
== 'B' || *scan
== 'I'
143 || *scan
== 'R' || *scan
== 'S' || *scan
== 'T')
162 if (*scan
== 'd' || *scan
== 'u' || *scan
== 'x')
174 if (arg_no
>= sizeof (args
) / sizeof (args
[0]))
176 args
[arg_no
].type
= arg_type
;
182 for (arg_no
= 0; arg_no
< arg_count
; arg_no
++)
184 switch (args
[arg_no
].type
)
187 args
[arg_no
].i
= va_arg (ap
, int);
190 args
[arg_no
].l
= va_arg (ap
, long);
193 args
[arg_no
].p
= va_arg (ap
, void *);
196 args
[arg_no
].v
= va_arg (ap
, bfd_vma
);
199 args
[arg_no
].reladdr
.abfd
= va_arg (ap
, bfd
*);
200 args
[arg_no
].reladdr
.sec
= va_arg (ap
, asection
*);
201 args
[arg_no
].reladdr
.off
= va_arg (ap
, bfd_vma
);
211 const char *str
= fmt
;
212 while (*fmt
!= '%' && *fmt
!= '\0')
215 if (fwrite (str
, 1, fmt
- str
, fp
))
225 if (*fmt
!= '0' && ISDIGIT (*fmt
) && fmt
[1] == '$')
243 /* no object output, fail return */
244 config
.make_executable
= false;
253 value
= args
[arg_no
].v
;
255 bfd_sprintf_vma (link_info
.output_bfd
, buf
, value
);
256 fprintf (fp
, "%s", buf
);
261 /* hex bfd_vma, no leading zeros */
263 uint64_t value
= args
[arg_no
].v
;
265 fprintf (fp
, "%" PRIx64
, value
);
270 /* hex bfd_vma with 0x with no leading zeroes taking up
271 10 spaces (including the 0x). */
276 value
= args
[arg_no
].v
;
278 sprintf (buf
, "0x%" PRIx64
, value
);
279 fprintf (fp
, "%10s", buf
);
284 /* Error is fatal. */
289 /* Print program name. */
290 fprintf (fp
, "%s", program_name
);
294 /* current bfd error or errno */
295 fprintf (fp
, "%s", bfd_errmsg (bfd_get_error ()));
302 /* Clever filename:linenumber with function name if possible.
303 The arguments are a BFD, a section, and an offset. */
305 static bfd
*last_bfd
;
306 static char *last_file
;
307 static char *last_function
;
311 asymbol
**asymbols
= NULL
;
312 const char *filename
;
313 const char *functionname
;
314 unsigned int linenumber
;
317 bfd_error_type last_bfd_error
= bfd_get_error ();
319 abfd
= args
[arg_no
].reladdr
.abfd
;
320 section
= args
[arg_no
].reladdr
.sec
;
321 offset
= args
[arg_no
].reladdr
.off
;
326 if (!bfd_generic_link_read_symbols (abfd
))
327 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd
);
329 asymbols
= bfd_get_outsymbols (abfd
);
332 /* The GNU Coding Standard requires that error messages
335 source-file-name:lineno: message
337 We do not always have a line number available so if
338 we cannot find them we print out the section name and
342 && bfd_find_nearest_line (abfd
, section
, asymbols
, offset
,
343 &filename
, &functionname
,
346 if (functionname
!= NULL
347 && (fmt
[-1] == 'C' || fmt
[-1] == 'H'))
349 /* Detect the case where we are printing out a
350 message for the same function as the last
351 call to vinfo ("%C"). In this situation do
352 not print out the ABFD filename or the
353 function name again. Note - we do still
354 print out the source filename, as this will
355 allow programs that parse the linker's output
356 (eg emacs) to correctly locate multiple
357 errors in the same source file. */
359 || last_function
== NULL
361 || (last_file
== NULL
) != (filename
== NULL
)
363 && filename_cmp (last_file
, filename
) != 0)
364 || strcmp (last_function
, functionname
) != 0)
366 lfinfo (fp
, _("%pB: in function `%pT':\n"),
373 last_file
= xstrdup (filename
);
374 free (last_function
);
375 last_function
= xstrdup (functionname
);
377 discard_last
= false;
380 lfinfo (fp
, "%pB:", abfd
);
382 if (filename
!= NULL
)
383 fprintf (fp
, "%s:", filename
);
385 done
= fmt
[-1] != 'H';
386 if (functionname
!= NULL
&& fmt
[-1] == 'G')
387 lfinfo (fp
, "%pT", functionname
);
388 else if (filename
!= NULL
&& linenumber
!= 0)
389 fprintf (fp
, "%u%s", linenumber
, done
? "" : ":");
395 lfinfo (fp
, "%pB:", abfd
);
399 lfinfo (fp
, "(%pA+0x%v)", section
, offset
);
400 bfd_set_error (last_bfd_error
);
407 free (last_function
);
408 last_function
= NULL
;
416 /* section name from a section */
421 sec
= (asection
*) args
[arg_no
].p
;
423 fprintf (fp
, "%s", sec
->name
);
427 const char *group
= bfd_group_name (abfd
, sec
);
429 fprintf (fp
, "[%s]", group
);
432 else if (*fmt
== 'B')
434 /* filename from a bfd */
435 bfd
*abfd
= (bfd
*) args
[arg_no
].p
;
440 fprintf (fp
, "%s generated", program_name
);
441 else if (abfd
->my_archive
!= NULL
442 && !bfd_is_thin_archive (abfd
->my_archive
))
443 fprintf (fp
, "%s(%s)",
444 bfd_get_filename (abfd
->my_archive
),
445 bfd_get_filename (abfd
));
447 fprintf (fp
, "%s", bfd_get_filename (abfd
));
449 else if (*fmt
== 'I')
451 /* filename from a lang_input_statement_type */
452 lang_input_statement_type
*i
;
455 i
= (lang_input_statement_type
*) args
[arg_no
].p
;
457 if (i
->the_bfd
!= NULL
458 && i
->the_bfd
->my_archive
!= NULL
459 && !bfd_is_thin_archive (i
->the_bfd
->my_archive
))
460 fprintf (fp
, "(%s)%s",
461 bfd_get_filename (i
->the_bfd
->my_archive
),
464 fprintf (fp
, "%s", i
->filename
);
466 else if (*fmt
== 'R')
468 /* Print all that's interesting about a relent. */
469 arelent
*relent
= (arelent
*) args
[arg_no
].p
;
473 lfinfo (fp
, "%s+0x%v (type %s)",
474 (*(relent
->sym_ptr_ptr
))->name
,
476 relent
->howto
->name
);
478 else if (*fmt
== 'S' || *fmt
== 'U')
480 /* Print script file and perhaps the associated linenumber. */
482 etree_type
*tp
= (etree_type
*) args
[arg_no
].p
;
489 tp
->type
.filename
= ldlex_filename ();
490 tp
->type
.lineno
= lineno
;
492 if (tp
->type
.filename
!= NULL
&& fmt
[-1] == 'S')
493 fprintf (fp
, "%s:%u", tp
->type
.filename
, tp
->type
.lineno
);
494 else if (tp
->type
.filename
!= NULL
&& fmt
[-1] == 'U')
495 fprintf (fp
, "%s", tp
->type
.filename
);
497 else if (*fmt
== 'T')
500 const char *name
= (const char *) args
[arg_no
].p
;
504 if (name
== NULL
|| *name
== 0)
506 fprintf (fp
, _("no symbol"));
513 demangled
= bfd_demangle (link_info
.output_bfd
, name
,
514 DMGL_ANSI
| DMGL_PARAMS
);
515 if (demangled
!= NULL
)
517 fprintf (fp
, "%s", demangled
);
522 fprintf (fp
, "%s", name
);
526 /* native (host) void* pointer, like printf */
527 fprintf (fp
, "%p", args
[arg_no
].p
);
533 /* arbitrary string, like printf */
534 fprintf (fp
, "%s", (char *) args
[arg_no
].p
);
539 /* integer, like printf */
540 fprintf (fp
, "%d", args
[arg_no
].i
);
545 /* unsigned integer, like printf */
546 fprintf (fp
, "%u", args
[arg_no
].i
);
551 /* unsigned integer, like printf */
552 fprintf (fp
, "%x", args
[arg_no
].i
);
559 fprintf (fp
, "%ld", args
[arg_no
].l
);
564 else if (*fmt
== 'u')
566 fprintf (fp
, "%lu", args
[arg_no
].l
);
571 else if (*fmt
== 'x')
573 fprintf (fp
, "%lx", args
[arg_no
].l
);
581 fprintf (fp
, "%%%c", fmt
[-1]);
587 if (is_warning
&& config
.fatal_warnings
)
588 config
.make_executable
= false;
594 /* Format info message and print on stdout. */
596 /* (You would think this should be called just "info", but then you
597 would be hosed by LynxOS, which defines that name in its libc.) */
600 info_msg (const char *fmt
, ...)
605 vfinfo (stdout
, fmt
, arg
, false);
609 /* ('e' for error.) Format info message and print on stderr. */
612 einfo (const char *fmt
, ...)
618 vfinfo (stderr
, fmt
, arg
, true);
624 info_assert (const char *file
, unsigned int line
)
626 einfo (_("%F%P: internal error %s %d\n"), file
, line
);
629 /* ('m' for map) Format info message and print on map. */
632 minfo (const char *fmt
, ...)
634 if (config
.map_file
!= NULL
)
639 if (fmt
[0] == '%' && fmt
[1] == '!' && fmt
[2] == 0)
641 /* Stash info about --as-needed shared libraries. Print
642 later so they don't appear intermingled with archive
644 struct asneeded_minfo
*m
= xmalloc (sizeof *m
);
647 m
->soname
= va_arg (arg
, const char *);
648 m
->ref
= va_arg (arg
, bfd
*);
649 m
->name
= va_arg (arg
, const char *);
650 *asneeded_list_tail
= m
;
651 asneeded_list_tail
= &m
->next
;
654 vfinfo (config
.map_file
, fmt
, arg
, false);
660 lfinfo (FILE *file
, const char *fmt
, ...)
665 vfinfo (file
, fmt
, arg
, false);
669 /* Functions to print the link map. */
672 print_spaces (int count
)
674 fprintf (config
.map_file
, "%*s", count
, "");
680 fprintf (config
.map_file
, "\n");
683 /* A more or less friendly abort message. In ld.h abort is defined to
684 call this function. */
687 ld_abort (const char *file
, int line
, const char *fn
)
690 einfo (_("%P: internal error: aborting at %s:%d in %s\n"),
693 einfo (_("%P: internal error: aborting at %s:%d\n"),
695 einfo (_("%F%P: please report this bug\n"));