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