From b08c6df6787971502bd51e30b0f1038c1ea0dc2c Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 10 Apr 2013 15:33:44 +0000 Subject: Template MachOObjectFile over endianness too. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179179 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/MachO.h | 189 +++++++++++++++++++++++--------------------- 1 file changed, 100 insertions(+), 89 deletions(-) (limited to 'include/llvm/Object/MachO.h') diff --git a/include/llvm/Object/MachO.h b/include/llvm/Object/MachO.h index f08b3b5257..49800fc381 100644 --- a/include/llvm/Object/MachO.h +++ b/include/llvm/Object/MachO.h @@ -289,34 +289,41 @@ private: const SymtabLoadCommand *SymtabLoadCmd) const; }; -template -struct MachOObjectFileHelperCommon { - typedef MachOFormat::SegmentLoadCommand > +template +struct MachOObjectFileHelperCommon; + +template +struct MachOObjectFileHelperCommon > { + typedef + MachOFormat::SegmentLoadCommand > SegmentLoadCommand; - typedef MachOFormat::SymbolTableEntry > + typedef MachOFormat::SymbolTableEntry > SymbolTableEntry; - typedef MachOFormat::Section > Section; + typedef MachOFormat::Section > Section; }; -template +template struct MachOObjectFileHelper; -template<> -struct MachOObjectFileHelper : - public MachOObjectFileHelperCommon { +template +struct MachOObjectFileHelper > : + public MachOObjectFileHelperCommon > { static const macho::LoadCommandType SegmentLoadType = macho::LCT_Segment; }; -template<> -struct MachOObjectFileHelper : - public MachOObjectFileHelperCommon { +template +struct MachOObjectFileHelper > : + public MachOObjectFileHelperCommon > { static const macho::LoadCommandType SegmentLoadType = macho::LCT_Segment64; }; -template +template class MachOObjectFile : public MachOObjectFileBase { public: - typedef MachOObjectFileHelper Helper; + static const endianness TargetEndianness = MachOT::TargetEndianness; + static const bool Is64Bits = MachOT::Is64Bits; + + typedef MachOObjectFileHelper Helper; static const macho::LoadCommandType SegmentLoadType = Helper::SegmentLoadType; typedef typename Helper::SegmentLoadCommand SegmentLoadCommand; typedef typename Helper::SymbolTableEntry SymbolTableEntry; @@ -357,10 +364,10 @@ public: void moveToNextSection(DataRefImpl &DRI) const; }; -template -MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, - error_code &ec) : - MachOObjectFileBase(Object, is64Bits, ec) { +template +MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, + error_code &ec) : + MachOObjectFileBase(Object, Is64Bits, ec) { DataRefImpl DRI; moveToNextSection(DRI); uint32_t LoadCommandCount = getHeader()->NumLoadCommands; @@ -371,28 +378,28 @@ MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, } } -template -bool MachOObjectFile::classof(const Binary *v) { - return v->getType() == getMachOType(true, is64Bits); +template +bool MachOObjectFile::classof(const Binary *v) { + return v->getType() == getMachOType(true, Is64Bits); } -template -const typename MachOObjectFile::Section * -MachOObjectFile::getSection(DataRefImpl DRI) const { +template +const typename MachOObjectFile::Section * +MachOObjectFile::getSection(DataRefImpl DRI) const { const SectionBase *Addr = getSectionBase(DRI); return reinterpret_cast(Addr); } -template -const typename MachOObjectFile::SymbolTableEntry * -MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { +template +const typename MachOObjectFile::SymbolTableEntry * +MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { const SymbolTableEntryBase *Base = getSymbolTableEntryBase(DRI); return reinterpret_cast(Base); } -template -const typename MachOObjectFile::RelocationEntry * -MachOObjectFile::getRelocation(DataRefImpl Rel) const { +template +const typename MachOObjectFile::RelocationEntry * +MachOObjectFile::getRelocation(DataRefImpl Rel) const { const Section *Sect = getSection(Sections[Rel.d.b]); uint32_t RelOffset = Sect->RelocationTableOffset; uint64_t Offset = RelOffset + Rel.d.a * sizeof(RelocationEntry); @@ -400,53 +407,53 @@ MachOObjectFile::getRelocation(DataRefImpl Rel) const { return reinterpret_cast(Data.data()); } -template +template error_code -MachOObjectFile::getSectionAddress(DataRefImpl Sec, - uint64_t &Res) const { +MachOObjectFile::getSectionAddress(DataRefImpl Sec, + uint64_t &Res) const { const Section *Sect = getSection(Sec); Res = Sect->Address; return object_error::success; } -template +template error_code -MachOObjectFile::getSectionSize(DataRefImpl Sec, - uint64_t &Res) const { +MachOObjectFile::getSectionSize(DataRefImpl Sec, + uint64_t &Res) const { const Section *Sect = getSection(Sec); Res = Sect->Size; return object_error::success; } -template +template error_code -MachOObjectFile::getSectionContents(DataRefImpl Sec, - StringRef &Res) const { +MachOObjectFile::getSectionContents(DataRefImpl Sec, + StringRef &Res) const { const Section *Sect = getSection(Sec); Res = getData(Sect->Offset, Sect->Size); return object_error::success; } -template +template error_code -MachOObjectFile::getSectionAlignment(DataRefImpl Sec, - uint64_t &Res) const { +MachOObjectFile::getSectionAlignment(DataRefImpl Sec, + uint64_t &Res) const { const Section *Sect = getSection(Sec); Res = uint64_t(1) << Sect->Align; return object_error::success; } -template +template error_code -MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const { +MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const { const Section *Sect = getSection(Sec); Res = Sect->Flags & macho::SF_PureInstructions; return object_error::success; } -template +template error_code -MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const { +MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const { const Section *Sect = getSection(Sec); unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType; Res = SectionType == MachO::SectionTypeZeroFill || @@ -454,9 +461,9 @@ MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const { return object_error::success; } -template +template relocation_iterator -MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const { +MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const { const Section *Sect = getSection(Sec); uint32_t LastReloc = Sect->NumRelocationTableEntries; DataRefImpl Ret; @@ -465,10 +472,10 @@ MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const { return relocation_iterator(RelocationRef(Ret, this)); } -template +template error_code -MachOObjectFile::getRelocationAddress(DataRefImpl Rel, - uint64_t &Res) const { +MachOObjectFile::getRelocationAddress(DataRefImpl Rel, + uint64_t &Res) const { const Section *Sect = getSection(Sections[Rel.d.b]); uint64_t SectAddress = Sect->Address; const RelocationEntry *RE = getRelocation(Rel); @@ -486,10 +493,10 @@ MachOObjectFile::getRelocationAddress(DataRefImpl Rel, return object_error::success; } -template +template error_code -MachOObjectFile::getRelocationOffset(DataRefImpl Rel, - uint64_t &Res) const { +MachOObjectFile::getRelocationOffset(DataRefImpl Rel, + uint64_t &Res) const { const RelocationEntry *RE = getRelocation(Rel); unsigned Arch = getArch(); @@ -502,10 +509,10 @@ MachOObjectFile::getRelocationOffset(DataRefImpl Rel, return object_error::success; } -template +template error_code -MachOObjectFile::getRelocationSymbol(DataRefImpl Rel, - SymbolRef &Res) const { +MachOObjectFile::getRelocationSymbol(DataRefImpl Rel, + SymbolRef &Res) const { const RelocationEntry *RE = getRelocation(Rel); uint32_t SymbolIdx = RE->Word1 & 0xffffff; bool isExtern = (RE->Word1 >> 27) & 1; @@ -524,10 +531,10 @@ MachOObjectFile::getRelocationSymbol(DataRefImpl Rel, return object_error::success; } -template +template error_code -MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, - int64_t &Res) const { +MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, + int64_t &Res) const { const RelocationEntry *RE = getRelocation(Rel); bool isExtern = (RE->Word1 >> 27) & 1; Res = 0; @@ -540,9 +547,9 @@ MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, return object_error::success; } -template -error_code MachOObjectFile::getRelocationType(DataRefImpl Rel, - uint64_t &Res) const { +template +error_code MachOObjectFile::getRelocationType(DataRefImpl Rel, + uint64_t &Res) const { const RelocationEntry *RE = getRelocation(Rel); Res = RE->Word0; Res <<= 32; @@ -550,9 +557,9 @@ error_code MachOObjectFile::getRelocationType(DataRefImpl Rel, return object_error::success; } -template +template error_code -MachOObjectFile::getRelocationTypeName(DataRefImpl Rel, +MachOObjectFile::getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl &Result) const { // TODO: Support scattered relocations. StringRef res; @@ -652,9 +659,9 @@ MachOObjectFile::getRelocationTypeName(DataRefImpl Rel, return object_error::success; } -template +template error_code -MachOObjectFile::getRelocationValueString(DataRefImpl Rel, +MachOObjectFile::getRelocationValueString(DataRefImpl Rel, SmallVectorImpl &Result) const { const RelocationEntry *RE = getRelocation(Rel); @@ -864,10 +871,10 @@ MachOObjectFile::getRelocationValueString(DataRefImpl Rel, return object_error::success; } -template +template error_code -MachOObjectFile::getRelocationHidden(DataRefImpl Rel, - bool &Result) const { +MachOObjectFile::getRelocationHidden(DataRefImpl Rel, + bool &Result) const { const RelocationEntry *RE = getRelocation(Rel); unsigned Arch = getArch(); @@ -902,10 +909,10 @@ MachOObjectFile::getRelocationHidden(DataRefImpl Rel, return object_error::success; } -template +template error_code -MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb, - uint64_t &Res) const { +MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb, + uint64_t &Res) const { const SymbolTableEntry *Entry = getSymbolTableEntry(Symb); Res = Entry->Value; if (Entry->SectionIndex) { @@ -916,11 +923,11 @@ MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb, return object_error::success; } -template +template error_code -MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, - DataRefImpl Symb, - bool &Result) const { +MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, + DataRefImpl Symb, + bool &Result) const { SymbolRef::Type ST; getSymbolType(Symb, ST); if (ST == SymbolRef::ST_Unknown) { @@ -940,16 +947,16 @@ MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, return object_error::success; } -template -error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb, - uint64_t &Res) const { +template +error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb, + uint64_t &Res) const { const SymbolTableEntry *Entry = getSymbolTableEntry(Symb); Res = Entry->Value; return object_error::success; } -template -error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI, +template +error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI, uint64_t &Result) const { uint32_t LoadCommandCount = getHeader()->NumLoadCommands; uint64_t BeginOffset; @@ -992,24 +999,24 @@ error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI, return object_error::success; } -template -error_code MachOObjectFile::getSectionNext(DataRefImpl Sec, - SectionRef &Res) const { +template +error_code MachOObjectFile::getSectionNext(DataRefImpl Sec, + SectionRef &Res) const { Sec.d.b++; moveToNextSection(Sec); Res = SectionRef(Sec, this); return object_error::success; } -template -section_iterator MachOObjectFile::begin_sections() const { +template +section_iterator MachOObjectFile::begin_sections() const { DataRefImpl DRI; moveToNextSection(DRI); return section_iterator(SectionRef(DRI, this)); } -template -void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const { +template +void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const { uint32_t LoadCommandCount = getHeader()->NumLoadCommands; while (DRI.d.a < LoadCommandCount) { const LoadCommand *Command = getLoadCommandInfo(DRI.d.a); @@ -1025,6 +1032,10 @@ void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const { } } + typedef MachOObjectFile > + MachOObjectFile32Le; + typedef MachOObjectFile > + MachOObjectFile64Le; } } -- cgit v1.2.3