1## Check that yaml2obj is able to produce output
2## when a symbol with section index SHN_XINDEX is used,
3## but no SHT_SYMTAB_SHNDX section is defined.
4
5# RUN: yaml2obj --docnum=1 %s -o %t1
6# RUN: llvm-readobj --symbols 2>&1 %t1 | FileCheck -DFILE=%t1 %s --check-prefix=CASE1
7
8# CASE1: warning: '[[FILE]]': extended symbol index (1) is past the end of the SHT_SYMTAB_SHNDX section of size 0
9
10--- !ELF
11FileHeader:
12  Class: ELFCLASS64
13  Data:  ELFDATA2LSB
14  Type:  ET_REL
15Sections:
16  - Name: bar
17    Type: SHT_PROGBITS
18Symbols:
19  - Type:  STT_SECTION
20    Index: SHN_XINDEX
21
22## Check that yaml2obj keeps the SHT_SYMTAB_SHNDX section in the output
23## even when symbol's section index value is low enough to not require the extended symtab.
24## Also, check that symbols in .symtab still have the SHN_XINDEX index.
25
26# RUN: yaml2obj --docnum=2 %s -o %t2
27# RUN: llvm-readobj --sections --symbols --section-data 2>&1 %t2 | FileCheck %s --check-prefix=CASE2
28
29# CASE2:      Section {
30# CASE2:       Name: .symtab_shndx (1)
31# CASE2-NEXT:  Type: SHT_SYMTAB_SHNDX (0x12)
32
33# CASE2:      Name: .symtab
34# CASE2:      SectionData (
35# CASE2-NEXT:   0000: 00000000 00000000 00000000 00000000
36# CASE2-NEXT:   0010: 00000000 00000000 00000000 0300FFFF
37##                                                   ^-- 0xFFFF here is a SHN_XINDEX.
38# CASE2-NEXT:   0020: 00000000 00000000 00000000 00000000
39# CASE2-NEXT: )
40
41# CASE2:      Symbol {
42# CASE2:        Name: bar (0)
43# CASE2-NEXT:   Value: 0x0
44# CASE2-NEXT:   Size: 0
45# CASE2-NEXT:   Binding: Local (0x0)
46# CASE2-NEXT:   Type: Section (0x3)
47# CASE2-NEXT:   Other: 0
48# CASE2-NEXT:   Section: bar (0x1)
49# CASE2-NEXT: }
50
51--- !ELF
52FileHeader:
53  Class: ELFCLASS64
54  Data:  ELFDATA2LSB
55  Type:  ET_REL
56Sections:
57  - Name: bar
58    Type: SHT_PROGBITS
59  - Name:    .symtab_shndx
60    Type:    SHT_SYMTAB_SHNDX
61    Entries: [ 0, 1 ]
62    Link:    .symtab
63Symbols:
64  - Type:  STT_SECTION
65    Index: SHN_XINDEX
66
67## Check that yaml2obj allows producing broken SHT_SYMTAB_SHNDX section
68## content (in the case below it contains 0xff as an index of a section,
69## which is larger than the total number of sections in the file).
70
71# RUN: yaml2obj --docnum=3 %s -o %t3
72# RUN: llvm-readobj --symbols 2>&1 %t3 | FileCheck %s -DFILE=%t3 --check-prefix=CASE3
73
74# CASE3: warning: '[[FILE]]': invalid section index: 255
75
76--- !ELF
77FileHeader:
78  Class: ELFCLASS64
79  Data:  ELFDATA2LSB
80  Type:  ET_REL
81Sections:
82  - Name: bar
83    Type: SHT_PROGBITS
84  - Name:    .symtab_shndx
85    Type:    SHT_SYMTAB_SHNDX
86    Entries: [ 0, 255 ]
87    Link:    .symtab
88Symbols:
89  - Type:  STT_SECTION
90    Index: SHN_XINDEX
91
92## Check that yaml2obj reports an error if a symbol index does not fit into 2 bytes.
93
94# RUN: not yaml2obj --docnum=4 %s -o %t4 2>&1 | FileCheck %s --check-prefix=CASE4
95
96# CASE4: error: out of range hex16 number
97
98--- !ELF
99FileHeader:
100  Class: ELFCLASS64
101  Data:  ELFDATA2LSB
102  Type:  ET_REL
103Symbols:
104  - Type:  STT_SECTION
105    Index: 65536
106
107## Check we can set a custom sh_entsize for SHT_SYMTAB_SHNDX section.
108
109# RUN: yaml2obj --docnum=5 %s -o %t5
110# RUN: llvm-readelf -S 2>&1 %t5 | FileCheck %s -DFILE=%t5 --check-prefix=CASE5
111
112# CASE5: warning: '[[FILE]]': section [index 1] has an invalid sh_entsize: 2
113
114--- !ELF
115FileHeader:
116  Class: ELFCLASS64
117  Data:  ELFDATA2LSB
118  Type:  ET_REL
119Sections:
120  - Name:    .symtab_shndx
121    Type:    SHT_SYMTAB_SHNDX
122    Entries: [ 0 ]
123    EntSize: 2
124
125## Check we can use the "Content" key with the "Size" key when the size is greater
126## than or equal to the content size.
127
128# RUN: not yaml2obj --docnum=6 -DSIZE=1 -DCONTENT="'0011'" %s 2>&1 | \
129# RUN:   FileCheck %s --check-prefix=CONTENT-SIZE-ERR
130
131# CONTENT-SIZE-ERR: error: Section size must be greater than or equal to the content size
132
133--- !ELF
134FileHeader:
135  Class: ELFCLASS64
136  Data:  ELFDATA2LSB
137  Type:  ET_DYN
138Sections:
139  - Name:    .symtab_shndx
140    Type:    SHT_SYMTAB_SHNDX
141    Link:    .symtab
142    Size:    [[SIZE=<none>]]
143    Content: [[CONTENT=<none>]]
144    Entries: [[ENTRIES=<none>]]
145  - Name: .symtab
146    Type: SHT_SYMTAB
147## llvm-readobj validates that SHT_SYMTAB_SHNDX section has the same number
148## of entries that the symbol table associated has.
149    Size: [[SYMTAB=0x24]]
150
151# RUN: yaml2obj --docnum=6 -DSIZE=4 -DCONTENT="'00112233'" %s -o %t.cont.size.eq.o
152# RUN: llvm-readobj --sections --section-data %t.cont.size.eq.o | \
153# RUN:   FileCheck %s --check-prefix=CHECK-CONTENT -DDATA="00112233"
154
155# RUN: yaml2obj --docnum=6 -DSIZE=8 -DSYMTAB=0x36 -DCONTENT="'00112233'" %s -o %t.cont.size.gr.o
156# RUN: llvm-readobj --sections --section-data %t.cont.size.gr.o | \
157# RUN:   FileCheck %s --check-prefix=CHECK-CONTENT -DDATA="00112233 00000000"
158
159# CHECK-CONTENT:      Name: .symtab_shndx
160# CHECK-CONTENT:      SectionData (
161# CHECK-CONTENT-NEXT:   0000: [[DATA]] |
162# CHECK-CONTENT-NEXT: )
163
164## Check we can use the "Size" key alone to create the section.
165
166# RUN: yaml2obj --docnum=6 -DSIZE=4 %s -o %t.size.o
167# RUN: llvm-readobj --sections --section-data %t.size.o | \
168# RUN:   FileCheck %s --check-prefix=CHECK-CONTENT -DDATA="00000000"
169
170## Check we can use the "Content" key alone to create the section.
171
172# RUN: yaml2obj --docnum=6 -DCONTENT="'11223344'" %s -o %t.content.o
173# RUN: llvm-readobj --sections --section-data %t.content.o | \
174# RUN:   FileCheck %s --check-prefix=CHECK-CONTENT -DDATA="11223344"
175
176## Check we can't use the "Entries" key together with the "Content" or "Size" keys.
177
178# RUN: not yaml2obj --docnum=6 -DSIZE=0 -DENTRIES="[]" %s 2>&1 | \
179# RUN:   FileCheck %s --check-prefix=ENTRIES-ERR
180# RUN: not yaml2obj --docnum=6 -DCONTENT="'00'" -DENTRIES="[]" %s 2>&1 | \
181# RUN:   FileCheck %s --check-prefix=ENTRIES-ERR
182
183# ENTRIES-ERR: error: "Entries" cannot be used with "Content" or "Size"
184
185## Check we create an empty section when none of "Size", "Entries" or "Content" are specified.
186
187# RUN: yaml2obj --docnum=6 -DSYMTAB=0x0 %s -o %t.empty
188# RUN: llvm-readelf --sections %t.empty | FileCheck %s --check-prefix=EMPTY
189
190# EMPTY: [Nr] Name          Type                   Address          Off    Size
191# EMPTY: [ 1] .symtab_shndx SYMTAB SECTION INDICES 0000000000000000 000040 000000
192