Skip to content

Detect OpenBSD opt-out of BTCFI. #1857

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ extension GenericUnixToolchain {
}
}

if targetTriple.os == .openbsd && targetTriple.arch == .aarch64 {
let btcfiEnabled = targetInfo.target.openbsdBTCFIEnabled ?? false
if !btcfiEnabled {
commandLine.appendFlag("-Xlinker")
commandLine.appendFlag("-z")
commandLine.appendFlag("-Xlinker")
commandLine.appendFlag("nobtcfi")
}
}

let staticStdlib = parsedOptions.hasFlag(positive: .staticStdlib,
negative: .noStaticStdlib,
default: false)
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftDriver/Jobs/PrintTargetInfoJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public struct FrontendTargetInfo: Codable {
/// Whether the Swift libraries need to be referenced in their system
/// location (/usr/lib/swift) via rpath.
let librariesRequireRPath: Bool

let openbsdBTCFIEnabled: Bool?
}

@_spi(Testing) public struct Paths: Codable {
Expand Down
14 changes: 8 additions & 6 deletions Sources/SwiftDriver/Toolchains/GenericUnixToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,14 @@ public final class GenericUnixToolchain: Toolchain {
}

if driver.targetTriple.os == .openbsd && driver.targetTriple.arch == .aarch64 {
commandLine.appendFlag(.Xcc)
commandLine.appendFlag("-Xclang=-msign-return-address=non-leaf")
commandLine.appendFlag(.Xcc)
commandLine.appendFlag("-Xclang=-msign-return-address-key=a_key")
commandLine.appendFlag(.Xcc)
commandLine.appendFlag("-Xclang=-mbranch-target-enforce")
if frontendTargetInfo.target.openbsdBTCFIEnabled ?? false {
commandLine.appendFlag(.Xcc)
commandLine.appendFlag("-Xclang=-msign-return-address=non-leaf")
commandLine.appendFlag(.Xcc)
commandLine.appendFlag("-Xclang=-msign-return-address-key=a_key")
commandLine.appendFlag(.Xcc)
commandLine.appendFlag("-Xclang=-mbranch-target-enforce")
}
}
}
}