2 Copyright (C) 1991-2014 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"
41 %A section name from a section
42 %B filename from a bfd
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 %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 arg, bfd_boolean is_warning)
69 bfd_boolean fatal = FALSE;
73 const char *str = fmt;
74 while (*fmt != '%' && *fmt != '\0')
77 if (fwrite (str, 1, fmt - str, fp))
93 /* no object output, fail return */
94 config.make_executable = FALSE;
100 bfd_vma value = va_arg (arg, bfd_vma);
101 fprintf_vma (fp, value);
106 /* hex bfd_vma, no leading zeros */
110 bfd_vma value = va_arg (arg, bfd_vma);
111 sprintf_vma (p, value);
121 /* hex bfd_vma with 0x with no leading zeroes taking up
129 value = va_arg (arg, bfd_vma);
130 sprintf_vma (buf, value);
131 for (p = buf; *p == '0'; ++p)
141 fprintf (fp, "0x%s", p);
148 const char *name = va_arg (arg, const char *);
150 if (name == NULL || *name == 0)
152 fprintf (fp, _("no symbol"));
159 demangled = bfd_demangle (link_info.output_bfd, name,
160 DMGL_ANSI | DMGL_PARAMS);
161 if (demangled != NULL)
163 fprintf (fp, "%s", demangled);
168 fprintf (fp, "%s", name);
173 /* section name from a section */
175 asection *sec = va_arg (arg, asection *);
176 bfd *abfd = sec->owner;
177 const char *group = NULL;
178 struct coff_comdat_info *ci;
180 fprintf (fp, "%s", sec->name);
182 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
183 && elf_next_in_group (sec) != NULL
184 && (sec->flags & SEC_GROUP) == 0)
185 group = elf_group_name (sec);
186 else if (abfd != NULL
187 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
188 && (ci = bfd_coff_get_comdat_section (sec->owner,
192 fprintf (fp, "[%s]", group);
197 /* filename from a bfd */
199 bfd *abfd = va_arg (arg, bfd *);
202 fprintf (fp, "%s generated", program_name);
203 else if (abfd->my_archive)
204 fprintf (fp, "%s(%s)", abfd->my_archive->filename,
207 fprintf (fp, "%s", abfd->filename);
212 /* Error is fatal. */
217 /* Print program name. */
218 fprintf (fp, "%s", program_name);
222 /* current bfd error or errno */
223 fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
227 /* filename from a lang_input_statement_type */
229 lang_input_statement_type *i;
231 i = va_arg (arg, lang_input_statement_type *);
232 if (bfd_my_archive (i->the_bfd) != NULL)
234 bfd_get_filename (bfd_my_archive (i->the_bfd)));
235 fprintf (fp, "%s", i->local_sym_name);
236 if (bfd_my_archive (i->the_bfd) == NULL
237 && filename_cmp (i->local_sym_name, i->filename) != 0)
238 fprintf (fp, " (%s)", i->filename);
243 /* Print script file and linenumber. */
246 etree_type *tp = va_arg (arg, etree_type *);
251 tp->type.filename = ldlex_filename ();
252 tp->type.lineno = lineno;
254 if (tp->type.filename != NULL)
255 fprintf (fp, "%s:%u", tp->type.filename, tp->type.lineno);
260 /* Print all that's interesting about a relent. */
262 arelent *relent = va_arg (arg, arelent *);
264 lfinfo (fp, "%s+0x%v (type %s)",
265 (*(relent->sym_ptr_ptr))->name,
267 relent->howto->name);
275 /* Clever filename:linenumber with function name if possible.
276 The arguments are a BFD, a section, and an offset. */
278 static bfd *last_bfd;
279 static char *last_file = NULL;
280 static char *last_function = NULL;
284 asymbol **asymbols = NULL;
285 const char *filename;
286 const char *functionname;
287 unsigned int linenumber;
288 bfd_boolean discard_last;
291 abfd = va_arg (arg, bfd *);
292 section = va_arg (arg, asection *);
293 offset = va_arg (arg, bfd_vma);
297 if (!bfd_generic_link_read_symbols (abfd))
298 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
300 asymbols = bfd_get_outsymbols (abfd);
303 /* The GNU Coding Standard requires that error messages
306 source-file-name:lineno: message
308 We do not always have a line number available so if
309 we cannot find them we print out the section name and
313 && bfd_find_nearest_line (abfd, section, asymbols, offset,
314 &filename, &functionname,
317 if (functionname != NULL
318 && (fmt[-1] == 'C' || fmt[-1] == 'H'))
320 /* Detect the case where we are printing out a
321 message for the same function as the last
322 call to vinfo ("%C"). In this situation do
323 not print out the ABFD filename or the
324 function name again. Note - we do still
325 print out the source filename, as this will
326 allow programs that parse the linker's output
327 (eg emacs) to correctly locate multiple
328 errors in the same source file. */
331 || last_function == NULL
334 && filename_cmp (last_file, filename) != 0)
335 || strcmp (last_function, functionname) != 0)
337 lfinfo (fp, _("%B: In function `%T':\n"),
341 if (last_file != NULL)
345 last_file = xstrdup (filename);
346 if (last_function != NULL)
347 free (last_function);
348 last_function = xstrdup (functionname);
350 discard_last = FALSE;
353 lfinfo (fp, "%B:", abfd);
355 if (filename != NULL)
356 fprintf (fp, "%s:", filename);
358 done = fmt[-1] != 'H';
359 if (functionname != NULL && fmt[-1] == 'G')
360 lfinfo (fp, "%T", functionname);
361 else if (filename != NULL && linenumber != 0)
362 fprintf (fp, "%u%s", linenumber, done ? "" : ":");
368 lfinfo (fp, "%B:", abfd);
372 lfinfo (fp, "(%A+0x%v)", section, offset);
377 if (last_file != NULL)
382 if (last_function != NULL)
384 free (last_function);
385 last_function = NULL;
392 /* native (host) void* pointer, like printf */
393 fprintf (fp, "%p", va_arg (arg, void *));
397 /* arbitrary string, like printf */
398 fprintf (fp, "%s", va_arg (arg, char *));
402 /* integer, like printf */
403 fprintf (fp, "%d", va_arg (arg, int));
407 /* unsigned integer, like printf */
408 fprintf (fp, "%u", va_arg (arg, unsigned int));
414 fprintf (fp, "%ld", va_arg (arg, long));
418 else if (*fmt == 'u')
420 fprintf (fp, "%lu", va_arg (arg, unsigned long));
427 fprintf (fp, "%%%c", fmt[-1]);
433 if (is_warning && config.fatal_warnings)
434 config.make_executable = FALSE;
440 /* Format info message and print on stdout. */
442 /* (You would think this should be called just "info", but then you
443 would be hosed by LynxOS, which defines that name in its libc.) */
446 info_msg (const char *fmt, ...)
451 vfinfo (stdout, fmt, arg, FALSE);
455 /* ('e' for error.) Format info message and print on stderr. */
458 einfo (const char *fmt, ...)
464 vfinfo (stderr, fmt, arg, TRUE);
470 info_assert (const char *file, unsigned int line)
472 einfo (_("%F%P: internal error %s %d\n"), file, line);
475 /* ('m' for map) Format info message and print on map. */
478 minfo (const char *fmt, ...)
480 if (config.map_file != NULL)
485 if (fmt[0] == '%' && fmt[1] == '!' && fmt[2] == 0)
487 /* Stash info about --as-needed shared libraries. Print
488 later so they don't appear intermingled with archive
490 struct asneeded_minfo *m = xmalloc (sizeof *m);
493 m->soname = va_arg (arg, const char *);
494 m->ref = va_arg (arg, bfd *);
495 m->name = va_arg (arg, const char *);
496 *asneeded_list_tail = m;
497 asneeded_list_tail = &m->next;
500 vfinfo (config.map_file, fmt, arg, FALSE);
506 lfinfo (FILE *file, const char *fmt, ...)
511 vfinfo (file, fmt, arg, FALSE);
515 /* Functions to print the link map. */
520 fprintf (config.map_file, " ");
526 fprintf (config.map_file, "\n");
529 /* A more or less friendly abort message. In ld.h abort is defined to
530 call this function. */
533 ld_abort (const char *file, int line, const char *fn)
536 einfo (_("%P: internal error: aborting at %s line %d in %s\n"),
539 einfo (_("%P: internal error: aborting at %s line %d\n"),
541 einfo (_("%P%F: please report this bug\n"));