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
Message(const char * Type,const Twine & Msg)38 static void Message(const char *Type, const Twine &Msg) {
39 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
40 }
41
Error(const Twine & Msg)42 static int Error(const Twine &Msg) {
43 Message("error", Msg);
44 return 1;
45 }
46
Warning(const Twine & Msg)47 static void Warning(const Twine &Msg) {
48 Message("warning", Msg);
49 }
50
51 ///
52
DumpSegmentCommandData(StringRef Name,uint64_t VMAddr,uint64_t VMSize,uint64_t FileOffset,uint64_t FileSize,uint32_t MaxProt,uint32_t InitProt,uint32_t NumSections,uint32_t Flags)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
DumpSectionData(const MachOObjectFile & Obj,unsigned Index,StringRef Name,StringRef SegmentName,uint64_t Address,uint64_t Size,uint32_t Offset,uint32_t Align,uint32_t RelocationTableOffset,uint32_t NumRelocationTableEntries,uint32_t Flags,uint32_t Reserved1,uint32_t Reserved2,uint64_t Reserved3=~0ULL)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 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";
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
DumpSegmentCommand(const MachOObjectFile & Obj,const MachOObjectFile::LoadCommandInfo & LCI)125 static int DumpSegmentCommand(const MachOObjectFile &Obj,
126 const MachOObjectFile::LoadCommandInfo &LCI) {
127 macho::SegmentLoadCommand SLC = Obj.getSegmentLoadCommand(LCI);
128
129 DumpSegmentCommandData(StringRef(SLC.Name, 16), SLC.VMAddress,
130 SLC.VMSize, SLC.FileOffset, SLC.FileSize,
131 SLC.MaxVMProtection, SLC.InitialVMProtection,
132 SLC.NumSections, SLC.Flags);
133
134 // Dump the sections.
135 outs() << " ('sections', [\n";
136 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);
144 }
145 outs() << " ])\n";
146
147 return 0;
148 }
149
DumpSegment64Command(const MachOObjectFile & Obj,const MachOObjectFile::LoadCommandInfo & LCI)150 static 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);
157
158 // Dump the sections.
159 outs() << " ('sections', [\n";
160 for (unsigned i = 0; i != SLC.NumSections; ++i) {
161 macho::Section64 Sect = Obj.getSection64(LCI, i);
162
163 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);
170 }
171 outs() << " ])\n";
172
173 return 0;
174 }
175
DumpSymbolTableEntryData(const MachOObjectFile & Obj,unsigned Index,uint32_t StringIndex,uint8_t Type,uint8_t SectionIndex,uint16_t Flags,uint64_t Value,StringRef StringTable)176 static void DumpSymbolTableEntryData(const MachOObjectFile &Obj,
177 unsigned Index, uint32_t StringIndex,
178 uint8_t Type, uint8_t SectionIndex,
179 uint16_t Flags, uint64_t Value,
180 StringRef StringTable) {
181 const char *Name = &StringTable.data()[StringIndex];
182 outs() << " # Symbol " << Index << "\n";
183 outs() << " (('n_strx', " << StringIndex << ")\n";
184 outs() << " ('n_type', " << format("0x%x", Type) << ")\n";
185 outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n";
186 outs() << " ('n_desc', " << Flags << ")\n";
187 outs() << " ('n_value', " << Value << ")\n";
188 outs() << " ('_string', '" << Name << "')\n";
189 outs() << " ),\n";
190 }
191
DumpSymtabCommand(const MachOObjectFile & Obj)192 static int DumpSymtabCommand(const MachOObjectFile &Obj) {
193 macho::SymtabLoadCommand SLC = Obj.getSymtabLoadCommand();
194
195 outs() << " ('symoff', " << SLC.SymbolTableOffset << ")\n";
196 outs() << " ('nsyms', " << SLC.NumSymbolTableEntries << ")\n";
197 outs() << " ('stroff', " << SLC.StringTableOffset << ")\n";
198 outs() << " ('strsize', " << SLC.StringTableSize << ")\n";
199
200 // Dump the string data.
201 outs() << " ('_string_data', '";
202 StringRef StringTable = Obj.getStringTableData();
203 outs().write_escaped(StringTable,
204 /*UseHexEscapes=*/true) << "')\n";
205
206 // Dump the symbol table.
207 outs() << " ('_symbols', [\n";
208 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();
213 if (Obj.is64Bit()) {
214 macho::Symbol64TableEntry STE = Obj.getSymbol64TableEntry(DRI);
215 DumpSymbolTableEntryData(Obj, SymNum, STE.StringIndex, STE.Type,
216 STE.SectionIndex, STE.Flags, STE.Value,
217 StringTable);
218 } else {
219 macho::SymbolTableEntry STE = Obj.getSymbolTableEntry(DRI);
220 DumpSymbolTableEntryData(Obj, SymNum, STE.StringIndex, STE.Type,
221 STE.SectionIndex, STE.Flags, STE.Value,
222 StringTable);
223 }
224 }
225 outs() << " ])\n";
226
227 return 0;
228 }
229
DumpDysymtabCommand(const MachOObjectFile & Obj)230 static int DumpDysymtabCommand(const MachOObjectFile &Obj) {
231 macho::DysymtabLoadCommand DLC = Obj.getDysymtabLoadCommand();
232
233 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";
244 outs() << " ('nextrefsyms', "
245 << DLC.NumReferencedSymbolTableEntries << ")\n";
246 outs() << " ('indirectsymoff', " << DLC.IndirectSymbolTableOffset << ")\n";
247 outs() << " ('nindirectsyms', "
248 << 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";
253
254 // Dump the indirect symbol table.
255 outs() << " ('_indirect_symbols', [\n";
256 for (unsigned i = 0; i != DLC.NumIndirectSymbolTableEntries; ++i) {
257 macho::IndirectSymbolTableEntry ISTE =
258 Obj.getIndirectSymbolTableEntry(DLC, i);
259 outs() << " # Indirect Symbol " << i << "\n";
260 outs() << " (('symbol_index', "
261 << format("0x%x", ISTE.Index) << "),),\n";
262 }
263 outs() << " ])\n";
264
265 return 0;
266 }
267
268 static int
DumpLinkeditDataCommand(const MachOObjectFile & Obj,const MachOObjectFile::LoadCommandInfo & LCI)269 DumpLinkeditDataCommand(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"
274 << " ('_addresses', [\n";
275
276 SmallVector<uint64_t, 8> Addresses;
277 Obj.ReadULEB128s(LLC.DataOffset, Addresses);
278 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";
283
284 return 0;
285 }
286
287 static int
DumpDataInCodeDataCommand(const MachOObjectFile & Obj,const MachOObjectFile::LoadCommandInfo & LCI)288 DumpDataInCodeDataCommand(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"
293 << " ('_data_regions', [\n";
294
295 unsigned NumRegions = LLC.DataSize / sizeof(macho::DataInCodeTableEntry);
296 for (unsigned i = 0; i < NumRegions; ++i) {
297 macho::DataInCodeTableEntry DICE =
298 Obj.getDataInCodeTableEntry(LLC.DataOffset, i);
299 outs() << " # DICE " << i << "\n"
300 << " ('offset', " << DICE.Offset << ")\n"
301 << " ('length', " << DICE.Length << ")\n"
302 << " ('kind', " << DICE.Kind << ")\n";
303 }
304
305 outs() <<" ])\n";
306
307 return 0;
308 }
309
310 static int
DumpLinkerOptionsCommand(const MachOObjectFile & Obj,const MachOObjectFile::LoadCommandInfo & LCI)311 DumpLinkerOptionsCommand(const MachOObjectFile &Obj,
312 const MachOObjectFile::LoadCommandInfo &LCI) {
313 macho::LinkerOptionsLoadCommand LOLC = Obj.getLinkerOptionsLoadCommand(LCI);
314 outs() << " ('count', " << LOLC.Count << ")\n"
315 << " ('_strings', [\n";
316
317 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";
328
329 return 0;
330 }
331
DumpLoadCommand(const MachOObjectFile & Obj,MachOObjectFile::LoadCommandInfo & LCI)332 static int DumpLoadCommand(const MachOObjectFile &Obj,
333 MachOObjectFile::LoadCommandInfo &LCI) {
334 switch (LCI.C.Type) {
335 case macho::LCT_Segment:
336 return DumpSegmentCommand(Obj, LCI);
337 case macho::LCT_Segment64:
338 return DumpSegment64Command(Obj, LCI);
339 case macho::LCT_Symtab:
340 return DumpSymtabCommand(Obj);
341 case macho::LCT_Dysymtab:
342 return DumpDysymtabCommand(Obj);
343 case macho::LCT_CodeSignature:
344 case macho::LCT_SegmentSplitInfo:
345 case macho::LCT_FunctionStarts:
346 return DumpLinkeditDataCommand(Obj, LCI);
347 case macho::LCT_DataInCode:
348 return DumpDataInCodeDataCommand(Obj, LCI);
349 case macho::LCT_LinkerOptions:
350 return DumpLinkerOptionsCommand(Obj, LCI);
351 default:
352 Warning("unknown load command: " + Twine(LCI.C.Type));
353 return 0;
354 }
355 }
356
357
DumpLoadCommand(const MachOObjectFile & Obj,unsigned Index,MachOObjectFile::LoadCommandInfo & LCI)358 static 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";
365 return Res;
366 }
367
printHeader(const MachOObjectFile * Obj,const macho::Header & Header)368 static 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
main(int argc,char ** argv)384 int main(int argc, char **argv) {
385 ProgramName = argv[0];
386 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
387
388 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
389
390 OwningPtr<Binary> Binary;
391 if (error_code EC = createBinary(InputFile, Binary))
392 return Error("unable to read input: '" + EC.message() + "'");
393
394 const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(Binary.get());
395 if (!InputObject)
396 return Error("Not a MachO object");
397
398 // Print the header
399 macho::Header Header = InputObject->getHeader();
400 printHeader(InputObject, Header);
401
402 // Print the load commands.
403 int Res = 0;
404 MachOObjectFile::LoadCommandInfo Command =
405 InputObject->getFirstLoadCommandInfo();
406 outs() << "('load_commands', [\n";
407 for (unsigned i = 0; ; ++i) {
408 if (DumpLoadCommand(*InputObject, i, Command))
409 break;
410
411 if (i == Header.NumLoadCommands - 1)
412 break;
413 Command = InputObject->getNextLoadCommandInfo(Command);
414 }
415 outs() << "])\n";
416
417 return Res;
418 }
419