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