1## Test how llvm-readobj/llvm-readelf tools handle invalid SHT_GNU_verdef sections.
2
3## Check that we report a warning when sh_link references a non-existent section.
4
5# RUN: yaml2obj %s --docnum=1 -DLINK=0xFF -o %t1
6# RUN: llvm-readobj -V %t1 2>&1 | FileCheck %s --check-prefix=INVALID-LINK-LLVM --implicit-check-not="warning:" -DFILE=%t1
7# RUN: llvm-readelf -V %t1 2>&1 | FileCheck %s --check-prefix=INVALID-LINK-GNU --implicit-check-not="warning:" -DFILE=%t1
8
9# INVALID-LINK-LLVM: warning: '[[FILE]]': invalid section linked to SHT_GNU_verdef section with index 1: invalid section index: 255
10
11# INVALID-LINK-GNU:      Version definition section '.gnu.version_d' contains 0 entries:
12# INVALID-LINK-GNU:      warning: '[[FILE]]': invalid section linked to SHT_GNU_verdef section with index 1: invalid section index: 255
13# INVALID-LINK-GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 255 (<corrupt>)
14
15--- !ELF
16FileHeader:
17  Class: ELFCLASS64
18  Data:  ELFDATA2LSB
19  Type:  ET_DYN
20Sections:
21  - Name:    .gnu.version_d
22    Type:    SHT_GNU_verdef
23    Link:    [[LINK]]
24    Info:    0x0
25    Entries: []
26
27## Check that we report a warning when the sh_link field of a SHT_GNU_verdef section references a non-string table section.
28
29# RUN: yaml2obj %s --docnum=1 -DLINK=0x0 -o %t2
30# RUN: llvm-readobj -V %t2 2>&1 | FileCheck %s --check-prefix=INVALID-STRING-TABLE -DFILE=%t2
31# RUN: llvm-readelf -V %t2 2>&1 | FileCheck %s --check-prefix=INVALID-STRING-TABLE -DFILE=%t2
32
33# INVALID-STRING-TABLE: warning: '[[FILE]]': invalid string table linked to SHT_GNU_verdef section with index 1: invalid sh_type for string table section [index 0]: expected SHT_STRTAB, but got SHT_NULL
34
35## Check that we report a warning when we can't read the content of the SHT_GNU_verdef section.
36
37# RUN: yaml2obj %s --docnum=2 -DSHOFFSET=0xFFFFFFFF -o %t3
38# RUN: llvm-readobj -V %t3 2>&1 | FileCheck %s --check-prefix=INVALID-DATA -DFILE=%t3
39# RUN: llvm-readelf -V %t3 2>&1 | FileCheck %s --check-prefix=INVALID-DATA -DFILE=%t3
40
41# INVALID-DATA: warning: '[[FILE]]': cannot read content of SHT_GNU_verdef section with index 1: section [index 1] has a sh_offset (0xffffffff) + sh_size (0x0) that is greater than the file size (0x230)
42
43--- !ELF
44FileHeader:
45  Class: ELFCLASS64
46  Data:  ELFDATA2LSB
47  Type:  ET_DYN
48Sections:
49  - Name:     .gnu.version_d
50    Type:     SHT_GNU_verdef
51    Link:     .dynstr
52    Info:     0x0
53    Entries:  []
54    ShOffset: [[SHOFFSET=<none>]]
55    ShName:   [[SHNAME=<none>]]
56  - Name:   .dynstr
57    Type:   SHT_STRTAB
58    ShName: [[DYNSTRNAME=<none>]]
59DynamicSymbols:
60  - Name: foo
61
62## Check that llvm-readelf reports a warning when the name of the SHT_GNU_verdef section can't be read.
63
64# RUN: yaml2obj %s --docnum=2 -DSHNAME=0xFF -o %t.invalid.name
65# RUN: llvm-readobj -V %t.invalid.name 2>&1 | \
66# RUN:   FileCheck %s --check-prefix=INVALID-NAME-LLVM -DFILE=%t.invalid.name --implicit-check-not=warning:
67# RUN: llvm-readelf -V %t.invalid.name 2>&1 | \
68# RUN:   FileCheck %s --check-prefix=INVALID-NAME-GNU -DFILE=%t.invalid.name --implicit-check-not=warning:
69
70# INVALID-NAME-LLVM:      VersionDefinitions [
71# INVALID-NAME-LLVM-NEXT: ]
72
73# INVALID-NAME-GNU:      warning: '[[FILE]]': unable to get the name of SHT_GNU_verdef section with index 1: a section [index 1] has an invalid sh_name (0xff) offset which goes past the end of the section name string table
74# INVALID-NAME-GNU-NEXT: Version definition section '<?>' contains 0 entries:
75# INVALID-NAME-GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 2 (.dynstr)
76
77## Check that llvm-readelf reports a warning when we are unable to read the name of
78## the section linked with the SHT_GNU_verdef section (usually ".dynstr").
79
80# RUN: yaml2obj %s --docnum=2 -DDYNSTRNAME=0xFF -o %t.invalid.name2
81# RUN: llvm-readobj -V %t.invalid.name2 2>&1 | \
82# RUN:   FileCheck %s --check-prefix=INVALID-NAME-LLVM -DFILE=%t.invalid.name2 --implicit-check-not=warning:
83# RUN: llvm-readelf -V %t.invalid.name2 2>&1 | \
84# RUN:   FileCheck %s --check-prefix=INVALID-NAME2-GNU -DFILE=%t.invalid.name2 --implicit-check-not=warning:
85
86# INVALID-NAME2-GNU:      Version definition section '.gnu.version_d' contains 0 entries:
87# INVALID-NAME2-GNU-NEXT: warning: '[[FILE]]': unable to get the name of SHT_STRTAB section with index 2: a section [index 2] has an invalid sh_name (0xff) offset which goes past the end of the section name string table
88# INVALID-NAME2-GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 2 (<?>)
89
90## Check that we report a warning when a SHT_GNU_verdef section contains a version definition
91## that goes past the end of the section.
92
93# RUN: yaml2obj %s --docnum=3 -o %t4
94# RUN: llvm-readobj -V %t4 2>&1 | FileCheck %s --check-prefix=DEF-PAST-END -DFILE=%t4
95# RUN: llvm-readelf -V %t4 2>&1 | FileCheck %s --check-prefix=DEF-PAST-END -DFILE=%t4
96
97# DEF-PAST-END: warning: '[[FILE]]': invalid SHT_GNU_verdef section with index 1: version definition 1 goes past the end of the section
98
99--- !ELF
100FileHeader:
101  Class: ELFCLASS64
102  Data:  ELFDATA2LSB
103  Type:  ET_DYN
104Sections:
105  - Name:    .gnu.version_d
106    Type:    SHT_GNU_verdef
107    Link:    .dynstr
108    Info:    0x1
109    Entries:
110      - Version:    1
111        Flags:      0
112        VersionNdx: 0
113        Hash:       0
114        Names:
115          - FOO
116    ShSize: 1
117DynamicSymbols:
118  - Name: foo
119
120## Check that we report a warning when a SHT_GNU_verdef section contains a version definition
121## that refers to an auxiliary entry that goes past the end of the section.
122
123# RUN: yaml2obj %s --docnum=4 -o %t5
124# RUN: llvm-readobj -V %t5 2>&1 | FileCheck %s --check-prefix=AUX-PAST-END -DFILE=%t5
125# RUN: llvm-readelf -V %t5 2>&1 | FileCheck %s --check-prefix=AUX-PAST-END -DFILE=%t5
126
127# AUX-PAST-END: warning: '[[FILE]]': invalid SHT_GNU_verdef section with index 1: version definition 1 refers to an auxiliary entry that goes past the end of the section
128
129--- !ELF
130FileHeader:
131  Class: ELFCLASS64
132  Data:  ELFDATA2LSB
133  Type:  ET_DYN
134Sections:
135  - Name:    .gnu.version_d
136    Type:    SHT_GNU_verdef
137    Link:    .dynstr
138    Info:    0x1
139    Entries:
140      - Version:    1
141        Flags:      0
142        VersionNdx: 0
143        Hash:       0
144        Names:
145          - FOO
146    ShSize: 21
147DynamicSymbols:
148  - Name: foo
149
150## Check that we can dump a SHT_GNU_verdef section properly even if it contains version names strings
151## that overrun the linked string table.
152
153# RUN: yaml2obj %s --docnum=5 -o %t6
154# RUN: llvm-readobj -V %t6 2>&1 | FileCheck %s --check-prefix=PAST-STRTAB-END-LLVM --implicit-check-not="warning:" -DFILE=%t6
155# RUN: llvm-readelf -V %t6 2>&1 | FileCheck %s --check-prefix=PAST-STRTAB-END-GNU --implicit-check-not="warning:" -DFILE=%t6
156
157# PAST-STRTAB-END-LLVM:      VersionDefinitions [
158# PAST-STRTAB-END-LLVM-NEXT:   Definition {
159# PAST-STRTAB-END-LLVM-NEXT:     Version: 1
160# PAST-STRTAB-END-LLVM-NEXT:     Flags [ (0x0)
161# PAST-STRTAB-END-LLVM-NEXT:     ]
162# PAST-STRTAB-END-LLVM-NEXT:     Index: 0
163# PAST-STRTAB-END-LLVM-NEXT:     Hash: 0
164# PAST-STRTAB-END-LLVM-NEXT:     Name: <invalid vda_name: 5>
165# PAST-STRTAB-END-LLVM-NEXT:     Predecessors: []
166# PAST-STRTAB-END-LLVM-NEXT:   }
167# PAST-STRTAB-END-LLVM-NEXT: ]
168
169# PAST-STRTAB-END-GNU:      Version definition section '.gnu.version_d' contains 1 entries:
170# PAST-STRTAB-END-GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 2 (.strtab)
171# PAST-STRTAB-END-GNU-NEXT:   0x0000: Rev: 1  Flags: none  Index: 0  Cnt: 1  Name: <invalid vda_name: 5>
172
173--- !ELF
174FileHeader:
175  Class: ELFCLASS64
176  Data:  ELFDATA2LSB
177  Type:  ET_DYN
178Sections:
179  - Name:    .gnu.version_d
180    Type:    SHT_GNU_verdef
181    Link:    .strtab
182    Info:    0x1
183    Entries:
184      - Version:    1
185        Flags:      0
186        VersionNdx: 0
187        Hash:       0
188        Names:
189          - FOO
190  - Name: .strtab
191    Type: SHT_STRTAB
192DynamicSymbols:
193  - Name: BAR
194
195## Check we report a warning when a version definition is not correctly aligned in memory.
196
197# RUN: yaml2obj %s --docnum=6 -o %t7
198# RUN: llvm-readobj -V %t7 2>&1 | FileCheck %s --check-prefix=MISALIGNED-DEF -DFILE=%t7
199# RUN: llvm-readelf -V %t7 2>&1 | FileCheck %s --check-prefix=MISALIGNED-DEF -DFILE=%t7
200
201# MISALIGNED-DEF: warning: '[[FILE]]': invalid SHT_GNU_verdef section with index 1: found a misaligned version definition entry at offset 0x0
202
203--- !ELF
204FileHeader:
205  Class: ELFCLASS64
206  Data:  ELFDATA2LSB
207  Type:  ET_DYN
208Sections:
209  - Type: Fill
210    Size: 0x1
211  - Name: .gnu.version_d
212    Type: SHT_GNU_verdef
213    Link: .dynstr
214    Info: 0x1
215    Entries:
216      - Version:    1
217        Flags:      0
218        VersionNdx: 0
219        Hash:       0
220        Names:
221          - FOO
222DynamicSymbols:
223  - Name: foo
224
225## Check we report a warning when an auxiliary entry is not correctly aligned in memory.
226
227# RUN: yaml2obj %s --docnum=7 -o %t8
228# RUN: llvm-readobj -V %t8 2>&1 | FileCheck %s --check-prefix=MISALIGNED-AUX -DFILE=%t8
229# RUN: llvm-readelf -V %t8 2>&1 | FileCheck %s --check-prefix=MISALIGNED-AUX -DFILE=%t8
230
231# MISALIGNED-AUX: warning: '[[FILE]]': invalid SHT_GNU_verdef section with index 1: found a misaligned auxiliary entry at offset 0x13
232
233--- !ELF
234FileHeader:
235  Class: ELFCLASS64
236  Data:  ELFDATA2LSB
237  Type:  ET_DYN
238Sections:
239  - Name:  .gnu.version_d
240    Type:  SHT_GNU_verdef
241    Flags: [ SHF_ALLOC ]
242    Link:  .dynstr
243    Info:  0x1
244## The byte offset to the auxiliary entry is 0x13, i.e. it is not correctly aligned in memory.
245    Content: "0100000000000100000000001300000000000000"
246DynamicSymbols:
247  - Name:    foo
248    Binding: STB_GLOBAL
249
250## Check how we handle a version definition entry with an unsupported version.
251
252# RUN: yaml2obj %s --docnum=8 -o %t9
253# RUN: llvm-readobj -V %t9 2>&1 | FileCheck %s --check-prefix=UNSUPPORTED-VERSION -DFILE=%t9
254# RUN: llvm-readelf -V %t9 2>&1 | FileCheck %s --check-prefix=UNSUPPORTED-VERSION -DFILE=%t9
255
256# UNSUPPORTED-VERSION: warning: '[[FILE]]': unable to dump SHT_GNU_verdef section with index 1: version 65278 is not yet supported
257
258--- !ELF
259FileHeader:
260  Class: ELFCLASS64
261  Data:  ELFDATA2LSB
262  Type:  ET_DYN
263Sections:
264  - Name: .gnu.version_d
265    Type: SHT_GNU_verdef
266    Link: .dynstr
267    Info: 0x1
268    Entries:
269      - Version:    0xfefe
270        Flags:      0
271        VersionNdx: 0
272        Hash:       0
273        Names: []
274DynamicSymbols:
275  - Name: foo
276
277## Check we report a warning when trying to print version symbols, but SHT_GNU_verdef
278## is invalid due to any reason.
279
280# RUN: yaml2obj %s --docnum=9 -o %t10
281# RUN: llvm-readobj -V %t10 2>&1 | FileCheck %s --check-prefix=INVALID-VERDEF-LLVM -DFILE=%t10
282# RUN: llvm-readelf -V %t10 2>&1 | FileCheck %s --check-prefix=INVALID-VERDEF-GNU -DFILE=%t10
283
284# INVALID-VERDEF-LLVM:      VersionSymbols [
285# INVALID-VERDEF-LLVM-NEXT:    Symbol {
286# INVALID-VERDEF-LLVM-NEXT:      Version: 0
287# INVALID-VERDEF-LLVM-NEXT:      Name:
288# INVALID-VERDEF-LLVM-NEXT:    }
289# INVALID-VERDEF-LLVM-NEXT:    Symbol {
290# INVALID-VERDEF-LLVM-NEXT:    Version: 2
291# INVALID-VERDEF-LLVM-NEXT:  warning: '[[FILE]]': invalid SHT_GNU_verdef section with index 2: version definition 1 goes past the end of the section
292# INVALID-VERDEF-LLVM-NEXT:    Name: foo@<corrupt>
293
294# INVALID-VERDEF-GNU:      Version symbols section '.gnu.version' contains 2 entries:
295# INVALID-VERDEF-GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 3 (.dynsym)
296# INVALID-VERDEF-GNU-NEXT: warning: '[[FILE]]': unable to get a version for entry 1 of SHT_GNU_versym section with index 1: invalid SHT_GNU_verdef section with index 2: version definition 1 goes past the end of the section
297# INVALID-VERDEF-GNU-NEXT:   000: 0 (*local*) 2 (<corrupt>)
298
299--- !ELF
300FileHeader:
301  Class: ELFCLASS64
302  Data:  ELFDATA2LSB
303  Type:  ET_DYN
304Sections:
305  - Name:         .gnu.version
306    Type:         SHT_GNU_versym
307    Flags:        [ SHF_ALLOC ]
308    Link:         .dynsym
309    AddressAlign: 0x0000000000000002
310    EntSize:      0x0000000000000002
311    Entries:      [ 0, 2 ]
312  - Name:         .gnu.version_d
313    Type:         SHT_GNU_verdef
314    Flags:        [ SHF_ALLOC ]
315    Link:         .dynstr
316    AddressAlign: 0x4
317    Info:         0x1
318    Entries: []
319DynamicSymbols:
320  - Name:    foo
321    Binding: STB_GLOBAL
322