Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -226,10 +226,12 @@ template void elf::ObjectFile::initializeSections( DenseSet &ComdatGroups) { + const ELFFile &Obj = this->ELFObj; uint64_t Size = this->ELFObj.getNumSections(); + if (Size > UINT16_MAX) + fatal(getFilename(this) + ": wrong amount of input sections"); Sections.resize(Size); unsigned I = -1; - const ELFFile &Obj = this->ELFObj; for (const Elf_Shdr &Sec : Obj.sections()) { ++I; if (Sections[I] == &InputSection::Discarded) Index: test/ELF/invalid/sections-count.s =================================================================== --- test/ELF/invalid/sections-count.s +++ test/ELF/invalid/sections-count.s @@ -0,0 +1,6 @@ +# REQUIRES: x86 + +## sections-count.elf contains symbol with huge sh_size of section +## header table. +# RUN: not ld.lld %S/Inputs/sections-count.elf -o %t 2>&1 | FileCheck %s +# CHECK: wrong amount of input sections