From e2684662ee16a6b6b26353c2c8a13dcec894b2cd Mon Sep 17 00:00:00 2001 From: George Rimar Date: Tue, 21 Aug 2018 08:13:06 +0000 Subject: [PATCH] [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 llvm-svn: 340257 --- lld/ELF/InputFiles.cpp | 18 ++---------------- lld/ELF/InputFiles.h | 2 -- lld/test/ELF/lazy-arch-conflict.s | 7 +++++++ 3 files changed, 9 insertions(+), 18 deletions(-) create mode 100644 lld/test/ELF/lazy-arch-conflict.s diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 70189af6d601..0fc99c5b2d72 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1261,25 +1261,11 @@ template void LazyObjFile::parse() { return; } - switch (getELFKind(this->MB)) { - case ELF32LEKind: - addElfSymbols(); - return; - case ELF32BEKind: - addElfSymbols(); - return; - case ELF64LEKind: - addElfSymbols(); + if (getELFKind(this->MB) != Config->EKind) { + error("incompatible file: " + this->MB.getBufferIdentifier()); return; - case ELF64BEKind: - addElfSymbols(); - return; - default: - llvm_unreachable("getELFKind"); } -} -template void LazyObjFile::addElfSymbols() { ELFFile Obj = check(ELFFile::create(MB.getBuffer())); ArrayRef Sections = CHECK(Obj.sections(), this); diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 0db3203b0ba2..e31ecbfd4b78 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -272,8 +272,6 @@ public: bool AddedToLink = false; private: - template void addElfSymbols(); - uint64_t OffsetInArchive; }; diff --git a/lld/test/ELF/lazy-arch-conflict.s b/lld/test/ELF/lazy-arch-conflict.s new file mode 100644 index 000000000000..09f2dad866f8 --- /dev/null +++ b/lld/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: incompatible file: {{.*}}32.o -- 2.34.1