[Object][ELF] Handle files with no section header string table.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Tue, 21 Jul 2015 21:40:33 +0000 (21:40 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Tue, 21 Jul 2015 21:40:33 +0000 (21:40 +0000)
llvm-svn: 242839

llvm/include/llvm/Object/ELF.h
llvm/test/Object/Inputs/no-section-header-string-table.elf-x86-64 [new file with mode: 0644]
llvm/test/Object/no-section-header-string-table.test [new file with mode: 0644]

index 775403c..ec7e59f 100644 (file)
@@ -629,14 +629,18 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
   }
 
   // Get string table sections.
-  ErrorOr<const Elf_Shdr *> StrTabSecOrErr = getSection(getStringTableIndex());
-  if ((EC = StrTabSecOrErr.getError()))
-    return;
+  uintX_t StringTableIndex = getStringTableIndex();
+  if (StringTableIndex) {
+    ErrorOr<const Elf_Shdr *> StrTabSecOrErr =
+        getSection(getStringTableIndex());
+    if ((EC = StrTabSecOrErr.getError()))
+      return;
 
-  ErrorOr<StringRef> SymtabOrErr = getStringTable(*StrTabSecOrErr);
-  if ((EC = SymtabOrErr.getError()))
-    return;
-  DotShstrtab = *SymtabOrErr;
+    ErrorOr<StringRef> SymtabOrErr = getStringTable(*StrTabSecOrErr);
+    if ((EC = SymtabOrErr.getError()))
+      return;
+    DotShstrtab = *SymtabOrErr;
+  }
 
   // Build symbol name side-mapping if there is one.
   if (SymbolTableSectionHeaderIndex) {
@@ -746,6 +750,8 @@ template <class ELFT>
 ErrorOr<StringRef>
 ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
   uint32_t Offset = Section->sh_name;
+  if (Offset == 0)
+    return StringRef();
   if (Offset >= DotShstrtab.size())
     return object_error::parse_failed;
   return StringRef(DotShstrtab.data() + Offset);
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
new file mode 100644 (file)
index 0000000..9024ad9
Binary files /dev/null and b/llvm/test/Object/Inputs/no-section-header-string-table.elf-x86-64 differ
diff --git a/llvm/test/Object/no-section-header-string-table.test b/llvm/test/Object/no-section-header-string-table.test
new file mode 100644 (file)
index 0000000..1f44932
--- /dev/null
@@ -0,0 +1,10 @@
+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)