[ELF] - Do not crash when unable to parse ELF object file.

createELFObj() may call error(...), for example when file is too short.
In that case header is not set and following line lead to crash:

EMachine = ELFObj.getHeader()->e_machine;

Patch fixes the issue.

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

llvm-svn: 283532
This commit is contained in:
George Rimar 2016-10-07 08:51:57 +00:00
parent d9edde4ae2
commit b7aec33125
5 changed files with 11 additions and 1 deletions

View File

@ -47,4 +47,8 @@ void elf::fatal(const Twine &Msg) {
exit(1);
}
void elf::fatal(std::error_code EC, const Twine &Prefix) {
fatal(Prefix + ": " + EC.message());
}
} // namespace lld

View File

@ -45,6 +45,7 @@ template <typename T> void error(const ErrorOr<T> &V, const Twine &Prefix) {
}
LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg);
LLVM_ATTRIBUTE_NORETURN void fatal(std::error_code EC, const Twine &Prefix);
template <class T> T check(ErrorOr<T> E) {
if (auto EC = E.getError())

View File

@ -57,7 +57,7 @@ template <class ELFT> static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
std::error_code EC;
ELFFile<ELFT> F(MB.getBuffer(), EC);
if (EC)
error(EC, "failed to read " + MB.getBufferIdentifier());
fatal(EC, "failed to read " + MB.getBufferIdentifier());
return F;
}

Binary file not shown.

View File

@ -0,0 +1,5 @@
# REQUIRES: x86
## too-short.elf file is a truncated ELF.
# RUN: not ld.lld %S/Inputs/too-short.elf -o %t 2>&1 | FileCheck %s
# CHECK: failed to read