Skip to content

Commit

Permalink
Merge pull request llvm#39 from lateralusX/lateralusX/fix-dwarf-cfi-w…
Browse files Browse the repository at this point in the history
…indows-x64

Fix correct Dwarf CFI for Windows x64.
  • Loading branch information
lateralusX authored and imhameed committed Jun 2, 2021
1 parent edcae59 commit 9d7d3f8
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions llvm/lib/Target/X86/X86FrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,7 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,

uint64_t NumBytes = 0;
int stackGrowth = -SlotSize;
int StackOffset = 2 * stackGrowth;

// Find the funclet establisher parameter
Register Establisher = X86::NoRegister;
Expand Down Expand Up @@ -1489,12 +1490,14 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
// Define the current CFA rule to use the provided offset.
assert(StackSize);
BuildCFI(MBB, MBBI, DL,
MCCFIInstruction::cfiDefCfaOffset(nullptr, -2 * stackGrowth));
MCCFIInstruction::cfiDefCfaOffset(nullptr, -StackOffset));

// Change the rule for the FramePtr to be an "offset" rule.
unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true);
BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createOffset(
nullptr, DwarfFramePtr, 2 * stackGrowth));
nullptr, DwarfFramePtr, StackOffset));

StackOffset += stackGrowth;
}

if (NeedsWinCFI) {
Expand Down Expand Up @@ -1551,7 +1554,6 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,

// Skip the callee-saved push instructions.
bool PushedRegs = false;
int StackOffset = 2 * stackGrowth;

while (MBBI != MBB.end() &&
MBBI->getFlag(MachineInstr::FrameSetup) &&
Expand All @@ -1561,12 +1563,19 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
Register Reg = MBBI->getOperand(0).getReg();
++MBBI;

if (!HasFP && NeedsDwarfCFI) {
if (!HasFP && NeedsDwarfCFI || (IsWin64Prologue && NeedsDwarfCFI)) {
// Mark callee-saved push instruction.
// Define the current CFA rule to use the provided offset.
assert(StackSize);
BuildCFI(MBB, MBBI, DL,
MCCFIInstruction::cfiDefCfaOffset(nullptr, -StackOffset));

if (IsWin64Prologue && NeedsDwarfCFI) {
unsigned DwarfReg= TRI->getDwarfRegNum(Reg, true);
BuildCFI(MBB, MBBI, DL,
MCCFIInstruction::createOffset(nullptr, DwarfReg, StackOffset));
}

StackOffset += stackGrowth;
}

Expand Down Expand Up @@ -1683,6 +1692,13 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
.setMIFlag(MachineInstr::FrameSetup);
}

// When emitting Dwarf CFI on Win64 account for adjustment at correct
// location in stream.
if (IsWin64Prologue && NeedsDwarfCFI && NumBytes) {
BuildCFI(MBB, MBBI, DL,
MCCFIInstruction::createAdjustCfaOffset(nullptr, NumBytes));
}

int SEHFrameOffset = 0;
unsigned SPOrEstablisher;
if (IsFunclet) {
Expand Down Expand Up @@ -1737,6 +1753,21 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
if (isAsynchronousEHPersonality(Personality))
MF.getWinEHFuncInfo()->SEHSetFrameOffset = SEHFrameOffset;
}

if (NeedsDwarfCFI && !IsFunclet) {
// Define the current CFA to use the EBP/RBP register.
unsigned DwarfFramePtr = TRI->getDwarfRegNum(FramePtr, true);
BuildCFI(MBB, MBBI, DL,
MCCFIInstruction::createDefCfaRegister(nullptr, DwarfFramePtr));

if (SEHFrameOffset) {
// Framepointer has been adjusted with an offset, make sure
// it is reflected in CFA offset.
BuildCFI(MBB, MBBI, DL,
MCCFIInstruction::createAdjustCfaOffset(nullptr, -SEHFrameOffset));
}
}

} else if (IsFunclet && STI.is32Bit()) {
// Reset EBP / ESI to something good for funclets.
MBBI = restoreWin32EHStackPointers(MBB, MBBI, DL);
Expand Down Expand Up @@ -1848,7 +1879,7 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
}
}

if (((!HasFP && NumBytes) || PushedRegs) && NeedsDwarfCFI) {
if (((!HasFP && NumBytes) || PushedRegs) && NeedsDwarfCFI && !IsWin64Prologue) {
// Mark end of stack pointer adjustment.
if (!HasFP && NumBytes) {
// Define the current CFA rule to use the provided offset.
Expand Down

0 comments on commit 9d7d3f8

Please sign in to comment.