Skip to content

Commit 0c31187

Browse files
Add branch protection attributes to the defaults.
These attributes are no longer inherited from the module flags, therefore need to be added for synthetic functions.
1 parent b5657d6 commit 0c31187

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,22 @@ static void getTrivialDefaultFunctionAttributes(
20302030
std::tie(Var, Value) = Attr.split('=');
20312031
FuncAttrs.addAttribute(Var, Value);
20322032
}
2033+
2034+
TargetInfo::BranchProtectionInfo BPI(LangOpts);
2035+
2036+
if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
2037+
FuncAttrs.addAttribute("sign-return-address", BPI.getSignReturnAddrStr());
2038+
FuncAttrs.addAttribute(
2039+
"sign-return-address-key",
2040+
BPI.SignKey == LangOptions::SignReturnAddressKeyKind::AKey ? "a_key"
2041+
: "b_key");
2042+
}
2043+
if (BPI.BranchTargetEnforcement)
2044+
FuncAttrs.addAttribute("branch-target-enforcement", "true");
2045+
if (BPI.BranchProtectionPAuthLR)
2046+
FuncAttrs.addAttribute("branch-protection-pauth-lr", "true");
2047+
if (BPI.GuardedControlStack)
2048+
FuncAttrs.addAttribute("guarded-control-stack", "true");
20332049
}
20342050

20352051
/// Merges `target-features` from \TargetOpts and \F, and sets the result in
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %clang_cc1 -triple aarch64-none-none -mbranch-target-enforce -msign-return-address=all -fcxx-exceptions -fexceptions -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK
2+
3+
// Check that functions generated by clang have the correct attributes
4+
5+
class Example {
6+
public:
7+
Example();
8+
int fn();
9+
};
10+
11+
// Initialization of var1 causes __cxx_global_var_init and __tls_init to be generated
12+
thread_local Example var1;
13+
extern thread_local Example var2;
14+
extern void fn();
15+
16+
int testfn() noexcept {
17+
// Calling fn in a noexcept function causes __clang_call_terminate to be generated
18+
fn();
19+
// Use of var1 and var2 causes TLS wrapper functions to be generated
20+
return var1.fn() + var2.fn();
21+
}
22+
23+
// CHECK: define {{.*}} @__cxx_global_var_init() [[ATTR1:#[0-9]+]]
24+
// CHECK: define {{.*}} @__clang_call_terminate({{.*}}) [[ATTR2:#[0-9]+]]
25+
// CHECK: define {{.*}} @_ZTW4var1() [[ATTR1]]
26+
// CHECK: define {{.*}} @_ZTW4var2() [[ATTR1]]
27+
// CHECK: define {{.*}} @__tls_init() [[ATTR1]]
28+
29+
// CHECK: attributes [[ATTR1]] = { {{.*}}"branch-target-enforcement"="true"{{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
30+
// CHECK: attributes [[ATTR2]] = { {{.*}}"branch-target-enforcement"="true"{{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"

0 commit comments

Comments
 (0)