Skip to content

Commit bd0ea04

Browse files
committed
[Clang][Driver] Add arm64e support for Swift’s "-none-macho" target triples in Clang Driver
This change moves arm64e pointer authentication flag computation the the DarwinClang toolchain to the base MachO toolchain. This makes the logic apply to all Mach-O target triples, not just Darwin ones.
1 parent 26c18a0 commit bd0ea04

File tree

2 files changed

+71
-57
lines changed

2 files changed

+71
-57
lines changed

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,65 +1337,9 @@ void DarwinClang::addClangWarningOptions(ArgStringList &CC1Args) const {
13371337

13381338
void DarwinClang::addClangTargetOptions(
13391339
const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
1340-
Action::OffloadKind DeviceOffloadKind) const{
1340+
Action::OffloadKind DeviceOffloadKind) const {
13411341

13421342
Darwin::addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind);
1343-
1344-
// On arm64e, enable pointer authentication (for the return address and
1345-
// indirect calls), as well as usage of the intrinsics.
1346-
if (getArchName() == "arm64e") {
1347-
// The ptrauth ABI version is 0 by default, but can be overridden.
1348-
static const constexpr unsigned DefaultPtrauthABIVersion = 0;
1349-
1350-
unsigned PtrAuthABIVersion = DefaultPtrauthABIVersion;
1351-
const Arg *A = DriverArgs.getLastArg(options::OPT_fptrauth_abi_version_EQ,
1352-
options::OPT_fno_ptrauth_abi_version);
1353-
bool HasVersionArg =
1354-
A && A->getOption().matches(options::OPT_fptrauth_abi_version_EQ);
1355-
if (HasVersionArg) {
1356-
unsigned PtrAuthABIVersionArg;
1357-
if (StringRef(A->getValue()).getAsInteger(10, PtrAuthABIVersionArg))
1358-
getDriver().Diag(diag::err_drv_invalid_value)
1359-
<< A->getAsString(DriverArgs) << A->getValue();
1360-
else
1361-
PtrAuthABIVersion = PtrAuthABIVersionArg;
1362-
}
1363-
1364-
// Pass the ABI version to -cc1, regardless of its value, if the user asked
1365-
// for it or if the user didn't explicitly disable it.
1366-
if (HasVersionArg ||
1367-
!DriverArgs.hasArg(options::OPT_fno_ptrauth_abi_version)) {
1368-
CC1Args.push_back(DriverArgs.MakeArgString(
1369-
"-fptrauth-abi-version=" + llvm::utostr(PtrAuthABIVersion)));
1370-
1371-
// -f(no-)ptrauth-kernel-abi-version can override -mkernel and
1372-
// -fapple-kext
1373-
if (DriverArgs.hasArg(options::OPT_fptrauth_kernel_abi_version,
1374-
options::OPT_mkernel, options::OPT_fapple_kext) &&
1375-
!DriverArgs.hasArg(options::OPT_fno_ptrauth_kernel_abi_version))
1376-
CC1Args.push_back("-fptrauth-kernel-abi-version");
1377-
}
1378-
1379-
if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
1380-
options::OPT_fno_ptrauth_returns))
1381-
CC1Args.push_back("-fptrauth-returns");
1382-
1383-
if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
1384-
options::OPT_fno_ptrauth_intrinsics))
1385-
CC1Args.push_back("-fptrauth-intrinsics");
1386-
1387-
if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
1388-
options::OPT_fno_ptrauth_calls))
1389-
CC1Args.push_back("-fptrauth-calls");
1390-
1391-
if (!DriverArgs.hasArg(options::OPT_fptrauth_indirect_gotos,
1392-
options::OPT_fno_ptrauth_indirect_gotos))
1393-
CC1Args.push_back("-fptrauth-indirect-gotos");
1394-
1395-
if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
1396-
options::OPT_fno_ptrauth_auth_traps))
1397-
CC1Args.push_back("-fptrauth-auth-traps");
1398-
}
13991343
}
14001344

14011345
/// Take a path that speculatively points into Xcode and return the
@@ -3270,9 +3214,75 @@ bool Darwin::isSizedDeallocationUnavailable() const {
32703214
return TargetVersion < sizedDeallocMinVersion(OS);
32713215
}
32723216

