[lld] r340257 - [LLD][ELF] - Check the architecture of lazy objects earlier.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 21 01:13:06 PDT 2018


Author: grimar
Date: Tue Aug 21 01:13:06 2018
New Revision: 340257

URL: http://llvm.org/viewvc/llvm-project?rev=340257&view=rev
Log:
[LLD][ELF] - Check the architecture of lazy objects earlier.

Our code in LazyObjFile::parse() has an ELFT switch and
adds a lazy object by its ELFT kind.
Though it might be possible to add a file using a different
architecture and make LLD to silently accept it (if the file
is empty or contains only week symbols). That itself, not a
huge issue perhaps (because the error would be reported later
if the file is fetched), but still does not look clean and correct.

It is possible to report an error earlier and clean up the
code. That is what the patch does.

Ideally, we might want to reuse isCompatible from SymbolTable.cpp,
but it is static and accepts a file as an argument, what is not
convenient. Since such a situation should be rare, I think it
should be OK to go with the way chosen in this patch.

Differential revision: https://reviews.llvm.org/D50899

Added:
    lld/trunk/test/ELF/lazy-arch-conflict.s
Modified:
    lld/trunk/ELF/InputFiles.cpp
    lld/trunk/ELF/InputFiles.h

Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=340257&r1=340256&r2=340257&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Tue Aug 21 01:13:06 2018
@@ -1261,25 +1261,11 @@ template <class ELFT> void LazyObjFile::
     return;
   }
 
-  switch (getELFKind(this->MB)) {
-  case ELF32LEKind:
-    addElfSymbols<ELF32LE>();
+  if (getELFKind(this->MB) != Config->EKind) {
+    error("incompatible file: " + this->MB.getBufferIdentifier());
     return;
-  case ELF32BEKind:
-    addElfSymbols<ELF32BE>();
-    return;
-  case ELF64LEKind:
-    addElfSymbols<ELF64LE>();
-    return;
-  case ELF64BEKind:
-    addElfSymbols<ELF64BE>();
-    return;
-  default:
-    llvm_unreachable("getELFKind");
   }
-}
 
-template <class ELFT> void LazyObjFile::addElfSymbols() {
   ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(MB.getBuffer()));
   ArrayRef<typename ELFT::Shdr> Sections = CHECK(Obj.sections(), this);
 

Modified: lld/trunk/ELF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=340257&r1=340256&r2=340257&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.h (original)
+++ lld/trunk/ELF/InputFiles.h Tue Aug 21 01:13:06 2018
@@ -272,8 +272,6 @@ public:
   bool AddedToLink = false;
 
 private:
-  template <class ELFT> void addElfSymbols();
-
   uint64_t OffsetInArchive;
 };
 

Added: lld/trunk/test/ELF/lazy-arch-conflict.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/lazy-arch-conflict.s?rev=340257&view=auto
==============================================================================
--- lld/trunk/test/ELF/lazy-arch-conflict.s (added)
+++ lld/trunk/test/ELF/lazy-arch-conflict.s Tue Aug 21 01:13:06 2018
@@ -0,0 +1,7 @@
+# REQUIRES: x86
+
+# RUN: echo '.globl foo; .weak foo; .quad foo;' | llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %t64.o
+# RUN: echo '.globl foo; foo:' | llvm-mc -filetype=obj -triple=i686-pc-linux - -o %t32.o
+# RUN: not ld.lld %t64.o --start-lib %t32.o --end-lib -o /dev/null 2>&1 | Filecheck %s
+
+# CHECK: error: incompatible file: {{.*}}32.o




More information about the llvm-commits mailing list