Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -70,6 +70,8 @@ template ELFFileBase::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB), ELFObj(createELFObj(MB)) { + if (HasError) + return; EKind = getELFKind(); EMachine = ELFObj.getHeader()->e_machine; } Index: test/ELF/invalid/too-short.s =================================================================== --- test/ELF/invalid/too-short.s +++ test/ELF/invalid/too-short.s @@ -0,0 +1,5 @@ +# REQUIRES: x86 + +## too-short.elf file is a truncated ELF. +# RUN: not ld.lld %S/Inputs/too-short.elf -o %t 2>&1 | FileCheck %s +# CHECK: failed to read