[PATCH] D24870: [ELF] Don't crash in GC mode when linker script is used and .debug_lines section is present

Eugene Leviant via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 23 08:45:13 PDT 2016


evgeny777 created this revision.
evgeny777 added reviewers: ruiu, rafael.
evgeny777 added subscribers: grimar, ikudrin, llvm-commits.
evgeny777 set the repository for this revision to rL LLVM.
evgeny777 added a project: lld.

This revision depends on:
https://reviews.llvm.org/D24733

.debug_lines is a special section, which may contain section relocations to member of comdat group. This comdat member
section may be discarded when input file is parsed, and this in its turn may cause a crash in addSection, when we'll try to analyze discarded
section header. This patch fixes this.

Repository:
  rL LLVM

https://reviews.llvm.org/D24870

Files:
  ELF/MarkLive.cpp
  test/ELF/linkerscript/Inputs/comdat-gc.s
  test/ELF/linkerscript/comdat-gc.s

Index: test/ELF/linkerscript/comdat-gc.s
===================================================================
--- test/ELF/linkerscript/comdat-gc.s
+++ test/ELF/linkerscript/comdat-gc.s
@@ -0,0 +1,12 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/comdat-gc.s -o %t1
+# RUN: echo "SECTIONS { .text : { *(.text*) } }" > %t.script
+# RUN: ld.lld --gc-sections --script %t.script %t %t1 -o %t2
+# RUN: llvm-readobj -sections -symbols %t2 | FileCheck -check-prefix=GC1 %s
+
+# GC1:     Name: .debug_lines
+
+.section  .text._Z3fooIiEvv,"axG", at progbits,_Z3fooIiEvv,comdat
+  ret
Index: test/ELF/linkerscript/Inputs/comdat-gc.s
===================================================================
--- test/ELF/linkerscript/Inputs/comdat-gc.s
+++ test/ELF/linkerscript/Inputs/comdat-gc.s
@@ -0,0 +1,5 @@
+.section  .text._Z3fooIiEvv,"axG", at progbits,_Z3fooIiEvv,comdat
+  ret
+
+.section .debug_lines,"", at progbits
+  .quad .text._Z3fooIiEvv
Index: ELF/MarkLive.cpp
===================================================================
--- ELF/MarkLive.cpp
+++ ELF/MarkLive.cpp
@@ -81,13 +81,6 @@
 template <class ELFT>
 static void forEachSuccessor(InputSection<ELFT> &Sec,
                              std::function<void(ResolvedReloc<ELFT>)> Fn) {
-  // Skip over discarded sections. This in theory shouldn't happen, because
-  // the ELF spec doesn't allow a relocation to point to a deduplicated
-  // COMDAT section directly. Unfortunately this happens in practice (e.g.
-  // .eh_frame) so we need to add a check.
-  if (&Sec == &InputSection<ELFT>::Discarded)
-    return;
-
   ELFFile<ELFT> &Obj = Sec.getFile()->getObj();
   for (const typename ELFT::Shdr *RelSec : Sec.RelocSections) {
     if (RelSec->sh_type == SHT_RELA) {
@@ -184,8 +177,8 @@
       return true;
 
     return S.startswith(".ctors") || S.startswith(".dtors") ||
-           S.startswith(".init") || S.startswith(".fini") ||
-           S.startswith(".jcr");
+           S.startswith(".debug") || S.startswith(".init") ||
+           S.startswith(".fini") || S.startswith(".jcr");
   }
 }
 
@@ -196,7 +189,11 @@
   SmallVector<InputSection<ELFT> *, 256> Q;
 
   auto Enqueue = [&](ResolvedReloc<ELFT> R) {
-    if (!R.Sec)
+    // Skip over discarded sections. This in theory shouldn't happen, because
+    // the ELF spec doesn't allow a relocation to point to a deduplicated
+    // COMDAT section directly. Unfortunately this happens in practice (e.g.
+    // .eh_frame) so we need to add a check.
+    if (!R.Sec || R.Sec == &InputSection<ELFT>::Discarded)
       return;
 
     // Usually, a whole section is marked as live or dead, but in mergeable


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24870.72290.patch
Type: text/x-patch
Size: 2734 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160923/49a8c5da/attachment.bin>


More information about the llvm-commits mailing list