[llvm] 095f6fb - [llvm-readelf/obj] - Stop printing invalid names for unnamed section symbols.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 03:06:20 PDT 2020


Author: Georgii Rymar
Date: 2020-09-21T13:05:46+03:00
New Revision: 095f6fbbd7b61af205d761f6951a869ec4a61722

URL: https://github.com/llvm/llvm-project/commit/095f6fbbd7b61af205d761f6951a869ec4a61722
DIFF: https://github.com/llvm/llvm-project/commit/095f6fbbd7b61af205d761f6951a869ec4a61722.diff

LOG: [llvm-readelf/obj] - Stop printing invalid names for unnamed section symbols.

We have an issue with `ELFDumper<ELFT>::getSymbolSectionName`:
1) It is used deeply for both LLVM/GNU styles and might return LLVM-style only
   values to describe symbols: "Undefined", "Processor Specific", "Absolute", etc.

2) `getSymbolSectionName` is used by `getFullSymbolName` and these special values
   might appear instead of symbol names in many places.
   This occurs for unnamed section symbols currently.

This patch extracts the LLVM specific logic to `LLVMStyle<ELFT>::printSymbolSection`,
which seems to be the only place where we want to print the special values mentioned.
It also adds a meaningful new warning that is reported when we are unable to get
a section index for a section symbol.

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

Added: 
    

Modified: 
    llvm/test/tools/llvm-readobj/ELF/dyn-symbols.test
    llvm/test/tools/llvm-readobj/ELF/hash-symbols.test
    llvm/test/tools/llvm-readobj/ELF/mips-got.test
    llvm/test/tools/llvm-readobj/ELF/mips-plt.test
    llvm/test/tools/llvm-readobj/ELF/symbol-shndx.test
    llvm/tools/llvm-readobj/ELFDumper.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-readobj/ELF/dyn-symbols.test b/llvm/test/tools/llvm-readobj/ELF/dyn-symbols.test
index a438535cc1c8..9a9aa7d6d1ef 100644
--- a/llvm/test/tools/llvm-readobj/ELF/dyn-symbols.test
+++ b/llvm/test/tools/llvm-readobj/ELF/dyn-symbols.test
@@ -329,25 +329,30 @@ DynamicSymbols:
     Type: [[TYPE=STT_NOTYPE]]
 
 ## Check the behavior for unnamed versioned section symbols.
-## TODO: we should print proper symbol names instead of descriptions.
 # RUN: yaml2obj %s -DTYPE=STT_SECTION -DNAME="''" -DINDEX=SHN_ABS --docnum=6 -o %t6.sec.sym
-# RUN: llvm-readobj -V --dyn-symbols %t6.sec.sym | FileCheck %s --check-prefix=VERSIONED-SEC-SYM-LLVM
-# RUN: llvm-readelf -V --dyn-symbols %t6.sec.sym | FileCheck %s --check-prefix=VERSIONED-SEC-SYM-GNU
+# RUN: llvm-readobj -V --dyn-symbols %t6.sec.sym 2>&1 | \
+# RUN:   FileCheck %s -DFILE=%t6.sec.sym --check-prefix=VERSIONED-SEC-SYM-LLVM
+# RUN: llvm-readelf -V --dyn-symbols %t6.sec.sym 2>&1 | \
+# RUN:   FileCheck %s -DFILE=%t6.sec.sym --check-prefix=VERSIONED-SEC-SYM-GNU
 
 # VERSIONED-SEC-SYM-LLVM: DynamicSymbols [
 # VERSIONED-SEC-SYM-LLVM:  Name: foo (12)
-# VERSIONED-SEC-SYM-LLVM:  Name: Absolute (0)
-# VERSIONED-SEC-SYM-LLVM:  Name: Undefined (0)
+# VERSIONED-SEC-SYM-LLVM: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
+# VERSIONED-SEC-SYM-LLVM:  Name: <?> (0)
+# VERSIONED-SEC-SYM-LLVM: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0x0 (SHN_UNDEF)
+# VERSIONED-SEC-SYM-LLVM:  Name: <?> (0)
 # VERSIONED-SEC-SYM-LLVM: VersionSymbols [
 # VERSIONED-SEC-SYM-LLVM:  Name: foo
