[Object/ELF] - Do not crash on invalid Header->e_shoff value.

sections_begin() may return unalignment pointer when Header->e_shoff isinvalid.
That may result in a crash in clients, for example we have one in LLD:

assert((PtrWord & ~PointerBitMask) == 0 &&
       "Pointer is not sufficiently aligned");
fails when trying to push_back Elf_Shdr* (unaligned) into TinyPtrVector.

Patch forces check for alignment of Header->e_shoff.

Differential revision: https://reviews.llvm.org/D25368

llvm-svn: 283740
This commit is contained in:
George Rimar 2016-10-10 10:51:38 +00:00
parent 2e2c24d2b2
commit e4dce5ce3e
3 changed files with 6 additions and 0 deletions

View File

@ -367,6 +367,8 @@ const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_begin() const {
if (Header->e_shentsize != sizeof(Elf_Shdr))
report_fatal_error(
"Invalid section header entry size (e_shentsize) in ELF header");
if (Header->e_shoff & (AlignOf<Elf_Shdr>::Alignment - 1))
report_fatal_error("Invalid address alignment of section headers");
return reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
}

View File

@ -64,3 +64,7 @@ RUN: FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s
RUN: not llvm-readobj -r %p/Inputs/invalid-relocation-sec-sh_offset.elf-x86-64 2>&1 | \
RUN: FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s
INVALID-RELOC-SH-OFFSET: Invalid data was encountered while parsing the file
RUN: not llvm-readobj -t %p/Inputs/invalid-sections-address-alignment.x86-64 2>&1 | \
RUN: FileCheck --check-prefix=INVALID-SEC-ADDRESS-ALIGNMENT %s
INVALID-SEC-ADDRESS-ALIGNMENT: Invalid address alignment of section headers