Skip to content

Commit 51283c4

Browse files
authored
Merge pull request #7055 from apple/eng/abougacha/arm64e-ptrauth-abi-version-swift5.9
[5.9] [AArch64][MachO] Support ptrauth ABI version.
2 parents 009faee + a0ce75f commit 51283c4

File tree

56 files changed

+865
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+865
-20
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ LANGOPT(PointerAuthReturns, 1, 0, "return pointer authentication")
163163
LANGOPT(PointerAuthIndirectGotos, 1, 0, "indirect gotos pointer authentication")
164164
LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication failure traps")
165165
LANGOPT(SoftPointerAuth , 1, 0, "software emulation of pointer authentication")
166+
VALUE_LANGOPT(PointerAuthABIVersion, 32, 0, "pointer authentication ABI version")
167+
LANGOPT(PointerAuthKernelABIVersion, 1, 0, "controls whether the pointer auth abi version represents a kernel ABI")
168+
LANGOPT(PointerAuthABIVersionEncoded, 1, 0, "controls whether the pointer auth abi version should be encoded in the IR")
169+
166170
LANGOPT(Unstable , 1, 0, "Enable unstable and experimental features")
167171
LANGOPT(ExperimentalLibrary, 1, 0, "enable unstable and experimental library features")
168172

clang/include/clang/Driver/Options.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,6 +3150,20 @@ let Group = f_Group in {
31503150
def fno_ptrauth_soft : Flag<["-"], "fno-ptrauth-soft">;
31513151
}
31523152

3153+
let Group = f_Group in {
3154+
def fptrauth_abi_version_EQ : Joined<["-"], "fptrauth-abi-version=">,
3155+
Flags<[CC1Option, CC1AsOption]>,
3156+
HelpText<"Pointer Authentication ABI version">;
3157+
def fno_ptrauth_abi_version : Flag<["-"], "fno-ptrauth-abi-version">,
3158+
HelpText<"Disable Pointer Authentication ABI versioning">;
3159+
3160+
def fptrauth_kernel_abi_version : Flag<["-"], "fptrauth-kernel-abi-version">,
3161+
Flags<[CC1Option, CC1AsOption]>,
3162+
HelpText<"Enable Pointer Authentication kernel ABI version">;
3163+
def fno_ptrauth_kernel_abi_version : Flag<["-"], "fno-ptrauth-kernel-abi-version">,
3164+
HelpText<"Disable Pointer Authentication kernel ABI versioning">;
3165+
}
3166+
31533167
def fenable_matrix : Flag<["-"], "fenable-matrix">, Group<f_Group>,
31543168
Flags<[CC1Option]>,
31553169
HelpText<"Enable matrix data type and related builtin functions">,

clang/lib/Basic/Targets/OSTargets.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
3434
if (Opts.PointerAuthIntrinsics)
3535
Builder.defineMacro("__PTRAUTH_INTRINSICS__");
3636

37+
if (Opts.PointerAuthABIVersionEncoded)
38+
Builder.defineMacro("__ptrauth_abi_version__",
39+
llvm::utostr(Opts.PointerAuthABIVersion));
40+
3741
// Darwin defines __weak, __strong, and __unsafe_unretained even in C mode.
3842
if (!Opts.ObjC) {
3943
// __weak is always defined, for use in blocks and with objc pointers.

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,11 @@ void CodeGenModule::Release() {
863863
if (LangOpts.HLSL)
864864
getHLSLRuntime().finishCodeGen();
865865

866+
if (LangOpts.PointerAuthABIVersionEncoded)
867+
TheModule.setPtrAuthABIVersion(
868+
{static_cast<int>(LangOpts.PointerAuthABIVersion),
869+
static_cast<bool>(LangOpts.PointerAuthKernelABIVersion)});
870+
866871
if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
867872
assert(PLevel < 3 && "Invalid PIC Level");
868873
getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel));

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8106,6 +8106,46 @@ void ClangAs::AddRISCVTargetArgs(const ArgList &Args,
81068106
CmdArgs.push_back(ABIName.data());
81078107
}
81088108

