Commit
5aa0f10c424e added a per_xvec_warn array to provide support for
warnings from elf_object_p (and a later patch for warnings from
pe_bfd_object_p) to be cached and then only printed if the target
matches. It was quite limited in the style of message supported, only
one message could be printed, and didn't really meet the stated aim of
only warning when a target matches: There are many other errors and
warnings that can be emitted by functions called from elf_object_p.
So this patch extends the error handler functions to support printing
to a string buffer, extends per_xvec_warn to support multiple errors/
warnings, and hooks this all into bfd_check_format_matches. If
bfd_check_format_matches succeeds then any errors/warnings are printed
for the matching target. If bfd_check_format_matches fails either due
to no match or to multiple matches and only one target vector produced
errors, then those errors are printed.
* bfd.c (MAX_ARGS): Define, use throughout.
(print_func): New typedef.
(_bfd_doprnt): Add new print param. Replace calls to fprintf
with print.
(PRINT_TYPE): Similarly.
(error_handler_fprintf): Renamed from error_handler_internal.
Use _bfd_get_error_program_name. Add fprintf arg. Move code
setting up args..
(_bfd_doprnt_scan): ..to here. Add ap param.
(struct buf_stream): New.
(err_sprintf): New function.
(error_handler_bfd): New static variable.
(error_handler_sprintf): New function.
(_bfd_set_error_handler_caching): New function.
(_bfd_get_error_program_name): New function.
* elfcode.h (elf_swap_shdr_in): Use _bfd_error_handler in
warning messages.
(elf_object_p): Likewise.
* format.c (print_warnmsg): New function.
(clear_warnmsg): Rewrite.
(null_error_handler): New function.
(bfd_check_format_matches): Ignore warnings from recursive calls
checking first element of an archive. Use caching error handler
otherwise. Print warnings on successful match, or when only one
target has emitted warnings/errors.
* peicode.h (pe_bfd_object_p): Use _bfd_error_handler in
warning messages.
* targets.c (per_xvec_warn): Change type of array elements.
(struct per_xvec_message): New.
(_bfd_per_xvec_warn): Rewrite.
* Makefile.am (LIBBFD_H_FILES): Add bfd.c.
* Makefile.in: Regenerate.
* bfd-in2.h: Regenerate.
* libbfd.h: Regenerate.
syms.c bfd.c archive.c corefile.c targets.c format.c \
linker.c simple.c compress.c
BFD64_H_FILES = archive64.c
-LIBBFD_H_FILES = libbfd-in.h libbfd.c bfdio.c bfdwin.c \
+LIBBFD_H_FILES = libbfd-in.h libbfd.c bfd.c bfdio.c bfdwin.c \
cache.c reloc.c section.c targets.c archures.c linker.c
LIBCOFF_H_FILES = libcoff-in.h coffcode.h
linker.c simple.c compress.c
BFD64_H_FILES = archive64.c
-LIBBFD_H_FILES = libbfd-in.h libbfd.c bfdio.c bfdwin.c \
+LIBBFD_H_FILES = libbfd-in.h libbfd.c bfd.c bfdio.c bfdwin.c \
cache.c reloc.c section.c targets.c archures.c linker.c
LIBCOFF_H_FILES = libcoff-in.h coffcode.h
return abfd->xvec->keep_unused_section_symbols;
}
+/* Cached _bfd_check_format messages are put in this. */
+struct per_xvec_message
+{
+ struct per_xvec_message *next;
+ char message[];
+};
+
bool bfd_set_default_target (const char *name);
const bfd_target *bfd_find_target (const char *target_name, bfd *abfd);
} type;
};
+/* Maximum number of _bfd_error_handler args. Don't increase this
+ without changing the code handling positional parameters. */
+#define MAX_ARGS 9
+
/* This macro and _bfd_doprnt taken from libiberty _doprnt.c, tidied a
little and extended to handle '%pA', '%pB' and positional parameters. */
do \
{ \
TYPE value = (TYPE) args[arg_no].FIELD; \
- result = fprintf (stream, specifier, value); \
+ result = print (stream, specifier, value); \
} while (0)
+typedef int (*print_func) (void *, const char *, ...);
+
static int
-_bfd_doprnt (FILE *stream, const char *format, union _bfd_doprnt_args *args)
+_bfd_doprnt (print_func print, void *stream, const char *format,
+ union _bfd_doprnt_args *args)
{
const char *ptr = format;
char specifier[128];
/* While we have regular characters, print them. */
char *end = strchr (ptr, '%');
if (end != NULL)
- result = fprintf (stream, "%.*s", (int) (end - ptr), ptr);
+ result = print (stream, "%.*s", (int) (end - ptr), ptr);
else
- result = fprintf (stream, "%s", ptr);
+ result = print (stream, "%s", ptr);
ptr += result;
}
else if (ptr[1] == '%')
sec)) != NULL)
group = ci->name;
if (group != NULL)
- result = fprintf (stream, "%s[%s]", sec->name, group);
+ result = print (stream, "%s[%s]", sec->name, group);
else
- result = fprintf (stream, "%s", sec->name);
+ result = print (stream, "%s", sec->name);
}
else if (*ptr == 'B')
{
abort ();
else if (abfd->my_archive
&& !bfd_is_thin_archive (abfd->my_archive))
- result = fprintf (stream, "%s(%s)",
- bfd_get_filename (abfd->my_archive),
- bfd_get_filename (abfd));
+ result = print (stream, "%s(%s)",
+ bfd_get_filename (abfd->my_archive),
+ bfd_get_filename (abfd));
else
- result = fprintf (stream, "%s", bfd_get_filename (abfd));
+ result = print (stream, "%s", bfd_get_filename (abfd));
}
else
PRINT_TYPE (void *, p);
/* First pass over FORMAT to gather ARGS. Returns number of args. */
static unsigned int
-_bfd_doprnt_scan (const char *format, union _bfd_doprnt_args *args)
+_bfd_doprnt_scan (const char *format, va_list ap, union _bfd_doprnt_args *args)
{
const char *ptr = format;
unsigned int arg_count = 0;
+ for (unsigned int i = 0; i < MAX_ARGS; i++)
+ args[i].type = Bad;
+
while (*ptr != '\0')
{
if (*ptr != '%')
arg_index = *ptr - '1';
ptr += 2;
}
- if (arg_index >= 9)
+ if (arg_index >= MAX_ARGS)
abort ();
args[arg_index].type = Int;
arg_count++;
arg_index = *ptr - '1';
ptr += 2;
}
- if (arg_index >= 9)
+ if (arg_index >= MAX_ARGS)
abort ();
args[arg_index].type = Int;
arg_count++;
abort();
}
- if (arg_no >= 9)
+ if (arg_no >= MAX_ARGS)
abort ();
args[arg_no].type = arg_type;
arg_count++;
}
}
- return arg_count;
-}
-
-static void
-error_handler_internal (const char *fmt, va_list ap)
-{
- unsigned int i, arg_count;
- union _bfd_doprnt_args args[9];
-
- for (i = 0; i < sizeof (args) / sizeof (args[0]); i++)
- args[i].type = Bad;
-
- arg_count = _bfd_doprnt_scan (fmt, args);
- for (i = 0; i < arg_count; i++)
+ for (unsigned int i = 0; i < arg_count; i++)
{
switch (args[i].type)
{
}
}
+ return arg_count;
+}
+
+/* The standard error handler that prints to stderr. */
+
+static void
+error_handler_fprintf (const char *fmt, va_list ap)
+{
+ union _bfd_doprnt_args args[MAX_ARGS];
+
+ _bfd_doprnt_scan (fmt, ap, args);
+
/* PR 4992: Don't interrupt output being sent to stdout. */
fflush (stdout);
- if (_bfd_error_program_name != NULL)
- fprintf (stderr, "%s: ", _bfd_error_program_name);
- else
- fprintf (stderr, "BFD: ");
+ fprintf (stderr, "%s: ", _bfd_get_error_program_name ());
- _bfd_doprnt (stderr, fmt, args);
+ _bfd_doprnt ((print_func) fprintf, stderr, fmt, args);
/* On AIX, putc is implemented as a macro that triggers a -Wunused-value
warning, so use the fputc function to avoid it. */
fflush (stderr);
}
+/* Control printing to a string buffer. */
+struct buf_stream
+{
+ char *ptr;
+ int left;
+};
+
+/* An fprintf like function that instead prints to a string buffer. */
+
+static int
+err_sprintf (void *stream, const char *fmt, ...)
+{
+ struct buf_stream *s = stream;
+ va_list ap;
+
+ va_start (ap, fmt);
+ int total = vsnprintf (s->ptr, s->left, fmt, ap);
+ va_end (ap);
+ if (total < 0)
+ ;
+ else if (total > s->left)
+ {
+ s->ptr += s->left;
+ s->left = 0;
+ }
+ else
+ {
+ s->ptr += total;
+ s->left -= total;
+ }
+ return total;
+}
+
+/* Communicate the bfd processed by bfd_check_format_matches to the
+ error handling function error_handler_sprintf. */
+
+static bfd *error_handler_bfd;
+
+/* An error handler that prints to a string, then dups that string to
+ a per-xvec cache. */
+
+static void
+error_handler_sprintf (const char *fmt, va_list ap)
+{
+ union _bfd_doprnt_args args[MAX_ARGS];
+ char error_buf[1024];
+ struct buf_stream error_stream;
+
+ _bfd_doprnt_scan (fmt, ap, args);
+
+ error_stream.ptr = error_buf;
+ error_stream.left = sizeof (error_buf);
+ _bfd_doprnt (err_sprintf, &error_stream, fmt, args);
+
+ size_t len = error_stream.ptr - error_buf;
+ struct per_xvec_message **warn
+ = _bfd_per_xvec_warn (error_handler_bfd->xvec, len + 1);
+ if (*warn)
+ {
+ memcpy ((*warn)->message, error_buf, len);
+ (*warn)->message[len] = 0;
+ }
+}
+
/* This is a function pointer to the routine which should handle BFD
error messages. It is called when a BFD routine encounters an
error for which it wants to print a message. Going through a
function pointer permits a program linked against BFD to intercept
the messages and deal with them itself. */
-static bfd_error_handler_type _bfd_error_internal = error_handler_internal;
+static bfd_error_handler_type _bfd_error_internal = error_handler_fprintf;
/*
FUNCTION
return pold;
}
+/*
+INTERNAL_FUNCTION
+ _bfd_set_error_handler_caching
+
+SYNOPSIS
+ bfd_error_handler_type _bfd_set_error_handler_caching (bfd *);
+
+DESCRIPTION
+ Set the BFD error handler function to one that stores messages
+ to the per_xvec_warn array. Returns the previous function.
+*/
+
+bfd_error_handler_type
+_bfd_set_error_handler_caching (bfd *abfd)
+{
+ error_handler_bfd = abfd;
+ return bfd_set_error_handler (error_handler_sprintf);
+}
+
/*
FUNCTION
bfd_set_error_program_name
_bfd_error_program_name = name;
}
+/*
+INTERNAL_FUNCTION
+ _bfd_get_error_program_name
+
+SYNOPSIS
+ const char *_bfd_get_error_program_name (void);
+
+DESCRIPTION
+ Get the program name used when printing a BFD error.
+*/
+
+const char *
+_bfd_get_error_program_name (void)
+{
+ if (_bfd_error_program_name != NULL)
+ return _bfd_error_program_name;
+ return "BFD";
+}
+
/*
SUBSECTION
BFD assert handler
|| dst->sh_size > filesize - dst->sh_offset)
&& !abfd->read_only)
{
- const char **warn = _bfd_per_xvec_warn (abfd->xvec);
- *warn = _("warning: %pB has a section extending past end of file");
+ _bfd_error_handler (_("warning: %pB has a section "
+ "extending past end of file"), abfd);
abfd->read_only = 1;
}
}
i_ehdrp->e_shstrndx = SHN_UNDEF;
if (!abfd->read_only)
{
- const char **warn = _bfd_per_xvec_warn (abfd->xvec);
- *warn = _("warning: %pB has a corrupt string table index");
+ _bfd_error_handler
+ (_("warning: %pB has a corrupt string table index"), abfd);
abfd->read_only = 1;
}
}
i_phdr->p_align &= -i_phdr->p_align;
if (!abfd->read_only)
{
- const char **warn = _bfd_per_xvec_warn (abfd->xvec);
- *warn = _("warning: %pB has a program header "
- "with invalid alignment");
+ _bfd_error_handler (_("warning: %pB has a program header "
+ "with invalid alignment"), abfd);
abfd->read_only = 1;
}
}
}
static void
-clear_warnmsg (const bfd_target *targ)
+print_warnmsg (struct per_xvec_message **list)
+{
+ fflush (stdout);
+ fprintf (stderr, "%s: ", _bfd_get_error_program_name ());
+
+ for (struct per_xvec_message *warn = *list; warn; warn = warn->next)
+ {
+ fputs (warn->message, stderr);
+ fputc ('\n', stderr);
+ }
+ fflush (stderr);
+}
+
+static void
+clear_warnmsg (struct per_xvec_message **list)
+{
+ struct per_xvec_message *warn = *list;
+ while (warn)
+ {
+ struct per_xvec_message *next = warn->next;
+ free (warn);
+ warn = next;
+ }
+ *list = NULL;
+}
+
+static void
+null_error_handler (const char *fmt ATTRIBUTE_UNUSED,
+ va_list ap ATTRIBUTE_UNUSED)
{
- const char **warn = _bfd_per_xvec_warn (targ);
- *warn = NULL;
}
/*
unsigned int initial_section_id = _bfd_section_id;
struct bfd_preserve preserve, preserve_match;
bfd_cleanup cleanup = NULL;
+ bfd_error_handler_type orig_error_handler;
+ static int in_check_format;
if (matching != NULL)
*matching = NULL;
abfd->format = format;
save_targ = abfd->xvec;
+ /* Don't report errors on recursive calls checking the first element
+ of an archive. */
+ if (in_check_format)
+ orig_error_handler = bfd_set_error_handler (null_error_handler);
+ else
+ orig_error_handler = _bfd_set_error_handler_caching (abfd);
+ ++in_check_format;
+
preserve_match.marker = NULL;
if (!bfd_preserve_save (abfd, &preserve, NULL))
goto err_ret;
if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) /* rewind! */
goto err_ret;
- clear_warnmsg (abfd->xvec);
cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
if (cleanup)
if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
goto err_ret;
- clear_warnmsg (abfd->xvec);
cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
if (cleanup)
{
if (preserve_match.marker != NULL)
bfd_preserve_finish (abfd, &preserve_match);
bfd_preserve_finish (abfd, &preserve);
+ bfd_set_error_handler (orig_error_handler);
- if (!abfd->my_archive)
- {
- const char **warn = _bfd_per_xvec_warn (abfd->xvec);
- if (*warn)
- _bfd_error_handler (*warn, abfd);
- }
+ struct per_xvec_message **list = _bfd_per_xvec_warn (abfd->xvec, 0);
+ if (*list)
+ print_warnmsg (list);
+ list = _bfd_per_xvec_warn (NULL, 0);
+ for (size_t i = 0; i < _bfd_target_vector_entries + 1; i++)
+ clear_warnmsg (list++);
+ --in_check_format;
/* File position has moved, BTW. */
return true;
abfd->xvec = save_targ;
abfd->format = bfd_unknown;
free (matching_vector);
- if (preserve_match.marker != NULL)
- bfd_preserve_finish (abfd, &preserve_match);
- bfd_preserve_restore (abfd, &preserve);
- return false;
+ goto out;
}
/* Restore original target type and format. */
free (matching_vector);
if (cleanup)
cleanup (abfd);
+ out:
if (preserve_match.marker != NULL)
bfd_preserve_finish (abfd, &preserve_match);
bfd_preserve_restore (abfd, &preserve);
+ bfd_set_error_handler (orig_error_handler);
+ struct per_xvec_message **list = _bfd_per_xvec_warn (NULL, 0);
+ struct per_xvec_message **one = NULL;
+ for (size_t i = 0; i < _bfd_target_vector_entries + 1; i++)
+ {
+ if (list[i])
+ {
+ if (!one)
+ one = list + i;
+ else
+ {
+ one = NULL;
+ break;
+ }
+ }
+ }
+ if (one)
+ print_warnmsg (one);
+ for (size_t i = 0; i < _bfd_target_vector_entries + 1; i++)
+ clear_warnmsg (list++);
+ --in_check_format;
return false;
}
/* DO NOT EDIT! -*- buffer-read-only: t -*- This file is automatically
- generated from "libbfd-in.h", "libbfd.c", "bfdio.c", "bfdwin.c",
- "cache.c", "reloc.c", "section.c", "targets.c", "archures.c"
+ generated from "libbfd-in.h", "libbfd.c", "bfd.c", "bfdio.c",
+ "bfdwin.c", "cache.c", "reloc.c", "section.c", "targets.c", "archures.c"
and "linker.c".
Run "make headers" in your build bfd/ to regenerate. */
unsigned int bfd_log2 (bfd_vma x);
+/* Extracted from bfd.c. */
+bfd_error_handler_type _bfd_set_error_handler_caching (bfd *);
+
+const char *_bfd_get_error_program_name (void);
+
/* Extracted from bfdio.c. */
struct bfd_iovec
{
bool _bfd_section_size_insane (bfd *abfd, asection *sec);
/* Extracted from targets.c. */
-const char **_bfd_per_xvec_warn (const bfd_target *);
+struct per_xvec_message **_bfd_per_xvec_warn (const bfd_target *, size_t);
/* Extracted from archures.c. */
extern const bfd_arch_info_type bfd_default_arch_struct;
if ((a->SectionAlignment & -a->SectionAlignment) != a->SectionAlignment
|| a->SectionAlignment >= 0x80000000)
{
- const char **warn = _bfd_per_xvec_warn (abfd->xvec);
- *warn = _("%pB: adjusting invalid SectionAlignment");
+ _bfd_error_handler (_("%pB: adjusting invalid SectionAlignment"),
+ abfd);
a->SectionAlignment &= -a->SectionAlignment;
if (a->SectionAlignment >= 0x80000000)
a->SectionAlignment = 0x40000000;
if ((a->FileAlignment & -a->FileAlignment) != a->FileAlignment
|| a->FileAlignment > a->SectionAlignment)
{
- const char **warn = _bfd_per_xvec_warn (abfd->xvec);
- *warn = _("%pB: adjusting invalid FileAlignment");
+ _bfd_error_handler (_("%pB: adjusting invalid FileAlignment"),
+ abfd);
a->FileAlignment &= -a->FileAlignment;
if (a->FileAlignment > a->SectionAlignment)
a->FileAlignment = a->SectionAlignment;
}
if (a->NumberOfRvaAndSizes > IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
- {
- const char **warn = _bfd_per_xvec_warn (abfd->xvec);
- *warn = _("%pB: invalid NumberOfRvaAndSizes");
- }
+ _bfd_error_handler (_("%pB: invalid NumberOfRvaAndSizes"), abfd);
}
result = coff_real_object_p (abfd, internal_f.f_nscns, &internal_f,
const size_t _bfd_target_vector_entries = ARRAY_SIZE (_bfd_target_vector);
/* A place to stash a warning from _bfd_check_format. */
-static const char *per_xvec_warn[ARRAY_SIZE (_bfd_target_vector) + 1];
+static struct per_xvec_message *per_xvec_warn[ARRAY_SIZE (_bfd_target_vector)
+ + 1];
\f
/* This array maps configuration triplets onto BFD vectors. */
};
/*
+CODE_FRAGMENT
+.{* Cached _bfd_check_format messages are put in this. *}
+.struct per_xvec_message
+.{
+. struct per_xvec_message *next;
+. char message[];
+.};
+.
INTERNAL_FUNCTION
_bfd_per_xvec_warn
SYNOPSIS
- const char **_bfd_per_xvec_warn (const bfd_target *);
+ struct per_xvec_message **_bfd_per_xvec_warn (const bfd_target *, size_t);
DESCRIPTION
Return a location for the given target xvec to use for
- warnings specific to that target.
+ warnings specific to that target. If TARG is NULL, returns
+ the array of per_xvec_message pointers, otherwise if ALLOC is
+ zero, returns a pointer to a pointer to the list of messages
+ for TARG, otherwise (both TARG and ALLOC non-zero), allocates
+ a new per_xvec_message with space for a string of ALLOC
+ bytes and returns a pointer to a pointer to it. May return a
+ pointer to a NULL pointer on allocation failure.
*/
-const char **
-_bfd_per_xvec_warn (const bfd_target *targ)
+struct per_xvec_message **
+_bfd_per_xvec_warn (const bfd_target *targ, size_t alloc)
{
size_t idx;
+ if (!targ)
+ return per_xvec_warn;
for (idx = 0; idx < ARRAY_SIZE (_bfd_target_vector); idx++)
if (_bfd_target_vector[idx] == targ)
break;
- return per_xvec_warn + idx;
+ struct per_xvec_message **m = per_xvec_warn + idx;
+ if (!alloc)
+ return m;
+ int count = 0;
+ while (*m)
+ {
+ m = &(*m)->next;
+ count++;
+ }
+ /* Anti-fuzzer measure. Don't cache more than 5 messages. */
+ if (count < 5)
+ {
+ *m = bfd_malloc (sizeof (**m) + alloc);
+ if (*m != NULL)
+ (*m)->next = NULL;
+ }
+ return m;
}
/* Find a target vector, given a name or configuration triplet. */