[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.
This commit is contained in:
Fangrui Song 2021-07-22 17:09:23 -07:00
parent 54bc2d812e
commit 120b18767c
2 changed files with 10 additions and 3 deletions

View File

@ -168,14 +168,15 @@ void MarkLive<ELFT>::scanEhFrameSection(EhInputSection &eh,
// garbage-collected. This function returns true if a given section is such
// section.
static bool isReserved(InputSectionBase *sec) {
if (sec->nextInSectionGroup)
return false;
switch (sec->type) {
case SHT_FINI_ARRAY:
case SHT_INIT_ARRAY:
case SHT_PREINIT_ARRAY:
return true;
case SHT_NOTE:
// SHT_NOTE sections in a group are subject to garbage collection.
return !sec->nextInSectionGroup;
return true;
default:
StringRef s = sec->name;
return s.startswith(".ctors") || s.startswith(".dtors") ||

View File

@ -10,6 +10,7 @@
## .mynote.ccc is retained because it is not in a group.
# CHECK-DEAD-NOT: Name: .myanote.aaa
# CHECK-DEAD-NOT: Name: .mytext.aaa
# CHECK-DEAD-NOT: Name: .myinit_array.aaa
# CHECK-DEAD-NOT: Name: .mybss.aaa
# CHECK-DEAD-NOT: Name: .mynote.aaa
# CHECK-DEAD-NOT: Name: .myanote.bbb
@ -32,6 +33,7 @@
# CHECK-LIVE-GROUP: Name: .myanote.aaa
# CHECK-LIVE-GROUP: Name: .mytext.aaa
# CHECK-LIVE-GROUP: Name: .myinit_array.aaa
# CHECK-LIVE-GROUP: Name: .mybss.aaa
# CHECK-LIVE-GROUP: Name: .mynote.aaa
# CHECK-LIVE-GROUP-NOT: Name: .myanote.bbb
@ -54,6 +56,7 @@
# CHECK-LIVE-COMDAT-NOT: Name: .myanote.aaa
# CHECK-LIVE-COMDAT-NOT: Name: .mytext.aaa
# CHECK-LIVE-COMDAT-NOT: Name: .myinit_array.aaa
# CHECK-LIVE-COMDAT-NOT: Name: .mybss.aaa
# CHECK-LIVE-COMDAT-NOT: Name: .mynote.aaa
# CHECK-LIVE-COMDAT: Name: .myanote.bbb
@ -73,6 +76,9 @@ anote_aaa:
aaa:
.byte 0
.section .myinit_array.aaa,"awG",@init_array,aaa
.byte 0
.section .mybss.aaa,"awG",@nobits,aaa
bss_aaa:
.byte 0