[PATCH] D71157: [ELF] Refine section group --gc-sections rules to not discard .debug_types

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 6 16:53:10 PST 2019


MaskRay created this revision.
MaskRay added reviewers: dblaikie, grimar, peter.smith, ruiu.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

clang/gcc -fdebug-type-sections places .debug_types and
.rela.debug_types in a section group, with a signature symbol which
represents the type signature. The section group is for deduplication
purposes.

After D70146 <https://reviews.llvm.org/D70146>, we will discard such section groups. Refine the rule so
that we will retain the group if no member has the SHF_ALLOC flag.

GNU ld has a similar rule to retain the group if all members have the
SEC_DEBUGGING flag. We try to be more general.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71157

Files:
  lld/ELF/InputFiles.cpp
  lld/test/ELF/gc-sections-group-debug.s
  lld/test/ELF/gc-sections-group.s


Index: lld/test/ELF/gc-sections-group.s
===================================================================
--- lld/test/ELF/gc-sections-group.s
+++ lld/test/ELF/gc-sections-group.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 ## Check that group members are retained or discarded as a unit, and
-## non-SHF_ALLOC sections in a group are subject to garbage collection.
-## This is compatible with GNU ld.
+## non-SHF_ALLOC sections in a group are subject to garbage collection,
+## if at least one member has the SHF_ALLOC flag.
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
 # RUN: ld.lld --gc-sections %t.o -o %t.dead
Index: lld/test/ELF/gc-sections-group-debug.s
===================================================================
--- /dev/null
+++ lld/test/ELF/gc-sections-group-debug.s
@@ -0,0 +1,12 @@
+# REQUIRES: x86
+## Check that group members are retained, if no member has the SHF_ALLOC flag.
+## This rule retains .debug_types and .rela.debug_types emitted by clang/gcc.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+# RUN: ld.lld --gc-sections %t.o -o %t
+# RUN: llvm-readobj -S %t | FileCheck %s
+
+# CHECK: Name: .debug_types
+
+.section .debug_types,"G", at progbits,abcd,comdat
+.quad .debug_types
Index: lld/ELF/InputFiles.cpp
===================================================================
--- lld/ELF/InputFiles.cpp
+++ lld/ELF/InputFiles.cpp
@@ -618,6 +618,22 @@
   // For each secion group, connect its members in a circular doubly-linked list
   // via nextInSectionGroup. See the comment in markLive().
   for (ArrayRef<Elf_Word> entries : selectedGroups) {
+    bool hasAlloc = false;
+    for (uint32_t secIndex : entries.slice(1)) {
+      if (secIndex >= this->sections.size())
+        continue;
+      InputSectionBase *s = this->sections[secIndex];
+      if (!s || s == &InputSection::discarded)
+        continue;
+      if (s->flags & SHF_ALLOC)
+        hasAlloc = true;
+    }
+
+    // If no member has the SHF_ALLOC flag, retain the whole group. This rule
+    // retains .debug_types and .rela.debug_types.
+    if (!hasAlloc)
+      continue;
+
     InputSectionBase *head;
     InputSectionBase *prev = nullptr;
     for (uint32_t secIndex : entries.slice(1)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71157.232679.patch
Type: text/x-patch
Size: 2219 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191207/fd818733/attachment.bin>


More information about the llvm-commits mailing list