Skip to content

Commit 500ddfe

Browse files
committed
[Driver] Don't hardcode default linker on Linux
This is the C++ driver counterpart to a change that landed in the Swift driver a while ago to use the clang-linker to determine what the default linker is. This is to avoid hard-coding gold, which is deprecated and not available on some newer Linux distributions. The challenge is that these newer Linux distributions don't already have Swift so we have to use the old C++ driver implementation.
1 parent d15f6c7 commit 500ddfe

File tree

3 files changed

+2
-45
lines changed

3 files changed

+2
-45
lines changed

lib/Driver/UnixToolChains.cpp

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -86,51 +86,9 @@ ToolChain::InvocationInfo toolchains::GenericUnix::constructInvocation(
8686

8787
return II;
8888
}
89-
// Amazon Linux 2023 requires lld as the default linker.
90-
bool isAmazonLinux2023Host() {
91-
std::ifstream file("/etc/os-release");
92-
std::string line;
93-
94-
while (std::getline(file, line)) {
95-
if (line.substr(0, 12) == "PRETTY_NAME=") {
96-
if (line.substr(12) == "\"Amazon Linux 2023\"") {
97-
file.close();
98-
return true;
99-
}
100-
}
101-
}
102-
return false;
103-
}
10489

10590
std::string toolchains::GenericUnix::getDefaultLinker() const {
106-
if (getTriple().isAndroid() || isAmazonLinux2023Host()
107-
|| (getTriple().isMusl()
108-
&& getTriple().getVendor() == llvm::Triple::Swift))
109-
return "lld";
110-
111-
switch (getTriple().getArch()) {
112-
case llvm::Triple::arm:
113-
case llvm::Triple::aarch64:
114-
case llvm::Triple::aarch64_32:
115-
case llvm::Triple::armeb:
116-
case llvm::Triple::thumb:
117-
case llvm::Triple::thumbeb:
118-
// BFD linker has issues wrt relocation of the protocol conformance
119-
// section on these targets, it also generates COPY relocations for
120-
// final executables, as such, unless specified, we default to gold
121-
// linker.
122-
return "gold";
123-
case llvm::Triple::x86:
124-
case llvm::Triple::x86_64:
125-
case llvm::Triple::ppc64:
126-
case llvm::Triple::ppc64le:
127-
case llvm::Triple::systemz:
128-
// BFD linker has issues wrt relocations against protected symbols.
129-
return "gold";
130-
default:
131-
// Otherwise, use the default BFD linker.
132-
return "";
133-
}
91+
return "";
13492
}
13593

13694
bool toolchains::GenericUnix::addRuntimeRPath(const llvm::Triple &T,

test/Driver/baremetal-target.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// RUN: %swiftc_driver_plain -target aarch64-unknown-none-none -driver-print-jobs %s 2>&1 | %FileCheck %s
22

33
// CHECK: {{.*}}swift{{c|c-legacy-driver|-frontend}}{{(.exe)?"?}} -frontend -c
4-
// CHECK: {{.*}}clang{{(.exe)?"?}} -fuse-ld=gold
4+
// CHECK: {{.*}}clang{{(.exe)?"?}}

test/Driver/linker.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@
344344

345345
// LINUX_DYNLIB-x86_64: clang{{(\.exe)?"? }}
346346
// LINUX_DYNLIB-x86_64-DAG: -shared
347-
// LINUX_DYNLIB-x86_64-DAG: -fuse-ld=gold
348347
// LINUX_DYNLIB-x86_64-NOT: -pie
349348
// LINUX_DYNLIB-x86_64-DAG: -Xlinker -rpath -Xlinker [[STDLIB_PATH:[^ ]+(/|\\\\)lib(/|\\\\)swift(/|\\\\)linux]]
350349
// LINUX_DYNLIB-x86_64: [[STDLIB_PATH]]{{/|\\\\}}x86_64{{/|\\\\}}swiftrt.o

0 commit comments

Comments
 (0)