8109+
void ClangAs::AddAArch64TargetArgs(const ArgList &Args,
8110+
ArgStringList &CmdArgs) const {
8111+
const llvm::Triple &Triple = getToolChain().getTriple();
8112+
8113+
// In the assembler, arm64e support mostly consists of setting an ABI version.
8114+
// It also enables various preprocessor macros, but that's done before -cc1as.
8115+
if (Triple.isArm64e()) {
8116+
// The ptrauth ABI version is 0 by default, but can be overridden.
8117+
static const constexpr unsigned DefaultPtrauthABIVersion = 0;
8118+
8119+
unsigned PtrAuthABIVersion = DefaultPtrauthABIVersion;
8120+
const Arg *A = Args.getLastArg(options::OPT_fptrauth_abi_version_EQ,
8121+
options::OPT_fno_ptrauth_abi_version);
8122+
bool HasVersionArg =
8123+
A && A->getOption().matches(options::OPT_fptrauth_abi_version_EQ);
8124+
if (HasVersionArg) {
8125+
unsigned PtrAuthABIVersionArg;
8126+
if (StringRef(A->getValue()).getAsInteger(10, PtrAuthABIVersionArg))
8127+
getToolChain().getDriver().Diag(diag::err_drv_invalid_value)
8128+
<< A->getAsString(Args) << A->getValue();
8129+
else
8130+
PtrAuthABIVersion = PtrAuthABIVersionArg;
8131+
}
8132+
8133+
// Pass the ABI version to -cc1, regardless of its value, if the user asked
8134+
// for it or if the user didn't explicitly disable it.
8135+
if (HasVersionArg || !Args.hasArg(options::OPT_fno_ptrauth_abi_version)) {
8136+
CmdArgs.push_back(Args.MakeArgString("-fptrauth-abi-version=" +
8137+
llvm::utostr(PtrAuthABIVersion)));
8138+
8139+
// -f(no-)ptrauth-kernel-abi-version can override -mkernel and
8140+
// -fapple-kext
8141+
if (Args.hasArg(options::OPT_fptrauth_kernel_abi_version,
8142+
options::OPT_mkernel, options::OPT_fapple_kext) &&
8143+
!Args.hasArg(options::OPT_fno_ptrauth_kernel_abi_version))
8144+
CmdArgs.push_back("-fptrauth-kernel-abi-version");
8145+
}
8146+
}
8147+
}
8148+
81098149
void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
81108150
const InputInfo &Output, const InputInfoList &Inputs,
81118151
const ArgList &Args,
@@ -8296,6 +8336,7 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
82968336
CmdArgs.push_back("-mllvm");
82978337
CmdArgs.push_back("-aarch64-mark-bti-property");
82988338
}
8339+
AddAArch64TargetArgs(Args, CmdArgs);
82998340
break;
83008341

83018342
case llvm::Triple::riscv32:

