[llvm] 1af6209 - [llvm-readelf] - Improve dumping of objects without a section header string table.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 24 03:30:35 PST 2020


Author: Georgii Rymar
Date: 2020-01-24T14:30:03+03:00
New Revision: 1af6209d64dc503800797b647f9c281cc2c4ec87

URL: https://github.com/llvm/llvm-project/commit/1af6209d64dc503800797b647f9c281cc2c4ec87
DIFF: https://github.com/llvm/llvm-project/commit/1af6209d64dc503800797b647f9c281cc2c4ec87.diff

LOG: [llvm-readelf] - Improve dumping of objects without a section header string table.

We have a test/Object/no-section-header-string-table.test which checks
what happens when an object does not have a section header string table.
It does not check the full output though.
Currently our output is different from GNU readelf, because the latter prints
"<no-strings>" instead of a section name, while we print nothing.

This patch fixes this, adds a proper test case and removes the one from test/Object,
as it is not a right folder for llvm-readelf tests.

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

Added: 
    llvm/test/tools/llvm-readobj/ELF/sections-no-section-header-string-table.test

Modified: 
    llvm/tools/llvm-readobj/ELFDumper.cpp

Removed: 
    llvm/test/Object/Inputs/no-section-header-string-table.elf-x86-64
    llvm/test/Object/no-section-header-string-table.test


################################################################################
diff  --git a/llvm/test/Object/Inputs/no-section-header-string-table.elf-x86-64 b/llvm/test/Object/Inputs/no-section-header-string-table.elf-x86-64
deleted file mode 100644
index 9024ad9b7573..000000000000
Binary files a/llvm/test/Object/Inputs/no-section-header-string-table.elf-x86-64 and /dev/null 
diff er

diff  --git a/llvm/test/Object/no-section-header-string-table.test b/llvm/test/Object/no-section-header-string-table.test
deleted file mode 100644
index 1f449328cabc..000000000000
--- a/llvm/test/Object/no-section-header-string-table.test
+++ /dev/null
@@ -1,10 +0,0 @@
-RUN: llvm-readobj %p/Inputs/no-section-header-string-table.elf-x86-64 --sections \
-RUN:   | FileCheck %s
-
-CHECK:    Type: SHT_PROGBITS (0x1)
-CHECK:    Type: SHT_PROGBITS (0x1)
-CHECK:    Type: SHT_PROGBITS (0x1)
-CHECK:    Type: SHT_RELA (0x4)
-CHECK:    Type: SHT_SYMTAB (0x2)
-CHECK:    Type: SHT_STRTAB (0x3)
-CHECK:    Type: SHT_STRTAB (0x3)

diff  --git a/llvm/test/tools/llvm-readobj/ELF/sections-no-section-header-string-table.test b/llvm/test/tools/llvm-readobj/ELF/sections-no-section-header-string-table.test
new file mode 100644
index 000000000000..4f9cd1b9e424
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/sections-no-section-header-string-table.test
@@ -0,0 +1,61 @@
+## Check what --sections prints when there is no section header string table.
+
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-readelf --sections %t \
+# RUN:   | FileCheck %s --strict-whitespace --match-full-lines --check-prefix=GNU
+# RUN: not llvm-readobj --sections %t 2>&1 \
+# RUN:   | FileCheck %s -DFILE=%t --check-prefix=LLVM
+
+#      GNU:Section Headers:
+# GNU-NEXT:  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
+# GNU-NEXT:  [ 0] <no-strings>      NULL            0000000000000000 000000 000000 00      0   0  0
+# GNU-NEXT:  [ 1] <no-strings>      PROGBITS        0000000000000000 000040 000000 00      0   0  0
+# GNU-NEXT:  [ 2] <no-strings>      PROGBITS        0000000000000000 000040 000000 00      0   0  0
+# GNU-NEXT:  [ 3] <no-strings>      STRTAB          0000000000000000 000040 000001 00      0   0  1
+# GNU-NEXT:  [ 4] <no-strings>      STRTAB          0000000000000000 000041 00001d 00      0   0  1
+
+# LLVM:      Sections [
+# LLVM-NEXT:   Section {
+# LLVM-NEXT:     Index: 0
+# LLVM-NEXT:     Name:  (0)
+# LLVM-NEXT:     Type: SHT_NULL (0x0)
+# LLVM-NEXT:     Flags [ (0x0)
+# LLVM-NEXT:     ]
+# LLVM-NEXT:     Address: 0x0
+# LLVM-NEXT:     Offset: 0x0
+# LLVM-NEXT:     Size: 0
+# LLVM-NEXT:     Link: 0
+# LLVM-NEXT:     Info: 0
+# LLVM-NEXT:     AddressAlignment: 0
+# LLVM-NEXT:     EntrySize: 0
+# LLVM-NEXT:   }
+# LLVM-NEXT:   Section {
+# LLVM-NEXT:     Index: 1
+# LLVM-NEXT:     Name:  (0)
+# LLVM-NEXT:     Type: SHT_PROGBITS (0x1)
+# LLVM-NEXT:     Flags [ (0x0)
+# LLVM-NEXT:     ]
+# LLVM-NEXT:     Address: 0x0
+# LLVM-NEXT:     Offset: 0x40
+# LLVM-NEXT:     Size: 0
+# LLVM-NEXT:     Link: 0
+# LLVM-NEXT:     Info: 0
+# LLVM-NEXT:     AddressAlignment: 0
+# LLVM-NEXT:     EntrySize: 0
+# LLVM-NEXT:   }
+# LLVM-NEXT: error: '[[FILE]]': a section [index 2] has an invalid sh_name (0x1) offset which goes past the end of the section name string table
+
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2LSB
+  Type:     ET_DYN
+  Machine:  EM_X86_64
+  SHStrNdx: 0
+Sections:
+  - Name:   .foo
+    Type:   SHT_PROGBITS
+    ShName: 0
+  - Name:   .bar
+    Type:   SHT_PROGBITS
+    ShName: 1

diff  --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index e08edb0a02f8..766684624fd7 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -3515,11 +3515,17 @@ void GNUStyle<ELFT>::printSectionHeaders(const ELFO *Obj) {
   OS << "\n";
 
   const ELFObjectFile<ELFT> *ElfObj = this->dumper()->getElfObject();
+  StringRef SecStrTable = unwrapOrError<StringRef>(
+      ElfObj->getFileName(),
+      Obj->getSectionStringTable(Sections, this->WarningHandler));
   size_t SectionIndex = 0;
   for (const Elf_Shdr &Sec : Sections) {
     Fields[0].Str = to_string(SectionIndex);
-    Fields[1].Str = unwrapOrError<StringRef>(
-        ElfObj->getFileName(), Obj->getSectionName(&Sec, this->WarningHandler));
+    if (SecStrTable.empty())
+      Fields[1].Str = "<no-strings>";
+    else
+      Fields[1].Str = unwrapOrError<StringRef>(
+          ElfObj->getFileName(), Obj->getSectionName(&Sec, SecStrTable));
     Fields[2].Str =
         getSectionTypeString(Obj->getHeader()->e_machine, Sec.sh_type);
     Fields[3].Str =


        


More information about the llvm-commits mailing list