[lld] r332589 - [ELF] - Do not crash when do --gc-sections for non-allocatable metadata sections.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu May 17 03:00:34 PDT 2018


Author: grimar
Date: Thu May 17 03:00:34 2018
New Revision: 332589

URL: http://llvm.org/viewvc/llvm-project?rev=332589&view=rev
Log:
[ELF] - Do not crash when do --gc-sections for non-allocatable metadata sections.

Currently, LLD marks all non-allocatable sections except SHF_REL[A] as Live
when doing GC.

This can be a reason of the crash when SHF_LINK_ORDER sections
are involved, because their parents can be dead.

We should do GC for them correctly. The patch implements it.

Differential revision: https://reviews.llvm.org/D46880

Added:
    lld/trunk/test/ELF/non-alloc-link-order-gc.s
Modified:
    lld/trunk/ELF/MarkLive.cpp

Modified: lld/trunk/ELF/MarkLive.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/MarkLive.cpp?rev=332589&r1=332588&r2=332589&view=diff
==============================================================================
--- lld/trunk/ELF/MarkLive.cpp (original)
+++ lld/trunk/ELF/MarkLive.cpp Thu May 17 03:00:34 2018
@@ -275,13 +275,18 @@ template <class ELFT> void elf::markLive
 
   // 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.
+  // unconditionally make non-SHF_ALLOC sections alive except
+  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
   //
-  // Non SHF_ALLOC sections are not removed even if they are
+  // Usually, 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.)
+  // want to keep it.).
+  //
+  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
+  // have a reverse dependency on the InputSection they are linked with.
+  // We are able to garbage collect them.
   //
   // Note on SHF_REL{,A}: Such sections reach here only when -r
   // or -emit-reloc were given. And they are subject of garbage
@@ -289,8 +294,9 @@ template <class ELFT> void elf::markLive
   // remove its relocation section.
   for (InputSectionBase *Sec : InputSections) {
     bool IsAlloc = (Sec->Flags & SHF_ALLOC);
+    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
     bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
-    if (!IsAlloc && !IsRel)
+    if (!IsAlloc && !IsLinkOrder && !IsRel)
       Sec->Live = true;
   }
 

Added: lld/trunk/test/ELF/non-alloc-link-order-gc.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/non-alloc-link-order-gc.s?rev=332589&view=auto
==============================================================================
--- lld/trunk/test/ELF/non-alloc-link-order-gc.s (added)
+++ lld/trunk/test/ELF/non-alloc-link-order-gc.s Thu May 17 03:00:34 2018
@@ -0,0 +1,34 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: ld.lld %t.o -o %t1 --gc-sections
+# RUN: llvm-objdump -section-headers -D %t1 | FileCheck %s
+
+## Check that we are able to GC non-allocatable metadata sections without crash.
+
+# CHECK:      Disassembly of section .stack_sizes:
+# CHECK-NEXT:   .stack_sizes:
+# CHECK-NEXT:    01
+
+# CHECK:      Name          Size
+# CHECK:      .stack_sizes  00000001
+
+.section .text.live,"ax", at progbits
+.globl live
+live:
+ nop
+
+.section .stack_sizes,"o", at progbits,.text.live,unique,0
+.byte 1
+
+.section .text.dead,"ax", at progbits
+.globl dead
+dead:
+ nop
+
+.section .stack_sizes,"o", at progbits,.text.dead,unique,1
+.byte 2
+
+.section .text.main,"ax", at progbits
+.globl _start
+_start:
+  callq live at PLT




More information about the llvm-commits mailing list