Skip to content

[6.1 cp] [Clang][Driver] Add arm64e support for Swift’s "-none-macho" target triples in Clang Driver #9850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 67 additions & 57 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1285,65 +1285,9 @@ void DarwinClang::addClangWarningOptions(ArgStringList &CC1Args) const {

void DarwinClang::addClangTargetOptions(
const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
Action::OffloadKind DeviceOffloadKind) const{
Action::OffloadKind DeviceOffloadKind) const {

Darwin::addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind);

// On arm64e, enable pointer authentication (for the return address and
// indirect calls), as well as usage of the intrinsics.
if (getArchName() == "arm64e") {
// The ptrauth ABI version is 0 by default, but can be overridden.
static const constexpr unsigned DefaultPtrauthABIVersion = 0;

unsigned PtrAuthABIVersion = DefaultPtrauthABIVersion;
const Arg *A = DriverArgs.getLastArg(options::OPT_fptrauth_abi_version_EQ,
options::OPT_fno_ptrauth_abi_version);
bool HasVersionArg =
A && A->getOption().matches(options::OPT_fptrauth_abi_version_EQ);
if (HasVersionArg) {
unsigned PtrAuthABIVersionArg;
if (StringRef(A->getValue()).getAsInteger(10, PtrAuthABIVersionArg))
getDriver().Diag(diag::err_drv_invalid_value)
<< A->getAsString(DriverArgs) << A->getValue();
else
PtrAuthABIVersion = PtrAuthABIVersionArg;
}

// Pass the ABI version to -cc1, regardless of its value, if the user asked
// for it or if the user didn't explicitly disable it.
if (HasVersionArg ||
!DriverArgs.hasArg(options::OPT_fno_ptrauth_abi_version)) {
CC1Args.push_back(DriverArgs.MakeArgString(
"-fptrauth-abi-version=" + llvm::utostr(PtrAuthABIVersion)));

// -f(no-)ptrauth-kernel-abi-version can override -mkernel and
// -fapple-kext
if (DriverArgs.hasArg(options::OPT_fptrauth_kernel_abi_version,
options::OPT_mkernel, options::OPT_fapple_kext) &&
!DriverArgs.hasArg(options::OPT_fno_ptrauth_kernel_abi_version))
CC1Args.push_back("-fptrauth-kernel-abi-version");
}

if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
options::OPT_fno_ptrauth_returns))
CC1Args.push_back("-fptrauth-returns");

if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
options::OPT_fno_ptrauth_intrinsics))
CC1Args.push_back("-fptrauth-intrinsics");

if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
options::OPT_fno_ptrauth_calls))
CC1Args.push_back("-fptrauth-calls");

if (!DriverArgs.hasArg(options::OPT_fptrauth_indirect_gotos,
options::OPT_fno_ptrauth_indirect_gotos))
CC1Args.push_back("-fptrauth-indirect-gotos");

if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
options::OPT_fno_ptrauth_auth_traps))
CC1Args.push_back("-fptrauth-auth-traps");
}
}

/// Take a path that speculatively points into Xcode and return the
Expand Down Expand Up @@ -3203,9 +3147,75 @@ bool Darwin::isSizedDeallocationUnavailable() const {
return TargetVersion < sizedDeallocMinVersion(OS);
}

void MachO::addClangTargetOptions(
const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
Action::OffloadKind DeviceOffloadKind) const {

ToolChain::addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind);

// On arm64e, enable pointer authentication (for the return address and
// indirect calls), as well as usage of the intrinsics.
if (getArchName() == "arm64e") {
// The ptrauth ABI version is 0 by default, but can be overridden.
static const constexpr unsigned DefaultPtrauthABIVersion = 0;

unsigned PtrAuthABIVersion = DefaultPtrauthABIVersion;
const Arg *A = DriverArgs.getLastArg(options::OPT_fptrauth_abi_version_EQ,
options::OPT_fno_ptrauth_abi_version);
bool HasVersionArg =
A && A->getOption().matches(options::OPT_fptrauth_abi_version_EQ);
if (HasVersionArg) {
unsigned PtrAuthABIVersionArg;
if (StringRef(A->getValue()).getAsInteger(10, PtrAuthABIVersionArg))
getDriver().Diag(diag::err_drv_invalid_value)
<< A->getAsString(DriverArgs) << A->getValue();
else
PtrAuthABIVersion = PtrAuthABIVersionArg;
}

// Pass the ABI version to -cc1, regardless of its value, if the user asked
// for it or if the user didn't explicitly disable it.
if (HasVersionArg ||
!DriverArgs.hasArg(options::OPT_fno_ptrauth_abi_version)) {
CC1Args.push_back(DriverArgs.MakeArgString(
"-fptrauth-abi-version=" + llvm::utostr(PtrAuthABIVersion)));

// -f(no-)ptrauth-kernel-abi-version can override -mkernel and
// -fapple-kext
if (DriverArgs.hasArg(options::OPT_fptrauth_kernel_abi_version,
options::OPT_mkernel, options::OPT_fapple_kext) &&
!DriverArgs.hasArg(options::OPT_fno_ptrauth_kernel_abi_version))
CC1Args.push_back("-fptrauth-kernel-abi-version");
}

if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
options::OPT_fno_ptrauth_returns))
CC1Args.push_back("-fptrauth-returns");

if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
options::OPT_fno_ptrauth_intrinsics))
CC1Args.push_back("-fptrauth-intrinsics");

if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
options::OPT_fno_ptrauth_calls))
CC1Args.push_back("-fptrauth-calls");

