Remove LoadCommandInfo now that we always have a pointer to the command.

LoadCommandInfo was needed to keep a command and its offset in the file. Now
that we always have a pointer to the command, we don't need the offset.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@178991 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2013-04-07 18:42:06 +00:00
parent 77638d9110
commit 6ab85a81d7
3 changed files with 31 additions and 78 deletions

View File

@ -129,14 +129,6 @@ namespace MachOFormat {
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;
@ -163,8 +155,6 @@ public:
ArrayRef<char> getSectionRawName(DataRefImpl Sec) const; ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const; ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const;
const MachOFormat::LinkeditDataLoadCommand *
getLinkeditDataLoadCommand(LoadCommandInfo LCI) const;
const MachOFormat::Section64 *getSection64(DataRefImpl DRI) const; const MachOFormat::Section64 *getSection64(DataRefImpl DRI) const;
const MachOFormat::Section *getSection(DataRefImpl DRI) const; const MachOFormat::Section *getSection(DataRefImpl DRI) const;
const MachOFormat::Symbol64TableEntry * const MachOFormat::Symbol64TableEntry *
@ -172,7 +162,7 @@ public:
const MachOFormat::SymbolTableEntry * const MachOFormat::SymbolTableEntry *
getSymbolTableEntry(DataRefImpl DRI) const; getSymbolTableEntry(DataRefImpl DRI) const;
bool is64Bit() const; bool is64Bit() const;
LoadCommandInfo getLoadCommandInfo(unsigned Index) const; const MachOFormat::LoadCommand *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;
@ -251,12 +241,6 @@ private:
void moveToNextSymbol(DataRefImpl &DRI) const; void moveToNextSymbol(DataRefImpl &DRI) const;
const MachOFormat::RelocationEntry *getRelocation(DataRefImpl Rel) const; const MachOFormat::RelocationEntry *getRelocation(DataRefImpl Rel) const;
const MachOFormat::SymtabLoadCommand *
getSymtabLoadCommand(LoadCommandInfo LCI) const;
const MachOFormat::SegmentLoadCommand *
getSegmentLoadCommand(LoadCommandInfo LCI) const;
const MachOFormat::Segment64LoadCommand *
getSegment64LoadCommand(LoadCommandInfo LCI) const;
std::size_t getSectionIndex(DataRefImpl Sec) const; std::size_t getSectionIndex(DataRefImpl Sec) const;
void printRelocationTargetName(const MachOFormat::RelocationEntry *RE, void printRelocationTargetName(const MachOFormat::RelocationEntry *RE,

View File

@ -58,7 +58,7 @@ bool MachOObjectFile::is64Bit() const {
return MachOObj->is64Bit(); return MachOObj->is64Bit();
} }
MachOObjectFile::LoadCommandInfo const MachOFormat::LoadCommand *
MachOObjectFile::getLoadCommandInfo(unsigned Index) const { MachOObjectFile::getLoadCommandInfo(unsigned Index) const {
uint64_t Offset; uint64_t Offset;
uint64_t NewOffset = MachOObj->getHeaderSize(); uint64_t NewOffset = MachOObj->getHeaderSize();
@ -73,8 +73,7 @@ MachOObjectFile::getLoadCommandInfo(unsigned Index) const {
++I; ++I;
} while (I != Index + 1); } while (I != Index + 1);
LoadCommandInfo Ret = {Load, Offset}; return Load;
return Ret;
} }
void MachOObjectFile::ReadULEB128s(uint64_t Index, void MachOObjectFile::ReadULEB128s(uint64_t Index,
@ -96,43 +95,13 @@ ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
/*===-- Symbols -----------------------------------------------------------===*/ /*===-- Symbols -----------------------------------------------------------===*/
const MachOFormat::SymtabLoadCommand *
MachOObjectFile::getSymtabLoadCommand(LoadCommandInfo LCI) const {
StringRef Data = MachOObj->getData(LCI.Offset,
sizeof(MachOFormat::SymtabLoadCommand));
return reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Data.data());
}
const MachOFormat::SegmentLoadCommand *
MachOObjectFile::getSegmentLoadCommand(LoadCommandInfo LCI) const {
StringRef Data = MachOObj->getData(LCI.Offset,
sizeof(MachOFormat::SegmentLoadCommand));
return reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Data.data());
}
const MachOFormat::LinkeditDataLoadCommand *
MachOObjectFile::getLinkeditDataLoadCommand(LoadCommandInfo LCI) const {
StringRef Data = MachOObj->getData(LCI.Offset,
sizeof(MachOFormat::LinkeditDataLoadCommand));
return
reinterpret_cast<const MachOFormat::LinkeditDataLoadCommand*>(Data.data());
}
const MachOFormat::Segment64LoadCommand *
MachOObjectFile::getSegment64LoadCommand(LoadCommandInfo LCI) const {
StringRef Data = MachOObj->getData(LCI.Offset,
sizeof(MachOFormat::Segment64LoadCommand));
return
reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Data.data());
}
void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const { 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); const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
if (LCI.Command->Type == macho::LCT_Symtab) { if (Command->Type == macho::LCT_Symtab) {
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd = const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
getSymtabLoadCommand(LCI); reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries) if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
return; return;
} }
@ -144,9 +113,9 @@ void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
const MachOFormat::SymbolTableEntry * const MachOFormat::SymbolTableEntry *
MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a); const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd = const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
getSymtabLoadCommand(LCI); reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
return getSymbolTableEntry(DRI, SymtabLoadCmd); return getSymbolTableEntry(DRI, SymtabLoadCmd);
} }
@ -165,9 +134,9 @@ MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
const MachOFormat::Symbol64TableEntry* const MachOFormat::Symbol64TableEntry*
MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a); const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd = const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
getSymtabLoadCommand(LCI); reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
return getSymbol64TableEntry(DRI, SymtabLoadCmd); return getSymbol64TableEntry(DRI, SymtabLoadCmd);
} }
@ -194,9 +163,9 @@ error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
error_code MachOObjectFile::getSymbolName(DataRefImpl DRI, error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
StringRef &Result) const { StringRef &Result) const {
LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a); const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd = const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
getSymtabLoadCommand(LCI); reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
StringRef StringTable = StringRef StringTable =
MachOObj->getData(SymtabLoadCmd->StringTableOffset, MachOObj->getData(SymtabLoadCmd->StringTableOffset,
@ -492,15 +461,15 @@ StringRef MachOObjectFile::getLoadName() const {
void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const { 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); const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
if (LCI.Command->Type == macho::LCT_Segment) { if (Command->Type == macho::LCT_Segment) {
const MachOFormat::SegmentLoadCommand *SegmentLoadCmd = const MachOFormat::SegmentLoadCommand *SegmentLoadCmd =
getSegmentLoadCommand(LCI); reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Command);
if (DRI.d.b < SegmentLoadCmd->NumSections) if (DRI.d.b < SegmentLoadCmd->NumSections)
return; return;
} else if (LCI.Command->Type == macho::LCT_Segment64) { } else if (Command->Type == macho::LCT_Segment64) {
const MachOFormat::Segment64LoadCommand *Segment64LoadCmd = const MachOFormat::Segment64LoadCommand *Segment64LoadCmd =
getSegment64LoadCommand(LCI); reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Command);
if (DRI.d.b < Segment64LoadCmd->NumSections) if (DRI.d.b < Segment64LoadCmd->NumSections)
return; return;
} }
@ -520,21 +489,21 @@ error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
static bool is64BitLoadCommand(const MachOObjectFile *MachOObj, static bool is64BitLoadCommand(const MachOObjectFile *MachOObj,
DataRefImpl DRI) { DataRefImpl DRI) {
MachOObjectFile::LoadCommandInfo LCI = const MachOFormat::LoadCommand *Command =
MachOObj->getLoadCommandInfo(DRI.d.a); MachOObj->getLoadCommandInfo(DRI.d.a);
if (LCI.Command->Type == macho::LCT_Segment64) if (Command->Type == macho::LCT_Segment64)
return true; return true;
assert(LCI.Command->Type == macho::LCT_Segment && "Unexpected Type."); assert(Command->Type == macho::LCT_Segment && "Unexpected Type.");
return false; return false;
} }
const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const { const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
assert(!is64BitLoadCommand(this, DRI)); assert(!is64BitLoadCommand(this, DRI));
LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a); const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) + uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(Command);
uintptr_t SectionAddr = CommandAddr + sizeof(macho::SegmentLoadCommand) +
DRI.d.b * sizeof(MachOFormat::Section); DRI.d.b * sizeof(MachOFormat::Section);
StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section)); return reinterpret_cast<const MachOFormat::Section*>(SectionAddr);
return reinterpret_cast<const MachOFormat::Section*>(Data.data());
} }
std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const { std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
@ -547,11 +516,11 @@ std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
const MachOFormat::Section64 * const MachOFormat::Section64 *
MachOObjectFile::getSection64(DataRefImpl DRI) const { MachOObjectFile::getSection64(DataRefImpl DRI) const {
assert(is64BitLoadCommand(this, DRI)); assert(is64BitLoadCommand(this, DRI));
LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a); const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) + uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(Command);
uintptr_t SectionAddr = CommandAddr + sizeof(macho::Segment64LoadCommand) +
DRI.d.b * sizeof(MachOFormat::Section64); DRI.d.b * sizeof(MachOFormat::Section64);
StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64)); return reinterpret_cast<const MachOFormat::Section64*>(SectionAddr);
return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
} }
static StringRef parseSegmentOrSectionName(const char *P) { static StringRef parseSegmentOrSectionName(const char *P) {

View File

@ -218,12 +218,12 @@ static void getSectionsAndSymbols(const macho::Header &Header,
} }
for (unsigned i = 0; i != Header.NumLoadCommands; ++i) { for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
MachOObjectFile::LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(i); const MachOFormat::LoadCommand *Command = MachOObj->getLoadCommandInfo(i);
if (LCI.Command->Type == macho::LCT_FunctionStarts) { if (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 =
MachOObj->getLinkeditDataLoadCommand(LCI); reinterpret_cast<const MachOFormat::LinkeditDataLoadCommand*>(Command);
MachOObj->ReadULEB128s(LLC->DataOffset, FoundFns); MachOObj->ReadULEB128s(LLC->DataOffset, FoundFns);
} }