#include "llvm/MC/MCWin64EH.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/Twine.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCObjectStreamer.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/Win64EH.h"
namespace llvm {
class MCSection;
}
using namespace llvm;
static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) {
uint8_t Count = 0;
for (const auto &I : Insns) {
switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
default:
llvm_unreachable("Unsupported unwind code");
case Win64EH::UOP_PushNonVol:
case Win64EH::UOP_AllocSmall:
case Win64EH::UOP_SetFPReg:
case Win64EH::UOP_PushMachFrame:
Count += 1;
break;
case Win64EH::UOP_SaveNonVol:
case Win64EH::UOP_SaveXMM128:
Count += 2;
break;
case Win64EH::UOP_SaveNonVolBig:
case Win64EH::UOP_SaveXMM128Big:
Count += 3;
break;
case Win64EH::UOP_AllocLarge:
Count += (I.Offset > 512 * 1024 - 8) ? 3 : 2;
break;
}
}
return Count;
}
static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
const MCSymbol *RHS) {
MCContext &Context = Streamer.getContext();
const MCExpr *Diff =
MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
MCSymbolRefExpr::create(RHS, Context), Context);
Streamer.emitValue(Diff, 1);
}
static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
WinEH::Instruction &inst) {
uint8_t b2;
uint16_t w;
b2 = (inst.Operation & 0x0F);
switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
default:
llvm_unreachable("Unsupported unwind code");
case Win64EH::UOP_PushNonVol:
EmitAbsDifference(streamer, inst.Label, begin);
b2 |= (inst.Register & 0x0F) << 4;
streamer.emitInt8(b2);
break;
case Win64EH::UOP_AllocLarge:
EmitAbsDifference(streamer, inst.Label, begin);
if (inst.Offset > 512 * 1024 - 8) {
b2 |= 0x10;
streamer.emitInt8(b2);
w = inst.Offset & 0xFFF8;
streamer.emitInt16(w);
w = inst.Offset >> 16;
} else {
streamer.emitInt8(b2);
w = inst.Offset >> 3;
}
streamer.emitInt16(w);
break;
case Win64EH::UOP_AllocSmall:
b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4;
EmitAbsDifference(streamer, inst.Label, begin);
streamer.emitInt8(b2);
break;
case Win64EH::UOP_SetFPReg:
EmitAbsDifference(streamer, inst.Label, begin);
streamer.emitInt8(b2);
break;
case Win64EH::UOP_SaveNonVol:
case Win64EH::UOP_SaveXMM128:
b2 |= (inst.Register & 0x0F) << 4;
EmitAbsDifference(streamer, inst.Label, begin);
streamer.emitInt8(b2);
w = inst.Offset >> 3;
if (inst.Operation == Win64EH::UOP_SaveXMM128)
w >>= 1;
streamer.emitInt16(w);
break;
case Win64EH::UOP_SaveNonVolBig:
case Win64EH::UOP_SaveXMM128Big:
b2 |= (inst.Register & 0x0F) << 4;
EmitAbsDifference(streamer, inst.Label, begin);
streamer.emitInt8(b2);
if (inst.Operation == Win64EH::UOP_SaveXMM128Big)
w = inst.Offset & 0xFFF0;
else
w = inst.Offset & 0xFFF8;
streamer.emitInt16(w);
w = inst.Offset >> 16;
streamer.emitInt16(w);
break;
case Win64EH::UOP_PushMachFrame:
if (inst.Offset == 1)
b2 |= 0x10;
EmitAbsDifference(streamer, inst.Label, begin);
streamer.emitInt8(b2);
break;
}
}
static void EmitSymbolRefWithOfs(MCStreamer &streamer,
const MCSymbol *Base,
const MCSymbol *Other) {
MCContext &Context = streamer.getContext();
const MCSymbolRefExpr *BaseRef = MCSymbolRefExpr::create(Base, Context);
const MCSymbolRefExpr *OtherRef = MCSymbolRefExpr::create(Other, Context);
const MCExpr *Ofs = MCBinaryExpr::createSub(OtherRef, BaseRef, Context);
const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::create(Base,
MCSymbolRefExpr::VK_COFF_IMGREL32,
Context);
streamer.emitValue(MCBinaryExpr::createAdd(BaseRefRel, Ofs, Context), 4);
}
static void EmitRuntimeFunction(MCStreamer &streamer,
const WinEH::FrameInfo *info) {
MCContext &context = streamer.getContext();
streamer.emitValueToAlignment(4);
EmitSymbolRefWithOfs(streamer, info->Begin, info->Begin);
EmitSymbolRefWithOfs(streamer, info->Begin, info->End);
streamer.emitValue(MCSymbolRefExpr::create(info->Symbol,
MCSymbolRefExpr::VK_COFF_IMGREL32,
context), 4);
}
static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
if (info->Symbol)
return;
MCContext &context = streamer.getContext();
MCSymbol *Label = context.createTempSymbol();
streamer.emitValueToAlignment(4);
streamer.emitLabel(Label);
info->Symbol = Label;
uint8_t flags = 0x01;
if (info->ChainedParent)
flags |= Win64EH::UNW_ChainInfo << 3;
else {
if (info->HandlesUnwind)
flags |= Win64EH::UNW_TerminateHandler << 3;
if (info->HandlesExceptions)
flags |= Win64EH::UNW_ExceptionHandler << 3;
}
streamer.emitInt8(flags);
if (info->PrologEnd)
EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
else
streamer.emitInt8(0);
uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
streamer.emitInt8(numCodes);
uint8_t frame = 0;
if (info->LastFrameInst >= 0) {
WinEH::Instruction &frameInst = info->Instructions[info->LastFrameInst];
assert(frameInst.Operation == Win64EH::UOP_SetFPReg);
frame = (frameInst.Register & 0x0F) | (frameInst.Offset & 0xF0);
}
streamer.emitInt8(frame);
uint8_t numInst = info->Instructions.size();
for (uint8_t c = 0; c < numInst; ++c) {
WinEH::Instruction inst = info->Instructions.back();
info->Instructions.pop_back();
EmitUnwindCode(streamer, info->Begin, inst);
}
if (numCodes & 1) {
streamer.emitInt16(0);
}
if (flags & (Win64EH::UNW_ChainInfo << 3))
EmitRuntimeFunction(streamer, info->ChainedParent);
else if (flags &
((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
streamer.emitValue(MCSymbolRefExpr::create(info->ExceptionHandler,
MCSymbolRefExpr::VK_COFF_IMGREL32,
context), 4);
else if (numCodes == 0) {
streamer.emitInt32(0);
}
}
void llvm::Win64EH::UnwindEmitter::Emit(MCStreamer &Streamer) const {
for (const auto &CFI : Streamer.getWinFrameInfos()) {
MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
Streamer.switchSection(XData);
::EmitUnwindInfo(Streamer, CFI.get());
}
for (const auto &CFI : Streamer.getWinFrameInfos()) {
MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
Streamer.switchSection(PData);
EmitRuntimeFunction(Streamer, CFI.get());
}
}
void llvm::Win64EH::UnwindEmitter::EmitUnwindInfo(MCStreamer &Streamer,
WinEH::FrameInfo *info,
bool HandlerData) const {
MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
Streamer.switchSection(XData);
::EmitUnwindInfo(Streamer, info);
}
static const MCExpr *GetSubDivExpr(MCStreamer &Streamer, const MCSymbol *LHS,
const MCSymbol *RHS, int Div) {
MCContext &Context = Streamer.getContext();
const MCExpr *Expr =
MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
MCSymbolRefExpr::create(RHS, Context), Context);
if (Div != 1)
Expr = MCBinaryExpr::createDiv(Expr, MCConstantExpr::create(Div, Context),
Context);
return Expr;
}
static Optional<int64_t> GetOptionalAbsDifference(MCStreamer &Streamer,
const MCSymbol *LHS,
const MCSymbol *RHS) {
MCContext &Context = Streamer.getContext();
const MCExpr *Diff =
MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
MCSymbolRefExpr::create(RHS, Context), Context);
MCObjectStreamer *OS = (MCObjectStreamer *)(&Streamer);
int64_t value;
if (!Diff->evaluateAsAbsolute(value, OS->getAssembler()))
return None;
return value;
}
static int64_t GetAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
const MCSymbol *RHS) {
Optional<int64_t> MaybeDiff = GetOptionalAbsDifference(Streamer, LHS, RHS);
if (!MaybeDiff)
report_fatal_error("Failed to evaluate function length in SEH unwind info");
return *MaybeDiff;
}
static uint32_t ARM64CountOfUnwindCodes(ArrayRef<WinEH::Instruction> Insns) {
uint32_t Count = 0;
for (const auto &I : Insns) {
switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
default:
llvm_unreachable("Unsupported ARM64 unwind code");
case Win64EH::UOP_AllocSmall:
Count += 1;
break;
case Win64EH::UOP_AllocMedium:
Count += 2;
break;
case Win64EH::UOP_AllocLarge:
Count += 4;
break;
case Win64EH::UOP_SaveR19R20X:
Count += 1;
break;
case Win64EH::UOP_SaveFPLRX:
Count += 1;
break;
case Win64EH::UOP_SaveFPLR:
Count += 1;
break;
case Win64EH::UOP_SaveReg:
Count += 2;
break;
case Win64EH::UOP_SaveRegP:
Count += 2;
break;
case Win64EH::UOP_SaveRegPX:
Count += 2;
break;
case Win64EH::UOP_SaveRegX:
Count += 2;
break;
case Win64EH::UOP_SaveLRPair:
Count += 2;
break;
case Win64EH::UOP_SaveFReg:
Count += 2;
break;
case Win64EH::UOP_SaveFRegP:
Count += 2;
break;
case Win64EH::UOP_SaveFRegX:
Count += 2;
break;
case Win64EH::UOP_SaveFRegPX:
Count += 2;
break;
case Win64EH::UOP_SetFP:
Count += 1;
break;
case Win64EH::UOP_AddFP:
Count += 2;
break;
case Win64EH::UOP_Nop:
Count += 1;
break;
case Win64EH::UOP_End:
Count += 1;
break;
case Win64EH::UOP_SaveNext:
Count += 1;
break;
case Win64EH::UOP_TrapFrame:
Count += 1;
break;
case Win64EH::UOP_PushMachFrame:
Count += 1;
break;
case Win64EH::UOP_Context:
Count += 1;
break;
case Win64EH::UOP_ClearUnwoundToCall:
Count += 1;
break;
}
}
return Count;
}
static void ARM64EmitUnwindCode(MCStreamer &streamer,
const WinEH::Instruction &inst) {
uint8_t b, reg;
switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
default:
llvm_unreachable("Unsupported ARM64 unwind code");
case Win64EH::UOP_AllocSmall:
b = (inst.Offset >> 4) & 0x1F;
streamer.emitInt8(b);
break;
case Win64EH::UOP_AllocMedium: {
uint16_t hw = (inst.Offset >> 4) & 0x7FF;
b = 0xC0;
b |= (hw >> 8);
streamer.emitInt8(b);
b = hw & 0xFF;
streamer.emitInt8(b);
break;
}
case Win64EH::UOP_AllocLarge: {
uint32_t w;
b = 0xE0;
streamer.emitInt8(b);
w = inst.Offset >> 4;
b = (w & 0x00FF0000) >> 16;
streamer.emitInt8(b);
b = (w & 0x0000FF00) >> 8;
streamer.emitInt8(b);
b = w & 0x000000FF;
streamer.emitInt8(b);
break;
}
case Win64EH::UOP_SetFP:
b = 0xE1;
streamer.emitInt8(b);
break;
case Win64EH::UOP_AddFP:
b = 0xE2;
streamer.emitInt8(b);
b = (inst.Offset >> 3);
streamer.emitInt8(b);
break;
case Win64EH::UOP_Nop:
b = 0xE3;
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveR19R20X:
b = 0x20;
b |= (inst.Offset >> 3) & 0x1F;
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveFPLRX:
b = 0x80;
b |= ((inst.Offset - 1) >> 3) & 0x3F;
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveFPLR:
b = 0x40;
b |= (inst.Offset >> 3) & 0x3F;
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveReg:
assert(inst.Register >= 19 && "Saved reg must be >= 19");
reg = inst.Register - 19;
b = 0xD0 | ((reg & 0xC) >> 2);
streamer.emitInt8(b);
b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveRegX:
assert(inst.Register >= 19 && "Saved reg must be >= 19");
reg = inst.Register - 19;
b = 0xD4 | ((reg & 0x8) >> 3);
streamer.emitInt8(b);
b = ((reg & 0x7) << 5) | ((inst.Offset >> 3) - 1);
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveRegP:
assert(inst.Register >= 19 && "Saved registers must be >= 19");
reg = inst.Register - 19;
b = 0xC8 | ((reg & 0xC) >> 2);
streamer.emitInt8(b);
b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveRegPX:
assert(inst.Register >= 19 && "Saved registers must be >= 19");
reg = inst.Register - 19;
b = 0xCC | ((reg & 0xC) >> 2);
streamer.emitInt8(b);
b = ((reg & 0x3) << 6) | ((inst.Offset >> 3) - 1);
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveLRPair:
assert(inst.Register >= 19 && "Saved reg must be >= 19");
reg = inst.Register - 19;
assert((reg % 2) == 0 && "Saved reg must be 19+2*X");
reg /= 2;
b = 0xD6 | ((reg & 0x7) >> 2);
streamer.emitInt8(b);
b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveFReg:
assert(inst.Register >= 8 && "Saved dreg must be >= 8");
reg = inst.Register - 8;
b = 0xDC | ((reg & 0x4) >> 2);
streamer.emitInt8(b);
b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveFRegX:
assert(inst.Register >= 8 && "Saved dreg must be >= 8");
reg = inst.Register - 8;
b = 0xDE;
streamer.emitInt8(b);
b = ((reg & 0x7) << 5) | ((inst.Offset >> 3) - 1);
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveFRegP:
assert(inst.Register >= 8 && "Saved dregs must be >= 8");
reg = inst.Register - 8;
b = 0xD8 | ((reg & 0x4) >> 2);
streamer.emitInt8(b);
b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveFRegPX:
assert(inst.Register >= 8 && "Saved dregs must be >= 8");
reg = inst.Register - 8;
b = 0xDA | ((reg & 0x4) >> 2);
streamer.emitInt8(b);
b = ((reg & 0x3) << 6) | ((inst.Offset >> 3) - 1);
streamer.emitInt8(b);
break;
case Win64EH::UOP_End:
b = 0xE4;
streamer.emitInt8(b);
break;
case Win64EH::UOP_SaveNext:
b = 0xE6;
streamer.emitInt8(b);
break;
case Win64EH::UOP_TrapFrame:
b = 0xE8;
streamer.emitInt8(b);
break;
case Win64EH::UOP_PushMachFrame:
b = 0xE9;
streamer.emitInt8(b);
break;
case Win64EH::UOP_Context:
b = 0xEA;
streamer.emitInt8(b);
break;
case Win64EH::UOP_ClearUnwoundToCall:
b = 0xEC;
streamer.emitInt8(b);
break;
}
}
static MCSymbol*
FindMatchingEpilog(const std::vector<WinEH::Instruction>& EpilogInstrs,
const std::vector<MCSymbol *>& Epilogs,
const WinEH::FrameInfo *info) {
for (auto *EpilogStart : Epilogs) {
auto InstrsIter = info->EpilogMap.find(EpilogStart);
assert(InstrsIter != info->EpilogMap.end() &&
"Epilog not found in EpilogMap");
const auto &Instrs = InstrsIter->second.Instructions;
if (Instrs.size() != EpilogInstrs.size())
continue;
bool Match = true;
for (unsigned i = 0; i < Instrs.size(); ++i)
if (Instrs[i] != EpilogInstrs[i]) {
Match = false;
break;
}
if (Match)
return EpilogStart;
}
return nullptr;
}
static void simplifyARM64Opcodes(std::vector<WinEH::Instruction> &Instructions,
bool Reverse) {
unsigned PrevOffset = -1;
unsigned PrevRegister = -1;
auto VisitInstruction = [&](WinEH::Instruction &Inst) {
if (Inst.Operation == Win64EH::UOP_SaveRegP && Inst.Register == 29) {
Inst.Operation = Win64EH::UOP_SaveFPLR;
Inst.Register = -1;
} else if (Inst.Operation == Win64EH::UOP_SaveRegPX &&
Inst.Register == 29) {
Inst.Operation = Win64EH::UOP_SaveFPLRX;
Inst.Register = -1;
} else if (Inst.Operation == Win64EH::UOP_SaveRegPX &&
Inst.Register == 19 && Inst.Offset <= 248) {
Inst.Operation = Win64EH::UOP_SaveR19R20X;
Inst.Register = -1;
} else if (Inst.Operation == Win64EH::UOP_AddFP && Inst.Offset == 0) {
Inst.Operation = Win64EH::UOP_SetFP;
} else if (Inst.Operation == Win64EH::UOP_SaveRegP &&
Inst.Register == PrevRegister + 2 &&
Inst.Offset == PrevOffset + 16) {
Inst.Operation = Win64EH::UOP_SaveNext;
Inst.Register = -1;
Inst.Offset = 0;
}
if (Inst.Operation == Win64EH::UOP_SaveR19R20X) {
PrevOffset = 0;
PrevRegister = 19;
} else if (Inst.Operation == Win64EH::UOP_SaveRegPX) {
PrevOffset = 0;
PrevRegister = Inst.Register;
} else if (Inst.Operation == Win64EH::UOP_SaveRegP) {
PrevOffset = Inst.Offset;
PrevRegister = Inst.Register;
} else if (Inst.Operation == Win64EH::UOP_SaveNext) {
PrevRegister += 2;
PrevOffset += 16;
} else {
PrevRegister = -1;
PrevOffset = -1;
}
};
if (Reverse) {
for (auto It = Instructions.rbegin(); It != Instructions.rend(); It++)
VisitInstruction(*It);
} else {
for (WinEH::Instruction &Inst : Instructions)
VisitInstruction(Inst);
}
}
static int
getARM64OffsetInProlog(const std::vector<WinEH::Instruction> &Prolog,
const std::vector<WinEH::Instruction> &Epilog) {
if (Epilog.size() > Prolog.size())
return -1;
for (int I = Epilog.size() - 1; I >= 0; I--) {
if (Prolog[I] != Epilog[Epilog.size() - 1 - I])
return -1;
}
if (Epilog.size() == Prolog.size())
return 0;
return ARM64CountOfUnwindCodes(ArrayRef<WinEH::Instruction>(
&Prolog[Epilog.size()], Prolog.size() - Epilog.size()));
}
static int checkARM64PackedEpilog(MCStreamer &streamer, WinEH::FrameInfo *info,
int PrologCodeBytes) {
if (info->EpilogMap.size() != 1)
return -1;
const std::vector<WinEH::Instruction> &Epilog =
info->EpilogMap.begin()->second.Instructions;
uint32_t DistanceFromEnd = (uint32_t)GetAbsDifference(
streamer, info->FuncletOrFuncEnd, info->EpilogMap.begin()->first);
if (DistanceFromEnd / 4 != Epilog.size())
return -1;
int RetVal = -1;
if (PrologCodeBytes <= 31 &&
PrologCodeBytes + ARM64CountOfUnwindCodes(Epilog) <= 124)
RetVal = PrologCodeBytes;
int Offset = getARM64OffsetInProlog(info->Instructions, Epilog);
if (Offset < 0)
return RetVal;
if (Offset > 31 || PrologCodeBytes > 124)
return RetVal;
info->EpilogMap.clear();
return Offset;
}
static bool tryARM64PackedUnwind(WinEH::FrameInfo *info, uint32_t FuncLength,
int PackedEpilogOffset) {
if (PackedEpilogOffset == 0) {
} else if (PackedEpilogOffset == 1) {
if (info->Instructions.back().Operation != Win64EH::UOP_SetFP)
return false;
} else {
return false;
}
unsigned RegI = 0, RegF = 0;
int Predecrement = 0;
enum {
Start,
Start2,
IntRegs,
FloatRegs,
InputArgs,
StackAdjust,
FrameRecord,
End
} Location = Start;
bool StandaloneLR = false, FPLRPair = false;
int StackOffset = 0;
int Nops = 0;
for (const WinEH::Instruction &Inst : info->Instructions) {
switch (Inst.Operation) {
case Win64EH::UOP_End:
if (Location != Start)
return false;
Location = Start2;
break;
case Win64EH::UOP_SaveR19R20X:
if (Location != Start2)
return false;
Predecrement = Inst.Offset;
RegI = 2;
Location = IntRegs;
break;
case Win64EH::UOP_SaveRegX:
if (Location != Start2)
return false;
Predecrement = Inst.Offset;
if (Inst.Register == 19)
RegI += 1;
else if (Inst.Register == 30)
StandaloneLR = true;
else
return false;
Location = FloatRegs;
break;
case Win64EH::UOP_SaveRegPX:
return false;
case Win64EH::UOP_SaveRegP:
if (Location != IntRegs || Inst.Offset != 8 * RegI ||
Inst.Register != 19 + RegI)
return false;
RegI += 2;
break;
case Win64EH::UOP_SaveReg:
if (Location != IntRegs || Inst.Offset != 8 * RegI)
return false;
if (Inst.Register == 19 + RegI)
RegI += 1;
else if (Inst.Register == 30)
StandaloneLR = true;
else
return false;
Location = FloatRegs;
break;
case Win64EH::UOP_SaveLRPair:
if (Location != IntRegs || Inst.Offset != 8 * RegI ||
Inst.Register != 19 + RegI)
return false;
RegI += 1;
StandaloneLR = true;
Location = FloatRegs;
break;
case Win64EH::UOP_SaveFRegX:
return false;
case Win64EH::UOP_SaveFReg:
if (Location != FloatRegs || RegF == 0 || Inst.Register != 8 + RegF ||
Inst.Offset != 8 * (RegI + (StandaloneLR ? 1 : 0) + RegF))
return false;
RegF += 1;
Location = InputArgs;
break;
case Win64EH::UOP_SaveFRegPX:
if (Location != Start2 || Inst.Register != 8)
return false;
Predecrement = Inst.Offset;
RegF = 2;
Location = FloatRegs;
break;
case Win64EH::UOP_SaveFRegP:
if ((Location != IntRegs && Location != FloatRegs) ||
Inst.Register != 8 + RegF ||
Inst.Offset != 8 * (RegI + (StandaloneLR ? 1 : 0) + RegF))
return false;
RegF += 2;
Location = FloatRegs;
break;
case Win64EH::UOP_SaveNext:
if (Location == IntRegs)
RegI += 2;
else if (Location == FloatRegs)
RegF += 2;
else
return false;
break;
case Win64EH::UOP_Nop:
if (Location != IntRegs && Location != FloatRegs && Location != InputArgs)
return false;
Location = InputArgs;
Nops++;
break;
case Win64EH::UOP_AllocSmall:
case Win64EH::UOP_AllocMedium:
if (Location != Start2 && Location != IntRegs && Location != FloatRegs &&
Location != InputArgs && Location != StackAdjust)
return false;
if (StackOffset == 0)
StackOffset = Inst.Offset;
else if (StackOffset != 4080)
return false;
else
StackOffset += Inst.Offset;
Location = StackAdjust;
break;
case Win64EH::UOP_SaveFPLRX:
if (Location != Start2 && Location != IntRegs && Location != FloatRegs &&
Location != InputArgs)
return false;
StackOffset = Inst.Offset;
Location = FrameRecord;
FPLRPair = true;
break;
case Win64EH::UOP_SaveFPLR:
if (Location != StackAdjust || Inst.Offset != 0)
return false;
Location = FrameRecord;
FPLRPair = true;
break;
case Win64EH::UOP_SetFP:
if (Location != FrameRecord)
return false;
Location = End;
break;
}
}
if (RegI > 10 || RegF > 8)
return false;
if (StandaloneLR && FPLRPair)
return false;
if (FPLRPair && Location != End)
return false;
if (Nops != 0 && Nops != 4)
return false;
int H = Nops == 4;
if (H)
return false;
int IntSZ = 8 * RegI;
if (StandaloneLR)
IntSZ += 8;
int FpSZ = 8 * RegF; int SavSZ = (IntSZ + FpSZ + 8 * 8 * H + 0xF) & ~0xF;
if (Predecrement != SavSZ)
return false;
if (FPLRPair && StackOffset < 16)
return false;
if (StackOffset % 16)
return false;
uint32_t FrameSize = (StackOffset + SavSZ) / 16;
if (FrameSize > 0x1FF)
return false;
assert(RegF != 1 && "One single float reg not allowed");
if (RegF > 0)
RegF--; assert(FuncLength <= 0x7FF && "FuncLength should have been checked earlier");
int Flag = 0x01; int CR = FPLRPair ? 3 : StandaloneLR ? 1 : 0;
info->PackedInfo |= Flag << 0;
info->PackedInfo |= (FuncLength & 0x7FF) << 2;
info->PackedInfo |= (RegF & 0x7) << 13;
info->PackedInfo |= (RegI & 0xF) << 16;
info->PackedInfo |= (H & 0x1) << 20;
info->PackedInfo |= (CR & 0x3) << 21;
info->PackedInfo |= (FrameSize & 0x1FF) << 23;
return true;
}
static void ARM64EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info,
bool TryPacked = true) {
if (info->Symbol)
return;
if (info->empty()) {
info->EmitAttempted = true;
return;
}
if (info->EmitAttempted) {
streamer.getContext().reportError(
SMLoc(), "Earlier .seh_handlerdata for " + info->Function->getName() +
" skipped due to no unwind info at the time "
"(.seh_handlerdata too early?), but the function later "
"did get unwind info that can't be emitted");
return;
}
simplifyARM64Opcodes(info->Instructions, false);
for (auto &I : info->EpilogMap)
simplifyARM64Opcodes(I.second.Instructions, true);
MCContext &context = streamer.getContext();
MCSymbol *Label = context.createTempSymbol();
streamer.emitValueToAlignment(4);
streamer.emitLabel(Label);
info->Symbol = Label;
int64_t RawFuncLength;
if (!info->FuncletOrFuncEnd) {
report_fatal_error("FuncletOrFuncEnd not set");
} else {
RawFuncLength = GetAbsDifference(streamer, info->FuncletOrFuncEnd,
info->Begin);
}
if (RawFuncLength > 0xFFFFF)
report_fatal_error("SEH unwind data splitting not yet implemented");
uint32_t FuncLength = (uint32_t)RawFuncLength / 4;
uint32_t PrologCodeBytes = ARM64CountOfUnwindCodes(info->Instructions);
uint32_t TotalCodeBytes = PrologCodeBytes;
int PackedEpilogOffset =
checkARM64PackedEpilog(streamer, info, PrologCodeBytes);
if (PackedEpilogOffset >= 0 &&
uint32_t(PackedEpilogOffset) < PrologCodeBytes &&
!info->HandlesExceptions && FuncLength <= 0x7ff && TryPacked) {
if (tryARM64PackedUnwind(info, FuncLength, PackedEpilogOffset))
return;
}
MapVector<MCSymbol *, uint32_t> EpilogInfo;
std::vector<MCSymbol *> AddedEpilogs;
for (auto &I : info->EpilogMap) {
MCSymbol *EpilogStart = I.first;
auto &EpilogInstrs = I.second.Instructions;
uint32_t CodeBytes = ARM64CountOfUnwindCodes(EpilogInstrs);
MCSymbol* MatchingEpilog =
FindMatchingEpilog(EpilogInstrs, AddedEpilogs, info);
int PrologOffset;
if (MatchingEpilog) {
assert(EpilogInfo.find(MatchingEpilog) != EpilogInfo.end() &&
"Duplicate epilog not found");
EpilogInfo[EpilogStart] = EpilogInfo.lookup(MatchingEpilog);
EpilogInstrs.clear();
} else if ((PrologOffset = getARM64OffsetInProlog(info->Instructions,
EpilogInstrs)) >= 0) {
EpilogInfo[EpilogStart] = PrologOffset;
EpilogInstrs.clear();
} else {
EpilogInfo[EpilogStart] = TotalCodeBytes;
TotalCodeBytes += CodeBytes;
AddedEpilogs.push_back(EpilogStart);
}
}
uint32_t row1 = 0x0;
uint32_t CodeWords = TotalCodeBytes / 4;
uint32_t CodeWordsMod = TotalCodeBytes % 4;
if (CodeWordsMod)
CodeWords++;
uint32_t EpilogCount =
PackedEpilogOffset >= 0 ? PackedEpilogOffset : info->EpilogMap.size();
bool ExtensionWord = EpilogCount > 31 || TotalCodeBytes > 124;
if (!ExtensionWord) {
row1 |= (EpilogCount & 0x1F) << 22;
row1 |= (CodeWords & 0x1F) << 27;
}
if (info->HandlesExceptions) row1 |= 1 << 20;
if (PackedEpilogOffset >= 0) row1 |= 1 << 21;
row1 |= FuncLength & 0x3FFFF;
streamer.emitInt32(row1);
if (ExtensionWord) {
if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
report_fatal_error("SEH unwind data splitting not yet implemented");
uint32_t row2 = 0x0;
row2 |= (CodeWords & 0xFF) << 16;
row2 |= (EpilogCount & 0xFFFF);
streamer.emitInt32(row2);
}
if (PackedEpilogOffset < 0) {
for (auto &I : EpilogInfo) {
MCSymbol *EpilogStart = I.first;
uint32_t EpilogIndex = I.second;
uint32_t EpilogOffset =
(uint32_t)GetAbsDifference(streamer, EpilogStart, info->Begin);
if (EpilogOffset)
EpilogOffset /= 4;
uint32_t row3 = EpilogOffset;
row3 |= (EpilogIndex & 0x3FF) << 22;
streamer.emitInt32(row3);
}
}
uint8_t numInst = info->Instructions.size();
for (uint8_t c = 0; c < numInst; ++c) {
WinEH::Instruction inst = info->Instructions.back();
info->Instructions.pop_back();
ARM64EmitUnwindCode(streamer, inst);
}
for (auto &I : info->EpilogMap) {
auto &EpilogInstrs = I.second.Instructions;
for (const WinEH::Instruction &inst : EpilogInstrs)
ARM64EmitUnwindCode(streamer, inst);
}
int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
assert(BytesMod >= 0);
for (int i = 0; i < BytesMod; i++)
streamer.emitInt8(0xE3);
if (info->HandlesExceptions)
streamer.emitValue(
MCSymbolRefExpr::create(info->ExceptionHandler,
MCSymbolRefExpr::VK_COFF_IMGREL32, context),
4);
}
static uint32_t ARMCountOfUnwindCodes(ArrayRef<WinEH::Instruction> Insns) {
uint32_t Count = 0;
for (const auto &I : Insns) {
switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
default:
llvm_unreachable("Unsupported ARM unwind code");
case Win64EH::UOP_AllocSmall:
Count += 1;
break;
case Win64EH::UOP_AllocLarge:
Count += 3;
break;
case Win64EH::UOP_AllocHuge:
Count += 4;
break;
case Win64EH::UOP_WideAllocMedium:
Count += 2;
break;
case Win64EH::UOP_WideAllocLarge:
Count += 3;
break;
case Win64EH::UOP_WideAllocHuge:
Count += 4;
break;
case Win64EH::UOP_WideSaveRegMask:
Count += 2;
break;
case Win64EH::UOP_SaveSP:
Count += 1;
break;
case Win64EH::UOP_SaveRegsR4R7LR:
Count += 1;
break;
case Win64EH::UOP_WideSaveRegsR4R11LR:
Count += 1;
break;
case Win64EH::UOP_SaveFRegD8D15:
Count += 1;
break;
case Win64EH::UOP_SaveRegMask:
Count += 2;
break;
case Win64EH::UOP_SaveLR:
Count += 2;
break;
case Win64EH::UOP_SaveFRegD0D15:
Count += 2;
break;
case Win64EH::UOP_SaveFRegD16D31:
Count += 2;
break;
case Win64EH::UOP_Nop:
case Win64EH::UOP_WideNop:
case Win64EH::UOP_End:
case Win64EH::UOP_EndNop:
case Win64EH::UOP_WideEndNop:
Count += 1;
break;
case Win64EH::UOP_Custom: {
int J;
for (J = 3; J > 0; J--)
if (I.Offset & (0xffu << (8 * J)))
break;
Count += J + 1;
break;
}
}
}
return Count;
}
static uint32_t ARMCountOfInstructionBytes(ArrayRef<WinEH::Instruction> Insns,
bool *HasCustom = nullptr) {
uint32_t Count = 0;
for (const auto &I : Insns) {
switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
default:
llvm_unreachable("Unsupported ARM unwind code");
case Win64EH::UOP_AllocSmall:
case Win64EH::UOP_AllocLarge:
case Win64EH::UOP_AllocHuge:
Count += 2;
break;
case Win64EH::UOP_WideAllocMedium:
case Win64EH::UOP_WideAllocLarge:
case Win64EH::UOP_WideAllocHuge:
Count += 4;
break;
case Win64EH::UOP_WideSaveRegMask:
case Win64EH::UOP_WideSaveRegsR4R11LR:
Count += 4;
break;
case Win64EH::UOP_SaveSP:
Count += 2;
break;
case Win64EH::UOP_SaveRegMask:
case Win64EH::UOP_SaveRegsR4R7LR:
Count += 2;
break;
case Win64EH::UOP_SaveFRegD8D15:
case Win64EH::UOP_SaveFRegD0D15:
case Win64EH::UOP_SaveFRegD16D31:
Count += 4;
break;
case Win64EH::UOP_SaveLR:
Count += 4;
break;
case Win64EH::UOP_Nop:
case Win64EH::UOP_EndNop:
Count += 2;
break;
case Win64EH::UOP_WideNop:
case Win64EH::UOP_WideEndNop:
Count += 4;
break;
case Win64EH::UOP_End:
break;
case Win64EH::UOP_Custom:
Count += 1000;
if (HasCustom)
*HasCustom = true;
break;
}
}
return Count;
}
static void checkARMInstructions(MCStreamer &Streamer,
ArrayRef<WinEH::Instruction> Insns,
const MCSymbol *Begin, const MCSymbol *End,
StringRef Name, StringRef Type) {
if (!End)
return;
Optional<int64_t> MaybeDistance =
GetOptionalAbsDifference(Streamer, End, Begin);
if (!MaybeDistance)
return;
uint32_t Distance = (uint32_t)*MaybeDistance;
bool HasCustom = false;
uint32_t InstructionBytes = ARMCountOfInstructionBytes(Insns, &HasCustom);
if (HasCustom)
return;
if (Distance != InstructionBytes) {
Streamer.getContext().reportError(
SMLoc(), "Incorrect size for " + Name + " " + Type + ": " +
Twine(Distance) +
" bytes of instructions in range, but .seh directives "
"corresponding to " +
Twine(InstructionBytes) + " bytes\n");
}
}
static bool isARMTerminator(const WinEH::Instruction &inst) {
switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
case Win64EH::UOP_End:
case Win64EH::UOP_EndNop:
case Win64EH::UOP_WideEndNop:
return true;
default:
return false;
}
}
static void ARMEmitUnwindCode(MCStreamer &streamer,
const WinEH::Instruction &inst) {
uint32_t w, lr;
int i;
switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
default:
llvm_unreachable("Unsupported ARM unwind code");
case Win64EH::UOP_AllocSmall:
assert((inst.Offset & 3) == 0);
assert(inst.Offset / 4 <= 0x7f);
streamer.emitInt8(inst.Offset / 4);
break;
case Win64EH::UOP_WideSaveRegMask:
assert((inst.Register & ~0x5fff) == 0);
lr = (inst.Register >> 14) & 1;
w = 0x8000 | (inst.Register & 0x1fff) | (lr << 13);
streamer.emitInt8((w >> 8) & 0xff);
streamer.emitInt8((w >> 0) & 0xff);
break;
case Win64EH::UOP_SaveSP:
assert(inst.Register <= 0x0f);
streamer.emitInt8(0xc0 | inst.Register);
break;
case Win64EH::UOP_SaveRegsR4R7LR:
assert(inst.Register >= 4 && inst.Register <= 7);
assert(inst.Offset <= 1);
streamer.emitInt8(0xd0 | (inst.Register - 4) | (inst.Offset << 2));
break;
case Win64EH::UOP_WideSaveRegsR4R11LR:
assert(inst.Register >= 8 && inst.Register <= 11);
assert(inst.Offset <= 1);
streamer.emitInt8(0xd8 | (inst.Register - 8) | (inst.Offset << 2));
break;
case Win64EH::UOP_SaveFRegD8D15:
assert(inst.Register >= 8 && inst.Register <= 15);
streamer.emitInt8(0xe0 | (inst.Register - 8));
break;
case Win64EH::UOP_WideAllocMedium:
assert((inst.Offset & 3) == 0);
assert(inst.Offset / 4 <= 0x3ff);
w = 0xe800 | (inst.Offset / 4);
streamer.emitInt8((w >> 8) & 0xff);
streamer.emitInt8((w >> 0) & 0xff);
break;
case Win64EH::UOP_SaveRegMask:
assert((inst.Register & ~0x40ff) == 0);
lr = (inst.Register >> 14) & 1;
w = 0xec00 | (inst.Register & 0x0ff) | (lr << 8);
streamer.emitInt8((w >> 8) & 0xff);
streamer.emitInt8((w >> 0) & 0xff);
break;
case Win64EH::UOP_SaveLR:
assert((inst.Offset & 3) == 0);
assert(inst.Offset / 4 <= 0x0f);
streamer.emitInt8(0xef);
streamer.emitInt8(inst.Offset / 4);
break;
case Win64EH::UOP_SaveFRegD0D15:
assert(inst.Register <= 15);
assert(inst.Offset <= 15);
assert(inst.Register <= inst.Offset);
streamer.emitInt8(0xf5);
streamer.emitInt8((inst.Register << 4) | inst.Offset);
break;
case Win64EH::UOP_SaveFRegD16D31:
assert(inst.Register >= 16 && inst.Register <= 31);
assert(inst.Offset >= 16 && inst.Offset <= 31);
assert(inst.Register <= inst.Offset);
streamer.emitInt8(0xf6);
streamer.emitInt8(((inst.Register - 16) << 4) | (inst.Offset - 16));
break;
case Win64EH::UOP_AllocLarge:
assert((inst.Offset & 3) == 0);
assert(inst.Offset / 4 <= 0xffff);
w = inst.Offset / 4;
streamer.emitInt8(0xf7);
streamer.emitInt8((w >> 8) & 0xff);
streamer.emitInt8((w >> 0) & 0xff);
break;
case Win64EH::UOP_AllocHuge:
assert((inst.Offset & 3) == 0);
assert(inst.Offset / 4 <= 0xffffff);
w = inst.Offset / 4;
streamer.emitInt8(0xf8);
streamer.emitInt8((w >> 16) & 0xff);
streamer.emitInt8((w >> 8) & 0xff);
streamer.emitInt8((w >> 0) & 0xff);
break;
case Win64EH::UOP_WideAllocLarge:
assert((inst.Offset & 3) == 0);
assert(inst.Offset / 4 <= 0xffff);
w = inst.Offset / 4;
streamer.emitInt8(0xf9);
streamer.emitInt8((w >> 8) & 0xff);
streamer.emitInt8((w >> 0) & 0xff);
break;
case Win64EH::UOP_WideAllocHuge:
assert((inst.Offset & 3) == 0);
assert(inst.Offset / 4 <= 0xffffff);
w = inst.Offset / 4;
streamer.emitInt8(0xfa);
streamer.emitInt8((w >> 16) & 0xff);
streamer.emitInt8((w >> 8) & 0xff);
streamer.emitInt8((w >> 0) & 0xff);
break;
case Win64EH::UOP_Nop:
streamer.emitInt8(0xfb);
break;
case Win64EH::UOP_WideNop:
streamer.emitInt8(0xfc);
break;
case Win64EH::UOP_EndNop:
streamer.emitInt8(0xfd);
break;
case Win64EH::UOP_WideEndNop:
streamer.emitInt8(0xfe);
break;
case Win64EH::UOP_End:
streamer.emitInt8(0xff);
break;
case Win64EH::UOP_Custom:
for (i = 3; i > 0; i--)
if (inst.Offset & (0xffu << (8 * i)))
break;
for (; i >= 0; i--)
streamer.emitInt8((inst.Offset >> (8 * i)) & 0xff);
break;
}
}
static int getARMOffsetInProlog(const std::vector<WinEH::Instruction> &Prolog,
const std::vector<WinEH::Instruction> &Epilog,
bool CanTweakProlog) {
if (Epilog.size() > Prolog.size())
return -1;
int EndIdx = CanTweakProlog ? 1 : 0;
for (int I = Epilog.size() - 1; I >= EndIdx; I--) {
if (Prolog[I] != Epilog[Epilog.size() - 1 - I])
return -1;
}
if (CanTweakProlog) {
if (Prolog.front().Operation != Win64EH::UOP_End)
return -1;
if (Epilog.back().Operation != Win64EH::UOP_End &&
Epilog.back().Operation != Win64EH::UOP_EndNop &&
Epilog.back().Operation != Win64EH::UOP_WideEndNop)
return -1;
}
if (Epilog.size() == Prolog.size())
return 0;
return ARMCountOfUnwindCodes(ArrayRef<WinEH::Instruction>(
&Prolog[Epilog.size()], Prolog.size() - Epilog.size()));
}
static int checkARMPackedEpilog(MCStreamer &streamer, WinEH::FrameInfo *info,
int PrologCodeBytes) {
if (info->EpilogMap.size() != 1)
return -1;
const WinEH::FrameInfo::Epilog &EpilogInfo = info->EpilogMap.begin()->second;
if (EpilogInfo.Condition != 0xe) return -1;
const std::vector<WinEH::Instruction> &Epilog = EpilogInfo.Instructions;
if (info->Instructions.empty() || Epilog.empty())
return -1;
Optional<int64_t> MaybeDistance = GetOptionalAbsDifference(
streamer, info->FuncletOrFuncEnd, info->EpilogMap.begin()->first);
if (!MaybeDistance)
return -1;
uint32_t DistanceFromEnd = (uint32_t)*MaybeDistance;
uint32_t InstructionBytes = ARMCountOfInstructionBytes(Epilog);
if (DistanceFromEnd != InstructionBytes)
return -1;
int RetVal = -1;
if (PrologCodeBytes <= 31 &&
PrologCodeBytes + ARMCountOfUnwindCodes(Epilog) <= 63)
RetVal = PrologCodeBytes;
int Offset =
getARMOffsetInProlog(info->Instructions, Epilog, true);
if (Offset < 0)
return RetVal;
if (Offset > 31 || PrologCodeBytes > 63)
return RetVal;
info->Instructions.front() = Epilog.back();
info->EpilogMap.clear();
return Offset;
}
static bool parseRegMask(unsigned Mask, bool &HasLR, bool &HasR11,
unsigned &Folded, int &IntRegs) {
if (Mask & (1 << 14)) {
HasLR = true;
Mask &= ~(1 << 14);
}
if (Mask & (1 << 11)) {
HasR11 = true;
Mask &= ~(1 << 11);
}
Folded = 0;
IntRegs = -1;
if (!Mask)
return true;
int First = 0;
while ((Mask & 1) == 0) {
First++;
Mask >>= 1;
}
if ((Mask & (Mask + 1)) != 0)
return false; int N = 0;
while (Mask & (1 << N))
N++;
if (First < 4) {
if (First + N < 4)
return false;
Folded = 4 - First;
N -= Folded;
First = 4;
}
if (First > 4)
return false; if (N >= 1)
IntRegs = N - 1;
return true;
}
static bool tryARMPackedUnwind(MCStreamer &streamer, WinEH::FrameInfo *info,
uint32_t FuncLength) {
int Step = 0;
bool Homing = false;
bool HasR11 = false;
bool HasChain = false;
bool HasLR = false;
int IntRegs = -1; int FloatRegs = -1; unsigned PF = 0; unsigned StackAdjust = 0;
for (const WinEH::Instruction &Inst : info->Instructions) {
switch (Inst.Operation) {
default:
llvm_unreachable("Unsupported ARM unwind code");
case Win64EH::UOP_Custom:
case Win64EH::UOP_AllocLarge:
case Win64EH::UOP_AllocHuge:
case Win64EH::UOP_WideAllocLarge:
case Win64EH::UOP_WideAllocHuge:
case Win64EH::UOP_SaveFRegD0D15:
case Win64EH::UOP_SaveFRegD16D31:
return false;
case Win64EH::UOP_SaveSP:
return false;
case Win64EH::UOP_SaveLR:
return false;
case Win64EH::UOP_End:
case Win64EH::UOP_EndNop:
case Win64EH::UOP_WideEndNop:
if (Step != 0)
return false;
Step = 1;
break;
case Win64EH::UOP_SaveRegsR4R7LR:
case Win64EH::UOP_WideSaveRegsR4R11LR:
if (Step != 1 && Step != 2)
return false;
assert(Inst.Register >= 4 && Inst.Register <= 11); assert(Inst.Offset <= 1); IntRegs = Inst.Register - 4;
if (Inst.Register == 11) {
HasR11 = true;
IntRegs--;
}
if (Inst.Offset)
HasLR = true;
Step = 3;
break;
case Win64EH::UOP_SaveRegMask:
if (Step == 1 && Inst.Register == 0x0f) {
Homing = true;
Step = 2;
break;
}
LLVM_FALLTHROUGH;
case Win64EH::UOP_WideSaveRegMask:
if (Step != 1 && Step != 2)
return false;
if (!parseRegMask(Inst.Register, HasLR, HasR11, PF, IntRegs))
return false;
Step = 3;
break;
case Win64EH::UOP_Nop:
if (Step != 3 || !HasR11 || IntRegs >= 0 || PF > 0)
return false;
HasChain = true;
Step = 4;
break;
case Win64EH::UOP_WideNop:
if (Step != 3 || !HasR11 || (IntRegs < 0 && PF == 0))
return false;
HasChain = true;
Step = 4;
break;
case Win64EH::UOP_SaveFRegD8D15:
if (Step != 1 && Step != 2 && Step != 3 && Step != 4)
return false;
assert(Inst.Register >= 8 && Inst.Register <= 15);
if (Inst.Register == 15)
return false; if (IntRegs >= 0)
return false;
FloatRegs = Inst.Register - 8;
Step = 5;
break;
case Win64EH::UOP_AllocSmall:
case Win64EH::UOP_WideAllocMedium:
if (Step != 1 && Step != 2 && Step != 3 && Step != 4 && Step != 5)
return false;
if (PF > 0) return false;
if (Inst.Offset / 4 >= 0x3f4)
return false;
StackAdjust = Inst.Offset / 4;
Step = 6;
break;
}
}
if (HasR11 && !HasChain) {
if (IntRegs + 4 == 10) {
IntRegs++;
HasR11 = false;
} else
return false;
}
if (HasChain && !HasLR)
return false;
if (info->EpilogMap.size() > 1)
return false;
unsigned EF = 0;
int Ret = 0;
if (info->EpilogMap.size() == 0) {
Ret = 3; } else {
const WinEH::FrameInfo::Epilog &EpilogInfo =
info->EpilogMap.begin()->second;
if (EpilogInfo.Condition != 0xe) return false;
const std::vector<WinEH::Instruction> &Epilog = EpilogInfo.Instructions;
Optional<int64_t> MaybeDistance = GetOptionalAbsDifference(
streamer, info->FuncletOrFuncEnd, info->EpilogMap.begin()->first);
if (!MaybeDistance)
return false;
uint32_t DistanceFromEnd = (uint32_t)*MaybeDistance;
uint32_t InstructionBytes = ARMCountOfInstructionBytes(Epilog);
if (DistanceFromEnd != InstructionBytes)
return false;
bool GotStackAdjust = false;
bool GotFloatRegs = false;
bool GotIntRegs = false;
bool GotHomingRestore = false;
bool GotLRRestore = false;
bool NeedsReturn = false;
bool GotReturn = false;
Step = 6;
for (const WinEH::Instruction &Inst : Epilog) {
switch (Inst.Operation) {
default:
llvm_unreachable("Unsupported ARM unwind code");
case Win64EH::UOP_Custom:
case Win64EH::UOP_AllocLarge:
case Win64EH::UOP_AllocHuge:
case Win64EH::UOP_WideAllocLarge:
case Win64EH::UOP_WideAllocHuge:
case Win64EH::UOP_SaveFRegD0D15:
case Win64EH::UOP_SaveFRegD16D31:
case Win64EH::UOP_SaveSP:
case Win64EH::UOP_Nop:
case Win64EH::UOP_WideNop:
return false;
case Win64EH::UOP_AllocSmall:
case Win64EH::UOP_WideAllocMedium:
if (Inst.Offset / 4 >= 0x3f4)
return false;
if (Step == 6) {
if (Homing && FloatRegs < 0 && IntRegs < 0 && StackAdjust == 0 &&
PF == 0 && Inst.Offset == 16) {
GotHomingRestore = true;
Step = 10;
} else {
if (StackAdjust > 0) {
if (StackAdjust != Inst.Offset / 4)
return false;
GotStackAdjust = true;
} else if (PF == Inst.Offset / 4) {
StackAdjust = Inst.Offset / 4;
GotStackAdjust = true;
} else {
return false;
}
Step = 7;
}
} else if (Step == 7 || Step == 8 || Step == 9) {
if (!Homing || Inst.Offset != 16)
return false;
GotHomingRestore = true;
Step = 10;
} else
return false;
break;
case Win64EH::UOP_SaveFRegD8D15:
if (Step != 6 && Step != 7)
return false;
assert(Inst.Register >= 8 && Inst.Register <= 15);
if (FloatRegs != (int)(Inst.Register - 8))
return false;
GotFloatRegs = true;
Step = 8;
break;
case Win64EH::UOP_SaveRegsR4R7LR:
case Win64EH::UOP_WideSaveRegsR4R11LR: {
if (Step != 6 && Step != 7 && Step != 8)
return false;
assert(Inst.Register >= 4 && Inst.Register <= 11); assert(Inst.Offset <= 1); if (Homing && HasLR) {
if (Inst.Offset) {
GotLRRestore = true;
NeedsReturn = true;
} else {
}
} else {
if (HasLR != (Inst.Offset == 1))
return false;
}
GotLRRestore = Inst.Offset == 1;
if (IntRegs < 0) return false;
int Expected = IntRegs;
if (HasChain) {
if (IntRegs != 6)
return false;
Expected++;
}
if (Expected != (int)(Inst.Register - 4))
return false;
GotIntRegs = true;
Step = 9;
break;
}
case Win64EH::UOP_SaveRegMask:
case Win64EH::UOP_WideSaveRegMask: {
if (Step != 6 && Step != 7 && Step != 8)
return false;
bool CurHasLR = false, CurHasR11 = false;
int Regs;
if (!parseRegMask(Inst.Register, CurHasLR, CurHasR11, EF, Regs))
return false;
if (EF > 0) {
if (EF != PF && EF != StackAdjust)
return false;
}
if (Homing && HasLR) {
if (CurHasLR) {
GotLRRestore = true;
NeedsReturn = true;
} else {
}
} else {
if (CurHasLR != HasLR)
return false;
GotLRRestore = CurHasLR;
}
int Expected = IntRegs;
if (HasChain) {
if (!CurHasR11)
return false;
} else if (Expected == 7) {
Expected--;
if (!CurHasR11)
return false;
} else {
if (CurHasR11)
return false;
}
if (Expected != Regs)
return false;
GotIntRegs = true;
Step = 9;
break;
}
case Win64EH::UOP_SaveLR:
if (Step != 6 && Step != 7 && Step != 8 && Step != 9)
return false;
if (!Homing || Inst.Offset != 20 || GotLRRestore)
return false;
GotLRRestore = true;
GotHomingRestore = true;
Step = 10;
break;
case Win64EH::UOP_EndNop:
case Win64EH::UOP_WideEndNop:
GotReturn = true;
Ret = (Inst.Operation == Win64EH::UOP_EndNop) ? 1 : 2;
LLVM_FALLTHROUGH;
case Win64EH::UOP_End:
if (Step != 6 && Step != 7 && Step != 8 && Step != 9 && Step != 10)
return false;
Step = 11;
break;
}
}
if (Step != 11)
return false;
if (StackAdjust > 0 && !GotStackAdjust && EF == 0)
return false;
if (FloatRegs >= 0 && !GotFloatRegs)
return false;
if (IntRegs >= 0 && !GotIntRegs)
return false;
if (Homing && !GotHomingRestore)
return false;
if (HasLR && !GotLRRestore)
return false;
if (NeedsReturn && !GotReturn)
return false;
}
assert(PF == 0 || EF == 0 ||
StackAdjust == 0); if (PF > 0 || EF > 0) {
StackAdjust = PF > 0 ? (PF - 1) : (EF - 1);
assert(StackAdjust <= 3);
StackAdjust |= 0x3f0;
if (PF > 0)
StackAdjust |= 1 << 2;
if (EF > 0)
StackAdjust |= 1 << 3;
}
assert(FuncLength <= 0x7FF && "FuncLength should have been checked earlier");
int Flag = info->Fragment ? 0x02 : 0x01;
int H = Homing ? 1 : 0;
int L = HasLR ? 1 : 0;
int C = HasChain ? 1 : 0;
assert(IntRegs < 0 || FloatRegs < 0);
unsigned Reg, R;
if (IntRegs >= 0) {
Reg = IntRegs;
assert(Reg <= 7);
R = 0;
} else if (FloatRegs >= 0) {
Reg = FloatRegs;
assert(Reg < 7);
R = 1;
} else {
Reg = 7;
R = 1;
}
info->PackedInfo |= Flag << 0;
info->PackedInfo |= (FuncLength & 0x7FF) << 2;
info->PackedInfo |= (Ret & 0x3) << 13;
info->PackedInfo |= H << 15;
info->PackedInfo |= Reg << 16;
info->PackedInfo |= R << 19;
info->PackedInfo |= L << 20;
info->PackedInfo |= C << 21;
assert(StackAdjust <= 0x3ff);
info->PackedInfo |= StackAdjust << 22;
return true;
}
static void ARMEmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info,
bool TryPacked = true) {
if (info->Symbol)
return;
if (info->empty()) {
info->EmitAttempted = true;
return;
}
if (info->EmitAttempted) {
streamer.getContext().reportError(
SMLoc(), "Earlier .seh_handlerdata for " + info->Function->getName() +
" skipped due to no unwind info at the time "
"(.seh_handlerdata too early?), but the function later "
"did get unwind info that can't be emitted");
return;
}
MCContext &context = streamer.getContext();
MCSymbol *Label = context.createTempSymbol();
streamer.emitValueToAlignment(4);
streamer.emitLabel(Label);
info->Symbol = Label;
if (!info->PrologEnd)
streamer.getContext().reportError(SMLoc(), "Prologue in " +
info->Function->getName() +
" not correctly terminated");
if (info->PrologEnd && !info->Fragment)
checkARMInstructions(streamer, info->Instructions, info->Begin,
info->PrologEnd, info->Function->getName(),
"prologue");
for (auto &I : info->EpilogMap) {
MCSymbol *EpilogStart = I.first;
auto &Epilog = I.second;
checkARMInstructions(streamer, Epilog.Instructions, EpilogStart, Epilog.End,
info->Function->getName(), "epilogue");
if (Epilog.Instructions.empty() ||
!isARMTerminator(Epilog.Instructions.back()))
streamer.getContext().reportError(
SMLoc(), "Epilogue in " + info->Function->getName() +
" not correctly terminated");
}
Optional<int64_t> RawFuncLength;
const MCExpr *FuncLengthExpr = nullptr;
if (!info->FuncletOrFuncEnd) {
report_fatal_error("FuncletOrFuncEnd not set");
} else {
RawFuncLength =
GetOptionalAbsDifference(streamer, info->FuncletOrFuncEnd, info->Begin);
if (!RawFuncLength)
FuncLengthExpr =
GetSubDivExpr(streamer, info->FuncletOrFuncEnd, info->Begin, 2);
}
uint32_t FuncLength = 0;
if (RawFuncLength)
FuncLength = (uint32_t)*RawFuncLength / 2;
if (FuncLength > 0x3FFFF)
report_fatal_error("SEH unwind data splitting not yet implemented");
uint32_t PrologCodeBytes = ARMCountOfUnwindCodes(info->Instructions);
uint32_t TotalCodeBytes = PrologCodeBytes;
if (!info->HandlesExceptions && RawFuncLength && FuncLength <= 0x7ff &&
TryPacked) {
if (tryARMPackedUnwind(streamer, info, FuncLength))
return;
}
int PackedEpilogOffset =
checkARMPackedEpilog(streamer, info, PrologCodeBytes);
MapVector<MCSymbol *, uint32_t> EpilogInfo;
std::vector<MCSymbol *> AddedEpilogs;
bool CanTweakProlog = true;
for (auto &I : info->EpilogMap) {
MCSymbol *EpilogStart = I.first;
auto &EpilogInstrs = I.second.Instructions;
uint32_t CodeBytes = ARMCountOfUnwindCodes(EpilogInstrs);
MCSymbol *MatchingEpilog =
FindMatchingEpilog(EpilogInstrs, AddedEpilogs, info);
int PrologOffset;
if (MatchingEpilog) {
assert(EpilogInfo.find(MatchingEpilog) != EpilogInfo.end() &&
"Duplicate epilog not found");
EpilogInfo[EpilogStart] = EpilogInfo.lookup(MatchingEpilog);
EpilogInstrs.clear();
} else if ((PrologOffset = getARMOffsetInProlog(
info->Instructions, EpilogInstrs, CanTweakProlog)) >= 0) {
if (CanTweakProlog) {
info->Instructions.front() = EpilogInstrs.back();
CanTweakProlog = false;
}
EpilogInfo[EpilogStart] = PrologOffset;
EpilogInstrs.clear();
} else {
EpilogInfo[EpilogStart] = TotalCodeBytes;
TotalCodeBytes += CodeBytes;
AddedEpilogs.push_back(EpilogStart);
}
}
uint32_t row1 = 0x0;
uint32_t CodeWords = TotalCodeBytes / 4;
uint32_t CodeWordsMod = TotalCodeBytes % 4;
if (CodeWordsMod)
CodeWords++;
uint32_t EpilogCount =
PackedEpilogOffset >= 0 ? PackedEpilogOffset : info->EpilogMap.size();
bool ExtensionWord = EpilogCount > 31 || CodeWords > 15;
if (!ExtensionWord) {
row1 |= (EpilogCount & 0x1F) << 23;
row1 |= (CodeWords & 0x0F) << 28;
}
if (info->HandlesExceptions) row1 |= 1 << 20;
if (PackedEpilogOffset >= 0) row1 |= 1 << 21;
if (info->Fragment) row1 |= 1 << 22;
row1 |= FuncLength & 0x3FFFF;
if (RawFuncLength)
streamer.emitInt32(row1);
else
streamer.emitValue(
MCBinaryExpr::createOr(FuncLengthExpr,
MCConstantExpr::create(row1, context), context),
4);
if (ExtensionWord) {
if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
report_fatal_error("SEH unwind data splitting not yet implemented");
uint32_t row2 = 0x0;
row2 |= (CodeWords & 0xFF) << 16;
row2 |= (EpilogCount & 0xFFFF);
streamer.emitInt32(row2);
}
if (PackedEpilogOffset < 0) {
for (auto &I : EpilogInfo) {
MCSymbol *EpilogStart = I.first;
uint32_t EpilogIndex = I.second;
Optional<int64_t> MaybeEpilogOffset =
GetOptionalAbsDifference(streamer, EpilogStart, info->Begin);
const MCExpr *OffsetExpr = nullptr;
uint32_t EpilogOffset = 0;
if (MaybeEpilogOffset)
EpilogOffset = *MaybeEpilogOffset / 2;
else
OffsetExpr = GetSubDivExpr(streamer, EpilogStart, info->Begin, 2);
assert(info->EpilogMap.find(EpilogStart) != info->EpilogMap.end());
unsigned Condition = info->EpilogMap[EpilogStart].Condition;
assert(Condition <= 0xf);
uint32_t row3 = EpilogOffset;
row3 |= Condition << 20;
row3 |= (EpilogIndex & 0x3FF) << 24;
if (MaybeEpilogOffset)
streamer.emitInt32(row3);
else
streamer.emitValue(
MCBinaryExpr::createOr(
OffsetExpr, MCConstantExpr::create(row3, context), context),
4);
}
}
uint8_t numInst = info->Instructions.size();
for (uint8_t c = 0; c < numInst; ++c) {
WinEH::Instruction inst = info->Instructions.back();
info->Instructions.pop_back();
ARMEmitUnwindCode(streamer, inst);
}
for (auto &I : info->EpilogMap) {
auto &EpilogInstrs = I.second.Instructions;
for (uint32_t i = 0; i < EpilogInstrs.size(); i++) {
WinEH::Instruction inst = EpilogInstrs[i];
ARMEmitUnwindCode(streamer, inst);
}
}
int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
assert(BytesMod >= 0);
for (int i = 0; i < BytesMod; i++)
streamer.emitInt8(0xFB);
if (info->HandlesExceptions)
streamer.emitValue(
MCSymbolRefExpr::create(info->ExceptionHandler,
MCSymbolRefExpr::VK_COFF_IMGREL32, context),
4);
}
static void ARMEmitRuntimeFunction(MCStreamer &streamer,
const WinEH::FrameInfo *info) {
MCContext &context = streamer.getContext();
streamer.emitValueToAlignment(4);
EmitSymbolRefWithOfs(streamer, info->Begin, info->Begin);
if (info->PackedInfo)
streamer.emitInt32(info->PackedInfo);
else
streamer.emitValue(
MCSymbolRefExpr::create(info->Symbol, MCSymbolRefExpr::VK_COFF_IMGREL32,
context),
4);
}
void llvm::Win64EH::ARM64UnwindEmitter::Emit(MCStreamer &Streamer) const {
for (const auto &CFI : Streamer.getWinFrameInfos()) {
WinEH::FrameInfo *Info = CFI.get();
if (Info->empty())
continue;
MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
Streamer.switchSection(XData);
ARM64EmitUnwindInfo(Streamer, Info);
}
for (const auto &CFI : Streamer.getWinFrameInfos()) {
WinEH::FrameInfo *Info = CFI.get();
if (!Info->Symbol)
continue;
MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
Streamer.switchSection(PData);
ARMEmitRuntimeFunction(Streamer, Info);
}
}
void llvm::Win64EH::ARM64UnwindEmitter::EmitUnwindInfo(MCStreamer &Streamer,
WinEH::FrameInfo *info,
bool HandlerData) const {
if (!info->FuncletOrFuncEnd) {
Streamer.switchSection(info->TextSection);
info->FuncletOrFuncEnd = Streamer.emitCFILabel();
}
MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
Streamer.switchSection(XData);
ARM64EmitUnwindInfo(Streamer, info, !HandlerData);
}
void llvm::Win64EH::ARMUnwindEmitter::Emit(MCStreamer &Streamer) const {
for (const auto &CFI : Streamer.getWinFrameInfos()) {
WinEH::FrameInfo *Info = CFI.get();
if (Info->empty())
continue;
MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
Streamer.switchSection(XData);
ARMEmitUnwindInfo(Streamer, Info);
}
for (const auto &CFI : Streamer.getWinFrameInfos()) {
WinEH::FrameInfo *Info = CFI.get();
if (!Info->Symbol)
continue;
MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
Streamer.switchSection(PData);
ARMEmitRuntimeFunction(Streamer, Info);
}
}
void llvm::Win64EH::ARMUnwindEmitter::EmitUnwindInfo(MCStreamer &Streamer,
WinEH::FrameInfo *info,
bool HandlerData) const {
if (!info->FuncletOrFuncEnd) {
Streamer.switchSection(info->TextSection);
info->FuncletOrFuncEnd = Streamer.emitCFILabel();
}
MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
Streamer.switchSection(XData);
ARMEmitUnwindInfo(Streamer, info, !HandlerData);
}