[lld] r283532 - [ELF] - Do not crash when unable to parse ELF object file.
George Rimar via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 7 01:51:57 PDT 2016
Author: grimar
Date: Fri Oct 7 03:51:57 2016
New Revision: 283532
URL: http://llvm.org/viewvc/llvm-project?rev=283532&view=rev
Log:
[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
Added:
lld/trunk/test/ELF/invalid/Inputs/too-short.elf (with props)
lld/trunk/test/ELF/invalid/too-short.s
Modified:
lld/trunk/ELF/Error.cpp
lld/trunk/ELF/Error.h
lld/trunk/ELF/InputFiles.cpp
Modified: lld/trunk/ELF/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Error.cpp?rev=283532&r1=283531&r2=283532&view=diff
==============================================================================
--- lld/trunk/ELF/Error.cpp (original)
+++ lld/trunk/ELF/Error.cpp Fri Oct 7 03:51:57 2016
@@ -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
Modified: lld/trunk/ELF/Error.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Error.h?rev=283532&r1=283531&r2=283532&view=diff
==============================================================================
--- lld/trunk/ELF/Error.h (original)
+++ lld/trunk/ELF/Error.h Fri Oct 7 03:51:57 2016
@@ -45,6 +45,7 @@ template <typename T> void error(const E
}
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())
Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=283532&r1=283531&r2=283532&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Fri Oct 7 03:51:57 2016
@@ -57,7 +57,7 @@ template <class ELFT> static ELFFile<ELF
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;
}
Added: lld/trunk/test/ELF/invalid/Inputs/too-short.elf
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/invalid/Inputs/too-short.elf?rev=283532&view=auto
==============================================================================
Binary file - no diff available.
Propchange: lld/trunk/test/ELF/invalid/Inputs/too-short.elf
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added: lld/trunk/test/ELF/invalid/too-short.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/invalid/too-short.s?rev=283532&view=auto
==============================================================================
--- lld/trunk/test/ELF/invalid/too-short.s (added)
+++ lld/trunk/test/ELF/invalid/too-short.s Fri Oct 7 03:51:57 2016
@@ -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
More information about the llvm-commits
mailing list