[PATCH] D89841: [ELF] --gc-sections: retain dependent sections of non-SHF_ALLOC sections

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 21 10:11:47 PDT 2020


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGb6e4aae2cc26: [ELF] --gc-sections: retain dependent sections of non-SHF_ALLOC sections (authored by MaskRay).

Changed prior to commit:
  https://reviews.llvm.org/D89841?vs=299733&id=299734#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89841/new/

https://reviews.llvm.org/D89841

Files:
  lld/ELF/MarkLive.cpp
  lld/test/ELF/gc-sections-linkorder2.s


Index: lld/test/ELF/gc-sections-linkorder2.s
===================================================================
--- lld/test/ELF/gc-sections-linkorder2.s
+++ lld/test/ELF/gc-sections-linkorder2.s
@@ -14,3 +14,9 @@
 .quad 0
 .section .zed,"ao", at progbits,.foo
 .quad 0
+
+.section .nonalloc
+.quad 0
+
+.section .nonalloc_linkorder,"o", at progbits,.nonalloc
+.quad 0
Index: lld/ELF/MarkLive.cpp
===================================================================
--- lld/ELF/MarkLive.cpp
+++ lld/ELF/MarkLive.cpp
@@ -339,16 +339,16 @@
 
   // Otherwise, do mark-sweep GC.
   //
-  // The -gc-sections option works only for SHF_ALLOC sections
-  // (sections that are memory-mapped at runtime). So we can
-  // unconditionally make non-SHF_ALLOC sections alive except
-  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
+  // The -gc-sections option works only for SHF_ALLOC sections (sections that
+  // are memory-mapped at runtime). So we can unconditionally make non-SHF_ALLOC
+  // sections alive except SHF_LINK_ORDER, SHT_REL/SHT_RELA sections, and
+  // sections in a group.
   //
   // Usually, non-SHF_ALLOC sections are not removed even if they are
-  // unreachable through relocations because reachability is not
-  // a good signal whether they are garbage or not (e.g. there is
-  // usually no section referring to a .comment section, but we
-  // want to keep it.).
+  // unreachable through relocations because reachability is not a good signal
+  // whether they are garbage or not (e.g. there is usually no section referring
+  // to a .comment section, but we want to keep it.) When a non-SHF_ALLOC
+  // section is retained, we also retain sections dependent on it.
   //
   // Note on SHF_LINK_ORDER: Such sections contain metadata and they
   // have a reverse dependency on the InputSection they are linked with.
@@ -370,8 +370,11 @@
     bool isLinkOrder = (sec->flags & SHF_LINK_ORDER);
     bool isRel = (sec->type == SHT_REL || sec->type == SHT_RELA);
 
-    if (!isAlloc && !isLinkOrder && !isRel && !sec->nextInSectionGroup)
+    if (!isAlloc && !isLinkOrder && !isRel && !sec->nextInSectionGroup) {
       sec->markLive();
+      for (InputSection *isec : sec->dependentSections)
+        isec->markLive();
+    }
   }
 
   // Follow the graph to mark all live sections.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89841.299734.patch
Type: text/x-patch
Size: 2299 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201021/c9662ab4/attachment.bin>


More information about the llvm-commits mailing list