mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-27 04:52:05 +08:00
3d16b64e28
This is embarrassing. The whole point of CTF is that it remains intact even after a binary is stripped, providing a compact mapping from symbols to types for everything in the externally-visible interface of an ELF object: it has connections to the symbol table for that purpose, and to the string table to avoid duplicating symbol names. So it's a shame that the hooks I implemented last year served to hook it up to the .symtab and .strtab, which obviously disappear on strip, leaving any accompanying the CTF dict containing references to strings (and, soon, symbols) which don't exist any more because their containing strtab has been vaporized. The original Solaris design used .dynsym and .dynstr (well, actually, .ldynsym, which has more symbols) which do not disappear. So should we. Thankfully the work we did before serves as guide rails, and adjusting things to use the .dynstr and .dynsym was fast and easy. The only annoyance is that the dynsym is assembled inside elflink.c in a fairly piecemeal fashion, so that the easiest way to get the symbols out was to hook in before every call to swap_symbol_out (we also leave in a hook in front of symbol additions to the .symtab because it seems plausible that we might want to hook them in future too: for now that hook is unused). We adjust things so that rather than being offered a whole hash table of symbols at once, libctf is now given symbols one at a time, with st_name indexes already resolved and pointing at their final .dynstr offsets: it's now up to libctf to resolve these to names as needed using the strtab info we pass it separately. Some bits might be contentious. The ctf_new_dynstr callback takes an elf_internal_sym, and this remains an elf_internal_sym right down through the generic emulation layers into ldelfgen. This is no worse than the elf_sym_strtab we used to pass down, but in the future when we gain non-ELF CTF symtab support we might want to lower the elf_internal_sym to some other representation (perhaps a ctf_link_symbol) in bfd or in ldlang_ctf_new_dynsym. We rename the 'apply_strsym' hooks to 'acquire_strings' instead, becuse they no longer have anything to do with symbols. There are some API changes to pieces of API which are technically public but actually totally unused by anything and/or unused by anything but ld so they can change freely: the ctf_link_symbol gains new fields to allow symbol names to be given as strtab offsets as well as strings, and a symidx so that the symbol index can be passed in. ctf_link_shuffle_syms loses its callback parameter: the idea now is that linkers call the new ctf_link_add_linker_symbol for every symbol in .dynsym, feed in all the strtab entries with ctf_link_add_strtab, and then a call to ctf_link_shuffle_syms will apply both and arrange to use them to reorder the CTF symtab at CTF serialization time (which is coming in the next commit). Inside libctf we have a new preamble flag CTF_F_DYNSTR which is always set in v3-format CTF dicts from this commit forwards: CTF dicts without this flag are associated with .strtab like they used to be, so that old dicts' external strings don't turn to garbage when loaded by new libctf. Dicts with this flag are associated with .dynstr and .dynsym instead. (The flag is not the next in sequence because this commit was written quite late: the missing flags will be filled in by the next commit.) Tests forthcoming in a later commit in this series. bfd/ChangeLog 2020-11-20 Nick Alcock <nick.alcock@oracle.com> * elflink.c (elf_finalize_dynstr): Call examine_strtab after dynstr finalization. (elf_link_swap_symbols_out): Don't call it here. Call ctf_new_symbol before swap_symbol_out. (elf_link_output_extsym): Call ctf_new_dynsym before swap_symbol_out. (bfd_elf_final_link): Likewise. * elf.c (swap_out_syms): Pass in bfd_link_info. Call ctf_new_symbol before swap_symbol_out. (_bfd_elf_compute_section_file_positions): Adjust. binutils/ChangeLog 2020-11-20 Nick Alcock <nick.alcock@oracle.com> * readelf.c (dump_section_as_ctf): Use .dynsym and .dynstr, not .symtab and .strtab. include/ChangeLog 2020-11-20 Nick Alcock <nick.alcock@oracle.com> * bfdlink.h (struct elf_sym_strtab): Replace with... (struct elf_internal_sym): ... this. (struct bfd_link_callbacks) <examine_strtab>: Take only a symstrtab argument. <ctf_new_symbol>: New. <ctf_new_dynsym>: Likewise. * ctf-api.h (struct ctf_link_sym) <st_symidx>: New. <st_nameidx>: Likewise. <st_nameidx_set>: Likewise. (ctf_link_iter_symbol_f): Removed. (ctf_link_shuffle_syms): Remove most parameters, just takes a ctf_dict_t now. (ctf_link_add_linker_symbol): New, split from ctf_link_shuffle_syms. * ctf.h (CTF_F_DYNSTR): New. (CTF_F_MAX): Adjust. ld/ChangeLog 2020-11-20 Nick Alcock <nick.alcock@oracle.com> * ldelfgen.c (struct ctf_strsym_iter_cb_arg): Rename to... (struct ctf_strtab_iter_cb_arg): ... this, changing fields: <syms>: Remove. <symcount>: Remove. <symstrtab>: Rename to... <strtab>: ... this. (ldelf_ctf_strtab_iter_cb): Adjust. (ldelf_ctf_symbols_iter_cb): Remove. (ldelf_new_dynsym_for_ctf): New, tell libctf about a single symbol. (ldelf_examine_strtab_for_ctf): Rename to... (ldelf_acquire_strings_for_ctf): ... this, only doing the strtab portion and not symbols. * ldelfgen.h: Adjust declarations accordingly. * ldemul.c (ldemul_examine_strtab_for_ctf): Rename to... (ldemul_acquire_strings_for_ctf): ... this. (ldemul_new_dynsym_for_ctf): New. * ldemul.h: Adjust declarations accordingly. * ldlang.c (ldlang_ctf_apply_strsym): Rename to... (ldlang_ctf_acquire_strings): ... this. (ldlang_ctf_new_dynsym): New. (lang_write_ctf): Call ldemul_new_dynsym_for_ctf with NULL to do the actual symbol shuffle. * ldlang.h (struct elf_strtab_hash): Adjust accordingly. * ldmain.c (bfd_link_callbacks): Wire up new/renamed callbacks. libctf/ChangeLog 2020-11-20 Nick Alcock <nick.alcock@oracle.com> * ctf-link.c (ctf_link_shuffle_syms): Adjust. (ctf_link_add_linker_symbol): New, unimplemented stub. * libctf.ver: Add it. * ctf-create.c (ctf_serialize): Set CTF_F_DYNSTR on newly-serialized dicts. * ctf-open-bfd.c (ctf_bfdopen_ctfsect): Check for the flag: open the symtab/strtab if not present, dynsym/dynstr otherwise. * ctf-archive.c (ctf_arc_bufpreamble): New, get the preamble from some arbitrary member of a CTF archive. * ctf-impl.h (ctf_arc_bufpreamble): Declare it.
373 lines
10 KiB
C
373 lines
10 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;
|
|
size_t symcount;
|
|
Elf_Internal_Sym *isymbuf;
|
|
bfd_byte *symtab = NULL;
|
|
const char *symtab_name;
|
|
const char *strtab = NULL;
|
|
const char *strtab_name;
|
|
size_t strsize;
|
|
const ctf_preamble_t *preamble;
|
|
|
|
if (ctfsect->cts_data == NULL)
|
|
{
|
|
bfderrstr = N_("CTF section is NULL");
|
|
goto err;
|
|
}
|
|
preamble = ctf_arc_bufpreamble (ctfsect);
|
|
|
|
if (preamble->ctp_flags & CTF_F_DYNSTR)
|
|
{
|
|
symhdr = &elf_tdata (abfd)->dynsymtab_hdr;
|
|
strtab_name = ".dynstr";
|
|
symtab_name = ".dynsym";
|
|
}
|
|
else
|
|
{
|
|
symhdr = &elf_tdata (abfd)->symtab_hdr;
|
|
strtab_name = ".strtab";
|
|
symtab_name = ".symtab";
|
|
}
|
|
|
|
/* 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 or .dynstr by name. */
|
|
{
|
|
bfd_byte *str_bcontents;
|
|
asection *str_asect;
|
|
|
|
if ((str_asect = bfd_get_section_by_name (abfd, strtab_name)) != 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_name;
|
|
strsect.cts_size = strsize;
|
|
strsectp = &strsect;
|
|
}
|
|
|
|
if (symtab)
|
|
{
|
|
assert (symhdr->sh_entsize == get_elf_backend_data (abfd)->s->sizeof_sym);
|
|
symsect.cts_name = symtab_name;
|
|
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 dicts. The file can be an ELF file, a file
|
|
containing raw CTF, 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_dict_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 dict. 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);
|
|
}
|