Skip to content

🍒[Swift 6.0]: Update Generic Unix linker selection #1595

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

Merged
Merged
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
4 changes: 2 additions & 2 deletions Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ extension DarwinToolchain {
}
}

if let arg = parsedOptions.getLastArgument(.useLd) {
commandLine.appendFlag("-fuse-ld=\(arg.asSingle)")
if let arg = parsedOptions.getLastArgument(.useLd)?.asSingle {
commandLine.appendFlag("-fuse-ld=\(arg)")
}

if let arg = parsedOptions.getLastArgument(.ldPath)?.asSingle {
Expand Down
55 changes: 4 additions & 51 deletions Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,6 @@ import func TSCBasic.lookupExecutablePath
import struct TSCBasic.AbsolutePath

extension GenericUnixToolchain {
private func defaultLinker(for targetTriple: Triple) -> String? {
if targetTriple.os == .openbsd || targetTriple.os == .freeBSD ||
targetTriple.environment == .android ||
targetTriple.isFullyStaticLinux {
return "lld"
}

switch targetTriple.arch {
case .arm, .aarch64, .armeb, .thumb, .thumbeb:
// BFD linker has issues wrt relocation of the protocol conformance
// section on these targets, it also generates COPY relocations for
// final executables, as such, unless specified, we default to gold
// linker.
return "gold"
case .x86, .x86_64, .ppc64, .ppc64le, .systemz:
// BFD linker has issues wrt relocations against protected symbols.
return "gold"
default:
// Otherwise, use the default BFD linker.
return ""
}
}

private func majorArchitectureName(for triple: Triple) -> String {
// The concept of a "major" arch name only applies to Linux triples
guard triple.os == .linux else { return triple.archName }
Expand Down Expand Up @@ -71,35 +48,11 @@ extension GenericUnixToolchain {
commandLine.appendFlag("-shared")
fallthrough
case .executable:
// Select the linker to use.
var linker: String?
if let arg = parsedOptions.getLastArgument(.useLd) {
linker = arg.asSingle
// Select the linker to use.
if let arg = parsedOptions.getLastArgument(.useLd)?.asSingle {
commandLine.appendFlag("--fuse-ld=\(arg)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be -fuse-ld=\(arg). Only one dash.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe cherry-pick #1624 as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already posted: #1725

} else if lto != nil {
linker = "lld"
} else {
linker = defaultLinker(for: targetTriple)
}

if let linker = linker {
#if os(Haiku)
// For now, passing -fuse-ld on Haiku doesn't work as swiftc doesn't
// recognise it. Passing -use-ld= as the argument works fine.
commandLine.appendFlag("-use-ld=\(linker)")
#else
commandLine.appendFlag("-fuse-ld=\(linker)")
#endif
// Starting with lld 13, Swift stopped working with the lld
// --gc-sections implementation for ELF, unless -z nostart-stop-gc is
// also passed to lld:
//
// https://reviews.llvm.org/D96914
if linker == "lld" || linker.hasSuffix("ld.lld") {
commandLine.appendFlag(.Xlinker)
commandLine.appendFlag("-z")
commandLine.appendFlag(.Xlinker)
commandLine.appendFlag("nostart-stop-gc")
}
commandLine.appendFlag("--fuse-ld=lld")
}

if let arg = parsedOptions.getLastArgument(.ldPath)?.asSingle {
Expand Down
13 changes: 2 additions & 11 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2401,20 +2401,11 @@ final class SwiftDriverTests: XCTestCase {

do {
// The Android NDK only uses the lld linker now
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "aarch64-unknown-linux-android24"], env: env)
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "aarch64-unknown-linux-android24", "-use-ld=lld"], env: env)
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
let lastJob = plannedJobs.last!
XCTAssertTrue(lastJob.tool.name.contains("clang"))
XCTAssertTrue(lastJob.commandLine.contains(subsequence: [.flag("-fuse-ld=lld"),
.flag("-Xlinker"), .flag("-z"), .flag("-Xlinker"), .flag("nostart-stop-gc")]))
}

do {
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "x86_64-unknown-freebsd"], env: env)
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
let lastJob = plannedJobs.last!
XCTAssertTrue(lastJob.tool.name.contains("clang"))
XCTAssertTrue(lastJob.commandLine.contains(.flag("-fuse-ld=lld")))
XCTAssertTrue(lastJob.commandLine.contains(.flag("--fuse-ld=lld")))
}
}

Expand Down