1## Show that llvm-readobj prints symbol shndxes correctly, for valid indexes,
2## invalid indexes (i.e. section indexes that don't correspond to a real
3## section), reserved values and processor/os-specific index values, for both GNU
4## and LLVM styles.
5
6# RUN: yaml2obj --docnum=1 %s -o %t1
7# RUN: llvm-readobj --symbols %t1 | FileCheck %s --check-prefix=LLVM1
8# RUN: llvm-readelf --symbols %t1 | FileCheck %s --check-prefix=GNU1
9
10# LLVM1: Name:    undef
11# LLVM1: Section:
12# LLVM1-SAME:     Undefined (0x0)
13# LLVM1: Name:    normal
14# LLVM1: Section:
15# LLVM1-SAME:     .text (0x1)
16# LLVM1: Name:    common
17# LLVM1: Section:
18# LLVM1-SAME:     Common (0xFFF2)
19# LLVM1: Name:    absolute
20# LLVM1: Section:
21# LLVM1-SAME:     Absolute (0xFFF1)
22# LLVM1: Name:    proc
23# LLVM1: Section:
24# LLVM1-SAME:     Processor Specific (0xFF01)
25# LLVM1: Name:    os
26# LLVM1: Section:
27# LLVM1-SAME:     Operating System Specific (0xFF21)
28# LLVM1: Name:    reserved
29# LLVM1: Section:
30# LLVM1-SAME:     Reserved (0xFFFE)
31# LLVM1: Name:    xindex
32# LLVM1: Section:
33# LLVM1:          .text (0x1)
34
35# GNU1:      Symbol table '.symtab' contains 9 entries:
36# GNU1-NEXT:   Num: {{.*}} Ndx Name
37# GNU1-NEXT:     0: {{.*}} UND
38# GNU1-NEXT:     1: {{.*}} UND undef
39# GNU1-NEXT:     2: {{.*}}   1 normal
40# GNU1-NEXT:     3: {{.*}} COM common
41# GNU1-NEXT:     4: {{.*}} ABS absolute
42# GNU1-NEXT:     5: {{.*}} PRC[0xff01] proc
43# GNU1-NEXT:     6: {{.*}} OS[0xff21] os
44# GNU1-NEXT:     7: {{.*}} RSV[0xfffe] reserved
45# GNU1-NEXT:     8: {{.*}}   1 xindex
46
47--- !ELF
48FileHeader:
49  Class: ELFCLASS32
50  Data:  ELFDATA2LSB
51  Type:  ET_REL
52Sections:
53  - Name: .text
54    Type: SHT_PROGBITS
55  - Name: .symtab_shndx
56    Type: SHT_SYMTAB_SHNDX
57    Link: .symtab
58    Entries: [ 0, 0, 0, 0, 0, 0, 0, 0, 1 ]
59Symbols:
60  - Name:    [[NAME=undef]]
61    Binding: STB_GLOBAL
62    Type:    [[TYPE=STT_NOTYPE]]
63  - Name:    [[NAME=normal]]
64    Section: .text
65    Binding: STB_GLOBAL
66    Type:    [[TYPE=STT_NOTYPE]]
67  - Name:    [[NAME=common]]
68    Index:   SHN_COMMON
69    Binding: STB_GLOBAL
70    Type:    [[TYPE=STT_NOTYPE]]
71  - Name:    [[NAME=absolute]]
72    Index:   SHN_ABS
73    Binding: STB_GLOBAL
74    Type:    [[TYPE=STT_NOTYPE]]
75  - Name:    [[NAME=proc]]
76    Index:   0xff01
77    Binding: STB_GLOBAL
78    Type:    [[TYPE=STT_NOTYPE]]
79  - Name:    [[NAME=os]]
80    Index:   0xff21
81    Binding: STB_GLOBAL
82    Type:    [[TYPE=STT_NOTYPE]]
83  - Name:    [[NAME=reserved]]
84    Index:   0xfffe
85    Binding: STB_GLOBAL
86    Type:    [[TYPE=STT_NOTYPE]]
87  - Name:    [[NAME=xindex]]
88    Index:   SHN_XINDEX
89    Binding: STB_GLOBAL
90    Type:    [[TYPE=STT_NOTYPE]]
91
92## Check the behavior for section symbols.
93# RUN: yaml2obj --docnum=1 -DTYPE=STT_SECTION %s -o %t1-sec
94# RUN: llvm-readobj --symbols %t1-sec | FileCheck %s --check-prefix=LLVM1
95# RUN: llvm-readelf --symbols %t1-sec | FileCheck %s --check-prefix=GNU1
96
97## Check the behavior for unnamed section symbols.
98# RUN: yaml2obj --docnum=1 -DTYPE=STT_SECTION -DNAME="''" %s -o %t1-sec-unnamed
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
103
104# LLVM1-SEC-SYMS:      Symbols [
105# LLVM1-SEC-SYMS-NEXT:   Symbol {
106# LLVM1-SEC-SYMS-NEXT:     Name:  (0)
107# LLVM1-SEC-SYMS:          Section: Undefined (0x0)
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)
111# LLVM1-SEC-SYMS:          Section: Undefined (0x0)
112# LLVM1-SEC-SYMS:        Symbol {
113# LLVM1-SEC-SYMS-NEXT:     Name: .text (0)
114# LLVM1-SEC-SYMS:          Section: .text (0x1)
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)
118# LLVM1-SEC-SYMS:          Section: Common (0xFFF2)
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)
122# LLVM1-SEC-SYMS:          Section: Absolute (0xFFF1)
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)
126# LLVM1-SEC-SYMS:          Section: Processor Specific (0xFF01)
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)
130# LLVM1-SEC-SYMS:          Section: Operating System Specific (0xFF21)
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)
134# LLVM1-SEC-SYMS:          Section: Reserved (0xFFFE)
135# LLVM1-SEC-SYMS:        Symbol {
136# LLVM1-SEC-SYMS-NEXT:     Name: .text (0)
137# LLVM1-SEC-SYMS:          Section: .text (0x1)
138
139# GNU1-SEC-SYMS:      Num: {{.*}} Ndx Name
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 <?>
143# GNU1-SEC-SYMS-NEXT:   2: {{.*}}   1 .text
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] <?>
154# GNU1-SEC-SYMS-NEXT:   8: {{.*}}   1 .text
155
156## In this case, the index does not correspond to a real section. Check that GNU
157## style just prints the section index as normal and LLVM style prints a warning
158## (but only once for each warning).
159# RUN: yaml2obj --docnum=2 %s -o %t2
160# RUN: llvm-readobj --symbols %t2 2> %t2.llvm.err | FileCheck %s --check-prefix=LLVM2
161# RUN: FileCheck %s --input-file=%t2.llvm.err --check-prefix=BAD-SHNDX --implicit-check-not=warning
162# RUN: llvm-readelf --symbols %t2 2> %t2.gnu.err | FileCheck %s --check-prefix=GNU2
163# RUN: FileCheck %s --input-file=%t2.gnu.err --allow-empty --implicit-check-not={{.}}
164
165# LLVM2: Name: bad
166# LLVM2: Section:
167# LLVM2-SAME:     <?> (0x42)
168# LLVM2: Name: bad2
169# LLVM2: Section:
170# LLVM2-SAME:     <?> (0x42)
171# LLVM2: Name: bad3
172# LLVM2: Section:
173# LLVM2-SAME:     <?> (0x43)
174# LLVM2: Name: invalid_shndx
175# LLVM2: Section:
176# LLVM2-SAME:     <?> (0x9)
177# LLVM2: Name: invalid_shndx2
178# LLVM2: Section:
179# LLVM2-SAME:     <?> (0x9)
180# LLVM2: Name: invalid_shndx3
181# LLVM2: Section:
182# LLVM2-SAME:     <?> (0xA)
183
184# GNU2:      Symbol table '.symtab' contains 7 entries:
185# GNU2-NEXT:   Num: {{.*}} Ndx Name
186# GNU2-NEXT:     0: {{.*}} UND
187# GNU2-NEXT:     1: {{.*}}  66 bad
188# GNU2-NEXT:     2: {{.*}}  66 bad2
189# GNU2-NEXT:     3: {{.*}}  67 bad3
190# GNU2-NEXT:     4: {{.*}}   9 invalid_shndx
191# GNU2-NEXT:     5: {{.*}}   9 invalid_shndx2
192# GNU2-NEXT:     6: {{.*}}  10 invalid_shndx3
193
194# BAD-SHNDX: warning: '{{.*}}tmp2': invalid section index: 66
195# BAD-SHNDX: warning: '{{.*}}tmp2': invalid section index: 67
196# BAD-SHNDX: warning: '{{.*}}tmp2': invalid section index: 9
197# BAD-SHNDX: warning: '{{.*}}tmp2': invalid section index: 10
198
199--- !ELF
200FileHeader:
201  Class: ELFCLASS32
202  Data:  ELFDATA2LSB
203  Type:  ET_REL
204Sections:
205  - Name: .symtab_shndx
206    Type: SHT_SYMTAB_SHNDX
207    Link: .symtab
208    Entries: [ 0, 0, 0, 0, 9, 9, 10 ]
209Symbols:
210  - Name:    bad
211    Index:   0x42
212  - Name:    bad2
213    Index:   0x42
214  - Name:    bad3
215    Index:   0x43
216  - Name:    invalid_shndx
217    Index:   SHN_XINDEX
218  - Name:    invalid_shndx2
219    Index:   SHN_XINDEX
220  - Name:    invalid_shndx3
221    Index:   SHN_XINDEX
222
223## In this case, the symtab shndx section is missing, so symbols with section
224## indexes of SHN_XINDEX print as Reserved symbols.
225# RUN: yaml2obj --docnum=3 %s -o %t3
226# RUN: llvm-readobj --symbols %t3 2> %t3.llvm.err | FileCheck %s --check-prefix=LLVM3
227# RUN: FileCheck %s --input-file=%t3.llvm.err --check-prefix=NO-SYMTAB-SHNDX --implicit-check-not=warning
228# RUN: llvm-readelf --symbols %t3 2> %t3.gnu.err | FileCheck %s --check-prefix=GNU3
229# RUN: FileCheck %s --input-file=%t3.gnu.err --check-prefix=NO-SYMTAB-SHNDX --implicit-check-not=warning
230
231# LLVM3: Name: no_shndx
232# LLVM3: Section:
233# LLVM3-SAME:     Reserved (0xFFFF)
234# LLVM3: Name: no_shndx2
235# LLVM3: Section:
236# LLVM3-SAME:     Reserved (0xFFFF)
237
238# GNU3:      Symbol table '.symtab' contains 3 entries:
239# GNU3-NEXT:   Num: {{.*}} Ndx Name
240# GNU3-NEXT:     0: {{.*}} UND
241# GNU3-NEXT:     1: {{.*}} RSV[0xffff] no_shndx
242# GNU3-NEXT:     2: {{.*}} RSV[0xffff] no_shndx2
243
244# NO-SYMTAB-SHNDX: warning: '{{.*}}tmp3': extended symbol index (1) is past the end of the SHT_SYMTAB_SHNDX section of size 0
245# NO-SYMTAB-SHNDX: warning: '{{.*}}tmp3': extended symbol index (2) is past the end of the SHT_SYMTAB_SHNDX section of size 0
246
247--- !ELF
248FileHeader:
249  Class: ELFCLASS32
250  Data:  ELFDATA2LSB
251  Type:  ET_REL
252Symbols:
253  - Name:  no_shndx
254    Index: SHN_XINDEX
255  - Name:  no_shndx2
256    Index: SHN_XINDEX
257
258## Check we can dump symbols even when the number of entries in the
259## SHT_SYMTAB_SHNDX section doesn't match the number of symbols in the symbol table.
260
261# RUN: yaml2obj --docnum=4 %s -o %t4
262# RUN: llvm-readelf --symbols %t4 2>&1 | FileCheck %s -DFILE=%t4 --check-prefix=SHNDX-ERR-GNU
263# RUN: llvm-readobj --symbols %t4 2>&1 | FileCheck %s -DFILE=%t4 --check-prefix=SHNDX-ERR-LLVM
264
265#       SHNDX-ERR-GNU: warning: '[[FILE]]': SHT_SYMTAB_SHNDX has 3 entries, but the symbol table associated has 2
266# SHNDX-ERR-GNU-EMPTY:
267#  SHNDX-ERR-GNU-NEXT: Symbol table '.symtab' contains 2 entries:
268#  SHNDX-ERR-GNU-NEXT:    Num:    Value          Size Type    Bind   Vis       Ndx Name
269#  SHNDX-ERR-GNU-NEXT:      0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND
270#  SHNDX-ERR-GNU-NEXT: warning: '[[FILE]]': extended symbol index (1) is past the end of the SHT_SYMTAB_SHNDX section of size 0
271#  SHNDX-ERR-GNU-NEXT:      1: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   RSV[0xffff]
272# SHNDX-ERR-GNU-EMPTY:
273#   SHNDX-ERR-GNU-NOT:{{.}}
274
275# SHNDX-ERR-LLVM: warning: '[[FILE]]': SHT_SYMTAB_SHNDX has 3 entries, but the symbol table associated has 2
276# SHNDX-ERR-LLVM:      Format: elf64-unknown
277# SHNDX-ERR-LLVM-NEXT: Arch: unknown
278# SHNDX-ERR-LLVM-NEXT: AddressSize: 64bit
279# SHNDX-ERR-LLVM-NEXT: LoadName: <Not found>
280# SHNDX-ERR-LLVM-NEXT: Symbols [
281# SHNDX-ERR-LLVM-NEXT:   Symbol {
282# SHNDX-ERR-LLVM-NEXT:     Name:  (0)
283# SHNDX-ERR-LLVM-NEXT:     Value: 0x0
284# SHNDX-ERR-LLVM-NEXT:     Size: 0
285# SHNDX-ERR-LLVM-NEXT:     Binding: Local (0x0)
286# SHNDX-ERR-LLVM-NEXT:     Type: None (0x0)
287# SHNDX-ERR-LLVM-NEXT:     Other: 0
288# SHNDX-ERR-LLVM-NEXT:     Section: Undefined (0x0)
289# SHNDX-ERR-LLVM-NEXT:   }
290# SHNDX-ERR-LLVM-NEXT:   Symbol {
291# SHNDX-ERR-LLVM-NEXT:     Name:  (0)
292# SHNDX-ERR-LLVM-NEXT:     Value: 0x0
293# SHNDX-ERR-LLVM-NEXT:     Size: 0
294# SHNDX-ERR-LLVM-NEXT:     Binding: Local (0x0)
295# SHNDX-ERR-LLVM-NEXT:     Type: None (0x0)
296# SHNDX-ERR-LLVM-NEXT:     Other: 0
297# SHNDX-ERR-LLVM-NEXT: warning: '[[FILE]]': extended symbol index (1) is past the end of the SHT_SYMTAB_SHNDX section of size 0
298# SHNDX-ERR-LLVM-NEXT:     Section: Reserved (0xFFFF)
299# SHNDX-ERR-LLVM-NEXT:   }
300# SHNDX-ERR-LLVM-NEXT: ]
301
302--- !ELF
303FileHeader:
304  Class: ELFCLASS64
305  Data:  ELFDATA2LSB
306  Type:  ET_REL
307Sections:
308  - Name:    .symtab_shndx
309    Type:    SHT_SYMTAB_SHNDX
310    Entries: [ 0, 1, 2 ]
311    Link:    .symtab
312Symbols:
313  - Index: SHN_XINDEX
314