From 010666d23d7245ede739a71a0a6b91cb84709ea1 Mon Sep 17 00:00:00 2001 From: Butta Date: Sun, 14 Aug 2022 16:10:36 +0530 Subject: [PATCH] Work around lld 13+ issue with --gc-sections for ELF by adding -z nostart-stop-gc --- .../Jobs/GenericUnixToolchain+LinkerSupport.swift | 11 +++++++++++ Tests/SwiftDriverTests/SwiftDriverTests.swift | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift index ff2799a82..c274901b3 100644 --- a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift +++ b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift @@ -84,6 +84,17 @@ extension GenericUnixToolchain { #else commandLine.appendFlag("-fuse-ld=\(linker)") #endif + // Starting with lld 13, Swift stopped working with the lld + // --gc-sections implementation for ELF, unless -z nostart-stop-gc is + // also passed to lld: + // + // https://reviews.llvm.org/D96914 + if linker == "lld" || linker.hasSuffix("ld.lld") { + commandLine.appendFlag(.Xlinker) + commandLine.appendFlag("-z") + commandLine.appendFlag(.Xlinker) + commandLine.appendFlag("nostart-stop-gc") + } } // Configure the toolchain. diff --git a/Tests/SwiftDriverTests/SwiftDriverTests.swift b/Tests/SwiftDriverTests/SwiftDriverTests.swift index c37de6f3e..e04f5acde 100644 --- a/Tests/SwiftDriverTests/SwiftDriverTests.swift +++ b/Tests/SwiftDriverTests/SwiftDriverTests.swift @@ -1923,7 +1923,8 @@ final class SwiftDriverTests: XCTestCase { let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs() let lastJob = plannedJobs.last! XCTAssertTrue(lastJob.tool.name.contains("clang")) - XCTAssertTrue(lastJob.commandLine.contains(.flag("-fuse-ld=lld"))) + XCTAssertTrue(lastJob.commandLine.contains(subsequence: [.flag("-fuse-ld=lld"), + .flag("-Xlinker"), .flag("-z"), .flag("-Xlinker"), .flag("nostart-stop-gc")])) } }