libctf, bfd: fix ctf_bfdopen_ctfsect opening symbol and string sections
authorNick Alcock <nick.alcock@oracle.com>
Thu, 11 Jul 2019 15:26:54 +0000 (16:26 +0100)
committerNick Alcock <nick.alcock@oracle.com>
Thu, 3 Oct 2019 16:04:55 +0000 (17:04 +0100)
commit6d5944fca682fe97f37e537f78b665ada2400f51
treeeb1e09536e33c9f0760a87bd41f7d0b4ceb6315c
parent9b32cba44ddeb32251092a05f1238d2462eb2345
libctf, bfd: fix ctf_bfdopen_ctfsect opening symbol and string sections

The code in ctf_bfdopen_ctfsect (which is the ultimate place where you
end up if you use ctf_open to open a CTF file and pull in the ELF string
and symbol tables) was written before it was possible to actually test
it, since the linker was not written.  Now it is, it turns out that the
previous code was completely nonfunctional: it assumed that you could
load the symbol table via bfd_section_from_elf_index (...,elf_onesymtab())
and the string table via bfd_section_from_elf_index on the sh_link.

Unfortunately BFD loads neither of these sections in the conventional
fashion it uses for most others: the symbol table is immediately
converted into internal form (which is useless for our purposes, since
we also have to work in the absence of BFD for readelf, etc) and the
string table is loaded specially via bfd_elf_get_str_section which is
private to bfd/elf.c.

So make this function public, export it in elf-bfd.h, and use it from
libctf, which does something similar to what bfd_elf_sym_name and
bfd_elf_string_from_elf_section do.  Similarly, load the symbol table
manually using bfd_elf_get_elf_syms and throw away the internal form
it generates for us (we never use it).

BFD allocates the strtab for us via bfd_alloc, so we can leave BFD to
deallocate it: we allocate the symbol table ourselves before calling
bfd_elf_get_elf_syms, so we still have to free it.

Also change the rules around what you are allowed to provide: It is
useful to provide a string section but no symbol table, because CTF
sections can legitimately have no function info or data object sections
while relying on the ELF strtab for some of their strings.  So allow
that combination.

v4: adjust to upstream changes.  ctf_bfdopen_ctfsect's first parameter
    is potentially unused again (if BFD is not in use for this link
    due to not supporting an ELF target).
v5: fix tabdamage.

bfd/
* elf-bfd.h (bfd_elf_get_str_section): Add.
* elf.c (bfd_elf_get_str_section): No longer static.

libctf/
* ctf-open-bfd.c: Add <assert.h>.
(ctf_bfdopen_ctfsect): Open string and symbol tables using
techniques borrowed from bfd_elf_sym_name.
(ctf_new_archive_internal): Improve comment.
* ctf-archive.c (ctf_arc_close): Do not free the ctfi_strsect.
* ctf-open.c (ctf_bufopen): Allow opening with a string section but
no symbol section, but not vice versa.
bfd/ChangeLog
bfd/elf-bfd.h
bfd/elf.c
libctf/ChangeLog
libctf/ctf-archive.c
libctf/ctf-open-bfd.c
libctf/ctf-open.c
This page took 0.025455 seconds and 4 git commands to generate.