clang/lib/Driver/ToolChains/Clang.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ class LLVM_LIBRARY_VISIBILITY ClangAs : public Tool {
137137
llvm::opt::ArgStringList &CmdArgs) const;
138138
void AddRISCVTargetArgs(const llvm::opt::ArgList &Args,
139139
llvm::opt::ArgStringList &CmdArgs) const;
140+
void AddAArch64TargetArgs(const llvm::opt::ArgList &Args,
141+
llvm::opt::ArgStringList &CmdArgs) const;
140142
bool hasGoodDiagnostics() const override { return true; }
141143
bool hasIntegratedAssembler() const override { return false; }
142144
bool hasIntegratedCPP() const override { return false; }

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,38 @@ void DarwinClang::addClangTargetOptions(
12441244
// On arm64e, enable pointer authentication (for the return address and
12451245
// indirect calls), as well as usage of the intrinsics.
12461246
if (getArchName() == "arm64e") {
1247+
// The ptrauth ABI version is 0 by default, but can be overridden.
1248+
static const constexpr unsigned DefaultPtrauthABIVersion = 0;
1249+
1250+
unsigned PtrAuthABIVersion = DefaultPtrauthABIVersion;
1251+
const Arg *A = DriverArgs.getLastArg(options::OPT_fptrauth_abi_version_EQ,
1252+
options::OPT_fno_ptrauth_abi_version);
1253+
bool HasVersionArg =
1254+
A && A->getOption().matches(options::OPT_fptrauth_abi_version_EQ);
1255+
if (HasVersionArg) {
1256+
unsigned PtrAuthABIVersionArg;
1257+
if (StringRef(A->getValue()).getAsInteger(10, PtrAuthABIVersionArg))
1258+
getDriver().Diag(diag::err_drv_invalid_value)
1259+
<< A->getAsString(DriverArgs) << A->getValue();
1260+
else
1261+
PtrAuthABIVersion = PtrAuthABIVersionArg;
1262+
}
1263+
1264+
// Pass the ABI version to -cc1, regardless of its value, if the user asked
1265+
// for it or if the user didn't explicitly disable it.
1266+
if (HasVersionArg ||
1267+
!DriverArgs.hasArg(options::OPT_fno_ptrauth_abi_version)) {
1268+
CC1Args.push_back(DriverArgs.MakeArgString(
1269+
"-fptrauth-abi-version=" + llvm::utostr(PtrAuthABIVersion)));
1270+
1271+
// -f(no-)ptrauth-kernel-abi-version can override -mkernel and
1272+
// -fapple-kext
1273+
if (DriverArgs.hasArg(options::OPT_fptrauth_kernel_abi_version,
1274+
options::OPT_mkernel, options::OPT_fapple_kext) &&
1275+
!DriverArgs.hasArg(options::OPT_fno_ptrauth_kernel_abi_version))
1276+
CC1Args.push_back("-fptrauth-kernel-abi-version");
1277+
}
1278+
12471279
if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
12481280
options::OPT_fno_ptrauth_returns))
12491281
CC1Args.push_back("-fptrauth-returns");

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3655,15 +3655,30 @@ static void GeneratePointerAuthArgs(LangOptions &Opts,
36553655
GenerateArg(Args, OPT_fptrauth_auth_traps, SA);
36563656
if (Opts.SoftPointerAuth)
36573657
GenerateArg(Args, OPT_fptrauth_soft, SA);
3658+
3659+
if (Opts.PointerAuthABIVersionEncoded) {
3660+
GenerateArg(Args, OPT_fptrauth_abi_version_EQ,
3661+
Twine(Opts.PointerAuthABIVersion), SA);
3662+
if (Opts.PointerAuthKernelABIVersion)
3663+
GenerateArg(Args, OPT_fptrauth_kernel_abi_version, SA);
3664+
}
36583665
}
36593666

3660-
static void ParsePointerAuthArgs(LangOptions &Opts, ArgList &Args) {
3667+
static void ParsePointerAuthArgs(LangOptions &Opts, ArgList &Args,
3668+
DiagnosticsEngine &Diags) {
36613669
Opts.PointerAuthIntrinsics = Args.hasArg(OPT_fptrauth_intrinsics);
36623670
Opts.PointerAuthCalls = Args.hasArg(OPT_fptrauth_calls);
36633671
Opts.PointerAuthReturns = Args.hasArg(OPT_fptrauth_returns);
36643672
Opts.PointerAuthIndirectGotos = Args.hasArg(OPT_fptrauth_indirect_gotos);
36653673
Opts.PointerAuthAuthTraps = Args.hasArg(OPT_fptrauth_auth_traps);
36663674
Opts.SoftPointerAuth = Args.hasArg(OPT_fptrauth_soft);
3675+
3676+
Opts.PointerAuthABIVersionEncoded =
3677+
Args.hasArg(OPT_fptrauth_abi_version_EQ) ||
3678+
Args.hasArg(OPT_fptrauth_kernel_abi_version);
3679+
Opts.PointerAuthABIVersion =
3680+
getLastArgIntValue(Args, OPT_fptrauth_abi_version_EQ, 0, Diags);
3681+
Opts.PointerAuthKernelABIVersion = Args.hasArg(OPT_fptrauth_kernel_abi_version);
36673682
}
36683683

36693684
/// Check if input file kind and language standard are compatible.
@@ -4958,7 +4973,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
49584973
ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags,
49594974
Res.getFileSystemOpts().WorkingDir);
49604975
ParseAPINotesArgs(Res.getAPINotesOpts(), Args, Diags);
4961-
ParsePointerAuthArgs(LangOpts, Args);
4976+
ParsePointerAuthArgs(LangOpts, Args, Diags);
49624977

