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
This commit is contained in:
Rafael Espindola
2015-06-30 19:58:10 +00:00
parent 482f87058a
commit 4a04da337e
5 changed files with 22 additions and 29 deletions

View File

@@ -143,8 +143,7 @@ public:
typedef iterator_range<Elf_Dyn_Iter> Elf_Dyn_Range; typedef iterator_range<Elf_Dyn_Iter> Elf_Dyn_Range;
typedef ELFEntityIterator<const Elf_Rela> Elf_Rela_Iter; typedef ELFEntityIterator<const Elf_Rela> Elf_Rela_Iter;
typedef ELFEntityIterator<const Elf_Rel> Elf_Rel_Iter; typedef ELFEntityIterator<const Elf_Rel> Elf_Rel_Iter;
typedef ELFEntityIterator<const Elf_Shdr> Elf_Shdr_Iter; typedef iterator_range<const Elf_Shdr *> Elf_Shdr_Range;
typedef iterator_range<Elf_Shdr_Iter> Elf_Shdr_Range;
/// \brief Archive files are 2 byte aligned, so we need this for /// \brief Archive files are 2 byte aligned, so we need this for
/// PointerIntPair to work. /// PointerIntPair to work.
@@ -264,8 +263,8 @@ public:
Header->getDataEncoding() == ELF::ELFDATA2LSB; Header->getDataEncoding() == ELF::ELFDATA2LSB;
} }
Elf_Shdr_Iter section_begin() const; const Elf_Shdr *section_begin() const;
Elf_Shdr_Iter section_end() const; const Elf_Shdr *section_end() const;
Elf_Shdr_Range sections() const { Elf_Shdr_Range sections() const {
return make_range(section_begin(), section_end()); return make_range(section_begin(), section_end());
} }
@@ -751,16 +750,15 @@ uint64_t ELFFile<ELFT>::getSymbolIndex(const Elf_Sym *Sym) const {
} }
template <class ELFT> template <class ELFT>
typename ELFFile<ELFT>::Elf_Shdr_Iter ELFFile<ELFT>::section_begin() const { const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_begin() const {
return Elf_Shdr_Iter(Header->e_shentsize, if (Header->e_shentsize != sizeof(Elf_Shdr))
(const char *)base() + Header->e_shoff); report_fatal_error("Invalid section header size");
return reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
} }
template <class ELFT> template <class ELFT>
typename ELFFile<ELFT>::Elf_Shdr_Iter ELFFile<ELFT>::section_end() const { const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_end() const {
return Elf_Shdr_Iter(Header->e_shentsize, return section_begin() + getNumSections();
(const char *)base() + Header->e_shoff +
(getNumSections() * Header->e_shentsize));
} }
template <class ELFT> template <class ELFT>

View File

@@ -189,7 +189,6 @@ public:
typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela; typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn; typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter; typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
protected: protected:
@@ -261,15 +260,8 @@ protected:
return DRI; return DRI;
} }
Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const { const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
return Elf_Shdr_Iter(EF.getHeader()->e_shentsize, return reinterpret_cast<const Elf_Shdr *>(Sec.p);
reinterpret_cast<const char *>(Sec.p));
}
DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
DataRefImpl DRI;
DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
return DRI;
} }
DataRefImpl toDRI(const Elf_Shdr *Sec) const { DataRefImpl toDRI(const Elf_Shdr *Sec) const {
@@ -541,7 +533,8 @@ ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
template <class ELFT> template <class ELFT>
void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const { void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
Sec = toDRI(++toELFShdrIter(Sec)); const Elf_Shdr *ESec = toELFShdrIter(Sec);
Sec = toDRI(++ESec);
} }
template <class ELFT> template <class ELFT>
@@ -568,7 +561,7 @@ template <class ELFT>
std::error_code std::error_code
ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec, ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
StringRef &Result) const { 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); Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
return std::error_code(); return std::error_code();
} }
@@ -585,14 +578,14 @@ bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
template <class ELFT> template <class ELFT>
bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const { bool ELFObjectFile<ELFT>::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) && return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
EShdr->sh_type == ELF::SHT_PROGBITS; EShdr->sh_type == ELF::SHT_PROGBITS;
} }
template <class ELFT> template <class ELFT>
bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const { bool ELFObjectFile<ELFT>::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) && return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
EShdr->sh_type == ELF::SHT_NOBITS; EShdr->sh_type == ELF::SHT_NOBITS;
} }
@@ -617,7 +610,7 @@ template <class ELFT>
relocation_iterator relocation_iterator
ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const { ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
DataRefImpl RelData; DataRefImpl RelData;
uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin().get()); uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize; RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
RelData.d.b = 0; RelData.d.b = 0;
return relocation_iterator(RelocationRef(RelData, this)); return relocation_iterator(RelocationRef(RelData, this));
@@ -627,7 +620,7 @@ template <class ELFT>
relocation_iterator relocation_iterator
ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const { ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
DataRefImpl RelData; DataRefImpl RelData;
uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin().get()); uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p); const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize; RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL) if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
@@ -644,7 +637,7 @@ ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
if (EF.getHeader()->e_type != ELF::ET_REL) if (EF.getHeader()->e_type != ELF::ET_REL)
return section_end(); return section_end();
Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); const Elf_Shdr *EShdr = toELFShdrIter(Sec);
uintX_t Type = EShdr->sh_type; uintX_t Type = EShdr->sh_type;
if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA) if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
return section_end(); return section_end();

Binary file not shown.

View File

@@ -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 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! 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

View File

@@ -312,7 +312,6 @@ class PrinterContext {
typedef typename object::ELFFile<ET>::Elf_Shdr Elf_Shdr; typedef typename object::ELFFile<ET>::Elf_Shdr Elf_Shdr;
typedef typename object::ELFFile<ET>::Elf_Rel_Iter Elf_Rel_iterator; typedef typename object::ELFFile<ET>::Elf_Rel_Iter Elf_Rel_iterator;
typedef typename object::ELFFile<ET>::Elf_Shdr_Iter Elf_Shdr_iterator;
static const size_t IndexTableEntrySize; static const size_t IndexTableEntrySize;