Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -1188,20 +1188,28 @@ Symbols.push_back(createBitcodeSymbol(KeptComdats, ObjSym, *this)); } -static ELFKind getELFKind(MemoryBufferRef MB) { +static ELFKind getELFKind(MemoryBufferRef MB, StringRef ArchiveName) { unsigned char Size; unsigned char Endian; std::tie(Size, Endian) = getElfArchType(MB.getBuffer()); + auto Fatal = [&](StringRef Msg) { + StringRef Filename = MB.getBufferIdentifier(); + if (ArchiveName.empty()) + fatal(Filename + ": corrupted ELF file: " + Msg); + else + fatal(ArchiveName + "(" + Filename + "): corrupted ELF file: " + Msg); + }; + if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB) - fatal(MB.getBufferIdentifier() + ": invalid data encoding"); + Fatal("invalid data encoding"); if (Size != ELFCLASS32 && Size != ELFCLASS64) - fatal(MB.getBufferIdentifier() + ": invalid file class"); + Fatal("invalid file class"); size_t BufSize = MB.getBuffer().size(); if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) || (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr))) - fatal(MB.getBufferIdentifier() + ": file is too short"); + Fatal("file is too short"); if (Size == ELFCLASS32) return (Endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind; @@ -1236,7 +1244,7 @@ if (isBitcode(MB)) return make(MB, ArchiveName, OffsetInArchive); - switch (getELFKind(MB)) { + switch (getELFKind(MB, ArchiveName)) { case ELF32LEKind: return make>(MB, ArchiveName); case ELF32BEKind: @@ -1251,7 +1259,7 @@ } InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) { - switch (getELFKind(MB)) { + switch (getELFKind(MB, "")) { case ELF32LEKind: return make>(MB, DefaultSoName); case ELF32BEKind: @@ -1293,7 +1301,7 @@ return; } - if (getELFKind(this->MB) != Config->EKind) { + if (getELFKind(this->MB, ArchiveName) != Config->EKind) { error("incompatible file: " + this->MB.getBufferIdentifier()); return; } Index: test/ELF/invalid/invalid-elf.test =================================================================== --- test/ELF/invalid/invalid-elf.test +++ test/ELF/invalid/invalid-elf.test @@ -3,11 +3,11 @@ # 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 +# INVALID-DATA-ENC: data-encoding.a(test.o): corrupted ELF file: invalid data encoding # RUN: not ld.lld %t %p/Inputs/file-class.a -o %t2 2>&1 | \ # RUN: FileCheck --check-prefix=INVALID-FILE-CLASS %s -# INVALID-FILE-CLASS: test.o: invalid file class +# INVALID-FILE-CLASS: file-class.a(test.o): corrupted ELF file: invalid file class # RUN: not ld.lld %p/Inputs/binding.elf -o %t2 2>&1 | \ # RUN: FileCheck --check-prefix=INVALID-BINDING %s