=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/ctfconv/elf.c,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- src/usr.bin/ctfconv/elf.c 2017/10/27 08:33:46 1.7 +++ src/usr.bin/ctfconv/elf.c 2017/11/06 14:59:27 1.8 @@ -1,4 +1,4 @@ -/* $OpenBSD: elf.c,v 1.7 2017/10/27 08:33:46 mpi Exp $ */ +/* $OpenBSD: elf.c,v 1.8 2017/11/06 14:59:27 mpi Exp $ */ /* * Copyright (c) 2016 Martin Pieuchot @@ -104,14 +104,16 @@ ssize_t elf_getsymtab(const char *p, size_t filesize, const char *shstab, - size_t shstabsz, const Elf_Sym **symtab, size_t *nsymb) + size_t shstabsz, const Elf_Sym **symtab, size_t *nsymb, const char **strtab, + size_t *strtabsz) { Elf_Ehdr *eh = (Elf_Ehdr *)p; - Elf_Shdr *sh; + Elf_Shdr *sh, *symsh; size_t snlen; ssize_t i; snlen = strlen(ELF_SYMTAB); + symsh = NULL; for (i = 0; i < eh->e_shnum; i++) { sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize); @@ -133,12 +135,26 @@ *symtab = (Elf_Sym *)(p + sh->sh_offset); if (nsymb != NULL) *nsymb = (sh->sh_size / sh->sh_entsize); + symsh = sh; - return i; + break; } } - return -1; + if (symsh == NULL || (symsh->sh_link >= eh->e_shnum)) + return -1; + + sh = (Elf_Shdr *)(p + eh->e_shoff + symsh->sh_link * eh->e_shentsize); + + if ((sh->sh_offset + sh->sh_size) > filesize) + return -1; + + if (strtab != NULL) + *strtab = p + sh->sh_offset; + if (strtabsz != NULL) + *strtabsz = sh->sh_size; + + return i; } ssize_t @@ -240,7 +256,7 @@ /* Find symbol table location and number of symbols. */ symtabidx = elf_getsymtab(p, filesize, shstab, shstabsz, &symtab, - &nsymb); + &nsymb, NULL, NULL); if (symtabidx == -1) { warnx("symbol table not found"); return;