xref: /llvm-project/llvm/test/tools/obj2yaml/ELF/program-headers.yaml (revision 8545093715c4efcfe7069aa29e9372d32f9c7b36)
1## Show that obj2yaml is able to dump program headers.
2
3## Part I. Base check. All simple cases that look OK as a part of a single large test live here.
4
5# RUN: yaml2obj %s -o %t1
6
7## Show the layout of the object before we dump it using obj2yaml.
8## The check is here to make it clear what the layout should look like.
9# RUN: llvm-readelf --segments %t1 | FileCheck %s --check-prefix=SEGMENT-MAPPING
10
11# SEGMENT-MAPPING:      Program Headers:
12# SEGMENT-MAPPING-NEXT:   Type      Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
13# SEGMENT-MAPPING-NEXT:   LOAD      0x000000 0x0000000000000000 0x0000000000000000 0x000281 0x000281 R   0x1000
14# SEGMENT-MAPPING-NEXT:   LOAD      0x000281 0x0000000000001000 0x0000000000001000 0x000010 0x000010 R E 0x1000
15# SEGMENT-MAPPING-NEXT:   LOAD      0x000291 0x0000000000002000 0x0000000000002000 0x000009 0x000009 R   0x1000
16# SEGMENT-MAPPING-NEXT:   LOAD      0x00029a 0x0000000000003ef0 0x0000000000003ef0 0x000011 0x000011 RW  0x1000
17# SEGMENT-MAPPING-NEXT:   DYNAMIC   0x00029a 0x0000000000003ef0 0x0000000000003ef0 0x000010 0x000010 RW  0x8
18# SEGMENT-MAPPING-NEXT:   GNU_RELRO 0x00029a 0x0000000000003ef0 0x0000000000003ef0 0x000010 0x000010 R   0x1
19# SEGMENT-MAPPING-NEXT:   LOAD      0x000000 0x0000000000004000 0x0000000000004000 0x000000 0x000000 R   0x1
20# SEGMENT-MAPPING-NEXT:   LOAD      0x000248 0x00000000000001a0 0x00000000000001a0 0x000020 0x000020 R   0x1
21# SEGMENT-MAPPING-NEXT:   LOAD      0x000248 0x00000000000001a0 0x00000000000001a0 0x000020 0x000020 R   0x1
22# SEGMENT-MAPPING:      Section to Segment mapping:
23# SEGMENT-MAPPING-NEXT:  Segment Sections...
24# SEGMENT-MAPPING-NEXT:   00     .hash .gnu.hash .dynsym .dynstr {{$}}
25# SEGMENT-MAPPING-NEXT:   01     .foo .zed {{$}}
26# SEGMENT-MAPPING-NEXT:   02     .foo .baz {{$}}
27# SEGMENT-MAPPING-NEXT:   03     .dynamic .dynamic.tail {{$}}
28# SEGMENT-MAPPING-NEXT:   04     .dynamic {{$}}
29# SEGMENT-MAPPING-NEXT:   05     .dynamic {{$}}
30# SEGMENT-MAPPING-NEXT:   06{{ *$}}
31# SEGMENT-MAPPING-NEXT:   07     .gnu.hash {{$}}
32# SEGMENT-MAPPING-NEXT:   08     .gnu.hash {{$}}
33# SEGMENT-MAPPING-NEXT:   None   .symtab .strtab .shstrtab {{$}}
34
35## Check that obj2yaml produced a correct program headers description.
36
37# RUN: obj2yaml %t1 | FileCheck %s --check-prefix=YAML
38
39# YAML:      ProgramHeaders:
40# YAML-NEXT:  - Type:     PT_LOAD
41# YAML-NEXT:    Flags:    [ PF_R ]
42# YAML-NEXT:    FirstSec: .hash
43# YAML-NEXT:    LastSec:  .dynstr
44# YAML-NEXT:    Align:    0x1000
45# YAML-NEXT:    Offset:   0x0
46# YAML-NEXT:  - Type:     PT_LOAD
47# YAML-NEXT:    Flags:    [ PF_X, PF_R ]
48# YAML-NEXT:    FirstSec: .foo
49# YAML-NEXT:    LastSec:  .zed
50# YAML-NEXT:    VAddr:    0x1000
51# YAML-NEXT:    Align:    0x1000
52# YAML-NEXT:    Offset:   0x281
53# YAML-NEXT:  - Type:     PT_LOAD
54# YAML-NEXT:    Flags:    [ PF_R ]
55# YAML-NEXT:    FirstSec: '.foo (1)'
56# YAML-NEXT:    LastSec:  .baz
57# YAML-NEXT:    VAddr:    0x2000
58# YAML-NEXT:    Align:    0x1000
59# YAML-NEXT:    Offset:   0x291
60# YAML-NEXT:  - Type:     PT_LOAD
61# YAML-NEXT:    Flags:    [ PF_W, PF_R ]
62# YAML-NEXT:    FirstSec: .dynamic
63# YAML-NEXT:    LastSec:  .dynamic.tail
64# YAML-NEXT:    VAddr:    0x3EF0
65# YAML-NEXT:    Align:    0x1000
66# YAML-NEXT:    Offset:   0x29A
67# YAML-NEXT:  - Type:     PT_DYNAMIC
68# YAML-NEXT:    Flags:    [ PF_W, PF_R ]
69# YAML-NEXT:    FirstSec: .dynamic
70# YAML-NEXT:    LastSec:  .dynamic
71# YAML-NEXT:    VAddr:    0x3EF0
72# YAML-NEXT:    Align:    0x8
73# YAML-NEXT:    Offset:   0x29A
74# YAML-NEXT:  - Type:     PT_GNU_RELRO
75# YAML-NEXT:    Flags:    [ PF_R ]
76# YAML-NEXT:    FirstSec: .dynamic
77# YAML-NEXT:    LastSec:  .dynamic
78# YAML-NEXT:    VAddr:    0x3EF0
79# YAML-NEXT:    Offset:   0x29A
80# YAML-NEXT:  - Type:  PT_LOAD
81# YAML-NEXT:    Flags: [ PF_R ]
82# YAML-NEXT:    VAddr: 0x4000
83# YAML-NEXT:    Offset:   0x0
84# YAML-NEXT:  - Type:     PT_LOAD
85# YAML-NEXT:    Flags:    [ PF_R ]
86# YAML-NEXT:    FirstSec: .gnu.hash
87# YAML-NEXT:    LastSec:  .gnu.hash
88# YAML-NEXT:    VAddr:    0x1A0
89# YAML-NEXT:    Offset:   0x248
90# YAML-NEXT:  - Type:     PT_LOAD
91# YAML-NEXT:    Flags:    [ PF_R ]
92# YAML-NEXT:    FirstSec: .gnu.hash
93# YAML-NEXT:    LastSec:  .gnu.hash
94# YAML-NEXT:    VAddr:    0x1A0
95# YAML-NEXT:    Offset:   0x248
96# YAML-NEXT: Sections:
97
98--- !ELF
99FileHeader:
100  Class: ELFCLASS64
101  Data:  ELFDATA2LSB
102  Type:  ET_DYN
103ProgramHeaders:
104## Check we can create a PT_LOAD with arbitrary (we used .hash, .gnu.hash)
105## and implicit sections (we use .dynsym, .dynstr). It also checks that the
106## SHT_NULL section at index 0 is not included in the segment.
107  - Type:     PT_LOAD
108    Flags:    [ PF_R ]
109    FirstSec: .hash
110    LastSec:  .dynstr
111    Align:  0x1000
112    Offset: 0x0
113## Check we can create a PT_LOAD with a different set of properties and sections.
114  - Type:     PT_LOAD
115    Flags:    [ PF_X, PF_R ]
116    FirstSec: .foo
117    LastSec:  .zed
118    VAddr:    0x1000
119    Align:    0x1000
120## Create a PT_LOAD to demonstate we are able to refer to output sections with the same name.
121  - Type:     PT_LOAD
122    Flags:    [ PF_R ]
123    FirstSec: '.foo (1)'
124    LastSec:  .baz
125    VAddr:    0x2000
126    Align:    0x1000
127## Show we can create a writeable PT_LOAD segment and put an arbitrary section into it.
128## Here we test both regular (SHT_PROGBITS) and a special section (SHT_DYNAMIC).
129  - Type:     PT_LOAD
130    Flags:    [ PF_W, PF_R ]
131    FirstSec: .dynamic
132    LastSec:  .dynamic.tail
133    VAddr:    0x3EF0
134    Align:    0x1000
135## Show we can create a nested dynamic segment and put a section into it.
136  - Type:     PT_DYNAMIC
137    Flags:    [ PF_W, PF_R ]
138    FirstSec: .dynamic
139    LastSec:  .dynamic
140    VAddr:    0x3EF0
141    Align:    0x8
142## Show we can create a relro segment and put a section into it.
143## We used .dynamic here and in tests above to demonstrate that
144## we can place a section in any number of segments.
145## Also, we explicitly set the "Align" property to 1 to demonstate
146## that we do not dump it, because it is the default alignment
147## value set by yaml2obj.
148  - Type:     PT_GNU_RELRO
149    Flags:    [ PF_R ]
150    FirstSec: .dynamic
151    LastSec:  .dynamic
152    VAddr:    0x3EF0
153    Align:    0x1
154## Show we can dump a standalone empty segment.
155  - Type:  PT_LOAD
156    Flags: [ PF_R ]
157    VAddr: 0x4000
158    Align: 0x1
159## ELF specification says that loadable segment entries in the
160## program header are sorted by virtual address.
161## Show we can dump an out of order segment.
162  - Type:     PT_LOAD
163    Flags:    [ PF_R ]
164    FirstSec: .gnu.hash
165    LastSec:  .gnu.hash
166    VAddr:    0x1A0
167    Align:    0x1
168## Test we are able to dump duplicated segments.
169## We use a segment that is the same as the previous one for this.
170  - Type:     PT_LOAD
171    Flags:    [ PF_R ]
172    FirstSec: .gnu.hash
173    LastSec:  .gnu.hash
174    VAddr:    0x1A0
175    Align:    0x1
176Sections:
177  - Name:    .hash
178    Type:    SHT_PROGBITS
179    Flags:   [ SHF_ALLOC ]
180    Address: 0x190
181    Size:    0x10
182  - Name:    .gnu.hash
183    Type:    SHT_PROGBITS
184    Flags:   [ SHF_ALLOC ]
185    Address: 0x1A0
186    Size:    0x20
187  - Name:    .dynsym
188    Type:    SHT_DYNSYM
189    Flags:   [ SHF_ALLOC ]
190    Address: 0x1C0
191    Link:    .dynstr
192    EntSize: 0x18
193  - Name:    .dynstr
194    Type:    SHT_STRTAB
195    Flags:   [ SHF_ALLOC ]
196    Address: 0x1D8
197  - Name:    .foo
198    Type:    SHT_PROGBITS
199    Flags:   [ SHF_ALLOC, SHF_EXECINSTR ]
200    Address: 0x1000
201    Size:    0x8
202  - Name:    .zed
203    Type:    SHT_PROGBITS
204    Flags:   [ SHF_ALLOC, SHF_EXECINSTR ]
205    Address: 0x1008
206    Size:    0x8
207  - Name:    '.foo (1)'
208    Type:    SHT_PROGBITS
209    Flags:   [ SHF_ALLOC ]
210    Address: 0x2000
211    Size:    0x8
212  - Name:    .baz
213    Type:    SHT_PROGBITS
214    Flags:   [ SHF_ALLOC ]
215    Address: 0x2008
216    Size:    0x1
217  - Name:    .dynamic
218    Type:    SHT_DYNAMIC
219    Flags:   [ SHF_WRITE, SHF_ALLOC ]
220    Address: 0x0000000000003EF0
221    Link:    .dynstr
222    Entries:
223      - Tag:   DT_NULL
224        Value: 0x0
225  - Name:    .dynamic.tail
226    Type:    SHT_PROGBITS
227    Flags:   [ SHF_WRITE, SHF_ALLOC ]
228    Content: "FE"
229Symbols: []
230DynamicSymbols: []
231
232## Part II. More specific tests.
233
234## Check we are able to dump segments that are empty or
235## contain empty sections.
236# RUN: yaml2obj --docnum=2 %s -o %t2
237# RUN: obj2yaml %t2 | FileCheck %s --check-prefix=EMPTY
238
239# EMPTY:      - Type:     PT_LOAD
240# EMPTY-NEXT:   Flags:    [ PF_W, PF_R ]
241# EMPTY-NEXT:   FirstSec: .empty.tls.start
242# EMPTY-NEXT:   LastSec:  .empty.tls.end
243# EMPTY-NEXT:   VAddr:    0x1000
244# EMPTY-NEXT:   Align:    0x1000
245# EMPTY-NEXT:   Offset:   0x120
246# EMPTY-NEXT: - Type:     PT_TLS
247# EMPTY-NEXT:   Flags:    [ PF_W, PF_R ]
248# EMPTY-NEXT:   FirstSec: .empty.tls.start
249# EMPTY-NEXT:   LastSec:  .empty.tls.start
250# EMPTY-NEXT:   VAddr:    0x1000
251# EMPTY-NEXT:   Offset:   0x120
252# EMPTY-NEXT: - Type:     PT_TLS
253# EMPTY-NEXT:   Flags:    [ PF_W, PF_R ]
254# EMPTY-NEXT:   FirstSec: .empty.tls.middle
255# EMPTY-NEXT:   LastSec:  .empty.tls.middle
256# EMPTY-NEXT:   VAddr:    0x1100
257# EMPTY-NEXT:   Offset:   0x220
258# EMPTY-NEXT: - Type:     PT_TLS
259# EMPTY-NEXT:   Flags:    [ PF_W, PF_R ]
260# EMPTY-NEXT:   FirstSec: .empty.tls.end
261# EMPTY-NEXT:   LastSec:  .empty.tls.end
262# EMPTY-NEXT:   VAddr:    0x1200
263# EMPTY-NEXT:   Offset:   0x320
264# EMPTY-NEXT: Sections:
265
266--- !ELF
267FileHeader:
268  Class: ELFCLASS64
269  Data:  ELFDATA2LSB
270  Type:  ET_DYN
271ProgramHeaders:
272  - Type:     PT_LOAD
273    Flags:    [ PF_W, PF_R ]
274    FirstSec: .empty.tls.start
275    LastSec:  .empty.tls.end
276    VAddr:    0x1000
277    Align:    0x1000
278  - Type:     PT_TLS
279    Flags:    [ PF_W, PF_R ]
280    FirstSec: .empty.tls.start
281    LastSec:  .empty.tls.start
282    VAddr:    0x1000
283    Align:    0x1
284  - Type:     PT_TLS
285    Flags:    [ PF_W, PF_R ]
286    FirstSec: .empty.tls.middle
287    LastSec:  .empty.tls.middle
288    VAddr:    0x1100
289    Align:    0x1
290  - Type:     PT_TLS
291    Flags:    [ PF_W, PF_R ]
292    FirstSec: .empty.tls.end
293    LastSec:  .empty.tls.end
294    VAddr:    0x1200
295    Align:    0x1
296Sections:
297  - Name:    .empty.tls.start
298    Type:    SHT_PROGBITS
299    Flags:   [ SHF_ALLOC, SHF_TLS ]
300    Size:    0x0
301    Address: 0x1000
302  - Name:  .section.1
303    Type:  SHT_PROGBITS
304    Flags: [ SHF_ALLOC ]
305    Size:  0x100
306  - Name:  .empty.tls.middle
307    Type:  SHT_PROGBITS
308    Flags: [ SHF_ALLOC, SHF_TLS ]
309    Size:  0x0
310  - Name:  .section.2
311    Type:  SHT_PROGBITS
312    Flags: [ SHF_ALLOC ]
313    Size:  0x100
314  - Name:  .empty.tls.end
315    Type:  SHT_PROGBITS
316    Flags: [ SHF_ALLOC, SHF_TLS ]
317    Size:  0x0
318
319## Document we are able to dump misaligned segments.
320## I.e. segments where (p_offset % p_align) != (p_vaddr % p_align).
321# RUN: yaml2obj --docnum=3 %s -o %t3
322# RUN: llvm-readelf --segments --sections %t3 | FileCheck %s --check-prefix=MISALIGNED-READELF
323# RUN: obj2yaml %t3 | FileCheck %s --check-prefix=MISALIGNED-YAML
324
325## As a misaligned p_offset value we use (`.foo` section offset - 1).
326# MISALIGNED-READELF:      [Nr] Name Type     Address          Off
327# MISALIGNED-READELF:      [ 1] .foo PROGBITS 0000000000001000 000078
328# MISALIGNED-READELF:      Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
329# MISALIGNED-READELF-NEXT: LOAD 0x000077 0x0000000000001000 0x0000000000001000 0x000078 0x000078 R   0x1000
330
331# MISALIGNED-YAML:      ProgramHeaders:
332# MISALIGNED-YAML-NEXT:  - Type:     PT_LOAD
333# MISALIGNED-YAML-NEXT:    Flags:    [ PF_R ]
334# MISALIGNED-YAML-NEXT:    FirstSec: .foo
335# MISALIGNED-YAML-NEXT:    LastSec:  .foo
336# MISALIGNED-YAML-NEXT:    VAddr:    0x1000
337# MISALIGNED-YAML-NEXT:    Align:    0x1000
338# MISALIGNED-YAML-NEXT:    Offset:   0x77
339# MISALIGNED-YAML-NEXT: Sections:
340
341--- !ELF
342FileHeader:
343  Class: ELFCLASS64
344  Data:  ELFDATA2LSB
345  Type:  ET_DYN
346ProgramHeaders:
347  - Type:     PT_LOAD
348    Flags:    [ PF_R ]
349    FirstSec: .foo
350    LastSec:  .foo
351    VAddr:    0x1000
352    Align:    0x1000
353    Offset:   0x000077
354Sections:
355  - Name:    .foo
356    Type:    SHT_PROGBITS
357    Flags:   [ SHF_ALLOC ]
358    Size:    0x77
359    Address: 0x1000
360
361## Test we include non-allocatable sections in segments.
362## We also document that SHT_NULL sections are not considered to be inside a segment.
363# RUN: yaml2obj --docnum=4 %s -o %t4
364# RUN: obj2yaml %t4 | FileCheck %s --check-prefix=NON-ALLOC
365
366# NON-ALLOC:      ProgramHeaders:
367# NON-ALLOC-NEXT: - Type:     PT_LOAD
368# NON-ALLOC-NEXT:   Flags:    [ PF_R ]
369# NON-ALLOC-NEXT:   FirstSec: .alloc.1
370# NON-ALLOC-NEXT:   LastSec:  .non-alloc.1
371# NON-ALLOC-NEXT:   Offset:   0x120
372# NON-ALLOC-NEXT: - Type:     PT_LOAD
373# NON-ALLOC-NEXT:   Flags:    [ PF_R ]
374# NON-ALLOC-NEXT:   FirstSec: .alloc.1
375# NON-ALLOC-NEXT:   LastSec:  .non-alloc.1
376# NON-ALLOC-NEXT:   Offset:   0x120
377# NON-ALLOC-NEXT: - Type:     PT_LOAD
378# NON-ALLOC-NEXT:   Flags:    [ PF_R ]
379# NON-ALLOC-NEXT:   FirstSec: .alloc.2
380# NON-ALLOC-NEXT:   LastSec:  .alloc.2
381# NON-ALLOC-NEXT:   Offset:   0x230
382# NON-ALLOC-NEXT: - Type:     PT_LOAD
383# NON-ALLOC-NEXT:   Flags:    [ PF_R ]
384# NON-ALLOC-NEXT:   FirstSec: .alloc.1
385# NON-ALLOC-NEXT:   LastSec:  .alloc.2
386# NON-ALLOC-NEXT:   Offset:   0x120
387# NON-ALLOC-NEXT: Sections:
388
389--- !ELF
390FileHeader:
391  Class: ELFCLASS64
392  Data:  ELFDATA2LSB
393  Type:  ET_DYN
394ProgramHeaders:
395  - Type:     PT_LOAD
396    Flags:    [ PF_R ]
397    FirstSec: .alloc.1
398    LastSec:  .non-alloc.1
399  - Type:     PT_LOAD
400    Flags:    [ PF_R ]
401    FirstSec: .alloc.1
402    LastSec:  .non-alloc.2
403  - Type:     PT_LOAD
404    Flags:    [ PF_R ]
405    FirstSec: .non-alloc.2
406    LastSec:  .alloc.2
407  - Type:     PT_LOAD
408    Flags:    [ PF_R ]
409    FirstSec: .alloc.1
410    LastSec:  .alloc.2
411Sections:
412  - Name:    .alloc.1
413    Type:    SHT_PROGBITS
414    Flags:   [ SHF_ALLOC ]
415    Size:    0x100
416    Address: 0x1000
417  - Name:    .non-alloc.1
418    Type:    SHT_PROGBITS
419    Flags:   [ ]
420    Size:    0x10
421  - Name:    .non-alloc.2
422    Type:    SHT_NULL
423    Flags:   [ ]
424    Size:    0x10
425  - Name:    .alloc.2
426    Type:    SHT_PROGBITS
427    Flags:   [ SHF_ALLOC ]
428    Size:    0x1
429
430## Check how we dump segments which contain SHT_NOBITS sections.
431# RUN: yaml2obj --docnum=5 %s -o %t5
432# RUN: obj2yaml %t5 | FileCheck %s --check-prefix=NOBITS
433
434# NOBITS:      ProgramHeaders:
435# NOBITS-NEXT:   - Type:     PT_LOAD
436# NOBITS-NEXT:     Flags:    [ PF_W, PF_R ]
437# NOBITS-NEXT:     FirstSec: .bss
438# NOBITS-NEXT:     LastSec:  .bss
439# NOBITS-NEXT:     Offset:   0x159
440# NOBITS-NEXT:   - Type:     PT_LOAD
441# NOBITS-NEXT:     Flags:    [ PF_W, PF_R ]
442# NOBITS-NEXT:     FirstSec: .data.1
443# NOBITS-NEXT:     LastSec:  .bss
444# NOBITS-NEXT:     Offset:   0x158
445# NOBITS-NEXT:   - Type:     PT_LOAD
446# NOBITS-NEXT:     Flags:    [ PF_W, PF_R ]
447# NOBITS-NEXT:     FirstSec: .data.1
448# NOBITS-NEXT:     LastSec:  .data.2
449# NOBITS-NEXT:     Offset:   0x158
450# NOBITS-NEXT:   - Type:     PT_LOAD
451# NOBITS-NEXT:     Flags:    [ PF_W, PF_R ]
452# NOBITS-NEXT:     FirstSec: .bss
453# NOBITS-NEXT:     LastSec:  .data.2
454# NOBITS-NEXT:     Offset:   0x159
455# NOBITS-NEXT:   - Type:     PT_LOAD
456# NOBITS-NEXT:     Flags:    [ PF_W, PF_R ]
457# NOBITS-NEXT:     FirstSec: .foo.bss
458# NOBITS-NEXT:     LastSec:  .bar.bss
459# NOBITS-NEXT:     VAddr:    0x200000000
460# NOBITS-NEXT:     Offset:   0x15A
461# NOBITS-NEXT: Sections:
462
463--- !ELF
464FileHeader:
465  Class: ELFCLASS64
466  Data:  ELFDATA2LSB
467  Type:  ET_EXEC
468ProgramHeaders:
469## Case 1: the segment contains a single SHT_NOBITS section.
470  - Type:     PT_LOAD
471    Flags:    [ PF_W, PF_R ]
472    FirstSec: .bss
473    LastSec:  .bss
474## Case 2: the SHT_NOBITS section is the last section in the segment.
475  - Type:     PT_LOAD
476    Flags:    [ PF_W, PF_R ]
477    FirstSec: .data.1
478    LastSec:  .bss
479## Case 3: the SHT_NOBITS section is in the middle of the segment.
480  - Type:     PT_LOAD
481    Flags:    [ PF_W, PF_R ]
482    FirstSec: .data.1
483    LastSec:  .data.2
484## Case 4: the SHT_NOBITS section is the first section in the segment.
485  - Type:     PT_LOAD
486    Flags:    [ PF_W, PF_R ]
487    FirstSec: .bss
488    LastSec:  .data.2
489## Case 5: another two SHT_NOBITS sections in a different segment.
490  - Type:     PT_LOAD
491    Flags:    [ PF_W, PF_R ]
492    FirstSec: .foo.bss
493    LastSec:  .bar.bss
494    VAddr:    0x200000000
495Sections:
496  - Name:    .data.1
497    Type:    SHT_PROGBITS
498    Flags:   [ SHF_WRITE, SHF_ALLOC ]
499## Use an arbitrary address and size.
500    Address: 0x1000
501    Size:    0x1
502  - Name:   .bss
503    Type:   SHT_NOBITS
504    Flags:  [ SHF_WRITE, SHF_ALLOC ]
505## Use a size that is larger than the file size.
506    ShSize: 0x00000000FFFFFFFF
507  - Name:  .data.2
508    Type:  SHT_PROGBITS
509    Flags: [ SHF_WRITE, SHF_ALLOC ]
510## Use an arbitrary size.
511    Size:  0x1
512  - Name:    .foo.bss
513    Type:    SHT_NOBITS
514    Flags:   [ SHF_WRITE, SHF_ALLOC ]
515## Set an arbitrary address and size so that this section can be used
516## to start a different non-overlapping segment.
517## I.e. its address is larger than addresses of previous sections.
518    Size:    0x10
519    Address: 0x200000000
520  - Name:    .bar.bss
521    Type:    SHT_NOBITS
522    Flags:   [ SHF_WRITE, SHF_ALLOC ]
523## Use an arbitrary size that is different to the size of
524## the previous section.
525    Size:    0x20
526
527## Check that we require sections in a program header
528## declaration to be sorted by their offsets.
529# RUN: not yaml2obj --docnum=6 %s -o %t6 2>&1 | \
530# RUN:   FileCheck %s --check-prefix=UNSORTED --implicit-check-not="error:"
531
532# UNSORTED:      error: program header with index 0: the section index of .bar is greater than the index of .foo
533# UNSORTED-NEXT: error: sections in the program header with index 3 are not sorted by their file offset
534
535--- !ELF
536FileHeader:
537  Class: ELFCLASS64
538  Data:  ELFDATA2LSB
539  Type:  ET_DYN
540ProgramHeaders:
541## Case 1: the .bar section is placed after the .foo section in the file.
542##         Check we report an error about the violation of the order.
543  - Type:     PT_LOAD
544    Flags:    [ PF_R ]
545    FirstSec: .bar
546    LastSec:  .foo
547    VAddr:    0x1000
548## There is nothing wrong with this segment. We have it to show that
549## we report correct program header indices in error messages.
550  - Type:     PT_LOAD
551    Flags:    [ PF_R ]
552    FirstSec: .foo
553    LastSec:  .bar
554    VAddr:    0x1000
555## Case 2: the .bar section is placed before the .zed section in the file,
556##         but the sh_offset of .zed is less than the sh_offset of
557##         the .bar section because of the "ShOffset" property.
558##         Document we report an error for such a case.
559  - Type:     PT_LOAD
560    Flags:    [ PF_R ]
561    FirstSec: .bar
562    LastSec:  .zed
563    VAddr:    0x1001
564Sections:
565  - Name:    .foo
566    Type:    SHT_PROGBITS
567    Flags:   [ SHF_ALLOC ]
568    Size:    0x1
569    Address: 0x1000
570  - Name:    .bar
571    Type:    SHT_PROGBITS
572    Flags:   [ SHF_ALLOC ]
573    Size:    0x1
574  - Name:     .zed
575    Type:     SHT_PROGBITS
576    Flags:    [ SHF_ALLOC ]
577    Size:     0x1
578    ShOffset: 0x0
579
580## Check how we dump segments which contain empty sections.
581# RUN: yaml2obj --docnum=7 %s -o %t7
582
583## Show the layout of the object before we dump it using obj2yaml.
584## Notes: 1) '.empty.foo', '.empty.bar1' and '.bar' have the same file offset, but '.empty.foo'
585##           has a VA that is outside of the segment, hence we should not include it in it.
586##        2) '.bar1' ends at 0x79, which is the starting file offset of both '.empty.bar2'
587##           and '.empty.zed'. We should only include '.empty.bar2', because the VA of the
588##           '.empty.zed' section is outside the segment's virtual space.
589# RUN: llvm-readelf -S %t7 | FileCheck %s --check-prefix=ZERO-SIZE-MAPPING
590
591# ZERO-SIZE-MAPPING:      Section Headers:
592# ZERO-SIZE-MAPPING-NEXT:   [Nr] Name        Type     Address          Off    Size
593# ZERO-SIZE-MAPPING:        [ 1] .empty.foo  PROGBITS 0000000000001000 000078 000000
594# ZERO-SIZE-MAPPING-NEXT:   [ 2] .empty.bar1 PROGBITS 0000000000002000 000078 000000
595# ZERO-SIZE-MAPPING-NEXT:   [ 3] .bar        PROGBITS 0000000000002000 000078 000001
596# ZERO-SIZE-MAPPING-NEXT:   [ 4] .empty.bar2 PROGBITS 0000000000002001 000079 000000
597# ZERO-SIZE-MAPPING-NEXT:   [ 5] .empty.zed  PROGBITS 0000000000003000 000079 000000
598
599# RUN: obj2yaml %t7 | FileCheck %s --check-prefix=ZERO-SIZE
600
601# ZERO-SIZE:      ProgramHeaders:
602# ZERO-SIZE-NEXT:   - Type:     PT_LOAD
603# ZERO-SIZE-NEXT:     Flags:    [ PF_W, PF_R ]
604# ZERO-SIZE-NEXT:     FirstSec: .empty.bar1
605# ZERO-SIZE-NEXT:     LastSec:  .empty.bar2
606# ZERO-SIZE-NEXT:     VAddr:    0x2000
607# ZERO-SIZE-NEXT:     Offset:   0x78
608# ZERO-SIZE-NEXT: Sections:
609
610--- !ELF
611FileHeader:
612  Class: ELFCLASS64
613  Data:  ELFDATA2LSB
614  Type:  ET_EXEC
615ProgramHeaders:
616  - Type:     PT_LOAD
617    Flags:    [ PF_W, PF_R ]
618    FirstSec: .bar
619    LastSec:  .bar
620    VAddr:    0x2000
621Sections:
622  - Name:    .empty.foo
623    Type:    SHT_PROGBITS
624    Flags:   [ SHF_ALLOC, SHF_EXECINSTR ]
625    Address: 0x1000
626  - Name:    .empty.bar1
627    Type:    SHT_PROGBITS
628    Flags:   [ SHF_ALLOC, SHF_EXECINSTR ]
629    Address: 0x2000
630  - Name:    .bar
631    Type:    SHT_PROGBITS
632    Flags:   [ SHF_WRITE, SHF_ALLOC ]
633    Address: 0x2000
634    Size:    0x1
635  - Name:    .empty.bar2
636    Type:    SHT_PROGBITS
637    Flags:   [ SHF_ALLOC, SHF_EXECINSTR ]
638    Address: 0x2001
639  - Name:    .empty.zed
640    Type:    SHT_PROGBITS
641    Flags:   [ SHF_ALLOC, SHF_EXECINSTR ]
642    Address: 0x3000
643
644## Check how we dump a segment when we have sections that are outside of the virtual
645## address space of a segment, but inside its file space. We do not include such sections
646## in a segment when they are at the edges of a segment, because this is a normal case and
647## it may mean they belong to a different segment.
648# RUN: yaml2obj --docnum=8 %s -o %t8
649# RUN: obj2yaml %t8 | FileCheck %s --check-prefix=BROKEN-VA
650
651# BROKEN-VA:      ProgramHeaders:
652# BROKEN-VA-NEXT:  - Type:     PT_LOAD
653# BROKEN-VA-NEXT:    Flags:    [ PF_W, PF_R ]
654# BROKEN-VA-NEXT:    FirstSec: .empty_middle
655# BROKEN-VA-NEXT:    LastSec:  .empty_middle
656# BROKEN-VA-NEXT:    VAddr:    0x1000
657
658--- !ELF
659FileHeader:
660  Class: ELFCLASS64
661  Data:  ELFDATA2LSB
662  Type:  ET_EXEC
663ProgramHeaders:
664  - Type:     PT_LOAD
665    Flags:    [ PF_W, PF_R ]
666    VAddr:    0x1000
667    FirstSec: .empty_begin
668    LastSec:  .empty_end
669Sections:
670  - Name:    .empty_begin
671    Type:    SHT_PROGBITS
672    Flags:   [ SHF_ALLOC, SHF_EXECINSTR ]
673    Address: 0xFEFEFEFE
674  - Type:    Fill
675    Pattern: "00"
676    Size:    1
677    Name:    begin
678  - Name:    .empty_middle
679    Type:    SHT_PROGBITS
680    Flags:   [ SHF_ALLOC, SHF_EXECINSTR ]
681    Address: 0xFEFEFEFE
682  - Type:    Fill
683    Pattern: "00"
684    Size:    1
685  - Name:    .empty_end
686    Type:    SHT_PROGBITS
687    Flags:   [ SHF_ALLOC, SHF_EXECINSTR ]
688    Address: 0xFEFEFEFE
689