Skip to content

[Driver] Don't hardcode default linker on Linux #80479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 1 addition & 43 deletions lib/Driver/UnixToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,51 +86,9 @@ ToolChain::InvocationInfo toolchains::GenericUnix::constructInvocation(

return II;
}
// Amazon Linux 2023 requires lld as the default linker.
bool isAmazonLinux2023Host() {
std::ifstream file("/etc/os-release");
std::string line;

while (std::getline(file, line)) {
if (line.substr(0, 12) == "PRETTY_NAME=") {
if (line.substr(12) == "\"Amazon Linux 2023\"") {
file.close();
return true;
}
}
}
return false;
}

std::string toolchains::GenericUnix::getDefaultLinker() const {
if (getTriple().isAndroid() || isAmazonLinux2023Host()
|| (getTriple().isMusl()
&& getTriple().getVendor() == llvm::Triple::Swift))
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 "";
}
return "";
}

bool toolchains::GenericUnix::addRuntimeRPath(const llvm::Triple &T,
Expand Down
2 changes: 1 addition & 1 deletion test/Driver/baremetal-target.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %swiftc_driver_plain -target aarch64-unknown-none-none -driver-print-jobs %s 2>&1 | %FileCheck %s

// CHECK: {{.*}}swift{{c|c-legacy-driver|-frontend}}{{(.exe)?"?}} -frontend -c
// CHECK: {{.*}}clang{{(.exe)?"?}} -fuse-ld=gold
// CHECK: {{.*}}clang{{(.exe)?"?}}
1 change: 0 additions & 1 deletion test/Driver/linker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@

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