Skip to content

Commit abb2485

Browse files
committed
Detect OpenBSD opt-out of BTCFI.
On the swift side, we added a new build flavor in swiftlang/swift#80389 to opt-out of BTCFI as a way of working around swiftlang/swift#80059. We communicate this to swift-driver via the frontend with FrontendTargetInfo.
1 parent 8b96d1b commit abb2485

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ extension GenericUnixToolchain {
119119
}
120120
}
121121

122+
if targetTriple.os == .openbsd && targetTriple.arch == .aarch64 {
123+
let btcfiEnabled = targetInfo.target.openbsdBTCFIEnabled ?? false
124+
if !btcfiEnabled {
125+
commandLine.appendFlag("-Xlinker")
126+
commandLine.appendFlag("-z")
127+
commandLine.appendFlag("-Xlinker")
128+
commandLine.appendFlag("nobtcfi")
129+
}
130+
}
131+
122132
let staticStdlib = parsedOptions.hasFlag(positive: .staticStdlib,
123133
negative: .noStaticStdlib,
124134
default: false)

Sources/SwiftDriver/Jobs/PrintTargetInfoJob.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public struct FrontendTargetInfo: Codable {
101101
/// Whether the Swift libraries need to be referenced in their system
102102
/// location (/usr/lib/swift) via rpath.
103103
let librariesRequireRPath: Bool
104+
105+
let openbsdBTCFIEnabled: Bool?
104106
}
105107

106108
@_spi(Testing) public struct Paths: Codable {

Sources/SwiftDriver/Toolchains/GenericUnixToolchain.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,14 @@ public final class GenericUnixToolchain: Toolchain {
163163
}
164164

165165
if driver.targetTriple.os == .openbsd && driver.targetTriple.arch == .aarch64 {
166-
commandLine.appendFlag(.Xcc)
167-
commandLine.appendFlag("-Xclang=-msign-return-address=non-leaf")
168-
commandLine.appendFlag(.Xcc)
169-
commandLine.appendFlag("-Xclang=-msign-return-address-key=a_key")
170-
commandLine.appendFlag(.Xcc)
171-
commandLine.appendFlag("-Xclang=-mbranch-target-enforce")
166+
if frontendTargetInfo.target.openbsdBTCFIEnabled ?? false {
167+
commandLine.appendFlag(.Xcc)
168+
commandLine.appendFlag("-Xclang=-msign-return-address=non-leaf")
169+
commandLine.appendFlag(.Xcc)
170+
commandLine.appendFlag("-Xclang=-msign-return-address-key=a_key")
171+
commandLine.appendFlag(.Xcc)
172+
commandLine.appendFlag("-Xclang=-mbranch-target-enforce")
173+
}
172174
}
173175
}
174176
}

0 commit comments

Comments
 (0)