binutils-gdb/libctf/ctf-open-bfd.c
Nick Alcock 926c9e7665 libctf, binutils, include, ld: gettextize and improve error handling
This commit follows on from the earlier commit "libctf, ld, binutils:
add textual error/warning reporting for libctf" and converts every error
in libctf that was reported using ctf_dprintf to use ctf_err_warn
instead, gettextizing them in the process, using N_() where necessary to
avoid doing gettext calls unless an error message is actually generated,
and rephrasing some error messages for ease of translation.

This requires a slight change in the ctf_errwarning_next API: this API
is public but has not been in a release yet, so can still change freely.
The problem is that many errors are emitted at open time (whether
opening of a CTF dict, or opening of a CTF archive): the former of these
throws away its incompletely-initialized ctf_file_t rather than return
it, and the latter has no ctf_file_t at all. So errors and warnings
emitted at open time cannot be stored in the ctf_file_t, and have to go
elsewhere.

We put them in a static local in ctf-subr.c (which is not very
thread-safe: a later commit will improve things here): ctf_err_warn with
a NULL fp adds to this list, and the public interface
ctf_errwarning_next with a NULL fp retrieves from it.

We need a slight exception from the usual iterator rules in this case:
with a NULL fp, there is nowhere to store the ECTF_NEXT_END "error"
which signifies the end of iteration, so we add a new err parameter to
ctf_errwarning_next which is used to report such iteration-related
errors.  (If an fp is provided -- i.e., if not reporting open errors --
this is optional, but even if it's optional it's still an API change.
This is actually useful from a usability POV as well, since
ctf_errwarning_next is usually called when there's been an error, so
overwriting the error code with ECTF_NEXT_END is not very helpful!
So, unusually, ctf_errwarning_next now uses the passed fp for its
error code *only* if no errp pointer is passed in, and leaves it
untouched otherwise.)

ld, objdump and readelf are adapted to call ctf_errwarning_next with a
NULL fp to report open errors where appropriate.

The ctf_err_warn API also has to change, gaining a new error-number
parameter which is used to add the error message corresponding to that
error number into the debug stream when LIBCTF_DEBUG is enabled:
changing this API is easy at this point since we are already touching
all existing calls to gettextize them.  We need this because the debug
stream should contain the errno's message, but the error reported in the
error/warning stream should *not*, because the caller will probably
report it themselves at failure time regardless, and reporting it in
every error message that leads up to it leads to a ridiculous chattering
on failure, which is likely to end up as ridiculous chattering on stderr
(trimmed a bit):

CTF error: `ld/testsuite/ld-ctf/A.c (0): lookup failure for type 3: flags 1: The parent CTF dictionary is unavailable'
CTF error: `ld/testsuite/ld-ctf/A.c (0): struct/union member type hashing error during type hashing for type 80000001, kind 6: The parent CTF dictionary is unavailable'
CTF error: `deduplicating link variable emission failed for ld/testsuite/ld-ctf/A.c: The parent CTF dictionary is unavailable'
ld/.libs/lt-ld-new: warning: CTF linking failed; output will have no CTF section: `The parent CTF dictionary is unavailable'

We only need to be told that the parent CTF dictionary is unavailable
*once*, not over and over again!

errmsgs are still emitted on warning generation, because warnings do not
usually lead to a failure propagated up to the caller and reported
there.

Debug-stream messages are not translated.  If translation is turned on,
there will be a mixture of English and translated messages in the debug
stream, but rather that than burden the translators with debug-only
output.

binutils/ChangeLog
2020-08-27  Nick Alcock  <nick.alcock@oracle.com>

	* objdump.c (dump_ctf_archive_member): Move error-
	reporting...
	(dump_ctf_errs): ... into this separate function.
	(dump_ctf): Call it on open errors.
	* readelf.c (dump_ctf_archive_member): Move error-
	reporting...
	(dump_ctf_errs): ... into this separate function.  Support
	calls with NULL fp. Adjust for new err parameter to
	ctf_errwarning_next.
	(dump_section_as_ctf): Call it on open errors.

