Skip to content

Commit aa15b23

Browse files
committed
[IRGen] Use llvm.used on ELF instead of llvm.compiler.used.
We changed to `llvm.compiler.used` because of the behaviour of `gold`, which refuses to coalesce sections that have different `SHF_GNU_RETAIN` flags, which causes problems with metadata. Originally I thought we were going to have to generate two sections with distinct names and have the runtime look for both of them, but it turns out that the runtime only wants to see sections that have `SHF_GNU_RETAIN` in any case. It's really the reflection code that is interested in being able to see non-retained sections. The upshot is that we don't need to use `llvm.compiler.used`; it's just fine if we have duplicate sections, as long as the reflection code looks for them when it's inspecting an ELF image. This also means we no longer need to pass `-z nostart-stop-gc` to the linker if we're using `lld`. rdar://123504095
1 parent d5e0ca5 commit aa15b23

10 files changed

+11
-35
lines changed

lib/Driver/UnixToolChains.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,6 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
214214
#else
215215
Arguments.push_back(context.Args.MakeArgString("-fuse-ld=" + Linker));
216216
#endif
217-
// Starting with lld 13, Swift stopped working with the lld --gc-sections
218-
// implementation for ELF, unless -z nostart-stop-gc is also passed to lld:
219-
//
220-
// https://reviews.llvm.org/D96914
221-
if (Linker == "lld" || (Linker.length() > 5 &&
222-
Linker.substr(Linker.length() - 6) == "ld.lld")) {
223-
Arguments.push_back("-Xlinker");
224-
Arguments.push_back("-z");
225-
Arguments.push_back("-Xlinker");
226-
Arguments.push_back("nostart-stop-gc");
227-
}
228217
}
229218

230219
// Configure the toolchain.

