[PATCH] D63266: [llvm-readobj] - Do not fail to dump the object which has wrong type of .shstrtab.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 14 03:09:08 PDT 2019


grimar updated this revision to Diff 204730.
grimar marked an inline comment as done.
grimar added a comment.

- Addressed review comments.
- Fixed compilation with GCC.
- Added 2 lines of code to fix the `Object/no-section-header-string-table.test`

(It is needed for no section string table case, it matches the existent behavior).


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D63266/new/

https://reviews.llvm.org/D63266

Files:
  test/tools/llvm-readobj/Inputs/wrong-shstrtab-type.elf-x86-64
  test/tools/llvm-readobj/elf-wrong-shstrtab-type.test
  tools/llvm-readobj/ELFDumper.cpp


Index: tools/llvm-readobj/ELFDumper.cpp
===================================================================
--- tools/llvm-readobj/ELFDumper.cpp
+++ tools/llvm-readobj/ELFDumper.cpp
@@ -3003,6 +3003,22 @@
   return "";
 }
 
+template <class ELFT>
+static StringRef getSectionName(const typename ELFT::Shdr &Sec,
+                                const ELFFile<ELFT> &Obj,
+                                ArrayRef<typename ELFT::Shdr> Sections) {
+  uint32_t Index = Obj.getHeader()->e_shstrndx;
+  if (Index == ELF::SHN_XINDEX)
+    Index = Sections[0].sh_link;
+  if (!Index) // no section string table.
+    return "";
+  if (Index >= Sections.size())
+    reportError("invalid section index");
+  StringRef Data = toStringRef(unwrapOrError(
+      Obj.template getSectionContentsAsArray<uint8_t>(&Sections[Index])));
+  return unwrapOrError(Obj.getSectionName(&Sec, Data));
+}
+
 template <class ELFT>
 void GNUStyle<ELFT>::printSectionHeaders(const ELFO *Obj) {
   unsigned Bias = ELFT::Is64Bits ? 0 : 8;
@@ -3023,7 +3039,7 @@
   size_t SectionIndex = 0;
   for (const Elf_Shdr &Sec : Sections) {
     Fields[0].Str = to_string(SectionIndex);
-    Fields[1].Str = unwrapOrError(Obj->getSectionName(&Sec));
+    Fields[1].Str = getSectionName(Sec, *Obj, Sections);
     Fields[2].Str =
         getSectionTypeString(Obj->getHeader()->e_machine, Sec.sh_type);
     Fields[3].Str =
@@ -4569,13 +4585,11 @@
   ListScope SectionsD(W, "Sections");
 
   int SectionIndex = -1;
-  for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
-    ++SectionIndex;
-
-    StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
-
+  ArrayRef<Elf_Shdr> Sections = unwrapOrError(Obj->sections());
+  for (const Elf_Shdr &Sec : Sections) {
+    StringRef Name = getSectionName(Sec, *Obj, Sections);
     DictScope SectionD(W, "Section");
-    W.printNumber("Index", SectionIndex);
+    W.printNumber("Index", ++SectionIndex);
     W.printNumber("Name", Name, Sec.sh_name);
     W.printHex(
         "Type",
Index: test/tools/llvm-readobj/elf-wrong-shstrtab-type.test
===================================================================
--- /dev/null
+++ test/tools/llvm-readobj/elf-wrong-shstrtab-type.test
@@ -0,0 +1,11 @@
+## wrong-shstrtab-type.elf-x86-64 contains .shstrtab section which has SHT_PROGBITS type.
+## Check we do not fail to dump the section headers in this case.
+
+# RUN: llvm-readobj -S %p/Inputs/wrong-shstrtab-type.elf-x86-64 | FileCheck %s --check-prefix LLVM
+# RUN: llvm-readelf -S %p/Inputs/wrong-shstrtab-type.elf-x86-64 | FileCheck %s --check-prefix GNU
+
+# LLVM:      Name: .shstrtab
+# LLVM-NEXT: Type: SHT_PROGBITS
+
+# GNU: [Nr] Name      Type
+# GNU: [ 3] .shstrtab PROGBITS


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63266.204730.patch
Type: text/x-patch
Size: 2703 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190614/c342af9d/attachment.bin>


More information about the llvm-commits mailing list