2015-05-29 06:47:01 +08:00
|
|
|
//===-- CodeGen/AsmPrinter/WinException.cpp - Dwarf Exception Impl ------===//
|
2011-05-28 07:47:32 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains support for writing Win64 exception info into asm files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-29 06:47:01 +08:00
|
|
|
#include "WinException.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2011-05-28 07:47:32 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2015-03-31 06:58:10 +08:00
|
|
|
#include "llvm/CodeGen/WinEHFuncInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-08 05:19:40 +08:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2011-05-28 07:47:32 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCSection.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2015-05-30 01:00:57 +08:00
|
|
|
#include "llvm/MC/MCWin64EH.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/Dwarf.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2011-05-28 07:47:32 +08:00
|
|
|
#include "llvm/Target/TargetFrameLowering.h"
|
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-05-30 01:00:57 +08:00
|
|
|
WinException::WinException(AsmPrinter *A) : EHStreamer(A) {
|
|
|
|
// MSVC's EH tables are always composed of 32-bit words. All known 64-bit
|
|
|
|
// platforms use an imagerel32 relocation to refer to symbols.
|
|
|
|
useImageRel32 = (A->getDataLayout().getPointerSizeInBits() == 64);
|
|
|
|
}
|
2011-05-28 07:47:32 +08:00
|
|
|
|
2015-05-29 06:47:01 +08:00
|
|
|
WinException::~WinException() {}
|
2011-05-28 07:47:32 +08:00
|
|
|
|
2013-11-26 21:34:55 +08:00
|
|
|
/// endModule - Emit all exception information that should come after the
|
2011-05-28 07:47:32 +08:00
|
|
|
/// content.
|
2015-05-29 06:47:01 +08:00
|
|
|
void WinException::endModule() {
|
2015-06-10 09:02:30 +08:00
|
|
|
auto &OS = *Asm->OutStreamer;
|
|
|
|
const Module *M = MMI->getModule();
|
2015-06-10 09:13:44 +08:00
|
|
|
for (const Function &F : *M)
|
|
|
|
if (F.hasFnAttribute("safeseh"))
|
2015-06-10 09:02:30 +08:00
|
|
|
OS.EmitCOFFSafeSEH(Asm->getSymbol(&F));
|
2011-05-28 07:47:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-29 06:47:01 +08:00
|
|
|
void WinException::beginFunction(const MachineFunction *MF) {
|
2011-05-28 12:21:04 +08:00
|
|
|
shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
|
|
|
|
|
|
|
|
// If any landing pads survive, we need an EH table.
|
|
|
|
bool hasLandingPads = !MMI->getLandingPads().empty();
|
|
|
|
|
2015-05-30 01:00:57 +08:00
|
|
|
const Function *F = MF->getFunction();
|
|
|
|
const Function *ParentF = MMI->getWinEHParent(F);
|
|
|
|
|
2011-05-28 12:21:04 +08:00
|
|
|
shouldEmitMoves = Asm->needsSEHMoves();
|
|
|
|
|
|
|
|
const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
|
|
|
|
unsigned PerEncoding = TLOF.getPersonalityEncoding();
|
2015-05-30 01:00:57 +08:00
|
|
|
const Function *Per = MMI->getPersonality();
|
2011-05-28 12:21:04 +08:00
|
|
|
|
|
|
|
shouldEmitPersonality = hasLandingPads &&
|
|
|
|
PerEncoding != dwarf::DW_EH_PE_omit && Per;
|
|
|
|
|
|
|
|
unsigned LSDAEncoding = TLOF.getLSDAEncoding();
|
|
|
|
shouldEmitLSDA = shouldEmitPersonality &&
|
|
|
|
LSDAEncoding != dwarf::DW_EH_PE_omit;
|
|
|
|
|
2015-05-30 01:00:57 +08:00
|
|
|
// If we're not using CFI, we don't want the CFI or the personality. Emit the
|
|
|
|
// LSDA if this is the parent function.
|
|
|
|
if (!Asm->MAI->usesWindowsCFI()) {
|
|
|
|
shouldEmitLSDA = (hasLandingPads && F == ParentF);
|
|
|
|
shouldEmitPersonality = false;
|
|
|
|
return;
|
|
|
|
}
|
2015-04-01 06:35:44 +08:00
|
|
|
|
|
|
|
// If this was an outlined handler, we need to define the label corresponding
|
|
|
|
// to the offset of the parent frame relative to the stack pointer after the
|
|
|
|
// prologue.
|
|
|
|
if (F != ParentF) {
|
|
|
|
WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(ParentF);
|
|
|
|
auto I = FuncInfo.CatchHandlerParentFrameObjOffset.find(F);
|
|
|
|
if (I != FuncInfo.CatchHandlerParentFrameObjOffset.end()) {
|
|
|
|
MCSymbol *HandlerTypeParentFrameOffset =
|
|
|
|
Asm->OutContext.getOrCreateParentFrameOffsetSymbol(
|
|
|
|
GlobalValue::getRealLinkageName(F->getName()));
|
|
|
|
|
|
|
|
// Emit a symbol assignment.
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitAssignment(
|
2015-04-01 06:35:44 +08:00
|
|
|
HandlerTypeParentFrameOffset,
|
2015-05-30 09:25:56 +08:00
|
|
|
MCConstantExpr::create(I->second, Asm->OutContext));
|
2015-04-01 06:35:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 01:00:57 +08:00
|
|
|
if (shouldEmitMoves || shouldEmitPersonality)
|
|
|
|
Asm->OutStreamer->EmitWinCFIStartProc(Asm->CurrentFnSym);
|
2011-05-28 12:21:04 +08:00
|
|
|
|
2015-05-30 01:00:57 +08:00
|
|
|
if (shouldEmitPersonality) {
|
|
|
|
const MCSymbol *PersHandlerSym =
|
|
|
|
TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
|
|
|
|
Asm->OutStreamer->EmitWinEHHandler(PersHandlerSym, true, true);
|
|
|
|
}
|
2011-05-28 07:47:32 +08:00
|
|
|
}
|
|
|
|
|
2013-11-26 21:34:55 +08:00
|
|
|
/// endFunction - Gather and emit post-function exception information.
|
2011-05-28 07:47:32 +08:00
|
|
|
///
|
2015-05-29 06:47:01 +08:00
|
|
|
void WinException::endFunction(const MachineFunction *MF) {
|
2015-05-30 01:00:57 +08:00
|
|
|
if (!shouldEmitPersonality && !shouldEmitMoves && !shouldEmitLSDA)
|
2011-05-28 12:21:04 +08:00
|
|
|
return;
|
|
|
|
|
2015-04-16 02:48:15 +08:00
|
|
|
EHPersonality Per = MMI->getPersonalityType();
|
|
|
|
|
|
|
|
// Get rid of any dead landing pads if we're not using a Windows EH scheme. In
|
|
|
|
// Windows EH schemes, the landing pad is not actually reachable. It only
|
|
|
|
// exists so that we can emit the right table data.
|
|
|
|
if (!isMSVCEHPersonality(Per))
|
|
|
|
MMI->TidyLandingPads();
|
2011-05-30 08:13:34 +08:00
|
|
|
|
2015-05-30 01:00:57 +08:00
|
|
|
if (shouldEmitPersonality || shouldEmitLSDA) {
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->PushSection();
|
2015-01-14 09:05:27 +08:00
|
|
|
|
2015-05-30 01:00:57 +08:00
|
|
|
if (shouldEmitMoves || shouldEmitPersonality) {
|
|
|
|
// Emit an UNWIND_INFO struct describing the prologue.
|
|
|
|
Asm->OutStreamer->EmitWinEHHandlerData();
|
|
|
|
} else {
|
|
|
|
// Just switch sections to the right xdata section. This use of
|
|
|
|
// CurrentFnSym assumes that we only emit the LSDA when ending the parent
|
|
|
|
// function.
|
|
|
|
MCSection *XData = WinEH::UnwindEmitter::getXDataSection(
|
|
|
|
Asm->CurrentFnSym, Asm->OutContext);
|
|
|
|
Asm->OutStreamer->SwitchSection(XData);
|
|
|
|
}
|
2015-01-14 09:05:27 +08:00
|
|
|
|
2015-01-15 02:50:10 +08:00
|
|
|
// Emit the tables appropriate to the personality function in use. If we
|
|
|
|
// don't recognize the personality, assume it uses an Itanium-style LSDA.
|
2015-02-14 08:21:02 +08:00
|
|
|
if (Per == EHPersonality::MSVC_Win64SEH)
|
2015-01-15 02:50:10 +08:00
|
|
|
emitCSpecificHandlerTable();
|
2015-05-30 01:00:57 +08:00
|
|
|
else if (Per == EHPersonality::MSVC_X86SEH)
|
2015-06-10 05:42:19 +08:00
|
|
|
emitExceptHandlerTable(MF);
|
2015-03-31 06:58:10 +08:00
|
|
|
else if (Per == EHPersonality::MSVC_CXX)
|
|
|
|
emitCXXFrameHandler3Table(MF);
|
2015-01-15 02:50:10 +08:00
|
|
|
else
|
2015-01-14 09:05:27 +08:00
|
|
|
emitExceptionTable();
|
|
|
|
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->PopSection();
|
2011-05-30 08:13:34 +08:00
|
|
|
}
|
2015-05-30 01:00:57 +08:00
|
|
|
|
|
|
|
if (shouldEmitMoves)
|
|
|
|
Asm->OutStreamer->EmitWinCFIEndProc();
|
2011-05-28 07:47:32 +08:00
|
|
|
}
|
2015-01-14 09:05:27 +08:00
|
|
|
|
2015-05-30 01:00:57 +08:00
|
|
|
const MCExpr *WinException::create32bitRef(const MCSymbol *Value) {
|
2015-03-31 06:58:10 +08:00
|
|
|
if (!Value)
|
2015-05-30 09:25:56 +08:00
|
|
|
return MCConstantExpr::create(0, Asm->OutContext);
|
|
|
|
return MCSymbolRefExpr::create(Value, useImageRel32
|
2015-05-30 01:00:57 +08:00
|
|
|
? MCSymbolRefExpr::VK_COFF_IMGREL32
|
|
|
|
: MCSymbolRefExpr::VK_None,
|
2015-01-14 09:05:27 +08:00
|
|
|
Asm->OutContext);
|
|
|
|
}
|
|
|
|
|
2015-05-30 01:00:57 +08:00
|
|
|
const MCExpr *WinException::create32bitRef(const GlobalValue *GV) {
|
2015-03-31 06:58:10 +08:00
|
|
|
if (!GV)
|
2015-05-30 09:25:56 +08:00
|
|
|
return MCConstantExpr::create(0, Asm->OutContext);
|
2015-05-30 01:00:57 +08:00
|
|
|
return create32bitRef(Asm->getSymbol(GV));
|
2015-03-31 06:58:10 +08:00
|
|
|
}
|
|
|
|
|
2015-01-14 09:05:27 +08:00
|
|
|
/// Emit the language-specific data that __C_specific_handler expects. This
|
|
|
|
/// handler lives in the x64 Microsoft C runtime and allows catching or cleaning
|
|
|
|
/// up after faults with __try, __except, and __finally. The typeinfo values
|
|
|
|
/// are not really RTTI data, but pointers to filter functions that return an
|
|
|
|
/// integer (1, 0, or -1) indicating how to handle the exception. For __finally
|
|
|
|
/// blocks and other cleanups, the landing pad label is zero, and the filter
|
|
|
|
/// function is actually a cleanup handler with the same prototype. A catch-all
|
|
|
|
/// entry is modeled with a null filter function field and a non-zero landing
|
|
|
|
/// pad label.
|
|
|
|
///
|
|
|
|
/// Possible filter function return values:
|
|
|
|
/// EXCEPTION_EXECUTE_HANDLER (1):
|
|
|
|
/// Jump to the landing pad label after cleanups.
|
|
|
|
/// EXCEPTION_CONTINUE_SEARCH (0):
|
|
|
|
/// Continue searching this table or continue unwinding.
|
|
|
|
/// EXCEPTION_CONTINUE_EXECUTION (-1):
|
|
|
|
/// Resume execution at the trapping PC.
|
|
|
|
///
|
|
|
|
/// Inferred table structure:
|
|
|
|
/// struct Table {
|
|
|
|
/// int NumEntries;
|
|
|
|
/// struct Entry {
|
|
|
|
/// imagerel32 LabelStart;
|
|
|
|
/// imagerel32 LabelEnd;
|
2015-01-22 10:27:44 +08:00
|
|
|
/// imagerel32 FilterOrFinally; // One means catch-all.
|
2015-01-14 09:05:27 +08:00
|
|
|
/// imagerel32 LabelLPad; // Zero means __finally.
|
|
|
|
/// } Entries[NumEntries];
|
|
|
|
/// };
|
2015-05-29 06:47:01 +08:00
|
|
|
void WinException::emitCSpecificHandlerTable() {
|
2015-01-14 09:05:27 +08:00
|
|
|
const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
|
|
|
|
|
|
|
|
// Simplifying assumptions for first implementation:
|
|
|
|
// - Cleanups are not implemented.
|
|
|
|
// - Filters are not implemented.
|
|
|
|
|
|
|
|
// The Itanium LSDA table sorts similar landing pads together to simplify the
|
|
|
|
// actions table, but we don't need that.
|
|
|
|
SmallVector<const LandingPadInfo *, 64> LandingPads;
|
|
|
|
LandingPads.reserve(PadInfos.size());
|
|
|
|
for (const auto &LP : PadInfos)
|
|
|
|
LandingPads.push_back(&LP);
|
|
|
|
|
|
|
|
// Compute label ranges for call sites as we would for the Itanium LSDA, but
|
|
|
|
// use an all zero action table because we aren't using these actions.
|
|
|
|
SmallVector<unsigned, 64> FirstActions;
|
|
|
|
FirstActions.resize(LandingPads.size());
|
|
|
|
SmallVector<CallSiteEntry, 64> CallSites;
|
|
|
|
computeCallSiteTable(CallSites, LandingPads, FirstActions);
|
|
|
|
|
2015-02-28 02:18:39 +08:00
|
|
|
MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin();
|
|
|
|
MCSymbol *EHFuncEndSym = Asm->getFunctionEnd();
|
2015-01-14 09:05:27 +08:00
|
|
|
|
|
|
|
// Emit the number of table entries.
|
|
|
|
unsigned NumEntries = 0;
|
|
|
|
for (const CallSiteEntry &CSE : CallSites) {
|
|
|
|
if (!CSE.LPad)
|
|
|
|
continue; // Ignore gaps.
|
2015-04-22 02:23:57 +08:00
|
|
|
NumEntries += CSE.LPad->SEHHandlers.size();
|
2015-01-14 09:05:27 +08:00
|
|
|
}
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitIntValue(NumEntries, 4);
|
2015-01-14 09:05:27 +08:00
|
|
|
|
2015-04-22 02:23:57 +08:00
|
|
|
// If there are no actions, we don't need to iterate again.
|
|
|
|
if (NumEntries == 0)
|
|
|
|
return;
|
|
|
|
|
2015-01-14 09:05:27 +08:00
|
|
|
// Emit the four-label records for each call site entry. The table has to be
|
|
|
|
// sorted in layout order, and the call sites should already be sorted.
|
|
|
|
for (const CallSiteEntry &CSE : CallSites) {
|
|
|
|
// Ignore gaps. Unlike the Itanium model, unwinding through a frame without
|
|
|
|
// an EH table entry will propagate the exception rather than terminating
|
|
|
|
// the program.
|
|
|
|
if (!CSE.LPad)
|
|
|
|
continue;
|
|
|
|
const LandingPadInfo *LPad = CSE.LPad;
|
|
|
|
|
|
|
|
// Compute the label range. We may reuse the function begin and end labels
|
|
|
|
// rather than forming new ones.
|
|
|
|
const MCExpr *Begin =
|
2015-05-30 01:00:57 +08:00
|
|
|
create32bitRef(CSE.BeginLabel ? CSE.BeginLabel : EHFuncBeginSym);
|
2015-01-14 09:05:27 +08:00
|
|
|
const MCExpr *End;
|
|
|
|
if (CSE.EndLabel) {
|
|
|
|
// The interval is half-open, so we have to add one to include the return
|
|
|
|
// address of the last invoke in the range.
|
2015-05-30 09:25:56 +08:00
|
|
|
End = MCBinaryExpr::createAdd(create32bitRef(CSE.EndLabel),
|
|
|
|
MCConstantExpr::create(1, Asm->OutContext),
|
2015-01-14 09:05:27 +08:00
|
|
|
Asm->OutContext);
|
|
|
|
} else {
|
2015-05-30 01:00:57 +08:00
|
|
|
End = create32bitRef(EHFuncEndSym);
|
2015-01-14 09:05:27 +08:00
|
|
|
}
|
|
|
|
|
2015-04-22 02:23:57 +08:00
|
|
|
// Emit an entry for each action.
|
|
|
|
for (SEHHandler Handler : LPad->SEHHandlers) {
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitValue(Begin, 4);
|
|
|
|
Asm->OutStreamer->EmitValue(End, 4);
|
2015-04-22 02:23:57 +08:00
|
|
|
|
|
|
|
// Emit the filter or finally function pointer, if present. Otherwise,
|
|
|
|
// emit '1' to indicate a catch-all.
|
|
|
|
const Function *F = Handler.FilterOrFinally;
|
|
|
|
if (F)
|
2015-05-30 01:00:57 +08:00
|
|
|
Asm->OutStreamer->EmitValue(create32bitRef(Asm->getSymbol(F)), 4);
|
2015-04-22 02:23:57 +08:00
|
|
|
else
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitIntValue(1, 4);
|
2015-04-22 02:23:57 +08:00
|
|
|
|
|
|
|
// Emit the recovery address, if present. Otherwise, this must be a
|
|
|
|
// finally.
|
|
|
|
const BlockAddress *BA = Handler.RecoverBA;
|
|
|
|
if (BA)
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitValue(
|
2015-05-30 01:00:57 +08:00
|
|
|
create32bitRef(Asm->GetBlockAddressSymbol(BA)), 4);
|
2015-04-22 02:23:57 +08:00
|
|
|
else
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitIntValue(0, 4);
|
2015-04-22 02:23:57 +08:00
|
|
|
}
|
2015-01-14 09:05:27 +08:00
|
|
|
}
|
|
|
|
}
|
2015-03-31 06:58:10 +08:00
|
|
|
|
2015-05-29 06:47:01 +08:00
|
|
|
void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
|
2015-03-31 06:58:10 +08:00
|
|
|
const Function *F = MF->getFunction();
|
|
|
|
const Function *ParentF = MMI->getWinEHParent(F);
|
2015-04-25 03:11:51 +08:00
|
|
|
auto &OS = *Asm->OutStreamer;
|
2015-04-01 06:35:44 +08:00
|
|
|
WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(ParentF);
|
2015-03-31 06:58:10 +08:00
|
|
|
|
|
|
|
StringRef ParentLinkageName =
|
|
|
|
GlobalValue::getRealLinkageName(ParentF->getName());
|
|
|
|
|
2015-05-30 01:00:57 +08:00
|
|
|
MCSymbol *FuncInfoXData = nullptr;
|
|
|
|
if (shouldEmitPersonality) {
|
|
|
|
FuncInfoXData = Asm->OutContext.getOrCreateSymbol(
|
|
|
|
Twine("$cppxdata$", ParentLinkageName));
|
|
|
|
OS.EmitValue(create32bitRef(FuncInfoXData), 4);
|
|
|
|
|
|
|
|
extendIP2StateTable(MF, ParentF, FuncInfo);
|
|
|
|
|
|
|
|
// Defer emission until we've visited the parent function and all the catch
|
|
|
|
// handlers. Cleanups don't contribute to the ip2state table, so don't count
|
|
|
|
// them.
|
|
|
|
if (ParentF != F && !FuncInfo.CatchHandlerMaxState.count(F))
|
|
|
|
return;
|
|
|
|
++FuncInfo.NumIPToStateFuncsVisited;
|
|
|
|
if (FuncInfo.NumIPToStateFuncsVisited != FuncInfo.CatchHandlerMaxState.size())
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
FuncInfoXData = Asm->OutContext.getOrCreateLSDASymbol(ParentLinkageName);
|
2015-03-31 06:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MCSymbol *UnwindMapXData = nullptr;
|
|
|
|
MCSymbol *TryBlockMapXData = nullptr;
|
|
|
|
MCSymbol *IPToStateXData = nullptr;
|
|
|
|
if (!FuncInfo.UnwindMap.empty())
|
2015-05-19 02:43:14 +08:00
|
|
|
UnwindMapXData = Asm->OutContext.getOrCreateSymbol(
|
2015-03-31 06:58:10 +08:00
|
|
|
Twine("$stateUnwindMap$", ParentLinkageName));
|
|
|
|
if (!FuncInfo.TryBlockMap.empty())
|
2015-05-19 02:43:14 +08:00
|
|
|
TryBlockMapXData = Asm->OutContext.getOrCreateSymbol(
|
2015-03-31 06:58:10 +08:00
|
|
|
Twine("$tryMap$", ParentLinkageName));
|
|
|
|
if (!FuncInfo.IPToStateList.empty())
|
2015-05-19 02:43:14 +08:00
|
|
|
IPToStateXData = Asm->OutContext.getOrCreateSymbol(
|
2015-03-31 06:58:10 +08:00
|
|
|
Twine("$ip2state$", ParentLinkageName));
|
|
|
|
|
|
|
|
// FuncInfo {
|
|
|
|
// uint32_t MagicNumber
|
|
|
|
// int32_t MaxState;
|
|
|
|
// UnwindMapEntry *UnwindMap;
|
|
|
|
// uint32_t NumTryBlocks;
|
|
|
|
// TryBlockMapEntry *TryBlockMap;
|
2015-05-30 01:00:57 +08:00
|
|
|
// uint32_t IPMapEntries; // always 0 for x86
|
|
|
|
// IPToStateMapEntry *IPToStateMap; // always 0 for x86
|
|
|
|
// uint32_t UnwindHelp; // non-x86 only
|
2015-03-31 06:58:10 +08:00
|
|
|
// ESTypeList *ESTypeList;
|
|
|
|
// int32_t EHFlags;
|
|
|
|
// }
|
|
|
|
// EHFlags & 1 -> Synchronous exceptions only, no async exceptions.
|
|
|
|
// EHFlags & 2 -> ???
|
|
|
|
// EHFlags & 4 -> The function is noexcept(true), unwinding can't continue.
|
|
|
|
OS.EmitLabel(FuncInfoXData);
|
|
|
|
OS.EmitIntValue(0x19930522, 4); // MagicNumber
|
|
|
|
OS.EmitIntValue(FuncInfo.UnwindMap.size(), 4); // MaxState
|
2015-05-30 01:00:57 +08:00
|
|
|
OS.EmitValue(create32bitRef(UnwindMapXData), 4); // UnwindMap
|
2015-03-31 06:58:10 +08:00
|
|
|
OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4); // NumTryBlocks
|
2015-05-30 01:00:57 +08:00
|
|
|
OS.EmitValue(create32bitRef(TryBlockMapXData), 4); // TryBlockMap
|
2015-03-31 06:58:10 +08:00
|
|
|
OS.EmitIntValue(FuncInfo.IPToStateList.size(), 4); // IPMapEntries
|
2015-05-30 01:00:57 +08:00
|
|
|
OS.EmitValue(create32bitRef(IPToStateXData), 4); // IPToStateMap
|
|
|
|
if (Asm->MAI->usesWindowsCFI())
|
|
|
|
OS.EmitIntValue(FuncInfo.UnwindHelpFrameOffset, 4); // UnwindHelp
|
2015-03-31 06:58:10 +08:00
|
|
|
OS.EmitIntValue(0, 4); // ESTypeList
|
|
|
|
OS.EmitIntValue(1, 4); // EHFlags
|
|
|
|
|
|
|
|
// UnwindMapEntry {
|
|
|
|
// int32_t ToState;
|
|
|
|
// void (*Action)();
|
|
|
|
// };
|
|
|
|
if (UnwindMapXData) {
|
|
|
|
OS.EmitLabel(UnwindMapXData);
|
|
|
|
for (const WinEHUnwindMapEntry &UME : FuncInfo.UnwindMap) {
|
|
|
|
OS.EmitIntValue(UME.ToState, 4); // ToState
|
2015-05-30 01:00:57 +08:00
|
|
|
OS.EmitValue(create32bitRef(UME.Cleanup), 4); // Action
|
2015-03-31 06:58:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryBlockMap {
|
|
|
|
// int32_t TryLow;
|
|
|
|
// int32_t TryHigh;
|
|
|
|
// int32_t CatchHigh;
|
|
|
|
// int32_t NumCatches;
|
|
|
|
// HandlerType *HandlerArray;
|
|
|
|
// };
|
|
|
|
if (TryBlockMapXData) {
|
|
|
|
OS.EmitLabel(TryBlockMapXData);
|
|
|
|
SmallVector<MCSymbol *, 1> HandlerMaps;
|
|
|
|
for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
|
|
|
|
WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
|
|
|
|
MCSymbol *HandlerMapXData = nullptr;
|
|
|
|
|
|
|
|
if (!TBME.HandlerArray.empty())
|
|
|
|
HandlerMapXData =
|
2015-05-19 02:43:14 +08:00
|
|
|
Asm->OutContext.getOrCreateSymbol(Twine("$handlerMap$")
|
2015-03-31 06:58:10 +08:00
|
|
|
.concat(Twine(I))
|
|
|
|
.concat("$")
|
|
|
|
.concat(ParentLinkageName));
|
|
|
|
|
|
|
|
HandlerMaps.push_back(HandlerMapXData);
|
|
|
|
|
2015-04-04 07:37:34 +08:00
|
|
|
int CatchHigh = -1;
|
|
|
|
for (WinEHHandlerType &HT : TBME.HandlerArray)
|
|
|
|
CatchHigh =
|
|
|
|
std::max(CatchHigh, FuncInfo.CatchHandlerMaxState[HT.Handler]);
|
|
|
|
|
2015-03-31 06:58:10 +08:00
|
|
|
assert(TBME.TryLow <= TBME.TryHigh);
|
|
|
|
OS.EmitIntValue(TBME.TryLow, 4); // TryLow
|
|
|
|
OS.EmitIntValue(TBME.TryHigh, 4); // TryHigh
|
2015-04-04 07:37:34 +08:00
|
|
|
OS.EmitIntValue(CatchHigh, 4); // CatchHigh
|
2015-03-31 06:58:10 +08:00
|
|
|
OS.EmitIntValue(TBME.HandlerArray.size(), 4); // NumCatches
|
2015-05-30 01:00:57 +08:00
|
|
|
OS.EmitValue(create32bitRef(HandlerMapXData), 4); // HandlerArray
|
2015-03-31 06:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
|
|
|
|
WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
|
|
|
|
MCSymbol *HandlerMapXData = HandlerMaps[I];
|
|
|
|
if (!HandlerMapXData)
|
|
|
|
continue;
|
|
|
|
// HandlerType {
|
|
|
|
// int32_t Adjectives;
|
|
|
|
// TypeDescriptor *Type;
|
|
|
|
// int32_t CatchObjOffset;
|
|
|
|
// void (*Handler)();
|
|
|
|
// int32_t ParentFrameOffset; // x64 only
|
|
|
|
// };
|
|
|
|
OS.EmitLabel(HandlerMapXData);
|
|
|
|
for (const WinEHHandlerType &HT : TBME.HandlerArray) {
|
2015-04-08 03:46:38 +08:00
|
|
|
// Get the frame escape label with the offset of the catch object. If
|
|
|
|
// the index is -1, then there is no catch object, and we should emit an
|
|
|
|
// offset of zero, indicating that no copy will occur.
|
|
|
|
const MCExpr *FrameAllocOffsetRef = nullptr;
|
|
|
|
if (HT.CatchObjRecoverIdx >= 0) {
|
|
|
|
MCSymbol *FrameAllocOffset =
|
|
|
|
Asm->OutContext.getOrCreateFrameAllocSymbol(
|
2015-04-16 01:47:26 +08:00
|
|
|
GlobalValue::getRealLinkageName(ParentF->getName()),
|
2015-04-08 03:46:38 +08:00
|
|
|
HT.CatchObjRecoverIdx);
|
2015-05-30 09:25:56 +08:00
|
|
|
FrameAllocOffsetRef = MCSymbolRefExpr::create(
|
2015-04-08 03:46:38 +08:00
|
|
|
FrameAllocOffset, MCSymbolRefExpr::VK_None, Asm->OutContext);
|
|
|
|
} else {
|
2015-05-30 09:25:56 +08:00
|
|
|
FrameAllocOffsetRef = MCConstantExpr::create(0, Asm->OutContext);
|
2015-04-08 03:46:38 +08:00
|
|
|
}
|
2015-04-04 06:49:05 +08:00
|
|
|
|
2015-03-31 06:58:10 +08:00
|
|
|
OS.EmitIntValue(HT.Adjectives, 4); // Adjectives
|
2015-05-30 01:00:57 +08:00
|
|
|
OS.EmitValue(create32bitRef(HT.TypeDescriptor), 4); // Type
|
2015-04-04 06:49:05 +08:00
|
|
|
OS.EmitValue(FrameAllocOffsetRef, 4); // CatchObjOffset
|
2015-05-30 01:00:57 +08:00
|
|
|
OS.EmitValue(create32bitRef(HT.Handler), 4); // Handler
|
|
|
|
|
|
|
|
if (shouldEmitPersonality) {
|
|
|
|
MCSymbol *ParentFrameOffset =
|
|
|
|
Asm->OutContext.getOrCreateParentFrameOffsetSymbol(
|
|
|
|
GlobalValue::getRealLinkageName(HT.Handler->getName()));
|
2015-05-30 09:25:56 +08:00
|
|
|
const MCSymbolRefExpr *ParentFrameOffsetRef = MCSymbolRefExpr::create(
|
2015-05-30 01:00:57 +08:00
|
|
|
ParentFrameOffset, MCSymbolRefExpr::VK_None, Asm->OutContext);
|
|
|
|
OS.EmitValue(ParentFrameOffsetRef, 4); // ParentFrameOffset
|
|
|
|
}
|
2015-03-31 06:58:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPToStateMapEntry {
|
|
|
|
// void *IP;
|
|
|
|
// int32_t State;
|
|
|
|
// };
|
|
|
|
if (IPToStateXData) {
|
|
|
|
OS.EmitLabel(IPToStateXData);
|
|
|
|
for (auto &IPStatePair : FuncInfo.IPToStateList) {
|
2015-05-30 01:00:57 +08:00
|
|
|
OS.EmitValue(create32bitRef(IPStatePair.first), 4); // IP
|
2015-03-31 06:58:10 +08:00
|
|
|
OS.EmitIntValue(IPStatePair.second, 4); // State
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-30 01:00:57 +08:00
|
|
|
|
|
|
|
void WinException::extendIP2StateTable(const MachineFunction *MF,
|
|
|
|
const Function *ParentF,
|
|
|
|
WinEHFuncInfo &FuncInfo) {
|
|
|
|
const Function *F = MF->getFunction();
|
|
|
|
|
|
|
|
// The Itanium LSDA table sorts similar landing pads together to simplify the
|
|
|
|
// actions table, but we don't need that.
|
|
|
|
SmallVector<const LandingPadInfo *, 64> LandingPads;
|
|
|
|
const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
|
|
|
|
LandingPads.reserve(PadInfos.size());
|
|
|
|
for (const auto &LP : PadInfos)
|
|
|
|
LandingPads.push_back(&LP);
|
|
|
|
|
|
|
|
RangeMapType PadMap;
|
|
|
|
computePadMap(LandingPads, PadMap);
|
|
|
|
|
|
|
|
// The end label of the previous invoke or nounwind try-range.
|
|
|
|
MCSymbol *LastLabel = Asm->getFunctionBegin();
|
|
|
|
|
|
|
|
// Whether there is a potentially throwing instruction (currently this means
|
|
|
|
// an ordinary call) between the end of the previous try-range and now.
|
|
|
|
bool SawPotentiallyThrowing = false;
|
|
|
|
|
|
|
|
int LastEHState = -2;
|
|
|
|
|
|
|
|
// The parent function and the catch handlers contribute to the 'ip2state'
|
|
|
|
// table.
|
|
|
|
|
|
|
|
// Include ip2state entries for the beginning of the main function and
|
|
|
|
// for catch handler functions.
|
|
|
|
if (F == ParentF) {
|
|
|
|
FuncInfo.IPToStateList.push_back(std::make_pair(LastLabel, -1));
|
|
|
|
LastEHState = -1;
|
|
|
|
} else if (FuncInfo.HandlerBaseState.count(F)) {
|
|
|
|
FuncInfo.IPToStateList.push_back(
|
|
|
|
std::make_pair(LastLabel, FuncInfo.HandlerBaseState[F]));
|
|
|
|
LastEHState = FuncInfo.HandlerBaseState[F];
|
|
|
|
}
|
|
|
|
for (const auto &MBB : *MF) {
|
|
|
|
for (const auto &MI : MBB) {
|
|
|
|
if (!MI.isEHLabel()) {
|
|
|
|
if (MI.isCall())
|
|
|
|
SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of the previous try-range?
|
|
|
|
MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol();
|
|
|
|
if (BeginLabel == LastLabel)
|
|
|
|
SawPotentiallyThrowing = false;
|
|
|
|
|
|
|
|
// Beginning of a new try-range?
|
|
|
|
RangeMapType::const_iterator L = PadMap.find(BeginLabel);
|
|
|
|
if (L == PadMap.end())
|
|
|
|
// Nope, it was just some random label.
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const PadRange &P = L->second;
|
|
|
|
const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
|
|
|
|
assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
|
|
|
|
"Inconsistent landing pad map!");
|
|
|
|
|
|
|
|
// FIXME: Should this be using FuncInfo.HandlerBaseState?
|
|
|
|
if (SawPotentiallyThrowing && LastEHState != -1) {
|
|
|
|
FuncInfo.IPToStateList.push_back(std::make_pair(LastLabel, -1));
|
|
|
|
SawPotentiallyThrowing = false;
|
|
|
|
LastEHState = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LandingPad->WinEHState != LastEHState)
|
|
|
|
FuncInfo.IPToStateList.push_back(
|
|
|
|
std::make_pair(BeginLabel, LandingPad->WinEHState));
|
|
|
|
LastEHState = LandingPad->WinEHState;
|
|
|
|
LastLabel = LandingPad->EndLabels[P.RangeIndex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-10 05:42:19 +08:00
|
|
|
|
|
|
|
/// Emit the language-specific data that _except_handler3 and 4 expect. This is
|
|
|
|
/// functionally equivalent to the __C_specific_handler table, except it is
|
|
|
|
/// indexed by state number instead of IP.
|
|
|
|
void WinException::emitExceptHandlerTable(const MachineFunction *MF) {
|
|
|
|
auto &OS = *Asm->OutStreamer;
|
|
|
|
|
|
|
|
// Emit the __ehtable label that we use for llvm.x86.seh.lsda.
|
|
|
|
const Function *F = MF->getFunction();
|
|
|
|
StringRef FLinkageName = GlobalValue::getRealLinkageName(F->getName());
|
|
|
|
MCSymbol *LSDALabel = Asm->OutContext.getOrCreateLSDASymbol(FLinkageName);
|
|
|
|
OS.EmitLabel(LSDALabel);
|
|
|
|
|
|
|
|
const Function *Per = MMI->getPersonality();
|
|
|
|
StringRef PerName = Per->getName();
|
|
|
|
int BaseState = -1;
|
|
|
|
if (PerName == "_except_handler4") {
|
|
|
|
// The LSDA for _except_handler4 starts with this struct, followed by the
|
|
|
|
// scope table:
|
|
|
|
//
|
|
|
|
// struct EH4ScopeTable {
|
|
|
|
// int32_t GSCookieOffset;
|
|
|
|
// int32_t GSCookieXOROffset;
|
|
|
|
// int32_t EHCookieOffset;
|
|
|
|
// int32_t EHCookieXOROffset;
|
|
|
|
// ScopeTableEntry ScopeRecord[];
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// Only the EHCookieOffset field appears to vary, and it appears to be the
|
|
|
|
// offset from the final saved SP value to the retaddr.
|
|
|
|
OS.EmitIntValue(-2, 4);
|
|
|
|
OS.EmitIntValue(0, 4);
|
|
|
|
// FIXME: Calculate.
|
|
|
|
OS.EmitIntValue(9999, 4);
|
|
|
|
OS.EmitIntValue(0, 4);
|
|
|
|
BaseState = -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a list of pointers to LandingPadInfos and then sort by WinEHState.
|
|
|
|
const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
|
|
|
|
SmallVector<const LandingPadInfo *, 4> LPads;
|
|
|
|
LPads.reserve((PadInfos.size()));
|
|
|
|
for (const LandingPadInfo &LPInfo : PadInfos)
|
|
|
|
LPads.push_back(&LPInfo);
|
|
|
|
std::sort(LPads.begin(), LPads.end(),
|
|
|
|
[](const LandingPadInfo *L, const LandingPadInfo *R) {
|
|
|
|
return L->WinEHState < R->WinEHState;
|
|
|
|
});
|
|
|
|
|
|
|
|
// For each action in each lpad, emit one of these:
|
|
|
|
// struct ScopeTableEntry {
|
|
|
|
// int32_t EnclosingLevel;
|
|
|
|
// int32_t (__cdecl *FilterOrFinally)();
|
|
|
|
// void *HandlerLabel;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// The "outermost" action will use BaseState as its enclosing level. Each
|
|
|
|
// other action will refer to the previous state as its enclosing level.
|
|
|
|
int CurState = 0;
|
|
|
|
for (const LandingPadInfo *LPInfo : LPads) {
|
|
|
|
int EnclosingLevel = BaseState;
|
2015-06-10 08:04:53 +08:00
|
|
|
assert(CurState + int(LPInfo->SEHHandlers.size()) - 1 ==
|
|
|
|
LPInfo->WinEHState &&
|
2015-06-10 05:42:19 +08:00
|
|
|
"gaps in the SEH scope table");
|
|
|
|
for (const SEHHandler &Handler : LPInfo->SEHHandlers) {
|
|
|
|
// Emit the filter or finally function pointer, if present. Otherwise,
|
2015-06-11 02:14:07 +08:00
|
|
|
// emit '0' to indicate a catch-all.
|
|
|
|
const Function *F = Handler.FilterOrFinally;
|
|
|
|
const MCExpr *FilterOrFinally =
|
|
|
|
create32bitRef(F ? Asm->getSymbol(F) : nullptr);
|
2015-06-10 05:42:19 +08:00
|
|
|
|
|
|
|
// Compute the recovery address, which is a block address or null.
|
|
|
|
const BlockAddress *BA = Handler.RecoverBA;
|
|
|
|
const MCExpr *RecoverBBOrNull =
|
|
|
|
create32bitRef(BA ? Asm->GetBlockAddressSymbol(BA) : nullptr);
|
|
|
|
|
|
|
|
OS.EmitIntValue(EnclosingLevel, 4);
|
|
|
|
OS.EmitValue(FilterOrFinally, 4);
|
|
|
|
OS.EmitValue(RecoverBBOrNull, 4);
|
|
|
|
|
|
|
|
// The next state unwinds to this state.
|
|
|
|
EnclosingLevel = CurState;
|
|
|
|
CurState++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|