From efdbec8b0a49fb67c3844be703548fdc6c1dded0 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 30 Jan 2014 02:49:50 +0000 Subject: Simplify the handling of iterators in ObjectFile. None of the object file formats reported error on iterator increment. In retrospect, that is not too surprising: no object format stores symbols or sections in a linked list or other structure that requires chasing pointers. As a consequence, all error checking can be done on begin() and end(). This reduces the text segment of bin/llvm-readobj in my machine from 521233 to 518526 bytes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@200442 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/COFF.h | 13 +++++-------- include/llvm/Object/ELFObjectFile.h | 26 ++++++++------------------ include/llvm/Object/MachO.h | 20 +++++++------------- include/llvm/Object/ObjectFile.h | 33 ++++++++++++++------------------- 4 files changed, 34 insertions(+), 58 deletions(-) (limited to 'include') diff --git a/include/llvm/Object/COFF.h b/include/llvm/Object/COFF.h index 2c2bd4823c..6091e61046 100644 --- a/include/llvm/Object/COFF.h +++ b/include/llvm/Object/COFF.h @@ -271,8 +271,7 @@ private: error_code initExportTablePtr(); protected: - error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const - LLVM_OVERRIDE; + void moveSymbolNext(DataRefImpl &Symb) const LLVM_OVERRIDE; error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const LLVM_OVERRIDE; error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const @@ -288,8 +287,7 @@ protected: LLVM_OVERRIDE; error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const LLVM_OVERRIDE; - error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const - LLVM_OVERRIDE; + void moveSectionNext(DataRefImpl &Sec) const LLVM_OVERRIDE; error_code getSectionName(DataRefImpl Sec, StringRef &Res) const LLVM_OVERRIDE; error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const @@ -313,8 +311,7 @@ protected: relocation_iterator section_rel_begin(DataRefImpl Sec) const LLVM_OVERRIDE; relocation_iterator section_rel_end(DataRefImpl Sec) const LLVM_OVERRIDE; - error_code getRelocationNext(DataRefImpl Rel, RelocationRef &Res) const - LLVM_OVERRIDE; + void moveRelocationNext(DataRefImpl &Rel) const LLVM_OVERRIDE; error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const LLVM_OVERRIDE; error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const @@ -397,7 +394,7 @@ public: : ImportTable(Table), Index(I), OwningObject(Owner) {} bool operator==(const ImportDirectoryEntryRef &Other) const; - error_code getNext(ImportDirectoryEntryRef &Result) const; + void moveNext(); error_code getName(StringRef &Result) const; error_code @@ -421,7 +418,7 @@ public: : ExportTable(Table), Index(I), OwningObject(Owner) {} bool operator==(const ExportDirectoryEntryRef &Other) const; - error_code getNext(ExportDirectoryEntryRef &Result) const; + void moveNext(); error_code getDllName(StringRef &Result) const; error_code getOrdinalBase(uint32_t &Result) const; diff --git a/include/llvm/Object/ELFObjectFile.h b/include/llvm/Object/ELFObjectFile.h index 0a16b07e42..45bcde6155 100644 --- a/include/llvm/Object/ELFObjectFile.h +++ b/include/llvm/Object/ELFObjectFile.h @@ -55,8 +55,7 @@ public: protected: ELFFile EF; - error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const - LLVM_OVERRIDE; + void moveSymbolNext(DataRefImpl &Symb) const LLVM_OVERRIDE; error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const LLVM_OVERRIDE; error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const @@ -78,8 +77,7 @@ protected: error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const LLVM_OVERRIDE; - error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const - LLVM_OVERRIDE; + void moveSectionNext(DataRefImpl &Sec) const LLVM_OVERRIDE; error_code getSectionName(DataRefImpl Sec, StringRef &Res) const LLVM_OVERRIDE; error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const @@ -102,8 +100,7 @@ protected: relocation_iterator section_rel_end(DataRefImpl Sec) const LLVM_OVERRIDE; section_iterator getRelocatedSection(DataRefImpl Sec) const LLVM_OVERRIDE; - error_code getRelocationNext(DataRefImpl Rel, RelocationRef &Res) const - LLVM_OVERRIDE; + void moveRelocationNext(DataRefImpl &Rel) const LLVM_OVERRIDE; error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const LLVM_OVERRIDE; error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const @@ -222,10 +219,8 @@ typedef ELFObjectFile > ELF32BEObjectFile; typedef ELFObjectFile > ELF64BEObjectFile; template -error_code ELFObjectFile::getSymbolNext(DataRefImpl Symb, - SymbolRef &Result) const { - Result = SymbolRef(toDRI(++toELFSymIter(Symb)), this); - return object_error::success; +void ELFObjectFile::moveSymbolNext(DataRefImpl &Symb) const { + Symb = toDRI(++toELFSymIter(Symb)); } template @@ -439,10 +434,8 @@ error_code ELFObjectFile::getSymbolValue(DataRefImpl Symb, } template -error_code ELFObjectFile::getSectionNext(DataRefImpl Sec, - SectionRef &Result) const { - Result = SectionRef(toDRI(++toELFShdrIter(Sec)), this); - return object_error::success; +void ELFObjectFile::moveSectionNext(DataRefImpl &Sec) const { + Sec = toDRI(++toELFShdrIter(Sec)); } template @@ -594,11 +587,8 @@ ELFObjectFile::getRelocatedSection(DataRefImpl Sec) const { // Relocations template -error_code ELFObjectFile::getRelocationNext(DataRefImpl Rel, - RelocationRef &Result) const { +void ELFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { ++Rel.d.b; - Result = RelocationRef(Rel, this); - return object_error::success; } template diff --git a/include/llvm/Object/MachO.h b/include/llvm/Object/MachO.h index 731a07be60..fb548e3262 100644 --- a/include/llvm/Object/MachO.h +++ b/include/llvm/Object/MachO.h @@ -38,7 +38,7 @@ public: bool operator==(const DiceRef &Other) const; bool operator<(const DiceRef &Other) const; - error_code getNext(DiceRef &Result) const; + void moveNext(); error_code getOffset(uint32_t &Result) const; error_code getLength(uint16_t &Result) const; @@ -59,8 +59,7 @@ public: MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits, error_code &EC, bool BufferOwned = true); - error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const - LLVM_OVERRIDE; + void moveSymbolNext(DataRefImpl &Symb) const LLVM_OVERRIDE; error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const LLVM_OVERRIDE; error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const @@ -79,8 +78,7 @@ public: error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const LLVM_OVERRIDE; - error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const - LLVM_OVERRIDE; + void moveSectionNext(DataRefImpl &Sec) const LLVM_OVERRIDE; error_code getSectionName(DataRefImpl Sec, StringRef &Res) const LLVM_OVERRIDE; error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const @@ -104,8 +102,7 @@ public: relocation_iterator section_rel_begin(DataRefImpl Sec) const LLVM_OVERRIDE; relocation_iterator section_rel_end(DataRefImpl Sec) const LLVM_OVERRIDE; - error_code getRelocationNext(DataRefImpl Rel, RelocationRef &Res) const - LLVM_OVERRIDE; + void moveRelocationNext(DataRefImpl &Rel) const LLVM_OVERRIDE; error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const LLVM_OVERRIDE; error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const @@ -244,13 +241,10 @@ inline bool DiceRef::operator<(const DiceRef &Other) const { return DicePimpl < Other.DicePimpl; } -inline error_code DiceRef::getNext(DiceRef &Result) const { - DataRefImpl Rel = DicePimpl; +inline void DiceRef::moveNext() { const MachO::data_in_code_entry *P = - reinterpret_cast(Rel.p); - Rel.p = reinterpret_cast(P + 1); - Result = DiceRef(Rel, OwningObject); - return object_error::success; + reinterpret_cast(DicePimpl.p); + DicePimpl.p = reinterpret_cast(P + 1); } // Since a Mach-O data in code reference, a DiceRef, can only be created when diff --git a/include/llvm/Object/ObjectFile.h b/include/llvm/Object/ObjectFile.h index 7533d625b1..1d6c536d4f 100644 --- a/include/llvm/Object/ObjectFile.h +++ b/include/llvm/Object/ObjectFile.h @@ -63,12 +63,8 @@ public: return !(*this == other); } - content_iterator& increment(error_code &err) { - content_type next; - if (error_code ec = Current.getNext(next)) - err = ec; - else - Current = next; + content_iterator &operator++() { // preincrement + Current.moveNext(); return *this; } }; @@ -101,7 +97,7 @@ public: bool operator==(const RelocationRef &Other) const; - error_code getNext(RelocationRef &Result) const; + void moveNext(); error_code getAddress(uint64_t &Result) const; error_code getOffset(uint64_t &Result) const; @@ -146,7 +142,7 @@ public: bool operator==(const SectionRef &Other) const; bool operator<(const SectionRef &Other) const; - error_code getNext(SectionRef &Result) const; + void moveNext(); error_code getName(StringRef &Result) const; error_code getAddress(uint64_t &Result) const; @@ -210,7 +206,7 @@ public: bool operator==(const SymbolRef &Other) const; bool operator<(const SymbolRef &Other) const; - error_code getNext(SymbolRef &Result) const; + void moveNext(); error_code getName(StringRef &Result) const; /// Returns the symbol virtual address (i.e. address at which it will be @@ -285,7 +281,7 @@ protected: // Implementations assume that the DataRefImpl is valid and has not been // modified externally. It's UB otherwise. friend class SymbolRef; - virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0; + virtual void moveSymbolNext(DataRefImpl &Symb) const = 0; virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0; virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const = 0; virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res)const=0; @@ -301,7 +297,7 @@ protected: // Same as above for SectionRef. friend class SectionRef; - virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const = 0; + virtual void moveSectionNext(DataRefImpl &Sec) const = 0; virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0; virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0; virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0; @@ -324,8 +320,7 @@ protected: // Same as above for RelocationRef. friend class RelocationRef; - virtual error_code getRelocationNext(DataRefImpl Rel, - RelocationRef &Res) const = 0; + virtual void moveRelocationNext(DataRefImpl &Rel) const = 0; virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const =0; virtual error_code getRelocationOffset(DataRefImpl Rel, @@ -412,8 +407,8 @@ inline bool SymbolRef::operator<(const SymbolRef &Other) const { return SymbolPimpl < Other.SymbolPimpl; } -inline error_code SymbolRef::getNext(SymbolRef &Result) const { - return OwningObject->getSymbolNext(SymbolPimpl, Result); +inline void SymbolRef::moveNext() { + return OwningObject->moveSymbolNext(SymbolPimpl); } inline error_code SymbolRef::getName(StringRef &Result) const { @@ -471,8 +466,8 @@ inline bool SectionRef::operator<(const SectionRef &Other) const { return SectionPimpl < Other.SectionPimpl; } -inline error_code SectionRef::getNext(SectionRef &Result) const { - return OwningObject->getSectionNext(SectionPimpl, Result); +inline void SectionRef::moveNext() { + return OwningObject->moveSectionNext(SectionPimpl); } inline error_code SectionRef::getName(StringRef &Result) const { @@ -554,8 +549,8 @@ inline bool RelocationRef::operator==(const RelocationRef &Other) const { return RelocationPimpl == Other.RelocationPimpl; } -inline error_code RelocationRef::getNext(RelocationRef &Result) const { - return OwningObject->getRelocationNext(RelocationPimpl, Result); +inline void RelocationRef::moveNext() { + return OwningObject->moveRelocationNext(RelocationPimpl); } inline error_code RelocationRef::getAddress(uint64_t &Result) const { -- cgit v1.2.3