Skip to content

Commit 59a7770

Browse files
authored
Merge pull request #1596 from apple/revert-1545-ewilde/linker-selection
Revert "Update Generic Unix linker selection"
2 parents 9bd28ec + 3ed6224 commit 59a7770

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ extension DarwinToolchain {
176176
}
177177
}
178178

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

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

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

+51-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,29 @@ 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+
1942
private func majorArchitectureName(for triple: Triple) -> String {
2043
// The concept of a "major" arch name only applies to Linux triples
2144
guard triple.os == .linux else { return triple.archName }
@@ -48,11 +71,35 @@ extension GenericUnixToolchain {
4871
commandLine.appendFlag("-shared")
4972
fallthrough
5073
case .executable:
51-
// Select the linker to use.
52-
if let arg = parsedOptions.getLastArgument(.useLd)?.asSingle {
53-
commandLine.appendFlag("--fuse-ld=\(arg)")
74+
// Select the linker to use.
75+
var linker: String?
76+
if let arg = parsedOptions.getLastArgument(.useLd) {
77+
linker = arg.asSingle
5478
} else if lto != nil {
55-
commandLine.appendFlag("--fuse-ld=lld")
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+
}
56103
}
57104

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

Sources/SwiftDriver/Jobs/WindowsToolchain+LinkerSupport.swift

+2-2
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)?.asSingle {
103-
commandLine.appendFlag("-fuse-ld=\(arg)")
102+
if let arg = parsedOptions.getLastArgument(.useLd) {
103+
commandLine.appendFlag("-fuse-ld=\(arg.asSingle)")
104104
} else if lto != nil {
105105
commandLine.appendFlag("-fuse-ld=lld")
106106
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

+11-2
Original file line numberDiff line numberDiff line change
@@ -2368,11 +2368,20 @@ 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", "-use-ld=lld"], env: env)
2371+
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "aarch64-unknown-linux-android24"], 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(.flag("--fuse-ld=lld")))
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")))
23762385
}
23772386
}
23782387

0 commit comments

Comments
 (0)