Skip to content

Commit a1d8bc5

Browse files
committed
[PPC, SSP] Support PowerPC Linux stack protection.
llvm-svn: 266809
1 parent 8743f8c commit a1d8bc5

11 files changed

+64
-26
lines changed

llvm/include/llvm/Target/TargetLowering.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ class TargetLoweringBase {
10241024
/// Return the variable that's previously inserted by insertSSPDeclarations,
10251025
/// if any, otherwise return nullptr. Should be used only when
10261026
/// getIRStackGuard returns nullptr.
1027-
virtual Value *getSDStackGuard(const Module &M) const;
1027+
virtual Value *getSDagStackGuard(const Module &M) const;
10281028

10291029
/// If the target has a standard location for the unsafe stack pointer,
10301030
/// returns the address of that location. Otherwise, returns nullptr.

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

+11-13
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,7 @@ static SDValue getLoadStackGuard(SelectionDAG &DAG, SDLoc DL, SDValue &Chain) {
20042004
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
20052005
EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
20062006
MachineFunction &MF = DAG.getMachineFunction();
2007-
Value *Global = TLI.getSDStackGuard(*MF.getFunction()->getParent());
2007+
Value *Global = TLI.getSDagStackGuard(*MF.getFunction()->getParent());
20082008
MachineSDNode *Node =
20092009
DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
20102010
if (Global) {
@@ -2034,27 +2034,25 @@ void SelectionDAGBuilder::visitSPDescriptorParent(StackProtectorDescriptor &SPD,
20342034
MachineFrameInfo *MFI = ParentBB->getParent()->getFrameInfo();
20352035
int FI = MFI->getStackProtectorIndex();
20362036

2037-
const Module &M = *ParentBB->getParent()->getFunction()->getParent();
2038-
const Value *IRGuard = TLI.getSDStackGuard(M);
2039-
assert(IRGuard && "Currently there must be an IR guard in order to use "
2040-
"SelectionDAG SSP");
2041-
SDValue GuardPtr = getValue(IRGuard);
2042-
SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
2043-
2044-
unsigned Align = DL->getPrefTypeAlignment(IRGuard->getType());
2045-
20462037
SDValue Guard;
20472038
SDLoc dl = getCurSDLoc();
2039+
SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
2040+
const Module &M = *ParentBB->getParent()->getFunction()->getParent();
2041+
unsigned Align = DL->getPrefTypeAlignment(Type::getInt8PtrTy(M.getContext()));
20482042

20492043
// If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
20502044
// Otherwise, emit a volatile load to retrieve the stack guard value.
20512045
SDValue Chain = DAG.getEntryNode();
2052-
if (TLI.useLoadStackGuardNode())
2046+
if (TLI.useLoadStackGuardNode()) {
20532047
Guard = getLoadStackGuard(DAG, dl, Chain);
2054-
else
2048+
} else {
2049+
const Value *IRGuard = TLI.getSDagStackGuard(M);
2050+
SDValue GuardPtr = getValue(IRGuard);
2051+
20552052
Guard =
20562053
DAG.getLoad(PtrTy, dl, Chain, GuardPtr, MachinePointerInfo(IRGuard, 0),
20572054
true, false, false, Align);
2055+
}
20582056

20592057
SDValue StackSlot = DAG.getLoad(
20602058
PtrTy, dl, DAG.getEntryNode(), StackSlotPtr,
@@ -5313,7 +5311,7 @@ SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
53135311
if (TLI.useLoadStackGuardNode()) {
53145312
Res = getLoadStackGuard(DAG, sdl, Chain);
53155313
} else {
5316-
const Value *Global = TLI.getSDStackGuard(M);
5314+
const Value *Global = TLI.getSDagStackGuard(M);
53175315
unsigned Align = DL->getPrefTypeAlignment(Global->getType());
53185316
Res =
53195317
DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),

llvm/lib/CodeGen/TargetLoweringBase.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,6 @@ void TargetLoweringBase::insertSSPDeclarations(Module &M) const {
18141814

18151815
// Currently only support "standard" __stack_chk_guard.
18161816
// TODO: add LOAD_STACK_GUARD support.
1817-
Value *TargetLoweringBase::getSDStackGuard(const Module &M) const {
1817+
Value *TargetLoweringBase::getSDagStackGuard(const Module &M) const {
18181818
return M.getGlobalVariable("__stack_chk_guard");
18191819
}

llvm/lib/Target/PowerPC/PPCISelLowering.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -12013,3 +12013,16 @@ void PPCTargetLowering::insertCopiesSplitCSR(
1201312013
.addReg(NewVR);
1201412014
}
1201512015
}
12016+
12017+
// Override to enable LOAD_STACK_GUARD lowering on Linux.
12018+
bool PPCTargetLowering::useLoadStackGuardNode() const {
12019+
if (!Subtarget.isTargetLinux())
12020+
return TargetLowering::useLoadStackGuardNode();
12021+
return true;
12022+
}
12023+
12024+
// Override to disable global variable loading on Linux.
12025+
void PPCTargetLowering::insertSSPDeclarations(Module &M) const {
12026+
if (!Subtarget.isTargetLinux())
12027+
return TargetLowering::insertSSPDeclarations(M);
12028+
}

llvm/lib/Target/PowerPC/PPCISelLowering.h

+4
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,10 @@ namespace llvm {
688688
unsigned
689689
getExceptionSelectorRegister(const Constant *PersonalityFn) const override;
690690

691+
/// Override to support customized stack guard loading.
692+
bool useLoadStackGuardNode() const override;
693+
void insertSSPDeclarations(Module &M) const override;
694+
691695
private:
692696
struct ReuseLoadInfo {
693697
SDValue Ptr;

llvm/lib/Target/PowerPC/PPCInstrInfo.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -1857,3 +1857,19 @@ PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
18571857
return makeArrayRef(TargetFlags);
18581858
}
18591859

1860+
bool PPCInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1861+
switch (MI->getOpcode()) {
1862+
case TargetOpcode::LOAD_STACK_GUARD: {
1863+
assert(Subtarget.isTargetLinux() &&
1864+
"Only Linux target is expected to contain LOAD_STACK_GUARD");
1865+
const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008;
1866+
const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2;
1867+
MI->setDesc(get(PPC::LD));
1868+
MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1869+
.addImm(Offset)
1870+
.addReg(Reg);
1871+
return true;
1872+
}
1873+
}
1874+
return false;
1875+
}

llvm/lib/Target/PowerPC/PPCInstrInfo.h

+3
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ class PPCInstrInfo : public PPCGenInstrInfo {
272272

273273
ArrayRef<std::pair<unsigned, const char *>>
274274
getSerializableBitmaskMachineOperandTargetFlags() const override;
275+
276+
// Lower pseudo instructions after register allocation.
277+
bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
275278
};
276279

277280
}

llvm/lib/Target/PowerPC/PPCSubtarget.h

+1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ class PPCSubtarget : public PPCGenSubtargetInfo {
286286

287287
bool isTargetELF() const { return TargetTriple.isOSBinFormatELF(); }
288288
bool isTargetMachO() const { return TargetTriple.isOSBinFormatMachO(); }
289+
bool isTargetLinux() const { return TargetTriple.isOSLinux(); }
289290

290291
bool isDarwinABI() const { return isTargetMachO() || isDarwin(); }
291292
bool isSVR4ABI() const { return !isDarwinABI(); }

llvm/lib/Target/X86/X86ISelLowering.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -2018,12 +2018,6 @@ void X86TargetLowering::insertSSPDeclarations(Module &M) const {
20182018
TargetLowering::insertSSPDeclarations(M);
20192019
}
20202020

2021-
Value *X86TargetLowering::getSDStackGuard(const Module &M) const {
2022-
if (!Subtarget.isTargetLinux())
2023-
return TargetLowering::getSDStackGuard(M);
2024-
return nullptr;
2025-
}
2026-
20272021
Value *X86TargetLowering::getSafeStackPointerLocation(IRBuilder<> &IRB) const {
20282022
if (!Subtarget.isTargetAndroid())
20292023
return TargetLowering::getSafeStackPointerLocation(IRB);

llvm/lib/Target/X86/X86ISelLowering.h

-2
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,6 @@ namespace llvm {
966966

967967
void insertSSPDeclarations(Module &M) const override;
968968

969-
Value *getSDStackGuard(const Module &M) const override;
970-
971969
/// Return true if the target stores SafeStack pointer at a fixed offset in
972970
/// some non-standard address space, and populates the address space and
973971
/// offset as appropriate.

llvm/test/CodeGen/PowerPC/stack-protector.ll

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
; RUN: llc -march=ppc32 -mtriple=ppc32-unknown-linux < %s | FileCheck %s
2-
; CHECK: __stack_chk_guard
3-
; CHECK: __stack_chk_fail
1+
; RUN: llc -mtriple=powerpc-apple-darwin8 < %s | FileCheck -check-prefix=DARWIN32 %s
2+
; RUN: llc -mtriple=powerpc64-apple-darwin < %s | FileCheck -check-prefix=DARWIN64 %s
3+
; RUN: llc -mtriple=ppc32-unknown-linux < %s | FileCheck -check-prefix=LINUX32 %s
4+
; RUN: llc -mtriple=powerpc64le-unknown-linux < %s | FileCheck -check-prefix=LINUX64 %s
5+
6+
; DARWIN32: __stack_chk_guard
7+
; DARWIN64: __stack_chk_guard
8+
; LINUX32: ld {{[0-9]+}}, -28680(2)
9+
; LINUX64: ld {{[0-9]+}}, -28688(13)
10+
11+
; DARWIN32: __stack_chk_fail
12+
; DARWIN64: __stack_chk_fail
13+
; LINUX32: __stack_chk_fail
14+
; LINUX64: __stack_chk_fail
415

516
@"\01LC" = internal constant [11 x i8] c"buf == %s\0A\00" ; <[11 x i8]*> [#uses=1]
617

0 commit comments

Comments
 (0)