From c958d8d621c3eccd9d0b7799471b8d060fed66df Mon Sep 17 00:00:00 2001 From: Eugene Leviant Date: Wed, 12 Oct 2016 08:19:30 +0000 Subject: [PATCH] Don't crash if reloc targets discarded section Differential revision: https://reviews.llvm.org/D25433 llvm-svn: 283984 --- lld/ELF/Symbols.cpp | 12 ++++++++++++ lld/test/ELF/linkerscript/discard-section.s | 9 +++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp index 7457224..1dcb8cb 100644 --- a/lld/ELF/Symbols.cpp +++ b/lld/ELF/Symbols.cpp @@ -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 +static std::string getSectionName(InputSectionBase *S) { + StringRef Filename = S->getFile()->getName(); + return (sys::path::filename(Filename) + "(" + S->Name + ")").str(); +} + +template 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(const SymbolBody &Body, 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; diff --git a/lld/test/ELF/linkerscript/discard-section.s b/lld/test/ELF/linkerscript/discard-section.s index d7d2acf..edeeee4 100644 --- a/lld/test/ELF/linkerscript/discard-section.s +++ b/lld/test/ELF/linkerscript/discard-section.s @@ -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 -- 2.7.4