[llvm-readobj] - Don`t crash when --section-symbols is requested for an object w...
authorGeorgii Rymar <grimar@accesssoftek.com>
Tue, 25 Aug 2020 10:37:32 +0000 (13:37 +0300)
committerGeorgii Rymar <grimar@accesssoftek.com>
Wed, 26 Aug 2020 11:13:05 +0000 (14:13 +0300)
llvm-readobj crashes when `-S --section-symbols` is used
on an object that has no symbol table.

The patch fixes it.

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

llvm/test/tools/llvm-readobj/ELF/sections-ext.test
llvm/tools/llvm-readobj/ELFDumper.cpp

index cc58f6d..f061e4e 100644 (file)
@@ -217,3 +217,28 @@ Sections:
 Symbols:
   - Name:    foo
     Section: .text
+
+## Check the output for an arbitrary section when --section-symbols is requested,
+## but there is no symbol table in the object.
+# RUN: yaml2obj --docnum=2 %s -o %t2
+# RUN: llvm-readobj -S --section-symbols %t2 2>&1 | \
+# RUN:   FileCheck %s --implicit-check-not=warning: --check-prefix=NOSYMTAB
+
+# NOSYMTAB:      Section {
+# NOSYMTAB:        Index: 1
+# NOSYMTAB-NEXT:   Name: .foo
+# NOSYMTAB:        Symbols [
+# NOSYMTAB-NEXT:   ]
+# NOSYMTAB-NEXT: }
+# NOSYMTAB-NEXT: Section {
+# NOSYMTAB-NEXT:   Index: 2
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+  - Name: .foo
+    Type: SHT_PROGBITS
index 7b6781e..644489f 100644 (file)
@@ -6297,20 +6297,21 @@ void LLVMStyle<ELFT>::printSectionHeaders(const ELFO *Obj) {
 
     if (opts::SectionSymbols) {
       ListScope D(W, "Symbols");
-      const Elf_Shdr *Symtab = this->dumper()->getDotSymtabSec();
-      StringRef StrTable =
-          unwrapOrError(this->FileName, Obj->getStringTableForSymtab(*Symtab));
-
-      for (const Elf_Sym &Sym :
-           unwrapOrError(this->FileName, Obj->symbols(Symtab))) {
-        const Elf_Shdr *SymSec = unwrapOrError(
-            this->FileName,
-            Obj->getSection(&Sym, Symtab, this->dumper()->getShndxTable()));
-        if (SymSec == &Sec)
-          printSymbol(
-              Obj, &Sym,
-              unwrapOrError(this->FileName, Obj->symbols(Symtab)).begin(),
-              StrTable, false, false);
+      if (const Elf_Shdr *Symtab = this->dumper()->getDotSymtabSec()) {
+        StringRef StrTable = unwrapOrError(
+            this->FileName, Obj->getStringTableForSymtab(*Symtab));
+
+        for (const Elf_Sym &Sym :
+             unwrapOrError(this->FileName, Obj->symbols(Symtab))) {
+          const Elf_Shdr *SymSec = unwrapOrError(
+              this->FileName,
+              Obj->getSection(&Sym, Symtab, this->dumper()->getShndxTable()));
+          if (SymSec == &Sec)
+            printSymbol(
+                Obj, &Sym,
+                unwrapOrError(this->FileName, Obj->symbols(Symtab)).begin(),
+                StrTable, false, false);
+        }
       }
     }