Skip to content

Commit 3286312

Browse files
committed
Update Generic Unix linker selection
Not all generic-"unix" environments have the Gold linker available to them, and in some cases, the vendor of the toolchain may provide their own linker. In these cases, the driver should be internally consistent with the toolchain that it is shipped with. Now that we have the clang-linker, we can lean on the linker selection in the clang-linker to determine a default linker. If the clang-linker, and thus, the swift compiler driver, are part of a specific toolchain, that clang-linker should be built for that platform with the appropriate linker defaults set. If someone overrides the linker with `-use-ld`, we should still honour that, but should otherwise be consistent with the appropriate toolchain linker. Fixes: rdar://123061492
1 parent 4163263 commit 3286312

File tree

3 files changed

+8
-54
lines changed

3 files changed

+8
-54
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 & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +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-
return "lld"
23-
}
24-
25-
switch targetTriple.arch {
26-
case .arm, .aarch64, .armeb, .thumb, .thumbeb:
27-
// BFD linker has issues wrt relocation of the protocol conformance
28-
// section on these targets, it also generates COPY relocations for
29-
// final executables, as such, unless specified, we default to gold
30-
// linker.
31-
return "gold"
32-
case .x86, .x86_64, .ppc64, .ppc64le, .systemz:
33-
// BFD linker has issues wrt relocations against protected symbols.
34-
return "gold"
35-
default:
36-
// Otherwise, use the default BFD linker.
37-
return ""
38-
}
39-
}
40-
4119
private func majorArchitectureName(for triple: Triple) -> String {
4220
// The concept of a "major" arch name only applies to Linux triples
4321
guard triple.os == .linux else { return triple.archName }
@@ -70,35 +48,11 @@ extension GenericUnixToolchain {
7048
commandLine.appendFlag("-shared")
7149
fallthrough
7250
case .executable:
73-
// Select the linker to use.
74-
var linker: String?
75-
if let arg = parsedOptions.getLastArgument(.useLd) {
76-
linker = arg.asSingle
51+
// Select the linker to use.
52+
if let arg = parsedOptions.getLastArgument(.useLd)?.asSingle {
53+
commandLine.appendFlag("--fuse-ld=\(arg)")
7754
} else if lto != nil {
78-
linker = "lld"
79-
} else {
80-
linker = defaultLinker(for: targetTriple)
81-
}
82-
83-
if let linker = linker {
84-
#if os(Haiku)
85-
// For now, passing -fuse-ld on Haiku doesn't work as swiftc doesn't
86-
// recognise it. Passing -use-ld= as the argument works fine.
87-
commandLine.appendFlag("-use-ld=\(linker)")
88-
#else
89-
commandLine.appendFlag("-fuse-ld=\(linker)")
90-
#endif
91-
// Starting with lld 13, Swift stopped working with the lld
92-
// --gc-sections implementation for ELF, unless -z nostart-stop-gc is
93-
// also passed to lld:
94-
//
95-
// https://reviews.llvm.org/D96914
96-
if linker == "lld" || linker.hasSuffix("ld.lld") {
97-
commandLine.appendFlag(.Xlinker)
98-
commandLine.appendFlag("-z")
99-
commandLine.appendFlag(.Xlinker)
100-
commandLine.appendFlag("nostart-stop-gc")
101-
}
55+
commandLine.appendFlag("--fuse-ld=lld")
10256
}
10357

10458
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
}

0 commit comments

Comments
 (0)