[PATCH] D50899: [LLD][ELF] - Check the architecture of lazy objects earlier.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 17 07:23:30 PDT 2018


grimar created this revision.
grimar added a reviewer: ruiu.
Herald added subscribers: arichardson, emaste.
Herald added a reviewer: espindola.

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, 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.


https://reviews.llvm.org/D50899

Files:
  ELF/InputFiles.cpp
  test/ELF/lazy-arch-conflict.s


Index: test/ELF/lazy-arch-conflict.s
===================================================================
--- test/ELF/lazy-arch-conflict.s
+++ test/ELF/lazy-arch-conflict.s
@@ -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: {{.*}}32.o has incompatible ELF kind
Index: ELF/InputFiles.cpp
===================================================================
--- ELF/InputFiles.cpp
+++ ELF/InputFiles.cpp
@@ -1261,22 +1261,10 @@
     return;
   }
 
-  switch (getELFKind(this->MB)) {
-  case ELF32LEKind:
-    addElfSymbols<ELF32LE>();
-    return;
-  case ELF32BEKind:
-    addElfSymbols<ELF32BE>();
-    return;
-  case ELF64LEKind:
-    addElfSymbols<ELF64LE>();
-    return;
-  case ELF64BEKind:
-    addElfSymbols<ELF64BE>();
-    return;
-  default:
-    llvm_unreachable("getELFKind");
-  }
+  if (getELFKind(this->MB) != Config->EKind)
+    error(this->MB.getBufferIdentifier() + " has incompatible ELF kind");
+  else
+    addElfSymbols<ELFT>();
 }
 
 template <class ELFT> void LazyObjFile::addElfSymbols() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50899.161238.patch
Type: text/x-patch
Size: 1310 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180817/3c2b9dea/attachment.bin>


More information about the llvm-commits mailing list