Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -478,11 +478,13 @@ // .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(this->Sections[I]); LinkSec->DependentSections.push_back(IS); if (!isa(LinkSec)) Index: test/ELF/invalid/linkorder-invalid-sec2.test =================================================================== --- test/ELF/invalid/linkorder-invalid-sec2.test +++ test/ELF/invalid/linkorder-invalid-sec2.test @@ -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