include/ChangeLog
2020-08-27  Nick Alcock  <nick.alcock@oracle.com>

	* ctf-api.h (ctf_errwarning_next): New err parameter.

ld/ChangeLog
2020-08-27  Nick Alcock  <nick.alcock@oracle.com>

	* ldlang.c (lang_ctf_errs_warnings): Support calls with NULL fp.
	Adjust for new err parameter to ctf_errwarning_next.  Only
	check for assertion failures when fp is non-NULL.
	(ldlang_open_ctf): Call it on open errors.
	* testsuite/ld-ctf/ctf.exp: Always use the C locale to avoid
	breaking the diags tests.

libctf/ChangeLog
2020-08-27  Nick Alcock  <nick.alcock@oracle.com>

	* ctf-subr.c (open_errors): New list.
	(ctf_err_warn): Calls with NULL fp append to open_errors.  Add err
	parameter, and use it to decorate the debug stream with errmsgs.
	(ctf_err_warn_to_open): Splice errors from a CTF dict into the
	open_errors.
	(ctf_errwarning_next): Calls with NULL fp report from open_errors.
	New err param to report iteration errors (including end-of-iteration)
	when fp is NULL.
	(ctf_assert_fail_internal): Adjust ctf_err_warn call for new err
	parameter: gettextize.
	* ctf-impl.h (ctfo_get_vbytes): Add ctf_file_t parameter.
	(LCTF_VBYTES): Adjust.
	(ctf_err_warn_to_open): New.
	(ctf_err_warn): Adjust.
	(ctf_bundle): Used in only one place: move...
	* ctf-create.c: ... here.
	(enumcmp): Use ctf_err_warn, not ctf_dprintf, passing the err number
	down as needed.  Don't emit the errmsg.  Gettextize.
	(membcmp): Likewise.
	(ctf_add_type_internal): Likewise.
	(ctf_write_mem): Likewise.
	(ctf_compress_write): Likewise.  Report errors writing the header or
	body.
	(ctf_write): Likewise.
	* ctf-archive.c (ctf_arc_write_fd): Use ctf_err_warn, not
	ctf_dprintf, and gettextize, as above.
	(ctf_arc_write): Likewise.
	(ctf_arc_bufopen): Likewise.
	(ctf_arc_open_internal): Likewise.
	* ctf-labels.c (ctf_label_iter): Likewise.
	* ctf-open-bfd.c (ctf_bfdclose): Likewise.
	(ctf_bfdopen): Likewise.
	(ctf_bfdopen_ctfsect): Likewise.
	(ctf_fdopen): Likewise.
	* ctf-string.c (ctf_str_write_strtab): Likewise.
	* ctf-types.c (ctf_type_resolve): Likewise.
	* ctf-open.c (get_vbytes_common): Likewise. Pass down the ctf dict.
	(get_vbytes_v1): Pass down the ctf dict.
	(get_vbytes_v2): Likewise.
	(flip_ctf): Likewise.
	(flip_types): Likewise. Use ctf_err_warn, not ctf_dprintf, and
	gettextize, as above.
	(upgrade_types_v1): Adjust calls.
	(init_types): Use ctf_err_warn, not ctf_dprintf, as above.
	(ctf_bufopen_internal): Likewise. Adjust calls. Transplant errors
	emitted into individual dicts into the open errors if this turns
	out to be a failed open in the end.
	* ctf-dump.c (ctf_dump_format_type): Adjust ctf_err_warn for new err
	argument.  Gettextize.  Don't emit the errmsg.
	(ctf_dump_funcs): Likewise.  Collapse err label into its only case.
	(ctf_dump_type): Likewise.
	* ctf-link.c (ctf_create_per_cu): Adjust ctf_err_warn for new err
	argument.  Gettextize.  Don't emit the errmsg.
	(ctf_link_one_type): Likewise.
	(ctf_link_lazy_open): Likewise.
	(ctf_link_one_input_archive): Likewise.
	(ctf_link_deduplicating_count_inputs): Likewise.
	(ctf_link_deduplicating_open_inputs): Likewise.
	(ctf_link_deduplicating_close_inputs): Likewise.
	(ctf_link_deduplicating): Likewise.
	(ctf_link): Likewise.
	(ctf_link_deduplicating_per_cu): Likewise. Add some missed
	ctf_set_errnos to obscure error cases.
	* ctf-dedup.c (ctf_dedup_rhash_type): Adjust ctf_err_warn for new
	err argument.  Gettextize.  Don't emit the errmsg.
	(ctf_dedup_populate_mappings): Likewise.
	(ctf_dedup_detect_name_ambiguity): Likewise.
	(ctf_dedup_init): Likewise.
	(ctf_dedup_multiple_input_dicts): Likewise.
	(ctf_dedup_conflictify_unshared): Likewise.
	(ctf_dedup): Likewise.
	(ctf_dedup_rwalk_one_output_mapping): Likewise.
	(ctf_dedup_id_to_target): Likewise.
	(ctf_dedup_emit_type): Likewise.
	(ctf_dedup_emit_struct_members): Likewise.
	(ctf_dedup_populate_type_mapping): Likewise.
	(ctf_dedup_populate_type_mappings): Likewise.
	(ctf_dedup_emit): Likewise.
	(ctf_dedup_hash_type): Likewise. Fix a bit of messed-up error
	status setting.
	(ctf_dedup_rwalk_one_output_mapping): Likewise. Don't hide
	unknown-type-kind messages (which signify file corruption).
