Skip to content

Commit 120b187

Browse files
committed
[ELF] --gc-sections: allow GC on reserved sections in a group
This generalizes D70146 (SHT_NOTE) to more reserved sections and makes our rules more consistent. Now SHF_GROUP is more similar to SHF_LINK_ORDER. For SHT_INIT_ARRAY/SHT_FINI_ARRAY, the rule will be closer to PE/COFF link.exe. Previously sanitizers use llvm.global_ctors to make module_ctor a GC root, which is considered an abuse. https://groups.google.com/g/generic-abi/c/TpleUEkNoQI We can squeak through on compatibility issues because compilers otherwise don't use SHF_GROUP special sections.
1 parent 54bc2d8 commit 120b187

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lld/ELF/MarkLive.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,15 @@ void MarkLive<ELFT>::scanEhFrameSection(EhInputSection &eh,
168168
// garbage-collected. This function returns true if a given section is such
169169
// section.
170170
static bool isReserved(InputSectionBase *sec) {
171+
if (sec->nextInSectionGroup)
172+
return false;
173+
171174
switch (sec->type) {
172175
case SHT_FINI_ARRAY:
173176
case SHT_INIT_ARRAY:
174177
case SHT_PREINIT_ARRAY:
175-
return true;
176178
case SHT_NOTE:
177-
// SHT_NOTE sections in a group are subject to garbage collection.
178-
return !sec->nextInSectionGroup;
179+
return true;
179180
default:
180181
StringRef s = sec->name;
181182
return s.startswith(".ctors") || s.startswith(".dtors") ||

lld/test/ELF/gc-sections-group.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
## .mynote.ccc is retained because it is not in a group.
1111
# CHECK-DEAD-NOT: Name: .myanote.aaa
1212
# CHECK-DEAD-NOT: Name: .mytext.aaa
13+
# CHECK-DEAD-NOT: Name: .myinit_array.aaa
1314
# CHECK-DEAD-NOT: Name: .mybss.aaa
1415
# CHECK-DEAD-NOT: Name: .mynote.aaa
1516
# CHECK-DEAD-NOT: Name: .myanote.bbb
@@ -32,6 +33,7 @@
3233

3334
# CHECK-LIVE-GROUP: Name: .myanote.aaa
3435
# CHECK-LIVE-GROUP: Name: .mytext.aaa
36+
# CHECK-LIVE-GROUP: Name: .myinit_array.aaa
3537
# CHECK-LIVE-GROUP: Name: .mybss.aaa
3638
# CHECK-LIVE-GROUP: Name: .mynote.aaa
3739
# CHECK-LIVE-GROUP-NOT: Name: .myanote.bbb
@@ -54,6 +56,7 @@
5456

5557
# CHECK-LIVE-COMDAT-NOT: Name: .myanote.aaa
5658
# CHECK-LIVE-COMDAT-NOT: Name: .mytext.aaa
59+
# CHECK-LIVE-COMDAT-NOT: Name: .myinit_array.aaa
5760
# CHECK-LIVE-COMDAT-NOT: Name: .mybss.aaa
5861
# CHECK-LIVE-COMDAT-NOT: Name: .mynote.aaa
5962
# CHECK-LIVE-COMDAT: Name: .myanote.bbb
@@ -73,6 +76,9 @@ anote_aaa:
7376
aaa:
7477
.byte 0
7578

79+
.section .myinit_array.aaa,"awG",@init_array,aaa
80+
.byte 0
81+
7682
.section .mybss.aaa,"awG",@nobits,aaa
7783
bss_aaa:
7884
.byte 0

0 commit comments

Comments
 (0)