llvm-readobj: Handle invalid references to the string table.
authorRafael Espindola <rafael.espindola@gmail.com>
Mon, 20 Jul 2015 03:38:17 +0000 (03:38 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Mon, 20 Jul 2015 03:38:17 +0000 (03:38 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242658 91177308-0d34-0410-b5e6-96231b3b80d8

test/Object/Inputs/corrupt-invalid-strtab.elf.x86-64 [new file with mode: 0755]
test/Object/corrupt.test
tools/llvm-readobj/ELFDumper.cpp
tools/llvm-readobj/llvm-readobj.cpp
tools/llvm-readobj/llvm-readobj.h

diff --git a/test/Object/Inputs/corrupt-invalid-strtab.elf.x86-64 b/test/Object/Inputs/corrupt-invalid-strtab.elf.x86-64
new file mode 100755 (executable)
index 0000000..bdaa3cf
Binary files /dev/null and b/test/Object/Inputs/corrupt-invalid-strtab.elf.x86-64 differ
index 0a655704610cdd74d2f0e521e7e554e579b5fe98..2181c7e8907a2168ff6c6ec2bcff33179ffa41f4 100644 (file)
@@ -24,3 +24,10 @@ RUN: not llvm-readobj %p/Inputs/corrupt-version.elf-x86_64 -dt \
 RUN:     2>&1 | FileCheck --check-prefix=VER %s
 
 VER: Error reading file: Invalid data was encountered while parsing the file.
+
+
+// The file is missing the dynamic string table but has references to it.
+RUN: not llvm-readobj -dynamic-table %p/Inputs/corrupt-invalid-strtab.elf.x86-64 \
+RUN:     2>&1 | FileCheck --check-prefix=STRTAB %s
+
+STRTAB: Invalid dynamic string table reference
index 3deeb8dfe5b414e3b82b1aa375813524afe328d2..045f4dfc636f238e8b6ebaed0b73fd5d65c02849 100644 (file)
@@ -953,6 +953,14 @@ void printFlags(T Value, ArrayRef<EnumEntry<TFlag>> Flags, raw_ostream &OS) {
   }
 }
 
+template <class ELFT>
+static const char *getDynamicString(const ELFFile<ELFT> &O, uint64_t Value) {
+  const char *Ret = O.getDynamicString(Value);
+  if (!Ret)
+    reportError("Invalid dynamic string table reference");
+  return Ret;
+}
+
 template <class ELFT>
 static void printValue(const ELFFile<ELFT> *O, uint64_t Type, uint64_t Value,
                        bool Is64, raw_ostream &OS) {
@@ -1011,14 +1019,14 @@ static void printValue(const ELFFile<ELFT> *O, uint64_t Type, uint64_t Value,
     OS << Value << " (bytes)";
     break;
   case DT_NEEDED:
-    OS << "SharedLibrary (" << O->getDynamicString(Value) << ")";
+    OS << "SharedLibrary (" << getDynamicString(*O, Value) << ")";
     break;
   case DT_SONAME:
-    OS << "LibrarySoname (" << O->getDynamicString(Value) << ")";
+    OS << "LibrarySoname (" << getDynamicString(*O, Value) << ")";
     break;
   case DT_RPATH:
   case DT_RUNPATH:
-    OS << O->getDynamicString(Value);
+    OS << getDynamicString(*O, Value);
     break;
   case DT_MIPS_FLAGS:
     printFlags(Value, makeArrayRef(ElfDynamicDTMipsFlags), OS);
@@ -1088,7 +1096,7 @@ void ELFDumper<ELFT>::printNeededLibraries() {
 
   for (const auto &Entry : Obj->dynamic_table())
     if (Entry.d_tag == ELF::DT_NEEDED)
-      Libs.push_back(Obj->getDynamicString(Entry.d_un.d_val));
+      Libs.push_back(getDynamicString(*Obj, Entry.d_un.d_val));
 
   std::stable_sort(Libs.begin(), Libs.end());
 
index b525ce1000420bed0c9eec63c11abe43a6b1dc14..3f03618bb224ce634d1e95ecfaf68f6b1b3611d8 100644 (file)
@@ -188,14 +188,14 @@ namespace opts {
 
 } // namespace opts
 
-static void reportError(Twine Msg) {
+namespace llvm {
+
+void reportError(Twine Msg) {
   outs() << Msg << "\n";
   outs().flush();
   exit(1);
 }
 
-namespace llvm {
-
 void error(std::error_code EC) {
   if (!EC)
     return;
index 8872fc21a3944e611a59d3a81f2b7b290106832c..58c50f58d750c7fd7e51bf8f956e077a1dd174cf 100644 (file)
@@ -19,6 +19,7 @@ namespace llvm {
   }
 
   // Various helper functions.
+  void reportError(Twine Msg);
   void error(std::error_code ec);
   bool relocAddressLess(object::RelocationRef A,
                         object::RelocationRef B);