blob: 0d422d83e2d372b439b1dcf676ca8a0954c8766e [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.
86 Asm->OutStreamer.EmitAssignment(
87 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
Saleem Abdulrasool7206a522014-06-29 01:52:01 +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);
Saleem Abdulrasool7206a522014-06-29 01:52:01 +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
Charles Davisa5752262011-05-30 00:13:34 +0000111 // Map all labels and get rid of any dead landing pads.
112 MMI->TidyLandingPads();
113
114 if (shouldEmitPersonality) {
Charles Davisa5752262011-05-30 00:13:34 +0000115 Asm->OutStreamer.PushSection();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000116
117 // Emit an UNWIND_INFO struct describing the prologue.
Saleem Abdulrasool7206a522014-06-29 01:52:01 +0000118 Asm->OutStreamer.EmitWinEHHandlerData();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000119
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000120 // Emit the tables appropriate to the personality function in use. If we
121 // don't recognize the personality, assume it uses an Itanium-style LSDA.
Reid Kleckner5cc15692015-01-23 18:49:01 +0000122 EHPersonality Per = MMI->getPersonalityType();
Reid Kleckner2d5fb682015-02-14 00:21:02 +0000123 if (Per == EHPersonality::MSVC_Win64SEH)
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000124 emitCSpecificHandlerTable();
David Majnemercde33032015-03-30 22:58:10 +0000125 else if (Per == EHPersonality::MSVC_CXX)
126 emitCXXFrameHandler3Table(MF);
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000127 else
Reid Kleckner0a57f652015-01-14 01:05:27 +0000128 emitExceptionTable();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000129
Charles Davisa5752262011-05-30 00:13:34 +0000130 Asm->OutStreamer.PopSection();
131 }
Saleem Abdulrasool7206a522014-06-29 01:52:01 +0000132 Asm->OutStreamer.EmitWinCFIEndProc();
Charles Davis91ed7992011-05-27 23:47:32 +0000133}
Reid Kleckner0a57f652015-01-14 01:05:27 +0000134
David Majnemercde33032015-03-30 22:58:10 +0000135const MCExpr *Win64Exception::createImageRel32(const MCSymbol *Value) {
136 if (!Value)
137 return MCConstantExpr::Create(0, Asm->OutContext);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000138 return MCSymbolRefExpr::Create(Value, MCSymbolRefExpr::VK_COFF_IMGREL32,
139 Asm->OutContext);
140}
141
David Majnemercde33032015-03-30 22:58:10 +0000142const MCExpr *Win64Exception::createImageRel32(const GlobalValue *GV) {
143 if (!GV)
144 return MCConstantExpr::Create(0, Asm->OutContext);
145 return createImageRel32(Asm->getSymbol(GV));
146}
147
Reid Kleckner0a57f652015-01-14 01:05:27 +0000148/// Emit the language-specific data that __C_specific_handler expects. This
149/// handler lives in the x64 Microsoft C runtime and allows catching or cleaning
150/// up after faults with __try, __except, and __finally. The typeinfo values
151/// are not really RTTI data, but pointers to filter functions that return an
152/// integer (1, 0, or -1) indicating how to handle the exception. For __finally
153/// blocks and other cleanups, the landing pad label is zero, and the filter
154/// function is actually a cleanup handler with the same prototype. A catch-all
155/// entry is modeled with a null filter function field and a non-zero landing
156/// pad label.
157///
158/// Possible filter function return values:
159/// EXCEPTION_EXECUTE_HANDLER (1):
160/// Jump to the landing pad label after cleanups.
161/// EXCEPTION_CONTINUE_SEARCH (0):
162/// Continue searching this table or continue unwinding.
163/// EXCEPTION_CONTINUE_EXECUTION (-1):
164/// Resume execution at the trapping PC.
165///
166/// Inferred table structure:
167/// struct Table {
168/// int NumEntries;
169/// struct Entry {
170/// imagerel32 LabelStart;
171/// imagerel32 LabelEnd;
Reid Klecknerf690f502015-01-22 02:27:44 +0000172/// imagerel32 FilterOrFinally; // One means catch-all.
Reid Kleckner0a57f652015-01-14 01:05:27 +0000173/// imagerel32 LabelLPad; // Zero means __finally.
174/// } Entries[NumEntries];
175/// };
176void Win64Exception::emitCSpecificHandlerTable() {
177 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
178
179 // Simplifying assumptions for first implementation:
180 // - Cleanups are not implemented.
181 // - Filters are not implemented.
182
183 // The Itanium LSDA table sorts similar landing pads together to simplify the
184 // actions table, but we don't need that.
185 SmallVector<const LandingPadInfo *, 64> LandingPads;
186 LandingPads.reserve(PadInfos.size());
187 for (const auto &LP : PadInfos)
188 LandingPads.push_back(&LP);
189
190 // Compute label ranges for call sites as we would for the Itanium LSDA, but
191 // use an all zero action table because we aren't using these actions.
192 SmallVector<unsigned, 64> FirstActions;
193 FirstActions.resize(LandingPads.size());
194 SmallVector<CallSiteEntry, 64> CallSites;
195 computeCallSiteTable(CallSites, LandingPads, FirstActions);
196
Rafael Espindola629cdba2015-02-27 18:18:39 +0000197 MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin();
198 MCSymbol *EHFuncEndSym = Asm->getFunctionEnd();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000199
200 // Emit the number of table entries.
201 unsigned NumEntries = 0;
202 for (const CallSiteEntry &CSE : CallSites) {
203 if (!CSE.LPad)
204 continue; // Ignore gaps.
205 for (int Selector : CSE.LPad->TypeIds) {
206 // Ignore C++ filter clauses in SEH.
207 // FIXME: Implement cleanup clauses.
208 if (isCatchEHSelector(Selector))
209 ++NumEntries;
210 }
211 }
212 Asm->OutStreamer.EmitIntValue(NumEntries, 4);
213
214 // Emit the four-label records for each call site entry. The table has to be
215 // sorted in layout order, and the call sites should already be sorted.
216 for (const CallSiteEntry &CSE : CallSites) {
217 // Ignore gaps. Unlike the Itanium model, unwinding through a frame without
218 // an EH table entry will propagate the exception rather than terminating
219 // the program.
220 if (!CSE.LPad)
221 continue;
222 const LandingPadInfo *LPad = CSE.LPad;
223
224 // Compute the label range. We may reuse the function begin and end labels
225 // rather than forming new ones.
226 const MCExpr *Begin =
227 createImageRel32(CSE.BeginLabel ? CSE.BeginLabel : EHFuncBeginSym);
228 const MCExpr *End;
229 if (CSE.EndLabel) {
230 // The interval is half-open, so we have to add one to include the return
231 // address of the last invoke in the range.
232 End = MCBinaryExpr::CreateAdd(createImageRel32(CSE.EndLabel),
233 MCConstantExpr::Create(1, Asm->OutContext),
234 Asm->OutContext);
235 } else {
236 End = createImageRel32(EHFuncEndSym);
237 }
238
239 // These aren't really type info globals, they are actually pointers to
240 // filter functions ordered by selector. The zero selector is used for
241 // cleanups, so slot zero corresponds to selector 1.
242 const std::vector<const GlobalValue *> &SelectorToFilter = MMI->getTypeInfos();
243
244 // Do a parallel iteration across typeids and clause labels, skipping filter
245 // clauses.
Reid Kleckner3d4638b2015-01-23 23:51:25 +0000246 size_t NextClauseLabel = 0;
Reid Kleckner0a57f652015-01-14 01:05:27 +0000247 for (size_t I = 0, E = LPad->TypeIds.size(); I < E; ++I) {
248 // AddLandingPadInfo stores the clauses in reverse, but there is a FIXME
249 // to change that.
250 int Selector = LPad->TypeIds[E - I - 1];
Reid Kleckner0a57f652015-01-14 01:05:27 +0000251
252 // Ignore C++ filter clauses in SEH.
253 // FIXME: Implement cleanup clauses.
254 if (!isCatchEHSelector(Selector))
255 continue;
256
257 Asm->OutStreamer.EmitValue(Begin, 4);
258 Asm->OutStreamer.EmitValue(End, 4);
259 if (isCatchEHSelector(Selector)) {
260 assert(unsigned(Selector - 1) < SelectorToFilter.size());
261 const GlobalValue *TI = SelectorToFilter[Selector - 1];
262 if (TI) // Emit the filter function pointer.
263 Asm->OutStreamer.EmitValue(createImageRel32(Asm->getSymbol(TI)), 4);
264 else // Otherwise, this is a "catch i8* null", or catch all.
Reid Klecknerf690f502015-01-22 02:27:44 +0000265 Asm->OutStreamer.EmitIntValue(1, 4);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000266 }
Reid Kleckner3d4638b2015-01-23 23:51:25 +0000267 MCSymbol *ClauseLabel = LPad->ClauseLabels[NextClauseLabel++];
Reid Kleckner0a57f652015-01-14 01:05:27 +0000268 Asm->OutStreamer.EmitValue(createImageRel32(ClauseLabel), 4);
269 }
270 }
271}
David Majnemercde33032015-03-30 22:58:10 +0000272
273void Win64Exception::emitCXXFrameHandler3Table(const MachineFunction *MF) {
274 const Function *F = MF->getFunction();
275 const Function *ParentF = MMI->getWinEHParent(F);
276 auto &OS = Asm->OutStreamer;
David Majnemera225a192015-03-31 22:35:44 +0000277 WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(ParentF);
David Majnemercde33032015-03-30 22:58:10 +0000278
279 StringRef ParentLinkageName =
280 GlobalValue::getRealLinkageName(ParentF->getName());
281
282 MCSymbol *FuncInfoXData =
283 Asm->OutContext.GetOrCreateSymbol(Twine("$cppxdata$", ParentLinkageName));
284 OS.EmitValue(createImageRel32(FuncInfoXData), 4);
285
286 // The Itanium LSDA table sorts similar landing pads together to simplify the
287 // actions table, but we don't need that.
288 SmallVector<const LandingPadInfo *, 64> LandingPads;
289 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
290 LandingPads.reserve(PadInfos.size());
291 for (const auto &LP : PadInfos)
292 LandingPads.push_back(&LP);
293
294 RangeMapType PadMap;
295 computePadMap(LandingPads, PadMap);
296
297 // The end label of the previous invoke or nounwind try-range.
298 MCSymbol *LastLabel = Asm->getFunctionBegin();
299
300 // Whether there is a potentially throwing instruction (currently this means
301 // an ordinary call) between the end of the previous try-range and now.
302 bool SawPotentiallyThrowing = false;
303
David Majnemercde33032015-03-30 22:58:10 +0000304 int LastEHState = -2;
305
306 // The parent function and the catch handlers contribute to the 'ip2state'
307 // table.
308 for (const auto &MBB : *MF) {
309 for (const auto &MI : MBB) {
310 if (!MI.isEHLabel()) {
311 if (MI.isCall())
312 SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI);
313 continue;
314 }
315
316 // End of the previous try-range?
317 MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol();
318 if (BeginLabel == LastLabel)
319 SawPotentiallyThrowing = false;
320
321 // Beginning of a new try-range?
322 RangeMapType::const_iterator L = PadMap.find(BeginLabel);
323 if (L == PadMap.end())
324 // Nope, it was just some random label.
325 continue;
326
327 const PadRange &P = L->second;
328 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
329 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
330 "Inconsistent landing pad map!");
331
332 if (SawPotentiallyThrowing) {
333 FuncInfo.IPToStateList.push_back(std::make_pair(LastLabel, -1));
334 SawPotentiallyThrowing = false;
335 LastEHState = -1;
336 }
337
338 if (LandingPad->WinEHState != LastEHState)
339 FuncInfo.IPToStateList.push_back(
340 std::make_pair(BeginLabel, LandingPad->WinEHState));
341 LastEHState = LandingPad->WinEHState;
342 LastLabel = LandingPad->EndLabels[P.RangeIndex];
343 }
344 }
345
David Majnemer5c65f582015-04-10 04:56:17 +0000346 // Defer emission until we've visited the parent function and all the catch
Reid Klecknere5f13832015-04-14 21:42:36 +0000347 // handlers. Cleanups don't contribute to the ip2state table yet, so don't
348 // count them.
349 if (ParentF != F && !FuncInfo.CatchHandlerMaxState.count(F))
350 return;
351 ++FuncInfo.NumIPToStateFuncsVisited;
David Majnemer5c65f582015-04-10 04:56:17 +0000352 if (FuncInfo.NumIPToStateFuncsVisited != FuncInfo.CatchHandlerMaxState.size())
David Majnemercde33032015-03-30 22:58:10 +0000353 return;
354
355 MCSymbol *UnwindMapXData = nullptr;
356 MCSymbol *TryBlockMapXData = nullptr;
357 MCSymbol *IPToStateXData = nullptr;
358 if (!FuncInfo.UnwindMap.empty())
359 UnwindMapXData = Asm->OutContext.GetOrCreateSymbol(
360 Twine("$stateUnwindMap$", ParentLinkageName));
361 if (!FuncInfo.TryBlockMap.empty())
362 TryBlockMapXData = Asm->OutContext.GetOrCreateSymbol(
363 Twine("$tryMap$", ParentLinkageName));
364 if (!FuncInfo.IPToStateList.empty())
365 IPToStateXData = Asm->OutContext.GetOrCreateSymbol(
366 Twine("$ip2state$", ParentLinkageName));
367
368 // FuncInfo {
369 // uint32_t MagicNumber
370 // int32_t MaxState;
371 // UnwindMapEntry *UnwindMap;
372 // uint32_t NumTryBlocks;
373 // TryBlockMapEntry *TryBlockMap;
374 // uint32_t IPMapEntries;
375 // IPToStateMapEntry *IPToStateMap;
376 // uint32_t UnwindHelp; // (x64/ARM only)
377 // ESTypeList *ESTypeList;
378 // int32_t EHFlags;
379 // }
380 // EHFlags & 1 -> Synchronous exceptions only, no async exceptions.
381 // EHFlags & 2 -> ???
382 // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue.
383 OS.EmitLabel(FuncInfoXData);
384 OS.EmitIntValue(0x19930522, 4); // MagicNumber
385 OS.EmitIntValue(FuncInfo.UnwindMap.size(), 4); // MaxState
386 OS.EmitValue(createImageRel32(UnwindMapXData), 4); // UnwindMap
387 OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4); // NumTryBlocks
388 OS.EmitValue(createImageRel32(TryBlockMapXData), 4); // TryBlockMap
389 OS.EmitIntValue(FuncInfo.IPToStateList.size(), 4); // IPMapEntries
390 OS.EmitValue(createImageRel32(IPToStateXData), 4); // IPToStateMap
391 OS.EmitIntValue(FuncInfo.UnwindHelpFrameOffset, 4); // UnwindHelp
392 OS.EmitIntValue(0, 4); // ESTypeList
393 OS.EmitIntValue(1, 4); // EHFlags
394
395 // UnwindMapEntry {
396 // int32_t ToState;
397 // void (*Action)();
398 // };
399 if (UnwindMapXData) {
400 OS.EmitLabel(UnwindMapXData);
401 for (const WinEHUnwindMapEntry &UME : FuncInfo.UnwindMap) {
402 OS.EmitIntValue(UME.ToState, 4); // ToState
403 OS.EmitValue(createImageRel32(UME.Cleanup), 4); // Action
404 }
405 }
406
407 // TryBlockMap {
408 // int32_t TryLow;
409 // int32_t TryHigh;
410 // int32_t CatchHigh;
411 // int32_t NumCatches;
412 // HandlerType *HandlerArray;
413 // };
414 if (TryBlockMapXData) {
415 OS.EmitLabel(TryBlockMapXData);
416 SmallVector<MCSymbol *, 1> HandlerMaps;
417 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
418 WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
419 MCSymbol *HandlerMapXData = nullptr;
420
421 if (!TBME.HandlerArray.empty())
422 HandlerMapXData =
423 Asm->OutContext.GetOrCreateSymbol(Twine("$handlerMap$")
424 .concat(Twine(I))
425 .concat("$")
426 .concat(ParentLinkageName));
427
428 HandlerMaps.push_back(HandlerMapXData);
429
David Majnemer7f5e7142015-04-03 23:37:34 +0000430 int CatchHigh = -1;
431 for (WinEHHandlerType &HT : TBME.HandlerArray)
432 CatchHigh =
433 std::max(CatchHigh, FuncInfo.CatchHandlerMaxState[HT.Handler]);
434
David Majnemercde33032015-03-30 22:58:10 +0000435 assert(TBME.TryLow <= TBME.TryHigh);
David Majnemer7f5e7142015-04-03 23:37:34 +0000436 assert(CatchHigh > TBME.TryHigh);
David Majnemercde33032015-03-30 22:58:10 +0000437 OS.EmitIntValue(TBME.TryLow, 4); // TryLow
438 OS.EmitIntValue(TBME.TryHigh, 4); // TryHigh
David Majnemer7f5e7142015-04-03 23:37:34 +0000439 OS.EmitIntValue(CatchHigh, 4); // CatchHigh
David Majnemercde33032015-03-30 22:58:10 +0000440 OS.EmitIntValue(TBME.HandlerArray.size(), 4); // NumCatches
441 OS.EmitValue(createImageRel32(HandlerMapXData), 4); // HandlerArray
442 }
443
444 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
445 WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
446 MCSymbol *HandlerMapXData = HandlerMaps[I];
447 if (!HandlerMapXData)
448 continue;
449 // HandlerType {
450 // int32_t Adjectives;
451 // TypeDescriptor *Type;
452 // int32_t CatchObjOffset;
453 // void (*Handler)();
454 // int32_t ParentFrameOffset; // x64 only
455 // };
456 OS.EmitLabel(HandlerMapXData);
457 for (const WinEHHandlerType &HT : TBME.HandlerArray) {
David Majnemera225a192015-03-31 22:35:44 +0000458 MCSymbol *ParentFrameOffset =
459 Asm->OutContext.getOrCreateParentFrameOffsetSymbol(
460 GlobalValue::getRealLinkageName(HT.Handler->getName()));
461 const MCSymbolRefExpr *ParentFrameOffsetRef = MCSymbolRefExpr::Create(
462 ParentFrameOffset, MCSymbolRefExpr::VK_None, Asm->OutContext);
463
Reid Klecknerf1853c62015-04-07 19:46:38 +0000464 // Get the frame escape label with the offset of the catch object. If
465 // the index is -1, then there is no catch object, and we should emit an
466 // offset of zero, indicating that no copy will occur.
467 const MCExpr *FrameAllocOffsetRef = nullptr;
468 if (HT.CatchObjRecoverIdx >= 0) {
469 MCSymbol *FrameAllocOffset =
470 Asm->OutContext.getOrCreateFrameAllocSymbol(
Reid Kleckner6e3b5d42015-04-15 17:47:26 +0000471 GlobalValue::getRealLinkageName(ParentF->getName()),
Reid Klecknerf1853c62015-04-07 19:46:38 +0000472 HT.CatchObjRecoverIdx);
473 FrameAllocOffsetRef = MCSymbolRefExpr::Create(
474 FrameAllocOffset, MCSymbolRefExpr::VK_None, Asm->OutContext);
475 } else {
476 FrameAllocOffsetRef = MCConstantExpr::Create(0, Asm->OutContext);
477 }
David Majnemer69132a72015-04-03 22:49:05 +0000478
David Majnemercde33032015-03-30 22:58:10 +0000479 OS.EmitIntValue(HT.Adjectives, 4); // Adjectives
480 OS.EmitValue(createImageRel32(HT.TypeDescriptor), 4); // Type
David Majnemer69132a72015-04-03 22:49:05 +0000481 OS.EmitValue(FrameAllocOffsetRef, 4); // CatchObjOffset
David Majnemercde33032015-03-30 22:58:10 +0000482 OS.EmitValue(createImageRel32(HT.Handler), 4); // Handler
David Majnemera225a192015-03-31 22:35:44 +0000483 OS.EmitValue(ParentFrameOffsetRef, 4); // ParentFrameOffset
David Majnemercde33032015-03-30 22:58:10 +0000484 }
485 }
486 }
487
488 // IPToStateMapEntry {
489 // void *IP;
490 // int32_t State;
491 // };
492 if (IPToStateXData) {
493 OS.EmitLabel(IPToStateXData);
494 for (auto &IPStatePair : FuncInfo.IPToStateList) {
495 OS.EmitValue(createImageRel32(IPStatePair.first), 4); // IP
496 OS.EmitIntValue(IPStatePair.second, 4); // State
497 }
498 }
499}