Skip to content

Commit c771555

Browse files
finagolfinkeith
authored andcommitted
Work around lld 13+ issue with --gc-sections for ELF by adding -z nostart-stop-gc
1 parent 21d9350 commit c771555

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ extension GenericUnixToolchain {
8484
#else
8585
commandLine.appendFlag("-fuse-ld=\(linker)")
8686
#endif
87+
// Starting with lld 13, Swift stopped working with the lld
88+
// --gc-sections implementation for ELF, unless -z nostart-stop-gc is
89+
// also passed to lld:
90+
//
91+
// https://reviews.llvm.org/D96914
92+
if linker == "lld" || linker.hasSuffix("ld.lld") {
93+
commandLine.appendFlag(.Xlinker)
94+
commandLine.appendFlag("-z")
95+
commandLine.appendFlag(.Xlinker)
96+
commandLine.appendFlag("nostart-stop-gc")
97+
}
8798
}
8899

89100
// Configure the toolchain.

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,8 @@ final class SwiftDriverTests: XCTestCase {
20712071
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
20722072
let lastJob = plannedJobs.last!
20732073
XCTAssertTrue(lastJob.tool.name.contains("clang"))
2074-
XCTAssertTrue(lastJob.commandLine.contains(.flag("-fuse-ld=lld")))
2074+
XCTAssertTrue(lastJob.commandLine.contains(subsequence: [.flag("-fuse-ld=lld"),
2075+
.flag("-Xlinker"), .flag("-z"), .flag("-Xlinker"), .flag("nostart-stop-gc")]))
20752076
}
20762077
}
20772078

0 commit comments

Comments
 (0)