-# VERSIONED-SEC-SYM-LLVM:  Name: Absolute
-# VERSIONED-SEC-SYM-LLVM:  Name: Undefined
-
-# VERSIONED-SEC-SYM-GNU: Symbol table '.dynsym' contains 4 entries:
-# VERSIONED-SEC-SYM-GNU:    Num: {{.*}} Ndx Name
-# VERSIONED-SEC-SYM-GNU:      1: {{.*}} UND foo
-# VERSIONED-SEC-SYM-GNU-NEXT: 2: {{.*}} ABS Absolute
-# VERSIONED-SEC-SYM-GNU-NEXT: 3: {{.*}} UND Undefined
+# VERSIONED-SEC-SYM-LLVM:  Name: <?>
+# VERSIONED-SEC-SYM-LLVM:  Name: <?>
+
+# VERSIONED-SEC-SYM-GNU:     Symbol table '.dynsym' contains 4 entries:
+# VERSIONED-SEC-SYM-GNU:        Num: {{.*}} Ndx Name
+# VERSIONED-SEC-SYM-GNU:          1: {{.*}} UND foo
+# VERSIONED-SEC-SYM-GNU:      warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
+# VERSIONED-SEC-SYM-GNU-NEXT:     2: {{.*}} ABS <?>
+# VERSIONED-SEC-SYM-GNU-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0x0 (SHN_UNDEF)
+# VERSIONED-SEC-SYM-GNU-NEXT:     3: {{.*}} UND <?>
 
 ## Case 8: Check what we print when:
 ## a) The dynamic symbol table does not exist.

diff  --git a/llvm/test/tools/llvm-readobj/ELF/hash-symbols.test b/llvm/test/tools/llvm-readobj/ELF/hash-symbols.test
index 7488bd5514e5..b1c211416793 100644
--- a/llvm/test/tools/llvm-readobj/ELF/hash-symbols.test
+++ b/llvm/test/tools/llvm-readobj/ELF/hash-symbols.test
@@ -112,22 +112,24 @@ ProgramHeaders:
       - Section: .dynamic
 
 ## Check what we print for unnamed section symbols.
-## TODO: we should print proper symbol names instead of descriptions.
 # RUN: yaml2obj --docnum=1 -DBITS=64 -DTYPE=STT_SECTION -DNAME="''" %s -o %t1-sec-syms.so
-# RUN: llvm-readelf --hash-symbols %t1-sec-syms.so | FileCheck %s --check-prefix=UNNAMED-SEC-SYMS
+# RUN: llvm-readelf --hash-symbols %t1-sec-syms.so 2>&1 | \
+# RUN:   FileCheck %s -DFILE=%t1-sec-syms.so --check-prefix=UNNAMED-SEC-SYMS
 
 # UNNAMED-SEC-SYMS:      Symbol table of .hash for image:
 # UNNAMED-SEC-SYMS-NEXT:  Num   {{.*}} Ndx Name
-# UNNAMED-SEC-SYMS-NEXT:    1   {{.*}} UND Undefined
+# UNNAMED-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0x0 (SHN_UNDEF)
+# UNNAMED-SEC-SYMS-NEXT:    1   {{.*}} UND <?>
 # UNNAMED-SEC-SYMS-NEXT:    5   {{.*}}   1 .hash
-# UNNAMED-SEC-SYMS-NEXT:    3   {{.*}} ABS Absolute
+# UNNAMED-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
+# UNNAMED-SEC-SYMS-NEXT:    3   {{.*}} ABS <?>
 # UNNAMED-SEC-SYMS-NEXT:    2   {{.*}}   1 .hash
 # UNNAMED-SEC-SYMS-NEXT:    4   {{.*}}   2 .gnu.hash
 # UNNAMED-SEC-SYMS-EMPTY:
 # UNNAMED-SEC-SYMS:      Symbol table of .gnu.hash for image:
 # UNNAMED-SEC-SYMS-NEXT:  Num {{.*}} Ndx Name
 # UNNAMED-SEC-SYMS-NEXT:    2 {{.*}}   1 .hash
