libctf, open: fix opening CTF in binaries with no symtab
This is a perfectly possible case, and half of ctf_bfdopen_ctfsect handled it fine. The other half hit a divide by zero or two before we got that far, and had no code path to load the strtab from anywhere in the absence of a symtab to point at it in any case. So, as a fallback, if there is no symtab, try loading ".strtab" explicitly by name, like we used to before we started looking for the strtab the symtab used. Of course, such a strtab is not kept hold of by BFD, so this means we have to bring back the code to possibly explicitly free the strtab that we read in. libctf/ * ctf-impl.h (struct ctf_archive_internal) <ctfi_free_strsect> New. * ctf-open-bfd.c (ctf_bfdopen_ctfsect): Explicitly open a strtab if the input has no symtab, rather than dividing by zero. Arrange to free it later via ctfi_free_ctfsect. * ctf-archive.c (ctf_new_archive_internal): Do not ctfi_free_strsect by default. (ctf_arc_close): Possibly free it here.
This commit is contained in:
parent
7044740174
commit
d50c08025d
4 changed files with 73 additions and 29 deletions
|
@ -1,3 +1,14 @@
|
||||||
|
2020-07-22 Nick Alcock <nick.alcock@oracle.com>
|
||||||
|
|
||||||
|
* ctf-impl.h (struct ctf_archive_internal) <ctfi_free_strsect>
|
||||||
|
New.
|
||||||
|
* ctf-open-bfd.c (ctf_bfdopen_ctfsect): Explicitly open a strtab
|
||||||
|
if the input has no symtab, rather than dividing by
|
||||||
|
zero. Arrange to free it later via ctfi_free_ctfsect.
|
||||||
|
* ctf-archive.c (ctf_new_archive_internal): Do not
|
||||||
|
ctfi_free_strsect by default.
|
||||||
|
(ctf_arc_close): Possibly free it here.
|
||||||
|
|
||||||
2020-07-22 Nick Alcock <nick.alcock@oracle.com>
|
2020-07-22 Nick Alcock <nick.alcock@oracle.com>
|
||||||
|
|
||||||
* ctf-dump.c (ctf_is_slice): Delete, unnecessary.
|
* ctf-dump.c (ctf_is_slice): Delete, unnecessary.
|
||||||
|
|
|
@ -368,6 +368,7 @@ ctf_new_archive_internal (int is_archive, int unmap_on_close,
|
||||||
if (strsect)
|
if (strsect)
|
||||||
memcpy (&arci->ctfi_strsect, strsect, sizeof (struct ctf_sect));
|
memcpy (&arci->ctfi_strsect, strsect, sizeof (struct ctf_sect));
|
||||||
arci->ctfi_free_symsect = 0;
|
arci->ctfi_free_symsect = 0;
|
||||||
|
arci->ctfi_free_strsect = 0;
|
||||||
arci->ctfi_unmap_on_close = unmap_on_close;
|
arci->ctfi_unmap_on_close = unmap_on_close;
|
||||||
|
|
||||||
return arci;
|
return arci;
|
||||||
|
@ -493,6 +494,8 @@ ctf_arc_close (ctf_archive_t *arc)
|
||||||
ctf_file_close (arc->ctfi_file);
|
ctf_file_close (arc->ctfi_file);
|
||||||
if (arc->ctfi_free_symsect)
|
if (arc->ctfi_free_symsect)
|
||||||
free ((void *) arc->ctfi_symsect.cts_data);
|
free ((void *) arc->ctfi_symsect.cts_data);
|
||||||
|
if (arc->ctfi_free_strsect)
|
||||||
|
free ((void *) arc->ctfi_strsect.cts_data);
|
||||||
free (arc->ctfi_data);
|
free (arc->ctfi_data);
|
||||||
if (arc->ctfi_bfd_close)
|
if (arc->ctfi_bfd_close)
|
||||||
arc->ctfi_bfd_close (arc);
|
arc->ctfi_bfd_close (arc);
|
||||||
|
|
|
@ -333,6 +333,7 @@ struct ctf_archive_internal
|
||||||
ctf_sect_t ctfi_symsect;
|
ctf_sect_t ctfi_symsect;
|
||||||
ctf_sect_t ctfi_strsect;
|
ctf_sect_t ctfi_strsect;
|
||||||
int ctfi_free_symsect;
|
int ctfi_free_symsect;
|
||||||
|
int ctfi_free_strsect;
|
||||||
void *ctfi_data;
|
void *ctfi_data;
|
||||||
bfd *ctfi_abfd; /* Optional source of section data. */
|
bfd *ctfi_abfd; /* Optional source of section data. */
|
||||||
void (*ctfi_bfd_close) (struct ctf_archive_internal *);
|
void (*ctfi_bfd_close) (struct ctf_archive_internal *);
|
||||||
|
|
|
@ -94,46 +94,69 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
|
||||||
ctf_sect_t *symsectp = NULL;
|
ctf_sect_t *symsectp = NULL;
|
||||||
ctf_sect_t *strsectp = NULL;
|
ctf_sect_t *strsectp = NULL;
|
||||||
const char *bfderrstr = NULL;
|
const char *bfderrstr = NULL;
|
||||||
|
char *strtab_alloc = NULL;
|
||||||
|
|
||||||
#ifdef HAVE_BFD_ELF
|
#ifdef HAVE_BFD_ELF
|
||||||
ctf_sect_t symsect, strsect;
|
ctf_sect_t symsect, strsect;
|
||||||
Elf_Internal_Shdr *strhdr;
|
|
||||||
Elf_Internal_Shdr *symhdr = &elf_symtab_hdr (abfd);
|
Elf_Internal_Shdr *symhdr = &elf_symtab_hdr (abfd);
|
||||||
size_t symcount = symhdr->sh_size / symhdr->sh_entsize;
|
size_t symcount;
|
||||||
Elf_Internal_Sym *isymbuf;
|
Elf_Internal_Sym *isymbuf;
|
||||||
bfd_byte *symtab;
|
bfd_byte *symtab = NULL;
|
||||||
const char *strtab = NULL;
|
const char *strtab = NULL;
|
||||||
|
size_t strsize;
|
||||||
/* TODO: handle SYMTAB_SHNDX. */
|
/* TODO: handle SYMTAB_SHNDX. */
|
||||||
|
|
||||||
if ((symtab = malloc (symhdr->sh_size)) == NULL)
|
/* Get the symtab, and the strtab associated with it. */
|
||||||
|
if (elf_tdata (abfd) && symhdr && symhdr->sh_size && symhdr->sh_entsize)
|
||||||
{
|
{
|
||||||
bfderrstr = "Cannot malloc symbol table";
|
symcount = symhdr->sh_size / symhdr->sh_entsize;
|
||||||
goto err;
|
if ((symtab = malloc (symhdr->sh_size)) == NULL)
|
||||||
}
|
|
||||||
|
|
||||||
isymbuf = bfd_elf_get_elf_syms (abfd, symhdr, symcount, 0,
|
|
||||||
NULL, symtab, NULL);
|
|
||||||
free (isymbuf);
|
|
||||||
if (isymbuf == NULL)
|
|
||||||
{
|
|
||||||
bfderrstr = "Cannot read symbol table";
|
|
||||||
goto err_free_sym;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (elf_elfsections (abfd) != NULL
|
|
||||||
&& symhdr->sh_link < elf_numsections (abfd))
|
|
||||||
{
|
|
||||||
strhdr = elf_elfsections (abfd)[symhdr->sh_link];
|
|
||||||
if (strhdr->contents == NULL)
|
|
||||||
{
|
{
|
||||||
if ((strtab = bfd_elf_get_str_section (abfd, symhdr->sh_link)) == NULL)
|
bfderrstr = "Cannot malloc symbol table";
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
isymbuf = bfd_elf_get_elf_syms (abfd, symhdr, symcount, 0,
|
||||||
|
NULL, symtab, NULL);
|
||||||
|
free (isymbuf);
|
||||||
|
if (isymbuf == NULL)
|
||||||
|
{
|
||||||
|
bfderrstr = "Cannot read symbol table";
|
||||||
|
goto err_free_sym;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (elf_elfsections (abfd) != NULL
|
||||||
|
&& symhdr->sh_link < elf_numsections (abfd))
|
||||||
|
{
|
||||||
|
Elf_Internal_Shdr *strhdr = elf_elfsections (abfd)[symhdr->sh_link];
|
||||||
|
|
||||||
|
strsize = strhdr->sh_size;
|
||||||
|
if (strhdr->contents == NULL)
|
||||||
{
|
{
|
||||||
bfderrstr = "Cannot read string table";
|
if ((strtab = bfd_elf_get_str_section (abfd, symhdr->sh_link)) == NULL)
|
||||||
goto err_free_sym;
|
{
|
||||||
|
bfderrstr = "Cannot read string table";
|
||||||
|
goto err_free_sym;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
strtab = (const char *) strhdr->contents;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else /* No symtab: just try getting .strtab by name. */
|
||||||
|
{
|
||||||
|
bfd_byte *str_bcontents;
|
||||||
|
asection *str_asect;
|
||||||
|
|
||||||
|
if ((str_asect = bfd_get_section_by_name (abfd, ".strtab")) != NULL)
|
||||||
|
{
|
||||||
|
if (bfd_malloc_and_get_section (abfd, str_asect, &str_bcontents))
|
||||||
|
{
|
||||||
|
strtab = (const char *) str_bcontents;
|
||||||
|
strtab_alloc = (char *) str_bcontents;
|
||||||
|
strsize = str_asect->size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
strtab = (const char *) strhdr->contents;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strtab)
|
if (strtab)
|
||||||
|
@ -144,9 +167,12 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
|
||||||
|
|
||||||
strsect.cts_data = strtab;
|
strsect.cts_data = strtab;
|
||||||
strsect.cts_name = ".strtab";
|
strsect.cts_name = ".strtab";
|
||||||
strsect.cts_size = strhdr->sh_size;
|
strsect.cts_size = strsize;
|
||||||
strsectp = &strsect;
|
strsectp = &strsect;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (symtab)
|
||||||
|
{
|
||||||
assert (symhdr->sh_entsize == get_elf_backend_data (abfd)->s->sizeof_sym);
|
assert (symhdr->sh_entsize == get_elf_backend_data (abfd)->s->sizeof_sym);
|
||||||
symsect.cts_name = ".symtab";
|
symsect.cts_name = ".symtab";
|
||||||
symsect.cts_entsize = symhdr->sh_entsize;
|
symsect.cts_entsize = symhdr->sh_entsize;
|
||||||
|
@ -159,13 +185,16 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
|
||||||
arci = ctf_arc_bufopen (ctfsect, symsectp, strsectp, errp);
|
arci = ctf_arc_bufopen (ctfsect, symsectp, strsectp, errp);
|
||||||
if (arci)
|
if (arci)
|
||||||
{
|
{
|
||||||
/* Request freeing of the symsect. */
|
/* Request freeing of the symsect and possibly the strsect. */
|
||||||
arci->ctfi_free_symsect = 1;
|
arci->ctfi_free_symsect = 1;
|
||||||
|
if (strtab_alloc)
|
||||||
|
arci->ctfi_free_strsect = 1;
|
||||||
return arci;
|
return arci;
|
||||||
}
|
}
|
||||||
#ifdef HAVE_BFD_ELF
|
#ifdef HAVE_BFD_ELF
|
||||||
err_free_sym:
|
err_free_sym:
|
||||||
free (symtab);
|
free (symtab);
|
||||||
|
free (strtab_alloc);
|
||||||
#endif
|
#endif
|
||||||
err: _libctf_unused_;
|
err: _libctf_unused_;
|
||||||
if (bfderrstr)
|
if (bfderrstr)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue