diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index 085595ed621..509355b105d 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -738,9 +738,13 @@ template void ELFFile::scanDynamicTable() { const Elf_Phdr **I = std::upper_bound( LoadSegments.begin(), LoadSegments.end(), VAddr, compareAddr); if (I == LoadSegments.begin()) - return nullptr; + report_fatal_error("Virtual address is not in any segment"); --I; - return this->base() + (*I)->p_offset + (VAddr - (*I)->p_vaddr); + const Elf_Phdr &Phdr = **I; + uint64_t Delta = VAddr - Phdr.p_vaddr; + if (Delta >= Phdr.p_filesz) + report_fatal_error("Virtual address is not in any segment"); + return this->base() + Phdr.p_offset + Delta; }; for (Elf_Dyn_Iter DynI = dynamic_table_begin(), DynE = dynamic_table_end(); diff --git a/test/Object/Inputs/corrupt-invalid-virtual-addr.elf.x86-64 b/test/Object/Inputs/corrupt-invalid-virtual-addr.elf.x86-64 new file mode 100755 index 00000000000..58d995933ec Binary files /dev/null and b/test/Object/Inputs/corrupt-invalid-virtual-addr.elf.x86-64 differ diff --git a/test/Object/corrupt.test b/test/Object/corrupt.test index 7c4b1c775ec..9b3daf7b4cf 100644 --- a/test/Object/corrupt.test +++ b/test/Object/corrupt.test @@ -37,3 +37,9 @@ RUN: %p/Inputs/corrupt-invalid-phentsize.elf.x86-64 2>&1 | \ RUN: FileCheck --check-prefix=PHENTSIZE %s PHENTSIZE: Invalid program header size + +RUN: not llvm-readobj -dynamic-table \ +RUN: %p/Inputs/corrupt-invalid-virtual-addr.elf.x86-64 2>&1 | \ +RUN: FileCheck --check-prefix=VIRTADDR %s + +VIRTADDR: Virtual address is not in any segment