Skip to content

Commit 6b68166

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 8996841 commit 6b68166

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,7 +3605,8 @@ 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.isPPC64() &&
3609+
!EffectiveTriple.isPPC32())
36093610
D.Diag(diag::err_drv_unsupported_opt_for_target)
36103611
<< A->getAsString(Args) << TripleStr;
36113612
if ((EffectiveTriple.isX86() || EffectiveTriple.isARM() ||
@@ -3645,7 +3646,8 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36453646
<< A->getOption().getName() << Value << "sysreg global";
36463647
return;
36473648
}
3648-
if (EffectiveTriple.isRISCV()) {
3649+
if (EffectiveTriple.isRISCV() || EffectiveTriple.isPPC64() ||
3650+
EffectiveTriple.isPPC32()) {
36493651
if (Value != "tls" && Value != "global") {
36503652
D.Diag(diag::err_drv_invalid_value_with_suggestion)
36513653
<< A->getOption().getName() << Value << "tls global";
@@ -3666,7 +3668,8 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36663668
StringRef Value = A->getValue();
36673669
if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
36683670
!EffectiveTriple.isARM() && !EffectiveTriple.isThumb() &&
3669-
!EffectiveTriple.isRISCV())
3671+
!EffectiveTriple.isRISCV() && !EffectiveTriple.isPPC64() &&
3672+
!EffectiveTriple.isPPC32())
36703673
D.Diag(diag::err_drv_unsupported_opt_for_target)
36713674
<< A->getAsString(Args) << TripleStr;
36723675
int Offset;
@@ -3686,7 +3689,8 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36863689
if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_reg_EQ)) {
36873690
StringRef Value = A->getValue();
36883691
if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
3689-
!EffectiveTriple.isRISCV())
3692+
!EffectiveTriple.isRISCV() && !EffectiveTriple.isPPC64() &&
3693+
!EffectiveTriple.isPPC32())
36903694
D.Diag(diag::err_drv_unsupported_opt_for_target)
36913695
<< A->getAsString(Args) << TripleStr;
36923696
if (EffectiveTriple.isX86() && (Value != "fs" && Value != "gs")) {
@@ -3703,6 +3707,16 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
37033707
<< A->getOption().getName() << Value << "tp";
37043708
return;
37053709
}
3710+
if (EffectiveTriple.isPPC64() && Value != "r13") {
3711+
D.Diag(diag::err_drv_invalid_value_with_suggestion)
3712+
<< A->getOption().getName() << Value << "r13";
3713+
return;
3714+
}
3715+
if (EffectiveTriple.isPPC32() && Value != "r2") {
3716+
D.Diag(diag::err_drv_invalid_value_with_suggestion)
3717+
<< A->getOption().getName() << Value << "r2";
3718+
return;
3719+
}
37063720
A->render(Args, CmdArgs);
37073721
}
37083722

llvm/lib/Target/PowerPC/PPCISelLowering.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17913,6 +17913,27 @@ Value *PPCTargetLowering::getSDagStackGuard(const Module &M) const {
1791317913
return TargetLowering::getSDagStackGuard(M);
1791417914
}
1791517915

17916+
static Value *useTpOffset(IRBuilderBase &IRB, unsigned Offset) {
17917+
Module *M = IRB.GetInsertBlock()->getModule();
17918+
Function *ThreadPointerFunc =
17919+
Intrinsic::getDeclaration(M, Intrinsic::thread_pointer);
17920+
return IRB.CreateConstGEP1_32(IRB.getInt8Ty(),
17921+
IRB.CreateCall(ThreadPointerFunc), Offset);
17922+
}
17923+
17924+
Value *PPCTargetLowering::getIRStackGuard(IRBuilderBase &IRB) const {
17925+
Module *M = IRB.GetInsertBlock()->getModule();
17926+
17927+
if (M->getStackProtectorGuard() == "tls") {
17928+
// Specially, some users may customize the base reg and offset.
17929+
int Offset = M->getStackProtectorGuardOffset();
17930+
return useTpOffset(IRB, Offset);
17931+
}
17932+
17933+
return TargetLowering::getIRStackGuard(IRB);
17934+
}
17935+
17936+
1791617937
bool PPCTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
1791717938
bool ForCodeSize) const {
1791817939
if (!VT.isSimple() || !Subtarget.hasVSX())

llvm/lib/Target/PowerPC/PPCISelLowering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,7 @@ namespace llvm {
11401140
bool useLoadStackGuardNode() const override;
11411141
void insertSSPDeclarations(Module &M) const override;
11421142
Value *getSDagStackGuard(const Module &M) const override;
1143+
Value *getIRStackGuard(IRBuilderBase &IRB) const override;
11431144

11441145
bool isFPImmLegal(const APFloat &Imm, EVT VT,
11451146
bool ForCodeSize) const override;

0 commit comments

Comments
 (0)