macho-dump: Add support for dumping dysymtab indirect symbol table.
[oota-llvm.git] / tools / macho-dump / macho-dump.cpp
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/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"
21 using namespace llvm;
22 using namespace llvm::object;
23
24 static cl::opt<std::string>
25 InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
26
27 static cl::opt<bool>
28 DumpSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
29                 cl::init(false));
30
31 ///
32
33 static const char *ProgramName;
34
35 static void Message(const char *Type, const Twine &Msg) {
36   errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
37 }
38
39 static int Error(const Twine &Msg) {
40   Message("error", Msg);
41   return 1;
42 }
43
44 static void Warning(const Twine &Msg) {
45   Message("warning", Msg);
46 }
47
48 ///
49
50 static 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
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";
84 }
85
86 static 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 }
100 static 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
115 static 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
130 static 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
158   // 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;
176 }
177
178 static int DumpLoadCommand(MachOObject &Obj, unsigned Index) {
179   const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index);
180   int Res = 0;
181
182   outs() << "  # Load Command " << Index << "\n"
183          << " (('command', " << LCI.Command.Type << ")\n"
184          << "  ('size', " << LCI.Command.Size << ")\n";
185   switch (LCI.Command.Type) {
186   case macho::LCT_Segment:
187     Res = DumpSegmentCommand(Obj, LCI);
188     break;
189   case macho::LCT_Segment64:
190     Res = DumpSegment64Command(Obj, LCI);
191     break;
192   case macho::LCT_Symtab:
193     Res = DumpSymtabCommand(Obj, LCI);
194     break;
195   case macho::LCT_Dysymtab:
196     Res = DumpDysymtabCommand(Obj, LCI);
197     break;
198   default:
199     Warning("unknown load command: " + Twine(LCI.Command.Type));
200     break;
201   }
202   outs() << " ),\n";
203
204   return Res;
205 }
206
207 int main(int argc, char **argv) {
208   ProgramName = argv[0];
209   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
210
211   cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
212
213   // Load the input file.
214   std::string ErrorStr;
215   OwningPtr<MemoryBuffer> InputBuffer(
216     MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr));
217   if (!InputBuffer)
218     return Error("unable to read input: '" + ErrorStr + "'");
219
220   // Construct the Mach-O wrapper object.
221   OwningPtr<MachOObject> InputObject(
222     MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr));
223   if (!InputObject)
224     return Error("unable to load object: '" + ErrorStr + "'");
225
226   if (int Res = DumpHeader(*InputObject))
227     return Res;
228
229   // Print the load commands.
230   int Res = 0;
231   outs() << "('load_commands', [\n";
232   for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i)
233     if ((Res = DumpLoadCommand(*InputObject, i)))
234       break;
235   outs() << "])\n";
236
237   return Res;
238 }