From 143d223447cb55ae08d313078f7a5917873247ae Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 19 Apr 2013 13:45:05 +0000 Subject: [PATCH] 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 --- lib/Object/MachOObjectFile.cpp | 90 ++++++++-------------------------- 1 file changed, 20 insertions(+), 70 deletions(-) diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index d26eb2ce611..2f5048688c0 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -165,28 +165,25 @@ void SwapStruct(macho::Segment64LoadCommand &C) { SwapValue(C.Flags); } -static bool isSwappedEndian(const MachOObjectFile *O) { - return O->isLittleEndian() != sys::IsLittleEndianHost; +template +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 getSegmentLoadCommand(const MachOObjectFile *O, const MachOObjectFile::LoadCommandInfo &L) { - macho::SegmentLoadCommand Cmd; - memcpy(&Cmd, L.Ptr, sizeof(macho::SegmentLoadCommand)); - if (isSwappedEndian(O)) - SwapStruct(Cmd); - return Cmd; + return getStruct(O, L.Ptr); } static macho::Segment64LoadCommand getSegment64LoadCommand(const MachOObjectFile *O, const MachOObjectFile::LoadCommandInfo &L) { - macho::Segment64LoadCommand Cmd; - memcpy(&Cmd, L.Ptr, sizeof(macho::Segment64LoadCommand)); - if (isSwappedEndian(O)) - SwapStruct(Cmd); - return Cmd; + return getStruct(O, L.Ptr); } static uint32_t @@ -236,12 +233,7 @@ static const char *getSymbolTableEntryPtr(const MachOObjectFile *O, static SymbolTableEntryBase getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) { const char *P = getSymbolTableEntryPtr(O, DRI); - SymbolTableEntryBase Ret; - memcpy(&Ret, P, sizeof(SymbolTableEntryBase)); - if (isSwappedEndian(O)) - SwapStruct(Ret); - - return Ret; + return getStruct(O, P); } static StringRef parseSegmentOrSectionName(const char *P) { @@ -1366,9 +1358,7 @@ MachOObjectFile::getFirstLoadCommandInfo() const { unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size; Load.Ptr = getPtr(this, HeaderSize); - memcpy(&Load.C, Load.Ptr, sizeof(macho::LoadCommand)); - if (isSwappedEndian(this)) - SwapStruct(Load.C); + Load.C = getStruct(this, Load.Ptr); return Load; } @@ -1376,59 +1366,33 @@ MachOObjectFile::LoadCommandInfo MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const { MachOObjectFile::LoadCommandInfo Next; Next.Ptr = L.Ptr + L.C.Size; - memcpy(&Next.C, Next.Ptr, sizeof(macho::LoadCommand)); - if (isSwappedEndian(this)) - SwapStruct(Next.C); + Next.C = getStruct(this, Next.Ptr); return Next; } macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const { - const SectionBase *Addr = - reinterpret_cast(Sections[DRI.d.a]); - macho::Section Ret; - memcpy(&Ret, Addr, sizeof(macho::Section)); - if (isSwappedEndian(this)) - SwapStruct(Ret); - return Ret; + return getStruct(this, Sections[DRI.d.a]); } macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const { - const SectionBase *Addr = - reinterpret_cast(Sections[DRI.d.a]); - macho::Section64 Ret; - memcpy(&Ret, Addr, sizeof(macho::Section64)); - if (isSwappedEndian(this)) - SwapStruct(Ret); - return Ret; + return getStruct(this, Sections[DRI.d.a]); } macho::SymbolTableEntry MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { const char *P = getSymbolTableEntryPtr(this, DRI); - macho::SymbolTableEntry Ret; - memcpy(&Ret, P, sizeof(macho::SymbolTableEntry)); - if (isSwappedEndian(this)) - SwapStruct(Ret); - return Ret; + return getStruct(this, P); } macho::Symbol64TableEntry MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { const char *P = getSymbolTableEntryPtr(this, DRI); - macho::Symbol64TableEntry Ret; - memcpy(&Ret, P, sizeof(macho::Symbol64TableEntry)); - if (isSwappedEndian(this)) - SwapStruct(Ret); - return Ret; + return getStruct(this, P); } macho::LinkeditDataLoadCommand MachOObjectFile::getLinkeditDataLoadCommand(const MachOObjectFile::LoadCommandInfo &L) const { - macho::LinkeditDataLoadCommand Cmd; - memcpy(&Cmd, L.Ptr, sizeof(macho::LinkeditDataLoadCommand)); - if (isSwappedEndian(this)) - SwapStruct(Cmd); - return Cmd; + return getStruct(this, L.Ptr); } macho::RelocationEntry @@ -1445,30 +1409,16 @@ MachOObjectFile::getRelocation(DataRefImpl Rel) const { } uint64_t Offset = RelOffset + Rel.d.a * sizeof(macho::RelocationEntry); - - macho::RelocationEntry Ret; - memcpy(&Ret, getPtr(this, Offset), sizeof(macho::RelocationEntry)); - if (isSwappedEndian(this)) - SwapStruct(Ret); - - return Ret; + return getStruct(this, getPtr(this, Offset)); } macho::Header MachOObjectFile::getHeader() const { - macho::Header H; - memcpy(&H, getPtr(this, 0), sizeof(macho::Header)); - if (isSwappedEndian(this)) - SwapStruct(H); - return H; + return getStruct(this, getPtr(this, 0)); } macho::SymtabLoadCommand MachOObjectFile::getSymtabLoadCommand() const { - macho::SymtabLoadCommand Cmd; - memcpy(&Cmd, SymtabLoadCmd, sizeof(macho::SymtabLoadCommand)); - if (isSwappedEndian(this)) - SwapStruct(Cmd); - return Cmd; + return getStruct(this, SymtabLoadCmd); } bool MachOObjectFile::is64Bit() const {