49634978
ParseLangArgs(LangOpts, Args, DashX, T, Res.getPreprocessorOpts().Includes,
49644979
Diags);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %clang_cc1 %s -triple arm64e-apple-ios -disable-llvm-passes -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-NONE
2+
// RUN: %clang_cc1 %s -fptrauth-kernel-abi-version -triple arm64e-apple-ios -disable-llvm-passes -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-WITH --check-prefix=CHECK-ZEROK
3+
// RUN: %clang_cc1 %s -fptrauth-abi-version=0 -triple arm64e-apple-ios -disable-llvm-passes -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-WITH --check-prefix=CHECK-ZERO
4+
// RUN: %clang_cc1 %s -fptrauth-abi-version=0 -fptrauth-kernel-abi-version -triple arm64e-apple-ios -disable-llvm-passes -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-WITH --check-prefix=CHECK-ZEROK
5+
// RUN: %clang_cc1 %s -fptrauth-abi-version=5 -triple arm64e-apple-ios -disable-llvm-passes -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-WITH --check-prefix=CHECK-FIVE
6+
// RUN: %clang_cc1 %s -fptrauth-abi-version=5 -fptrauth-kernel-abi-version -triple arm64e-apple-ios -disable-llvm-passes -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-WITH --check-prefix=CHECK-FIVEK
7+
8+
int f(void) {
9+
return 0;
10+
}
11+
// CHECK-NONE-NOT: ptrauth.abi-version
12+
// CHECK-WITH: !llvm.module.flags = !{{{.*}} ![[ABI_VERSION_REF:[0-9]+]]}
13+
// CHECK-WITH: ![[ABI_VERSION_REF]] = !{i32 6, !"ptrauth.abi-version", ![[ABI_VERSION_VAR:[0-9]+]]}
14+
// CHECK-WITH: ![[ABI_VERSION_VAR]] = !{![[ABI_VERSION_VAL:[0-9]+]]}
15+
// CHECK-ZERO: ![[ABI_VERSION_VAL]] = !{i32 0, i1 false}
16+
// CHECK-ZEROK: ![[ABI_VERSION_VAL]] = !{i32 0, i1 true}
17+
// CHECK-FIVE: ![[ABI_VERSION_VAL]] = !{i32 5, i1 false}
18+
// CHECK-FIVEK: ![[ABI_VERSION_VAL]] = !{i32 5, i1 true}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Check the ABI version support defaults.
2+
3+
// RUN: %clang -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION-DEFAULT --check-prefix NOKERNELABIVERSION
4+
// RUN: %clang -mkernel -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION-DEFAULT --check-prefix KERNELABIVERSION
5+
// RUN: %clang -fapple-kext -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION-DEFAULT --check-prefix KERNELABIVERSION
6+
//
7+
// RUN: %clang -fno-ptrauth-kernel-abi-version -mkernel -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION-DEFAULT --check-prefix NOKERNELABIVERSION
8+
// RUN: %clang -mkernel -fno-ptrauth-kernel-abi-version -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION-DEFAULT --check-prefix NOKERNELABIVERSION
9+
// RUN: %clang -fno-ptrauth-kernel-abi-version -fapple-kext -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION-DEFAULT --check-prefix NOKERNELABIVERSION
10+
// RUN: %clang -fapple-kext -fno-ptrauth-kernel-abi-version -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION-DEFAULT --check-prefix NOKERNELABIVERSION
11+
// RUN: %clang -fno-ptrauth-kernel-abi-version -fptrauth-kernel-abi-version -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION-DEFAULT --check-prefix NOKERNELABIVERSION
12+
// RUN: %clang -fptrauth-kernel-abi-version -fno-ptrauth-kernel-abi-version -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION-DEFAULT --check-prefix NOKERNELABIVERSION
13+
//
14+
// ABIVERSION-DEFAULT: "-fptrauth-abi-version=0"
15+
// KERNELABIVERSION: "-fptrauth-kernel-abi-version"
16+
// NOKERNELABIVERSION-NOT: fptrauth-kernel-abi-version
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Check the ABI version support.
2+
3+
// RUN: %clang -fptrauth-abi-version=5 -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION --check-prefix NOKERNELABIVERSION
4+
// RUN: %clang -fptrauth-abi-version=5 -mkernel -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION --check-prefix KERNELABIVERSION
5+
// RUN: %clang -fptrauth-abi-version=5 -fapple-kext -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION --check-prefix KERNELABIVERSION
6+
// RUN: %clang -fptrauth-abi-version=5 -fptrauth-kernel-abi-version -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION --check-prefix KERNELABIVERSION
7+
8+
// RUN: %clang -fno-ptrauth-abi-version -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix NOABIVERSION --check-prefix NOKERNELABIVERSION
9+
// RUN: %clang -fptrauth-abi-version=5 -fno-ptrauth-abi-version -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix NOABIVERSION --check-prefix NOKERNELABIVERSION
10+
// RUN: %clang -fno-ptrauth-abi-version -fptrauth-abi-version=5 -arch arm64e -c %s -### 2>&1 | FileCheck %s --check-prefix ABIVERSION --check-prefix NOKERNELABIVERSION
11+
12+
// ABIVERSION: "-fptrauth-abi-version=5"
13+
// ABIVERSION-DEFAULT: "-fptrauth-abi-version=0"
14+
// NOABIVERSION-NOT: fptrauth-abi-version
15+
// KERNELABIVERSION: "-fptrauth-kernel-abi-version"
16+
// NOKERNELABIVERSION-NOT: fptrauth-kernel-abi-version
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang_cc1 -E -dM -ffreestanding -triple=arm64e-apple-ios < /dev/null | FileCheck %s --check-prefix=NONE
2+
// RUN: %clang_cc1 -E -dM -ffreestanding -triple=arm64e-apple-ios -fptrauth-abi-version=0 < /dev/null | FileCheck %s --check-prefix=ZERO
3+
// RUN: %clang_cc1 -E -dM -ffreestanding -triple=arm64e-apple-ios -fptrauth-kernel-abi-version < /dev/null | FileCheck %s --check-prefix=ZERO
4+
// RUN: %clang_cc1 -E -dM -ffreestanding -triple=arm64e-apple-ios -fptrauth-abi-version=5 < /dev/null | FileCheck %s --check-prefix=FIVE
5+
6+
// ZERO: #define __ptrauth_abi_version__ 0
7+
// FIVE: #define __ptrauth_abi_version__ 5
8+
// NONE-NOT: __ptrauth_abi_version__