-# UNNAMED-SEC-SYMS-NEXT:    3 {{.*}} ABS Absolute
+# UNNAMED-SEC-SYMS-NEXT:    3 {{.*}} ABS <?>
 # UNNAMED-SEC-SYMS-NEXT:    4 {{.*}}   2 .gnu.hash
 # UNNAMED-SEC-SYMS-NEXT:    5 {{.*}}   1 .hash
 

diff  --git a/llvm/test/tools/llvm-readobj/ELF/mips-got.test b/llvm/test/tools/llvm-readobj/ELF/mips-got.test
index f1c3e4d1fc22..dac5284c5dee 100644
--- a/llvm/test/tools/llvm-readobj/ELF/mips-got.test
+++ b/llvm/test/tools/llvm-readobj/ELF/mips-got.test
@@ -653,7 +653,6 @@ DynamicSymbols:
   - Name: foo
 
 ## Check how we print global GOT entries when they are unnamed section symbols.
-## TODO: we should print proper symbol names instead of descriptions.
 # RUN: yaml2obj --docnum=5 %s -o %t.err8.o
 # RUN: llvm-readobj -A %t.err8.o 2>&1 | FileCheck %s -DFILE=%t.err8.o --check-prefix=SEC-SYMS-LLVM
 # RUN: llvm-readelf -A %t.err8.o 2>&1 | FileCheck %s -DFILE=%t.err8.o --check-prefix=SEC-SYMS-GNU
@@ -661,7 +660,8 @@ DynamicSymbols:
 # SEC-SYMS-LLVM:      Global entries [
 # SEC-SYMS-LLVM-NEXT:   Entry {
 # SEC-SYMS-LLVM:          Section: Absolute (0xFFF1)
-# SEC-SYMS-LLVM-NEXT:     Name: Absolute (0)
+# SEC-SYMS-LLVM-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
+# SEC-SYMS-LLVM-NEXT:     Name: <?> (0)
 # SEC-SYMS-LLVM-NEXT:   }
 # SEC-SYMS-LLVM-NEXT:   Entry {
 # SEC-SYMS-LLVM:          Section: .got (0x1)
@@ -669,15 +669,18 @@ DynamicSymbols:
 # SEC-SYMS-LLVM-NEXT:   }
 # SEC-SYMS-LLVM-NEXT:   Entry {
 # SEC-SYMS-LLVM:          Section: Common (0xFFF2)
-# SEC-SYMS-LLVM-NEXT:     Name: Common (0)
+# SEC-SYMS-LLVM-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
+# SEC-SYMS-LLVM-NEXT:     Name: <?> (0)
 # SEC-SYMS-LLVM-NEXT:   }
 # SEC-SYMS-LLVM-NEXT: ]
 
 # SEC-SYMS-GNU:      Global entries:
 # SEC-SYMS-GNU-NEXT:  {{.*}} Ndx Name
-# SEC-SYMS-GNU-NEXT:  {{.*}} ABS Absolute
+# SEC-SYMS-GNU-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
+# SEC-SYMS-GNU-NEXT:  {{.*}} ABS <?>
 # SEC-SYMS-GNU-NEXT:  {{.*}}   1 .got
-# SEC-SYMS-GNU-NEXT:  {{.*}} COM Common
+# SEC-SYMS-GNU-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
+# SEC-SYMS-GNU-NEXT:  {{.*}} COM <?>
 
 --- !ELF
 FileHeader:

diff  --git a/llvm/test/tools/llvm-readobj/ELF/mips-plt.test b/llvm/test/tools/llvm-readobj/ELF/mips-plt.test
index 7f3fd0897747..3d4c70f38cd1 100644
--- a/llvm/test/tools/llvm-readobj/ELF/mips-plt.test
+++ b/llvm/test/tools/llvm-readobj/ELF/mips-plt.test
@@ -142,16 +142,16 @@ DynamicSymbols: []
 # ERR7: error: '[[FILE]]': unable to get a string table for the SHT_DYNAMIC section with index 1: invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM
 
 ## Check how we print PLT entries when they are unnamed section symbols.
