Skip to content

Commit 77a0a01

Browse files
committed
[PowerPC][ISelLowering] Support -mstack-protector-guard=tls
Add support for using a thread-local variable with a specified offset for holding the stack guard canary value. Signed-off-by: Keith Packard <[email protected]>
1 parent 6fc2953 commit 77a0a01

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,7 +3605,7 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36053605
StringRef Value = A->getValue();
36063606
if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
36073607
!EffectiveTriple.isARM() && !EffectiveTriple.isThumb() &&
3608-
!EffectiveTriple.isRISCV())
3608+
!EffectiveTriple.isRISCV() && !EffectiveTriple.isPPC())
36093609
D.Diag(diag::err_drv_unsupported_opt_for_target)
36103610
<< A->getAsString(Args) << TripleStr;
36113611
if ((EffectiveTriple.isX86() || EffectiveTriple.isARM() ||
@@ -3645,7 +3645,7 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36453645
<< A->getOption().getName() << Value << "sysreg global";
36463646
return;
36473647
}
3648-
if (EffectiveTriple.isRISCV()) {
3648+
if (EffectiveTriple.isRISCV() || EffectiveTriple.isPPC()) {
36493649
if (Value != "tls" && Value != "global") {
36503650
D.Diag(diag::err_drv_invalid_value_with_suggestion)
36513651
<< A->getOption().getName() << Value << "tls global";
@@ -3666,7 +3666,7 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36663666
StringRef Value = A->getValue();
36673667
if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
36683668
!EffectiveTriple.isARM() && !EffectiveTriple.isThumb() &&
3669-
!EffectiveTriple.isRISCV())
3669+
!EffectiveTriple.isRISCV() && !EffectiveTriple.isPPC())
36703670
D.Diag(diag::err_drv_unsupported_opt_for_target)
36713671
<< A->getAsString(Args) << TripleStr;
36723672
int Offset;
@@ -3686,7 +3686,7 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36863686
if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_reg_EQ)) {
36873687
StringRef Value = A->getValue();
36883688
if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
3689-
!EffectiveTriple.isRISCV())
3689+
!EffectiveTriple.isRISCV() && !EffectiveTriple.isPPC())
36903690
D.Diag(diag::err_drv_unsupported_opt_for_target)
36913691
<< A->getAsString(Args) << TripleStr;
36923692
if (EffectiveTriple.isX86() && (Value != "fs" && Value != "gs")) {
@@ -3703,6 +3703,16 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
37033703
<< A->getOption().getName() << Value << "tp";
37043704
return;
37053705
}
3706+
if (EffectiveTriple.isPPC64() && Value != "r13") {
3707+
D.Diag(diag::err_drv_invalid_value_with_suggestion)
3708+
<< A->getOption().getName() << Value << "r13";
3709+
return;
3710+
}
3711+
if (EffectiveTriple.isPPC32() && Value != "r2") {
3712+
D.Diag(diag::err_drv_invalid_value_with_suggestion)
3713+
<< A->getOption().getName() << Value << "r2";
3714+
return;
3715+
}
37063716
A->render(Args, CmdArgs);
37073717
}
37083718

llvm/lib/Target/PowerPC/PPCISelLowering.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17889,10 +17889,10 @@ SDValue PPCTargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG,
1788917889
}
1789017890

1789117891
// Override to enable LOAD_STACK_GUARD lowering on Linux.
17892-
bool PPCTargetLowering::useLoadStackGuardNode() const {
17893-
if (!Subtarget.isTargetLinux())
17894-
return TargetLowering::useLoadStackGuardNode();
17895-
return true;
17892+
bool PPCTargetLowering::useLoadStackGuardNode(const Module &M) const {
17893+
if (M.getStackProtectorGuard() == "tls" || Subtarget.isTargetLinux())
17894+
return true;
17895+
return TargetLowering::useLoadStackGuardNode();
1789617896
}
1789717897

1789817898
// Override to disable global variable loading on Linux and insert AIX canary

llvm/lib/Target/PowerPC/PPCISelLowering.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ namespace llvm {
11371137
getExceptionSelectorRegister(const Constant *PersonalityFn) const override;
11381138

11391139
/// Override to support customized stack guard loading.
1140-
bool useLoadStackGuardNode() const override;
1140+
bool useLoadStackGuardNode(const Module &M) const override;
11411141
void insertSSPDeclarations(Module &M) const override;
11421142
Value *getSDagStackGuard(const Module &M) const override;
11431143

llvm/lib/Target/PowerPC/PPCInstrInfo.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "llvm/CodeGen/ScheduleDAG.h"
3636
#include "llvm/CodeGen/SlotIndexes.h"
3737
#include "llvm/CodeGen/StackMaps.h"
38+
#include "llvm/IR/Module.h"
3839
#include "llvm/MC/MCAsmInfo.h"
3940
#include "llvm/MC/MCInst.h"
4041
#include "llvm/MC/TargetRegistry.h"
@@ -3107,9 +3108,17 @@ bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
31073108
return true;
31083109
}
31093110
case TargetOpcode::LOAD_STACK_GUARD: {
3110-
assert(Subtarget.isTargetLinux() &&
3111-
"Only Linux target is expected to contain LOAD_STACK_GUARD");
3112-
const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008;
3111+
// MBB might not have an associated BB; use the function to find one
3112+
auto M = MBB.getParent()->getFunction().getEntryBlock().getModule();
3113+
assert(
3114+
(Subtarget.isTargetLinux() || M->getStackProtectorGuard() == "tls") &&
3115+
"Only Linux target or tls mode are expected to contain "
3116+
"LOAD_STACK_GUARD");
3117+
int64_t Offset;
3118+
if (M->getStackProtectorGuard() == "tls")
3119+
Offset = M->getStackProtectorGuardOffset();
3120+
else
3121+
Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008;
31133122
const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2;
31143123
MI.setDesc(get(Subtarget.isPPC64() ? PPC::LD : PPC::LWZ));
31153124
MachineInstrBuilder(*MI.getParent()->getParent(), MI)

0 commit comments

Comments
 (0)