blob: e6e8367f29341c041ac8f7d67540fec507b6a0de [file] [log] [blame]
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +00001//===- Win64EHDumper.cpp - Win64 EH Printer ---------------------*- 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#include "Win64EHDumper.h"
11#include "llvm-readobj.h"
12#include "llvm/Object/COFF.h"
13#include "llvm/Support/ErrorHandling.h"
14#include "llvm/Support/Format.h"
15
16using namespace llvm;
17using namespace llvm::object;
18using namespace llvm::Win64EH;
19
20static const EnumEntry<unsigned> UnwindFlags[] = {
21 { "ExceptionHandler", UNW_ExceptionHandler },
22 { "TerminateHandler", UNW_TerminateHandler },
23 { "ChainInfo" , UNW_ChainInfo }
24};
25
26static const EnumEntry<unsigned> UnwindOpInfo[] = {
27 { "RAX", 0 },
28 { "RCX", 1 },
29 { "RDX", 2 },
30 { "RBX", 3 },
31 { "RSP", 4 },
32 { "RBP", 5 },
33 { "RSI", 6 },
34 { "RDI", 7 },
35 { "R8", 8 },
36 { "R9", 9 },
37 { "R10", 10 },
38 { "R11", 11 },
39 { "R12", 12 },
40 { "R13", 13 },
41 { "R14", 14 },
42 { "R15", 15 }
43};
44
45static uint64_t getOffsetOfLSDA(const UnwindInfo& UI) {
46 return static_cast<const char*>(UI.getLanguageSpecificData())
47 - reinterpret_cast<const char*>(&UI);
48}
49
50static uint32_t getLargeSlotValue(ArrayRef<UnwindCode> UC) {
51 if (UC.size() < 3)
52 return 0;
53 return UC[1].FrameOffset + (static_cast<uint32_t>(UC[2].FrameOffset) << 16);
54}
55
56// Returns the name of the unwind code.
57static StringRef getUnwindCodeTypeName(uint8_t Code) {
58 switch (Code) {
59 default: llvm_unreachable("Invalid unwind code");
60 case UOP_PushNonVol: return "PUSH_NONVOL";
61 case UOP_AllocLarge: return "ALLOC_LARGE";
62 case UOP_AllocSmall: return "ALLOC_SMALL";
63 case UOP_SetFPReg: return "SET_FPREG";
64 case UOP_SaveNonVol: return "SAVE_NONVOL";
65 case UOP_SaveNonVolBig: return "SAVE_NONVOL_FAR";
66 case UOP_SaveXMM128: return "SAVE_XMM128";
67 case UOP_SaveXMM128Big: return "SAVE_XMM128_FAR";
68 case UOP_PushMachFrame: return "PUSH_MACHFRAME";
69 }
70}
71
72// Returns the name of a referenced register.
73static StringRef getUnwindRegisterName(uint8_t Reg) {
74 switch (Reg) {
75 default: llvm_unreachable("Invalid register");
76 case 0: return "RAX";
77 case 1: return "RCX";
78 case 2: return "RDX";
79 case 3: return "RBX";
80 case 4: return "RSP";
81 case 5: return "RBP";
82 case 6: return "RSI";
83 case 7: return "RDI";
84 case 8: return "R8";
85 case 9: return "R9";
86 case 10: return "R10";
87 case 11: return "R11";
88 case 12: return "R12";
89 case 13: return "R13";
90 case 14: return "R14";
91 case 15: return "R15";
92 }
93}
94
95// Calculates the number of array slots required for the unwind code.
96static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) {
97 switch (UnwindCode.getUnwindOp()) {
98 default: llvm_unreachable("Invalid unwind code");
99 case UOP_PushNonVol:
100 case UOP_AllocSmall:
101 case UOP_SetFPReg:
102 case UOP_PushMachFrame:
103 return 1;
104 case UOP_SaveNonVol:
105 case UOP_SaveXMM128:
106 return 2;
107 case UOP_SaveNonVolBig:
108 case UOP_SaveXMM128Big:
109 return 3;
110 case UOP_AllocLarge:
111 return (UnwindCode.getOpInfo() == 0) ? 2 : 3;
112 }
113}
114
115static std::string formatSymbol(const Dumper::Context &Ctx,
116 const coff_section *Section, uint64_t Offset,
117 uint32_t Displacement) {
Alp Tokere69170a2014-06-26 22:52:05 +0000118 std::string Buffer;
119 raw_string_ostream OS(Buffer);
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000120
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000121 SymbolRef Symbol;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000122 if (!Ctx.ResolveSymbol(Section, Offset, Symbol, Ctx.UserData)) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000123 Expected<StringRef> Name = Symbol.getName();
124 if (Name) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000125 OS << *Name;
126 if (Displacement > 0)
127 OS << format(" +0x%X (0x%" PRIX64 ")", Displacement, Offset);
128 else
129 OS << format(" (0x%" PRIX64 ")", Offset);
130 return OS.str();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000131 } else {
132 // TODO: Actually report errors helpfully.
133 consumeError(Name.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000134 }
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000135 }
136
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000137 OS << format(" (0x%" PRIX64 ")", Offset);
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000138 return OS.str();
139}
140
Rafael Espindola4453e42942014-06-13 03:07:50 +0000141static std::error_code resolveRelocation(const Dumper::Context &Ctx,
142 const coff_section *Section,
143 uint64_t Offset,
144 const coff_section *&ResolvedSection,
145 uint64_t &ResolvedAddress) {
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000146 SymbolRef Symbol;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000147 if (std::error_code EC =
148 Ctx.ResolveSymbol(Section, Offset, Symbol, Ctx.UserData))
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000149 return EC;
150
Rafael Espindolaed067c42015-07-03 18:19:00 +0000151 ErrorOr<uint64_t> ResolvedAddressOrErr = Symbol.getAddress();
152 if (std::error_code EC = ResolvedAddressOrErr.getError())
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000153 return EC;
Rafael Espindolaed067c42015-07-03 18:19:00 +0000154 ResolvedAddress = *ResolvedAddressOrErr;
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000155
Rafael Espindola8bab8892015-08-07 23:27:14 +0000156 ErrorOr<section_iterator> SI = Symbol.getSection();
157 ResolvedSection = Ctx.COFF.getCOFFSection(**SI);
Rui Ueyama7d099192015-06-09 15:20:42 +0000158 return std::error_code();
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000159}
160
161namespace llvm {
162namespace Win64EH {
163void Dumper::printRuntimeFunctionEntry(const Context &Ctx,
164 const coff_section *Section,
165 uint64_t Offset,
166 const RuntimeFunction &RF) {
167 SW.printString("StartAddress",
168 formatSymbol(Ctx, Section, Offset + 0, RF.StartAddress));
169 SW.printString("EndAddress",
170 formatSymbol(Ctx, Section, Offset + 4, RF.EndAddress));
171 SW.printString("UnwindInfoAddress",
172 formatSymbol(Ctx, Section, Offset + 8, RF.UnwindInfoOffset));
173}
174
175// Prints one unwind code. Because an unwind code can occupy up to 3 slots in
176// the unwind codes array, this function requires that the correct number of
177// slots is provided.
178void Dumper::printUnwindCode(const UnwindInfo& UI, ArrayRef<UnwindCode> UC) {
179 assert(UC.size() >= getNumUsedSlots(UC[0]));
180
181 SW.startLine() << format("0x%02X: ", unsigned(UC[0].u.CodeOffset))
182 << getUnwindCodeTypeName(UC[0].getUnwindOp());
183
184 switch (UC[0].getUnwindOp()) {
185 case UOP_PushNonVol:
186 OS << " reg=" << getUnwindRegisterName(UC[0].getOpInfo());
187 break;
188
189 case UOP_AllocLarge:
190 OS << " size="
191 << ((UC[0].getOpInfo() == 0) ? UC[1].FrameOffset * 8
192 : getLargeSlotValue(UC));
193 break;
194
195 case UOP_AllocSmall:
196 OS << " size=" << (UC[0].getOpInfo() + 1) * 8;
197 break;
198
199 case UOP_SetFPReg:
200 if (UI.getFrameRegister() == 0)
201 OS << " reg=<invalid>";
202 else
203 OS << " reg=" << getUnwindRegisterName(UI.getFrameRegister())
204 << format(", offset=0x%X", UI.getFrameOffset() * 16);
205 break;
206
207 case UOP_SaveNonVol:
208 OS << " reg=" << getUnwindRegisterName(UC[0].getOpInfo())
209 << format(", offset=0x%X", UC[1].FrameOffset * 8);
210 break;
211
212 case UOP_SaveNonVolBig:
213 OS << " reg=" << getUnwindRegisterName(UC[0].getOpInfo())
214 << format(", offset=0x%X", getLargeSlotValue(UC));
215 break;
216
217 case UOP_SaveXMM128:
218 OS << " reg=XMM" << static_cast<uint32_t>(UC[0].getOpInfo())
219 << format(", offset=0x%X", UC[1].FrameOffset * 16);
220 break;
221
222 case UOP_SaveXMM128Big:
223 OS << " reg=XMM" << static_cast<uint32_t>(UC[0].getOpInfo())
224 << format(", offset=0x%X", getLargeSlotValue(UC));
225 break;
226
227 case UOP_PushMachFrame:
228 OS << " errcode=" << (UC[0].getOpInfo() == 0 ? "no" : "yes");
229 break;
230 }
231
232 OS << "\n";
233}
234
235void Dumper::printUnwindInfo(const Context &Ctx, const coff_section *Section,
236 off_t Offset, const UnwindInfo &UI) {
237 DictScope UIS(SW, "UnwindInfo");
238 SW.printNumber("Version", UI.getVersion());
239 SW.printFlags("Flags", UI.getFlags(), makeArrayRef(UnwindFlags));
240 SW.printNumber("PrologSize", UI.PrologSize);
241 if (UI.getFrameRegister()) {
242 SW.printEnum("FrameRegister", UI.getFrameRegister(),
243 makeArrayRef(UnwindOpInfo));
244 SW.printHex("FrameOffset", UI.getFrameOffset());
245 } else {
246 SW.printString("FrameRegister", StringRef("-"));
247 SW.printString("FrameOffset", StringRef("-"));
248 }
249
250 SW.printNumber("UnwindCodeCount", UI.NumCodes);
251 {
252 ListScope UCS(SW, "UnwindCodes");
253 ArrayRef<UnwindCode> UC(&UI.UnwindCodes[0], UI.NumCodes);
254 for (const UnwindCode *UCI = UC.begin(), *UCE = UC.end(); UCI < UCE; ++UCI) {
255 unsigned UsedSlots = getNumUsedSlots(*UCI);
256 if (UsedSlots > UC.size()) {
257 errs() << "corrupt unwind data";
258 return;
259 }
260
Craig Topper0013be12015-09-21 05:32:41 +0000261 printUnwindCode(UI, makeArrayRef(UCI, UCE));
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000262 UCI = UCI + UsedSlots - 1;
263 }
264 }
265
266 uint64_t LSDAOffset = Offset + getOffsetOfLSDA(UI);
267 if (UI.getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
268 SW.printString("Handler",
269 formatSymbol(Ctx, Section, LSDAOffset,
270 UI.getLanguageSpecificHandlerOffset()));
271 } else if (UI.getFlags() & UNW_ChainInfo) {
272 if (const RuntimeFunction *Chained = UI.getChainedFunctionEntry()) {
273 DictScope CS(SW, "Chained");
274 printRuntimeFunctionEntry(Ctx, Section, LSDAOffset, *Chained);
275 }
276 }
277}
278
279void Dumper::printRuntimeFunction(const Context &Ctx,
280 const coff_section *Section,
281 uint64_t SectionOffset,
282 const RuntimeFunction &RF) {
283 DictScope RFS(SW, "RuntimeFunction");
284 printRuntimeFunctionEntry(Ctx, Section, SectionOffset, RF);
285
286 const coff_section *XData;
287 uint64_t Offset;
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000288 resolveRelocation(Ctx, Section, SectionOffset + 8, XData, Offset);
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000289
290 ArrayRef<uint8_t> Contents;
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000291 error(Ctx.COFF.getSectionContents(XData, Contents));
292 if (Contents.empty())
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000293 return;
294
295 Offset = Offset + RF.UnwindInfoOffset;
296 if (Offset > Contents.size())
297 return;
298
299 const auto UI = reinterpret_cast<const UnwindInfo*>(Contents.data() + Offset);
300 printUnwindInfo(Ctx, XData, Offset, *UI);
301}
302
303void Dumper::printData(const Context &Ctx) {
304 for (const auto &Section : Ctx.COFF.sections()) {
305 StringRef Name;
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000306 Section.getName(Name);
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000307
308 if (Name != ".pdata" && !Name.startswith(".pdata$"))
309 continue;
310
311 const coff_section *PData = Ctx.COFF.getCOFFSection(Section);
312 ArrayRef<uint8_t> Contents;
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000313 error(Ctx.COFF.getSectionContents(PData, Contents));
314 if (Contents.empty())
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +0000315 continue;
316
317 const RuntimeFunction *Entries =
318 reinterpret_cast<const RuntimeFunction *>(Contents.data());
319 const size_t Count = Contents.size() / sizeof(RuntimeFunction);
320 ArrayRef<RuntimeFunction> RuntimeFunctions(Entries, Count);
321
322 size_t Index = 0;
323 for (const auto &RF : RuntimeFunctions) {
324 printRuntimeFunction(Ctx, Ctx.COFF.getCOFFSection(Section),
325 Index * sizeof(RuntimeFunction), RF);
326 ++Index;
327 }
328 }
329}
330}
331}
332