-## TODO: we should print proper symbol names instead of descriptions.
-# RUN: yaml2obj --docnum=3 %s -o %t.3
-# RUN: llvm-readobj -A %t.3 2>&1 | FileCheck %s -DFILE=%t.err8.o --check-prefix=SEC-SYMS-LLVM
-# RUN: llvm-readelf -A %t.3 2>&1 | FileCheck %s -DFILE=%t.err8.o --check-prefix=SEC-SYMS-GNU
+# RUN: yaml2obj --docnum=3 %s -o %t3
+# RUN: llvm-readobj -A %t3 2>&1 | FileCheck %s -DFILE=%t3 --check-prefix=SEC-SYMS-LLVM
+# RUN: llvm-readelf -A %t3 2>&1 | FileCheck %s -DFILE=%t3 --check-prefix=SEC-SYMS-GNU
 
 # SEC-SYMS-LLVM:      PLT GOT {
 # SEC-SYMS-LLVM:        Entries [
 # SEC-SYMS-LLVM:          Entry {
 # SEC-SYMS-LLVM:            Section: Absolute (0xFFF1)
-# SEC-SYMS-LLVM-NEXT:       Name: Absolute (0)
+# SEC-SYMS-LLVM-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
+# SEC-SYMS-LLVM-NEXT:       Name: <?> (0)
 # SEC-SYMS-LLVM-NEXT:     }
 # SEC-SYMS-LLVM-NEXT:     Entry {
 # SEC-SYMS-LLVM:            Section: .got.plt (0x2)
@@ -159,7 +159,8 @@ DynamicSymbols: []
 # SEC-SYMS-LLVM-NEXT:     }
 # SEC-SYMS-LLVM-NEXT:     Entry {
 # SEC-SYMS-LLVM:            Section: Common (0xFFF2)
-# SEC-SYMS-LLVM-NEXT:       Name: Common (0)
+# SEC-SYMS-LLVM-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
+# SEC-SYMS-LLVM-NEXT:       Name: <?> (0)
 # SEC-SYMS-LLVM-NEXT:     }
 # SEC-SYMS-LLVM-NEXT:   ]
 # SEC-SYMS-LLVM-NEXT: }
@@ -167,9 +168,11 @@ DynamicSymbols: []
 # SEC-SYMS-GNU:      PLT GOT:
 # SEC-SYMS-GNU:       Entries:
 # SEC-SYMS-GNU-NEXT:   Address          {{.*}} Ndx Name
-# SEC-SYMS-GNU-NEXT:   0000000000002010 {{.*}} ABS Absolute
+# SEC-SYMS-GNU-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
+# SEC-SYMS-GNU-NEXT:   0000000000002010 {{.*}} ABS <?>
 # SEC-SYMS-GNU-NEXT:   0000000000002018 {{.*}}   2 .got.plt
-# SEC-SYMS-GNU-NEXT:   0000000000002020 {{.*}} COM Common
+# SEC-SYMS-GNU-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
+# SEC-SYMS-GNU-NEXT:   0000000000002020 {{.*}} COM <?>
 
 --- !ELF
 FileHeader:

diff  --git a/llvm/test/tools/llvm-readobj/ELF/symbol-shndx.test b/llvm/test/tools/llvm-readobj/ELF/symbol-shndx.test
index b2d1e2f6d2ec..1163b0b8fafa 100644
--- a/llvm/test/tools/llvm-readobj/ELF/symbol-shndx.test
+++ b/llvm/test/tools/llvm-readobj/ELF/symbol-shndx.test
@@ -95,49 +95,62 @@ Symbols:
 # RUN: llvm-readelf --symbols %t1-sec | FileCheck %s --check-prefix=GNU1
 
 ## Check the behavior for unnamed section symbols.
