df9c2cf2dac94d35270365072bfd00d3c98a70ac
[oota-llvm.git] / tools / llvm-objdump / COFFDump.cpp
1 //===-- COFFDump.cpp - COFF-specific dumper ---------------------*- C++ -*-===//
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 /// \file
11 /// \brief This file implements the COFF-specific dumper for llvm-objdump.
12 /// It outputs the Win64 EH data structures as plain text.
13 /// The encoding of the unwind codes is decribed in MSDN:
14 /// http://msdn.microsoft.com/en-us/library/ck9asaa9.aspx
15 ///
16 //===----------------------------------------------------------------------===//
17
18 #include "llvm-objdump.h"
19 #include "llvm/Object/COFF.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Support/Win64EH.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/system_error.h"
26 #include <algorithm>
27 #include <cstring>
28
29 using namespace llvm;
30 using namespace object;
31 using namespace llvm::Win64EH;
32
33 // Returns the name of the unwind code.
34 static StringRef getUnwindCodeTypeName(uint8_t Code) {
35   switch(Code) {
36   default: llvm_unreachable("Invalid unwind code");
37   case UOP_PushNonVol: return "UOP_PushNonVol";
38   case UOP_AllocLarge: return "UOP_AllocLarge";
39   case UOP_AllocSmall: return "UOP_AllocSmall";
40   case UOP_SetFPReg: return "UOP_SetFPReg";
41   case UOP_SaveNonVol: return "UOP_SaveNonVol";
42   case UOP_SaveNonVolBig: return "UOP_SaveNonVolBig";
43   case UOP_SaveXMM128: return "UOP_SaveXMM128";
44   case UOP_SaveXMM128Big: return "UOP_SaveXMM128Big";
45   case UOP_PushMachFrame: return "UOP_PushMachFrame";
46   }
47 }
48
49 // Returns the name of a referenced register.
50 static StringRef getUnwindRegisterName(uint8_t Reg) {
51   switch(Reg) {
52   default: llvm_unreachable("Invalid register");
53   case 0: return "RAX";
54   case 1: return "RCX";
55   case 2: return "RDX";
56   case 3: return "RBX";
57   case 4: return "RSP";
58   case 5: return "RBP";
59   case 6: return "RSI";
60   case 7: return "RDI";
61   case 8: return "R8";
62   case 9: return "R9";
63   case 10: return "R10";
64   case 11: return "R11";
65   case 12: return "R12";
66   case 13: return "R13";
67   case 14: return "R14";
68   case 15: return "R15";
69   }
70 }
71
72 // Calculates the number of array slots required for the unwind code.
73 static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) {
74   switch (UnwindCode.getUnwindOp()) {
75   default: llvm_unreachable("Invalid unwind code");
76   case UOP_PushNonVol:
77   case UOP_AllocSmall:
78   case UOP_SetFPReg:
79   case UOP_PushMachFrame:
80     return 1;
81   case UOP_SaveNonVol:
82   case UOP_SaveXMM128:
83     return 2;
84   case UOP_SaveNonVolBig:
85   case UOP_SaveXMM128Big:
86     return 3;
87   case UOP_AllocLarge:
88     return (UnwindCode.getOpInfo() == 0) ? 2 : 3;
89   }
90 }
91
92 // Prints one unwind code. Because an unwind code can occupy up to 3 slots in
93 // the unwind codes array, this function requires that the correct number of
94 // slots is provided.
95 static void printUnwindCode(ArrayRef<UnwindCode> UCs) {
96   assert(UCs.size() >= getNumUsedSlots(UCs[0]));
97   outs() <<  format("    0x%02x: ", unsigned(UCs[0].u.CodeOffset))
98          << getUnwindCodeTypeName(UCs[0].getUnwindOp());
99   switch (UCs[0].getUnwindOp()) {
100   case UOP_PushNonVol:
101     outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo());
102     break;
103   case UOP_AllocLarge:
104     if (UCs[0].getOpInfo() == 0) {
105       outs() << " " << UCs[1].FrameOffset;
106     } else {
107       outs() << " " << UCs[1].FrameOffset
108                        + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16);
109     }
110     break;
111   case UOP_AllocSmall:
112     outs() << " " << ((UCs[0].getOpInfo() + 1) * 8);
113     break;
114   case UOP_SetFPReg:
115     outs() << " ";
116     break;
117   case UOP_SaveNonVol:
118     outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
119            << format(" [0x%04x]", 8 * UCs[1].FrameOffset);
120     break;
121   case UOP_SaveNonVolBig:
122     outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
123            << format(" [0x%08x]", UCs[1].FrameOffset
124                     + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16));
125     break;
126   case UOP_SaveXMM128:
127     outs() << " XMM" << static_cast<uint32_t>(UCs[0].getOpInfo())
128            << format(" [0x%04x]", 16 * UCs[1].FrameOffset);
129     break;
130   case UOP_SaveXMM128Big:
131     outs() << " XMM" << UCs[0].getOpInfo()
132            << format(" [0x%08x]", UCs[1].FrameOffset
133                            + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16));
134     break;
135   case UOP_PushMachFrame:
136     outs() << " " << (UCs[0].getOpInfo() ? "w/o" : "w")
137            << " error code";
138     break;
139   }
140   outs() << "\n";
141 }
142
143 static void printAllUnwindCodes(ArrayRef<UnwindCode> UCs) {
144   for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ) {
145     unsigned UsedSlots = getNumUsedSlots(*I);
146     if (UsedSlots > UCs.size()) {
147       outs() << "Unwind data corrupted: Encountered unwind op "
148              << getUnwindCodeTypeName((*I).getUnwindOp())
149              << " which requires " << UsedSlots
150              << " slots, but only " << UCs.size()
151              << " remaining in buffer";
152       return ;
153     }
154     printUnwindCode(ArrayRef<UnwindCode>(I, E));
155     I += UsedSlots;
156   }
157 }
158
159 // Given a symbol sym this functions returns the address and section of it.
160 static error_code resolveSectionAndAddress(const COFFObjectFile *Obj,
161                                            const SymbolRef &Sym,
162                                            const coff_section *&ResolvedSection,
163                                            uint64_t &ResolvedAddr) {
164   if (error_code ec = Sym.getAddress(ResolvedAddr)) return ec;
165   section_iterator iter(Obj->begin_sections());
166   if (error_code ec = Sym.getSection(iter)) return ec;
167   ResolvedSection = Obj->getCOFFSection(iter);
168   return object_error::success;
169 }
170
171 // Given a vector of relocations for a section and an offset into this section
172 // the function returns the symbol used for the relocation at the offset.
173 static error_code resolveSymbol(const std::vector<RelocationRef> &Rels,
174                                 uint64_t Offset, SymbolRef &Sym) {
175   for (std::vector<RelocationRef>::const_iterator I = Rels.begin(),
176                                                   E = Rels.end();
177                                                   I != E; ++I) {
178     uint64_t Ofs;
179     if (error_code ec = I->getOffset(Ofs)) return ec;
180     if (Ofs == Offset) {
181       Sym = *I->getSymbol();
182       break;
183     }
184   }
185   return object_error::success;
186 }
187
188 // Given a vector of relocations for a section and an offset into this section
189 // the function resolves the symbol used for the relocation at the offset and
190 // returns the section content and the address inside the content pointed to
191 // by the symbol.
192 static error_code getSectionContents(const COFFObjectFile *Obj,
193                                      const std::vector<RelocationRef> &Rels,
194                                      uint64_t Offset,
195                                      ArrayRef<uint8_t> &Contents,
196                                      uint64_t &Addr) {
197   SymbolRef Sym;
198   if (error_code ec = resolveSymbol(Rels, Offset, Sym)) return ec;
199   const coff_section *Section;
200   if (error_code ec = resolveSectionAndAddress(Obj, Sym, Section, Addr))
201     return ec;
202   if (error_code ec = Obj->getSectionContents(Section, Contents)) return ec;
203   return object_error::success;
204 }
205
206 // Given a vector of relocations for a section and an offset into this section
207 // the function returns the name of the symbol used for the relocation at the
208 // offset.
209 static error_code resolveSymbolName(const std::vector<RelocationRef> &Rels,
210                                     uint64_t Offset, StringRef &Name) {
211   SymbolRef Sym;
212   if (error_code ec = resolveSymbol(Rels, Offset, Sym)) return ec;
213   if (error_code ec = Sym.getName(Name)) return ec;
214   return object_error::success;
215 }
216
217 static void printCOFFSymbolAddress(llvm::raw_ostream &Out,
218                                    const std::vector<RelocationRef> &Rels,
219                                    uint64_t Offset, uint32_t Disp) {
220   StringRef Sym;
221   if (error_code ec = resolveSymbolName(Rels, Offset, Sym)) {
222     error(ec);
223     return ;
224   }
225   Out << Sym;
226   if (Disp > 0)
227     Out << format(" + 0x%04x", Disp);
228 }
229
230 // Prints import tables. The import table is a table containing the list of
231 // DLL name and symbol names which will be linked by the loader.
232 static void printImportTables(const COFFObjectFile *Obj) {
233   import_directory_iterator i = Obj->import_directory_begin();
234   import_directory_iterator e = Obj->import_directory_end();
235   if (i == e)
236     return;
237   outs() << "The Import Tables:\n";
238   error_code ec;
239   for (; i != e; i = i.increment(ec)) {
240     if (ec)
241       return;
242
243     const import_directory_table_entry *Dir;
244     StringRef Name;
245     if (i->getImportTableEntry(Dir)) return;
246     if (i->getName(Name)) return;
247
248     outs() << format("  lookup %08x time %08x fwd %08x name %08x addr %08x\n\n",
249                      static_cast<uint32_t>(Dir->ImportLookupTableRVA),
250                      static_cast<uint32_t>(Dir->TimeDateStamp),
251                      static_cast<uint32_t>(Dir->ForwarderChain),
252                      static_cast<uint32_t>(Dir->NameRVA),
253                      static_cast<uint32_t>(Dir->ImportAddressTableRVA));
254     outs() << "    DLL Name: " << Name << "\n";
255     outs() << "    Hint/Ord  Name\n";
256     const import_lookup_table_entry32 *entry;
257     if (i->getImportLookupEntry(entry))
258       return;
259     for (; entry->data; ++entry) {
260       if (entry->isOrdinal()) {
261         outs() << format("      % 6d\n", entry->getOrdinal());
262         continue;
263       }
264       uint16_t Hint;
265       StringRef Name;
266       if (Obj->getHintName(entry->getHintNameRVA(), Hint, Name))
267         return;
268       outs() << format("      % 6d  ", Hint) << Name << "\n";
269     }
270     outs() << "\n";
271   }
272 }
273
274 // Prints export tables. The export table is a table containing the list of
275 // exported symbol from the DLL.
276 static void printExportTable(const COFFObjectFile *Obj) {
277   outs() << "Export Table:\n";
278   export_directory_iterator I = Obj->export_directory_begin();
279   export_directory_iterator E = Obj->export_directory_end();
280   if (I == E)
281     return;
282   outs() << " Ordinal      RVA  Name\n";
283   error_code EC;
284   for (; I != E; I = I.increment(EC)) {
285     if (EC)
286       return;
287     uint32_t Ordinal;
288     if (I->getOrdinal(Ordinal))
289       return;
290     uint32_t RVA;
291     if (I->getExportRVA(RVA))
292       return;
293     outs() << format("    % 4d %# 8x", Ordinal, RVA);
294
295     StringRef Name;
296     if (I->getName(Name))
297       continue;
298     if (!Name.empty())
299       outs() << "  " << Name;
300     outs() << "\n";
301   }
302 }
303
304 void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) {
305   const coff_file_header *Header;
306   if (error(Obj->getCOFFHeader(Header))) return;
307
308   if (Header->Machine != COFF::IMAGE_FILE_MACHINE_AMD64) {
309     errs() << "Unsupported image machine type "
310               "(currently only AMD64 is supported).\n";
311     return;
312   }
313
314   const coff_section *Pdata = 0;
315
316   error_code ec;
317   for (section_iterator SI = Obj->begin_sections(),
318                         SE = Obj->end_sections();
319                         SI != SE; SI.increment(ec)) {
320     if (error(ec)) return;
321
322     StringRef Name;
323     if (error(SI->getName(Name))) continue;
324
325     if (Name != ".pdata") continue;
326
327     Pdata = Obj->getCOFFSection(SI);
328     std::vector<RelocationRef> Rels;
329     for (relocation_iterator RI = SI->begin_relocations(),
330                              RE = SI->end_relocations();
331                              RI != RE; RI.increment(ec)) {
332       if (error(ec)) break;
333       Rels.push_back(*RI);
334     }
335
336     // Sort relocations by address.
337     std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
338
339     ArrayRef<uint8_t> Contents;
340     if (error(Obj->getSectionContents(Pdata, Contents))) continue;
341     if (Contents.empty()) continue;
342
343     ArrayRef<RuntimeFunction> RFs(
344                   reinterpret_cast<const RuntimeFunction *>(Contents.data()),
345                                   Contents.size() / sizeof(RuntimeFunction));
346     for (const RuntimeFunction *I = RFs.begin(), *E = RFs.end(); I < E; ++I) {
347       const uint64_t SectionOffset = std::distance(RFs.begin(), I)
348                                      * sizeof(RuntimeFunction);
349
350       outs() << "Function Table:\n";
351
352       outs() << "  Start Address: ";
353       printCOFFSymbolAddress(outs(), Rels, SectionOffset +
354                              /*offsetof(RuntimeFunction, StartAddress)*/ 0,
355                              I->StartAddress);
356       outs() << "\n";
357
358       outs() << "  End Address: ";
359       printCOFFSymbolAddress(outs(), Rels, SectionOffset +
360                              /*offsetof(RuntimeFunction, EndAddress)*/ 4,
361                              I->EndAddress);
362       outs() << "\n";
363
364       outs() << "  Unwind Info Address: ";
365       printCOFFSymbolAddress(outs(), Rels, SectionOffset +
366                              /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
367                              I->UnwindInfoOffset);
368       outs() << "\n";
369
370       ArrayRef<uint8_t> XContents;
371       uint64_t UnwindInfoOffset = 0;
372       if (error(getSectionContents(Obj, Rels, SectionOffset +
373                               /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
374                                    XContents, UnwindInfoOffset))) continue;
375       if (XContents.empty()) continue;
376
377       UnwindInfoOffset += I->UnwindInfoOffset;
378       if (UnwindInfoOffset > XContents.size()) continue;
379
380       const Win64EH::UnwindInfo *UI =
381                             reinterpret_cast<const Win64EH::UnwindInfo *>
382                               (XContents.data() + UnwindInfoOffset);
383
384       // The casts to int are required in order to output the value as number.
385       // Without the casts the value would be interpreted as char data (which
386       // results in garbage output).
387       outs() << "  Version: " << static_cast<int>(UI->getVersion()) << "\n";
388       outs() << "  Flags: " << static_cast<int>(UI->getFlags());
389       if (UI->getFlags()) {
390           if (UI->getFlags() & UNW_ExceptionHandler)
391             outs() << " UNW_ExceptionHandler";
392           if (UI->getFlags() & UNW_TerminateHandler)
393             outs() << " UNW_TerminateHandler";
394           if (UI->getFlags() & UNW_ChainInfo)
395             outs() << " UNW_ChainInfo";
396       }
397       outs() << "\n";
398       outs() << "  Size of prolog: "
399              << static_cast<int>(UI->PrologSize) << "\n";
400       outs() << "  Number of Codes: "
401              << static_cast<int>(UI->NumCodes) << "\n";
402       // Maybe this should move to output of UOP_SetFPReg?
403       if (UI->getFrameRegister()) {
404         outs() << "  Frame register: "
405                 << getUnwindRegisterName(UI->getFrameRegister())
406                 << "\n";
407         outs() << "  Frame offset: "
408                 << 16 * UI->getFrameOffset()
409                 << "\n";
410       } else {
411         outs() << "  No frame pointer used\n";
412       }
413       if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
414         // FIXME: Output exception handler data
415       } else if (UI->getFlags() & UNW_ChainInfo) {
416         // FIXME: Output chained unwind info
417       }
418
419       if (UI->NumCodes)
420         outs() << "  Unwind Codes:\n";
421
422       printAllUnwindCodes(ArrayRef<UnwindCode>(&UI->UnwindCodes[0],
423                           UI->NumCodes));
424
425       outs() << "\n\n";
426       outs().flush();
427     }
428   }
429 }
430
431 void llvm::printCOFFFileHeader(const object::ObjectFile *Obj) {
432   const COFFObjectFile *file = dyn_cast<const COFFObjectFile>(Obj);
433   printImportTables(file);
434   printExportTable(file);
435 }