Skip to content

Commit ff95e04

Browse files
atrosinenkokovdan01
authored andcommitted
Make it possible to enable arm64e-like options via -mbranch-protection=pauthabi
1 parent 63a1313 commit ff95e04

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,74 @@ void AddUnalignedAccessWarning(ArgStringList &CmdArgs) {
15811581
}
15821582
}
15831583

1584+
// See DarwinClang::addClangTargetOptions()
1585+
static void handlePAuthABIOption(const ArgList &DriverArgs,
1586+
ArgStringList &CC1Args,
1587+
const Driver &D) {
1588+
// The ptrauth ABI version is 0 by default, but can be overridden.
1589+
static const constexpr unsigned DefaultPtrauthABIVersion = 0;
1590+
1591+
unsigned PtrAuthABIVersion = DefaultPtrauthABIVersion;
1592+
const Arg *A = DriverArgs.getLastArg(options::OPT_fptrauth_abi_version_EQ,
1593+
options::OPT_fno_ptrauth_abi_version);
1594+
bool HasVersionArg =
1595+
A && A->getOption().matches(options::OPT_fptrauth_abi_version_EQ);
1596+
if (HasVersionArg) {
1597+
unsigned PtrAuthABIVersionArg;
1598+
if (StringRef(A->getValue()).getAsInteger(10, PtrAuthABIVersionArg))
1599+
D.Diag(diag::err_drv_invalid_value)
1600+
<< A->getAsString(DriverArgs) << A->getValue();
1601+
else
1602+
PtrAuthABIVersion = PtrAuthABIVersionArg;
1603+
}
1604+
1605+
// Pass the ABI version to -cc1, regardless of its value, if the user asked
1606+
// for it or if the user didn't explicitly disable it.
1607+
if (HasVersionArg ||
1608+
!DriverArgs.hasArg(options::OPT_fno_ptrauth_abi_version)) {
1609+
CC1Args.push_back(DriverArgs.MakeArgString(
1610+
"-fptrauth-abi-version=" + llvm::utostr(PtrAuthABIVersion)));
1611+
}
1612+
1613+
if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
1614+
options::OPT_fno_ptrauth_returns))
1615+
CC1Args.push_back("-fptrauth-returns");
1616+
1617+
if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
1618+
options::OPT_fno_ptrauth_intrinsics))
1619+
CC1Args.push_back("-fptrauth-intrinsics");
1620+
1621+
if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
1622+
options::OPT_fno_ptrauth_calls))
1623+
CC1Args.push_back("-fptrauth-calls");
1624+
1625+
if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
1626+
options::OPT_fno_ptrauth_auth_traps))
1627+
CC1Args.push_back("-fptrauth-auth-traps");
1628+
1629+
#if 1
1630+
if (!DriverArgs.hasArg(
1631+
options::OPT_fptrauth_block_descriptor_pointers,
1632+
options::OPT_fno_ptrauth_block_descriptor_pointers))
1633+
CC1Args.push_back("-fptrauth-block-descriptor-pointers");
1634+
1635+
if (!DriverArgs.hasArg(
1636+
options::OPT_fptrauth_vtable_pointer_address_discrimination,
1637+
options::OPT_fno_ptrauth_vtable_pointer_address_discrimination))
1638+
CC1Args.push_back("-fptrauth-vtable-pointer-address-discrimination");
1639+
1640+
if (!DriverArgs.hasArg(
1641+
options::OPT_fptrauth_vtable_pointer_type_discrimination,
1642+
options::OPT_fno_ptrauth_vtable_pointer_type_discrimination))
1643+
CC1Args.push_back("-fptrauth-vtable-pointer-type-discrimination");
1644+
1645+
if (!DriverArgs.hasArg(
1646+
options::OPT_fptrauth_function_pointer_type_discrimination,
1647+
options::OPT_fno_ptrauth_function_pointer_type_discrimination))
1648+
CC1Args.push_back("-fptrauth-function-pointer-type-discrimination");
1649+
#endif
1650+
}
1651+
15841652
static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
15851653
ArgStringList &CmdArgs, bool isAArch64) {
15861654
const Arg *A = isAArch64
@@ -1615,9 +1683,14 @@ static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
16151683
if (!isAArch64 && PBP.Key == "b_key")
16161684
D.Diag(diag::warn_unsupported_branch_protection)
16171685
<< "b-key" << A->getAsString(Args);
1686+
if (!isAArch64 && PBP.HasPauthABI)
1687+
D.Diag(diag::warn_unsupported_branch_protection)
1688+
<< "pauthabi" << A->getAsString(Args);
16181689
Scope = PBP.Scope;
16191690
Key = PBP.Key;
16201691
IndirectBranches = PBP.BranchTargetEnforcement;
1692+
if (isAArch64 && PBP.HasPauthABI)
1693+
handlePAuthABIOption(Args, CmdArgs, D);
16211694
}
16221695

16231696
CmdArgs.push_back(

llvm/include/llvm/TargetParser/ARMTargetParserCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct ParsedBranchProtection {
4141
StringRef Scope;
4242
StringRef Key;
4343
bool BranchTargetEnforcement;
44+
bool HasPauthABI;
4445
};
4546

4647
bool parseBranchProtection(StringRef Spec, ParsedBranchProtection &PBP,

llvm/lib/TargetParser/ARMTargetParserCommon.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ ARM::EndianKind ARM::parseArchEndian(StringRef Arch) {
139139
// an erroneous part of the spec.
140140
bool ARM::parseBranchProtection(StringRef Spec, ParsedBranchProtection &PBP,
141141
StringRef &Err) {
142-
PBP = {"none", "a_key", false};
142+
PBP = {"none", "a_key", false, false};
143143
if (Spec == "none")
144144
return true; // defaults are ok
145145

@@ -157,6 +157,10 @@ bool ARM::parseBranchProtection(StringRef Spec, ParsedBranchProtection &PBP,
157157
PBP.BranchTargetEnforcement = true;
158158
continue;
159159
}
160+
if (Opt == "pauthabi") {
161+
PBP.HasPauthABI = true;
162+
continue;
163+
}
160164
if (Opt == "pac-ret") {
161165
PBP.Scope = "non-leaf";
162166
for (; I + 1 != E; ++I) {

0 commit comments

Comments
 (0)