blob: 274c41bd25ccf49fe81065b7952624565e46cd3b [file] [log] [blame]
Daniel Dunbar75373ac2010-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
Daniel Dunbarad125242010-11-27 06:19:17 +000014#include "llvm/Object/MachOObject.h"
Daniel Dunbara956d8b2010-11-27 07:19:41 +000015#include "llvm/ADT/Twine.h"
Daniel Dunbar75373ac2010-11-27 05:58:44 +000016#include "llvm/Support/CommandLine.h"
Daniel Dunbar4c55e0d2010-11-27 13:26:12 +000017#include "llvm/Support/Format.h"
Daniel Dunbar75373ac2010-11-27 05:58:44 +000018#include "llvm/Support/ManagedStatic.h"
Daniel Dunbarad125242010-11-27 06:19:17 +000019#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar75373ac2010-11-27 05:58:44 +000020#include "llvm/Support/raw_ostream.h"
21using namespace llvm;
Daniel Dunbarad125242010-11-27 06:19:17 +000022using namespace llvm::object;
Daniel Dunbar75373ac2010-11-27 05:58:44 +000023
24static cl::opt<std::string>
25InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
26
Daniel Dunbarad125242010-11-27 06:19:17 +000027static cl::opt<bool>
Daniel Dunbar2acadbd2010-11-27 13:33:15 +000028ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
Daniel Dunbarad125242010-11-27 06:19:17 +000029 cl::init(false));
30
Daniel Dunbara956d8b2010-11-27 07:19:41 +000031///
32
33static const char *ProgramName;
34
35static void Message(const char *Type, const Twine &Msg) {
36 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
37}
38
39static int Error(const Twine &Msg) {
40 Message("error", Msg);
41 return 1;
42}
43
44static void Warning(const Twine &Msg) {
45 Message("warning", Msg);
46}
47
48///
49
50static int DumpHeader(MachOObject &Obj) {
51 // Read the header.
52 const macho::Header &Hdr = Obj.getHeader();
53 outs() << "('cputype', " << Hdr.CPUType << ")\n";
54 outs() << "('cpusubtype', " << Hdr.CPUSubtype << ")\n";
55 outs() << "('filetype', " << Hdr.FileType << ")\n";
56 outs() << "('num_load_commands', " << Hdr.NumLoadCommands << ")\n";
57 outs() << "('load_commands_size', " << Hdr.SizeOfLoadCommands << ")\n";
58 outs() << "('flag', " << Hdr.Flags << ")\n";
59
60 // Print extended header if 64-bit.
61 if (Obj.is64Bit()) {
62 const macho::Header64Ext &Hdr64 = Obj.getHeader64Ext();
63 outs() << "('reserved', " << Hdr64.Reserved << ")\n";
64 }
65
66 return 0;
67}
68
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +000069static void DumpSegmentCommandData(StringRef Name,
70 uint64_t VMAddr, uint64_t VMSize,
71 uint64_t FileOffset, uint64_t FileSize,
72 uint32_t MaxProt, uint32_t InitProt,
73 uint32_t NumSections, uint32_t Flags) {
74 outs() << " ('segment_name', '";
75 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
76 outs() << " ('vm_addr', " << VMAddr << ")\n";
77 outs() << " ('vm_size', " << VMSize << ")\n";
78 outs() << " ('file_offset', " << FileOffset << ")\n";
79 outs() << " ('file_size', " << FileSize << ")\n";
80 outs() << " ('maxprot', " << MaxProt << ")\n";
81 outs() << " ('initprot', " << InitProt << ")\n";
82 outs() << " ('num_sections', " << NumSections << ")\n";
83 outs() << " ('flags', " << Flags << ")\n";
84}
85
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +000086static int DumpSectionData(MachOObject &Obj, unsigned Index, StringRef Name,
87 StringRef SegmentName, uint64_t Address,
88 uint64_t Size, uint32_t Offset,
89 uint32_t Align, uint32_t RelocationTableOffset,
90 uint32_t NumRelocationTableEntries,
91 uint32_t Flags, uint32_t Reserved1,
92 uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) {
Daniel Dunbar2acadbd2010-11-27 13:33:15 +000093 outs() << " # Section " << Index << "\n";
94 outs() << " (('section_name', '";
95 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
96 outs() << " ('segment_name', '";
97 outs().write_escaped(SegmentName, /*UseHexEscapes=*/true) << "')\n";
98 outs() << " ('address', " << Address << ")\n";
99 outs() << " ('size', " << Size << ")\n";
100 outs() << " ('offset', " << Offset << ")\n";
101 outs() << " ('alignment', " << Align << ")\n";
102 outs() << " ('reloc_offset', " << RelocationTableOffset << ")\n";
103 outs() << " ('num_reloc', " << NumRelocationTableEntries << ")\n";
104 outs() << " ('flags', " << format("%#x", Flags) << ")\n";
105 outs() << " ('reserved1', " << Reserved1 << ")\n";
106 outs() << " ('reserved2', " << Reserved2 << ")\n";
107 if (Reserved3 != ~0ULL)
108 outs() << " ('reserved3', " << Reserved3 << ")\n";
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000109 outs() << " ),\n";
110
111 // Dump the relocation entries.
112 int Res = 0;
113 outs() << " ('_relocations', [\n";
114 for (unsigned i = 0; i != NumRelocationTableEntries; ++i) {
115 InMemoryStruct<macho::RelocationEntry> RE;
116 Obj.ReadRelocationEntry(RelocationTableOffset, i, RE);
117 if (!RE) {
118 Res = Error("unable to read relocation table entry '" + Twine(i) + "'");
119 break;
120 }
121
122 outs() << " # Relocation " << i << "\n";
123 outs() << " (('word-0', " << format("%#x", RE->Word0) << "),\n";
124 outs() << " ('word-1', " << format("%#x", RE->Word1) << ")),\n";
125 }
126 outs() << " ])\n";
127
128 return Res;
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000129}
130
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000131static int DumpSegmentCommand(MachOObject &Obj,
132 const MachOObject::LoadCommandInfo &LCI) {
133 InMemoryStruct<macho::SegmentLoadCommand> SLC;
134 Obj.ReadSegmentLoadCommand(LCI, SLC);
135 if (!SLC)
136 return Error("unable to read segment load command");
137
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000138 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress,
139 SLC->VMSize, SLC->FileOffset, SLC->FileSize,
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000140 SLC->MaxVMProtection, SLC->InitialVMProtection,
141 SLC->NumSections, SLC->Flags);
142
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000143 // Dump the sections.
144 int Res = 0;
145 outs() << " ('sections', [\n";
146 for (unsigned i = 0; i != SLC->NumSections; ++i) {
147 InMemoryStruct<macho::Section> Sect;
148 Obj.ReadSection(LCI, i, Sect);
149 if (!SLC) {
150 Res = Error("unable to read section '" + Twine(i) + "'");
151 break;
152 }
153
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000154 if ((Res = DumpSectionData(Obj, i, StringRef(Sect->Name, 16),
155 StringRef(Sect->SegmentName, 16), Sect->Address,
156 Sect->Size, Sect->Offset, Sect->Align,
157 Sect->RelocationTableOffset,
158 Sect->NumRelocationTableEntries, Sect->Flags,
159 Sect->Reserved1, Sect->Reserved2)))
160 break;
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000161 }
162 outs() << " ])\n";
163
164 return Res;
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000165}
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000166
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000167static int DumpSegment64Command(MachOObject &Obj,
168 const MachOObject::LoadCommandInfo &LCI) {
169 InMemoryStruct<macho::Segment64LoadCommand> SLC;
170 Obj.ReadSegment64LoadCommand(LCI, SLC);
171 if (!SLC)
172 return Error("unable to read segment load command");
173
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000174 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress,
175 SLC->VMSize, SLC->FileOffset, SLC->FileSize,
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000176 SLC->MaxVMProtection, SLC->InitialVMProtection,
177 SLC->NumSections, SLC->Flags);
178
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000179 // Dump the sections.
180 int Res = 0;
181 outs() << " ('sections', [\n";
182 for (unsigned i = 0; i != SLC->NumSections; ++i) {
183 InMemoryStruct<macho::Section64> Sect;
184 Obj.ReadSection64(LCI, i, Sect);
185 if (!SLC) {
186 Res = Error("unable to read section '" + Twine(i) + "'");
187 break;
188 }
189
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000190 if ((Res = DumpSectionData(Obj, i, StringRef(Sect->Name, 16),
191 StringRef(Sect->SegmentName, 16), Sect->Address,
192 Sect->Size, Sect->Offset, Sect->Align,
193 Sect->RelocationTableOffset,
194 Sect->NumRelocationTableEntries, Sect->Flags,
195 Sect->Reserved1, Sect->Reserved2,
196 Sect->Reserved3)))
197 break;
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000198 }
199 outs() << " ])\n";
200
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000201 return 0;
202}
203
Daniel Dunbarf879f142010-11-27 08:33:44 +0000204static int DumpSymtabCommand(MachOObject &Obj,
205 const MachOObject::LoadCommandInfo &LCI) {
206 InMemoryStruct<macho::SymtabLoadCommand> SLC;
207 Obj.ReadSymtabLoadCommand(LCI, SLC);
208 if (!SLC)
209 return Error("unable to read segment load command");
210
211 outs() << " ('symoff', " << SLC->SymbolTableOffset << ")\n";
212 outs() << " ('nsyms', " << SLC->NumSymbolTableEntries << ")\n";
213 outs() << " ('stroff', " << SLC->StringTableOffset << ")\n";
214 outs() << " ('strsize', " << SLC->StringTableSize << ")\n";
215
216 return 0;
217}
218
219static int DumpDysymtabCommand(MachOObject &Obj,
220 const MachOObject::LoadCommandInfo &LCI) {
221 InMemoryStruct<macho::DysymtabLoadCommand> DLC;
222 Obj.ReadDysymtabLoadCommand(LCI, DLC);
223 if (!DLC)
224 return Error("unable to read segment load command");
225
226 outs() << " ('ilocalsym', " << DLC->LocalSymbolIndex << ")\n";
227 outs() << " ('nlocalsym', " << DLC->NumLocalSymbols << ")\n";
228 outs() << " ('iextdefsym', " << DLC->ExternalSymbolsIndex << ")\n";
229 outs() << " ('nextdefsym', " << DLC->NumExternalSymbols << ")\n";
230 outs() << " ('iundefsym', " << DLC->UndefinedSymbolsIndex << ")\n";
231 outs() << " ('nundefsym', " << DLC->NumUndefinedSymbols << ")\n";
232 outs() << " ('tocoff', " << DLC->TOCOffset << ")\n";
233 outs() << " ('ntoc', " << DLC->NumTOCEntries << ")\n";
234 outs() << " ('modtaboff', " << DLC->ModuleTableOffset << ")\n";
235 outs() << " ('nmodtab', " << DLC->NumModuleTableEntries << ")\n";
236 outs() << " ('extrefsymoff', " << DLC->ReferenceSymbolTableOffset << ")\n";
237 outs() << " ('nextrefsyms', "
238 << DLC->NumReferencedSymbolTableEntries << ")\n";
239 outs() << " ('indirectsymoff', " << DLC->IndirectSymbolTableOffset << ")\n";
240 outs() << " ('nindirectsyms', "
241 << DLC->NumIndirectSymbolTableEntries << ")\n";
242 outs() << " ('extreloff', " << DLC->ExternalRelocationTableOffset << ")\n";
243 outs() << " ('nextrel', " << DLC->NumExternalRelocationTableEntries << ")\n";
244 outs() << " ('locreloff', " << DLC->LocalRelocationTableOffset << ")\n";
245 outs() << " ('nlocrel', " << DLC->NumLocalRelocationTableEntries << ")\n";
246
Daniel Dunbar4c55e0d2010-11-27 13:26:12 +0000247 // Dump the indirect symbol table.
248 int Res = 0;
249 outs() << " ('_indirect_symbols', [\n";
250 for (unsigned i = 0; i != DLC->NumIndirectSymbolTableEntries; ++i) {
251 InMemoryStruct<macho::IndirectSymbolTableEntry> ISTE;
252 Obj.ReadIndirectSymbolTableEntry(*DLC, i, ISTE);
253 if (!ISTE) {
254 Res = Error("unable to read segment load command");
255 break;
256 }
257
258 outs() << " # Indirect Symbol " << i << "\n";
259 outs() << " (('symbol_index', "
260 << format("%#x", ISTE->Index) << "),),\n";
261 }
262 outs() << " ])\n";
263
264 return Res;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000265}
266
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000267static int DumpLoadCommand(MachOObject &Obj, unsigned Index) {
268 const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index);
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000269 int Res = 0;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000270
271 outs() << " # Load Command " << Index << "\n"
272 << " (('command', " << LCI.Command.Type << ")\n"
273 << " ('size', " << LCI.Command.Size << ")\n";
274 switch (LCI.Command.Type) {
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000275 case macho::LCT_Segment:
276 Res = DumpSegmentCommand(Obj, LCI);
277 break;
278 case macho::LCT_Segment64:
279 Res = DumpSegment64Command(Obj, LCI);
280 break;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000281 case macho::LCT_Symtab:
282 Res = DumpSymtabCommand(Obj, LCI);
283 break;
284 case macho::LCT_Dysymtab:
285 Res = DumpDysymtabCommand(Obj, LCI);
286 break;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000287 default:
288 Warning("unknown load command: " + Twine(LCI.Command.Type));
289 break;
290 }
291 outs() << " ),\n";
292
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000293 return Res;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000294}
295
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000296int main(int argc, char **argv) {
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000297 ProgramName = argv[0];
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000298 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
299
300 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
301
Daniel Dunbarad125242010-11-27 06:19:17 +0000302 // Load the input file.
303 std::string ErrorStr;
304 OwningPtr<MemoryBuffer> InputBuffer(
305 MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr));
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000306 if (!InputBuffer)
307 return Error("unable to read input: '" + ErrorStr + "'");
Daniel Dunbarad125242010-11-27 06:19:17 +0000308
309 // Construct the Mach-O wrapper object.
310 OwningPtr<MachOObject> InputObject(
311 MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr));
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000312 if (!InputObject)
313 return Error("unable to load object: '" + ErrorStr + "'");
Daniel Dunbarad125242010-11-27 06:19:17 +0000314
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000315 if (int Res = DumpHeader(*InputObject))
316 return Res;
317
318 // Print the load commands.
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000319 int Res = 0;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000320 outs() << "('load_commands', [\n";
321 for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i)
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000322 if ((Res = DumpLoadCommand(*InputObject, i)))
323 break;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000324 outs() << "])\n";
325
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000326 return Res;
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000327}