Skip to content

Commit e49afc8

Browse files
authored
Merge pull request #80389 from 3405691582/nobtcfi
Add a build flavor to opt-out of BTCFI on OpenBSD.
2 parents 367ad78 + a341ce5 commit e49afc8

File tree

7 files changed

+52
-1
lines changed

7 files changed

+52
-1
lines changed

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,17 @@ set(SWIFT_DARWIN_XCRUN_TOOLCHAIN "XcodeDefault" CACHE STRING
575575
set(SWIFT_DARWIN_STDLIB_INSTALL_NAME_DIR "/usr/lib/swift" CACHE STRING
576576
"The directory of the install_name for standard library dylibs")
577577

578+
#
579+
# User-configurable OpenBSD-specific options.
580+
#
581+
582+
option(SWIFT_OPENBSD_BTCFI
583+
"Emit branch target identification instructions and sign return addresses when available"
584+
FALSE)
585+
if(SWIFT_OPENBSD_BTCFI)
586+
add_definitions("-DSWIFT_OPENBSD_BTCFI")
587+
endif()
588+
578589
# We don't want to use the same install_name_dir as the standard library which
579590
# will be installed in /usr/lib/swift. These private libraries should continue
580591
# to use @rpath for now.

include/swift/Basic/Platform.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ namespace swift {
7171
/// (eg. in /usr/lib/swift).
7272
bool tripleRequiresRPathForSwiftLibrariesInOS(const llvm::Triple &triple);
7373

74+
/// Returns true if the given triple represents a version of OpenBSD
75+
/// that enforces BTCFI by default.
76+
bool tripleBTCFIByDefaultInOpenBSD(const llvm::Triple &triple);
77+
7478
/// Returns the platform name for a given target triple.
7579
///
7680
/// For example, the iOS simulator has the name "iphonesimulator", while real

lib/Basic/Platform.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ bool swift::tripleRequiresRPathForSwiftLibrariesInOS(
140140
return false;
141141
}
142142

143+
bool swift::tripleBTCFIByDefaultInOpenBSD(const llvm::Triple &triple) {
144+
return triple.isOSOpenBSD() && triple.getArch() == llvm::Triple::aarch64;
145+
}
146+
143147
DarwinPlatformKind swift::getDarwinPlatformKind(const llvm::Triple &triple) {
144148
if (triple.isiOS()) {
145149
if (triple.isTvOS()) {

lib/Basic/TargetInfo.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ void printTripleInfo(const CompilerInvocation &invocation,
171171
out << " \"compatibilityLibraries\": [ ],\n";
172172
}
173173

174+
if (tripleBTCFIByDefaultInOpenBSD(triple)) {
175+
#if SWIFT_OPENBSD_BTCFI
176+
out << " \"openbsdBTCFIEnabled\": true,\n";
177+
#else
178+
out << " \"openbsdBTCFIEnabled\": false,\n";
179+
#endif
180+
} else {
181+
out << " \"openbsdBTCFIEnabled\": false,\n";
182+
}
183+
174184
out << " \"librariesRequireRPath\": "
175185
<< (tripleRequiresRPathForSwiftLibrariesInOS(triple) ? "true" : "false")
176186
<< "\n";

lib/Driver/ToolChains.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,14 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
205205
}
206206

207207
if (Triple.isOSOpenBSD() && Triple.getArch() == llvm::Triple::aarch64) {
208+
#ifdef SWIFT_OPENBSD_BTCFI
208209
arguments.push_back("-Xcc");
209210
arguments.push_back("-Xclang=-mbranch-target-enforce");
210211
arguments.push_back("-Xcc");
211212
arguments.push_back("-Xclang=-msign-return-address=non-leaf");
212213
arguments.push_back("-Xcc");
213214
arguments.push_back("-Xclang=-msign-return-address-key=a_key");
215+
#endif
214216
}
215217

216218
if (inputArgs.getLastArg(options::OPT_experimental_serialize_debug_info)) {

lib/Driver/UnixToolChains.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
211211
#endif
212212
}
213213

214+
if (tripleBTCFIByDefaultInOpenBSD(getTriple())) {
215+
#ifndef SWIFT_OPENBSD_BTCFI
216+
Arguments.push_back("-Xlinker");
217+
Arguments.push_back("-z");
218+
Arguments.push_back("-Xlinker");
219+
Arguments.push_back("nobtcfi");
220+
#endif
221+
}
222+
214223
// Configure the toolchain.
215224
if (const Arg *A = context.Args.getLastArg(options::OPT_tools_directory)) {
216225
StringRef toolchainPath(A->getValue());

utils/build-script-impl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ KNOWN_SETTINGS=(
124124
darwin-toolchain-require-use-os-runtime "0" "When setting up a plist for a toolchain, require the users of the toolchain to link against the OS instead of the packaged toolchain runtime. 0 for false, 1 for true"
125125
darwin-xcrun-toolchain "default" "the name of the toolchain to use on Darwin"
126126

127+
## OpenBSD options
128+
openbsd-btcfi "" "enables BTCFI when possible. May cause stability problems."
129+
127130
## Runtime options
128131
min-runtime-version "" "Used to specify the minimum host runtime version when building the compiler on non-Darwin platforms"
129132

@@ -877,6 +880,12 @@ function set_build_options_for_host() {
877880
swift_cmake_options+=(
878881
-DCOVERAGE_DB="${COVERAGE_DB}"
879882
)
883+
884+
if [[ "${OPENBSD_BTCFI}" ]]; then
885+
swift_cmake_options+=(
886+
-DSWIFT_OPENBSD_BTCFI:BOOL=TRUE
887+
)
888+
fi
880889
}
881890

882891
function configure_default_options() {
@@ -1383,7 +1392,9 @@ function swift_c_flags() {
13831392
echo -n " -D_GNU_SOURCE -DHAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME"
13841393
;;
13851394
openbsd-aarch64)
1386-
echo -n " -Xclang=-msign-return-address=non-leaf -Xclang=-msign-return-address-key=a_key -Xclang=-mbranch-target-enforce"
1395+
if [[ "${OPENBSD_BTCFI}" ]]; then
1396+
echo -n " -Xclang=-msign-return-address=non-leaf -Xclang=-msign-return-address-key=a_key -Xclang=-mbranch-target-enforce"
1397+
fi
13871398
;;
13881399
esac
13891400
}

0 commit comments

Comments
 (0)