[PATCH] D64714: [Object/llvm-readelf/llvm-readobj] - Improve error reporting when e_shstrndx is broken.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 15 03:48:25 PDT 2019


grimar created this revision.
grimar added reviewers: jhenderson, MaskRay.
Herald added a subscriber: rupprecht.

When `e_shstrndx` is broken, it is impossible to get a section name.
In this patch I improved the error message we show and 
added tests for Object and for llvm-readelf/llvm-readobj

Message was changed in two places:

- llvm-readelf/llvm-readobj previously used a code from Object/ELF.h,

now they have a modified version of it (it has less checks and allows
dumping broken things).

- Code in Object/ELF.h is still used for generic cases.


https://reviews.llvm.org/D64714

Files:
  include/llvm/Object/ELF.h
  test/Object/invalid.test
  test/tools/llvm-readobj/elf-invalid-shstrndx.test
  tools/llvm-readobj/ELFDumper.cpp
  tools/llvm-readobj/llvm-readobj.cpp


Index: tools/llvm-readobj/llvm-readobj.cpp
===================================================================
--- tools/llvm-readobj/llvm-readobj.cpp
+++ tools/llvm-readobj/llvm-readobj.cpp
@@ -371,6 +371,7 @@
 namespace llvm {
 
 LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
+  fouts().flush();
   errs() << "\n";
   WithColor::error(errs()) << Msg << "\n";
   exit(1);
Index: tools/llvm-readobj/ELFDumper.cpp
===================================================================
--- tools/llvm-readobj/ELFDumper.cpp
+++ tools/llvm-readobj/ELFDumper.cpp
@@ -3017,7 +3017,9 @@
   if (!Index) // no section string table.
     return "";
   if (Index >= Sections.size())
-    reportError("invalid section index");
+    reportError(
+        "e_shstrndx/SHN_XINDEX refers to a section that doesn't exist: " +
+        Twine(Index));
   StringRef Data = toStringRef(unwrapOrError(
       Obj.template getSectionContentsAsArray<uint8_t>(&Sections[Index])));
   return unwrapOrError(Obj.getSectionName(&Sec, Data));
Index: test/tools/llvm-readobj/elf-invalid-shstrndx.test
===================================================================
--- /dev/null
+++ test/tools/llvm-readobj/elf-invalid-shstrndx.test
@@ -0,0 +1,26 @@
+# RUN: yaml2obj %s -o %t
+# RUN: not llvm-readelf --headers -S 2>&1 %t | FileCheck %s --check-prefix=GNU
+# RUN: not llvm-readobj --headers -S 2>&1 %t | FileCheck %s --check-prefix=LLVM
+
+# GNU:      ELF Header:
+# GNU:        Section header string table index: 255
+# GNU-NEXT:   There are 4 section headers, starting at offset 0x40:
+# GNU:      Section Headers:
+# GNU-NEXT:  [Nr] Name
+# GNU-EMPTY:
+# GNU-NEXT:  error: e_shstrndx/SHN_XINDEX refers to a section that doesn't exist: 255
+
+# LLVM:      ElfHeader {
+# LLVM:        StringTableSectionIndex: 255
+# LLVM-NEXT: }
+# LLVM-NEXT: Sections [
+# LLVM-EMPTY:
+# LLVM-NEXT: error: e_shstrndx/SHN_XINDEX refers to a section that doesn't exist: 255
+
+--- !ELF
+FileHeader:
+  Class:     ELFCLASS64
+  Data:      ELFDATA2LSB
+  Type:      ET_REL
+  Machine:   EM_X86_64
+  SHStrNdx:  0xFF
Index: test/Object/invalid.test
===================================================================
--- test/Object/invalid.test
+++ test/Object/invalid.test
@@ -536,3 +536,19 @@
     FileSize: 0xffff0000
     Sections:
       - Section: .dynamic
+
+# RUN: yaml2obj --docnum=25 %s -o %t25
+# RUN: not obj2yaml 2>&1 %t25 | FileCheck %s --check-prefix=INVALID-SHSTRNDX
+
+# INVALID-SHSTRNDX: Error reading file: {{.*}}25: e_shstrndx/SHN_XINDEX refers to a section that doesn't exist: 255
+
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2LSB
+  Type:     ET_REL
+  Machine:  EM_X86_64
+  SHStrNdx: 0xFF
+Sections:
+  - Name: .foo
+    Type: SHT_PROGBITS
Index: include/llvm/Object/ELF.h
===================================================================
--- include/llvm/Object/ELF.h
+++ include/llvm/Object/ELF.h
@@ -467,8 +467,9 @@
   if (!Index) // no section string table.
     return "";
   if (Index >= Sections.size())
-    // TODO: this error is untested.
-    return createError("invalid section index");
+    return createError(
+        "e_shstrndx/SHN_XINDEX refers to a section that doesn't exist: " +
+        Twine(Index));
   return getStringTable(&Sections[Index]);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64714.209788.patch
Type: text/x-patch
Size: 3283 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190715/4c12372c/attachment.bin>


More information about the llvm-commits mailing list