-## TODO: we should print proper symbol names instead of descriptions.
 # RUN: yaml2obj --docnum=1 -DTYPE=STT_SECTION -DNAME="''" %s -o %t1-sec-unnamed
-# RUN: llvm-readobj --symbols %t1-sec-unnamed | FileCheck %s --check-prefix=LLVM1-SEC-SYMS
-# RUN: llvm-readelf --symbols %t1-sec-unnamed | FileCheck %s --check-prefix=GNU1-SEC-SYMS
+# RUN: llvm-readobj --symbols %t1-sec-unnamed 2>&1 | \
+# RUN:   FileCheck %s -DFILE=%t1-sec-unnamed --check-prefix=LLVM1-SEC-SYMS
+# RUN: llvm-readelf --symbols %t1-sec-unnamed 2>&1 | \
+# RUN:   FileCheck %s -DFILE=%t1-sec-unnamed --check-prefix=GNU1-SEC-SYMS
 
 # LLVM1-SEC-SYMS:      Symbols [
 # LLVM1-SEC-SYMS-NEXT:   Symbol {
 # LLVM1-SEC-SYMS-NEXT:     Name:  (0)
 # LLVM1-SEC-SYMS:          Section: Undefined (0x0)
-# LLVM1-SEC-SYMS:        Symbol {
-# LLVM1-SEC-SYMS-NEXT:     Name: Undefined (0)
+# LLVM1-SEC-SYMS: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0x0 (SHN_UNDEF)
+# LLVM1-SEC-SYMS-NEXT:   Symbol {
+# LLVM1-SEC-SYMS-NEXT:     Name: <?> (0)
 # LLVM1-SEC-SYMS:          Section: Undefined (0x0)
 # LLVM1-SEC-SYMS:        Symbol {
 # LLVM1-SEC-SYMS-NEXT:     Name: .text (0)
 # LLVM1-SEC-SYMS:          Section: .text (0x1)
-# LLVM1-SEC-SYMS:        Symbol {
-# LLVM1-SEC-SYMS-NEXT:     Name: Common (0)
+# LLVM1-SEC-SYMS: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
+# LLVM1-SEC-SYMS-NEXT:   Symbol {
+# LLVM1-SEC-SYMS-NEXT:     Name: <?> (0)
 # LLVM1-SEC-SYMS:          Section: Common (0xFFF2)
-# LLVM1-SEC-SYMS:        Symbol {
-# LLVM1-SEC-SYMS-NEXT:     Name: Absolute (0)
+# LLVM1-SEC-SYMS: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
+# LLVM1-SEC-SYMS-NEXT:   Symbol {
+# LLVM1-SEC-SYMS-NEXT:     Name: <?> (0)
 # LLVM1-SEC-SYMS:          Section: Absolute (0xFFF1)
-# LLVM1-SEC-SYMS:        Symbol {
-# LLVM1-SEC-SYMS-NEXT:     Name: Processor Specific (0)
+# LLVM1-SEC-SYMS: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xff01 (SHN_LOPROC+0x1)
+# LLVM1-SEC-SYMS-NEXT:   Symbol {
+# LLVM1-SEC-SYMS-NEXT:     Name: <?> (0)
 # LLVM1-SEC-SYMS:          Section: Processor Specific (0xFF01)
-# LLVM1-SEC-SYMS:        Symbol {
-# LLVM1-SEC-SYMS-NEXT:     Name: Operating System Specific (0)
+# LLVM1-SEC-SYMS: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xff21 (SHN_LOOS+0x1)
+# LLVM1-SEC-SYMS-NEXT:   Symbol {
+# LLVM1-SEC-SYMS-NEXT:     Name: <?> (0)
 # LLVM1-SEC-SYMS:          Section: Operating System Specific (0xFF21)
