1 ## Show that obj2yaml is able to dump program headers.
3 ## Part I. Base check. All simple cases that look OK as a part of a single large test live here.
5 # RUN: yaml2obj %s -o %t1
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
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 {{$}}
35 ## Check that obj2yaml produced a correct program headers description.
37 # RUN: obj2yaml %t1 | FileCheck %s --check-prefix=YAML
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:
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.
113 ## Check we can create a PT_LOAD with a different set of properties and sections.
115 Flags: [ PF_X, PF_R ]
120 ## Create a PT_LOAD to demonstate we are able to refer to output sections with the same name.
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).
130 Flags: [ PF_W, PF_R ]
132 LastSec: .dynamic.tail
135 ## Show we can create a nested dynamic segment and put a section into it.
137 Flags: [ PF_W, PF_R ]
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.
154 ## Show we can dump a standalone empty segment.
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.
168 ## Test we are able to dump duplicated segments.
169 ## We use a segment that is the same as the previous one for this.
199 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
204 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
219 Flags: [ SHF_WRITE, SHF_ALLOC ]
220 Address: 0x0000000000003EF0
225 - Name: .dynamic.tail
227 Flags: [ SHF_WRITE, SHF_ALLOC ]
232 ## Part II. More specific tests.
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
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:
273 Flags: [ PF_W, PF_R ]
274 FirstSec: .empty.tls.start
275 LastSec: .empty.tls.end
279 Flags: [ PF_W, PF_R ]
280 FirstSec: .empty.tls.start
281 LastSec: .empty.tls.start
285 Flags: [ PF_W, PF_R ]
286 FirstSec: .empty.tls.middle
287 LastSec: .empty.tls.middle
291 Flags: [ PF_W, PF_R ]
292 FirstSec: .empty.tls.end
293 LastSec: .empty.tls.end
297 - Name: .empty.tls.start
299 Flags: [ SHF_ALLOC, SHF_TLS ]
306 - Name: .empty.tls.middle
308 Flags: [ SHF_ALLOC, SHF_TLS ]
314 - Name: .empty.tls.end
316 Flags: [ SHF_ALLOC, SHF_TLS ]
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
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
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:
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
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:
398 LastSec: .non-alloc.1
402 LastSec: .non-alloc.2
405 FirstSec: .non-alloc.2
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
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:
469 ## Case 1: the segment contains a single SHT_NOBITS section.
471 Flags: [ PF_W, PF_R ]
474 ## Case 2: the SHT_NOBITS section is the last section in the segment.
476 Flags: [ PF_W, PF_R ]
479 ## Case 3: the SHT_NOBITS section is in the middle of the segment.
481 Flags: [ PF_W, PF_R ]
484 ## Case 4: the SHT_NOBITS section is the first section in the segment.
486 Flags: [ PF_W, PF_R ]
489 ## Case 5: another two SHT_NOBITS sections in a different segment.
491 Flags: [ PF_W, PF_R ]
498 Flags: [ SHF_WRITE, SHF_ALLOC ]
499 ## Use an arbitrary address and size.
504 Flags: [ SHF_WRITE, SHF_ALLOC ]
505 ## Use a size that is larger than the file size.
506 ShSize: 0x00000000FFFFFFFF
509 Flags: [ SHF_WRITE, SHF_ALLOC ]
510 ## Use an arbitrary size.
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.
522 Flags: [ SHF_WRITE, SHF_ALLOC ]
523 ## Use an arbitrary size that is different to the size of
524 ## the previous section.
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:"
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
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.
548 ## There is nothing wrong with this segment. We have it to show that
549 ## we report correct program header indices in error messages.
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.
580 ## Check how we dump segments which contain empty sections.
581 # RUN: yaml2obj --docnum=7 %s -o %t7
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
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
599 # RUN: obj2yaml %t7 | FileCheck %s --check-prefix=ZERO-SIZE
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:
617 Flags: [ PF_W, PF_R ]
624 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
628 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
632 Flags: [ SHF_WRITE, SHF_ALLOC ]
637 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
641 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
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
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
665 Flags: [ PF_W, PF_R ]
667 FirstSec: .empty_begin
672 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
678 - Name: .empty_middle
680 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
687 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]