From cbbdae4851aee0d1b35627c94b39a341b7eef829 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 1 Nov 2016 21:33:55 +0000 Subject: [PATCH] Don't compute DotShstrtab eagerly. This saves a field that is not always used. It also avoids failing a program that doesn't need the section names. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285753 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/ELF.h | 45 +++++++++++------- test/Object/Inputs/invalid-section-index2.elf | Bin 435 -> 0 bytes test/Object/invalid.test | 1 - 3 files changed, 27 insertions(+), 19 deletions(-) delete mode 100644 test/Object/Inputs/invalid-section-index2.elf diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index 8e28b1dc2b4..3e0802e4855 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -73,7 +73,6 @@ private: const Elf_Ehdr *Header; const Elf_Shdr *SectionHeaderTable = nullptr; - StringRef DotShstrtab; // Section header string table. public: template @@ -154,7 +153,8 @@ public: } uint64_t getNumSections() const; - uint32_t getStringTableIndex() const; + ErrorOr getSectionStringTable() const; + uint32_t getSectionStringTableIndex() const; uint32_t getExtendedSymbolTableIndex(const Elf_Sym *Sym, const Elf_Shdr *SymTab, ArrayRef ShndxTable) const; @@ -175,6 +175,8 @@ public: } ErrorOr getSectionName(const Elf_Shdr *Section) const; + ErrorOr getSectionName(const Elf_Shdr *Section, + StringRef DotShstrtab) const; template ErrorOr> getSectionContentsAsArray(const Elf_Shdr *Sec) const; ErrorOr > getSectionContents(const Elf_Shdr *Sec) const; @@ -299,15 +301,26 @@ uint64_t ELFFile::getNumSections() const { return Header->e_shnum; } -template uint32_t ELFFile::getStringTableIndex() const { +template +uint32_t ELFFile::getSectionStringTableIndex() const { if (Header->e_shstrndx == ELF::SHN_XINDEX) return SectionHeaderTable->sh_link; return Header->e_shstrndx; } template -ELFFile::ELFFile(StringRef Object, std::error_code &EC) - : Buf(Object) { +ErrorOr ELFFile::getSectionStringTable() const { + uint32_t Index = getSectionStringTableIndex(); + if (!Index) // no section string table. + return ""; + ErrorOr StrTabSecOrErr = getSection(Index); + if (std::error_code EC = StrTabSecOrErr.getError()) + return EC; + return getStringTable(*StrTabSecOrErr); +} + +template +ELFFile::ELFFile(StringRef Object, std::error_code &EC) : Buf(Object) { const uint64_t FileSize = Buf.size(); if (sizeof(Elf_Ehdr) > FileSize) { @@ -357,19 +370,6 @@ ELFFile::ELFFile(StringRef Object, std::error_code &EC) return; } - // Get string table sections. - uint32_t StringTableIndex = getStringTableIndex(); - if (StringTableIndex) { - ErrorOr StrTabSecOrErr = getSection(StringTableIndex); - if ((EC = StrTabSecOrErr.getError())) - return; - - ErrorOr StringTableOrErr = getStringTable(*StrTabSecOrErr); - if ((EC = StringTableOrErr.getError())) - return; - DotShstrtab = *StringTableOrErr; - } - EC = std::error_code(); } @@ -472,6 +472,15 @@ ELFFile::getStringTableForSymtab(const Elf_Shdr &Sec) const { template ErrorOr ELFFile::getSectionName(const Elf_Shdr *Section) const { + ErrorOr Table = getSectionStringTable(); + if (std::error_code EC = Table.getError()) + return EC; + return getSectionName(Section, *Table); +} + +template +ErrorOr ELFFile::getSectionName(const Elf_Shdr *Section, + StringRef DotShstrtab) const { uint32_t Offset = Section->sh_name; if (Offset == 0) return StringRef(); diff --git a/test/Object/Inputs/invalid-section-index2.elf b/test/Object/Inputs/invalid-section-index2.elf deleted file mode 100644 index 92c372a25f8cdb95ef29a3eb9fc31aaebb14ebac..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 435 zcmb<-^>JfjWMqH=Mg}_u1P><4z;FV=gtEY@gcz7GB;&1 | File INVALID-DYNSYM-SIZE: Invalid entity 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-index2.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 -- 2.40.0