blob: bb997e0e621ac7db39b879b54dfc8703b46842f6 [file] [log] [blame]
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001//===-- 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"
Chandler Carruthb034cb72013-01-02 10:26:28 +000023#include "llvm/Support/Win64EH.h"
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000024#include "llvm/Support/raw_ostream.h"
25#include "llvm/Support/system_error.h"
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000026#include <algorithm>
27#include <cstring>
28
29using namespace llvm;
30using namespace object;
31using namespace llvm::Win64EH;
32
33// Returns the name of the unwind code.
34static 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.
50static 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.
73static 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.
95static 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
143static 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.
160static error_code resolveSectionAndAddress(const COFFObjectFile *Obj,
161 const SymbolRef &Sym,
162 const coff_section *&ResolvedSection,
163 uint64_t &ResolvedAddr) {
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000164 if (error_code EC = Sym.getAddress(ResolvedAddr))
165 return EC;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000166 section_iterator iter(Obj->begin_sections());
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000167 if (error_code EC = Sym.getSection(iter))
168 return EC;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000169 ResolvedSection = Obj->getCOFFSection(iter);
170 return object_error::success;
171}
172
173// Given a vector of relocations for a section and an offset into this section
174// the function returns the symbol used for the relocation at the offset.
175static error_code resolveSymbol(const std::vector<RelocationRef> &Rels,
176 uint64_t Offset, SymbolRef &Sym) {
177 for (std::vector<RelocationRef>::const_iterator I = Rels.begin(),
178 E = Rels.end();
179 I != E; ++I) {
180 uint64_t Ofs;
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000181 if (error_code EC = I->getOffset(Ofs))
182 return EC;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000183 if (Ofs == Offset) {
Rafael Espindola806f0062013-06-05 01:33:53 +0000184 Sym = *I->getSymbol();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000185 break;
186 }
187 }
188 return object_error::success;
189}
190
191// Given a vector of relocations for a section and an offset into this section
192// the function resolves the symbol used for the relocation at the offset and
193// returns the section content and the address inside the content pointed to
194// by the symbol.
195static error_code getSectionContents(const COFFObjectFile *Obj,
196 const std::vector<RelocationRef> &Rels,
197 uint64_t Offset,
198 ArrayRef<uint8_t> &Contents,
199 uint64_t &Addr) {
200 SymbolRef Sym;
201 if (error_code ec = resolveSymbol(Rels, Offset, Sym)) return ec;
202 const coff_section *Section;
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000203 if (error_code EC = resolveSectionAndAddress(Obj, Sym, Section, Addr))
204 return EC;
205 if (error_code EC = Obj->getSectionContents(Section, Contents))
206 return EC;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000207 return object_error::success;
208}
209
210// Given a vector of relocations for a section and an offset into this section
211// the function returns the name of the symbol used for the relocation at the
212// offset.
213static error_code resolveSymbolName(const std::vector<RelocationRef> &Rels,
214 uint64_t Offset, StringRef &Name) {
215 SymbolRef Sym;
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000216 if (error_code EC = resolveSymbol(Rels, Offset, Sym))
217 return EC;
218 if (error_code EC = Sym.getName(Name))
219 return EC;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000220 return object_error::success;
221}
222
223static void printCOFFSymbolAddress(llvm::raw_ostream &Out,
224 const std::vector<RelocationRef> &Rels,
225 uint64_t Offset, uint32_t Disp) {
226 StringRef Sym;
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000227 if (error_code EC = resolveSymbolName(Rels, Offset, Sym)) {
228 error(EC);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000229 return ;
230 }
231 Out << Sym;
232 if (Disp > 0)
233 Out << format(" + 0x%04x", Disp);
234}
235
Rui Ueyamac2bed422013-09-27 21:04:00 +0000236// Prints import tables. The import table is a table containing the list of
237// DLL name and symbol names which will be linked by the loader.
238static void printImportTables(const COFFObjectFile *Obj) {
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000239 import_directory_iterator I = Obj->import_directory_begin();
240 import_directory_iterator E = Obj->import_directory_end();
241 if (I == E)
Rui Ueyama908dfcd2014-01-15 23:46:18 +0000242 return;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000243 outs() << "The Import Tables:\n";
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000244 error_code EC;
245 for (; I != E; I = I.increment(EC)) {
246 if (EC)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000247 return;
248
249 const import_directory_table_entry *Dir;
250 StringRef Name;
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000251 if (I->getImportTableEntry(Dir)) return;
252 if (I->getName(Name)) return;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000253
254 outs() << format(" lookup %08x time %08x fwd %08x name %08x addr %08x\n\n",
255 static_cast<uint32_t>(Dir->ImportLookupTableRVA),
256 static_cast<uint32_t>(Dir->TimeDateStamp),
257 static_cast<uint32_t>(Dir->ForwarderChain),
258 static_cast<uint32_t>(Dir->NameRVA),
259 static_cast<uint32_t>(Dir->ImportAddressTableRVA));
260 outs() << " DLL Name: " << Name << "\n";
261 outs() << " Hint/Ord Name\n";
262 const import_lookup_table_entry32 *entry;
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000263 if (I->getImportLookupEntry(entry))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000264 return;
265 for (; entry->data; ++entry) {
266 if (entry->isOrdinal()) {
267 outs() << format(" % 6d\n", entry->getOrdinal());
268 continue;
269 }
270 uint16_t Hint;
271 StringRef Name;
272 if (Obj->getHintName(entry->getHintNameRVA(), Hint, Name))
273 return;
274 outs() << format(" % 6d ", Hint) << Name << "\n";
275 }
276 outs() << "\n";
277 }
278}
279
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000280// Prints export tables. The export table is a table containing the list of
281// exported symbol from the DLL.
282static void printExportTable(const COFFObjectFile *Obj) {
283 outs() << "Export Table:\n";
284 export_directory_iterator I = Obj->export_directory_begin();
285 export_directory_iterator E = Obj->export_directory_end();
286 if (I == E)
287 return;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000288 StringRef DllName;
289 if (I->getDllName(DllName))
290 return;
291 outs() << " DLL name: " << DllName << "\n";
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000292 outs() << " Ordinal RVA Name\n";
293 error_code EC;
294 for (; I != E; I = I.increment(EC)) {
295 if (EC)
296 return;
297 uint32_t Ordinal;
298 if (I->getOrdinal(Ordinal))
299 return;
300 uint32_t RVA;
301 if (I->getExportRVA(RVA))
302 return;
303 outs() << format(" % 4d %# 8x", Ordinal, RVA);
304
305 StringRef Name;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000306 if (I->getSymbolName(Name))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000307 continue;
308 if (!Name.empty())
309 outs() << " " << Name;
310 outs() << "\n";
311 }
312}
313
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000314void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) {
315 const coff_file_header *Header;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000316 if (error(Obj->getCOFFHeader(Header))) return;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000317
318 if (Header->Machine != COFF::IMAGE_FILE_MACHINE_AMD64) {
319 errs() << "Unsupported image machine type "
320 "(currently only AMD64 is supported).\n";
321 return;
322 }
323
324 const coff_section *Pdata = 0;
325
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000326 error_code EC;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000327 for (section_iterator SI = Obj->begin_sections(),
328 SE = Obj->end_sections();
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000329 SI != SE; SI.increment(EC)) {
330 if (error(EC)) return;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000331
332 StringRef Name;
333 if (error(SI->getName(Name))) continue;
334
335 if (Name != ".pdata") continue;
336
337 Pdata = Obj->getCOFFSection(SI);
338 std::vector<RelocationRef> Rels;
339 for (relocation_iterator RI = SI->begin_relocations(),
340 RE = SI->end_relocations();
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000341 RI != RE; RI.increment(EC)) {
342 if (error(EC)) break;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000343 Rels.push_back(*RI);
344 }
345
346 // Sort relocations by address.
347 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
348
349 ArrayRef<uint8_t> Contents;
350 if (error(Obj->getSectionContents(Pdata, Contents))) continue;
351 if (Contents.empty()) continue;
352
353 ArrayRef<RuntimeFunction> RFs(
354 reinterpret_cast<const RuntimeFunction *>(Contents.data()),
355 Contents.size() / sizeof(RuntimeFunction));
356 for (const RuntimeFunction *I = RFs.begin(), *E = RFs.end(); I < E; ++I) {
357 const uint64_t SectionOffset = std::distance(RFs.begin(), I)
358 * sizeof(RuntimeFunction);
359
360 outs() << "Function Table:\n";
361
362 outs() << " Start Address: ";
363 printCOFFSymbolAddress(outs(), Rels, SectionOffset +
Michael J. Spencer6fa518c2012-12-05 22:38:01 +0000364 /*offsetof(RuntimeFunction, StartAddress)*/ 0,
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000365 I->StartAddress);
366 outs() << "\n";
367
368 outs() << " End Address: ";
369 printCOFFSymbolAddress(outs(), Rels, SectionOffset +
Michael J. Spencer6fa518c2012-12-05 22:38:01 +0000370 /*offsetof(RuntimeFunction, EndAddress)*/ 4,
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000371 I->EndAddress);
372 outs() << "\n";
373
374 outs() << " Unwind Info Address: ";
375 printCOFFSymbolAddress(outs(), Rels, SectionOffset +
Michael J. Spencer6fa518c2012-12-05 22:38:01 +0000376 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000377 I->UnwindInfoOffset);
378 outs() << "\n";
379
380 ArrayRef<uint8_t> XContents;
381 uint64_t UnwindInfoOffset = 0;
382 if (error(getSectionContents(Obj, Rels, SectionOffset +
Michael J. Spencer6fa518c2012-12-05 22:38:01 +0000383 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000384 XContents, UnwindInfoOffset))) continue;
385 if (XContents.empty()) continue;
386
387 UnwindInfoOffset += I->UnwindInfoOffset;
388 if (UnwindInfoOffset > XContents.size()) continue;
389
390 const Win64EH::UnwindInfo *UI =
391 reinterpret_cast<const Win64EH::UnwindInfo *>
392 (XContents.data() + UnwindInfoOffset);
393
394 // The casts to int are required in order to output the value as number.
395 // Without the casts the value would be interpreted as char data (which
396 // results in garbage output).
397 outs() << " Version: " << static_cast<int>(UI->getVersion()) << "\n";
398 outs() << " Flags: " << static_cast<int>(UI->getFlags());
399 if (UI->getFlags()) {
400 if (UI->getFlags() & UNW_ExceptionHandler)
401 outs() << " UNW_ExceptionHandler";
402 if (UI->getFlags() & UNW_TerminateHandler)
403 outs() << " UNW_TerminateHandler";
404 if (UI->getFlags() & UNW_ChainInfo)
405 outs() << " UNW_ChainInfo";
406 }
407 outs() << "\n";
408 outs() << " Size of prolog: "
409 << static_cast<int>(UI->PrologSize) << "\n";
410 outs() << " Number of Codes: "
411 << static_cast<int>(UI->NumCodes) << "\n";
412 // Maybe this should move to output of UOP_SetFPReg?
413 if (UI->getFrameRegister()) {
414 outs() << " Frame register: "
415 << getUnwindRegisterName(UI->getFrameRegister())
416 << "\n";
417 outs() << " Frame offset: "
418 << 16 * UI->getFrameOffset()
419 << "\n";
420 } else {
421 outs() << " No frame pointer used\n";
422 }
423 if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
424 // FIXME: Output exception handler data
425 } else if (UI->getFlags() & UNW_ChainInfo) {
426 // FIXME: Output chained unwind info
427 }
428
429 if (UI->NumCodes)
430 outs() << " Unwind Codes:\n";
431
432 printAllUnwindCodes(ArrayRef<UnwindCode>(&UI->UnwindCodes[0],
433 UI->NumCodes));
434
435 outs() << "\n\n";
436 outs().flush();
437 }
438 }
439}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000440
441void llvm::printCOFFFileHeader(const object::ObjectFile *Obj) {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000442 const COFFObjectFile *file = dyn_cast<const COFFObjectFile>(Obj);
443 printImportTables(file);
444 printExportTable(file);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000445}