1 //===-- macho-dump.cpp - Mach Object Dumping Tool -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is a testing tool for use with the MC/Mach-O LLVM components.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/MachOObject.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm::object;
24 static cl::opt<std::string>
25 InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
28 DumpSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
33 static const char *ProgramName;
35 static void Message(const char *Type, const Twine &Msg) {
36 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
39 static int Error(const Twine &Msg) {
40 Message("error", Msg);
44 static void Warning(const Twine &Msg) {
45 Message("warning", Msg);
50 static int DumpHeader(MachOObject &Obj) {
52 const macho::Header &Hdr = Obj.getHeader();
53 outs() << "('cputype', " << Hdr.CPUType << ")\n";
54 outs() << "('cpusubtype', " << Hdr.CPUSubtype << ")\n";
55 outs() << "('filetype', " << Hdr.FileType << ")\n";
56 outs() << "('num_load_commands', " << Hdr.NumLoadCommands << ")\n";
57 outs() << "('load_commands_size', " << Hdr.SizeOfLoadCommands << ")\n";
58 outs() << "('flag', " << Hdr.Flags << ")\n";
60 // Print extended header if 64-bit.
62 const macho::Header64Ext &Hdr64 = Obj.getHeader64Ext();
63 outs() << "('reserved', " << Hdr64.Reserved << ")\n";
69 static void DumpSegmentCommandData(StringRef Name,
70 uint64_t VMAddr, uint64_t VMSize,
71 uint64_t FileOffset, uint64_t FileSize,
72 uint32_t MaxProt, uint32_t InitProt,
73 uint32_t NumSections, uint32_t Flags) {
74 outs() << " ('segment_name', '";
75 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
76 outs() << " ('vm_addr', " << VMAddr << ")\n";
77 outs() << " ('vm_size', " << VMSize << ")\n";
78 outs() << " ('file_offset', " << FileOffset << ")\n";
79 outs() << " ('file_size', " << FileSize << ")\n";
80 outs() << " ('maxprot', " << MaxProt << ")\n";
81 outs() << " ('initprot', " << InitProt << ")\n";
82 outs() << " ('num_sections', " << NumSections << ")\n";
83 outs() << " ('flags', " << Flags << ")\n";
86 static int DumpSegmentCommand(MachOObject &Obj,
87 const MachOObject::LoadCommandInfo &LCI) {
88 InMemoryStruct<macho::SegmentLoadCommand> SLC;
89 Obj.ReadSegmentLoadCommand(LCI, SLC);
91 return Error("unable to read segment load command");
93 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, SLC->VMSize,
94 SLC->FileOffset, SLC->FileSize,
95 SLC->MaxVMProtection, SLC->InitialVMProtection,
96 SLC->NumSections, SLC->Flags);
100 static int DumpSegment64Command(MachOObject &Obj,
101 const MachOObject::LoadCommandInfo &LCI) {
102 InMemoryStruct<macho::Segment64LoadCommand> SLC;
103 Obj.ReadSegment64LoadCommand(LCI, SLC);
105 return Error("unable to read segment load command");
107 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, SLC->VMSize,
108 SLC->FileOffset, SLC->FileSize,
109 SLC->MaxVMProtection, SLC->InitialVMProtection,
110 SLC->NumSections, SLC->Flags);
115 static int DumpSymtabCommand(MachOObject &Obj,
116 const MachOObject::LoadCommandInfo &LCI) {
117 InMemoryStruct<macho::SymtabLoadCommand> SLC;
118 Obj.ReadSymtabLoadCommand(LCI, SLC);
120 return Error("unable to read segment load command");
122 outs() << " ('symoff', " << SLC->SymbolTableOffset << ")\n";
123 outs() << " ('nsyms', " << SLC->NumSymbolTableEntries << ")\n";
124 outs() << " ('stroff', " << SLC->StringTableOffset << ")\n";
125 outs() << " ('strsize', " << SLC->StringTableSize << ")\n";
130 static int DumpDysymtabCommand(MachOObject &Obj,
131 const MachOObject::LoadCommandInfo &LCI) {
132 InMemoryStruct<macho::DysymtabLoadCommand> DLC;
133 Obj.ReadDysymtabLoadCommand(LCI, DLC);
135 return Error("unable to read segment load command");
137 outs() << " ('ilocalsym', " << DLC->LocalSymbolIndex << ")\n";
138 outs() << " ('nlocalsym', " << DLC->NumLocalSymbols << ")\n";
139 outs() << " ('iextdefsym', " << DLC->ExternalSymbolsIndex << ")\n";
140 outs() << " ('nextdefsym', " << DLC->NumExternalSymbols << ")\n";
141 outs() << " ('iundefsym', " << DLC->UndefinedSymbolsIndex << ")\n";
142 outs() << " ('nundefsym', " << DLC->NumUndefinedSymbols << ")\n";
143 outs() << " ('tocoff', " << DLC->TOCOffset << ")\n";
144 outs() << " ('ntoc', " << DLC->NumTOCEntries << ")\n";
145 outs() << " ('modtaboff', " << DLC->ModuleTableOffset << ")\n";
146 outs() << " ('nmodtab', " << DLC->NumModuleTableEntries << ")\n";
147 outs() << " ('extrefsymoff', " << DLC->ReferenceSymbolTableOffset << ")\n";
148 outs() << " ('nextrefsyms', "
149 << DLC->NumReferencedSymbolTableEntries << ")\n";
150 outs() << " ('indirectsymoff', " << DLC->IndirectSymbolTableOffset << ")\n";
151 outs() << " ('nindirectsyms', "
152 << DLC->NumIndirectSymbolTableEntries << ")\n";
153 outs() << " ('extreloff', " << DLC->ExternalRelocationTableOffset << ")\n";
154 outs() << " ('nextrel', " << DLC->NumExternalRelocationTableEntries << ")\n";
155 outs() << " ('locreloff', " << DLC->LocalRelocationTableOffset << ")\n";
156 outs() << " ('nlocrel', " << DLC->NumLocalRelocationTableEntries << ")\n";
158 // Dump the indirect symbol table.
160 outs() << " ('_indirect_symbols', [\n";
161 for (unsigned i = 0; i != DLC->NumIndirectSymbolTableEntries; ++i) {
162 InMemoryStruct<macho::IndirectSymbolTableEntry> ISTE;
163 Obj.ReadIndirectSymbolTableEntry(*DLC, i, ISTE);
165 Res = Error("unable to read segment load command");
169 outs() << " # Indirect Symbol " << i << "\n";
170 outs() << " (('symbol_index', "
171 << format("%#x", ISTE->Index) << "),),\n";
178 static int DumpLoadCommand(MachOObject &Obj, unsigned Index) {
179 const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index);
182 outs() << " # Load Command " << Index << "\n"
183 << " (('command', " << LCI.Command.Type << ")\n"
184 << " ('size', " << LCI.Command.Size << ")\n";
185 switch (LCI.Command.Type) {
186 case macho::LCT_Segment:
187 Res = DumpSegmentCommand(Obj, LCI);
189 case macho::LCT_Segment64:
190 Res = DumpSegment64Command(Obj, LCI);
192 case macho::LCT_Symtab:
193 Res = DumpSymtabCommand(Obj, LCI);
195 case macho::LCT_Dysymtab:
196 Res = DumpDysymtabCommand(Obj, LCI);
199 Warning("unknown load command: " + Twine(LCI.Command.Type));
207 int main(int argc, char **argv) {
208 ProgramName = argv[0];
209 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
211 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
213 // Load the input file.
214 std::string ErrorStr;
215 OwningPtr<MemoryBuffer> InputBuffer(
216 MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr));
218 return Error("unable to read input: '" + ErrorStr + "'");
220 // Construct the Mach-O wrapper object.
221 OwningPtr<MachOObject> InputObject(
222 MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr));
224 return Error("unable to load object: '" + ErrorStr + "'");
226 if (int Res = DumpHeader(*InputObject))
229 // Print the load commands.
231 outs() << "('load_commands', [\n";
232 for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i)
233 if ((Res = DumpLoadCommand(*InputObject, i)))