Skip to content

Commit 156b989

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 0bc8168 commit 156b989

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 25 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;
@@ -3669,7 +3685,8 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36693685

36703686
if (Arg *A = Args.getLastArg(options::OPT_mstack_protector_guard_reg_EQ)) {
36713687
StringRef Value = A->getValue();
3672-
if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64())
3688+
if (!EffectiveTriple.isX86() && !EffectiveTriple.isAArch64() &&
3689+
!EffectiveTriple.isRISCV())
36733690
D.Diag(diag::err_drv_unsupported_opt_for_target)
36743691
<< A->getAsString(Args) << TripleStr;
36753692
if (EffectiveTriple.isX86() && (Value != "fs" && Value != "gs")) {
@@ -3681,6 +3698,11 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
36813698
D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value;
36823699
return;
36833700
}
3701+
if (EffectiveTriple.isRISCV() && Value != "tp") {
3702+
D.Diag(diag::err_drv_invalid_value_with_suggestion)
3703+
<< A->getOption().getName() << Value << "tp";
3704+
return;
3705+
}
36843706
A->render(Args, CmdArgs);
36853707
}
36863708

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()->getModule();
21232+
21233+
if (M->getStackProtectorGuard() == "tls") {
21234+
// Users must specify the offset explicitly
21235+
int Offset = M->getStackProtectorGuardOffset();
21236+
return useTpOffset(IRB, Offset);
21237+
}
21238+
2123121239
return TargetLowering::getIRStackGuard(IRB);
2123221240
}
2123321241

0 commit comments

Comments
 (0)