Skip to content

Commit 9924f57

Browse files
committed
[PAC][AArch64][clang][ELF] Support PAuth ABI compatibility tag
Emit PAuth ABI compatibility tag values as llvm module flags: - `aarch64-elf-pauthabi-platform` - `aarch64-elf-pauthabi-version`
1 parent 919af72 commit 9924f57

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "llvm/ADT/StringExtras.h"
5454
#include "llvm/ADT/StringSwitch.h"
5555
#include "llvm/Analysis/TargetLibraryInfo.h"
56+
#include "llvm/BinaryFormat/ELF.h"
5657
#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
5758
#include "llvm/IR/AttributeMask.h"
5859
#include "llvm/IR/CallingConv.h"
@@ -1187,6 +1188,36 @@ void CodeGenModule::Release() {
11871188
if (!LangOpts.isSignReturnAddressWithAKey())
11881189
getModule().addModuleFlag(llvm::Module::Min,
11891190
"sign-return-address-with-bkey", 1);
1191+
1192+
if (getTriple().isOSLinux() && getTriple().isOSBinFormatELF()) {
1193+
using namespace llvm::ELF;
1194+
uint64_t PAuthABIVersion =
1195+
(LangOpts.PointerAuthIntrinsics
1196+
<< AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INTRINSICS) |
1197+
(LangOpts.PointerAuthCalls
1198+
<< AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_CALLS) |
1199+
(LangOpts.PointerAuthReturns
1200+
<< AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_RETURNS) |
1201+
(LangOpts.PointerAuthAuthTraps
1202+
<< AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_AUTHTRAPS) |
1203+
(LangOpts.PointerAuthVTPtrAddressDiscrimination
1204+
<< AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRADDRDISCR) |
1205+
(LangOpts.PointerAuthVTPtrTypeDiscrimination
1206+
<< AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRTYPEDISCR) |
1207+
(LangOpts.PointerAuthInitFini
1208+
<< AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI);
1209+
static_assert(AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI ==
1210+
AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_LAST,
1211+
"Update when new enum items are defined");
1212+
if (PAuthABIVersion != 0) {
1213+
getModule().addModuleFlag(llvm::Module::Error,
1214+
"aarch64-elf-pauthabi-platform",
1215+
AARCH64_PAUTH_PLATFORM_LLVM_LINUX);
1216+
getModule().addModuleFlag(llvm::Module::Error,
1217+
"aarch64-elf-pauthabi-version",
1218+
PAuthABIVersion);
1219+
}
1220+
}
11901221
}
11911222

11921223
if (CodeGenOpts.StackClashProtector)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
2+
// RUN: -fptrauth-intrinsics \
3+
// RUN: -fptrauth-calls \
4+
// RUN: -fptrauth-returns \
5+
// RUN: -fptrauth-auth-traps \
6+
// RUN: -fptrauth-vtable-pointer-address-discrimination \
7+
// RUN: -fptrauth-vtable-pointer-type-discrimination \
8+
// RUN: -fptrauth-init-fini %s | \
9+
// RUN: FileCheck %s --check-prefix=ALL
10+
11+
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
12+
// RUN: -fptrauth-intrinsics %s | FileCheck %s --check-prefix=INTRIN
13+
14+
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
15+
// RUN: -fptrauth-calls %s | FileCheck %s --check-prefix=CALL
16+
17+
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
18+
// RUN: -fptrauth-returns %s | FileCheck %s --check-prefix=RET
19+
20+
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
21+
// RUN: -fptrauth-auth-traps %s | FileCheck %s --check-prefix=TRAP
22+
23+
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
24+
// RUN: -fptrauth-calls -fptrauth-vtable-pointer-address-discrimination %s | \
25+
// RUN: FileCheck %s --check-prefix=VPTRADDR
26+
27+
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
28+
// RUN: -fptrauth-calls -fptrauth-vtable-pointer-type-discrimination %s | \
29+
// RUN: FileCheck %s --check-prefix=VPTRTYPE
30+
31+
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
32+
// RUN: -fptrauth-calls -fptrauth-init-fini %s | \
33+
// RUN: FileCheck %s --check-prefix=INITFINI
34+
35+
// REQUIRES: aarch64-registered-target
36+
37+
// ALL: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
38+
// ALL: !{i32 1, !"aarch64-elf-pauthabi-version", i32 127}
39+
40+
// INTRIN: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
41+
// INTRIN: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1}
42+
43+
// CALL: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
44+
// CALL: !{i32 1, !"aarch64-elf-pauthabi-version", i32 2}
45+
46+
// RET: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
47+
// RET: !{i32 1, !"aarch64-elf-pauthabi-version", i32 4}
48+
49+
// TRAP: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
50+
// TRAP: !{i32 1, !"aarch64-elf-pauthabi-version", i32 8}
51+
52+
// VPTRADDR: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
53+
// VPTRADDR: !{i32 1, !"aarch64-elf-pauthabi-version", i32 18}
54+
55+
// VPTRTYPE: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
56+
// VPTRTYPE: !{i32 1, !"aarch64-elf-pauthabi-version", i32 34}
57+
58+
// INITFINI: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
59+
// INITFINI: !{i32 1, !"aarch64-elf-pauthabi-version", i32 66}
60+
61+
void foo() {}

0 commit comments

Comments
 (0)