blob: 9648caec5584c4de16b67b8e1cddff0712ddd734 [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"
17#include "llvm/Support/ManagedStatic.h"
Daniel Dunbarad125242010-11-27 06:19:17 +000018#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar75373ac2010-11-27 05:58:44 +000019#include "llvm/Support/raw_ostream.h"
20using namespace llvm;
Daniel Dunbarad125242010-11-27 06:19:17 +000021using namespace llvm::object;
Daniel Dunbar75373ac2010-11-27 05:58:44 +000022
23static cl::opt<std::string>
24InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
25
Daniel Dunbarad125242010-11-27 06:19:17 +000026static cl::opt<bool>
27DumpSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
28 cl::init(false));
29
Daniel Dunbara956d8b2010-11-27 07:19:41 +000030///
31
32static const char *ProgramName;
33
34static void Message(const char *Type, const Twine &Msg) {
35 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
36}
37
38static int Error(const Twine &Msg) {
39 Message("error", Msg);
40 return 1;
41}
42
43static void Warning(const Twine &Msg) {
44 Message("warning", Msg);
45}
46
47///
48
49static int DumpHeader(MachOObject &Obj) {
50 // Read the header.
51 const macho::Header &Hdr = Obj.getHeader();
52 outs() << "('cputype', " << Hdr.CPUType << ")\n";
53 outs() << "('cpusubtype', " << Hdr.CPUSubtype << ")\n";
54 outs() << "('filetype', " << Hdr.FileType << ")\n";
55 outs() << "('num_load_commands', " << Hdr.NumLoadCommands << ")\n";
56 outs() << "('load_commands_size', " << Hdr.SizeOfLoadCommands << ")\n";
57 outs() << "('flag', " << Hdr.Flags << ")\n";
58
59 // Print extended header if 64-bit.
60 if (Obj.is64Bit()) {
61 const macho::Header64Ext &Hdr64 = Obj.getHeader64Ext();
62 outs() << "('reserved', " << Hdr64.Reserved << ")\n";
63 }
64
65 return 0;
66}
67
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +000068static void DumpSegmentCommandData(StringRef Name,
69 uint64_t VMAddr, uint64_t VMSize,
70 uint64_t FileOffset, uint64_t FileSize,
71 uint32_t MaxProt, uint32_t InitProt,
72 uint32_t NumSections, uint32_t Flags) {
73 outs() << " ('segment_name', '";
74 outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
75 outs() << " ('vm_addr', " << VMAddr << ")\n";
76 outs() << " ('vm_size', " << VMSize << ")\n";
77 outs() << " ('file_offset', " << FileOffset << ")\n";
78 outs() << " ('file_size', " << FileSize << ")\n";
79 outs() << " ('maxprot', " << MaxProt << ")\n";
80 outs() << " ('initprot', " << InitProt << ")\n";
81 outs() << " ('num_sections', " << NumSections << ")\n";
82 outs() << " ('flags', " << Flags << ")\n";
83}
84
85static int DumpSegmentCommand(MachOObject &Obj,
86 const MachOObject::LoadCommandInfo &LCI) {
87 InMemoryStruct<macho::SegmentLoadCommand> SLC;
88 Obj.ReadSegmentLoadCommand(LCI, SLC);
89 if (!SLC)
90 return Error("unable to read segment load command");
91
92 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, SLC->VMSize,
93 SLC->FileOffset, SLC->FileSize,
94 SLC->MaxVMProtection, SLC->InitialVMProtection,
95 SLC->NumSections, SLC->Flags);
96
97 return 0;
98}
99static int DumpSegment64Command(MachOObject &Obj,
100 const MachOObject::LoadCommandInfo &LCI) {
101 InMemoryStruct<macho::Segment64LoadCommand> SLC;
102 Obj.ReadSegment64LoadCommand(LCI, SLC);
103 if (!SLC)
104 return Error("unable to read segment load command");
105
106 DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, SLC->VMSize,
107 SLC->FileOffset, SLC->FileSize,
108 SLC->MaxVMProtection, SLC->InitialVMProtection,
109 SLC->NumSections, SLC->Flags);
110
111 return 0;
112}
113
Daniel Dunbarf879f142010-11-27 08:33:44 +0000114static int DumpSymtabCommand(MachOObject &Obj,
115 const MachOObject::LoadCommandInfo &LCI) {
116 InMemoryStruct<macho::SymtabLoadCommand> SLC;
117 Obj.ReadSymtabLoadCommand(LCI, SLC);
118 if (!SLC)
119 return Error("unable to read segment load command");
120
121 outs() << " ('symoff', " << SLC->SymbolTableOffset << ")\n";
122 outs() << " ('nsyms', " << SLC->NumSymbolTableEntries << ")\n";
123 outs() << " ('stroff', " << SLC->StringTableOffset << ")\n";
124 outs() << " ('strsize', " << SLC->StringTableSize << ")\n";
125
126 return 0;
127}
128
129static int DumpDysymtabCommand(MachOObject &Obj,
130 const MachOObject::LoadCommandInfo &LCI) {
131 InMemoryStruct<macho::DysymtabLoadCommand> DLC;
132 Obj.ReadDysymtabLoadCommand(LCI, DLC);
133 if (!DLC)
134 return Error("unable to read segment load command");
135
136 outs() << " ('ilocalsym', " << DLC->LocalSymbolIndex << ")\n";
137 outs() << " ('nlocalsym', " << DLC->NumLocalSymbols << ")\n";
138 outs() << " ('iextdefsym', " << DLC->ExternalSymbolsIndex << ")\n";
139 outs() << " ('nextdefsym', " << DLC->NumExternalSymbols << ")\n";
140 outs() << " ('iundefsym', " << DLC->UndefinedSymbolsIndex << ")\n";
141 outs() << " ('nundefsym', " << DLC->NumUndefinedSymbols << ")\n";
142 outs() << " ('tocoff', " << DLC->TOCOffset << ")\n";
143 outs() << " ('ntoc', " << DLC->NumTOCEntries << ")\n";
144 outs() << " ('modtaboff', " << DLC->ModuleTableOffset << ")\n";
145 outs() << " ('nmodtab', " << DLC->NumModuleTableEntries << ")\n";
146 outs() << " ('extrefsymoff', " << DLC->ReferenceSymbolTableOffset << ")\n";
147 outs() << " ('nextrefsyms', "
148 << DLC->NumReferencedSymbolTableEntries << ")\n";
149 outs() << " ('indirectsymoff', " << DLC->IndirectSymbolTableOffset << ")\n";
150 outs() << " ('nindirectsyms', "
151 << DLC->NumIndirectSymbolTableEntries << ")\n";
152 outs() << " ('extreloff', " << DLC->ExternalRelocationTableOffset << ")\n";
153 outs() << " ('nextrel', " << DLC->NumExternalRelocationTableEntries << ")\n";
154 outs() << " ('locreloff', " << DLC->LocalRelocationTableOffset << ")\n";
155 outs() << " ('nlocrel', " << DLC->NumLocalRelocationTableEntries << ")\n";
156
157 return 0;
158}
159
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000160static int DumpLoadCommand(MachOObject &Obj, unsigned Index) {
161 const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index);
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000162 int Res = 0;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000163
164 outs() << " # Load Command " << Index << "\n"
165 << " (('command', " << LCI.Command.Type << ")\n"
166 << " ('size', " << LCI.Command.Size << ")\n";
167 switch (LCI.Command.Type) {
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000168 case macho::LCT_Segment:
169 Res = DumpSegmentCommand(Obj, LCI);
170 break;
171 case macho::LCT_Segment64:
172 Res = DumpSegment64Command(Obj, LCI);
173 break;
Daniel Dunbarf879f142010-11-27 08:33:44 +0000174 case macho::LCT_Symtab:
175 Res = DumpSymtabCommand(Obj, LCI);
176 break;
177 case macho::LCT_Dysymtab:
178 Res = DumpDysymtabCommand(Obj, LCI);
179 break;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000180 default:
181 Warning("unknown load command: " + Twine(LCI.Command.Type));
182 break;
183 }
184 outs() << " ),\n";
185
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000186 return Res;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000187}
188
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000189int main(int argc, char **argv) {
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000190 ProgramName = argv[0];
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000191 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
192
193 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
194
Daniel Dunbarad125242010-11-27 06:19:17 +0000195 // Load the input file.
196 std::string ErrorStr;
197 OwningPtr<MemoryBuffer> InputBuffer(
198 MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr));
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000199 if (!InputBuffer)
200 return Error("unable to read input: '" + ErrorStr + "'");
Daniel Dunbarad125242010-11-27 06:19:17 +0000201
202 // Construct the Mach-O wrapper object.
203 OwningPtr<MachOObject> InputObject(
204 MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr));
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000205 if (!InputObject)
206 return Error("unable to load object: '" + ErrorStr + "'");
Daniel Dunbarad125242010-11-27 06:19:17 +0000207
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000208 if (int Res = DumpHeader(*InputObject))
209 return Res;
210
211 // Print the load commands.
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000212 int Res = 0;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000213 outs() << "('load_commands', [\n";
214 for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i)
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000215 if ((Res = DumpLoadCommand(*InputObject, i)))
216 break;
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000217 outs() << "])\n";
218
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000219 return Res;
Daniel Dunbar75373ac2010-11-27 05:58:44 +0000220}