Skip to content

Commit f6a56ae

Browse files
committed
[llvm-readelf] Add --extra-sym-info
GNU readelf introduced --extra-sym-info/-X to display the section name for --syms (https://sourceware.org/PR30684). Port the feature, which is currently llvm-readelf only. For STO_AARCH64_VARIANT_PCS/STO_RISCV_VARIANT_PCS, the Ndx and Name columns may not be aligned.
1 parent 65a15a5 commit f6a56ae

13 files changed

+197
-61
lines changed

llvm/docs/CommandGuide/llvm-readelf.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ OPTIONS
7777
``GNU`` (the default) output mimics the equivalent GNU :program:`readelf`
7878
output. ``JSON`` is JSON formatted output intended for machine consumption.
7979

80+
.. option:: --extra-sym-info
81+
82+
Display extra information (section name) when showing symbols.
83+
8084
.. option:: --section-groups, -g
8185

8286
Display section groups.

llvm/test/tools/llvm-readobj/ELF/aarch64-symbols-stother.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# RUN: llvm-readobj --symbols %t.o | FileCheck %s --check-prefix=LLVM
55
# RUN: llvm-readobj --symbols %t.o --elf-output-style=JSON --pretty-print | FileCheck %s --check-prefix=JSON
66
# RUN: llvm-readelf --symbols %t.o | FileCheck %s --check-prefix=GNU
7+
# RUN: llvm-readelf --symbols --extra-sym-info %t.o | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=GNUX
78

89
# LLVM: Name: foo1
910
# LLVM: Other [ (0x80)
@@ -29,6 +30,14 @@
2930
# GNU-NEXT: 3: 0000000000000000 0 NOTYPE LOCAL PROTECTED [VARIANT_PCS] UND foo3
3031
# GNU-NEXT: 4: 0000000000000000 0 NOTYPE LOCAL PROTECTED UND foo4
3132

33+
# GNUX:Symbol table '.symtab' contains 5 entries:
34+
# GNUX-NEXT: Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]
35+
# GNUX-NEXT: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
36+
# GNUX-NEXT: 1: 0000000000000000 0 NOTYPE LOCAL DEFAULT [VARIANT_PCS] UND foo1
37+
# GNUX-NEXT: 2: 0000000000000000 0 NOTYPE LOCAL DEFAULT [VARIANT_PCS | 40] UND foo2
38+
# GNUX-NEXT: 3: 0000000000000000 0 NOTYPE LOCAL PROTECTED [VARIANT_PCS] UND foo3
39+
# GNUX-NEXT: 4: 0000000000000000 0 NOTYPE LOCAL PROTECTED UND foo4
40+
3241
# JSON: "Name": "foo1",
3342
# JSON: "Other": {
3443
# JSON-NEXT: "Value": 128,

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# RUN: FileCheck %s -DFILE=%t1 --check-prefix=LLVM1 --implicit-check-not="warning:"
88
# RUN: llvm-readelf %t1 --symbols --relocations 2>&1 | \
99
# RUN: FileCheck %s -DFILE=%t1 --check-prefix=GNU1 --implicit-check-not="warning:"
10+
# RUN: llvm-readelf %t1 --symbols -X 2>&1 | \
11+
# RUN: FileCheck %s -DFILE=%t1 --check-prefix=GNUX1 --implicit-check-not="warning:"
1012

1113
# LLVM1: Relocations [
1214
# LLVM1-NEXT: Section (5) .rela.foo {
@@ -62,6 +64,22 @@
6264
# GNU1-NEXT: warning: '[[FILE]]': a section [index 3] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table
6365
# GNU1-NEXT: 9: {{.*}} SECTION {{.*}} 3 <section 3>
6466

67+
# GNUX1: Symbol table '.symtab' contains 10 entries:
68+
# GNUX1-NEXT: Num: {{.*}} Type {{.*}} Ndx(SecName) Name
69+
# GNUX1-NEXT: 0: {{.*}} NOTYPE {{.*}} UND {{$}}
70+
# GNUX1-NEXT: 1: {{.*}} SECTION {{.*}} 1 (.foo) .foo
71+
# GNUX1-NEXT: 2: {{.*}} SECTION {{.*}} 1 (.foo) symbol1
72+
# GNUX1-NEXT: warning: '[[FILE]]': invalid section index: 67
73+
# GNUX1-NEXT: 3: {{.*}} SECTION {{.*}} 67 <section 67>
74+
# GNUX1-NEXT: 4: {{.*}} SECTION {{.*}} 67 symbol2
75+
# GNUX1-NEXT: 5: {{.*}} SECTION {{.*}} 2 (.bar) .bar
76+
# GNUX1-NEXT: 6: {{.*}} SECTION {{.*}} 2 (.bar) symbol3
77+
# GNUX1-NEXT: warning: '[[FILE]]': invalid section index: 66
78+
# GNUX1-NEXT: 7: {{.*}} SECTION {{.*}} 66 <section 66>
79+
# GNUX1-NEXT: 8: {{.*}} SECTION {{.*}} 66 symbol4
80+
# GNUX1-NEXT: warning: '[[FILE]]': a section [index 3] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table
81+
# GNUX1-NEXT: 9: {{.*}} SECTION {{.*}} 3 <section 3>
82+
6583
--- !ELF
6684
FileHeader:
6785
Class: ELFCLASS32

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

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
# RUN: yaml2obj %s -DBITS=64 -DTYPE=ET_REL -o %t64
1414
# RUN: llvm-readobj --symbols %t64 | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-LLVM
1515
# RUN: llvm-readelf --symbols %t64 | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-GNU64
16+
# RUN: llvm-readelf -s --extra-sym-info %t64 | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-GNU64X
1617
# RUN: yaml2obj %s -DBITS=32 -DTYPE=ET_REL -o %t32
1718
# RUN: llvm-readobj --symbols %t32 | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-LLVM
1819
# RUN: llvm-readelf --symbols %t32 | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-GNU32
20+
# RUN: llvm-readelf -s -X %t32 | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-GNU32X
1921
## b) Check dynamic objects.
2022
# RUN: yaml2obj %s -DBITS=64 -DTYPE=ET_DYN -o %t64.so
21-
# RUN: llvm-readobj --symbols %t64.so | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-LLVM
23+
# RUN: llvm-readobj --symbols %t64.so > %t64llvm.txt
24+
# RUN: FileCheck --input-file=%t64llvm.txt %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-LLVM
2225
# RUN: llvm-readelf --symbols %t64.so | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-GNU64
26+
## --extra-sym-info is a no-op for llvm-readobj.
27+
# RUN: llvm-readobj --symbols --extra-sym-info %t64.so | diff %t64llvm.txt -
2328
# RUN: yaml2obj %s -DBITS=32 -DTYPE=ET_DYN -o %t32.so
2429
# RUN: llvm-readobj --symbols %t32.so | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-LLVM
2530
# RUN: llvm-readelf --symbols %t32.so | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-GNU32
@@ -41,7 +46,7 @@
4146
# SYMBOLS-LLVM-NEXT: Binding: Local (0x0)
4247
# SYMBOLS-LLVM-NEXT: Type: None (0x0)
4348
# SYMBOLS-LLVM-NEXT: Other: 0
44-
# SYMBOLS-LLVM-NEXT: Section: Undefined (0x0)
49+
# SYMBOLS-LLVM-NEXT: Section: .text (0x1)
4550
# SYMBOLS-LLVM-NEXT: }
4651
# SYMBOLS-LLVM-NEXT: Symbol {
4752
# SYMBOLS-LLVM-NEXT: Name: bar (1)
@@ -52,19 +57,44 @@
5257
# SYMBOLS-LLVM-NEXT: Other: 0
5358
# SYMBOLS-LLVM-NEXT: Section: Undefined (0x0)
5459
# SYMBOLS-LLVM-NEXT: }
60+
# SYMBOLS-LLVM-NEXT: Symbol {
61+
# SYMBOLS-LLVM-NEXT: Name: data (9)
62+
# SYMBOLS-LLVM-NEXT: Value: 0x3
63+
# SYMBOLS-LLVM-NEXT: Size: 0
64+
# SYMBOLS-LLVM-NEXT: Binding: Global (0x1)
65+
# SYMBOLS-LLVM-NEXT: Type: None (0x0)
66+
# SYMBOLS-LLVM-NEXT: Other: 0
67+
# SYMBOLS-LLVM-NEXT: Section: .data (0x2)
68+
# SYMBOLS-LLVM-NEXT: }
5569
# SYMBOLS-LLVM-NEXT:]
5670

57-
# SYMBOLS-GNU64:Symbol table '.symtab' contains 3 entries:
71+
# SYMBOLS-GNU64:Symbol table '.symtab' contains 4 entries:
5872
# SYMBOLS-GNU64-NEXT: Num: Value Size Type Bind Vis Ndx Name
5973
# SYMBOLS-GNU64-NEXT: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
60-
# SYMBOLS-GNU64-NEXT: 1: 0000000000000001 0 NOTYPE LOCAL DEFAULT UND foo
74+
# SYMBOLS-GNU64-NEXT: 1: 0000000000000001 0 NOTYPE LOCAL DEFAULT 1 foo
6175
# SYMBOLS-GNU64-NEXT: 2: 0000000000000002 0 NOTYPE LOCAL DEFAULT UND bar
76+
# SYMBOLS-GNU64-NEXT: 3: 0000000000000003 0 NOTYPE GLOBAL DEFAULT 2 data
77+
78+
# SYMBOLS-GNU64X:Symbol table '.symtab' contains 4 entries:
79+
#SYMBOLS-GNU64X-NEXT: Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]
80+
#SYMBOLS-GNU64X-NEXT: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
81+
#SYMBOLS-GNU64X-NEXT: 1: 0000000000000001 0 NOTYPE LOCAL DEFAULT 1 (.text) foo
82+
#SYMBOLS-GNU64X-NEXT: 2: 0000000000000002 0 NOTYPE LOCAL DEFAULT UND bar
83+
#SYMBOLS-GNU64X-NEXT: 3: 0000000000000003 0 NOTYPE GLOBAL DEFAULT 2 (.data) data
6284

63-
# SYMBOLS-GNU32:Symbol table '.symtab' contains 3 entries:
85+
# SYMBOLS-GNU32:Symbol table '.symtab' contains 4 entries:
6486
# SYMBOLS-GNU32-NEXT: Num: Value Size Type Bind Vis Ndx Name
6587
# SYMBOLS-GNU32-NEXT: 0: 00000000 0 NOTYPE LOCAL DEFAULT UND
66-
# SYMBOLS-GNU32-NEXT: 1: 00000001 0 NOTYPE LOCAL DEFAULT UND foo
88+
# SYMBOLS-GNU32-NEXT: 1: 00000001 0 NOTYPE LOCAL DEFAULT 1 foo
6789
# SYMBOLS-GNU32-NEXT: 2: 00000002 0 NOTYPE LOCAL DEFAULT UND bar
90+
# SYMBOLS-GNU32-NEXT: 3: 00000003 0 NOTYPE GLOBAL DEFAULT 2 data
91+
92+
# SYMBOLS-GNU32X:Symbol table '.symtab' contains 4 entries:
93+
#SYMBOLS-GNU32X-NEXT: Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]
94+
#SYMBOLS-GNU32X-NEXT: 0: 00000000 0 NOTYPE LOCAL DEFAULT UND
95+
#SYMBOLS-GNU32X-NEXT: 1: 00000001 0 NOTYPE LOCAL DEFAULT 1 (.text) foo
96+
#SYMBOLS-GNU32X-NEXT: 2: 00000002 0 NOTYPE LOCAL DEFAULT UND bar
97+
#SYMBOLS-GNU32X-NEXT: 3: 00000003 0 NOTYPE GLOBAL DEFAULT 2 (.data) data
6898

6999
## Case 2: Check flag aliases produce identical output
70100
# RUN: llvm-readobj --symbols %t64 > %t.symbols
@@ -100,10 +130,10 @@
100130
## Case 6: Test that the Num index starts from zero at every new symbol table.
101131
# RUN: llvm-readelf --symbols %t64 %t64 | FileCheck %s --check-prefix=NUM-INDEX
102132

103-
# NUM-INDEX: Symbol table '.symtab' contains 3 entries:
133+
# NUM-INDEX: Symbol table '.symtab' contains 4 entries:
104134
# NUM-INDEX-NEXT: Num: {{.*}}
105135
# NUM-INDEX-NEXT: 0: {{.*}}
106-
# NUM-INDEX: Symbol table '.symtab' contains 3 entries:
136+
# NUM-INDEX: Symbol table '.symtab' contains 4 entries:
107137
# NUM-INDEX-NEXT: Num: {{.*}}
108138
# NUM-INDEX-NEXT: 0: {{.*}}
109139

@@ -112,11 +142,21 @@ FileHeader:
112142
Class: ELFCLASS[[BITS]]
113143
Data: ELFDATA2LSB
114144
Type: [[TYPE]]
145+
Sections:
146+
- Name: .text
147+
Type: SHT_PROGBITS
148+
- Name: .data
149+
Type: SHT_PROGBITS
115150
Symbols:
116-
- Name: foo
117-
Value: 0x1
118-
- Name: bar
119-
Value: 0x2
151+
- Name: foo
152+
Value: 0x1
153+
Section: .text
154+
- Name: bar
155+
Value: 0x2
156+
- Name: data
157+
Value: 0x3
158+
Binding: STB_GLOBAL
159+
Section: .data
120160
DynamicSymbols:
121161
- Name: zed
122162

@@ -232,6 +272,13 @@ Symbols:
232272
## Test a symbol defined relative to a section with an invalid name.
233273
# RUN: yaml2obj --docnum=3 %s -o %t64.err4
234274
# RUN: llvm-readelf -s %t64.err4 2>&1 | FileCheck /dev/null --implicit-check-not=warning:
275+
# RUN: llvm-readelf -s -X %t64.err4 2>&1 | \
276+
# RUN: FileCheck %s -DFILE=%t64.err4 --check-prefix=INVALID-SECNAME-GNU --implicit-check-not=warning:
277+
278+
# INVALID-SECNAME-GNU: Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]
279+
# INVALID-SECNAME-GNU-NEXT: 0: 00000000 0 NOTYPE LOCAL DEFAULT UND
280+
# INVALID-SECNAME-GNU-NEXT: warning: '[[FILE]]': a section [index 1] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table
281+
# INVALID-SECNAME-GNU-NEXT: 1: 00000000 0 NOTYPE LOCAL DEFAULT 1 foo
235282

236283
--- !ELF
237284
FileHeader:

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
# RUN: yaml2obj --docnum=1 %s -o %t1
99
# RUN: llvm-readelf --symbols --dyn-syms %t1 2>&1 | FileCheck %s --check-prefix=GNU
10+
# RUN: llvm-readelf --symbols --dyn-syms --extra-sym-info %t1 2>&1 | FileCheck %s --check-prefix=GNUX
1011
# RUN: llvm-readobj --symbols --dyn-syms %t1 2>&1 | FileCheck %s --check-prefix=LLVM
1112

1213
# GNU: Symbol table '.dynsym' contains 3 entries:
@@ -20,6 +21,17 @@
2021
# GNU-NEXT: 1: 00000000 0 NOTYPE LOCAL DEFAULT 2 sym1
2122
# GNU-NEXT: 2: 00000000 0 NOTYPE LOCAL DEFAULT 1 sym2
2223

24+
# GNUX: Symbol table '.dynsym' contains 3 entries:
25+
# GNUX-NEXT: Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]
26+
# GNUX-NEXT: 0: 00000000 0 NOTYPE LOCAL DEFAULT UND
27+
# GNUX-NEXT: 1: 00000000 0 NOTYPE LOCAL DEFAULT 3 (.section3) dynsym1
28+
# GNUX-NEXT: 2: 00000000 0 NOTYPE LOCAL DEFAULT 2 (.section2) dynsym2
29+
# GNUX: Symbol table '.symtab' contains 3 entries:
30+
# GNUX-NEXT: Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]
31+
# GNUX-NEXT: 0: 00000000 0 NOTYPE LOCAL DEFAULT UND
32+
# GNUX-NEXT: 1: 00000000 0 NOTYPE LOCAL DEFAULT 2 (.section2) sym1
33+
# GNUX-NEXT: 2: 00000000 0 NOTYPE LOCAL DEFAULT 1 (.section1) sym2
34+
2335
# LLVM: Symbols [
2436
# LLVM-NEXT: Symbol {
2537
# LLVM-NEXT: Name: (0)

llvm/tools/llvm-readobj/COFFDumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class COFFDumper : public ObjDumper {
110110

111111
private:
112112
StringRef getSymbolName(uint32_t Index);
113-
void printSymbols() override;
113+
void printSymbols(bool ExtraSymInfo) override;
114114
void printDynamicSymbols() override;
115115
void printSymbol(const SymbolRef &Sym);
116116
void printRelocation(const SectionRef &Section, const RelocationRef &Reloc,
@@ -1609,7 +1609,7 @@ void COFFDumper::printRelocation(const SectionRef &Section,
16091609
}
16101610
}
16111611

1612-
void COFFDumper::printSymbols() {
1612+
void COFFDumper::printSymbols(bool /*ExtraSymInfo*/) {
16131613
ListScope Group(W, "Symbols");
16141614

16151615
for (const SymbolRef &Symbol : Obj->symbols())

0 commit comments

Comments
 (0)