1 //===-- macho-dump.cpp - Mach Object Dumping Tool -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is a testing tool for use with the MC/Mach-O LLVM components.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/MachOObject.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm::object;
24 static cl::opt<std::string>
25 InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
28 ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
33 static const char *ProgramName;
35 static void Message(const char *Type, const Twine &Msg) {
36 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
39 static int Error(const Twine &Msg) {
40 Message("error", Msg);
44 static void Warning(const Twine &Msg) {
45 Message("warning", Msg);
50 static int DumpHeader(MachOObject &Obj) {
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";
60 // Print extended header if 64-bit.
62 const macho::Header64Ext &Hdr64 = Obj.getHeader64Ext();
63 outs() << "('reserved', " << Hdr64.Reserved << ")\n";
69 static 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";
86 static 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) {
93 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";
111 // Dump the relocation entries.
113 outs() << " ('_relocations', [\n";
114 for (unsigned i = 0; i != NumRelocationTableEntries; ++i) {
115 InMemoryStruct<macho::RelocationEntry> RE;
116 Obj.ReadRelocationEntry(RelocationTableOffset, i, RE);
118 Res = Error("unable to read relocation table entry '" + Twine(i) + "'");
122 outs() << " # Relocation " << i << "\n";
123 outs() << " (('word-0', " << format("%#x", RE->Word0) << "),\n";
124 outs() << " ('word-1', " << format("%#x", RE->Word1) << ")),\n";
131 static int DumpSegmentCommand(MachOObject &Obj,
132 const MachOObject::LoadCommandInfo &LCI) {
133 InMemoryStruct<macho::SegmentLoadCommand> SLC;
134 Obj.ReadSegmentLoadCommand(LCI, SLC);
136 return Error("unable to read segment load command");
138 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress,
139 SLC->VMSize, SLC->FileOffset, SLC->FileSize,
140 SLC->MaxVMProtection, SLC->InitialVMProtection,
141 SLC->NumSections, SLC->Flags);
143 // Dump the sections.
145 outs() << " ('sections', [\n";
146 for (unsigned i = 0; i != SLC->NumSections; ++i) {
147 InMemoryStruct<macho::Section> Sect;
148 Obj.ReadSection(LCI, i, Sect);
150 Res = Error("unable to read section '" + Twine(i) + "'");
154 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)))
167 static int DumpSegment64Command(MachOObject &Obj,
168 const MachOObject::LoadCommandInfo &LCI) {
169 InMemoryStruct<macho::Segment64LoadCommand> SLC;
170 Obj.ReadSegment64LoadCommand(LCI, SLC);
172 return Error("unable to read segment load command");
174 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress,
175 SLC->VMSize, SLC->FileOffset, SLC->FileSize,
176 SLC->MaxVMProtection, SLC->InitialVMProtection,
177 SLC->NumSections, SLC->Flags);
179 // Dump the sections.
181 outs() << " ('sections', [\n";
182 for (unsigned i = 0; i != SLC->NumSections; ++i) {
183 InMemoryStruct<macho::Section64> Sect;
184 Obj.ReadSection64(LCI, i, Sect);
186 Res = Error("unable to read section '" + Twine(i) + "'");
190 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,
204 static int DumpSymtabCommand(MachOObject &Obj,
205 const MachOObject::LoadCommandInfo &LCI) {
206 InMemoryStruct<macho::SymtabLoadCommand> SLC;
207 Obj.ReadSymtabLoadCommand(LCI, SLC);
209 return Error("unable to read segment load command");
211 outs() << " ('symoff', " << SLC->SymbolTableOffset << ")\n";
212 outs() << " ('nsyms', " << SLC->NumSymbolTableEntries << ")\n";
213 outs() << " ('stroff', " << SLC->StringTableOffset << ")\n";
214 outs() << " ('strsize', " << SLC->StringTableSize << ")\n";
219 static int DumpDysymtabCommand(MachOObject &Obj,
220 const MachOObject::LoadCommandInfo &LCI) {
221 InMemoryStruct<macho::DysymtabLoadCommand> DLC;
222 Obj.ReadDysymtabLoadCommand(LCI, DLC);
224 return Error("unable to read segment load command");
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";
247 // Dump the indirect symbol table.
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);
254 Res = Error("unable to read segment load command");
258 outs() << " # Indirect Symbol " << i << "\n";
259 outs() << " (('symbol_index', "
260 << format("%#x", ISTE->Index) << "),),\n";
267 static int DumpLoadCommand(MachOObject &Obj, unsigned Index) {
268 const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index);
271 outs() << " # Load Command " << Index << "\n"
272 << " (('command', " << LCI.Command.Type << ")\n"
273 << " ('size', " << LCI.Command.Size << ")\n";
274 switch (LCI.Command.Type) {
275 case macho::LCT_Segment:
276 Res = DumpSegmentCommand(Obj, LCI);
278 case macho::LCT_Segment64:
279 Res = DumpSegment64Command(Obj, LCI);
281 case macho::LCT_Symtab:
282 Res = DumpSymtabCommand(Obj, LCI);
284 case macho::LCT_Dysymtab:
285 Res = DumpDysymtabCommand(Obj, LCI);
288 Warning("unknown load command: " + Twine(LCI.Command.Type));
296 int main(int argc, char **argv) {
297 ProgramName = argv[0];
298 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
300 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
302 // Load the input file.
303 std::string ErrorStr;
304 OwningPtr<MemoryBuffer> InputBuffer(
305 MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr));
307 return Error("unable to read input: '" + ErrorStr + "'");
309 // Construct the Mach-O wrapper object.
310 OwningPtr<MachOObject> InputObject(
311 MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr));
313 return Error("unable to load object: '" + ErrorStr + "'");
315 if (int Res = DumpHeader(*InputObject))
318 // Print the load commands.
320 outs() << "('load_commands', [\n";
321 for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i)
322 if ((Res = DumpLoadCommand(*InputObject, i)))