From 804fd781f18c7252fd1c411acfb04721949425a3 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 5 Apr 2013 18:45:28 +0000 Subject: [PATCH] Define versions of Section that are explicitly marked as little endian. These should really be templated like ELF, but this is a start. llvm-svn: 178896 --- include/llvm/Object/MachO.h | 36 +++++++++++++++++- lib/Object/MachOObjectFile.cpp | 68 +++++++++++++++++----------------- 2 files changed, 68 insertions(+), 36 deletions(-) diff --git a/include/llvm/Object/MachO.h b/include/llvm/Object/MachO.h index b5aa539a7e4..badd4401e77 100644 --- a/include/llvm/Object/MachO.h +++ b/include/llvm/Object/MachO.h @@ -19,12 +19,44 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/Object/MachOObject.h" #include "llvm/Object/ObjectFile.h" +#include "llvm/Support/Endian.h" #include "llvm/Support/MachO.h" #include "llvm/Support/raw_ostream.h" namespace llvm { namespace object { +namespace MachOFormat { + struct Section { + char Name[16]; + char SegmentName[16]; + support::ulittle32_t Address; + support::ulittle32_t Size; + support::ulittle32_t Offset; + support::ulittle32_t Align; + support::ulittle32_t RelocationTableOffset; + support::ulittle32_t NumRelocationTableEntries; + support::ulittle32_t Flags; + support::ulittle32_t Reserved1; + support::ulittle32_t Reserved2; + }; + + struct Section64 { + char Name[16]; + char SegmentName[16]; + support::ulittle64_t Address; + support::ulittle64_t Size; + support::ulittle32_t Offset; + support::ulittle32_t Align; + support::ulittle32_t RelocationTableOffset; + support::ulittle32_t NumRelocationTableEntries; + support::ulittle32_t Flags; + support::ulittle32_t Reserved1; + support::ulittle32_t Reserved2; + support::ulittle32_t Reserved3; + }; +} + typedef MachOObject::LoadCommandInfo LoadCommandInfo; class MachOObjectFile : public ObjectFile { @@ -127,8 +159,8 @@ private: void getSymbol64TableEntry(DataRefImpl DRI, InMemoryStruct &Res) const; void moveToNextSymbol(DataRefImpl &DRI) const; - const macho::Section *getSection(DataRefImpl DRI) const; - const macho::Section64 *getSection64(DataRefImpl DRI) const; + const MachOFormat::Section *getSection(DataRefImpl DRI) const; + const MachOFormat::Section64 *getSection64(DataRefImpl DRI) const; void getRelocation(DataRefImpl Rel, InMemoryStruct &Res) const; std::size_t getSectionIndex(DataRefImpl Sec) const; diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index 684a43b48b1..7de5462dd3a 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -138,7 +138,7 @@ error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI, getSymbol64TableEntry(DRI, Entry); Result = Entry->Value; if (Entry->SectionIndex) { - const macho::Section64 *Section = + const MachOFormat::Section64 *Section = getSection64(Sections[Entry->SectionIndex-1]); Result += Section->Offset - Section->Address; } @@ -147,7 +147,7 @@ error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI, getSymbolTableEntry(DRI, Entry); Result = Entry->Value; if (Entry->SectionIndex) { - const macho::Section *Section = + const MachOFormat::Section *Section = getSection(Sections[Entry->SectionIndex-1]); Result += Section->Offset - Section->Address; } @@ -452,13 +452,13 @@ static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) { return false; } -const macho::Section *MachOObjectFile::getSection(DataRefImpl DRI) const { +const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const { assert(!is64BitLoadCommand(MachOObj.get(), DRI)); LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) + - DRI.d.b * sizeof(macho::Section); - StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section)); - return reinterpret_cast(Data.data()); + DRI.d.b * sizeof(MachOFormat::Section); + StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section)); + return reinterpret_cast(Data.data()); } std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const { @@ -468,14 +468,14 @@ std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const { return std::distance(Sections.begin(), loc); } -const macho::Section64 * +const MachOFormat::Section64 * MachOObjectFile::getSection64(DataRefImpl DRI) const { assert(is64BitLoadCommand(MachOObj.get(), DRI)); LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) + - DRI.d.b * sizeof(macho::Section64); - StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64)); - return reinterpret_cast(Data.data()); + DRI.d.b * sizeof(MachOFormat::Section64); + StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64)); + return reinterpret_cast(Data.data()); } static StringRef parseSegmentOrSectionName(const char *P) { @@ -488,10 +488,10 @@ static StringRef parseSegmentOrSectionName(const char *P) { ArrayRef MachOObjectFile::getSectionRawName(DataRefImpl DRI) const { if (is64BitLoadCommand(MachOObj.get(), DRI)) { - const macho::Section64 *sec = getSection64(DRI); + const MachOFormat::Section64 *sec = getSection64(DRI); return ArrayRef(sec->Name); } else { - const macho::Section *sec = getSection(DRI); + const MachOFormat::Section *sec = getSection(DRI); return ArrayRef(sec->Name); } } @@ -506,10 +506,10 @@ error_code MachOObjectFile::getSectionName(DataRefImpl DRI, ArrayRef MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const { if (is64BitLoadCommand(MachOObj.get(), Sec)) { - const macho::Section64 *sec = getSection64(Sec); + const MachOFormat::Section64 *sec = getSection64(Sec); return ArrayRef(sec->SegmentName, 16); } else { - const macho::Section *sec = getSection(Sec); + const MachOFormat::Section *sec = getSection(Sec); return ArrayRef(sec->SegmentName); } } @@ -522,10 +522,10 @@ StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const { error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI, uint64_t &Result) const { if (is64BitLoadCommand(MachOObj.get(), DRI)) { - const macho::Section64 *Sect = getSection64(DRI); + const MachOFormat::Section64 *Sect = getSection64(DRI); Result = Sect->Address; } else { - const macho::Section *Sect = getSection(DRI); + const MachOFormat::Section *Sect = getSection(DRI); Result = Sect->Address; } return object_error::success; @@ -534,10 +534,10 @@ error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI, error_code MachOObjectFile::getSectionSize(DataRefImpl DRI, uint64_t &Result) const { if (is64BitLoadCommand(MachOObj.get(), DRI)) { - const macho::Section64 *Sect = getSection64(DRI); + const MachOFormat::Section64 *Sect = getSection64(DRI); Result = Sect->Size; } else { - const macho::Section *Sect = getSection(DRI); + const MachOFormat::Section *Sect = getSection(DRI); Result = Sect->Size; } return object_error::success; @@ -546,10 +546,10 @@ error_code MachOObjectFile::getSectionSize(DataRefImpl DRI, error_code MachOObjectFile::getSectionContents(DataRefImpl DRI, StringRef &Result) const { if (is64BitLoadCommand(MachOObj.get(), DRI)) { - const macho::Section64 *Sect = getSection64(DRI); + const MachOFormat::Section64 *Sect = getSection64(DRI); Result = MachOObj->getData(Sect->Offset, Sect->Size); } else { - const macho::Section *Sect = getSection(DRI); + const MachOFormat::Section *Sect = getSection(DRI); Result = MachOObj->getData(Sect->Offset, Sect->Size); } return object_error::success; @@ -558,10 +558,10 @@ error_code MachOObjectFile::getSectionContents(DataRefImpl DRI, error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI, uint64_t &Result) const { if (is64BitLoadCommand(MachOObj.get(), DRI)) { - const macho::Section64 *Sect = getSection64(DRI); + const MachOFormat::Section64 *Sect = getSection64(DRI); Result = uint64_t(1) << Sect->Align; } else { - const macho::Section *Sect = getSection(DRI); + const MachOFormat::Section *Sect = getSection(DRI); Result = uint64_t(1) << Sect->Align; } return object_error::success; @@ -570,10 +570,10 @@ error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI, error_code MachOObjectFile::isSectionText(DataRefImpl DRI, bool &Result) const { if (is64BitLoadCommand(MachOObj.get(), DRI)) { - const macho::Section64 *Sect = getSection64(DRI); + const MachOFormat::Section64 *Sect = getSection64(DRI); Result = Sect->Flags & macho::SF_PureInstructions; } else { - const macho::Section *Sect = getSection(DRI); + const MachOFormat::Section *Sect = getSection(DRI); Result = Sect->Flags & macho::SF_PureInstructions; } return object_error::success; @@ -610,12 +610,12 @@ error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec, error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI, bool &Result) const { if (MachOObj->is64Bit()) { - const macho::Section64 *Sect = getSection64(DRI); + const MachOFormat::Section64 *Sect = getSection64(DRI); unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType; Result = (SectionType == MachO::SectionTypeZeroFill || SectionType == MachO::SectionTypeZeroFillLarge); } else { - const macho::Section *Sect = getSection(DRI); + const MachOFormat::Section *Sect = getSection(DRI); unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType; Result = (SectionType == MachO::SectionTypeZeroFill || SectionType == MachO::SectionTypeZeroFillLarge); @@ -673,10 +673,10 @@ relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const { relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const { uint32_t last_reloc; if (is64BitLoadCommand(MachOObj.get(), Sec)) { - const macho::Section64 *Sect = getSection64(Sec); + const MachOFormat::Section64 *Sect = getSection64(Sec); last_reloc = Sect->NumRelocationTableEntries; } else { - const macho::Section *Sect = getSection(Sec); + const MachOFormat::Section *Sect = getSection(Sec); last_reloc = Sect->NumRelocationTableEntries; } DataRefImpl ret; @@ -704,10 +704,10 @@ getRelocation(DataRefImpl Rel, InMemoryStruct &Res) const { uint32_t relOffset; if (MachOObj->is64Bit()) { - const macho::Section64 *Sect = getSection64(Sections[Rel.d.b]); + const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]); relOffset = Sect->RelocationTableOffset; } else { - const macho::Section *Sect = getSection(Sections[Rel.d.b]); + const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]); relOffset = Sect->RelocationTableOffset; } MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res); @@ -722,10 +722,10 @@ error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const { const uint8_t* sectAddress = 0; if (MachOObj->is64Bit()) { - const macho::Section64 *Sect = getSection64(Sections[Rel.d.b]); + const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]); sectAddress += Sect->Address; } else { - const macho::Section *Sect = getSection(Sections[Rel.d.b]); + const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]); sectAddress += Sect->Address; } InMemoryStruct RE; @@ -895,10 +895,10 @@ error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, if (!isExtern) { const uint8_t* sectAddress = base(); if (MachOObj->is64Bit()) { - const macho::Section64 *Sect = getSection64(Sections[Rel.d.b]); + const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]); sectAddress += Sect->Offset; } else { - const macho::Section *Sect = getSection(Sections[Rel.d.b]); + const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]); sectAddress += Sect->Offset; } Res = reinterpret_cast(sectAddress);