[lld] r355888 - [ELF] Print a better error for an archive containing a non-ELF file.
Eli Friedman via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 11 18:24:39 PDT 2019
Author: efriedma
Date: Mon Mar 11 18:24:39 2019
New Revision: 355888
URL: http://llvm.org/viewvc/llvm-project?rev=355888&view=rev
Log:
[ELF] Print a better error for an archive containing a non-ELF file.
Hopefully gives a more readable error message for the most obvious
mistake.
Differential Revision: https://reviews.llvm.org/D59170
Modified:
lld/trunk/ELF/InputFiles.cpp
lld/trunk/test/ELF/invalid/invalid-elf.test
Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=355888&r1=355887&r2=355888&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Mon Mar 11 18:24:39 2019
@@ -1196,20 +1196,22 @@ static ELFKind getELFKind(MemoryBufferRe
auto Fatal = [&](StringRef Msg) {
StringRef Filename = MB.getBufferIdentifier();
if (ArchiveName.empty())
- fatal(Filename + ": corrupted ELF file: " + Msg);
+ fatal(Filename + ": " + Msg);
else
- fatal(ArchiveName + "(" + Filename + "): corrupted ELF file: " + Msg);
+ fatal(ArchiveName + "(" + Filename + "): " + Msg);
};
+ if (!MB.getBuffer().startswith(ElfMagic))
+ Fatal("not an ELF file");
if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
- Fatal("invalid data encoding");
+ Fatal("corrupted ELF file: invalid data encoding");
if (Size != ELFCLASS32 && Size != ELFCLASS64)
- Fatal("invalid file class");
+ Fatal("corrupted ELF file: invalid file class");
size_t BufSize = MB.getBuffer().size();
if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) ||
(Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr)))
- Fatal("file is too short");
+ Fatal("corrupted ELF file: file is too short");
if (Size == ELFCLASS32)
return (Endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;
Modified: lld/trunk/test/ELF/invalid/invalid-elf.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/invalid/invalid-elf.test?rev=355888&r1=355887&r2=355888&view=diff
==============================================================================
--- lld/trunk/test/ELF/invalid/invalid-elf.test (original)
+++ lld/trunk/test/ELF/invalid/invalid-elf.test Mon Mar 11 18:24:39 2019
@@ -1,11 +1,18 @@
# REQUIRES: x86
-# RUN: llvm-mc %s -o %t -filetype=obj -triple x86_64-pc-linux
+# RUN: rm -rf %t && mkdir -p %t
+# RUN: llvm-mc %s -o %t/simple.o -filetype=obj -triple x86_64-pc-linux
+# RUN: echo > %t/empty.o
+# RUN: llvm-ar cr %t/not-elf.a %t/empty.o
-# RUN: not ld.lld %t %p/Inputs/data-encoding.a -o %t2 2>&1 | \
+# RUN: not ld.lld %t/simple.o %t/not-elf.a -o %t2 2>&1 | \
+# RUN: FileCheck --check-prefix=NOT-ELF %s
+# NOT-ELF: not-elf.a(empty.o): not an ELF file
+
+# RUN: not ld.lld %t/simple.o %p/Inputs/data-encoding.a -o %t2 2>&1 | \
# RUN: FileCheck --check-prefix=INVALID-DATA-ENC %s
# 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: not ld.lld %t/simple.o %p/Inputs/file-class.a -o %t2 2>&1 | \
# RUN: FileCheck --check-prefix=INVALID-FILE-CLASS %s
# INVALID-FILE-CLASS: file-class.a(test.o): corrupted ELF file: invalid file class
More information about the llvm-commits
mailing list