blob: ffa0ceb1a602387c761ce92b520e2045dc214b99 [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();
55 for (const Function &F : *M) {
56 if (F.hasFnAttribute("safeseh")) {
57 llvm::errs() << ".safeseh " << F.getName() << "\n";
58 OS.EmitCOFFSafeSEH(Asm->getSymbol(&F));
59 }
60 }
Charles Davis91ed7992011-05-27 23:47:32 +000061}
62
Reid Kleckner60b640b2015-05-28 22:47:01 +000063void WinException::beginFunction(const MachineFunction *MF) {
Charles Davis5638b9f2011-05-28 04:21:04 +000064 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
65
66 // If any landing pads survive, we need an EH table.
67 bool hasLandingPads = !MMI->getLandingPads().empty();
68
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000069 const Function *F = MF->getFunction();
70 const Function *ParentF = MMI->getWinEHParent(F);
71
Charles Davis5638b9f2011-05-28 04:21:04 +000072 shouldEmitMoves = Asm->needsSEHMoves();
73
74 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
75 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000076 const Function *Per = MMI->getPersonality();
Charles Davis5638b9f2011-05-28 04:21:04 +000077
78 shouldEmitPersonality = hasLandingPads &&
79 PerEncoding != dwarf::DW_EH_PE_omit && Per;
80
81 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
82 shouldEmitLSDA = shouldEmitPersonality &&
83 LSDAEncoding != dwarf::DW_EH_PE_omit;
84
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000085 // If we're not using CFI, we don't want the CFI or the personality. Emit the
86 // LSDA if this is the parent function.
87 if (!Asm->MAI->usesWindowsCFI()) {
88 shouldEmitLSDA = (hasLandingPads && F == ParentF);
89 shouldEmitPersonality = false;
90 return;
91 }
David Majnemera225a192015-03-31 22:35:44 +000092
93 // If this was an outlined handler, we need to define the label corresponding
94 // to the offset of the parent frame relative to the stack pointer after the
95 // prologue.
David Majnemera225a192015-03-31 22:35:44 +000096 if (F != ParentF) {
97 WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(ParentF);
98 auto I = FuncInfo.CatchHandlerParentFrameObjOffset.find(F);
99 if (I != FuncInfo.CatchHandlerParentFrameObjOffset.end()) {
100 MCSymbol *HandlerTypeParentFrameOffset =
101 Asm->OutContext.getOrCreateParentFrameOffsetSymbol(
102 GlobalValue::getRealLinkageName(F->getName()));
103
104 // Emit a symbol assignment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000105 Asm->OutStreamer->EmitAssignment(
David Majnemera225a192015-03-31 22:35:44 +0000106 HandlerTypeParentFrameOffset,
Jim Grosbach13760bd2015-05-30 01:25:56 +0000107 MCConstantExpr::create(I->second, Asm->OutContext));
David Majnemera225a192015-03-31 22:35:44 +0000108 }
109 }
110
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000111 if (shouldEmitMoves || shouldEmitPersonality)
112 Asm->OutStreamer->EmitWinCFIStartProc(Asm->CurrentFnSym);
Charles Davis5638b9f2011-05-28 04:21:04 +0000113
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000114 if (shouldEmitPersonality) {
115 const MCSymbol *PersHandlerSym =
116 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
117 Asm->OutStreamer->EmitWinEHHandler(PersHandlerSym, true, true);
118 }
Charles Davis91ed7992011-05-27 23:47:32 +0000119}
120
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000121/// endFunction - Gather and emit post-function exception information.
Charles Davis91ed7992011-05-27 23:47:32 +0000122///
Reid Kleckner60b640b2015-05-28 22:47:01 +0000123void WinException::endFunction(const MachineFunction *MF) {
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000124 if (!shouldEmitPersonality && !shouldEmitMoves && !shouldEmitLSDA)
Charles Davis5638b9f2011-05-28 04:21:04 +0000125 return;
126
Reid Kleckner3e9fadf2015-04-15 18:48:15 +0000127 EHPersonality Per = MMI->getPersonalityType();
128
129 // Get rid of any dead landing pads if we're not using a Windows EH scheme. In
130 // Windows EH schemes, the landing pad is not actually reachable. It only
131 // exists so that we can emit the right table data.
132 if (!isMSVCEHPersonality(Per))
133 MMI->TidyLandingPads();
Charles Davisa5752262011-05-30 00:13:34 +0000134
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000135 if (shouldEmitPersonality || shouldEmitLSDA) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000136 Asm->OutStreamer->PushSection();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000137
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000138 if (shouldEmitMoves || shouldEmitPersonality) {
139 // Emit an UNWIND_INFO struct describing the prologue.
140 Asm->OutStreamer->EmitWinEHHandlerData();
141 } else {
142 // Just switch sections to the right xdata section. This use of
143 // CurrentFnSym assumes that we only emit the LSDA when ending the parent
144 // function.
145 MCSection *XData = WinEH::UnwindEmitter::getXDataSection(
146 Asm->CurrentFnSym, Asm->OutContext);
147 Asm->OutStreamer->SwitchSection(XData);
148 }
Reid Kleckner0a57f652015-01-14 01:05:27 +0000149
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000150 // Emit the tables appropriate to the personality function in use. If we
151 // don't recognize the personality, assume it uses an Itanium-style LSDA.
Reid Kleckner2d5fb682015-02-14 00:21:02 +0000152 if (Per == EHPersonality::MSVC_Win64SEH)
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000153 emitCSpecificHandlerTable();
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000154 else if (Per == EHPersonality::MSVC_X86SEH)
Reid Klecknerf12c0302015-06-09 21:42:19 +0000155 emitExceptHandlerTable(MF);
David Majnemercde33032015-03-30 22:58:10 +0000156 else if (Per == EHPersonality::MSVC_CXX)
157 emitCXXFrameHandler3Table(MF);
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000158 else
Reid Kleckner0a57f652015-01-14 01:05:27 +0000159 emitExceptionTable();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000160
Lang Hames9ff69c82015-04-24 19:11:51 +0000161 Asm->OutStreamer->PopSection();
Charles Davisa5752262011-05-30 00:13:34 +0000162 }
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000163
164 if (shouldEmitMoves)
165 Asm->OutStreamer->EmitWinCFIEndProc();
Charles Davis91ed7992011-05-27 23:47:32 +0000166}
Reid Kleckner0a57f652015-01-14 01:05:27 +0000167
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000168const MCExpr *WinException::create32bitRef(const MCSymbol *Value) {
David Majnemercde33032015-03-30 22:58:10 +0000169 if (!Value)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000170 return MCConstantExpr::create(0, Asm->OutContext);
171 return MCSymbolRefExpr::create(Value, useImageRel32
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000172 ? MCSymbolRefExpr::VK_COFF_IMGREL32
173 : MCSymbolRefExpr::VK_None,
Reid Kleckner0a57f652015-01-14 01:05:27 +0000174 Asm->OutContext);
175}
176
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000177const MCExpr *WinException::create32bitRef(const GlobalValue *GV) {
David Majnemercde33032015-03-30 22:58:10 +0000178 if (!GV)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000179 return MCConstantExpr::create(0, Asm->OutContext);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000180 return create32bitRef(Asm->getSymbol(GV));
David Majnemercde33032015-03-30 22:58:10 +0000181}
182
Reid Kleckner0a57f652015-01-14 01:05:27 +0000183/// Emit the language-specific data that __C_specific_handler expects. This
184/// handler lives in the x64 Microsoft C runtime and allows catching or cleaning
185/// up after faults with __try, __except, and __finally. The typeinfo values
186/// are not really RTTI data, but pointers to filter functions that return an
187/// integer (1, 0, or -1) indicating how to handle the exception. For __finally
188/// blocks and other cleanups, the landing pad label is zero, and the filter
189/// function is actually a cleanup handler with the same prototype. A catch-all
190/// entry is modeled with a null filter function field and a non-zero landing
191/// pad label.
192///
193/// Possible filter function return values:
194/// EXCEPTION_EXECUTE_HANDLER (1):
195/// Jump to the landing pad label after cleanups.
196/// EXCEPTION_CONTINUE_SEARCH (0):
197/// Continue searching this table or continue unwinding.
198/// EXCEPTION_CONTINUE_EXECUTION (-1):
199/// Resume execution at the trapping PC.
200///
201/// Inferred table structure:
202/// struct Table {
203/// int NumEntries;
204/// struct Entry {
205/// imagerel32 LabelStart;
206/// imagerel32 LabelEnd;
Reid Klecknerf690f502015-01-22 02:27:44 +0000207/// imagerel32 FilterOrFinally; // One means catch-all.
Reid Kleckner0a57f652015-01-14 01:05:27 +0000208/// imagerel32 LabelLPad; // Zero means __finally.
209/// } Entries[NumEntries];
210/// };
Reid Kleckner60b640b2015-05-28 22:47:01 +0000211void WinException::emitCSpecificHandlerTable() {
Reid Kleckner0a57f652015-01-14 01:05:27 +0000212 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
213
214 // Simplifying assumptions for first implementation:
215 // - Cleanups are not implemented.
216 // - Filters are not implemented.
217
218 // The Itanium LSDA table sorts similar landing pads together to simplify the
219 // actions table, but we don't need that.
220 SmallVector<const LandingPadInfo *, 64> LandingPads;
221 LandingPads.reserve(PadInfos.size());
222 for (const auto &LP : PadInfos)
223 LandingPads.push_back(&LP);
224
225 // Compute label ranges for call sites as we would for the Itanium LSDA, but
226 // use an all zero action table because we aren't using these actions.
227 SmallVector<unsigned, 64> FirstActions;
228 FirstActions.resize(LandingPads.size());
229 SmallVector<CallSiteEntry, 64> CallSites;
230 computeCallSiteTable(CallSites, LandingPads, FirstActions);
231
Rafael Espindola629cdba2015-02-27 18:18:39 +0000232 MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin();
233 MCSymbol *EHFuncEndSym = Asm->getFunctionEnd();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000234
235 // Emit the number of table entries.
236 unsigned NumEntries = 0;
237 for (const CallSiteEntry &CSE : CallSites) {
238 if (!CSE.LPad)
239 continue; // Ignore gaps.
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000240 NumEntries += CSE.LPad->SEHHandlers.size();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000241 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000242 Asm->OutStreamer->EmitIntValue(NumEntries, 4);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000243
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000244 // If there are no actions, we don't need to iterate again.
245 if (NumEntries == 0)
246 return;
247
Reid Kleckner0a57f652015-01-14 01:05:27 +0000248 // Emit the four-label records for each call site entry. The table has to be
249 // sorted in layout order, and the call sites should already be sorted.
250 for (const CallSiteEntry &CSE : CallSites) {
251 // Ignore gaps. Unlike the Itanium model, unwinding through a frame without
252 // an EH table entry will propagate the exception rather than terminating
253 // the program.
254 if (!CSE.LPad)
255 continue;
256 const LandingPadInfo *LPad = CSE.LPad;
257
258 // Compute the label range. We may reuse the function begin and end labels
259 // rather than forming new ones.
260 const MCExpr *Begin =
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000261 create32bitRef(CSE.BeginLabel ? CSE.BeginLabel : EHFuncBeginSym);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000262 const MCExpr *End;
263 if (CSE.EndLabel) {
264 // The interval is half-open, so we have to add one to include the return
265 // address of the last invoke in the range.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000266 End = MCBinaryExpr::createAdd(create32bitRef(CSE.EndLabel),
267 MCConstantExpr::create(1, Asm->OutContext),
Reid Kleckner0a57f652015-01-14 01:05:27 +0000268 Asm->OutContext);
269 } else {
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000270 End = create32bitRef(EHFuncEndSym);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000271 }
272
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000273 // Emit an entry for each action.
274 for (SEHHandler Handler : LPad->SEHHandlers) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000275 Asm->OutStreamer->EmitValue(Begin, 4);
276 Asm->OutStreamer->EmitValue(End, 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000277
278 // Emit the filter or finally function pointer, if present. Otherwise,
279 // emit '1' to indicate a catch-all.
280 const Function *F = Handler.FilterOrFinally;
281 if (F)
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000282 Asm->OutStreamer->EmitValue(create32bitRef(Asm->getSymbol(F)), 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000283 else
Lang Hames9ff69c82015-04-24 19:11:51 +0000284 Asm->OutStreamer->EmitIntValue(1, 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000285
286 // Emit the recovery address, if present. Otherwise, this must be a
287 // finally.
288 const BlockAddress *BA = Handler.RecoverBA;
289 if (BA)
Lang Hames9ff69c82015-04-24 19:11:51 +0000290 Asm->OutStreamer->EmitValue(
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000291 create32bitRef(Asm->GetBlockAddressSymbol(BA)), 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000292 else
Lang Hames9ff69c82015-04-24 19:11:51 +0000293 Asm->OutStreamer->EmitIntValue(0, 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000294 }
Reid Kleckner0a57f652015-01-14 01:05:27 +0000295 }
296}
David Majnemercde33032015-03-30 22:58:10 +0000297
Reid Kleckner60b640b2015-05-28 22:47:01 +0000298void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
David Majnemercde33032015-03-30 22:58:10 +0000299 const Function *F = MF->getFunction();
300 const Function *ParentF = MMI->getWinEHParent(F);
Lang Hames9ff69c82015-04-24 19:11:51 +0000301 auto &OS = *Asm->OutStreamer;
David Majnemera225a192015-03-31 22:35:44 +0000302 WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(ParentF);
David Majnemercde33032015-03-30 22:58:10 +0000303
304 StringRef ParentLinkageName =
305 GlobalValue::getRealLinkageName(ParentF->getName());
306
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000307 MCSymbol *FuncInfoXData = nullptr;
308 if (shouldEmitPersonality) {
309 FuncInfoXData = Asm->OutContext.getOrCreateSymbol(
310 Twine("$cppxdata$", ParentLinkageName));
311 OS.EmitValue(create32bitRef(FuncInfoXData), 4);
312
313 extendIP2StateTable(MF, ParentF, FuncInfo);
314
315 // Defer emission until we've visited the parent function and all the catch
316 // handlers. Cleanups don't contribute to the ip2state table, so don't count
317 // them.
318 if (ParentF != F && !FuncInfo.CatchHandlerMaxState.count(F))
319 return;
320 ++FuncInfo.NumIPToStateFuncsVisited;
321 if (FuncInfo.NumIPToStateFuncsVisited != FuncInfo.CatchHandlerMaxState.size())
322 return;
323 } else {
324 FuncInfoXData = Asm->OutContext.getOrCreateLSDASymbol(ParentLinkageName);
325 }
326
327 MCSymbol *UnwindMapXData = nullptr;
328 MCSymbol *TryBlockMapXData = nullptr;
329 MCSymbol *IPToStateXData = nullptr;
330 if (!FuncInfo.UnwindMap.empty())
331 UnwindMapXData = Asm->OutContext.getOrCreateSymbol(
332 Twine("$stateUnwindMap$", ParentLinkageName));
333 if (!FuncInfo.TryBlockMap.empty())
334 TryBlockMapXData = Asm->OutContext.getOrCreateSymbol(
335 Twine("$tryMap$", ParentLinkageName));
336 if (!FuncInfo.IPToStateList.empty())
337 IPToStateXData = Asm->OutContext.getOrCreateSymbol(
338 Twine("$ip2state$", ParentLinkageName));
339
340 // FuncInfo {
341 // uint32_t MagicNumber
342 // int32_t MaxState;
343 // UnwindMapEntry *UnwindMap;
344 // uint32_t NumTryBlocks;
345 // TryBlockMapEntry *TryBlockMap;
346 // uint32_t IPMapEntries; // always 0 for x86
347 // IPToStateMapEntry *IPToStateMap; // always 0 for x86
348 // uint32_t UnwindHelp; // non-x86 only
349 // ESTypeList *ESTypeList;
350 // int32_t EHFlags;
351 // }
352 // EHFlags & 1 -> Synchronous exceptions only, no async exceptions.
353 // EHFlags & 2 -> ???
354 // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue.
355 OS.EmitLabel(FuncInfoXData);
356 OS.EmitIntValue(0x19930522, 4); // MagicNumber
357 OS.EmitIntValue(FuncInfo.UnwindMap.size(), 4); // MaxState
358 OS.EmitValue(create32bitRef(UnwindMapXData), 4); // UnwindMap
359 OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4); // NumTryBlocks
360 OS.EmitValue(create32bitRef(TryBlockMapXData), 4); // TryBlockMap
361 OS.EmitIntValue(FuncInfo.IPToStateList.size(), 4); // IPMapEntries
362 OS.EmitValue(create32bitRef(IPToStateXData), 4); // IPToStateMap
363 if (Asm->MAI->usesWindowsCFI())
364 OS.EmitIntValue(FuncInfo.UnwindHelpFrameOffset, 4); // UnwindHelp
365 OS.EmitIntValue(0, 4); // ESTypeList
366 OS.EmitIntValue(1, 4); // EHFlags
367
368 // UnwindMapEntry {
369 // int32_t ToState;
370 // void (*Action)();
371 // };
372 if (UnwindMapXData) {
373 OS.EmitLabel(UnwindMapXData);
374 for (const WinEHUnwindMapEntry &UME : FuncInfo.UnwindMap) {
375 OS.EmitIntValue(UME.ToState, 4); // ToState
376 OS.EmitValue(create32bitRef(UME.Cleanup), 4); // Action
377 }
378 }
379
380 // TryBlockMap {
381 // int32_t TryLow;
382 // int32_t TryHigh;
383 // int32_t CatchHigh;
384 // int32_t NumCatches;
385 // HandlerType *HandlerArray;
386 // };
387 if (TryBlockMapXData) {
388 OS.EmitLabel(TryBlockMapXData);
389 SmallVector<MCSymbol *, 1> HandlerMaps;
390 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
391 WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
392 MCSymbol *HandlerMapXData = nullptr;
393
394 if (!TBME.HandlerArray.empty())
395 HandlerMapXData =
396 Asm->OutContext.getOrCreateSymbol(Twine("$handlerMap$")
397 .concat(Twine(I))
398 .concat("$")
399 .concat(ParentLinkageName));
400
401 HandlerMaps.push_back(HandlerMapXData);
402
403 int CatchHigh = -1;
404 for (WinEHHandlerType &HT : TBME.HandlerArray)
405 CatchHigh =
406 std::max(CatchHigh, FuncInfo.CatchHandlerMaxState[HT.Handler]);
407
408 assert(TBME.TryLow <= TBME.TryHigh);
409 OS.EmitIntValue(TBME.TryLow, 4); // TryLow
410 OS.EmitIntValue(TBME.TryHigh, 4); // TryHigh
411 OS.EmitIntValue(CatchHigh, 4); // CatchHigh
412 OS.EmitIntValue(TBME.HandlerArray.size(), 4); // NumCatches
413 OS.EmitValue(create32bitRef(HandlerMapXData), 4); // HandlerArray
414 }
415
416 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
417 WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
418 MCSymbol *HandlerMapXData = HandlerMaps[I];
419 if (!HandlerMapXData)
420 continue;
421 // HandlerType {
422 // int32_t Adjectives;
423 // TypeDescriptor *Type;
424 // int32_t CatchObjOffset;
425 // void (*Handler)();
426 // int32_t ParentFrameOffset; // x64 only
427 // };
428 OS.EmitLabel(HandlerMapXData);
429 for (const WinEHHandlerType &HT : TBME.HandlerArray) {
430 // Get the frame escape label with the offset of the catch object. If
431 // the index is -1, then there is no catch object, and we should emit an
432 // offset of zero, indicating that no copy will occur.
433 const MCExpr *FrameAllocOffsetRef = nullptr;
434 if (HT.CatchObjRecoverIdx >= 0) {
435 MCSymbol *FrameAllocOffset =
436 Asm->OutContext.getOrCreateFrameAllocSymbol(
437 GlobalValue::getRealLinkageName(ParentF->getName()),
438 HT.CatchObjRecoverIdx);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000439 FrameAllocOffsetRef = MCSymbolRefExpr::create(
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000440 FrameAllocOffset, MCSymbolRefExpr::VK_None, Asm->OutContext);
441 } else {
Jim Grosbach13760bd2015-05-30 01:25:56 +0000442 FrameAllocOffsetRef = MCConstantExpr::create(0, Asm->OutContext);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000443 }
444
445 OS.EmitIntValue(HT.Adjectives, 4); // Adjectives
446 OS.EmitValue(create32bitRef(HT.TypeDescriptor), 4); // Type
447 OS.EmitValue(FrameAllocOffsetRef, 4); // CatchObjOffset
448 OS.EmitValue(create32bitRef(HT.Handler), 4); // Handler
449
450 if (shouldEmitPersonality) {
451 MCSymbol *ParentFrameOffset =
452 Asm->OutContext.getOrCreateParentFrameOffsetSymbol(
453 GlobalValue::getRealLinkageName(HT.Handler->getName()));
Jim Grosbach13760bd2015-05-30 01:25:56 +0000454 const MCSymbolRefExpr *ParentFrameOffsetRef = MCSymbolRefExpr::create(
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000455 ParentFrameOffset, MCSymbolRefExpr::VK_None, Asm->OutContext);
456 OS.EmitValue(ParentFrameOffsetRef, 4); // ParentFrameOffset
457 }
458 }
459 }
460 }
461
462 // IPToStateMapEntry {
463 // void *IP;
464 // int32_t State;
465 // };
466 if (IPToStateXData) {
467 OS.EmitLabel(IPToStateXData);
468 for (auto &IPStatePair : FuncInfo.IPToStateList) {
469 OS.EmitValue(create32bitRef(IPStatePair.first), 4); // IP
470 OS.EmitIntValue(IPStatePair.second, 4); // State
471 }
472 }
473}
474
475void WinException::extendIP2StateTable(const MachineFunction *MF,
476 const Function *ParentF,
477 WinEHFuncInfo &FuncInfo) {
478 const Function *F = MF->getFunction();
David Majnemercde33032015-03-30 22:58:10 +0000479
480 // The Itanium LSDA table sorts similar landing pads together to simplify the
481 // actions table, but we don't need that.
482 SmallVector<const LandingPadInfo *, 64> LandingPads;
483 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
484 LandingPads.reserve(PadInfos.size());
485 for (const auto &LP : PadInfos)
486 LandingPads.push_back(&LP);
487
488 RangeMapType PadMap;
489 computePadMap(LandingPads, PadMap);
490
491 // The end label of the previous invoke or nounwind try-range.
492 MCSymbol *LastLabel = Asm->getFunctionBegin();
493
494 // Whether there is a potentially throwing instruction (currently this means
495 // an ordinary call) between the end of the previous try-range and now.
496 bool SawPotentiallyThrowing = false;
497
David Majnemercde33032015-03-30 22:58:10 +0000498 int LastEHState = -2;
499
500 // The parent function and the catch handlers contribute to the 'ip2state'
501 // table.
Andrew Kaylor762a6be2015-05-11 19:41:19 +0000502
503 // Include ip2state entries for the beginning of the main function and
504 // for catch handler functions.
505 if (F == ParentF) {
506 FuncInfo.IPToStateList.push_back(std::make_pair(LastLabel, -1));
507 LastEHState = -1;
508 } else if (FuncInfo.HandlerBaseState.count(F)) {
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000509 FuncInfo.IPToStateList.push_back(
510 std::make_pair(LastLabel, FuncInfo.HandlerBaseState[F]));
Andrew Kaylor762a6be2015-05-11 19:41:19 +0000511 LastEHState = FuncInfo.HandlerBaseState[F];
512 }
David Majnemercde33032015-03-30 22:58:10 +0000513 for (const auto &MBB : *MF) {
514 for (const auto &MI : MBB) {
515 if (!MI.isEHLabel()) {
516 if (MI.isCall())
517 SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI);
518 continue;
519 }
520
521 // End of the previous try-range?
522 MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol();
523 if (BeginLabel == LastLabel)
524 SawPotentiallyThrowing = false;
525
526 // Beginning of a new try-range?
527 RangeMapType::const_iterator L = PadMap.find(BeginLabel);
528 if (L == PadMap.end())
529 // Nope, it was just some random label.
530 continue;
531
532 const PadRange &P = L->second;
533 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
534 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
535 "Inconsistent landing pad map!");
536
Andrew Kaylor762a6be2015-05-11 19:41:19 +0000537 // FIXME: Should this be using FuncInfo.HandlerBaseState?
538 if (SawPotentiallyThrowing && LastEHState != -1) {
David Majnemercde33032015-03-30 22:58:10 +0000539 FuncInfo.IPToStateList.push_back(std::make_pair(LastLabel, -1));
540 SawPotentiallyThrowing = false;
541 LastEHState = -1;
542 }
543
544 if (LandingPad->WinEHState != LastEHState)
545 FuncInfo.IPToStateList.push_back(
546 std::make_pair(BeginLabel, LandingPad->WinEHState));
547 LastEHState = LandingPad->WinEHState;
548 LastLabel = LandingPad->EndLabels[P.RangeIndex];
549 }
550 }
David Majnemercde33032015-03-30 22:58:10 +0000551}
Reid Klecknerf12c0302015-06-09 21:42:19 +0000552
553/// Emit the language-specific data that _except_handler3 and 4 expect. This is
554/// functionally equivalent to the __C_specific_handler table, except it is
555/// indexed by state number instead of IP.
556void WinException::emitExceptHandlerTable(const MachineFunction *MF) {
557 auto &OS = *Asm->OutStreamer;
558
559 // Emit the __ehtable label that we use for llvm.x86.seh.lsda.
560 const Function *F = MF->getFunction();
561 StringRef FLinkageName = GlobalValue::getRealLinkageName(F->getName());
562 MCSymbol *LSDALabel = Asm->OutContext.getOrCreateLSDASymbol(FLinkageName);
563 OS.EmitLabel(LSDALabel);
564
565 const Function *Per = MMI->getPersonality();
566 StringRef PerName = Per->getName();
567 int BaseState = -1;
568 if (PerName == "_except_handler4") {
569 // The LSDA for _except_handler4 starts with this struct, followed by the
570 // scope table:
571 //
572 // struct EH4ScopeTable {
573 // int32_t GSCookieOffset;
574 // int32_t GSCookieXOROffset;
575 // int32_t EHCookieOffset;
576 // int32_t EHCookieXOROffset;
577 // ScopeTableEntry ScopeRecord[];
578 // };
579 //
580 // Only the EHCookieOffset field appears to vary, and it appears to be the
581 // offset from the final saved SP value to the retaddr.
582 OS.EmitIntValue(-2, 4);
583 OS.EmitIntValue(0, 4);
584 // FIXME: Calculate.
585 OS.EmitIntValue(9999, 4);
586 OS.EmitIntValue(0, 4);
587 BaseState = -2;
588 }
589
590 // Build a list of pointers to LandingPadInfos and then sort by WinEHState.
591 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
592 SmallVector<const LandingPadInfo *, 4> LPads;
593 LPads.reserve((PadInfos.size()));
594 for (const LandingPadInfo &LPInfo : PadInfos)
595 LPads.push_back(&LPInfo);
596 std::sort(LPads.begin(), LPads.end(),
597 [](const LandingPadInfo *L, const LandingPadInfo *R) {
598 return L->WinEHState < R->WinEHState;
599 });
600
601 // For each action in each lpad, emit one of these:
602 // struct ScopeTableEntry {
603 // int32_t EnclosingLevel;
604 // int32_t (__cdecl *FilterOrFinally)();
605 // void *HandlerLabel;
606 // };
607 //
608 // The "outermost" action will use BaseState as its enclosing level. Each
609 // other action will refer to the previous state as its enclosing level.
610 int CurState = 0;
611 for (const LandingPadInfo *LPInfo : LPads) {
612 int EnclosingLevel = BaseState;
Reid Kleckner7912d9b2015-06-10 00:04:53 +0000613 assert(CurState + int(LPInfo->SEHHandlers.size()) - 1 ==
614 LPInfo->WinEHState &&
Reid Klecknerf12c0302015-06-09 21:42:19 +0000615 "gaps in the SEH scope table");
616 for (const SEHHandler &Handler : LPInfo->SEHHandlers) {
617 // Emit the filter or finally function pointer, if present. Otherwise,
618 // emit '1' to indicate a catch-all.
619 const MCExpr *FilterOrFinally;
620 if (const Function *F = Handler.FilterOrFinally)
621 FilterOrFinally = create32bitRef(Asm->getSymbol(F));
622 else
623 FilterOrFinally = MCConstantExpr::create(1, Asm->OutContext);
624
625 // Compute the recovery address, which is a block address or null.
626 const BlockAddress *BA = Handler.RecoverBA;
627 const MCExpr *RecoverBBOrNull =
628 create32bitRef(BA ? Asm->GetBlockAddressSymbol(BA) : nullptr);
629
630 OS.EmitIntValue(EnclosingLevel, 4);
631 OS.EmitValue(FilterOrFinally, 4);
632 OS.EmitValue(RecoverBBOrNull, 4);
633
634 // The next state unwinds to this state.
635 EnclosingLevel = CurState;
636 CurState++;
637 }
638 }
639}