[PATCH] D59170: [lld] [ELF] Print a better error for an archive containing a non-ELF file.

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 11 17:54:44 PDT 2019


efriedma updated this revision to Diff 190192.
efriedma added a comment.

Rebased. Changed error message.  Changed test to build archive with llvm-ar.


Repository:
  rLLD LLVM Linker

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D59170/new/

https://reviews.llvm.org/D59170

Files:
  ELF/InputFiles.cpp
  test/ELF/invalid/invalid-elf.test


Index: test/ELF/invalid/invalid-elf.test
===================================================================
--- test/ELF/invalid/invalid-elf.test
+++ test/ELF/invalid/invalid-elf.test
@@ -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
 
Index: ELF/InputFiles.cpp
===================================================================
--- ELF/InputFiles.cpp
+++ ELF/InputFiles.cpp
@@ -1193,14 +1193,17 @@
   unsigned char Endian;
   std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
 
-  auto Fatal = [&](StringRef Msg) {
+  auto Fatal = [&](StringRef Msg, bool NotElf = false) {
     StringRef Filename = MB.getBufferIdentifier();
+    StringRef MsgPrefix = NotElf ? ": " : ": corrupted ELF file: ";
     if (ArchiveName.empty())
-      fatal(Filename + ": corrupted ELF file: " + Msg);
+      fatal(Filename + MsgPrefix + Msg);
     else
-      fatal(ArchiveName + "(" + Filename + "): corrupted ELF file: " + Msg);
+      fatal(ArchiveName + "(" + Filename + ")" + MsgPrefix + Msg);
   };
 
+  if (!MB.getBuffer().startswith(ElfMagic))
+    Fatal("not an ELF file", true);
   if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
     Fatal("invalid data encoding");
   if (Size != ELFCLASS32 && Size != ELFCLASS64)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59170.190192.patch
Type: text/x-patch
Size: 2154 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190312/47b1b508/attachment.bin>


More information about the llvm-commits mailing list