From 9ce1baf3f3dd42567ae53409d277be37b23a2ce1 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 3 Feb 2024 20:32:17 +0000 Subject: [PATCH 1/3] Revert "IRGen: Use llvm.compiler.used instead of llvm.used on ELF" This reverts commit 2bbea54dc0bf95d2622f71ecc3e100eff3a19183. --- lib/IRGen/GenDecl.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp index 9f936bbce5ead..e240ac07ac728 100644 --- a/lib/IRGen/GenDecl.cpp +++ b/lib/IRGen/GenDecl.cpp @@ -1030,15 +1030,6 @@ bool LinkInfo::isUsed(IRLinkage IRL) { /// /// This value must have a definition by the time the module is finalized. void IRGenModule::addUsedGlobal(llvm::GlobalValue *global) { - - // As of reviews.llvm.org/D97448 "ELF: Create unique SHF_GNU_RETAIN sections - // for llvm.used global objects" LLVM creates separate sections for globals in - // llvm.used on ELF. Therefore we use llvm.compiler.used on ELF instead. - if (TargetInfo.OutputObjectFormat == llvm::Triple::ELF) { - addCompilerUsedGlobal(global); - return; - } - LLVMUsed.push_back(global); } From 0e410da168b014733142c07916fb899091de96fe Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Sat, 22 Jul 2023 19:41:34 +0000 Subject: [PATCH 2/3] Mark start/stop sections as SHF_GNU_RETAIN Fixes https://github.com/apple/swift-package-manager/issues/5698 --- lib/Driver/UnixToolChains.cpp | 11 ----------- stdlib/public/runtime/SwiftRT-ELF-WASM.cpp | 2 +- test/Driver/link-time-opt.swift | 2 -- 3 files changed, 1 insertion(+), 14 deletions(-) diff --git a/lib/Driver/UnixToolChains.cpp b/lib/Driver/UnixToolChains.cpp index f6856c17e1639..be0472b98e25a 100644 --- a/lib/Driver/UnixToolChains.cpp +++ b/lib/Driver/UnixToolChains.cpp @@ -214,17 +214,6 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job, #else Arguments.push_back(context.Args.MakeArgString("-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.length() > 5 && - Linker.substr(Linker.length() - 6) == "ld.lld")) { - Arguments.push_back("-Xlinker"); - Arguments.push_back("-z"); - Arguments.push_back("-Xlinker"); - Arguments.push_back("nostart-stop-gc"); - } } // Configure the toolchain. diff --git a/stdlib/public/runtime/SwiftRT-ELF-WASM.cpp b/stdlib/public/runtime/SwiftRT-ELF-WASM.cpp index cbb0b7641d896..980c7e9630f39 100644 --- a/stdlib/public/runtime/SwiftRT-ELF-WASM.cpp +++ b/stdlib/public/runtime/SwiftRT-ELF-WASM.cpp @@ -36,7 +36,7 @@ static const void *__backtraceRef __attribute__((used)) // by the linker. Otherwise, we may end up with undefined symbol references as // the linker table section was never constructed. #if defined(__ELF__) -# define DECLARE_EMPTY_METADATA_SECTION(name) __asm__("\t.section " #name ",\"a\"\n"); +# define DECLARE_EMPTY_METADATA_SECTION(name) __asm__("\t.section " #name ",\"aR\"\n"); #elif defined(__wasm__) # define DECLARE_EMPTY_METADATA_SECTION(name) __asm__("\t.section " #name ",\"\",@\n"); #endif diff --git a/test/Driver/link-time-opt.swift b/test/Driver/link-time-opt.swift index 8cc918e8cd833..4d505be9b28b0 100644 --- a/test/Driver/link-time-opt.swift +++ b/test/Driver/link-time-opt.swift @@ -14,7 +14,6 @@ // CHECK-SIMPLE-THIN-linux-gnu: clang // CHECK-SIMPLE-THIN-linux-gnu-DAG: -flto=thin // CHECK-SIMPLE-THIN-linux-gnu-DAG: -fuse-ld=lld -// CHECK-SIMPLE-THIN-linux-gnu-DAG: -Xlinker -z -Xlinker nostart-stop-gc // CHECK-SIMPLE-THIN-linux-gnu-DAG: [[BITCODEFILE]] // CHECK-SIMPLE-THIN-linux-gnu-NOT: swift-autolink-extract @@ -36,7 +35,6 @@ // CHECK-SIMPLE-FULL-linux-gnu: clang // CHECK-SIMPLE-FULL-linux-gnu-DAG: -flto=full // CHECK-SIMPLE-FULL-linux-gnu-DAG: -fuse-ld=lld -// CHECK-SIMPLE-FULL-linux-gnu-DAG: -Xlinker -z -Xlinker nostart-stop-gc // CHECK-SIMPLE-FULL-linux-gnu-DAG: [[BITCODEFILE]] // CHECK-SIMPLE-FULL-linux-gnu-NOT: swift-autolink-extract From 05e2fc1fb47260d0d3cf947b3483dd794987fb17 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 7 Feb 2024 07:02:27 +0000 Subject: [PATCH 3/3] Stop using `SHF_GNU_RETAIN` for some reflection sections To have consistent section flags between compiler emitted object files and swiftrt.o. --- stdlib/public/runtime/SwiftRT-ELF-WASM.cpp | 37 ++++++++++++++-------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/stdlib/public/runtime/SwiftRT-ELF-WASM.cpp b/stdlib/public/runtime/SwiftRT-ELF-WASM.cpp index 980c7e9630f39..a69fb45a521e3 100644 --- a/stdlib/public/runtime/SwiftRT-ELF-WASM.cpp +++ b/stdlib/public/runtime/SwiftRT-ELF-WASM.cpp @@ -36,31 +36,40 @@ static const void *__backtraceRef __attribute__((used)) // by the linker. Otherwise, we may end up with undefined symbol references as // the linker table section was never constructed. #if defined(__ELF__) -# define DECLARE_EMPTY_METADATA_SECTION(name) __asm__("\t.section " #name ",\"aR\"\n"); +# define DECLARE_EMPTY_METADATA_SECTION(name) __asm__("\t.section " #name ",\"a\"\n"); +# define DECLARE_EMPTY_RETAINED_METADATA_SECTION(name) __asm__("\t.section " #name ",\"aR\"\n"); #elif defined(__wasm__) # define DECLARE_EMPTY_METADATA_SECTION(name) __asm__("\t.section " #name ",\"\",@\n"); +# define DECLARE_EMPTY_RETAINED_METADATA_SECTION(name) DECLAREDECLARE_EMPTY_METADATA_SECTION(name) #endif -#define DECLARE_SWIFT_SECTION(name) \ - DECLARE_EMPTY_METADATA_SECTION(name) \ +#define DECLARE_SWIFT_ENCAPSULATION_SYMBOLS(name) \ __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __start_##name; \ __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __stop_##name; +#define DECLARE_SWIFT_SECTION(name) \ + DECLARE_EMPTY_METADATA_SECTION(name) \ + DECLARE_SWIFT_ENCAPSULATION_SYMBOLS(name) + +#define DECLARE_SWIFT_RETAINED_SECTION(name) \ + DECLARE_EMPTY_RETAINED_METADATA_SECTION(name) \ + DECLARE_SWIFT_ENCAPSULATION_SYMBOLS(name) + extern "C" { -DECLARE_SWIFT_SECTION(swift5_protocols) -DECLARE_SWIFT_SECTION(swift5_protocol_conformances) -DECLARE_SWIFT_SECTION(swift5_type_metadata) +DECLARE_SWIFT_RETAINED_SECTION(swift5_protocols) +DECLARE_SWIFT_RETAINED_SECTION(swift5_protocol_conformances) +DECLARE_SWIFT_RETAINED_SECTION(swift5_type_metadata) DECLARE_SWIFT_SECTION(swift5_typeref) DECLARE_SWIFT_SECTION(swift5_reflstr) -DECLARE_SWIFT_SECTION(swift5_fieldmd) -DECLARE_SWIFT_SECTION(swift5_assocty) -DECLARE_SWIFT_SECTION(swift5_replace) -DECLARE_SWIFT_SECTION(swift5_replac2) -DECLARE_SWIFT_SECTION(swift5_builtin) -DECLARE_SWIFT_SECTION(swift5_capture) -DECLARE_SWIFT_SECTION(swift5_mpenum) -DECLARE_SWIFT_SECTION(swift5_accessible_functions) +DECLARE_SWIFT_RETAINED_SECTION(swift5_fieldmd) +DECLARE_SWIFT_RETAINED_SECTION(swift5_assocty) +DECLARE_SWIFT_RETAINED_SECTION(swift5_replace) +DECLARE_SWIFT_RETAINED_SECTION(swift5_replac2) +DECLARE_SWIFT_RETAINED_SECTION(swift5_builtin) +DECLARE_SWIFT_RETAINED_SECTION(swift5_capture) +DECLARE_SWIFT_RETAINED_SECTION(swift5_mpenum) +DECLARE_SWIFT_RETAINED_SECTION(swift5_accessible_functions) DECLARE_SWIFT_SECTION(swift5_runtime_attributes) }