blob: 13e8a4b80ca98933ada125d958bbaee34eaf3548 [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"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000023#include <system_error>
Daniel Dunbarfdfb6352010-11-27 05:58:44 +000024using namespace llvm;
Daniel Dunbar9630fb32010-11-27 06:19:17 +000025using namespace llvm::object;
Rafael Espindola3acea392014-06-12 21:46:39 +000026using std::error_code;
Daniel Dunbarfdfb6352010-11-27 05:58:44 +000027
28static cl::opt<std::string>
29InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
30
Daniel Dunbar9630fb32010-11-27 06:19:17 +000031static cl::opt<bool>
Daniel Dunbar58676902010-11-27 13:33:15 +000032ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
Daniel Dunbar9630fb32010-11-27 06:19:17 +000033 cl::init(false));
34
Daniel Dunbar3977e7d2010-11-27 07:19:41 +000035///
36
37static const char *ProgramName;
38
39static void Message(const char *Type, const Twine &Msg) {
40 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
41}
42
43static int Error(const Twine &Msg) {
44 Message("error", Msg);
45 return 1;
46}
47
48static void Warning(const Twine &Msg) {
49 Message("warning", Msg);
50}
51
52///
53
Daniel Dunbara8070e02010-11-27 08:22:29 +000054static void DumpSegmentCommandData(StringRef Name,
55 uint64_t VMAddr, uint64_t VMSize,
56 uint64_t FileOffset, uint64_t FileSize,
57 uint32_t MaxProt, uint32_t InitProt,
58 uint32_t NumSections, uint32_t Flags) {
59 outs() << " ('segment_name', '";
60 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
61 outs() << " ('vm_addr', " << VMAddr << ")\n";
62 outs() << " ('vm_size', " << VMSize << ")\n";
63 outs() << " ('file_offset', " << FileOffset << ")\n";
64 outs() << " ('file_size', " << FileSize << ")\n";
65 outs() << " ('maxprot', " << MaxProt << ")\n";
66 outs() << " ('initprot', " << InitProt << ")\n";
67 outs() << " ('num_sections', " << NumSections << ")\n";
68 outs() << " ('flags', " << Flags << ")\n";
69}
70
Rafael Espindola6e040c02013-04-26 20:07:33 +000071static int DumpSectionData(const MachOObjectFile &Obj, unsigned Index,
72 StringRef Name,
Daniel Dunbar0ac77d52010-11-27 13:39:48 +000073 StringRef SegmentName, uint64_t Address,
74 uint64_t Size, uint32_t Offset,
75 uint32_t Align, uint32_t RelocationTableOffset,
76 uint32_t NumRelocationTableEntries,
77 uint32_t Flags, uint32_t Reserved1,
78 uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) {
Daniel Dunbar58676902010-11-27 13:33:15 +000079 outs() << " # Section " << Index << "\n";
80 outs() << " (('section_name', '";
81 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
82 outs() << " ('segment_name', '";
83 outs().write_escaped(SegmentName, /*UseHexEscapes=*/true) << "')\n";
84 outs() << " ('address', " << Address << ")\n";
85 outs() << " ('size', " << Size << ")\n";
86 outs() << " ('offset', " << Offset << ")\n";
87 outs() << " ('alignment', " << Align << ")\n";
88 outs() << " ('reloc_offset', " << RelocationTableOffset << ")\n";
89 outs() << " ('num_reloc', " << NumRelocationTableEntries << ")\n";
Daniel Dunbare308b8e2010-11-27 13:58:16 +000090 outs() << " ('flags', " << format("0x%x", Flags) << ")\n";
Daniel Dunbar58676902010-11-27 13:33:15 +000091 outs() << " ('reserved1', " << Reserved1 << ")\n";
92 outs() << " ('reserved2', " << Reserved2 << ")\n";
93 if (Reserved3 != ~0ULL)
94 outs() << " ('reserved3', " << Reserved3 << ")\n";
Daniel Dunbar0ac77d52010-11-27 13:39:48 +000095 outs() << " ),\n";
96
97 // Dump the relocation entries.
Daniel Dunbar0ac77d52010-11-27 13:39:48 +000098 outs() << " ('_relocations', [\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +000099 unsigned RelNum = 0;
Rui Ueyamabc654b12013-09-27 21:47:05 +0000100 for (relocation_iterator I = Obj.section_rel_begin(Index),
Rafael Espindola5e812af2014-01-30 02:49:50 +0000101 E = Obj.section_rel_end(Index);
102 I != E; ++I, ++RelNum) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000103 MachO::any_relocation_info RE = Obj.getRelocation(I->getRawDataRefImpl());
Rafael Espindola6e040c02013-04-26 20:07:33 +0000104 outs() << " # Relocation " << RelNum << "\n";
Charles Davis8bdfafd2013-09-01 04:28:48 +0000105 outs() << " (('word-0', " << format("0x%x", RE.r_word0) << "),\n";
106 outs() << " ('word-1', " << format("0x%x", RE.r_word1) << ")),\n";
Daniel Dunbar0ac77d52010-11-27 13:39:48 +0000107 }
108 outs() << " ])\n";
109
Daniel Dunbare308b8e2010-11-27 13:58:16 +0000110 // Dump the section data, if requested.
111 if (ShowSectionData) {
112 outs() << " ('_section_data', '";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000113 StringRef Data = Obj.getData().substr(Offset, Size);
Daniel Dunbare308b8e2010-11-27 13:58:16 +0000114 for (unsigned i = 0; i != Data.size(); ++i) {
115 if (i && (i % 4) == 0)
116 outs() << ' ';
117 outs() << hexdigit((Data[i] >> 4) & 0xF, /*LowerCase=*/true);
118 outs() << hexdigit((Data[i] >> 0) & 0xF, /*LowerCase=*/true);
119 }
120 outs() << "')\n";
121 }
122
Rafael Espindola6e040c02013-04-26 20:07:33 +0000123 return 0;
Daniel Dunbar58676902010-11-27 13:33:15 +0000124}
125
Rafael Espindola6e040c02013-04-26 20:07:33 +0000126static int DumpSegmentCommand(const MachOObjectFile &Obj,
127 const MachOObjectFile::LoadCommandInfo &LCI) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000128 MachO::segment_command SLC = Obj.getSegmentLoadCommand(LCI);
Daniel Dunbara8070e02010-11-27 08:22:29 +0000129
Charles Davis8bdfafd2013-09-01 04:28:48 +0000130 DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr,
131 SLC.vmsize, SLC.fileoff, SLC.filesize,
132 SLC.maxprot, SLC.initprot, SLC.nsects, 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";
Charles Davis8bdfafd2013-09-01 04:28:48 +0000136 for (unsigned i = 0; i != SLC.nsects; ++i) {
137 MachO::section Sect = Obj.getSection(LCI, i);
138 DumpSectionData(Obj, i, StringRef(Sect.sectname, 16),
139 StringRef(Sect.segname, 16), Sect.addr,
140 Sect.size, Sect.offset, Sect.align,
141 Sect.reloff, Sect.nreloc, Sect.flags,
142 Sect.reserved1, Sect.reserved2);
Daniel Dunbar58676902010-11-27 13:33:15 +0000143 }
144 outs() << " ])\n";
145
Rafael Espindola6e040c02013-04-26 20:07:33 +0000146 return 0;
Daniel Dunbara8070e02010-11-27 08:22:29 +0000147}
Daniel Dunbar58676902010-11-27 13:33:15 +0000148
Rafael Espindola6e040c02013-04-26 20:07:33 +0000149static int DumpSegment64Command(const MachOObjectFile &Obj,
150 const MachOObjectFile::LoadCommandInfo &LCI) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000151 MachO::segment_command_64 SLC = Obj.getSegment64LoadCommand(LCI);
152 DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr,
153 SLC.vmsize, SLC.fileoff, SLC.filesize,
154 SLC.maxprot, SLC.initprot, SLC.nsects, SLC.flags);
Daniel Dunbara8070e02010-11-27 08:22:29 +0000155
Daniel Dunbar58676902010-11-27 13:33:15 +0000156 // Dump the sections.
Daniel Dunbar58676902010-11-27 13:33:15 +0000157 outs() << " ('sections', [\n";
Charles Davis8bdfafd2013-09-01 04:28:48 +0000158 for (unsigned i = 0; i != SLC.nsects; ++i) {
159 MachO::section_64 Sect = Obj.getSection64(LCI, i);
Daniel Dunbar58676902010-11-27 13:33:15 +0000160
Charles Davis8bdfafd2013-09-01 04:28:48 +0000161 DumpSectionData(Obj, i, StringRef(Sect.sectname, 16),
162 StringRef(Sect.segname, 16), Sect.addr,
163 Sect.size, Sect.offset, Sect.align,
164 Sect.reloff, Sect.nreloc, Sect.flags,
165 Sect.reserved1, Sect.reserved2,
166 Sect.reserved3);
Daniel Dunbar58676902010-11-27 13:33:15 +0000167 }
168 outs() << " ])\n";
169
Rafael Espindola6e040c02013-04-26 20:07:33 +0000170 return 0;
Daniel Dunbara8070e02010-11-27 08:22:29 +0000171}
172
Rafael Espindola6e040c02013-04-26 20:07:33 +0000173static void DumpSymbolTableEntryData(const MachOObjectFile &Obj,
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000174 unsigned Index, uint32_t StringIndex,
175 uint8_t Type, uint8_t SectionIndex,
Rafael Espindola6e040c02013-04-26 20:07:33 +0000176 uint16_t Flags, uint64_t Value,
177 StringRef StringTable) {
178 const char *Name = &StringTable.data()[StringIndex];
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000179 outs() << " # Symbol " << Index << "\n";
180 outs() << " (('n_strx', " << StringIndex << ")\n";
Daniel Dunbare308b8e2010-11-27 13:58:16 +0000181 outs() << " ('n_type', " << format("0x%x", Type) << ")\n";
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000182 outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n";
183 outs() << " ('n_desc', " << Flags << ")\n";
184 outs() << " ('n_value', " << Value << ")\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000185 outs() << " ('_string', '" << Name << "')\n";
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000186 outs() << " ),\n";
187}
188
Rafael Espindola6e040c02013-04-26 20:07:33 +0000189static int DumpSymtabCommand(const MachOObjectFile &Obj) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000190 MachO::symtab_command SLC = Obj.getSymtabLoadCommand();
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000191
Charles Davis8bdfafd2013-09-01 04:28:48 +0000192 outs() << " ('symoff', " << SLC.symoff << ")\n";
193 outs() << " ('nsyms', " << SLC.nsyms << ")\n";
194 outs() << " ('stroff', " << SLC.stroff << ")\n";
195 outs() << " ('strsize', " << SLC.strsize << ")\n";
Daniel Dunbar8680ce62010-11-27 13:46:11 +0000196
197 // Dump the string data.
198 outs() << " ('_string_data', '";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000199 StringRef StringTable = Obj.getStringTableData();
200 outs().write_escaped(StringTable,
Daniel Dunbar8680ce62010-11-27 13:46:11 +0000201 /*UseHexEscapes=*/true) << "')\n";
202
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000203 // Dump the symbol table.
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000204 outs() << " ('_symbols', [\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000205 unsigned SymNum = 0;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000206 for (const SymbolRef &Symbol : Obj.symbols()) {
207 DataRefImpl DRI = Symbol.getRawDataRefImpl();
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000208 if (Obj.is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000209 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(DRI);
210 DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
211 STE.n_sect, STE.n_desc, STE.n_value,
Rafael Espindola6e040c02013-04-26 20:07:33 +0000212 StringTable);
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000213 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000214 MachO::nlist STE = Obj.getSymbolTableEntry(DRI);
215 DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
216 STE.n_sect, STE.n_desc, STE.n_value,
Rafael Espindola6e040c02013-04-26 20:07:33 +0000217 StringTable);
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000218 }
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000219 SymNum++;
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000220 }
221 outs() << " ])\n";
222
Rafael Espindola6e040c02013-04-26 20:07:33 +0000223 return 0;
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000224}
225
Rafael Espindola6e040c02013-04-26 20:07:33 +0000226static int DumpDysymtabCommand(const MachOObjectFile &Obj) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000227 MachO::dysymtab_command DLC = Obj.getDysymtabLoadCommand();
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000228
Charles Davis8bdfafd2013-09-01 04:28:48 +0000229 outs() << " ('ilocalsym', " << DLC.ilocalsym << ")\n";
230 outs() << " ('nlocalsym', " << DLC.nlocalsym << ")\n";
231 outs() << " ('iextdefsym', " << DLC.iextdefsym << ")\n";
232 outs() << " ('nextdefsym', " << DLC.nextdefsym << ")\n";
233 outs() << " ('iundefsym', " << DLC.iundefsym << ")\n";
234 outs() << " ('nundefsym', " << DLC.nundefsym << ")\n";
235 outs() << " ('tocoff', " << DLC.tocoff << ")\n";
236 outs() << " ('ntoc', " << DLC.ntoc << ")\n";
237 outs() << " ('modtaboff', " << DLC.modtaboff << ")\n";
238 outs() << " ('nmodtab', " << DLC.nmodtab << ")\n";
239 outs() << " ('extrefsymoff', " << DLC.extrefsymoff << ")\n";
240 outs() << " ('nextrefsyms', " << DLC.nextrefsyms << ")\n";
241 outs() << " ('indirectsymoff', " << DLC.indirectsymoff << ")\n";
242 outs() << " ('nindirectsyms', " << DLC.nindirectsyms << ")\n";
243 outs() << " ('extreloff', " << DLC.extreloff << ")\n";
244 outs() << " ('nextrel', " << DLC.nextrel << ")\n";
245 outs() << " ('locreloff', " << DLC.locreloff << ")\n";
246 outs() << " ('nlocrel', " << DLC.nlocrel << ")\n";
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000247
Daniel Dunbarc983afc2010-11-27 13:26:12 +0000248 // Dump the indirect symbol table.
Daniel Dunbarc983afc2010-11-27 13:26:12 +0000249 outs() << " ('_indirect_symbols', [\n";
Charles Davis8bdfafd2013-09-01 04:28:48 +0000250 for (unsigned i = 0; i != DLC.nindirectsyms; ++i) {
251 uint32_t ISTE = Obj.getIndirectSymbolTableEntry(DLC, i);
Daniel Dunbarc983afc2010-11-27 13:26:12 +0000252 outs() << " # Indirect Symbol " << i << "\n";
Charles Davis8bdfafd2013-09-01 04:28:48 +0000253 outs() << " (('symbol_index', " << format("0x%x", ISTE) << "),),\n";
Daniel Dunbarc983afc2010-11-27 13:26:12 +0000254 }
255 outs() << " ])\n";
256
Rafael Espindola6e040c02013-04-26 20:07:33 +0000257 return 0;
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000258}
259
Rafael Espindola6e040c02013-04-26 20:07:33 +0000260static int
261DumpLinkeditDataCommand(const MachOObjectFile &Obj,
262 const MachOObjectFile::LoadCommandInfo &LCI) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000263 MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI);
264 outs() << " ('dataoff', " << LLC.dataoff << ")\n"
265 << " ('datasize', " << LLC.datasize << ")\n"
Benjamin Kramer58298f02011-08-30 22:10:58 +0000266 << " ('_addresses', [\n";
267
268 SmallVector<uint64_t, 8> Addresses;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000269 Obj.ReadULEB128s(LLC.dataoff, Addresses);
Benjamin Kramer58298f02011-08-30 22:10:58 +0000270 for (unsigned i = 0, e = Addresses.size(); i != e; ++i)
271 outs() << " # Address " << i << '\n'
272 << " ('address', " << format("0x%x", Addresses[i]) << "),\n";
273
274 outs() << " ])\n";
Benjamin Kramer267a6d92011-08-30 18:33:37 +0000275
276 return 0;
277}
278
Rafael Espindola6e040c02013-04-26 20:07:33 +0000279static int
280DumpDataInCodeDataCommand(const MachOObjectFile &Obj,
281 const MachOObjectFile::LoadCommandInfo &LCI) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000282 MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI);
283 outs() << " ('dataoff', " << LLC.dataoff << ")\n"
284 << " ('datasize', " << LLC.datasize << ")\n"
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000285 << " ('_data_regions', [\n";
286
Charles Davis8bdfafd2013-09-01 04:28:48 +0000287 unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000288 for (unsigned i = 0; i < NumRegions; ++i) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000289 MachO::data_in_code_entry DICE= Obj.getDataInCodeTableEntry(LLC.dataoff, i);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000290 outs() << " # DICE " << i << "\n"
Charles Davis8bdfafd2013-09-01 04:28:48 +0000291 << " ('offset', " << DICE.offset << ")\n"
292 << " ('length', " << DICE.length << ")\n"
293 << " ('kind', " << DICE.kind << ")\n";
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000294 }
295
296 outs() <<" ])\n";
297
298 return 0;
299}
300
Rafael Espindola6e040c02013-04-26 20:07:33 +0000301static int
302DumpLinkerOptionsCommand(const MachOObjectFile &Obj,
303 const MachOObjectFile::LoadCommandInfo &LCI) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000304 MachO::linker_options_command LOLC = Obj.getLinkerOptionsLoadCommand(LCI);
305 outs() << " ('count', " << LOLC.count << ")\n"
306 << " ('_strings', [\n";
Daniel Dunbareec0f322013-01-18 01:26:07 +0000307
Charles Davis8bdfafd2013-09-01 04:28:48 +0000308 uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_options_command);
309 const char *P = LCI.Ptr + sizeof(MachO::linker_options_command);
310 StringRef Data(P, DataSize);
311 for (unsigned i = 0; i != LOLC.count; ++i) {
312 std::pair<StringRef,StringRef> Split = Data.split('\0');
313 outs() << "\t\"";
314 outs().write_escaped(Split.first);
315 outs() << "\",\n";
316 Data = Split.second;
317 }
318 outs() <<" ])\n";
Daniel Dunbareec0f322013-01-18 01:26:07 +0000319
320 return 0;
321}
322
Jim Grosbach448334a2014-03-18 22:09:05 +0000323static int
324DumpVersionMin(const MachOObjectFile &Obj,
325 const MachOObjectFile::LoadCommandInfo &LCI) {
326 MachO::version_min_command VMLC = Obj.getVersionMinLoadCommand(LCI);
327 outs() << " ('version, " << VMLC.version << ")\n"
328 << " ('reserved, " << VMLC.reserved << ")\n";
329 return 0;
330}
331
Rafael Espindola6e040c02013-04-26 20:07:33 +0000332static int DumpLoadCommand(const MachOObjectFile &Obj,
333 MachOObjectFile::LoadCommandInfo &LCI) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000334 switch (LCI.C.cmd) {
335 case MachO::LC_SEGMENT:
David Blaikieed80aa52013-08-27 05:16:07 +0000336 return DumpSegmentCommand(Obj, LCI);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000337 case MachO::LC_SEGMENT_64:
David Blaikieed80aa52013-08-27 05:16:07 +0000338 return DumpSegment64Command(Obj, LCI);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000339 case MachO::LC_SYMTAB:
David Blaikieed80aa52013-08-27 05:16:07 +0000340 return DumpSymtabCommand(Obj);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000341 case MachO::LC_DYSYMTAB:
David Blaikieed80aa52013-08-27 05:16:07 +0000342 return DumpDysymtabCommand(Obj);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000343 case MachO::LC_CODE_SIGNATURE:
344 case MachO::LC_SEGMENT_SPLIT_INFO:
345 case MachO::LC_FUNCTION_STARTS:
David Blaikieed80aa52013-08-27 05:16:07 +0000346 return DumpLinkeditDataCommand(Obj, LCI);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000347 case MachO::LC_DATA_IN_CODE:
David Blaikieed80aa52013-08-27 05:16:07 +0000348 return DumpDataInCodeDataCommand(Obj, LCI);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000349 case MachO::LC_LINKER_OPTIONS:
David Blaikieed80aa52013-08-27 05:16:07 +0000350 return DumpLinkerOptionsCommand(Obj, LCI);
Jim Grosbach448334a2014-03-18 22:09:05 +0000351 case MachO::LC_VERSION_MIN_IPHONEOS:
352 case MachO::LC_VERSION_MIN_MACOSX:
353 return DumpVersionMin(Obj, LCI);
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000354 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000355 Warning("unknown load command: " + Twine(LCI.C.cmd));
David Blaikieed80aa52013-08-27 05:16:07 +0000356 return 0;
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000357 }
Rafael Espindola6e040c02013-04-26 20:07:33 +0000358}
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000359
Rafael Espindola6e040c02013-04-26 20:07:33 +0000360
361static int DumpLoadCommand(const MachOObjectFile &Obj, unsigned Index,
362 MachOObjectFile::LoadCommandInfo &LCI) {
363 outs() << " # Load Command " << Index << "\n"
Charles Davis8bdfafd2013-09-01 04:28:48 +0000364 << " (('command', " << LCI.C.cmd << ")\n"
365 << " ('size', " << LCI.C.cmdsize << ")\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000366 int Res = DumpLoadCommand(Obj, LCI);
367 outs() << " ),\n";
Daniel Dunbara8070e02010-11-27 08:22:29 +0000368 return Res;
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000369}
370
Rafael Espindola6e040c02013-04-26 20:07:33 +0000371static void printHeader(const MachOObjectFile *Obj,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000372 const MachO::mach_header &Header) {
373 outs() << "('cputype', " << Header.cputype << ")\n";
374 outs() << "('cpusubtype', " << Header.cpusubtype << ")\n";
375 outs() << "('filetype', " << Header.filetype << ")\n";
376 outs() << "('num_load_commands', " << Header.ncmds << ")\n";
377 outs() << "('load_commands_size', " << Header.sizeofcmds << ")\n";
378 outs() << "('flag', " << Header.flags << ")\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000379
380 // Print extended header if 64-bit.
381 if (Obj->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000382 const MachO::mach_header_64 *Header64 =
383 reinterpret_cast<const MachO::mach_header_64 *>(&Header);
384 outs() << "('reserved', " << Header64->reserved << ")\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000385 }
386}
387
Daniel Dunbarfdfb6352010-11-27 05:58:44 +0000388int main(int argc, char **argv) {
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000389 ProgramName = argv[0];
Daniel Dunbarfdfb6352010-11-27 05:58:44 +0000390 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
391
392 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
393
Rafael Espindola63da2952014-01-15 19:37:43 +0000394 ErrorOr<Binary *> BinaryOrErr = createBinary(InputFile);
395 if (error_code EC = BinaryOrErr.getError())
Rafael Espindola6e040c02013-04-26 20:07:33 +0000396 return Error("unable to read input: '" + EC.message() + "'");
Ahmed Charles56440fd2014-03-06 05:51:42 +0000397 std::unique_ptr<Binary> Binary(BinaryOrErr.get());
Daniel Dunbar9630fb32010-11-27 06:19:17 +0000398
Rafael Espindola6e040c02013-04-26 20:07:33 +0000399 const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(Binary.get());
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000400 if (!InputObject)
Rafael Espindola6e040c02013-04-26 20:07:33 +0000401 return Error("Not a MachO object");
Daniel Dunbar9630fb32010-11-27 06:19:17 +0000402
Eric Christopher19ea5ae2011-04-03 23:51:47 +0000403 // Print the header
Charles Davis8bdfafd2013-09-01 04:28:48 +0000404 MachO::mach_header_64 Header64;
405 MachO::mach_header *Header = reinterpret_cast<MachO::mach_header*>(&Header64);
406 if (InputObject->is64Bit())
407 Header64 = InputObject->getHeader64();
408 else
409 *Header = InputObject->getHeader();
410 printHeader(InputObject, *Header);
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000411
412 // Print the load commands.
Daniel Dunbara8070e02010-11-27 08:22:29 +0000413 int Res = 0;
Rafael Espindola6e040c02013-04-26 20:07:33 +0000414 MachOObjectFile::LoadCommandInfo Command =
415 InputObject->getFirstLoadCommandInfo();
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000416 outs() << "('load_commands', [\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000417 for (unsigned i = 0; ; ++i) {
418 if (DumpLoadCommand(*InputObject, i, Command))
Daniel Dunbara8070e02010-11-27 08:22:29 +0000419 break;
Rafael Espindola6e040c02013-04-26 20:07:33 +0000420
Charles Davis8bdfafd2013-09-01 04:28:48 +0000421 if (i == Header->ncmds - 1)
Rafael Espindola6e040c02013-04-26 20:07:33 +0000422 break;
423 Command = InputObject->getNextLoadCommandInfo(Command);
424 }
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000425 outs() << "])\n";
426
Daniel Dunbara8070e02010-11-27 08:22:29 +0000427 return Res;
Daniel Dunbarfdfb6352010-11-27 05:58:44 +0000428}