Daniel Dunbar | 75373ac | 2010-11-27 05:58:44 +0000 | [diff] [blame] | 1 | //===-- macho-dump.cpp - Mach Object Dumping Tool -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is a testing tool for use with the MC/Mach-O LLVM components. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | ad12524 | 2010-11-27 06:19:17 +0000 | [diff] [blame] | 14 | #include "llvm/Object/MachOObject.h" |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | 75373ac | 2010-11-27 05:58:44 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CommandLine.h" |
| 17 | #include "llvm/Support/ManagedStatic.h" |
Daniel Dunbar | ad12524 | 2010-11-27 06:19:17 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 75373ac | 2010-11-27 05:58:44 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | using namespace llvm; |
Daniel Dunbar | ad12524 | 2010-11-27 06:19:17 +0000 | [diff] [blame] | 21 | using namespace llvm::object; |
Daniel Dunbar | 75373ac | 2010-11-27 05:58:44 +0000 | [diff] [blame] | 22 | |
| 23 | static cl::opt<std::string> |
| 24 | InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 25 | |
Daniel Dunbar | ad12524 | 2010-11-27 06:19:17 +0000 | [diff] [blame] | 26 | static cl::opt<bool> |
| 27 | DumpSectionData("dump-section-data", cl::desc("Dump the contents of sections"), |
| 28 | cl::init(false)); |
| 29 | |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 30 | /// |
| 31 | |
| 32 | static const char *ProgramName; |
| 33 | |
| 34 | static void Message(const char *Type, const Twine &Msg) { |
| 35 | errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; |
| 36 | } |
| 37 | |
| 38 | static int Error(const Twine &Msg) { |
| 39 | Message("error", Msg); |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | static void Warning(const Twine &Msg) { |
| 44 | Message("warning", Msg); |
| 45 | } |
| 46 | |
| 47 | /// |
| 48 | |
| 49 | static int DumpHeader(MachOObject &Obj) { |
| 50 | // Read the header. |
| 51 | const macho::Header &Hdr = Obj.getHeader(); |
| 52 | outs() << "('cputype', " << Hdr.CPUType << ")\n"; |
| 53 | outs() << "('cpusubtype', " << Hdr.CPUSubtype << ")\n"; |
| 54 | outs() << "('filetype', " << Hdr.FileType << ")\n"; |
| 55 | outs() << "('num_load_commands', " << Hdr.NumLoadCommands << ")\n"; |
| 56 | outs() << "('load_commands_size', " << Hdr.SizeOfLoadCommands << ")\n"; |
| 57 | outs() << "('flag', " << Hdr.Flags << ")\n"; |
| 58 | |
| 59 | // Print extended header if 64-bit. |
| 60 | if (Obj.is64Bit()) { |
| 61 | const macho::Header64Ext &Hdr64 = Obj.getHeader64Ext(); |
| 62 | outs() << "('reserved', " << Hdr64.Reserved << ")\n"; |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int DumpLoadCommand(MachOObject &Obj, unsigned Index) { |
| 69 | const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index); |
| 70 | |
| 71 | outs() << " # Load Command " << Index << "\n" |
| 72 | << " (('command', " << LCI.Command.Type << ")\n" |
| 73 | << " ('size', " << LCI.Command.Size << ")\n"; |
| 74 | switch (LCI.Command.Type) { |
| 75 | default: |
| 76 | Warning("unknown load command: " + Twine(LCI.Command.Type)); |
| 77 | break; |
| 78 | } |
| 79 | outs() << " ),\n"; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Daniel Dunbar | 75373ac | 2010-11-27 05:58:44 +0000 | [diff] [blame] | 84 | int main(int argc, char **argv) { |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 85 | ProgramName = argv[0]; |
Daniel Dunbar | 75373ac | 2010-11-27 05:58:44 +0000 | [diff] [blame] | 86 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 87 | |
| 88 | cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n"); |
| 89 | |
Daniel Dunbar | ad12524 | 2010-11-27 06:19:17 +0000 | [diff] [blame] | 90 | // Load the input file. |
| 91 | std::string ErrorStr; |
| 92 | OwningPtr<MemoryBuffer> InputBuffer( |
| 93 | MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr)); |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 94 | if (!InputBuffer) |
| 95 | return Error("unable to read input: '" + ErrorStr + "'"); |
Daniel Dunbar | ad12524 | 2010-11-27 06:19:17 +0000 | [diff] [blame] | 96 | |
| 97 | // Construct the Mach-O wrapper object. |
| 98 | OwningPtr<MachOObject> InputObject( |
| 99 | MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr)); |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 100 | if (!InputObject) |
| 101 | return Error("unable to load object: '" + ErrorStr + "'"); |
Daniel Dunbar | ad12524 | 2010-11-27 06:19:17 +0000 | [diff] [blame] | 102 | |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 103 | if (int Res = DumpHeader(*InputObject)) |
| 104 | return Res; |
| 105 | |
| 106 | // Print the load commands. |
| 107 | outs() << "('load_commands', [\n"; |
| 108 | for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i) |
| 109 | DumpLoadCommand(*InputObject, i); |
| 110 | outs() << "])\n"; |
| 111 | |
Daniel Dunbar | ad12524 | 2010-11-27 06:19:17 +0000 | [diff] [blame] | 112 | return 0; |
Daniel Dunbar | 75373ac | 2010-11-27 05:58:44 +0000 | [diff] [blame] | 113 | } |