1## Check how the "Offset" field is dumped by obj2yaml.
2## For each section we calulate the expected offset.
3## When it does not match the actual offset, we emit the "Offset" key.
4
5# RUN: yaml2obj %s -o %t1.o
6# RUN: obj2yaml %t1.o | FileCheck %s --check-prefix=BASIC
7
8#   BASIC:      --- !ELF
9#   BASIC-NEXT: FileHeader:
10#   BASIC-NEXT:   Class: ELFCLASS64
11#   BASIC-NEXT:   Data:  ELFDATA2LSB
12#   BASIC-NEXT:   Type:  ET_REL
13#   BASIC-NEXT: Sections:
14#   BASIC-NEXT:   - Name:         .foo1
15#   BASIC-NEXT:     Type:         SHT_PROGBITS
16#   BASIC-NEXT:     Content:      '00'
17#   BASIC-NEXT:   - Name:         .foo2
18#   BASIC-NEXT:     Type:         SHT_PROGBITS
19#   BASIC-NEXT:     Content:      '00'
20#   BASIC-NEXT:   - Name:         .foo3
21#   BASIC-NEXT:     Type:         SHT_PROGBITS
22#   BASIC-NEXT:     Content:      '00'
23#   BASIC-NEXT:   - Name:         .bar1
24#   BASIC-NEXT:     Type:         SHT_PROGBITS
25#   BASIC-NEXT:     Offset:       0x100
26#   BASIC-NEXT:     Content:      '00'
27#   BASIC-NEXT:   - Name:         .bar2
28#   BASIC-NEXT:     Type:         SHT_PROGBITS
29#   BASIC-NEXT:     AddressAlign: 0x10
30#   BASIC-NEXT:     Content:      '00'
31#   BASIC-NEXT:   - Name:         .bar3
32#   BASIC-NEXT:     Type:         SHT_PROGBITS
33#   BASIC-NEXT:     AddressAlign: 0x10
34#   BASIC-NEXT:     Offset:       0x200
35#   BASIC-NEXT:   - Name:         .bar4
36#   BASIC-NEXT:     Type:         SHT_PROGBITS
37#   BASIC-NEXT:     AddressAlign: 0x100000000
38#   BASIC-NEXT:     Offset:       0x210
39# HEADERS-NEXT: SectionHeaderTable:
40# HEADERS-NEXT:   Sections:
41# HEADERS-NEXT:     - Name:            .bar4
42# HEADERS-NEXT:     - Name:            .bar3
43# HEADERS-NEXT:     - Name:            .bar2
44# HEADERS-NEXT:     - Name:            .bar1
45# HEADERS-NEXT:     - Name:            .foo3
46# HEADERS-NEXT:     - Name:            .foo2
47# HEADERS-NEXT:     - Name:            .foo1
48# HEADERS-NEXT:     - Name:            .strtab
49# HEADERS-NEXT:     - Name:            .shstrtab
50# BASIC-NEXT: ...
51
52--- !ELF
53FileHeader:
54  Class: ELFCLASS64
55  Data:  ELFDATA2LSB
56  Type:  ET_REL
57Sections:
58## The offset of .foo1 by default is 0x40, because it is placed right
59## after the ELF header. In this case we don't dump the "Offset" key,
60## because the file offset is naturally expected.
61  - Name:         .foo1
62    Type:         SHT_PROGBITS
63    Size:         1
64    Offset:       [[FIRSTOFF=<none>]]
65    AddressAlign: [[FIRSTADDRALIGN=0]]
66## Offset of .foo2 == offset of .foo1 + size of .foo1.
67## We don't dump the "Offset" key in this case.
68## sh_offset of .foo2 is 0x41.
69  - Name: .foo2
70    Type: SHT_PROGBITS
71    Size: 1
72## Offset of .foo3 == offset of .foo2 + size of .foo2,
73## We don't dump the "Offset" key in this case.
74## sh_offset of .foo3 is 0x42.
75  - Name: .foo3
76    Type: SHT_PROGBITS
77    Size: 1
78## Offset of .bar1 != offset of .foo3 + size of .foo3.
79## We dump the "Offset" key in this case.
80## sh_offset of .bar1 is 0x100.
81  - Name:   .bar1
82    Type:   SHT_PROGBITS
83    Offset: 0x100
84    Size: 1
85## [Offset of .bar1 + size of .bar1] aligned by 0x10 is equal to the offset
86## of .bar2. We don't dump the "Offset" key in this case.
87## sh_offset of .bar2 is 0x110.
88  - Name:         .bar2
89    Type:         SHT_PROGBITS
90    AddressAlign: 0x10
91    Offset:       0x110
92    Size:         1
93## [Offset of .bar2 + size of .bar2] aligned by 0x10 is not equal to the offset
94## of .bar3. We dump the "Offset" key in this case.
95## sh_offset of .bar3 is 0x200.
96  - Name:         .bar3
97    Type:         SHT_PROGBITS
98    AddressAlign: 0x10
99    Offset:       0x200
100## A case where AddressAlign > MAX_UINT32 and (uint32_t)AddressAlign == 0.
101## Check we dump an offset in this case properly.
102  - Name:         .bar4
103    Type:         SHT_PROGBITS
104    AddressAlign: 0x100000000
105    Offset:       0x210
106SectionHeaderTable:
107  Sections:
108## By default we have the same order of sections as defined by the "Sections" key.
109    - Name: [[SEC1=.foo1]]
110    - Name: [[SEC2=.foo2]]
111    - Name: [[SEC3=.foo3]]
112    - Name: [[SEC4=.bar1]]
113    - Name: [[SEC5=.bar2]]
114    - Name: [[SEC6=.bar3]]
115    - Name: [[SEC7=.bar4]]
116    - Name: .strtab
117    - Name: .shstrtab
118
119## In this case we change the order of sections in the section header table.
120## Check that we still dump offsets correctly.
121
122# RUN: yaml2obj %s -DSEC1=.bar4 -DSEC2=.bar3 -DSEC3=.bar2 \
123# RUN:   -DSEC4=.bar1 -DSEC5=.foo3 -DSEC6=.foo2 -DSEC7=.foo1 -o %t1-sechdr.o
124# RUN: obj2yaml %t1-sechdr.o | FileCheck --check-prefixes=BASIC,HEADERS %s
125
126## Show we dump the "Offset" key for the first section when
127## it has an unexpected file offset.
128
129# RUN: yaml2obj %s -DFIRSTOFF=0x40 -o %t2a.o
130# RUN: obj2yaml %t2a.o | FileCheck %s --check-prefix=BASIC
131# RUN: yaml2obj %s -DFIRSTOFF=0x41 -o %t2b.o
132# RUN: obj2yaml %t2b.o | FileCheck %s --check-prefix=FIRSTSEC
133
134# FIRSTSEC:      Sections:
135# FIRSTSEC-NEXT:   - Name:    .foo1
136# FIRSTSEC-NEXT:     Type:    SHT_PROGBITS
137# FIRSTSEC-NEXT:     Offset:  0x41
138# FIRSTSEC-NEXT:     Content: '00'
139
140## Test that we take the alignment of the first section into account
141## when calculating the expected offset for it. In this case we don't
142## dump the "Offset", because it is expected.
143
144# RUN: yaml2obj %s -DFIRSTOFF=0x80 -DFIRSTADDRALIGN=0x80 -o %t3.o
145# RUN: obj2yaml %t3.o | FileCheck %s --check-prefix=FIRSTSECALIGN
146
147# FIRSTSECALIGN:      - Name:         .foo1
148# FIRSTSECALIGN-NEXT:   Type:         SHT_PROGBITS
149# FIRSTSECALIGN-NEXT:   AddressAlign: 0x80
150# FIRSTSECALIGN-NEXT:   Content:      '00'
151# FIRSTSECALIGN-NEXT: - Name:
152
153## Test that we take the program headers offset and size into account when calculating
154## the expected file offset of the first section.
155
156# RUN: yaml2obj %s --docnum=2 -o %t4a.o
157# RUN: obj2yaml %t4a.o | FileCheck %s --check-prefix=FIRSTSECPHDRS
158## The expected file offset of the first section is:
159## 0x40 (start of program headers) + 0x38 (size of program headers) * 2(number of program headers) = 0xB0
160# RUN: yaml2obj %s --docnum=2 -DFIRSTOFF=0xB0 -o %t4b.o
161# RUN: obj2yaml %t4b.o | FileCheck %s --check-prefix=FIRSTSECPHDRS
162# RUN: yaml2obj %s --docnum=2 -DFIRSTOFF=0xB1 -o %t4c.o
163# RUN: obj2yaml %t4c.o | FileCheck %s --check-prefixes=FIRSTSECPHDRS,FIRSTSECPHDRSOFFSET
164
165#       FIRSTSECPHDRS:      Sections:
166#       FIRSTSECPHDRS-NEXT:   - Name:   .foo
167#       FIRSTSECPHDRS-NEXT:     Type:   SHT_PROGBITS
168# FIRSTSECPHDRSOFFSET-NEXT:     Offset: 0xB1
169#       FIRSTSECPHDRS-NEXT:   ...
170
171--- !ELF
172FileHeader:
173  Class: ELFCLASS64
174  Data:  ELFDATA2LSB
175  Type:  ET_REL
176Sections:
177  - Name:   .foo
178    Type:   SHT_PROGBITS
179    Offset: [[FIRSTOFF=<none>]]
180ProgramHeaders:
181  - Type: PT_LOAD
182  - Type: PT_LOAD
183
184## Test that when there are no program headers in the file, we don't take SHT_NOBITS
185## section sizes into account, but respect their alignment when calculating the expected
186## section offsets.
187
188# RUN: yaml2obj %s --docnum=3 -o %t5.o
189# RUN: obj2yaml %t5.o | FileCheck %s --check-prefix=NOBITS
190
191# NOBITS:      Sections:
192# NOBITS-NEXT:   - Name:         .progbits1
193# NOBITS-NEXT:     Type:         SHT_PROGBITS
194# NOBITS-NEXT:     Content:      '00'
195# NOBITS-NEXT:   - Name:         .nobits1
196# NOBITS-NEXT:     Type:         SHT_NOBITS
197# NOBITS-NEXT:     Size:         0x10
198# NOBITS-NEXT:   - Name:         .progbits2
199# NOBITS-NEXT:     Type:         SHT_PROGBITS
200# NOBITS-NEXT:     Content:      '0000'
201# NOBITS-NEXT:   - Name:         .nobits2
202# NOBITS-NEXT:     Type:         SHT_NOBITS
203# NOBITS-NEXT:     AddressAlign: 0x100
204# NOBITS-NEXT:     Size:         0x100
205# NOBITS-NEXT:   - Name:         .progbits3
206# NOBITS-NEXT:     Type:         SHT_PROGBITS
207# NOBITS-NEXT:     Content:      '000000'
208# NOBITS-NEXT: ...
209
210--- !ELF
211FileHeader:
212  Class: ELFCLASS64
213  Data:  ELFDATA2LSB
214  Type:  ET_REL
215Sections:
216## sh_offset == 0x40.
217  - Name: .progbits1
218    Type: SHT_PROGBITS
219    Size: 0x1
220## sh_offset == 0x41.
221  - Name: .nobits1
222    Type: SHT_NOBITS
223    Size: 0x10
224## sh_offset == 0x41.
225  - Name: .progbits2
226    Type: SHT_PROGBITS
227    Size: 0x2
228## sh_offset == 0x100.
229  - Name:         .nobits2
230    Type:         SHT_NOBITS
231    Size:         0x100
232    AddressAlign: 0x100
233## sh_offset == 0x100.
234  - Name: .progbits3
235    Type: SHT_PROGBITS
236    Size: 0x3
237
238## Check that we might take sizes of SHT_NOBITS sections into account when calculating
239## the expected offsets when there are program headers in the file. The rule is the following:
240## we assume that the file space is allocated for the SHT_NOBITS section when there are
241## other non-nobits sections in the same segment that follows it.
242
243# RUN: yaml2obj %s --docnum=4 -o %t6.o
244# RUN: obj2yaml %t6.o | FileCheck %s --check-prefix=NOBITS-PHDRS
245
246# NOBITS-PHDRS:      Sections:
247# NOBITS-PHDRS-NEXT:   - Name:    .nobits1
248# NOBITS-PHDRS-NEXT:     Type:    SHT_NOBITS
249# NOBITS-PHDRS-NEXT:     Size:    0x1
250# NOBITS-PHDRS-NEXT:   - Name:    .progbits
251# NOBITS-PHDRS-NEXT:     Type:    SHT_PROGBITS
252# NOBITS-PHDRS-NEXT:     Content: '0000'
253# NOBITS-PHDRS-NEXT:   - Name:    .nobits3
254# NOBITS-PHDRS-NEXT:     Type:    SHT_NOBITS
255# NOBITS-PHDRS-NEXT:     Size:    0x100
256# NOBITS-PHDRS-NEXT:   - Name:    .nobits4
257# NOBITS-PHDRS-NEXT:     Type:    SHT_NOBITS
258# NOBITS-PHDRS-NEXT:     Size:    0x200
259# NOBITS-PHDRS-NEXT:   - Name:    .nobits5
260# NOBITS-PHDRS-NEXT:     Type:    SHT_NOBITS
261# NOBITS-PHDRS-NEXT:     Offset:  0x100
262# NOBITS-PHDRS-NEXT:     Size:    0x300
263# NOBITS-PHDRS-NEXT: ...
264
265--- !ELF
266FileHeader:
267  Class: ELFCLASS64
268  Data:  ELFDATA2LSB
269  Type:  ET_REL
270Sections:
271## sh_offset == 0xe8.
272  - Name: .nobits1
273    Type: SHT_NOBITS
274    Size: 0x1
275## sh_offset == 0xe9.
276  - Name: .progbits
277    Type: SHT_PROGBITS
278    Size: 0x2
279## sh_offset == 0xeb.
280  - Name: .nobits3
281    Type: SHT_NOBITS
282    Size: 0x100
283## sh_offset == 0xeb.
284  - Name: .nobits4
285    Type: SHT_NOBITS
286    Size: 0x200
287## sh_offset == 0x100.
288  - Name:   .nobits5
289    Type:   SHT_NOBITS
290    Size:   0x300
291    Offset: 0x100
292ProgramHeaders:
293  - Type:     PT_LOAD
294    FirstSec: .nobits1
295    LastSec:  .progbits
296  - Type:     PT_LOAD
297    FirstSec: .nobits3
298    LastSec:  .nobits4
299  - Type:     PT_LOAD
300    FirstSec: .nobits5
301    LastSec:  .nobits5
302