xref: /llvm-project/llvm/test/tools/llvm-readobj/ELF/symbols.test (revision eb81493e95c9f0e7feecd699758caa56b7d3514e)
1## This is a general test for --symbols option and its aliases.
2## There are also other symbols* tests that check specific things.
3
4## For testing and behavior documenting purposes we are using ET_REL and ET_DYN inputs.
5## Relocatable ELF objects are very different by nature from executables and dynamic libraries.
6## They might never have dynamic symbols normally, for example, though we still want
7## to document the --dyn-symbols behavior.
8## There is no need to check ET_EXEC inputs because for dumpers there is no difference
9## between them and ET_DYN inputs in the context of this test.
10
11## Case 1: Test how llvm-readobj and llvm-readelf dumps symbols.
12## a) Check relocatable objects.
13# RUN: yaml2obj %s -DBITS=64 -DTYPE=ET_REL -o %t64
14# RUN: llvm-readobj --symbols %t64 | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-LLVM
15# 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
17# RUN: yaml2obj %s -DBITS=32 -DTYPE=ET_REL -o %t32
18# RUN: llvm-readobj --symbols %t32 | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-LLVM
19# 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
21## b) Check dynamic objects.
22# RUN: yaml2obj %s -DBITS=64 -DTYPE=ET_DYN -o %t64.so
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
25# 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 -
28# RUN: yaml2obj %s -DBITS=32 -DTYPE=ET_DYN -o %t32.so
29# RUN: llvm-readobj --symbols %t32.so | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-LLVM
30# RUN: llvm-readelf --symbols %t32.so | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=SYMBOLS-GNU32
31
32#      SYMBOLS-LLVM:Symbols [
33# SYMBOLS-LLVM-NEXT:  Symbol {
34# SYMBOLS-LLVM-NEXT:    Name:  (0)
35# SYMBOLS-LLVM-NEXT:    Value: 0x0
36# SYMBOLS-LLVM-NEXT:    Size: 0
37# SYMBOLS-LLVM-NEXT:    Binding: Local (0x0)
38# SYMBOLS-LLVM-NEXT:    Type: None (0x0)
39# SYMBOLS-LLVM-NEXT:    Other: 0
40# SYMBOLS-LLVM-NEXT:    Section: Undefined (0x0)
41# SYMBOLS-LLVM-NEXT:  }
42# SYMBOLS-LLVM-NEXT:  Symbol {
43# SYMBOLS-LLVM-NEXT:    Name: foo (5)
44# SYMBOLS-LLVM-NEXT:    Value: 0x1
45# SYMBOLS-LLVM-NEXT:    Size: 0
46# SYMBOLS-LLVM-NEXT:    Binding: Local (0x0)
47# SYMBOLS-LLVM-NEXT:    Type: None (0x0)
48# SYMBOLS-LLVM-NEXT:    Other: 0
49# SYMBOLS-LLVM-NEXT:    Section: .text (0x1)
50# SYMBOLS-LLVM-NEXT:  }
51# SYMBOLS-LLVM-NEXT:  Symbol {
52# SYMBOLS-LLVM-NEXT:    Name: bar (1)
53# SYMBOLS-LLVM-NEXT:    Value: 0x2
54# SYMBOLS-LLVM-NEXT:    Size: 0
55# SYMBOLS-LLVM-NEXT:    Binding: Local (0x0)
56# SYMBOLS-LLVM-NEXT:    Type: None (0x0)
57# SYMBOLS-LLVM-NEXT:    Other: 0
58# SYMBOLS-LLVM-NEXT:    Section: Undefined (0x0)
59# 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:  }
69# SYMBOLS-LLVM-NEXT:]
70
71#      SYMBOLS-GNU64:Symbol table '.symtab' contains 4 entries:
72# SYMBOLS-GNU64-NEXT:   Num:    Value          Size Type    Bind   Vis       Ndx Name
73# SYMBOLS-GNU64-NEXT:     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND
74# SYMBOLS-GNU64-NEXT:     1: 0000000000000001     0 NOTYPE  LOCAL  DEFAULT     1 foo
75# 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
84
85#      SYMBOLS-GNU32:Symbol table '.symtab' contains 4 entries:
86# SYMBOLS-GNU32-NEXT:   Num:    Value  Size Type    Bind   Vis       Ndx Name
87# SYMBOLS-GNU32-NEXT:     0: 00000000     0 NOTYPE  LOCAL  DEFAULT   UND
88# SYMBOLS-GNU32-NEXT:     1: 00000001     0 NOTYPE  LOCAL  DEFAULT     1 foo
89# 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
98
99## Case 2: Check flag aliases produce identical output
100# RUN: llvm-readobj --symbols %t64 > %t.symbols
101# RUN: llvm-readobj --syms %t64 > %t.syms
102# RUN: cmp %t.symbols %t.syms
103# RUN: llvm-readobj -s %t64 | diff %t.symbols -
104# RUN: llvm-readelf -s --elf-output-style=LLVM %t64 | diff %t.symbols -
105
106# RUN: llvm-readelf --symbols %t64 > %t.symbols.gnu
107# RUN: llvm-readelf --syms %t64 > %t.syms.gnu
108# RUN: cmp %t.symbols.gnu %t.syms.gnu
109# RUN: llvm-readelf -s %t64 | diff %t.symbols.gnu -
110
111## Case 3: Test that both regular and dynamic symbols are dumped when `--symbols` and `--dyn-symbols`
112##         are specified together. Note that the order is different for different styles.
113# RUN: llvm-readobj --symbols --dyn-symbols %t64 | FileCheck %s --check-prefix=BOTH-LLVM
114# RUN: llvm-readelf --symbols --dyn-symbols %t64 | FileCheck %s --check-prefix=BOTH-GNU
115
116# BOTH-LLVM: Symbols [
117# BOTH-LLVM: DynamicSymbols [
118
119# BOTH-GNU: Symbol table '.dynsym'
120# BOTH-GNU: Symbol table '.symtab'
121
122## Case 4: --symbols implies --dyn-symbols for llvm-readelf.
123# RUN: llvm-readobj --symbols %t64 | FileCheck %s --implicit-check-not="DynamicSymbols ["
124# RUN: llvm-readelf --symbols %t64 | FileCheck %s --check-prefix=BOTH-GNU
125
126## Case 5: Test that `--dyn-symbols` does not trigger `--symbols` implicitly.
127# RUN: llvm-readobj --dyn-symbols %t64 | FileCheck %s --implicit-check-not="{{^}}Symbols ["
128# RUN: llvm-readelf --dyn-symbols %t64 | FileCheck %s --implicit-check-not="Symbol table '.symtab'"
129
130## Case 6: Test that the Num index starts from zero at every new symbol table.
131# RUN: llvm-readelf --symbols %t64 %t64 | FileCheck %s --check-prefix=NUM-INDEX
132
133# NUM-INDEX:      Symbol table '.symtab' contains 4 entries:
134# NUM-INDEX-NEXT:    Num: {{.*}}
135# NUM-INDEX-NEXT:      0: {{.*}}
136# NUM-INDEX:      Symbol table '.symtab' contains 4 entries:
137# NUM-INDEX-NEXT:    Num: {{.*}}
138# NUM-INDEX-NEXT:      0: {{.*}}
139
140--- !ELF
141FileHeader:
142  Class: ELFCLASS[[BITS]]
143  Data:  ELFDATA2LSB
144  Type:  [[TYPE]]
145Sections:
146  - Name:  .text
147    Type: SHT_PROGBITS
148  - Name:  .data
149    Type: SHT_PROGBITS
150Symbols:
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
160DynamicSymbols:
161  - Name:  zed
162
163## Check the behavior when we are unable to print symbols due to an error.
164
165## Case 1: check we are able to dump symbols even when can't get the string table for the
166## SHT_SYMTAB section because of invalid sh_link value.
167# RUN: yaml2obj --docnum=2 -DLINK=0xff %s -o %t64.err1
168# RUN: llvm-readobj --symbols %t64.err1 2>&1 | \
169# RUN:   FileCheck %s -DFILE=%t64.err1 --check-prefix=STRTAB-LINK-ERR-LLVM
170# RUN: llvm-readelf --symbols %t64.err1 2>&1 | \
171# RUN:   FileCheck %s -DFILE=%t64.err1 --check-prefix=STRTAB-LINK-ERR-GNU
172
173# STRTAB-LINK-ERR-LLVM:      Symbols [
174# STRTAB-LINK-ERR-LLVM-NEXT: warning: '[[FILE]]': unable to get the string table for the SHT_SYMTAB section: invalid section index: 255
175# STRTAB-LINK-ERR-LLVM-NEXT:   Symbol {
176# STRTAB-LINK-ERR-LLVM-NEXT:     Name: <?> (0)
177# STRTAB-LINK-ERR-LLVM-NEXT:     Value: 0x0
178# STRTAB-LINK-ERR-LLVM-NEXT:     Size: 0
179# STRTAB-LINK-ERR-LLVM-NEXT:     Binding: Local (0x0)
180# STRTAB-LINK-ERR-LLVM-NEXT:     Type: None (0x0)
181# STRTAB-LINK-ERR-LLVM-NEXT:     Other: 0
182# STRTAB-LINK-ERR-LLVM-NEXT:     Section: Undefined (0x0)
183# STRTAB-LINK-ERR-LLVM-NEXT:   }
184# STRTAB-LINK-ERR-LLVM-NEXT:   Symbol {
185# STRTAB-LINK-ERR-LLVM-NEXT:     Name: <?> (1)
186# STRTAB-LINK-ERR-LLVM-NEXT:     Value: 0x1
187# STRTAB-LINK-ERR-LLVM-NEXT:     Size: 0
188# STRTAB-LINK-ERR-LLVM-NEXT:     Binding: Global (0x1)
189# STRTAB-LINK-ERR-LLVM-NEXT:     Type: None (0x0)
190# STRTAB-LINK-ERR-LLVM-NEXT:     Other: 0
191# STRTAB-LINK-ERR-LLVM-NEXT:     Section: .symtab (0x1)
192# STRTAB-LINK-ERR-LLVM-NEXT:   }
193# STRTAB-LINK-ERR-LLVM-NEXT: ]
194
195# STRTAB-LINK-ERR-GNU:      warning: '[[FILE]]': unable to get the string table for the SHT_SYMTAB section: invalid section index: 255
196# STRTAB-LINK-ERR-GNU:      Symbol table '.symtab' contains 2 entries:
197# STRTAB-LINK-ERR-GNU-NEXT:    Num:    Value          Size Type    Bind   Vis       Ndx Name
198# STRTAB-LINK-ERR-GNU-NEXT:      0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND <?>
199# STRTAB-LINK-ERR-GNU-NEXT:      1: 0000000000000001     0 NOTYPE  GLOBAL DEFAULT     1 <?>
200
201--- !ELF
202FileHeader:
203  Class:     ELFCLASS64
204  Data:      ELFDATA2LSB
205  Type:      ET_REL
206  EShStrNdx: [[SHSTRTAB=2]]
207Sections:
208  - Name:    .symtab
209    Type:    SHT_SYMTAB
210    Link:    [[LINK=.strtab]]
211    EntSize: [[ENTSIZE=0x18]]
212## Define the .shstrtab section to reveal its position.
213  - Name: .shstrtab
214    Type: SHT_STRTAB
215Symbols:
216  - Name: foo
217    Value: 0x1
218    Binding: STB_GLOBAL
219    Index: 1
220
221## Case 2: check we report a warning when we are unable to read symbols
222##         from the the SHT_SYMTAB section.
223# RUN: yaml2obj --docnum=2 -DENTSIZE=0xFF %s -o %t64.err2
224# RUN: llvm-readobj --symbols %t64.err2 2>&1 | \
225# RUN:  FileCheck %s -DFILE=%t64.err2 --check-prefix=SYMTAB-ENTSIZE-ERR-LLVM
226# RUN: llvm-readelf --symbols %t64.err2 2>&1 | \
227# RUN:  FileCheck %s -DFILE=%t64.err2 --check-prefix=SYMTAB-ENTSIZE-ERR-GNU
228
229# SYMTAB-ENTSIZE-ERR-LLVM:      Symbols [
230# SYMTAB-ENTSIZE-ERR-LLVM-NEXT:   warning: '[[FILE]]': unable to read symbols from the SHT_SYMTAB section: section [index 1] has invalid sh_entsize: expected 24, but got 255
231# SYMTAB-ENTSIZE-ERR-LLVM:      ]
232
233# SYMTAB-ENTSIZE-ERR-GNU:     warning: '[[FILE]]': unable to read symbols from the SHT_SYMTAB section: section [index 1] has invalid sh_entsize: expected 24, but got 255
234# SYMTAB-ENTSIZE-ERR-GNU-NOT: {{.}}
235
236## Case 3: check we report a warning, but continue dumping, when we are unable to read the name of the SHT_SYMTAB section.
237##         In this case we set the e_shstrndx field to an invalid index so that the .shstrtab section can't be located.
238# RUN: yaml2obj --docnum=2 -DSHSTRTAB=0xff %s -o %t64.err3
239# RUN: llvm-readobj --symbols %t64.err3 2>&1 | \
240# RUN:  FileCheck %s -DFILE=%t64.err3 --check-prefix=SYMTAB-SHSTRTAB-ERR-LLVM --implicit-check-not=warning:
241# RUN: llvm-readelf --symbols %t64.err3 2>&1 | \
242# RUN:  FileCheck %s -DFILE=%t64.err3 --check-prefix=SYMTAB-SHSTRTAB-ERR-GNU --implicit-check-not=warning:
243
244# SYMTAB-SHSTRTAB-ERR-LLVM:      Symbols [
245# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:   Symbol {
246# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Name:  (0)
247# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Value: 0x0
248# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Size: 0
249# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Binding: Local (0x0)
250# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Type: None (0x0)
251# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Other: 0
252# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Section: Undefined (0x0)
253# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:   }
254# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:   Symbol {
255# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Name: foo (1)
256# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Value: 0x1
257# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Size: 0
258# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Binding: Global (0x1)
259# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Type: None (0x0)
260# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Other: 0
261# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT: warning: '[[FILE]]': section header string table index 255 does not exist
262# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:     Section: <?> (0x1)
263# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT:   }
264# SYMTAB-SHSTRTAB-ERR-LLVM-NEXT: ]
265
266# SYMTAB-SHSTRTAB-ERR-GNU:      warning: '[[FILE]]': unable to get the name of SHT_SYMTAB section with index 1: section header string table index 255 does not exist
267# SYMTAB-SHSTRTAB-ERR-GNU:      Symbol table '<?>' contains 2 entries:
268# SYMTAB-SHSTRTAB-ERR-GNU-NEXT:    Num:    Value          Size Type    Bind   Vis       Ndx Name
269# SYMTAB-SHSTRTAB-ERR-GNU-NEXT:      0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND
270# SYMTAB-SHSTRTAB-ERR-GNU-NEXT:      1: 0000000000000001     0 NOTYPE  GLOBAL DEFAULT     1 foo
271
272## Test a symbol defined relative to a section with an invalid name.
273# RUN: yaml2obj --docnum=3 %s -o %t64.err4
274# 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
282
283--- !ELF
284FileHeader:
285  Class:   ELFCLASS32
286  Data:    ELFDATA2LSB
287  Type:    ET_REL
288  Machine: EM_X86_64
289Sections:
290  - Name:   .invalid
291    Type:   SHT_PROGBITS
292    ShName: 0xffff
293Symbols:
294  - Name:    foo
295    Section: .invalid
296