blob: cd8077e7d54865cd6c9ba151367a9a6f62c2491b [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Charles Davis91ed7992011-05-27 23:47:32 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing Win64 exception info into asm files.
10//
11//===----------------------------------------------------------------------===//
12
Reid Kleckner60b640b2015-05-28 22:47:01 +000013#include "WinException.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000015#include "llvm/BinaryFormat/COFF.h"
16#include "llvm/BinaryFormat/Dwarf.h"
Charles Davis91ed7992011-05-27 23:47:32 +000017#include "llvm/CodeGen/AsmPrinter.h"
Charles Davis91ed7992011-05-27 23:47:32 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000021#include "llvm/CodeGen/TargetFrameLowering.h"
22#include "llvm/CodeGen/TargetLowering.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000023#include "llvm/CodeGen/TargetSubtargetInfo.h"
David Majnemercde33032015-03-30 22:58:10 +000024#include "llvm/CodeGen/WinEHFuncInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/DataLayout.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000026#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
Charles Davis91ed7992011-05-27 23:47:32 +000028#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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/FormattedStream.h"
David Blaikie6054e652018-03-23 23:58:19 +000036#include "llvm/Target/TargetLoweringObjectFile.h"
Simon Pilgrimfe0006c2020-05-23 19:49:38 +010037#include "llvm/Target/TargetMachine.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
Eli Friedman0b61d222019-05-03 00:10:45 +0000113void WinException::markFunctionEnd() {
114 if (isAArch64 && CurrentFuncletEntry &&
115 (shouldEmitMoves || shouldEmitPersonality))
116 Asm->OutStreamer->EmitWinCFIFuncletOrFuncEnd();
117}
118
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000119/// endFunction - Gather and emit post-function exception information.
Charles Davis91ed7992011-05-27 23:47:32 +0000120///
Reid Kleckner60b640b2015-05-28 22:47:01 +0000121void WinException::endFunction(const MachineFunction *MF) {
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000122 if (!shouldEmitPersonality && !shouldEmitMoves && !shouldEmitLSDA)
Charles Davis5638b9f2011-05-28 04:21:04 +0000123 return;
124
Matthias Braunf1caa282017-12-15 22:22:58 +0000125 const Function &F = MF->getFunction();
Reid Kleckner9a1a9192015-07-13 20:41:46 +0000126 EHPersonality Per = EHPersonality::Unknown;
Matthias Braunf1caa282017-12-15 22:22:58 +0000127 if (F.hasPersonalityFn())
128 Per = classifyEHPersonality(F.getPersonalityFn()->stripPointerCasts());
Reid Kleckner3e9fadf2015-04-15 18:48:15 +0000129
Joseph Tremoulet2afea542015-10-06 20:28:16 +0000130 // Get rid of any dead landing pads if we're not using funclets. In funclet
131 // schemes, the landing pad is not actually reachable. It only exists so
132 // that we can emit the right table data.
Matthias Braund0ee66c2016-12-01 19:32:15 +0000133 if (!isFuncletEHPersonality(Per)) {
134 MachineFunction *NonConstMF = const_cast<MachineFunction*>(MF);
135 NonConstMF->tidyLandingPads();
136 }
Charles Davisa5752262011-05-30 00:13:34 +0000137
Eli Friedman0b61d222019-05-03 00:10:45 +0000138 endFuncletImpl();
David Majnemera80c1512015-09-29 20:12:33 +0000139
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000140 // endFunclet will emit the necessary .xdata tables for x64 SEH.
Matthias Braund0ee66c2016-12-01 19:32:15 +0000141 if (Per == EHPersonality::MSVC_Win64SEH && MF->hasEHFunclets())
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000142 return;
143
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000144 if (shouldEmitPersonality || shouldEmitLSDA) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000145 Asm->OutStreamer->PushSection();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000146
Reid Kleckner97837b72016-05-02 23:22:18 +0000147 // Just switch sections to the right xdata section.
148 MCSection *XData = Asm->OutStreamer->getAssociatedXDataSection(
149 Asm->OutStreamer->getCurrentSectionOnly());
David Majnemera80c1512015-09-29 20:12:33 +0000150 Asm->OutStreamer->SwitchSection(XData);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000151
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000152 // Emit the tables appropriate to the personality function in use. If we
153 // don't recognize the personality, assume it uses an Itanium-style LSDA.
Reid Kleckner2d5fb682015-02-14 00:21:02 +0000154 if (Per == EHPersonality::MSVC_Win64SEH)
Reid Kleckner94b704c2015-09-09 21:10:03 +0000155 emitCSpecificHandlerTable(MF);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000156 else if (Per == EHPersonality::MSVC_X86SEH)
Reid Klecknerf12c0302015-06-09 21:42:19 +0000157 emitExceptHandlerTable(MF);
David Majnemercde33032015-03-30 22:58:10 +0000158 else if (Per == EHPersonality::MSVC_CXX)
159 emitCXXFrameHandler3Table(MF);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000160 else if (Per == EHPersonality::CoreCLR)
161 emitCLRExceptionTable(MF);
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000162 else
Reid Kleckner0a57f652015-01-14 01:05:27 +0000163 emitExceptionTable();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000164
Lang Hames9ff69c82015-04-24 19:11:51 +0000165 Asm->OutStreamer->PopSection();
Charles Davisa5752262011-05-30 00:13:34 +0000166 }
David Majnemera80c1512015-09-29 20:12:33 +0000167}
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000168
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000169/// Retrieve the MCSymbol for a GlobalValue or MachineBasicBlock.
David Majnemerbfa5b982015-10-10 00:04:29 +0000170static MCSymbol *getMCSymbolForMBB(AsmPrinter *Asm,
171 const MachineBasicBlock *MBB) {
172 if (!MBB)
David Majnemera80c1512015-09-29 20:12:33 +0000173 return nullptr;
David Majnemera80c1512015-09-29 20:12:33 +0000174
David Majnemerbfa5b982015-10-10 00:04:29 +0000175 assert(MBB->isEHFuncletEntry());
176
177 // Give catches and cleanups a name based off of their parent function and
178 // their funclet entry block's number.
179 const MachineFunction *MF = MBB->getParent();
Matthias Braunf1caa282017-12-15 22:22:58 +0000180 const Function &F = MF->getFunction();
181 StringRef FuncLinkageName = GlobalValue::dropLLVMManglingEscape(F.getName());
David Majnemerbfa5b982015-10-10 00:04:29 +0000182 MCContext &Ctx = MF->getContext();
183 StringRef HandlerPrefix = MBB->isCleanupFuncletEntry() ? "dtor" : "catch";
184 return Ctx.getOrCreateSymbol("?" + HandlerPrefix + "$" +
185 Twine(MBB->getNumber()) + "@?0?" +
186 FuncLinkageName + "@4HA");
David Majnemera80c1512015-09-29 20:12:33 +0000187}
188
189void WinException::beginFunclet(const MachineBasicBlock &MBB,
190 MCSymbol *Sym) {
191 CurrentFuncletEntry = &MBB;
192
Matthias Braunf1caa282017-12-15 22:22:58 +0000193 const Function &F = Asm->MF->getFunction();
David Majnemera80c1512015-09-29 20:12:33 +0000194 // If a symbol was not provided for the funclet, invent one.
195 if (!Sym) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000196 Sym = getMCSymbolForMBB(Asm, &MBB);
David Majnemera80c1512015-09-29 20:12:33 +0000197
198 // Describe our funclet symbol as a function with internal linkage.
199 Asm->OutStreamer->BeginCOFFSymbolDef(Sym);
200 Asm->OutStreamer->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
201 Asm->OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
202 << COFF::SCT_COMPLEX_TYPE_SHIFT);
203 Asm->OutStreamer->EndCOFFSymbolDef();
204
205 // We want our funclet's entry point to be aligned such that no nops will be
206 // present after the label.
Fangrui Song1d49eb02020-02-13 16:36:27 -0800207 Asm->emitAlignment(std::max(Asm->MF->getAlignment(), MBB.getAlignment()),
Guillaume Chatelet97264362019-09-11 13:37:35 +0000208 &F);
David Majnemera80c1512015-09-29 20:12:33 +0000209
210 // Now that we've emitted the alignment directive, point at our funclet.
Fangrui Song6d2d5892020-02-14 19:21:58 -0800211 Asm->OutStreamer->emitLabel(Sym);
David Majnemera80c1512015-09-29 20:12:33 +0000212 }
213
214 // Mark 'Sym' as starting our funclet.
Reid Kleckner92647362016-12-28 19:05:12 +0000215 if (shouldEmitMoves || shouldEmitPersonality) {
216 CurrentFuncletTextSection = Asm->OutStreamer->getCurrentSectionOnly();
David Majnemera80c1512015-09-29 20:12:33 +0000217 Asm->OutStreamer->EmitWinCFIStartProc(Sym);
Reid Kleckner92647362016-12-28 19:05:12 +0000218 }
David Majnemera80c1512015-09-29 20:12:33 +0000219
220 if (shouldEmitPersonality) {
221 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
222 const Function *PerFn = nullptr;
223
224 // Determine which personality routine we are using for this funclet.
Matthias Braunf1caa282017-12-15 22:22:58 +0000225 if (F.hasPersonalityFn())
226 PerFn = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
David Majnemera80c1512015-09-29 20:12:33 +0000227 const MCSymbol *PersHandlerSym =
Eric Christopher4367c7f2016-09-16 07:33:15 +0000228 TLOF.getCFIPersonalitySymbol(PerFn, Asm->TM, MMI);
David Majnemera80c1512015-09-29 20:12:33 +0000229
Reid Kleckner785e7d22016-12-08 20:38:46 +0000230 // Do not emit a .seh_handler directives for cleanup funclets.
231 // FIXME: This means cleanup funclets cannot handle exceptions. Given that
232 // Clang doesn't produce EH constructs inside cleanup funclets and LLVM's
233 // inliner doesn't allow inlining them, this isn't a major problem in
234 // practice.
235 if (!CurrentFuncletEntry->isCleanupFuncletEntry())
David Majnemera80c1512015-09-29 20:12:33 +0000236 Asm->OutStreamer->EmitWinEHHandler(PersHandlerSym, true, true);
237 }
238}
239
240void WinException::endFunclet() {
Eli Friedman0b61d222019-05-03 00:10:45 +0000241 if (isAArch64 && CurrentFuncletEntry &&
242 (shouldEmitMoves || shouldEmitPersonality)) {
243 Asm->OutStreamer->SwitchSection(CurrentFuncletTextSection);
244 Asm->OutStreamer->EmitWinCFIFuncletOrFuncEnd();
245 }
246 endFuncletImpl();
247}
248
249void WinException::endFuncletImpl() {
David Majnemera80c1512015-09-29 20:12:33 +0000250 // No funclet to process? Great, we have nothing to do.
251 if (!CurrentFuncletEntry)
252 return;
253
Matthias Braund0ee66c2016-12-01 19:32:15 +0000254 const MachineFunction *MF = Asm->MF;
David Majnemera80c1512015-09-29 20:12:33 +0000255 if (shouldEmitMoves || shouldEmitPersonality) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000256 const Function &F = MF->getFunction();
David Majnemera80c1512015-09-29 20:12:33 +0000257 EHPersonality Per = EHPersonality::Unknown;
Matthias Braunf1caa282017-12-15 22:22:58 +0000258 if (F.hasPersonalityFn())
259 Per = classifyEHPersonality(F.getPersonalityFn()->stripPointerCasts());
David Majnemera80c1512015-09-29 20:12:33 +0000260
Sanjin Sijaric96f2ea32018-10-27 06:13:06 +0000261 // On funclet exit, we emit a fake "function" end marker, so that the call
262 // to EmitWinEHHandlerData below can calculate the size of the funclet or
263 // function.
264 if (isAArch64) {
Sanjin Sijaric96f2ea32018-10-27 06:13:06 +0000265 MCSection *XData = Asm->OutStreamer->getAssociatedXDataSection(
266 Asm->OutStreamer->getCurrentSectionOnly());
267 Asm->OutStreamer->SwitchSection(XData);
268 }
269
David Majnemera80c1512015-09-29 20:12:33 +0000270 // Emit an UNWIND_INFO struct describing the prologue.
271 Asm->OutStreamer->EmitWinEHHandlerData();
272
David Majnemera80c1512015-09-29 20:12:33 +0000273 if (Per == EHPersonality::MSVC_CXX && shouldEmitPersonality &&
274 !CurrentFuncletEntry->isCleanupFuncletEntry()) {
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000275 // If this is a C++ catch funclet (or the parent function),
276 // emit a reference to the LSDA for the parent function.
Matthias Braunf1caa282017-12-15 22:22:58 +0000277 StringRef FuncLinkageName = GlobalValue::dropLLVMManglingEscape(F.getName());
David Majnemera80c1512015-09-29 20:12:33 +0000278 MCSymbol *FuncInfoXData = Asm->OutContext.getOrCreateSymbol(
279 Twine("$cppxdata$", FuncLinkageName));
Fangrui Song77497102020-02-14 22:40:47 -0800280 Asm->OutStreamer->emitValue(create32bitRef(FuncInfoXData), 4);
Matthias Braund0ee66c2016-12-01 19:32:15 +0000281 } else if (Per == EHPersonality::MSVC_Win64SEH && MF->hasEHFunclets() &&
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000282 !CurrentFuncletEntry->isEHFuncletEntry()) {
283 // If this is the parent function in Win64 SEH, emit the LSDA immediately
284 // following .seh_handlerdata.
Matthias Braund0ee66c2016-12-01 19:32:15 +0000285 emitCSpecificHandlerTable(MF);
David Majnemera80c1512015-09-29 20:12:33 +0000286 }
287
Reid Kleckner92647362016-12-28 19:05:12 +0000288 // Switch back to the funclet start .text section now that we are done
289 // writing to .xdata, and emit an .seh_endproc directive to mark the end of
290 // the function.
291 Asm->OutStreamer->SwitchSection(CurrentFuncletTextSection);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000292 Asm->OutStreamer->EmitWinCFIEndProc();
David Majnemera80c1512015-09-29 20:12:33 +0000293 }
294
295 // Let's make sure we don't try to end the same funclet twice.
296 CurrentFuncletEntry = nullptr;
Charles Davis91ed7992011-05-27 23:47:32 +0000297}
Reid Kleckner0a57f652015-01-14 01:05:27 +0000298
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000299const MCExpr *WinException::create32bitRef(const MCSymbol *Value) {
David Majnemercde33032015-03-30 22:58:10 +0000300 if (!Value)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000301 return MCConstantExpr::create(0, Asm->OutContext);
302 return MCSymbolRefExpr::create(Value, useImageRel32
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000303 ? MCSymbolRefExpr::VK_COFF_IMGREL32
304 : MCSymbolRefExpr::VK_None,
Reid Kleckner0a57f652015-01-14 01:05:27 +0000305 Asm->OutContext);
306}
307
Joseph Tremoulet3d0fbf12015-10-23 15:06:05 +0000308const MCExpr *WinException::create32bitRef(const GlobalValue *GV) {
309 if (!GV)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000310 return MCConstantExpr::create(0, Asm->OutContext);
Joseph Tremoulet3d0fbf12015-10-23 15:06:05 +0000311 return create32bitRef(Asm->getSymbol(GV));
David Majnemercde33032015-03-30 22:58:10 +0000312}
313
Sanjin Sijaric96f2ea32018-10-27 06:13:06 +0000314const MCExpr *WinException::getLabel(const MCSymbol *Label) {
315 if (isAArch64)
316 return MCSymbolRefExpr::create(Label, MCSymbolRefExpr::VK_COFF_IMGREL32,
317 Asm->OutContext);
Reid Klecknerc71d6272015-09-28 23:56:30 +0000318 return MCBinaryExpr::createAdd(create32bitRef(Label),
319 MCConstantExpr::create(1, Asm->OutContext),
320 Asm->OutContext);
321}
322
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000323const MCExpr *WinException::getOffset(const MCSymbol *OffsetOf,
324 const MCSymbol *OffsetFrom) {
325 return MCBinaryExpr::createSub(
326 MCSymbolRefExpr::create(OffsetOf, Asm->OutContext),
327 MCSymbolRefExpr::create(OffsetFrom, Asm->OutContext), Asm->OutContext);
328}
329
330const MCExpr *WinException::getOffsetPlusOne(const MCSymbol *OffsetOf,
331 const MCSymbol *OffsetFrom) {
332 return MCBinaryExpr::createAdd(getOffset(OffsetOf, OffsetFrom),
333 MCConstantExpr::create(1, Asm->OutContext),
334 Asm->OutContext);
335}
336
Reid Klecknerc20276d2015-11-17 21:10:25 +0000337int WinException::getFrameIndexOffset(int FrameIndex,
338 const WinEHFuncInfo &FuncInfo) {
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000339 const TargetFrameLowering &TFI = *Asm->MF->getSubtarget().getFrameLowering();
Matt Arsenault2481f262020-04-07 16:33:58 -0400340 Register UnusedReg;
Sanjoy Das0ebc9612016-06-16 18:54:06 +0000341 if (Asm->MAI->usesWindowsCFI()) {
342 int Offset =
343 TFI.getFrameIndexReferencePreferSP(*Asm->MF, FrameIndex, UnusedReg,
344 /*IgnoreSPUpdates*/ true);
345 assert(UnusedReg ==
346 Asm->MF->getSubtarget()
347 .getTargetLowering()
348 ->getStackPointerRegisterToSaveRestore());
349 return Offset;
350 }
351
Reid Kleckner6ddae312015-11-05 21:09:49 +0000352 // For 32-bit, offsets should be relative to the end of the EH registration
353 // node. For 64-bit, it's relative to SP at the end of the prologue.
354 assert(FuncInfo.EHRegNodeEndOffset != INT_MAX);
355 int Offset = TFI.getFrameIndexReference(*Asm->MF, FrameIndex, UnusedReg);
356 Offset += FuncInfo.EHRegNodeEndOffset;
357 return Offset;
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000358}
359
Benjamin Kramer808d2a02015-10-05 21:20:26 +0000360namespace {
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000361
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000362/// Top-level state used to represent unwind to caller
363const int NullState = -1;
364
365struct InvokeStateChange {
366 /// EH Label immediately after the last invoke in the previous state, or
367 /// nullptr if the previous state was the null state.
368 const MCSymbol *PreviousEndLabel;
369
370 /// EH label immediately before the first invoke in the new state, or nullptr
371 /// if the new state is the null state.
372 const MCSymbol *NewStartLabel;
373
374 /// State of the invoke following NewStartLabel, or NullState to indicate
375 /// the presence of calls which may unwind to caller.
376 int NewState;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000377};
378
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000379/// Iterator that reports all the invoke state changes in a range of machine
380/// basic blocks. Changes to the null state are reported whenever a call that
381/// may unwind to caller is encountered. The MBB range is expected to be an
382/// entire function or funclet, and the start and end of the range are treated
383/// as being in the NullState even if there's not an unwind-to-caller call
384/// before the first invoke or after the last one (i.e., the first state change
385/// reported is the first change to something other than NullState, and a
386/// change back to NullState is always reported at the end of iteration).
387class InvokeStateChangeIterator {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000388 InvokeStateChangeIterator(const WinEHFuncInfo &EHInfo,
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000389 MachineFunction::const_iterator MFI,
390 MachineFunction::const_iterator MFE,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000391 MachineBasicBlock::const_iterator MBBI,
392 int BaseState)
393 : EHInfo(EHInfo), MFI(MFI), MFE(MFE), MBBI(MBBI), BaseState(BaseState) {
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000394 LastStateChange.PreviousEndLabel = nullptr;
395 LastStateChange.NewStartLabel = nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000396 LastStateChange.NewState = BaseState;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000397 scan();
398 }
399
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000400public:
401 static iterator_range<InvokeStateChangeIterator>
Reid Klecknerc20276d2015-11-17 21:10:25 +0000402 range(const WinEHFuncInfo &EHInfo, MachineFunction::const_iterator Begin,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000403 MachineFunction::const_iterator End, int BaseState = NullState) {
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000404 // Reject empty ranges to simplify bookkeeping by ensuring that we can get
405 // the end of the last block.
406 assert(Begin != End);
407 auto BlockBegin = Begin->begin();
408 auto BlockEnd = std::prev(End)->end();
David Majnemer8a1c45d2015-12-12 05:38:55 +0000409 return make_range(
410 InvokeStateChangeIterator(EHInfo, Begin, End, BlockBegin, BaseState),
411 InvokeStateChangeIterator(EHInfo, End, End, BlockEnd, BaseState));
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000412 }
413
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000414 // Iterator methods.
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000415 bool operator==(const InvokeStateChangeIterator &O) const {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000416 assert(BaseState == O.BaseState);
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000417 // Must be visiting same block.
418 if (MFI != O.MFI)
419 return false;
420 // Must be visiting same isntr.
421 if (MBBI != O.MBBI)
422 return false;
423 // At end of block/instr iteration, we can still have two distinct states:
424 // one to report the final EndLabel, and another indicating the end of the
425 // state change iteration. Check for CurrentEndLabel equality to
426 // distinguish these.
427 return CurrentEndLabel == O.CurrentEndLabel;
428 }
429
430 bool operator!=(const InvokeStateChangeIterator &O) const {
431 return !operator==(O);
432 }
433 InvokeStateChange &operator*() { return LastStateChange; }
434 InvokeStateChange *operator->() { return &LastStateChange; }
435 InvokeStateChangeIterator &operator++() { return scan(); }
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000436
437private:
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000438 InvokeStateChangeIterator &scan();
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000439
Reid Klecknerc20276d2015-11-17 21:10:25 +0000440 const WinEHFuncInfo &EHInfo;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000441 const MCSymbol *CurrentEndLabel = nullptr;
442 MachineFunction::const_iterator MFI;
443 MachineFunction::const_iterator MFE;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000444 MachineBasicBlock::const_iterator MBBI;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000445 InvokeStateChange LastStateChange;
446 bool VisitingInvoke = false;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000447 int BaseState;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000448};
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000449
Benjamin Kramer808d2a02015-10-05 21:20:26 +0000450} // end anonymous namespace
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000451
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000452InvokeStateChangeIterator &InvokeStateChangeIterator::scan() {
453 bool IsNewBlock = false;
454 for (; MFI != MFE; ++MFI, IsNewBlock = true) {
455 if (IsNewBlock)
456 MBBI = MFI->begin();
457 for (auto MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
458 const MachineInstr &MI = *MBBI;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000459 if (!VisitingInvoke && LastStateChange.NewState != BaseState &&
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000460 MI.isCall() && !EHStreamer::callToNoUnwindFunction(&MI)) {
461 // Indicate a change of state to the null state. We don't have
462 // start/end EH labels handy but the caller won't expect them for
463 // null state regions.
464 LastStateChange.PreviousEndLabel = CurrentEndLabel;
465 LastStateChange.NewStartLabel = nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000466 LastStateChange.NewState = BaseState;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000467 CurrentEndLabel = nullptr;
468 // Don't re-visit this instr on the next scan
469 ++MBBI;
470 return *this;
471 }
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000472
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000473 // All other state changes are at EH labels before/after invokes.
474 if (!MI.isEHLabel())
475 continue;
476 MCSymbol *Label = MI.getOperand(0).getMCSymbol();
477 if (Label == CurrentEndLabel) {
478 VisitingInvoke = false;
479 continue;
480 }
David Majnemer8a1c45d2015-12-12 05:38:55 +0000481 auto InvokeMapIter = EHInfo.LabelToStateMap.find(Label);
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000482 // Ignore EH labels that aren't the ones inserted before an invoke
David Majnemer8a1c45d2015-12-12 05:38:55 +0000483 if (InvokeMapIter == EHInfo.LabelToStateMap.end())
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000484 continue;
485 auto &StateAndEnd = InvokeMapIter->second;
486 int NewState = StateAndEnd.first;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000487 // Keep track of the fact that we're between EH start/end labels so
488 // we know not to treat the inoke we'll see as unwinding to caller.
489 VisitingInvoke = true;
490 if (NewState == LastStateChange.NewState) {
491 // The state isn't actually changing here. Record the new end and
492 // keep going.
493 CurrentEndLabel = StateAndEnd.second;
494 continue;
495 }
496 // Found a state change to report
497 LastStateChange.PreviousEndLabel = CurrentEndLabel;
498 LastStateChange.NewStartLabel = Label;
499 LastStateChange.NewState = NewState;
500 // Start keeping track of the new current end
501 CurrentEndLabel = StateAndEnd.second;
502 // Don't re-visit this instr on the next scan
503 ++MBBI;
504 return *this;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000505 }
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000506 }
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000507 // Iteration hit the end of the block range.
David Majnemer8a1c45d2015-12-12 05:38:55 +0000508 if (LastStateChange.NewState != BaseState) {
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000509 // Report the end of the last new state
510 LastStateChange.PreviousEndLabel = CurrentEndLabel;
511 LastStateChange.NewStartLabel = nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000512 LastStateChange.NewState = BaseState;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000513 // Leave CurrentEndLabel non-null to distinguish this state from end.
514 assert(CurrentEndLabel != nullptr);
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000515 return *this;
516 }
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000517 // We've reported all state changes and hit the end state.
518 CurrentEndLabel = nullptr;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000519 return *this;
520}
521
Reid Kleckner0a57f652015-01-14 01:05:27 +0000522/// Emit the language-specific data that __C_specific_handler expects. This
523/// handler lives in the x64 Microsoft C runtime and allows catching or cleaning
524/// up after faults with __try, __except, and __finally. The typeinfo values
525/// are not really RTTI data, but pointers to filter functions that return an
526/// integer (1, 0, or -1) indicating how to handle the exception. For __finally
527/// blocks and other cleanups, the landing pad label is zero, and the filter
528/// function is actually a cleanup handler with the same prototype. A catch-all
529/// entry is modeled with a null filter function field and a non-zero landing
530/// pad label.
531///
532/// Possible filter function return values:
533/// EXCEPTION_EXECUTE_HANDLER (1):
534/// Jump to the landing pad label after cleanups.
535/// EXCEPTION_CONTINUE_SEARCH (0):
536/// Continue searching this table or continue unwinding.
537/// EXCEPTION_CONTINUE_EXECUTION (-1):
538/// Resume execution at the trapping PC.
539///
540/// Inferred table structure:
541/// struct Table {
542/// int NumEntries;
543/// struct Entry {
544/// imagerel32 LabelStart;
545/// imagerel32 LabelEnd;
Reid Klecknerf690f502015-01-22 02:27:44 +0000546/// imagerel32 FilterOrFinally; // One means catch-all.
Reid Kleckner14e77352015-10-09 23:34:53 +0000547/// imagerel32 LabelLPad; // Zero means __finally.
Reid Kleckner0a57f652015-01-14 01:05:27 +0000548/// } Entries[NumEntries];
549/// };
Reid Kleckner94b704c2015-09-09 21:10:03 +0000550void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) {
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000551 auto &OS = *Asm->OutStreamer;
552 MCContext &Ctx = Asm->OutContext;
Reid Klecknerc20276d2015-11-17 21:10:25 +0000553 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
Reid Kleckner7850c9f2015-12-15 23:40:58 +0000554
David Majnemer081e8fe2015-12-27 06:07:12 +0000555 bool VerboseAsm = OS.isVerboseAsm();
556 auto AddComment = [&](const Twine &Comment) {
557 if (VerboseAsm)
558 OS.AddComment(Comment);
559 };
560
Mandeep Singh Grang33c49c02019-01-16 19:52:59 +0000561 if (!isAArch64) {
562 // Emit a label assignment with the SEH frame offset so we can use it for
563 // llvm.eh.recoverfp.
564 StringRef FLinkageName =
565 GlobalValue::dropLLVMManglingEscape(MF->getFunction().getName());
566 MCSymbol *ParentFrameOffset =
567 Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName);
568 const MCExpr *MCOffset =
569 MCConstantExpr::create(FuncInfo.SEHSetFrameOffset, Ctx);
Fangrui Songa55daa12020-02-14 18:16:24 -0800570 Asm->OutStreamer->emitAssignment(ParentFrameOffset, MCOffset);
Mandeep Singh Grang33c49c02019-01-16 19:52:59 +0000571 }
Reid Kleckner7850c9f2015-12-15 23:40:58 +0000572
Reid Kleckner14e77352015-10-09 23:34:53 +0000573 // Use the assembler to compute the number of table entries through label
574 // difference and division.
575 MCSymbol *TableBegin =
576 Ctx.createTempSymbol("lsda_begin", /*AlwaysAddSuffix=*/true);
577 MCSymbol *TableEnd =
578 Ctx.createTempSymbol("lsda_end", /*AlwaysAddSuffix=*/true);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000579 const MCExpr *LabelDiff = getOffset(TableEnd, TableBegin);
Reid Kleckner14e77352015-10-09 23:34:53 +0000580 const MCExpr *EntrySize = MCConstantExpr::create(16, Ctx);
581 const MCExpr *EntryCount = MCBinaryExpr::createDiv(LabelDiff, EntrySize, Ctx);
David Majnemer081e8fe2015-12-27 06:07:12 +0000582 AddComment("Number of call sites");
Fangrui Song77497102020-02-14 22:40:47 -0800583 OS.emitValue(EntryCount, 4);
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000584
Fangrui Song6d2d5892020-02-14 19:21:58 -0800585 OS.emitLabel(TableBegin);
Reid Klecknereb7cd6c2015-10-09 23:05:54 +0000586
Reid Kleckner14e77352015-10-09 23:34:53 +0000587 // Iterate over all the invoke try ranges. Unlike MSVC, LLVM currently only
588 // models exceptions from invokes. LLVM also allows arbitrary reordering of
589 // the code, so our tables end up looking a bit different. Rather than
590 // trying to match MSVC's tables exactly, we emit a denormalized table. For
591 // each range of invokes in the same state, we emit table entries for all
592 // the actions that would be taken in that state. This means our tables are
593 // slightly bigger, which is OK.
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000594 const MCSymbol *LastStartLabel = nullptr;
595 int LastEHState = -1;
596 // Break out before we enter into a finally funclet.
597 // FIXME: We need to emit separate EH tables for cleanups.
598 MachineFunction::const_iterator End = MF->end();
599 MachineFunction::const_iterator Stop = std::next(MF->begin());
600 while (Stop != End && !Stop->isEHFuncletEntry())
601 ++Stop;
602 for (const auto &StateChange :
603 InvokeStateChangeIterator::range(FuncInfo, MF->begin(), Stop)) {
604 // Emit all the actions for the state we just transitioned out of
605 // if it was not the null state
606 if (LastEHState != -1)
607 emitSEHActionsForRange(FuncInfo, LastStartLabel,
608 StateChange.PreviousEndLabel, LastEHState);
609 LastStartLabel = StateChange.NewStartLabel;
610 LastEHState = StateChange.NewState;
Reid Kleckner0a57f652015-01-14 01:05:27 +0000611 }
Reid Kleckner14e77352015-10-09 23:34:53 +0000612
Fangrui Song6d2d5892020-02-14 19:21:58 -0800613 OS.emitLabel(TableEnd);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000614}
David Majnemercde33032015-03-30 22:58:10 +0000615
Reid Klecknerc20276d2015-11-17 21:10:25 +0000616void WinException::emitSEHActionsForRange(const WinEHFuncInfo &FuncInfo,
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000617 const MCSymbol *BeginLabel,
618 const MCSymbol *EndLabel, int State) {
Reid Klecknerd880dc72015-10-09 20:39:39 +0000619 auto &OS = *Asm->OutStreamer;
620 MCContext &Ctx = Asm->OutContext;
David Majnemer081e8fe2015-12-27 06:07:12 +0000621 bool VerboseAsm = OS.isVerboseAsm();
622 auto AddComment = [&](const Twine &Comment) {
623 if (VerboseAsm)
624 OS.AddComment(Comment);
625 };
626
Reid Klecknerd880dc72015-10-09 20:39:39 +0000627 assert(BeginLabel && EndLabel);
628 while (State != -1) {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000629 const SEHUnwindMapEntry &UME = FuncInfo.SEHUnwindMap[State];
Reid Klecknerd880dc72015-10-09 20:39:39 +0000630 const MCExpr *FilterOrFinally;
631 const MCExpr *ExceptOrNull;
632 auto *Handler = UME.Handler.get<MachineBasicBlock *>();
633 if (UME.IsFinally) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000634 FilterOrFinally = create32bitRef(getMCSymbolForMBB(Asm, Handler));
Reid Klecknerd880dc72015-10-09 20:39:39 +0000635 ExceptOrNull = MCConstantExpr::create(0, Ctx);
636 } else {
637 // For an except, the filter can be 1 (catch-all) or a function
638 // label.
639 FilterOrFinally = UME.Filter ? create32bitRef(UME.Filter)
640 : MCConstantExpr::create(1, Ctx);
641 ExceptOrNull = create32bitRef(Handler->getSymbol());
642 }
643
David Majnemer081e8fe2015-12-27 06:07:12 +0000644 AddComment("LabelStart");
Fangrui Song77497102020-02-14 22:40:47 -0800645 OS.emitValue(getLabel(BeginLabel), 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000646 AddComment("LabelEnd");
Fangrui Song77497102020-02-14 22:40:47 -0800647 OS.emitValue(getLabel(EndLabel), 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000648 AddComment(UME.IsFinally ? "FinallyFunclet" : UME.Filter ? "FilterFunction"
649 : "CatchAll");
Fangrui Song77497102020-02-14 22:40:47 -0800650 OS.emitValue(FilterOrFinally, 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000651 AddComment(UME.IsFinally ? "Null" : "ExceptionHandler");
Fangrui Song77497102020-02-14 22:40:47 -0800652 OS.emitValue(ExceptOrNull, 4);
Reid Klecknerd880dc72015-10-09 20:39:39 +0000653
654 assert(UME.ToState < State && "states should decrease");
655 State = UME.ToState;
656 }
657}
658
Reid Kleckner60b640b2015-05-28 22:47:01 +0000659void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000660 const Function &F = MF->getFunction();
Lang Hames9ff69c82015-04-24 19:11:51 +0000661 auto &OS = *Asm->OutStreamer;
Reid Klecknerc20276d2015-11-17 21:10:25 +0000662 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
David Majnemercde33032015-03-30 22:58:10 +0000663
Matthias Braunf1caa282017-12-15 22:22:58 +0000664 StringRef FuncLinkageName = GlobalValue::dropLLVMManglingEscape(F.getName());
David Majnemercde33032015-03-30 22:58:10 +0000665
Reid Klecknerc71d6272015-09-28 23:56:30 +0000666 SmallVector<std::pair<const MCExpr *, int>, 4> IPToStateTable;
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000667 MCSymbol *FuncInfoXData = nullptr;
668 if (shouldEmitPersonality) {
Reid Klecknerc71d6272015-09-28 23:56:30 +0000669 // If we're 64-bit, emit a pointer to the C++ EH data, and build a map from
670 // IPs to state numbers.
Reid Kleckner813f1b62015-09-16 22:14:46 +0000671 FuncInfoXData =
672 Asm->OutContext.getOrCreateSymbol(Twine("$cppxdata$", FuncLinkageName));
Reid Klecknerc71d6272015-09-28 23:56:30 +0000673 computeIP2StateTable(MF, FuncInfo, IPToStateTable);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000674 } else {
Reid Kleckner813f1b62015-09-16 22:14:46 +0000675 FuncInfoXData = Asm->OutContext.getOrCreateLSDASymbol(FuncLinkageName);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000676 }
677
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000678 int UnwindHelpOffset = 0;
679 if (Asm->MAI->usesWindowsCFI())
Reid Kleckner6ddae312015-11-05 21:09:49 +0000680 UnwindHelpOffset =
681 getFrameIndexOffset(FuncInfo.UnwindHelpFrameIdx, FuncInfo);
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000682
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000683 MCSymbol *UnwindMapXData = nullptr;
684 MCSymbol *TryBlockMapXData = nullptr;
685 MCSymbol *IPToStateXData = nullptr;
Reid Kleckner14e77352015-10-09 23:34:53 +0000686 if (!FuncInfo.CxxUnwindMap.empty())
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000687 UnwindMapXData = Asm->OutContext.getOrCreateSymbol(
Reid Kleckner813f1b62015-09-16 22:14:46 +0000688 Twine("$stateUnwindMap$", FuncLinkageName));
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000689 if (!FuncInfo.TryBlockMap.empty())
Reid Kleckner813f1b62015-09-16 22:14:46 +0000690 TryBlockMapXData =
691 Asm->OutContext.getOrCreateSymbol(Twine("$tryMap$", FuncLinkageName));
Reid Klecknerc71d6272015-09-28 23:56:30 +0000692 if (!IPToStateTable.empty())
Reid Kleckner813f1b62015-09-16 22:14:46 +0000693 IPToStateXData =
694 Asm->OutContext.getOrCreateSymbol(Twine("$ip2state$", FuncLinkageName));
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000695
David Majnemer081e8fe2015-12-27 06:07:12 +0000696 bool VerboseAsm = OS.isVerboseAsm();
697 auto AddComment = [&](const Twine &Comment) {
698 if (VerboseAsm)
699 OS.AddComment(Comment);
700 };
701
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000702 // FuncInfo {
703 // uint32_t MagicNumber
704 // int32_t MaxState;
705 // UnwindMapEntry *UnwindMap;
706 // uint32_t NumTryBlocks;
707 // TryBlockMapEntry *TryBlockMap;
708 // uint32_t IPMapEntries; // always 0 for x86
709 // IPToStateMapEntry *IPToStateMap; // always 0 for x86
710 // uint32_t UnwindHelp; // non-x86 only
711 // ESTypeList *ESTypeList;
712 // int32_t EHFlags;
713 // }
714 // EHFlags & 1 -> Synchronous exceptions only, no async exceptions.
715 // EHFlags & 2 -> ???
716 // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue.
Fangrui Song6d2d5892020-02-14 19:21:58 -0800717 OS.emitValueToAlignment(4);
718 OS.emitLabel(FuncInfoXData);
David Majnemer081e8fe2015-12-27 06:07:12 +0000719
720 AddComment("MagicNumber");
Fangrui Song692e0c92020-02-29 08:25:22 -0800721 OS.emitInt32(0x19930522);
David Majnemer081e8fe2015-12-27 06:07:12 +0000722
723 AddComment("MaxState");
Fangrui Song692e0c92020-02-29 08:25:22 -0800724 OS.emitInt32(FuncInfo.CxxUnwindMap.size());
David Majnemer081e8fe2015-12-27 06:07:12 +0000725
726 AddComment("UnwindMap");
Fangrui Song77497102020-02-14 22:40:47 -0800727 OS.emitValue(create32bitRef(UnwindMapXData), 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000728
729 AddComment("NumTryBlocks");
Fangrui Song692e0c92020-02-29 08:25:22 -0800730 OS.emitInt32(FuncInfo.TryBlockMap.size());
David Majnemer081e8fe2015-12-27 06:07:12 +0000731
732 AddComment("TryBlockMap");
Fangrui Song77497102020-02-14 22:40:47 -0800733 OS.emitValue(create32bitRef(TryBlockMapXData), 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000734
735 AddComment("IPMapEntries");
Fangrui Song692e0c92020-02-29 08:25:22 -0800736 OS.emitInt32(IPToStateTable.size());
David Majnemer081e8fe2015-12-27 06:07:12 +0000737
738 AddComment("IPToStateXData");
Fangrui Song77497102020-02-14 22:40:47 -0800739 OS.emitValue(create32bitRef(IPToStateXData), 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000740
741 if (Asm->MAI->usesWindowsCFI()) {
742 AddComment("UnwindHelp");
Fangrui Song692e0c92020-02-29 08:25:22 -0800743 OS.emitInt32(UnwindHelpOffset);
David Majnemer081e8fe2015-12-27 06:07:12 +0000744 }
745
746 AddComment("ESTypeList");
Fangrui Song692e0c92020-02-29 08:25:22 -0800747 OS.emitInt32(0);
David Majnemer081e8fe2015-12-27 06:07:12 +0000748
749 AddComment("EHFlags");
Fangrui Song692e0c92020-02-29 08:25:22 -0800750 OS.emitInt32(1);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000751
752 // UnwindMapEntry {
753 // int32_t ToState;
754 // void (*Action)();
755 // };
756 if (UnwindMapXData) {
Fangrui Song6d2d5892020-02-14 19:21:58 -0800757 OS.emitLabel(UnwindMapXData);
Reid Kleckner14e77352015-10-09 23:34:53 +0000758 for (const CxxUnwindMapEntry &UME : FuncInfo.CxxUnwindMap) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000759 MCSymbol *CleanupSym =
760 getMCSymbolForMBB(Asm, UME.Cleanup.dyn_cast<MachineBasicBlock *>());
David Majnemer081e8fe2015-12-27 06:07:12 +0000761 AddComment("ToState");
Fangrui Song692e0c92020-02-29 08:25:22 -0800762 OS.emitInt32(UME.ToState);
David Majnemer081e8fe2015-12-27 06:07:12 +0000763
764 AddComment("Action");
Fangrui Song77497102020-02-14 22:40:47 -0800765 OS.emitValue(create32bitRef(CleanupSym), 4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000766 }
767 }
768
769 // TryBlockMap {
770 // int32_t TryLow;
771 // int32_t TryHigh;
772 // int32_t CatchHigh;
773 // int32_t NumCatches;
774 // HandlerType *HandlerArray;
775 // };
776 if (TryBlockMapXData) {
Fangrui Song6d2d5892020-02-14 19:21:58 -0800777 OS.emitLabel(TryBlockMapXData);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000778 SmallVector<MCSymbol *, 1> HandlerMaps;
779 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000780 const WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000781
Reid Kleckner813f1b62015-09-16 22:14:46 +0000782 MCSymbol *HandlerMapXData = nullptr;
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000783 if (!TBME.HandlerArray.empty())
784 HandlerMapXData =
785 Asm->OutContext.getOrCreateSymbol(Twine("$handlerMap$")
786 .concat(Twine(I))
787 .concat("$")
Reid Kleckner813f1b62015-09-16 22:14:46 +0000788 .concat(FuncLinkageName));
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000789 HandlerMaps.push_back(HandlerMapXData);
790
Reid Kleckner813f1b62015-09-16 22:14:46 +0000791 // TBMEs should form intervals.
792 assert(0 <= TBME.TryLow && "bad trymap interval");
793 assert(TBME.TryLow <= TBME.TryHigh && "bad trymap interval");
794 assert(TBME.TryHigh < TBME.CatchHigh && "bad trymap interval");
Reid Kleckner14e77352015-10-09 23:34:53 +0000795 assert(TBME.CatchHigh < int(FuncInfo.CxxUnwindMap.size()) &&
Reid Kleckner813f1b62015-09-16 22:14:46 +0000796 "bad trymap interval");
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000797
David Majnemer081e8fe2015-12-27 06:07:12 +0000798 AddComment("TryLow");
Fangrui Song692e0c92020-02-29 08:25:22 -0800799 OS.emitInt32(TBME.TryLow);
David Majnemer081e8fe2015-12-27 06:07:12 +0000800
801 AddComment("TryHigh");
Fangrui Song692e0c92020-02-29 08:25:22 -0800802 OS.emitInt32(TBME.TryHigh);
David Majnemer081e8fe2015-12-27 06:07:12 +0000803
804 AddComment("CatchHigh");
Fangrui Song692e0c92020-02-29 08:25:22 -0800805 OS.emitInt32(TBME.CatchHigh);
David Majnemer081e8fe2015-12-27 06:07:12 +0000806
807 AddComment("NumCatches");
Fangrui Song692e0c92020-02-29 08:25:22 -0800808 OS.emitInt32(TBME.HandlerArray.size());
David Majnemer081e8fe2015-12-27 06:07:12 +0000809
810 AddComment("HandlerArray");
Fangrui Song77497102020-02-14 22:40:47 -0800811 OS.emitValue(create32bitRef(HandlerMapXData), 4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000812 }
813
Reid Kleckner28e49032015-10-16 23:43:27 +0000814 // All funclets use the same parent frame offset currently.
815 unsigned ParentFrameOffset = 0;
816 if (shouldEmitPersonality) {
817 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
818 ParentFrameOffset = TFI->getWinEHParentFrameOffset(*MF);
819 }
820
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000821 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000822 const WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000823 MCSymbol *HandlerMapXData = HandlerMaps[I];
824 if (!HandlerMapXData)
825 continue;
826 // HandlerType {
827 // int32_t Adjectives;
828 // TypeDescriptor *Type;
829 // int32_t CatchObjOffset;
830 // void (*Handler)();
Sanjin Sijaric96f2ea32018-10-27 06:13:06 +0000831 // int32_t ParentFrameOffset; // x64 and AArch64 only
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000832 // };
Fangrui Song6d2d5892020-02-14 19:21:58 -0800833 OS.emitLabel(HandlerMapXData);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000834 for (const WinEHHandlerType &HT : TBME.HandlerArray) {
835 // Get the frame escape label with the offset of the catch object. If
David Majnemer99c1d132015-10-12 16:44:22 +0000836 // the index is INT_MAX, then there is no catch object, and we should
837 // emit an offset of zero, indicating that no copy will occur.
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000838 const MCExpr *FrameAllocOffsetRef = nullptr;
David Majnemer99c1d132015-10-12 16:44:22 +0000839 if (HT.CatchObj.FrameIndex != INT_MAX) {
Reid Kleckner6ddae312015-11-05 21:09:49 +0000840 int Offset = getFrameIndexOffset(HT.CatchObj.FrameIndex, FuncInfo);
David Majnemercb305de2016-03-01 04:30:16 +0000841 assert(Offset != 0 && "Illegal offset for catch object!");
Reid Klecknerb005d282015-09-16 20:16:27 +0000842 FrameAllocOffsetRef = MCConstantExpr::create(Offset, Asm->OutContext);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000843 } else {
Jim Grosbach13760bd2015-05-30 01:25:56 +0000844 FrameAllocOffsetRef = MCConstantExpr::create(0, Asm->OutContext);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000845 }
846
David Majnemerbfa5b982015-10-10 00:04:29 +0000847 MCSymbol *HandlerSym =
848 getMCSymbolForMBB(Asm, HT.Handler.dyn_cast<MachineBasicBlock *>());
Reid Kleckner94b704c2015-09-09 21:10:03 +0000849
David Majnemer081e8fe2015-12-27 06:07:12 +0000850 AddComment("Adjectives");
Fangrui Song692e0c92020-02-29 08:25:22 -0800851 OS.emitInt32(HT.Adjectives);
David Majnemer081e8fe2015-12-27 06:07:12 +0000852
853 AddComment("Type");
Fangrui Song77497102020-02-14 22:40:47 -0800854 OS.emitValue(create32bitRef(HT.TypeDescriptor), 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000855
856 AddComment("CatchObjOffset");
Fangrui Song77497102020-02-14 22:40:47 -0800857 OS.emitValue(FrameAllocOffsetRef, 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000858
859 AddComment("Handler");
Fangrui Song77497102020-02-14 22:40:47 -0800860 OS.emitValue(create32bitRef(HandlerSym), 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000861
862 if (shouldEmitPersonality) {
863 AddComment("ParentFrameOffset");
Fangrui Song692e0c92020-02-29 08:25:22 -0800864 OS.emitInt32(ParentFrameOffset);
David Majnemer081e8fe2015-12-27 06:07:12 +0000865 }
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000866 }
867 }
868 }
869
870 // IPToStateMapEntry {
871 // void *IP;
872 // int32_t State;
873 // };
874 if (IPToStateXData) {
Fangrui Song6d2d5892020-02-14 19:21:58 -0800875 OS.emitLabel(IPToStateXData);
Reid Klecknerc71d6272015-09-28 23:56:30 +0000876 for (auto &IPStatePair : IPToStateTable) {
David Majnemer081e8fe2015-12-27 06:07:12 +0000877 AddComment("IP");
Fangrui Song77497102020-02-14 22:40:47 -0800878 OS.emitValue(IPStatePair.first, 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000879 AddComment("ToState");
Fangrui Song692e0c92020-02-29 08:25:22 -0800880 OS.emitInt32(IPStatePair.second);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000881 }
882 }
883}
884
Reid Klecknerc71d6272015-09-28 23:56:30 +0000885void WinException::computeIP2StateTable(
Reid Klecknerc20276d2015-11-17 21:10:25 +0000886 const MachineFunction *MF, const WinEHFuncInfo &FuncInfo,
Reid Klecknerc71d6272015-09-28 23:56:30 +0000887 SmallVectorImpl<std::pair<const MCExpr *, int>> &IPToStateTable) {
Andrew Kaylor762a6be2015-05-11 19:41:19 +0000888
David Majnemer8a1c45d2015-12-12 05:38:55 +0000889 for (MachineFunction::const_iterator FuncletStart = MF->begin(),
890 FuncletEnd = MF->begin(),
891 End = MF->end();
892 FuncletStart != End; FuncletStart = FuncletEnd) {
893 // Find the end of the funclet
894 while (++FuncletEnd != End) {
895 if (FuncletEnd->isEHFuncletEntry()) {
896 break;
897 }
898 }
899
900 // Don't emit ip2state entries for cleanup funclets. Any interesting
901 // exceptional actions in cleanups must be handled in a separate IR
902 // function.
903 if (FuncletStart->isCleanupFuncletEntry())
904 continue;
905
906 MCSymbol *StartLabel;
907 int BaseState;
908 if (FuncletStart == MF->begin()) {
909 BaseState = NullState;
910 StartLabel = Asm->getFunctionBegin();
911 } else {
912 auto *FuncletPad =
913 cast<FuncletPadInst>(FuncletStart->getBasicBlock()->getFirstNonPHI());
914 assert(FuncInfo.FuncletBaseStateMap.count(FuncletPad) != 0);
915 BaseState = FuncInfo.FuncletBaseStateMap.find(FuncletPad)->second;
916 StartLabel = getMCSymbolForMBB(Asm, &*FuncletStart);
917 }
918 assert(StartLabel && "need local function start label");
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000919 IPToStateTable.push_back(
David Majnemer8a1c45d2015-12-12 05:38:55 +0000920 std::make_pair(create32bitRef(StartLabel), BaseState));
921
922 for (const auto &StateChange : InvokeStateChangeIterator::range(
923 FuncInfo, FuncletStart, FuncletEnd, BaseState)) {
924 // Compute the label to report as the start of this entry; use the EH
925 // start label for the invoke if we have one, otherwise (this is a call
926 // which may unwind to our caller and does not have an EH start label, so)
927 // use the previous end label.
928 const MCSymbol *ChangeLabel = StateChange.NewStartLabel;
929 if (!ChangeLabel)
930 ChangeLabel = StateChange.PreviousEndLabel;
931 // Emit an entry indicating that PCs after 'Label' have this EH state.
932 IPToStateTable.push_back(
Sanjin Sijaric96f2ea32018-10-27 06:13:06 +0000933 std::make_pair(getLabel(ChangeLabel), StateChange.NewState));
David Majnemer8a1c45d2015-12-12 05:38:55 +0000934 // FIXME: assert that NewState is between CatchLow and CatchHigh.
935 }
Reid Klecknerc71d6272015-09-28 23:56:30 +0000936 }
David Majnemercde33032015-03-30 22:58:10 +0000937}
Reid Klecknerf12c0302015-06-09 21:42:19 +0000938
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000939void WinException::emitEHRegistrationOffsetLabel(const WinEHFuncInfo &FuncInfo,
940 StringRef FLinkageName) {
941 // Outlined helpers called by the EH runtime need to know the offset of the EH
942 // registration in order to recover the parent frame pointer. Now that we know
943 // we've code generated the parent, we can emit the label assignment that
944 // those helpers use to get the offset of the registration node.
Reid Kleckner9cb915b2016-09-30 22:10:12 +0000945
946 // Compute the parent frame offset. The EHRegNodeFrameIndex will be invalid if
947 // after optimization all the invokes were eliminated. We still need to emit
948 // the parent frame offset label, but it should be garbage and should never be
949 // used.
950 int64_t Offset = 0;
951 int FI = FuncInfo.EHRegNodeFrameIndex;
952 if (FI != INT_MAX) {
953 const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
Mandeep Singh Grang70d484d2019-02-01 21:41:33 +0000954 Offset = TFI->getNonLocalFrameIndexReference(*Asm->MF, FI);
Reid Kleckner9cb915b2016-09-30 22:10:12 +0000955 }
956
Reid Klecknerc20276d2015-11-17 21:10:25 +0000957 MCContext &Ctx = Asm->OutContext;
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000958 MCSymbol *ParentFrameOffset =
Reid Klecknerc20276d2015-11-17 21:10:25 +0000959 Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName);
Fangrui Songa55daa12020-02-14 18:16:24 -0800960 Asm->OutStreamer->emitAssignment(ParentFrameOffset,
Reid Kleckner9cb915b2016-09-30 22:10:12 +0000961 MCConstantExpr::create(Offset, Ctx));
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000962}
963
Reid Klecknerf12c0302015-06-09 21:42:19 +0000964/// Emit the language-specific data that _except_handler3 and 4 expect. This is
965/// functionally equivalent to the __C_specific_handler table, except it is
966/// indexed by state number instead of IP.
967void WinException::emitExceptHandlerTable(const MachineFunction *MF) {
Reid Klecknera9d62532015-06-11 22:32:23 +0000968 MCStreamer &OS = *Asm->OutStreamer;
Matthias Braunf1caa282017-12-15 22:22:58 +0000969 const Function &F = MF->getFunction();
970 StringRef FLinkageName = GlobalValue::dropLLVMManglingEscape(F.getName());
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000971
David Majnemer081e8fe2015-12-27 06:07:12 +0000972 bool VerboseAsm = OS.isVerboseAsm();
973 auto AddComment = [&](const Twine &Comment) {
974 if (VerboseAsm)
975 OS.AddComment(Comment);
976 };
977
Reid Klecknerc20276d2015-11-17 21:10:25 +0000978 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000979 emitEHRegistrationOffsetLabel(FuncInfo, FLinkageName);
Reid Klecknerf12c0302015-06-09 21:42:19 +0000980
981 // Emit the __ehtable label that we use for llvm.x86.seh.lsda.
Reid Klecknerf12c0302015-06-09 21:42:19 +0000982 MCSymbol *LSDALabel = Asm->OutContext.getOrCreateLSDASymbol(FLinkageName);
Fangrui Song6d2d5892020-02-14 19:21:58 -0800983 OS.emitValueToAlignment(4);
984 OS.emitLabel(LSDALabel);
Reid Klecknerf12c0302015-06-09 21:42:19 +0000985
Simon Pilgrimc9129ce2019-10-02 11:48:32 +0000986 const auto *Per = cast<Function>(F.getPersonalityFn()->stripPointerCasts());
Reid Klecknerf12c0302015-06-09 21:42:19 +0000987 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 //
Etienne Bergeronf6be62f2016-06-21 15:58:55 +00001001 // 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;
Matthias Braun941a7052016-07-28 18:40:00 +00001013 const MachineFrameInfo &MFI = MF->getFrameInfo();
1014 if (MFI.hasStackProtectorIndex()) {
Matt Arsenault2481f262020-04-07 16:33:58 -04001015 Register UnusedReg;
Etienne Bergeronf6be62f2016-06-21 15:58:55 +00001016 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
Matthias Braun941a7052016-07-28 18:40:00 +00001017 int SSPIdx = MFI.getStackProtectorIndex();
Etienne Bergeronf6be62f2016-06-21 15:58:55 +00001018 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) {
Matt Arsenault2481f262020-04-07 16:33:58 -04001025 Register UnusedReg;
Etienne Bergeronf6be62f2016-06-21 15:58:55 +00001026 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
1027 int EHGuardIdx = FuncInfo.EHGuardFrameIndex;
1028 EHCookieOffset = TFI->getFrameIndexReference(*MF, EHGuardIdx, UnusedReg);
1029 }
1030
David Majnemer081e8fe2015-12-27 06:07:12 +00001031 AddComment("GSCookieOffset");
Fangrui Song692e0c92020-02-29 08:25:22 -08001032 OS.emitInt32(GSCookieOffset);
David Majnemer081e8fe2015-12-27 06:07:12 +00001033 AddComment("GSCookieXOROffset");
Fangrui Song692e0c92020-02-29 08:25:22 -08001034 OS.emitInt32(0);
David Majnemer081e8fe2015-12-27 06:07:12 +00001035 AddComment("EHCookieOffset");
Fangrui Song692e0c92020-02-29 08:25:22 -08001036 OS.emitInt32(EHCookieOffset);
David Majnemer081e8fe2015-12-27 06:07:12 +00001037 AddComment("EHCookieXOROffset");
Fangrui Song692e0c92020-02-29 08:25:22 -08001038 OS.emitInt32(0);
Reid Klecknerf12c0302015-06-09 21:42:19 +00001039 BaseState = -2;
1040 }
1041
Reid Kleckner14e77352015-10-09 23:34:53 +00001042 assert(!FuncInfo.SEHUnwindMap.empty());
Reid Klecknerc20276d2015-11-17 21:10:25 +00001043 for (const SEHUnwindMapEntry &UME : FuncInfo.SEHUnwindMap) {
David Majnemer081e8fe2015-12-27 06:07:12 +00001044 auto *Handler = UME.Handler.get<MachineBasicBlock *>();
1045 const MCSymbol *ExceptOrFinally =
1046 UME.IsFinally ? getMCSymbolForMBB(Asm, Handler) : Handler->getSymbol();
Reid Kleckner14e77352015-10-09 23:34:53 +00001047 // -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;
David Majnemer081e8fe2015-12-27 06:07:12 +00001050 AddComment("ToState");
Fangrui Song692e0c92020-02-29 08:25:22 -08001051 OS.emitInt32(ToState);
David Majnemer081e8fe2015-12-27 06:07:12 +00001052 AddComment(UME.IsFinally ? "Null" : "FilterFunction");
Fangrui Song77497102020-02-14 22:40:47 -08001053 OS.emitValue(create32bitRef(UME.Filter), 4);
David Majnemer081e8fe2015-12-27 06:07:12 +00001054 AddComment(UME.IsFinally ? "FinallyFunclet" : "ExceptionHandler");
Fangrui Song77497102020-02-14 22:40:47 -08001055 OS.emitValue(create32bitRef(ExceptOrFinally), 4);
Reid Klecknerf12c0302015-06-09 21:42:19 +00001056 }
1057}
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001058
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001059static int getTryRank(const WinEHFuncInfo &FuncInfo, int State) {
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001060 int Rank = 0;
1061 while (State != -1) {
1062 ++Rank;
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001063 State = FuncInfo.ClrEHUnwindMap[State].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001064 }
1065 return Rank;
1066}
1067
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001068static int getTryAncestor(const WinEHFuncInfo &FuncInfo, int Left, int Right) {
1069 int LeftRank = getTryRank(FuncInfo, Left);
1070 int RightRank = getTryRank(FuncInfo, Right);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001071
1072 while (LeftRank < RightRank) {
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001073 Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001074 --RightRank;
1075 }
1076
1077 while (RightRank < LeftRank) {
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001078 Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001079 --LeftRank;
1080 }
1081
1082 while (Left != Right) {
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001083 Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState;
1084 Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001085 }
1086
1087 return Left;
1088}
1089
1090void 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;
Reid Klecknerc20276d2015-11-17 21:10:25 +00001095 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001096 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!");
1112 DenseMap<const MachineBasicBlock *, int> HandlerStates;
1113 for (int State = 0; State < NumStates; ++State) {
1114 MachineBasicBlock *HandlerBlock =
1115 FuncInfo.ClrEHUnwindMap[State].Handler.get<MachineBasicBlock *>();
1116 HandlerStates[HandlerBlock] = State;
1117 // Use this loop through all handlers to verify our assumption (used in
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001118 // the MinEnclosingState computation) that enclosing funclets have lower
1119 // state numbers than their enclosed funclets.
1120 assert(FuncInfo.ClrEHUnwindMap[State].HandlerParentState < State &&
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001121 "ill-formed state numbering");
1122 }
1123 // Map the main function to the NullState.
Duncan P. N. Exon Smitha25ad062015-10-20 00:36:08 +00001124 HandlerStates[&MF->front()] = NullState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001125
1126 // Write out a sentinel indicating the end of the standard (Windows) xdata
1127 // and the start of the additional (CLR) info.
Fangrui Song692e0c92020-02-29 08:25:22 -08001128 OS.emitInt32(0xffffffff);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001129 // Write out the number of funclets
Fangrui Song692e0c92020-02-29 08:25:22 -08001130 OS.emitInt32(NumStates);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001131
1132 // Walk the machine blocks/instrs, computing and emitting a few things:
1133 // 1. Emit a list of the offsets to each handler entry, in lexical order.
1134 // 2. Compute a map (EndSymbolMap) from each funclet to the symbol at its end.
1135 // 3. Compute the list of ClrClauses, in the required order (inner before
1136 // outer, earlier before later; the order by which a forward scan with
1137 // early termination will find the innermost enclosing clause covering
1138 // a given address).
1139 // 4. A map (MinClauseMap) from each handler index to the index of the
1140 // outermost funclet/function which contains a try clause targeting the
1141 // key handler. This will be used to determine IsDuplicate-ness when
1142 // emitting ClrClauses. The NullState value is used to indicate that the
1143 // top-level function contains a try clause targeting the key handler.
1144 // HandlerStack is a stack of (PendingStartLabel, PendingState) pairs for
1145 // try regions we entered before entering the PendingState try but which
1146 // we haven't yet exited.
1147 SmallVector<std::pair<const MCSymbol *, int>, 4> HandlerStack;
1148 // EndSymbolMap and MinClauseMap are maps described above.
1149 std::unique_ptr<MCSymbol *[]> EndSymbolMap(new MCSymbol *[NumStates]);
1150 SmallVector<int, 4> MinClauseMap((size_t)NumStates, NumStates);
1151
1152 // Visit the root function and each funclet.
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001153 for (MachineFunction::const_iterator FuncletStart = MF->begin(),
1154 FuncletEnd = MF->begin(),
1155 End = MF->end();
1156 FuncletStart != End; FuncletStart = FuncletEnd) {
Duncan P. N. Exon Smitha25ad062015-10-20 00:36:08 +00001157 int FuncletState = HandlerStates[&*FuncletStart];
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001158 // Find the end of the funclet
1159 MCSymbol *EndSymbol = FuncEndSym;
1160 while (++FuncletEnd != End) {
1161 if (FuncletEnd->isEHFuncletEntry()) {
Duncan P. N. Exon Smitha25ad062015-10-20 00:36:08 +00001162 EndSymbol = getMCSymbolForMBB(Asm, &*FuncletEnd);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001163 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.
Fangrui Song77497102020-02-14 22:40:47 -08001168 OS.emitValue(getOffset(EndSymbol, FuncBeginSym), 4);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001169 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
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001182 int StillPendingState =
1183 getTryAncestor(FuncInfo, CurrentState, StateChange.NewState);
1184 while (CurrentState != StillPendingState) {
1185 assert(CurrentState != NullState &&
1186 "Failed to find still-pending state!");
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001187 // Close the pending clause
1188 Clauses.push_back({CurrentStartLabel, StateChange.PreviousEndLabel,
1189 CurrentState, FuncletState});
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001190 // Now the next-outer try region is current
1191 CurrentState = FuncInfo.ClrEHUnwindMap[CurrentState].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001192 // Pop the new start label from the handler stack if we've exited all
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001193 // inner try regions of the corresponding try region.
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001194 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;
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001204 EnteredState =
1205 FuncInfo.ClrEHUnwindMap[EnteredState].TryParentState) {
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001206 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.
Fangrui Song692e0c92020-02-29 08:25:22 -08001221 OS.emitInt32(Clauses.size());
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001222 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
Reid Klecknerc20276d2015-11-17 21:10:25 +00001275 const ClrEHUnwindMapEntry &Entry = FuncInfo.ClrEHUnwindMap[Clause.State];
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001276 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;
1287 case ClrHandlerType::Filter:
1288 Flags |= 1;
1289 break;
1290 case ClrHandlerType::Finally:
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 }
Fangrui Song692e0c92020-02-29 08:25:22 -08001303 OS.emitInt32(Flags);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001304
1305 // Write the clause start/end
Fangrui Song77497102020-02-14 22:40:47 -08001306 OS.emitValue(ClauseBegin, 4);
1307 OS.emitValue(ClauseEnd, 4);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001308
1309 // Write out the handler start/end
Fangrui Song77497102020-02-14 22:40:47 -08001310 OS.emitValue(HandlerBegin, 4);
1311 OS.emitValue(HandlerEnd, 4);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001312
1313 // Write out the type token or filter offset
1314 assert(Entry.HandlerType != ClrHandlerType::Filter && "NYI: filters");
Fangrui Song692e0c92020-02-29 08:25:22 -08001315 OS.emitInt32(Entry.TypeToken);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001316 }
1317}