blob: 487a00607b8844a3813cd25f20e9f9b3f2147876 [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>
28DumpSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
29 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
86static int DumpSegmentCommand(MachOObject &Obj,
87 const MachOObject::LoadCommandInfo &LCI) {
88 InMemoryStruct<macho::SegmentLoadCommand> SLC;
89 Obj.ReadSegmentLoadCommand(LCI, SLC);
90 if (!SLC)
91 return Error("unable to read segment load command");
92
93 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, SLC->VMSize,
94 SLC->FileOffset, SLC->FileSize,
95 SLC->MaxVMProtection, SLC->InitialVMProtection,
96 SLC->NumSections, SLC->Flags);
97
98 return 0;
99}
100static int DumpSegment64Command(MachOObject &Obj,
101 const MachOObject::LoadCommandInfo &LCI) {
102 InMemoryStruct<macho::Segment64LoadCommand> SLC;
103 Obj.ReadSegment64LoadCommand(LCI, SLC);
104 if (!SLC)
105 return Error("unable to read segment load command");
106
107 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, SLC->VMSize,
108 SLC->FileOffset, SLC->FileSize,
109 SLC->MaxVMProtection, SLC->InitialVMProtection,
110 SLC->NumSections, SLC->Flags);
111
112 return 0;
113}
114
Daniel Dunbarf879f142010-11-27 08:33:44 +0000115static int DumpSymtabCommand(MachOObject &Obj,
116 const MachOObject::LoadCommandInfo &LCI) {
117 InMemoryStruct<macho::SymtabLoadCommand> SLC;
118 Obj.ReadSymtabLoadCommand(LCI, SLC);
119 if (!SLC)
120 return Error("unable to read segment load command");
121
122 outs() << " ('symoff', " << SLC->SymbolTableOffset << ")\n";
123 outs() << " ('nsyms', " << SLC->NumSymbolTableEntries << ")\n";
124 outs() << " ('stroff', " << SLC->StringTableOffset << ")\n";
125 outs() << " ('strsize', " << SLC->StringTableSize << ")\n";
126
127 return 0;
128}
129
130static int DumpDysymtabCommand(MachOObject &Obj,
131 const MachOObject::LoadCommandInfo &LCI) {
132 InMemoryStruct<macho::DysymtabLoadCommand> DLC;
133 Obj.ReadDysymtabLoadCommand(LCI, DLC);
134 if (!DLC)
135 return Error("unable to read segment load command");
136
137 outs() << " ('ilocalsym', " << DLC->LocalSymbolIndex << ")\n";
138 outs() << " ('nlocalsym', " << DLC->NumLocalSymbols << ")\n";
139 outs() << " ('iextdefsym', " << DLC->ExternalSymbolsIndex << ")\n";
140 outs() << " ('nextdefsym', " << DLC->NumExternalSymbols << ")\n";
141 outs() << " ('iundefsym', " << DLC->UndefinedSymbolsIndex << ")\n";
142 outs() << " ('nundefsym', " << DLC->NumUndefinedSymbols << ")\n";
143 outs() << " ('tocoff', " << DLC->TOCOffset << ")\n";
144 outs() << " ('ntoc', " << DLC->NumTOCEntries << ")\n";
145 outs() << " ('modtaboff', " << DLC->ModuleTableOffset << ")\n";
146 outs() << " ('nmodtab', " << DLC->NumModuleTableEntries << ")\n";
147 outs() << " ('extrefsymoff', " << DLC->ReferenceSymbolTableOffset << ")\n";
148 outs() << " ('nextrefsyms', "
149 << DLC->NumReferencedSymbolTableEntries << ")\n";
150 outs() << " ('indirectsymoff', " << DLC->IndirectSymbolTableOffset << ")\n";
151 outs() << " ('nindirectsyms', "
152 << DLC->NumIndirectSymbolTableEntries << ")\n";
153 outs() << " ('extreloff', " << DLC->ExternalRelocationTableOffset << ")\n";
154 outs() << " ('nextrel', " << DLC->NumExternalRelocationTableEntries << ")\n";
155 outs() << " ('locreloff', " << DLC->LocalRelocationTableOffset << ")\n";
156 outs() << " ('nlocrel', " << DLC->NumLocalRelocationTableEntries << ")\n";
157
Daniel Dunbar4c55e0d2010-11-27 13:26:12 +0000158 // Dump the indirect symbol table.
159 int Res = 0;
160 outs() << " ('_indirect_symbols', [\n";
161 for (unsigned i = 0; i != DLC->NumIndirectSymbolTableEntries; ++i) {
162 InMemoryStruct<macho::IndirectSymbolTableEntry> ISTE;
163 Obj.ReadIndirectSymbolTableEntry(*DLC, i, ISTE);
164 if (!ISTE) {
165 Res = Error("unable to read segment load command");
166 break;
167 }
168
169 outs() << " # Indirect Symbol " << i << "\n";
170 outs() << " (('symbol_index', "
171 << format("%#x", ISTE->Index) << "),),\n";
172 }
173 outs() << " ])\n";
174
175 return Res;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000176}
177
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000178static int DumpLoadCommand(MachOObject &Obj, unsigned Index) {
179 const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index);
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000180 int Res = 0;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000181
182 outs() << " # Load Command " << Index << "\n"
183 << " (('command', " << LCI.Command.Type << ")\n"
184 << " ('size', " << LCI.Command.Size << ")\n";
185 switch (LCI.Command.Type) {
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000186 case macho::LCT_Segment:
187 Res = DumpSegmentCommand(Obj, LCI);
188 break;
189 case macho::LCT_Segment64:
190 Res = DumpSegment64Command(Obj, LCI);
191 break;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000192 case macho::LCT_Symtab:
193 Res = DumpSymtabCommand(Obj, LCI);
194 break;
195 case macho::LCT_Dysymtab:
196 Res = DumpDysymtabCommand(Obj, LCI);
197 break;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000198 default:
199 Warning("unknown load command: " + Twine(LCI.Command.Type));
200 break;
201 }
202 outs() << " ),\n";
203
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000204 return Res;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000205}
206
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000207int main(int argc, char **argv) {
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000208 ProgramName = argv[0];
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000209 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
210
211 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
212
Daniel Dunbarad125242010-11-27 06:19:17 +0000213 // Load the input file.
214 std::string ErrorStr;
215 OwningPtr<MemoryBuffer> InputBuffer(
216 MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr));
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000217 if (!InputBuffer)
218 return Error("unable to read input: '" + ErrorStr + "'");
Daniel Dunbarad125242010-11-27 06:19:17 +0000219
220 // Construct the Mach-O wrapper object.
221 OwningPtr<MachOObject> InputObject(
222 MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr));
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000223 if (!InputObject)
224 return Error("unable to load object: '" + ErrorStr + "'");
Daniel Dunbarad125242010-11-27 06:19:17 +0000225
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000226 if (int Res = DumpHeader(*InputObject))
227 return Res;
228
229 // Print the load commands.
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000230 int Res = 0;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000231 outs() << "('load_commands', [\n";
232 for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i)
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000233 if ((Res = DumpLoadCommand(*InputObject, i)))
234 break;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000235 outs() << "])\n";
236
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000237 return Res;
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000238}