[lld] r341611 - [ELF] Check if LinkSec is nullptr when initializing SHF_LINK_ORDER sections
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 6 17:18:07 PDT 2018
Author: maskray
Date: Thu Sep 6 17:18:07 2018
New Revision: 341611
URL: http://llvm.org/viewvc/llvm-project?rev=341611&view=rev
Log:
[ELF] Check if LinkSec is nullptr when initializing SHF_LINK_ORDER sections
Summary: This protects lld from a null pointer dereference when a faulty input file has such invalid sh_link fields.
Reviewers: ruiu, espindola
Reviewed By: ruiu
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D51743
Added:
lld/trunk/test/ELF/invalid/linkorder-invalid-sec2.test
Modified:
lld/trunk/ELF/InputFiles.cpp
Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=341611&r1=341610&r2=341611&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Thu Sep 6 17:18:07 2018
@@ -478,11 +478,13 @@ void ObjFile<ELFT>::initializeSections(
// .ARM.exidx sections have a reverse dependency on the InputSection they
// have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
if (Sec.sh_flags & SHF_LINK_ORDER) {
- if (Sec.sh_link >= this->Sections.size())
+ InputSectionBase *LinkSec = nullptr;
+ if (Sec.sh_link < this->Sections.size())
+ LinkSec = this->Sections[Sec.sh_link];
+ if (!LinkSec)
fatal(toString(this) +
": invalid sh_link index: " + Twine(Sec.sh_link));
- InputSectionBase *LinkSec = this->Sections[Sec.sh_link];
InputSection *IS = cast<InputSection>(this->Sections[I]);
LinkSec->DependentSections.push_back(IS);
if (!isa<InputSection>(LinkSec))
Added: lld/trunk/test/ELF/invalid/linkorder-invalid-sec2.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/invalid/linkorder-invalid-sec2.test?rev=341611&view=auto
==============================================================================
--- lld/trunk/test/ELF/invalid/linkorder-invalid-sec2.test (added)
+++ lld/trunk/test/ELF/invalid/linkorder-invalid-sec2.test Thu Sep 6 17:18:07 2018
@@ -0,0 +1,16 @@
+# REQUIRES: x86
+# RUN: yaml2obj %s -o %t.o
+# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
+# CHECK: invalid sh_link index: 0
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .linkorder
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR, SHF_LINK_ORDER ]
+ Link: 0
More information about the llvm-commits
mailing list