Skip to content

Commit 610c351

Browse files
authored
Enable PACBTI on OpenBSD/arm64. (#78394)
BTI enforcement is mandatory, which means if PAC and BTI instructions are not emitted, then the compiled binary gets killed with SIGILL. The platform default compiler achieves enabling PAC and BTI by embedding the relevant enabled Clang compilation option flags into the local platform toolchain, which affects C/C++ code generation. For Swift however, to achieve the same effect, we would need to add the relevant LLVM module flags in the IRGen process. But, since Swift uses the Clang code generator when doing this, using the same option flag approach will work here as well, and is probably preferable to introducing operating system-dependent logic to the ClangImporter, for example. Finally, the stdlib needs to be built with PACBTI as well, since the stdlib's global constructors get run when a compiled binary does. Since the Swift build uses the just-built Clang for the stdlib, just embed the necessary options into `CMAKE_C_FLAGS` and `CMAKE_CXX_FLAGS` via `build-script-impl`. This will be redundant with the host compiler, but at least it will be thorough.
1 parent 342bbd3 commit 610c351

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/Driver/ToolChains.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
204204
arguments.push_back("-disable-objc-interop");
205205
}
206206

207+
if (Triple.isOSOpenBSD() && Triple.getArch() == llvm::Triple::aarch64) {
208+
arguments.push_back("-Xcc");
209+
arguments.push_back("-Xclang=-mbranch-target-enforce");
210+
arguments.push_back("-Xcc");
211+
arguments.push_back("-Xclang=-msign-return-address=non-leaf");
212+
arguments.push_back("-Xcc");
213+
arguments.push_back("-Xclang=-msign-return-address-key=a_key");
214+
}
215+
207216
if (inputArgs.getLastArg(options::OPT_experimental_serialize_debug_info)) {
208217
arguments.push_back(
209218
inputArgs.MakeArgString(Twine("-experimental-serialize-debug-info")));

utils/build-script-impl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,9 @@ function swift_c_flags() {
14251425
linux-static-*)
14261426
echo -n " -D_GNU_SOURCE -DHAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME"
14271427
;;
1428+
openbsd-aarch64)
1429+
echo -n " -Xclang=-msign-return-address=non-leaf -Xclang=-msign-return-address-key=a_key -Xclang=-mbranch-target-enforce"
1430+
;;
14281431
esac
14291432
}
14301433

0 commit comments

Comments
 (0)