diff options
author | Daniel Dunbar <daniel@zuster.org> | 2010-11-27 07:19:41 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2010-11-27 07:19:41 +0000 |
commit | a956d8b71755b908d8a150736e8857d2214060c6 (patch) | |
tree | 6ac44d294bb7e144b41f7316b518250c8b07a61d /tools/macho-dump | |
parent | bce55776af16b70408e2ae2e6d91f8ac1e43f6a7 (diff) | |
download | external_llvm-a956d8b71755b908d8a150736e8857d2214060c6.zip external_llvm-a956d8b71755b908d8a150736e8857d2214060c6.tar.gz external_llvm-a956d8b71755b908d8a150736e8857d2214060c6.tar.bz2 |
Object/Mach-O: Add header and load command information.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120198 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/macho-dump')
-rw-r--r-- | tools/macho-dump/macho-dump.cpp | 81 |
1 files changed, 69 insertions, 12 deletions
diff --git a/tools/macho-dump/macho-dump.cpp b/tools/macho-dump/macho-dump.cpp index 945406a..8bfd189 100644 --- a/tools/macho-dump/macho-dump.cpp +++ b/tools/macho-dump/macho-dump.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Object/MachOObject.h" +#include "llvm/ADT/Twine.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MemoryBuffer.h" @@ -26,8 +27,62 @@ static cl::opt<bool> DumpSectionData("dump-section-data", cl::desc("Dump the contents of sections"), cl::init(false)); +/// + +static const char *ProgramName; + +static void Message(const char *Type, const Twine &Msg) { + errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; +} + +static int Error(const Twine &Msg) { + Message("error", Msg); + return 1; +} + +static void Warning(const Twine &Msg) { + Message("warning", Msg); +} + +/// + +static int DumpHeader(MachOObject &Obj) { + // Read the header. + const macho::Header &Hdr = Obj.getHeader(); + outs() << "('cputype', " << Hdr.CPUType << ")\n"; + outs() << "('cpusubtype', " << Hdr.CPUSubtype << ")\n"; + outs() << "('filetype', " << Hdr.FileType << ")\n"; + outs() << "('num_load_commands', " << Hdr.NumLoadCommands << ")\n"; + outs() << "('load_commands_size', " << Hdr.SizeOfLoadCommands << ")\n"; + outs() << "('flag', " << Hdr.Flags << ")\n"; + + // Print extended header if 64-bit. + if (Obj.is64Bit()) { + const macho::Header64Ext &Hdr64 = Obj.getHeader64Ext(); + outs() << "('reserved', " << Hdr64.Reserved << ")\n"; + } + + return 0; +} + +static int DumpLoadCommand(MachOObject &Obj, unsigned Index) { + const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index); + + outs() << " # Load Command " << Index << "\n" + << " (('command', " << LCI.Command.Type << ")\n" + << " ('size', " << LCI.Command.Size << ")\n"; + switch (LCI.Command.Type) { + default: + Warning("unknown load command: " + Twine(LCI.Command.Type)); + break; + } + outs() << " ),\n"; + + return 0; +} + int main(int argc, char **argv) { - const char *ProgramName = argv[0]; + ProgramName = argv[0]; llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n"); @@ -36,21 +91,23 @@ int main(int argc, char **argv) { std::string ErrorStr; OwningPtr<MemoryBuffer> InputBuffer( MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr)); - if (!InputBuffer) { - errs() << ProgramName << ": " << "unable to read input: '" - << ErrorStr << "'\n"; - return 1; - } + if (!InputBuffer) + return Error("unable to read input: '" + ErrorStr + "'"); // Construct the Mach-O wrapper object. OwningPtr<MachOObject> InputObject( MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr)); - if (!InputObject) { - errs() << ProgramName << ": " << "unable to load object: '" - << ErrorStr << "'\n"; - return 1; - } + if (!InputObject) + return Error("unable to load object: '" + ErrorStr + "'"); + + if (int Res = DumpHeader(*InputObject)) + return Res; + + // Print the load commands. + outs() << "('load_commands', [\n"; + for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i) + DumpLoadCommand(*InputObject, i); + outs() << "])\n"; - errs() << "ok\n"; return 0; } |