macho-dump: Add support for dumping symbol table entries.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120218 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2010-11-27 13:52:53 +00:00
parent f2e2a5ff04
commit 2208b58b83
4 changed files with 101 additions and 1 deletions

View File

@ -250,6 +250,25 @@ namespace macho {
uint32_t Reserved3;
};
/// @}
/// @name Symbol Table Entries
/// @{
struct SymbolTableEntry {
uint32_t StringIndex;
uint8_t Type;
uint8_t SectionIndex;
uint16_t Flags;
uint32_t Value;
};
struct Symbol64TableEntry {
uint32_t StringIndex;
uint8_t Type;
uint8_t SectionIndex;
uint16_t Flags;
uint64_t Value;
};
/// @}
/// @name Indirect Symbol Table
/// @{

View File

@ -111,6 +111,11 @@ public:
return StringTable;
}
StringRef getStringAtIndex(unsigned Index) const {
size_t End = getStringTableData().find('\0', Index);
return getStringTableData().slice(Index, End);
}
void RegisterStringTable(macho::SymtabLoadCommand &SLC);
/// @}
@ -157,6 +162,12 @@ public:
void ReadRelocationEntry(
uint64_t RelocationTableOffset, unsigned Index,
InMemoryStruct<macho::RelocationEntry> &Res) const;
void ReadSymbolTableEntry(
uint64_t SymbolTableOffset, unsigned Index,
InMemoryStruct<macho::SymbolTableEntry> &Res) const;
void ReadSymbol64TableEntry(
uint64_t SymbolTableOffset, unsigned Index,
InMemoryStruct<macho::Symbol64TableEntry> &Res) const;
/// @}
};

View File

@ -308,3 +308,31 @@ void MachOObject::ReadRelocationEntry(uint64_t RelocationTableOffset,
Index * sizeof(macho::RelocationEntry));
ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
}
template<>
void SwapStruct(macho::SymbolTableEntry &Value) {
SwapValue(Value.StringIndex);
SwapValue(Value.Flags);
SwapValue(Value.Value);
}
void MachOObject::ReadSymbolTableEntry(uint64_t SymbolTableOffset,
unsigned Index,
InMemoryStruct<macho::SymbolTableEntry> &Res) const {
uint64_t Offset = (SymbolTableOffset +
Index * sizeof(macho::SymbolTableEntry));
ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
}
template<>
void SwapStruct(macho::Symbol64TableEntry &Value) {
SwapValue(Value.StringIndex);
SwapValue(Value.Flags);
SwapValue(Value.Value);
}
void MachOObject::ReadSymbol64TableEntry(uint64_t SymbolTableOffset,
unsigned Index,
InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
uint64_t Offset = (SymbolTableOffset +
Index * sizeof(macho::Symbol64TableEntry));
ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
}

View File

@ -201,6 +201,20 @@ static int DumpSegment64Command(MachOObject &Obj,
return 0;
}
static void DumpSymbolTableEntryData(MachOObject &Obj,
unsigned Index, uint32_t StringIndex,
uint8_t Type, uint8_t SectionIndex,
uint16_t Flags, uint64_t Value) {
outs() << " # Symbol " << Index << "\n";
outs() << " (('n_strx', " << StringIndex << ")\n";
outs() << " ('n_type', " << format("%#x", Type) << ")\n";
outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n";
outs() << " ('n_desc', " << Flags << ")\n";
outs() << " ('n_value', " << Value << ")\n";
outs() << " ('_string', '" << Obj.getStringAtIndex(StringIndex) << "')\n";
outs() << " ),\n";
}
static int DumpSymtabCommand(MachOObject &Obj,
const MachOObject::LoadCommandInfo &LCI) {
InMemoryStruct<macho::SymtabLoadCommand> SLC;
@ -221,7 +235,35 @@ static int DumpSymtabCommand(MachOObject &Obj,
outs().write_escaped(Obj.getStringTableData(),
/*UseHexEscapes=*/true) << "')\n";
return 0;
// Dump the symbol table.
int Res = 0;
outs() << " ('_symbols', [\n";
for (unsigned i = 0; i != SLC->NumSymbolTableEntries; ++i) {
if (Obj.is64Bit()) {
InMemoryStruct<macho::Symbol64TableEntry> STE;
Obj.ReadSymbol64TableEntry(SLC->SymbolTableOffset, i, STE);
if (!STE) {
Res = Error("unable to read symbol: '" + Twine(i) + "'");
break;
}
DumpSymbolTableEntryData(Obj, i, STE->StringIndex, STE->Type,
STE->SectionIndex, STE->Flags, STE->Value);
} else {
InMemoryStruct<macho::SymbolTableEntry> STE;
Obj.ReadSymbolTableEntry(SLC->SymbolTableOffset, i, STE);
if (!SLC) {
Res = Error("unable to read symbol: '" + Twine(i) + "'");
break;
}
DumpSymbolTableEntryData(Obj, i, STE->StringIndex, STE->Type,
STE->SectionIndex, STE->Flags, STE->Value);
}
}
outs() << " ])\n";
return Res;
}
static int DumpDysymtabCommand(MachOObject &Obj,