[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
This commit is contained in:
George Rimar 2018-08-21 08:13:06 +00:00
parent 097ef69182
commit e2684662ee
3 changed files with 9 additions and 18 deletions

View File

@ -1261,25 +1261,11 @@ template <class ELFT> void LazyObjFile::parse() {
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);

View File

@ -272,8 +272,6 @@ public:
bool AddedToLink = false;
private:
template <class ELFT> void addElfSymbols();
uint64_t OffsetInArchive;
};

View File

@ -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