Add MachOObjectFile::LoadCommandInfo.

This avoids using MachOObject::getLoadCommandInfo.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@178990 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola
2013-04-07 18:08:12 +00:00
parent f305127f0b
commit 77638d9110
3 changed files with 39 additions and 13 deletions

View File

@@ -77,6 +77,11 @@ namespace MachOFormat {
support::ulittle64_t Value; support::ulittle64_t Value;
}; };
struct LoadCommand {
support::ulittle32_t Type;
support::ulittle32_t Size;
};
struct SymtabLoadCommand { struct SymtabLoadCommand {
support::ulittle32_t Type; support::ulittle32_t Type;
support::ulittle32_t Size; support::ulittle32_t Size;
@@ -122,10 +127,16 @@ namespace MachOFormat {
}; };
} }
typedef MachOObject::LoadCommandInfo LoadCommandInfo;
class MachOObjectFile : public ObjectFile { class MachOObjectFile : public ObjectFile {
public: public:
struct LoadCommandInfo {
/// The load command information.
const MachOFormat::LoadCommand *Command;
/// The offset to the start of the load command in memory.
uint64_t Offset;
};
MachOObjectFile(MemoryBuffer *Object, error_code &ec); MachOObjectFile(MemoryBuffer *Object, error_code &ec);
virtual symbol_iterator begin_symbols() const; virtual symbol_iterator begin_symbols() const;
@@ -161,7 +172,7 @@ public:
const MachOFormat::SymbolTableEntry * const MachOFormat::SymbolTableEntry *
getSymbolTableEntry(DataRefImpl DRI) const; getSymbolTableEntry(DataRefImpl DRI) const;
bool is64Bit() const; bool is64Bit() const;
const LoadCommandInfo &getLoadCommandInfo(unsigned Index) const; LoadCommandInfo getLoadCommandInfo(unsigned Index) const;
void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const; void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
const macho::Header &getHeader() const; const macho::Header &getHeader() const;

View File

@@ -58,9 +58,23 @@ bool MachOObjectFile::is64Bit() const {
return MachOObj->is64Bit(); return MachOObj->is64Bit();
} }
const LoadCommandInfo & MachOObjectFile::LoadCommandInfo
MachOObjectFile::getLoadCommandInfo(unsigned Index) const { MachOObjectFile::getLoadCommandInfo(unsigned Index) const {
return MachOObj->getLoadCommandInfo(Index); uint64_t Offset;
uint64_t NewOffset = MachOObj->getHeaderSize();
const MachOFormat::LoadCommand *Load;
unsigned I = 0;
do {
Offset = NewOffset;
StringRef Data = MachOObj->getData(Offset,
sizeof(MachOFormat::LoadCommand));
Load = reinterpret_cast<const MachOFormat::LoadCommand*>(Data.data());
NewOffset = Offset + Load->Size;
++I;
} while (I != Index + 1);
LoadCommandInfo Ret = {Load, Offset};
return Ret;
} }
void MachOObjectFile::ReadULEB128s(uint64_t Index, void MachOObjectFile::ReadULEB128s(uint64_t Index,
@@ -116,7 +130,7 @@ void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands; uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
while (DRI.d.a < LoadCommandCount) { while (DRI.d.a < LoadCommandCount) {
LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a); LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
if (LCI.Command.Type == macho::LCT_Symtab) { if (LCI.Command->Type == macho::LCT_Symtab) {
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd = const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
getSymtabLoadCommand(LCI); getSymtabLoadCommand(LCI);
if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries) if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
@@ -479,12 +493,12 @@ void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands; uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
while (DRI.d.a < LoadCommandCount) { while (DRI.d.a < LoadCommandCount) {
LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a); LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
if (LCI.Command.Type == macho::LCT_Segment) { if (LCI.Command->Type == macho::LCT_Segment) {
const MachOFormat::SegmentLoadCommand *SegmentLoadCmd = const MachOFormat::SegmentLoadCommand *SegmentLoadCmd =
getSegmentLoadCommand(LCI); getSegmentLoadCommand(LCI);
if (DRI.d.b < SegmentLoadCmd->NumSections) if (DRI.d.b < SegmentLoadCmd->NumSections)
return; return;
} else if (LCI.Command.Type == macho::LCT_Segment64) { } else if (LCI.Command->Type == macho::LCT_Segment64) {
const MachOFormat::Segment64LoadCommand *Segment64LoadCmd = const MachOFormat::Segment64LoadCommand *Segment64LoadCmd =
getSegment64LoadCommand(LCI); getSegment64LoadCommand(LCI);
if (DRI.d.b < Segment64LoadCmd->NumSections) if (DRI.d.b < Segment64LoadCmd->NumSections)
@@ -506,10 +520,11 @@ error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
static bool is64BitLoadCommand(const MachOObjectFile *MachOObj, static bool is64BitLoadCommand(const MachOObjectFile *MachOObj,
DataRefImpl DRI) { DataRefImpl DRI) {
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); MachOObjectFile::LoadCommandInfo LCI =
if (LCI.Command.Type == macho::LCT_Segment64) MachOObj->getLoadCommandInfo(DRI.d.a);
if (LCI.Command->Type == macho::LCT_Segment64)
return true; return true;
assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type."); assert(LCI.Command->Type == macho::LCT_Segment && "Unexpected Type.");
return false; return false;
} }

View File

@@ -218,8 +218,8 @@ static void getSectionsAndSymbols(const macho::Header &Header,
} }
for (unsigned i = 0; i != Header.NumLoadCommands; ++i) { for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
const MachOObject::LoadCommandInfo &LCI = MachOObj->getLoadCommandInfo(i); MachOObjectFile::LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(i);
if (LCI.Command.Type == macho::LCT_FunctionStarts) { if (LCI.Command->Type == macho::LCT_FunctionStarts) {
// We found a function starts segment, parse the addresses for later // We found a function starts segment, parse the addresses for later
// consumption. // consumption.
const MachOFormat::LinkeditDataLoadCommand *LLC = const MachOFormat::LinkeditDataLoadCommand *LLC =