binutils-gdb/libctf
Nick Alcock 211bcd0133 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.
2021-03-02 15:10:10 +00:00
..
testsuite libctf, include: find types of symbols by name 2021-02-20 16:37:08 +00:00
.gitignore libctf: restructure error handling to reduce relocations 2020-07-22 17:57:20 +01:00
aclocal.m4 Implement a workaround for GNU mak jobserver 2021-01-12 05:45:44 -08:00
ChangeLog bfd, ld, libctf: skip zero-refcount strings in CTF string reporting 2021-03-02 15:10:10 +00:00
ChangeLog-2020 libctf: fix old ChangeLog typo 2021-01-05 14:53:40 +00:00
config.h.in libctf, dedup: add deduplicator 2020-07-22 18:02:19 +01:00
configure libctf AC_CANONICAL_TARGET 2021-02-21 14:26:38 +10:30
configure.ac libctf AC_CANONICAL_TARGET 2021-02-21 14:26:38 +10:30
ctf-archive.c libctf: reimplement many _iter iterators in terms of _next 2021-03-02 15:09:18 +00:00
ctf-create.c bfd, ld, libctf: skip zero-refcount strings in CTF string reporting 2021-03-02 15:10:10 +00:00
ctf-decl.c libctf, ld: dump enums: generally improve dump formatting 2021-01-05 14:53:39 +00:00
ctf-decls.h Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
ctf-dedup.c libctf: minor error-handling fixes 2021-03-02 15:10:08 +00:00
ctf-dump.c libctf: fix signed/unsigned comparison confusion 2021-03-02 15:10:08 +00:00
ctf-endian.h Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
ctf-error.c Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
ctf-hash.c Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
ctf-impl.h libctf: add a deduplicator-specific type mapping table 2021-03-02 15:10:07 +00:00
ctf-inlines.h Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
ctf-intl.h Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
ctf-labels.c Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
ctf-link.c bfd, ld, libctf: skip zero-refcount strings in CTF string reporting 2021-03-02 15:10:10 +00:00
ctf-lookup.c libctf, include: find types of symbols by name 2021-02-20 16:37:08 +00:00
ctf-open-bfd.c Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
ctf-open.c libctf, include: find types of symbols by name 2021-02-20 16:37:08 +00:00
ctf-qsort_r.c Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
ctf-sha1.c Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
ctf-sha1.h Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
ctf-string.c libctf: always name nameless types "", never NULL 2021-02-04 16:01:53 +00:00
ctf-subr.c Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
ctf-types.c libctf: reimplement many _iter iterators in terms of _next 2021-03-02 15:09:18 +00:00
ctf-util.c libctf, include: support unnamed structure members better 2021-01-05 14:53:40 +00:00
elf.h Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30
libctf.ver libctf, include: find types of symbols by name 2021-02-20 16:37:08 +00:00
Makefile.am libctf: require a Tcl capable of try/catch to run tests 2021-02-10 15:26:57 +00:00
Makefile.in libctf regen for NEWS 2021-02-26 13:29:01 +10:30
NEWS libctf: add a NEWS 2021-02-20 16:37:08 +00:00
swap.h Update year range in copyright notice of binutils files 2021-01-01 10:31:05 +10:30