xref: /llvm-project/llvm/test/tools/llvm-nm/dynamic.test (revision 94828afd0a178d00abec87143b48fe0d7c063198)
1541bead8Svgxbj## This is a test for --dynamic/-D option.
2541bead8Svgxbj
3541bead8Svgxbj## Test llvm-nm dumping ELF file with valid .dynsym section.
4541bead8Svgxbj# RUN: yaml2obj --docnum=1 %s -o %t1.o
5541bead8Svgxbj# RUN: llvm-nm --dynamic %t1.o | \
6541bead8Svgxbj# RUN:   FileCheck %s --match-full-lines --strict-whitespace --check-prefix DYNSYM
7541bead8Svgxbj# RUN: llvm-nm -D %t1.o | \
8541bead8Svgxbj# RUN:   FileCheck %s --match-full-lines --strict-whitespace --check-prefix DYNSYM
9541bead8Svgxbj
10541bead8Svgxbj#       DYNSYM:                 U globalsym
11541bead8Svgxbj#  DYNSYM-NEXT:                 U localsym1
12541bead8Svgxbj#  DYNSYM-NEXT:0000000000000000 n localsym2
13541bead8Svgxbj# DYNSYM-EMPTY:
14541bead8Svgxbj
15541bead8Svgxbj--- !ELF
16541bead8SvgxbjFileHeader:
17541bead8Svgxbj  Class:   ELFCLASS64
18541bead8Svgxbj  Data:    ELFDATA2LSB
19541bead8Svgxbj  Type:    ET_DYN
20541bead8Svgxbj  Machine: EM_X86_64
21541bead8SvgxbjSections:
22541bead8Svgxbj  - Name: section
23541bead8Svgxbj    Type: SHT_PROGBITS
24541bead8SvgxbjDynamicSymbols:
25541bead8Svgxbj  - Name:    localsym1
26541bead8Svgxbj    Type:    STT_OBJECT
27541bead8Svgxbj  - Name:    localsym2
28541bead8Svgxbj    Section: section
29541bead8Svgxbj  - Name:    globalsym
30541bead8Svgxbj    Type:    STT_OBJECT
31541bead8Svgxbj    Binding: STB_GLOBAL
32541bead8Svgxbj
33541bead8Svgxbj## Test llvm-nm dumping ELF file without a .dynsym section.
34541bead8Svgxbj# RUN: yaml2obj --docnum=2 %s -o %t2.o
35541bead8Svgxbj# RUN: llvm-nm --dynamic %t2.o 2>&1 | \
36541bead8Svgxbj# RUN:   FileCheck %s --match-full-lines --strict-whitespace -DFILE=%t2.o --check-prefix NO-SYMS
37541bead8Svgxbj
38541bead8Svgxbj#       NO-SYMS:[[FILE]]: no symbols
39541bead8Svgxbj# NO-SYMS-EMPTY:
40541bead8Svgxbj
41541bead8Svgxbj--- !ELF
42541bead8SvgxbjFileHeader:
43541bead8Svgxbj  Class:   ELFCLASS64
44541bead8Svgxbj  Data:    ELFDATA2LSB
45541bead8Svgxbj  Type:    ET_DYN
46541bead8Svgxbj  Machine: EM_X86_64
47541bead8Svgxbj
48541bead8Svgxbj## Test llvm-nm dumping ELF file with an empty .dynsym section.
49541bead8Svgxbj# RUN: yaml2obj --docnum=3 %s -o %t3.o
50541bead8Svgxbj# RUN: llvm-nm --dynamic %t3.o 2>&1 | \
51541bead8Svgxbj# RUN:   FileCheck %s --match-full-lines --strict-whitespace -DFILE=%t3.o --check-prefix NO-SYMS
52541bead8Svgxbj
53541bead8Svgxbj--- !ELF
54541bead8SvgxbjFileHeader:
55541bead8Svgxbj  Class:   ELFCLASS64
56541bead8Svgxbj  Data:    ELFDATA2LSB
57541bead8Svgxbj  Type:    ET_DYN
58541bead8Svgxbj  Machine: EM_X86_64
59541bead8SvgxbjSections:
60541bead8Svgxbj  - Name: .dynsym
61541bead8Svgxbj    Type: SHT_DYNSYM
62541bead8Svgxbj    Size: 0
63dd5c9828SGeorgii Rymar
64dd5c9828SGeorgii Rymar## Check we print symbol versions, when they are available.
65db92d47cSGeorgii Rymar## A default version is one that is contained in the version table (SHT_GNU_versym)
66db92d47cSGeorgii Rymar## and only available for defined symbols. Check we use the "@@" prefix to print it.
67dd5c9828SGeorgii Rymar
68db92d47cSGeorgii Rymar# RUN: yaml2obj --docnum=4 %s -o %t4-undef.o
69db92d47cSGeorgii Rymar# RUN: llvm-nm --dynamic %t4-undef.o 2>&1 | \
70db92d47cSGeorgii Rymar# RUN:   FileCheck %s -DFILE=%t4-undef.o -DTYPE=U \
71db92d47cSGeorgii Rymar# RUN:     --check-prefixes=VERSIONED-SYMS,VERSIONED-UNDEF-SYMS
72dd5c9828SGeorgii Rymar
73db92d47cSGeorgii Rymar# RUN: yaml2obj --docnum=4 -DINDEX=0x1 %s -o %t4-def.o
74db92d47cSGeorgii Rymar# RUN: llvm-nm --dynamic %t4-def.o 2>&1 | \
75db92d47cSGeorgii Rymar# RUN:   FileCheck %s -DFILE=%t4-def.o -DTYPE=r \
76db92d47cSGeorgii Rymar# RUN:     --check-prefixes=VERSIONED-SYMS,VERSIONED-DEF-SYMS
77db92d47cSGeorgii Rymar
78db92d47cSGeorgii Rymar#            VERSIONED-SYMS: [[TYPE]] globalversym
79db92d47cSGeorgii Rymar#       VERSIONED-SYMS-NEXT: [[TYPE]] localversym
80db92d47cSGeorgii Rymar# VERSIONED-UNDEF-SYMS-NEXT: [[TYPE]] version2sym@v2
81db92d47cSGeorgii Rymar#   VERSIONED-DEF-SYMS-NEXT: [[TYPE]] version2sym@@v2
82db92d47cSGeorgii Rymar#       VERSIONED-SYMS-NEXT: [[TYPE]] version3sym@v3hidden
83db92d47cSGeorgii Rymar#       VERSIONED-SYMS-NEXT: [[TYPE]] version4sym@v4
84db92d47cSGeorgii Rymar#       VERSIONED-SYMS-NEXT: [[TYPE]] version5sym@v5hidden
85dd5c9828SGeorgii Rymar
86dd5c9828SGeorgii Rymar--- !ELF
87dd5c9828SGeorgii RymarFileHeader:
88dd5c9828SGeorgii Rymar  Class: ELFCLASS64
89dd5c9828SGeorgii Rymar  Data:  ELFDATA2LSB
90dd5c9828SGeorgii Rymar  Type:  ET_DYN
91dd5c9828SGeorgii RymarSections:
92dd5c9828SGeorgii Rymar  - Name:    .gnu.version
93dd5c9828SGeorgii Rymar    Type:    SHT_GNU_versym
94dd5c9828SGeorgii Rymar    Flags:   [ SHF_ALLOC ]
95dd5c9828SGeorgii Rymar## 0x8000 is a special VERSYM_HIDDEN bit.
96dd5c9828SGeorgii Rymar    Entries: [ 0, 0, 1, [[VERSYMENTRY=2]], 0x8003, 4, 0x8005 ]
97dd5c9828SGeorgii Rymar    ShSize:  [[VERSYMSIZE=<none>]]
98dd5c9828SGeorgii Rymar  - Name:         .gnu.version_d
99dd5c9828SGeorgii Rymar    Type:         SHT_GNU_verdef
100dd5c9828SGeorgii Rymar    Flags:        [ SHF_ALLOC ]
101dd5c9828SGeorgii Rymar    Link:         .dynstr
102dd5c9828SGeorgii Rymar    AddressAlign: 0x4
103dd5c9828SGeorgii Rymar    Info:         0x2
104dd5c9828SGeorgii Rymar    ShOffset:     [[VERDEFOFFSET=<none>]]
105dd5c9828SGeorgii Rymar    Entries:
106dd5c9828SGeorgii Rymar      - VersionNdx: 2
107dd5c9828SGeorgii Rymar        Names:
108dd5c9828SGeorgii Rymar          - v2
109dd5c9828SGeorgii Rymar      - VersionNdx: 3
110dd5c9828SGeorgii Rymar        Names:
111dd5c9828SGeorgii Rymar          - v3hidden
112dd5c9828SGeorgii Rymar  - Name:  .gnu.version_r
113dd5c9828SGeorgii Rymar    Type:  SHT_GNU_verneed
114dd5c9828SGeorgii Rymar    Flags: [ SHF_ALLOC ]
115dd5c9828SGeorgii Rymar    Link:  .dynstr
116dd5c9828SGeorgii Rymar    Info:  0x2
117dd5c9828SGeorgii Rymar    Dependencies:
118dd5c9828SGeorgii Rymar      - Version: 1
119dd5c9828SGeorgii Rymar        File:    file1.so
120dd5c9828SGeorgii Rymar        Entries:
121dd5c9828SGeorgii Rymar          - Name:  v4
122dd5c9828SGeorgii Rymar            Hash:  0
123dd5c9828SGeorgii Rymar            Flags: 0
124dd5c9828SGeorgii Rymar            Other: 4
125dd5c9828SGeorgii Rymar      - Version: 1
126dd5c9828SGeorgii Rymar        File:    file2.0
127dd5c9828SGeorgii Rymar        Entries:
128dd5c9828SGeorgii Rymar          - Name:  v5hidden
129dd5c9828SGeorgii Rymar            Hash:  0
130dd5c9828SGeorgii Rymar            Flags: 0
131dd5c9828SGeorgii Rymar            Other: 5
132db92d47cSGeorgii Rymar  - Name:    .dynsym
133db92d47cSGeorgii Rymar    Type:    SHT_DYNSYM
134db92d47cSGeorgii Rymar    EntSize: [[ENTSIZE=<none>]]
135dd5c9828SGeorgii RymarDynamicSymbols:
136dd5c9828SGeorgii Rymar  - Name:  localversym
137db92d47cSGeorgii Rymar    Index: [[INDEX=<none>]]
138dd5c9828SGeorgii Rymar  - Name:  globalversym
139db92d47cSGeorgii Rymar    Index: [[INDEX=<none>]]
140dd5c9828SGeorgii Rymar  - Name:  version2sym
141db92d47cSGeorgii Rymar    Index: [[INDEX=<none>]]
142dd5c9828SGeorgii Rymar  - Name:  version3sym
143db92d47cSGeorgii Rymar    Index: [[INDEX=<none>]]
144dd5c9828SGeorgii Rymar  - Name:  version4sym
145db92d47cSGeorgii Rymar    Index: [[INDEX=<none>]]
146dd5c9828SGeorgii Rymar  - Name:  version5sym
147db92d47cSGeorgii Rymar    Index: [[INDEX=<none>]]
148dd5c9828SGeorgii Rymar
149dd5c9828SGeorgii Rymar## In the following cases we check we report warnings when unable to read symbol version.
150dd5c9828SGeorgii Rymar## Check that we still print unversioned symbol names.
151dd5c9828SGeorgii Rymar
152dd5c9828SGeorgii Rymar## Case 1: check we report a warning when unable to read symbol versions
153dd5c9828SGeorgii Rymar## from a broken SHT_GNU_verdef section. In this case its sh_offset
154dd5c9828SGeorgii Rymar## field has a too large value that goes past the EOF.
155dd5c9828SGeorgii Rymar
156dd5c9828SGeorgii Rymar# RUN: yaml2obj --docnum=4 -DVERDEFOFFSET=0xffffffff %s -o %t4-broken-verdef.o
157dd5c9828SGeorgii Rymar# RUN: llvm-nm --dynamic %t4-broken-verdef.o 2>&1 | \
158db92d47cSGeorgii Rymar# RUN:   FileCheck %s --check-prefixes=VERSION-ERR,VERSION-ERR1
159dd5c9828SGeorgii Rymar
160dd5c9828SGeorgii Rymar# VERSION-ERR1: warning: unable to read symbol versions: cannot read content of SHT_GNU_verdef section with index 2: section [index 2] has a sh_offset (0xffffffff) + sh_size (0x38) that is greater than the file size (0x438)
161dd5c9828SGeorgii Rymar# VERSION-ERR2: warning: unable to read symbol versions: unable to read an entry with index 1 from SHT_GNU_versym section with index 1: section [index 1] has an invalid sh_size (255) which is not a multiple of its sh_entsize (2)
162dd5c9828SGeorgii Rymar# VERSION-ERR3: warning: unable to read symbol versions: unable to get a version for entry 3 of SHT_GNU_versym section with index 1: SHT_GNU_versym section refers to a version index 255 which is missing
163dd5c9828SGeorgii Rymar
164dd5c9828SGeorgii Rymar# VERSION-ERR-NEXT: U globalversym{{$}}
165dd5c9828SGeorgii Rymar# VERSION-ERR-NEXT: U localversym{{$}}
166dd5c9828SGeorgii Rymar# VERSION-ERR-NEXT: U version2sym{{$}}
167dd5c9828SGeorgii Rymar# VERSION-ERR-NEXT: U version3sym{{$}}
168dd5c9828SGeorgii Rymar# VERSION-ERR-NEXT: U version4sym{{$}}
169dd5c9828SGeorgii Rymar# VERSION-ERR-NEXT: U version5sym{{$}}
170dd5c9828SGeorgii Rymar
171dd5c9828SGeorgii Rymar## Case 2: check we report a warning when we are unable to read a SHT_GNU_versym section entry.
172dd5c9828SGeorgii Rymar## In this case, the section has a size that is not a multiple of its sh_entsize.
173dd5c9828SGeorgii Rymar
174dd5c9828SGeorgii Rymar# RUN: yaml2obj --docnum=4 -DVERSYMSIZE=0xff %s -o %t4-broken-versym.o
175dd5c9828SGeorgii Rymar# RUN: llvm-nm --dynamic %t4-broken-versym.o 2>&1 | \
176db92d47cSGeorgii Rymar# RUN:   FileCheck %s --check-prefixes=VERSION-ERR,VERSION-ERR2
177dd5c9828SGeorgii Rymar
178db92d47cSGeorgii Rymar## Case 3: check we report a warning when we are unable to get a version for a SHT_GNU_versym section entry.
179dd5c9828SGeorgii Rymar## In this case the SHT_GNU_versym section refers to a version index 255 which is missing.
180dd5c9828SGeorgii Rymar
181dd5c9828SGeorgii Rymar# RUN: yaml2obj --docnum=4 -DVERSYMENTRY=0xff %s -o %t4-broken-index.o
182dd5c9828SGeorgii Rymar# RUN: llvm-nm --dynamic %t4-broken-index.o 2>&1 | \
183db92d47cSGeorgii Rymar# RUN:   FileCheck %s --check-prefixes=VERSION-ERR,VERSION-ERR3
184db92d47cSGeorgii Rymar
185db92d47cSGeorgii Rymar## Case 4: check we report a warning when we are unable to get symbol flags.
186db92d47cSGeorgii Rymar## In this case the dynamic symbol table has a wrong sh_entsize and we can't read a symbol.
187db92d47cSGeorgii Rymar
188db92d47cSGeorgii Rymar# RUN: yaml2obj --docnum=4 -DENTSIZE=0xff %s -o %t4-broken-dynsym.o
189db92d47cSGeorgii Rymar# RUN: not llvm-nm --dynamic %t4-broken-dynsym.o 2>&1 | \
190db92d47cSGeorgii Rymar# RUN:   FileCheck %s -DFILE=%t4-broken-dynsym.o --check-prefix=VERSION-ERR4
191db92d47cSGeorgii Rymar
192db92d47cSGeorgii Rymar# VERSION-ERR4: warning: unable to read symbol versions: unable to read flags for symbol with index 1: section [index 4] has invalid sh_entsize: expected 24, but got 255
193*94828afdSJames Henderson# VERSION-ERR4: error: [[FILE]]: section [index 4] has invalid sh_entsize: expected 24, but got 255
194