[llvm] 31fe8c2 - [llvm-readelf] - Don't crash when e_shstrndx==SHN_XINDEX, but there is no section header.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 24 04:10:26 PDT 2020


Author: Georgii Rymar
Date: 2020-06-24T14:09:34+03:00
New Revision: 31fe8c2763a8982e16646f85ac09f2362341306c

URL: https://github.com/llvm/llvm-project/commit/31fe8c2763a8982e16646f85ac09f2362341306c
DIFF: https://github.com/llvm/llvm-project/commit/31fe8c2763a8982e16646f85ac09f2362341306c.diff

LOG: [llvm-readelf] - Don't crash when e_shstrndx==SHN_XINDEX, but there is no section header.

Currently we crash when trying to print --sections and the SHN_XINDEX escape value
is used for the e_shstrndx field, but there is no section header at index 0 to
read the value from.

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

Added: 
    

Modified: 
    llvm/include/llvm/Object/ELF.h
    llvm/test/tools/llvm-readobj/ELF/many-sections.s

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Object/ELF.h b/llvm/include/llvm/Object/ELF.h
index 15d473805ecc..c7f2a8e709f0 100644
--- a/llvm/include/llvm/Object/ELF.h
+++ b/llvm/include/llvm/Object/ELF.h
@@ -516,8 +516,17 @@ Expected<StringRef>
 ELFFile<ELFT>::getSectionStringTable(Elf_Shdr_Range Sections,
                                      WarningHandler WarnHandler) const {
   uint32_t Index = getHeader()->e_shstrndx;
-  if (Index == ELF::SHN_XINDEX)
+  if (Index == ELF::SHN_XINDEX) {
+    // If the section name string table section index is greater than
+    // or equal to SHN_LORESERVE, then the actual index of the section name
+    // string table section is contained in the sh_link field of the section
+    // header at index 0.
+    if (Sections.empty())
+      return createError(
+          "e_shstrndx == SHN_XINDEX, but the section header table is empty");
+
     Index = Sections[0].sh_link;
+  }
 
   if (!Index) // no section string table.
     return "";

diff  --git a/llvm/test/tools/llvm-readobj/ELF/many-sections.s b/llvm/test/tools/llvm-readobj/ELF/many-sections.s
index ae7ce34706ca..482476feca5b 100644
--- a/llvm/test/tools/llvm-readobj/ELF/many-sections.s
+++ b/llvm/test/tools/llvm-readobj/ELF/many-sections.s
@@ -34,13 +34,24 @@ Sections:
 
 # RUN: yaml2obj --docnum=2 %s -o %t2
 
-# RUN: llvm-readelf --file-headers %t2 | FileCheck %s --check-prefix=GNU2
+# RUN: not llvm-readelf --file-headers --sections %t2 2>&1 | \
+# RUN:   FileCheck %s -DFILE=%t2 --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 %t2 | FileCheck %s --check-prefix=LLVM2
-# LLVM2: SectionHeaderCount: 0
-# LLVM2: StringTableSectionIndex: 65535 (corrupt: out of range)
+# GNU2:       There are 0 section headers, starting at offset 0x0:
+# GNU2-EMPTY:
+# GNU2-NEXT:  Section Headers:
+# GNU2-NEXT:   [Nr] Name Type Address Off Size ES Flg Lk Inf Al
+# GNU2-NEXT:   error: '[[FILE]]': e_shstrndx == SHN_XINDEX, but the section header table is empty
+
+# RUN: llvm-readobj --file-headers --sections %t2 | \
+# RUN:   FileCheck %s --check-prefix=LLVM2 --implicit-check-not="warning:"
+# LLVM2:       SectionHeaderCount: 0
+# LLVM2:       StringTableSectionIndex: 65535 (corrupt: out of range)
+# LLVM2-NEXT: }
+# LLVM2-NEXT: Sections [
+# LLVM2-NEXT: ]
 
 --- !ELF
 FileHeader:


        


More information about the llvm-commits mailing list