dbb2700b77e5e79f8c0e5cbde8f84c4be2832cd0
[oota-llvm.git] / tools / macho-dump / macho-dump.cpp
1 //===-- 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
14 #include "llvm/Object/MachO.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/Casting.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/Format.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Support/system_error.h"
24 using namespace llvm;
25 using namespace llvm::object;
26
27 static cl::opt<std::string>
28 InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
29
30 static cl::opt<bool>
31 ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
32                 cl::init(false));
33
34 ///
35
36 static const char *ProgramName;
37
38 static void Message(const char *Type, const Twine &Msg) {
39   errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
40 }
41
42 static int Error(const Twine &Msg) {
43   Message("error", Msg);
44   return 1;
45 }
46
47 static void Warning(const Twine &Msg) {
48   Message("warning", Msg);
49 }
50
51 ///
52
53 static 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
70 static int DumpSectionData(const MachOObjectFile &Obj, unsigned Index,
71                            StringRef Name,
72                            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) {
78   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";
89   outs() << "    ('flags', " << format("0x%x", Flags) << ")\n";
90   outs() << "    ('reserved1', " << Reserved1 << ")\n";
91   outs() << "    ('reserved2', " << Reserved2 << ")\n";
92   if (Reserved3 != ~0ULL)
93     outs() << "    ('reserved3', " << Reserved3 << ")\n";
94   outs() << "   ),\n";
95
96   // Dump the relocation entries.
97   outs() << "  ('_relocations', [\n";
98   unsigned RelNum = 0;
99   for (relocation_iterator I = Obj.section_rel_begin(Index),
100                            E = Obj.section_rel_end(Index);
101        I != E; ++I, ++RelNum) {
102     MachO::any_relocation_info RE = Obj.getRelocation(I->getRawDataRefImpl());
103     outs() << "    # Relocation " << RelNum << "\n";
104     outs() << "    (('word-0', " << format("0x%x", RE.r_word0) << "),\n";
105     outs() << "     ('word-1', " << format("0x%x", RE.r_word1) << ")),\n";
106   }
107   outs() << "  ])\n";
108
109   // Dump the section data, if requested.
110   if (ShowSectionData) {
111     outs() << "  ('_section_data', '";
112     StringRef Data = Obj.getData().substr(Offset, Size);
113     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
122   return 0;
123 }
124
125 static int DumpSegmentCommand(const MachOObjectFile &Obj,
126                               const MachOObjectFile::LoadCommandInfo &LCI) {
127   MachO::segment_command SLC = Obj.getSegmentLoadCommand(LCI);
128
129   DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr,
130                          SLC.vmsize, SLC.fileoff, SLC.filesize,
131                          SLC.maxprot, SLC.initprot, SLC.nsects, SLC.flags);
132
133   // Dump the sections.
134   outs() << "  ('sections', [\n";
135   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);
142   }
143   outs() << "  ])\n";
144
145   return 0;
146 }
147
148 static int DumpSegment64Command(const MachOObjectFile &Obj,
149                                 const MachOObjectFile::LoadCommandInfo &LCI) {
150   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);
154
155   // Dump the sections.
156   outs() << "  ('sections', [\n";
157   for (unsigned i = 0; i != SLC.nsects; ++i) {
158     MachO::section_64 Sect = Obj.getSection64(LCI, i);
159
160     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);
166   }
167   outs() << "  ])\n";
168
169   return 0;
170 }
171
172 static void DumpSymbolTableEntryData(const MachOObjectFile &Obj,
173                                      unsigned Index, uint32_t StringIndex,
174                                      uint8_t Type, uint8_t SectionIndex,
175                                      uint16_t Flags, uint64_t Value,
176                                      StringRef StringTable) {
177   const char *Name = &StringTable.data()[StringIndex];
178   outs() << "    # Symbol " << Index << "\n";
179   outs() << "   (('n_strx', " << StringIndex << ")\n";
180   outs() << "    ('n_type', " << format("0x%x", Type) << ")\n";
181   outs() << "    ('n_sect', " << uint32_t(SectionIndex) << ")\n";
182   outs() << "    ('n_desc', " << Flags << ")\n";
183   outs() << "    ('n_value', " << Value << ")\n";
184   outs() << "    ('_string', '" << Name << "')\n";
185   outs() << "   ),\n";
186 }
187
188 static int DumpSymtabCommand(const MachOObjectFile &Obj) {
189   MachO::symtab_command SLC = Obj.getSymtabLoadCommand();
190
191   outs() << "  ('symoff', " << SLC.symoff << ")\n";
192   outs() << "  ('nsyms', " << SLC.nsyms << ")\n";
193   outs() << "  ('stroff', " << SLC.stroff << ")\n";
194   outs() << "  ('strsize', " << SLC.strsize << ")\n";
195
196   // Dump the string data.
197   outs() << "  ('_string_data', '";
198   StringRef StringTable = Obj.getStringTableData();
199   outs().write_escaped(StringTable,
200                        /*UseHexEscapes=*/true) << "')\n";
201
202   // Dump the symbol table.
203   outs() << "  ('_symbols', [\n";
204   unsigned SymNum = 0;
205   for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
206        ++I, ++SymNum) {
207     DataRefImpl DRI = I->getRawDataRefImpl();
208     if (Obj.is64Bit()) {
209       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,
212                                StringTable);
213     } else {
214       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,
217                                StringTable);
218     }
219   }
220   outs() << "  ])\n";
221
222   return 0;
223 }
224
225 static int DumpDysymtabCommand(const MachOObjectFile &Obj) {
226   MachO::dysymtab_command DLC = Obj.getDysymtabLoadCommand();
227
228   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";
246
247   // Dump the indirect symbol table.
248   outs() << "  ('_indirect_symbols', [\n";
249   for (unsigned i = 0; i != DLC.nindirectsyms; ++i) {
250     uint32_t ISTE = Obj.getIndirectSymbolTableEntry(DLC, i);
251     outs() << "    # Indirect Symbol " << i << "\n";
252     outs() << "    (('symbol_index', " << format("0x%x", ISTE) << "),),\n";
253   }
254   outs() << "  ])\n";
255
256   return 0;
257 }
258
259 static int
260 DumpLinkeditDataCommand(const MachOObjectFile &Obj,
261                         const MachOObjectFile::LoadCommandInfo &LCI) {
262   MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI);
263   outs() << "  ('dataoff', " << LLC.dataoff << ")\n"
264          << "  ('datasize', " << LLC.datasize << ")\n"
265          << "  ('_addresses', [\n";
266
267   SmallVector<uint64_t, 8> Addresses;
268   Obj.ReadULEB128s(LLC.dataoff, Addresses);
269   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";
274
275   return 0;
276 }
277
278 static int
279 DumpDataInCodeDataCommand(const MachOObjectFile &Obj,
280                           const MachOObjectFile::LoadCommandInfo &LCI) {
281   MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI);
282   outs() << "  ('dataoff', " << LLC.dataoff << ")\n"
283          << "  ('datasize', " << LLC.datasize << ")\n"
284          << "  ('_data_regions', [\n";
285
286   unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry);
287   for (unsigned i = 0; i < NumRegions; ++i) {
288     MachO::data_in_code_entry DICE= Obj.getDataInCodeTableEntry(LLC.dataoff, i);
289     outs() << "    # DICE " << i << "\n"
290            << "    ('offset', " << DICE.offset << ")\n"
291            << "    ('length', " << DICE.length << ")\n"
292            << "    ('kind', " << DICE.kind << ")\n";
293   }
294
295   outs() <<"  ])\n";
296
297   return 0;
298 }
299
300 static int
301 DumpLinkerOptionsCommand(const MachOObjectFile &Obj,
302                          const MachOObjectFile::LoadCommandInfo &LCI) {
303   MachO::linker_options_command LOLC = Obj.getLinkerOptionsLoadCommand(LCI);
304   outs() << "  ('count', " << LOLC.count << ")\n"
305          << "  ('_strings', [\n";
306
307   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";
318
319   return 0;
320 }
321
322 static int DumpLoadCommand(const MachOObjectFile &Obj,
323                            MachOObjectFile::LoadCommandInfo &LCI) {
324   switch (LCI.C.cmd) {
325   case MachO::LC_SEGMENT:
326     return DumpSegmentCommand(Obj, LCI);
327   case MachO::LC_SEGMENT_64:
328     return DumpSegment64Command(Obj, LCI);
329   case MachO::LC_SYMTAB:
330     return DumpSymtabCommand(Obj);
331   case MachO::LC_DYSYMTAB:
332     return DumpDysymtabCommand(Obj);
333   case MachO::LC_CODE_SIGNATURE:
334   case MachO::LC_SEGMENT_SPLIT_INFO:
335   case MachO::LC_FUNCTION_STARTS:
336     return DumpLinkeditDataCommand(Obj, LCI);
337   case MachO::LC_DATA_IN_CODE:
338     return DumpDataInCodeDataCommand(Obj, LCI);
339   case MachO::LC_LINKER_OPTIONS:
340     return DumpLinkerOptionsCommand(Obj, LCI);
341   default:
342     Warning("unknown load command: " + Twine(LCI.C.cmd));
343     return 0;
344   }
345 }
346
347
348 static int DumpLoadCommand(const MachOObjectFile &Obj, unsigned Index,
349                            MachOObjectFile::LoadCommandInfo &LCI) {
350   outs() << "  # Load Command " << Index << "\n"
351          << " (('command', " << LCI.C.cmd << ")\n"
352          << "  ('size', " << LCI.C.cmdsize << ")\n";
353   int Res = DumpLoadCommand(Obj, LCI);
354   outs() << " ),\n";
355   return Res;
356 }
357
358 static void printHeader(const MachOObjectFile *Obj,
359                         const MachO::mach_header &Header) {
360   outs() << "('cputype', " << Header.cputype << ")\n";
361   outs() << "('cpusubtype', " << Header.cpusubtype << ")\n";
362   outs() << "('filetype', " << Header.filetype << ")\n";
363   outs() << "('num_load_commands', " << Header.ncmds << ")\n";
364   outs() << "('load_commands_size', " << Header.sizeofcmds << ")\n";
365   outs() << "('flag', " << Header.flags << ")\n";
366
367   // Print extended header if 64-bit.
368   if (Obj->is64Bit()) {
369     const MachO::mach_header_64 *Header64 =
370       reinterpret_cast<const MachO::mach_header_64 *>(&Header);
371     outs() << "('reserved', " << Header64->reserved << ")\n";
372   }
373 }
374
375 int main(int argc, char **argv) {
376   ProgramName = argv[0];
377   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
378
379   cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
380
381   ErrorOr<Binary *> BinaryOrErr = createBinary(InputFile);
382   if (error_code EC = BinaryOrErr.getError())
383     return Error("unable to read input: '" + EC.message() + "'");
384   OwningPtr<Binary> Binary(BinaryOrErr.get());
385
386   const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(Binary.get());
387   if (!InputObject)
388     return Error("Not a MachO object");
389
390   // Print the header
391   MachO::mach_header_64 Header64;
392   MachO::mach_header *Header = reinterpret_cast<MachO::mach_header*>(&Header64);
393   if (InputObject->is64Bit())
394     Header64 = InputObject->getHeader64();
395   else
396     *Header = InputObject->getHeader();
397   printHeader(InputObject, *Header);
398
399   // Print the load commands.
400   int Res = 0;
401   MachOObjectFile::LoadCommandInfo Command =
402     InputObject->getFirstLoadCommandInfo();
403   outs() << "('load_commands', [\n";
404   for (unsigned i = 0; ; ++i) {
405     if (DumpLoadCommand(*InputObject, i, Command))
406       break;
407
408     if (i == Header->ncmds - 1)
409       break;
410     Command = InputObject->getNextLoadCommandInfo(Command);
411   }
412   outs() << "])\n";
413
414   return Res;
415 }