[ELF] Error if the linked-to section of a SHF_LINK_ORDER section is discarded
authorFangrui Song <maskray@google.com>
Fri, 20 Sep 2019 15:03:21 +0000 (15:03 +0000)
committerFangrui Song <maskray@google.com>
Fri, 20 Sep 2019 15:03:21 +0000 (15:03 +0000)
commit267205149587595e956db25a12252835a51cd372
treebb4e841a9dd2e00124099adbec65e8ab11e311e8
parentbe428513cb05893e74a7276a21b12b709db53bb9
[ELF] Error if the linked-to section of a SHF_LINK_ORDER section is discarded

Summary:
If st_link(A)=B, and A has the SHF_LINK_ORDER flag, we may dereference
a null pointer if B is garbage collected (PR43147):

1. In Wrter.cpp:compareByFilePosition, `aOut->sectionIndex` or `bOut->sectionIndex`
2. In OutputSections::finalize, `d->getParent()->sectionIndex`

Simply error and bail out to avoid null pointer dereferences. ld.bfd has
a similar error:

    sh_link of section `.bar' points to discarded section `.foo0' of `a.o'

ld.bfd is more permissive in that it just checks whether the linked-to
section of the first input section is discarded. This is likely because
it sets sh_link of the output section according to the first input
section.

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D67761

llvm-svn: 372400
lld/ELF/Writer.cpp
lld/test/ELF/gc-sections-metadata-err.s [new file with mode: 0644]