LLVM  10.0.0svn
WinException.cpp
Go to the documentation of this file.
1 //===-- CodeGen/AsmPrinter/WinException.cpp - Dwarf Exception Impl ------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for writing Win64 exception info into asm files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "WinException.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/BinaryFormat/COFF.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"
38 using namespace llvm;
39 
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();
45 }
46 
48 
49 /// endModule - Emit all exception information that should come after the
50 /// content.
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));
57 }
58 
60  shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
61 
62  // If any landing pads survive, we need an EH table.
63  bool hasLandingPads = !MF->getLandingPads().empty();
64  bool hasEHFunclets = MF->hasEHFunclets();
65 
66  const Function &F = MF->getFunction();
67 
68  shouldEmitMoves = Asm->needsSEHMoves() && MF->hasWinCFI();
69 
71  unsigned PerEncoding = TLOF.getPersonalityEncoding();
72 
74  const Function *PerFn = nullptr;
75  if (F.hasPersonalityFn()) {
77  Per = classifyEHPersonality(PerFn);
78  }
79 
80  bool forceEmitPersonality = F.hasPersonalityFn() &&
81  !isNoOpWithoutInvoke(Per) &&
83 
84  shouldEmitPersonality =
85  forceEmitPersonality || ((hasLandingPads || hasEHFunclets) &&
86  PerEncoding != dwarf::DW_EH_PE_omit && PerFn);
87 
88  unsigned LSDAEncoding = TLOF.getLSDAEncoding();
89  shouldEmitLSDA = shouldEmitPersonality &&
90  LSDAEncoding != dwarf::DW_EH_PE_omit;
91 
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 =
102  emitEHRegistrationOffsetLabel(FuncInfo, FLinkageName);
103  }
104  shouldEmitLSDA = hasEHFunclets;
105  shouldEmitPersonality = false;
106  return;
107  }
108 
110 }
111 
113  if (isAArch64 && CurrentFuncletEntry &&
114  (shouldEmitMoves || shouldEmitPersonality))
115  Asm->OutStreamer->EmitWinCFIFuncletOrFuncEnd();
116 }
117 
118 /// endFunction - Gather and emit post-function exception information.
119 ///
121  if (!shouldEmitPersonality && !shouldEmitMoves && !shouldEmitLSDA)
122  return;
123 
124  const Function &F = MF->getFunction();
126  if (F.hasPersonalityFn())
128 
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();
135  }
136 
137  endFuncletImpl();
138 
139  // endFunclet will emit the necessary .xdata tables for x64 SEH.
140  if (Per == EHPersonality::MSVC_Win64SEH && MF->hasEHFunclets())
141  return;
142 
143  if (shouldEmitPersonality || shouldEmitLSDA) {
144  Asm->OutStreamer->PushSection();
145 
146  // Just switch sections to the right xdata section.
147  MCSection *XData = Asm->OutStreamer->getAssociatedXDataSection(
148  Asm->OutStreamer->getCurrentSectionOnly());
149  Asm->OutStreamer->SwitchSection(XData);
150 
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);
161  else
163 
164  Asm->OutStreamer->PopSection();
165  }
166 }
167 
168 /// Retrieve the MCSymbol for a GlobalValue or MachineBasicBlock.
170  const MachineBasicBlock *MBB) {
171  if (!MBB)
172  return nullptr;
173 
174  assert(MBB->isEHFuncletEntry());
175 
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();
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");
186 }
187 
189  MCSymbol *Sym) {
190  CurrentFuncletEntry = &MBB;
191 
192  const Function &F = Asm->MF->getFunction();
193  // If a symbol was not provided for the funclet, invent one.
194  if (!Sym) {
195  Sym = getMCSymbolForMBB(Asm, &MBB);
196 
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
202  Asm->OutStreamer->EndCOFFSymbolDef();
203 
204  // We want our funclet's entry point to be aligned such that no nops will be
205  // present after the label.
207  &F);
208 
209  // Now that we've emitted the alignment directive, point at our funclet.
210  Asm->OutStreamer->EmitLabel(Sym);
211  }
212 
213  // Mark 'Sym' as starting our funclet.
214  if (shouldEmitMoves || shouldEmitPersonality) {
215  CurrentFuncletTextSection = Asm->OutStreamer->getCurrentSectionOnly();
216  Asm->OutStreamer->EmitWinCFIStartProc(Sym);
217  }
218 
219  if (shouldEmitPersonality) {
221  const Function *PerFn = nullptr;
222 
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);
228 
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
233  // practice.
234  if (!CurrentFuncletEntry->isCleanupFuncletEntry())
235  Asm->OutStreamer->EmitWinEHHandler(PersHandlerSym, true, true);
236  }
237 }
238 
240  if (isAArch64 && CurrentFuncletEntry &&
241  (shouldEmitMoves || shouldEmitPersonality)) {
242  Asm->OutStreamer->SwitchSection(CurrentFuncletTextSection);
243  Asm->OutStreamer->EmitWinCFIFuncletOrFuncEnd();
244  }
245  endFuncletImpl();
246 }
247 
248 void WinException::endFuncletImpl() {
249  // No funclet to process? Great, we have nothing to do.
250  if (!CurrentFuncletEntry)
251  return;
252 
253  const MachineFunction *MF = Asm->MF;
254  if (shouldEmitMoves || shouldEmitPersonality) {
255  const Function &F = MF->getFunction();
257  if (F.hasPersonalityFn())
259 
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
262  // function.
263  if (isAArch64) {
264  MCSection *XData = Asm->OutStreamer->getAssociatedXDataSection(
265  Asm->OutStreamer->getCurrentSectionOnly());
266  Asm->OutStreamer->SwitchSection(XData);
267  }
268 
269  // Emit an UNWIND_INFO struct describing the prologue.
270  Asm->OutStreamer->EmitWinEHHandlerData();
271 
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.
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);
285  }
286 
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
289  // the function.
290  Asm->OutStreamer->SwitchSection(CurrentFuncletTextSection);
291  Asm->OutStreamer->EmitWinCFIEndProc();
292  }
293 
294  // Let's make sure we don't try to end the same funclet twice.
295  CurrentFuncletEntry = nullptr;
296 }
297 
298 const MCExpr *WinException::create32bitRef(const MCSymbol *Value) {
299  if (!Value)
301  return MCSymbolRefExpr::create(Value, useImageRel32
304  Asm->OutContext);
305 }
306 
307 const MCExpr *WinException::create32bitRef(const GlobalValue *GV) {
308  if (!GV)
310  return create32bitRef(Asm->getSymbol(GV));
311 }
312 
313 const MCExpr *WinException::getLabel(const MCSymbol *Label) {
314  if (isAArch64)
316  Asm->OutContext);
317  return MCBinaryExpr::createAdd(create32bitRef(Label),
319  Asm->OutContext);
320 }
321 
322 const MCExpr *WinException::getOffset(const MCSymbol *OffsetOf,
323  const MCSymbol *OffsetFrom) {
327 }
328 
329 const MCExpr *WinException::getOffsetPlusOne(const MCSymbol *OffsetOf,
330  const MCSymbol *OffsetFrom) {
331  return MCBinaryExpr::createAdd(getOffset(OffsetOf, OffsetFrom),
333  Asm->OutContext);
334 }
335 
336 int WinException::getFrameIndexOffset(int FrameIndex,
337  const WinEHFuncInfo &FuncInfo) {
339  unsigned UnusedReg;
340  if (Asm->MAI->usesWindowsCFI()) {
341  int Offset =
343  /*IgnoreSPUpdates*/ true);
344  assert(UnusedReg ==
345  Asm->MF->getSubtarget()
348  return Offset;
349  }
350 
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;
356  return Offset;
357 }
358 
359 namespace {
360 
361 /// Top-level state used to represent unwind to caller
362 const int NullState = -1;
363 
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;
368 
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;
372 
373  /// State of the invoke following NewStartLabel, or NullState to indicate
374  /// the presence of calls which may unwind to caller.
375  int NewState;
376 };
377 
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,
391  int BaseState)
392  : EHInfo(EHInfo), MFI(MFI), MFE(MFE), MBBI(MBBI), BaseState(BaseState) {
393  LastStateChange.PreviousEndLabel = nullptr;
394  LastStateChange.NewStartLabel = nullptr;
395  LastStateChange.NewState = BaseState;
396  scan();
397  }
398 
399 public:
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();
408  return make_range(
409  InvokeStateChangeIterator(EHInfo, Begin, End, BlockBegin, BaseState),
410  InvokeStateChangeIterator(EHInfo, End, End, BlockEnd, BaseState));
411  }
412 
413  // Iterator methods.
414  bool operator==(const InvokeStateChangeIterator &O) const {
415  assert(BaseState == O.BaseState);
416  // Must be visiting same block.
417  if (MFI != O.MFI)
418  return false;
419  // Must be visiting same isntr.
420  if (MBBI != O.MBBI)
421  return false;
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;
427  }
428 
429  bool operator!=(const InvokeStateChangeIterator &O) const {
430  return !operator==(O);
431  }
432  InvokeStateChange &operator*() { return LastStateChange; }
433  InvokeStateChange *operator->() { return &LastStateChange; }
434  InvokeStateChangeIterator &operator++() { return scan(); }
435 
436 private:
437  InvokeStateChangeIterator &scan();
438 
439  const WinEHFuncInfo &EHInfo;
440  const MCSymbol *CurrentEndLabel = nullptr;
444  InvokeStateChange LastStateChange;
445  bool VisitingInvoke = false;
446  int BaseState;
447 };
448 
449 } // end anonymous namespace
450 
451 InvokeStateChangeIterator &InvokeStateChangeIterator::scan() {
452  bool IsNewBlock = false;
453  for (; MFI != MFE; ++MFI, IsNewBlock = true) {
454  if (IsNewBlock)
455  MBBI = MFI->begin();
456  for (auto MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
457  const MachineInstr &MI = *MBBI;
458  if (!VisitingInvoke && LastStateChange.NewState != BaseState &&
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
468  ++MBBI;
469  return *this;
470  }
471 
472  // All other state changes are at EH labels before/after invokes.
473  if (!MI.isEHLabel())
474  continue;
475  MCSymbol *Label = MI.getOperand(0).getMCSymbol();
476  if (Label == CurrentEndLabel) {
477  VisitingInvoke = false;
478  continue;
479  }
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())
483  continue;
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
491  // keep going.
492  CurrentEndLabel = StateAndEnd.second;
493  continue;
494  }
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
502  ++MBBI;
503  return *this;
504  }
505  }
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);
514  return *this;
515  }
516  // We've reported all state changes and hit the end state.
517  CurrentEndLabel = nullptr;
518  return *this;
519 }
520 
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
529 /// pad label.
530 ///
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.
538 ///
539 /// Inferred table structure:
540 /// struct Table {
541 /// int NumEntries;
542 /// struct Entry {
543 /// imagerel32 LabelStart;
544 /// imagerel32 LabelEnd;
545 /// imagerel32 FilterOrFinally; // One means catch-all.
546 /// imagerel32 LabelLPad; // Zero means __finally.
547 /// } Entries[NumEntries];
548 /// };
549 void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) {
550  auto &OS = *Asm->OutStreamer;
551  MCContext &Ctx = Asm->OutContext;
552  const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
553 
554  bool VerboseAsm = OS.isVerboseAsm();
555  auto AddComment = [&](const Twine &Comment) {
556  if (VerboseAsm)
557  OS.AddComment(Comment);
558  };
559 
560  if (!isAArch64) {
561  // Emit a label assignment with the SEH frame offset so we can use it for
562  // llvm.eh.recoverfp.
563  StringRef FLinkageName =
565  MCSymbol *ParentFrameOffset =
566  Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName);
567  const MCExpr *MCOffset =
569  Asm->OutStreamer->EmitAssignment(ParentFrameOffset, MCOffset);
570  }
571 
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);
576  MCSymbol *TableEnd =
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);
583 
584  OS.EmitLabel(TableBegin);
585 
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.
598  MachineFunction::const_iterator Stop = std::next(MF->begin());
599  while (Stop != End && !Stop->isEHFuncletEntry())
600  ++Stop;
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;
610  }
611 
612  OS.EmitLabel(TableEnd);
613 }
614 
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) {
622  if (VerboseAsm)
623  OS.AddComment(Comment);
624  };
625 
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 *>();
632  if (UME.IsFinally) {
633  FilterOrFinally = create32bitRef(getMCSymbolForMBB(Asm, Handler));
634  ExceptOrNull = MCConstantExpr::create(0, Ctx);
635  } else {
636  // For an except, the filter can be 1 (catch-all) or a function
637  // label.
638  FilterOrFinally = UME.Filter ? create32bitRef(UME.Filter)
639  : MCConstantExpr::create(1, Ctx);
640  ExceptOrNull = create32bitRef(Handler->getSymbol());
641  }
642 
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"
648  : "CatchAll");
649  OS.EmitValue(FilterOrFinally, 4);
650  AddComment(UME.IsFinally ? "Null" : "ExceptionHandler");
651  OS.EmitValue(ExceptOrNull, 4);
652 
653  assert(UME.ToState < State && "states should decrease");
654  State = UME.ToState;
655  }
656 }
657 
658 void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
659  const Function &F = MF->getFunction();
660  auto &OS = *Asm->OutStreamer;
661  const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
662 
664 
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.
670  FuncInfoXData =
671  Asm->OutContext.getOrCreateSymbol(Twine("$cppxdata$", FuncLinkageName));
672  computeIP2StateTable(MF, FuncInfo, IPToStateTable);
673  } else {
674  FuncInfoXData = Asm->OutContext.getOrCreateLSDASymbol(FuncLinkageName);
675  }
676 
677  int UnwindHelpOffset = 0;
678  if (Asm->MAI->usesWindowsCFI())
679  UnwindHelpOffset =
680  getFrameIndexOffset(FuncInfo.UnwindHelpFrameIdx, FuncInfo);
681 
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())
689  TryBlockMapXData =
690  Asm->OutContext.getOrCreateSymbol(Twine("$tryMap$", FuncLinkageName));
691  if (!IPToStateTable.empty())
692  IPToStateXData =
693  Asm->OutContext.getOrCreateSymbol(Twine("$ip2state$", FuncLinkageName));
694 
695  bool VerboseAsm = OS.isVerboseAsm();
696  auto AddComment = [&](const Twine &Comment) {
697  if (VerboseAsm)
698  OS.AddComment(Comment);
699  };
700 
701  // FuncInfo {
702  // uint32_t MagicNumber
703  // int32_t MaxState;
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;
711  // int32_t EHFlags;
712  // }
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);
718 
719  AddComment("MagicNumber");
720  OS.EmitIntValue(0x19930522, 4);
721 
722  AddComment("MaxState");
723  OS.EmitIntValue(FuncInfo.CxxUnwindMap.size(), 4);
724 
725  AddComment("UnwindMap");
726  OS.EmitValue(create32bitRef(UnwindMapXData), 4);
727 
728  AddComment("NumTryBlocks");
729  OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4);
730 
731  AddComment("TryBlockMap");
732  OS.EmitValue(create32bitRef(TryBlockMapXData), 4);
733 
734  AddComment("IPMapEntries");
735  OS.EmitIntValue(IPToStateTable.size(), 4);
736 
737  AddComment("IPToStateXData");
738  OS.EmitValue(create32bitRef(IPToStateXData), 4);
739 
740  if (Asm->MAI->usesWindowsCFI()) {
741  AddComment("UnwindHelp");
742  OS.EmitIntValue(UnwindHelpOffset, 4);
743  }
744 
745  AddComment("ESTypeList");
746  OS.EmitIntValue(0, 4);
747 
748  AddComment("EHFlags");
749  OS.EmitIntValue(1, 4);
750 
751  // UnwindMapEntry {
752  // int32_t ToState;
753  // void (*Action)();
754  // };
755  if (UnwindMapXData) {
756  OS.EmitLabel(UnwindMapXData);
757  for (const CxxUnwindMapEntry &UME : FuncInfo.CxxUnwindMap) {
758  MCSymbol *CleanupSym =
760  AddComment("ToState");
761  OS.EmitIntValue(UME.ToState, 4);
762 
763  AddComment("Action");
764  OS.EmitValue(create32bitRef(CleanupSym), 4);
765  }
766  }
767 
768  // TryBlockMap {
769  // int32_t TryLow;
770  // int32_t TryHigh;
771  // int32_t CatchHigh;
772  // int32_t NumCatches;
773  // HandlerType *HandlerArray;
774  // };
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];
780 
781  MCSymbol *HandlerMapXData = nullptr;
782  if (!TBME.HandlerArray.empty())
783  HandlerMapXData =
784  Asm->OutContext.getOrCreateSymbol(Twine("$handlerMap$")
785  .concat(Twine(I))
786  .concat("$")
787  .concat(FuncLinkageName));
788  HandlerMaps.push_back(HandlerMapXData);
789 
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");
796 
797  AddComment("TryLow");
798  OS.EmitIntValue(TBME.TryLow, 4);
799 
800  AddComment("TryHigh");
801  OS.EmitIntValue(TBME.TryHigh, 4);
802 
803  AddComment("CatchHigh");
804  OS.EmitIntValue(TBME.CatchHigh, 4);
805 
806  AddComment("NumCatches");
807  OS.EmitIntValue(TBME.HandlerArray.size(), 4);
808 
809  AddComment("HandlerArray");
810  OS.EmitValue(create32bitRef(HandlerMapXData), 4);
811  }
812 
813  // All funclets use the same parent frame offset currently.
814  unsigned ParentFrameOffset = 0;
815  if (shouldEmitPersonality) {
817  ParentFrameOffset = TFI->getWinEHParentFrameOffset(*MF);
818  }
819 
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)
824  continue;
825  // HandlerType {
826  // int32_t Adjectives;
827  // TypeDescriptor *Type;
828  // int32_t CatchObjOffset;
829  // void (*Handler)();
830  // int32_t ParentFrameOffset; // x64 and AArch64 only
831  // };
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);
842  } else {
843  FrameAllocOffsetRef = MCConstantExpr::create(0, Asm->OutContext);
844  }
845 
846  MCSymbol *HandlerSym =
848 
849  AddComment("Adjectives");
850  OS.EmitIntValue(HT.Adjectives, 4);
851 
852  AddComment("Type");
853  OS.EmitValue(create32bitRef(HT.TypeDescriptor), 4);
854 
855  AddComment("CatchObjOffset");
856  OS.EmitValue(FrameAllocOffsetRef, 4);
857 
858  AddComment("Handler");
859  OS.EmitValue(create32bitRef(HandlerSym), 4);
860 
861  if (shouldEmitPersonality) {
862  AddComment("ParentFrameOffset");
863  OS.EmitIntValue(ParentFrameOffset, 4);
864  }
865  }
866  }
867  }
868 
869  // IPToStateMapEntry {
870  // void *IP;
871  // int32_t State;
872  // };
873  if (IPToStateXData) {
874  OS.EmitLabel(IPToStateXData);
875  for (auto &IPStatePair : IPToStateTable) {
876  AddComment("IP");
877  OS.EmitValue(IPStatePair.first, 4);
878  AddComment("ToState");
879  OS.EmitIntValue(IPStatePair.second, 4);
880  }
881  }
882 }
883 
884 void WinException::computeIP2StateTable(
885  const MachineFunction *MF, const WinEHFuncInfo &FuncInfo,
886  SmallVectorImpl<std::pair<const MCExpr *, int>> &IPToStateTable) {
887 
888  for (MachineFunction::const_iterator FuncletStart = MF->begin(),
889  FuncletEnd = MF->begin(),
890  End = MF->end();
891  FuncletStart != End; FuncletStart = FuncletEnd) {
892  // Find the end of the funclet
893  while (++FuncletEnd != End) {
894  if (FuncletEnd->isEHFuncletEntry()) {
895  break;
896  }
897  }
898 
899  // Don't emit ip2state entries for cleanup funclets. Any interesting
900  // exceptional actions in cleanups must be handled in a separate IR
901  // function.
902  if (FuncletStart->isCleanupFuncletEntry())
903  continue;
904 
905  MCSymbol *StartLabel;
906  int BaseState;
907  if (FuncletStart == MF->begin()) {
908  BaseState = NullState;
909  StartLabel = Asm->getFunctionBegin();
910  } else {
911  auto *FuncletPad =
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);
916  }
917  assert(StartLabel && "need local function start label");
918  IPToStateTable.push_back(
919  std::make_pair(create32bitRef(StartLabel), BaseState));
920 
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;
928  if (!ChangeLabel)
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.
934  }
935  }
936 }
937 
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.
944 
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
948  // used.
949  int64_t Offset = 0;
950  int FI = FuncInfo.EHRegNodeFrameIndex;
951  if (FI != INT_MAX) {
953  Offset = TFI->getNonLocalFrameIndexReference(*Asm->MF, FI);
954  }
955 
956  MCContext &Ctx = Asm->OutContext;
957  MCSymbol *ParentFrameOffset =
958  Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName);
959  Asm->OutStreamer->EmitAssignment(ParentFrameOffset,
960  MCConstantExpr::create(Offset, Ctx));
961 }
962 
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();
970 
971  bool VerboseAsm = OS.isVerboseAsm();
972  auto AddComment = [&](const Twine &Comment) {
973  if (VerboseAsm)
974  OS.AddComment(Comment);
975  };
976 
977  const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
978  emitEHRegistrationOffsetLabel(FuncInfo, FLinkageName);
979 
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);
984 
985  const Function *Per =
987  StringRef PerName = Per->getName();
988  int BaseState = -1;
989  if (PerName == "_except_handler4") {
990  // The LSDA for _except_handler4 starts with this struct, followed by the
991  // scope table:
992  //
993  // struct EH4ScopeTable {
994  // int32_t GSCookieOffset;
995  // int32_t GSCookieXOROffset;
996  // int32_t EHCookieOffset;
997  // int32_t EHCookieXOROffset;
998  // ScopeTableEntry ScopeRecord[];
999  // };
1000  //
1001  // Offsets are %ebp relative.
1002  //
1003  // The GS cookie is present only if the function needs stack protection.
1004  // GSCookieOffset = -2 means that GS cookie is not used.
1005  //
1006  // The EH cookie is always present.
1007  //
1008  // Check is done the following way:
1009  // (ebp+CookieXOROffset) ^ [ebp+CookieOffset] == _security_cookie
1010 
1011  // Retrieve the Guard Stack slot.
1012  int GSCookieOffset = -2;
1013  const MachineFrameInfo &MFI = MF->getFrameInfo();
1014  if (MFI.hasStackProtectorIndex()) {
1015  unsigned UnusedReg;
1016  const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
1017  int SSPIdx = MFI.getStackProtectorIndex();
1018  GSCookieOffset = TFI->getFrameIndexReference(*MF, SSPIdx, UnusedReg);
1019  }
1020 
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) {
1025  unsigned UnusedReg;
1026  const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
1027  int EHGuardIdx = FuncInfo.EHGuardFrameIndex;
1028  EHCookieOffset = TFI->getFrameIndexReference(*MF, EHGuardIdx, UnusedReg);
1029  }
1030 
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);
1039  BaseState = -2;
1040  }
1041 
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);
1056  }
1057 }
1058 
1059 static int getTryRank(const WinEHFuncInfo &FuncInfo, int State) {
1060  int Rank = 0;
1061  while (State != -1) {
1062  ++Rank;
1063  State = FuncInfo.ClrEHUnwindMap[State].TryParentState;
1064  }
1065  return Rank;
1066 }
1067 
1068 static int getTryAncestor(const WinEHFuncInfo &FuncInfo, int Left, int Right) {
1069  int LeftRank = getTryRank(FuncInfo, Left);
1070  int RightRank = getTryRank(FuncInfo, Right);
1071 
1072  while (LeftRank < RightRank) {
1073  Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState;
1074  --RightRank;
1075  }
1076 
1077  while (RightRank < LeftRank) {
1078  Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState;
1079  --LeftRank;
1080  }
1081 
1082  while (Left != Right) {
1083  Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState;
1084  Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState;
1085  }
1086 
1087  return Left;
1088 }
1089 
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();
1098 
1099  // A ClrClause describes a protected region.
1100  struct ClrClause {
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
1105  };
1106  SmallVector<ClrClause, 8> Clauses;
1107 
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!");
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");
1122  }
1123  // Map the main function to the NullState.
1124  HandlerStates[&MF->front()] = NullState;
1125 
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);
1131 
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.
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);
1151 
1152  // Visit the root function and each funclet.
1153  for (MachineFunction::const_iterator FuncletStart = MF->begin(),
1154  FuncletEnd = MF->begin(),
1155  End = MF->end();
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);
1163  break;
1164  }
1165  }
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;
1172  }
1173 
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;
1196  }
1197 
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
1201  // it.
1202  for (int EnteredState = StateChange.NewState;
1203  EnteredState != CurrentState;
1204  EnteredState =
1205  FuncInfo.ClrEHUnwindMap[EnteredState].TryParentState) {
1206  int &MinEnclosingState = MinClauseMap[EnteredState];
1207  if (FuncletState < MinEnclosingState)
1208  MinEnclosingState = FuncletState;
1209  }
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;
1215  }
1216  }
1217  assert(HandlerStack.empty());
1218  }
1219 
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 :
1224  /*
1225  struct CORINFO_EH_CLAUSE
1226  {
1227  CORINFO_EH_CLAUSE_FLAGS Flags; // actually a CorExceptionFlag
1228  DWORD TryOffset;
1229  DWORD TryLength; // actually TryEndOffset
1230  DWORD HandlerOffset;
1231  DWORD HandlerLength; // actually HandlerEndOffset
1232  union
1233  {
1234  DWORD ClassToken; // use for catch clauses
1235  DWORD FilterOffset; // use for filter clauses
1236  };
1237  };
1238 
1239  enum CORINFO_EH_CLAUSE_FLAGS
1240  {
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
1245  };
1246  typedef enum CorExceptionFlag
1247  {
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
1256  } CorExceptionFlag;
1257  */
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.
1270 
1271  const MCExpr *ClauseBegin =
1272  getOffsetPlusOne(Clause.StartLabel, FuncBeginSym);
1273  const MCExpr *ClauseEnd = getOffsetPlusOne(Clause.EndLabel, FuncBeginSym);
1274 
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);
1281 
1282  uint32_t Flags = 0;
1283  switch (Entry.HandlerType) {
1284  case ClrHandlerType::Catch:
1285  // Leaving bits 0-2 clear indicates catch.
1286  break;
1288  Flags |= 1;
1289  break;
1291  Flags |= 2;
1292  break;
1293  case ClrHandlerType::Fault:
1294  Flags |= 4;
1295  break;
1296  }
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]);
1301  Flags |= 8;
1302  }
1303  OS.EmitIntValue(Flags, 4);
1304 
1305  // Write the clause start/end
1306  OS.EmitValue(ClauseBegin, 4);
1307  OS.EmitValue(ClauseEnd, 4);
1308 
1309  // Write out the handler start/end
1310  OS.EmitValue(HandlerBegin, 4);
1311  OS.EmitValue(HandlerEnd, 4);
1312 
1313  // Write out the type token or filter offset
1314  assert(Entry.HandlerType != ClrHandlerType::Filter && "NYI: filters");
1315  OS.EmitIntValue(Entry.TypeToken, 4);
1316  }
1317 }
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&... Ranges)
Concatenated range across two or more ranges.
Definition: STLExtras.h:953
MBBOrBasicBlock Handler
Definition: WinEHFuncInfo.h:82
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:214
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:641
bool usesWindowsCFI() const
Definition: MCAsmInfo.h:590
SmallVector< WinEHHandlerType, 1 > HandlerArray
Definition: WinEHFuncInfo.h:76
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
bool isCall(QueryType Type=AnyInBundle) const
Definition: MachineInstr.h:635
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
static int getTryAncestor(const WinEHFuncInfo &FuncInfo, int Left, int Right)
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:331
MBBOrBasicBlock Cleanup
Definition: WinEHFuncInfo.h:42
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static MCSymbol * getMCSymbolForMBB(AsmPrinter *Asm, const MachineBasicBlock *MBB)
Retrieve the MCSymbol for a GlobalValue or MachineBasicBlock.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
bool hasStackProtectorIndex() const
SmallVector< SEHUnwindMapEntry, 4 > SEHUnwindMap
Definition: WinEHFuncInfo.h:97
virtual int getFrameIndexReference(const MachineFunction &MF, int FI, unsigned &FrameReg) const
getFrameIndexReference - This method should return the base register and offset used to reference a f...
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:88
bool isCleanupFuncletEntry() const
Returns true if this is the entry block of a cleanup funclet.
void beginFunclet(const MachineBasicBlock &MBB, MCSymbol *Sym) override
Emit target-specific EH funclet machinery.
DenseMap< const FuncletPadInst *, int > FuncletBaseStateMap
Definition: WinEHFuncInfo.h:92
static bool callToNoUnwindFunction(const MachineInstr *MI)
Return `true&#39; if this is a call to a function marked `nounwind&#39;.
Definition: EHStreamer.cpp:168
virtual const TargetLowering * getTargetLowering() const
MCSymbol * getOrCreateParentFrameOffsetSymbol(StringRef FuncName)
Definition: MCContext.cpp:146
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
Definition: DataLayout.h:388
F(f)
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:96
virtual int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI, unsigned &FrameReg, bool IgnoreSPUpdates) const
Same as getFrameIndexReference, except that the stack pointer (as opposed to the frame pointer) will ...
T get() const
Returns the value of the specified pointer type.
Definition: PointerUnion.h:205
MachineModuleInfo * MMI
Collected machine module information.
Definition: EHStreamer.h:36
MBBOrBasicBlock Handler
Holds the __except or __finally basic block.
Definition: WinEHFuncInfo.h:57
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwises returns null...
Definition: PointerUnion.h:212
void endModule() override
Emit all exception information that should come after the content.
Emits exception handling directives.
Definition: EHStreamer.h:30
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
bool isAArch64() const
Tests whether the target is AArch64 (little and big endian).
Definition: Triple.h:704
static const MCBinaryExpr * createDiv(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:479
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
unsigned getAlignment() const
getAlignment - Return the alignment (log2, not bytes) of the function.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
APInt operator*(APInt a, uint64_t RHS)
Definition: APInt.h:2090
virtual unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const
int ToState
If unwinding continues through this handler, transition to the handler at this state.
Definition: WinEHFuncInfo.h:49
Context object for machine code objects.
Definition: MCContext.h:64
virtual void AddComment(const Twine &T, bool EOL=true)
Add a textual comment.
Definition: MCStreamer.h:309
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:554
MCSymbol * getFunctionBegin() const
Definition: AsmPrinter.h:208
virtual int getNonLocalFrameIndexReference(const MachineFunction &MF, int FI) const
getNonLocalFrameIndexReference - This method returns the offset used to reference a frame index locat...
void tidyLandingPads(DenseMap< MCSymbol *, uintptr_t > *LPMap=nullptr, bool TidyIfNoBeginLabels=true)
Remap landing pad labels and remove any deleted landing pads.
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition: Function.h:726
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they&#39;re not in a MachineFuncti...
Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT))
Definition: COFF.h:265
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:469
void endFunclet() override
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:128
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:159
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false)
Definition: MCExpr.cpp:169
union llvm::WinEHHandlerType::@200 CatchObj
The CatchObj starts out life as an LLVM alloca and is eventually turned frame index.
Similar to CxxUnwindMapEntry, but supports SEH filters.
Definition: WinEHFuncInfo.h:46
SmallVector< ClrEHUnwindMapEntry, 4 > ClrEHUnwindMap
Definition: WinEHFuncInfo.h:98
Streaming machine code generation interface.
Definition: MCStreamer.h:188
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:223
MCSymbol * CurrentFnSym
The symbol for the current function.
Definition: AsmPrinter.h:112
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:84
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool hasEHFunclets() const
WinException(AsmPrinter *A)
void EmitAlignment(unsigned NumBits, const GlobalObject *GV=nullptr) const
Emit an alignment directive to the specified power of two boundary.
unsigned getAlignment() const
Return alignment of the basic block.
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:81
int getStackProtectorIndex() const
Return the index for the stack protector object.
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
void endFunction(const MachineFunction *) override
Gather and emit post-function exception information.
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
bool isEHLabel() const
Definition: MachineInstr.h:992
const Triple & getTargetTriple() const
const Constant * stripPointerCasts() const
Definition: Constant.h:177
const MachineBasicBlock & front() const
size_t size() const
Definition: SmallVector.h:52
AsmPrinter * Asm
Target of directive emission.
Definition: EHStreamer.h:33
MBBOrBasicBlock Handler
Definition: WinEHFuncInfo.h:69
void beginFunction(const MachineFunction *MF) override
Gather pre-function exception information.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
SmallVector< CxxUnwindMapEntry, 4 > CxxUnwindMap
Definition: WinEHFuncInfo.h:95
const std::vector< LandingPadInfo > & getLandingPads() const
Return a reference to the landing pad info for the current function.
const Function * Filter
Holds the filter expression function.
Definition: WinEHFuncInfo.h:54
Iterator for intrusive lists based on ilist_node.
MCSymbol * getOrCreateLSDASymbol(StringRef FuncName)
Definition: MCContext.cpp:151
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:443
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Module.h This file contains the declarations for the Module class.
void markFunctionEnd() override
Information about stack frame layout on the target.
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:374
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling...
MCSymbol * getFunctionEnd() const
Definition: AsmPrinter.h:209
const Function & getFunction() const
Return the LLVM function that this machine code represents.
bool needsUnwindTableEntry() const
True if this function needs an unwind table.
Definition: Function.h:594
SmallVector< WinEHTryBlockMapEntry, 4 > TryBlockMap
Definition: WinEHFuncInfo.h:96
A range adaptor for a pair of iterators.
This file contains constants used for implementing Dwarf debug support.
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character &#39;\1&#39;, drop it.
Definition: GlobalValue.h:481
GlobalVariable * TypeDescriptor
Definition: WinEHFuncInfo.h:68
~WinException() override
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:1968
Representation of each machine instruction.
Definition: MachineInstr.h:64
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:127
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
bool isEHFuncletEntry() const
Returns true if this is the entry block of an EH funclet.
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
#define I(x, y, z)
Definition: MD5.cpp:58
virtual const TargetFrameLowering * getFrameLowering() const
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
ClrHandlerType HandlerType
Definition: WinEHFuncInfo.h:87
const Module * getModule() const
static int getTryRank(const WinEHFuncInfo &FuncInfo, int State)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSymbol * emitExceptionTable()
Emit landing pads and actions.
Definition: EHStreamer.cpp:349
MCSymbol * getMCSymbol() const
LLVM Value Representation.
Definition: Value.h:72
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Definition: Function.cpp:1384
unsigned getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:351
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:218
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1966
virtual MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const
virtual bool isVerboseAsm() const
Return true if this streamer supports verbose assembly and if it is enabled.
Definition: MCStreamer.h:286
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:416
bool isNoOpWithoutInvoke(EHPersonality Pers)
Return true if this personality may be safely removed if there are no invoke instructions remaining i...
static Expected< BitVector > scan(StringRef &S, StringRef Original)
Definition: GlobPattern.cpp:66
This file describes how to lower LLVM code to machine code.
A function that returns a base type.
Definition: COFF.h:261