xref: /llvm-project/llvm/test/tools/llvm-readobj/ELF/call-graph-profile.test (revision 9c645a99e4e7451c0073dddea74d52dac7f7e837)
1## This test checks how we handle the --elf-cg-profile option.
2
3# RUN: yaml2obj %s -o %t.o
4# RUN: llvm-readobj %t.o --cg-profile | FileCheck %s --check-prefix=LLVM
5# RUN: llvm-readelf %t.o --cg-profile | FileCheck %s --check-prefix=GNU
6# RUN: llvm-readobj %t.o --elf-cg-profile | FileCheck %s --check-prefix=LLVM
7# RUN: llvm-readelf %t.o --elf-cg-profile | FileCheck %s --check-prefix=GNU
8
9# LLVM:      CGProfile [
10# LLVM-NEXT:  CGProfileEntry {
11# LLVM-NEXT:    From: foo (1)
12# LLVM-NEXT:    To: bar (2)
13# LLVM-NEXT:    Weight: 89
14# LLVM-NEXT:  }
15# LLVM-NEXT:  CGProfileEntry {
16# LLVM-NEXT:    From: bar (2)
17# LLVM-NEXT:    To: foo (1)
18# LLVM-NEXT:    Weight: 98
19# LLVM-NEXT:  }
20# LLVM-NEXT: ]
21
22# GNU: GNUStyle::printCGProfile not implemented
23
24--- !ELF
25FileHeader:
26  Class: ELFCLASS64
27  Data:  ELFDATA2LSB
28  Type:  ET_DYN
29  Machine: EM_X86_64
30Sections:
31  - Name: .llvm.call-graph-profile
32    Type: SHT_LLVM_CALL_GRAPH_PROFILE
33    Entries:
34      - Weight: 89
35      - Weight: 98
36    EntSize: [[ENTSIZE=<none>]]
37  - Name: .rel.llvm.call-graph-profile
38    Type: SHT_REL
39    Info: .llvm.call-graph-profile
40    Relocations:
41      - Symbol: foo
42        Type:   R_X86_64_NONE
43      - Offset: 0x0
44        Symbol: bar
45        Type:   R_X86_64_NONE
46      - Offset: 0x8
47        Symbol: bar
48        Type:   R_X86_64_NONE
49      - Offset: 0x8
50        Symbol: foo
51        Type:   R_X86_64_NONE
52Symbols:
53  - Name: foo
54  - Name: bar
55
56## Check we report a warning when unable to get the content of the SHT_LLVM_CALL_GRAPH_PROFILE section.
57# RUN: yaml2obj %s -DENTSIZE=0xF -o %t2.o
58# RUN: llvm-readobj %t2.o --cg-profile 2>&1 | FileCheck %s -DFILE=%t2.o --check-prefix=LLVM-ERR
59# RUN: llvm-readelf %t2.o --cg-profile | FileCheck %s --check-prefix=GNU
60
61# LLVM-ERR: warning: '[[FILE]]': unable to load the SHT_LLVM_CALL_GRAPH_PROFILE section: section [index 1] has invalid sh_entsize: expected 8, but got 15
62
63## Check we report a warning when unable to dump a name of a symbol.
64# RUN: yaml2obj %s --docnum=2 -o %t3.o
65# RUN: llvm-readobj %t3.o --cg-profile 2>&1 | FileCheck %s -DFILE=%t3.o --check-prefix=LLVM-BROKEN-SYM
66# RUN: llvm-readelf %t3.o --cg-profile | FileCheck %s --check-prefix=GNU
67
68# LLVM-BROKEN-SYM:      CGProfile [
69# LLVM-BROKEN-SYM-NEXT:   CGProfileEntry {
70# LLVM-BROKEN-SYM-NEXT:     From: A (1)
71# LLVM-BROKEN-SYM-NEXT: warning: '[[FILE]]': unable to read the name of symbol with index 2: st_name (0xff) is past the end of the string table of size 0x5
72# LLVM-BROKEN-SYM-NEXT:     To: <?> (2)
73# LLVM-BROKEN-SYM-NEXT:     Weight: 10
74# LLVM-BROKEN-SYM-NEXT:   }
75# LLVM-BROKEN-SYM-NEXT:   CGProfileEntry {
76# LLVM-BROKEN-SYM-NEXT:     From: <?> (2)
77# LLVM-BROKEN-SYM-NEXT:     To: B (3)
78# LLVM-BROKEN-SYM-NEXT:     Weight: 20
79# LLVM-BROKEN-SYM-NEXT:   }
80# LLVM-BROKEN-SYM-NEXT:   CGProfileEntry {
81# LLVM-BROKEN-SYM-NEXT:     From: (0)
82# LLVM-BROKEN-SYM-NEXT: warning: '[[FILE]]': unable to read the name of symbol with index 4: unable to get symbol from section [index 4]: invalid symbol index (4)
83# LLVM-BROKEN-SYM-NEXT:     To: <?> (4)
84# LLVM-BROKEN-SYM-NEXT:     Weight: 20
85# LLVM-BROKEN-SYM-NEXT:   }
86# LLVM-BROKEN-SYM-NEXT: ]
87
88--- !ELF
89FileHeader:
90  Class: ELFCLASS64
91  Data:  ELFDATA2LSB
92  Type:  ET_DYN
93  Machine: EM_X86_64
94Sections:
95  - Name: .llvm.call-graph-profile
96    Type: SHT_LLVM_CALL_GRAPH_PROFILE
97    Entries:
98      - Weight: 10
99      - Weight: 20
100      - Weight: 20
101  - Name: .rel.llvm.call-graph-profile
102    Type: SHT_REL
103    Info: .llvm.call-graph-profile
104    Relocations:
105      - Symbol: 1
106        Type:   R_X86_64_NONE
107      - Offset: 0x0
108        Symbol: 2
109        Type:   R_X86_64_NONE
110      - Offset: 0x8
111        Symbol: 2
112        Type:   R_X86_64_NONE
113      - Offset: 0x8
114        Symbol: 3
115        Type:   R_X86_64_NONE
116      - Offset: 0x10
117        Symbol: 0x0 ## Null symbol.
118        Type:   R_X86_64_NONE
119      - Offset: 0x10
120        Symbol: 0x4 ## This index goes past the end of the symbol table.
121        Type:   R_X86_64_NONE
122  - Name:    .strtab
123    Type:    SHT_STRTAB
124    Content: "0041004200" ## '\0', 'A', '\0', 'B', '\0'
125Symbols:
126  - StName: 1    ## 'A'
127  - StName: 0xFF ## An arbitrary currupted index in the string table.
128  - StName: 3    ## 'B'
129
130## Check we report a warning when a relocation section is not present.
131# RUN: yaml2obj %s --docnum=3 -o %t4.o
132# RUN: llvm-readobj %t4.o --cg-profile 2>&1 | FileCheck %s -DFILE=%t4.o --check-prefix=LLVM-NO-RELOC
133# RUN: llvm-readobj %t4.o --elf-cg-profile 2>&1 | FileCheck %s -DFILE=%t4.o --check-prefix=LLVM-NO-RELOC
134
135# LLVM-NO-RELOC:      warning: '[[FILE]]': relocation section for a call graph section doesn't exist
136# LLVM-NO-RELOC-NEXT: CGProfile [
137# LLVM-NO-RELOC-NEXT:  CGProfileEntry {
138# LLVM-NO-RELOC-NEXT:    Weight: 89
139# LLVM-NO-RELOC-NEXT:  }
140# LLVM-NO-RELOC-NEXT:  CGProfileEntry {
141# LLVM-NO-RELOC-NEXT:    Weight: 98
142# LLVM-NO-RELOC-NEXT:  }
143# LLVM-NO-RELOC-NEXT: ]
144
145--- !ELF
146FileHeader:
147  Class: ELFCLASS64
148  Data:  ELFDATA2LSB
149  Type:  ET_DYN
150Sections:
151  - Name: .llvm.call-graph-profile
152    Type: SHT_LLVM_CALL_GRAPH_PROFILE
153    Entries:
154      - Weight: 89
155      - Weight: 98
156Symbols:
157  - Name: foo
158  - Name: bar
159
160## Check we report a warning when the number of relocation section entries does not match the number of call graph entries.
161# RUN: yaml2obj %s --docnum=4 -o %t5.o
162# RUN: llvm-readobj %t5.o --cg-profile 2>&1 | FileCheck %s -DFILE=%t5.o --check-prefix=LLVM-RELOC-GRAPH-NOT-MATCH
163# RUN: llvm-readobj %t5.o --elf-cg-profile 2>&1 | FileCheck %s -DFILE=%t5.o --check-prefix=LLVM-RELOC-GRAPH-NOT-MATCH
164
165# LLVM-RELOC-GRAPH-NOT-MATCH:      warning: '[[FILE]]': number of from/to pairs does not match number of frequencies
166# LLVM-RELOC-GRAPH-NOT-MATCH-NEXT: CGProfile [
167# LLVM-RELOC-GRAPH-NOT-MATCH-NEXT:  CGProfileEntry {
168# LLVM-RELOC-GRAPH-NOT-MATCH-NEXT:    Weight: 89
169# LLVM-RELOC-GRAPH-NOT-MATCH-NEXT:  }
170# LLVM-RELOC-GRAPH-NOT-MATCH-NEXT:  CGProfileEntry {
171# LLVM-RELOC-GRAPH-NOT-MATCH-NEXT:    Weight: 98
172# LLVM-RELOC-GRAPH-NOT-MATCH-NEXT:  }
173# LLVM-RELOC-GRAPH-NOT-MATCH-NEXT: ]
174
175--- !ELF
176FileHeader:
177  Class: ELFCLASS64
178  Data:  ELFDATA2LSB
179  Type:  ET_DYN
180  Machine: EM_X86_64
181Sections:
182  - Name: .llvm.call-graph-profile
183    Type: SHT_LLVM_CALL_GRAPH_PROFILE
184    Entries:
185      - Weight: 89
186      - Weight: 98
187  - Name: .rel.llvm.call-graph-profile
188    Type: SHT_REL
189    Info: .llvm.call-graph-profile
190    Relocations:
191      - Symbol: foo
192        Type:   R_X86_64_NONE
193      - Offset: 0x0
194        Symbol: bar
195        Type:   R_X86_64_NONE
196      - Offset: 0x8
197        Symbol: bar
198        Type:   R_X86_64_NONE
199      - Offset: 0x8
200        Symbol: foo
201        Type:   R_X86_64_NONE
202      - Offset: 0x10
203        Symbol: foo
204        Type:   R_X86_64_NONE
205Symbols:
206  - Name: foo
207  - Name: bar
208
209## Check we report a warning when a REL relocation section can't be loaded.
210# RUN: yaml2obj %s --docnum=5 -o %t6.o
211# RUN: llvm-readobj %t6.o --cg-profile 2>&1 | FileCheck %s -DFILE=%t6.o --check-prefix=LLVM-RELOC-WRONG-SIZE
212# RUN: llvm-readobj %t6.o --elf-cg-profile 2>&1 | FileCheck %s -DFILE=%t6.o --check-prefix=LLVM-RELOC-WRONG-SIZE
213
214# LLVM-RELOC-WRONG-SIZE:      warning: '[[FILE]]': unable to load relocations for SHT_LLVM_CALL_GRAPH_PROFILE section: section [index 2] has invalid sh_entsize: expected 16, but got 24
215# LLVM-RELOC-WRONG-SIZE-NEXT: CGProfile [
216# LLVM-RELOC-WRONG-SIZE-NEXT:  CGProfileEntry {
217# LLVM-RELOC-WRONG-SIZE-NEXT:    Weight: 89
218# LLVM-RELOC-WRONG-SIZE-NEXT:  }
219# LLVM-RELOC-WRONG-SIZE-NEXT:  CGProfileEntry {
220# LLVM-RELOC-WRONG-SIZE-NEXT:    Weight: 98
221# LLVM-RELOC-WRONG-SIZE-NEXT:  }
222# LLVM-RELOC-WRONG-SIZE-NEXT: ]
223
224--- !ELF
225FileHeader:
226  Class: ELFCLASS64
227  Data:  ELFDATA2LSB
228  Type:  ET_DYN
229  Machine: EM_X86_64
230Sections:
231  - Name: .llvm.call-graph-profile
232    Type: SHT_LLVM_CALL_GRAPH_PROFILE
233    Entries:
234      - Weight: 89
235      - Weight: 98
236  - Name: .rel.llvm.call-graph-profile
237    Type: SHT_REL
238    Info: .llvm.call-graph-profile
239    Relocations:
240      - Symbol: foo
241        Type:   R_X86_64_NONE
242      - Offset: 0x0
243        Symbol: bar
244        Type:   R_X86_64_NONE
245      - Offset: 0x8
246        Symbol: bar
247        Type:   R_X86_64_NONE
248      - Offset: 0x8
249        Symbol: foo
250        Type:   R_X86_64_NONE
251    EntSize: 24
252Symbols:
253  - Name: foo
254  - Name: bar
255
256## GNU strip may convert SHT_REL to SHT_RELA. Test we can handle SHT_RELA.
257# RUN: yaml2obj %s --docnum=6 -o %t7.o
258# RUN: llvm-readobj %t7.o --cg-profile | FileCheck %s --check-prefix=LLVM-RELA
259# RUN: llvm-readelf %t7.o --cg-profile | FileCheck %s --check-prefix=GNU-RELA
260
261# LLVM-RELA:      CGProfile [
262# LLVM-RELA-NEXT:  CGProfileEntry {
263# LLVM-RELA-NEXT:    From: foo (1)
264# LLVM-RELA-NEXT:    To: bar (2)
265# LLVM-RELA-NEXT:    Weight: 89
266# LLVM-RELA-NEXT:  }
267# LLVM-RELA-NEXT:  CGProfileEntry {
268# LLVM-RELA-NEXT:    From: bar (2)
269# LLVM-RELA-NEXT:    To: foo (1)
270# LLVM-RELA-NEXT:    Weight: 98
271# LLVM-RELA-NEXT:  }
272# LLVM-RELA-NEXT: ]
273
274# GNU-RELA: GNUStyle::printCGProfile not implemented
275
276--- !ELF
277FileHeader:
278  Class: ELFCLASS64
279  Data:  ELFDATA2LSB
280  Type:  ET_DYN
281  Machine: EM_X86_64
282Sections:
283  - Name: .llvm.call-graph-profile
284    Type: SHT_LLVM_CALL_GRAPH_PROFILE
285    Entries:
286      - Weight: 89
287      - Weight: 98
288  - Name: .rela.llvm.call-graph-profile
289    Type: SHT_RELA
290    Info: .llvm.call-graph-profile
291    Relocations:
292      - Symbol: foo
293        Type:   R_X86_64_NONE
294      - Offset: 0x0
295        Symbol: bar
296        Type:   R_X86_64_NONE
297      - Offset: 0x8
298        Symbol: bar
299        Type:   R_X86_64_NONE
300      - Offset: 0x8
301        Symbol: foo
302        Type:   R_X86_64_NONE
303Symbols:
304  - Name: foo
305  - Name: bar
306
307## Check we report a warning when a RELA relocation section can't be loaded.
308# RUN: yaml2obj %s --docnum=7 -o %t8.o
309# RUN: llvm-readobj %t8.o --cg-profile 2>&1 | FileCheck %s -DFILE=%t8.o --check-prefix=LLVM-RELOC-WRONG-SIZE-RELA
310# RUN: llvm-readobj %t8.o --elf-cg-profile 2>&1 | FileCheck %s -DFILE=%t8.o --check-prefix=LLVM-RELOC-WRONG-SIZE-RELA
311
312# LLVM-RELOC-WRONG-SIZE-RELA:      warning: '[[FILE]]': unable to load relocations for SHT_LLVM_CALL_GRAPH_PROFILE section: section [index 2] has invalid sh_entsize: expected 24, but got 16
313# LLVM-RELOC-WRONG-SIZE-RELA-NEXT: CGProfile [
314# LLVM-RELOC-WRONG-SIZE-RELA-NEXT:  CGProfileEntry {
315# LLVM-RELOC-WRONG-SIZE-RELA-NEXT:    Weight: 89
316# LLVM-RELOC-WRONG-SIZE-RELA-NEXT:  }
317# LLVM-RELOC-WRONG-SIZE-RELA-NEXT:  CGProfileEntry {
318# LLVM-RELOC-WRONG-SIZE-RELA-NEXT:    Weight: 98
319# LLVM-RELOC-WRONG-SIZE-RELA-NEXT:  }
320# LLVM-RELOC-WRONG-SIZE-RELA-NEXT: ]
321
322--- !ELF
323FileHeader:
324  Class: ELFCLASS64
325  Data:  ELFDATA2LSB
326  Type:  ET_DYN
327  Machine: EM_X86_64
328Sections:
329  - Name: .llvm.call-graph-profile
330    Type: SHT_LLVM_CALL_GRAPH_PROFILE
331    Entries:
332      - Weight: 89
333      - Weight: 98
334  - Name: .rela.llvm.call-graph-profile
335    Type: SHT_RELA
336    Info: .llvm.call-graph-profile
337    Relocations:
338      - Symbol: foo
339        Type:   R_X86_64_NONE
340      - Offset: 0x0
341        Symbol: bar
342        Type:   R_X86_64_NONE
343      - Offset: 0x8
344        Symbol: bar
345        Type:   R_X86_64_NONE
346      - Offset: 0x8
347        Symbol: foo
348        Type:   R_X86_64_NONE
349    EntSize: 16
350Symbols:
351  - Name: foo
352  - Name: bar
353
354## Check that we report a warning when we fail to get a section associated with
355## a relocation section.
356
357# RUN: yaml2obj %s --docnum=8 -o %t9.o
358# RUN: llvm-readobj %t9.o --cg-profile 2>&1 | FileCheck %s -DFILE=%t9.o --check-prefix=LLVM-RELOC-NO-SECTIONS
359# RUN: llvm-readobj %t9.o --elf-cg-profile 2>&1 | FileCheck %s -DFILE=%t9.o --check-prefix=LLVM-RELOC-NO-SECTIONS
360
361# LLVM-RELOC-NO-SECTIONS: warning: '[[FILE]]': unable to get CG Profile section(s): SHT_RELA section with index 1: failed to get a relocated section: invalid section index: 255
362
363--- !ELF
364FileHeader:
365  Class: ELFCLASS64
366  Data:  ELFDATA2LSB
367  Type:  ET_DYN
368  Machine: EM_X86_64
369Sections:
370  - Name: .rela.llvm.call-graph-profile
371    Type: SHT_RELA
372    Info: 0xFF
373