-# LLVM1-SEC-SYMS:        Symbol {
-# LLVM1-SEC-SYMS-NEXT:     Name: Reserved (0)
+# LLVM1-SEC-SYMS: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfffe (SHN_LORESERVE+0xfe)
+# LLVM1-SEC-SYMS-NEXT:   Symbol {
+# LLVM1-SEC-SYMS-NEXT:     Name: <?> (0)
 # LLVM1-SEC-SYMS:          Section: Reserved (0xFFFE)
 # LLVM1-SEC-SYMS:        Symbol {
 # LLVM1-SEC-SYMS-NEXT:     Name: .text (0)
 # LLVM1-SEC-SYMS:          Section: .text (0x1)
 
 # GNU1-SEC-SYMS:      Num: {{.*}} Ndx Name
-# GNU1-SEC-SYMS-NEXT:   0: {{.*}} UND
-# GNU1-SEC-SYMS-NEXT:   1: {{.*}} UND Undefined
+# GNU1-SEC-SYMS-NEXT:   0: {{.*}} UND {{$}}
+# GNU1-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0x0 (SHN_UNDEF)
+# GNU1-SEC-SYMS-NEXT:   1: {{.*}} UND <?>
 # GNU1-SEC-SYMS-NEXT:   2: {{.*}}   1 .text
-# GNU1-SEC-SYMS-NEXT:   3: {{.*}} COM Common
-# GNU1-SEC-SYMS-NEXT:   4: {{.*}} ABS Absolute
-# GNU1-SEC-SYMS-NEXT:   5: {{.*}} PRC[0xff01] Processor Specific
-# GNU1-SEC-SYMS-NEXT:   6: {{.*}} OS[0xff21] Operating System Specific
-# GNU1-SEC-SYMS-NEXT:   7: {{.*}} RSV[0xfffe] Reserved
+# GNU1-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
+# GNU1-SEC-SYMS-NEXT:   3: {{.*}} COM <?>
+# GNU1-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
+# GNU1-SEC-SYMS-NEXT:   4: {{.*}} ABS <?>
+# GNU1-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xff01 (SHN_LOPROC+0x1)
+# GNU1-SEC-SYMS-NEXT:   5: {{.*}} PRC[0xff01] <?>
+# GNU1-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xff21 (SHN_LOOS+0x1)
+# GNU1-SEC-SYMS-NEXT:   6: {{.*}} OS[0xff21] <?>
+# GNU1-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfffe (SHN_LORESERVE+0xfe)
+# GNU1-SEC-SYMS-NEXT:   7: {{.*}} RSV[0xfffe] <?>
 # GNU1-SEC-SYMS-NEXT:   8: {{.*}}   1 .text
 
 ## In this case, the index does not correspond to a real section. Check that GNU

diff  --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 051308ed7d44..f9da512e3a91 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -1225,37 +1225,43 @@ template <typename ELFT>
 Expected<unsigned>
 ELFDumper<ELFT>::getSymbolSectionIndex(const Elf_Sym *Symbol,
                                        const Elf_Sym *FirstSym) const {
-  return Symbol->st_shndx == SHN_XINDEX
-             ? object::getExtendedSymbolTableIndex<ELFT>(*Symbol, *FirstSym,
-                                                         ShndxTable)
-             : Symbol->st_shndx;
+  unsigned Ndx = Symbol->st_shndx;
+  if (Ndx == SHN_XINDEX)
+    return object::getExtendedSymbolTableIndex<ELFT>(*Symbol, *FirstSym,
+                                                     ShndxTable);
+  if (Ndx != SHN_UNDEF && Ndx < SHN_LORESERVE)
+    return Ndx;
+
+  auto CreateErr = [&](const Twine &Name, Optional<unsigned> Offset = None) {
+    std::string Desc;
+    if (Offset)
+      Desc = (Name + "+0x" + Twine::utohexstr(*Offset)).str();
+    else
+      Desc = Name.str();
+    return createError(
+        "unable to get section index for symbol with st_shndx = 0x" +
+        Twine::utohexstr(Ndx) + " (" + Desc + ")");
+  };
+
+  if (Ndx >= ELF::SHN_LOPROC && Ndx <= ELF::SHN_HIPROC)
+    return CreateErr("SHN_LOPROC", Ndx - ELF::SHN_LOPROC);
+  if (Ndx >= ELF::SHN_LOOS && Ndx <= ELF::SHN_HIOS)
+    return CreateErr("SHN_LOOS", Ndx - ELF::SHN_LOOS);
+  if (Ndx == ELF::SHN_UNDEF)
+    return CreateErr("SHN_UNDEF");
+  if (Ndx == ELF::SHN_ABS)
+    return CreateErr("SHN_ABS");
+  if (Ndx == ELF::SHN_COMMON)
+    return CreateErr("SHN_COMMON");
+  return CreateErr("SHN_LORESERVE", Ndx - SHN_LORESERVE);
 }
 
