From 433611bdf395d08093e3edd52846c1774b46caf2 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Sun, 7 Apr 2013 19:26:57 +0000 Subject: [PATCH] Implement MachOObjectFile::getHeader directly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@178994 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/MachO.h | 12 +++++++++++- lib/Object/MachOObjectFile.cpp | 29 +++++++++++++++-------------- tools/llvm-objdump/MachODump.cpp | 8 ++++---- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/include/llvm/Object/MachO.h b/include/llvm/Object/MachO.h index 4cdf091fc16..0d20146aae4 100644 --- a/include/llvm/Object/MachO.h +++ b/include/llvm/Object/MachO.h @@ -125,6 +125,16 @@ namespace MachOFormat { support::ulittle32_t DataOffset; support::ulittle32_t DataSize; }; + + struct Header { + support::ulittle32_t Magic; + support::ulittle32_t CPUType; + support::ulittle32_t CPUSubtype; + support::ulittle32_t FileType; + support::ulittle32_t NumLoadCommands; + support::ulittle32_t SizeOfLoadCommands; + support::ulittle32_t Flags; + }; } class MachOObjectFile : public ObjectFile { @@ -164,7 +174,7 @@ public: bool is64Bit() const; const MachOFormat::LoadCommand *getLoadCommandInfo(unsigned Index) const; void ReadULEB128s(uint64_t Index, SmallVectorImpl &Out) const; - const macho::Header &getHeader() const; + const MachOFormat::Header *getHeader() const; unsigned getHeaderSize() const; StringRef getData(size_t Offset, size_t Size) const; diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index f011da1e3a0..31e8be0f94b 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -46,7 +46,7 @@ MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, error_code &ec) DataRefImpl DRI; moveToNextSection(DRI); - uint32_t LoadCommandCount = getHeader().NumLoadCommands; + uint32_t LoadCommandCount = getHeader()->NumLoadCommands; while (DRI.d.a < LoadCommandCount) { Sections.push_back(DRI); DRI.d.b++; @@ -80,8 +80,9 @@ void MachOObjectFile::ReadULEB128s(uint64_t Index, return MachOObj->ReadULEB128s(Index, Out); } -const macho::Header &MachOObjectFile::getHeader() const { - return MachOObj->getHeader(); +const MachOFormat::Header *MachOObjectFile::getHeader() const { + StringRef Data = getData(0, sizeof(MachOFormat::Header)); + return reinterpret_cast(Data.data()); } unsigned MachOObjectFile::getHeaderSize() const { @@ -103,7 +104,7 @@ ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { /*===-- Symbols -----------------------------------------------------------===*/ void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const { - uint32_t LoadCommandCount = getHeader().NumLoadCommands; + uint32_t LoadCommandCount = getHeader()->NumLoadCommands; while (DRI.d.a < LoadCommandCount) { const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a); if (Command->Type == macho::LCT_Symtab) { @@ -229,7 +230,7 @@ error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI, error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI, uint64_t &Result) const { - uint32_t LoadCommandCount = getHeader().NumLoadCommands; + uint32_t LoadCommandCount = getHeader()->NumLoadCommands; uint64_t BeginOffset; uint64_t EndOffset = 0; uint8_t SectionIndex; @@ -431,7 +432,7 @@ symbol_iterator MachOObjectFile::begin_symbols() const { symbol_iterator MachOObjectFile::end_symbols() const { DataRefImpl DRI; - DRI.d.a = getHeader().NumLoadCommands; + DRI.d.a = getHeader()->NumLoadCommands; return symbol_iterator(SymbolRef(DRI, this)); } @@ -463,7 +464,7 @@ StringRef MachOObjectFile::getLoadName() const { /*===-- Sections ----------------------------------------------------------===*/ void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const { - uint32_t LoadCommandCount = getHeader().NumLoadCommands; + uint32_t LoadCommandCount = getHeader()->NumLoadCommands; while (DRI.d.a < LoadCommandCount) { const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a); if (Command->Type == macho::LCT_Segment) { @@ -740,7 +741,7 @@ section_iterator MachOObjectFile::begin_sections() const { section_iterator MachOObjectFile::end_sections() const { DataRefImpl DRI; - DRI.d.a = getHeader().NumLoadCommands; + DRI.d.a = getHeader()->NumLoadCommands; return section_iterator(SectionRef(DRI, this)); } @@ -816,7 +817,7 @@ error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel, for (unsigned i = 0; i < SymbolIdx; i++) { Sym.d.b++; moveToNextSymbol(Sym); - assert(Sym.d.a < getHeader().NumLoadCommands && + assert(Sym.d.a < getHeader()->NumLoadCommands && "Relocation symbol index out of range!"); } } @@ -1300,7 +1301,7 @@ uint8_t MachOObjectFile::getBytesInAddress() const { StringRef MachOObjectFile::getFileFormatName() const { if (!is64Bit()) { - switch (getHeader().CPUType) { + switch (getHeader()->CPUType) { case llvm::MachO::CPUTypeI386: return "Mach-O 32-bit i386"; case llvm::MachO::CPUTypeARM: @@ -1308,18 +1309,18 @@ StringRef MachOObjectFile::getFileFormatName() const { case llvm::MachO::CPUTypePowerPC: return "Mach-O 32-bit ppc"; default: - assert((getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 && + assert((getHeader()->CPUType & llvm::MachO::CPUArchABI64) == 0 && "64-bit object file when we're not 64-bit?"); return "Mach-O 32-bit unknown"; } } // Make sure the cpu type has the correct mask. - assert((getHeader().CPUType & llvm::MachO::CPUArchABI64) + assert((getHeader()->CPUType & llvm::MachO::CPUArchABI64) == llvm::MachO::CPUArchABI64 && "32-bit object file when we're 64-bit?"); - switch (getHeader().CPUType) { + switch (getHeader()->CPUType) { case llvm::MachO::CPUTypeX86_64: return "Mach-O 64-bit x86-64"; case llvm::MachO::CPUTypePowerPC64: @@ -1330,7 +1331,7 @@ StringRef MachOObjectFile::getFileFormatName() const { } unsigned MachOObjectFile::getArch() const { - switch (getHeader().CPUType) { + switch (getHeader()->CPUType) { case llvm::MachO::CPUTypeI386: return Triple::x86; case llvm::MachO::CPUTypeX86_64: diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index 9275fe147ab..7c6565832eb 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -56,7 +56,7 @@ static const Target *GetTarget(const MachOObjectFile *MachOObj) { // Figure out the target triple. if (TripleName.empty()) { llvm::Triple TT("unknown-unknown-unknown"); - switch (MachOObj->getHeader().CPUType) { + switch (MachOObj->getHeader()->CPUType) { case llvm::MachO::CPUTypeI386: TT.setArch(Triple::ArchType(Triple::x86)); break; @@ -199,7 +199,7 @@ static void emitDOTFile(const char *FileName, const MCFunction &f, Out << "}\n"; } -static void getSectionsAndSymbols(const macho::Header &Header, +static void getSectionsAndSymbols(const MachOFormat::Header *Header, MachOObjectFile *MachOObj, std::vector &Sections, std::vector &Symbols, @@ -217,7 +217,7 @@ static void getSectionsAndSymbols(const macho::Header &Header, Sections.push_back(*SI); } - for (unsigned i = 0; i != Header.NumLoadCommands; ++i) { + for (unsigned i = 0; i != Header->NumLoadCommands; ++i) { const MachOFormat::LoadCommand *Command = MachOObj->getLoadCommandInfo(i); if (Command->Type == macho::LCT_FunctionStarts) { // We found a function starts segment, parse the addresses for later @@ -269,7 +269,7 @@ void llvm::DisassembleInputMachO(StringRef Filename) { outs() << '\n' << Filename << ":\n\n"; - const macho::Header &Header = MachOOF->getHeader(); + const MachOFormat::Header *Header = MachOOF->getHeader(); std::vector Sections; std::vector Symbols;