mirror of
https://github.com/RPCSX/llvm.git
synced 2024-11-24 12:19:53 +00:00
[Object, MachO] Introduce MachOObjectFile::load_commands() range iterator.
Summary: Now users don't have to manually deal with getFirstLoadCommandInfo() / getNextLoadCommandInfo(), calculate the number of load segments, etc. No functionality change. Test Plan: regression test suite Reviewers: rafael, lhames, loladiro Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D10144 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@238983 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
5eb7481d03
commit
40ec40a179
@ -190,6 +190,8 @@ public:
|
||||
const char *Ptr; // Where in memory the load command is.
|
||||
MachO::load_command C; // The command itself.
|
||||
};
|
||||
typedef SmallVector<LoadCommandInfo, 4> LoadCommandList;
|
||||
typedef LoadCommandList::const_iterator load_command_iterator;
|
||||
|
||||
MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
|
||||
std::error_code &EC);
|
||||
@ -270,10 +272,14 @@ public:
|
||||
|
||||
dice_iterator begin_dices() const;
|
||||
dice_iterator end_dices() const;
|
||||
|
||||
|
||||
load_command_iterator begin_load_commands() const;
|
||||
load_command_iterator end_load_commands() const;
|
||||
iterator_range<load_command_iterator> load_commands() const;
|
||||
|
||||
/// For use iterating over all exported symbols.
|
||||
iterator_range<export_iterator> exports() const;
|
||||
|
||||
|
||||
/// For use examining a trie not in a MachOObjectFile.
|
||||
static iterator_range<export_iterator> exports(ArrayRef<uint8_t> Trie);
|
||||
|
||||
@ -326,10 +332,6 @@ public:
|
||||
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
|
||||
SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const;
|
||||
|
||||
// Walk load commands.
|
||||
LoadCommandInfo getFirstLoadCommandInfo() const;
|
||||
LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
|
||||
|
||||
// MachO specific structures.
|
||||
MachO::section getSection(DataRefImpl DRI) const;
|
||||
MachO::section_64 getSection64(DataRefImpl DRI) const;
|
||||
@ -427,10 +429,15 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
// Walk load commands.
|
||||
LoadCommandInfo getFirstLoadCommandInfo() const;
|
||||
LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
|
||||
|
||||
typedef SmallVector<const char*, 1> SectionList;
|
||||
SectionList Sections;
|
||||
typedef SmallVector<const char*, 1> LibraryList;
|
||||
LibraryList Libraries;
|
||||
LoadCommandList LoadCommands;
|
||||
typedef SmallVector<StringRef, 1> LibraryShortName;
|
||||
mutable LibraryShortName LibrariesShortNames;
|
||||
const char *SymtabLoadCmd;
|
||||
|
@ -194,8 +194,9 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
|
||||
MachO::LoadCommandType SegmentLoadType = is64Bit() ?
|
||||
MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
|
||||
|
||||
MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
|
||||
for (unsigned I = 0; ; ++I) {
|
||||
LoadCommandInfo Load = getFirstLoadCommandInfo();
|
||||
for (unsigned I = 0; I < LoadCommandCount; ++I) {
|
||||
LoadCommands.push_back(Load);
|
||||
if (Load.C.cmd == MachO::LC_SYMTAB) {
|
||||
// Multiple symbol tables
|
||||
if (SymtabLoadCmd) {
|
||||
@ -260,12 +261,10 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
|
||||
Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
|
||||
Libraries.push_back(Load.Ptr);
|
||||
}
|
||||
|
||||
if (I == LoadCommandCount - 1)
|
||||
break;
|
||||
else
|
||||
if (I < LoadCommandCount - 1)
|
||||
Load = getNextLoadCommandInfo(Load);
|
||||
}
|
||||
assert(LoadCommands.size() == LoadCommandCount);
|
||||
}
|
||||
|
||||
void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
|
||||
@ -1863,6 +1862,22 @@ iterator_range<bind_iterator> MachOObjectFile::weakBindTable() const {
|
||||
MachOBindEntry::Kind::Weak);
|
||||
}
|
||||
|
||||
MachOObjectFile::load_command_iterator
|
||||
MachOObjectFile::begin_load_commands() const {
|
||||
return LoadCommands.begin();
|
||||
}
|
||||
|
||||
MachOObjectFile::load_command_iterator
|
||||
MachOObjectFile::end_load_commands() const {
|
||||
return LoadCommands.end();
|
||||
}
|
||||
|
||||
iterator_range<MachOObjectFile::load_command_iterator>
|
||||
MachOObjectFile::load_commands() const {
|
||||
return iterator_range<load_command_iterator>(begin_load_commands(),
|
||||
end_load_commands());
|
||||
}
|
||||
|
||||
StringRef
|
||||
MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
|
||||
ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
|
||||
|
@ -281,8 +281,7 @@ static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
|
||||
return Size;
|
||||
}
|
||||
|
||||
static void getSectionsAndSymbols(const MachO::mach_header Header,
|
||||
MachOObjectFile *MachOObj,
|
||||
static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
|
||||
std::vector<SectionRef> &Sections,
|
||||
std::vector<SymbolRef> &Symbols,
|
||||
SmallVectorImpl<uint64_t> &FoundFns,
|
||||
@ -300,10 +299,8 @@ static void getSectionsAndSymbols(const MachO::mach_header Header,
|
||||
Sections.push_back(Section);
|
||||
}
|
||||
|
||||
MachOObjectFile::LoadCommandInfo Command =
|
||||
MachOObj->getFirstLoadCommandInfo();
|
||||
bool BaseSegmentAddressSet = false;
|
||||
for (unsigned i = 0;; ++i) {
|
||||
for (const auto &Command : MachOObj->load_commands()) {
|
||||
if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
|
||||
// We found a function starts segment, parse the addresses for later
|
||||
// consumption.
|
||||
@ -319,11 +316,6 @@ static void getSectionsAndSymbols(const MachO::mach_header Header,
|
||||
BaseSegmentAddress = SLC.vmaddr;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == Header.ncmds - 1)
|
||||
break;
|
||||
else
|
||||
Command = MachOObj->getNextLoadCommandInfo(Command);
|
||||
}
|
||||
}
|
||||
|
||||
@ -386,9 +378,7 @@ static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
|
||||
}
|
||||
|
||||
static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
|
||||
uint32_t LoadCommandCount = O->getHeader().ncmds;
|
||||
MachOObjectFile::LoadCommandInfo Load = O->getFirstLoadCommandInfo();
|
||||
for (unsigned I = 0;; ++I) {
|
||||
for (const auto &Load : O->load_commands()) {
|
||||
if (Load.C.cmd == MachO::LC_SEGMENT_64) {
|
||||
MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
|
||||
for (unsigned J = 0; J < Seg.nsects; ++J) {
|
||||
@ -446,10 +436,6 @@ static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (I == LoadCommandCount - 1)
|
||||
break;
|
||||
else
|
||||
Load = O->getNextLoadCommandInfo(Load);
|
||||
}
|
||||
}
|
||||
|
||||
@ -553,9 +539,8 @@ static void PrintLinkOptHints(MachOObjectFile *O) {
|
||||
}
|
||||
|
||||
static void PrintDylibs(MachOObjectFile *O, bool JustId) {
|
||||
uint32_t LoadCommandCount = O->getHeader().ncmds;
|
||||
MachOObjectFile::LoadCommandInfo Load = O->getFirstLoadCommandInfo();
|
||||
for (unsigned I = 0;; ++I) {
|
||||
unsigned Index = 0;
|
||||
for (const auto &Load : O->load_commands()) {
|
||||
if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
|
||||
(!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
|
||||
Load.C.cmd == MachO::LC_LOAD_DYLIB ||
|
||||
@ -595,13 +580,9 @@ static void PrintDylibs(MachOObjectFile *O, bool JustId) {
|
||||
outs() << "LC_LOAD_UPWARD_DYLIB ";
|
||||
else
|
||||
outs() << "LC_??? ";
|
||||
outs() << "command " << I << "\n";
|
||||
outs() << "command " << Index++ << "\n";
|
||||
}
|
||||
}
|
||||
if (I == LoadCommandCount - 1)
|
||||
break;
|
||||
else
|
||||
Load = O->getNextLoadCommandInfo(Load);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2132,9 +2113,7 @@ static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
|
||||
// it returns a pointer to that string. Else it returns nullptr.
|
||||
static const char *GuessCstringPointer(uint64_t ReferenceValue,
|
||||
struct DisassembleInfo *info) {
|
||||
uint32_t LoadCommandCount = info->O->getHeader().ncmds;
|
||||
MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
|
||||
for (unsigned I = 0;; ++I) {
|
||||
for (const auto &Load : info->O->load_commands()) {
|
||||
if (Load.C.cmd == MachO::LC_SEGMENT_64) {
|
||||
MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
|
||||
for (unsigned J = 0; J < Seg.nsects; ++J) {
|
||||
@ -2178,10 +2157,6 @@ static const char *GuessCstringPointer(uint64_t ReferenceValue,
|
||||
}
|
||||
}
|
||||
}
|
||||
if (I == LoadCommandCount - 1)
|
||||
break;
|
||||
else
|
||||
Load = info->O->getNextLoadCommandInfo(Load);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
@ -2192,11 +2167,9 @@ static const char *GuessCstringPointer(uint64_t ReferenceValue,
|
||||
// symbol name being referenced by the stub or pointer.
|
||||
static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
|
||||
struct DisassembleInfo *info) {
|
||||
uint32_t LoadCommandCount = info->O->getHeader().ncmds;
|
||||
MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
|
||||
MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
|
||||
MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
|
||||
for (unsigned I = 0;; ++I) {
|
||||
for (const auto &Load : info->O->load_commands()) {
|
||||
if (Load.C.cmd == MachO::LC_SEGMENT_64) {
|
||||
MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
|
||||
for (unsigned J = 0; J < Seg.nsects; ++J) {
|
||||
@ -2266,10 +2239,6 @@ static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
|
||||
}
|
||||
}
|
||||
}
|
||||
if (I == LoadCommandCount - 1)
|
||||
break;
|
||||
else
|
||||
Load = info->O->getNextLoadCommandInfo(Load);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
@ -2356,9 +2325,7 @@ static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
|
||||
selref = false;
|
||||
msgref = false;
|
||||
cfstring = false;
|
||||
uint32_t LoadCommandCount = info->O->getHeader().ncmds;
|
||||
MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
|
||||
for (unsigned I = 0;; ++I) {
|
||||
for (const auto &Load : info->O->load_commands()) {
|
||||
if (Load.C.cmd == MachO::LC_SEGMENT_64) {
|
||||
MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
|
||||
for (unsigned J = 0; J < Seg.nsects; ++J) {
|
||||
@ -2403,10 +2370,6 @@ static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
|
||||
}
|
||||
}
|
||||
// TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
|
||||
if (I == LoadCommandCount - 1)
|
||||
break;
|
||||
else
|
||||
Load = info->O->getNextLoadCommandInfo(Load);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -6075,7 +6038,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
|
||||
SmallVector<uint64_t, 8> FoundFns;
|
||||
uint64_t BaseSegmentAddress;
|
||||
|
||||
getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
|
||||
getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
|
||||
BaseSegmentAddress);
|
||||
|
||||
// Sort the symbols by address, just in case they didn't come in that way.
|
||||
@ -8367,15 +8330,12 @@ static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
|
||||
outs() << "\n";
|
||||
}
|
||||
|
||||
static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds,
|
||||
uint32_t filetype, uint32_t cputype,
|
||||
bool verbose) {
|
||||
if (ncmds == 0)
|
||||
return;
|
||||
static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
|
||||
uint32_t cputype, bool verbose) {
|
||||
StringRef Buf = Obj->getData();
|
||||
MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo();
|
||||
for (unsigned i = 0;; ++i) {
|
||||
outs() << "Load command " << i << "\n";
|
||||
unsigned Index = 0;
|
||||
for (const auto &Command : Obj->load_commands()) {
|
||||
outs() << "Load command " << Index++ << "\n";
|
||||
if (Command.C.cmd == MachO::LC_SEGMENT) {
|
||||
MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
|
||||
const char *sg_segname = SLC.segname;
|
||||
@ -8494,14 +8454,10 @@ static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds,
|
||||
// TODO: get and print the raw bytes of the load command.
|
||||
}
|
||||
// TODO: print all the other kinds of load commands.
|
||||
if (i == ncmds - 1)
|
||||
break;
|
||||
else
|
||||
Command = Obj->getNextLoadCommandInfo(Command);
|
||||
}
|
||||
}
|
||||
|
||||
static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds,
|
||||
static void getAndPrintMachHeader(const MachOObjectFile *Obj,
|
||||
uint32_t &filetype, uint32_t &cputype,
|
||||
bool verbose) {
|
||||
if (Obj->is64Bit()) {
|
||||
@ -8509,7 +8465,6 @@ static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds,
|
||||
H_64 = Obj->getHeader64();
|
||||
PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
|
||||
H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
|
||||
ncmds = H_64.ncmds;
|
||||
filetype = H_64.filetype;
|
||||
cputype = H_64.cputype;
|
||||
} else {
|
||||
@ -8517,7 +8472,6 @@ static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds,
|
||||
H = Obj->getHeader();
|
||||
PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
|
||||
H.sizeofcmds, H.flags, verbose);
|
||||
ncmds = H.ncmds;
|
||||
filetype = H.filetype;
|
||||
cputype = H.cputype;
|
||||
}
|
||||
@ -8525,11 +8479,10 @@ static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds,
|
||||
|
||||
void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
|
||||
const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
|
||||
uint32_t ncmds = 0;
|
||||
uint32_t filetype = 0;
|
||||
uint32_t cputype = 0;
|
||||
getAndPrintMachHeader(file, ncmds, filetype, cputype, !NonVerbose);
|
||||
PrintLoadCommands(file, ncmds, filetype, cputype, !NonVerbose);
|
||||
getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
|
||||
PrintLoadCommands(file, filetype, cputype, !NonVerbose);
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
@ -122,12 +122,10 @@ static void PrintDarwinSectionSizes(MachOObjectFile *MachO) {
|
||||
fmt << "0x";
|
||||
fmt << "%" << radix_fmt;
|
||||
|
||||
uint32_t LoadCommandCount = MachO->getHeader().ncmds;
|
||||
uint32_t Filetype = MachO->getHeader().filetype;
|
||||
MachOObjectFile::LoadCommandInfo Load = MachO->getFirstLoadCommandInfo();
|
||||
|
||||
uint64_t total = 0;
|
||||
for (unsigned I = 0;; ++I) {
|
||||
for (const auto &Load : MachO->load_commands()) {
|
||||
if (Load.C.cmd == MachO::LC_SEGMENT_64) {
|
||||
MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
|
||||
outs() << "Segment " << Seg.segname << ": "
|
||||
@ -181,10 +179,6 @@ static void PrintDarwinSectionSizes(MachOObjectFile *MachO) {
|
||||
if (Seg.nsects != 0)
|
||||
outs() << "\ttotal " << format(fmt.str().c_str(), sec_total) << "\n";
|
||||
}
|
||||
if (I == LoadCommandCount - 1)
|
||||
break;
|
||||
else
|
||||
Load = MachO->getNextLoadCommandInfo(Load);
|
||||
}
|
||||
outs() << "total " << format(fmt.str().c_str(), total) << "\n";
|
||||
}
|
||||
@ -194,14 +188,11 @@ static void PrintDarwinSectionSizes(MachOObjectFile *MachO) {
|
||||
/// This is when used when @c OutputFormat is berkeley with a Mach-O file and
|
||||
/// produces the same output as darwin's size(1) default output.
|
||||
static void PrintDarwinSegmentSizes(MachOObjectFile *MachO) {
|
||||
uint32_t LoadCommandCount = MachO->getHeader().ncmds;
|
||||
MachOObjectFile::LoadCommandInfo Load = MachO->getFirstLoadCommandInfo();
|
||||
|
||||
uint64_t total_text = 0;
|
||||
uint64_t total_data = 0;
|
||||
uint64_t total_objc = 0;
|
||||
uint64_t total_others = 0;
|
||||
for (unsigned I = 0;; ++I) {
|
||||
for (const auto &Load : MachO->load_commands()) {
|
||||
if (Load.C.cmd == MachO::LC_SEGMENT_64) {
|
||||
MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Load);
|
||||
if (MachO->getHeader().filetype == MachO::MH_OBJECT) {
|
||||
@ -255,10 +246,6 @@ static void PrintDarwinSegmentSizes(MachOObjectFile *MachO) {
|
||||
total_others += Seg.vmsize;
|
||||
}
|
||||
}
|
||||
if (I == LoadCommandCount - 1)
|
||||
break;
|
||||
else
|
||||
Load = MachO->getNextLoadCommandInfo(Load);
|
||||
}
|
||||
uint64_t total = total_text + total_data + total_objc + total_others;
|
||||
|
||||
|
@ -340,7 +340,7 @@ DumpDylibID(const MachOObjectFile &Obj,
|
||||
}
|
||||
|
||||
static int DumpLoadCommand(const MachOObjectFile &Obj,
|
||||
MachOObjectFile::LoadCommandInfo &LCI) {
|
||||
const MachOObjectFile::LoadCommandInfo &LCI) {
|
||||
switch (LCI.C.cmd) {
|
||||
case MachO::LC_SEGMENT:
|
||||
return DumpSegmentCommand(Obj, LCI);
|
||||
@ -369,9 +369,8 @@ static int DumpLoadCommand(const MachOObjectFile &Obj,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int DumpLoadCommand(const MachOObjectFile &Obj, unsigned Index,
|
||||
MachOObjectFile::LoadCommandInfo &LCI) {
|
||||
const MachOObjectFile::LoadCommandInfo &LCI) {
|
||||
outs() << " # Load Command " << Index << "\n"
|
||||
<< " (('command', " << LCI.C.cmd << ")\n"
|
||||
<< " ('size', " << LCI.C.cmdsize << ")\n";
|
||||
@ -423,16 +422,11 @@ int main(int argc, char **argv) {
|
||||
|
||||
// Print the load commands.
|
||||
int Res = 0;
|
||||
MachOObjectFile::LoadCommandInfo Command =
|
||||
InputObject->getFirstLoadCommandInfo();
|
||||
unsigned Index = 0;
|
||||
outs() << "('load_commands', [\n";
|
||||
for (unsigned i = 0; ; ++i) {
|
||||
if (DumpLoadCommand(*InputObject, i, Command))
|
||||
for (const auto &Load : InputObject->load_commands()) {
|
||||
if (DumpLoadCommand(*InputObject, Index++, Load))
|
||||
break;
|
||||
|
||||
if (i == Header->ncmds - 1)
|
||||
break;
|
||||
Command = InputObject->getNextLoadCommandInfo(Command);
|
||||
}
|
||||
outs() << "])\n";
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user