Skip to content

Commit fcf5c3a

Browse files
authored
Merge pull request #1608 from apple/revert-1596-revert-1545-ewilde/linker-selection
Revert "Revert "Update Generic Unix linker selection""
2 parents dd5bbca + 93378cc commit fcf5c3a

File tree

4 files changed

+10
-66
lines changed

4 files changed

+10
-66
lines changed

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

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

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

183183
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 {

Sources/SwiftDriver/Jobs/WindowsToolchain+LinkerSupport.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ extension WindowsToolchain {
9999
}
100100

101101
// Select the linker to use.
102-
if let arg = parsedOptions.getLastArgument(.useLd) {
103-
commandLine.appendFlag("-fuse-ld=\(arg.asSingle)")
102+
if let arg = parsedOptions.getLastArgument(.useLd)?.asSingle {
103+
commandLine.appendFlag("-fuse-ld=\(arg)")
104104
} else if lto != nil {
105105
commandLine.appendFlag("-fuse-ld=lld")
106106
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

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

23692369
do {
23702370
// The Android NDK only uses the lld linker now
2371-
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "aarch64-unknown-linux-android24"], env: env)
2371+
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "aarch64-unknown-linux-android24", "-use-ld=lld"], env: env)
23722372
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
23732373
let lastJob = plannedJobs.last!
23742374
XCTAssertTrue(lastJob.tool.name.contains("clang"))
2375-
XCTAssertTrue(lastJob.commandLine.contains(subsequence: [.flag("-fuse-ld=lld"),
2376-
.flag("-Xlinker"), .flag("-z"), .flag("-Xlinker"), .flag("nostart-stop-gc")]))
2377-
}
2378-
2379-
do {
2380-
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "x86_64-unknown-freebsd"], env: env)
2381-
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
2382-
let lastJob = plannedJobs.last!
2383-
XCTAssertTrue(lastJob.tool.name.contains("clang"))
2384-
XCTAssertTrue(lastJob.commandLine.contains(.flag("-fuse-ld=lld")))
2375+
XCTAssertTrue(lastJob.commandLine.contains(.flag("--fuse-ld=lld")))
23852376
}
23862377
}
23872378

0 commit comments

Comments
 (0)