blob: 02fa36680f8aa696b142a4a0710c46b5e9113b0d [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"
22using namespace llvm;
Daniel Dunbarad125242010-11-27 06:19:17 +000023using namespace llvm::object;
Daniel Dunbar75373ac2010-11-27 05:58:44 +000024
25static cl::opt<std::string>
26InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
27
Daniel Dunbarad125242010-11-27 06:19:17 +000028static cl::opt<bool>
Daniel Dunbar2acadbd2010-11-27 13:33:15 +000029ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
Daniel Dunbarad125242010-11-27 06:19:17 +000030 cl::init(false));
31
Daniel Dunbara956d8b2010-11-27 07:19:41 +000032///
33
34static const char *ProgramName;
35
36static void Message(const char *Type, const Twine &Msg) {
37 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
38}
39
40static int Error(const Twine &Msg) {
41 Message("error", Msg);
42 return 1;
43}
44
45static void Warning(const Twine &Msg) {
46 Message("warning", Msg);
47}
48
49///
50
51static int DumpHeader(MachOObject &Obj) {
52 // Read the header.
53 const macho::Header &Hdr = Obj.getHeader();
54 outs() << "('cputype', " << Hdr.CPUType << ")\n";
55 outs() << "('cpusubtype', " << Hdr.CPUSubtype << ")\n";
56 outs() << "('filetype', " << Hdr.FileType << ")\n";
57 outs() << "('num_load_commands', " << Hdr.NumLoadCommands << ")\n";
58 outs() << "('load_commands_size', " << Hdr.SizeOfLoadCommands << ")\n";
59 outs() << "('flag', " << Hdr.Flags << ")\n";
60
61 // Print extended header if 64-bit.
62 if (Obj.is64Bit()) {
63 const macho::Header64Ext &Hdr64 = Obj.getHeader64Ext();
64 outs() << "('reserved', " << Hdr64.Reserved << ")\n";
65 }
66
67 return 0;
68}
69
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +000070static void DumpSegmentCommandData(StringRef Name,
71 uint64_t VMAddr, uint64_t VMSize,
72 uint64_t FileOffset, uint64_t FileSize,
73 uint32_t MaxProt, uint32_t InitProt,
74 uint32_t NumSections, uint32_t Flags) {
75 outs() << " ('segment_name', '";
76 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
77 outs() << " ('vm_addr', " << VMAddr << ")\n";
78 outs() << " ('vm_size', " << VMSize << ")\n";
79 outs() << " ('file_offset', " << FileOffset << ")\n";
80 outs() << " ('file_size', " << FileSize << ")\n";
81 outs() << " ('maxprot', " << MaxProt << ")\n";
82 outs() << " ('initprot', " << InitProt << ")\n";
83 outs() << " ('num_sections', " << NumSections << ")\n";
84 outs() << " ('flags', " << Flags << ")\n";
85}
86
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +000087static int DumpSectionData(MachOObject &Obj, unsigned Index, StringRef Name,
88 StringRef SegmentName, uint64_t Address,
89 uint64_t Size, uint32_t Offset,
90 uint32_t Align, uint32_t RelocationTableOffset,
91 uint32_t NumRelocationTableEntries,
92 uint32_t Flags, uint32_t Reserved1,
93 uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) {
Daniel Dunbar2acadbd2010-11-27 13:33:15 +000094 outs() << " # Section " << Index << "\n";
95 outs() << " (('section_name', '";
96 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
97 outs() << " ('segment_name', '";
98 outs().write_escaped(SegmentName, /*UseHexEscapes=*/true) << "')\n";
99 outs() << " ('address', " << Address << ")\n";
100 outs() << " ('size', " << Size << ")\n";
101 outs() << " ('offset', " << Offset << ")\n";
102 outs() << " ('alignment', " << Align << ")\n";
103 outs() << " ('reloc_offset', " << RelocationTableOffset << ")\n";
104 outs() << " ('num_reloc', " << NumRelocationTableEntries << ")\n";
Daniel Dunbar71130f82010-11-27 13:58:16 +0000105 outs() << " ('flags', " << format("0x%x", Flags) << ")\n";
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000106 outs() << " ('reserved1', " << Reserved1 << ")\n";
107 outs() << " ('reserved2', " << Reserved2 << ")\n";
108 if (Reserved3 != ~0ULL)
109 outs() << " ('reserved3', " << Reserved3 << ")\n";
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000110 outs() << " ),\n";
111
112 // Dump the relocation entries.
113 int Res = 0;
114 outs() << " ('_relocations', [\n";
115 for (unsigned i = 0; i != NumRelocationTableEntries; ++i) {
116 InMemoryStruct<macho::RelocationEntry> RE;
117 Obj.ReadRelocationEntry(RelocationTableOffset, i, RE);
118 if (!RE) {
119 Res = Error("unable to read relocation table entry '" + Twine(i) + "'");
120 break;
121 }
122
123 outs() << " # Relocation " << i << "\n";
Daniel Dunbar71130f82010-11-27 13:58:16 +0000124 outs() << " (('word-0', " << format("0x%x", RE->Word0) << "),\n";
125 outs() << " ('word-1', " << format("0x%x", RE->Word1) << ")),\n";
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000126 }
127 outs() << " ])\n";
128
Daniel Dunbar71130f82010-11-27 13:58:16 +0000129 // Dump the section data, if requested.
130 if (ShowSectionData) {
131 outs() << " ('_section_data', '";
132 StringRef Data = Obj.getData(Offset, Size);
133 for (unsigned i = 0; i != Data.size(); ++i) {
134 if (i && (i % 4) == 0)
135 outs() << ' ';
136 outs() << hexdigit((Data[i] >> 4) & 0xF, /*LowerCase=*/true);
137 outs() << hexdigit((Data[i] >> 0) & 0xF, /*LowerCase=*/true);
138 }
139 outs() << "')\n";
140 }
141
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000142 return Res;
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000143}
144
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000145static int DumpSegmentCommand(MachOObject &Obj,
146 const MachOObject::LoadCommandInfo &LCI) {
147 InMemoryStruct<macho::SegmentLoadCommand> SLC;
148 Obj.ReadSegmentLoadCommand(LCI, SLC);
149 if (!SLC)
150 return Error("unable to read segment load command");
151
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000152 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress,
153 SLC->VMSize, SLC->FileOffset, SLC->FileSize,
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000154 SLC->MaxVMProtection, SLC->InitialVMProtection,
155 SLC->NumSections, SLC->Flags);
156
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000157 // Dump the sections.
158 int Res = 0;
159 outs() << " ('sections', [\n";
160 for (unsigned i = 0; i != SLC->NumSections; ++i) {
161 InMemoryStruct<macho::Section> Sect;
162 Obj.ReadSection(LCI, i, Sect);
163 if (!SLC) {
164 Res = Error("unable to read section '" + Twine(i) + "'");
165 break;
166 }
167
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000168 if ((Res = DumpSectionData(Obj, i, StringRef(Sect->Name, 16),
169 StringRef(Sect->SegmentName, 16), Sect->Address,
170 Sect->Size, Sect->Offset, Sect->Align,
171 Sect->RelocationTableOffset,
172 Sect->NumRelocationTableEntries, Sect->Flags,
173 Sect->Reserved1, Sect->Reserved2)))
174 break;
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000175 }
176 outs() << " ])\n";
177
178 return Res;
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000179}
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000180
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000181static int DumpSegment64Command(MachOObject &Obj,
182 const MachOObject::LoadCommandInfo &LCI) {
183 InMemoryStruct<macho::Segment64LoadCommand> SLC;
184 Obj.ReadSegment64LoadCommand(LCI, SLC);
185 if (!SLC)
186 return Error("unable to read segment load command");
187
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000188 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress,
189 SLC->VMSize, SLC->FileOffset, SLC->FileSize,
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000190 SLC->MaxVMProtection, SLC->InitialVMProtection,
191 SLC->NumSections, SLC->Flags);
192
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000193 // Dump the sections.
194 int Res = 0;
195 outs() << " ('sections', [\n";
196 for (unsigned i = 0; i != SLC->NumSections; ++i) {
197 InMemoryStruct<macho::Section64> Sect;
198 Obj.ReadSection64(LCI, i, Sect);
199 if (!SLC) {
200 Res = Error("unable to read section '" + Twine(i) + "'");
201 break;
202 }
203
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000204 if ((Res = DumpSectionData(Obj, i, StringRef(Sect->Name, 16),
205 StringRef(Sect->SegmentName, 16), Sect->Address,
206 Sect->Size, Sect->Offset, Sect->Align,
207 Sect->RelocationTableOffset,
208 Sect->NumRelocationTableEntries, Sect->Flags,
209 Sect->Reserved1, Sect->Reserved2,
210 Sect->Reserved3)))
211 break;
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000212 }
213 outs() << " ])\n";
214
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000215 return 0;
216}
217
Daniel Dunbar2208b582010-11-27 13:52:53 +0000218static void DumpSymbolTableEntryData(MachOObject &Obj,
219 unsigned Index, uint32_t StringIndex,
220 uint8_t Type, uint8_t SectionIndex,
221 uint16_t Flags, uint64_t Value) {
222 outs() << " # Symbol " << Index << "\n";
223 outs() << " (('n_strx', " << StringIndex << ")\n";
Daniel Dunbar71130f82010-11-27 13:58:16 +0000224 outs() << " ('n_type', " << format("0x%x", Type) << ")\n";
Daniel Dunbar2208b582010-11-27 13:52:53 +0000225 outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n";
226 outs() << " ('n_desc', " << Flags << ")\n";
227 outs() << " ('n_value', " << Value << ")\n";
228 outs() << " ('_string', '" << Obj.getStringAtIndex(StringIndex) << "')\n";
229 outs() << " ),\n";
230}
231
Daniel Dunbarf879f142010-11-27 08:33:44 +0000232static int DumpSymtabCommand(MachOObject &Obj,
233 const MachOObject::LoadCommandInfo &LCI) {
234 InMemoryStruct<macho::SymtabLoadCommand> SLC;
235 Obj.ReadSymtabLoadCommand(LCI, SLC);
236 if (!SLC)
237 return Error("unable to read segment load command");
238
239 outs() << " ('symoff', " << SLC->SymbolTableOffset << ")\n";
240 outs() << " ('nsyms', " << SLC->NumSymbolTableEntries << ")\n";
241 outs() << " ('stroff', " << SLC->StringTableOffset << ")\n";
242 outs() << " ('strsize', " << SLC->StringTableSize << ")\n";
243
Daniel Dunbarf2e2a5f2010-11-27 13:46:11 +0000244 // Cache the string table data.
245 Obj.RegisterStringTable(*SLC);
246
247 // Dump the string data.
248 outs() << " ('_string_data', '";
249 outs().write_escaped(Obj.getStringTableData(),
250 /*UseHexEscapes=*/true) << "')\n";
251
Daniel Dunbar2208b582010-11-27 13:52:53 +0000252 // Dump the symbol table.
253 int Res = 0;
254 outs() << " ('_symbols', [\n";
255 for (unsigned i = 0; i != SLC->NumSymbolTableEntries; ++i) {
256 if (Obj.is64Bit()) {
257 InMemoryStruct<macho::Symbol64TableEntry> STE;
258 Obj.ReadSymbol64TableEntry(SLC->SymbolTableOffset, i, STE);
259 if (!STE) {
260 Res = Error("unable to read symbol: '" + Twine(i) + "'");
261 break;
262 }
263
264 DumpSymbolTableEntryData(Obj, i, STE->StringIndex, STE->Type,
265 STE->SectionIndex, STE->Flags, STE->Value);
266 } else {
267 InMemoryStruct<macho::SymbolTableEntry> STE;
268 Obj.ReadSymbolTableEntry(SLC->SymbolTableOffset, i, STE);
269 if (!SLC) {
270 Res = Error("unable to read symbol: '" + Twine(i) + "'");
271 break;
272 }
273
274 DumpSymbolTableEntryData(Obj, i, STE->StringIndex, STE->Type,
275 STE->SectionIndex, STE->Flags, STE->Value);
276 }
277 }
278 outs() << " ])\n";
279
280 return Res;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000281}
282
283static int DumpDysymtabCommand(MachOObject &Obj,
284 const MachOObject::LoadCommandInfo &LCI) {
285 InMemoryStruct<macho::DysymtabLoadCommand> DLC;
286 Obj.ReadDysymtabLoadCommand(LCI, DLC);
287 if (!DLC)
288 return Error("unable to read segment load command");
289
290 outs() << " ('ilocalsym', " << DLC->LocalSymbolIndex << ")\n";
291 outs() << " ('nlocalsym', " << DLC->NumLocalSymbols << ")\n";
292 outs() << " ('iextdefsym', " << DLC->ExternalSymbolsIndex << ")\n";
293 outs() << " ('nextdefsym', " << DLC->NumExternalSymbols << ")\n";
294 outs() << " ('iundefsym', " << DLC->UndefinedSymbolsIndex << ")\n";
295 outs() << " ('nundefsym', " << DLC->NumUndefinedSymbols << ")\n";
296 outs() << " ('tocoff', " << DLC->TOCOffset << ")\n";
297 outs() << " ('ntoc', " << DLC->NumTOCEntries << ")\n";
298 outs() << " ('modtaboff', " << DLC->ModuleTableOffset << ")\n";
299 outs() << " ('nmodtab', " << DLC->NumModuleTableEntries << ")\n";
300 outs() << " ('extrefsymoff', " << DLC->ReferenceSymbolTableOffset << ")\n";
301 outs() << " ('nextrefsyms', "
302 << DLC->NumReferencedSymbolTableEntries << ")\n";
303 outs() << " ('indirectsymoff', " << DLC->IndirectSymbolTableOffset << ")\n";
304 outs() << " ('nindirectsyms', "
305 << DLC->NumIndirectSymbolTableEntries << ")\n";
306 outs() << " ('extreloff', " << DLC->ExternalRelocationTableOffset << ")\n";
307 outs() << " ('nextrel', " << DLC->NumExternalRelocationTableEntries << ")\n";
308 outs() << " ('locreloff', " << DLC->LocalRelocationTableOffset << ")\n";
309 outs() << " ('nlocrel', " << DLC->NumLocalRelocationTableEntries << ")\n";
310
Daniel Dunbar4c55e0d2010-11-27 13:26:12 +0000311 // Dump the indirect symbol table.
312 int Res = 0;
313 outs() << " ('_indirect_symbols', [\n";
314 for (unsigned i = 0; i != DLC->NumIndirectSymbolTableEntries; ++i) {
315 InMemoryStruct<macho::IndirectSymbolTableEntry> ISTE;
316 Obj.ReadIndirectSymbolTableEntry(*DLC, i, ISTE);
317 if (!ISTE) {
318 Res = Error("unable to read segment load command");
319 break;
320 }
321
322 outs() << " # Indirect Symbol " << i << "\n";
323 outs() << " (('symbol_index', "
Daniel Dunbar71130f82010-11-27 13:58:16 +0000324 << format("0x%x", ISTE->Index) << "),),\n";
Daniel Dunbar4c55e0d2010-11-27 13:26:12 +0000325 }
326 outs() << " ])\n";
327
328 return Res;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000329}
330
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000331static int DumpLoadCommand(MachOObject &Obj, unsigned Index) {
332 const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index);
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000333 int Res = 0;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000334
335 outs() << " # Load Command " << Index << "\n"
336 << " (('command', " << LCI.Command.Type << ")\n"
337 << " ('size', " << LCI.Command.Size << ")\n";
338 switch (LCI.Command.Type) {
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000339 case macho::LCT_Segment:
340 Res = DumpSegmentCommand(Obj, LCI);
341 break;
342 case macho::LCT_Segment64:
343 Res = DumpSegment64Command(Obj, LCI);
344 break;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000345 case macho::LCT_Symtab:
346 Res = DumpSymtabCommand(Obj, LCI);
347 break;
348 case macho::LCT_Dysymtab:
349 Res = DumpDysymtabCommand(Obj, LCI);
350 break;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000351 default:
352 Warning("unknown load command: " + Twine(LCI.Command.Type));
353 break;
354 }
355 outs() << " ),\n";
356
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000357 return Res;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000358}
359
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000360int main(int argc, char **argv) {
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000361 ProgramName = argv[0];
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000362 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
363
364 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
365
Daniel Dunbarad125242010-11-27 06:19:17 +0000366 // Load the input file.
367 std::string ErrorStr;
368 OwningPtr<MemoryBuffer> InputBuffer(
369 MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr));
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000370 if (!InputBuffer)
371 return Error("unable to read input: '" + ErrorStr + "'");
Daniel Dunbarad125242010-11-27 06:19:17 +0000372
373 // Construct the Mach-O wrapper object.
374 OwningPtr<MachOObject> InputObject(
375 MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr));
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000376 if (!InputObject)
377 return Error("unable to load object: '" + ErrorStr + "'");
Daniel Dunbarad125242010-11-27 06:19:17 +0000378
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000379 if (int Res = DumpHeader(*InputObject))
380 return Res;
381
382 // Print the load commands.
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000383 int Res = 0;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000384 outs() << "('load_commands', [\n";
385 for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i)
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000386 if ((Res = DumpLoadCommand(*InputObject, i)))
387 break;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000388 outs() << "])\n";
389
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000390 return Res;
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000391}