[lld] r284010 - Alternative fix for reloc tareting discarded section

Eugene Leviant via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 12 11:18:29 PDT 2016


The computeInputSections() function sets OutSec to (-1), see
LinkerScript.cpp:216.
This is being done to avoid adding input section twice, because
computeInputSections() is being called
for every input section pattern in a command.
As (-1) is not 0, condition S->OutSec in getSymVA() is true, and you
get crash in a call to S->OutSec->getVA().
To fix this I added piece of code which zeroes OutSec field after all
input sections have been added to result vector.


2016-10-12 21:10 GMT+03:00 Rui Ueyama <ruiu at google.com>:
> On Wed, Oct 12, 2016 at 5:31 AM, Eugene Leviant via llvm-commits
> <llvm-commits at lists.llvm.org> wrote:
>>
>> Author: evgeny777
>> Date: Wed Oct 12 07:31:34 2016
>> New Revision: 284010
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=284010&view=rev
>> Log:
>> Alternative fix for reloc tareting discarded section
>>
>> r283984 introduced a problem of too many warning messages being shown
>> when -ffunction-sections and -fdata-sections were used in conjunction
>> with --gc-sections linker flag and debugging information present. This
>> happens because lot of relocations from .debug_line section may become
>> invalid in such case. The newer fix doesn't show any warning message but
>> zeroes OutSec pointer in createInputSectionList() to avoid crash, when
>> relocations are written
>>
>> Modified:
>>     lld/trunk/ELF/LinkerScript.cpp
>>     lld/trunk/ELF/Symbols.cpp
>>     lld/trunk/test/ELF/linkerscript/discard-section.s
>>
>> Modified: lld/trunk/ELF/LinkerScript.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=284010&r1=284009&r2=284010&view=diff
>>
>> ==============================================================================
>> --- lld/trunk/ELF/LinkerScript.cpp (original)
>> +++ lld/trunk/ELF/LinkerScript.cpp Wed Oct 12 07:31:34 2016
>> @@ -239,6 +239,12 @@ LinkerScript<ELFT>::createInputSectionLi
>>        Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S));
>>    }
>>
>> +  // After we created final list we should now set OutSec pointer to
>> null,
>> +  // instead of -1. Otherwise we may get a crash when writing relocs, in
>> +  // case section is discarded by linker script
>> +  for (InputSectionBase<ELFT> *S : Ret)
>> +    S->OutSec = nullptr;
>
>
> I was thinking that it is an invariant that InputSection::OutSec always
> points to an output section if the input section is included in the output
> section, so I'm confused with this code and comment. How does it work?
>
>> +
>>    return Ret;
>>  }
>>
>>
>> Modified: lld/trunk/ELF/Symbols.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.cpp?rev=284010&r1=284009&r2=284010&view=diff
>>
>> ==============================================================================
>> --- lld/trunk/ELF/Symbols.cpp (original)
>> +++ lld/trunk/ELF/Symbols.cpp Wed Oct 12 07:31:34 2016
>> @@ -25,12 +25,6 @@ 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;
>> @@ -60,11 +54,6 @@ 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=284010&r1=284009&r2=284010&view=diff
>>
>> ==============================================================================
>> --- lld/trunk/test/ELF/linkerscript/discard-section.s (original)
>> +++ lld/trunk/test/ELF/linkerscript/discard-section.s Wed Oct 12 07:31:34
>> 2016
>> @@ -1,10 +1,9 @@
>>  # 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 2>&1 | FileCheck
>> --check-prefix=WARN %s
>> +# RUN: ld.lld -o %t1 --script %t.script %t
>>  # RUN: llvm-objdump -section-headers %t1 | FileCheck %s
>>
>> -# WARN: relocation refers to discarded section {{.+}}(.aaa)
>>  # CHECK-NOT: .aaa
>>
>>  .section .aaa,"a"
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
>


More information about the llvm-commits mailing list