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

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 16 06:02:06 PDT 2018


grimar created this revision.
grimar added a reviewer: ruiu.

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).


https://reviews.llvm.org/D49369

Files:
  test/tools/llvm-readobj/Inputs/many-sections.elf-x86_64
  test/tools/llvm-readobj/many-sections.s
  tools/llvm-readobj/ELFDumper.cpp


Index: tools/llvm-readobj/ELFDumper.cpp
===================================================================
--- tools/llvm-readobj/ELFDumper.cpp
+++ tools/llvm-readobj/ELFDumper.cpp
@@ -2470,6 +2470,24 @@
   OS.flush();
 }
 
+template <class ELFT>
+static std::string getSectionHeadersNumString(const ELFFile<ELFT> *Obj) {
+  const typename ELFT::Ehdr *ElfHeader = Obj->getHeader();
+  if (ElfHeader->e_shnum)
+    return to_string(ElfHeader->e_shnum);
+  const typename ELFFile<ELFT>::Elf_Shdr *Sec = unwrapOrError(Obj->getSection(0));
+  return "0 (" + to_string(Sec->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);
+  const typename ELFFile<ELFT>::Elf_Shdr *Sec = unwrapOrError(Obj->getSection(0));
+  return to_string(ElfHeader->e_shstrndx) + " (" + to_string(Sec->sh_link) + ")";
+}
+
 template <class ELFT> void GNUStyle<ELFT>::printFileHeaders(const ELFO *Obj) {
   const Elf_Ehdr *e = Obj->getHeader();
   OS << "ELF Header:\n";
@@ -2515,9 +2533,9 @@
   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);
 }
 
@@ -2560,13 +2578,13 @@
 
     StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
     StringRef Signature = StrTable.data() + Sym->st_name;
-    Ret.push_back({Name, 
-                   Signature, 
-                   Sec.sh_name, 
+    Ret.push_back({Name,
+                   Signature,
+                   Sec.sh_name,
                    I - 1,
                    Sec.sh_link,
                    Sec.sh_info,
-                   Data[0], 
+                   Data[0],
                    {}});
 
     std::vector<GroupMember> &GM = Ret.back().Members;
@@ -3996,8 +4014,8 @@
     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));
   }
 }
 
Index: test/tools/llvm-readobj/many-sections.s
===================================================================
--- test/tools/llvm-readobj/many-sections.s
+++ test/tools/llvm-readobj/many-sections.s
@@ -0,0 +1,22 @@
+## many-sections.elf-x86_64 is a file that generated to simulate
+## an object with more than ~65k sections. When ELF object
+## has SHN_LORESERVE (0xff00) or more of sections, it's e_shnum field
+## should be zero and sh_size of the section header at index 0 used
+## to store the value. If 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 used to store the value.
+##
+## many-sections.elf-x86_64 has little amount of sections to save disk
+## space, but its e_shnum, e_shstrndx, sh_size and sh_link fields are set
+## in according to 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=GNU
+# GNU: Number of section headers:         0 (5)
+# GNU: 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=LLVM
+# LLVM: SectionHeaderCount: 0 (5)
+# LLVM: StringTableSectionIndex: 65535 (3)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49369.155654.patch
Type: text/x-patch
Size: 4099 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180716/4299b98a/attachment.bin>


More information about the llvm-commits mailing list