1 //===- Win64EHDumper.cpp - Win64 EH Printer ---------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "Win64EHDumper.h"
10 #include "llvm-readobj.h"
11 #include "llvm/Object/COFF.h"
12 #include "llvm/Support/ErrorHandling.h"
13 #include "llvm/Support/Format.h"
16 using namespace llvm::object
;
17 using namespace llvm::Win64EH
;
19 static const EnumEntry
<unsigned> UnwindFlags
[] = {
20 { "ExceptionHandler", UNW_ExceptionHandler
},
21 { "TerminateHandler", UNW_TerminateHandler
},
22 { "ChainInfo" , UNW_ChainInfo
}
25 static const EnumEntry
<unsigned> UnwindOpInfo
[] = {
44 static uint64_t getOffsetOfLSDA(const UnwindInfo
& UI
) {
45 return static_cast<const char*>(UI
.getLanguageSpecificData())
46 - reinterpret_cast<const char*>(&UI
);
49 static uint32_t getLargeSlotValue(ArrayRef
<UnwindCode
> UC
) {
52 return UC
[1].FrameOffset
+ (static_cast<uint32_t>(UC
[2].FrameOffset
) << 16);
55 // Returns the name of the unwind code.
56 static StringRef
getUnwindCodeTypeName(uint8_t Code
) {
58 default: llvm_unreachable("Invalid unwind code");
59 case UOP_PushNonVol
: return "PUSH_NONVOL";
60 case UOP_AllocLarge
: return "ALLOC_LARGE";
61 case UOP_AllocSmall
: return "ALLOC_SMALL";
62 case UOP_SetFPReg
: return "SET_FPREG";
63 case UOP_SaveNonVol
: return "SAVE_NONVOL";
64 case UOP_SaveNonVolBig
: return "SAVE_NONVOL_FAR";
65 case UOP_SaveXMM128
: return "SAVE_XMM128";
66 case UOP_SaveXMM128Big
: return "SAVE_XMM128_FAR";
67 case UOP_PushMachFrame
: return "PUSH_MACHFRAME";
71 // Returns the name of a referenced register.
72 static StringRef
getUnwindRegisterName(uint8_t Reg
) {
74 default: llvm_unreachable("Invalid register");
85 case 10: return "R10";
86 case 11: return "R11";
87 case 12: return "R12";
88 case 13: return "R13";
89 case 14: return "R14";
90 case 15: return "R15";
94 // Calculates the number of array slots required for the unwind code.
95 static unsigned getNumUsedSlots(const UnwindCode
&UnwindCode
) {
96 switch (UnwindCode
.getUnwindOp()) {
97 default: llvm_unreachable("Invalid unwind code");
101 case UOP_PushMachFrame
:
106 case UOP_SaveNonVolBig
:
107 case UOP_SaveXMM128Big
:
110 return (UnwindCode
.getOpInfo() == 0) ? 2 : 3;
114 static std::string
formatSymbol(const Dumper::Context
&Ctx
,
115 const coff_section
*Section
, uint64_t Offset
,
116 uint32_t Displacement
) {
118 raw_string_ostream
OS(Buffer
);
121 if (!Ctx
.ResolveSymbol(Section
, Offset
, Symbol
, Ctx
.UserData
)) {
122 Expected
<StringRef
> Name
= Symbol
.getName();
125 if (Displacement
> 0)
126 OS
<< format(" +0x%X (0x%" PRIX64
")", Displacement
, Offset
);
128 OS
<< format(" (0x%" PRIX64
")", Offset
);
131 // TODO: Actually report errors helpfully.
132 consumeError(Name
.takeError());
136 OS
<< format(" (0x%" PRIX64
")", Offset
);
140 static std::error_code
resolveRelocation(const Dumper::Context
&Ctx
,
141 const coff_section
*Section
,
143 const coff_section
*&ResolvedSection
,
144 uint64_t &ResolvedAddress
) {
146 if (std::error_code EC
=
147 Ctx
.ResolveSymbol(Section
, Offset
, Symbol
, Ctx
.UserData
))
150 Expected
<uint64_t> ResolvedAddressOrErr
= Symbol
.getAddress();
151 if (!ResolvedAddressOrErr
)
152 return errorToErrorCode(ResolvedAddressOrErr
.takeError());
153 ResolvedAddress
= *ResolvedAddressOrErr
;
155 Expected
<section_iterator
> SI
= Symbol
.getSection();
157 return errorToErrorCode(SI
.takeError());
158 ResolvedSection
= Ctx
.COFF
.getCOFFSection(**SI
);
159 return std::error_code();
164 void Dumper::printRuntimeFunctionEntry(const Context
&Ctx
,
165 const coff_section
*Section
,
167 const RuntimeFunction
&RF
) {
168 SW
.printString("StartAddress",
169 formatSymbol(Ctx
, Section
, Offset
+ 0, RF
.StartAddress
));
170 SW
.printString("EndAddress",
171 formatSymbol(Ctx
, Section
, Offset
+ 4, RF
.EndAddress
));
172 SW
.printString("UnwindInfoAddress",
173 formatSymbol(Ctx
, Section
, Offset
+ 8, RF
.UnwindInfoOffset
));
176 // Prints one unwind code. Because an unwind code can occupy up to 3 slots in
177 // the unwind codes array, this function requires that the correct number of
178 // slots is provided.
179 void Dumper::printUnwindCode(const UnwindInfo
& UI
, ArrayRef
<UnwindCode
> UC
) {
180 assert(UC
.size() >= getNumUsedSlots(UC
[0]));
182 SW
.startLine() << format("0x%02X: ", unsigned(UC
[0].u
.CodeOffset
))
183 << getUnwindCodeTypeName(UC
[0].getUnwindOp());
185 switch (UC
[0].getUnwindOp()) {
187 OS
<< " reg=" << getUnwindRegisterName(UC
[0].getOpInfo());
192 << ((UC
[0].getOpInfo() == 0) ? UC
[1].FrameOffset
* 8
193 : getLargeSlotValue(UC
));
197 OS
<< " size=" << (UC
[0].getOpInfo() + 1) * 8;
201 if (UI
.getFrameRegister() == 0)
202 OS
<< " reg=<invalid>";
204 OS
<< " reg=" << getUnwindRegisterName(UI
.getFrameRegister())
205 << format(", offset=0x%X", UI
.getFrameOffset() * 16);
209 OS
<< " reg=" << getUnwindRegisterName(UC
[0].getOpInfo())
210 << format(", offset=0x%X", UC
[1].FrameOffset
* 8);
213 case UOP_SaveNonVolBig
:
214 OS
<< " reg=" << getUnwindRegisterName(UC
[0].getOpInfo())
215 << format(", offset=0x%X", getLargeSlotValue(UC
));
219 OS
<< " reg=XMM" << static_cast<uint32_t>(UC
[0].getOpInfo())
220 << format(", offset=0x%X", UC
[1].FrameOffset
* 16);
223 case UOP_SaveXMM128Big
:
224 OS
<< " reg=XMM" << static_cast<uint32_t>(UC
[0].getOpInfo())
225 << format(", offset=0x%X", getLargeSlotValue(UC
));
228 case UOP_PushMachFrame
:
229 OS
<< " errcode=" << (UC
[0].getOpInfo() == 0 ? "no" : "yes");
236 void Dumper::printUnwindInfo(const Context
&Ctx
, const coff_section
*Section
,
237 off_t Offset
, const UnwindInfo
&UI
) {
238 DictScope
UIS(SW
, "UnwindInfo");
239 SW
.printNumber("Version", UI
.getVersion());
240 SW
.printFlags("Flags", UI
.getFlags(), makeArrayRef(UnwindFlags
));
241 SW
.printNumber("PrologSize", UI
.PrologSize
);
242 if (UI
.getFrameRegister()) {
243 SW
.printEnum("FrameRegister", UI
.getFrameRegister(),
244 makeArrayRef(UnwindOpInfo
));
245 SW
.printHex("FrameOffset", UI
.getFrameOffset());
247 SW
.printString("FrameRegister", StringRef("-"));
248 SW
.printString("FrameOffset", StringRef("-"));
251 SW
.printNumber("UnwindCodeCount", UI
.NumCodes
);
253 ListScope
UCS(SW
, "UnwindCodes");
254 ArrayRef
<UnwindCode
> UC(&UI
.UnwindCodes
[0], UI
.NumCodes
);
255 for (const UnwindCode
*UCI
= UC
.begin(), *UCE
= UC
.end(); UCI
< UCE
; ++UCI
) {
256 unsigned UsedSlots
= getNumUsedSlots(*UCI
);
257 if (UsedSlots
> UC
.size()) {
258 errs() << "corrupt unwind data";
262 printUnwindCode(UI
, makeArrayRef(UCI
, UCE
));
263 UCI
= UCI
+ UsedSlots
- 1;
267 uint64_t LSDAOffset
= Offset
+ getOffsetOfLSDA(UI
);
268 if (UI
.getFlags() & (UNW_ExceptionHandler
| UNW_TerminateHandler
)) {
269 SW
.printString("Handler",
270 formatSymbol(Ctx
, Section
, LSDAOffset
,
271 UI
.getLanguageSpecificHandlerOffset()));
272 } else if (UI
.getFlags() & UNW_ChainInfo
) {
273 if (const RuntimeFunction
*Chained
= UI
.getChainedFunctionEntry()) {
274 DictScope
CS(SW
, "Chained");
275 printRuntimeFunctionEntry(Ctx
, Section
, LSDAOffset
, *Chained
);
280 void Dumper::printRuntimeFunction(const Context
&Ctx
,
281 const coff_section
*Section
,
282 uint64_t SectionOffset
,
283 const RuntimeFunction
&RF
) {
284 DictScope
RFS(SW
, "RuntimeFunction");
285 printRuntimeFunctionEntry(Ctx
, Section
, SectionOffset
, RF
);
287 const coff_section
*XData
;
289 resolveRelocation(Ctx
, Section
, SectionOffset
+ 8, XData
, Offset
);
291 ArrayRef
<uint8_t> Contents
;
292 if (Error E
= Ctx
.COFF
.getSectionContents(XData
, Contents
))
293 reportError(std::move(E
), Ctx
.COFF
.getFileName());
295 if (Contents
.empty())
298 Offset
= Offset
+ RF
.UnwindInfoOffset
;
299 if (Offset
> Contents
.size())
302 const auto UI
= reinterpret_cast<const UnwindInfo
*>(Contents
.data() + Offset
);
303 printUnwindInfo(Ctx
, XData
, Offset
, *UI
);
306 void Dumper::printData(const Context
&Ctx
) {
307 for (const auto &Section
: Ctx
.COFF
.sections()) {
309 if (Expected
<StringRef
> NameOrErr
= Section
.getName())
312 consumeError(NameOrErr
.takeError());
314 if (Name
!= ".pdata" && !Name
.startswith(".pdata$"))
317 const coff_section
*PData
= Ctx
.COFF
.getCOFFSection(Section
);
318 ArrayRef
<uint8_t> Contents
;
320 if (Error E
= Ctx
.COFF
.getSectionContents(PData
, Contents
))
321 reportError(std::move(E
), Ctx
.COFF
.getFileName());
322 if (Contents
.empty())
325 const RuntimeFunction
*Entries
=
326 reinterpret_cast<const RuntimeFunction
*>(Contents
.data());
327 const size_t Count
= Contents
.size() / sizeof(RuntimeFunction
);
328 ArrayRef
<RuntimeFunction
> RuntimeFunctions(Entries
, Count
);
331 for (const auto &RF
: RuntimeFunctions
) {
332 printRuntimeFunction(Ctx
, Ctx
.COFF
.getCOFFSection(Section
),
333 Index
* sizeof(RuntimeFunction
), RF
);