Skip to content

Commit 56a7884

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 b385bbf commit 56a7884

File tree

3 files changed

+8
-55
lines changed

3 files changed

+8
-55
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
}

0 commit comments

Comments
 (0)