From 2a7a6ef1b44f250abf840165bac4c91ca0af928b Mon Sep 17 00:00:00 2001 From: Jesse Huang Date: Sun, 13 Oct 2024 15:11:06 +0800 Subject: [PATCH] [Clang][RISCV] Support -fcf-protection=return for RISC-V --- clang/lib/Basic/Targets/RISCV.h | 6 ++++++ clang/lib/CodeGen/CodeGenFunction.cpp | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/clang/lib/Basic/Targets/RISCV.h b/clang/lib/Basic/Targets/RISCV.h index bf40edb8683b3..3165623593fdf 100644 --- a/clang/lib/Basic/Targets/RISCV.h +++ b/clang/lib/Basic/Targets/RISCV.h @@ -141,6 +141,12 @@ class RISCVTargetInfo : public TargetInfo { return true; } + bool checkCFProtectionReturnSupported(DiagnosticsEngine &Diags) const override { + if (ISAInfo->hasExtension("zimop")) + return true; + return TargetInfo::checkCFProtectionReturnSupported(Diags); + } + CFBranchLabelSchemeKind getDefaultCFBranchLabelScheme() const override { return CFBranchLabelSchemeKind::FuncSig; } diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 2306043c90f40..d8f0f7c14f6b4 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -899,6 +899,10 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, if (CodeGenOpts.PointerAuth.IndirectGotos) Fn->addFnAttr("ptrauth-indirect-gotos"); + // Add return control flow integrity attributes. + if (CodeGenOpts.CFProtectionReturn) + Fn->addFnAttr("hw-shadow-stack"); + // Apply xray attributes to the function (as a string, for now) bool AlwaysXRayAttr = false; if (const auto *XRayAttr = D ? D->getAttr() : nullptr) {