Template MachOObjectFile over endianness too.

llvm-svn: 179179
This commit is contained in:
Rafael Espindola 2013-04-10 15:33:44 +00:00
parent 6cd7cc7e30
commit 641c9bcfd5
3 changed files with 117 additions and 106 deletions

View File

@ -289,34 +289,41 @@ private:
const SymtabLoadCommand *SymtabLoadCmd) const;
};
template<bool is64Bits>
struct MachOObjectFileHelperCommon {
typedef MachOFormat::SegmentLoadCommand<MachOType<support::little, is64Bits> >
template<class MachOT>
struct MachOObjectFileHelperCommon;
template<endianness TargetEndianness, bool Is64Bits>
struct MachOObjectFileHelperCommon<MachOType<TargetEndianness, Is64Bits> > {
typedef
MachOFormat::SegmentLoadCommand<MachOType<TargetEndianness, Is64Bits> >
SegmentLoadCommand;
typedef MachOFormat::SymbolTableEntry<MachOType<support::little, is64Bits> >
typedef MachOFormat::SymbolTableEntry<MachOType<TargetEndianness, Is64Bits> >
SymbolTableEntry;
typedef MachOFormat::Section<MachOType<support::little, is64Bits> > Section;
typedef MachOFormat::Section<MachOType<TargetEndianness, Is64Bits> > Section;
};
template<bool is64Bits>
template<class MachOT>
struct MachOObjectFileHelper;
template<>
struct MachOObjectFileHelper<false> :
public MachOObjectFileHelperCommon<false> {
template<endianness TargetEndianness>
struct MachOObjectFileHelper<MachOType<TargetEndianness, false> > :
public MachOObjectFileHelperCommon<MachOType<TargetEndianness, false> > {
static const macho::LoadCommandType SegmentLoadType = macho::LCT_Segment;
};
template<>
struct MachOObjectFileHelper<true> :
public MachOObjectFileHelperCommon<true> {
template<endianness TargetEndianness>
struct MachOObjectFileHelper<MachOType<TargetEndianness, true> > :
public MachOObjectFileHelperCommon<MachOType<TargetEndianness, true> > {
static const macho::LoadCommandType SegmentLoadType = macho::LCT_Segment64;
};
template<bool is64Bits>
template<class MachOT>
class MachOObjectFile : public MachOObjectFileBase {
public:
typedef MachOObjectFileHelper<is64Bits> Helper;
static const endianness TargetEndianness = MachOT::TargetEndianness;
static const bool Is64Bits = MachOT::Is64Bits;
typedef MachOObjectFileHelper<MachOT> 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<bool is64Bits>
MachOObjectFile<is64Bits>::MachOObjectFile(MemoryBuffer *Object,
error_code &ec) :
MachOObjectFileBase(Object, is64Bits, ec) {
template<class MachOT>
MachOObjectFile<MachOT>::MachOObjectFile(MemoryBuffer *Object,
error_code &ec) :
MachOObjectFileBase(Object, Is64Bits, ec) {
DataRefImpl DRI;
moveToNextSection(DRI);
uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
@ -371,28 +378,28 @@ MachOObjectFile<is64Bits>::MachOObjectFile(MemoryBuffer *Object,
}
}
template<bool is64Bits>
bool MachOObjectFile<is64Bits>::classof(const Binary *v) {
return v->getType() == getMachOType(true, is64Bits);
template<class MachOT>
bool MachOObjectFile<MachOT>::classof(const Binary *v) {
return v->getType() == getMachOType(true, Is64Bits);
}
template<bool is64Bits>
const typename MachOObjectFile<is64Bits>::Section *
MachOObjectFile<is64Bits>::getSection(DataRefImpl DRI) const {
template<class MachOT>
const typename MachOObjectFile<MachOT>::Section *
MachOObjectFile<MachOT>::getSection(DataRefImpl DRI) const {
const SectionBase *Addr = getSectionBase(DRI);
return reinterpret_cast<const Section*>(Addr);
}
template<bool is64Bits>
const typename MachOObjectFile<is64Bits>::SymbolTableEntry *
MachOObjectFile<is64Bits>::getSymbolTableEntry(DataRefImpl DRI) const {
template<class MachOT>
const typename MachOObjectFile<MachOT>::SymbolTableEntry *
MachOObjectFile<MachOT>::getSymbolTableEntry(DataRefImpl DRI) const {
const SymbolTableEntryBase *Base = getSymbolTableEntryBase(DRI);
return reinterpret_cast<const SymbolTableEntry*>(Base);
}
template<bool is64Bits>
const typename MachOObjectFile<is64Bits>::RelocationEntry *
MachOObjectFile<is64Bits>::getRelocation(DataRefImpl Rel) const {
template<class MachOT>
const typename MachOObjectFile<MachOT>::RelocationEntry *
MachOObjectFile<MachOT>::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<is64Bits>::getRelocation(DataRefImpl Rel) const {
return reinterpret_cast<const RelocationEntry*>(Data.data());
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::getSectionAddress(DataRefImpl Sec,
uint64_t &Res) const {
MachOObjectFile<MachOT>::getSectionAddress(DataRefImpl Sec,
uint64_t &Res) const {
const Section *Sect = getSection(Sec);
Res = Sect->Address;
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::getSectionSize(DataRefImpl Sec,
uint64_t &Res) const {
MachOObjectFile<MachOT>::getSectionSize(DataRefImpl Sec,
uint64_t &Res) const {
const Section *Sect = getSection(Sec);
Res = Sect->Size;
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::getSectionContents(DataRefImpl Sec,
StringRef &Res) const {
MachOObjectFile<MachOT>::getSectionContents(DataRefImpl Sec,
StringRef &Res) const {
const Section *Sect = getSection(Sec);
Res = getData(Sect->Offset, Sect->Size);
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::getSectionAlignment(DataRefImpl Sec,
uint64_t &Res) const {
MachOObjectFile<MachOT>::getSectionAlignment(DataRefImpl Sec,
uint64_t &Res) const {
const Section *Sect = getSection(Sec);
Res = uint64_t(1) << Sect->Align;
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::isSectionText(DataRefImpl Sec, bool &Res) const {
MachOObjectFile<MachOT>::isSectionText(DataRefImpl Sec, bool &Res) const {
const Section *Sect = getSection(Sec);
Res = Sect->Flags & macho::SF_PureInstructions;
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
MachOObjectFile<MachOT>::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<is64Bits>::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
relocation_iterator
MachOObjectFile<is64Bits>::getSectionRelEnd(DataRefImpl Sec) const {
MachOObjectFile<MachOT>::getSectionRelEnd(DataRefImpl Sec) const {
const Section *Sect = getSection(Sec);
uint32_t LastReloc = Sect->NumRelocationTableEntries;
DataRefImpl Ret;
@ -465,10 +472,10 @@ MachOObjectFile<is64Bits>::getSectionRelEnd(DataRefImpl Sec) const {
return relocation_iterator(RelocationRef(Ret, this));
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::getRelocationAddress(DataRefImpl Rel,
uint64_t &Res) const {
MachOObjectFile<MachOT>::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<is64Bits>::getRelocationAddress(DataRefImpl Rel,
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::getRelocationOffset(DataRefImpl Rel,
uint64_t &Res) const {
MachOObjectFile<MachOT>::getRelocationOffset(DataRefImpl Rel,
uint64_t &Res) const {
const RelocationEntry *RE = getRelocation(Rel);
unsigned Arch = getArch();
@ -502,10 +509,10 @@ MachOObjectFile<is64Bits>::getRelocationOffset(DataRefImpl Rel,
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::getRelocationSymbol(DataRefImpl Rel,
SymbolRef &Res) const {
MachOObjectFile<MachOT>::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<is64Bits>::getRelocationSymbol(DataRefImpl Rel,
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::getRelocationAdditionalInfo(DataRefImpl Rel,
int64_t &Res) const {
MachOObjectFile<MachOT>::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<is64Bits>::getRelocationAdditionalInfo(DataRefImpl Rel,
return object_error::success;
}
template<bool is64Bits>
error_code MachOObjectFile<is64Bits>::getRelocationType(DataRefImpl Rel,
uint64_t &Res) const {
template<class MachOT>
error_code MachOObjectFile<MachOT>::getRelocationType(DataRefImpl Rel,
uint64_t &Res) const {
const RelocationEntry *RE = getRelocation(Rel);
Res = RE->Word0;
Res <<= 32;
@ -550,9 +557,9 @@ error_code MachOObjectFile<is64Bits>::getRelocationType(DataRefImpl Rel,
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::getRelocationTypeName(DataRefImpl Rel,
MachOObjectFile<MachOT>::getRelocationTypeName(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const {
// TODO: Support scattered relocations.
StringRef res;
@ -652,9 +659,9 @@ MachOObjectFile<is64Bits>::getRelocationTypeName(DataRefImpl Rel,
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::getRelocationValueString(DataRefImpl Rel,
MachOObjectFile<MachOT>::getRelocationValueString(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const {
const RelocationEntry *RE = getRelocation(Rel);
@ -864,10 +871,10 @@ MachOObjectFile<is64Bits>::getRelocationValueString(DataRefImpl Rel,
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::getRelocationHidden(DataRefImpl Rel,
bool &Result) const {
MachOObjectFile<MachOT>::getRelocationHidden(DataRefImpl Rel,
bool &Result) const {
const RelocationEntry *RE = getRelocation(Rel);
unsigned Arch = getArch();
@ -902,10 +909,10 @@ MachOObjectFile<is64Bits>::getRelocationHidden(DataRefImpl Rel,
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::getSymbolFileOffset(DataRefImpl Symb,
uint64_t &Res) const {
MachOObjectFile<MachOT>::getSymbolFileOffset(DataRefImpl Symb,
uint64_t &Res) const {
const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Res = Entry->Value;
if (Entry->SectionIndex) {
@ -916,11 +923,11 @@ MachOObjectFile<is64Bits>::getSymbolFileOffset(DataRefImpl Symb,
return object_error::success;
}
template<bool is64Bits>
template<class MachOT>
error_code
MachOObjectFile<is64Bits>::sectionContainsSymbol(DataRefImpl Sec,
DataRefImpl Symb,
bool &Result) const {
MachOObjectFile<MachOT>::sectionContainsSymbol(DataRefImpl Sec,
DataRefImpl Symb,
bool &Result) const {
SymbolRef::Type ST;
getSymbolType(Symb, ST);
if (ST == SymbolRef::ST_Unknown) {
@ -940,16 +947,16 @@ MachOObjectFile<is64Bits>::sectionContainsSymbol(DataRefImpl Sec,
return object_error::success;
}
template<bool is64Bits>
error_code MachOObjectFile<is64Bits>::getSymbolAddress(DataRefImpl Symb,
uint64_t &Res) const {
template<class MachOT>
error_code MachOObjectFile<MachOT>::getSymbolAddress(DataRefImpl Symb,
uint64_t &Res) const {
const SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Res = Entry->Value;
return object_error::success;
}
template<bool is64Bits>
error_code MachOObjectFile<is64Bits>::getSymbolSize(DataRefImpl DRI,
template<class MachOT>
error_code MachOObjectFile<MachOT>::getSymbolSize(DataRefImpl DRI,
uint64_t &Result) const {
uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
uint64_t BeginOffset;
@ -992,24 +999,24 @@ error_code MachOObjectFile<is64Bits>::getSymbolSize(DataRefImpl DRI,
return object_error::success;
}
template<bool is64Bits>
error_code MachOObjectFile<is64Bits>::getSectionNext(DataRefImpl Sec,
SectionRef &Res) const {
template<class MachOT>
error_code MachOObjectFile<MachOT>::getSectionNext(DataRefImpl Sec,
SectionRef &Res) const {
Sec.d.b++;
moveToNextSection(Sec);
Res = SectionRef(Sec, this);
return object_error::success;
}
template<bool is64Bits>
section_iterator MachOObjectFile<is64Bits>::begin_sections() const {
template<class MachOT>
section_iterator MachOObjectFile<MachOT>::begin_sections() const {
DataRefImpl DRI;
moveToNextSection(DRI);
return section_iterator(SectionRef(DRI, this));
}
template<bool is64Bits>
void MachOObjectFile<is64Bits>::moveToNextSection(DataRefImpl &DRI) const {
template<class MachOT>
void MachOObjectFile<MachOT>::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<is64Bits>::moveToNextSection(DataRefImpl &DRI) const {
}
}
typedef MachOObjectFile<MachOType<support::little, false> >
MachOObjectFile32Le;
typedef MachOObjectFile<MachOType<support::little, true> >
MachOObjectFile64Le;
}
}

View File

@ -35,7 +35,7 @@ MachOObjectFileBase::MachOObjectFileBase(MemoryBuffer *Object, bool Is64bits,
}
bool MachOObjectFileBase::is64Bit() const {
return isa<MachOObjectFile<true> >(this);
return isa<MachOObjectFile64Le>(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<true>(Buffer, ec);
Ret = new MachOObjectFile64Le(Buffer, ec);
else
Ret = new MachOObjectFile<false>(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<true>::SymbolTableEntry) :
sizeof(MachOObjectFile<false>::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<true>::SegmentLoadCommand) :
sizeof(MachOObjectFile<false>::SegmentLoadCommand);
unsigned SectionSize = Is64 ? sizeof(MachOObjectFile<true>::Section) :
sizeof(MachOObjectFile<false>::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<const SectionBase*>(SectionAddr);

View File

@ -160,8 +160,8 @@ namespace {
static void getSection(const MachOObjectFileBase *Obj,
DataRefImpl DRI,
MachOSection &Section) {
if (const MachOObjectFile<true> *O = dyn_cast<MachOObjectFile<true> >(Obj)) {
const MachOObjectFile<true>::Section *Sect = O->getSection(DRI);
if (const MachOObjectFile64Le *O = dyn_cast<MachOObjectFile64Le>(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<false> *O2 = cast<MachOObjectFile<false> >(Obj);
const MachOObjectFile<false>::Section *Sect = O2->getSection(DRI);
const MachOObjectFile32Le *O2 = cast<MachOObjectFile32Le>(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<true> *O = dyn_cast<MachOObjectFile<true> >(Obj)) {
const MachOObjectFile<true>::SymbolTableEntry *Entry =
if (const MachOObjectFile64Le *O = dyn_cast<MachOObjectFile64Le>(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<false> *O2 = cast<MachOObjectFile<false> >(Obj);
const MachOObjectFile<false>::SymbolTableEntry *Entry =
const MachOObjectFile32Le *O2 = cast<MachOObjectFile32Le>(Obj);
const MachOObjectFile32Le::SymbolTableEntry *Entry =
O2->getSymbolTableEntry(DRI);
Symbol.StringIndex = Entry->StringIndex;
Symbol.Type = Entry->Type;