Skip to content

Commit 1540247

Browse files
authored
Merge pull request #1595 from etcwilde/ewilde/swift-6.0-linker-selection
🍒[Swift 6.0]: Update Generic Unix linker selection
2 parents ac0b93f + 07a5afc commit 1540247

File tree

3 files changed

+8
-64
lines changed

3 files changed

+8
-64
lines changed

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ extension DarwinToolchain {
177177
}
178178
}
179179

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

184184
if let arg = parsedOptions.getLastArgument(.ldPath)?.asSingle {

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,6 @@ import func TSCBasic.lookupExecutablePath
1616
import struct TSCBasic.AbsolutePath
1717

1818
extension GenericUnixToolchain {
19-
private func defaultLinker(for targetTriple: Triple) -> String? {
20-
if targetTriple.os == .openbsd || targetTriple.os == .freeBSD ||
21-
targetTriple.environment == .android ||
22-
targetTriple.isFullyStaticLinux {
23-
return "lld"
24-
}
25-
26-
switch targetTriple.arch {
27-
case .arm, .aarch64, .armeb, .thumb, .thumbeb:
28-
// BFD linker has issues wrt relocation of the protocol conformance
29-
// section on these targets, it also generates COPY relocations for
30-
// final executables, as such, unless specified, we default to gold
31-
// linker.
32-
return "gold"
33-
case .x86, .x86_64, .ppc64, .ppc64le, .systemz:
34-
// BFD linker has issues wrt relocations against protected symbols.
35-
return "gold"
36-
default:
37-
// Otherwise, use the default BFD linker.
38-
return ""
39-
}
40-
}
41-
4219
private func majorArchitectureName(for triple: Triple) -> String {
4320
// The concept of a "major" arch name only applies to Linux triples
4421
guard triple.os == .linux else { return triple.archName }
@@ -71,35 +48,11 @@ extension GenericUnixToolchain {
7148
commandLine.appendFlag("-shared")
7249
fallthrough
7350
case .executable:
74-
// Select the linker to use.
75-
var linker: String?
76-
if let arg = parsedOptions.getLastArgument(.useLd) {
77-
linker = arg.asSingle
51+
// Select the linker to use.
52+
if let arg = parsedOptions.getLastArgument(.useLd)?.asSingle {
53+
commandLine.appendFlag("--fuse-ld=\(arg)")
7854
} else if lto != nil {
79-
linker = "lld"
80-
} else {
81-
linker = defaultLinker(for: targetTriple)
82-
}
83-
84-
if let linker = linker {
85-
#if os(Haiku)
86-
// For now, passing -fuse-ld on Haiku doesn't work as swiftc doesn't
87-
// recognise it. Passing -use-ld= as the argument works fine.
88-
commandLine.appendFlag("-use-ld=\(linker)")
89-
#else
90-
commandLine.appendFlag("-fuse-ld=\(linker)")
91-
#endif
92-
// Starting with lld 13, Swift stopped working with the lld
93-
// --gc-sections implementation for ELF, unless -z nostart-stop-gc is
94-
// also passed to lld:
95-
//
96-
// https://reviews.llvm.org/D96914
97-
if linker == "lld" || linker.hasSuffix("ld.lld") {
98-
commandLine.appendFlag(.Xlinker)
99-
commandLine.appendFlag("-z")
100-
commandLine.appendFlag(.Xlinker)
101-
commandLine.appendFlag("nostart-stop-gc")
102-
}
55+
commandLine.appendFlag("--fuse-ld=lld")
10356
}
10457

10558
if let arg = parsedOptions.getLastArgument(.ldPath)?.asSingle {

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,20 +2401,11 @@ final class SwiftDriverTests: XCTestCase {
24012401

24022402
do {
24032403
// The Android NDK only uses the lld linker now
2404-
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "aarch64-unknown-linux-android24"], env: env)
2404+
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "aarch64-unknown-linux-android24", "-use-ld=lld"], env: env)
24052405
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
24062406
let lastJob = plannedJobs.last!
24072407
XCTAssertTrue(lastJob.tool.name.contains("clang"))
2408-
XCTAssertTrue(lastJob.commandLine.contains(subsequence: [.flag("-fuse-ld=lld"),
2409-
.flag("-Xlinker"), .flag("-z"), .flag("-Xlinker"), .flag("nostart-stop-gc")]))
2410-
}
2411-
2412-
do {
2413-
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "x86_64-unknown-freebsd"], env: env)
2414-
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
2415-
let lastJob = plannedJobs.last!
2416-
XCTAssertTrue(lastJob.tool.name.contains("clang"))
2417-
XCTAssertTrue(lastJob.commandLine.contains(.flag("-fuse-ld=lld")))
2408+
XCTAssertTrue(lastJob.commandLine.contains(.flag("--fuse-ld=lld")))
24182409
}
24192410
}
24202411

0 commit comments

Comments
 (0)