aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen
diff options
context:
space:
mode:
authorAnatoly Trosinenko <atrosinenko@accesssoftek.com>2025-05-20 12:50:58 +0300
committerGitHub <noreply@github.com>2025-05-20 12:50:58 +0300
commitf10a90587f044e134d470ca1ab56d8f244f25336 (patch)
tree3eae52f9527b7e30bb53db1eebc75798a02d3b4a /clang/lib/CodeGen
parentc9d62491981fe720c1b3255fa2f9ddf744590c65 (diff)
downloadllvm-f10a90587f044e134d470ca1ab56d8f244f25336.zip
llvm-f10a90587f044e134d470ca1ab56d8f244f25336.tar.gz
llvm-f10a90587f044e134d470ca1ab56d8f244f25336.tar.bz2
[clang][AArch64] Move initialization of ptrauth-* function attrs (#140277)
Move the initialization of ptrauth-* function attributes near the initialization of branch protection attributes. The semantics of these groups of attributes partially overlaps, so handle both groups in getDefaultFunctionAttributes() and setTargetAttributes() functions to prevent getting them out of sync. This fixes C++ TLS wrappers.
Diffstat (limited to 'clang/lib/CodeGen')
-rw-r--r--clang/lib/CodeGen/CGCall.cpp5
-rw-r--r--clang/lib/CodeGen/CodeGenFunction.cpp13
-rw-r--r--clang/lib/CodeGen/TargetInfo.cpp29
-rw-r--r--clang/lib/CodeGen/TargetInfo.h9
-rw-r--r--clang/lib/CodeGen/Targets/AArch64.cpp1
5 files changed, 44 insertions, 13 deletions
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index bcd5794..bd920a2 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2216,6 +2216,11 @@ void CodeGenModule::getDefaultFunctionAttributes(StringRef Name,
llvm::AttrBuilder &FuncAttrs) {
getTrivialDefaultFunctionAttributes(Name, HasOptnone, AttrOnCallSite,
FuncAttrs);
+
+ if (!AttrOnCallSite)
+ TargetCodeGenInfo::initPointerAuthFnAttributes(CodeGenOpts.PointerAuth,
+ FuncAttrs);
+
// If we're just getting the default, get the default values for mergeable
// attributes.
if (!AttrOnCallSite)
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index ac40aab..4e79cdf 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -890,19 +890,6 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass)
SanOpts.Mask &= ~SanitizerKind::Null;
- // Add pointer authentication attributes.
- const CodeGenOptions &CodeGenOpts = CGM.getCodeGenOpts();
- if (CodeGenOpts.PointerAuth.ReturnAddresses)
- Fn->addFnAttr("ptrauth-returns");
- if (CodeGenOpts.PointerAuth.FunctionPointers)
- Fn->addFnAttr("ptrauth-calls");
- if (CodeGenOpts.PointerAuth.AuthTraps)
- Fn->addFnAttr("ptrauth-auth-traps");
- if (CodeGenOpts.PointerAuth.IndirectGotos)
- Fn->addFnAttr("ptrauth-indirect-gotos");
- if (CodeGenOpts.PointerAuth.AArch64JumpTableHardening)
- Fn->addFnAttr("aarch64-jump-table-hardening");
-
// Apply xray attributes to the function (as a string, for now)
bool AlwaysXRayAttr = false;
if (const auto *XRayAttr = D ? D->getAttr<XRayInstrumentAttr>() : nullptr) {
diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp
index 75a7d3c..7d176e4 100644
--- a/clang/lib/CodeGen/TargetInfo.cpp
+++ b/clang/lib/CodeGen/TargetInfo.cpp
@@ -258,6 +258,35 @@ void TargetCodeGenInfo::initBranchProtectionFnAttributes(
FuncAttrs.addAttribute("guarded-control-stack");
}
+void TargetCodeGenInfo::setPointerAuthFnAttributes(
+ const PointerAuthOptions &Opts, llvm::Function &F) {
+ auto UpdateAttr = [&F](bool AttrShouldExist, StringRef AttrName) {
+ if (AttrShouldExist && !F.hasFnAttribute(AttrName))
+ F.addFnAttr(AttrName);
+ if (!AttrShouldExist && F.hasFnAttribute(AttrName))
+ F.removeFnAttr(AttrName);
+ };
+ UpdateAttr(Opts.ReturnAddresses, "ptrauth-returns");
+ UpdateAttr((bool)Opts.FunctionPointers, "ptrauth-calls");
+ UpdateAttr(Opts.AuthTraps, "ptrauth-auth-traps");
+ UpdateAttr(Opts.IndirectGotos, "ptrauth-indirect-gotos");
+ UpdateAttr(Opts.AArch64JumpTableHardening, "aarch64-jump-table-hardening");
+}
+
+void TargetCodeGenInfo::initPointerAuthFnAttributes(
+ const PointerAuthOptions &Opts, llvm::AttrBuilder &FuncAttrs) {
+ if (Opts.ReturnAddresses)
+ FuncAttrs.addAttribute("ptrauth-returns");
+ if (Opts.FunctionPointers)
+ FuncAttrs.addAttribute("ptrauth-calls");
+ if (Opts.AuthTraps)
+ FuncAttrs.addAttribute("ptrauth-auth-traps");
+ if (Opts.IndirectGotos)
+ FuncAttrs.addAttribute("ptrauth-indirect-gotos");
+ if (Opts.AArch64JumpTableHardening)
+ FuncAttrs.addAttribute("aarch64-jump-table-hardening");
+}
+
namespace {
class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
public:
diff --git a/clang/lib/CodeGen/TargetInfo.h b/clang/lib/CodeGen/TargetInfo.h
index 77831ab..2783e22 100644
--- a/clang/lib/CodeGen/TargetInfo.h
+++ b/clang/lib/CodeGen/TargetInfo.h
@@ -459,6 +459,15 @@ public:
initBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
llvm::AttrBuilder &FuncAttrs);
+ // Set the ptrauth-* attributes of the Function accordingly to the Opts.
+ // Remove attributes that contradict with current Opts.
+ static void setPointerAuthFnAttributes(const PointerAuthOptions &Opts,
+ llvm::Function &F);
+
+ // Add the ptrauth-* Attributes to the FuncAttrs.
+ static void initPointerAuthFnAttributes(const PointerAuthOptions &Opts,
+ llvm::AttrBuilder &FuncAttrs);
+
protected:
static std::string qualifyWindowsLibrary(StringRef Lib);
diff --git a/clang/lib/CodeGen/Targets/AArch64.cpp b/clang/lib/CodeGen/Targets/AArch64.cpp
index f098f09e..6311d92 100644
--- a/clang/lib/CodeGen/Targets/AArch64.cpp
+++ b/clang/lib/CodeGen/Targets/AArch64.cpp
@@ -155,6 +155,7 @@ public:
}
}
setBranchProtectionFnAttributes(BPI, *Fn);
+ setPointerAuthFnAttributes(CGM.getCodeGenOpts().PointerAuth, *Fn);
}
bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,