1## Check that we correctly display the contents of the .stack_sizes section
2## in a relocatable object file.
3
4# RUN: yaml2obj --docnum=1 %s -o %t01
5# RUN: llvm-readelf --stack-sizes %t01 \
6# RUN:   | FileCheck %s --check-prefix=RELOC-GNU --strict-whitespace --match-full-lines
7# RUN: llvm-readobj --stack-sizes %t01 | FileCheck %s --check-prefix=RELOC-LLVM
8
9#      RELOC-GNU:         Size     Function
10# RELOC-GNU-NEXT:           16     referenced_by_symbol_foo
11# RELOC-GNU-NEXT:           32     referenced_via_section_bar
12# RELOC-GNU-NEXT:            8     separate_text_section_baz
13#  RELOC-GNU-NOT:{{.}}
14
15# RELOC-LLVM:      StackSizes [
16# RELOC-LLVM-NEXT:   Entry {
17# RELOC-LLVM-NEXT:     Function: referenced_by_symbol_foo
18# RELOC-LLVM-NEXT:     Size: 0x10
19# RELOC-LLVM-NEXT:   }
20# RELOC-LLVM-NEXT:   Entry {
21# RELOC-LLVM-NEXT:     Function: referenced_via_section_bar
22# RELOC-LLVM-NEXT:     Size: 0x20
23# RELOC-LLVM-NEXT:   }
24# RELOC-LLVM-NEXT:   Entry {
25# RELOC-LLVM-NEXT:     Function: separate_text_section_baz
26# RELOC-LLVM-NEXT:     Size: 0x8
27# RELOC-LLVM-NEXT:   }
28# RELOC-LLVM-NEXT: ]
29
30--- !ELF
31FileHeader:
32  Class:   ELFCLASS64
33  Data:    ELFDATA2LSB
34  Type:    ET_REL
35  Machine: EM_X86_64
36Sections:
37  - Name:    .text
38    Type:    SHT_PROGBITS
39    Flags:   [SHF_ALLOC]
40    Size:    16
41  - Name:    .text.baz
42    Type:    SHT_PROGBITS
43    Flags:   [SHF_ALLOC]
44    Size:    16
45  - Name:    .stack_sizes
46    Type:    SHT_PROGBITS
47    Entries:
48      - Size: 0x10
49      - Size: 0x20
50    Link:    .text
51  - Name:    '.stack_sizes (1)'
52    Type:    SHT_PROGBITS
53    Entries:
54      - Address: 0x20
55        Size:    0x8
56    Link:    .text.baz
57  - Name:    .rela.stack_sizes
58    Type:    SHT_RELA
59    Info:    .stack_sizes
60    Relocations:
61## A symbol relative reference.
62      - Offset: 0
63        Symbol: referenced_by_symbol_foo
64        Type:   R_X86_64_64
65## A section relative reference.
66      - Offset: 9
67        Addend: 16
68        Symbol: .text
69        Type:   R_X86_64_64
70  - Name:   '.rela.stack_sizes (1)'
71    Type:   SHT_RELA
72    Info:   '.stack_sizes (1)'
73    Relocations:
74      - Offset: 0
75        Symbol: separate_text_section_baz
76        Type:   R_X86_64_64
77  - Name:     .symtab
78    Type:     SHT_SYMTAB
79    ShOffset: [[SYMTABOFFSET=<none>]]
80Symbols:
81  - Name:    separate_text_section_baz
82    Section: [[SEC1=.text.baz]]
83    Type:    STT_FUNC
84    Index:   [[SEC1INDEX=<none>]]
85  - Name:    .text
86    Section: .text
87    Type:    STT_SECTION
88  - Name:    referenced_by_symbol_foo
89    Section: .text
90    Type:    STT_FUNC
91    Binding: STB_GLOBAL
92  - Name:    referenced_via_section_bar
93    Section: .text
94    Value:   0x10
95    Type:    STT_FUNC
96    Binding: STB_GLOBAL
97
98## Check that we report a warning when we are unable to read
99## the symbol table when dumping stack sizes.
100
101# RUN: yaml2obj --docnum=1 %s -DSYMTABOFFSET=0xffffeeee -o %t01.broken.symtab
102# RUN: llvm-readelf --stack-sizes %t01.broken.symtab 2>&1 | \
103# RUN:   FileCheck %s -DFILE=%t01.broken.symtab --check-prefix=SYMTAB-GNU --implicit-check-not=warning:
104# RUN: llvm-readobj --stack-sizes %t01.broken.symtab 2>&1 | \
105# RUN:   FileCheck %s -DFILE=%t01.broken.symtab --check-prefix=SYMTAB-LLVM --implicit-check-not=warning:
106
107# SYMTAB-GNU:      Stack Sizes:
108# SYMTAB-GNU-NEXT:          Size     Function
109# SYMTAB-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 0 in SHT_RELA section with index 5: unable to read an entry with index 3 from SHT_SYMTAB section with index 7: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450)
110# SYMTAB-GNU-NEXT: warning: '[[FILE]]': unable to read the symbol table: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450)
111# SYMTAB-GNU-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 3
112# SYMTAB-GNU-NEXT:            16     ?
113# SYMTAB-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 1 in SHT_RELA section with index 5: unable to read an entry with index 2 from SHT_SYMTAB section with index 7: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450)
114# SYMTAB-GNU-NEXT:            32     ?
115# SYMTAB-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 0 in SHT_RELA section with index 6: unable to read an entry with index 1 from SHT_SYMTAB section with index 7: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450)
116# SYMTAB-GNU-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 4
117# SYMTAB-GNU-NEXT:             8     ?
118
119# SYMTAB-LLVM:      StackSizes [
120# SYMTAB-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 0 in SHT_RELA section with index 5: unable to read an entry with index 3 from SHT_SYMTAB section with index 7: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450)
121# SYMTAB-LLVM-NEXT: warning: '[[FILE]]': unable to read the symbol table: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450)
122# SYMTAB-LLVM-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 3
123# SYMTAB-LLVM-NEXT:   Entry {
124# SYMTAB-LLVM-NEXT:     Function: ?
125# SYMTAB-LLVM-NEXT:     Size: 0x10
126# SYMTAB-LLVM-NEXT:   }
127# SYMTAB-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 1 in SHT_RELA section with index 5: unable to read an entry with index 2 from SHT_SYMTAB section with index 7: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450)
128# SYMTAB-LLVM-NEXT:   Entry {
129# SYMTAB-LLVM-NEXT:     Function: ?
130# SYMTAB-LLVM-NEXT:     Size: 0x20
131# SYMTAB-LLVM-NEXT:   }
132# SYMTAB-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 0 in SHT_RELA section with index 6: unable to read an entry with index 1 from SHT_SYMTAB section with index 7: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450)
133# SYMTAB-LLVM-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 4
134# SYMTAB-LLVM-NEXT:   Entry {
135# SYMTAB-LLVM-NEXT:     Function: ?
136# SYMTAB-LLVM-NEXT:     Size: 0x8
137# SYMTAB-LLVM-NEXT:   }
138# SYMTAB-LLVM-NEXT: ]
139
140## In this case we have a function symbol with an invalid section index.
141## Document what we dump.
142
143# RUN: yaml2obj --docnum=1 %s -DSEC1="<none>" -DSEC1INDEX=0xFF -o %t01.broken.sym
144# RUN: llvm-readelf --stack-sizes %t01.broken.sym 2>&1 | \
145# RUN:   FileCheck %s -DFILE=%t01.broken.sym --check-prefix=SYM-GNU --implicit-check-not=warning:
146# RUN: llvm-readobj --stack-sizes %t01.broken.sym 2>&1 | \
147# RUN:   FileCheck %s -DFILE=%t01.broken.sym --check-prefix=SYM-LLVM --implicit-check-not=warning:
148
149# SYM-GNU:      Stack Sizes:
150# SYM-GNU-NEXT:          Size     Function
151# SYM-GNU-NEXT: warning: '[[FILE]]': unable to get address of symbol 'separate_text_section_baz': invalid section index: 255
152# SYM-GNU-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 3
153# SYM-GNU-NEXT:            16     ?
154# SYM-GNU-NEXT:            32     ?
155# SYM-GNU-NEXT: warning: '[[FILE]]': cannot identify the section for relocation symbol 'separate_text_section_baz': invalid section index: 255
156# SYM-GNU-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 4
157# SYM-GNU-NEXT:             8     ?
158
159# SYM-LLVM:      StackSizes [
160# SYM-LLVM-NEXT: warning: '[[FILE]]': unable to get address of symbol 'separate_text_section_baz': invalid section index: 255
161# SYM-LLVM-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 3
162# SYM-LLVM-NEXT:   Entry {
163# SYM-LLVM-NEXT:     Function: ?
164# SYM-LLVM-NEXT:     Size: 0x10
165# SYM-LLVM-NEXT:   }
166# SYM-LLVM-NEXT:   Entry {
167# SYM-LLVM-NEXT:     Function: ?
168# SYM-LLVM-NEXT:     Size: 0x20
169# SYM-LLVM-NEXT:   }
170# SYM-LLVM-NEXT: warning: '[[FILE]]': cannot identify the section for relocation symbol 'separate_text_section_baz': invalid section index: 255
171# SYM-LLVM-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 4
172# SYM-LLVM-NEXT:   Entry {
173# SYM-LLVM-NEXT:     Function: ?
174# SYM-LLVM-NEXT:     Size: 0x8
175# SYM-LLVM-NEXT:   }
176# SYM-LLVM-NEXT: ]
177
178## Check that we correctly report the stack sizes in an executable (non-relocatable)
179## object file. This also shows that the sh_link field is ignored in this situation
180## without warning.
181
182# RUN: yaml2obj --docnum=2 %s -o %t02
183# RUN: llvm-readelf --stack-sizes %t02 2>&1 \
184# RUN:   | FileCheck %s --check-prefix=EXEC-GNU --strict-whitespace \
185# RUN:                  --match-full-lines --implicit-check-not=warning:
186# RUN: llvm-readobj --stack-sizes %t02 2>&1 \
187# RUN:   | FileCheck %s --check-prefix=EXEC-LLVM --implicit-check-not=warning:
188
189#      EXEC-GNU:         Size     Function
190# EXEC-GNU-NEXT:           16     other
191# EXEC-GNU-NEXT:           32     other_end
192# EXEC-GNU-NEXT:           48     bar
193#  EXEC-GNU-NOT:{{.}}
194
195# EXEC-LLVM:      StackSizes [
196# EXEC-LLVM-NEXT:   Entry {
197# EXEC-LLVM-NEXT:     Function: other
198# EXEC-LLVM-NEXT:     Size: 0x10
199# EXEC-LLVM-NEXT:   }
200# EXEC-LLVM-NEXT:   Entry {
201# EXEC-LLVM-NEXT:     Function: other_end
202# EXEC-LLVM-NEXT:     Size: 0x20
203# EXEC-LLVM-NEXT:   }
204# EXEC-LLVM-NEXT:   Entry {
205# EXEC-LLVM-NEXT:     Function: bar
206# EXEC-LLVM-NEXT:     Size: 0x30
207# EXEC-LLVM-NEXT:   }
208# EXEC-LLVM-NEXT: ]
209
210--- !ELF
211FileHeader:
212  Class: ELFCLASS64
213  Data:  ELFDATA2LSB
214  Type:  ET_EXEC
215Sections:
216  - Name:    .text
217    Type:    SHT_PROGBITS
218    Flags:   [SHF_ALLOC]
219    Size:    16
220  - Name:    .text2
221    Type:    SHT_PROGBITS
222    Flags:   [SHF_ALLOC]
223    Size:    16
224  - Name:    .stack_sizes
225    Type:    SHT_PROGBITS
226    Entries:
227      - Address: 0x0
228        Size:    0x10
229      - Address: 0x10
230        Size:    0x20
231      - Address: 0x20
232        Size:    0x30
233    Link:    .text2
234Symbols:
235  ## Undefined symbols are ignored.
236  - Name:    undefined
237    Type:    STT_FUNC
238    Binding: STB_GLOBAL
239  ## sh_link of .stack_sizes is ignored for non-reloctable objects.
240  - Name:    other
241    Section: .text
242    Value:   0
243    Type:    STT_FUNC
244    Binding: STB_GLOBAL
245  ## If two symbols have the same value, the first is picked, regardless of
246  ## the sh_link value of the .stack_sizes section.
247  - Name:    other_end
248    Section: .text
249    Value:   0x10
250    Type:    STT_FUNC
251    Binding: STB_GLOBAL
252  - Name:    foo
253    Section: .text2
254    Value:   0x10
255    Type:    STT_FUNC
256    Binding: STB_GLOBAL
257  - Name:    bar
258    Section: .text2
259    Value:   0x20
260    Type:    STT_FUNC
261    Binding: STB_GLOBAL
262
263## Check that we report a warning when we find relocations whose offsets point outside
264## of the .stack_sizes section.
265
266# RUN: yaml2obj --docnum=3 %s -o %t03
267# RUN: llvm-readelf --stack-sizes %t03 2>&1 | FileCheck %s --check-prefix=SHORT-GNU -DFILE=%t03
268# RUN: llvm-readobj --stack-sizes %t03 2>&1 | FileCheck %s --check-prefix=SHORT-LLVM -DFILE=%t03
269
270# SHORT-GNU:      Stack Sizes:
271# SHORT-GNU-NEXT:  Size     Function
272# SHORT-GNU-NEXT:     8     foo
273# SHORT-GNU-NEXT: warning: '[[FILE]]': found invalid relocation offset (0x1) into SHT_PROGBITS section with index 2 while trying to extract a stack size entry
274# SHORT-GNU-NEXT:     8     foo
275
276# SHORT-LLVM:     StackSizes [
277# SHORT-LLVM-NEXT:   Entry {
278# SHORT-LLVM-NEXT:     Function: foo
279# SHORT-LLVM-NEXT:     Size: 0x8
280# SHORT-LLVM-NEXT:   }
281# SHORT-LLVM-NEXT: warning: '[[FILE]]': found invalid relocation offset (0x1) into SHT_PROGBITS section with index 2 while trying to extract a stack size entry
282# SHORT-LLVM-NEXT:   Entry {
283# SHORT-LLVM-NEXT:     Function: foo
284# SHORT-LLVM-NEXT:     Size: 0x8
285# SHORT-LLVM-NEXT:   }
286# SHORT-LLVM-NEXT: ]
287
288--- !ELF
289FileHeader:
290  Class:   ELFCLASS64
291  Data:    ELFDATA2LSB
292  Type:    ET_REL
293  Machine: EM_X86_64
294Sections:
295  - Name:    .text
296    Type:    SHT_PROGBITS
297    Flags:   [SHF_ALLOC]
298    Size:    16
299  - Name:    .stack_sizes
300    Type:    SHT_PROGBITS
301    Link:    .text
302    Entries:
303      - Size: 0x8
304  - Name:    .rela.stack_sizes
305    Type:    SHT_RELA
306    Info:    .stack_sizes
307    Relocations:
308      - Offset: 0x0
309        Symbol: foo
310        Type:   R_X86_64_64
311      - Offset: 0x1
312        Symbol: foo
313        Type:   R_X86_64_64
314      - Offset: 0x1
315        Symbol: foo
316        Type:   R_X86_64_64
317      - Offset: 0x0
318        Symbol: foo
319        Type:   R_X86_64_64
320Symbols:
321  - Name:    foo
322    Section: .text
323    Type:    STT_FUNC
324    Binding: STB_GLOBAL
325
326## Check that we warn about a function symbol that is not in the section
327## that is referenced by the stack sizes section's sh_link, for relocatable
328## output.
329
330# RUN: yaml2obj --docnum=4 %s -o %t04
331# RUN: llvm-readelf --stack-sizes %t04 2> %t04-gnu.err | FileCheck %s --check-prefix=WRONGSECTION-GNU
332# RUN: FileCheck %s < %t04-gnu.err --check-prefix=WRONGSECTION-ERR -DFILE=%t04
333# RUN: llvm-readobj --stack-sizes %t04 2> %t04-llvm.err | FileCheck %s --check-prefix=WRONGSECTION-LLVM
334# RUN: FileCheck %s < %t04-llvm.err --check-prefix=WRONGSECTION-ERR -DFILE=%t04
335
336# RUN: llvm-readelf --stack-sizes --demangle %t04 2>&1 | FileCheck %s --check-prefix=WRONGSECTION-DEMANGLE-ERR -DFILE=%t04
337# RUN: llvm-readobj --stack-sizes --demangle %t04 2>&1 | FileCheck %s --check-prefix=WRONGSECTION-DEMANGLE-ERR -DFILE=%t04
338
339# WRONGSECTION-GNU:      Size Function
340# WRONGSECTION-GNU-NEXT: 8 _Z3foof
341
342# WRONGSECTION-LLVM:      StackSizes [
343# WRONGSECTION-LLVM-NEXT:   Entry {
344# WRONGSECTION-LLVM-NEXT:     Function: _Z3foof
345# WRONGSECTION-LLVM-NEXT:     Size: 0x8
346# WRONGSECTION-LLVM-NEXT:   }
347# WRONGSECTION-LLVM-NEXT: ]
348
349# WRONGSECTION-ERR:           warning: '[[FILE]]': relocation symbol '_Z3foof' is not in the expected section
350# WRONGSECTION-DEMANGLE-ERR:  warning: '[[FILE]]': relocation symbol 'foo(float)' is not in the expected section
351
352--- !ELF
353FileHeader:
354  Class:   ELFCLASS64
355  Data:    ELFDATA2LSB
356  Type:    ET_REL
357  Machine: EM_X86_64
358Sections:
359  - Name:    .text
360    Type:    SHT_PROGBITS
361    Size:    8
362  - Name:    .text2
363    Type:    SHT_PROGBITS
364    Size:    8
365    Flags:   [SHF_ALLOC]
366  - Name:    .stack_sizes
367    Type:    SHT_PROGBITS
368    Entries:
369      - Size: 0x8
370    Link:    .text2
371  - Name:    .rela.stack_sizes
372    Type:    SHT_RELA
373    Info:    .stack_sizes
374    Relocations:
375      - Offset: 0
376        Symbol: _Z3foof
377        Type:   R_X86_64_64
378Symbols:
379  - Name:    _Z3foof
380    Section: .text
381    Type:    STT_FUNC
382    Binding: STB_GLOBAL
383
384## Check that we report a warning when a stack sizes section ends with an incomplete stack size entry.
385
386# RUN: yaml2obj --docnum=5 %s -o %t05
387# RUN: llvm-readelf --stack-sizes %t05 2>&1 | \
388# RUN:   FileCheck %s --check-prefix=SUDDENEND-GNU -DFILE=%t05
389# RUN: llvm-readobj --stack-sizes %t05 2>&1 | \
390# RUN:   FileCheck %s --check-prefix=SUDDENEND-LLVM -DFILE=%t05
391
392# SUDDENEND-GNU:      Stack Sizes:
393# SUDDENEND-GNU-NEXT:  Size     Function
394# SUDDENEND-GNU-NEXT:     8     foo
395# SUDDENEND-GNU-NEXT: warning: '[[FILE]]': SHT_PROGBITS section with index 2 ended while trying to extract a stack size entry
396# SUDDENEND-GNU-NEXT:     8     foo
397# SUDDENEND-GNU-NEXT: warning: '[[FILE]]': SHT_PROGBITS section with index 3 ended while trying to extract a stack size entry
398
399# SUDDENEND-LLVM:      StackSizes [
400# SUDDENEND-LLVM-NEXT:   Entry {
401# SUDDENEND-LLVM-NEXT:     Function: foo
402# SUDDENEND-LLVM-NEXT:     Size: 0x8
403# SUDDENEND-LLVM-NEXT:   }
404# SUDDENEND-LLVM-NEXT: warning: '[[FILE]]': SHT_PROGBITS section with index 2 ended while trying to extract a stack size entry
405# SUDDENEND-LLVM-NEXT:   Entry {
406# SUDDENEND-LLVM-NEXT:     Function: foo
407# SUDDENEND-LLVM-NEXT:     Size: 0x8
408# SUDDENEND-LLVM-NEXT:   }
409# SUDDENEND-LLVM-NEXT: warning: '[[FILE]]': SHT_PROGBITS section with index 3 ended while trying to extract a stack size entry
410# SUDDENEND-LLVM-NEXT: ]
411
412--- !ELF
413FileHeader:
414  Class:   ELFCLASS64
415  Data:    ELFDATA2LSB
416  Type:    ET_EXEC
417  Machine: EM_X86_64
418Sections:
419  - Name: .text
420    Type: SHT_PROGBITS
421  - Name: .stack_sizes
422    Type: SHT_PROGBITS
423    Link: .text
424    Entries:
425      - Size: 0x8
426      - Size: 0x10
427## 0x11 == the normal size minus 1.
428    ShSize: 0x11
429  - Name: .stack_sizes (1)
430    Type: SHT_PROGBITS
431    Link: .text
432    Entries:
433      - Size: 0x8
434      - Size: 0x10
435    ShSize: 0x11
436Symbols:
437  - Name:    foo
438    Section: .text
439    Type:    STT_FUNC
440
441## Check that we report an invalid stack size, which is represented by a ULEB that
442## ends in a byte with the high bit set.
443
444# RUN: yaml2obj --docnum=6 %s -o %t06
445# RUN: llvm-readelf --stack-sizes %t06 2>&1 | \
446# RUN:   FileCheck %s --check-prefix=BADSIZE -DFILE=%t06 --implicit-check-not=warning:
447# RUN: llvm-readobj --stack-sizes %t06 2>&1 | \
448# RUN:   FileCheck %s --check-prefix=BADSIZE -DFILE=%t06 --implicit-check-not=warning:
449
450# BADSIZE: warning: '[[FILE]]': could not extract a valid stack size from SHT_PROGBITS section with index 2: unable to decode LEB128 at offset 0x00000008: malformed uleb128, extends past end
451# BADSIZE: warning: '[[FILE]]': could not extract a valid stack size from SHT_PROGBITS section with index 3: unable to decode LEB128 at offset 0x00000008: malformed uleb128, extends past end
452
453--- !ELF
454FileHeader:
455  Class: ELFCLASS64
456  Data:  ELFDATA2LSB
457  Type:  ET_EXEC
458Sections:
459  - Name:    .text
460    Type:    SHT_PROGBITS
461    Flags:   [SHF_ALLOC]
462    Size:    16
463  - Name:    .stack_sizes
464    Type:    SHT_PROGBITS
465    Content: "100000000000000080"
466    Link:    .text
467  - Name:    .stack_sizes (1)
468    Type:    SHT_PROGBITS
469    Content: "100000000000000080"
470    Link:    .text
471Symbols:
472  - Name:    foo
473    Section: .text
474    Value:   0x10
475    Type:    STT_FUNC
476    Binding: STB_GLOBAL
477
478## Check that we report a warning when a relocation symbol does not belong to a
479## valid section or when it has an invalid index. We expect a stack size entry
480## with an unknown symbol in the output.
481
482# RUN: yaml2obj --docnum=7 %s -o %t07
483# RUN: llvm-readelf --stack-sizes %t07 2>&1 | \
484# RUN:   FileCheck %s -DFILE=%t07 --check-prefix=BADSECTION-OUT-GNU --implicit-check-not=warning:
485# RUN: llvm-readobj --stack-sizes %t07 2>&1 | \
486# RUN:   FileCheck %s -DFILE=%t07 --check-prefix=BADSECTION-OUT-LLVM --implicit-check-not=warning:
487
488# BADSECTION-OUT-GNU:      Stack Sizes:
489# BADSECTION-OUT-GNU-NEXT:          Size     Function
490# BADSECTION-OUT-GNU-NEXT: warning: '[[FILE]]': cannot identify the section for relocation symbol '_Z3foof': invalid section index: 10
491# BADSECTION-OUT-GNU-NEXT: warning: '[[FILE]]': unable to get address of symbol '_Z3foof': invalid section index: 10
492# BADSECTION-OUT-GNU-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 2
493# BADSECTION-OUT-GNU-NEXT:             8     ?
494# BADSECTION-OUT-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 1 in SHT_RELA section with index 3: unable to read an entry with index 255 from SHT_SYMTAB section with index 4: can't read an entry at 0x17e8: it goes past the end of the section (0x30)
495# BADSECTION-OUT-GNU-NEXT:            22     ?
496# BADSECTION-OUT-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 2 in SHT_RELA section with index 3: unable to read an entry with index 255 from SHT_SYMTAB section with index 4: can't read an entry at 0x17e8: it goes past the end of the section (0x30)
497# BADSECTION-OUT-GNU-NEXT:            36     ?
498
499# BADSECTION-OUT-LLVM:      StackSizes [
500# BADSECTION-OUT-LLVM-NEXT: warning: '[[FILE]]': cannot identify the section for relocation symbol '_Z3foof': invalid section index: 10
501# BADSECTION-OUT-LLVM-NEXT: warning: '[[FILE]]': unable to get address of symbol '_Z3foof': invalid section index: 10
502# BADSECTION-OUT-LLVM-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 2
503# BADSECTION-OUT-LLVM-NEXT:   Entry {
504# BADSECTION-OUT-LLVM-NEXT:     Function: ?
505# BADSECTION-OUT-LLVM-NEXT:     Size: 0x8
506# BADSECTION-OUT-LLVM-NEXT:   }
507# BADSECTION-OUT-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 1 in SHT_RELA section with index 3: unable to read an entry with index 255 from SHT_SYMTAB section with index 4: can't read an entry at 0x17e8: it goes past the end of the section (0x30)
508# BADSECTION-OUT-LLVM-NEXT:   Entry {
509# BADSECTION-OUT-LLVM-NEXT:     Function: ?
510# BADSECTION-OUT-LLVM-NEXT:     Size: 0x16
511# BADSECTION-OUT-LLVM-NEXT:   }
512# BADSECTION-OUT-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 2 in SHT_RELA section with index 3: unable to read an entry with index 255 from SHT_SYMTAB section with index 4: can't read an entry at 0x17e8: it goes past the end of the section (0x30)
513# BADSECTION-OUT-LLVM-NEXT:   Entry {
514# BADSECTION-OUT-LLVM-NEXT:     Function: ?
515# BADSECTION-OUT-LLVM-NEXT:     Size: 0x24
516# BADSECTION-OUT-LLVM-NEXT:   }
517# BADSECTION-OUT-LLVM-NEXT: ]
518
519# RUN: llvm-readelf --stack-sizes --demangle %t07 2>&1 | FileCheck %s --check-prefix=BADSECTION-DEMANGLE-ERR -DFILE=%t07
520# RUN: llvm-readobj --stack-sizes --demangle %t07 2>&1 | FileCheck %s --check-prefix=BADSECTION-DEMANGLE-ERR -DFILE=%t07
521
522# BADSECTION-DEMANGLE-ERR: warning: '[[FILE]]': cannot identify the section for relocation symbol 'foo(float)'
523
524--- !ELF
525FileHeader:
526  Class:   ELFCLASS64
527  Data:    ELFDATA2LSB
528  Type:    ET_REL
529  Machine: EM_X86_64
530Sections:
531  - Name:    .text
532    Type:    SHT_PROGBITS
533    Size:    16
534  - Name:    .stack_sizes
535    Type:    SHT_PROGBITS
536    Link:    .text
537    Entries:
538      - Size: 0x8
539      - Size: 0x16
540      - Size: 0x24
541  - Name:    .rela.stack_sizes
542    Type:    SHT_RELA
543    Info:    .stack_sizes
544    Relocations:
545    - Offset: 0
546      Symbol: _Z3foof
547      Type:   R_X86_64_64
548    - Offset: 9
549## An invalid symbol index.
550      Symbol: 0xff
551      Type:   R_X86_64_64
552## One more invalid symbol index with the same symbol value (0xff).
553    - Offset: 0x12
554      Symbol: 0xff
555      Type:   R_X86_64_64
556Symbols:
557  - Name:    _Z3foof
558## An invalid section index.
559    Index:   10
560    Type:    STT_FUNC
561    Binding: STB_GLOBAL
562
563## Check that we report a warning when a stack sizes section does not come with
564## a corresponding relocation section.
565
566# RUN: yaml2obj --docnum=8 %s -o %t08
567# RUN: llvm-readelf --stack-sizes %t08 2> %t08-gnu.err | FileCheck %s --check-prefix=NORELOCSECTION-OUT-GNU
568# RUN: FileCheck %s < %t08-gnu.err --check-prefix=NORELOCSECTION-ERR -DFILE=%t08
569# RUN: llvm-readobj --stack-sizes %t08 2> %t08-llvm.err | FileCheck %s --check-prefix=NORELOCSECTION-OUT-LLVM
570# RUN: FileCheck %s < %t08-llvm.err --check-prefix=NORELOCSECTION-ERR -DFILE=%t08
571
572# NORELOCSECTION-OUT-GNU:     Size Function
573# NORELOCSECTION-OUT-GNU-NOT: {{.}}
574
575# NORELOCSECTION-OUT-LLVM:      StackSizes [
576# NORELOCSECTION-OUT-LLVM-NEXT: ]
577
578# NORELOCSECTION-ERR: warning: '[[FILE]]': .stack_sizes (SHT_PROGBITS section with index 2) does not have a corresponding relocation section
579
580--- !ELF
581FileHeader:
582  Class: ELFCLASS64
583  Data:  ELFDATA2LSB
584  Type:  ET_REL
585Sections:
586  - Name:    .text
587    Type:    SHT_PROGBITS
588    Size:    8
589  - Name:    .stack_sizes
590    Type:    SHT_PROGBITS
591    Link:    .text
592    Entries:
593      - Size: 0x1
594
595## Check that we handle multiple object files, separately and when they
596## are in an archive. This also checks whether we have blank lines between the
597## tables.
598
599# RUN: llvm-ar rc %t1.a %t01 %t02
600# RUN: llvm-readelf --stack-sizes %t01 %t02 \
601# RUN:   | FileCheck %s --check-prefixes=MULTIPLE-GNU,OBJECT -DFILE1=%t01 -DFILE2=%t02
602# RUN: llvm-readelf --stack-sizes %t1.a \
603# RUN:   | FileCheck %s --check-prefixes=MULTIPLE-GNU,ARCHIVE --strict-whitespace\
604# RUN:   --match-full-lines -DFILE=%t1.a
605# RUN: llvm-readobj --stack-sizes %t01 %t02 \
606# RUN:   | FileCheck %s --check-prefixes=MULTIPLE-LLVM,OBJECT -DFILE1=%t01 -DFILE2=%t02
607# RUN: llvm-readobj --stack-sizes %t1.a \
608# RUN:   | FileCheck %s --check-prefixes=MULTIPLE-LLVM,ARCHIVE -DFILE=%t1.a
609
610#        OBJECT:File: [[FILE1]]
611#       ARCHIVE:File: [[FILE]]({{.*01}})
612
613#      MULTIPLE-GNU:Stack Sizes:
614# MULTIPLE-GNU-NEXT:         Size     Function
615# MULTIPLE-GNU-NEXT:           16     referenced_by_symbol_foo
616# MULTIPLE-GNU-NEXT:           32     referenced_via_section_bar
617# MULTIPLE-GNU-NEXT:            8     separate_text_section_baz
618# MULTIPLE-GNU-EMPTY:
619
620# MULTIPLE-LLVM:      StackSizes [
621# MULTIPLE-LLVM-NEXT:   Entry {
622# MULTIPLE-LLVM-NEXT:     Function: referenced_by_symbol_foo
623# MULTIPLE-LLVM-NEXT:     Size: 0x10
624# MULTIPLE-LLVM-NEXT:   }
625# MULTIPLE-LLVM-NEXT:   Entry {
626# MULTIPLE-LLVM-NEXT:     Function: referenced_via_section_bar
627# MULTIPLE-LLVM-NEXT:     Size: 0x20
628# MULTIPLE-LLVM-NEXT:   }
629# MULTIPLE-LLVM-NEXT:   Entry {
630# MULTIPLE-LLVM-NEXT:     Function: separate_text_section_baz
631# MULTIPLE-LLVM-NEXT:     Size: 0x8
632# MULTIPLE-LLVM-NEXT:   }
633# MULTIPLE-LLVM-NEXT: ]
634
635#        OBJECT:File: [[FILE2]]
636#       ARCHIVE:File: [[FILE]]({{.*02}})
637
638# MULTIPLE-GNU-EMPTY:
639# MULTIPLE-GNU-NEXT:Stack Sizes:
640# MULTIPLE-GNU-NEXT:         Size     Function
641# MULTIPLE-GNU-NEXT:           16     other
642# MULTIPLE-GNU-NEXT:           32     other_end
643# MULTIPLE-GNU-NEXT:           48     bar
644
645# MULTIPLE-LLVM:      StackSizes [
646# MULTIPLE-LLVM-NEXT:   Entry {
647# MULTIPLE-LLVM-NEXT:     Function: other
648# MULTIPLE-LLVM-NEXT:     Size: 0x10
649# MULTIPLE-LLVM-NEXT:   }
650# MULTIPLE-LLVM-NEXT:   Entry {
651# MULTIPLE-LLVM-NEXT:     Function: other_end
652# MULTIPLE-LLVM-NEXT:     Size: 0x20
653# MULTIPLE-LLVM-NEXT:   }
654# MULTIPLE-LLVM-NEXT:   Entry {
655# MULTIPLE-LLVM-NEXT:     Function: bar
656# MULTIPLE-LLVM-NEXT:     Size: 0x30
657# MULTIPLE-LLVM-NEXT:   }
658# MULTIPLE-LLVM-NEXT: ]
659
660## Check that we do not consider symbols that are not function symbols, even though
661## a relocation references them.
662
663# RUN: yaml2obj --docnum=9 %s -o %t14
664# RUN: llvm-readelf --stack-sizes %t14 2> %t14-gnu.err | FileCheck %s --check-prefix=NONFUNCTIONSYM-GNU
665# RUN: FileCheck %s < %t14-gnu.err --check-prefix=NONFUNCTIONSYM-ERR -DFILE=%t14
666# RUN: llvm-readobj --stack-sizes %t14 2> %t14-llvm.err | FileCheck %s --check-prefix=NONFUNCTIONSYM-LLVM
667# RUN: FileCheck %s < %t14-llvm.err --check-prefix=NONFUNCTIONSYM-ERR -DFILE=%t14
668
669# NONFUNCTIONSYM-GNU:     Stack Sizes:
670# NONFUNCTIONSYM-GNU:     0 ?
671
672# NONFUNCTIONSYM-LLVM:      StackSizes [
673# NONFUNCTIONSYM-LLVM-NEXT:   Entry {
674# NONFUNCTIONSYM-LLVM-NEXT:     Function: ?
675# NONFUNCTIONSYM-LLVM-NEXT:     Size: 0x0
676# NONFUNCTIONSYM-LLVM-NEXT:   }
677# NONFUNCTIONSYM-LLVM-NEXT: ]
678
679# NONFUNCTIONSYM-ERR: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 2
680
681--- !ELF
682FileHeader:
683  Class:   ELFCLASS64
684  Data:    ELFDATA2LSB
685  Type:    ET_REL
686  Machine: EM_X86_64
687Sections:
688  - Name: .text
689    Type: SHT_PROGBITS
690    Size: 16
691  - Name: .stack_sizes
692    Type: SHT_PROGBITS
693    Entries:
694      - Size: 0
695    Link: .text
696  - Name: .rela.stack_sizes
697    Type: SHT_RELA
698    Info: .stack_sizes
699    Relocations:
700      - Offset: 0
701        Symbol: foo
702        Type:   R_X86_64_64
703Symbols:
704  - Name:    foo
705    Section: .text
706    Type:    STT_OBJECT
707    Binding: STB_GLOBAL
708
709## Check that we report a warning when we find an unsupported relocation
710## in the section that contains the stack size entries' relocations.
711
712# RUN: yaml2obj --docnum=10 %s -o %t15
713# RUN: llvm-readelf --stack-sizes %t15 2>&1 | FileCheck %s --check-prefix=UNSUPPRELOC-GNU -DFILE=%t15
714# RUN: llvm-readobj --stack-sizes %t15 2>&1 | FileCheck %s --check-prefix=UNSUPPRELOC-LLVM -DFILE=%t15
715
716# UNSUPPRELOC-GNU:      Stack Sizes:
717# UNSUPPRELOC-GNU-NEXT:   Size     Function
718# UNSUPPRELOC-GNU-NEXT: warning: '[[FILE]]': SHT_RELA section with index 3 contains an unsupported relocation with index 0: R_X86_64_RELATIVE
719# UNSUPPRELOC-GNU-NEXT:      0     foo
720# UNSUPPRELOC-GNU-NEXT: warning: '[[FILE]]': SHT_RELA section with index 3 contains an unsupported relocation with index 2: R_X86_64_RELATIVE
721
722# UNSUPPRELOC-LLVM:      StackSizes [
723# UNSUPPRELOC-LLVM-NEXT: warning: '[[FILE]]': SHT_RELA section with index 3 contains an unsupported relocation with index 0: R_X86_64_RELATIVE
724# UNSUPPRELOC-LLVM-NEXT:   Entry {
725# UNSUPPRELOC-LLVM-NEXT:     Function: foo
726# UNSUPPRELOC-LLVM-NEXT:     Size: 0x0
727# UNSUPPRELOC-LLVM-NEXT:   }
728# UNSUPPRELOC-LLVM-NEXT: warning: '[[FILE]]': SHT_RELA section with index 3 contains an unsupported relocation with index 2: R_X86_64_RELATIVE
729# UNSUPPRELOC-LLVM-NEXT: ]
730
731--- !ELF
732FileHeader:
733  Class:   ELFCLASS64
734  Data:    ELFDATA2LSB
735  Type:    ET_REL
736  Machine: EM_X86_64
737Sections:
738  - Name: .text
739    Type: SHT_PROGBITS
740    Size: 8
741  - Name: .stack_sizes
742    Type: SHT_PROGBITS
743    Link: .text
744    Entries:
745      - Size: 0
746  - Name: .rela.stack_sizes
747    Type: SHT_RELA
748    Info: .stack_sizes
749    Relocations:
750      - Offset: 0
751        Symbol: foo
752        Type:   R_X86_64_RELATIVE
753      - Offset: 0
754        Symbol: foo
755        Type:   R_X86_64_64
756      - Offset: 0
757        Symbol: foo
758        Type:   R_X86_64_RELATIVE
759Symbols:
760  - Name:    foo
761    Section: .text
762    Type:    STT_FUNC
763    Binding: STB_GLOBAL
764
765## Check that warning messages in archives do not impact other members. In the following
766## test, the first archive member generates a warning and we make sure all the information
767## is still dumped.
768
769# RUN: llvm-ar rc %t2.a %t04 %t01
770# RUN: llvm-readelf --stack-sizes %t2.a 2>&1 | FileCheck %s --check-prefix=ARCHIVEWARN-GNU \
771# RUN:   -DFILE=%t2.a --strict-whitespace --match-full-lines
772# RUN: llvm-readobj --stack-sizes %t2.a 2>&1 | FileCheck %s --check-prefix=ARCHIVEWARN-LLVM -DFILE=%t2.a
773
774#      ARCHIVEWARN-GNU:File: [[FILE]]({{.*04}})
775#      ARCHIVEWARN-GNU:Stack Sizes:
776# ARCHIVEWARN-GNU-NEXT:         Size     Function
777#      ARCHIVEWARN-GNU:{{.*}}: warning: '{{.*04}}': relocation symbol '_Z3foof' is not in the expected section
778#      ARCHIVEWARN-GNU:            8     _Z3foof
779#      ARCHIVEWARN-GNU:File: [[FILE]]({{.*01}})
780#      ARCHIVEWARN-GNU:Stack Sizes:
781# ARCHIVEWARN-GNU-NEXT:         Size     Function
782# ARCHIVEWARN-GNU-NEXT:           16     referenced_by_symbol_foo
783# ARCHIVEWARN-GNU-NEXT:           32     referenced_via_section_bar
784# ARCHIVEWARN-GNU-NEXT:            8     separate_text_section_baz
785#  ARCHIVEWARN-GNU-NOT:{{.}}
786
787
788# ARCHIVEWARN-LLVM:      File: [[FILE]]({{.*04}})
789# ARCHIVEWARN-LLVM:      StackSizes [
790# ARCHIVEWARN-LLVM:      warning: '{{.*04}}': relocation symbol '_Z3foof' is not in the expected section
791# ARCHIVEWARN-LLVM-NEXT:   Entry {
792# ARCHIVEWARN-LLVM-NEXT:     Function: _Z3foof
793# ARCHIVEWARN-LLVM-NEXT:     Size: 0x8
794# ARCHIVEWARN-LLVM-NEXT:   }
795# ARCHIVEWARN-LLVM-NEXT: ]
796# ARCHIVEWARN-LLVM:      File: [[FILE]]({{.*01}})
797# ARCHIVEWARN-LLVM:      StackSizes [
798# ARCHIVEWARN-LLVM-NEXT:   Entry {
799# ARCHIVEWARN-LLVM-NEXT:     Function: referenced_by_symbol_foo
800# ARCHIVEWARN-LLVM-NEXT:     Size: 0x10
801# ARCHIVEWARN-LLVM-NEXT:   }
802# ARCHIVEWARN-LLVM-NEXT:   Entry {
803# ARCHIVEWARN-LLVM-NEXT:     Function: referenced_via_section_bar
804# ARCHIVEWARN-LLVM-NEXT:     Size: 0x20
805# ARCHIVEWARN-LLVM-NEXT:   }
806# ARCHIVEWARN-LLVM-NEXT:   Entry {
807# ARCHIVEWARN-LLVM-NEXT:     Function: separate_text_section_baz
808# ARCHIVEWARN-LLVM-NEXT:     Size: 0x8
809# ARCHIVEWARN-LLVM-NEXT:   }
810# ARCHIVEWARN-LLVM-NEXT: ]
811
812## Check that we demangle function names when requested.
813
814# RUN: yaml2obj --docnum=11 %s -o %t16
815# RUN: llvm-readelf --stack-sizes --demangle %t16 | FileCheck %s --check-prefix=DEMANGLE-GNU
816# RUN: llvm-readobj --stack-sizes --demangle %t16 | FileCheck %s --check-prefix=DEMANGLE-LLVM
817
818# DEMANGLE-GNU:  16 foo(float)
819# DEMANGLE-LLVM: Function: foo(float)
820
821--- !ELF
822FileHeader:
823  Class: ELFCLASS64
824  Data:  ELFDATA2LSB
825  Type:  ET_EXEC
826Sections:
827  - Name:    .text
828    Type:    SHT_PROGBITS
829    Flags:   [SHF_ALLOC]
830    Size:    16
831  - Name:    .stack_sizes
832    Type:    SHT_PROGBITS
833    Entries:
834      - Address: 0x10
835        Size:    0x10
836    Link:    .text
837Symbols:
838  - Name:    _Z3foof
839    Section: .text
840    Value:   0x10
841    Type:    STT_FUNC
842    Binding: STB_GLOBAL
843
844## Check that we report a warning when we are unable to resolve a relocation for a given ELF architecture.
845## Here we have a 64-bit relocation used in a 32-bit object.
846
847# RUN: yaml2obj --docnum=12 %s -o %t17
848# RUN: llvm-readelf --stack-sizes %t17 2>&1 | FileCheck %s -DFILE=%t17 --check-prefix=UNSUPPRELOC2
849# RUN: llvm-readobj --stack-sizes %t17 2>&1 | FileCheck %s -DFILE=%t17 --check-prefix=UNSUPPRELOC2
850
851# UNSUPPRELOC2: warning: '[[FILE]]': SHT_RELA section with index 2 contains an unsupported relocation with index 0: R_X86_64_64
852
853--- !ELF
854FileHeader:
855  Class:   ELFCLASS32
856  Data:    ELFDATA2MSB
857  Type:    ET_REL
858  Machine: EM_X86_64
859Sections:
860  - Name: .stack_sizes
861    Type: SHT_PROGBITS
862    Content: "00"
863  - Name: .rela.stack_sizes
864    Type: SHT_RELA
865    Info: .stack_sizes
866    Relocations:
867      - Offset: 0
868        Type:   R_X86_64_64
869
870## Check we report a warning when dumping stack sizes if the relocated section
871## identified by the sh_info field is invalid. Here the sh_info value is larger than
872## the number of sections.
873
874# RUN: yaml2obj --docnum=13 %s -o %t18
875# RUN: llvm-readelf --stack-sizes %t18 2>&1 | \
876# RUN:   FileCheck %s --implicit-check-not="warning:" -DFILE=%t18 --check-prefix=INVALID-TARGET
877# RUN: llvm-readobj --stack-sizes %t18 2>&1 | \
878# RUN:   FileCheck %s --implicit-check-not="warning:" -DFILE=%t18 --check-prefix=INVALID-TARGET
879
880# INVALID-TARGET: warning: '[[FILE]]': SHT_RELA section with index 1: failed to get a relocated section: invalid section index: 255
881# INVALID-TARGET: warning: '[[FILE]]': SHT_RELA section with index 2: failed to get a relocated section: invalid section index: 255
882
883--- !ELF
884FileHeader:
885  Class: ELFCLASS32
886  Data:  ELFDATA2MSB
887  Type:  ET_REL
888Sections:
889  - Name: .rela.stack_sizes
890    Type: SHT_RELA
891    Link: 0
892    Info: 0xFF
893    Relocations: []
894  - Name: .rela.stack_sizes (1)
895    Type: SHT_RELA
896    Link: 0
897    Info: 0xFF
898    Relocations: []
899