if (!DriverArgs.hasArg(options::OPT_fptrauth_indirect_gotos,
options::OPT_fno_ptrauth_indirect_gotos))
CC1Args.push_back("-fptrauth-indirect-gotos");

if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
options::OPT_fno_ptrauth_auth_traps))
CC1Args.push_back("-fptrauth-auth-traps");
}
}

void Darwin::addClangTargetOptions(
const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
Action::OffloadKind DeviceOffloadKind) const {

MachO::addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind);

// Pass "-faligned-alloc-unavailable" only when the user hasn't manually
// enabled or disabled aligned allocations.
if (!DriverArgs.hasArgNoClaim(options::OPT_faligned_allocation,
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Driver/ToolChains/Darwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
Tool *buildStaticLibTool() const override;
Tool *getTool(Action::ActionClass AC) const override;

void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args,
Action::OffloadKind DeviceOffloadKind) const override;

private:
mutable std::unique_ptr<tools::darwin::Lipo> Lipo;
mutable std::unique_ptr<tools::darwin::Dsymutil> Dsymutil;
Expand Down
13 changes: 8 additions & 5 deletions clang/test/Driver/arch-arm64e.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,33 @@
// RUN: %clang -target arm64e-apple-ios -c %s -### 2>&1 | FileCheck %s --check-prefix DEFAULT
// RUN: %clang -mkernel -target arm64e-apple-ios -c %s -### 2>&1 | FileCheck %s --check-prefix DEFAULT
// RUN: %clang -fapple-kext -target arm64e-apple-ios -c %s -### 2>&1 | FileCheck %s --check-prefix DEFAULT
// DEFAULT: "-fptrauth-returns" "-fptrauth-intrinsics" "-fptrauth-calls" "-fptrauth-indirect-gotos" "-fptrauth-auth-traps" "-target-cpu" "apple-a12"{{.*}}
// DEFAULT: "-fptrauth-returns" "-fptrauth-intrinsics" "-fptrauth-calls" "-fptrauth-indirect-gotos" "-fptrauth-auth-traps" {{.*}}"-target-cpu" "apple-a12"{{.*}}

// RUN: %clang -target arm64e-apple-none-macho -c %s -### 2>&1 | FileCheck %s --check-prefix DEFAULT-MACHO
// DEFAULT-MACHO: "-fptrauth-returns" "-fptrauth-intrinsics" "-fptrauth-calls" "-fptrauth-indirect-gotos" "-fptrauth-auth-traps" {{.*}}"-target-cpu" "apple-a12"{{.*}}


// RUN: %clang -target arm64e-apple-ios -fno-ptrauth-calls -c %s -### 2>&1 | FileCheck %s --check-prefix DEFAULT-NOCALL
// RUN: %clang -mkernel -target arm64e-apple-ios -fno-ptrauth-calls -c %s -### 2>&1 | FileCheck %s --check-prefix DEFAULT-NOCALL
// RUN: %clang -fapple-kext -target arm64e-apple-ios -fno-ptrauth-calls -c %s -### 2>&1 | FileCheck %s --check-prefix DEFAULT-NOCALL
// DEFAULT-NOCALL-NOT: "-fptrauth-calls"
// DEFAULT-NOCALL: "-fptrauth-returns" "-fptrauth-intrinsics" "-fptrauth-indirect-gotos" "-fptrauth-auth-traps" "-target-cpu" "apple-a12"
// DEFAULT-NOCALL: "-fptrauth-returns" "-fptrauth-intrinsics" "-fptrauth-indirect-gotos" "-fptrauth-auth-traps" {{.*}}"-target-cpu" "apple-a12"


// RUN: %clang -target arm64e-apple-ios -fno-ptrauth-returns -c %s -### 2>&1 | FileCheck %s --check-prefix NORET

// NORET-NOT: "-fptrauth-returns"
// NORET: "-fptrauth-intrinsics" "-fptrauth-calls" "-fptrauth-indirect-gotos" "-fptrauth-auth-traps" "-target-cpu" "apple-a12"
// NORET: "-fptrauth-intrinsics" "-fptrauth-calls" "-fptrauth-indirect-gotos" "-fptrauth-auth-traps" {{.*}}"-target-cpu" "apple-a12"

// RUN: %clang -target arm64e-apple-ios -fno-ptrauth-intrinsics -c %s -### 2>&1 | FileCheck %s --check-prefix NOINTRIN

// NOINTRIN: "-fptrauth-returns"
// NOINTRIN-NOT: "-fptrauth-intrinsics"
// NOINTRIN: "-fptrauth-calls" "-fptrauth-indirect-gotos" "-fptrauth-auth-traps" "-target-cpu" "apple-a12"{{.*}}
// NOINTRIN: "-fptrauth-calls" "-fptrauth-indirect-gotos" "-fptrauth-auth-traps" {{.*}}"-target-cpu" "apple-a12"{{.*}}


// RUN: %clang -target arm64e-apple-ios -fno-ptrauth-auth-traps -c %s -### 2>&1 | FileCheck %s --check-prefix NOTRAP
// NOTRAP: "-fptrauth-returns" "-fptrauth-intrinsics" "-fptrauth-calls" "-fptrauth-indirect-gotos" "-target-cpu" "apple-a12"
// NOTRAP: "-fptrauth-returns" "-fptrauth-intrinsics" "-fptrauth-calls" "-fptrauth-indirect-gotos" {{.*}}"-target-cpu" "apple-a12"


// Check the CPU defaults and overrides.
Expand Down