https://github.com/jaidTw created 
https://github.com/llvm/llvm-project/pull/112477

This patches add a string attribute "hw-shadow-stack" to every function if 
`-fcf-protection=return` is set on RISC-V

>From 2a7a6ef1b44f250abf840165bac4c91ca0af928b Mon Sep 17 00:00:00 2001
From: Jesse Huang <jesse.hu...@sifive.com>
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 bf40edb8683b3e..3165623593fdf7 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 2306043c90f406..d8f0f7c14f6b40 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<XRayInstrumentAttr>() : nullptr) {

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to