Don't use InMemoryStruct<macho::SymtabLoadCommand>.

This also required not using the RegisterStringTable API, which is also a
good thing.

llvm-svn: 178947
This commit is contained in:
Rafael Espindola 2013-04-06 03:31:08 +00:00
parent a65f5de499
commit 15e2a9cd57
2 changed files with 65 additions and 21 deletions

View File

@ -76,6 +76,15 @@ namespace MachOFormat {
support::ulittle16_t Flags;
support::ulittle64_t Value;
};
struct SymtabLoadCommand {
support::ulittle32_t Type;
support::ulittle32_t Size;
support::ulittle32_t SymbolTableOffset;
support::ulittle32_t NumSymbolTableEntries;
support::ulittle32_t StringTableOffset;
support::ulittle32_t StringTableSize;
};
}
typedef MachOObject::LoadCommandInfo LoadCommandInfo;
@ -169,20 +178,32 @@ protected:
private:
OwningPtr<MachOObject> MachOObj;
mutable uint32_t RegisteredStringTable;
typedef SmallVector<DataRefImpl, 1> SectionList;
SectionList Sections;
void moveToNextSection(DataRefImpl &DRI) const;
const MachOFormat::SymbolTableEntry *
getSymbolTableEntry(DataRefImpl DRI) const;
const MachOFormat::SymbolTableEntry *
getSymbolTableEntry(DataRefImpl DRI,
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const;
const MachOFormat::Symbol64TableEntry *
getSymbol64TableEntry(DataRefImpl DRI) const;
const MachOFormat::Symbol64TableEntry *
getSymbol64TableEntry(DataRefImpl DRI,
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const;
void moveToNextSymbol(DataRefImpl &DRI) const;
const MachOFormat::Section *getSection(DataRefImpl DRI) const;
const MachOFormat::Section64 *getSection64(DataRefImpl DRI) const;
const MachOFormat::RelocationEntry *getRelocation(DataRefImpl Rel) const;
const MachOFormat::SymtabLoadCommand *
getSymtabLoadCommand(LoadCommandInfo LCI) const;
std::size_t getSectionIndex(DataRefImpl Sec) const;
void printRelocationTargetName(const MachOFormat::RelocationEntry *RE,

View File

@ -30,8 +30,7 @@ namespace object {
MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
error_code &ec)
: ObjectFile(Binary::ID_MachO, Object, ec),
MachOObj(MOO),
RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
MachOObj(MOO) {
DataRefImpl DRI;
moveToNextSection(DRI);
uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
@ -62,13 +61,20 @@ ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
/*===-- 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());
}
void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
while (DRI.d.a < LoadCommandCount) {
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
if (LCI.Command.Type == macho::LCT_Symtab) {
InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
getSymtabLoadCommand(LCI);
if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
return;
}
@ -80,15 +86,16 @@ void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
const MachOFormat::SymbolTableEntry *
MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
getSymtabLoadCommand(LCI);
if (RegisteredStringTable != DRI.d.a) {
MachOObj->RegisterStringTable(*SymtabLoadCmd);
RegisteredStringTable = DRI.d.a;
}
return getSymbolTableEntry(DRI, SymtabLoadCmd);
}
const MachOFormat::SymbolTableEntry *
MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
unsigned Index = DRI.d.b;
uint64_t Offset = (SymbolTableOffset +
@ -100,15 +107,16 @@ MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
const MachOFormat::Symbol64TableEntry*
MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
getSymtabLoadCommand(LCI);
if (RegisteredStringTable != DRI.d.a) {
MachOObj->RegisterStringTable(*SymtabLoadCmd);
RegisteredStringTable = DRI.d.a;
}
return getSymbol64TableEntry(DRI, SymtabLoadCmd);
}
const MachOFormat::Symbol64TableEntry*
MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
unsigned Index = DRI.d.b;
uint64_t Offset = (SymbolTableOffset +
@ -128,13 +136,28 @@ error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
StringRef &Result) const {
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
getSymtabLoadCommand(LCI);
StringRef StringTable =
MachOObj->getData(SymtabLoadCmd->StringTableOffset,
SymtabLoadCmd->StringTableSize);
uint32_t StringIndex;
if (MachOObj->is64Bit()) {
const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Result = MachOObj->getStringAtIndex(Entry->StringIndex);
const MachOFormat::Symbol64TableEntry *Entry =
getSymbol64TableEntry(DRI, SymtabLoadCmd);
StringIndex = Entry->StringIndex;
} else {
const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Result = MachOObj->getStringAtIndex(Entry->StringIndex);
const MachOFormat::SymbolTableEntry *Entry =
getSymbolTableEntry(DRI, SymtabLoadCmd);
StringIndex = Entry->StringIndex;
}
const char *Start = &StringTable.data()[StringIndex];
Result = StringRef(Start);
return object_error::success;
}