lib/IRGen/GenDecl.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,15 +1030,6 @@ bool LinkInfo::isUsed(IRLinkage IRL) {
10301030
///
10311031
/// This value must have a definition by the time the module is finalized.
10321032
void IRGenModule::addUsedGlobal(llvm::GlobalValue *global) {
1033-
1034-
// As of reviews.llvm.org/D97448 "ELF: Create unique SHF_GNU_RETAIN sections
1035-
// for llvm.used global objects" LLVM creates separate sections for globals in
1036-
// llvm.used on ELF. Therefore we use llvm.compiler.used on ELF instead.
1037-
if (TargetInfo.OutputObjectFormat == llvm::Triple::ELF) {
1038-
addCompilerUsedGlobal(global);
1039-
return;
1040-
}
1041-
10421033
LLVMUsed.push_back(global);
10431034
}
10441035

test/Distributed/distributed_actor_accessor_section_elf.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public distributed actor MyOtherActor {
137137
// CHECK-SAME: (ptr @"$s27distributed_actor_accessors12MyOtherActorC5emptyyyYaKFTETFTu" to i{{32|64}})
138138
// CHECK-SAME: , section "swift5_accessible_functions", {{.*}}
139139

140-
// CHECK: @llvm.compiler.used = appending global [{{.*}} x ptr] [
140+
// CHECK: @llvm.used = appending global [{{.*}} x ptr] [
141141
// CHECK-SAME: @"$s27distributed_actor_accessors7MyActorC7simple1yySiYaKFTEHF"
142142
// CHECK-SAME: @"$s27distributed_actor_accessors7MyActorC7simple2ySSSiYaKFTEHF"
143143
// CHECK-SAME: @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSYaKFTEHF"

test/Driver/link-time-opt.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
// CHECK-SIMPLE-THIN-linux-gnu: clang
1515
// CHECK-SIMPLE-THIN-linux-gnu-DAG: -flto=thin
1616
// CHECK-SIMPLE-THIN-linux-gnu-DAG: -fuse-ld=lld
17-
// CHECK-SIMPLE-THIN-linux-gnu-DAG: -Xlinker -z -Xlinker nostart-stop-gc
1817
// CHECK-SIMPLE-THIN-linux-gnu-DAG: [[BITCODEFILE]]
1918
// CHECK-SIMPLE-THIN-linux-gnu-NOT: swift-autolink-extract
2019

@@ -36,7 +35,6 @@
3635
// CHECK-SIMPLE-FULL-linux-gnu: clang
3736
// CHECK-SIMPLE-FULL-linux-gnu-DAG: -flto=full
3837
// CHECK-SIMPLE-FULL-linux-gnu-DAG: -fuse-ld=lld
39-
// CHECK-SIMPLE-FULL-linux-gnu-DAG: -Xlinker -z -Xlinker nostart-stop-gc
4038
// CHECK-SIMPLE-FULL-linux-gnu-DAG: [[BITCODEFILE]]
4139
// CHECK-SIMPLE-FULL-linux-gnu-NOT: swift-autolink-extract
4240

test/IRGen/ELF-remove-autolink-section.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
print("Hi from Swift!")
1414

15-
// ELF: @llvm.compiler.used = {{.*}}ptr @_swift1_autolink_entries
15+
// ELF: @llvm.used = {{.*}}ptr @_swift1_autolink_entries
1616

1717
// SECTION: .swift1_autolink_entries
1818
// NOSECTION-NOT: .swift1_autolink_entries

test/IRGen/autolink_elf.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ import Empty
1010
// as used.
1111

1212
// CHECK-DAG: @_swift1_autolink_entries = private constant [26 x i8] c"-lswiftEmpty\00-lanotherLib\00", section ".swift1_autolink_entries",{{.*}} align 8
13-
// CHECK-DAG: @llvm.compiler.used = appending global [{{.*}} x ptr] [{{.*}}ptr @_swift1_autolink_entries{{.*}}], section "llvm.metadata"
14-
13+
// CHECK-DAG: @llvm.used = appending global [{{.*}} x ptr] [{{.*}}ptr @_swift1_autolink_entries{{.*}}], section "llvm.metadata"

test/IRGen/objc_protocol_vars.sil

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@ import Foundation
2727
}
2828

2929
// CHECK-macho: @llvm.used = appending global [{{.*}}] [{{.*}}, ptr @"\01l_OBJC_LABEL_PROTOCOL_$__TtP18objc_protocol_vars1T_", ptr @"\01l_OBJC_PROTOCOL_REFERENCE_$__TtP18objc_protocol_vars1T_", {{.*}}], {{.*}}
30-
// CHECK-elf: @llvm.compiler.used = appending global [{{.*}}] [{{.*}}, ptr @"\01l_OBJC_LABEL_PROTOCOL_$__TtP18objc_protocol_vars1T_", ptr @"\01l_OBJC_PROTOCOL_REFERENCE_$__TtP18objc_protocol_vars1T_", {{.*}}], {{.*}}
30+
// CHECK-elf: @llvm.used = appending global [{{.*}}] [{{.*}}, ptr @"\01l_OBJC_LABEL_PROTOCOL_$__TtP18objc_protocol_vars1T_", ptr @"\01l_OBJC_PROTOCOL_REFERENCE_$__TtP18objc_protocol_vars1T_", {{.*}}], {{.*}}
3131
// CHECK-coff: @llvm.used = appending global [{{.*}}] [{{.*}}, ptr @"\01l_OBJC_LABEL_PROTOCOL_$__TtP18objc_protocol_vars1T_", ptr @"\01l_OBJC_PROTOCOL_REFERENCE_$__TtP18objc_protocol_vars1T_", {{.*}}], {{.*}}
32-

test/IRGen/section_asm.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
// REQUIRES: swift_in_compiler
33
// UNSUPPORTED: CPU=wasm32
44

5-
// CHECK: .section{{.*}}__TEXT,__mysection
5+
// CHECK: .section{{.*}}"__TEXT,__mysection","ax"
66
// CHECK-NOT: .section
77
// CHECK: $s7section3fooyyF:
88

9-
// CHECK: .section{{.*}}__TEXT,__mysection
9+
// CHECK: .section{{.*}}"__TEXT,__mysection","ax"
1010
// CHECK-NOT: .section
1111
// CHECK: $s7section8MyStructV3fooyyF:
1212

13-
// CHECK: .section{{.*}}__DATA,__mysection
13+
// CHECK: .section{{.*}}"__DATA,__mysection","aw"
1414
// CHECK-NOT: .section
1515
// CHECK: $s7section2g0Sivp:
1616
// CHECK-NOT: .section
1717
// CHECK: $s7section2g1Si_Sitvp:
1818
// CHECK-NOT: .section
1919
// CHECK: $s7section2g2Sbvp:
20-
// CHECK-NOT: .section
20+
// CHECK: .section{{.*}}"__DATA,__mysection","awR"
2121
// CHECK: $s7section2g3Sbvp:
22-
// CHECK-NOT: .section
22+
// CHECK: .section{{.*}}"__DATA,__mysection","aw"
2323
// CHECK: $s7section2g4SpySiGSgvp:
2424
// CHECK-NOT: .section
2525
// CHECK: $s7section2g5SpySiGSgvp:

test/IRGen/unused.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>
5656
// CHECK-elf: @"\01l_entry_point" = private constant { i32, i32 } { i32 trunc (i64 sub (i64 ptrtoint (ptr @main to i64), i64 ptrtoint (ptr @"\01l_entry_point" to i64)) to i32), i32 0 }, section "swift5_entry", align 4
5757

5858
// CHECK-macho: @llvm.used = appending global [4 x ptr] [ptr @frieda, ptr @main, ptr @"\01l_entry_point", ptr @__swift_reflection_version], section "llvm.metadata"
59-
// CHECK-elf: @llvm.compiler.used = appending global [5 x ptr] [ptr @frieda, ptr @main, ptr @"\01l_entry_point", ptr @__swift_reflection_version, ptr @_swift1_autolink_entries], section "llvm.metadata"
59+
// CHECK-elf: @llvm.used = appending global [5 x ptr] [ptr @frieda, ptr @main, ptr @"\01l_entry_point", ptr @__swift_reflection_version, ptr @_swift1_autolink_entries], section "llvm.metadata"
6060

6161
// CHECK: define linkonce_odr hidden swiftcc void @qux()
6262
// CHECK: define hidden swiftcc void @fred()

test/embedded/internalize-no-stdlib.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public func main() {
2626
}
2727

2828
// CHECK-ELF: @_swift1_autolink_entries =
29-
// CHECK-ELF: @llvm.compiler.used = appending global [1 x ptr] [ptr @_swift1_autolink_entries], section "llvm.metadata"
29+
// CHECK-ELF: @llvm.used = appending global [1 x ptr] [ptr @_swift1_autolink_entries], section "llvm.metadata"
3030
// CHECK-ELF-NOT: @llvm.used
3131

3232
// CHECK-MACHO-NOT: @llvm.compiler.used

0 commit comments

Comments
 (0)