aboutsummaryrefslogtreecommitdiff
path: root/libctf
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2020-11-20 13:34:04 +0000
committerNick Alcock <nick.alcock@oracle.com>2020-11-20 13:34:07 +0000
commit3d16b64e28ab2cd7e69c0b31bc3ab1601891c969 (patch)
tree437086ea5f07f3be6400e95422735a446ce28801 /libctf
parent83d59285d549dab64d1be496408c0e62f30030b4 (diff)
downloadgdb-3d16b64e28ab2cd7e69c0b31bc3ab1601891c969.zip
gdb-3d16b64e28ab2cd7e69c0b31bc3ab1601891c969.tar.gz
gdb-3d16b64e28ab2cd7e69c0b31bc3ab1601891c969.tar.bz2
bfd, include, ld, binutils, libctf: CTF should use the dynstr/sym
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.
Diffstat (limited to 'libctf')
-rw-r--r--libctf/ChangeLog13
-rw-r--r--libctf/ctf-archive.c19
-rw-r--r--libctf/ctf-create.c2
-rw-r--r--libctf/ctf-impl.h1
-rw-r--r--libctf/ctf-link.c9
-rw-r--r--libctf/ctf-open-bfd.c34
-rw-r--r--libctf/libctf.ver2
7 files changed, 72 insertions, 8 deletions
diff --git a/libctf/ChangeLog b/libctf/ChangeLog
index 804f131..6670a6a 100644
--- a/libctf/ChangeLog
+++ b/libctf/ChangeLog
@@ -1,5 +1,18 @@
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.
+
+2020-11-20 Nick Alcock <nick.alcock@oracle.com>
+
* ctf-archive.c (ctf_arc_open_by_offset): Rename to...
(ctf_dict_open_by_offset): ... this. Adjust callers.
(ctf_arc_open_by_name_internal): Rename to...
diff --git a/libctf/ctf-archive.c b/libctf/ctf-archive.c
index 72cdef9..25c30f6 100644
--- a/libctf/ctf-archive.c
+++ b/libctf/ctf-archive.c
@@ -377,6 +377,25 @@ ctf_new_archive_internal (int is_archive, int unmap_on_close,
return arci;
}
+/* Get the CTF preamble from data in a buffer, which may be either an archive or
+ a CTF dict. If multiple dicts are present in an archive, the preamble comes
+ from an arbitrary dict. The preamble is a pointer into the ctfsect passed
+ in. */
+
+const ctf_preamble_t *
+ctf_arc_bufpreamble (const ctf_sect_t *ctfsect)
+{
+ if (ctfsect->cts_size > sizeof (uint64_t) &&
+ (le64toh ((*(uint64_t *) ctfsect->cts_data)) == CTFA_MAGIC))
+ {
+ struct ctf_archive *arc = (struct ctf_archive *) ctfsect->cts_data;
+ return (const ctf_preamble_t *) ((char *) arc + le64toh (arc->ctfa_ctfs)
+ + sizeof (uint64_t));
+ }
+ else
+ return (const ctf_preamble_t *) ctfsect->cts_data;
+}
+
/* Open a CTF archive or dictionary from data in a buffer (which the caller must
preserve until ctf_arc_close() time). Returns the archive, or NULL and an
error in *err (if not NULL). */
diff --git a/libctf/ctf-create.c b/libctf/ctf-create.c
index c1a7a8a..384e8cf 100644
--- a/libctf/ctf-create.c
+++ b/libctf/ctf-create.c
@@ -298,6 +298,8 @@ ctf_serialize (ctf_dict_t *fp)
hdr.cth_magic = CTF_MAGIC;
hdr.cth_version = CTF_VERSION;
+ hdr.cth_flags = CTF_F_DYNSTR;
+
/* Iterate through the dynamic type definition list and compute the
size of the CTF type section we will need to generate. */
diff --git a/libctf/ctf-impl.h b/libctf/ctf-impl.h
index 3f4cfac..0e09a45 100644
--- a/libctf/ctf-impl.h
+++ b/libctf/ctf-impl.h
@@ -683,6 +683,7 @@ ctf_new_archive_internal (int is_archive, int unmap_on_close,
const ctf_sect_t *strsect, int *errp);
extern struct ctf_archive *ctf_arc_open_internal (const char *, int *);
extern void ctf_arc_close_internal (struct ctf_archive *);
+extern const ctf_preamble_t *ctf_arc_bufpreamble (const ctf_sect_t *);
extern void *ctf_set_open_errno (int *, int);
extern unsigned long ctf_set_errno (ctf_dict_t *, int);
diff --git a/libctf/ctf-link.c b/libctf/ctf-link.c
index 192d4fd..3f4f2ee 100644
--- a/libctf/ctf-link.c
+++ b/libctf/ctf-link.c
@@ -1596,9 +1596,12 @@ ctf_link_add_strtab (ctf_dict_t *fp, ctf_link_strtab_string_f *add_string,
/* Not yet implemented. */
int
-ctf_link_shuffle_syms (ctf_dict_t *fp _libctf_unused_,
- ctf_link_iter_symbol_f *add_sym _libctf_unused_,
- void *arg _libctf_unused_)
+ctf_link_add_linker_symbol (ctf_dict_t *fp, ctf_link_sym_t *sym)
+{
+ return 0;
+}
+int
+ctf_link_shuffle_syms (ctf_dict_t *fp)
{
return 0;
}
diff --git a/libctf/ctf-open-bfd.c b/libctf/ctf-open-bfd.c
index 449347e..a6f0d3f 100644
--- a/libctf/ctf-open-bfd.c
+++ b/libctf/ctf-open-bfd.c
@@ -100,12 +100,36 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
#ifdef HAVE_BFD_ELF
ctf_sect_t symsect, strsect;
- Elf_Internal_Shdr *symhdr = &elf_symtab_hdr (abfd);
+ 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. */
@@ -145,12 +169,12 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
strtab = (const char *) strhdr->contents;
}
}
- else /* No symtab: just try getting .strtab by name. */
+ 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")) != NULL)
+ if ((str_asect = bfd_get_section_by_name (abfd, strtab_name)) != NULL)
{
if (bfd_malloc_and_get_section (abfd, str_asect, &str_bcontents))
{
@@ -168,7 +192,7 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
use it for anything but debugging. */
strsect.cts_data = strtab;
- strsect.cts_name = ".strtab";
+ strsect.cts_name = strtab_name;
strsect.cts_size = strsize;
strsectp = &strsect;
}
@@ -176,7 +200,7 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
if (symtab)
{
assert (symhdr->sh_entsize == get_elf_backend_data (abfd)->s->sizeof_sym);
- symsect.cts_name = ".symtab";
+ symsect.cts_name = symtab_name;
symsect.cts_entsize = symhdr->sh_entsize;
symsect.cts_size = symhdr->sh_size;
symsect.cts_data = symtab;
diff --git a/libctf/libctf.ver b/libctf/libctf.ver
index cf4fa73..317913e 100644
--- a/libctf/libctf.ver
+++ b/libctf/libctf.ver
@@ -181,4 +181,6 @@ LIBCTF_1.1 {
ctf_dict_open_sections;
ctf_dict_close;
ctf_parent_dict;
+
+ ctf_link_add_linker_symbol;
} LIBCTF_1.0;