diff options
author | Nick Alcock <nick.alcock@oracle.com> | 2021-03-02 15:10:05 +0000 |
---|---|---|
committer | Nick Alcock <nick.alcock@oracle.com> | 2021-03-02 15:10:10 +0000 |
commit | 211bcd013331fcaefa9bb31b6145ae0df13a3363 (patch) | |
tree | fe0ede7f5dcdee9bd7b6fae3e6d9c632ee5b9957 /bfd | |
parent | 8e7e446446b2de0c44e453a298718a0411f47205 (diff) | |
download | gdb-211bcd013331fcaefa9bb31b6145ae0df13a3363.zip gdb-211bcd013331fcaefa9bb31b6145ae0df13a3363.tar.gz gdb-211bcd013331fcaefa9bb31b6145ae0df13a3363.tar.bz2 |
bfd, ld, libctf: skip zero-refcount strings in CTF string reporting
This is a tricky one. BFD, on the linker's behalf, reports symbols to
libctf via the ctf_new_symbol and ctf_new_dynsym callbacks, which
ultimately call ctf_link_add_linker_symbol. But while this happens
after strtab offsets are finalized, it happens before the .dynstr is
actually laid out, so we can't iterate over it at this stage and
it is not clear what the reported symbols are actually called. So
a second callback, examine_strtab, is called after the .dynstr is
finalized, which calls ctf_link_add_strtab and ultimately leads
to ldelf_ctf_strtab_iter_cb being called back repeatedly until the
offsets of every string in the .dynstr is passed to libctf.
libctf can then use this to get symbol names out of the input (which
usually stores symbol types in the form of a name -> type mapping at
this stage) and extract the types of those symbols, feeding them back
into their final form as a 1:1 association with the real symtab's
STT_OBJ and STT_FUNC symbols (with a few skipped, see
ctf_symtab_skippable).
This representation is compact, but has one problem: if libctf somehow
gets confused about the st_type of a symbol, it'll stick an entry into
the function symtypetab when it should put it into the object
symtypetab, or vice versa, and *every symbol from that one on* will have
the wrong CTF type because it's actually looking up the type for a
different symbol.
And we have just such a bug. ctf_link_add_strtab was not taking the
refcounts of strings into consideration, so even strings that had been
eliminated from the strtab by virtue of being in objects eliminated via
--as-needed etc were being reported. This is harmful because it can
lead to multiple strings with the same apparent offset, and if the last
duplicate to be reported relates to an eliminated symbol, we look up the
wrong symbol from the input and gets its type wrong: if it's unlucky and
the eliminated symbol is also of the wrong st_type, we will end up with
a corrupted symtypetab.
Thankfully the wrong-st_type case is already diagnosed by a
this-can-never-happen paranoid warning:
CTF warning: Symbol 61a added to CTF as a function but is of type 1
or the converse
* CTF warning: Symbol a3 added to CTF as a data object but is of type 2
so at least we can tell when the corruption has spread to more than one
symbol's type.
Skipping zero-refcounted strings is easy: teach _bfd_elf_strtab_str to
skip them, and ldelf_ctf_strtab_iter_cb to loop over skipped strings
until it falls off the end or finds one that isn't skipped.
bfd/ChangeLog
2021-03-02 Nick Alcock <nick.alcock@oracle.com>
* elf-strtab.c (_bfd_elf_strtab_str): Skip strings with zero refcount.
ld/ChangeLog
2021-03-02 Nick Alcock <nick.alcock@oracle.com>
* ldelfgen.c (ldelf_ctf_strtab_iter_cb): Skip zero-refcount strings.
libctf/ChangeLog
2021-03-02 Nick Alcock <nick.alcock@oracle.com>
* ctf-create.c (symtypetab_density): Report the symbol name as
well as index in the name != object error; note the likely
consequences.
* ctf-link.c (ctf_link_shuffle_syms): Report the symbol index
as well as name.
Diffstat (limited to 'bfd')
-rw-r--r-- | bfd/ChangeLog | 4 | ||||
-rw-r--r-- | bfd/elf-strtab.c | 4 |
2 files changed, 7 insertions, 1 deletions
diff --git a/bfd/ChangeLog b/bfd/ChangeLog index c4c4fa2..c56cfc4 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,7 @@ +2021-03-02 Nick Alcock <nick.alcock@oracle.com> + + * elf-strtab.c (_bfd_elf_strtab_str): Skip strings with zero refcount. + 2021-03-02 Alan Modra <amodra@gmail.com> PR 27451 diff --git a/bfd/elf-strtab.c b/bfd/elf-strtab.c index 3e5e162..a3cb4ef 100644 --- a/bfd/elf-strtab.c +++ b/bfd/elf-strtab.c @@ -299,9 +299,11 @@ _bfd_elf_strtab_str (struct elf_strtab_hash *tab, size_t idx, bfd_size_type *offset) { if (idx == 0) - return 0; + return NULL; BFD_ASSERT (idx < tab->size); BFD_ASSERT (tab->sec_size); + if (tab->array[idx]->refcount == 0) + return NULL; if (offset) *offset = tab->array[idx]->u.index; return tab->array[idx]->root.string; |