Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -1193,6 +1193,8 @@ unsigned char Endian; std::tie(Size, Endian) = getElfArchType(MB.getBuffer()); + if (!MB.getBuffer().startswith(ElfMagic)) + fatal(MB.getBufferIdentifier() + ": expected ELF file in archive"); if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB) fatal(MB.getBufferIdentifier() + ": invalid data encoding"); if (Size != ELFCLASS32 && Size != ELFCLASS64) Index: test/ELF/invalid/Inputs/not-elf.a =================================================================== --- /dev/null +++ test/ELF/invalid/Inputs/not-elf.a @@ -0,0 +1,2 @@ +! +test.o/ 0 0 0 644 0 ` Index: test/ELF/invalid/invalid-elf.test =================================================================== --- test/ELF/invalid/invalid-elf.test +++ test/ELF/invalid/invalid-elf.test @@ -1,6 +1,10 @@ # REQUIRES: x86 # RUN: llvm-mc %s -o %t -filetype=obj -triple x86_64-pc-linux +# RUN: not ld.lld %t %p/Inputs/not-elf.a -o %t2 2>&1 | \ +# RUN: FileCheck --check-prefix=NOT-ELF %s +# NOT-ELF: test.o: expected ELF file in archive + # RUN: not ld.lld %t %p/Inputs/data-encoding.a -o %t2 2>&1 | \ # RUN: FileCheck --check-prefix=INVALID-DATA-ENC %s # INVALID-DATA-ENC: test.o: invalid data encoding