diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index 0cb5c7a81ca..4c9cacd7e51 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -285,17 +285,18 @@ public: } const Elf_Sym *begin_dynamic_symbols() const { - if (DynSymRegion.Addr) - return reinterpret_cast(DynSymRegion.Addr); - return nullptr; + if (!DynSymRegion.Addr) + return nullptr; + if (DynSymRegion.EntSize != sizeof(Elf_Sym)) + report_fatal_error("Invalid symbol size"); + return reinterpret_cast(DynSymRegion.Addr); } const Elf_Sym *end_dynamic_symbols() const { - if (DynSymRegion.Addr) - return reinterpret_cast( - ((const char *)DynSymRegion.Addr + DynSymRegion.Size)); - - return nullptr; + if (!DynSymRegion.Addr) + return nullptr; + return reinterpret_cast( + ((const char *)DynSymRegion.Addr + DynSymRegion.Size)); } Elf_Sym_Range dynamic_symbols() const { @@ -766,6 +767,8 @@ template const typename ELFFile::Elf_Sym *ELFFile::begin_symbols() const { if (!dot_symtab_sec) return nullptr; + if (dot_symtab_sec->sh_entsize != sizeof(Elf_Sym)) + report_fatal_error("Invalid symbol size"); return reinterpret_cast(base() + dot_symtab_sec->sh_offset); } diff --git a/test/Object/Inputs/invalid-sh_entsize.elf b/test/Object/Inputs/invalid-sh_entsize.elf new file mode 100755 index 00000000000..9ea80731491 Binary files /dev/null and b/test/Object/Inputs/invalid-sh_entsize.elf differ diff --git a/test/Object/invalid.test b/test/Object/invalid.test index 73a6ad8021a..88685fab20c 100644 --- a/test/Object/invalid.test +++ b/test/Object/invalid.test @@ -5,3 +5,35 @@ CHECK: Invalid data was encountered while parsing the file RUN: not llvm-objdump -s %p/Inputs/invalid-strtab-non-null.elf 2>&1 | FileCheck --check-prefix=NON-NULL %s NON-NULL: String table must end with a null terminator + +Test the sh_entsize are invalid +RUN: llvm-readobj -s %p/Inputs/invalid-sh_entsize.elf | FileCheck --check-prefix=SECTION %s + +SECTION: Name: .dynsym +SECTION-NEXT: Type: SHT_DYNSYM +SECTION-NEXT: Flags [ +SECTION-NEXT: SHF_ALLOC +SECTION-NEXT: ] +SECTION-NEXT: Address: +SECTION-NEXT: Offset: +SECTION-NEXT: Size: +SECTION-NEXT: Link: +SECTION-NEXT: Info: +SECTION-NEXT: AddressAlignment: +SECTION-NEXT: EntrySize: 32 + +SECTION: Name: .symtab +SECTION-NEXT: Type: SHT_SYMTAB +SECTION-NEXT: Flags [ +SECTION-NEXT: ] +SECTION-NEXT: Address: +SECTION-NEXT: Offset: +SECTION-NEXT: Size: +SECTION-NEXT: Link: +SECTION-NEXT: Info: +SECTION-NEXT: AddressAlignment: +SECTION-NEXT: EntrySize: 32 + +RUN: not llvm-readobj -t %p/Inputs/invalid-sh_entsize.elf 2>&1 | FileCheck --check-prefix=INVALID-SYM-SIZE %s +RUN: not llvm-readobj --dyn-symbols %p/Inputs/invalid-sh_entsize.elf 2>&1 | FileCheck --check-prefix=INVALID-SYM-SIZE %s +INVALID-SYM-SIZE: Invalid symbol size