clang/tools/driver/cc1as_main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ struct AssemblerInvocation {
152152
/// for which the code is being compiled.
153153
llvm::Optional<llvm::Triple> DarwinTargetVariantTriple;
154154

155+
/// The ptrauth ABI version targeted by the backend.
156+
unsigned PointerAuthABIVersion;
157+
/// Whether the ptrauth ABI version represents a kernel ABI.
158+
unsigned PointerAuthKernelABIVersion : 1;
159+
/// Whether the assembler should encode the ptrauth ABI version.
160+
unsigned PointerAuthABIVersionEncoded : 1;
161+
155162
/// The name of a file to use with \c .secure_log_unique directives.
156163
std::string AsSecureLogFile;
157164
/// @}
@@ -318,6 +325,14 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
318325
Args.hasArg(OPT_mincremental_linker_compatible);
319326
Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym);
320327

328+
Opts.PointerAuthABIVersionEncoded =
329+
Args.hasArg(OPT_fptrauth_abi_version_EQ) ||
330+
Args.hasArg(OPT_fptrauth_kernel_abi_version);
331+
Opts.PointerAuthABIVersion =
332+
getLastArgIntValue(Args, OPT_fptrauth_abi_version_EQ, 0, Diags);
333+
Opts.PointerAuthKernelABIVersion =
334+
Args.hasArg(OPT_fptrauth_kernel_abi_version);
335+
321336
// EmbedBitcode Option. If -fembed-bitcode is enabled, set the flag.
322337
// EmbedBitcode behaves the same for all embed options for assembly files.
323338
if (auto *A = Args.getLastArg(OPT_fembed_bitcode_EQ)) {
@@ -537,6 +552,11 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
537552
// Assembly to object compilation should leverage assembly info.
538553
Str->setUseAssemblerInfoForParsing(true);
539554

555+
// Emit the ptrauth ABI version, if any.
556+
if (Opts.PointerAuthABIVersionEncoded)
557+
Str->EmitPtrAuthABIVersion(Opts.PointerAuthABIVersion,
558+
Opts.PointerAuthKernelABIVersion);
559+
540560
bool Failed = false;
541561

542562
std::unique_ptr<MCAsmParser> Parser(

llvm/docs/LangRef.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7516,6 +7516,11 @@ stored at::
75167516
!llvm.embedded.objects = !{!0}
75177517
!0 = !{ptr @object, !".section"}
75187518

7519+
Pointer Authentication ABI Version Module Flags Metadata
7520+
--------------------------------------------------------
7521+
7522+
FIXME: write about abi version, maybe?
7523+
75197524
Automatic Linker Flags Named Metadata
75207525
=====================================
75217526

llvm/include/llvm/BinaryFormat/MachO.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,8 +1641,39 @@ enum CPUSubTypeARM64 {
16411641
CPU_SUBTYPE_ARM64_ALL = 0,
16421642
CPU_SUBTYPE_ARM64_V8 = 1,
16431643
CPU_SUBTYPE_ARM64E = 2,
1644+
1645+
// arm64 reserves bits in the high byte for subtype-specific flags.
1646+
// On arm64e, the 6 low bits represent the ptrauth ABI version.
1647+
CPU_SUBTYPE_ARM64E_PTRAUTH_MASK = 0x3f000000,
1648+
// On arm64e, the top bit tells whether the Mach-O is versioned.
1649+
CPU_SUBTYPE_ARM64E_VERSIONED_PTRAUTH_ABI_MASK = 0x80000000,
1650+
// On arm64e, the 2nd high bit tells whether the Mach-O is using kernel ABI.
1651+
CPU_SUBTYPE_ARM64E_KERNEL_PTRAUTH_ABI_MASK = 0x40000000
16441652
};
16451653

1654+
inline int CPU_SUBTYPE_ARM64E_PTRAUTH_VERSION(unsigned ST) {
1655+
return (ST & CPU_SUBTYPE_ARM64E_PTRAUTH_MASK) >> 24;
1656+
}
1657+
1658+
inline unsigned
1659+
CPU_SUBTYPE_ARM64E_WITH_PTRAUTH_VERSION(unsigned PtrAuthABIVersion,
1660+
bool PtrAuthKernelABIVersion) {
1661+
assert((PtrAuthABIVersion <= 0x3F) &&
1662+
"ptrauth abi version must fit in 6 bits");
1663+
return CPU_SUBTYPE_ARM64E | CPU_SUBTYPE_ARM64E_VERSIONED_PTRAUTH_ABI_MASK |
1664+
(PtrAuthKernelABIVersion ? CPU_SUBTYPE_ARM64E_KERNEL_PTRAUTH_ABI_MASK
1665+
: 0) |
1666+
(PtrAuthABIVersion << 24);
1667+
}
1668+
1669+
inline unsigned CPU_SUBTYPE_ARM64E_IS_VERSIONED_PTRAUTH_ABI(unsigned ST) {
1670+
return ST & CPU_SUBTYPE_ARM64E_VERSIONED_PTRAUTH_ABI_MASK;
1671+
}
1672+
1673+
inline unsigned CPU_SUBTYPE_ARM64E_IS_KERNEL_PTRAUTH_ABI(unsigned ST) {
1674+
return ST & CPU_SUBTYPE_ARM64E_KERNEL_PTRAUTH_ABI_MASK;
1675+
}
1676+
16461677
enum CPUSubTypeARM64_32 { CPU_SUBTYPE_ARM64_32_V8 = 1 };
16471678

16481679
enum CPUSubTypeSPARC { CPU_SUBTYPE_SPARC_ALL = 0 };
@@ -1668,6 +1699,8 @@ enum CPUSubTypePowerPC {
16681699

16691700
Expected<uint32_t> getCPUType(const Triple &T);
16701701
Expected<uint32_t> getCPUSubType(const Triple &T);
1702+
Expected<uint32_t> getCPUSubType(const Triple &T, unsigned PtrAuthABIVersion,
1703+
bool PtrAuthKernelABIVersion);
16711704

16721705
struct x86_thread_state32_t {
16731706
uint32_t eax;

0 commit comments

Comments
 (0)