[llvm] r337360 - [llvm-readobj] - Teach tool to dump objects with >= SHN_LORESERVE of sections.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 18 01:19:58 PDT 2018


Author: grimar
Date: Wed Jul 18 01:19:58 2018
New Revision: 337360

URL: http://llvm.org/viewvc/llvm-project?rev=337360&view=rev
Log:
[llvm-readobj] - Teach tool to dump objects with >= SHN_LORESERVE of sections.

http://www.sco.com/developers/gabi/2003-12-17/ch4.eheader.html

says that e_shnum and/or e_shstrndx may have special values if
"the number of sections is greater than or equal to SHN_LORESERVE" or
"the section name string table section index is greater than or equal to SHN_LORESERVE (0xff00)"

Previously llvm-readobj was unable to dump such files, patch changes that.

I had to add a precompiled test case because it does not seem possible to
prepare a test using yaml2obj or llvm-mc (not clear how to make .shstrtab
to have index >= SHN_LORESERVE).

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

Added:
    llvm/trunk/test/tools/llvm-readobj/Inputs/many-sections-stripped.elf-x86_64   (with props)
    llvm/trunk/test/tools/llvm-readobj/Inputs/many-sections.elf-x86_64   (with props)
    llvm/trunk/test/tools/llvm-readobj/many-sections.s
Modified:
    llvm/trunk/tools/llvm-readobj/ELFDumper.cpp

Added: llvm/trunk/test/tools/llvm-readobj/Inputs/many-sections-stripped.elf-x86_64
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/Inputs/many-sections-stripped.elf-x86_64?rev=337360&view=auto
==============================================================================
Binary file - no diff available.

Propchange: llvm/trunk/test/tools/llvm-readobj/Inputs/many-sections-stripped.elf-x86_64
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: llvm/trunk/test/tools/llvm-readobj/Inputs/many-sections.elf-x86_64
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/Inputs/many-sections.elf-x86_64?rev=337360&view=auto
==============================================================================
Binary file - no diff available.

Propchange: llvm/trunk/test/tools/llvm-readobj/Inputs/many-sections.elf-x86_64
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: llvm/trunk/test/tools/llvm-readobj/many-sections.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/many-sections.s?rev=337360&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-readobj/many-sections.s (added)
+++ llvm/trunk/test/tools/llvm-readobj/many-sections.s Wed Jul 18 01:19:58 2018
@@ -0,0 +1,36 @@
+## many-sections.elf-x86_64 is a file that was generated to simulate
+## an object with more than ~65k sections. When an ELF object
+## has SHN_LORESERVE (0xff00) or more sections, its e_shnum field
+## should be zero and sh_size of the section header at index 0 is used
+## to store the value. If the section name string table section index is
+## greater than or equal to SHN_LORESERVE, then e_shstrndx field
+## should have the value of SHN_XINDEX and sh_link of the section header
+## at index 0 is used to store the value.
+##
+## many-sections.elf-x86_64 has few sections to save disk
+## space, but its e_shnum, e_shstrndx, sh_size and sh_link fields are set
+## according to the above description, so that we can test the dumper.
+
+# RUN: llvm-readobj -file-headers -elf-output-style GNU \
+# RUN:   %p/Inputs/many-sections.elf-x86_64 | FileCheck %s --check-prefix=GNU1
+# GNU1: Number of section headers:         0 (5)
+# GNU1: Section header string table index: 65535 (3)
+
+# RUN: llvm-readobj -file-headers -elf-output-style LLVM \
+# RUN:   %p/Inputs/many-sections.elf-x86_64 | FileCheck %s --check-prefix=LLVM1
+# LLVM1: SectionHeaderCount: 0 (5)
+# LLVM1: StringTableSectionIndex: 65535 (3)
+
+## many-sections-stripped.elf-x86_64 is many-sections.elf-x86_64 with
+## e_shoff field set to zero, but not e_shstrndx, to show that
+## this corrupt case is handled correctly.
+
+# RUN: llvm-readobj -file-headers -elf-output-style GNU \
+# RUN:   %p/Inputs/many-sections-stripped.elf-x86_64 | FileCheck %s --check-prefix=GNU2
+# GNU2: Number of section headers:         0
+# GNU2: Section header string table index: 65535 (corrupt: out of range)
+
+# RUN: llvm-readobj -file-headers -elf-output-style LLVM \
+# RUN:   %p/Inputs/many-sections-stripped.elf-x86_64 | FileCheck %s --check-prefix=LLVM2
+# LLVM2: SectionHeaderCount: 0
+# LLVM2: StringTableSectionIndex: 65535 (corrupt: out of range)

Modified: llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ELFDumper.cpp?rev=337360&r1=337359&r2=337360&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ELFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/ELFDumper.cpp Wed Jul 18 01:19:58 2018
@@ -2486,6 +2486,30 @@ static inline void printFields(formatted
   OS.flush();
 }
 
+template <class ELFT>
+static std::string getSectionHeadersNumString(const ELFFile<ELFT> *Obj) {
+  const typename ELFT::Ehdr *ElfHeader = Obj->getHeader();
+  if (ElfHeader->e_shnum != 0)
+    return to_string(ElfHeader->e_shnum);
+
+  ArrayRef<typename ELFT::Shdr> Arr = unwrapOrError(Obj->sections());
+  if (Arr.empty())
+    return "0";
+  return "0 (" + to_string(Arr[0].sh_size) + ")";
+}
+
+template <class ELFT>
+static std::string getSectionHeaderTableIndexString(const ELFFile<ELFT> *Obj) {
+  const typename ELFT::Ehdr *ElfHeader = Obj->getHeader();
+  if (ElfHeader->e_shstrndx != SHN_XINDEX)
+    return to_string(ElfHeader->e_shstrndx);
+
+  ArrayRef<typename ELFT::Shdr> Arr = unwrapOrError(Obj->sections());
+  if (Arr.empty())
+    return "65535 (corrupt: out of range)";
+  return to_string(ElfHeader->e_shstrndx) + " (" + to_string(Arr[0].sh_link) + ")";
+}
+
 template <class ELFT> void GNUStyle<ELFT>::printFileHeaders(const ELFO *Obj) {
   const Elf_Ehdr *e = Obj->getHeader();
   OS << "ELF Header:\n";
@@ -2531,9 +2555,9 @@ template <class ELFT> void GNUStyle<ELFT
   printFields(OS, "Number of program headers:", Str);
   Str = to_string(e->e_shentsize) + " (bytes)";
   printFields(OS, "Size of section headers:", Str);
-  Str = to_string(e->e_shnum);
+  Str = getSectionHeadersNumString(Obj);
   printFields(OS, "Number of section headers:", Str);
-  Str = to_string(e->e_shstrndx);
+  Str = getSectionHeaderTableIndexString(Obj);
   printFields(OS, "Section header string table index:", Str);
 }
 
@@ -4019,8 +4043,8 @@ template <class ELFT> void LLVMStyle<ELF
     W.printNumber("ProgramHeaderEntrySize", e->e_phentsize);
     W.printNumber("ProgramHeaderCount", e->e_phnum);
     W.printNumber("SectionHeaderEntrySize", e->e_shentsize);
-    W.printNumber("SectionHeaderCount", e->e_shnum);
-    W.printNumber("StringTableSectionIndex", e->e_shstrndx);
+    W.printString("SectionHeaderCount", getSectionHeadersNumString(Obj));
+    W.printString("StringTableSectionIndex", getSectionHeaderTableIndexString(Obj));
   }
 }
 




More information about the llvm-commits mailing list