Skip to content

Commit 095f6fb

Browse files
author
Georgii Rymar
committed
[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
1 parent 13c7363 commit 095f6fb

File tree

6 files changed

+131
-78
lines changed

6 files changed

+131
-78
lines changed

llvm/test/tools/llvm-readobj/ELF/dyn-symbols.test

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -329,25 +329,30 @@ DynamicSymbols:
329329
Type: [[TYPE=STT_NOTYPE]]
330330

331331
## Check the behavior for unnamed versioned section symbols.
332-
## TODO: we should print proper symbol names instead of descriptions.
333332
# RUN: yaml2obj %s -DTYPE=STT_SECTION -DNAME="''" -DINDEX=SHN_ABS --docnum=6 -o %t6.sec.sym
334-
# RUN: llvm-readobj -V --dyn-symbols %t6.sec.sym | FileCheck %s --check-prefix=VERSIONED-SEC-SYM-LLVM
335-
# RUN: llvm-readelf -V --dyn-symbols %t6.sec.sym | FileCheck %s --check-prefix=VERSIONED-SEC-SYM-GNU
333+
# RUN: llvm-readobj -V --dyn-symbols %t6.sec.sym 2>&1 | \
334+
# RUN: FileCheck %s -DFILE=%t6.sec.sym --check-prefix=VERSIONED-SEC-SYM-LLVM
335+
# RUN: llvm-readelf -V --dyn-symbols %t6.sec.sym 2>&1 | \
336+
# RUN: FileCheck %s -DFILE=%t6.sec.sym --check-prefix=VERSIONED-SEC-SYM-GNU
336337

337338
# VERSIONED-SEC-SYM-LLVM: DynamicSymbols [
338339
# VERSIONED-SEC-SYM-LLVM: Name: foo (12)
339-
# VERSIONED-SEC-SYM-LLVM: Name: Absolute (0)
340-
# VERSIONED-SEC-SYM-LLVM: Name: Undefined (0)
340+
# VERSIONED-SEC-SYM-LLVM: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
341+
# VERSIONED-SEC-SYM-LLVM: Name: <?> (0)
342+
# VERSIONED-SEC-SYM-LLVM: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0x0 (SHN_UNDEF)
343+
# VERSIONED-SEC-SYM-LLVM: Name: <?> (0)
341344
# VERSIONED-SEC-SYM-LLVM: VersionSymbols [
342345
# VERSIONED-SEC-SYM-LLVM: Name: foo
343-
# VERSIONED-SEC-SYM-LLVM: Name: Absolute
344-
# VERSIONED-SEC-SYM-LLVM: Name: Undefined
345-
346-
# VERSIONED-SEC-SYM-GNU: Symbol table '.dynsym' contains 4 entries:
347-
# VERSIONED-SEC-SYM-GNU: Num: {{.*}} Ndx Name
348-
# VERSIONED-SEC-SYM-GNU: 1: {{.*}} UND foo
349-
# VERSIONED-SEC-SYM-GNU-NEXT: 2: {{.*}} ABS Absolute
350-
# VERSIONED-SEC-SYM-GNU-NEXT: 3: {{.*}} UND Undefined
346+
# VERSIONED-SEC-SYM-LLVM: Name: <?>
347+
# VERSIONED-SEC-SYM-LLVM: Name: <?>
348+
349+
# VERSIONED-SEC-SYM-GNU: Symbol table '.dynsym' contains 4 entries:
350+
# VERSIONED-SEC-SYM-GNU: Num: {{.*}} Ndx Name
351+
# VERSIONED-SEC-SYM-GNU: 1: {{.*}} UND foo
352+
# VERSIONED-SEC-SYM-GNU: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
353+
# VERSIONED-SEC-SYM-GNU-NEXT: 2: {{.*}} ABS <?>
354+
# VERSIONED-SEC-SYM-GNU-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0x0 (SHN_UNDEF)
355+
# VERSIONED-SEC-SYM-GNU-NEXT: 3: {{.*}} UND <?>
351356

352357
## Case 8: Check what we print when:
353358
## a) The dynamic symbol table does not exist.

llvm/test/tools/llvm-readobj/ELF/hash-symbols.test

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,24 @@ ProgramHeaders:
112112
- Section: .dynamic
113113

114114
## Check what we print for unnamed section symbols.
115-
## TODO: we should print proper symbol names instead of descriptions.
116115
# RUN: yaml2obj --docnum=1 -DBITS=64 -DTYPE=STT_SECTION -DNAME="''" %s -o %t1-sec-syms.so
117-
# RUN: llvm-readelf --hash-symbols %t1-sec-syms.so | FileCheck %s --check-prefix=UNNAMED-SEC-SYMS
116+
# RUN: llvm-readelf --hash-symbols %t1-sec-syms.so 2>&1 | \
117+
# RUN: FileCheck %s -DFILE=%t1-sec-syms.so --check-prefix=UNNAMED-SEC-SYMS
118118

119119
# UNNAMED-SEC-SYMS: Symbol table of .hash for image:
120120
# UNNAMED-SEC-SYMS-NEXT: Num {{.*}} Ndx Name
121-
# UNNAMED-SEC-SYMS-NEXT: 1 {{.*}} UND Undefined
121+
# UNNAMED-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0x0 (SHN_UNDEF)
122+
# UNNAMED-SEC-SYMS-NEXT: 1 {{.*}} UND <?>
122123
# UNNAMED-SEC-SYMS-NEXT: 5 {{.*}} 1 .hash
123-
# UNNAMED-SEC-SYMS-NEXT: 3 {{.*}} ABS Absolute
124+
# UNNAMED-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
125+
# UNNAMED-SEC-SYMS-NEXT: 3 {{.*}} ABS <?>
124126
# UNNAMED-SEC-SYMS-NEXT: 2 {{.*}} 1 .hash
125127
# UNNAMED-SEC-SYMS-NEXT: 4 {{.*}} 2 .gnu.hash
126128
# UNNAMED-SEC-SYMS-EMPTY:
127129
# UNNAMED-SEC-SYMS: Symbol table of .gnu.hash for image:
128130
# UNNAMED-SEC-SYMS-NEXT: Num {{.*}} Ndx Name
129131
# UNNAMED-SEC-SYMS-NEXT: 2 {{.*}} 1 .hash
130-
# UNNAMED-SEC-SYMS-NEXT: 3 {{.*}} ABS Absolute
132+
# UNNAMED-SEC-SYMS-NEXT: 3 {{.*}} ABS <?>
131133
# UNNAMED-SEC-SYMS-NEXT: 4 {{.*}} 2 .gnu.hash
132134
# UNNAMED-SEC-SYMS-NEXT: 5 {{.*}} 1 .hash
133135

llvm/test/tools/llvm-readobj/ELF/mips-got.test

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -653,31 +653,34 @@ DynamicSymbols:
653653
- Name: foo
654654

655655
## Check how we print global GOT entries when they are unnamed section symbols.
656-
## TODO: we should print proper symbol names instead of descriptions.
657656
# RUN: yaml2obj --docnum=5 %s -o %t.err8.o
658657
# RUN: llvm-readobj -A %t.err8.o 2>&1 | FileCheck %s -DFILE=%t.err8.o --check-prefix=SEC-SYMS-LLVM
659658
# RUN: llvm-readelf -A %t.err8.o 2>&1 | FileCheck %s -DFILE=%t.err8.o --check-prefix=SEC-SYMS-GNU
660659

661660
# SEC-SYMS-LLVM: Global entries [
662661
# SEC-SYMS-LLVM-NEXT: Entry {
663662
# SEC-SYMS-LLVM: Section: Absolute (0xFFF1)
664-
# SEC-SYMS-LLVM-NEXT: Name: Absolute (0)
663+
# SEC-SYMS-LLVM-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
664+
# SEC-SYMS-LLVM-NEXT: Name: <?> (0)
665665
# SEC-SYMS-LLVM-NEXT: }
666666
# SEC-SYMS-LLVM-NEXT: Entry {
667667
# SEC-SYMS-LLVM: Section: .got (0x1)
668668
# SEC-SYMS-LLVM-NEXT: Name: .got (0)
669669
# SEC-SYMS-LLVM-NEXT: }
670670
# SEC-SYMS-LLVM-NEXT: Entry {
671671
# SEC-SYMS-LLVM: Section: Common (0xFFF2)
672-
# SEC-SYMS-LLVM-NEXT: Name: Common (0)
672+
# SEC-SYMS-LLVM-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
673+
# SEC-SYMS-LLVM-NEXT: Name: <?> (0)
673674
# SEC-SYMS-LLVM-NEXT: }
674675
# SEC-SYMS-LLVM-NEXT: ]
675676

676677
# SEC-SYMS-GNU: Global entries:
677678
# SEC-SYMS-GNU-NEXT: {{.*}} Ndx Name
678-
# SEC-SYMS-GNU-NEXT: {{.*}} ABS Absolute
679+
# SEC-SYMS-GNU-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
680+
# SEC-SYMS-GNU-NEXT: {{.*}} ABS <?>
679681
# SEC-SYMS-GNU-NEXT: {{.*}} 1 .got
680-
# SEC-SYMS-GNU-NEXT: {{.*}} COM Common
682+
# SEC-SYMS-GNU-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
683+
# SEC-SYMS-GNU-NEXT: {{.*}} COM <?>
681684

682685
--- !ELF
683686
FileHeader:

llvm/test/tools/llvm-readobj/ELF/mips-plt.test

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,34 +142,37 @@ DynamicSymbols: []
142142
# 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
143143

144144
## Check how we print PLT entries when they are unnamed section symbols.
145-
## TODO: we should print proper symbol names instead of descriptions.
146-
# RUN: yaml2obj --docnum=3 %s -o %t.3
147-
# RUN: llvm-readobj -A %t.3 2>&1 | FileCheck %s -DFILE=%t.err8.o --check-prefix=SEC-SYMS-LLVM
148-
# RUN: llvm-readelf -A %t.3 2>&1 | FileCheck %s -DFILE=%t.err8.o --check-prefix=SEC-SYMS-GNU
145+
# RUN: yaml2obj --docnum=3 %s -o %t3
146+
# RUN: llvm-readobj -A %t3 2>&1 | FileCheck %s -DFILE=%t3 --check-prefix=SEC-SYMS-LLVM
147+
# RUN: llvm-readelf -A %t3 2>&1 | FileCheck %s -DFILE=%t3 --check-prefix=SEC-SYMS-GNU
149148

150149
# SEC-SYMS-LLVM: PLT GOT {
151150
# SEC-SYMS-LLVM: Entries [
152151
# SEC-SYMS-LLVM: Entry {
153152
# SEC-SYMS-LLVM: Section: Absolute (0xFFF1)
154-
# SEC-SYMS-LLVM-NEXT: Name: Absolute (0)
153+
# SEC-SYMS-LLVM-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
154+
# SEC-SYMS-LLVM-NEXT: Name: <?> (0)
155155
# SEC-SYMS-LLVM-NEXT: }
156156
# SEC-SYMS-LLVM-NEXT: Entry {
157157
# SEC-SYMS-LLVM: Section: .got.plt (0x2)
158158
# SEC-SYMS-LLVM-NEXT: Name: .got.plt (0)
159159
# SEC-SYMS-LLVM-NEXT: }
160160
# SEC-SYMS-LLVM-NEXT: Entry {
161161
# SEC-SYMS-LLVM: Section: Common (0xFFF2)
162-
# SEC-SYMS-LLVM-NEXT: Name: Common (0)
162+
# SEC-SYMS-LLVM-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
163+
# SEC-SYMS-LLVM-NEXT: Name: <?> (0)
163164
# SEC-SYMS-LLVM-NEXT: }
164165
# SEC-SYMS-LLVM-NEXT: ]
165166
# SEC-SYMS-LLVM-NEXT: }
166167

167168
# SEC-SYMS-GNU: PLT GOT:
168169
# SEC-SYMS-GNU: Entries:
169170
# SEC-SYMS-GNU-NEXT: Address {{.*}} Ndx Name
170-
# SEC-SYMS-GNU-NEXT: 0000000000002010 {{.*}} ABS Absolute
171+
# SEC-SYMS-GNU-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
172+
# SEC-SYMS-GNU-NEXT: 0000000000002010 {{.*}} ABS <?>
171173
# SEC-SYMS-GNU-NEXT: 0000000000002018 {{.*}} 2 .got.plt
172-
# SEC-SYMS-GNU-NEXT: 0000000000002020 {{.*}} COM Common
174+
# SEC-SYMS-GNU-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
175+
# SEC-SYMS-GNU-NEXT: 0000000000002020 {{.*}} COM <?>
173176

174177
--- !ELF
175178
FileHeader:

llvm/test/tools/llvm-readobj/ELF/symbol-shndx.test

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,49 +95,62 @@ Symbols:
9595
# RUN: llvm-readelf --symbols %t1-sec | FileCheck %s --check-prefix=GNU1
9696

9797
## Check the behavior for unnamed section symbols.
98-
## TODO: we should print proper symbol names instead of descriptions.
9998
# RUN: yaml2obj --docnum=1 -DTYPE=STT_SECTION -DNAME="''" %s -o %t1-sec-unnamed
100-
# RUN: llvm-readobj --symbols %t1-sec-unnamed | FileCheck %s --check-prefix=LLVM1-SEC-SYMS
101-
# RUN: llvm-readelf --symbols %t1-sec-unnamed | FileCheck %s --check-prefix=GNU1-SEC-SYMS
99+
# RUN: llvm-readobj --symbols %t1-sec-unnamed 2>&1 | \
100+
# RUN: FileCheck %s -DFILE=%t1-sec-unnamed --check-prefix=LLVM1-SEC-SYMS
101+
# RUN: llvm-readelf --symbols %t1-sec-unnamed 2>&1 | \
102+
# RUN: FileCheck %s -DFILE=%t1-sec-unnamed --check-prefix=GNU1-SEC-SYMS
102103

103104
# LLVM1-SEC-SYMS: Symbols [
104105
# LLVM1-SEC-SYMS-NEXT: Symbol {
105106
# LLVM1-SEC-SYMS-NEXT: Name: (0)
106107
# LLVM1-SEC-SYMS: Section: Undefined (0x0)
107-
# LLVM1-SEC-SYMS: Symbol {
108-
# LLVM1-SEC-SYMS-NEXT: Name: Undefined (0)
108+
# LLVM1-SEC-SYMS: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0x0 (SHN_UNDEF)
109+
# LLVM1-SEC-SYMS-NEXT: Symbol {
110+
# LLVM1-SEC-SYMS-NEXT: Name: <?> (0)
109111
# LLVM1-SEC-SYMS: Section: Undefined (0x0)
110112
# LLVM1-SEC-SYMS: Symbol {
111113
# LLVM1-SEC-SYMS-NEXT: Name: .text (0)
112114
# LLVM1-SEC-SYMS: Section: .text (0x1)
113-
# LLVM1-SEC-SYMS: Symbol {
114-
# LLVM1-SEC-SYMS-NEXT: Name: Common (0)
115+
# LLVM1-SEC-SYMS: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
116+
# LLVM1-SEC-SYMS-NEXT: Symbol {
117+
# LLVM1-SEC-SYMS-NEXT: Name: <?> (0)
115118
# LLVM1-SEC-SYMS: Section: Common (0xFFF2)
116-
# LLVM1-SEC-SYMS: Symbol {
117-
# LLVM1-SEC-SYMS-NEXT: Name: Absolute (0)
119+
# LLVM1-SEC-SYMS: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
120+
# LLVM1-SEC-SYMS-NEXT: Symbol {
121+
# LLVM1-SEC-SYMS-NEXT: Name: <?> (0)
118122
# LLVM1-SEC-SYMS: Section: Absolute (0xFFF1)
119-
# LLVM1-SEC-SYMS: Symbol {
120-
# LLVM1-SEC-SYMS-NEXT: Name: Processor Specific (0)
123+
# LLVM1-SEC-SYMS: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xff01 (SHN_LOPROC+0x1)
124+
# LLVM1-SEC-SYMS-NEXT: Symbol {
125+
# LLVM1-SEC-SYMS-NEXT: Name: <?> (0)
121126
# LLVM1-SEC-SYMS: Section: Processor Specific (0xFF01)
122-
# LLVM1-SEC-SYMS: Symbol {
123-
# LLVM1-SEC-SYMS-NEXT: Name: Operating System Specific (0)
127+
# LLVM1-SEC-SYMS: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xff21 (SHN_LOOS+0x1)
128+
# LLVM1-SEC-SYMS-NEXT: Symbol {
129+
# LLVM1-SEC-SYMS-NEXT: Name: <?> (0)
124130
# LLVM1-SEC-SYMS: Section: Operating System Specific (0xFF21)
125-
# LLVM1-SEC-SYMS: Symbol {
126-
# LLVM1-SEC-SYMS-NEXT: Name: Reserved (0)
131+
# LLVM1-SEC-SYMS: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfffe (SHN_LORESERVE+0xfe)
132+
# LLVM1-SEC-SYMS-NEXT: Symbol {
133+
# LLVM1-SEC-SYMS-NEXT: Name: <?> (0)
127134
# LLVM1-SEC-SYMS: Section: Reserved (0xFFFE)
128135
# LLVM1-SEC-SYMS: Symbol {
129136
# LLVM1-SEC-SYMS-NEXT: Name: .text (0)
130137
# LLVM1-SEC-SYMS: Section: .text (0x1)
131138

132139
# GNU1-SEC-SYMS: Num: {{.*}} Ndx Name
133-
# GNU1-SEC-SYMS-NEXT: 0: {{.*}} UND
134-
# GNU1-SEC-SYMS-NEXT: 1: {{.*}} UND Undefined
140+
# GNU1-SEC-SYMS-NEXT: 0: {{.*}} UND {{$}}
141+
# GNU1-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0x0 (SHN_UNDEF)
142+
# GNU1-SEC-SYMS-NEXT: 1: {{.*}} UND <?>
135143
# GNU1-SEC-SYMS-NEXT: 2: {{.*}} 1 .text
136-
# GNU1-SEC-SYMS-NEXT: 3: {{.*}} COM Common
137-
# GNU1-SEC-SYMS-NEXT: 4: {{.*}} ABS Absolute
138-
# GNU1-SEC-SYMS-NEXT: 5: {{.*}} PRC[0xff01] Processor Specific
139-
# GNU1-SEC-SYMS-NEXT: 6: {{.*}} OS[0xff21] Operating System Specific
140-
# GNU1-SEC-SYMS-NEXT: 7: {{.*}} RSV[0xfffe] Reserved
144+
# GNU1-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
145+
# GNU1-SEC-SYMS-NEXT: 3: {{.*}} COM <?>
146+
# GNU1-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
147+
# GNU1-SEC-SYMS-NEXT: 4: {{.*}} ABS <?>
148+
# GNU1-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xff01 (SHN_LOPROC+0x1)
149+
# GNU1-SEC-SYMS-NEXT: 5: {{.*}} PRC[0xff01] <?>
150+
# GNU1-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xff21 (SHN_LOOS+0x1)
151+
# GNU1-SEC-SYMS-NEXT: 6: {{.*}} OS[0xff21] <?>
152+
# GNU1-SEC-SYMS-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfffe (SHN_LORESERVE+0xfe)
153+
# GNU1-SEC-SYMS-NEXT: 7: {{.*}} RSV[0xfffe] <?>
141154
# GNU1-SEC-SYMS-NEXT: 8: {{.*}} 1 .text
142155

143156
## In this case, the index does not correspond to a real section. Check that GNU

llvm/tools/llvm-readobj/ELFDumper.cpp

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,37 +1225,43 @@ template <typename ELFT>
12251225
Expected<unsigned>
12261226
ELFDumper<ELFT>::getSymbolSectionIndex(const Elf_Sym *Symbol,
12271227
const Elf_Sym *FirstSym) const {
1228-
return Symbol->st_shndx == SHN_XINDEX
1229-
? object::getExtendedSymbolTableIndex<ELFT>(*Symbol, *FirstSym,
1230-
ShndxTable)
1231-
: Symbol->st_shndx;
1228+
unsigned Ndx = Symbol->st_shndx;
1229+
if (Ndx == SHN_XINDEX)
1230+
return object::getExtendedSymbolTableIndex<ELFT>(*Symbol, *FirstSym,
1231+
ShndxTable);
1232+
if (Ndx != SHN_UNDEF && Ndx < SHN_LORESERVE)
1233+
return Ndx;
1234+
1235+
auto CreateErr = [&](const Twine &Name, Optional<unsigned> Offset = None) {
1236+
std::string Desc;
1237+
if (Offset)
1238+
Desc = (Name + "+0x" + Twine::utohexstr(*Offset)).str();
1239+
else
1240+
Desc = Name.str();
1241+
return createError(
1242+
"unable to get section index for symbol with st_shndx = 0x" +
1243+
Twine::utohexstr(Ndx) + " (" + Desc + ")");
1244+
};
1245+
1246+
if (Ndx >= ELF::SHN_LOPROC && Ndx <= ELF::SHN_HIPROC)
1247+
return CreateErr("SHN_LOPROC", Ndx - ELF::SHN_LOPROC);
1248+
if (Ndx >= ELF::SHN_LOOS && Ndx <= ELF::SHN_HIOS)
1249+
return CreateErr("SHN_LOOS", Ndx - ELF::SHN_LOOS);
1250+
if (Ndx == ELF::SHN_UNDEF)
1251+
return CreateErr("SHN_UNDEF");
1252+
if (Ndx == ELF::SHN_ABS)
1253+
return CreateErr("SHN_ABS");
1254+
if (Ndx == ELF::SHN_COMMON)
1255+
return CreateErr("SHN_COMMON");
1256+
return CreateErr("SHN_LORESERVE", Ndx - SHN_LORESERVE);
12321257
}
12331258

1234-
// If the Symbol has a reserved st_shndx other than SHN_XINDEX, return a
1235-
// descriptive interpretation of the st_shndx value. Otherwise, return the name
1236-
// of the section with index SectionIndex. This function assumes that if the
1237-
// Symbol has st_shndx == SHN_XINDEX the SectionIndex will be the value derived
1238-
// from the SHT_SYMTAB_SHNDX section.
12391259
template <typename ELFT>
12401260
Expected<StringRef>
12411261
ELFDumper<ELFT>::getSymbolSectionName(const Elf_Sym *Symbol,
12421262
unsigned SectionIndex) const {
1243-
if (Symbol->isUndefined())
1244-
return "Undefined";
1245-
if (Symbol->isProcessorSpecific())
1246-
return "Processor Specific";
1247-
if (Symbol->isOSSpecific())
1248-
return "Operating System Specific";
1249-
if (Symbol->isAbsolute())
1250-
return "Absolute";
1251-
if (Symbol->isCommon())
1252-
return "Common";
1253-
if (Symbol->isReserved() && Symbol->st_shndx != SHN_XINDEX)
1254-
return "Reserved";
1255-
12561263
const ELFFile<ELFT> *Obj = ObjF->getELFFile();
1257-
Expected<const Elf_Shdr *> SecOrErr =
1258-
Obj->getSection(SectionIndex);
1264+
Expected<const Elf_Shdr *> SecOrErr = Obj->getSection(SectionIndex);
12591265
if (!SecOrErr)
12601266
return SecOrErr.takeError();
12611267
return Obj->getSectionName(**SecOrErr);
@@ -3933,7 +3939,7 @@ std::string GNUStyle<ELFT>::getSymbolSectionNdx(const Elf_Sym *Symbol,
39333939
*Symbol, *FirstSym, this->dumper()->getShndxTable());
39343940
if (!IndexOrErr) {
39353941
assert(Symbol->st_shndx == SHN_XINDEX &&
3936-
"getSymbolSectionIndex should only fail due to an invalid "
3942+
"getExtendedSymbolTableIndex should only fail due to an invalid "
39373943
"SHT_SYMTAB_SHNDX table/reference");
39383944
this->reportUniqueWarning(IndexOrErr.takeError());
39393945
return "RSV[0xffff]";
@@ -6168,6 +6174,27 @@ template <class ELFT> void LLVMStyle<ELFT>::printSectionHeaders() {
61686174
template <class ELFT>
61696175
void LLVMStyle<ELFT>::printSymbolSection(const Elf_Sym *Symbol,
61706176
const Elf_Sym *First) {
6177+
auto GetSectionSpecialType = [&]() -> Optional<StringRef> {
6178+
if (Symbol->isUndefined())
6179+
return StringRef("Undefined");
6180+
if (Symbol->isProcessorSpecific())
6181+
return StringRef("Processor Specific");
6182+
if (Symbol->isOSSpecific())
6183+
return StringRef("Operating System Specific");
6184+
if (Symbol->isAbsolute())
6185+
return StringRef("Absolute");
6186+
if (Symbol->isCommon())
6187+
return StringRef("Common");
6188+
if (Symbol->isReserved() && Symbol->st_shndx != SHN_XINDEX)
6189+
return StringRef("Reserved");
6190+
return None;
6191+
};
6192+
6193+
if (Optional<StringRef> Type = GetSectionSpecialType()) {
6194+
W.printHex("Section", *Type, Symbol->st_shndx);
6195+
return;
6196+
}
6197+
61716198
Expected<unsigned> SectionIndex =
61726199
this->dumper()->getSymbolSectionIndex(Symbol, First);
61736200
if (!SectionIndex) {

0 commit comments

Comments
 (0)