2020-08-27 13:15:43 +01:00

349 lines
9.7 KiB
C

/* Opening CTF files with BFD.
Copyright (C) 2019-2020 Free Software Foundation, Inc.
This file is part of libctf.
libctf is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not see
<http://www.gnu.org/licenses/>. */
#include <ctf-impl.h>
#include <stddef.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <elf.h>
#include <bfd.h>
#include "swap.h"
#include "ctf-endian.h"
#include "elf-bfd.h"
/* Free the BFD bits of a CTF file on ctf_arc_close(). */
static void
ctf_bfdclose (struct ctf_archive_internal *arci)
{
if (arci->ctfi_abfd != NULL)
if (!bfd_close_all_done (arci->ctfi_abfd))
ctf_err_warn (NULL, 0, 0, _("cannot close BFD: %s"),
bfd_errmsg (bfd_get_error ()));
}
/* Open a CTF file given the specified BFD. */
ctf_archive_t *
ctf_bfdopen (struct bfd *abfd, int *errp)
{
ctf_archive_t *arc;
asection *ctf_asect;
bfd_byte *contents;
ctf_sect_t ctfsect;
libctf_init_debug();
if ((ctf_asect = bfd_get_section_by_name (abfd, _CTF_SECTION)) == NULL)
{
return (ctf_set_open_errno (errp, ECTF_NOCTFDATA));
}
if (!bfd_malloc_and_get_section (abfd, ctf_asect, &contents))
{
ctf_err_warn (NULL, 0, 0, _("ctf_bfdopen(): cannot malloc "
"CTF section: %s"),
bfd_errmsg (bfd_get_error ()));
return (ctf_set_open_errno (errp, ECTF_FMT));
}
ctfsect.cts_name = _CTF_SECTION;
ctfsect.cts_entsize = 1;
ctfsect.cts_size = bfd_section_size (ctf_asect);
ctfsect.cts_data = contents;
if ((arc = ctf_bfdopen_ctfsect (abfd, &ctfsect, errp)) != NULL)
{
/* This frees the cts_data later. */
arc->ctfi_data = (void *) ctfsect.cts_data;
return arc;
}
free (contents);
return NULL; /* errno is set for us. */
}
/* Open a CTF file given the specified BFD and CTF section (which may contain a
CTF archive or a file). */
ctf_archive_t *
ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
const ctf_sect_t *ctfsect, int *errp)
{
ctf_archive_t *arci;
ctf_sect_t *symsectp = NULL;
ctf_sect_t *strsectp = NULL;
const char *bfderrstr = NULL;
char *strtab_alloc = NULL;
#ifdef HAVE_BFD_ELF
ctf_sect_t symsect, strsect;
Elf_Internal_Shdr *symhdr = &elf_symtab_hdr (abfd);
size_t symcount;
Elf_Internal_Sym *isymbuf;
bfd_byte *symtab = NULL;
const char *strtab = NULL;
size_t strsize;
/* TODO: handle SYMTAB_SHNDX. */
/* Get the symtab, and the strtab associated with it. */
if (elf_tdata (abfd) && symhdr && symhdr->sh_size && symhdr->sh_entsize)
{
symcount = symhdr->sh_size / symhdr->sh_entsize;
if ((symtab = malloc (symhdr->sh_size)) == NULL)
{
bfderrstr = N_("cannot malloc symbol table");
goto err;
}
isymbuf = bfd_elf_get_elf_syms (abfd, symhdr, symcount, 0,
NULL, symtab, NULL);
free (isymbuf);
if (isymbuf == NULL)
{
bfderrstr = N_("cannot read symbol table");
goto err_free_sym;
}
if (elf_elfsections (abfd) != NULL
&& symhdr->sh_link < elf_numsections (abfd))
{
Elf_Internal_Shdr *strhdr = elf_elfsections (abfd)[symhdr->sh_link];
strsize = strhdr->sh_size;
if (strhdr->contents == NULL)
{
if ((strtab = bfd_elf_get_str_section (abfd, symhdr->sh_link)) == NULL)
{
bfderrstr = N_("cannot read string table");
goto err_free_sym;
}
}
else
strtab = (const char *) strhdr->contents;
}
}
else /* No symtab: just try getting .strtab by name. */
{
bfd_byte *str_bcontents;
asection *str_asect;
if ((str_asect = bfd_get_section_by_name (abfd, ".strtab")) != NULL)
{
if (bfd_malloc_and_get_section (abfd, str_asect, &str_bcontents))
{
strtab = (const char *) str_bcontents;
strtab_alloc = (char *) str_bcontents;
strsize = str_asect->size;
}
}
}
if (strtab)
{
/* The names here are more or less arbitrary, but there is no point
thrashing around digging the name out of the shstrtab given that we don't
use it for anything but debugging. */
strsect.cts_data = strtab;
strsect.cts_name = ".strtab";
strsect.cts_size = strsize;
strsectp = &strsect;
}
if (symtab)
{
assert (symhdr->sh_entsize == get_elf_backend_data (abfd)->s->sizeof_sym);
symsect.cts_name = ".symtab";
symsect.cts_entsize = symhdr->sh_entsize;
symsect.cts_size = symhdr->sh_size;
symsect.cts_data = symtab;
symsectp = &symsect;
}
#endif
arci = ctf_arc_bufopen (ctfsect, symsectp, strsectp, errp);
if (arci)
{
/* Request freeing of the symsect and possibly the strsect. */
arci->ctfi_free_symsect = 1;
if (strtab_alloc)
arci->ctfi_free_strsect = 1;
return arci;
}
#ifdef HAVE_BFD_ELF
err_free_sym:
free (symtab);
free (strtab_alloc);
#endif
err: _libctf_unused_;
if (bfderrstr)
{
ctf_err_warn (NULL, 0, 0, "ctf_bfdopen(): %s: %s", gettext (bfderrstr),
bfd_errmsg (bfd_get_error()));
ctf_set_open_errno (errp, ECTF_FMT);
}
return NULL;
}
/* Open the specified file descriptor and return a pointer to a CTF archive that
contains one or more CTF containers. The file can be an ELF file, a raw CTF
file, or a CTF archive. The caller is responsible for closing the file
descriptor when it is no longer needed. If this is an ELF file, TARGET, if
non-NULL, should be the name of a suitable BFD target. */
ctf_archive_t *
ctf_fdopen (int fd, const char *filename, const char *target, int *errp)
{
ctf_archive_t *arci;
bfd *abfd;
int nfd;
struct stat st;
ssize_t nbytes;
ctf_preamble_t ctfhdr;
uint64_t arc_magic;
memset (&ctfhdr, 0, sizeof (ctfhdr));
libctf_init_debug();
if (fstat (fd, &st) == -1)
return (ctf_set_open_errno (errp, errno));
if ((nbytes = ctf_pread (fd, &ctfhdr, sizeof (ctfhdr), 0)) <= 0)
return (ctf_set_open_errno (errp, nbytes < 0 ? errno : ECTF_FMT));
/* If we have read enough bytes to form a CTF header and the magic string
matches, in either endianness, attempt to interpret the file as raw
CTF. */
if ((size_t) nbytes >= sizeof (ctf_preamble_t)
&& (ctfhdr.ctp_magic == CTF_MAGIC
|| ctfhdr.ctp_magic == bswap_16 (CTF_MAGIC)))
{
ctf_file_t *fp = NULL;
void *data;
if ((data = ctf_mmap (st.st_size, 0, fd)) == NULL)
return (ctf_set_open_errno (errp, errno));
if ((fp = ctf_simple_open (data, (size_t) st.st_size, NULL, 0, 0,
NULL, 0, errp)) == NULL)
{
ctf_munmap (data, (size_t) st.st_size);
return NULL; /* errno is set for us. */
}
fp->ctf_data_mmapped = data;
fp->ctf_data_mmapped_len = (size_t) st.st_size;
return ctf_new_archive_internal (0, 1, NULL, fp, NULL, NULL, errp);
}
if ((nbytes = ctf_pread (fd, &arc_magic, sizeof (arc_magic), 0)) <= 0)
return (ctf_set_open_errno (errp, nbytes < 0 ? errno : ECTF_FMT));
if ((size_t) nbytes >= sizeof (uint64_t) && le64toh (arc_magic) == CTFA_MAGIC)
{
struct ctf_archive *arc;
if ((arc = ctf_arc_open_internal (filename, errp)) == NULL)
return NULL; /* errno is set for us. */
return ctf_new_archive_internal (1, 1, arc, NULL, NULL, NULL, errp);
}
/* Attempt to open the file with BFD. We must dup the fd first, since bfd
takes ownership of the passed fd. */
if ((nfd = dup (fd)) < 0)
return (ctf_set_open_errno (errp, errno));
if ((abfd = bfd_fdopenr (filename, target, nfd)) == NULL)
{
ctf_err_warn (NULL, 0, 0, _("cannot open BFD from %s: %s"),
filename ? filename : _("(unknown file)"),
bfd_errmsg (bfd_get_error ()));
return (ctf_set_open_errno (errp, ECTF_FMT));
}
bfd_set_cacheable (abfd, 1);
if (!bfd_check_format (abfd, bfd_object))
{
ctf_err_warn (NULL, 0, 0, _("BFD format problem in %s: %s"),
filename ? filename : _("(unknown file)"),
bfd_errmsg (bfd_get_error ()));
if (bfd_get_error() == bfd_error_file_ambiguously_recognized)
return (ctf_set_open_errno (errp, ECTF_BFD_AMBIGUOUS));
else
return (ctf_set_open_errno (errp, ECTF_FMT));
}
if ((arci = ctf_bfdopen (abfd, errp)) == NULL)
{
if (!bfd_close_all_done (abfd))
ctf_err_warn (NULL, 0, 0, _("cannot close BFD: %s"),
bfd_errmsg (bfd_get_error ()));
return NULL; /* errno is set for us. */
}
arci->ctfi_bfd_close = ctf_bfdclose;
arci->ctfi_abfd = abfd;
return arci;
}
/* Open the specified file and return a pointer to a CTF container. The file
can be either an ELF file or raw CTF file. This is just a convenient
wrapper around ctf_fdopen() for callers. */
ctf_archive_t *
ctf_open (const char *filename, const char *target, int *errp)
{
ctf_archive_t *arc;
int fd;
if ((fd = open (filename, O_RDONLY)) == -1)
{
if (errp != NULL)
*errp = errno;
return NULL;
}
arc = ctf_fdopen (fd, filename, target, errp);
(void) close (fd);
return arc;
}
/* Public entry point: open a CTF archive, or CTF file. Returns the archive, or
NULL and an error in *err. Despite the fact that this uses CTF archives, it
must be in this file to avoid dragging in BFD into non-BFD-using programs. */
ctf_archive_t *
ctf_arc_open (const char *filename, int *errp)
{
return ctf_open (filename, NULL, errp);
}