llvm.org GIT mirror llvm / ac94d4b
[PPC, SSP] Support PowerPC Linux stack protection. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266809 91177308-0d34-0410-b5e6-96231b3b80d8 Tim Shen 4 years ago
11 changed file(s) with 64 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
10231023 /// Return the variable that's previously inserted by insertSSPDeclarations,
10241024 /// if any, otherwise return nullptr. Should be used only when
10251025 /// getIRStackGuard returns nullptr.
1026 virtual Value *getSDStackGuard(const Module &M) const;
1026 virtual Value *getSDagStackGuard(const Module &M) const;
10271027
10281028 /// If the target has a standard location for the unsafe stack pointer,
10291029 /// returns the address of that location. Otherwise, returns nullptr.
20032003 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
20042004 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
20052005 MachineFunction &MF = DAG.getMachineFunction();
2006 Value *Global = TLI.getSDStackGuard(*MF.getFunction()->getParent());
2006 Value *Global = TLI.getSDagStackGuard(*MF.getFunction()->getParent());
20072007 MachineSDNode *Node =
20082008 DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
20092009 if (Global) {
20332033 MachineFrameInfo *MFI = ParentBB->getParent()->getFrameInfo();
20342034 int FI = MFI->getStackProtectorIndex();
20352035
2036 const Module &M = *ParentBB->getParent()->getFunction()->getParent();
2037 const Value *IRGuard = TLI.getSDStackGuard(M);
2038 assert(IRGuard && "Currently there must be an IR guard in order to use "
2039 "SelectionDAG SSP");
2040 SDValue GuardPtr = getValue(IRGuard);
2041 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
2042
2043 unsigned Align = DL->getPrefTypeAlignment(IRGuard->getType());
2044
20452036 SDValue Guard;
20462037 SDLoc dl = getCurSDLoc();
2038 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
2039 const Module &M = *ParentBB->getParent()->getFunction()->getParent();
2040 unsigned Align = DL->getPrefTypeAlignment(Type::getInt8PtrTy(M.getContext()));
20472041
20482042 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
20492043 // Otherwise, emit a volatile load to retrieve the stack guard value.
20502044 SDValue Chain = DAG.getEntryNode();
2051 if (TLI.useLoadStackGuardNode())
2045 if (TLI.useLoadStackGuardNode()) {
20522046 Guard = getLoadStackGuard(DAG, dl, Chain);
2053 else
2047 } else {
2048 const Value *IRGuard = TLI.getSDagStackGuard(M);
2049 SDValue GuardPtr = getValue(IRGuard);
2050
20542051 Guard =
20552052 DAG.getLoad(PtrTy, dl, Chain, GuardPtr, MachinePointerInfo(IRGuard, 0),
20562053 true, false, false, Align);
2054 }
20572055
20582056 SDValue StackSlot = DAG.getLoad(
20592057 PtrTy, dl, DAG.getEntryNode(), StackSlotPtr,
53125310 if (TLI.useLoadStackGuardNode()) {
53135311 Res = getLoadStackGuard(DAG, sdl, Chain);
53145312 } else {
5315 const Value *Global = TLI.getSDStackGuard(M);
5313 const Value *Global = TLI.getSDagStackGuard(M);
53165314 unsigned Align = DL->getPrefTypeAlignment(Global->getType());
53175315 Res =
53185316 DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
18131813
18141814 // Currently only support "standard" __stack_chk_guard.
18151815 // TODO: add LOAD_STACK_GUARD support.
1816 Value *TargetLoweringBase::getSDStackGuard(const Module &M) const {
1816 Value *TargetLoweringBase::getSDagStackGuard(const Module &M) const {
18171817 return M.getGlobalVariable("__stack_chk_guard");
18181818 }
1201212012 .addReg(NewVR);
1201312013 }
1201412014 }
12015
12016 // Override to enable LOAD_STACK_GUARD lowering on Linux.
12017 bool PPCTargetLowering::useLoadStackGuardNode() const {
12018 if (!Subtarget.isTargetLinux())
12019 return TargetLowering::useLoadStackGuardNode();
12020 return true;
12021 }
12022
12023 // Override to disable global variable loading on Linux.
12024 void PPCTargetLowering::insertSSPDeclarations(Module &M) const {
12025 if (!Subtarget.isTargetLinux())
12026 return TargetLowering::insertSSPDeclarations(M);
12027 }
686686 /// exception typeid on entry to a landing pad.
687687 unsigned
688688 getExceptionSelectorRegister(const Constant *PersonalityFn) const override;
689
690 /// Override to support customized stack guard loading.
691 bool useLoadStackGuardNode() const override;
692 void insertSSPDeclarations(Module &M) const override;
689693
690694 private:
691695 struct ReuseLoadInfo {
18561856 return makeArrayRef(TargetFlags);
18571857 }
18581858
1859 bool PPCInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1860 switch (MI->getOpcode()) {
1861 case TargetOpcode::LOAD_STACK_GUARD: {
1862 assert(Subtarget.isTargetLinux() &&
1863 "Only Linux target is expected to contain LOAD_STACK_GUARD");
1864 const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008;
1865 const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2;
1866 MI->setDesc(get(PPC::LD));
1867 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1868 .addImm(Offset)
1869 .addReg(Reg);
1870 return true;
1871 }
1872 }
1873 return false;
1874 }
271271
272272 ArrayRef>
273273 getSerializableBitmaskMachineOperandTargetFlags() const override;
274
275 // Lower pseudo instructions after register allocation.
276 bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
274277 };
275278
276279 }
285285
286286 bool isTargetELF() const { return TargetTriple.isOSBinFormatELF(); }
287287 bool isTargetMachO() const { return TargetTriple.isOSBinFormatMachO(); }
288 bool isTargetLinux() const { return TargetTriple.isOSLinux(); }
288289
289290 bool isDarwinABI() const { return isTargetMachO() || isDarwin(); }
290291 bool isSVR4ABI() const { return !isDarwinABI(); }
20152015 void X86TargetLowering::insertSSPDeclarations(Module &M) const {
20162016 if (!Subtarget.isTargetLinux())
20172017 TargetLowering::insertSSPDeclarations(M);
2018 }
2019
2020 Value *X86TargetLowering::getSDStackGuard(const Module &M) const {
2021 if (!Subtarget.isTargetLinux())
2022 return TargetLowering::getSDStackGuard(M);
2023 return nullptr;
20242018 }
20252019
20262020 Value *X86TargetLowering::getSafeStackPointerLocation(IRBuilder<> &IRB) const {
964964 Value *getIRStackGuard(IRBuilder<> &IRB) const override;
965965
966966 void insertSSPDeclarations(Module &M) const override;
967
968 Value *getSDStackGuard(const Module &M) const override;
969967
970968 /// Return true if the target stores SafeStack pointer at a fixed offset in
971969 /// some non-standard address space, and populates the address space and
None ; RUN: llc -march=ppc32 -mtriple=ppc32-unknown-linux < %s | FileCheck %s
1 ; CHECK: __stack_chk_guard
2 ; CHECK: __stack_chk_fail
0 ; RUN: llc -mtriple=powerpc-apple-darwin8 < %s | FileCheck -check-prefix=DARWIN32 %s
1 ; RUN: llc -mtriple=powerpc64-apple-darwin < %s | FileCheck -check-prefix=DARWIN64 %s
2 ; RUN: llc -mtriple=ppc32-unknown-linux < %s | FileCheck -check-prefix=LINUX32 %s
3 ; RUN: llc -mtriple=powerpc64le-unknown-linux < %s | FileCheck -check-prefix=LINUX64 %s
4
5 ; DARWIN32: __stack_chk_guard
6 ; DARWIN64: __stack_chk_guard
7 ; LINUX32: ld {{[0-9]+}}, -28680(2)
8 ; LINUX64: ld {{[0-9]+}}, -28688(13)
9
10 ; DARWIN32: __stack_chk_fail
11 ; DARWIN64: __stack_chk_fail
12 ; LINUX32: __stack_chk_fail
13 ; LINUX64: __stack_chk_fail
314
415 @"\01LC" = internal constant [11 x i8] c"buf == %s\0A\00" ; <[11 x i8]*> [#uses=1]
516