4610d93 libctf, bfd: fix ctf_bfdopen_ctfsect opening symbol and string sections

Authored and Committed by Nick Alcock 4 years ago
    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).
    
    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.
    
        
file modified
+5 -0
file modified
+1 -0
file modified
+1 -1
file modified
+10 -0
file modified
+1 -1
file modified
+59 -45
file modified
+1 -1