blob: 88fd4529ab481bc1999ea060d19d1d5848359eaf [file] [log] [blame]
Daniel Dunbarfdfb6352010-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
Rafael Espindola6e040c02013-04-26 20:07:33 +000014#include "llvm/Object/MachO.h"
Daniel Dunbare308b8e2010-11-27 13:58:16 +000015#include "llvm/ADT/StringExtras.h"
Daniel Dunbar3977e7d2010-11-27 07:19:41 +000016#include "llvm/ADT/Twine.h"
Rafael Espindola6e040c02013-04-26 20:07:33 +000017#include "llvm/Support/Casting.h"
Daniel Dunbarfdfb6352010-11-27 05:58:44 +000018#include "llvm/Support/CommandLine.h"
Daniel Dunbarc983afc2010-11-27 13:26:12 +000019#include "llvm/Support/Format.h"
Daniel Dunbarfdfb6352010-11-27 05:58:44 +000020#include "llvm/Support/ManagedStatic.h"
Daniel Dunbar9630fb32010-11-27 06:19:17 +000021#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbarfdfb6352010-11-27 05:58:44 +000022#include "llvm/Support/raw_ostream.h"
Michael J. Spencer7b6fef82010-12-09 17:36:48 +000023#include "llvm/Support/system_error.h"
Daniel Dunbarfdfb6352010-11-27 05:58:44 +000024using namespace llvm;
Daniel Dunbar9630fb32010-11-27 06:19:17 +000025using namespace llvm::object;
Daniel Dunbarfdfb6352010-11-27 05:58:44 +000026
27static cl::opt<std::string>
28InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
29
Daniel Dunbar9630fb32010-11-27 06:19:17 +000030static cl::opt<bool>
Daniel Dunbar58676902010-11-27 13:33:15 +000031ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
Daniel Dunbar9630fb32010-11-27 06:19:17 +000032 cl::init(false));
33
Daniel Dunbar3977e7d2010-11-27 07:19:41 +000034///
35
36static const char *ProgramName;
37
38static void Message(const char *Type, const Twine &Msg) {
39 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
40}
41
42static int Error(const Twine &Msg) {
43 Message("error", Msg);
44 return 1;
45}
46
47static void Warning(const Twine &Msg) {
48 Message("warning", Msg);
49}
50
51///
52
Daniel Dunbara8070e02010-11-27 08:22:29 +000053static void DumpSegmentCommandData(StringRef Name,
54 uint64_t VMAddr, uint64_t VMSize,
55 uint64_t FileOffset, uint64_t FileSize,
56 uint32_t MaxProt, uint32_t InitProt,
57 uint32_t NumSections, uint32_t Flags) {
58 outs() << " ('segment_name', '";
59 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
60 outs() << " ('vm_addr', " << VMAddr << ")\n";
61 outs() << " ('vm_size', " << VMSize << ")\n";
62 outs() << " ('file_offset', " << FileOffset << ")\n";
63 outs() << " ('file_size', " << FileSize << ")\n";
64 outs() << " ('maxprot', " << MaxProt << ")\n";
65 outs() << " ('initprot', " << InitProt << ")\n";
66 outs() << " ('num_sections', " << NumSections << ")\n";
67 outs() << " ('flags', " << Flags << ")\n";
68}
69
Rafael Espindola6e040c02013-04-26 20:07:33 +000070static int DumpSectionData(const MachOObjectFile &Obj, unsigned Index,
71 StringRef Name,
Daniel Dunbar0ac77d52010-11-27 13:39:48 +000072 StringRef SegmentName, uint64_t Address,
73 uint64_t Size, uint32_t Offset,
74 uint32_t Align, uint32_t RelocationTableOffset,
75 uint32_t NumRelocationTableEntries,
76 uint32_t Flags, uint32_t Reserved1,
77 uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) {
Daniel Dunbar58676902010-11-27 13:33:15 +000078 outs() << " # Section " << Index << "\n";
79 outs() << " (('section_name', '";
80 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
81 outs() << " ('segment_name', '";
82 outs().write_escaped(SegmentName, /*UseHexEscapes=*/true) << "')\n";
83 outs() << " ('address', " << Address << ")\n";
84 outs() << " ('size', " << Size << ")\n";
85 outs() << " ('offset', " << Offset << ")\n";
86 outs() << " ('alignment', " << Align << ")\n";
87 outs() << " ('reloc_offset', " << RelocationTableOffset << ")\n";
88 outs() << " ('num_reloc', " << NumRelocationTableEntries << ")\n";
Daniel Dunbare308b8e2010-11-27 13:58:16 +000089 outs() << " ('flags', " << format("0x%x", Flags) << ")\n";
Daniel Dunbar58676902010-11-27 13:33:15 +000090 outs() << " ('reserved1', " << Reserved1 << ")\n";
91 outs() << " ('reserved2', " << Reserved2 << ")\n";
92 if (Reserved3 != ~0ULL)
93 outs() << " ('reserved3', " << Reserved3 << ")\n";
Daniel Dunbar0ac77d52010-11-27 13:39:48 +000094 outs() << " ),\n";
95
96 // Dump the relocation entries.
Daniel Dunbar0ac77d52010-11-27 13:39:48 +000097 outs() << " ('_relocations', [\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +000098 unsigned RelNum = 0;
99 error_code EC;
100 for (relocation_iterator I = Obj.getSectionRelBegin(Index),
101 E = Obj.getSectionRelEnd(Index); I != E; I.increment(EC), ++RelNum) {
102 macho::RelocationEntry RE = Obj.getRelocation(I->getRawDataRefImpl());
103 outs() << " # Relocation " << RelNum << "\n";
104 outs() << " (('word-0', " << format("0x%x", RE.Word0) << "),\n";
105 outs() << " ('word-1', " << format("0x%x", RE.Word1) << ")),\n";
Daniel Dunbar0ac77d52010-11-27 13:39:48 +0000106 }
107 outs() << " ])\n";
108
Daniel Dunbare308b8e2010-11-27 13:58:16 +0000109 // Dump the section data, if requested.
110 if (ShowSectionData) {
111 outs() << " ('_section_data', '";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000112 StringRef Data = Obj.getData().substr(Offset, Size);
Daniel Dunbare308b8e2010-11-27 13:58:16 +0000113 for (unsigned i = 0; i != Data.size(); ++i) {
114 if (i && (i % 4) == 0)
115 outs() << ' ';
116 outs() << hexdigit((Data[i] >> 4) & 0xF, /*LowerCase=*/true);
117 outs() << hexdigit((Data[i] >> 0) & 0xF, /*LowerCase=*/true);
118 }
119 outs() << "')\n";
120 }
121
Rafael Espindola6e040c02013-04-26 20:07:33 +0000122 return 0;
Daniel Dunbar58676902010-11-27 13:33:15 +0000123}
124
Rafael Espindola6e040c02013-04-26 20:07:33 +0000125static int DumpSegmentCommand(const MachOObjectFile &Obj,
126 const MachOObjectFile::LoadCommandInfo &LCI) {
127 macho::SegmentLoadCommand SLC = Obj.getSegmentLoadCommand(LCI);
Daniel Dunbara8070e02010-11-27 08:22:29 +0000128
Rafael Espindola6e040c02013-04-26 20:07:33 +0000129 DumpSegmentCommandData(StringRef(SLC.Name, 16), SLC.VMAddress,
130 SLC.VMSize, SLC.FileOffset, SLC.FileSize,
131 SLC.MaxVMProtection, SLC.InitialVMProtection,
132 SLC.NumSections, SLC.Flags);
Daniel Dunbara8070e02010-11-27 08:22:29 +0000133
Daniel Dunbar58676902010-11-27 13:33:15 +0000134 // Dump the sections.
Daniel Dunbar58676902010-11-27 13:33:15 +0000135 outs() << " ('sections', [\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000136 for (unsigned i = 0; i != SLC.NumSections; ++i) {
137 macho::Section Sect = Obj.getSection(LCI, i);
138 DumpSectionData(Obj, i, StringRef(Sect.Name, 16),
139 StringRef(Sect.SegmentName, 16), Sect.Address,
140 Sect.Size, Sect.Offset, Sect.Align,
141 Sect.RelocationTableOffset,
142 Sect.NumRelocationTableEntries, Sect.Flags,
143 Sect.Reserved1, Sect.Reserved2);
Daniel Dunbar58676902010-11-27 13:33:15 +0000144 }
145 outs() << " ])\n";
146
Rafael Espindola6e040c02013-04-26 20:07:33 +0000147 return 0;
Daniel Dunbara8070e02010-11-27 08:22:29 +0000148}
Daniel Dunbar58676902010-11-27 13:33:15 +0000149
Rafael Espindola6e040c02013-04-26 20:07:33 +0000150static int DumpSegment64Command(const MachOObjectFile &Obj,
151 const MachOObjectFile::LoadCommandInfo &LCI) {
152 macho::Segment64LoadCommand SLC = Obj.getSegment64LoadCommand(LCI);
153 DumpSegmentCommandData(StringRef(SLC.Name, 16), SLC.VMAddress,
154 SLC.VMSize, SLC.FileOffset, SLC.FileSize,
155 SLC.MaxVMProtection, SLC.InitialVMProtection,
156 SLC.NumSections, SLC.Flags);
Daniel Dunbara8070e02010-11-27 08:22:29 +0000157
Daniel Dunbar58676902010-11-27 13:33:15 +0000158 // Dump the sections.
Daniel Dunbar58676902010-11-27 13:33:15 +0000159 outs() << " ('sections', [\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000160 for (unsigned i = 0; i != SLC.NumSections; ++i) {
161 macho::Section64 Sect = Obj.getSection64(LCI, i);
Daniel Dunbar58676902010-11-27 13:33:15 +0000162
Rafael Espindola6e040c02013-04-26 20:07:33 +0000163 DumpSectionData(Obj, i, StringRef(Sect.Name, 16),
164 StringRef(Sect.SegmentName, 16), Sect.Address,
165 Sect.Size, Sect.Offset, Sect.Align,
166 Sect.RelocationTableOffset,
167 Sect.NumRelocationTableEntries, Sect.Flags,
168 Sect.Reserved1, Sect.Reserved2,
169 Sect.Reserved3);
Daniel Dunbar58676902010-11-27 13:33:15 +0000170 }
171 outs() << " ])\n";
172
Rafael Espindola6e040c02013-04-26 20:07:33 +0000173 return 0;
Daniel Dunbara8070e02010-11-27 08:22:29 +0000174}
175
Rafael Espindola6e040c02013-04-26 20:07:33 +0000176static void DumpSymbolTableEntryData(const MachOObjectFile &Obj,
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000177 unsigned Index, uint32_t StringIndex,
178 uint8_t Type, uint8_t SectionIndex,
Rafael Espindola6e040c02013-04-26 20:07:33 +0000179 uint16_t Flags, uint64_t Value,
180 StringRef StringTable) {
181 const char *Name = &StringTable.data()[StringIndex];
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000182 outs() << " # Symbol " << Index << "\n";
183 outs() << " (('n_strx', " << StringIndex << ")\n";
Daniel Dunbare308b8e2010-11-27 13:58:16 +0000184 outs() << " ('n_type', " << format("0x%x", Type) << ")\n";
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000185 outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n";
186 outs() << " ('n_desc', " << Flags << ")\n";
187 outs() << " ('n_value', " << Value << ")\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000188 outs() << " ('_string', '" << Name << "')\n";
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000189 outs() << " ),\n";
190}
191
Rafael Espindola6e040c02013-04-26 20:07:33 +0000192static int DumpSymtabCommand(const MachOObjectFile &Obj) {
193 macho::SymtabLoadCommand SLC = Obj.getSymtabLoadCommand();
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000194
Rafael Espindola6e040c02013-04-26 20:07:33 +0000195 outs() << " ('symoff', " << SLC.SymbolTableOffset << ")\n";
196 outs() << " ('nsyms', " << SLC.NumSymbolTableEntries << ")\n";
197 outs() << " ('stroff', " << SLC.StringTableOffset << ")\n";
198 outs() << " ('strsize', " << SLC.StringTableSize << ")\n";
Daniel Dunbar8680ce62010-11-27 13:46:11 +0000199
200 // Dump the string data.
201 outs() << " ('_string_data', '";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000202 StringRef StringTable = Obj.getStringTableData();
203 outs().write_escaped(StringTable,
Daniel Dunbar8680ce62010-11-27 13:46:11 +0000204 /*UseHexEscapes=*/true) << "')\n";
205
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000206 // Dump the symbol table.
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000207 outs() << " ('_symbols', [\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000208 error_code EC;
209 unsigned SymNum = 0;
210 for (symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols(); I != E;
211 I.increment(EC), ++SymNum) {
212 DataRefImpl DRI = I->getRawDataRefImpl();
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000213 if (Obj.is64Bit()) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000214 macho::Symbol64TableEntry STE = Obj.getSymbol64TableEntry(DRI);
215 DumpSymbolTableEntryData(Obj, SymNum, STE.StringIndex, STE.Type,
216 STE.SectionIndex, STE.Flags, STE.Value,
217 StringTable);
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000218 } else {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000219 macho::SymbolTableEntry STE = Obj.getSymbolTableEntry(DRI);
220 DumpSymbolTableEntryData(Obj, SymNum, STE.StringIndex, STE.Type,
221 STE.SectionIndex, STE.Flags, STE.Value,
222 StringTable);
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000223 }
224 }
225 outs() << " ])\n";
226
Rafael Espindola6e040c02013-04-26 20:07:33 +0000227 return 0;
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000228}
229
Rafael Espindola6e040c02013-04-26 20:07:33 +0000230static int DumpDysymtabCommand(const MachOObjectFile &Obj) {
231 macho::DysymtabLoadCommand DLC = Obj.getDysymtabLoadCommand();
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000232
Rafael Espindola6e040c02013-04-26 20:07:33 +0000233 outs() << " ('ilocalsym', " << DLC.LocalSymbolsIndex << ")\n";
234 outs() << " ('nlocalsym', " << DLC.NumLocalSymbols << ")\n";
235 outs() << " ('iextdefsym', " << DLC.ExternalSymbolsIndex << ")\n";
236 outs() << " ('nextdefsym', " << DLC.NumExternalSymbols << ")\n";
237 outs() << " ('iundefsym', " << DLC.UndefinedSymbolsIndex << ")\n";
238 outs() << " ('nundefsym', " << DLC.NumUndefinedSymbols << ")\n";
239 outs() << " ('tocoff', " << DLC.TOCOffset << ")\n";
240 outs() << " ('ntoc', " << DLC.NumTOCEntries << ")\n";
241 outs() << " ('modtaboff', " << DLC.ModuleTableOffset << ")\n";
242 outs() << " ('nmodtab', " << DLC.NumModuleTableEntries << ")\n";
243 outs() << " ('extrefsymoff', " << DLC.ReferenceSymbolTableOffset << ")\n";
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000244 outs() << " ('nextrefsyms', "
Rafael Espindola6e040c02013-04-26 20:07:33 +0000245 << DLC.NumReferencedSymbolTableEntries << ")\n";
246 outs() << " ('indirectsymoff', " << DLC.IndirectSymbolTableOffset << ")\n";
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000247 outs() << " ('nindirectsyms', "
Rafael Espindola6e040c02013-04-26 20:07:33 +0000248 << DLC.NumIndirectSymbolTableEntries << ")\n";
249 outs() << " ('extreloff', " << DLC.ExternalRelocationTableOffset << ")\n";
250 outs() << " ('nextrel', " << DLC.NumExternalRelocationTableEntries << ")\n";
251 outs() << " ('locreloff', " << DLC.LocalRelocationTableOffset << ")\n";
252 outs() << " ('nlocrel', " << DLC.NumLocalRelocationTableEntries << ")\n";
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000253
Daniel Dunbarc983afc2010-11-27 13:26:12 +0000254 // Dump the indirect symbol table.
Daniel Dunbarc983afc2010-11-27 13:26:12 +0000255 outs() << " ('_indirect_symbols', [\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000256 for (unsigned i = 0; i != DLC.NumIndirectSymbolTableEntries; ++i) {
257 macho::IndirectSymbolTableEntry ISTE =
258 Obj.getIndirectSymbolTableEntry(DLC, i);
Daniel Dunbarc983afc2010-11-27 13:26:12 +0000259 outs() << " # Indirect Symbol " << i << "\n";
260 outs() << " (('symbol_index', "
Rafael Espindola6e040c02013-04-26 20:07:33 +0000261 << format("0x%x", ISTE.Index) << "),),\n";
Daniel Dunbarc983afc2010-11-27 13:26:12 +0000262 }
263 outs() << " ])\n";
264
Rafael Espindola6e040c02013-04-26 20:07:33 +0000265 return 0;
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000266}
267
Rafael Espindola6e040c02013-04-26 20:07:33 +0000268static int
269DumpLinkeditDataCommand(const MachOObjectFile &Obj,
270 const MachOObjectFile::LoadCommandInfo &LCI) {
271 macho::LinkeditDataLoadCommand LLC = Obj.getLinkeditDataLoadCommand(LCI);
272 outs() << " ('dataoff', " << LLC.DataOffset << ")\n"
273 << " ('datasize', " << LLC.DataSize << ")\n"
Benjamin Kramer58298f02011-08-30 22:10:58 +0000274 << " ('_addresses', [\n";
275
276 SmallVector<uint64_t, 8> Addresses;
Rafael Espindola6e040c02013-04-26 20:07:33 +0000277 Obj.ReadULEB128s(LLC.DataOffset, Addresses);
Benjamin Kramer58298f02011-08-30 22:10:58 +0000278 for (unsigned i = 0, e = Addresses.size(); i != e; ++i)
279 outs() << " # Address " << i << '\n'
280 << " ('address', " << format("0x%x", Addresses[i]) << "),\n";
281
282 outs() << " ])\n";
Benjamin Kramer267a6d92011-08-30 18:33:37 +0000283
284 return 0;
285}
286
Rafael Espindola6e040c02013-04-26 20:07:33 +0000287static int
288DumpDataInCodeDataCommand(const MachOObjectFile &Obj,
289 const MachOObjectFile::LoadCommandInfo &LCI) {
290 macho::LinkeditDataLoadCommand LLC = Obj.getLinkeditDataLoadCommand(LCI);
291 outs() << " ('dataoff', " << LLC.DataOffset << ")\n"
292 << " ('datasize', " << LLC.DataSize << ")\n"
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000293 << " ('_data_regions', [\n";
294
Rafael Espindola6e040c02013-04-26 20:07:33 +0000295 unsigned NumRegions = LLC.DataSize / 8;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000296 for (unsigned i = 0; i < NumRegions; ++i) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000297 macho::DataInCodeTableEntry DICE =
298 Obj.getDataInCodeTableEntry(LLC.DataOffset, i);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000299 outs() << " # DICE " << i << "\n"
Rafael Espindola6e040c02013-04-26 20:07:33 +0000300 << " ('offset', " << DICE.Offset << ")\n"
301 << " ('length', " << DICE.Length << ")\n"
302 << " ('kind', " << DICE.Kind << ")\n";
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000303 }
304
305 outs() <<" ])\n";
306
307 return 0;
308}
309
Rafael Espindola6e040c02013-04-26 20:07:33 +0000310static int
311DumpLinkerOptionsCommand(const MachOObjectFile &Obj,
312 const MachOObjectFile::LoadCommandInfo &LCI) {
313 macho::LinkerOptionsLoadCommand LOLC = Obj.getLinkerOptionsLoadCommand(LCI);
314 outs() << " ('count', " << LOLC.Count << ")\n"
315 << " ('_strings', [\n";
Daniel Dunbareec0f322013-01-18 01:26:07 +0000316
Rafael Espindola6e040c02013-04-26 20:07:33 +0000317 uint64_t DataSize = LOLC.Size - sizeof(macho::LinkerOptionsLoadCommand);
318 const char *P = LCI.Ptr + sizeof(macho::LinkerOptionsLoadCommand);
319 StringRef Data(P, DataSize);
320 for (unsigned i = 0; i != LOLC.Count; ++i) {
321 std::pair<StringRef,StringRef> Split = Data.split('\0');
322 outs() << "\t\"";
323 outs().write_escaped(Split.first);
324 outs() << "\",\n";
325 Data = Split.second;
326 }
327 outs() <<" ])\n";
Daniel Dunbareec0f322013-01-18 01:26:07 +0000328
329 return 0;
330}
331
Rafael Espindola6e040c02013-04-26 20:07:33 +0000332static int DumpLoadCommand(const MachOObjectFile &Obj,
333 MachOObjectFile::LoadCommandInfo &LCI) {
334 switch (LCI.C.Type) {
Daniel Dunbara8070e02010-11-27 08:22:29 +0000335 case macho::LCT_Segment:
Rafael Espindola6e040c02013-04-26 20:07:33 +0000336 return DumpSegmentCommand(Obj, LCI);
Daniel Dunbara8070e02010-11-27 08:22:29 +0000337 case macho::LCT_Segment64:
Rafael Espindola6e040c02013-04-26 20:07:33 +0000338 return DumpSegment64Command(Obj, LCI);
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000339 case macho::LCT_Symtab:
Rafael Espindola6e040c02013-04-26 20:07:33 +0000340 return DumpSymtabCommand(Obj);
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000341 case macho::LCT_Dysymtab:
Rafael Espindola6e040c02013-04-26 20:07:33 +0000342 return DumpDysymtabCommand(Obj);
Benjamin Kramer267a6d92011-08-30 18:33:37 +0000343 case macho::LCT_CodeSignature:
344 case macho::LCT_SegmentSplitInfo:
345 case macho::LCT_FunctionStarts:
Rafael Espindola6e040c02013-04-26 20:07:33 +0000346 return DumpLinkeditDataCommand(Obj, LCI);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000347 case macho::LCT_DataInCode:
Rafael Espindola6e040c02013-04-26 20:07:33 +0000348 return DumpDataInCodeDataCommand(Obj, LCI);
Daniel Dunbareec0f322013-01-18 01:26:07 +0000349 case macho::LCT_LinkerOptions:
Rafael Espindola6e040c02013-04-26 20:07:33 +0000350 return DumpLinkerOptionsCommand(Obj, LCI);
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000351 default:
Rafael Espindola6e040c02013-04-26 20:07:33 +0000352 Warning("unknown load command: " + Twine(LCI.C.Type));
353 return 0;
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000354 }
Rafael Espindola6e040c02013-04-26 20:07:33 +0000355}
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000356
Rafael Espindola6e040c02013-04-26 20:07:33 +0000357
358static int DumpLoadCommand(const MachOObjectFile &Obj, unsigned Index,
359 MachOObjectFile::LoadCommandInfo &LCI) {
360 outs() << " # Load Command " << Index << "\n"
361 << " (('command', " << LCI.C.Type << ")\n"
362 << " ('size', " << LCI.C.Size << ")\n";
363 int Res = DumpLoadCommand(Obj, LCI);
364 outs() << " ),\n";
Daniel Dunbara8070e02010-11-27 08:22:29 +0000365 return Res;
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000366}
367
Rafael Espindola6e040c02013-04-26 20:07:33 +0000368static void printHeader(const MachOObjectFile *Obj,
369 const macho::Header &Header) {
370 outs() << "('cputype', " << Header.CPUType << ")\n";
371 outs() << "('cpusubtype', " << Header.CPUSubtype << ")\n";
372 outs() << "('filetype', " << Header.FileType << ")\n";
373 outs() << "('num_load_commands', " << Header.NumLoadCommands << ")\n";
374 outs() << "('load_commands_size', " << Header.SizeOfLoadCommands << ")\n";
375 outs() << "('flag', " << Header.Flags << ")\n";
376
377 // Print extended header if 64-bit.
378 if (Obj->is64Bit()) {
379 macho::Header64Ext Header64Ext = Obj->getHeader64Ext();
380 outs() << "('reserved', " << Header64Ext.Reserved << ")\n";
381 }
382}
383
Daniel Dunbarfdfb6352010-11-27 05:58:44 +0000384int main(int argc, char **argv) {
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000385 ProgramName = argv[0];
Daniel Dunbarfdfb6352010-11-27 05:58:44 +0000386 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
387
388 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
389
Rafael Espindola6e040c02013-04-26 20:07:33 +0000390 OwningPtr<Binary> Binary;
391 if (error_code EC = createBinary(InputFile, Binary))
392 return Error("unable to read input: '" + EC.message() + "'");
Daniel Dunbar9630fb32010-11-27 06:19:17 +0000393
Rafael Espindola6e040c02013-04-26 20:07:33 +0000394 const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(Binary.get());
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000395 if (!InputObject)
Rafael Espindola6e040c02013-04-26 20:07:33 +0000396 return Error("Not a MachO object");
Daniel Dunbar9630fb32010-11-27 06:19:17 +0000397
Eric Christopher19ea5ae2011-04-03 23:51:47 +0000398 // Print the header
Rafael Espindola6e040c02013-04-26 20:07:33 +0000399 macho::Header Header = InputObject->getHeader();
400 printHeader(InputObject, Header);
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000401
402 // Print the load commands.
Daniel Dunbara8070e02010-11-27 08:22:29 +0000403 int Res = 0;
Rafael Espindola6e040c02013-04-26 20:07:33 +0000404 MachOObjectFile::LoadCommandInfo Command =
405 InputObject->getFirstLoadCommandInfo();
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000406 outs() << "('load_commands', [\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000407 for (unsigned i = 0; ; ++i) {
408 if (DumpLoadCommand(*InputObject, i, Command))
Daniel Dunbara8070e02010-11-27 08:22:29 +0000409 break;
Rafael Espindola6e040c02013-04-26 20:07:33 +0000410
411 if (i == Header.NumLoadCommands - 1)
412 break;
413 Command = InputObject->getNextLoadCommandInfo(Command);
414 }
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000415 outs() << "])\n";
416
Daniel Dunbara8070e02010-11-27 08:22:29 +0000417 return Res;
Daniel Dunbarfdfb6352010-11-27 05:58:44 +0000418}