From 4a04da337ee5a08ae5f23471cfa36b84496c435d Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 30 Jun 2015 19:58:10 +0000 Subject: [PATCH] Remove Elf_Shdr_Iter. Diagnose files with invalid section header sizes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241109 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/ELF.h | 20 +++++++-------- include/llvm/Object/ELFObjectFile.h | 27 ++++++++------------ test/Object/Inputs/invalid-section-size.elf | Bin 0 -> 584 bytes test/Object/invalid.test | 3 +++ tools/llvm-readobj/ARMEHABIPrinter.h | 1 - 5 files changed, 22 insertions(+), 29 deletions(-) create mode 100644 test/Object/Inputs/invalid-section-size.elf diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index 7953dba164f..e700b62c48c 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -143,8 +143,7 @@ public: typedef iterator_range Elf_Dyn_Range; typedef ELFEntityIterator Elf_Rela_Iter; typedef ELFEntityIterator Elf_Rel_Iter; - typedef ELFEntityIterator Elf_Shdr_Iter; - typedef iterator_range Elf_Shdr_Range; + typedef iterator_range Elf_Shdr_Range; /// \brief Archive files are 2 byte aligned, so we need this for /// PointerIntPair to work. @@ -264,8 +263,8 @@ public: Header->getDataEncoding() == ELF::ELFDATA2LSB; } - Elf_Shdr_Iter section_begin() const; - Elf_Shdr_Iter section_end() const; + const Elf_Shdr *section_begin() const; + const Elf_Shdr *section_end() const; Elf_Shdr_Range sections() const { return make_range(section_begin(), section_end()); } @@ -751,16 +750,15 @@ uint64_t ELFFile::getSymbolIndex(const Elf_Sym *Sym) const { } template -typename ELFFile::Elf_Shdr_Iter ELFFile::section_begin() const { - return Elf_Shdr_Iter(Header->e_shentsize, - (const char *)base() + Header->e_shoff); +const typename ELFFile::Elf_Shdr *ELFFile::section_begin() const { + if (Header->e_shentsize != sizeof(Elf_Shdr)) + report_fatal_error("Invalid section header size"); + return reinterpret_cast(base() + Header->e_shoff); } template -typename ELFFile::Elf_Shdr_Iter ELFFile::section_end() const { - return Elf_Shdr_Iter(Header->e_shentsize, - (const char *)base() + Header->e_shoff + - (getNumSections() * Header->e_shentsize)); +const typename ELFFile::Elf_Shdr *ELFFile::section_end() const { + return section_begin() + getNumSections(); } template diff --git a/include/llvm/Object/ELFObjectFile.h b/include/llvm/Object/ELFObjectFile.h index 7623ea4e097..091cb62f589 100644 --- a/include/llvm/Object/ELFObjectFile.h +++ b/include/llvm/Object/ELFObjectFile.h @@ -189,7 +189,6 @@ public: typedef typename ELFFile::Elf_Rela Elf_Rela; typedef typename ELFFile::Elf_Dyn Elf_Dyn; - typedef typename ELFFile::Elf_Shdr_Iter Elf_Shdr_Iter; typedef typename ELFFile::Elf_Dyn_Iter Elf_Dyn_Iter; protected: @@ -261,15 +260,8 @@ protected: return DRI; } - Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const { - return Elf_Shdr_Iter(EF.getHeader()->e_shentsize, - reinterpret_cast(Sec.p)); - } - - DataRefImpl toDRI(Elf_Shdr_Iter Sec) const { - DataRefImpl DRI; - DRI.p = reinterpret_cast(Sec.get()); - return DRI; + const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const { + return reinterpret_cast(Sec.p); } DataRefImpl toDRI(const Elf_Shdr *Sec) const { @@ -541,7 +533,8 @@ ELFObjectFile::getSymbolSection(DataRefImpl Symb, template void ELFObjectFile::moveSectionNext(DataRefImpl &Sec) const { - Sec = toDRI(++toELFShdrIter(Sec)); + const Elf_Shdr *ESec = toELFShdrIter(Sec); + Sec = toDRI(++ESec); } template @@ -568,7 +561,7 @@ template std::error_code ELFObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Result) const { - Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); + const Elf_Shdr *EShdr = toELFShdrIter(Sec); Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size); return std::error_code(); } @@ -585,14 +578,14 @@ bool ELFObjectFile::isSectionText(DataRefImpl Sec) const { template bool ELFObjectFile::isSectionData(DataRefImpl Sec) const { - Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); + const Elf_Shdr *EShdr = toELFShdrIter(Sec); return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) && EShdr->sh_type == ELF::SHT_PROGBITS; } template bool ELFObjectFile::isSectionBSS(DataRefImpl Sec) const { - Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); + const Elf_Shdr *EShdr = toELFShdrIter(Sec); return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) && EShdr->sh_type == ELF::SHT_NOBITS; } @@ -617,7 +610,7 @@ template relocation_iterator ELFObjectFile::section_rel_begin(DataRefImpl Sec) const { DataRefImpl RelData; - uintptr_t SHT = reinterpret_cast(EF.section_begin().get()); + uintptr_t SHT = reinterpret_cast(EF.section_begin()); RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize; RelData.d.b = 0; return relocation_iterator(RelocationRef(RelData, this)); @@ -627,7 +620,7 @@ template relocation_iterator ELFObjectFile::section_rel_end(DataRefImpl Sec) const { DataRefImpl RelData; - uintptr_t SHT = reinterpret_cast(EF.section_begin().get()); + uintptr_t SHT = reinterpret_cast(EF.section_begin()); const Elf_Shdr *S = reinterpret_cast(Sec.p); RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize; if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL) @@ -644,7 +637,7 @@ ELFObjectFile::getRelocatedSection(DataRefImpl Sec) const { if (EF.getHeader()->e_type != ELF::ET_REL) return section_end(); - Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); + const Elf_Shdr *EShdr = toELFShdrIter(Sec); uintX_t Type = EShdr->sh_type; if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA) return section_end(); diff --git a/test/Object/Inputs/invalid-section-size.elf b/test/Object/Inputs/invalid-section-size.elf new file mode 100644 index 0000000000000000000000000000000000000000..c111a4c6262261ae1f6ed9d2e4b30a38fe6130c1 GIT binary patch literal 584 zcmb<-^>JfjWMpQ50!9Wq21XbMiJpPPb^xl&yp$24Z6KJ4_rT#|Gjc0S6>D zGLr?FgT$5rvO(scyU77b4Kfp#IqX1196(H|IU+zau*4FB15g|U3?LNP%^-J!>;wS` H2n8nrDWebc literal 0 HcmV?d00001 diff --git a/test/Object/invalid.test b/test/Object/invalid.test index 32598691de4..75402631c9f 100644 --- a/test/Object/invalid.test +++ b/test/Object/invalid.test @@ -41,3 +41,6 @@ INVALID-SYM-SIZE: Invalid symbol size RUN: not llvm-readobj -t %p/Inputs/invalid-section-index.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-INDEX %s INVALID-SECTION-INDEX: Invalid section index! + +RUN: not llvm-readobj -s %p/Inputs/invalid-section-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-SIZE %s +INVALID-SECTION-SIZE: Invalid section header size diff --git a/tools/llvm-readobj/ARMEHABIPrinter.h b/tools/llvm-readobj/ARMEHABIPrinter.h index 6ee3c0d4449..55d10b151de 100644 --- a/tools/llvm-readobj/ARMEHABIPrinter.h +++ b/tools/llvm-readobj/ARMEHABIPrinter.h @@ -312,7 +312,6 @@ class PrinterContext { typedef typename object::ELFFile::Elf_Shdr Elf_Shdr; typedef typename object::ELFFile::Elf_Rel_Iter Elf_Rel_iterator; - typedef typename object::ELFFile::Elf_Shdr_Iter Elf_Shdr_iterator; static const size_t IndexTableEntrySize; -- 2.34.1