1 //===-- CodeGen/AsmPrinter/WinException.cpp - Dwarf Exception Impl ------===//
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 // This file contains support for writing Win64 exception info into asm files.
11 //===----------------------------------------------------------------------===//
13 #include "WinException.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/BinaryFormat/COFF.h"
16 #include "llvm/BinaryFormat/Dwarf.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineModuleInfo.h"
21 #include "llvm/CodeGen/TargetFrameLowering.h"
22 #include "llvm/CodeGen/TargetLowering.h"
23 #include "llvm/CodeGen/TargetSubtargetInfo.h"
24 #include "llvm/CodeGen/WinEHFuncInfo.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/Mangler.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/MC/MCContext.h"
30 #include "llvm/MC/MCExpr.h"
31 #include "llvm/MC/MCSection.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/FormattedStream.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Target/TargetOptions.h"
40 WinException::WinException(AsmPrinter
*A
) : EHStreamer(A
) {
41 // MSVC's EH tables are always composed of 32-bit words. All known 64-bit
42 // platforms use an imagerel32 relocation to refer to symbols.
43 useImageRel32
= (A
->getDataLayout().getPointerSizeInBits() == 64);
44 isAArch64
= Asm
->TM
.getTargetTriple().isAArch64();
47 WinException::~WinException() {}
49 /// endModule - Emit all exception information that should come after the
51 void WinException::endModule() {
52 auto &OS
= *Asm
->OutStreamer
;
53 const Module
*M
= MMI
->getModule();
54 for (const Function
&F
: *M
)
55 if (F
.hasFnAttribute("safeseh"))
56 OS
.EmitCOFFSafeSEH(Asm
->getSymbol(&F
));
59 void WinException::beginFunction(const MachineFunction
*MF
) {
60 shouldEmitMoves
= shouldEmitPersonality
= shouldEmitLSDA
= false;
62 // If any landing pads survive, we need an EH table.
63 bool hasLandingPads
= !MF
->getLandingPads().empty();
64 bool hasEHFunclets
= MF
->hasEHFunclets();
66 const Function
&F
= MF
->getFunction();
68 shouldEmitMoves
= Asm
->needsSEHMoves() && MF
->hasWinCFI();
70 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
71 unsigned PerEncoding
= TLOF
.getPersonalityEncoding();
73 EHPersonality Per
= EHPersonality::Unknown
;
74 const Function
*PerFn
= nullptr;
75 if (F
.hasPersonalityFn()) {
76 PerFn
= dyn_cast
<Function
>(F
.getPersonalityFn()->stripPointerCasts());
77 Per
= classifyEHPersonality(PerFn
);
80 bool forceEmitPersonality
= F
.hasPersonalityFn() &&
81 !isNoOpWithoutInvoke(Per
) &&
82 F
.needsUnwindTableEntry();
84 shouldEmitPersonality
=
85 forceEmitPersonality
|| ((hasLandingPads
|| hasEHFunclets
) &&
86 PerEncoding
!= dwarf::DW_EH_PE_omit
&& PerFn
);
88 unsigned LSDAEncoding
= TLOF
.getLSDAEncoding();
89 shouldEmitLSDA
= shouldEmitPersonality
&&
90 LSDAEncoding
!= dwarf::DW_EH_PE_omit
;
92 // If we're not using CFI, we don't want the CFI or the personality, but we
93 // might want EH tables if we had EH pads.
94 if (!Asm
->MAI
->usesWindowsCFI()) {
95 if (Per
== EHPersonality::MSVC_X86SEH
&& !hasEHFunclets
) {
96 // If this is 32-bit SEH and we don't have any funclets (really invokes),
97 // make sure we emit the parent offset label. Some unreferenced filter
98 // functions may still refer to it.
99 const WinEHFuncInfo
&FuncInfo
= *MF
->getWinEHFuncInfo();
100 StringRef FLinkageName
=
101 GlobalValue::dropLLVMManglingEscape(MF
->getFunction().getName());
102 emitEHRegistrationOffsetLabel(FuncInfo
, FLinkageName
);
104 shouldEmitLSDA
= hasEHFunclets
;
105 shouldEmitPersonality
= false;
109 beginFunclet(MF
->front(), Asm
->CurrentFnSym
);
112 void WinException::markFunctionEnd() {
113 if (isAArch64
&& CurrentFuncletEntry
&&
114 (shouldEmitMoves
|| shouldEmitPersonality
))
115 Asm
->OutStreamer
->EmitWinCFIFuncletOrFuncEnd();
118 /// endFunction - Gather and emit post-function exception information.
120 void WinException::endFunction(const MachineFunction
*MF
) {
121 if (!shouldEmitPersonality
&& !shouldEmitMoves
&& !shouldEmitLSDA
)
124 const Function
&F
= MF
->getFunction();
125 EHPersonality Per
= EHPersonality::Unknown
;
126 if (F
.hasPersonalityFn())
127 Per
= classifyEHPersonality(F
.getPersonalityFn()->stripPointerCasts());
129 // Get rid of any dead landing pads if we're not using funclets. In funclet
130 // schemes, the landing pad is not actually reachable. It only exists so
131 // that we can emit the right table data.
132 if (!isFuncletEHPersonality(Per
)) {
133 MachineFunction
*NonConstMF
= const_cast<MachineFunction
*>(MF
);
134 NonConstMF
->tidyLandingPads();
139 // endFunclet will emit the necessary .xdata tables for x64 SEH.
140 if (Per
== EHPersonality::MSVC_Win64SEH
&& MF
->hasEHFunclets())
143 if (shouldEmitPersonality
|| shouldEmitLSDA
) {
144 Asm
->OutStreamer
->PushSection();
146 // Just switch sections to the right xdata section.
147 MCSection
*XData
= Asm
->OutStreamer
->getAssociatedXDataSection(
148 Asm
->OutStreamer
->getCurrentSectionOnly());
149 Asm
->OutStreamer
->SwitchSection(XData
);
151 // Emit the tables appropriate to the personality function in use. If we
152 // don't recognize the personality, assume it uses an Itanium-style LSDA.
153 if (Per
== EHPersonality::MSVC_Win64SEH
)
154 emitCSpecificHandlerTable(MF
);
155 else if (Per
== EHPersonality::MSVC_X86SEH
)
156 emitExceptHandlerTable(MF
);
157 else if (Per
== EHPersonality::MSVC_CXX
)
158 emitCXXFrameHandler3Table(MF
);
159 else if (Per
== EHPersonality::CoreCLR
)
160 emitCLRExceptionTable(MF
);
162 emitExceptionTable();
164 Asm
->OutStreamer
->PopSection();
168 /// Retrieve the MCSymbol for a GlobalValue or MachineBasicBlock.
169 static MCSymbol
*getMCSymbolForMBB(AsmPrinter
*Asm
,
170 const MachineBasicBlock
*MBB
) {
174 assert(MBB
->isEHFuncletEntry());
176 // Give catches and cleanups a name based off of their parent function and
177 // their funclet entry block's number.
178 const MachineFunction
*MF
= MBB
->getParent();
179 const Function
&F
= MF
->getFunction();
180 StringRef FuncLinkageName
= GlobalValue::dropLLVMManglingEscape(F
.getName());
181 MCContext
&Ctx
= MF
->getContext();
182 StringRef HandlerPrefix
= MBB
->isCleanupFuncletEntry() ? "dtor" : "catch";
183 return Ctx
.getOrCreateSymbol("?" + HandlerPrefix
+ "$" +
184 Twine(MBB
->getNumber()) + "@?0?" +
185 FuncLinkageName
+ "@4HA");
188 void WinException::beginFunclet(const MachineBasicBlock
&MBB
,
190 CurrentFuncletEntry
= &MBB
;
192 const Function
&F
= Asm
->MF
->getFunction();
193 // If a symbol was not provided for the funclet, invent one.
195 Sym
= getMCSymbolForMBB(Asm
, &MBB
);
197 // Describe our funclet symbol as a function with internal linkage.
198 Asm
->OutStreamer
->BeginCOFFSymbolDef(Sym
);
199 Asm
->OutStreamer
->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC
);
200 Asm
->OutStreamer
->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
201 << COFF::SCT_COMPLEX_TYPE_SHIFT
);
202 Asm
->OutStreamer
->EndCOFFSymbolDef();
204 // We want our funclet's entry point to be aligned such that no nops will be
205 // present after the label.
206 Asm
->EmitAlignment(std::max(Asm
->MF
->getAlignment(), MBB
.getAlignment()),
209 // Now that we've emitted the alignment directive, point at our funclet.
210 Asm
->OutStreamer
->EmitLabel(Sym
);
213 // Mark 'Sym' as starting our funclet.
214 if (shouldEmitMoves
|| shouldEmitPersonality
) {
215 CurrentFuncletTextSection
= Asm
->OutStreamer
->getCurrentSectionOnly();
216 Asm
->OutStreamer
->EmitWinCFIStartProc(Sym
);
219 if (shouldEmitPersonality
) {
220 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
221 const Function
*PerFn
= nullptr;
223 // Determine which personality routine we are using for this funclet.
224 if (F
.hasPersonalityFn())
225 PerFn
= dyn_cast
<Function
>(F
.getPersonalityFn()->stripPointerCasts());
226 const MCSymbol
*PersHandlerSym
=
227 TLOF
.getCFIPersonalitySymbol(PerFn
, Asm
->TM
, MMI
);
229 // Do not emit a .seh_handler directives for cleanup funclets.
230 // FIXME: This means cleanup funclets cannot handle exceptions. Given that
231 // Clang doesn't produce EH constructs inside cleanup funclets and LLVM's
232 // inliner doesn't allow inlining them, this isn't a major problem in
234 if (!CurrentFuncletEntry
->isCleanupFuncletEntry())
235 Asm
->OutStreamer
->EmitWinEHHandler(PersHandlerSym
, true, true);
239 void WinException::endFunclet() {
240 if (isAArch64
&& CurrentFuncletEntry
&&
241 (shouldEmitMoves
|| shouldEmitPersonality
)) {
242 Asm
->OutStreamer
->SwitchSection(CurrentFuncletTextSection
);
243 Asm
->OutStreamer
->EmitWinCFIFuncletOrFuncEnd();
248 void WinException::endFuncletImpl() {
249 // No funclet to process? Great, we have nothing to do.
250 if (!CurrentFuncletEntry
)
253 const MachineFunction
*MF
= Asm
->MF
;
254 if (shouldEmitMoves
|| shouldEmitPersonality
) {
255 const Function
&F
= MF
->getFunction();
256 EHPersonality Per
= EHPersonality::Unknown
;
257 if (F
.hasPersonalityFn())
258 Per
= classifyEHPersonality(F
.getPersonalityFn()->stripPointerCasts());
260 // On funclet exit, we emit a fake "function" end marker, so that the call
261 // to EmitWinEHHandlerData below can calculate the size of the funclet or
264 MCSection
*XData
= Asm
->OutStreamer
->getAssociatedXDataSection(
265 Asm
->OutStreamer
->getCurrentSectionOnly());
266 Asm
->OutStreamer
->SwitchSection(XData
);
269 // Emit an UNWIND_INFO struct describing the prologue.
270 Asm
->OutStreamer
->EmitWinEHHandlerData();
272 if (Per
== EHPersonality::MSVC_CXX
&& shouldEmitPersonality
&&
273 !CurrentFuncletEntry
->isCleanupFuncletEntry()) {
274 // If this is a C++ catch funclet (or the parent function),
275 // emit a reference to the LSDA for the parent function.
276 StringRef FuncLinkageName
= GlobalValue::dropLLVMManglingEscape(F
.getName());
277 MCSymbol
*FuncInfoXData
= Asm
->OutContext
.getOrCreateSymbol(
278 Twine("$cppxdata$", FuncLinkageName
));
279 Asm
->OutStreamer
->EmitValue(create32bitRef(FuncInfoXData
), 4);
280 } else if (Per
== EHPersonality::MSVC_Win64SEH
&& MF
->hasEHFunclets() &&
281 !CurrentFuncletEntry
->isEHFuncletEntry()) {
282 // If this is the parent function in Win64 SEH, emit the LSDA immediately
283 // following .seh_handlerdata.
284 emitCSpecificHandlerTable(MF
);
287 // Switch back to the funclet start .text section now that we are done
288 // writing to .xdata, and emit an .seh_endproc directive to mark the end of
290 Asm
->OutStreamer
->SwitchSection(CurrentFuncletTextSection
);
291 Asm
->OutStreamer
->EmitWinCFIEndProc();
294 // Let's make sure we don't try to end the same funclet twice.
295 CurrentFuncletEntry
= nullptr;
298 const MCExpr
*WinException::create32bitRef(const MCSymbol
*Value
) {
300 return MCConstantExpr::create(0, Asm
->OutContext
);
301 return MCSymbolRefExpr::create(Value
, useImageRel32
302 ? MCSymbolRefExpr::VK_COFF_IMGREL32
303 : MCSymbolRefExpr::VK_None
,
307 const MCExpr
*WinException::create32bitRef(const GlobalValue
*GV
) {
309 return MCConstantExpr::create(0, Asm
->OutContext
);
310 return create32bitRef(Asm
->getSymbol(GV
));
313 const MCExpr
*WinException::getLabel(const MCSymbol
*Label
) {
315 return MCSymbolRefExpr::create(Label
, MCSymbolRefExpr::VK_COFF_IMGREL32
,
317 return MCBinaryExpr::createAdd(create32bitRef(Label
),
318 MCConstantExpr::create(1, Asm
->OutContext
),
322 const MCExpr
*WinException::getOffset(const MCSymbol
*OffsetOf
,
323 const MCSymbol
*OffsetFrom
) {
324 return MCBinaryExpr::createSub(
325 MCSymbolRefExpr::create(OffsetOf
, Asm
->OutContext
),
326 MCSymbolRefExpr::create(OffsetFrom
, Asm
->OutContext
), Asm
->OutContext
);
329 const MCExpr
*WinException::getOffsetPlusOne(const MCSymbol
*OffsetOf
,
330 const MCSymbol
*OffsetFrom
) {
331 return MCBinaryExpr::createAdd(getOffset(OffsetOf
, OffsetFrom
),
332 MCConstantExpr::create(1, Asm
->OutContext
),
336 int WinException::getFrameIndexOffset(int FrameIndex
,
337 const WinEHFuncInfo
&FuncInfo
) {
338 const TargetFrameLowering
&TFI
= *Asm
->MF
->getSubtarget().getFrameLowering();
340 if (Asm
->MAI
->usesWindowsCFI()) {
342 TFI
.getFrameIndexReferencePreferSP(*Asm
->MF
, FrameIndex
, UnusedReg
,
343 /*IgnoreSPUpdates*/ true);
345 Asm
->MF
->getSubtarget()
347 ->getStackPointerRegisterToSaveRestore());
351 // For 32-bit, offsets should be relative to the end of the EH registration
352 // node. For 64-bit, it's relative to SP at the end of the prologue.
353 assert(FuncInfo
.EHRegNodeEndOffset
!= INT_MAX
);
354 int Offset
= TFI
.getFrameIndexReference(*Asm
->MF
, FrameIndex
, UnusedReg
);
355 Offset
+= FuncInfo
.EHRegNodeEndOffset
;
361 /// Top-level state used to represent unwind to caller
362 const int NullState
= -1;
364 struct InvokeStateChange
{
365 /// EH Label immediately after the last invoke in the previous state, or
366 /// nullptr if the previous state was the null state.
367 const MCSymbol
*PreviousEndLabel
;
369 /// EH label immediately before the first invoke in the new state, or nullptr
370 /// if the new state is the null state.
371 const MCSymbol
*NewStartLabel
;
373 /// State of the invoke following NewStartLabel, or NullState to indicate
374 /// the presence of calls which may unwind to caller.
378 /// Iterator that reports all the invoke state changes in a range of machine
379 /// basic blocks. Changes to the null state are reported whenever a call that
380 /// may unwind to caller is encountered. The MBB range is expected to be an
381 /// entire function or funclet, and the start and end of the range are treated
382 /// as being in the NullState even if there's not an unwind-to-caller call
383 /// before the first invoke or after the last one (i.e., the first state change
384 /// reported is the first change to something other than NullState, and a
385 /// change back to NullState is always reported at the end of iteration).
386 class InvokeStateChangeIterator
{
387 InvokeStateChangeIterator(const WinEHFuncInfo
&EHInfo
,
388 MachineFunction::const_iterator MFI
,
389 MachineFunction::const_iterator MFE
,
390 MachineBasicBlock::const_iterator MBBI
,
392 : EHInfo(EHInfo
), MFI(MFI
), MFE(MFE
), MBBI(MBBI
), BaseState(BaseState
) {
393 LastStateChange
.PreviousEndLabel
= nullptr;
394 LastStateChange
.NewStartLabel
= nullptr;
395 LastStateChange
.NewState
= BaseState
;
400 static iterator_range
<InvokeStateChangeIterator
>
401 range(const WinEHFuncInfo
&EHInfo
, MachineFunction::const_iterator Begin
,
402 MachineFunction::const_iterator End
, int BaseState
= NullState
) {
403 // Reject empty ranges to simplify bookkeeping by ensuring that we can get
404 // the end of the last block.
405 assert(Begin
!= End
);
406 auto BlockBegin
= Begin
->begin();
407 auto BlockEnd
= std::prev(End
)->end();
409 InvokeStateChangeIterator(EHInfo
, Begin
, End
, BlockBegin
, BaseState
),
410 InvokeStateChangeIterator(EHInfo
, End
, End
, BlockEnd
, BaseState
));
414 bool operator==(const InvokeStateChangeIterator
&O
) const {
415 assert(BaseState
== O
.BaseState
);
416 // Must be visiting same block.
419 // Must be visiting same isntr.
422 // At end of block/instr iteration, we can still have two distinct states:
423 // one to report the final EndLabel, and another indicating the end of the
424 // state change iteration. Check for CurrentEndLabel equality to
425 // distinguish these.
426 return CurrentEndLabel
== O
.CurrentEndLabel
;
429 bool operator!=(const InvokeStateChangeIterator
&O
) const {
430 return !operator==(O
);
432 InvokeStateChange
&operator*() { return LastStateChange
; }
433 InvokeStateChange
*operator->() { return &LastStateChange
; }
434 InvokeStateChangeIterator
&operator++() { return scan(); }
437 InvokeStateChangeIterator
&scan();
439 const WinEHFuncInfo
&EHInfo
;
440 const MCSymbol
*CurrentEndLabel
= nullptr;
441 MachineFunction::const_iterator MFI
;
442 MachineFunction::const_iterator MFE
;
443 MachineBasicBlock::const_iterator MBBI
;
444 InvokeStateChange LastStateChange
;
445 bool VisitingInvoke
= false;
449 } // end anonymous namespace
451 InvokeStateChangeIterator
&InvokeStateChangeIterator::scan() {
452 bool IsNewBlock
= false;
453 for (; MFI
!= MFE
; ++MFI
, IsNewBlock
= true) {
456 for (auto MBBE
= MFI
->end(); MBBI
!= MBBE
; ++MBBI
) {
457 const MachineInstr
&MI
= *MBBI
;
458 if (!VisitingInvoke
&& LastStateChange
.NewState
!= BaseState
&&
459 MI
.isCall() && !EHStreamer::callToNoUnwindFunction(&MI
)) {
460 // Indicate a change of state to the null state. We don't have
461 // start/end EH labels handy but the caller won't expect them for
462 // null state regions.
463 LastStateChange
.PreviousEndLabel
= CurrentEndLabel
;
464 LastStateChange
.NewStartLabel
= nullptr;
465 LastStateChange
.NewState
= BaseState
;
466 CurrentEndLabel
= nullptr;
467 // Don't re-visit this instr on the next scan
472 // All other state changes are at EH labels before/after invokes.
475 MCSymbol
*Label
= MI
.getOperand(0).getMCSymbol();
476 if (Label
== CurrentEndLabel
) {
477 VisitingInvoke
= false;
480 auto InvokeMapIter
= EHInfo
.LabelToStateMap
.find(Label
);
481 // Ignore EH labels that aren't the ones inserted before an invoke
482 if (InvokeMapIter
== EHInfo
.LabelToStateMap
.end())
484 auto &StateAndEnd
= InvokeMapIter
->second
;
485 int NewState
= StateAndEnd
.first
;
486 // Keep track of the fact that we're between EH start/end labels so
487 // we know not to treat the inoke we'll see as unwinding to caller.
488 VisitingInvoke
= true;
489 if (NewState
== LastStateChange
.NewState
) {
490 // The state isn't actually changing here. Record the new end and
492 CurrentEndLabel
= StateAndEnd
.second
;
495 // Found a state change to report
496 LastStateChange
.PreviousEndLabel
= CurrentEndLabel
;
497 LastStateChange
.NewStartLabel
= Label
;
498 LastStateChange
.NewState
= NewState
;
499 // Start keeping track of the new current end
500 CurrentEndLabel
= StateAndEnd
.second
;
501 // Don't re-visit this instr on the next scan
506 // Iteration hit the end of the block range.
507 if (LastStateChange
.NewState
!= BaseState
) {
508 // Report the end of the last new state
509 LastStateChange
.PreviousEndLabel
= CurrentEndLabel
;
510 LastStateChange
.NewStartLabel
= nullptr;
511 LastStateChange
.NewState
= BaseState
;
512 // Leave CurrentEndLabel non-null to distinguish this state from end.
513 assert(CurrentEndLabel
!= nullptr);
516 // We've reported all state changes and hit the end state.
517 CurrentEndLabel
= nullptr;
521 /// Emit the language-specific data that __C_specific_handler expects. This
522 /// handler lives in the x64 Microsoft C runtime and allows catching or cleaning
523 /// up after faults with __try, __except, and __finally. The typeinfo values
524 /// are not really RTTI data, but pointers to filter functions that return an
525 /// integer (1, 0, or -1) indicating how to handle the exception. For __finally
526 /// blocks and other cleanups, the landing pad label is zero, and the filter
527 /// function is actually a cleanup handler with the same prototype. A catch-all
528 /// entry is modeled with a null filter function field and a non-zero landing
531 /// Possible filter function return values:
532 /// EXCEPTION_EXECUTE_HANDLER (1):
533 /// Jump to the landing pad label after cleanups.
534 /// EXCEPTION_CONTINUE_SEARCH (0):
535 /// Continue searching this table or continue unwinding.
536 /// EXCEPTION_CONTINUE_EXECUTION (-1):
537 /// Resume execution at the trapping PC.
539 /// Inferred table structure:
543 /// imagerel32 LabelStart;
544 /// imagerel32 LabelEnd;
545 /// imagerel32 FilterOrFinally; // One means catch-all.
546 /// imagerel32 LabelLPad; // Zero means __finally.
547 /// } Entries[NumEntries];
549 void WinException::emitCSpecificHandlerTable(const MachineFunction
*MF
) {
550 auto &OS
= *Asm
->OutStreamer
;
551 MCContext
&Ctx
= Asm
->OutContext
;
552 const WinEHFuncInfo
&FuncInfo
= *MF
->getWinEHFuncInfo();
554 bool VerboseAsm
= OS
.isVerboseAsm();
555 auto AddComment
= [&](const Twine
&Comment
) {
557 OS
.AddComment(Comment
);
561 // Emit a label assignment with the SEH frame offset so we can use it for
562 // llvm.eh.recoverfp.
563 StringRef FLinkageName
=
564 GlobalValue::dropLLVMManglingEscape(MF
->getFunction().getName());
565 MCSymbol
*ParentFrameOffset
=
566 Ctx
.getOrCreateParentFrameOffsetSymbol(FLinkageName
);
567 const MCExpr
*MCOffset
=
568 MCConstantExpr::create(FuncInfo
.SEHSetFrameOffset
, Ctx
);
569 Asm
->OutStreamer
->EmitAssignment(ParentFrameOffset
, MCOffset
);
572 // Use the assembler to compute the number of table entries through label
573 // difference and division.
574 MCSymbol
*TableBegin
=
575 Ctx
.createTempSymbol("lsda_begin", /*AlwaysAddSuffix=*/true);
577 Ctx
.createTempSymbol("lsda_end", /*AlwaysAddSuffix=*/true);
578 const MCExpr
*LabelDiff
= getOffset(TableEnd
, TableBegin
);
579 const MCExpr
*EntrySize
= MCConstantExpr::create(16, Ctx
);
580 const MCExpr
*EntryCount
= MCBinaryExpr::createDiv(LabelDiff
, EntrySize
, Ctx
);
581 AddComment("Number of call sites");
582 OS
.EmitValue(EntryCount
, 4);
584 OS
.EmitLabel(TableBegin
);
586 // Iterate over all the invoke try ranges. Unlike MSVC, LLVM currently only
587 // models exceptions from invokes. LLVM also allows arbitrary reordering of
588 // the code, so our tables end up looking a bit different. Rather than
589 // trying to match MSVC's tables exactly, we emit a denormalized table. For
590 // each range of invokes in the same state, we emit table entries for all
591 // the actions that would be taken in that state. This means our tables are
592 // slightly bigger, which is OK.
593 const MCSymbol
*LastStartLabel
= nullptr;
594 int LastEHState
= -1;
595 // Break out before we enter into a finally funclet.
596 // FIXME: We need to emit separate EH tables for cleanups.
597 MachineFunction::const_iterator End
= MF
->end();
598 MachineFunction::const_iterator Stop
= std::next(MF
->begin());
599 while (Stop
!= End
&& !Stop
->isEHFuncletEntry())
601 for (const auto &StateChange
:
602 InvokeStateChangeIterator::range(FuncInfo
, MF
->begin(), Stop
)) {
603 // Emit all the actions for the state we just transitioned out of
604 // if it was not the null state
605 if (LastEHState
!= -1)
606 emitSEHActionsForRange(FuncInfo
, LastStartLabel
,
607 StateChange
.PreviousEndLabel
, LastEHState
);
608 LastStartLabel
= StateChange
.NewStartLabel
;
609 LastEHState
= StateChange
.NewState
;
612 OS
.EmitLabel(TableEnd
);
615 void WinException::emitSEHActionsForRange(const WinEHFuncInfo
&FuncInfo
,
616 const MCSymbol
*BeginLabel
,
617 const MCSymbol
*EndLabel
, int State
) {
618 auto &OS
= *Asm
->OutStreamer
;
619 MCContext
&Ctx
= Asm
->OutContext
;
620 bool VerboseAsm
= OS
.isVerboseAsm();
621 auto AddComment
= [&](const Twine
&Comment
) {
623 OS
.AddComment(Comment
);
626 assert(BeginLabel
&& EndLabel
);
627 while (State
!= -1) {
628 const SEHUnwindMapEntry
&UME
= FuncInfo
.SEHUnwindMap
[State
];
629 const MCExpr
*FilterOrFinally
;
630 const MCExpr
*ExceptOrNull
;
631 auto *Handler
= UME
.Handler
.get
<MachineBasicBlock
*>();
633 FilterOrFinally
= create32bitRef(getMCSymbolForMBB(Asm
, Handler
));
634 ExceptOrNull
= MCConstantExpr::create(0, Ctx
);
636 // For an except, the filter can be 1 (catch-all) or a function
638 FilterOrFinally
= UME
.Filter
? create32bitRef(UME
.Filter
)
639 : MCConstantExpr::create(1, Ctx
);
640 ExceptOrNull
= create32bitRef(Handler
->getSymbol());
643 AddComment("LabelStart");
644 OS
.EmitValue(getLabel(BeginLabel
), 4);
645 AddComment("LabelEnd");
646 OS
.EmitValue(getLabel(EndLabel
), 4);
647 AddComment(UME
.IsFinally
? "FinallyFunclet" : UME
.Filter
? "FilterFunction"
649 OS
.EmitValue(FilterOrFinally
, 4);
650 AddComment(UME
.IsFinally
? "Null" : "ExceptionHandler");
651 OS
.EmitValue(ExceptOrNull
, 4);
653 assert(UME
.ToState
< State
&& "states should decrease");
658 void WinException::emitCXXFrameHandler3Table(const MachineFunction
*MF
) {
659 const Function
&F
= MF
->getFunction();
660 auto &OS
= *Asm
->OutStreamer
;
661 const WinEHFuncInfo
&FuncInfo
= *MF
->getWinEHFuncInfo();
663 StringRef FuncLinkageName
= GlobalValue::dropLLVMManglingEscape(F
.getName());
665 SmallVector
<std::pair
<const MCExpr
*, int>, 4> IPToStateTable
;
666 MCSymbol
*FuncInfoXData
= nullptr;
667 if (shouldEmitPersonality
) {
668 // If we're 64-bit, emit a pointer to the C++ EH data, and build a map from
669 // IPs to state numbers.
671 Asm
->OutContext
.getOrCreateSymbol(Twine("$cppxdata$", FuncLinkageName
));
672 computeIP2StateTable(MF
, FuncInfo
, IPToStateTable
);
674 FuncInfoXData
= Asm
->OutContext
.getOrCreateLSDASymbol(FuncLinkageName
);
677 int UnwindHelpOffset
= 0;
678 if (Asm
->MAI
->usesWindowsCFI())
680 getFrameIndexOffset(FuncInfo
.UnwindHelpFrameIdx
, FuncInfo
);
682 MCSymbol
*UnwindMapXData
= nullptr;
683 MCSymbol
*TryBlockMapXData
= nullptr;
684 MCSymbol
*IPToStateXData
= nullptr;
685 if (!FuncInfo
.CxxUnwindMap
.empty())
686 UnwindMapXData
= Asm
->OutContext
.getOrCreateSymbol(
687 Twine("$stateUnwindMap$", FuncLinkageName
));
688 if (!FuncInfo
.TryBlockMap
.empty())
690 Asm
->OutContext
.getOrCreateSymbol(Twine("$tryMap$", FuncLinkageName
));
691 if (!IPToStateTable
.empty())
693 Asm
->OutContext
.getOrCreateSymbol(Twine("$ip2state$", FuncLinkageName
));
695 bool VerboseAsm
= OS
.isVerboseAsm();
696 auto AddComment
= [&](const Twine
&Comment
) {
698 OS
.AddComment(Comment
);
702 // uint32_t MagicNumber
704 // UnwindMapEntry *UnwindMap;
705 // uint32_t NumTryBlocks;
706 // TryBlockMapEntry *TryBlockMap;
707 // uint32_t IPMapEntries; // always 0 for x86
708 // IPToStateMapEntry *IPToStateMap; // always 0 for x86
709 // uint32_t UnwindHelp; // non-x86 only
710 // ESTypeList *ESTypeList;
713 // EHFlags & 1 -> Synchronous exceptions only, no async exceptions.
714 // EHFlags & 2 -> ???
715 // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue.
716 OS
.EmitValueToAlignment(4);
717 OS
.EmitLabel(FuncInfoXData
);
719 AddComment("MagicNumber");
720 OS
.EmitIntValue(0x19930522, 4);
722 AddComment("MaxState");
723 OS
.EmitIntValue(FuncInfo
.CxxUnwindMap
.size(), 4);
725 AddComment("UnwindMap");
726 OS
.EmitValue(create32bitRef(UnwindMapXData
), 4);
728 AddComment("NumTryBlocks");
729 OS
.EmitIntValue(FuncInfo
.TryBlockMap
.size(), 4);
731 AddComment("TryBlockMap");
732 OS
.EmitValue(create32bitRef(TryBlockMapXData
), 4);
734 AddComment("IPMapEntries");
735 OS
.EmitIntValue(IPToStateTable
.size(), 4);
737 AddComment("IPToStateXData");
738 OS
.EmitValue(create32bitRef(IPToStateXData
), 4);
740 if (Asm
->MAI
->usesWindowsCFI()) {
741 AddComment("UnwindHelp");
742 OS
.EmitIntValue(UnwindHelpOffset
, 4);
745 AddComment("ESTypeList");
746 OS
.EmitIntValue(0, 4);
748 AddComment("EHFlags");
749 OS
.EmitIntValue(1, 4);
755 if (UnwindMapXData
) {
756 OS
.EmitLabel(UnwindMapXData
);
757 for (const CxxUnwindMapEntry
&UME
: FuncInfo
.CxxUnwindMap
) {
758 MCSymbol
*CleanupSym
=
759 getMCSymbolForMBB(Asm
, UME
.Cleanup
.dyn_cast
<MachineBasicBlock
*>());
760 AddComment("ToState");
761 OS
.EmitIntValue(UME
.ToState
, 4);
763 AddComment("Action");
764 OS
.EmitValue(create32bitRef(CleanupSym
), 4);
771 // int32_t CatchHigh;
772 // int32_t NumCatches;
773 // HandlerType *HandlerArray;
775 if (TryBlockMapXData
) {
776 OS
.EmitLabel(TryBlockMapXData
);
777 SmallVector
<MCSymbol
*, 1> HandlerMaps
;
778 for (size_t I
= 0, E
= FuncInfo
.TryBlockMap
.size(); I
!= E
; ++I
) {
779 const WinEHTryBlockMapEntry
&TBME
= FuncInfo
.TryBlockMap
[I
];
781 MCSymbol
*HandlerMapXData
= nullptr;
782 if (!TBME
.HandlerArray
.empty())
784 Asm
->OutContext
.getOrCreateSymbol(Twine("$handlerMap$")
787 .concat(FuncLinkageName
));
788 HandlerMaps
.push_back(HandlerMapXData
);
790 // TBMEs should form intervals.
791 assert(0 <= TBME
.TryLow
&& "bad trymap interval");
792 assert(TBME
.TryLow
<= TBME
.TryHigh
&& "bad trymap interval");
793 assert(TBME
.TryHigh
< TBME
.CatchHigh
&& "bad trymap interval");
794 assert(TBME
.CatchHigh
< int(FuncInfo
.CxxUnwindMap
.size()) &&
795 "bad trymap interval");
797 AddComment("TryLow");
798 OS
.EmitIntValue(TBME
.TryLow
, 4);
800 AddComment("TryHigh");
801 OS
.EmitIntValue(TBME
.TryHigh
, 4);
803 AddComment("CatchHigh");
804 OS
.EmitIntValue(TBME
.CatchHigh
, 4);
806 AddComment("NumCatches");
807 OS
.EmitIntValue(TBME
.HandlerArray
.size(), 4);
809 AddComment("HandlerArray");
810 OS
.EmitValue(create32bitRef(HandlerMapXData
), 4);
813 // All funclets use the same parent frame offset currently.
814 unsigned ParentFrameOffset
= 0;
815 if (shouldEmitPersonality
) {
816 const TargetFrameLowering
*TFI
= MF
->getSubtarget().getFrameLowering();
817 ParentFrameOffset
= TFI
->getWinEHParentFrameOffset(*MF
);
820 for (size_t I
= 0, E
= FuncInfo
.TryBlockMap
.size(); I
!= E
; ++I
) {
821 const WinEHTryBlockMapEntry
&TBME
= FuncInfo
.TryBlockMap
[I
];
822 MCSymbol
*HandlerMapXData
= HandlerMaps
[I
];
823 if (!HandlerMapXData
)
826 // int32_t Adjectives;
827 // TypeDescriptor *Type;
828 // int32_t CatchObjOffset;
829 // void (*Handler)();
830 // int32_t ParentFrameOffset; // x64 and AArch64 only
832 OS
.EmitLabel(HandlerMapXData
);
833 for (const WinEHHandlerType
&HT
: TBME
.HandlerArray
) {
834 // Get the frame escape label with the offset of the catch object. If
835 // the index is INT_MAX, then there is no catch object, and we should
836 // emit an offset of zero, indicating that no copy will occur.
837 const MCExpr
*FrameAllocOffsetRef
= nullptr;
838 if (HT
.CatchObj
.FrameIndex
!= INT_MAX
) {
839 int Offset
= getFrameIndexOffset(HT
.CatchObj
.FrameIndex
, FuncInfo
);
840 assert(Offset
!= 0 && "Illegal offset for catch object!");
841 FrameAllocOffsetRef
= MCConstantExpr::create(Offset
, Asm
->OutContext
);
843 FrameAllocOffsetRef
= MCConstantExpr::create(0, Asm
->OutContext
);
846 MCSymbol
*HandlerSym
=
847 getMCSymbolForMBB(Asm
, HT
.Handler
.dyn_cast
<MachineBasicBlock
*>());
849 AddComment("Adjectives");
850 OS
.EmitIntValue(HT
.Adjectives
, 4);
853 OS
.EmitValue(create32bitRef(HT
.TypeDescriptor
), 4);
855 AddComment("CatchObjOffset");
856 OS
.EmitValue(FrameAllocOffsetRef
, 4);
858 AddComment("Handler");
859 OS
.EmitValue(create32bitRef(HandlerSym
), 4);
861 if (shouldEmitPersonality
) {
862 AddComment("ParentFrameOffset");
863 OS
.EmitIntValue(ParentFrameOffset
, 4);
869 // IPToStateMapEntry {
873 if (IPToStateXData
) {
874 OS
.EmitLabel(IPToStateXData
);
875 for (auto &IPStatePair
: IPToStateTable
) {
877 OS
.EmitValue(IPStatePair
.first
, 4);
878 AddComment("ToState");
879 OS
.EmitIntValue(IPStatePair
.second
, 4);
884 void WinException::computeIP2StateTable(
885 const MachineFunction
*MF
, const WinEHFuncInfo
&FuncInfo
,
886 SmallVectorImpl
<std::pair
<const MCExpr
*, int>> &IPToStateTable
) {
888 for (MachineFunction::const_iterator FuncletStart
= MF
->begin(),
889 FuncletEnd
= MF
->begin(),
891 FuncletStart
!= End
; FuncletStart
= FuncletEnd
) {
892 // Find the end of the funclet
893 while (++FuncletEnd
!= End
) {
894 if (FuncletEnd
->isEHFuncletEntry()) {
899 // Don't emit ip2state entries for cleanup funclets. Any interesting
900 // exceptional actions in cleanups must be handled in a separate IR
902 if (FuncletStart
->isCleanupFuncletEntry())
905 MCSymbol
*StartLabel
;
907 if (FuncletStart
== MF
->begin()) {
908 BaseState
= NullState
;
909 StartLabel
= Asm
->getFunctionBegin();
912 cast
<FuncletPadInst
>(FuncletStart
->getBasicBlock()->getFirstNonPHI());
913 assert(FuncInfo
.FuncletBaseStateMap
.count(FuncletPad
) != 0);
914 BaseState
= FuncInfo
.FuncletBaseStateMap
.find(FuncletPad
)->second
;
915 StartLabel
= getMCSymbolForMBB(Asm
, &*FuncletStart
);
917 assert(StartLabel
&& "need local function start label");
918 IPToStateTable
.push_back(
919 std::make_pair(create32bitRef(StartLabel
), BaseState
));
921 for (const auto &StateChange
: InvokeStateChangeIterator::range(
922 FuncInfo
, FuncletStart
, FuncletEnd
, BaseState
)) {
923 // Compute the label to report as the start of this entry; use the EH
924 // start label for the invoke if we have one, otherwise (this is a call
925 // which may unwind to our caller and does not have an EH start label, so)
926 // use the previous end label.
927 const MCSymbol
*ChangeLabel
= StateChange
.NewStartLabel
;
929 ChangeLabel
= StateChange
.PreviousEndLabel
;
930 // Emit an entry indicating that PCs after 'Label' have this EH state.
931 IPToStateTable
.push_back(
932 std::make_pair(getLabel(ChangeLabel
), StateChange
.NewState
));
933 // FIXME: assert that NewState is between CatchLow and CatchHigh.
938 void WinException::emitEHRegistrationOffsetLabel(const WinEHFuncInfo
&FuncInfo
,
939 StringRef FLinkageName
) {
940 // Outlined helpers called by the EH runtime need to know the offset of the EH
941 // registration in order to recover the parent frame pointer. Now that we know
942 // we've code generated the parent, we can emit the label assignment that
943 // those helpers use to get the offset of the registration node.
945 // Compute the parent frame offset. The EHRegNodeFrameIndex will be invalid if
946 // after optimization all the invokes were eliminated. We still need to emit
947 // the parent frame offset label, but it should be garbage and should never be
950 int FI
= FuncInfo
.EHRegNodeFrameIndex
;
952 const TargetFrameLowering
*TFI
= Asm
->MF
->getSubtarget().getFrameLowering();
953 Offset
= TFI
->getNonLocalFrameIndexReference(*Asm
->MF
, FI
);
956 MCContext
&Ctx
= Asm
->OutContext
;
957 MCSymbol
*ParentFrameOffset
=
958 Ctx
.getOrCreateParentFrameOffsetSymbol(FLinkageName
);
959 Asm
->OutStreamer
->EmitAssignment(ParentFrameOffset
,
960 MCConstantExpr::create(Offset
, Ctx
));
963 /// Emit the language-specific data that _except_handler3 and 4 expect. This is
964 /// functionally equivalent to the __C_specific_handler table, except it is
965 /// indexed by state number instead of IP.
966 void WinException::emitExceptHandlerTable(const MachineFunction
*MF
) {
967 MCStreamer
&OS
= *Asm
->OutStreamer
;
968 const Function
&F
= MF
->getFunction();
969 StringRef FLinkageName
= GlobalValue::dropLLVMManglingEscape(F
.getName());
971 bool VerboseAsm
= OS
.isVerboseAsm();
972 auto AddComment
= [&](const Twine
&Comment
) {
974 OS
.AddComment(Comment
);
977 const WinEHFuncInfo
&FuncInfo
= *MF
->getWinEHFuncInfo();
978 emitEHRegistrationOffsetLabel(FuncInfo
, FLinkageName
);
980 // Emit the __ehtable label that we use for llvm.x86.seh.lsda.
981 MCSymbol
*LSDALabel
= Asm
->OutContext
.getOrCreateLSDASymbol(FLinkageName
);
982 OS
.EmitValueToAlignment(4);
983 OS
.EmitLabel(LSDALabel
);
985 const Function
*Per
=
986 dyn_cast
<Function
>(F
.getPersonalityFn()->stripPointerCasts());
987 StringRef PerName
= Per
->getName();
989 if (PerName
== "_except_handler4") {
990 // The LSDA for _except_handler4 starts with this struct, followed by the
993 // struct EH4ScopeTable {
994 // int32_t GSCookieOffset;
995 // int32_t GSCookieXOROffset;
996 // int32_t EHCookieOffset;
997 // int32_t EHCookieXOROffset;
998 // ScopeTableEntry ScopeRecord[];
1001 // Offsets are %ebp relative.
1003 // The GS cookie is present only if the function needs stack protection.
1004 // GSCookieOffset = -2 means that GS cookie is not used.
1006 // The EH cookie is always present.
1008 // Check is done the following way:
1009 // (ebp+CookieXOROffset) ^ [ebp+CookieOffset] == _security_cookie
1011 // Retrieve the Guard Stack slot.
1012 int GSCookieOffset
= -2;
1013 const MachineFrameInfo
&MFI
= MF
->getFrameInfo();
1014 if (MFI
.hasStackProtectorIndex()) {
1016 const TargetFrameLowering
*TFI
= MF
->getSubtarget().getFrameLowering();
1017 int SSPIdx
= MFI
.getStackProtectorIndex();
1018 GSCookieOffset
= TFI
->getFrameIndexReference(*MF
, SSPIdx
, UnusedReg
);
1021 // Retrieve the EH Guard slot.
1022 // TODO(etienneb): Get rid of this value and change it for and assertion.
1023 int EHCookieOffset
= 9999;
1024 if (FuncInfo
.EHGuardFrameIndex
!= INT_MAX
) {
1026 const TargetFrameLowering
*TFI
= MF
->getSubtarget().getFrameLowering();
1027 int EHGuardIdx
= FuncInfo
.EHGuardFrameIndex
;
1028 EHCookieOffset
= TFI
->getFrameIndexReference(*MF
, EHGuardIdx
, UnusedReg
);
1031 AddComment("GSCookieOffset");
1032 OS
.EmitIntValue(GSCookieOffset
, 4);
1033 AddComment("GSCookieXOROffset");
1034 OS
.EmitIntValue(0, 4);
1035 AddComment("EHCookieOffset");
1036 OS
.EmitIntValue(EHCookieOffset
, 4);
1037 AddComment("EHCookieXOROffset");
1038 OS
.EmitIntValue(0, 4);
1042 assert(!FuncInfo
.SEHUnwindMap
.empty());
1043 for (const SEHUnwindMapEntry
&UME
: FuncInfo
.SEHUnwindMap
) {
1044 auto *Handler
= UME
.Handler
.get
<MachineBasicBlock
*>();
1045 const MCSymbol
*ExceptOrFinally
=
1046 UME
.IsFinally
? getMCSymbolForMBB(Asm
, Handler
) : Handler
->getSymbol();
1047 // -1 is usually the base state for "unwind to caller", but for
1048 // _except_handler4 it's -2. Do that replacement here if necessary.
1049 int ToState
= UME
.ToState
== -1 ? BaseState
: UME
.ToState
;
1050 AddComment("ToState");
1051 OS
.EmitIntValue(ToState
, 4);
1052 AddComment(UME
.IsFinally
? "Null" : "FilterFunction");
1053 OS
.EmitValue(create32bitRef(UME
.Filter
), 4);
1054 AddComment(UME
.IsFinally
? "FinallyFunclet" : "ExceptionHandler");
1055 OS
.EmitValue(create32bitRef(ExceptOrFinally
), 4);
1059 static int getTryRank(const WinEHFuncInfo
&FuncInfo
, int State
) {
1061 while (State
!= -1) {
1063 State
= FuncInfo
.ClrEHUnwindMap
[State
].TryParentState
;
1068 static int getTryAncestor(const WinEHFuncInfo
&FuncInfo
, int Left
, int Right
) {
1069 int LeftRank
= getTryRank(FuncInfo
, Left
);
1070 int RightRank
= getTryRank(FuncInfo
, Right
);
1072 while (LeftRank
< RightRank
) {
1073 Right
= FuncInfo
.ClrEHUnwindMap
[Right
].TryParentState
;
1077 while (RightRank
< LeftRank
) {
1078 Left
= FuncInfo
.ClrEHUnwindMap
[Left
].TryParentState
;
1082 while (Left
!= Right
) {
1083 Left
= FuncInfo
.ClrEHUnwindMap
[Left
].TryParentState
;
1084 Right
= FuncInfo
.ClrEHUnwindMap
[Right
].TryParentState
;
1090 void WinException::emitCLRExceptionTable(const MachineFunction
*MF
) {
1091 // CLR EH "states" are really just IDs that identify handlers/funclets;
1092 // states, handlers, and funclets all have 1:1 mappings between them, and a
1093 // handler/funclet's "state" is its index in the ClrEHUnwindMap.
1094 MCStreamer
&OS
= *Asm
->OutStreamer
;
1095 const WinEHFuncInfo
&FuncInfo
= *MF
->getWinEHFuncInfo();
1096 MCSymbol
*FuncBeginSym
= Asm
->getFunctionBegin();
1097 MCSymbol
*FuncEndSym
= Asm
->getFunctionEnd();
1099 // A ClrClause describes a protected region.
1101 const MCSymbol
*StartLabel
; // Start of protected region
1102 const MCSymbol
*EndLabel
; // End of protected region
1103 int State
; // Index of handler protecting the protected region
1104 int EnclosingState
; // Index of funclet enclosing the protected region
1106 SmallVector
<ClrClause
, 8> Clauses
;
1108 // Build a map from handler MBBs to their corresponding states (i.e. their
1109 // indices in the ClrEHUnwindMap).
1110 int NumStates
= FuncInfo
.ClrEHUnwindMap
.size();
1111 assert(NumStates
> 0 && "Don't need exception table!");
1112 DenseMap
<const MachineBasicBlock
*, int> HandlerStates
;
1113 for (int State
= 0; State
< NumStates
; ++State
) {
1114 MachineBasicBlock
*HandlerBlock
=
1115 FuncInfo
.ClrEHUnwindMap
[State
].Handler
.get
<MachineBasicBlock
*>();
1116 HandlerStates
[HandlerBlock
] = State
;
1117 // Use this loop through all handlers to verify our assumption (used in
1118 // the MinEnclosingState computation) that enclosing funclets have lower
1119 // state numbers than their enclosed funclets.
1120 assert(FuncInfo
.ClrEHUnwindMap
[State
].HandlerParentState
< State
&&
1121 "ill-formed state numbering");
1123 // Map the main function to the NullState.
1124 HandlerStates
[&MF
->front()] = NullState
;
1126 // Write out a sentinel indicating the end of the standard (Windows) xdata
1127 // and the start of the additional (CLR) info.
1128 OS
.EmitIntValue(0xffffffff, 4);
1129 // Write out the number of funclets
1130 OS
.EmitIntValue(NumStates
, 4);
1132 // Walk the machine blocks/instrs, computing and emitting a few things:
1133 // 1. Emit a list of the offsets to each handler entry, in lexical order.
1134 // 2. Compute a map (EndSymbolMap) from each funclet to the symbol at its end.
1135 // 3. Compute the list of ClrClauses, in the required order (inner before
1136 // outer, earlier before later; the order by which a forward scan with
1137 // early termination will find the innermost enclosing clause covering
1138 // a given address).
1139 // 4. A map (MinClauseMap) from each handler index to the index of the
1140 // outermost funclet/function which contains a try clause targeting the
1141 // key handler. This will be used to determine IsDuplicate-ness when
1142 // emitting ClrClauses. The NullState value is used to indicate that the
1143 // top-level function contains a try clause targeting the key handler.
1144 // HandlerStack is a stack of (PendingStartLabel, PendingState) pairs for
1145 // try regions we entered before entering the PendingState try but which
1146 // we haven't yet exited.
1147 SmallVector
<std::pair
<const MCSymbol
*, int>, 4> HandlerStack
;
1148 // EndSymbolMap and MinClauseMap are maps described above.
1149 std::unique_ptr
<MCSymbol
*[]> EndSymbolMap(new MCSymbol
*[NumStates
]);
1150 SmallVector
<int, 4> MinClauseMap((size_t)NumStates
, NumStates
);
1152 // Visit the root function and each funclet.
1153 for (MachineFunction::const_iterator FuncletStart
= MF
->begin(),
1154 FuncletEnd
= MF
->begin(),
1156 FuncletStart
!= End
; FuncletStart
= FuncletEnd
) {
1157 int FuncletState
= HandlerStates
[&*FuncletStart
];
1158 // Find the end of the funclet
1159 MCSymbol
*EndSymbol
= FuncEndSym
;
1160 while (++FuncletEnd
!= End
) {
1161 if (FuncletEnd
->isEHFuncletEntry()) {
1162 EndSymbol
= getMCSymbolForMBB(Asm
, &*FuncletEnd
);
1166 // Emit the function/funclet end and, if this is a funclet (and not the
1167 // root function), record it in the EndSymbolMap.
1168 OS
.EmitValue(getOffset(EndSymbol
, FuncBeginSym
), 4);
1169 if (FuncletState
!= NullState
) {
1170 // Record the end of the handler.
1171 EndSymbolMap
[FuncletState
] = EndSymbol
;
1174 // Walk the state changes in this function/funclet and compute its clauses.
1175 // Funclets always start in the null state.
1176 const MCSymbol
*CurrentStartLabel
= nullptr;
1177 int CurrentState
= NullState
;
1178 assert(HandlerStack
.empty());
1179 for (const auto &StateChange
:
1180 InvokeStateChangeIterator::range(FuncInfo
, FuncletStart
, FuncletEnd
)) {
1181 // Close any try regions we're not still under
1182 int StillPendingState
=
1183 getTryAncestor(FuncInfo
, CurrentState
, StateChange
.NewState
);
1184 while (CurrentState
!= StillPendingState
) {
1185 assert(CurrentState
!= NullState
&&
1186 "Failed to find still-pending state!");
1187 // Close the pending clause
1188 Clauses
.push_back({CurrentStartLabel
, StateChange
.PreviousEndLabel
,
1189 CurrentState
, FuncletState
});
1190 // Now the next-outer try region is current
1191 CurrentState
= FuncInfo
.ClrEHUnwindMap
[CurrentState
].TryParentState
;
1192 // Pop the new start label from the handler stack if we've exited all
1193 // inner try regions of the corresponding try region.
1194 if (HandlerStack
.back().second
== CurrentState
)
1195 CurrentStartLabel
= HandlerStack
.pop_back_val().first
;
1198 if (StateChange
.NewState
!= CurrentState
) {
1199 // For each clause we're starting, update the MinClauseMap so we can
1200 // know which is the topmost funclet containing a clause targeting
1202 for (int EnteredState
= StateChange
.NewState
;
1203 EnteredState
!= CurrentState
;
1205 FuncInfo
.ClrEHUnwindMap
[EnteredState
].TryParentState
) {
1206 int &MinEnclosingState
= MinClauseMap
[EnteredState
];
1207 if (FuncletState
< MinEnclosingState
)
1208 MinEnclosingState
= FuncletState
;
1210 // Save the previous current start/label on the stack and update to
1211 // the newly-current start/state.
1212 HandlerStack
.emplace_back(CurrentStartLabel
, CurrentState
);
1213 CurrentStartLabel
= StateChange
.NewStartLabel
;
1214 CurrentState
= StateChange
.NewState
;
1217 assert(HandlerStack
.empty());
1220 // Now emit the clause info, starting with the number of clauses.
1221 OS
.EmitIntValue(Clauses
.size(), 4);
1222 for (ClrClause
&Clause
: Clauses
) {
1223 // Emit a CORINFO_EH_CLAUSE :
1225 struct CORINFO_EH_CLAUSE
1227 CORINFO_EH_CLAUSE_FLAGS Flags; // actually a CorExceptionFlag
1229 DWORD TryLength; // actually TryEndOffset
1230 DWORD HandlerOffset;
1231 DWORD HandlerLength; // actually HandlerEndOffset
1234 DWORD ClassToken; // use for catch clauses
1235 DWORD FilterOffset; // use for filter clauses
1239 enum CORINFO_EH_CLAUSE_FLAGS
1241 CORINFO_EH_CLAUSE_NONE = 0,
1242 CORINFO_EH_CLAUSE_FILTER = 0x0001, // This clause is for a filter
1243 CORINFO_EH_CLAUSE_FINALLY = 0x0002, // This clause is a finally clause
1244 CORINFO_EH_CLAUSE_FAULT = 0x0004, // This clause is a fault clause
1246 typedef enum CorExceptionFlag
1248 COR_ILEXCEPTION_CLAUSE_NONE,
1249 COR_ILEXCEPTION_CLAUSE_FILTER = 0x0001, // This is a filter clause
1250 COR_ILEXCEPTION_CLAUSE_FINALLY = 0x0002, // This is a finally clause
1251 COR_ILEXCEPTION_CLAUSE_FAULT = 0x0004, // This is a fault clause
1252 COR_ILEXCEPTION_CLAUSE_DUPLICATED = 0x0008, // duplicated clause. This
1253 // clause was duplicated
1254 // to a funclet which was
1255 // pulled out of line
1258 // Add 1 to the start/end of the EH clause; the IP associated with a
1259 // call when the runtime does its scan is the IP of the next instruction
1260 // (the one to which control will return after the call), so we need
1261 // to add 1 to the end of the clause to cover that offset. We also add
1262 // 1 to the start of the clause to make sure that the ranges reported
1263 // for all clauses are disjoint. Note that we'll need some additional
1264 // logic when machine traps are supported, since in that case the IP
1265 // that the runtime uses is the offset of the faulting instruction
1266 // itself; if such an instruction immediately follows a call but the
1267 // two belong to different clauses, we'll need to insert a nop between
1268 // them so the runtime can distinguish the point to which the call will
1269 // return from the point at which the fault occurs.
1271 const MCExpr
*ClauseBegin
=
1272 getOffsetPlusOne(Clause
.StartLabel
, FuncBeginSym
);
1273 const MCExpr
*ClauseEnd
= getOffsetPlusOne(Clause
.EndLabel
, FuncBeginSym
);
1275 const ClrEHUnwindMapEntry
&Entry
= FuncInfo
.ClrEHUnwindMap
[Clause
.State
];
1276 MachineBasicBlock
*HandlerBlock
= Entry
.Handler
.get
<MachineBasicBlock
*>();
1277 MCSymbol
*BeginSym
= getMCSymbolForMBB(Asm
, HandlerBlock
);
1278 const MCExpr
*HandlerBegin
= getOffset(BeginSym
, FuncBeginSym
);
1279 MCSymbol
*EndSym
= EndSymbolMap
[Clause
.State
];
1280 const MCExpr
*HandlerEnd
= getOffset(EndSym
, FuncBeginSym
);
1283 switch (Entry
.HandlerType
) {
1284 case ClrHandlerType::Catch
:
1285 // Leaving bits 0-2 clear indicates catch.
1287 case ClrHandlerType::Filter
:
1290 case ClrHandlerType::Finally
:
1293 case ClrHandlerType::Fault
:
1297 if (Clause
.EnclosingState
!= MinClauseMap
[Clause
.State
]) {
1298 // This is a "duplicate" clause; the handler needs to be entered from a
1299 // frame above the one holding the invoke.
1300 assert(Clause
.EnclosingState
> MinClauseMap
[Clause
.State
]);
1303 OS
.EmitIntValue(Flags
, 4);
1305 // Write the clause start/end
1306 OS
.EmitValue(ClauseBegin
, 4);
1307 OS
.EmitValue(ClauseEnd
, 4);
1309 // Write out the handler start/end
1310 OS
.EmitValue(HandlerBegin
, 4);
1311 OS
.EmitValue(HandlerEnd
, 4);
1313 // Write out the type token or filter offset
1314 assert(Entry
.HandlerType
!= ClrHandlerType::Filter
&& "NYI: filters");
1315 OS
.EmitIntValue(Entry
.TypeToken
, 4);