[lld] 120b187 - [ELF] --gc-sections: allow GC on reserved sections in a group

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 22 17:09:28 PDT 2021


Author: Fangrui Song
Date: 2021-07-22T17:09:23-07:00
New Revision: 120b18767c5243520f91e81b3baaa2bcf0d2c4ac

URL: https://github.com/llvm/llvm-project/commit/120b18767c5243520f91e81b3baaa2bcf0d2c4ac
DIFF: https://github.com/llvm/llvm-project/commit/120b18767c5243520f91e81b3baaa2bcf0d2c4ac.diff

LOG: [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.

Added: 
    

Modified: 
    lld/ELF/MarkLive.cpp
    lld/test/ELF/gc-sections-group.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp
index e828429b421c3..0378a19e1919d 100644
--- a/lld/ELF/MarkLive.cpp
+++ b/lld/ELF/MarkLive.cpp
@@ -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") ||

diff  --git a/lld/test/ELF/gc-sections-group.s b/lld/test/ELF/gc-sections-group.s
index da4cad98f3653..0b74f6fa707ea 100644
--- a/lld/test/ELF/gc-sections-group.s
+++ b/lld/test/ELF/gc-sections-group.s
@@ -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", at init_array,aaa
+.byte 0
+
 .section .mybss.aaa,"awG", at nobits,aaa
 bss_aaa:
 .byte 0


        


More information about the llvm-commits mailing list