blob: b89ca011648975486dd3ec4845519179708c6ec1 [file] [log] [blame]
Charles Davis91ed7992011-05-27 23:47:32 +00001//===-- CodeGen/AsmPrinter/Win64Exception.cpp - Dwarf Exception Impl ------===//
2//
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
Saleem Abdulrasool0fba7b52014-09-01 23:48:34 +000014#include "Win64Exception.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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/Dwarf.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/FormattedStream.h"
Charles Davis91ed7992011-05-27 23:47:32 +000035#include "llvm/Target/TargetFrameLowering.h"
36#include "llvm/Target/TargetLoweringObjectFile.h"
Charles Davis91ed7992011-05-27 23:47:32 +000037#include "llvm/Target/TargetOptions.h"
38#include "llvm/Target/TargetRegisterInfo.h"
Charles Davis91ed7992011-05-27 23:47:32 +000039using namespace llvm;
40
41Win64Exception::Win64Exception(AsmPrinter *A)
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000042 : EHStreamer(A), shouldEmitPersonality(false), shouldEmitLSDA(false),
43 shouldEmitMoves(false) {}
Charles Davis91ed7992011-05-27 23:47:32 +000044
45Win64Exception::~Win64Exception() {}
46
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000047/// endModule - Emit all exception information that should come after the
Charles Davis91ed7992011-05-27 23:47:32 +000048/// content.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000049void Win64Exception::endModule() {
Charles Davis91ed7992011-05-27 23:47:32 +000050}
51
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000052void Win64Exception::beginFunction(const MachineFunction *MF) {
Charles Davis5638b9f2011-05-28 04:21:04 +000053 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
54
55 // If any landing pads survive, we need an EH table.
56 bool hasLandingPads = !MMI->getLandingPads().empty();
57
58 shouldEmitMoves = Asm->needsSEHMoves();
59
60 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
61 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Reid Klecknere80a0a72015-01-14 22:47:54 +000062 const Function *Per = MF->getMMI().getPersonality();
Charles Davis5638b9f2011-05-28 04:21:04 +000063
64 shouldEmitPersonality = hasLandingPads &&
65 PerEncoding != dwarf::DW_EH_PE_omit && Per;
66
67 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
68 shouldEmitLSDA = shouldEmitPersonality &&
69 LSDAEncoding != dwarf::DW_EH_PE_omit;
70
David Majnemera225a192015-03-31 22:35:44 +000071
72 // If this was an outlined handler, we need to define the label corresponding
73 // to the offset of the parent frame relative to the stack pointer after the
74 // prologue.
75 const Function *F = MF->getFunction();
76 const Function *ParentF = MMI->getWinEHParent(F);
77 if (F != ParentF) {
78 WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(ParentF);
79 auto I = FuncInfo.CatchHandlerParentFrameObjOffset.find(F);
80 if (I != FuncInfo.CatchHandlerParentFrameObjOffset.end()) {
81 MCSymbol *HandlerTypeParentFrameOffset =
82 Asm->OutContext.getOrCreateParentFrameOffsetSymbol(
83 GlobalValue::getRealLinkageName(F->getName()));
84
85 // Emit a symbol assignment.
Lang Hames9ff69c82015-04-24 19:11:51 +000086 Asm->OutStreamer->EmitAssignment(
David Majnemera225a192015-03-31 22:35:44 +000087 HandlerTypeParentFrameOffset,
88 MCConstantExpr::Create(I->second, Asm->OutContext));
89 }
90 }
91
Charles Davis5638b9f2011-05-28 04:21:04 +000092 if (!shouldEmitPersonality && !shouldEmitMoves)
93 return;
94
Lang Hames9ff69c82015-04-24 19:11:51 +000095 Asm->OutStreamer->EmitWinCFIStartProc(Asm->CurrentFnSym);
Charles Davisb0257242011-05-29 04:28:35 +000096
97 if (!shouldEmitPersonality)
98 return;
99
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000100 const MCSymbol *PersHandlerSym =
101 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
Lang Hames9ff69c82015-04-24 19:11:51 +0000102 Asm->OutStreamer->EmitWinEHHandler(PersHandlerSym, true, true);
Charles Davis91ed7992011-05-27 23:47:32 +0000103}
104
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000105/// endFunction - Gather and emit post-function exception information.
Charles Davis91ed7992011-05-27 23:47:32 +0000106///
David Majnemercde33032015-03-30 22:58:10 +0000107void Win64Exception::endFunction(const MachineFunction *MF) {
Charles Davis5638b9f2011-05-28 04:21:04 +0000108 if (!shouldEmitPersonality && !shouldEmitMoves)
109 return;
110
Reid Kleckner3e9fadf2015-04-15 18:48:15 +0000111 EHPersonality Per = MMI->getPersonalityType();
112
113 // Get rid of any dead landing pads if we're not using a Windows EH scheme. In
114 // Windows EH schemes, the landing pad is not actually reachable. It only
115 // exists so that we can emit the right table data.
116 if (!isMSVCEHPersonality(Per))
117 MMI->TidyLandingPads();
Charles Davisa5752262011-05-30 00:13:34 +0000118
119 if (shouldEmitPersonality) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000120 Asm->OutStreamer->PushSection();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000121
122 // Emit an UNWIND_INFO struct describing the prologue.
Lang Hames9ff69c82015-04-24 19:11:51 +0000123 Asm->OutStreamer->EmitWinEHHandlerData();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000124
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000125 // Emit the tables appropriate to the personality function in use. If we
126 // don't recognize the personality, assume it uses an Itanium-style LSDA.
Reid Kleckner2d5fb682015-02-14 00:21:02 +0000127 if (Per == EHPersonality::MSVC_Win64SEH)
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000128 emitCSpecificHandlerTable();
David Majnemercde33032015-03-30 22:58:10 +0000129 else if (Per == EHPersonality::MSVC_CXX)
130 emitCXXFrameHandler3Table(MF);
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000131 else
Reid Kleckner0a57f652015-01-14 01:05:27 +0000132 emitExceptionTable();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000133
Lang Hames9ff69c82015-04-24 19:11:51 +0000134 Asm->OutStreamer->PopSection();
Charles Davisa5752262011-05-30 00:13:34 +0000135 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000136 Asm->OutStreamer->EmitWinCFIEndProc();
Charles Davis91ed7992011-05-27 23:47:32 +0000137}
Reid Kleckner0a57f652015-01-14 01:05:27 +0000138
David Majnemercde33032015-03-30 22:58:10 +0000139const MCExpr *Win64Exception::createImageRel32(const MCSymbol *Value) {
140 if (!Value)
141 return MCConstantExpr::Create(0, Asm->OutContext);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000142 return MCSymbolRefExpr::Create(Value, MCSymbolRefExpr::VK_COFF_IMGREL32,
143 Asm->OutContext);
144}
145
David Majnemercde33032015-03-30 22:58:10 +0000146const MCExpr *Win64Exception::createImageRel32(const GlobalValue *GV) {
147 if (!GV)
148 return MCConstantExpr::Create(0, Asm->OutContext);
149 return createImageRel32(Asm->getSymbol(GV));
150}
151
Reid Kleckner0a57f652015-01-14 01:05:27 +0000152/// Emit the language-specific data that __C_specific_handler expects. This
153/// handler lives in the x64 Microsoft C runtime and allows catching or cleaning
154/// up after faults with __try, __except, and __finally. The typeinfo values
155/// are not really RTTI data, but pointers to filter functions that return an
156/// integer (1, 0, or -1) indicating how to handle the exception. For __finally
157/// blocks and other cleanups, the landing pad label is zero, and the filter
158/// function is actually a cleanup handler with the same prototype. A catch-all
159/// entry is modeled with a null filter function field and a non-zero landing
160/// pad label.
161///
162/// Possible filter function return values:
163/// EXCEPTION_EXECUTE_HANDLER (1):
164/// Jump to the landing pad label after cleanups.
165/// EXCEPTION_CONTINUE_SEARCH (0):
166/// Continue searching this table or continue unwinding.
167/// EXCEPTION_CONTINUE_EXECUTION (-1):
168/// Resume execution at the trapping PC.
169///
170/// Inferred table structure:
171/// struct Table {
172/// int NumEntries;
173/// struct Entry {
174/// imagerel32 LabelStart;
175/// imagerel32 LabelEnd;
Reid Klecknerf690f502015-01-22 02:27:44 +0000176/// imagerel32 FilterOrFinally; // One means catch-all.
Reid Kleckner0a57f652015-01-14 01:05:27 +0000177/// imagerel32 LabelLPad; // Zero means __finally.
178/// } Entries[NumEntries];
179/// };
180void Win64Exception::emitCSpecificHandlerTable() {
181 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
182
183 // Simplifying assumptions for first implementation:
184 // - Cleanups are not implemented.
185 // - Filters are not implemented.
186
187 // The Itanium LSDA table sorts similar landing pads together to simplify the
188 // actions table, but we don't need that.
189 SmallVector<const LandingPadInfo *, 64> LandingPads;
190 LandingPads.reserve(PadInfos.size());
191 for (const auto &LP : PadInfos)
192 LandingPads.push_back(&LP);
193
194 // Compute label ranges for call sites as we would for the Itanium LSDA, but
195 // use an all zero action table because we aren't using these actions.
196 SmallVector<unsigned, 64> FirstActions;
197 FirstActions.resize(LandingPads.size());
198 SmallVector<CallSiteEntry, 64> CallSites;
199 computeCallSiteTable(CallSites, LandingPads, FirstActions);
200
Rafael Espindola629cdba2015-02-27 18:18:39 +0000201 MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin();
202 MCSymbol *EHFuncEndSym = Asm->getFunctionEnd();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000203
204 // Emit the number of table entries.
205 unsigned NumEntries = 0;
206 for (const CallSiteEntry &CSE : CallSites) {
207 if (!CSE.LPad)
208 continue; // Ignore gaps.
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000209 NumEntries += CSE.LPad->SEHHandlers.size();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000210 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000211 Asm->OutStreamer->EmitIntValue(NumEntries, 4);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000212
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000213 // If there are no actions, we don't need to iterate again.
214 if (NumEntries == 0)
215 return;
216
Reid Kleckner0a57f652015-01-14 01:05:27 +0000217 // Emit the four-label records for each call site entry. The table has to be
218 // sorted in layout order, and the call sites should already be sorted.
219 for (const CallSiteEntry &CSE : CallSites) {
220 // Ignore gaps. Unlike the Itanium model, unwinding through a frame without
221 // an EH table entry will propagate the exception rather than terminating
222 // the program.
223 if (!CSE.LPad)
224 continue;
225 const LandingPadInfo *LPad = CSE.LPad;
226
227 // Compute the label range. We may reuse the function begin and end labels
228 // rather than forming new ones.
229 const MCExpr *Begin =
230 createImageRel32(CSE.BeginLabel ? CSE.BeginLabel : EHFuncBeginSym);
231 const MCExpr *End;
232 if (CSE.EndLabel) {
233 // The interval is half-open, so we have to add one to include the return
234 // address of the last invoke in the range.
235 End = MCBinaryExpr::CreateAdd(createImageRel32(CSE.EndLabel),
236 MCConstantExpr::Create(1, Asm->OutContext),
237 Asm->OutContext);
238 } else {
239 End = createImageRel32(EHFuncEndSym);
240 }
241
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000242 // Emit an entry for each action.
243 for (SEHHandler Handler : LPad->SEHHandlers) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000244 Asm->OutStreamer->EmitValue(Begin, 4);
245 Asm->OutStreamer->EmitValue(End, 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000246
247 // Emit the filter or finally function pointer, if present. Otherwise,
248 // emit '1' to indicate a catch-all.
249 const Function *F = Handler.FilterOrFinally;
250 if (F)
Lang Hames9ff69c82015-04-24 19:11:51 +0000251 Asm->OutStreamer->EmitValue(createImageRel32(Asm->getSymbol(F)), 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000252 else
Lang Hames9ff69c82015-04-24 19:11:51 +0000253 Asm->OutStreamer->EmitIntValue(1, 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000254
255 // Emit the recovery address, if present. Otherwise, this must be a
256 // finally.
257 const BlockAddress *BA = Handler.RecoverBA;
258 if (BA)
Lang Hames9ff69c82015-04-24 19:11:51 +0000259 Asm->OutStreamer->EmitValue(
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000260 createImageRel32(Asm->GetBlockAddressSymbol(BA)), 4);
261 else
Lang Hames9ff69c82015-04-24 19:11:51 +0000262 Asm->OutStreamer->EmitIntValue(0, 4);
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000263 }
Reid Kleckner0a57f652015-01-14 01:05:27 +0000264 }
265}
David Majnemercde33032015-03-30 22:58:10 +0000266
267void Win64Exception::emitCXXFrameHandler3Table(const MachineFunction *MF) {
268 const Function *F = MF->getFunction();
269 const Function *ParentF = MMI->getWinEHParent(F);
Lang Hames9ff69c82015-04-24 19:11:51 +0000270 auto &OS = *Asm->OutStreamer;
David Majnemera225a192015-03-31 22:35:44 +0000271 WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(ParentF);
David Majnemercde33032015-03-30 22:58:10 +0000272
273 StringRef ParentLinkageName =
274 GlobalValue::getRealLinkageName(ParentF->getName());
275
276 MCSymbol *FuncInfoXData =
277 Asm->OutContext.GetOrCreateSymbol(Twine("$cppxdata$", ParentLinkageName));
278 OS.EmitValue(createImageRel32(FuncInfoXData), 4);
279
280 // The Itanium LSDA table sorts similar landing pads together to simplify the
281 // actions table, but we don't need that.
282 SmallVector<const LandingPadInfo *, 64> LandingPads;
283 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
284 LandingPads.reserve(PadInfos.size());
285 for (const auto &LP : PadInfos)
286 LandingPads.push_back(&LP);
287
288 RangeMapType PadMap;
289 computePadMap(LandingPads, PadMap);
290
291 // The end label of the previous invoke or nounwind try-range.
292 MCSymbol *LastLabel = Asm->getFunctionBegin();
293
294 // Whether there is a potentially throwing instruction (currently this means
295 // an ordinary call) between the end of the previous try-range and now.
296 bool SawPotentiallyThrowing = false;
297
David Majnemercde33032015-03-30 22:58:10 +0000298 int LastEHState = -2;
299
300 // The parent function and the catch handlers contribute to the 'ip2state'
301 // table.
302 for (const auto &MBB : *MF) {
303 for (const auto &MI : MBB) {
304 if (!MI.isEHLabel()) {
305 if (MI.isCall())
306 SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI);
307 continue;
308 }
309
310 // End of the previous try-range?
311 MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol();
312 if (BeginLabel == LastLabel)
313 SawPotentiallyThrowing = false;
314
315 // Beginning of a new try-range?
316 RangeMapType::const_iterator L = PadMap.find(BeginLabel);
317 if (L == PadMap.end())
318 // Nope, it was just some random label.
319 continue;
320
321 const PadRange &P = L->second;
322 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
323 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
324 "Inconsistent landing pad map!");
325
326 if (SawPotentiallyThrowing) {
327 FuncInfo.IPToStateList.push_back(std::make_pair(LastLabel, -1));
328 SawPotentiallyThrowing = false;
329 LastEHState = -1;
330 }
331
332 if (LandingPad->WinEHState != LastEHState)
333 FuncInfo.IPToStateList.push_back(
334 std::make_pair(BeginLabel, LandingPad->WinEHState));
335 LastEHState = LandingPad->WinEHState;
336 LastLabel = LandingPad->EndLabels[P.RangeIndex];
337 }
338 }
339
David Majnemer5c65f582015-04-10 04:56:17 +0000340 // Defer emission until we've visited the parent function and all the catch
Reid Klecknere5f13832015-04-14 21:42:36 +0000341 // handlers. Cleanups don't contribute to the ip2state table yet, so don't
342 // count them.
343 if (ParentF != F && !FuncInfo.CatchHandlerMaxState.count(F))
344 return;
345 ++FuncInfo.NumIPToStateFuncsVisited;
David Majnemer5c65f582015-04-10 04:56:17 +0000346 if (FuncInfo.NumIPToStateFuncsVisited != FuncInfo.CatchHandlerMaxState.size())
David Majnemercde33032015-03-30 22:58:10 +0000347 return;
348
349 MCSymbol *UnwindMapXData = nullptr;
350 MCSymbol *TryBlockMapXData = nullptr;
351 MCSymbol *IPToStateXData = nullptr;
352 if (!FuncInfo.UnwindMap.empty())
353 UnwindMapXData = Asm->OutContext.GetOrCreateSymbol(
354 Twine("$stateUnwindMap$", ParentLinkageName));
355 if (!FuncInfo.TryBlockMap.empty())
356 TryBlockMapXData = Asm->OutContext.GetOrCreateSymbol(
357 Twine("$tryMap$", ParentLinkageName));
358 if (!FuncInfo.IPToStateList.empty())
359 IPToStateXData = Asm->OutContext.GetOrCreateSymbol(
360 Twine("$ip2state$", ParentLinkageName));
361
362 // FuncInfo {
363 // uint32_t MagicNumber
364 // int32_t MaxState;
365 // UnwindMapEntry *UnwindMap;
366 // uint32_t NumTryBlocks;
367 // TryBlockMapEntry *TryBlockMap;
368 // uint32_t IPMapEntries;
369 // IPToStateMapEntry *IPToStateMap;
370 // uint32_t UnwindHelp; // (x64/ARM only)
371 // ESTypeList *ESTypeList;
372 // int32_t EHFlags;
373 // }
374 // EHFlags & 1 -> Synchronous exceptions only, no async exceptions.
375 // EHFlags & 2 -> ???
376 // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue.
377 OS.EmitLabel(FuncInfoXData);
378 OS.EmitIntValue(0x19930522, 4); // MagicNumber
379 OS.EmitIntValue(FuncInfo.UnwindMap.size(), 4); // MaxState
380 OS.EmitValue(createImageRel32(UnwindMapXData), 4); // UnwindMap
381 OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4); // NumTryBlocks
382 OS.EmitValue(createImageRel32(TryBlockMapXData), 4); // TryBlockMap
383 OS.EmitIntValue(FuncInfo.IPToStateList.size(), 4); // IPMapEntries
384 OS.EmitValue(createImageRel32(IPToStateXData), 4); // IPToStateMap
385 OS.EmitIntValue(FuncInfo.UnwindHelpFrameOffset, 4); // UnwindHelp
386 OS.EmitIntValue(0, 4); // ESTypeList
387 OS.EmitIntValue(1, 4); // EHFlags
388
389 // UnwindMapEntry {
390 // int32_t ToState;
391 // void (*Action)();
392 // };
393 if (UnwindMapXData) {
394 OS.EmitLabel(UnwindMapXData);
395 for (const WinEHUnwindMapEntry &UME : FuncInfo.UnwindMap) {
396 OS.EmitIntValue(UME.ToState, 4); // ToState
397 OS.EmitValue(createImageRel32(UME.Cleanup), 4); // Action
398 }
399 }
400
401 // TryBlockMap {
402 // int32_t TryLow;
403 // int32_t TryHigh;
404 // int32_t CatchHigh;
405 // int32_t NumCatches;
406 // HandlerType *HandlerArray;
407 // };
408 if (TryBlockMapXData) {
409 OS.EmitLabel(TryBlockMapXData);
410 SmallVector<MCSymbol *, 1> HandlerMaps;
411 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
412 WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
413 MCSymbol *HandlerMapXData = nullptr;
414
415 if (!TBME.HandlerArray.empty())
416 HandlerMapXData =
417 Asm->OutContext.GetOrCreateSymbol(Twine("$handlerMap$")
418 .concat(Twine(I))
419 .concat("$")
420 .concat(ParentLinkageName));
421
422 HandlerMaps.push_back(HandlerMapXData);
423
David Majnemer7f5e7142015-04-03 23:37:34 +0000424 int CatchHigh = -1;
425 for (WinEHHandlerType &HT : TBME.HandlerArray)
426 CatchHigh =
427 std::max(CatchHigh, FuncInfo.CatchHandlerMaxState[HT.Handler]);
428
David Majnemercde33032015-03-30 22:58:10 +0000429 assert(TBME.TryLow <= TBME.TryHigh);
David Majnemercde33032015-03-30 22:58:10 +0000430 OS.EmitIntValue(TBME.TryLow, 4); // TryLow
431 OS.EmitIntValue(TBME.TryHigh, 4); // TryHigh
David Majnemer7f5e7142015-04-03 23:37:34 +0000432 OS.EmitIntValue(CatchHigh, 4); // CatchHigh
David Majnemercde33032015-03-30 22:58:10 +0000433 OS.EmitIntValue(TBME.HandlerArray.size(), 4); // NumCatches
434 OS.EmitValue(createImageRel32(HandlerMapXData), 4); // HandlerArray
435 }
436
437 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
438 WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
439 MCSymbol *HandlerMapXData = HandlerMaps[I];
440 if (!HandlerMapXData)
441 continue;
442 // HandlerType {
443 // int32_t Adjectives;
444 // TypeDescriptor *Type;
445 // int32_t CatchObjOffset;
446 // void (*Handler)();
447 // int32_t ParentFrameOffset; // x64 only
448 // };
449 OS.EmitLabel(HandlerMapXData);
450 for (const WinEHHandlerType &HT : TBME.HandlerArray) {
David Majnemera225a192015-03-31 22:35:44 +0000451 MCSymbol *ParentFrameOffset =
452 Asm->OutContext.getOrCreateParentFrameOffsetSymbol(
453 GlobalValue::getRealLinkageName(HT.Handler->getName()));
454 const MCSymbolRefExpr *ParentFrameOffsetRef = MCSymbolRefExpr::Create(
455 ParentFrameOffset, MCSymbolRefExpr::VK_None, Asm->OutContext);
456
Reid Klecknerf1853c62015-04-07 19:46:38 +0000457 // Get the frame escape label with the offset of the catch object. If
458 // the index is -1, then there is no catch object, and we should emit an
459 // offset of zero, indicating that no copy will occur.
460 const MCExpr *FrameAllocOffsetRef = nullptr;
461 if (HT.CatchObjRecoverIdx >= 0) {
462 MCSymbol *FrameAllocOffset =
463 Asm->OutContext.getOrCreateFrameAllocSymbol(
Reid Kleckner6e3b5d42015-04-15 17:47:26 +0000464 GlobalValue::getRealLinkageName(ParentF->getName()),
Reid Klecknerf1853c62015-04-07 19:46:38 +0000465 HT.CatchObjRecoverIdx);
466 FrameAllocOffsetRef = MCSymbolRefExpr::Create(
467 FrameAllocOffset, MCSymbolRefExpr::VK_None, Asm->OutContext);
468 } else {
469 FrameAllocOffsetRef = MCConstantExpr::Create(0, Asm->OutContext);
470 }
David Majnemer69132a72015-04-03 22:49:05 +0000471
David Majnemercde33032015-03-30 22:58:10 +0000472 OS.EmitIntValue(HT.Adjectives, 4); // Adjectives
473 OS.EmitValue(createImageRel32(HT.TypeDescriptor), 4); // Type
David Majnemer69132a72015-04-03 22:49:05 +0000474 OS.EmitValue(FrameAllocOffsetRef, 4); // CatchObjOffset
David Majnemercde33032015-03-30 22:58:10 +0000475 OS.EmitValue(createImageRel32(HT.Handler), 4); // Handler
David Majnemera225a192015-03-31 22:35:44 +0000476 OS.EmitValue(ParentFrameOffsetRef, 4); // ParentFrameOffset
David Majnemercde33032015-03-30 22:58:10 +0000477 }
478 }
479 }
480
481 // IPToStateMapEntry {
482 // void *IP;
483 // int32_t State;
484 // };
485 if (IPToStateXData) {
486 OS.EmitLabel(IPToStateXData);
487 for (auto &IPStatePair : FuncInfo.IPToStateList) {
488 OS.EmitValue(createImageRel32(IPStatePair.first), 4); // IP
489 OS.EmitIntValue(IPStatePair.second, 4); // State
490 }
491 }
492}