Report an error if a SHT_SYMTAB_SHNDX section has the wrong size.

llvm-svn: 245873
This commit is contained in:
Rafael Espindola 2015-08-24 21:09:41 +00:00
parent 7bd84e30f7
commit a72d6ae775
3 changed files with 22 additions and 8 deletions

View File

@ -197,9 +197,9 @@ public:
uint64_t getNumSections() const;
uintX_t getStringTableIndex() const;
ELF::Elf64_Word
getExtendedSymbolTableIndex(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
ArrayRef<Elf_Word> ShndxTable) const;
uint32_t getExtendedSymbolTableIndex(const Elf_Sym *Sym,
const Elf_Shdr *SymTab,
ArrayRef<Elf_Word> ShndxTable) const;
const Elf_Ehdr *getHeader() const { return Header; }
ErrorOr<const Elf_Shdr *> getSection(const Elf_Sym *Sym,
const Elf_Shdr *SymTab,
@ -220,13 +220,13 @@ typedef ELFFile<ELFType<support::big, false>> ELF32BEFile;
typedef ELFFile<ELFType<support::big, true>> ELF64BEFile;
template <class ELFT>
ELF::Elf64_Word ELFFile<ELFT>::getExtendedSymbolTableIndex(
uint32_t ELFFile<ELFT>::getExtendedSymbolTableIndex(
const Elf_Sym *Sym, const Elf_Shdr *SymTab,
ArrayRef<Elf_Word> ShndxTable) const {
assert(Sym->st_shndx == ELF::SHN_XINDEX);
unsigned Index = Sym - symbol_begin(SymTab);
// FIXME: error checking
// The size of the table was checked in getSHNDXTable.
return ShndxTable[Index];
}
@ -471,12 +471,22 @@ ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section) const {
assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
const Elf_Word *ShndxTableBegin =
reinterpret_cast<const Elf_Word *>(base() + Section.sh_offset);
uintX_t Size = Section.sh_offset;
if (Size % sizeof(uintX_t))
uintX_t Size = Section.sh_size;
if (Size % sizeof(uint32_t))
return object_error::parse_failed;
const Elf_Word *ShndxTableEnd = ShndxTableBegin + Size / sizeof(uintX_t);
uintX_t NumSymbols = Size / sizeof(uint32_t);
const Elf_Word *ShndxTableEnd = ShndxTableBegin + NumSymbols;
if (reinterpret_cast<const char *>(ShndxTableEnd) > Buf.end())
return object_error::parse_failed;
ErrorOr<const Elf_Shdr *> SymTableOrErr = getSection(Section.sh_link);
if (std::error_code EC = SymTableOrErr.getError())
return EC;
const Elf_Shdr &SymTable = **SymTableOrErr;
if (SymTable.sh_type != ELF::SHT_SYMTAB &&
SymTable.sh_type != ELF::SHT_DYNSYM)
return object_error::parse_failed;
if (NumSymbols != (SymTable.sh_size / sizeof(Elf_Sym)))
return object_error::parse_failed;
return ArrayRef<Elf_Word>(ShndxTableBegin, ShndxTableEnd);
}

Binary file not shown.

View File

@ -48,3 +48,7 @@ INVALID-SECTION-SIZE: Invalid section header entry size (e_shentsize) in ELF hea
RUN: not llvm-readobj -t %p/Inputs/invalid-symbol-table-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SYMTAB-SIZE %s
INVALID-SYMTAB-SIZE: Invalid symbol table size
RUN: not llvm-readobj -t %p/Inputs/invalid-xindex-size.elf 2>&1 | FileCheck --check-prefix=INVALID-XINDEX-SIZE %s
INVALID-XINDEX-SIZE: Invalid data was encountered while parsing the file.