blob: e706d73dee2ac32c7fe8b12c92724c6e474a1aa6 [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/SmallString.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/ADT/Twine.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 Majnemercde33032015-03-30 22:58:10 +000022#include "llvm/CodeGen/WinEHFuncInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000024#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Module.h"
Charles Davis91ed7992011-05-27 23:47:32 +000026#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCContext.h"
28#include "llvm/MC/MCExpr.h"
29#include "llvm/MC/MCSection.h"
30#include "llvm/MC/MCStreamer.h"
31#include "llvm/MC/MCSymbol.h"
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000032#include "llvm/MC/MCWin64EH.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Support/Dwarf.h"
34#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/FormattedStream.h"
Charles Davis91ed7992011-05-27 23:47:32 +000036#include "llvm/Target/TargetFrameLowering.h"
37#include "llvm/Target/TargetLoweringObjectFile.h"
Charles Davis91ed7992011-05-27 23:47:32 +000038#include "llvm/Target/TargetOptions.h"
39#include "llvm/Target/TargetRegisterInfo.h"
Charles Davis91ed7992011-05-27 23:47:32 +000040using namespace llvm;
41
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000042WinException::WinException(AsmPrinter *A) : EHStreamer(A) {
43 // MSVC's EH tables are always composed of 32-bit words. All known 64-bit
44 // platforms use an imagerel32 relocation to refer to symbols.
45 useImageRel32 = (A->getDataLayout().getPointerSizeInBits() == 64);
46}
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.
64 bool hasLandingPads = !MMI->getLandingPads().empty();
Reid Kleckner0e288232015-08-27 23:27:47 +000065 bool hasEHFunclets = MMI->hasEHFunclets();
Charles Davis5638b9f2011-05-28 04:21:04 +000066
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000067 const Function *F = MF->getFunction();
68 const Function *ParentF = MMI->getWinEHParent(F);
69
Charles Davis5638b9f2011-05-28 04:21:04 +000070 shouldEmitMoves = Asm->needsSEHMoves();
71
72 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
73 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Reid Kleckner9a1a9192015-07-13 20:41:46 +000074 const Function *Per = nullptr;
75 if (F->hasPersonalityFn())
76 Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
Charles Davis5638b9f2011-05-28 04:21:04 +000077
Keno Fischeraff703a2015-07-14 19:22:51 +000078 bool forceEmitPersonality =
79 F->hasPersonalityFn() && !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
80 F->needsUnwindTableEntry();
81
Reid Kleckner0e288232015-08-27 23:27:47 +000082 shouldEmitPersonality =
83 forceEmitPersonality || ((hasLandingPads || hasEHFunclets) &&
84 PerEncoding != dwarf::DW_EH_PE_omit && Per);
Charles Davis5638b9f2011-05-28 04:21:04 +000085
86 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
87 shouldEmitLSDA = shouldEmitPersonality &&
88 LSDAEncoding != dwarf::DW_EH_PE_omit;
89
Reid Kleckner0e288232015-08-27 23:27:47 +000090 // If we're not using CFI, we don't want the CFI or the personality, but we
91 // might want EH tables if we had EH pads.
92 // FIXME: If WinEHPrepare outlined something, we should emit the LSDA. Remove
93 // this once WinEHPrepare stops doing that.
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000094 if (!Asm->MAI->usesWindowsCFI()) {
Reid Kleckner0e288232015-08-27 23:27:47 +000095 shouldEmitLSDA =
96 hasEHFunclets || (F->hasFnAttribute("wineh-parent") && F == ParentF);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000097 shouldEmitPersonality = false;
98 return;
99 }
David Majnemera225a192015-03-31 22:35:44 +0000100
101 // If this was an outlined handler, we need to define the label corresponding
102 // to the offset of the parent frame relative to the stack pointer after the
103 // prologue.
David Majnemera225a192015-03-31 22:35:44 +0000104 if (F != ParentF) {
105 WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(ParentF);
106 auto I = FuncInfo.CatchHandlerParentFrameObjOffset.find(F);
107 if (I != FuncInfo.CatchHandlerParentFrameObjOffset.end()) {
108 MCSymbol *HandlerTypeParentFrameOffset =
109 Asm->OutContext.getOrCreateParentFrameOffsetSymbol(
110 GlobalValue::getRealLinkageName(F->getName()));
111
112 // Emit a symbol assignment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000113 Asm->OutStreamer->EmitAssignment(
David Majnemera225a192015-03-31 22:35:44 +0000114 HandlerTypeParentFrameOffset,
Jim Grosbach13760bd2015-05-30 01:25:56 +0000115 MCConstantExpr::create(I->second, Asm->OutContext));
David Majnemera225a192015-03-31 22:35:44 +0000116 }
117 }
118
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000119 if (shouldEmitMoves || shouldEmitPersonality)
120 Asm->OutStreamer->EmitWinCFIStartProc(Asm->CurrentFnSym);
Charles Davis5638b9f2011-05-28 04:21:04 +0000121
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000122 if (shouldEmitPersonality) {
123 const MCSymbol *PersHandlerSym =
124 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
125 Asm->OutStreamer->EmitWinEHHandler(PersHandlerSym, true, true);
126 }
Charles Davis91ed7992011-05-27 23:47:32 +0000127}
128
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000129/// endFunction - Gather and emit post-function exception information.
Charles Davis91ed7992011-05-27 23:47:32 +0000130///
Reid Kleckner60b640b2015-05-28 22:47:01 +0000131void WinException::endFunction(const MachineFunction *MF) {
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000132 if (!shouldEmitPersonality && !shouldEmitMoves && !shouldEmitLSDA)
Charles Davis5638b9f2011-05-28 04:21:04 +0000133 return;
134
Reid Kleckner9a1a9192015-07-13 20:41:46 +0000135 const Function *F = MF->getFunction();
136 EHPersonality Per = EHPersonality::Unknown;
137 if (F->hasPersonalityFn())
138 Per = classifyEHPersonality(F->getPersonalityFn());
Reid Kleckner3e9fadf2015-04-15 18:48:15 +0000139
140 // Get rid of any dead landing pads if we're not using a Windows EH scheme. In
141 // Windows EH schemes, the landing pad is not actually reachable. It only
142 // exists so that we can emit the right table data.
143 if (!isMSVCEHPersonality(Per))
144 MMI->TidyLandingPads();
Charles Davisa5752262011-05-30 00:13:34 +0000145
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000146 if (shouldEmitPersonality || shouldEmitLSDA) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000147 Asm->OutStreamer->PushSection();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000148
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000149 if (shouldEmitMoves || shouldEmitPersonality) {
150 // Emit an UNWIND_INFO struct describing the prologue.
151 Asm->OutStreamer->EmitWinEHHandlerData();
152 } else {
153 // Just switch sections to the right xdata section. This use of
154 // CurrentFnSym assumes that we only emit the LSDA when ending the parent
155 // function.
156 MCSection *XData = WinEH::UnwindEmitter::getXDataSection(
157 Asm->CurrentFnSym, Asm->OutContext);
158 Asm->OutStreamer->SwitchSection(XData);
159 }
Reid Kleckner0a57f652015-01-14 01:05:27 +0000160
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000161 // Emit the tables appropriate to the personality function in use. If we
162 // don't recognize the personality, assume it uses an Itanium-style LSDA.
Reid Kleckner2d5fb682015-02-14 00:21:02 +0000163 if (Per == EHPersonality::MSVC_Win64SEH)
Reid Kleckner94b704c2015-09-09 21:10:03 +0000164 emitCSpecificHandlerTable(MF);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000165 else if (Per == EHPersonality::MSVC_X86SEH)
Reid Klecknerf12c0302015-06-09 21:42:19 +0000166 emitExceptHandlerTable(MF);
David Majnemercde33032015-03-30 22:58:10 +0000167 else if (Per == EHPersonality::MSVC_CXX)
168 emitCXXFrameHandler3Table(MF);
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000169 else
Reid Kleckner0a57f652015-01-14 01:05:27 +0000170 emitExceptionTable();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000171
Lang Hames9ff69c82015-04-24 19:11:51 +0000172 Asm->OutStreamer->PopSection();
Charles Davisa5752262011-05-30 00:13:34 +0000173 }
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000174
175 if (shouldEmitMoves)
176 Asm->OutStreamer->EmitWinCFIEndProc();
Charles Davis91ed7992011-05-27 23:47:32 +0000177}
Reid Kleckner0a57f652015-01-14 01:05:27 +0000178
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000179const MCExpr *WinException::create32bitRef(const MCSymbol *Value) {
David Majnemercde33032015-03-30 22:58:10 +0000180 if (!Value)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000181 return MCConstantExpr::create(0, Asm->OutContext);
182 return MCSymbolRefExpr::create(Value, useImageRel32
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000183 ? MCSymbolRefExpr::VK_COFF_IMGREL32
184 : MCSymbolRefExpr::VK_None,
Reid Kleckner0a57f652015-01-14 01:05:27 +0000185 Asm->OutContext);
186}
187
David Majnemer0ad363e2015-08-18 19:07:12 +0000188const MCExpr *WinException::create32bitRef(const Value *V) {
189 if (!V)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000190 return MCConstantExpr::create(0, Asm->OutContext);
Reid Kleckner0e288232015-08-27 23:27:47 +0000191 // FIXME: Delete the GlobalValue case once the new IR is fully functional.
192 if (const auto *GV = dyn_cast<GlobalValue>(V))
193 return create32bitRef(Asm->getSymbol(GV));
194 return create32bitRef(MMI->getAddrLabelSymbol(cast<BasicBlock>(V)));
David Majnemercde33032015-03-30 22:58:10 +0000195}
196
Reid Kleckner0a57f652015-01-14 01:05:27 +0000197/// Emit the language-specific data that __C_specific_handler expects. This
198/// handler lives in the x64 Microsoft C runtime and allows catching or cleaning
199/// up after faults with __try, __except, and __finally. The typeinfo values
200/// are not really RTTI data, but pointers to filter functions that return an
201/// integer (1, 0, or -1) indicating how to handle the exception. For __finally
202/// blocks and other cleanups, the landing pad label is zero, and the filter
203/// function is actually a cleanup handler with the same prototype. A catch-all
204/// entry is modeled with a null filter function field and a non-zero landing
205/// pad label.
206///
207/// Possible filter function return values:
208/// EXCEPTION_EXECUTE_HANDLER (1):
209/// Jump to the landing pad label after cleanups.
210/// EXCEPTION_CONTINUE_SEARCH (0):
211/// Continue searching this table or continue unwinding.
212/// EXCEPTION_CONTINUE_EXECUTION (-1):
213/// Resume execution at the trapping PC.
214///
215/// Inferred table structure:
216/// struct Table {
217/// int NumEntries;
218/// struct Entry {
219/// imagerel32 LabelStart;
220/// imagerel32 LabelEnd;
Reid Klecknerf690f502015-01-22 02:27:44 +0000221/// imagerel32 FilterOrFinally; // One means catch-all.
Reid Kleckner0a57f652015-01-14 01:05:27 +0000222/// imagerel32 LabelLPad; // Zero means __finally.
223/// } Entries[NumEntries];
224/// };
Reid Kleckner94b704c2015-09-09 21:10:03 +0000225void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) {
Reid Kleckner0a57f652015-01-14 01:05:27 +0000226 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
227
Reid Kleckner94b704c2015-09-09 21:10:03 +0000228 WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(MF->getFunction());
229 if (!FuncInfo.SEHUnwindMap.empty())
230 report_fatal_error("x64 SEH tables not yet implemented");
231
Reid Kleckner0a57f652015-01-14 01:05:27 +0000232 // Simplifying assumptions for first implementation:
233 // - Cleanups are not implemented.
234 // - Filters are not implemented.
235
236 // The Itanium LSDA table sorts similar landing pads together to simplify the
237 // actions table, but we don't need that.
238 SmallVector<const LandingPadInfo *, 64> LandingPads;
239 LandingPads.reserve(PadInfos.size());
240 for (const auto &LP : PadInfos)
241 LandingPads.push_back(&LP);
242
243 // Compute label ranges for call sites as we would for the Itanium LSDA, but
244 // use an all zero action table because we aren't using these actions.
245 SmallVector<unsigned, 64> FirstActions;
246 FirstActions.resize(LandingPads.size());
247 SmallVector<CallSiteEntry, 64> CallSites;
248 computeCallSiteTable(CallSites, LandingPads, FirstActions);
249
Rafael Espindola629cdba2015-02-27 18:18:39 +0000250 MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin();
251 MCSymbol *EHFuncEndSym = Asm->getFunctionEnd();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000252
253 // Emit the number of table entries.
254 unsigned NumEntries = 0;
255 for (const CallSiteEntry &CSE : CallSites) {
256 if (!CSE.LPad)
257 continue; // Ignore gaps.
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000258 NumEntries += CSE.LPad->SEHHandlers.size();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000259 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000260 Asm->OutStreamer->EmitIntValue(NumEntries, 4);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000261
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000262 // If there are no actions, we don't need to iterate again.
263 if (NumEntries == 0)
264 return;
265
Reid Kleckner0a57f652015-01-14 01:05:27 +0000266 // Emit the four-label records for each call site entry. The table has to be
267 // sorted in layout order, and the call sites should already be sorted.
268 for (const CallSiteEntry &CSE : CallSites) {
269 // Ignore gaps. Unlike the Itanium model, unwinding through a frame without
270 // an EH table entry will propagate the exception rather than terminating
271 // the program.
272 if (!CSE.LPad)
273 continue;
274 const LandingPadInfo *LPad = CSE.LPad;
275
276 // Compute the label range. We may reuse the function begin and end labels
277 // rather than forming new ones.
278 const MCExpr *Begin =
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000279 create32bitRef(CSE.BeginLabel ? CSE.BeginLabel : EHFuncBeginSym);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000280 const MCExpr *End;
281 if (CSE.EndLabel) {
282 // The interval is half-open, so we have to add one to include the return
283 // address of the last invoke in the range.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000284 End = MCBinaryExpr::createAdd(create32bitRef(CSE.EndLabel),
285 MCConstantExpr::create(1, Asm->OutContext),
Reid Kleckner0a57f652015-01-14 01:05:27 +0000286 Asm->OutContext);
287 } else {
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000288 End = create32bitRef(EHFuncEndSym);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000289 }
290
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000291 // Emit an entry for each action.
292 for (SEHHandler Handler : LPad->SEHHandlers) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000293 Asm->OutStreamer->EmitValue(Begin, 4);
294 Asm->OutStreamer->EmitValue(End, 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000295
296 // Emit the filter or finally function pointer, if present. Otherwise,
297 // emit '1' to indicate a catch-all.
298 const Function *F = Handler.FilterOrFinally;
299 if (F)
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000300 Asm->OutStreamer->EmitValue(create32bitRef(Asm->getSymbol(F)), 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000301 else
Lang Hames9ff69c82015-04-24 19:11:51 +0000302 Asm->OutStreamer->EmitIntValue(1, 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000303
304 // Emit the recovery address, if present. Otherwise, this must be a
305 // finally.
306 const BlockAddress *BA = Handler.RecoverBA;
307 if (BA)
Lang Hames9ff69c82015-04-24 19:11:51 +0000308 Asm->OutStreamer->EmitValue(
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000309 create32bitRef(Asm->GetBlockAddressSymbol(BA)), 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000310 else
Lang Hames9ff69c82015-04-24 19:11:51 +0000311 Asm->OutStreamer->EmitIntValue(0, 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000312 }
Reid Kleckner0a57f652015-01-14 01:05:27 +0000313 }
314}
David Majnemercde33032015-03-30 22:58:10 +0000315
Reid Kleckner94b704c2015-09-09 21:10:03 +0000316/// Retreive the MCSymbol for a GlobalValue or MachineBasicBlock. GlobalValues
317/// are used in the old WinEH scheme, and they will be removed eventually.
318static MCSymbol *getMCSymbolForMBBOrGV(AsmPrinter *Asm, ValueOrMBB Handler) {
319 if (Handler.is<MachineBasicBlock *>())
320 return Handler.get<MachineBasicBlock *>()->getSymbol();
321 else
322 return Asm->getSymbol(cast<GlobalValue>(Handler.get<const Value *>()));
323}
324
Reid Kleckner60b640b2015-05-28 22:47:01 +0000325void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
David Majnemercde33032015-03-30 22:58:10 +0000326 const Function *F = MF->getFunction();
327 const Function *ParentF = MMI->getWinEHParent(F);
Lang Hames9ff69c82015-04-24 19:11:51 +0000328 auto &OS = *Asm->OutStreamer;
David Majnemera225a192015-03-31 22:35:44 +0000329 WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(ParentF);
David Majnemercde33032015-03-30 22:58:10 +0000330
331 StringRef ParentLinkageName =
332 GlobalValue::getRealLinkageName(ParentF->getName());
333
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000334 MCSymbol *FuncInfoXData = nullptr;
335 if (shouldEmitPersonality) {
336 FuncInfoXData = Asm->OutContext.getOrCreateSymbol(
337 Twine("$cppxdata$", ParentLinkageName));
338 OS.EmitValue(create32bitRef(FuncInfoXData), 4);
339
340 extendIP2StateTable(MF, ParentF, FuncInfo);
341
Reid Kleckner0e288232015-08-27 23:27:47 +0000342 if (!MMI->hasEHFunclets()) {
343 // Defer emission until we've visited the parent function and all the
344 // catch handlers. Cleanups don't contribute to the ip2state table, so
345 // don't count them.
346 if (ParentF != F && !FuncInfo.CatchHandlerMaxState.count(F))
347 return;
348 ++FuncInfo.NumIPToStateFuncsVisited;
349 if (FuncInfo.NumIPToStateFuncsVisited !=
350 FuncInfo.CatchHandlerMaxState.size())
351 return;
352 }
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000353 } else {
354 FuncInfoXData = Asm->OutContext.getOrCreateLSDASymbol(ParentLinkageName);
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000355 emitEHRegistrationOffsetLabel(FuncInfo, ParentLinkageName);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000356 }
357
358 MCSymbol *UnwindMapXData = nullptr;
359 MCSymbol *TryBlockMapXData = nullptr;
360 MCSymbol *IPToStateXData = nullptr;
361 if (!FuncInfo.UnwindMap.empty())
362 UnwindMapXData = Asm->OutContext.getOrCreateSymbol(
363 Twine("$stateUnwindMap$", ParentLinkageName));
364 if (!FuncInfo.TryBlockMap.empty())
365 TryBlockMapXData = Asm->OutContext.getOrCreateSymbol(
366 Twine("$tryMap$", ParentLinkageName));
367 if (!FuncInfo.IPToStateList.empty())
368 IPToStateXData = Asm->OutContext.getOrCreateSymbol(
369 Twine("$ip2state$", ParentLinkageName));
370
371 // FuncInfo {
372 // uint32_t MagicNumber
373 // int32_t MaxState;
374 // UnwindMapEntry *UnwindMap;
375 // uint32_t NumTryBlocks;
376 // TryBlockMapEntry *TryBlockMap;
377 // uint32_t IPMapEntries; // always 0 for x86
378 // IPToStateMapEntry *IPToStateMap; // always 0 for x86
379 // uint32_t UnwindHelp; // non-x86 only
380 // ESTypeList *ESTypeList;
381 // int32_t EHFlags;
382 // }
383 // EHFlags & 1 -> Synchronous exceptions only, no async exceptions.
384 // EHFlags & 2 -> ???
385 // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue.
Reid Kleckner85a24502015-07-10 00:08:49 +0000386 OS.EmitValueToAlignment(4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000387 OS.EmitLabel(FuncInfoXData);
388 OS.EmitIntValue(0x19930522, 4); // MagicNumber
389 OS.EmitIntValue(FuncInfo.UnwindMap.size(), 4); // MaxState
390 OS.EmitValue(create32bitRef(UnwindMapXData), 4); // UnwindMap
391 OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4); // NumTryBlocks
392 OS.EmitValue(create32bitRef(TryBlockMapXData), 4); // TryBlockMap
393 OS.EmitIntValue(FuncInfo.IPToStateList.size(), 4); // IPMapEntries
394 OS.EmitValue(create32bitRef(IPToStateXData), 4); // IPToStateMap
395 if (Asm->MAI->usesWindowsCFI())
396 OS.EmitIntValue(FuncInfo.UnwindHelpFrameOffset, 4); // UnwindHelp
397 OS.EmitIntValue(0, 4); // ESTypeList
398 OS.EmitIntValue(1, 4); // EHFlags
399
400 // UnwindMapEntry {
401 // int32_t ToState;
402 // void (*Action)();
403 // };
404 if (UnwindMapXData) {
405 OS.EmitLabel(UnwindMapXData);
406 for (const WinEHUnwindMapEntry &UME : FuncInfo.UnwindMap) {
407 OS.EmitIntValue(UME.ToState, 4); // ToState
408 OS.EmitValue(create32bitRef(UME.Cleanup), 4); // Action
409 }
410 }
411
412 // TryBlockMap {
413 // int32_t TryLow;
414 // int32_t TryHigh;
415 // int32_t CatchHigh;
416 // int32_t NumCatches;
417 // HandlerType *HandlerArray;
418 // };
419 if (TryBlockMapXData) {
420 OS.EmitLabel(TryBlockMapXData);
421 SmallVector<MCSymbol *, 1> HandlerMaps;
422 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
423 WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
424 MCSymbol *HandlerMapXData = nullptr;
425
426 if (!TBME.HandlerArray.empty())
427 HandlerMapXData =
428 Asm->OutContext.getOrCreateSymbol(Twine("$handlerMap$")
429 .concat(Twine(I))
430 .concat("$")
431 .concat(ParentLinkageName));
432
433 HandlerMaps.push_back(HandlerMapXData);
434
Reid Kleckner0e288232015-08-27 23:27:47 +0000435 int CatchHigh = TBME.CatchHigh;
436 if (CatchHigh == -1) {
437 for (WinEHHandlerType &HT : TBME.HandlerArray)
Reid Kleckner94b704c2015-09-09 21:10:03 +0000438 CatchHigh =
439 std::max(CatchHigh, FuncInfo.CatchHandlerMaxState[cast<Function>(
440 HT.Handler.get<const Value *>())]);
Reid Kleckner0e288232015-08-27 23:27:47 +0000441 }
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000442
443 assert(TBME.TryLow <= TBME.TryHigh);
444 OS.EmitIntValue(TBME.TryLow, 4); // TryLow
445 OS.EmitIntValue(TBME.TryHigh, 4); // TryHigh
446 OS.EmitIntValue(CatchHigh, 4); // CatchHigh
447 OS.EmitIntValue(TBME.HandlerArray.size(), 4); // NumCatches
448 OS.EmitValue(create32bitRef(HandlerMapXData), 4); // HandlerArray
449 }
450
451 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
452 WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
453 MCSymbol *HandlerMapXData = HandlerMaps[I];
454 if (!HandlerMapXData)
455 continue;
456 // HandlerType {
457 // int32_t Adjectives;
458 // TypeDescriptor *Type;
459 // int32_t CatchObjOffset;
460 // void (*Handler)();
461 // int32_t ParentFrameOffset; // x64 only
462 // };
463 OS.EmitLabel(HandlerMapXData);
464 for (const WinEHHandlerType &HT : TBME.HandlerArray) {
465 // Get the frame escape label with the offset of the catch object. If
466 // the index is -1, then there is no catch object, and we should emit an
467 // offset of zero, indicating that no copy will occur.
468 const MCExpr *FrameAllocOffsetRef = nullptr;
469 if (HT.CatchObjRecoverIdx >= 0) {
470 MCSymbol *FrameAllocOffset =
471 Asm->OutContext.getOrCreateFrameAllocSymbol(
472 GlobalValue::getRealLinkageName(ParentF->getName()),
473 HT.CatchObjRecoverIdx);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000474 FrameAllocOffsetRef = MCSymbolRefExpr::create(
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000475 FrameAllocOffset, MCSymbolRefExpr::VK_None, Asm->OutContext);
476 } else {
Jim Grosbach13760bd2015-05-30 01:25:56 +0000477 FrameAllocOffsetRef = MCConstantExpr::create(0, Asm->OutContext);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000478 }
479
Reid Kleckner94b704c2015-09-09 21:10:03 +0000480 MCSymbol *HandlerSym = getMCSymbolForMBBOrGV(Asm, HT.Handler);
481
482 OS.EmitIntValue(HT.Adjectives, 4); // Adjectives
483 OS.EmitValue(create32bitRef(HT.TypeDescriptor), 4); // Type
484 OS.EmitValue(FrameAllocOffsetRef, 4); // CatchObjOffset
485 OS.EmitValue(create32bitRef(HandlerSym), 4); // Handler
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000486
487 if (shouldEmitPersonality) {
Reid Klecknerdf129512015-09-08 22:44:41 +0000488 if (FuncInfo.CatchHandlerParentFrameObjOffset.empty()) {
489 // With the new IR, this is always 16 + 8 + getMaxCallFrameSize().
490 // Keep this in sync with X86FrameLowering::emitPrologue.
491 int ParentFrameOffset =
492 16 + 8 + MF->getFrameInfo()->getMaxCallFrameSize();
493 OS.EmitIntValue(ParentFrameOffset, 4); // ParentFrameOffset
494 } else {
495 MCSymbol *ParentFrameOffset =
496 Asm->OutContext.getOrCreateParentFrameOffsetSymbol(
Reid Kleckner94b704c2015-09-09 21:10:03 +0000497 GlobalValue::getRealLinkageName(
498 HT.Handler.get<const Value *>()->getName()));
Reid Klecknerdf129512015-09-08 22:44:41 +0000499 const MCSymbolRefExpr *ParentFrameOffsetRef =
500 MCSymbolRefExpr::create(ParentFrameOffset, Asm->OutContext);
501 OS.EmitValue(ParentFrameOffsetRef, 4); // ParentFrameOffset
502 }
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000503 }
504 }
505 }
506 }
507
508 // IPToStateMapEntry {
509 // void *IP;
510 // int32_t State;
511 // };
512 if (IPToStateXData) {
513 OS.EmitLabel(IPToStateXData);
514 for (auto &IPStatePair : FuncInfo.IPToStateList) {
515 OS.EmitValue(create32bitRef(IPStatePair.first), 4); // IP
516 OS.EmitIntValue(IPStatePair.second, 4); // State
517 }
518 }
519}
520
521void WinException::extendIP2StateTable(const MachineFunction *MF,
522 const Function *ParentF,
523 WinEHFuncInfo &FuncInfo) {
524 const Function *F = MF->getFunction();
David Majnemercde33032015-03-30 22:58:10 +0000525
526 // The Itanium LSDA table sorts similar landing pads together to simplify the
527 // actions table, but we don't need that.
528 SmallVector<const LandingPadInfo *, 64> LandingPads;
529 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
530 LandingPads.reserve(PadInfos.size());
531 for (const auto &LP : PadInfos)
532 LandingPads.push_back(&LP);
533
534 RangeMapType PadMap;
535 computePadMap(LandingPads, PadMap);
536
537 // The end label of the previous invoke or nounwind try-range.
538 MCSymbol *LastLabel = Asm->getFunctionBegin();
539
540 // Whether there is a potentially throwing instruction (currently this means
541 // an ordinary call) between the end of the previous try-range and now.
542 bool SawPotentiallyThrowing = false;
543
David Majnemercde33032015-03-30 22:58:10 +0000544 int LastEHState = -2;
545
546 // The parent function and the catch handlers contribute to the 'ip2state'
547 // table.
Andrew Kaylor762a6be2015-05-11 19:41:19 +0000548
549 // Include ip2state entries for the beginning of the main function and
550 // for catch handler functions.
551 if (F == ParentF) {
552 FuncInfo.IPToStateList.push_back(std::make_pair(LastLabel, -1));
553 LastEHState = -1;
554 } else if (FuncInfo.HandlerBaseState.count(F)) {
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000555 FuncInfo.IPToStateList.push_back(
556 std::make_pair(LastLabel, FuncInfo.HandlerBaseState[F]));
Andrew Kaylor762a6be2015-05-11 19:41:19 +0000557 LastEHState = FuncInfo.HandlerBaseState[F];
558 }
David Majnemercde33032015-03-30 22:58:10 +0000559 for (const auto &MBB : *MF) {
560 for (const auto &MI : MBB) {
561 if (!MI.isEHLabel()) {
562 if (MI.isCall())
563 SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI);
564 continue;
565 }
566
567 // End of the previous try-range?
568 MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol();
569 if (BeginLabel == LastLabel)
570 SawPotentiallyThrowing = false;
571
572 // Beginning of a new try-range?
573 RangeMapType::const_iterator L = PadMap.find(BeginLabel);
574 if (L == PadMap.end())
575 // Nope, it was just some random label.
576 continue;
577
578 const PadRange &P = L->second;
579 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
580 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
581 "Inconsistent landing pad map!");
582
Andrew Kaylor762a6be2015-05-11 19:41:19 +0000583 // FIXME: Should this be using FuncInfo.HandlerBaseState?
584 if (SawPotentiallyThrowing && LastEHState != -1) {
David Majnemercde33032015-03-30 22:58:10 +0000585 FuncInfo.IPToStateList.push_back(std::make_pair(LastLabel, -1));
586 SawPotentiallyThrowing = false;
587 LastEHState = -1;
588 }
589
590 if (LandingPad->WinEHState != LastEHState)
591 FuncInfo.IPToStateList.push_back(
592 std::make_pair(BeginLabel, LandingPad->WinEHState));
593 LastEHState = LandingPad->WinEHState;
594 LastLabel = LandingPad->EndLabels[P.RangeIndex];
595 }
596 }
David Majnemercde33032015-03-30 22:58:10 +0000597}
Reid Klecknerf12c0302015-06-09 21:42:19 +0000598
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000599void WinException::emitEHRegistrationOffsetLabel(const WinEHFuncInfo &FuncInfo,
600 StringRef FLinkageName) {
601 // Outlined helpers called by the EH runtime need to know the offset of the EH
602 // registration in order to recover the parent frame pointer. Now that we know
603 // we've code generated the parent, we can emit the label assignment that
604 // those helpers use to get the offset of the registration node.
605 assert(FuncInfo.EHRegNodeEscapeIndex != INT_MAX &&
Reid Kleckner60381792015-07-07 22:25:32 +0000606 "no EH reg node localescape index");
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000607 MCSymbol *ParentFrameOffset =
608 Asm->OutContext.getOrCreateParentFrameOffsetSymbol(FLinkageName);
609 MCSymbol *RegistrationOffsetSym = Asm->OutContext.getOrCreateFrameAllocSymbol(
610 FLinkageName, FuncInfo.EHRegNodeEscapeIndex);
611 const MCExpr *RegistrationOffsetSymRef =
612 MCSymbolRefExpr::create(RegistrationOffsetSym, Asm->OutContext);
613 Asm->OutStreamer->EmitAssignment(ParentFrameOffset, RegistrationOffsetSymRef);
614}
615
Reid Klecknerf12c0302015-06-09 21:42:19 +0000616/// Emit the language-specific data that _except_handler3 and 4 expect. This is
617/// functionally equivalent to the __C_specific_handler table, except it is
618/// indexed by state number instead of IP.
619void WinException::emitExceptHandlerTable(const MachineFunction *MF) {
Reid Klecknera9d62532015-06-11 22:32:23 +0000620 MCStreamer &OS = *Asm->OutStreamer;
Reid Klecknera9d62532015-06-11 22:32:23 +0000621 const Function *F = MF->getFunction();
Reid Klecknera9d62532015-06-11 22:32:23 +0000622 StringRef FLinkageName = GlobalValue::getRealLinkageName(F->getName());
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000623
624 WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(F);
625 emitEHRegistrationOffsetLabel(FuncInfo, FLinkageName);
Reid Klecknerf12c0302015-06-09 21:42:19 +0000626
627 // Emit the __ehtable label that we use for llvm.x86.seh.lsda.
Reid Klecknerf12c0302015-06-09 21:42:19 +0000628 MCSymbol *LSDALabel = Asm->OutContext.getOrCreateLSDASymbol(FLinkageName);
Reid Kleckner85a24502015-07-10 00:08:49 +0000629 OS.EmitValueToAlignment(4);
Reid Klecknerf12c0302015-06-09 21:42:19 +0000630 OS.EmitLabel(LSDALabel);
631
Reid Kleckner9a1a9192015-07-13 20:41:46 +0000632 const Function *Per =
633 dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
Reid Klecknerf12c0302015-06-09 21:42:19 +0000634 StringRef PerName = Per->getName();
635 int BaseState = -1;
636 if (PerName == "_except_handler4") {
637 // The LSDA for _except_handler4 starts with this struct, followed by the
638 // scope table:
639 //
640 // struct EH4ScopeTable {
641 // int32_t GSCookieOffset;
642 // int32_t GSCookieXOROffset;
643 // int32_t EHCookieOffset;
644 // int32_t EHCookieXOROffset;
645 // ScopeTableEntry ScopeRecord[];
646 // };
647 //
648 // Only the EHCookieOffset field appears to vary, and it appears to be the
649 // offset from the final saved SP value to the retaddr.
650 OS.EmitIntValue(-2, 4);
651 OS.EmitIntValue(0, 4);
652 // FIXME: Calculate.
653 OS.EmitIntValue(9999, 4);
654 OS.EmitIntValue(0, 4);
655 BaseState = -2;
656 }
657
Reid Kleckner94b704c2015-09-09 21:10:03 +0000658 if (!FuncInfo.SEHUnwindMap.empty()) {
659 for (SEHUnwindMapEntry &UME : FuncInfo.SEHUnwindMap) {
660 MCSymbol *ExceptOrFinally =
661 UME.Handler.get<MachineBasicBlock *>()->getSymbol();
662 OS.EmitIntValue(UME.ToState, 4); // ToState
663 OS.EmitValue(create32bitRef(UME.Filter), 4); // Filter
664 OS.EmitValue(create32bitRef(ExceptOrFinally), 4); // Except/Finally
665 }
666 return;
667 }
668 // FIXME: The following code is for the old landingpad-based SEH
669 // implementation. Remove it when possible.
670
Reid Klecknerf12c0302015-06-09 21:42:19 +0000671 // Build a list of pointers to LandingPadInfos and then sort by WinEHState.
672 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
673 SmallVector<const LandingPadInfo *, 4> LPads;
674 LPads.reserve((PadInfos.size()));
675 for (const LandingPadInfo &LPInfo : PadInfos)
676 LPads.push_back(&LPInfo);
677 std::sort(LPads.begin(), LPads.end(),
678 [](const LandingPadInfo *L, const LandingPadInfo *R) {
679 return L->WinEHState < R->WinEHState;
680 });
681
682 // For each action in each lpad, emit one of these:
683 // struct ScopeTableEntry {
684 // int32_t EnclosingLevel;
Reid Kleckner81d1cc02015-06-11 23:37:18 +0000685 // int32_t (__cdecl *Filter)();
686 // void *HandlerOrFinally;
Reid Klecknerf12c0302015-06-09 21:42:19 +0000687 // };
688 //
689 // The "outermost" action will use BaseState as its enclosing level. Each
690 // other action will refer to the previous state as its enclosing level.
691 int CurState = 0;
692 for (const LandingPadInfo *LPInfo : LPads) {
693 int EnclosingLevel = BaseState;
Reid Kleckner7912d9b2015-06-10 00:04:53 +0000694 assert(CurState + int(LPInfo->SEHHandlers.size()) - 1 ==
695 LPInfo->WinEHState &&
Reid Klecknerf12c0302015-06-09 21:42:19 +0000696 "gaps in the SEH scope table");
Reid Kleckner81d1cc02015-06-11 23:37:18 +0000697 for (auto I = LPInfo->SEHHandlers.rbegin(), E = LPInfo->SEHHandlers.rend();
698 I != E; ++I) {
699 const SEHHandler &Handler = *I;
Reid Klecknerf12c0302015-06-09 21:42:19 +0000700 const BlockAddress *BA = Handler.RecoverBA;
Reid Kleckner81d1cc02015-06-11 23:37:18 +0000701 const Function *F = Handler.FilterOrFinally;
702 assert(F && "cannot catch all in 32-bit SEH without filter function");
703 const MCExpr *FilterOrNull =
704 create32bitRef(BA ? Asm->getSymbol(F) : nullptr);
705 const MCExpr *ExceptOrFinally = create32bitRef(
706 BA ? Asm->GetBlockAddressSymbol(BA) : Asm->getSymbol(F));
Reid Klecknerf12c0302015-06-09 21:42:19 +0000707
708 OS.EmitIntValue(EnclosingLevel, 4);
Reid Kleckner81d1cc02015-06-11 23:37:18 +0000709 OS.EmitValue(FilterOrNull, 4);
710 OS.EmitValue(ExceptOrFinally, 4);
Reid Klecknerf12c0302015-06-09 21:42:19 +0000711
712 // The next state unwinds to this state.
713 EnclosingLevel = CurState;
714 CurState++;
715 }
716 }
717}