Skip to content

Commit 7822a1e

Browse files
committed
riscv: 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 b7e51b4 commit 7822a1e

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3604,7 +3604,8 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36043604
if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_EQ)) {
36053605
StringRef Value = A->getValue();
36063606
if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
3607-
!EffectiveTriple.isARM() && !EffectiveTriple.isThumb())
3607+
!EffectiveTriple.isARM() && !EffectiveTriple.isThumb() &&
3608+
!EffectiveTriple.isRISCV())
36083609
D.Diag(diag::err_drv_unsupported_opt_for_target)
36093610
<< A->getAsString(Args) << TripleStr;
36103611
if ((EffectiveTriple.isX86() || EffectiveTriple.isARM() ||
@@ -3644,13 +3645,28 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36443645
<< A->getOption().getName() << Value << "sysreg global";
36453646
return;
36463647
}
3648+
if (EffectiveTriple.isRISCV()) {
3649+
if (Value != "tls" && Value != "global") {
3650+
D.Diag(diag::err_drv_invalid_value_with_suggestion)
3651+
<< A->getOption().getName() << Value << "tls global";
3652+
return;
3653+
}
3654+
if (Value == "tls") {
3655+
if (!Args.hasArg(options::OPT_mstack_protector_guard_offset_EQ)) {
3656+
D.Diag(diag::err_drv_ssp_missing_offset_argument)
3657+
<< A->getAsString(Args);
3658+
return;
3659+
}
3660+
}
3661+
}
36473662
A->render(Args, CmdArgs);
36483663
}
36493664

36503665
if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_offset_EQ)) {
36513666
StringRef Value = A->getValue();
36523667
if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
3653-
!EffectiveTriple.isARM() && !EffectiveTriple.isThumb())
3668+
!EffectiveTriple.isARM() && !EffectiveTriple.isThumb() &&
3669+
!EffectiveTriple.isRISCV())
36543670
D.Diag(diag::err_drv_unsupported_opt_for_target)
36553671
<< A->getAsString(Args) << TripleStr;
36563672
int Offset;
@@ -3664,12 +3680,18 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36643680
<< A->getOption().getName() << Value;
36653681
return;
36663682
}
3683+
if (EffectiveTriple.isRISCV() && (Offset <= -2048 || Offset >= 2048)) {
3684+
D.Diag(diag::err_drv_invalid_int_value)
3685+
<< A->getOption().getName() << Value;
3686+
return;
3687+
}
36673688
A->render(Args, CmdArgs);
36683689
}
36693690

36703691
if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_reg_EQ)) {
36713692
StringRef Value = A->getValue();
3672-
if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64())
3693+
if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
3694+
!EffectiveTriple.isRISCV())
36733695
D.Diag(diag::err_drv_unsupported_opt_for_target)
36743696
<< A->getAsString(Args) << TripleStr;
36753697
if (EffectiveTriple.isX86() && (Value != "fs" && Value != "gs")) {
@@ -3681,6 +3703,11 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36813703
D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value;
36823704
return;
36833705
}
3706+
if (EffectiveTriple.isRISCV() && Value != "tp") {
3707+
D.Diag(diag::err_drv_invalid_value_with_suggestion)
3708+
<< A->getOption().getName() << Value << "tp";
3709+
return;
3710+
}
36843711
A->render(Args, CmdArgs);
36853712
}
36863713

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21228,6 +21228,14 @@ Value *RISCVTargetLowering::getIRStackGuard(IRBuilderBase &IRB) const {
2122821228
if (Subtarget.isTargetAndroid())
2122921229
return useTpOffset(IRB, -0x18);
2123021230

21231+
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
21232+
21233+
if (M->getStackProtectorGuard() == "tls") {
21234+
// Specially, some users may customize the base reg and offset.
21235+
int Offset = M->getStackProtectorGuardOffset();
21236+
return useTpOffset(IRB, Offset);
21237+
}
21238+
2123121239
return TargetLowering::getIRStackGuard(IRB);
2123221240
}
2123321241

0 commit comments

Comments
 (0)