blob: 92df09b7d6a23ad5d6f9498252002d1451b5d152 [file] [log] [blame]
Reid Kleckner60b640b2015-05-28 22:47:01 +00001//===-- CodeGen/AsmPrinter/WinException.cpp - Dwarf Exception Impl ------===//
Charles Davis91ed7992011-05-27 23:47:32 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing Win64 exception info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
Reid Kleckner60b640b2015-05-28 22:47:01 +000014#include "WinException.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000016#include "llvm/BinaryFormat/COFF.h"
17#include "llvm/BinaryFormat/Dwarf.h"
Charles Davis91ed7992011-05-27 23:47:32 +000018#include "llvm/CodeGen/AsmPrinter.h"
Charles Davis91ed7992011-05-27 23:47:32 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000022#include "llvm/CodeGen/TargetFrameLowering.h"
23#include "llvm/CodeGen/TargetLowering.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000024#include "llvm/CodeGen/TargetSubtargetInfo.h"
David Majnemercde33032015-03-30 22:58:10 +000025#include "llvm/CodeGen/WinEHFuncInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/DataLayout.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000027#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Module.h"
Charles Davis91ed7992011-05-27 23:47:32 +000029#include "llvm/MC/MCAsmInfo.h"
30#include "llvm/MC/MCContext.h"
31#include "llvm/MC/MCExpr.h"
32#include "llvm/MC/MCSection.h"
33#include "llvm/MC/MCStreamer.h"
34#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/FormattedStream.h"
David Blaikie6054e652018-03-23 23:58:19 +000037#include "llvm/Target/TargetLoweringObjectFile.h"
Charles Davis91ed7992011-05-27 23:47:32 +000038#include "llvm/Target/TargetOptions.h"
Charles Davis91ed7992011-05-27 23:47:32 +000039using namespace llvm;
40
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000041WinException::WinException(AsmPrinter *A) : EHStreamer(A) {
42 // MSVC's EH tables are always composed of 32-bit words. All known 64-bit
43 // platforms use an imagerel32 relocation to refer to symbols.
44 useImageRel32 = (A->getDataLayout().getPointerSizeInBits() == 64);
Sanjin Sijaric96f2ea32018-10-27 06:13:06 +000045 isAArch64 = Asm->TM.getTargetTriple().isAArch64();
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000046}
Charles Davis91ed7992011-05-27 23:47:32 +000047
Reid Kleckner60b640b2015-05-28 22:47:01 +000048WinException::~WinException() {}
Charles Davis91ed7992011-05-27 23:47:32 +000049
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000050/// endModule - Emit all exception information that should come after the
Charles Davis91ed7992011-05-27 23:47:32 +000051/// content.
Reid Kleckner60b640b2015-05-28 22:47:01 +000052void WinException::endModule() {
Reid Kleckner2bc93ca2015-06-10 01:02:30 +000053 auto &OS = *Asm->OutStreamer;
54 const Module *M = MMI->getModule();
Reid Klecknerca6ef662015-06-10 01:13:44 +000055 for (const Function &F : *M)
56 if (F.hasFnAttribute("safeseh"))
Reid Kleckner2bc93ca2015-06-10 01:02:30 +000057 OS.EmitCOFFSafeSEH(Asm->getSymbol(&F));
Charles Davis91ed7992011-05-27 23:47:32 +000058}
59
Reid Kleckner60b640b2015-05-28 22:47:01 +000060void WinException::beginFunction(const MachineFunction *MF) {
Charles Davis5638b9f2011-05-28 04:21:04 +000061 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
62
63 // If any landing pads survive, we need an EH table.
Matthias Braund0ee66c2016-12-01 19:32:15 +000064 bool hasLandingPads = !MF->getLandingPads().empty();
65 bool hasEHFunclets = MF->hasEHFunclets();
Charles Davis5638b9f2011-05-28 04:21:04 +000066
Matthias Braunf1caa282017-12-15 22:22:58 +000067 const Function &F = MF->getFunction();
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000068
Reid Kleckner8819c732017-03-20 17:45:59 +000069 shouldEmitMoves = Asm->needsSEHMoves() && MF->hasWinCFI();
Charles Davis5638b9f2011-05-28 04:21:04 +000070
71 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
72 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Charles Davis5638b9f2011-05-28 04:21:04 +000073
Reid Kleckner9cb915b2016-09-30 22:10:12 +000074 EHPersonality Per = EHPersonality::Unknown;
75 const Function *PerFn = nullptr;
Matthias Braunf1caa282017-12-15 22:22:58 +000076 if (F.hasPersonalityFn()) {
77 PerFn = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
Reid Kleckner9cb915b2016-09-30 22:10:12 +000078 Per = classifyEHPersonality(PerFn);
79 }
80
Matthias Braunf1caa282017-12-15 22:22:58 +000081 bool forceEmitPersonality = F.hasPersonalityFn() &&
Reid Kleckner9cb915b2016-09-30 22:10:12 +000082 !isNoOpWithoutInvoke(Per) &&
Matthias Braunf1caa282017-12-15 22:22:58 +000083 F.needsUnwindTableEntry();
Keno Fischeraff703a2015-07-14 19:22:51 +000084
Reid Kleckner0e288232015-08-27 23:27:47 +000085 shouldEmitPersonality =
86 forceEmitPersonality || ((hasLandingPads || hasEHFunclets) &&
Reid Kleckner9cb915b2016-09-30 22:10:12 +000087 PerEncoding != dwarf::DW_EH_PE_omit && PerFn);
Charles Davis5638b9f2011-05-28 04:21:04 +000088
89 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
90 shouldEmitLSDA = shouldEmitPersonality &&
91 LSDAEncoding != dwarf::DW_EH_PE_omit;
92
Reid Kleckner0e288232015-08-27 23:27:47 +000093 // If we're not using CFI, we don't want the CFI or the personality, but we
94 // might want EH tables if we had EH pads.
Reid Kleckner8819c732017-03-20 17:45:59 +000095 if (!Asm->MAI->usesWindowsCFI()) {
Reid Kleckner9cb915b2016-09-30 22:10:12 +000096 if (Per == EHPersonality::MSVC_X86SEH && !hasEHFunclets) {
97 // If this is 32-bit SEH and we don't have any funclets (really invokes),
98 // make sure we emit the parent offset label. Some unreferenced filter
99 // functions may still refer to it.
100 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
101 StringRef FLinkageName =
Matthias Braunf1caa282017-12-15 22:22:58 +0000102 GlobalValue::dropLLVMManglingEscape(MF->getFunction().getName());
Reid Kleckner9cb915b2016-09-30 22:10:12 +0000103 emitEHRegistrationOffsetLabel(FuncInfo, FLinkageName);
104 }
David Majnemerbfa5b982015-10-10 00:04:29 +0000105 shouldEmitLSDA = hasEHFunclets;
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000106 shouldEmitPersonality = false;
107 return;
108 }
David Majnemera225a192015-03-31 22:35:44 +0000109
David Majnemera80c1512015-09-29 20:12:33 +0000110 beginFunclet(MF->front(), Asm->CurrentFnSym);
Charles Davis91ed7992011-05-27 23:47:32 +0000111}
112
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000113/// endFunction - Gather and emit post-function exception information.
Charles Davis91ed7992011-05-27 23:47:32 +0000114///
Reid Kleckner60b640b2015-05-28 22:47:01 +0000115void WinException::endFunction(const MachineFunction *MF) {
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000116 if (!shouldEmitPersonality && !shouldEmitMoves && !shouldEmitLSDA)
Charles Davis5638b9f2011-05-28 04:21:04 +0000117 return;
118
Matthias Braunf1caa282017-12-15 22:22:58 +0000119 const Function &F = MF->getFunction();
Reid Kleckner9a1a9192015-07-13 20:41:46 +0000120 EHPersonality Per = EHPersonality::Unknown;
Matthias Braunf1caa282017-12-15 22:22:58 +0000121 if (F.hasPersonalityFn())
122 Per = classifyEHPersonality(F.getPersonalityFn()->stripPointerCasts());
Reid Kleckner3e9fadf2015-04-15 18:48:15 +0000123
Joseph Tremoulet2afea542015-10-06 20:28:16 +0000124 // Get rid of any dead landing pads if we're not using funclets. In funclet
125 // schemes, the landing pad is not actually reachable. It only exists so
126 // that we can emit the right table data.
Matthias Braund0ee66c2016-12-01 19:32:15 +0000127 if (!isFuncletEHPersonality(Per)) {
128 MachineFunction *NonConstMF = const_cast<MachineFunction*>(MF);
129 NonConstMF->tidyLandingPads();
130 }
Charles Davisa5752262011-05-30 00:13:34 +0000131
David Majnemera80c1512015-09-29 20:12:33 +0000132 endFunclet();
133
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000134 // endFunclet will emit the necessary .xdata tables for x64 SEH.
Matthias Braund0ee66c2016-12-01 19:32:15 +0000135 if (Per == EHPersonality::MSVC_Win64SEH && MF->hasEHFunclets())
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000136 return;
137
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000138 if (shouldEmitPersonality || shouldEmitLSDA) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000139 Asm->OutStreamer->PushSection();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000140
Reid Kleckner97837b72016-05-02 23:22:18 +0000141 // Just switch sections to the right xdata section.
142 MCSection *XData = Asm->OutStreamer->getAssociatedXDataSection(
143 Asm->OutStreamer->getCurrentSectionOnly());
David Majnemera80c1512015-09-29 20:12:33 +0000144 Asm->OutStreamer->SwitchSection(XData);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000145
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000146 // Emit the tables appropriate to the personality function in use. If we
147 // don't recognize the personality, assume it uses an Itanium-style LSDA.
Reid Kleckner2d5fb682015-02-14 00:21:02 +0000148 if (Per == EHPersonality::MSVC_Win64SEH)
Reid Kleckner94b704c2015-09-09 21:10:03 +0000149 emitCSpecificHandlerTable(MF);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000150 else if (Per == EHPersonality::MSVC_X86SEH)
Reid Klecknerf12c0302015-06-09 21:42:19 +0000151 emitExceptHandlerTable(MF);
David Majnemercde33032015-03-30 22:58:10 +0000152 else if (Per == EHPersonality::MSVC_CXX)
153 emitCXXFrameHandler3Table(MF);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000154 else if (Per == EHPersonality::CoreCLR)
155 emitCLRExceptionTable(MF);
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000156 else
Reid Kleckner0a57f652015-01-14 01:05:27 +0000157 emitExceptionTable();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000158
Lang Hames9ff69c82015-04-24 19:11:51 +0000159 Asm->OutStreamer->PopSection();
Charles Davisa5752262011-05-30 00:13:34 +0000160 }
David Majnemera80c1512015-09-29 20:12:33 +0000161}
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000162
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000163/// Retrieve the MCSymbol for a GlobalValue or MachineBasicBlock.
David Majnemerbfa5b982015-10-10 00:04:29 +0000164static MCSymbol *getMCSymbolForMBB(AsmPrinter *Asm,
165 const MachineBasicBlock *MBB) {
166 if (!MBB)
David Majnemera80c1512015-09-29 20:12:33 +0000167 return nullptr;
David Majnemera80c1512015-09-29 20:12:33 +0000168
David Majnemerbfa5b982015-10-10 00:04:29 +0000169 assert(MBB->isEHFuncletEntry());
170
171 // Give catches and cleanups a name based off of their parent function and
172 // their funclet entry block's number.
173 const MachineFunction *MF = MBB->getParent();
Matthias Braunf1caa282017-12-15 22:22:58 +0000174 const Function &F = MF->getFunction();
175 StringRef FuncLinkageName = GlobalValue::dropLLVMManglingEscape(F.getName());
David Majnemerbfa5b982015-10-10 00:04:29 +0000176 MCContext &Ctx = MF->getContext();
177 StringRef HandlerPrefix = MBB->isCleanupFuncletEntry() ? "dtor" : "catch";
178 return Ctx.getOrCreateSymbol("?" + HandlerPrefix + "$" +
179 Twine(MBB->getNumber()) + "@?0?" +
180 FuncLinkageName + "@4HA");
David Majnemera80c1512015-09-29 20:12:33 +0000181}
182
183void WinException::beginFunclet(const MachineBasicBlock &MBB,
184 MCSymbol *Sym) {
185 CurrentFuncletEntry = &MBB;
186
Matthias Braunf1caa282017-12-15 22:22:58 +0000187 const Function &F = Asm->MF->getFunction();
David Majnemera80c1512015-09-29 20:12:33 +0000188 // If a symbol was not provided for the funclet, invent one.
189 if (!Sym) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000190 Sym = getMCSymbolForMBB(Asm, &MBB);
David Majnemera80c1512015-09-29 20:12:33 +0000191
192 // Describe our funclet symbol as a function with internal linkage.
193 Asm->OutStreamer->BeginCOFFSymbolDef(Sym);
194 Asm->OutStreamer->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
195 Asm->OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
196 << COFF::SCT_COMPLEX_TYPE_SHIFT);
197 Asm->OutStreamer->EndCOFFSymbolDef();
198
199 // We want our funclet's entry point to be aligned such that no nops will be
200 // present after the label.
201 Asm->EmitAlignment(std::max(Asm->MF->getAlignment(), MBB.getAlignment()),
Matthias Braunf1caa282017-12-15 22:22:58 +0000202 &F);
David Majnemera80c1512015-09-29 20:12:33 +0000203
204 // Now that we've emitted the alignment directive, point at our funclet.
205 Asm->OutStreamer->EmitLabel(Sym);
206 }
207
208 // Mark 'Sym' as starting our funclet.
Reid Kleckner92647362016-12-28 19:05:12 +0000209 if (shouldEmitMoves || shouldEmitPersonality) {
210 CurrentFuncletTextSection = Asm->OutStreamer->getCurrentSectionOnly();
David Majnemera80c1512015-09-29 20:12:33 +0000211 Asm->OutStreamer->EmitWinCFIStartProc(Sym);
Reid Kleckner92647362016-12-28 19:05:12 +0000212 }
David Majnemera80c1512015-09-29 20:12:33 +0000213
214 if (shouldEmitPersonality) {
215 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
216 const Function *PerFn = nullptr;
217
218 // Determine which personality routine we are using for this funclet.
Matthias Braunf1caa282017-12-15 22:22:58 +0000219 if (F.hasPersonalityFn())
220 PerFn = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
David Majnemera80c1512015-09-29 20:12:33 +0000221 const MCSymbol *PersHandlerSym =
Eric Christopher4367c7f2016-09-16 07:33:15 +0000222 TLOF.getCFIPersonalitySymbol(PerFn, Asm->TM, MMI);
David Majnemera80c1512015-09-29 20:12:33 +0000223
Reid Kleckner785e7d22016-12-08 20:38:46 +0000224 // Do not emit a .seh_handler directives for cleanup funclets.
225 // FIXME: This means cleanup funclets cannot handle exceptions. Given that
226 // Clang doesn't produce EH constructs inside cleanup funclets and LLVM's
227 // inliner doesn't allow inlining them, this isn't a major problem in
228 // practice.
229 if (!CurrentFuncletEntry->isCleanupFuncletEntry())
David Majnemera80c1512015-09-29 20:12:33 +0000230 Asm->OutStreamer->EmitWinEHHandler(PersHandlerSym, true, true);
231 }
232}
233
234void WinException::endFunclet() {
235 // No funclet to process? Great, we have nothing to do.
236 if (!CurrentFuncletEntry)
237 return;
238
Matthias Braund0ee66c2016-12-01 19:32:15 +0000239 const MachineFunction *MF = Asm->MF;
David Majnemera80c1512015-09-29 20:12:33 +0000240 if (shouldEmitMoves || shouldEmitPersonality) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000241 const Function &F = MF->getFunction();
David Majnemera80c1512015-09-29 20:12:33 +0000242 EHPersonality Per = EHPersonality::Unknown;
Matthias Braunf1caa282017-12-15 22:22:58 +0000243 if (F.hasPersonalityFn())
244 Per = classifyEHPersonality(F.getPersonalityFn()->stripPointerCasts());
David Majnemera80c1512015-09-29 20:12:33 +0000245
Sanjin Sijaric96f2ea32018-10-27 06:13:06 +0000246 // On funclet exit, we emit a fake "function" end marker, so that the call
247 // to EmitWinEHHandlerData below can calculate the size of the funclet or
248 // function.
249 if (isAArch64) {
250 Asm->OutStreamer->SwitchSection(CurrentFuncletTextSection);
251 Asm->OutStreamer->EmitWinCFIFuncletOrFuncEnd();
252 MCSection *XData = Asm->OutStreamer->getAssociatedXDataSection(
253 Asm->OutStreamer->getCurrentSectionOnly());
254 Asm->OutStreamer->SwitchSection(XData);
255 }
256
David Majnemera80c1512015-09-29 20:12:33 +0000257 // Emit an UNWIND_INFO struct describing the prologue.
258 Asm->OutStreamer->EmitWinEHHandlerData();
259
David Majnemera80c1512015-09-29 20:12:33 +0000260 if (Per == EHPersonality::MSVC_CXX && shouldEmitPersonality &&
261 !CurrentFuncletEntry->isCleanupFuncletEntry()) {
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000262 // If this is a C++ catch funclet (or the parent function),
263 // emit a reference to the LSDA for the parent function.
Matthias Braunf1caa282017-12-15 22:22:58 +0000264 StringRef FuncLinkageName = GlobalValue::dropLLVMManglingEscape(F.getName());
David Majnemera80c1512015-09-29 20:12:33 +0000265 MCSymbol *FuncInfoXData = Asm->OutContext.getOrCreateSymbol(
266 Twine("$cppxdata$", FuncLinkageName));
267 Asm->OutStreamer->EmitValue(create32bitRef(FuncInfoXData), 4);
Matthias Braund0ee66c2016-12-01 19:32:15 +0000268 } else if (Per == EHPersonality::MSVC_Win64SEH && MF->hasEHFunclets() &&
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000269 !CurrentFuncletEntry->isEHFuncletEntry()) {
270 // If this is the parent function in Win64 SEH, emit the LSDA immediately
271 // following .seh_handlerdata.
Matthias Braund0ee66c2016-12-01 19:32:15 +0000272 emitCSpecificHandlerTable(MF);
David Majnemera80c1512015-09-29 20:12:33 +0000273 }
274
Reid Kleckner92647362016-12-28 19:05:12 +0000275 // Switch back to the funclet start .text section now that we are done
276 // writing to .xdata, and emit an .seh_endproc directive to mark the end of
277 // the function.
278 Asm->OutStreamer->SwitchSection(CurrentFuncletTextSection);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000279 Asm->OutStreamer->EmitWinCFIEndProc();
David Majnemera80c1512015-09-29 20:12:33 +0000280 }
281
282 // Let's make sure we don't try to end the same funclet twice.
283 CurrentFuncletEntry = nullptr;
Charles Davis91ed7992011-05-27 23:47:32 +0000284}
Reid Kleckner0a57f652015-01-14 01:05:27 +0000285
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000286const MCExpr *WinException::create32bitRef(const MCSymbol *Value) {
David Majnemercde33032015-03-30 22:58:10 +0000287 if (!Value)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000288 return MCConstantExpr::create(0, Asm->OutContext);
289 return MCSymbolRefExpr::create(Value, useImageRel32
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000290 ? MCSymbolRefExpr::VK_COFF_IMGREL32
291 : MCSymbolRefExpr::VK_None,
Reid Kleckner0a57f652015-01-14 01:05:27 +0000292 Asm->OutContext);
293}
294
Joseph Tremoulet3d0fbf12015-10-23 15:06:05 +0000295const MCExpr *WinException::create32bitRef(const GlobalValue *GV) {
296 if (!GV)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000297 return MCConstantExpr::create(0, Asm->OutContext);
Joseph Tremoulet3d0fbf12015-10-23 15:06:05 +0000298 return create32bitRef(Asm->getSymbol(GV));
David Majnemercde33032015-03-30 22:58:10 +0000299}
300
Sanjin Sijaric96f2ea32018-10-27 06:13:06 +0000301const MCExpr *WinException::getLabel(const MCSymbol *Label) {
302 if (isAArch64)
303 return MCSymbolRefExpr::create(Label, MCSymbolRefExpr::VK_COFF_IMGREL32,
304 Asm->OutContext);
Reid Klecknerc71d6272015-09-28 23:56:30 +0000305 return MCBinaryExpr::createAdd(create32bitRef(Label),
306 MCConstantExpr::create(1, Asm->OutContext),
307 Asm->OutContext);
308}
309
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000310const MCExpr *WinException::getOffset(const MCSymbol *OffsetOf,
311 const MCSymbol *OffsetFrom) {
312 return MCBinaryExpr::createSub(
313 MCSymbolRefExpr::create(OffsetOf, Asm->OutContext),
314 MCSymbolRefExpr::create(OffsetFrom, Asm->OutContext), Asm->OutContext);
315}
316
317const MCExpr *WinException::getOffsetPlusOne(const MCSymbol *OffsetOf,
318 const MCSymbol *OffsetFrom) {
319 return MCBinaryExpr::createAdd(getOffset(OffsetOf, OffsetFrom),
320 MCConstantExpr::create(1, Asm->OutContext),
321 Asm->OutContext);
322}
323
Reid Klecknerc20276d2015-11-17 21:10:25 +0000324int WinException::getFrameIndexOffset(int FrameIndex,
325 const WinEHFuncInfo &FuncInfo) {
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000326 const TargetFrameLowering &TFI = *Asm->MF->getSubtarget().getFrameLowering();
327 unsigned UnusedReg;
Sanjoy Das0ebc9612016-06-16 18:54:06 +0000328 if (Asm->MAI->usesWindowsCFI()) {
329 int Offset =
330 TFI.getFrameIndexReferencePreferSP(*Asm->MF, FrameIndex, UnusedReg,
331 /*IgnoreSPUpdates*/ true);
332 assert(UnusedReg ==
333 Asm->MF->getSubtarget()
334 .getTargetLowering()
335 ->getStackPointerRegisterToSaveRestore());
336 return Offset;
337 }
338
Reid Kleckner6ddae312015-11-05 21:09:49 +0000339 // For 32-bit, offsets should be relative to the end of the EH registration
340 // node. For 64-bit, it's relative to SP at the end of the prologue.
341 assert(FuncInfo.EHRegNodeEndOffset != INT_MAX);
342 int Offset = TFI.getFrameIndexReference(*Asm->MF, FrameIndex, UnusedReg);
343 Offset += FuncInfo.EHRegNodeEndOffset;
344 return Offset;
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000345}
346
Benjamin Kramer808d2a02015-10-05 21:20:26 +0000347namespace {
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000348
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000349/// Top-level state used to represent unwind to caller
350const int NullState = -1;
351
352struct InvokeStateChange {
353 /// EH Label immediately after the last invoke in the previous state, or
354 /// nullptr if the previous state was the null state.
355 const MCSymbol *PreviousEndLabel;
356
357 /// EH label immediately before the first invoke in the new state, or nullptr
358 /// if the new state is the null state.
359 const MCSymbol *NewStartLabel;
360
361 /// State of the invoke following NewStartLabel, or NullState to indicate
362 /// the presence of calls which may unwind to caller.
363 int NewState;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000364};
365
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000366/// Iterator that reports all the invoke state changes in a range of machine
367/// basic blocks. Changes to the null state are reported whenever a call that
368/// may unwind to caller is encountered. The MBB range is expected to be an
369/// entire function or funclet, and the start and end of the range are treated
370/// as being in the NullState even if there's not an unwind-to-caller call
371/// before the first invoke or after the last one (i.e., the first state change
372/// reported is the first change to something other than NullState, and a
373/// change back to NullState is always reported at the end of iteration).
374class InvokeStateChangeIterator {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000375 InvokeStateChangeIterator(const WinEHFuncInfo &EHInfo,
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000376 MachineFunction::const_iterator MFI,
377 MachineFunction::const_iterator MFE,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000378 MachineBasicBlock::const_iterator MBBI,
379 int BaseState)
380 : EHInfo(EHInfo), MFI(MFI), MFE(MFE), MBBI(MBBI), BaseState(BaseState) {
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000381 LastStateChange.PreviousEndLabel = nullptr;
382 LastStateChange.NewStartLabel = nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000383 LastStateChange.NewState = BaseState;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000384 scan();
385 }
386
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000387public:
388 static iterator_range<InvokeStateChangeIterator>
Reid Klecknerc20276d2015-11-17 21:10:25 +0000389 range(const WinEHFuncInfo &EHInfo, MachineFunction::const_iterator Begin,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000390 MachineFunction::const_iterator End, int BaseState = NullState) {
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000391 // Reject empty ranges to simplify bookkeeping by ensuring that we can get
392 // the end of the last block.
393 assert(Begin != End);
394 auto BlockBegin = Begin->begin();
395 auto BlockEnd = std::prev(End)->end();
David Majnemer8a1c45d2015-12-12 05:38:55 +0000396 return make_range(
397 InvokeStateChangeIterator(EHInfo, Begin, End, BlockBegin, BaseState),
398 InvokeStateChangeIterator(EHInfo, End, End, BlockEnd, BaseState));
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000399 }
400
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000401 // Iterator methods.
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000402 bool operator==(const InvokeStateChangeIterator &O) const {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000403 assert(BaseState == O.BaseState);
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000404 // Must be visiting same block.
405 if (MFI != O.MFI)
406 return false;
407 // Must be visiting same isntr.
408 if (MBBI != O.MBBI)
409 return false;
410 // At end of block/instr iteration, we can still have two distinct states:
411 // one to report the final EndLabel, and another indicating the end of the
412 // state change iteration. Check for CurrentEndLabel equality to
413 // distinguish these.
414 return CurrentEndLabel == O.CurrentEndLabel;
415 }
416
417 bool operator!=(const InvokeStateChangeIterator &O) const {
418 return !operator==(O);
419 }
420 InvokeStateChange &operator*() { return LastStateChange; }
421 InvokeStateChange *operator->() { return &LastStateChange; }
422 InvokeStateChangeIterator &operator++() { return scan(); }
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000423
424private:
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000425 InvokeStateChangeIterator &scan();
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000426
Reid Klecknerc20276d2015-11-17 21:10:25 +0000427 const WinEHFuncInfo &EHInfo;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000428 const MCSymbol *CurrentEndLabel = nullptr;
429 MachineFunction::const_iterator MFI;
430 MachineFunction::const_iterator MFE;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000431 MachineBasicBlock::const_iterator MBBI;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000432 InvokeStateChange LastStateChange;
433 bool VisitingInvoke = false;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000434 int BaseState;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000435};
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000436
Benjamin Kramer808d2a02015-10-05 21:20:26 +0000437} // end anonymous namespace
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000438
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000439InvokeStateChangeIterator &InvokeStateChangeIterator::scan() {
440 bool IsNewBlock = false;
441 for (; MFI != MFE; ++MFI, IsNewBlock = true) {
442 if (IsNewBlock)
443 MBBI = MFI->begin();
444 for (auto MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
445 const MachineInstr &MI = *MBBI;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000446 if (!VisitingInvoke && LastStateChange.NewState != BaseState &&
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000447 MI.isCall() && !EHStreamer::callToNoUnwindFunction(&MI)) {
448 // Indicate a change of state to the null state. We don't have
449 // start/end EH labels handy but the caller won't expect them for
450 // null state regions.
451 LastStateChange.PreviousEndLabel = CurrentEndLabel;
452 LastStateChange.NewStartLabel = nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000453 LastStateChange.NewState = BaseState;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000454 CurrentEndLabel = nullptr;
455 // Don't re-visit this instr on the next scan
456 ++MBBI;
457 return *this;
458 }
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000459
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000460 // All other state changes are at EH labels before/after invokes.
461 if (!MI.isEHLabel())
462 continue;
463 MCSymbol *Label = MI.getOperand(0).getMCSymbol();
464 if (Label == CurrentEndLabel) {
465 VisitingInvoke = false;
466 continue;
467 }
David Majnemer8a1c45d2015-12-12 05:38:55 +0000468 auto InvokeMapIter = EHInfo.LabelToStateMap.find(Label);
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000469 // Ignore EH labels that aren't the ones inserted before an invoke
David Majnemer8a1c45d2015-12-12 05:38:55 +0000470 if (InvokeMapIter == EHInfo.LabelToStateMap.end())
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000471 continue;
472 auto &StateAndEnd = InvokeMapIter->second;
473 int NewState = StateAndEnd.first;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000474 // Keep track of the fact that we're between EH start/end labels so
475 // we know not to treat the inoke we'll see as unwinding to caller.
476 VisitingInvoke = true;
477 if (NewState == LastStateChange.NewState) {
478 // The state isn't actually changing here. Record the new end and
479 // keep going.
480 CurrentEndLabel = StateAndEnd.second;
481 continue;
482 }
483 // Found a state change to report
484 LastStateChange.PreviousEndLabel = CurrentEndLabel;
485 LastStateChange.NewStartLabel = Label;
486 LastStateChange.NewState = NewState;
487 // Start keeping track of the new current end
488 CurrentEndLabel = StateAndEnd.second;
489 // Don't re-visit this instr on the next scan
490 ++MBBI;
491 return *this;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000492 }
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000493 }
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000494 // Iteration hit the end of the block range.
David Majnemer8a1c45d2015-12-12 05:38:55 +0000495 if (LastStateChange.NewState != BaseState) {
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000496 // Report the end of the last new state
497 LastStateChange.PreviousEndLabel = CurrentEndLabel;
498 LastStateChange.NewStartLabel = nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000499 LastStateChange.NewState = BaseState;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000500 // Leave CurrentEndLabel non-null to distinguish this state from end.
501 assert(CurrentEndLabel != nullptr);
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000502 return *this;
503 }
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000504 // We've reported all state changes and hit the end state.
505 CurrentEndLabel = nullptr;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000506 return *this;
507}
508
Reid Kleckner0a57f652015-01-14 01:05:27 +0000509/// Emit the language-specific data that __C_specific_handler expects. This
510/// handler lives in the x64 Microsoft C runtime and allows catching or cleaning
511/// up after faults with __try, __except, and __finally. The typeinfo values
512/// are not really RTTI data, but pointers to filter functions that return an
513/// integer (1, 0, or -1) indicating how to handle the exception. For __finally
514/// blocks and other cleanups, the landing pad label is zero, and the filter
515/// function is actually a cleanup handler with the same prototype. A catch-all
516/// entry is modeled with a null filter function field and a non-zero landing
517/// pad label.
518///
519/// Possible filter function return values:
520/// EXCEPTION_EXECUTE_HANDLER (1):
521/// Jump to the landing pad label after cleanups.
522/// EXCEPTION_CONTINUE_SEARCH (0):
523/// Continue searching this table or continue unwinding.
524/// EXCEPTION_CONTINUE_EXECUTION (-1):
525/// Resume execution at the trapping PC.
526///
527/// Inferred table structure:
528/// struct Table {
529/// int NumEntries;
530/// struct Entry {
531/// imagerel32 LabelStart;
532/// imagerel32 LabelEnd;
Reid Klecknerf690f502015-01-22 02:27:44 +0000533/// imagerel32 FilterOrFinally; // One means catch-all.
Reid Kleckner14e77352015-10-09 23:34:53 +0000534/// imagerel32 LabelLPad; // Zero means __finally.
Reid Kleckner0a57f652015-01-14 01:05:27 +0000535/// } Entries[NumEntries];
536/// };
Reid Kleckner94b704c2015-09-09 21:10:03 +0000537void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) {
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000538 auto &OS = *Asm->OutStreamer;
539 MCContext &Ctx = Asm->OutContext;
Reid Klecknerc20276d2015-11-17 21:10:25 +0000540 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
Reid Kleckner7850c9f2015-12-15 23:40:58 +0000541
David Majnemer081e8fe2015-12-27 06:07:12 +0000542 bool VerboseAsm = OS.isVerboseAsm();
543 auto AddComment = [&](const Twine &Comment) {
544 if (VerboseAsm)
545 OS.AddComment(Comment);
546 };
547
Mandeep Singh Grang33c49c02019-01-16 19:52:59 +0000548 if (!isAArch64) {
549 // Emit a label assignment with the SEH frame offset so we can use it for
550 // llvm.eh.recoverfp.
551 StringRef FLinkageName =
552 GlobalValue::dropLLVMManglingEscape(MF->getFunction().getName());
553 MCSymbol *ParentFrameOffset =
554 Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName);
555 const MCExpr *MCOffset =
556 MCConstantExpr::create(FuncInfo.SEHSetFrameOffset, Ctx);
557 Asm->OutStreamer->EmitAssignment(ParentFrameOffset, MCOffset);
558 }
Reid Kleckner7850c9f2015-12-15 23:40:58 +0000559
Reid Kleckner14e77352015-10-09 23:34:53 +0000560 // Use the assembler to compute the number of table entries through label
561 // difference and division.
562 MCSymbol *TableBegin =
563 Ctx.createTempSymbol("lsda_begin", /*AlwaysAddSuffix=*/true);
564 MCSymbol *TableEnd =
565 Ctx.createTempSymbol("lsda_end", /*AlwaysAddSuffix=*/true);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000566 const MCExpr *LabelDiff = getOffset(TableEnd, TableBegin);
Reid Kleckner14e77352015-10-09 23:34:53 +0000567 const MCExpr *EntrySize = MCConstantExpr::create(16, Ctx);
568 const MCExpr *EntryCount = MCBinaryExpr::createDiv(LabelDiff, EntrySize, Ctx);
David Majnemer081e8fe2015-12-27 06:07:12 +0000569 AddComment("Number of call sites");
Reid Kleckner14e77352015-10-09 23:34:53 +0000570 OS.EmitValue(EntryCount, 4);
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000571
Reid Kleckner14e77352015-10-09 23:34:53 +0000572 OS.EmitLabel(TableBegin);
Reid Klecknereb7cd6c2015-10-09 23:05:54 +0000573
Reid Kleckner14e77352015-10-09 23:34:53 +0000574 // Iterate over all the invoke try ranges. Unlike MSVC, LLVM currently only
575 // models exceptions from invokes. LLVM also allows arbitrary reordering of
576 // the code, so our tables end up looking a bit different. Rather than
577 // trying to match MSVC's tables exactly, we emit a denormalized table. For
578 // each range of invokes in the same state, we emit table entries for all
579 // the actions that would be taken in that state. This means our tables are
580 // slightly bigger, which is OK.
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000581 const MCSymbol *LastStartLabel = nullptr;
582 int LastEHState = -1;
583 // Break out before we enter into a finally funclet.
584 // FIXME: We need to emit separate EH tables for cleanups.
585 MachineFunction::const_iterator End = MF->end();
586 MachineFunction::const_iterator Stop = std::next(MF->begin());
587 while (Stop != End && !Stop->isEHFuncletEntry())
588 ++Stop;
589 for (const auto &StateChange :
590 InvokeStateChangeIterator::range(FuncInfo, MF->begin(), Stop)) {
591 // Emit all the actions for the state we just transitioned out of
592 // if it was not the null state
593 if (LastEHState != -1)
594 emitSEHActionsForRange(FuncInfo, LastStartLabel,
595 StateChange.PreviousEndLabel, LastEHState);
596 LastStartLabel = StateChange.NewStartLabel;
597 LastEHState = StateChange.NewState;
Reid Kleckner0a57f652015-01-14 01:05:27 +0000598 }
Reid Kleckner14e77352015-10-09 23:34:53 +0000599
Reid Kleckner14e77352015-10-09 23:34:53 +0000600 OS.EmitLabel(TableEnd);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000601}
David Majnemercde33032015-03-30 22:58:10 +0000602
Reid Klecknerc20276d2015-11-17 21:10:25 +0000603void WinException::emitSEHActionsForRange(const WinEHFuncInfo &FuncInfo,
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000604 const MCSymbol *BeginLabel,
605 const MCSymbol *EndLabel, int State) {
Reid Klecknerd880dc72015-10-09 20:39:39 +0000606 auto &OS = *Asm->OutStreamer;
607 MCContext &Ctx = Asm->OutContext;
David Majnemer081e8fe2015-12-27 06:07:12 +0000608 bool VerboseAsm = OS.isVerboseAsm();
609 auto AddComment = [&](const Twine &Comment) {
610 if (VerboseAsm)
611 OS.AddComment(Comment);
612 };
613
Reid Klecknerd880dc72015-10-09 20:39:39 +0000614 assert(BeginLabel && EndLabel);
615 while (State != -1) {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000616 const SEHUnwindMapEntry &UME = FuncInfo.SEHUnwindMap[State];
Reid Klecknerd880dc72015-10-09 20:39:39 +0000617 const MCExpr *FilterOrFinally;
618 const MCExpr *ExceptOrNull;
619 auto *Handler = UME.Handler.get<MachineBasicBlock *>();
620 if (UME.IsFinally) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000621 FilterOrFinally = create32bitRef(getMCSymbolForMBB(Asm, Handler));
Reid Klecknerd880dc72015-10-09 20:39:39 +0000622 ExceptOrNull = MCConstantExpr::create(0, Ctx);
623 } else {
624 // For an except, the filter can be 1 (catch-all) or a function
625 // label.
626 FilterOrFinally = UME.Filter ? create32bitRef(UME.Filter)
627 : MCConstantExpr::create(1, Ctx);
628 ExceptOrNull = create32bitRef(Handler->getSymbol());
629 }
630
David Majnemer081e8fe2015-12-27 06:07:12 +0000631 AddComment("LabelStart");
Sanjin Sijaric96f2ea32018-10-27 06:13:06 +0000632 OS.EmitValue(getLabel(BeginLabel), 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000633 AddComment("LabelEnd");
Sanjin Sijaric96f2ea32018-10-27 06:13:06 +0000634 OS.EmitValue(getLabel(EndLabel), 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000635 AddComment(UME.IsFinally ? "FinallyFunclet" : UME.Filter ? "FilterFunction"
636 : "CatchAll");
Reid Klecknerd880dc72015-10-09 20:39:39 +0000637 OS.EmitValue(FilterOrFinally, 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000638 AddComment(UME.IsFinally ? "Null" : "ExceptionHandler");
Reid Klecknerd880dc72015-10-09 20:39:39 +0000639 OS.EmitValue(ExceptOrNull, 4);
640
641 assert(UME.ToState < State && "states should decrease");
642 State = UME.ToState;
643 }
644}
645
Reid Kleckner60b640b2015-05-28 22:47:01 +0000646void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000647 const Function &F = MF->getFunction();
Lang Hames9ff69c82015-04-24 19:11:51 +0000648 auto &OS = *Asm->OutStreamer;
Reid Klecknerc20276d2015-11-17 21:10:25 +0000649 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
David Majnemercde33032015-03-30 22:58:10 +0000650
Matthias Braunf1caa282017-12-15 22:22:58 +0000651 StringRef FuncLinkageName = GlobalValue::dropLLVMManglingEscape(F.getName());
David Majnemercde33032015-03-30 22:58:10 +0000652
Reid Klecknerc71d6272015-09-28 23:56:30 +0000653 SmallVector<std::pair<const MCExpr *, int>, 4> IPToStateTable;
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000654 MCSymbol *FuncInfoXData = nullptr;
655 if (shouldEmitPersonality) {
Reid Klecknerc71d6272015-09-28 23:56:30 +0000656 // If we're 64-bit, emit a pointer to the C++ EH data, and build a map from
657 // IPs to state numbers.
Reid Kleckner813f1b62015-09-16 22:14:46 +0000658 FuncInfoXData =
659 Asm->OutContext.getOrCreateSymbol(Twine("$cppxdata$", FuncLinkageName));
Reid Klecknerc71d6272015-09-28 23:56:30 +0000660 computeIP2StateTable(MF, FuncInfo, IPToStateTable);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000661 } else {
Reid Kleckner813f1b62015-09-16 22:14:46 +0000662 FuncInfoXData = Asm->OutContext.getOrCreateLSDASymbol(FuncLinkageName);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000663 }
664
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000665 int UnwindHelpOffset = 0;
666 if (Asm->MAI->usesWindowsCFI())
Reid Kleckner6ddae312015-11-05 21:09:49 +0000667 UnwindHelpOffset =
668 getFrameIndexOffset(FuncInfo.UnwindHelpFrameIdx, FuncInfo);
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000669
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000670 MCSymbol *UnwindMapXData = nullptr;
671 MCSymbol *TryBlockMapXData = nullptr;
672 MCSymbol *IPToStateXData = nullptr;
Reid Kleckner14e77352015-10-09 23:34:53 +0000673 if (!FuncInfo.CxxUnwindMap.empty())
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000674 UnwindMapXData = Asm->OutContext.getOrCreateSymbol(
Reid Kleckner813f1b62015-09-16 22:14:46 +0000675 Twine("$stateUnwindMap$", FuncLinkageName));
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000676 if (!FuncInfo.TryBlockMap.empty())
Reid Kleckner813f1b62015-09-16 22:14:46 +0000677 TryBlockMapXData =
678 Asm->OutContext.getOrCreateSymbol(Twine("$tryMap$", FuncLinkageName));
Reid Klecknerc71d6272015-09-28 23:56:30 +0000679 if (!IPToStateTable.empty())
Reid Kleckner813f1b62015-09-16 22:14:46 +0000680 IPToStateXData =
681 Asm->OutContext.getOrCreateSymbol(Twine("$ip2state$", FuncLinkageName));
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000682
David Majnemer081e8fe2015-12-27 06:07:12 +0000683 bool VerboseAsm = OS.isVerboseAsm();
684 auto AddComment = [&](const Twine &Comment) {
685 if (VerboseAsm)
686 OS.AddComment(Comment);
687 };
688
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000689 // FuncInfo {
690 // uint32_t MagicNumber
691 // int32_t MaxState;
692 // UnwindMapEntry *UnwindMap;
693 // uint32_t NumTryBlocks;
694 // TryBlockMapEntry *TryBlockMap;
695 // uint32_t IPMapEntries; // always 0 for x86
696 // IPToStateMapEntry *IPToStateMap; // always 0 for x86
697 // uint32_t UnwindHelp; // non-x86 only
698 // ESTypeList *ESTypeList;
699 // int32_t EHFlags;
700 // }
701 // EHFlags & 1 -> Synchronous exceptions only, no async exceptions.
702 // EHFlags & 2 -> ???
703 // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue.
Reid Kleckner85a24502015-07-10 00:08:49 +0000704 OS.EmitValueToAlignment(4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000705 OS.EmitLabel(FuncInfoXData);
David Majnemer081e8fe2015-12-27 06:07:12 +0000706
707 AddComment("MagicNumber");
708 OS.EmitIntValue(0x19930522, 4);
709
710 AddComment("MaxState");
711 OS.EmitIntValue(FuncInfo.CxxUnwindMap.size(), 4);
712
713 AddComment("UnwindMap");
714 OS.EmitValue(create32bitRef(UnwindMapXData), 4);
715
716 AddComment("NumTryBlocks");
717 OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4);
718
719 AddComment("TryBlockMap");
720 OS.EmitValue(create32bitRef(TryBlockMapXData), 4);
721
722 AddComment("IPMapEntries");
723 OS.EmitIntValue(IPToStateTable.size(), 4);
724
725 AddComment("IPToStateXData");
726 OS.EmitValue(create32bitRef(IPToStateXData), 4);
727
728 if (Asm->MAI->usesWindowsCFI()) {
729 AddComment("UnwindHelp");
730 OS.EmitIntValue(UnwindHelpOffset, 4);
731 }
732
733 AddComment("ESTypeList");
734 OS.EmitIntValue(0, 4);
735
736 AddComment("EHFlags");
737 OS.EmitIntValue(1, 4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000738
739 // UnwindMapEntry {
740 // int32_t ToState;
741 // void (*Action)();
742 // };
743 if (UnwindMapXData) {
744 OS.EmitLabel(UnwindMapXData);
Reid Kleckner14e77352015-10-09 23:34:53 +0000745 for (const CxxUnwindMapEntry &UME : FuncInfo.CxxUnwindMap) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000746 MCSymbol *CleanupSym =
747 getMCSymbolForMBB(Asm, UME.Cleanup.dyn_cast<MachineBasicBlock *>());
David Majnemer081e8fe2015-12-27 06:07:12 +0000748 AddComment("ToState");
749 OS.EmitIntValue(UME.ToState, 4);
750
751 AddComment("Action");
752 OS.EmitValue(create32bitRef(CleanupSym), 4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000753 }
754 }
755
756 // TryBlockMap {
757 // int32_t TryLow;
758 // int32_t TryHigh;
759 // int32_t CatchHigh;
760 // int32_t NumCatches;
761 // HandlerType *HandlerArray;
762 // };
763 if (TryBlockMapXData) {
764 OS.EmitLabel(TryBlockMapXData);
765 SmallVector<MCSymbol *, 1> HandlerMaps;
766 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000767 const WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000768
Reid Kleckner813f1b62015-09-16 22:14:46 +0000769 MCSymbol *HandlerMapXData = nullptr;
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000770 if (!TBME.HandlerArray.empty())
771 HandlerMapXData =
772 Asm->OutContext.getOrCreateSymbol(Twine("$handlerMap$")
773 .concat(Twine(I))
774 .concat("$")
Reid Kleckner813f1b62015-09-16 22:14:46 +0000775 .concat(FuncLinkageName));
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000776 HandlerMaps.push_back(HandlerMapXData);
777
Reid Kleckner813f1b62015-09-16 22:14:46 +0000778 // TBMEs should form intervals.
779 assert(0 <= TBME.TryLow && "bad trymap interval");
780 assert(TBME.TryLow <= TBME.TryHigh && "bad trymap interval");
781 assert(TBME.TryHigh < TBME.CatchHigh && "bad trymap interval");
Reid Kleckner14e77352015-10-09 23:34:53 +0000782 assert(TBME.CatchHigh < int(FuncInfo.CxxUnwindMap.size()) &&
Reid Kleckner813f1b62015-09-16 22:14:46 +0000783 "bad trymap interval");
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000784
David Majnemer081e8fe2015-12-27 06:07:12 +0000785 AddComment("TryLow");
786 OS.EmitIntValue(TBME.TryLow, 4);
787
788 AddComment("TryHigh");
789 OS.EmitIntValue(TBME.TryHigh, 4);
790
791 AddComment("CatchHigh");
792 OS.EmitIntValue(TBME.CatchHigh, 4);
793
794 AddComment("NumCatches");
795 OS.EmitIntValue(TBME.HandlerArray.size(), 4);
796
797 AddComment("HandlerArray");
798 OS.EmitValue(create32bitRef(HandlerMapXData), 4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000799 }
800
Reid Kleckner28e49032015-10-16 23:43:27 +0000801 // All funclets use the same parent frame offset currently.
802 unsigned ParentFrameOffset = 0;
803 if (shouldEmitPersonality) {
804 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
805 ParentFrameOffset = TFI->getWinEHParentFrameOffset(*MF);
806 }
807
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000808 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000809 const WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000810 MCSymbol *HandlerMapXData = HandlerMaps[I];
811 if (!HandlerMapXData)
812 continue;
813 // HandlerType {
814 // int32_t Adjectives;
815 // TypeDescriptor *Type;
816 // int32_t CatchObjOffset;
817 // void (*Handler)();
Sanjin Sijaric96f2ea32018-10-27 06:13:06 +0000818 // int32_t ParentFrameOffset; // x64 and AArch64 only
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000819 // };
820 OS.EmitLabel(HandlerMapXData);
821 for (const WinEHHandlerType &HT : TBME.HandlerArray) {
822 // Get the frame escape label with the offset of the catch object. If
David Majnemer99c1d132015-10-12 16:44:22 +0000823 // the index is INT_MAX, then there is no catch object, and we should
824 // emit an offset of zero, indicating that no copy will occur.
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000825 const MCExpr *FrameAllocOffsetRef = nullptr;
David Majnemer99c1d132015-10-12 16:44:22 +0000826 if (HT.CatchObj.FrameIndex != INT_MAX) {
Reid Kleckner6ddae312015-11-05 21:09:49 +0000827 int Offset = getFrameIndexOffset(HT.CatchObj.FrameIndex, FuncInfo);
David Majnemercb305de2016-03-01 04:30:16 +0000828 assert(Offset != 0 && "Illegal offset for catch object!");
Reid Klecknerb005d282015-09-16 20:16:27 +0000829 FrameAllocOffsetRef = MCConstantExpr::create(Offset, Asm->OutContext);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000830 } else {
Jim Grosbach13760bd2015-05-30 01:25:56 +0000831 FrameAllocOffsetRef = MCConstantExpr::create(0, Asm->OutContext);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000832 }
833
David Majnemerbfa5b982015-10-10 00:04:29 +0000834 MCSymbol *HandlerSym =
835 getMCSymbolForMBB(Asm, HT.Handler.dyn_cast<MachineBasicBlock *>());
Reid Kleckner94b704c2015-09-09 21:10:03 +0000836
David Majnemer081e8fe2015-12-27 06:07:12 +0000837 AddComment("Adjectives");
838 OS.EmitIntValue(HT.Adjectives, 4);
839
840 AddComment("Type");
841 OS.EmitValue(create32bitRef(HT.TypeDescriptor), 4);
842
843 AddComment("CatchObjOffset");
844 OS.EmitValue(FrameAllocOffsetRef, 4);
845
846 AddComment("Handler");
847 OS.EmitValue(create32bitRef(HandlerSym), 4);
848
849 if (shouldEmitPersonality) {
850 AddComment("ParentFrameOffset");
851 OS.EmitIntValue(ParentFrameOffset, 4);
852 }
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000853 }
854 }
855 }
856
857 // IPToStateMapEntry {
858 // void *IP;
859 // int32_t State;
860 // };
861 if (IPToStateXData) {
862 OS.EmitLabel(IPToStateXData);
Reid Klecknerc71d6272015-09-28 23:56:30 +0000863 for (auto &IPStatePair : IPToStateTable) {
David Majnemer081e8fe2015-12-27 06:07:12 +0000864 AddComment("IP");
865 OS.EmitValue(IPStatePair.first, 4);
866 AddComment("ToState");
867 OS.EmitIntValue(IPStatePair.second, 4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000868 }
869 }
870}
871
Reid Klecknerc71d6272015-09-28 23:56:30 +0000872void WinException::computeIP2StateTable(
Reid Klecknerc20276d2015-11-17 21:10:25 +0000873 const MachineFunction *MF, const WinEHFuncInfo &FuncInfo,
Reid Klecknerc71d6272015-09-28 23:56:30 +0000874 SmallVectorImpl<std::pair<const MCExpr *, int>> &IPToStateTable) {
Andrew Kaylor762a6be2015-05-11 19:41:19 +0000875
David Majnemer8a1c45d2015-12-12 05:38:55 +0000876 for (MachineFunction::const_iterator FuncletStart = MF->begin(),
877 FuncletEnd = MF->begin(),
878 End = MF->end();
879 FuncletStart != End; FuncletStart = FuncletEnd) {
880 // Find the end of the funclet
881 while (++FuncletEnd != End) {
882 if (FuncletEnd->isEHFuncletEntry()) {
883 break;
884 }
885 }
886
887 // Don't emit ip2state entries for cleanup funclets. Any interesting
888 // exceptional actions in cleanups must be handled in a separate IR
889 // function.
890 if (FuncletStart->isCleanupFuncletEntry())
891 continue;
892
893 MCSymbol *StartLabel;
894 int BaseState;
895 if (FuncletStart == MF->begin()) {
896 BaseState = NullState;
897 StartLabel = Asm->getFunctionBegin();
898 } else {
899 auto *FuncletPad =
900 cast<FuncletPadInst>(FuncletStart->getBasicBlock()->getFirstNonPHI());
901 assert(FuncInfo.FuncletBaseStateMap.count(FuncletPad) != 0);
902 BaseState = FuncInfo.FuncletBaseStateMap.find(FuncletPad)->second;
903 StartLabel = getMCSymbolForMBB(Asm, &*FuncletStart);
904 }
905 assert(StartLabel && "need local function start label");
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000906 IPToStateTable.push_back(
David Majnemer8a1c45d2015-12-12 05:38:55 +0000907 std::make_pair(create32bitRef(StartLabel), BaseState));
908
909 for (const auto &StateChange : InvokeStateChangeIterator::range(
910 FuncInfo, FuncletStart, FuncletEnd, BaseState)) {
911 // Compute the label to report as the start of this entry; use the EH
912 // start label for the invoke if we have one, otherwise (this is a call
913 // which may unwind to our caller and does not have an EH start label, so)
914 // use the previous end label.
915 const MCSymbol *ChangeLabel = StateChange.NewStartLabel;
916 if (!ChangeLabel)
917 ChangeLabel = StateChange.PreviousEndLabel;
918 // Emit an entry indicating that PCs after 'Label' have this EH state.
919 IPToStateTable.push_back(
Sanjin Sijaric96f2ea32018-10-27 06:13:06 +0000920 std::make_pair(getLabel(ChangeLabel), StateChange.NewState));
David Majnemer8a1c45d2015-12-12 05:38:55 +0000921 // FIXME: assert that NewState is between CatchLow and CatchHigh.
922 }
Reid Klecknerc71d6272015-09-28 23:56:30 +0000923 }
David Majnemercde33032015-03-30 22:58:10 +0000924}
Reid Klecknerf12c0302015-06-09 21:42:19 +0000925
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000926void WinException::emitEHRegistrationOffsetLabel(const WinEHFuncInfo &FuncInfo,
927 StringRef FLinkageName) {
928 // Outlined helpers called by the EH runtime need to know the offset of the EH
929 // registration in order to recover the parent frame pointer. Now that we know
930 // we've code generated the parent, we can emit the label assignment that
931 // those helpers use to get the offset of the registration node.
Reid Kleckner9cb915b2016-09-30 22:10:12 +0000932
933 // Compute the parent frame offset. The EHRegNodeFrameIndex will be invalid if
934 // after optimization all the invokes were eliminated. We still need to emit
935 // the parent frame offset label, but it should be garbage and should never be
936 // used.
937 int64_t Offset = 0;
938 int FI = FuncInfo.EHRegNodeFrameIndex;
939 if (FI != INT_MAX) {
940 const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
941 unsigned UnusedReg;
Mandeep Singh Grang33c49c02019-01-16 19:52:59 +0000942 // FIXME: getFrameIndexReference needs to match the behavior of
943 // AArch64RegisterInfo::hasBasePointer in which one of the scenarios where
944 // SP is used is if frame size >= 256.
Reid Kleckner9cb915b2016-09-30 22:10:12 +0000945 Offset = TFI->getFrameIndexReference(*Asm->MF, FI, UnusedReg);
946 }
947
Reid Klecknerc20276d2015-11-17 21:10:25 +0000948 MCContext &Ctx = Asm->OutContext;
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000949 MCSymbol *ParentFrameOffset =
Reid Klecknerc20276d2015-11-17 21:10:25 +0000950 Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName);
Reid Kleckner9cb915b2016-09-30 22:10:12 +0000951 Asm->OutStreamer->EmitAssignment(ParentFrameOffset,
952 MCConstantExpr::create(Offset, Ctx));
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000953}
954
Reid Klecknerf12c0302015-06-09 21:42:19 +0000955/// Emit the language-specific data that _except_handler3 and 4 expect. This is
956/// functionally equivalent to the __C_specific_handler table, except it is
957/// indexed by state number instead of IP.
958void WinException::emitExceptHandlerTable(const MachineFunction *MF) {
Reid Klecknera9d62532015-06-11 22:32:23 +0000959 MCStreamer &OS = *Asm->OutStreamer;
Matthias Braunf1caa282017-12-15 22:22:58 +0000960 const Function &F = MF->getFunction();
961 StringRef FLinkageName = GlobalValue::dropLLVMManglingEscape(F.getName());
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000962
David Majnemer081e8fe2015-12-27 06:07:12 +0000963 bool VerboseAsm = OS.isVerboseAsm();
964 auto AddComment = [&](const Twine &Comment) {
965 if (VerboseAsm)
966 OS.AddComment(Comment);
967 };
968
Reid Klecknerc20276d2015-11-17 21:10:25 +0000969 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000970 emitEHRegistrationOffsetLabel(FuncInfo, FLinkageName);
Reid Klecknerf12c0302015-06-09 21:42:19 +0000971
972 // Emit the __ehtable label that we use for llvm.x86.seh.lsda.
Reid Klecknerf12c0302015-06-09 21:42:19 +0000973 MCSymbol *LSDALabel = Asm->OutContext.getOrCreateLSDASymbol(FLinkageName);
Reid Kleckner85a24502015-07-10 00:08:49 +0000974 OS.EmitValueToAlignment(4);
Reid Klecknerf12c0302015-06-09 21:42:19 +0000975 OS.EmitLabel(LSDALabel);
976
Reid Kleckner9a1a9192015-07-13 20:41:46 +0000977 const Function *Per =
Matthias Braunf1caa282017-12-15 22:22:58 +0000978 dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
Reid Klecknerf12c0302015-06-09 21:42:19 +0000979 StringRef PerName = Per->getName();
980 int BaseState = -1;
981 if (PerName == "_except_handler4") {
982 // The LSDA for _except_handler4 starts with this struct, followed by the
983 // scope table:
984 //
985 // struct EH4ScopeTable {
986 // int32_t GSCookieOffset;
987 // int32_t GSCookieXOROffset;
988 // int32_t EHCookieOffset;
989 // int32_t EHCookieXOROffset;
990 // ScopeTableEntry ScopeRecord[];
991 // };
992 //
Etienne Bergeronf6be62f2016-06-21 15:58:55 +0000993 // Offsets are %ebp relative.
994 //
995 // The GS cookie is present only if the function needs stack protection.
996 // GSCookieOffset = -2 means that GS cookie is not used.
997 //
998 // The EH cookie is always present.
999 //
1000 // Check is done the following way:
1001 // (ebp+CookieXOROffset) ^ [ebp+CookieOffset] == _security_cookie
1002
1003 // Retrieve the Guard Stack slot.
1004 int GSCookieOffset = -2;
Matthias Braun941a7052016-07-28 18:40:00 +00001005 const MachineFrameInfo &MFI = MF->getFrameInfo();
1006 if (MFI.hasStackProtectorIndex()) {
Etienne Bergeronf6be62f2016-06-21 15:58:55 +00001007 unsigned UnusedReg;
1008 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
Matthias Braun941a7052016-07-28 18:40:00 +00001009 int SSPIdx = MFI.getStackProtectorIndex();
Etienne Bergeronf6be62f2016-06-21 15:58:55 +00001010 GSCookieOffset = TFI->getFrameIndexReference(*MF, SSPIdx, UnusedReg);
1011 }
1012
1013 // Retrieve the EH Guard slot.
1014 // TODO(etienneb): Get rid of this value and change it for and assertion.
1015 int EHCookieOffset = 9999;
1016 if (FuncInfo.EHGuardFrameIndex != INT_MAX) {
1017 unsigned UnusedReg;
1018 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
1019 int EHGuardIdx = FuncInfo.EHGuardFrameIndex;
1020 EHCookieOffset = TFI->getFrameIndexReference(*MF, EHGuardIdx, UnusedReg);
1021 }
1022
David Majnemer081e8fe2015-12-27 06:07:12 +00001023 AddComment("GSCookieOffset");
Etienne Bergeronf6be62f2016-06-21 15:58:55 +00001024 OS.EmitIntValue(GSCookieOffset, 4);
David Majnemer081e8fe2015-12-27 06:07:12 +00001025 AddComment("GSCookieXOROffset");
Reid Klecknerf12c0302015-06-09 21:42:19 +00001026 OS.EmitIntValue(0, 4);
David Majnemer081e8fe2015-12-27 06:07:12 +00001027 AddComment("EHCookieOffset");
Etienne Bergeronf6be62f2016-06-21 15:58:55 +00001028 OS.EmitIntValue(EHCookieOffset, 4);
David Majnemer081e8fe2015-12-27 06:07:12 +00001029 AddComment("EHCookieXOROffset");
Reid Klecknerf12c0302015-06-09 21:42:19 +00001030 OS.EmitIntValue(0, 4);
1031 BaseState = -2;
1032 }
1033
Reid Kleckner14e77352015-10-09 23:34:53 +00001034 assert(!FuncInfo.SEHUnwindMap.empty());
Reid Klecknerc20276d2015-11-17 21:10:25 +00001035 for (const SEHUnwindMapEntry &UME : FuncInfo.SEHUnwindMap) {
David Majnemer081e8fe2015-12-27 06:07:12 +00001036 auto *Handler = UME.Handler.get<MachineBasicBlock *>();
1037 const MCSymbol *ExceptOrFinally =
1038 UME.IsFinally ? getMCSymbolForMBB(Asm, Handler) : Handler->getSymbol();
Reid Kleckner14e77352015-10-09 23:34:53 +00001039 // -1 is usually the base state for "unwind to caller", but for
1040 // _except_handler4 it's -2. Do that replacement here if necessary.
1041 int ToState = UME.ToState == -1 ? BaseState : UME.ToState;
David Majnemer081e8fe2015-12-27 06:07:12 +00001042 AddComment("ToState");
1043 OS.EmitIntValue(ToState, 4);
1044 AddComment(UME.IsFinally ? "Null" : "FilterFunction");
1045 OS.EmitValue(create32bitRef(UME.Filter), 4);
1046 AddComment(UME.IsFinally ? "FinallyFunclet" : "ExceptionHandler");
1047 OS.EmitValue(create32bitRef(ExceptOrFinally), 4);
Reid Klecknerf12c0302015-06-09 21:42:19 +00001048 }
1049}
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001050
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001051static int getTryRank(const WinEHFuncInfo &FuncInfo, int State) {
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001052 int Rank = 0;
1053 while (State != -1) {
1054 ++Rank;
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001055 State = FuncInfo.ClrEHUnwindMap[State].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001056 }
1057 return Rank;
1058}
1059
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001060static int getTryAncestor(const WinEHFuncInfo &FuncInfo, int Left, int Right) {
1061 int LeftRank = getTryRank(FuncInfo, Left);
1062 int RightRank = getTryRank(FuncInfo, Right);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001063
1064 while (LeftRank < RightRank) {
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001065 Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001066 --RightRank;
1067 }
1068
1069 while (RightRank < LeftRank) {
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001070 Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001071 --LeftRank;
1072 }
1073
1074 while (Left != Right) {
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001075 Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState;
1076 Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001077 }
1078
1079 return Left;
1080}
1081
1082void WinException::emitCLRExceptionTable(const MachineFunction *MF) {
1083 // CLR EH "states" are really just IDs that identify handlers/funclets;
1084 // states, handlers, and funclets all have 1:1 mappings between them, and a
1085 // handler/funclet's "state" is its index in the ClrEHUnwindMap.
1086 MCStreamer &OS = *Asm->OutStreamer;
Reid Klecknerc20276d2015-11-17 21:10:25 +00001087 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001088 MCSymbol *FuncBeginSym = Asm->getFunctionBegin();
1089 MCSymbol *FuncEndSym = Asm->getFunctionEnd();
1090
1091 // A ClrClause describes a protected region.
1092 struct ClrClause {
1093 const MCSymbol *StartLabel; // Start of protected region
1094 const MCSymbol *EndLabel; // End of protected region
1095 int State; // Index of handler protecting the protected region
1096 int EnclosingState; // Index of funclet enclosing the protected region
1097 };
1098 SmallVector<ClrClause, 8> Clauses;
1099
1100 // Build a map from handler MBBs to their corresponding states (i.e. their
1101 // indices in the ClrEHUnwindMap).
1102 int NumStates = FuncInfo.ClrEHUnwindMap.size();
1103 assert(NumStates > 0 && "Don't need exception table!");
1104 DenseMap<const MachineBasicBlock *, int> HandlerStates;
1105 for (int State = 0; State < NumStates; ++State) {
1106 MachineBasicBlock *HandlerBlock =
1107 FuncInfo.ClrEHUnwindMap[State].Handler.get<MachineBasicBlock *>();
1108 HandlerStates[HandlerBlock] = State;
1109 // Use this loop through all handlers to verify our assumption (used in
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001110 // the MinEnclosingState computation) that enclosing funclets have lower
1111 // state numbers than their enclosed funclets.
1112 assert(FuncInfo.ClrEHUnwindMap[State].HandlerParentState < State &&
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001113 "ill-formed state numbering");
1114 }
1115 // Map the main function to the NullState.
Duncan P. N. Exon Smitha25ad062015-10-20 00:36:08 +00001116 HandlerStates[&MF->front()] = NullState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001117
1118 // Write out a sentinel indicating the end of the standard (Windows) xdata
1119 // and the start of the additional (CLR) info.
1120 OS.EmitIntValue(0xffffffff, 4);
1121 // Write out the number of funclets
1122 OS.EmitIntValue(NumStates, 4);
1123
1124 // Walk the machine blocks/instrs, computing and emitting a few things:
1125 // 1. Emit a list of the offsets to each handler entry, in lexical order.
1126 // 2. Compute a map (EndSymbolMap) from each funclet to the symbol at its end.
1127 // 3. Compute the list of ClrClauses, in the required order (inner before
1128 // outer, earlier before later; the order by which a forward scan with
1129 // early termination will find the innermost enclosing clause covering
1130 // a given address).
1131 // 4. A map (MinClauseMap) from each handler index to the index of the
1132 // outermost funclet/function which contains a try clause targeting the
1133 // key handler. This will be used to determine IsDuplicate-ness when
1134 // emitting ClrClauses. The NullState value is used to indicate that the
1135 // top-level function contains a try clause targeting the key handler.
1136 // HandlerStack is a stack of (PendingStartLabel, PendingState) pairs for
1137 // try regions we entered before entering the PendingState try but which
1138 // we haven't yet exited.
1139 SmallVector<std::pair<const MCSymbol *, int>, 4> HandlerStack;
1140 // EndSymbolMap and MinClauseMap are maps described above.
1141 std::unique_ptr<MCSymbol *[]> EndSymbolMap(new MCSymbol *[NumStates]);
1142 SmallVector<int, 4> MinClauseMap((size_t)NumStates, NumStates);
1143
1144 // Visit the root function and each funclet.
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001145 for (MachineFunction::const_iterator FuncletStart = MF->begin(),
1146 FuncletEnd = MF->begin(),
1147 End = MF->end();
1148 FuncletStart != End; FuncletStart = FuncletEnd) {
Duncan P. N. Exon Smitha25ad062015-10-20 00:36:08 +00001149 int FuncletState = HandlerStates[&*FuncletStart];
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001150 // Find the end of the funclet
1151 MCSymbol *EndSymbol = FuncEndSym;
1152 while (++FuncletEnd != End) {
1153 if (FuncletEnd->isEHFuncletEntry()) {
Duncan P. N. Exon Smitha25ad062015-10-20 00:36:08 +00001154 EndSymbol = getMCSymbolForMBB(Asm, &*FuncletEnd);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001155 break;
1156 }
1157 }
1158 // Emit the function/funclet end and, if this is a funclet (and not the
1159 // root function), record it in the EndSymbolMap.
1160 OS.EmitValue(getOffset(EndSymbol, FuncBeginSym), 4);
1161 if (FuncletState != NullState) {
1162 // Record the end of the handler.
1163 EndSymbolMap[FuncletState] = EndSymbol;
1164 }
1165
1166 // Walk the state changes in this function/funclet and compute its clauses.
1167 // Funclets always start in the null state.
1168 const MCSymbol *CurrentStartLabel = nullptr;
1169 int CurrentState = NullState;
1170 assert(HandlerStack.empty());
1171 for (const auto &StateChange :
1172 InvokeStateChangeIterator::range(FuncInfo, FuncletStart, FuncletEnd)) {
1173 // Close any try regions we're not still under
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001174 int StillPendingState =
1175 getTryAncestor(FuncInfo, CurrentState, StateChange.NewState);
1176 while (CurrentState != StillPendingState) {
1177 assert(CurrentState != NullState &&
1178 "Failed to find still-pending state!");
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001179 // Close the pending clause
1180 Clauses.push_back({CurrentStartLabel, StateChange.PreviousEndLabel,
1181 CurrentState, FuncletState});
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001182 // Now the next-outer try region is current
1183 CurrentState = FuncInfo.ClrEHUnwindMap[CurrentState].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001184 // Pop the new start label from the handler stack if we've exited all
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001185 // inner try regions of the corresponding try region.
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001186 if (HandlerStack.back().second == CurrentState)
1187 CurrentStartLabel = HandlerStack.pop_back_val().first;
1188 }
1189
1190 if (StateChange.NewState != CurrentState) {
1191 // For each clause we're starting, update the MinClauseMap so we can
1192 // know which is the topmost funclet containing a clause targeting
1193 // it.
1194 for (int EnteredState = StateChange.NewState;
1195 EnteredState != CurrentState;
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001196 EnteredState =
1197 FuncInfo.ClrEHUnwindMap[EnteredState].TryParentState) {
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001198 int &MinEnclosingState = MinClauseMap[EnteredState];
1199 if (FuncletState < MinEnclosingState)
1200 MinEnclosingState = FuncletState;
1201 }
1202 // Save the previous current start/label on the stack and update to
1203 // the newly-current start/state.
1204 HandlerStack.emplace_back(CurrentStartLabel, CurrentState);
1205 CurrentStartLabel = StateChange.NewStartLabel;
1206 CurrentState = StateChange.NewState;
1207 }
1208 }
1209 assert(HandlerStack.empty());
1210 }
1211
1212 // Now emit the clause info, starting with the number of clauses.
1213 OS.EmitIntValue(Clauses.size(), 4);
1214 for (ClrClause &Clause : Clauses) {
1215 // Emit a CORINFO_EH_CLAUSE :
1216 /*
1217 struct CORINFO_EH_CLAUSE
1218 {
1219 CORINFO_EH_CLAUSE_FLAGS Flags; // actually a CorExceptionFlag
1220 DWORD TryOffset;
1221 DWORD TryLength; // actually TryEndOffset
1222 DWORD HandlerOffset;
1223 DWORD HandlerLength; // actually HandlerEndOffset
1224 union
1225 {
1226 DWORD ClassToken; // use for catch clauses
1227 DWORD FilterOffset; // use for filter clauses
1228 };
1229 };
1230
1231 enum CORINFO_EH_CLAUSE_FLAGS
1232 {
1233 CORINFO_EH_CLAUSE_NONE = 0,
1234 CORINFO_EH_CLAUSE_FILTER = 0x0001, // This clause is for a filter
1235 CORINFO_EH_CLAUSE_FINALLY = 0x0002, // This clause is a finally clause
1236 CORINFO_EH_CLAUSE_FAULT = 0x0004, // This clause is a fault clause
1237 };
1238 typedef enum CorExceptionFlag
1239 {
1240 COR_ILEXCEPTION_CLAUSE_NONE,
1241 COR_ILEXCEPTION_CLAUSE_FILTER = 0x0001, // This is a filter clause
1242 COR_ILEXCEPTION_CLAUSE_FINALLY = 0x0002, // This is a finally clause
1243 COR_ILEXCEPTION_CLAUSE_FAULT = 0x0004, // This is a fault clause
1244 COR_ILEXCEPTION_CLAUSE_DUPLICATED = 0x0008, // duplicated clause. This
1245 // clause was duplicated
1246 // to a funclet which was
1247 // pulled out of line
1248 } CorExceptionFlag;
1249 */
1250 // Add 1 to the start/end of the EH clause; the IP associated with a
1251 // call when the runtime does its scan is the IP of the next instruction
1252 // (the one to which control will return after the call), so we need
1253 // to add 1 to the end of the clause to cover that offset. We also add
1254 // 1 to the start of the clause to make sure that the ranges reported
1255 // for all clauses are disjoint. Note that we'll need some additional
1256 // logic when machine traps are supported, since in that case the IP
1257 // that the runtime uses is the offset of the faulting instruction
1258 // itself; if such an instruction immediately follows a call but the
1259 // two belong to different clauses, we'll need to insert a nop between
1260 // them so the runtime can distinguish the point to which the call will
1261 // return from the point at which the fault occurs.
1262
1263 const MCExpr *ClauseBegin =
1264 getOffsetPlusOne(Clause.StartLabel, FuncBeginSym);
1265 const MCExpr *ClauseEnd = getOffsetPlusOne(Clause.EndLabel, FuncBeginSym);
1266
Reid Klecknerc20276d2015-11-17 21:10:25 +00001267 const ClrEHUnwindMapEntry &Entry = FuncInfo.ClrEHUnwindMap[Clause.State];
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001268 MachineBasicBlock *HandlerBlock = Entry.Handler.get<MachineBasicBlock *>();
1269 MCSymbol *BeginSym = getMCSymbolForMBB(Asm, HandlerBlock);
1270 const MCExpr *HandlerBegin = getOffset(BeginSym, FuncBeginSym);
1271 MCSymbol *EndSym = EndSymbolMap[Clause.State];
1272 const MCExpr *HandlerEnd = getOffset(EndSym, FuncBeginSym);
1273
1274 uint32_t Flags = 0;
1275 switch (Entry.HandlerType) {
1276 case ClrHandlerType::Catch:
1277 // Leaving bits 0-2 clear indicates catch.
1278 break;
1279 case ClrHandlerType::Filter:
1280 Flags |= 1;
1281 break;
1282 case ClrHandlerType::Finally:
1283 Flags |= 2;
1284 break;
1285 case ClrHandlerType::Fault:
1286 Flags |= 4;
1287 break;
1288 }
1289 if (Clause.EnclosingState != MinClauseMap[Clause.State]) {
1290 // This is a "duplicate" clause; the handler needs to be entered from a
1291 // frame above the one holding the invoke.
1292 assert(Clause.EnclosingState > MinClauseMap[Clause.State]);
1293 Flags |= 8;
1294 }
1295 OS.EmitIntValue(Flags, 4);
1296
1297 // Write the clause start/end
1298 OS.EmitValue(ClauseBegin, 4);
1299 OS.EmitValue(ClauseEnd, 4);
1300
1301 // Write out the handler start/end
1302 OS.EmitValue(HandlerBegin, 4);
1303 OS.EmitValue(HandlerEnd, 4);
1304
1305 // Write out the type token or filter offset
1306 assert(Entry.HandlerType != ClrHandlerType::Filter && "NYI: filters");
1307 OS.EmitIntValue(Entry.TypeToken, 4);
1308 }
1309}