blob: aac720ddd59c7cbb14a2e7a34d588e9e38f23d6a [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;
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;
Rui Ueyamabc654b12013-09-27 21:47:05 +000099 for (relocation_iterator I = Obj.section_rel_begin(Index),
Rafael Espindola5e812af2014-01-30 02:49:50 +0000100 E = Obj.section_rel_end(Index);
101 I != E; ++I, ++RelNum) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000102 MachO::any_relocation_info RE = Obj.getRelocation(I->getRawDataRefImpl());
Rafael Espindola6e040c02013-04-26 20:07:33 +0000103 outs() << " # Relocation " << RelNum << "\n";
Charles Davis8bdfafd2013-09-01 04:28:48 +0000104 outs() << " (('word-0', " << format("0x%x", RE.r_word0) << "),\n";
105 outs() << " ('word-1', " << format("0x%x", RE.r_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) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000127 MachO::segment_command SLC = Obj.getSegmentLoadCommand(LCI);
Daniel Dunbara8070e02010-11-27 08:22:29 +0000128
Charles Davis8bdfafd2013-09-01 04:28:48 +0000129 DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr,
130 SLC.vmsize, SLC.fileoff, SLC.filesize,
131 SLC.maxprot, SLC.initprot, SLC.nsects, SLC.flags);
Daniel Dunbara8070e02010-11-27 08:22:29 +0000132
Daniel Dunbar58676902010-11-27 13:33:15 +0000133 // Dump the sections.
Daniel Dunbar58676902010-11-27 13:33:15 +0000134 outs() << " ('sections', [\n";
Charles Davis8bdfafd2013-09-01 04:28:48 +0000135 for (unsigned i = 0; i != SLC.nsects; ++i) {
136 MachO::section Sect = Obj.getSection(LCI, i);
137 DumpSectionData(Obj, i, StringRef(Sect.sectname, 16),
138 StringRef(Sect.segname, 16), Sect.addr,
139 Sect.size, Sect.offset, Sect.align,
140 Sect.reloff, Sect.nreloc, Sect.flags,
141 Sect.reserved1, Sect.reserved2);
Daniel Dunbar58676902010-11-27 13:33:15 +0000142 }
143 outs() << " ])\n";
144
Rafael Espindola6e040c02013-04-26 20:07:33 +0000145 return 0;
Daniel Dunbara8070e02010-11-27 08:22:29 +0000146}
Daniel Dunbar58676902010-11-27 13:33:15 +0000147
Rafael Espindola6e040c02013-04-26 20:07:33 +0000148static int DumpSegment64Command(const MachOObjectFile &Obj,
149 const MachOObjectFile::LoadCommandInfo &LCI) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000150 MachO::segment_command_64 SLC = Obj.getSegment64LoadCommand(LCI);
151 DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr,
152 SLC.vmsize, SLC.fileoff, SLC.filesize,
153 SLC.maxprot, SLC.initprot, SLC.nsects, SLC.flags);
Daniel Dunbara8070e02010-11-27 08:22:29 +0000154
Daniel Dunbar58676902010-11-27 13:33:15 +0000155 // Dump the sections.
Daniel Dunbar58676902010-11-27 13:33:15 +0000156 outs() << " ('sections', [\n";
Charles Davis8bdfafd2013-09-01 04:28:48 +0000157 for (unsigned i = 0; i != SLC.nsects; ++i) {
158 MachO::section_64 Sect = Obj.getSection64(LCI, i);
Daniel Dunbar58676902010-11-27 13:33:15 +0000159
Charles Davis8bdfafd2013-09-01 04:28:48 +0000160 DumpSectionData(Obj, i, StringRef(Sect.sectname, 16),
161 StringRef(Sect.segname, 16), Sect.addr,
162 Sect.size, Sect.offset, Sect.align,
163 Sect.reloff, Sect.nreloc, Sect.flags,
164 Sect.reserved1, Sect.reserved2,
165 Sect.reserved3);
Daniel Dunbar58676902010-11-27 13:33:15 +0000166 }
167 outs() << " ])\n";
168
Rafael Espindola6e040c02013-04-26 20:07:33 +0000169 return 0;
Daniel Dunbara8070e02010-11-27 08:22:29 +0000170}
171
Rafael Espindola6e040c02013-04-26 20:07:33 +0000172static void DumpSymbolTableEntryData(const MachOObjectFile &Obj,
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000173 unsigned Index, uint32_t StringIndex,
174 uint8_t Type, uint8_t SectionIndex,
Rafael Espindola6e040c02013-04-26 20:07:33 +0000175 uint16_t Flags, uint64_t Value,
176 StringRef StringTable) {
177 const char *Name = &StringTable.data()[StringIndex];
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000178 outs() << " # Symbol " << Index << "\n";
179 outs() << " (('n_strx', " << StringIndex << ")\n";
Daniel Dunbare308b8e2010-11-27 13:58:16 +0000180 outs() << " ('n_type', " << format("0x%x", Type) << ")\n";
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000181 outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n";
182 outs() << " ('n_desc', " << Flags << ")\n";
183 outs() << " ('n_value', " << Value << ")\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000184 outs() << " ('_string', '" << Name << "')\n";
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000185 outs() << " ),\n";
186}
187
Rafael Espindola6e040c02013-04-26 20:07:33 +0000188static int DumpSymtabCommand(const MachOObjectFile &Obj) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000189 MachO::symtab_command SLC = Obj.getSymtabLoadCommand();
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000190
Charles Davis8bdfafd2013-09-01 04:28:48 +0000191 outs() << " ('symoff', " << SLC.symoff << ")\n";
192 outs() << " ('nsyms', " << SLC.nsyms << ")\n";
193 outs() << " ('stroff', " << SLC.stroff << ")\n";
194 outs() << " ('strsize', " << SLC.strsize << ")\n";
Daniel Dunbar8680ce62010-11-27 13:46:11 +0000195
196 // Dump the string data.
197 outs() << " ('_string_data', '";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000198 StringRef StringTable = Obj.getStringTableData();
199 outs().write_escaped(StringTable,
Daniel Dunbar8680ce62010-11-27 13:46:11 +0000200 /*UseHexEscapes=*/true) << "')\n";
201
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000202 // Dump the symbol table.
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000203 outs() << " ('_symbols', [\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000204 unsigned SymNum = 0;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000205 for (const SymbolRef &Symbol : Obj.symbols()) {
206 DataRefImpl DRI = Symbol.getRawDataRefImpl();
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000207 if (Obj.is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000208 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(DRI);
209 DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
210 STE.n_sect, STE.n_desc, STE.n_value,
Rafael Espindola6e040c02013-04-26 20:07:33 +0000211 StringTable);
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000212 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000213 MachO::nlist STE = Obj.getSymbolTableEntry(DRI);
214 DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
215 STE.n_sect, STE.n_desc, STE.n_value,
Rafael Espindola6e040c02013-04-26 20:07:33 +0000216 StringTable);
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000217 }
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000218 SymNum++;
Daniel Dunbar83224fc2010-11-27 13:52:53 +0000219 }
220 outs() << " ])\n";
221
Rafael Espindola6e040c02013-04-26 20:07:33 +0000222 return 0;
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000223}
224
Rafael Espindola6e040c02013-04-26 20:07:33 +0000225static int DumpDysymtabCommand(const MachOObjectFile &Obj) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000226 MachO::dysymtab_command DLC = Obj.getDysymtabLoadCommand();
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000227
Charles Davis8bdfafd2013-09-01 04:28:48 +0000228 outs() << " ('ilocalsym', " << DLC.ilocalsym << ")\n";
229 outs() << " ('nlocalsym', " << DLC.nlocalsym << ")\n";
230 outs() << " ('iextdefsym', " << DLC.iextdefsym << ")\n";
231 outs() << " ('nextdefsym', " << DLC.nextdefsym << ")\n";
232 outs() << " ('iundefsym', " << DLC.iundefsym << ")\n";
233 outs() << " ('nundefsym', " << DLC.nundefsym << ")\n";
234 outs() << " ('tocoff', " << DLC.tocoff << ")\n";
235 outs() << " ('ntoc', " << DLC.ntoc << ")\n";
236 outs() << " ('modtaboff', " << DLC.modtaboff << ")\n";
237 outs() << " ('nmodtab', " << DLC.nmodtab << ")\n";
238 outs() << " ('extrefsymoff', " << DLC.extrefsymoff << ")\n";
239 outs() << " ('nextrefsyms', " << DLC.nextrefsyms << ")\n";
240 outs() << " ('indirectsymoff', " << DLC.indirectsymoff << ")\n";
241 outs() << " ('nindirectsyms', " << DLC.nindirectsyms << ")\n";
242 outs() << " ('extreloff', " << DLC.extreloff << ")\n";
243 outs() << " ('nextrel', " << DLC.nextrel << ")\n";
244 outs() << " ('locreloff', " << DLC.locreloff << ")\n";
245 outs() << " ('nlocrel', " << DLC.nlocrel << ")\n";
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000246
Daniel Dunbarc983afc2010-11-27 13:26:12 +0000247 // Dump the indirect symbol table.
Daniel Dunbarc983afc2010-11-27 13:26:12 +0000248 outs() << " ('_indirect_symbols', [\n";
Charles Davis8bdfafd2013-09-01 04:28:48 +0000249 for (unsigned i = 0; i != DLC.nindirectsyms; ++i) {
250 uint32_t ISTE = Obj.getIndirectSymbolTableEntry(DLC, i);
Daniel Dunbarc983afc2010-11-27 13:26:12 +0000251 outs() << " # Indirect Symbol " << i << "\n";
Charles Davis8bdfafd2013-09-01 04:28:48 +0000252 outs() << " (('symbol_index', " << format("0x%x", ISTE) << "),),\n";
Daniel Dunbarc983afc2010-11-27 13:26:12 +0000253 }
254 outs() << " ])\n";
255
Rafael Espindola6e040c02013-04-26 20:07:33 +0000256 return 0;
Daniel Dunbar33dab2a2010-11-27 08:33:44 +0000257}
258
Rafael Espindola6e040c02013-04-26 20:07:33 +0000259static int
260DumpLinkeditDataCommand(const MachOObjectFile &Obj,
261 const MachOObjectFile::LoadCommandInfo &LCI) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000262 MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI);
263 outs() << " ('dataoff', " << LLC.dataoff << ")\n"
264 << " ('datasize', " << LLC.datasize << ")\n"
Benjamin Kramer58298f02011-08-30 22:10:58 +0000265 << " ('_addresses', [\n";
266
267 SmallVector<uint64_t, 8> Addresses;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000268 Obj.ReadULEB128s(LLC.dataoff, Addresses);
Benjamin Kramer58298f02011-08-30 22:10:58 +0000269 for (unsigned i = 0, e = Addresses.size(); i != e; ++i)
270 outs() << " # Address " << i << '\n'
271 << " ('address', " << format("0x%x", Addresses[i]) << "),\n";
272
273 outs() << " ])\n";
Benjamin Kramer267a6d92011-08-30 18:33:37 +0000274
275 return 0;
276}
277
Rafael Espindola6e040c02013-04-26 20:07:33 +0000278static int
279DumpDataInCodeDataCommand(const MachOObjectFile &Obj,
280 const MachOObjectFile::LoadCommandInfo &LCI) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000281 MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI);
282 outs() << " ('dataoff', " << LLC.dataoff << ")\n"
283 << " ('datasize', " << LLC.datasize << ")\n"
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000284 << " ('_data_regions', [\n";
285
Charles Davis8bdfafd2013-09-01 04:28:48 +0000286 unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000287 for (unsigned i = 0; i < NumRegions; ++i) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000288 MachO::data_in_code_entry DICE= Obj.getDataInCodeTableEntry(LLC.dataoff, i);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000289 outs() << " # DICE " << i << "\n"
Charles Davis8bdfafd2013-09-01 04:28:48 +0000290 << " ('offset', " << DICE.offset << ")\n"
291 << " ('length', " << DICE.length << ")\n"
292 << " ('kind', " << DICE.kind << ")\n";
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000293 }
294
295 outs() <<" ])\n";
296
297 return 0;
298}
299
Rafael Espindola6e040c02013-04-26 20:07:33 +0000300static int
301DumpLinkerOptionsCommand(const MachOObjectFile &Obj,
302 const MachOObjectFile::LoadCommandInfo &LCI) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000303 MachO::linker_options_command LOLC = Obj.getLinkerOptionsLoadCommand(LCI);
304 outs() << " ('count', " << LOLC.count << ")\n"
305 << " ('_strings', [\n";
Daniel Dunbareec0f322013-01-18 01:26:07 +0000306
Charles Davis8bdfafd2013-09-01 04:28:48 +0000307 uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_options_command);
308 const char *P = LCI.Ptr + sizeof(MachO::linker_options_command);
309 StringRef Data(P, DataSize);
310 for (unsigned i = 0; i != LOLC.count; ++i) {
311 std::pair<StringRef,StringRef> Split = Data.split('\0');
312 outs() << "\t\"";
313 outs().write_escaped(Split.first);
314 outs() << "\",\n";
315 Data = Split.second;
316 }
317 outs() <<" ])\n";
Daniel Dunbareec0f322013-01-18 01:26:07 +0000318
319 return 0;
320}
321
Jim Grosbach448334a2014-03-18 22:09:05 +0000322static int
323DumpVersionMin(const MachOObjectFile &Obj,
324 const MachOObjectFile::LoadCommandInfo &LCI) {
325 MachO::version_min_command VMLC = Obj.getVersionMinLoadCommand(LCI);
326 outs() << " ('version, " << VMLC.version << ")\n"
Kevin Enderby8ae63c12014-09-04 16:54:47 +0000327 << " ('sdk, " << VMLC.sdk << ")\n";
Jim Grosbach448334a2014-03-18 22:09:05 +0000328 return 0;
329}
330
Tim Northover8f9590b2014-06-30 14:40:57 +0000331static int
332DumpDylibID(const MachOObjectFile &Obj,
333 const MachOObjectFile::LoadCommandInfo &LCI) {
334 MachO::dylib_command DLLC = Obj.getDylibIDLoadCommand(LCI);
335 outs() << " ('install_name', '" << LCI.Ptr + DLLC.dylib.name << "')\n"
336 << " ('timestamp, " << DLLC.dylib.timestamp << ")\n"
337 << " ('cur_version, " << DLLC.dylib.current_version << ")\n"
338 << " ('compat_version, " << DLLC.dylib.compatibility_version << ")\n";
339 return 0;
340}
341
Rafael Espindola6e040c02013-04-26 20:07:33 +0000342static int DumpLoadCommand(const MachOObjectFile &Obj,
343 MachOObjectFile::LoadCommandInfo &LCI) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000344 switch (LCI.C.cmd) {
345 case MachO::LC_SEGMENT:
David Blaikieed80aa52013-08-27 05:16:07 +0000346 return DumpSegmentCommand(Obj, LCI);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000347 case MachO::LC_SEGMENT_64:
David Blaikieed80aa52013-08-27 05:16:07 +0000348 return DumpSegment64Command(Obj, LCI);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000349 case MachO::LC_SYMTAB:
David Blaikieed80aa52013-08-27 05:16:07 +0000350 return DumpSymtabCommand(Obj);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000351 case MachO::LC_DYSYMTAB:
David Blaikieed80aa52013-08-27 05:16:07 +0000352 return DumpDysymtabCommand(Obj);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000353 case MachO::LC_CODE_SIGNATURE:
354 case MachO::LC_SEGMENT_SPLIT_INFO:
355 case MachO::LC_FUNCTION_STARTS:
David Blaikieed80aa52013-08-27 05:16:07 +0000356 return DumpLinkeditDataCommand(Obj, LCI);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000357 case MachO::LC_DATA_IN_CODE:
David Blaikieed80aa52013-08-27 05:16:07 +0000358 return DumpDataInCodeDataCommand(Obj, LCI);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000359 case MachO::LC_LINKER_OPTIONS:
David Blaikieed80aa52013-08-27 05:16:07 +0000360 return DumpLinkerOptionsCommand(Obj, LCI);
Jim Grosbach448334a2014-03-18 22:09:05 +0000361 case MachO::LC_VERSION_MIN_IPHONEOS:
362 case MachO::LC_VERSION_MIN_MACOSX:
363 return DumpVersionMin(Obj, LCI);
Tim Northover8f9590b2014-06-30 14:40:57 +0000364 case MachO::LC_ID_DYLIB:
365 return DumpDylibID(Obj, LCI);
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000366 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000367 Warning("unknown load command: " + Twine(LCI.C.cmd));
David Blaikieed80aa52013-08-27 05:16:07 +0000368 return 0;
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000369 }
Rafael Espindola6e040c02013-04-26 20:07:33 +0000370}
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000371
Rafael Espindola6e040c02013-04-26 20:07:33 +0000372
373static int DumpLoadCommand(const MachOObjectFile &Obj, unsigned Index,
374 MachOObjectFile::LoadCommandInfo &LCI) {
375 outs() << " # Load Command " << Index << "\n"
Charles Davis8bdfafd2013-09-01 04:28:48 +0000376 << " (('command', " << LCI.C.cmd << ")\n"
377 << " ('size', " << LCI.C.cmdsize << ")\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000378 int Res = DumpLoadCommand(Obj, LCI);
379 outs() << " ),\n";
Daniel Dunbara8070e02010-11-27 08:22:29 +0000380 return Res;
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000381}
382
Rafael Espindola6e040c02013-04-26 20:07:33 +0000383static void printHeader(const MachOObjectFile *Obj,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000384 const MachO::mach_header &Header) {
385 outs() << "('cputype', " << Header.cputype << ")\n";
386 outs() << "('cpusubtype', " << Header.cpusubtype << ")\n";
387 outs() << "('filetype', " << Header.filetype << ")\n";
388 outs() << "('num_load_commands', " << Header.ncmds << ")\n";
389 outs() << "('load_commands_size', " << Header.sizeofcmds << ")\n";
390 outs() << "('flag', " << Header.flags << ")\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000391
392 // Print extended header if 64-bit.
393 if (Obj->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000394 const MachO::mach_header_64 *Header64 =
395 reinterpret_cast<const MachO::mach_header_64 *>(&Header);
396 outs() << "('reserved', " << Header64->reserved << ")\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000397 }
398}
399
Daniel Dunbarfdfb6352010-11-27 05:58:44 +0000400int main(int argc, char **argv) {
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000401 ProgramName = argv[0];
Daniel Dunbarfdfb6352010-11-27 05:58:44 +0000402 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
403
404 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
405
Rafael Espindola48af1c22014-08-19 18:44:46 +0000406 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFile);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000407 if (std::error_code EC = BinaryOrErr.getError())
Rafael Espindola6e040c02013-04-26 20:07:33 +0000408 return Error("unable to read input: '" + EC.message() + "'");
Rafael Espindola48af1c22014-08-19 18:44:46 +0000409 Binary &Binary = *BinaryOrErr.get().getBinary();
Daniel Dunbar9630fb32010-11-27 06:19:17 +0000410
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000411 const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(&Binary);
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000412 if (!InputObject)
Rafael Espindola6e040c02013-04-26 20:07:33 +0000413 return Error("Not a MachO object");
Daniel Dunbar9630fb32010-11-27 06:19:17 +0000414
Eric Christopher19ea5ae2011-04-03 23:51:47 +0000415 // Print the header
Charles Davis8bdfafd2013-09-01 04:28:48 +0000416 MachO::mach_header_64 Header64;
417 MachO::mach_header *Header = reinterpret_cast<MachO::mach_header*>(&Header64);
418 if (InputObject->is64Bit())
419 Header64 = InputObject->getHeader64();
420 else
421 *Header = InputObject->getHeader();
422 printHeader(InputObject, *Header);
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000423
424 // Print the load commands.
Daniel Dunbara8070e02010-11-27 08:22:29 +0000425 int Res = 0;
Rafael Espindola6e040c02013-04-26 20:07:33 +0000426 MachOObjectFile::LoadCommandInfo Command =
427 InputObject->getFirstLoadCommandInfo();
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000428 outs() << "('load_commands', [\n";
Rafael Espindola6e040c02013-04-26 20:07:33 +0000429 for (unsigned i = 0; ; ++i) {
430 if (DumpLoadCommand(*InputObject, i, Command))
Daniel Dunbara8070e02010-11-27 08:22:29 +0000431 break;
Rafael Espindola6e040c02013-04-26 20:07:33 +0000432
Charles Davis8bdfafd2013-09-01 04:28:48 +0000433 if (i == Header->ncmds - 1)
Rafael Espindola6e040c02013-04-26 20:07:33 +0000434 break;
435 Command = InputObject->getNextLoadCommandInfo(Command);
436 }
Daniel Dunbar3977e7d2010-11-27 07:19:41 +0000437 outs() << "])\n";
438
Daniel Dunbara8070e02010-11-27 08:22:29 +0000439 return Res;
Daniel Dunbarfdfb6352010-11-27 05:58:44 +0000440}