refactor the struct byte swapping to a helper function.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179851 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2013-04-19 13:45:05 +00:00
parent baecbb82d8
commit 143d223447

View File

@ -165,28 +165,25 @@ void SwapStruct(macho::Segment64LoadCommand &C) {
SwapValue(C.Flags); SwapValue(C.Flags);
} }
static bool isSwappedEndian(const MachOObjectFile *O) { template<typename T>
return O->isLittleEndian() != sys::IsLittleEndianHost; T getStruct(const MachOObjectFile *O, const char *P) {
T Cmd;
memcpy(&Cmd, P, sizeof(T));
if (O->isLittleEndian() != sys::IsLittleEndianHost)
SwapStruct(Cmd);
return Cmd;
} }
static macho::SegmentLoadCommand static macho::SegmentLoadCommand
getSegmentLoadCommand(const MachOObjectFile *O, getSegmentLoadCommand(const MachOObjectFile *O,
const MachOObjectFile::LoadCommandInfo &L) { const MachOObjectFile::LoadCommandInfo &L) {
macho::SegmentLoadCommand Cmd; return getStruct<macho::SegmentLoadCommand>(O, L.Ptr);
memcpy(&Cmd, L.Ptr, sizeof(macho::SegmentLoadCommand));
if (isSwappedEndian(O))
SwapStruct(Cmd);
return Cmd;
} }
static macho::Segment64LoadCommand static macho::Segment64LoadCommand
getSegment64LoadCommand(const MachOObjectFile *O, getSegment64LoadCommand(const MachOObjectFile *O,
const MachOObjectFile::LoadCommandInfo &L) { const MachOObjectFile::LoadCommandInfo &L) {
macho::Segment64LoadCommand Cmd; return getStruct<macho::Segment64LoadCommand>(O, L.Ptr);
memcpy(&Cmd, L.Ptr, sizeof(macho::Segment64LoadCommand));
if (isSwappedEndian(O))
SwapStruct(Cmd);
return Cmd;
} }
static uint32_t static uint32_t
@ -236,12 +233,7 @@ static const char *getSymbolTableEntryPtr(const MachOObjectFile *O,
static SymbolTableEntryBase static SymbolTableEntryBase
getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) { getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
const char *P = getSymbolTableEntryPtr(O, DRI); const char *P = getSymbolTableEntryPtr(O, DRI);
SymbolTableEntryBase Ret; return getStruct<SymbolTableEntryBase>(O, P);
memcpy(&Ret, P, sizeof(SymbolTableEntryBase));
if (isSwappedEndian(O))
SwapStruct(Ret);
return Ret;
} }
static StringRef parseSegmentOrSectionName(const char *P) { static StringRef parseSegmentOrSectionName(const char *P) {
@ -1366,9 +1358,7 @@ MachOObjectFile::getFirstLoadCommandInfo() const {
unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size; unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size;
Load.Ptr = getPtr(this, HeaderSize); Load.Ptr = getPtr(this, HeaderSize);
memcpy(&Load.C, Load.Ptr, sizeof(macho::LoadCommand)); Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr);
if (isSwappedEndian(this))
SwapStruct(Load.C);
return Load; return Load;
} }
@ -1376,59 +1366,33 @@ MachOObjectFile::LoadCommandInfo
MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const { MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
MachOObjectFile::LoadCommandInfo Next; MachOObjectFile::LoadCommandInfo Next;
Next.Ptr = L.Ptr + L.C.Size; Next.Ptr = L.Ptr + L.C.Size;
memcpy(&Next.C, Next.Ptr, sizeof(macho::LoadCommand)); Next.C = getStruct<macho::LoadCommand>(this, Next.Ptr);
if (isSwappedEndian(this))
SwapStruct(Next.C);
return Next; return Next;
} }
macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const { macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const {
const SectionBase *Addr = return getStruct<macho::Section>(this, Sections[DRI.d.a]);
reinterpret_cast<const SectionBase*>(Sections[DRI.d.a]);
macho::Section Ret;
memcpy(&Ret, Addr, sizeof(macho::Section));
if (isSwappedEndian(this))
SwapStruct(Ret);
return Ret;
} }
macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const { macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
const SectionBase *Addr = return getStruct<macho::Section64>(this, Sections[DRI.d.a]);
reinterpret_cast<const SectionBase*>(Sections[DRI.d.a]);
macho::Section64 Ret;
memcpy(&Ret, Addr, sizeof(macho::Section64));
if (isSwappedEndian(this))
SwapStruct(Ret);
return Ret;
} }
macho::SymbolTableEntry macho::SymbolTableEntry
MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
const char *P = getSymbolTableEntryPtr(this, DRI); const char *P = getSymbolTableEntryPtr(this, DRI);
macho::SymbolTableEntry Ret; return getStruct<macho::SymbolTableEntry>(this, P);
memcpy(&Ret, P, sizeof(macho::SymbolTableEntry));
if (isSwappedEndian(this))
SwapStruct(Ret);
return Ret;
} }
macho::Symbol64TableEntry macho::Symbol64TableEntry
MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
const char *P = getSymbolTableEntryPtr(this, DRI); const char *P = getSymbolTableEntryPtr(this, DRI);
macho::Symbol64TableEntry Ret; return getStruct<macho::Symbol64TableEntry>(this, P);
memcpy(&Ret, P, sizeof(macho::Symbol64TableEntry));
if (isSwappedEndian(this))
SwapStruct(Ret);
return Ret;
} }
macho::LinkeditDataLoadCommand macho::LinkeditDataLoadCommand
MachOObjectFile::getLinkeditDataLoadCommand(const MachOObjectFile::LoadCommandInfo &L) const { MachOObjectFile::getLinkeditDataLoadCommand(const MachOObjectFile::LoadCommandInfo &L) const {
macho::LinkeditDataLoadCommand Cmd; return getStruct<macho::LinkeditDataLoadCommand>(this, L.Ptr);
memcpy(&Cmd, L.Ptr, sizeof(macho::LinkeditDataLoadCommand));
if (isSwappedEndian(this))
SwapStruct(Cmd);
return Cmd;
} }
macho::RelocationEntry macho::RelocationEntry
@ -1445,30 +1409,16 @@ MachOObjectFile::getRelocation(DataRefImpl Rel) const {
} }
uint64_t Offset = RelOffset + Rel.d.a * sizeof(macho::RelocationEntry); uint64_t Offset = RelOffset + Rel.d.a * sizeof(macho::RelocationEntry);
return getStruct<macho::RelocationEntry>(this, getPtr(this, Offset));
macho::RelocationEntry Ret;
memcpy(&Ret, getPtr(this, Offset), sizeof(macho::RelocationEntry));
if (isSwappedEndian(this))
SwapStruct(Ret);
return Ret;
} }
macho::Header MachOObjectFile::getHeader() const { macho::Header MachOObjectFile::getHeader() const {
macho::Header H; return getStruct<macho::Header>(this, getPtr(this, 0));
memcpy(&H, getPtr(this, 0), sizeof(macho::Header));
if (isSwappedEndian(this))
SwapStruct(H);
return H;
} }
macho::SymtabLoadCommand macho::SymtabLoadCommand
MachOObjectFile::getSymtabLoadCommand() const { MachOObjectFile::getSymtabLoadCommand() const {
macho::SymtabLoadCommand Cmd; return getStruct<macho::SymtabLoadCommand>(this, SymtabLoadCmd);
memcpy(&Cmd, SymtabLoadCmd, sizeof(macho::SymtabLoadCommand));
if (isSwappedEndian(this))
SwapStruct(Cmd);
return Cmd;
} }
bool MachOObjectFile::is64Bit() const { bool MachOObjectFile::is64Bit() const {