Skip to content

Commit cc0e122

Browse files
authored
[Driver] Work around lld 13+ issue with --gc-sections for ELF by adding -z nostart-stop-gc (#60544)
Fixes #60406 Ran the linux x86_64 compiler validation suite on this pull with `-use-ld=lld -Xlinker --gc-sections`, then without stripping as in the linked issue, and only saw the 20 unrelated failures just like @drodriguez had.
1 parent 7c721c0 commit cc0e122

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/Driver/UnixToolChains.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
184184
#else
185185
Arguments.push_back(context.Args.MakeArgString("-fuse-ld=" + Linker));
186186
#endif
187+
// Starting with lld 13, Swift stopped working with the lld --gc-sections
188+
// implementation for ELF, unless -z nostart-stop-gc is also passed to lld:
189+
//
190+
// https://reviews.llvm.org/D96914
191+
if (Linker == "lld" || (Linker.length() > 5 &&
192+
Linker.substr(Linker.length() - 6) == "ld.lld")) {
193+
Arguments.push_back("-Xlinker");
194+
Arguments.push_back("-z");
195+
Arguments.push_back("-Xlinker");
196+
Arguments.push_back("nostart-stop-gc");
197+
}
187198
}
188199

189200
// Configure the toolchain.

test/Driver/link-time-opt.swift

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// CHECK-SIMPLE-THIN-linux-gnu: clang
1717
// CHECK-SIMPLE-THIN-linux-gnu-DAG: -flto=thin
1818
// CHECK-SIMPLE-THIN-linux-gnu-DAG: -fuse-ld=lld
19+
// CHECK-SIMPLE-THIN-linux-gnu-DAG: -Xlinker -z -Xlinker nostart-stop-gc
1920
// CHECK-SIMPLE-THIN-linux-gnu-DAG: [[BITCODEFILE]]
2021
// CHECK-SIMPLE-THIN-linux-gnu-NOT: swift-autolink-extract
2122

@@ -37,6 +38,7 @@
3738
// CHECK-SIMPLE-FULL-linux-gnu: clang
3839
// CHECK-SIMPLE-FULL-linux-gnu-DAG: -flto=full
3940
// CHECK-SIMPLE-FULL-linux-gnu-DAG: -fuse-ld=lld
41+
// CHECK-SIMPLE-FULL-linux-gnu-DAG: -Xlinker -z -Xlinker nostart-stop-gc
4042
// CHECK-SIMPLE-FULL-linux-gnu-DAG: [[BITCODEFILE]]
4143
// CHECK-SIMPLE-FULL-linux-gnu-NOT: swift-autolink-extract
4244

0 commit comments

Comments
 (0)