1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# Swift defaults to building with `gold` on Linux because it doesn't build with
# `bfd`; Gentoo no longer supports `gold`, so we have to build with `lld`.
#
# We need to:
# 1. Have the Swift drivers default to linking with `lld` over `gold` (both for)
# eventual end-user code, but also for the built Swift compiler stages to
# bootstrap the next stage,
# 2. Have the `SWIFT_USE_LINKER` flag be correctly respected everywhere, and
# 3. Set the right linker flags for `lld` because its behavior for ELF on Linux
# differs from other platforms, and certain Swift symbols need to not be GC'd
# out
#
# See https://github.com/swiftlang/swift/pull/69538 (merged to main after 5.10
# branched).
--- a/swift/lib/Driver/UnixToolChains.cpp
+++ b/swift/lib/Driver/UnixToolChains.cpp
@@ -110,34 +110,7 @@ ToolChain::InvocationInfo toolchains::GenericUnix::constructInvocation(
return II;
}
-std::string toolchains::GenericUnix::getDefaultLinker() const {
- if (getTriple().isAndroid())
- return "lld";
-
- switch (getTriple().getArch()) {
- case llvm::Triple::arm:
- case llvm::Triple::aarch64:
- case llvm::Triple::aarch64_32:
- case llvm::Triple::armeb:
- case llvm::Triple::thumb:
- case llvm::Triple::thumbeb:
- // BFD linker has issues wrt relocation of the protocol conformance
- // section on these targets, it also generates COPY relocations for
- // final executables, as such, unless specified, we default to gold
- // linker.
- return "gold";
- case llvm::Triple::x86:
- case llvm::Triple::x86_64:
- case llvm::Triple::ppc64:
- case llvm::Triple::ppc64le:
- case llvm::Triple::systemz:
- // BFD linker has issues wrt relocations against protected symbols.
- return "gold";
- default:
- // Otherwise, use the default BFD linker.
- return "";
- }
-}
+std::string toolchains::GenericUnix::getDefaultLinker() const { return "lld"; }
bool toolchains::GenericUnix::addRuntimeRPath(const llvm::Triple &T,
const llvm::opt::ArgList &Args) const {
--- a/swift-driver/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift
+++ b/swift-driver/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift
@@ -17,25 +17,7 @@ import struct TSCBasic.AbsolutePath
extension GenericUnixToolchain {
private func defaultLinker(for targetTriple: Triple) -> String? {
- if targetTriple.os == .openbsd || targetTriple.os == .freeBSD ||
- targetTriple.environment == .android {
- return "lld"
- }
-
- switch targetTriple.arch {
- case .arm, .aarch64, .armeb, .thumb, .thumbeb:
- // BFD linker has issues wrt relocation of the protocol conformance
- // section on these targets, it also generates COPY relocations for
- // final executables, as such, unless specified, we default to gold
- // linker.
- return "gold"
- case .x86, .x86_64, .ppc64, .ppc64le, .systemz:
- // BFD linker has issues wrt relocations against protected symbols.
- return "gold"
- default:
- // Otherwise, use the default BFD linker.
- return ""
- }
+ "lld"
}
private func majorArchitectureName(for triple: Triple) -> String {
--- a/swift/cmake/modules/AddPureSwift.cmake
+++ b/swift/cmake/modules/AddPureSwift.cmake
@@ -224,6 +224,10 @@ function(add_pure_swift_host_library name)
target_link_options(${name} PRIVATE
"-use-ld=${LLVM_USE_LINKER}"
)
+ elseif(SWIFT_USE_LINKER)
+ target_link_options(${name} PRIVATE
+ "-use-ld=${SWIFT_USE_LINKER}"
+ )
endif()
# Export this target.
@@ -322,6 +326,10 @@ function(add_pure_swift_host_tool name)
target_link_options(${name} PRIVATE
"-use-ld=${LLVM_USE_LINKER}"
)
+ elseif(SWIFT_USE_LINKER)
+ target_link_options(${name} PRIVATE
+ "-use-ld=${SWIFT_USE_LINKER}"
+ )
endif()
# Workaround to touch the library and its objects so that we don't
--- a/swift/cmake/modules/AddSwift.cmake
+++ b/swift/cmake/modules/AddSwift.cmake
@@ -629,6 +629,10 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
endif()
endif()
endif()
+
+ if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND SWIFT_USE_LINKER STREQUAL "lld")
+ target_link_options(${target} PRIVATE "LINKER:-z,nostart-stop-gc")
+ endif()
endif()
set_property(TARGET ${target} PROPERTY BUILD_WITH_INSTALL_RPATH YES)
--- a/swift/tools/libStaticMirror/CMakeLists.txt
+++ b/swift/tools/libStaticMirror/CMakeLists.txt
@@ -29,6 +29,10 @@ add_llvm_symbol_exports(libStaticMirror ${LLVM_EXPORTED_SYMBOL_FILE})
# Adds -dead_strip option
add_link_opts(libStaticMirror)
+if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND SWIFT_USE_LINKER STREQUAL "lld")
+ target_link_options(libStaticMirror PRIVATE "LINKER:-z,nostart-stop-gc")
+endif()
+
add_dependencies(static-mirror-lib libStaticMirror)
swift_install_in_component(TARGETS libStaticMirror
ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}" COMPONENT static-mirror-lib
--- a/swift/tools/libSwiftScan/CMakeLists.txt
+++ b/swift/tools/libSwiftScan/CMakeLists.txt
@@ -67,6 +67,10 @@ add_llvm_symbol_exports(libSwiftScan ${LLVM_EXPORTED_SYMBOL_FILE})
# Adds -dead_strip option
add_link_opts(libSwiftScan)
+if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND SWIFT_USE_LINKER STREQUAL "lld")
+ target_link_options(libSwiftScan PRIVATE "LINKER:-z,nostart-stop-gc")
+endif()
+
add_dependencies(compiler libSwiftScan)
|