Skip to content

Commit 38ab244

Browse files
committed
Enable PACBTI on OpenBSD/arm64.
BTI enforcement is mandatory here, which means if PAC and BTI instructions are not emitted, then the compiled binary gets killed with SIGILL. The 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 55189ba commit 38ab244

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 (const Arg *arg = inputArgs.getLastArg(
208217
options::OPT_experimental_serialize_debug_info)) {
209218
arguments.push_back(

utils/build-script-impl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,9 @@ function swift_c_flags() {
14321432
linux-static-*)
14331433
echo -n " -D_GNU_SOURCE -DHAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME"
14341434
;;
1435+
openbsd-aarch64)
1436+
echo -n " -Xclang=-msign-return-address=non-leaf -Xclang=-msign-return-address-key=a_key -Xclang=-mbranch-target-enforce"
1437+
;;
14351438
esac
14361439
}
14371440

0 commit comments

Comments
 (0)