3217+
void MachO::addClangTargetOptions(
3218+
const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
3219+
Action::OffloadKind DeviceOffloadKind) const {
3220+
3221+
ToolChain::addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind);
3222+
3223+
// On arm64e, enable pointer authentication (for the return address and
3224+
// indirect calls), as well as usage of the intrinsics.
3225+
if (getArchName() == "arm64e") {
3226+
// The ptrauth ABI version is 0 by default, but can be overridden.
3227+
static const constexpr unsigned DefaultPtrauthABIVersion = 0;
3228+
3229+
unsigned PtrAuthABIVersion = DefaultPtrauthABIVersion;
3230+
const Arg *A = DriverArgs.getLastArg(options::OPT_fptrauth_abi_version_EQ,
3231+
options::OPT_fno_ptrauth_abi_version);
3232+
bool HasVersionArg =
3233+
A && A->getOption().matches(options::OPT_fptrauth_abi_version_EQ);
3234+
if (HasVersionArg) {
3235+
unsigned PtrAuthABIVersionArg;
3236+
if (StringRef(A->getValue()).getAsInteger(10, PtrAuthABIVersionArg))
3237+
getDriver().Diag(diag::err_drv_invalid_value)
3238+
<< A->getAsString(DriverArgs) << A->getValue();
3239+
else
3240+
PtrAuthABIVersion = PtrAuthABIVersionArg;
3241+
}
3242+
3243+
// Pass the ABI version to -cc1, regardless of its value, if the user asked
3244+
// for it or if the user didn't explicitly disable it.
3245+
if (HasVersionArg ||
3246+
!DriverArgs.hasArg(options::OPT_fno_ptrauth_abi_version)) {
3247+
CC1Args.push_back(DriverArgs.MakeArgString(
3248+
"-fptrauth-abi-version=" + llvm::utostr(PtrAuthABIVersion)));
3249+
3250+
// -f(no-)ptrauth-kernel-abi-version can override -mkernel and
3251+
// -fapple-kext
3252+
if (DriverArgs.hasArg(options::OPT_fptrauth_kernel_abi_version,
3253+
options::OPT_mkernel, options::OPT_fapple_kext) &&
3254+
!DriverArgs.hasArg(options::OPT_fno_ptrauth_kernel_abi_version))
3255+
CC1Args.push_back("-fptrauth-kernel-abi-version");
3256+
}
3257+
3258+
if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
3259+
options::OPT_fno_ptrauth_returns))
3260+
CC1Args.push_back("-fptrauth-returns");
3261+
3262+
if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
3263+
options::OPT_fno_ptrauth_intrinsics))
3264+
CC1Args.push_back("-fptrauth-intrinsics");
3265+
3266+
if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
3267+
options::OPT_fno_ptrauth_calls))
3268+
CC1Args.push_back("-fptrauth-calls");
3269+
3270+
if (!DriverArgs.hasArg(options::OPT_fptrauth_indirect_gotos,
3271+
options::OPT_fno_ptrauth_indirect_gotos))
3272+
CC1Args.push_back("-fptrauth-indirect-gotos");
3273+
3274+
if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
3275+
options::OPT_fno_ptrauth_auth_traps))
3276+
CC1Args.push_back("-fptrauth-auth-traps");
3277+
}
3278+
}
3279+
32733280
void Darwin::addClangTargetOptions(
32743281
const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
32753282
Action::OffloadKind DeviceOffloadKind) const {
3283+
3284+
MachO::addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind);
3285+
32763286
// Pass "-faligned-alloc-unavailable" only when the user hasn't manually
32773287
// enabled or disabled aligned allocations.
32783288
if (!DriverArgs.hasArgNoClaim(options::OPT_faligned_allocation,

clang/lib/Driver/ToolChains/Darwin.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
145145
Tool *buildStaticLibTool() const override;
146146
Tool *getTool(Action::ActionClass AC) const override;
147147

148+
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
149+
llvm::opt::ArgStringList &CC1Args,
150+
Action::OffloadKind DeviceOffloadKind) const override;
151+
148152
private:
149153
mutable std::unique_ptr<tools::darwin::Lipo> Lipo;
150154
mutable std::unique_ptr<tools::darwin::Dsymutil> Dsymutil;

0 commit comments

Comments
 (0)