-// If the Symbol has a reserved st_shndx other than SHN_XINDEX, return a
-// descriptive interpretation of the st_shndx value. Otherwise, return the name
-// of the section with index SectionIndex. This function assumes that if the
-// Symbol has st_shndx == SHN_XINDEX the SectionIndex will be the value derived
-// from the SHT_SYMTAB_SHNDX section.
 template <typename ELFT>
 Expected<StringRef>
 ELFDumper<ELFT>::getSymbolSectionName(const Elf_Sym *Symbol,
                                       unsigned SectionIndex) const {
-  if (Symbol->isUndefined())
-    return "Undefined";
-  if (Symbol->isProcessorSpecific())
-    return "Processor Specific";
-  if (Symbol->isOSSpecific())
-    return "Operating System Specific";
-  if (Symbol->isAbsolute())
-    return "Absolute";
-  if (Symbol->isCommon())
-    return "Common";
-  if (Symbol->isReserved() && Symbol->st_shndx != SHN_XINDEX)
-    return "Reserved";
-
   const ELFFile<ELFT> *Obj = ObjF->getELFFile();
-  Expected<const Elf_Shdr *> SecOrErr =
-      Obj->getSection(SectionIndex);
+  Expected<const Elf_Shdr *> SecOrErr = Obj->getSection(SectionIndex);
   if (!SecOrErr)
     return SecOrErr.takeError();
   return Obj->getSectionName(**SecOrErr);
@@ -3933,7 +3939,7 @@ std::string GNUStyle<ELFT>::getSymbolSectionNdx(const Elf_Sym *Symbol,
         *Symbol, *FirstSym, this->dumper()->getShndxTable());
     if (!IndexOrErr) {
       assert(Symbol->st_shndx == SHN_XINDEX &&
-             "getSymbolSectionIndex should only fail due to an invalid "
+             "getExtendedSymbolTableIndex should only fail due to an invalid "
              "SHT_SYMTAB_SHNDX table/reference");
       this->reportUniqueWarning(IndexOrErr.takeError());
       return "RSV[0xffff]";
@@ -6168,6 +6174,27 @@ template <class ELFT> void LLVMStyle<ELFT>::printSectionHeaders() {
 template <class ELFT>
 void LLVMStyle<ELFT>::printSymbolSection(const Elf_Sym *Symbol,
                                          const Elf_Sym *First) {
+  auto GetSectionSpecialType = [&]() -> Optional<StringRef> {
+    if (Symbol->isUndefined())
+      return StringRef("Undefined");
+    if (Symbol->isProcessorSpecific())
+      return StringRef("Processor Specific");
+    if (Symbol->isOSSpecific())
+      return StringRef("Operating System Specific");
+    if (Symbol->isAbsolute())
+      return StringRef("Absolute");
+    if (Symbol->isCommon())
+      return StringRef("Common");
+    if (Symbol->isReserved() && Symbol->st_shndx != SHN_XINDEX)
+      return StringRef("Reserved");
+    return None;
+  };
+
+  if (Optional<StringRef> Type = GetSectionSpecialType()) {
+    W.printHex("Section", *Type, Symbol->st_shndx);
+    return;
+  }
+
   Expected<unsigned> SectionIndex =
       this->dumper()->getSymbolSectionIndex(Symbol, First);
   if (!SectionIndex) {


        


More information about the llvm-commits mailing list