blob: 68e61ba8d669b16fafcb257c2b6e1fe232f1decb [file] [log] [blame]
Daniel Dunbar75373ac2010-11-27 05:58:44 +00001//===-- 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 Dunbarad125242010-11-27 06:19:17 +000014#include "llvm/Object/MachOObject.h"
Daniel Dunbara956d8b2010-11-27 07:19:41 +000015#include "llvm/ADT/Twine.h"
Daniel Dunbar75373ac2010-11-27 05:58:44 +000016#include "llvm/Support/CommandLine.h"
Daniel Dunbar4c55e0d2010-11-27 13:26:12 +000017#include "llvm/Support/Format.h"
Daniel Dunbar75373ac2010-11-27 05:58:44 +000018#include "llvm/Support/ManagedStatic.h"
Daniel Dunbarad125242010-11-27 06:19:17 +000019#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar75373ac2010-11-27 05:58:44 +000020#include "llvm/Support/raw_ostream.h"
21using namespace llvm;
Daniel Dunbarad125242010-11-27 06:19:17 +000022using namespace llvm::object;
Daniel Dunbar75373ac2010-11-27 05:58:44 +000023
24static cl::opt<std::string>
25InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
26
Daniel Dunbarad125242010-11-27 06:19:17 +000027static cl::opt<bool>
Daniel Dunbar2acadbd2010-11-27 13:33:15 +000028ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
Daniel Dunbarad125242010-11-27 06:19:17 +000029 cl::init(false));
30
Daniel Dunbara956d8b2010-11-27 07:19:41 +000031///
32
33static const char *ProgramName;
34
35static void Message(const char *Type, const Twine &Msg) {
36 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
37}
38
39static int Error(const Twine &Msg) {
40 Message("error", Msg);
41 return 1;
42}
43
44static void Warning(const Twine &Msg) {
45 Message("warning", Msg);
46}
47
48///
49
50static int DumpHeader(MachOObject &Obj) {
51 // Read the header.
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";
59
60 // Print extended header if 64-bit.
61 if (Obj.is64Bit()) {
62 const macho::Header64Ext &Hdr64 = Obj.getHeader64Ext();
63 outs() << "('reserved', " << Hdr64.Reserved << ")\n";
64 }
65
66 return 0;
67}
68
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +000069static 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";
84}
85
Daniel Dunbar2acadbd2010-11-27 13:33:15 +000086static void DumpSectionData(unsigned Index, StringRef Name,
87 StringRef SegmentName, uint64_t Address,
88 uint64_t Size, uint32_t Offset,
89 uint32_t Align, uint32_t RelocationTableOffset,
90 uint32_t NumRelocationTableEntries,
91 uint32_t Flags, uint32_t Reserved1,
92 uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) {
93 outs() << " # Section " << Index << "\n";
94 outs() << " (('section_name', '";
95 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
96 outs() << " ('segment_name', '";
97 outs().write_escaped(SegmentName, /*UseHexEscapes=*/true) << "')\n";
98 outs() << " ('address', " << Address << ")\n";
99 outs() << " ('size', " << Size << ")\n";
100 outs() << " ('offset', " << Offset << ")\n";
101 outs() << " ('alignment', " << Align << ")\n";
102 outs() << " ('reloc_offset', " << RelocationTableOffset << ")\n";
103 outs() << " ('num_reloc', " << NumRelocationTableEntries << ")\n";
104 outs() << " ('flags', " << format("%#x", Flags) << ")\n";
105 outs() << " ('reserved1', " << Reserved1 << ")\n";
106 outs() << " ('reserved2', " << Reserved2 << ")\n";
107 if (Reserved3 != ~0ULL)
108 outs() << " ('reserved3', " << Reserved3 << ")\n";
109}
110
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000111static int DumpSegmentCommand(MachOObject &Obj,
112 const MachOObject::LoadCommandInfo &LCI) {
113 InMemoryStruct<macho::SegmentLoadCommand> SLC;
114 Obj.ReadSegmentLoadCommand(LCI, SLC);
115 if (!SLC)
116 return Error("unable to read segment load command");
117
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000118 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress,
119 SLC->VMSize, SLC->FileOffset, SLC->FileSize,
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000120 SLC->MaxVMProtection, SLC->InitialVMProtection,
121 SLC->NumSections, SLC->Flags);
122
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000123 // Dump the sections.
124 int Res = 0;
125 outs() << " ('sections', [\n";
126 for (unsigned i = 0; i != SLC->NumSections; ++i) {
127 InMemoryStruct<macho::Section> Sect;
128 Obj.ReadSection(LCI, i, Sect);
129 if (!SLC) {
130 Res = Error("unable to read section '" + Twine(i) + "'");
131 break;
132 }
133
134 DumpSectionData(i, StringRef(Sect->Name, 16),
135 StringRef(Sect->SegmentName, 16), Sect->Address, Sect->Size,
136 Sect->Offset, Sect->Align, Sect->RelocationTableOffset,
137 Sect->NumRelocationTableEntries, Sect->Flags,
138 Sect->Reserved1, Sect->Reserved2);
139 outs() << " ),\n";
140 }
141 outs() << " ])\n";
142
143 return Res;
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000144}
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000145
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000146static int DumpSegment64Command(MachOObject &Obj,
147 const MachOObject::LoadCommandInfo &LCI) {
148 InMemoryStruct<macho::Segment64LoadCommand> SLC;
149 Obj.ReadSegment64LoadCommand(LCI, SLC);
150 if (!SLC)
151 return Error("unable to read segment load command");
152
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000153 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress,
154 SLC->VMSize, SLC->FileOffset, SLC->FileSize,
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000155 SLC->MaxVMProtection, SLC->InitialVMProtection,
156 SLC->NumSections, SLC->Flags);
157
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000158 // Dump the sections.
159 int Res = 0;
160 outs() << " ('sections', [\n";
161 for (unsigned i = 0; i != SLC->NumSections; ++i) {
162 InMemoryStruct<macho::Section64> Sect;
163 Obj.ReadSection64(LCI, i, Sect);
164 if (!SLC) {
165 Res = Error("unable to read section '" + Twine(i) + "'");
166 break;
167 }
168
169 DumpSectionData(i, StringRef(Sect->Name, 16),
170 StringRef(Sect->SegmentName, 16), Sect->Address, Sect->Size,
171 Sect->Offset, Sect->Align, Sect->RelocationTableOffset,
172 Sect->NumRelocationTableEntries, Sect->Flags,
173 Sect->Reserved1, Sect->Reserved2, Sect->Reserved3);
174 outs() << " ),\n";
175 }
176 outs() << " ])\n";
177
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000178 return 0;
179}
180
Daniel Dunbarf879f142010-11-27 08:33:44 +0000181static int DumpSymtabCommand(MachOObject &Obj,
182 const MachOObject::LoadCommandInfo &LCI) {
183 InMemoryStruct<macho::SymtabLoadCommand> SLC;
184 Obj.ReadSymtabLoadCommand(LCI, SLC);
185 if (!SLC)
186 return Error("unable to read segment load command");
187
188 outs() << " ('symoff', " << SLC->SymbolTableOffset << ")\n";
189 outs() << " ('nsyms', " << SLC->NumSymbolTableEntries << ")\n";
190 outs() << " ('stroff', " << SLC->StringTableOffset << ")\n";
191 outs() << " ('strsize', " << SLC->StringTableSize << ")\n";
192
193 return 0;
194}
195
196static int DumpDysymtabCommand(MachOObject &Obj,
197 const MachOObject::LoadCommandInfo &LCI) {
198 InMemoryStruct<macho::DysymtabLoadCommand> DLC;
199 Obj.ReadDysymtabLoadCommand(LCI, DLC);
200 if (!DLC)
201 return Error("unable to read segment load command");
202
203 outs() << " ('ilocalsym', " << DLC->LocalSymbolIndex << ")\n";
204 outs() << " ('nlocalsym', " << DLC->NumLocalSymbols << ")\n";
205 outs() << " ('iextdefsym', " << DLC->ExternalSymbolsIndex << ")\n";
206 outs() << " ('nextdefsym', " << DLC->NumExternalSymbols << ")\n";
207 outs() << " ('iundefsym', " << DLC->UndefinedSymbolsIndex << ")\n";
208 outs() << " ('nundefsym', " << DLC->NumUndefinedSymbols << ")\n";
209 outs() << " ('tocoff', " << DLC->TOCOffset << ")\n";
210 outs() << " ('ntoc', " << DLC->NumTOCEntries << ")\n";
211 outs() << " ('modtaboff', " << DLC->ModuleTableOffset << ")\n";
212 outs() << " ('nmodtab', " << DLC->NumModuleTableEntries << ")\n";
213 outs() << " ('extrefsymoff', " << DLC->ReferenceSymbolTableOffset << ")\n";
214 outs() << " ('nextrefsyms', "
215 << DLC->NumReferencedSymbolTableEntries << ")\n";
216 outs() << " ('indirectsymoff', " << DLC->IndirectSymbolTableOffset << ")\n";
217 outs() << " ('nindirectsyms', "
218 << DLC->NumIndirectSymbolTableEntries << ")\n";
219 outs() << " ('extreloff', " << DLC->ExternalRelocationTableOffset << ")\n";
220 outs() << " ('nextrel', " << DLC->NumExternalRelocationTableEntries << ")\n";
221 outs() << " ('locreloff', " << DLC->LocalRelocationTableOffset << ")\n";
222 outs() << " ('nlocrel', " << DLC->NumLocalRelocationTableEntries << ")\n";
223
Daniel Dunbar4c55e0d2010-11-27 13:26:12 +0000224 // Dump the indirect symbol table.
225 int Res = 0;
226 outs() << " ('_indirect_symbols', [\n";
227 for (unsigned i = 0; i != DLC->NumIndirectSymbolTableEntries; ++i) {
228 InMemoryStruct<macho::IndirectSymbolTableEntry> ISTE;
229 Obj.ReadIndirectSymbolTableEntry(*DLC, i, ISTE);
230 if (!ISTE) {
231 Res = Error("unable to read segment load command");
232 break;
233 }
234
235 outs() << " # Indirect Symbol " << i << "\n";
236 outs() << " (('symbol_index', "
237 << format("%#x", ISTE->Index) << "),),\n";
238 }
239 outs() << " ])\n";
240
241 return Res;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000242}
243
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000244static int DumpLoadCommand(MachOObject &Obj, unsigned Index) {
245 const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index);
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000246 int Res = 0;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000247
248 outs() << " # Load Command " << Index << "\n"
249 << " (('command', " << LCI.Command.Type << ")\n"
250 << " ('size', " << LCI.Command.Size << ")\n";
251 switch (LCI.Command.Type) {
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000252 case macho::LCT_Segment:
253 Res = DumpSegmentCommand(Obj, LCI);
254 break;
255 case macho::LCT_Segment64:
256 Res = DumpSegment64Command(Obj, LCI);
257 break;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000258 case macho::LCT_Symtab:
259 Res = DumpSymtabCommand(Obj, LCI);
260 break;
261 case macho::LCT_Dysymtab:
262 Res = DumpDysymtabCommand(Obj, LCI);
263 break;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000264 default:
265 Warning("unknown load command: " + Twine(LCI.Command.Type));
266 break;
267 }
268 outs() << " ),\n";
269
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000270 return Res;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000271}
272
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000273int main(int argc, char **argv) {
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000274 ProgramName = argv[0];
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000275 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
276
277 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
278
Daniel Dunbarad125242010-11-27 06:19:17 +0000279 // Load the input file.
280 std::string ErrorStr;
281 OwningPtr<MemoryBuffer> InputBuffer(
282 MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr));
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000283 if (!InputBuffer)
284 return Error("unable to read input: '" + ErrorStr + "'");
Daniel Dunbarad125242010-11-27 06:19:17 +0000285
286 // Construct the Mach-O wrapper object.
287 OwningPtr<MachOObject> InputObject(
288 MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr));
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000289 if (!InputObject)
290 return Error("unable to load object: '" + ErrorStr + "'");
Daniel Dunbarad125242010-11-27 06:19:17 +0000291
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000292 if (int Res = DumpHeader(*InputObject))
293 return Res;
294
295 // Print the load commands.
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000296 int Res = 0;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000297 outs() << "('load_commands', [\n";
298 for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i)
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000299 if ((Res = DumpLoadCommand(*InputObject, i)))
300 break;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000301 outs() << "])\n";
302
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000303 return Res;
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000304}