[lld] r283984 - Don't crash if reloc targets discarded section

Eugene Leviant via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 12 01:19:31 PDT 2016


Author: evgeny777
Date: Wed Oct 12 03:19:30 2016
New Revision: 283984

URL: http://llvm.org/viewvc/llvm-project?rev=283984&view=rev
Log:
Don't crash if reloc targets discarded section

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

Modified:
    lld/trunk/ELF/Symbols.cpp
    lld/trunk/test/ELF/linkerscript/discard-section.s

Modified: lld/trunk/ELF/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.cpp?rev=283984&r1=283983&r2=283984&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.cpp (original)
+++ lld/trunk/ELF/Symbols.cpp Wed Oct 12 03:19:30 2016
@@ -15,6 +15,7 @@
 #include "Target.h"
 
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Path.h"
 
 using namespace llvm;
 using namespace llvm::object;
@@ -24,6 +25,12 @@ using namespace lld;
 using namespace lld::elf;
 
 template <class ELFT>
+static std::string getSectionName(InputSectionBase<ELFT> *S) {
+  StringRef Filename = S->getFile()->getName();
+  return (sys::path::filename(Filename) + "(" + S->Name + ")").str();
+}
+
+template <class ELFT>
 static typename ELFT::uint getSymVA(const SymbolBody &Body,
                                     typename ELFT::uint &Addend) {
   typedef typename ELFT::uint uintX_t;
@@ -53,6 +60,11 @@ static typename ELFT::uint getSymVA(cons
     if (!SC)
       return D.Value;
 
+    if (!SC->Live) {
+      warn("relocation refers to discarded section '" + getSectionName(SC) + "'");
+      return 0;
+    }
+
     uintX_t Offset = D.Value;
     if (D.isSection()) {
       Offset += Addend;

Modified: lld/trunk/test/ELF/linkerscript/discard-section.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/discard-section.s?rev=283984&r1=283983&r2=283984&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/discard-section.s (original)
+++ lld/trunk/test/ELF/linkerscript/discard-section.s Wed Oct 12 03:19:30 2016
@@ -1,10 +1,15 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: echo "SECTIONS { /DISCARD/ : { *(.aaa*) } }" > %t.script
-# RUN: ld.lld -o %t1 --script %t.script %t
+# RUN: ld.lld -o %t1 --script %t.script %t 2>&1 | FileCheck --check-prefix=WARN %s
 # RUN: llvm-objdump -section-headers %t1 | FileCheck %s
+
+# WARN: relocation refers to discarded section {{.+}}(.aaa)
 # CHECK-NOT: .aaa
 
 .section .aaa,"a"
-aaa:
+aab:
   .quad 0
+
+.section .zzz,"a"
+  .quad aab




More information about the llvm-commits mailing list