From: Rafael Espindola Date: Wed, 10 Apr 2013 15:33:44 +0000 (+0000) Subject: Template MachOObjectFile over endianness too. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=b08c6df6787971502bd51e30b0f1038c1ea0dc2c;p=oota-llvm.git Template MachOObjectFile over endianness too. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179179 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Object/MachO.h b/include/llvm/Object/MachO.h index f08b3b52579..49800fc381a 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; } } diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index 33c63b0f896..f4df8e012cf 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -35,7 +35,7 @@ MachOObjectFileBase::MachOObjectFileBase(MemoryBuffer *Object, bool Is64bits, } bool MachOObjectFileBase::is64Bit() const { - return isa >(this); + return isa(this); } const MachOObjectFileBase::LoadCommand * @@ -86,9 +86,9 @@ ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { bool Is64Bits = Magic == "\xFE\xED\xFA\xCF" || Magic == "\xCF\xFA\xED\xFE"; ObjectFile *Ret; if (Is64Bits) - Ret = new MachOObjectFile(Buffer, ec); + Ret = new MachOObjectFile64Le(Buffer, ec); else - Ret = new MachOObjectFile(Buffer, ec); + Ret = new MachOObjectFile32Le(Buffer, ec); if (ec) return NULL; return Ret; @@ -127,8 +127,8 @@ MachOObjectFileBase::getSymbolTableEntryBase(DataRefImpl DRI, unsigned Index = DRI.d.b; unsigned SymbolTableEntrySize = is64Bit() ? - sizeof(MachOObjectFile::SymbolTableEntry) : - sizeof(MachOObjectFile::SymbolTableEntry); + sizeof(MachOObjectFile64Le::SymbolTableEntry) : + sizeof(MachOObjectFile32Le::SymbolTableEntry); uint64_t Offset = SymbolTableOffset + Index * SymbolTableEntrySize; StringRef Data = getData(Offset, SymbolTableEntrySize); @@ -314,10 +314,10 @@ MachOObjectFileBase::getSectionBase(DataRefImpl DRI) const { bool Is64 = is64Bit(); unsigned SegmentLoadSize = - Is64 ? sizeof(MachOObjectFile::SegmentLoadCommand) : - sizeof(MachOObjectFile::SegmentLoadCommand); - unsigned SectionSize = Is64 ? sizeof(MachOObjectFile::Section) : - sizeof(MachOObjectFile::Section); + Is64 ? sizeof(MachOObjectFile64Le::SegmentLoadCommand) : + sizeof(MachOObjectFile32Le::SegmentLoadCommand); + unsigned SectionSize = Is64 ? sizeof(MachOObjectFile64Le::Section) : + sizeof(MachOObjectFile32Le::Section); uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + DRI.d.b * SectionSize; return reinterpret_cast(SectionAddr); diff --git a/tools/llvm-readobj/MachODumper.cpp b/tools/llvm-readobj/MachODumper.cpp index 3dad4d6e548..2073ddf4639 100644 --- a/tools/llvm-readobj/MachODumper.cpp +++ b/tools/llvm-readobj/MachODumper.cpp @@ -160,8 +160,8 @@ namespace { static void getSection(const MachOObjectFileBase *Obj, DataRefImpl DRI, MachOSection &Section) { - if (const MachOObjectFile *O = dyn_cast >(Obj)) { - const MachOObjectFile::Section *Sect = O->getSection(DRI); + if (const MachOObjectFile64Le *O = dyn_cast(Obj)) { + const MachOObjectFile64Le::Section *Sect = O->getSection(DRI); Section.Address = Sect->Address; Section.Size = Sect->Size; @@ -173,8 +173,8 @@ static void getSection(const MachOObjectFileBase *Obj, Section.Reserved1 = Sect->Reserved1; Section.Reserved2 = Sect->Reserved2; } else { - const MachOObjectFile *O2 = cast >(Obj); - const MachOObjectFile::Section *Sect = O2->getSection(DRI); + const MachOObjectFile32Le *O2 = cast(Obj); + const MachOObjectFile32Le::Section *Sect = O2->getSection(DRI); Section.Address = Sect->Address; Section.Size = Sect->Size; @@ -191,8 +191,8 @@ static void getSection(const MachOObjectFileBase *Obj, static void getSymbol(const MachOObjectFileBase *Obj, DataRefImpl DRI, MachOSymbol &Symbol) { - if (const MachOObjectFile *O = dyn_cast >(Obj)) { - const MachOObjectFile::SymbolTableEntry *Entry = + if (const MachOObjectFile64Le *O = dyn_cast(Obj)) { + const MachOObjectFile64Le::SymbolTableEntry *Entry = O->getSymbolTableEntry(DRI); Symbol.StringIndex = Entry->StringIndex; Symbol.Type = Entry->Type; @@ -200,8 +200,8 @@ static void getSymbol(const MachOObjectFileBase *Obj, Symbol.Flags = Entry->Flags; Symbol.Value = Entry->Value; } else { - const MachOObjectFile *O2 = cast >(Obj); - const MachOObjectFile::SymbolTableEntry *Entry = + const MachOObjectFile32Le *O2 = cast(Obj); + const MachOObjectFile32Le::SymbolTableEntry *Entry = O2->getSymbolTableEntry(DRI); Symbol.StringIndex = Entry->StringIndex; Symbol.Type = Entry->Type;