1## Here we check that we are able to define sections with a type of "Fill".
2## Fills are custom pieces of data that can be placed anywhere just like normal
3## output sections, but they are not real output sections and you'll never see them in
4## the section headers.
5
6## Check we can create named and unnamed fills and use "Pattern" and "Size" fields
7## to describe the data emitted.
8## Check the data emitted and how it affects regular sections offsets.
9## Check that the "Name" field is optional for fills.
10## Check that "Size" can be greater than or equal to the pattern data size.
11
12# RUN: yaml2obj --docnum=1 %s -o %t1
13# RUN: llvm-readelf --sections --headers %t1 | FileCheck %s --check-prefix=BASIC
14
15# BASIC:        Number of section headers: 5
16# BASIC:        Section Headers:
17# BASIC-NEXT:   [Nr] Name      Type     Address          Off    Size   ES Flg Lk Inf Al
18# BASIC-NEXT:   [ 0]           NULL     0000000000000000 000000 000000 00     0   0  0
19# BASIC-NEXT:   [ 1] .foo      PROGBITS 0000000000000000 000043 000002 00     0   0  0
20# BASIC-NEXT:   [ 2] .bar      PROGBITS 0000000000000000 000049 000001 00     0   0  0
21# BASIC-NEXT:   [ 3] .strtab   STRTAB   0000000000000000 00004b 000001 00     0   0  1
22# BASIC-NEXT:   [ 4] .shstrtab STRTAB   0000000000000000 00004c 00001d 00     0   0  1
23
24## The fill we dump starts at (offset of .foo - 3), which is (0x43 - 3) = 0x40.
25# RUN: od -t x1 -v -j 0x40 -N 11 %t1 | FileCheck %s --ignore-case --check-prefix=DATA
26# DATA: aa bb aa 11 22 cc dd cc dd ff ee
27
28--- !ELF
29FileHeader:
30  Class: ELFCLASS64
31  Data:  ELFDATA2LSB
32  Type:  ET_DYN
33Sections:
34  - Type:    Fill
35    Pattern: "AABB"
36    Size:    0x3
37  - Name:    .foo
38    Type:    SHT_PROGBITS
39    Content: "1122"
40  - Type:    Fill
41    Name:    unusedName
42    Pattern: "CCDD"
43    Size:    4
44  - Name:    .bar
45    Type:    SHT_PROGBITS
46    Content: "FF"
47  - Type:    Fill
48    Pattern: "EE"
49    Size:    1
50
51## Check we can have no explicit regular sections in the YAML description, and can
52## describe the content with the use of fills only.
53## Check that "Size" can be less than the pattern data size.
54
55# RUN: yaml2obj --docnum=2 %s -o %t2
56# RUN: llvm-readelf --sections --headers %t2 | FileCheck %s --check-prefix=NOSECTIONS
57
58## The fill we dump starts at (offset of .strtab - 3 - 2), which is (0x45 - 5) = 0x40.
59# RUN: od -t x1 -v -j 0x40 -N 6 %t2 | FileCheck %s --ignore-case --check-prefix=NOSECTIONS-DATA
60
61# NOSECTIONS:      Number of section headers: 3
62# NOSECTIONS:      Section Headers:
63# NOSECTIONS-NEXT:  [Nr] Name      Type   Address          Off    Size   ES Flg Lk Inf Al
64# NOSECTIONS-NEXT:  [ 0]           NULL   0000000000000000 000000 000000 00     0   0  0
65# NOSECTIONS-NEXT:  [ 1] .strtab   STRTAB 0000000000000000 000045 000001 00     0   0  1
66# NOSECTIONS-NEXT:  [ 2] .shstrtab STRTAB 0000000000000000 000046 000013 00     0   0  1
67
68## .strtab that follows fills starts at 0x46 and always has a null character at the begining.
69# NOSECTIONS-DATA: aa bb cc dd ee 00
70
71--- !ELF
72FileHeader:
73  Class: ELFCLASS64
74  Data:  ELFDATA2LSB
75  Type:  ET_DYN
76Sections:
77  - Type:    Fill
78    Pattern: "AABBCCFF"
79    Size:    0x3
80  - Type:    Fill
81    Pattern: "DDEEFF"
82    Size:    0x2
83
84## Check we can use named fills when describing program headers.
85## Check that fills consume the file size and therefore affect the p_filesz fields of segments.
86## Check that the fill does not affect the p_align field of the segment.
87
88# RUN: yaml2obj --docnum=3 %s -o %t3
89# RUN: llvm-readelf --sections --program-headers %t3 | FileCheck %s --check-prefix=PHDR
90
91# PHDR: [Nr] Name      Type     Address          Off    Size   ES Flg Lk Inf  Al
92# PHDR: [ 0]           NULL     0000000000000000 000000 000000 00      0   0  0
93# PHDR: [ 1] .bar      PROGBITS 0000000000000100 0000c0 000005 00      0   0  2
94# PHDR: [ 2] .strtab   STRTAB   0000000000000000 00010a 000001 00      0   0  1
95# PHDR: [ 3] .shstrtab STRTAB   0000000000000000 00010b 000018 00      0   0  1
96
97# PHDR: Program Headers:
98# PHDR:   Type      Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
99# PHDR:   LOAD      0x0000b0 0x0000000000000100 0x0000000000000100 0x00005a 0x00005a     0x2
100# PHDR:   GNU_RELRO 0x0000c5 0x0000000000000105 0x0000000000000105 0x000045 0x000045     0x1
101
102--- !ELF
103FileHeader:
104  Class: ELFCLASS64
105  Data:  ELFDATA2LSB
106  Type:  ET_DYN
107Sections:
108  - Type:    Fill
109    Name:    fill1
110    Pattern: ""
111    Size:    0x10
112  - Name:         .bar
113    Type:         SHT_PROGBITS
114    Size:         0x5
115    Address:      0x100
116    AddressAlign: 2
117  - Type:    Fill
118    Name:    fill2
119    Pattern: ""
120    Size:    0x45
121ProgramHeaders:
122  - Type:     PT_LOAD
123    VAddr:    0x100
124    FirstSec: fill1
125    LastSec:  fill2
126  - Type:     PT_GNU_RELRO
127    VAddr:    0x105
128    FirstSec: fill2
129    LastSec:  fill2
130
131## Check that the "Pattern" field is not mandatory.
132# RUN: yaml2obj --docnum=4 2>&1 -o %t4 %s
133# RUN: llvm-readelf --sections %t4 | FileCheck %s --check-prefix=NOPATTERN
134
135## The fill we dump starts at (offset of .strtab - 1 - 3 - 1), which is (0x45 - 5) = 0x40.
136# RUN: od -t x1 -v -j 0x40 -N 5 %t4 | FileCheck %s --ignore-case --check-prefix=NOPATTERN-DATA
137
138# NOPATTERN: [Nr] Name    Type   Address          Off
139# NOPATTERN: [ 1] .strtab STRTAB 0000000000000000 000045
140
141# NOPATTERN-DATA: aa 00 00 00 bb
142
143--- !ELF
144FileHeader:
145  Class: ELFCLASS64
146  Data:  ELFDATA2LSB
147  Type:  ET_DYN
148Sections:
149  - Type:    Fill
150    Size:    0x1
151    Pattern: "AA"
152  - Type: Fill
153    Size: 0x3
154  - Type:    Fill
155    Size:    0x1
156    Pattern: "BB"
157
158## Check that the "Size" field is mandatory.
159# RUN: not yaml2obj --docnum=5 2>&1 %s | FileCheck %s --check-prefix=NOSIZE
160
161## NOSIZE: error: missing required key 'Size'
162
163--- !ELF
164FileHeader:
165  Class: ELFCLASS64
166  Data:  ELFDATA2LSB
167  Type:  ET_DYN
168Sections:
169  - Type:    Fill
170    Pattern: "00"
171
172## Check that fills are not allowed to have duplicate names.
173# RUN: not yaml2obj --docnum=6 2>&1 %s | FileCheck %s --check-prefix=UNIQUE-NAME
174
175# UNIQUE-NAME: error: repeated section/fill name: 'foo' at YAML section/fill number 2
176# UNIQUE-NAME: error: repeated section/fill name: 'foo' at YAML section/fill number 3
177
178--- !ELF
179FileHeader:
180  Class: ELFCLASS64
181  Data:  ELFDATA2LSB
182  Type:  ET_DYN
183Sections:
184  - Type:    Fill
185    Name:    foo
186    Pattern: "00"
187    Size:    1
188  - Type:    Fill
189    Name:    foo
190    Pattern: "00"
191    Size:    1
192  - Name: foo
193    Type: SHT_PROGBITS
194
195## Check that "Pattern" can be empty, when "Size" is zero.
196# RUN: yaml2obj --docnum=7 2>&1 %s -o %t7
197# RUN: llvm-readelf --sections %t7 | FileCheck %s --check-prefix=NOOP
198
199# NOOP: [Nr] Name  Type     Address          Off
200# NOOP: [ 1] begin PROGBITS 0000000000000000 000040
201# NOOP: [ 2] end   PROGBITS 0000000000000000 000041
202
203--- !ELF
204FileHeader:
205  Class: ELFCLASS64
206  Data:  ELFDATA2LSB
207  Type:  ET_DYN
208Sections:
209  - Name: begin
210    Type: SHT_PROGBITS
211    Size: 1
212  - Type:    Fill
213    Pattern: ""
214    Size:    0
215  - Name: end
216    Type: SHT_PROGBITS
217    Size: 1
218
219## Check that we can have an empty "Pattern", but have non-zero "Size".
220## In this case we emit Size number of zeroes to the output.
221
222# RUN: yaml2obj --docnum=8 2>&1 -o %t8 %s
223# RUN: llvm-readelf --sections %t8 | FileCheck %s --check-prefix=EMPTY-PATTERN
224
225## The fill we dump starts at (offset of .strtab - 1 - 3 - 1), which is (0x45 - 5) = 0x40.
226# RUN: od -t x1 -v -j 0x40 -N 5 %t8 | FileCheck %s --ignore-case --check-prefix=EMPTY-PATTERN-DATA
227
228# EMPTY-PATTERN:      Section Headers:
229# EMPTY-PATTERN-NEXT:  [Nr] Name    Type   Address          Off    Size   ES Flg Lk Inf Al
230# EMPTY-PATTERN-NEXT:  [ 0]         NULL   0000000000000000 000000 000000 00     0   0  0
231# EMPTY-PATTERN-NEXT:  [ 1] .strtab STRTAB 0000000000000000 000045 000001 00     0   0  1
232
233# EMPTY-PATTERN-DATA: aa 00 00 00 bb
234
235--- !ELF
236FileHeader:
237  Class: ELFCLASS64
238  Data:  ELFDATA2LSB
239  Type:  ET_DYN
240Sections:
241  - Type:    Fill
242    Pattern: "AA"
243    Size:    0x1
244  - Type:    Fill
245    Size:    3
246    Pattern: ""
247  - Type:    Fill
248    Pattern: "BB"
249    Size:    0x1
250
251## Check that "Size" can't be 0, when "Pattern" is not empty.
252# RUN: not yaml2obj --docnum=9 2>&1 %s | FileCheck %s --check-prefix=ZERO-SIZE-ERR
253
254# ZERO-SIZE-ERR: error: "Size" can't be 0 when "Pattern" is not empty
255
256--- !ELF
257FileHeader:
258  Class: ELFCLASS64
259  Data:  ELFDATA2LSB
260  Type:  ET_DYN
261Sections:
262  - Type:    Fill
263    Pattern: "00"
264    Size:    0
265
266## Check we report an error when a program header references
267## an unknown section or fill and have at least one Fill defined.
268
269# RUN: not yaml2obj --docnum=10 2>&1 %s | FileCheck %s --check-prefix=UNKNOWN-ERR
270# UNKNOWN-ERR: error: unknown section or fill referenced: 'fill' by the 'FirstSec' key of the program header with index 0
271# UNKNOWN-ERR: error: unknown section or fill referenced: 'fill' by the 'LastSec' key of the program header with index 0
272
273--- !ELF
274FileHeader:
275  Class: ELFCLASS64
276  Data:  ELFDATA2LSB
277  Type:  ET_DYN
278Sections:
279  - Type:    Fill
280    Pattern: ""
281    Size:    0
282ProgramHeaders:
283  - Type:     PT_LOAD
284    FirstSec: fill
285    LastSec:  fill
286
287## Show that we can use the "Offset" key to set an arbitrary offset for a Fill.
288
289## 0x41 is the minimal possible valid offset for Fill,
290## because the .foo section of size 0x1 is placed at 0x40.
291# RUN: yaml2obj --docnum=11 -DOFFSET=0x41 -o %t11 %s
292# RUN: llvm-readelf --section-headers %t11 | FileCheck %s --check-prefix=OFFSET-MIN
293
294## 0x123 is an arbitrary offset.
295# RUN: yaml2obj --docnum=11 -DOFFSET=0x123 -o %t12 %s
296# RUN: llvm-readelf --section-headers %t12 | FileCheck %s --check-prefix=OFFSET
297
298# OFFSET-MIN:      Section Headers:
299# OFFSET-MIN-NEXT:  [Nr] Name Type     Address          Off    Size
300# OFFSET-MIN-NEXT:  [ 0]      NULL     0000000000000000 000000 000000
301# OFFSET-MIN-NEXT:  [ 1] .foo PROGBITS 0000000000000000 000040 000001
302# OFFSET-MIN-NEXT:  [ 2] .bar PROGBITS 0000000000000000 000042 000001
303
304# OFFSET:      Section Headers:
305# OFFSET-NEXT:  [Nr] Name Type     Address          Off    Size
306# OFFSET-NEXT:  [ 0]      NULL     0000000000000000 000000 000000
307# OFFSET-NEXT:  [ 1] .foo PROGBITS 0000000000000000 000040 000001
308# OFFSET-NEXT:  [ 2] .bar PROGBITS 0000000000000000 000124 000001
309
310--- !ELF
311FileHeader:
312  Class: ELFCLASS64
313  Data:  ELFDATA2LSB
314  Type:  ET_DYN
315Sections:
316  - Name: .foo
317    Type: SHT_PROGBITS
318    Size: 1
319  - Type:    Fill
320    Pattern: "AA"
321    Size:    0x1
322    Offset:  [[OFFSET]]
323  - Name: .bar
324    Type: SHT_PROGBITS
325    Size: 1
326
327## Show that the "Offset" value can't go backward.
328# RUN: not yaml2obj --docnum=11 -DOFFSET=0x40 2>&1 %s | FileCheck %s --check-prefix=OFFSET-ERR
329
330# OFFSET-ERR: error: the 'Offset' value (0x40) goes backward
331