717569cd43bb9a21991e3a82090d7d15bc84b9ad
[oota-llvm.git] / tools / llvm-readobj / MachODumper.cpp
1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
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 file implements the MachO-specific dumper for llvm-readobj.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm-readobj.h"
15 #include "Error.h"
16 #include "ObjDumper.h"
17 #include "StreamWriter.h"
18
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/Object/MachO.h"
21 #include "llvm/Support/Casting.h"
22
23 using namespace llvm;
24 using namespace object;
25
26 namespace {
27
28 class MachODumper : public ObjDumper {
29 public:
30   MachODumper(const llvm::object::MachOObjectFile *Obj, StreamWriter& Writer)
31     : ObjDumper(Writer)
32     , Obj(Obj) { }
33
34   virtual void printFileHeaders() LLVM_OVERRIDE;
35   virtual void printSections() LLVM_OVERRIDE;
36   virtual void printRelocations() LLVM_OVERRIDE;
37   virtual void printSymbols() LLVM_OVERRIDE;
38   virtual void printDynamicSymbols() LLVM_OVERRIDE;
39   virtual void printUnwindInfo() LLVM_OVERRIDE;
40
41 private:
42   void printSymbol(symbol_iterator SymI);
43
44   void printRelocation(section_iterator SecI, relocation_iterator RelI);
45
46   const llvm::object::MachOObjectFile *Obj;
47 };
48
49 } // namespace
50
51
52 namespace llvm {
53
54 error_code createMachODumper(const object::ObjectFile *Obj,
55                              StreamWriter& Writer,
56                              OwningPtr<ObjDumper> &Result) {
57   const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(Obj);
58   if (!MachOObj)
59     return readobj_error::unsupported_obj_file_format;
60
61   Result.reset(new MachODumper(MachOObj, Writer));
62   return readobj_error::success;
63 }
64
65 } // namespace llvm
66
67
68 static const EnumEntry<unsigned> MachOSectionTypes[] = {
69   { "Regular"                        , 0x00 },
70   { "ZeroFill"                       , 0x01 },
71   { "CStringLiterals"                , 0x02 },
72   { "4ByteLiterals"                  , 0x03 },
73   { "8ByteLiterals"                  , 0x04 },
74   { "LiteralPointers"                , 0x05 },
75   { "NonLazySymbolPointers"          , 0x06 },
76   { "LazySymbolPointers"             , 0x07 },
77   { "SymbolStubs"                    , 0x08 },
78   { "ModInitFuncs"                   , 0x09 },
79   { "ModTermFuncs"                   , 0x0A },
80   { "Coalesced"                      , 0x0B },
81   { "GBZeroFill"                     , 0x0C },
82   { "Interposing"                    , 0x0D },
83   { "16ByteLiterals"                 , 0x0E },
84   { "DTraceDOF"                      , 0x0F },
85   { "LazyDylibSymbolPoints"          , 0x10 },
86   { "ThreadLocalRegular"             , 0x11 },
87   { "ThreadLocalZerofill"            , 0x12 },
88   { "ThreadLocalVariables"           , 0x13 },
89   { "ThreadLocalVariablePointers"    , 0x14 },
90   { "ThreadLocalInitFunctionPointers", 0x15 }
91 };
92
93 static const EnumEntry<unsigned> MachOSectionAttributes[] = {
94   { "LocReloc"         , 1 <<  0 /*S_ATTR_LOC_RELOC          */ },
95   { "ExtReloc"         , 1 <<  1 /*S_ATTR_EXT_RELOC          */ },
96   { "SomeInstructions" , 1 <<  2 /*S_ATTR_SOME_INSTRUCTIONS  */ },
97   { "Debug"            , 1 << 17 /*S_ATTR_DEBUG              */ },
98   { "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ },
99   { "LiveSupport"      , 1 << 19 /*S_ATTR_LIVE_SUPPORT       */ },
100   { "NoDeadStrip"      , 1 << 20 /*S_ATTR_NO_DEAD_STRIP      */ },
101   { "StripStaticSyms"  , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS  */ },
102   { "NoTOC"            , 1 << 22 /*S_ATTR_NO_TOC             */ },
103   { "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS  */ },
104 };
105
106 static const EnumEntry<unsigned> MachOSymbolRefTypes[] = {
107   { "UndefinedNonLazy",                     0 },
108   { "ReferenceFlagUndefinedLazy",           1 },
109   { "ReferenceFlagDefined",                 2 },
110   { "ReferenceFlagPrivateDefined",          3 },
111   { "ReferenceFlagPrivateUndefinedNonLazy", 4 },
112   { "ReferenceFlagPrivateUndefinedLazy",    5 }
113 };
114
115 static const EnumEntry<unsigned> MachOSymbolFlags[] = {
116   { "ReferencedDynamically", 0x10 },
117   { "NoDeadStrip",           0x20 },
118   { "WeakRef",               0x40 },
119   { "WeakDef",               0x80 }
120 };
121
122 static const EnumEntry<unsigned> MachOSymbolTypes[] = {
123   { "Undef",           0x0 },
124   { "External",        0x1 },
125   { "Abs",             0x2 },
126   { "Indirect",        0xA },
127   { "PreboundUndef",   0xC },
128   { "Section",         0xE },
129   { "PrivateExternal", 0x10 }
130 };
131
132 namespace {
133   enum {
134     N_STAB = 0xE0
135   };
136
137   struct MachOSection {
138     ArrayRef<char> Name;
139     ArrayRef<char> SegmentName;
140     uint64_t Address;
141     uint64_t Size;
142     uint32_t Offset;
143     uint32_t Alignment;
144     uint32_t RelocationTableOffset;
145     uint32_t NumRelocationTableEntries;
146     uint32_t Flags;
147     uint32_t Reserved1;
148     uint32_t Reserved2;
149   };
150
151   struct MachOSymbol {
152     uint32_t StringIndex;
153     uint8_t Type;
154     uint8_t SectionIndex;
155     uint16_t Flags;
156     uint64_t Value;
157   };
158 }
159
160 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
161   LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
162   if (LCI.Command.Type == macho::LCT_Segment64)
163     return true;
164   assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
165   return false;
166 }
167
168 static void getSection(const MachOObjectFile *Obj,
169                        DataRefImpl DRI,
170                        MachOSection &Section) {
171   const MachOObject *MachOObj = Obj->getObject();
172
173   if (is64BitLoadCommand(MachOObj, DRI)) {
174     const MachOFormat::Section64 *Sect = Obj->getSection64(DRI);
175
176     Section.Address     = Sect->Address;
177     Section.Size        = Sect->Size;
178     Section.Offset      = Sect->Offset;
179     Section.Alignment   = Sect->Align;
180     Section.RelocationTableOffset = Sect->RelocationTableOffset;
181     Section.NumRelocationTableEntries = Sect->NumRelocationTableEntries;
182     Section.Flags       = Sect->Flags;
183     Section.Reserved1   = Sect->Reserved1;
184     Section.Reserved2   = Sect->Reserved2;
185   } else {
186     const MachOFormat::Section *Sect = Obj->getSection(DRI);
187
188     Section.Address     = Sect->Address;
189     Section.Size        = Sect->Size;
190     Section.Offset      = Sect->Offset;
191     Section.Alignment   = Sect->Align;
192     Section.RelocationTableOffset = Sect->RelocationTableOffset;
193     Section.NumRelocationTableEntries = Sect->NumRelocationTableEntries;
194     Section.Flags       = Sect->Flags;
195     Section.Reserved1   = Sect->Reserved1;
196     Section.Reserved2   = Sect->Reserved2;
197   }
198 }
199
200 static void getSymbolTableEntry(const MachOObject *MachO,
201                                 DataRefImpl DRI,
202                                 InMemoryStruct<macho::SymbolTableEntry> &Res) {
203   InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
204   LoadCommandInfo LCI = MachO->getLoadCommandInfo(DRI.d.a);
205   MachO->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
206   MachO->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b, Res);
207 }
208
209 static void getSymbol64TableEntry(const MachOObject *MachO,
210                                   DataRefImpl DRI,
211                                InMemoryStruct<macho::Symbol64TableEntry> &Res) {
212   InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
213   LoadCommandInfo LCI = MachO->getLoadCommandInfo(DRI.d.a);
214   MachO->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
215   MachO->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b, Res);
216 }
217
218 static void getSymbol(const MachOObject *MachOObj,
219                       DataRefImpl DRI,
220                       MachOSymbol &Symbol) {
221   if (MachOObj->is64Bit()) {
222     InMemoryStruct<macho::Symbol64TableEntry> Entry;
223     getSymbol64TableEntry(MachOObj, DRI, Entry);
224     Symbol.StringIndex  = Entry->StringIndex;
225     Symbol.Type         = Entry->Type;
226     Symbol.SectionIndex = Entry->SectionIndex;
227     Symbol.Flags        = Entry->Flags;
228     Symbol.Value        = Entry->Value;
229   } else {
230     InMemoryStruct<macho::SymbolTableEntry> Entry;
231     getSymbolTableEntry(MachOObj, DRI, Entry);
232     Symbol.StringIndex  = Entry->StringIndex;
233     Symbol.Type         = Entry->Type;
234     Symbol.SectionIndex = Entry->SectionIndex;
235     Symbol.Flags        = Entry->Flags;
236     Symbol.Value        = Entry->Value;
237   }
238 }
239
240 void MachODumper::printFileHeaders() {
241   W.startLine() << "FileHeaders not implemented.\n";
242 }
243
244 void MachODumper::printSections() {
245   ListScope Group(W, "Sections");
246
247   int SectionIndex = -1;
248   error_code EC;
249   for (section_iterator SecI = Obj->begin_sections(),
250                         SecE = Obj->end_sections();
251                         SecI != SecE; SecI.increment(EC)) {
252     if (error(EC)) break;
253
254     ++SectionIndex;
255
256     MachOSection Section;
257     getSection(Obj, SecI->getRawDataRefImpl(), Section);
258     DataRefImpl DR = SecI->getRawDataRefImpl();
259
260     StringRef Name;
261     if (error(SecI->getName(Name)))
262         Name = "";
263
264     ArrayRef<char> RawName = Obj->getSectionRawName(DR);
265     StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
266     ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
267
268     DictScope SectionD(W, "Section");
269     W.printNumber("Index", SectionIndex);
270     W.printBinary("Name", Name, RawName);
271     W.printBinary("Segment", SegmentName, RawSegmentName);
272     W.printHex   ("Address", Section.Address);
273     W.printHex   ("Size", Section.Size);
274     W.printNumber("Offset", Section.Offset);
275     W.printNumber("Alignment", Section.Alignment);
276     W.printHex   ("RelocationOffset", Section.RelocationTableOffset);
277     W.printNumber("RelocationCount", Section.NumRelocationTableEntries);
278     W.printEnum  ("Type", Section.Flags & 0xFF,
279                   makeArrayRef(MachOSectionAttributes));
280     W.printFlags ("Attributes", Section.Flags >> 8,
281                   makeArrayRef(MachOSectionAttributes));
282     W.printHex   ("Reserved1", Section.Reserved1);
283     W.printHex   ("Reserved2", Section.Reserved2);
284
285     if (opts::SectionRelocations) {
286       ListScope D(W, "Relocations");
287       for (relocation_iterator RelI = SecI->begin_relocations(),
288                                RelE = SecI->end_relocations();
289                                RelI != RelE; RelI.increment(EC)) {
290         if (error(EC)) break;
291
292         printRelocation(SecI, RelI);
293       }
294     }
295
296     if (opts::SectionSymbols) {
297       ListScope D(W, "Symbols");
298       for (symbol_iterator SymI = Obj->begin_symbols(),
299                            SymE = Obj->end_symbols();
300                            SymI != SymE; SymI.increment(EC)) {
301         if (error(EC)) break;
302
303         bool Contained = false;
304         if (SecI->containsSymbol(*SymI, Contained) || !Contained)
305           continue;
306
307         printSymbol(SymI);
308       }
309     }
310
311     if (opts::SectionData) {
312       StringRef Data;
313       if (error(SecI->getContents(Data))) break;
314
315       W.printBinaryBlock("SectionData", Data);
316     }
317   }
318 }
319
320 void MachODumper::printRelocations() {
321   ListScope D(W, "Relocations");
322
323   error_code EC;
324   for (section_iterator SecI = Obj->begin_sections(),
325                         SecE = Obj->end_sections();
326                         SecI != SecE; SecI.increment(EC)) {
327     if (error(EC)) break;
328
329     StringRef Name;
330     if (error(SecI->getName(Name)))
331       continue;
332
333     bool PrintedGroup = false;
334     for (relocation_iterator RelI = SecI->begin_relocations(),
335                              RelE = SecI->end_relocations();
336                              RelI != RelE; RelI.increment(EC)) {
337       if (error(EC)) break;
338
339       if (!PrintedGroup) {
340         W.startLine() << "Section " << Name << " {\n";
341         W.indent();
342         PrintedGroup = true;
343       }
344
345       printRelocation(SecI, RelI);
346     }
347
348     if (PrintedGroup) {
349       W.unindent();
350       W.startLine() << "}\n";
351     }
352   }
353 }
354
355 void MachODumper::printRelocation(section_iterator SecI,
356                                   relocation_iterator RelI) {
357   uint64_t Offset;
358   SmallString<32> RelocName;
359   int64_t Info;
360   StringRef SymbolName;
361   SymbolRef Symbol;
362   if (error(RelI->getOffset(Offset))) return;
363   if (error(RelI->getTypeName(RelocName))) return;
364   if (error(RelI->getAdditionalInfo(Info))) return;
365   if (error(RelI->getSymbol(Symbol))) return;
366   if (error(Symbol.getName(SymbolName))) return;
367
368   raw_ostream& OS = W.startLine();
369   OS << W.hex(Offset)
370      << " " << RelocName
371      << " " << (SymbolName.size() > 0 ? SymbolName : "-")
372      << " " << W.hex(Info)
373      << "\n";
374 }
375
376 void MachODumper::printSymbols() {
377   ListScope Group(W, "Symbols");
378
379   error_code EC;
380   for (symbol_iterator SymI = Obj->begin_symbols(),
381                        SymE = Obj->end_symbols();
382                        SymI != SymE; SymI.increment(EC)) {
383     if (error(EC)) break;
384
385     printSymbol(SymI);
386   }
387 }
388
389 void MachODumper::printDynamicSymbols() {
390   ListScope Group(W, "DynamicSymbols");
391 }
392
393 void MachODumper::printSymbol(symbol_iterator SymI) {
394   error_code EC;
395
396   StringRef SymbolName;
397   if (SymI->getName(SymbolName))
398     SymbolName = "";
399
400   const MachOObject *MachO = const_cast<MachOObjectFile*>(Obj)->getObject();
401
402   MachOSymbol Symbol;
403   getSymbol(MachO, SymI->getRawDataRefImpl(), Symbol);
404
405   StringRef SectionName;
406   section_iterator SecI(Obj->end_sections());
407   if (error(SymI->getSection(SecI)) ||
408       error(SecI->getName(SectionName)))
409     SectionName = "";
410
411   DictScope D(W, "Symbol");
412   W.printNumber("Name", SymbolName, Symbol.StringIndex);
413   if (Symbol.Type & N_STAB) {
414     W.printHex ("Type", "SymDebugTable", Symbol.Type);
415   } else {
416     W.printEnum("Type", Symbol.Type, makeArrayRef(MachOSymbolTypes));
417   }
418   W.printHex   ("Section", SectionName, Symbol.SectionIndex);
419   W.printEnum  ("RefType", static_cast<uint16_t>(Symbol.Flags & 0xF),
420                   makeArrayRef(MachOSymbolRefTypes));
421   W.printFlags ("Flags", static_cast<uint16_t>(Symbol.Flags & ~0xF),
422                   makeArrayRef(MachOSymbolFlags));
423   W.printHex   ("Value", Symbol.Value);
424 }
425
426 void MachODumper::printUnwindInfo() {
427   W.startLine() << "UnwindInfo not implemented.\n";
428 }