blob: 2b22c3b0fd91d269724463e0eeba6646d6582583 [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 Dunbar71130f82010-11-27 13:58:16 +000015#include "llvm/ADT/StringExtras.h"
Daniel Dunbara956d8b2010-11-27 07:19:41 +000016#include "llvm/ADT/Twine.h"
Daniel Dunbar75373ac2010-11-27 05:58:44 +000017#include "llvm/Support/CommandLine.h"
Daniel Dunbar4c55e0d2010-11-27 13:26:12 +000018#include "llvm/Support/Format.h"
Daniel Dunbar75373ac2010-11-27 05:58:44 +000019#include "llvm/Support/ManagedStatic.h"
Daniel Dunbarad125242010-11-27 06:19:17 +000020#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar75373ac2010-11-27 05:58:44 +000021#include "llvm/Support/raw_ostream.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000022#include "llvm/Support/system_error.h"
Daniel Dunbar75373ac2010-11-27 05:58:44 +000023using namespace llvm;
Daniel Dunbarad125242010-11-27 06:19:17 +000024using namespace llvm::object;
Daniel Dunbar75373ac2010-11-27 05:58:44 +000025
26static cl::opt<std::string>
27InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
28
Daniel Dunbarad125242010-11-27 06:19:17 +000029static cl::opt<bool>
Daniel Dunbar2acadbd2010-11-27 13:33:15 +000030ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
Daniel Dunbarad125242010-11-27 06:19:17 +000031 cl::init(false));
32
Daniel Dunbara956d8b2010-11-27 07:19:41 +000033///
34
35static const char *ProgramName;
36
37static void Message(const char *Type, const Twine &Msg) {
38 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
39}
40
41static int Error(const Twine &Msg) {
42 Message("error", Msg);
43 return 1;
44}
45
46static void Warning(const Twine &Msg) {
47 Message("warning", Msg);
48}
49
50///
51
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +000052static void DumpSegmentCommandData(StringRef Name,
53 uint64_t VMAddr, uint64_t VMSize,
54 uint64_t FileOffset, uint64_t FileSize,
55 uint32_t MaxProt, uint32_t InitProt,
56 uint32_t NumSections, uint32_t Flags) {
57 outs() << " ('segment_name', '";
58 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
59 outs() << " ('vm_addr', " << VMAddr << ")\n";
60 outs() << " ('vm_size', " << VMSize << ")\n";
61 outs() << " ('file_offset', " << FileOffset << ")\n";
62 outs() << " ('file_size', " << FileSize << ")\n";
63 outs() << " ('maxprot', " << MaxProt << ")\n";
64 outs() << " ('initprot', " << InitProt << ")\n";
65 outs() << " ('num_sections', " << NumSections << ")\n";
66 outs() << " ('flags', " << Flags << ")\n";
67}
68
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +000069static int DumpSectionData(MachOObject &Obj, unsigned Index, StringRef Name,
70 StringRef SegmentName, uint64_t Address,
71 uint64_t Size, uint32_t Offset,
72 uint32_t Align, uint32_t RelocationTableOffset,
73 uint32_t NumRelocationTableEntries,
74 uint32_t Flags, uint32_t Reserved1,
75 uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) {
Daniel Dunbar2acadbd2010-11-27 13:33:15 +000076 outs() << " # Section " << Index << "\n";
77 outs() << " (('section_name', '";
78 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
79 outs() << " ('segment_name', '";
80 outs().write_escaped(SegmentName, /*UseHexEscapes=*/true) << "')\n";
81 outs() << " ('address', " << Address << ")\n";
82 outs() << " ('size', " << Size << ")\n";
83 outs() << " ('offset', " << Offset << ")\n";
84 outs() << " ('alignment', " << Align << ")\n";
85 outs() << " ('reloc_offset', " << RelocationTableOffset << ")\n";
86 outs() << " ('num_reloc', " << NumRelocationTableEntries << ")\n";
Daniel Dunbar71130f82010-11-27 13:58:16 +000087 outs() << " ('flags', " << format("0x%x", Flags) << ")\n";
Daniel Dunbar2acadbd2010-11-27 13:33:15 +000088 outs() << " ('reserved1', " << Reserved1 << ")\n";
89 outs() << " ('reserved2', " << Reserved2 << ")\n";
90 if (Reserved3 != ~0ULL)
91 outs() << " ('reserved3', " << Reserved3 << ")\n";
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +000092 outs() << " ),\n";
93
94 // Dump the relocation entries.
95 int Res = 0;
96 outs() << " ('_relocations', [\n";
97 for (unsigned i = 0; i != NumRelocationTableEntries; ++i) {
98 InMemoryStruct<macho::RelocationEntry> RE;
99 Obj.ReadRelocationEntry(RelocationTableOffset, i, RE);
100 if (!RE) {
101 Res = Error("unable to read relocation table entry '" + Twine(i) + "'");
102 break;
103 }
104
105 outs() << " # Relocation " << i << "\n";
Daniel Dunbar71130f82010-11-27 13:58:16 +0000106 outs() << " (('word-0', " << format("0x%x", RE->Word0) << "),\n";
107 outs() << " ('word-1', " << format("0x%x", RE->Word1) << ")),\n";
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000108 }
109 outs() << " ])\n";
110
Daniel Dunbar71130f82010-11-27 13:58:16 +0000111 // Dump the section data, if requested.
112 if (ShowSectionData) {
113 outs() << " ('_section_data', '";
114 StringRef Data = Obj.getData(Offset, Size);
115 for (unsigned i = 0; i != Data.size(); ++i) {
116 if (i && (i % 4) == 0)
117 outs() << ' ';
118 outs() << hexdigit((Data[i] >> 4) & 0xF, /*LowerCase=*/true);
119 outs() << hexdigit((Data[i] >> 0) & 0xF, /*LowerCase=*/true);
120 }
121 outs() << "')\n";
122 }
123
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000124 return Res;
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000125}
126
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000127static int DumpSegmentCommand(MachOObject &Obj,
128 const MachOObject::LoadCommandInfo &LCI) {
129 InMemoryStruct<macho::SegmentLoadCommand> SLC;
130 Obj.ReadSegmentLoadCommand(LCI, SLC);
131 if (!SLC)
132 return Error("unable to read segment load command");
133
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000134 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress,
135 SLC->VMSize, SLC->FileOffset, SLC->FileSize,
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000136 SLC->MaxVMProtection, SLC->InitialVMProtection,
137 SLC->NumSections, SLC->Flags);
138
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000139 // Dump the sections.
140 int Res = 0;
141 outs() << " ('sections', [\n";
142 for (unsigned i = 0; i != SLC->NumSections; ++i) {
143 InMemoryStruct<macho::Section> Sect;
144 Obj.ReadSection(LCI, i, Sect);
145 if (!SLC) {
146 Res = Error("unable to read section '" + Twine(i) + "'");
147 break;
148 }
149
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000150 if ((Res = DumpSectionData(Obj, i, StringRef(Sect->Name, 16),
151 StringRef(Sect->SegmentName, 16), Sect->Address,
152 Sect->Size, Sect->Offset, Sect->Align,
153 Sect->RelocationTableOffset,
154 Sect->NumRelocationTableEntries, Sect->Flags,
155 Sect->Reserved1, Sect->Reserved2)))
156 break;
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000157 }
158 outs() << " ])\n";
159
160 return Res;
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000161}
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000162
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000163static int DumpSegment64Command(MachOObject &Obj,
164 const MachOObject::LoadCommandInfo &LCI) {
165 InMemoryStruct<macho::Segment64LoadCommand> SLC;
166 Obj.ReadSegment64LoadCommand(LCI, SLC);
167 if (!SLC)
168 return Error("unable to read segment load command");
169
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000170 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress,
171 SLC->VMSize, SLC->FileOffset, SLC->FileSize,
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000172 SLC->MaxVMProtection, SLC->InitialVMProtection,
173 SLC->NumSections, SLC->Flags);
174
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000175 // Dump the sections.
176 int Res = 0;
177 outs() << " ('sections', [\n";
178 for (unsigned i = 0; i != SLC->NumSections; ++i) {
179 InMemoryStruct<macho::Section64> Sect;
180 Obj.ReadSection64(LCI, i, Sect);
181 if (!SLC) {
182 Res = Error("unable to read section '" + Twine(i) + "'");
183 break;
184 }
185
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000186 if ((Res = DumpSectionData(Obj, i, StringRef(Sect->Name, 16),
187 StringRef(Sect->SegmentName, 16), Sect->Address,
188 Sect->Size, Sect->Offset, Sect->Align,
189 Sect->RelocationTableOffset,
190 Sect->NumRelocationTableEntries, Sect->Flags,
191 Sect->Reserved1, Sect->Reserved2,
192 Sect->Reserved3)))
193 break;
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000194 }
195 outs() << " ])\n";
196
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000197 return 0;
198}
199
Daniel Dunbar2208b582010-11-27 13:52:53 +0000200static void DumpSymbolTableEntryData(MachOObject &Obj,
201 unsigned Index, uint32_t StringIndex,
202 uint8_t Type, uint8_t SectionIndex,
203 uint16_t Flags, uint64_t Value) {
204 outs() << " # Symbol " << Index << "\n";
205 outs() << " (('n_strx', " << StringIndex << ")\n";
Daniel Dunbar71130f82010-11-27 13:58:16 +0000206 outs() << " ('n_type', " << format("0x%x", Type) << ")\n";
Daniel Dunbar2208b582010-11-27 13:52:53 +0000207 outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n";
208 outs() << " ('n_desc', " << Flags << ")\n";
209 outs() << " ('n_value', " << Value << ")\n";
210 outs() << " ('_string', '" << Obj.getStringAtIndex(StringIndex) << "')\n";
211 outs() << " ),\n";
212}
213
Daniel Dunbarf879f142010-11-27 08:33:44 +0000214static int DumpSymtabCommand(MachOObject &Obj,
215 const MachOObject::LoadCommandInfo &LCI) {
216 InMemoryStruct<macho::SymtabLoadCommand> SLC;
217 Obj.ReadSymtabLoadCommand(LCI, SLC);
218 if (!SLC)
219 return Error("unable to read segment load command");
220
221 outs() << " ('symoff', " << SLC->SymbolTableOffset << ")\n";
222 outs() << " ('nsyms', " << SLC->NumSymbolTableEntries << ")\n";
223 outs() << " ('stroff', " << SLC->StringTableOffset << ")\n";
224 outs() << " ('strsize', " << SLC->StringTableSize << ")\n";
225
Daniel Dunbarf2e2a5f2010-11-27 13:46:11 +0000226 // Cache the string table data.
227 Obj.RegisterStringTable(*SLC);
228
229 // Dump the string data.
230 outs() << " ('_string_data', '";
231 outs().write_escaped(Obj.getStringTableData(),
232 /*UseHexEscapes=*/true) << "')\n";
233
Daniel Dunbar2208b582010-11-27 13:52:53 +0000234 // Dump the symbol table.
235 int Res = 0;
236 outs() << " ('_symbols', [\n";
237 for (unsigned i = 0; i != SLC->NumSymbolTableEntries; ++i) {
238 if (Obj.is64Bit()) {
239 InMemoryStruct<macho::Symbol64TableEntry> STE;
240 Obj.ReadSymbol64TableEntry(SLC->SymbolTableOffset, i, STE);
241 if (!STE) {
242 Res = Error("unable to read symbol: '" + Twine(i) + "'");
243 break;
244 }
245
246 DumpSymbolTableEntryData(Obj, i, STE->StringIndex, STE->Type,
247 STE->SectionIndex, STE->Flags, STE->Value);
248 } else {
249 InMemoryStruct<macho::SymbolTableEntry> STE;
250 Obj.ReadSymbolTableEntry(SLC->SymbolTableOffset, i, STE);
251 if (!SLC) {
252 Res = Error("unable to read symbol: '" + Twine(i) + "'");
253 break;
254 }
255
256 DumpSymbolTableEntryData(Obj, i, STE->StringIndex, STE->Type,
257 STE->SectionIndex, STE->Flags, STE->Value);
258 }
259 }
260 outs() << " ])\n";
261
262 return Res;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000263}
264
265static int DumpDysymtabCommand(MachOObject &Obj,
266 const MachOObject::LoadCommandInfo &LCI) {
267 InMemoryStruct<macho::DysymtabLoadCommand> DLC;
268 Obj.ReadDysymtabLoadCommand(LCI, DLC);
269 if (!DLC)
270 return Error("unable to read segment load command");
271
Daniel Dunbara87d7ec2010-12-10 06:19:39 +0000272 outs() << " ('ilocalsym', " << DLC->LocalSymbolsIndex << ")\n";
Daniel Dunbarf879f142010-11-27 08:33:44 +0000273 outs() << " ('nlocalsym', " << DLC->NumLocalSymbols << ")\n";
274 outs() << " ('iextdefsym', " << DLC->ExternalSymbolsIndex << ")\n";
275 outs() << " ('nextdefsym', " << DLC->NumExternalSymbols << ")\n";
276 outs() << " ('iundefsym', " << DLC->UndefinedSymbolsIndex << ")\n";
277 outs() << " ('nundefsym', " << DLC->NumUndefinedSymbols << ")\n";
278 outs() << " ('tocoff', " << DLC->TOCOffset << ")\n";
279 outs() << " ('ntoc', " << DLC->NumTOCEntries << ")\n";
280 outs() << " ('modtaboff', " << DLC->ModuleTableOffset << ")\n";
281 outs() << " ('nmodtab', " << DLC->NumModuleTableEntries << ")\n";
282 outs() << " ('extrefsymoff', " << DLC->ReferenceSymbolTableOffset << ")\n";
283 outs() << " ('nextrefsyms', "
284 << DLC->NumReferencedSymbolTableEntries << ")\n";
285 outs() << " ('indirectsymoff', " << DLC->IndirectSymbolTableOffset << ")\n";
286 outs() << " ('nindirectsyms', "
287 << DLC->NumIndirectSymbolTableEntries << ")\n";
288 outs() << " ('extreloff', " << DLC->ExternalRelocationTableOffset << ")\n";
289 outs() << " ('nextrel', " << DLC->NumExternalRelocationTableEntries << ")\n";
290 outs() << " ('locreloff', " << DLC->LocalRelocationTableOffset << ")\n";
291 outs() << " ('nlocrel', " << DLC->NumLocalRelocationTableEntries << ")\n";
292
Daniel Dunbar4c55e0d2010-11-27 13:26:12 +0000293 // Dump the indirect symbol table.
294 int Res = 0;
295 outs() << " ('_indirect_symbols', [\n";
296 for (unsigned i = 0; i != DLC->NumIndirectSymbolTableEntries; ++i) {
297 InMemoryStruct<macho::IndirectSymbolTableEntry> ISTE;
298 Obj.ReadIndirectSymbolTableEntry(*DLC, i, ISTE);
299 if (!ISTE) {
300 Res = Error("unable to read segment load command");
301 break;
302 }
303
304 outs() << " # Indirect Symbol " << i << "\n";
305 outs() << " (('symbol_index', "
Daniel Dunbar71130f82010-11-27 13:58:16 +0000306 << format("0x%x", ISTE->Index) << "),),\n";
Daniel Dunbar4c55e0d2010-11-27 13:26:12 +0000307 }
308 outs() << " ])\n";
309
310 return Res;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000311}
312
Benjamin Kramer9942aca2011-08-30 18:33:37 +0000313static int DumpLinkeditDataCommand(MachOObject &Obj,
314 const MachOObject::LoadCommandInfo &LCI) {
315 InMemoryStruct<macho::LinkeditDataLoadCommand> LLC;
316 Obj.ReadLinkeditDataLoadCommand(LCI, LLC);
317 if (!LLC)
318 return Error("unable to read segment load command");
319
320 outs() << " ('dataoff', " << LLC->DataOffset << ")\n"
Benjamin Kramerd4522462011-08-30 22:10:58 +0000321 << " ('datasize', " << LLC->DataSize << ")\n"
322 << " ('_addresses', [\n";
323
324 SmallVector<uint64_t, 8> Addresses;
325 Obj.ReadULEB128s(LLC->DataOffset, Addresses);
326 for (unsigned i = 0, e = Addresses.size(); i != e; ++i)
327 outs() << " # Address " << i << '\n'
328 << " ('address', " << format("0x%x", Addresses[i]) << "),\n";
329
330 outs() << " ])\n";
Benjamin Kramer9942aca2011-08-30 18:33:37 +0000331
332 return 0;
333}
334
335
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000336static int DumpLoadCommand(MachOObject &Obj, unsigned Index) {
337 const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index);
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000338 int Res = 0;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000339
340 outs() << " # Load Command " << Index << "\n"
341 << " (('command', " << LCI.Command.Type << ")\n"
342 << " ('size', " << LCI.Command.Size << ")\n";
343 switch (LCI.Command.Type) {
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000344 case macho::LCT_Segment:
345 Res = DumpSegmentCommand(Obj, LCI);
346 break;
347 case macho::LCT_Segment64:
348 Res = DumpSegment64Command(Obj, LCI);
349 break;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000350 case macho::LCT_Symtab:
351 Res = DumpSymtabCommand(Obj, LCI);
352 break;
353 case macho::LCT_Dysymtab:
354 Res = DumpDysymtabCommand(Obj, LCI);
355 break;
Benjamin Kramer9942aca2011-08-30 18:33:37 +0000356 case macho::LCT_CodeSignature:
357 case macho::LCT_SegmentSplitInfo:
358 case macho::LCT_FunctionStarts:
359 Res = DumpLinkeditDataCommand(Obj, LCI);
360 break;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000361 default:
362 Warning("unknown load command: " + Twine(LCI.Command.Type));
363 break;
364 }
365 outs() << " ),\n";
366
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000367 return Res;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000368}
369
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000370int main(int argc, char **argv) {
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000371 ProgramName = argv[0];
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000372 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
373
374 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
375
Daniel Dunbarad125242010-11-27 06:19:17 +0000376 // Load the input file.
377 std::string ErrorStr;
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000378 OwningPtr<MemoryBuffer> InputBuffer;
379 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer))
Michael J. Spencer333fb042010-12-09 17:36:48 +0000380 return Error("unable to read input: '" + ec.message() + "'");
Daniel Dunbarad125242010-11-27 06:19:17 +0000381
382 // Construct the Mach-O wrapper object.
383 OwningPtr<MachOObject> InputObject(
384 MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr));
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000385 if (!InputObject)
386 return Error("unable to load object: '" + ErrorStr + "'");
Daniel Dunbarad125242010-11-27 06:19:17 +0000387
Eric Christopher592cf782011-04-03 23:51:47 +0000388 // Print the header
389 InputObject->printHeader(outs());
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000390
391 // Print the load commands.
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000392 int Res = 0;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000393 outs() << "('load_commands', [\n";
394 for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i)
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000395 if ((Res = DumpLoadCommand(*InputObject, i)))
396 break;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000397 outs() << "])\n";
398
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000399 return Res;
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000400}