1# RUN: yaml2obj --docnum=1 %s -o %t
2# RUN: llvm-readobj -l %t | FileCheck %s
3
4--- !ELF
5FileHeader:
6  Class: ELFCLASS64
7  Data:  ELFDATA2LSB
8  Type:  ET_EXEC
9Sections:
10  - Name:            .text
11    Type:            SHT_PROGBITS
12    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
13    AddressAlign:    0x0000000000001000
14    Content:         "00000000"
15  - Name:            .init
16    Type:            SHT_PROGBITS
17    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
18    Content:         "00000000"
19    AddressAlign:    0x0000000000000010
20  - Name:            .data
21    Type:            SHT_PROGBITS
22    Flags:           [ SHF_ALLOC ]
23    Content:         "00000000"
24    AddressAlign:    0x0000000000001000
25ProgramHeaders:
26  - Type:     PT_LOAD
27    Flags:    [ PF_X, PF_R ]
28    VAddr:    0xAAAA1000
29    PAddr:    0xFFFF1000
30    FirstSec: .text
31    LastSec:  .init
32  - Type:     PT_LOAD
33    Flags:    [ PF_R ]
34    VAddr:    0xAAAA2000
35    PAddr:    0xFFFF2000
36    FirstSec: .data
37    LastSec:  .data
38  - Type: PT_GNU_EH_FRAME
39  - Type: PT_GNU_STACK
40  - Type: PT_GNU_RELRO
41  - Type: PT_GNU_PROPERTY
42
43#CHECK:     ProgramHeaders [
44#CHECK-NEXT:   ProgramHeader {
45#CHECK-NEXT:    Type: PT_LOAD
46#CHECK-NEXT:    Offset: 0x1000
47#CHECK-NEXT:    VirtualAddress: 0xAAAA1000
48#CHECK-NEXT:    PhysicalAddress: 0xFFFF1000
49#CHECK-NEXT:    FileSize: 20
50#CHECK-NEXT:    MemSize: 20
51#CHECK-NEXT:    Flags [
52#CHECK-NEXT:      PF_R
53#CHECK-NEXT:      PF_X
54#CHECK-NEXT:    ]
55#CHECK-NEXT:    Alignment: 4096
56#CHECK-NEXT:  }
57#CHECK-NEXT:  ProgramHeader {
58#CHECK-NEXT:    Type: PT_LOAD
59#CHECK-NEXT:    Offset: 0x2000
60#CHECK-NEXT:    VirtualAddress: 0xAAAA2000
61#CHECK-NEXT:    PhysicalAddress: 0xFFFF2000
62#CHECK-NEXT:    FileSize: 4
63#CHECK-NEXT:    MemSize: 4
64#CHECK-NEXT:    Flags [
65#CHECK-NEXT:      PF_R
66#CHECK-NEXT:    ]
67#CHECK-NEXT:    Alignment: 4096
68#CHECK-NEXT:  }
69#CHECK-NEXT:  ProgramHeader {
70#CHECK-NEXT:    Type: PT_GNU_EH_FRAME (0x6474E550)
71#CHECK:       }
72#CHECK-NEXT:  ProgramHeader {
73#CHECK-NEXT:    Type: PT_GNU_STACK (0x6474E551)
74#CHECK:       }
75#CHECK-NEXT:  ProgramHeader {
76#CHECK-NEXT:    Type: PT_GNU_RELRO (0x6474E552)
77#CHECK:       }
78#CHECK-NEXT:  ProgramHeader {
79#CHECK-NEXT:    Type: PT_GNU_PROPERTY (0x6474E553)
80#CHECK:       }
81#CHECK-NEXT:]
82
83## Check we do not allow referencing sections that do not exist.
84# RUN: not yaml2obj -DFIRST=".unknown1" -DLAST=".unknown2" --docnum=2 %s 2>&1 | \
85# RUN:   FileCheck %s --check-prefix=UNKNOWN-ERR
86
87# UNKNOWN-ERR: error: unknown section or fill referenced: '.unknown1' by the 'FirstSec' key of the program header with index 0
88# UNKNOWN-ERR: error: unknown section or fill referenced: '.unknown2' by the 'LastSec' key of the program header with index 0
89
90--- !ELF
91FileHeader:
92  Class: ELFCLASS64
93  Data:  ELFDATA2LSB
94  Type:  ET_EXEC
95Sections:
96  - Name:   .foo
97    Type:   SHT_PROGBITS
98    Offset: 0x100
99    Size:   0x10
100  - Name:   .bar
101    Type:   SHT_PROGBITS
102    Offset: 0x200
103    Size:   0x20
104  - Name:   .zed
105    Type:   SHT_PROGBITS
106    Offset: 0x300
107    Size:   0x30
108  - Name:   .fill1
109    Type:   Fill
110    Offset: 0x400
111    Size:   0x40
112  - Name:   .fill2
113    Type:   Fill
114    Offset: 0x500
115    Size:   0x50
116ProgramHeaders:
117  - Type:     PT_LOAD
118    FirstSec: [[FIRST=<none>]]
119    LastSec:  [[LAST=<none>]]
120
121## Check we report an error when the index of the section specified by the "FirstSec" key
122## is greater than the index of the section specified by the "LastSec" key.
123
124# RUN: not yaml2obj -DFIRST=".bar" -DLAST=".foo" --docnum=2 %s 2>&1 | \
125# RUN:   FileCheck %s --check-prefix=ORDER-ERR
126
127# ORDER-ERR: error: program header with index 0: the section index of .bar is greater than the index of .foo
128
129## Check that we can't use the "LastSec" key without the "FirstSec" key.
130
131# RUN: not yaml2obj -DLAST=".foo" --docnum=2 %s 2>&1 | \
132# RUN:   FileCheck %s --check-prefix=ONLY-LAST-ERR
133
134# ONLY-LAST-ERR: error: the "LastSec" key can't be used without the "FirstSec" key
135
136## Check that we can't use the "FirstSec" key without the "LastSec" key.
137
138# RUN: not yaml2obj -DFIRST=".foo" --docnum=2 %s 2>&1 | \
139# RUN:   FileCheck %s --check-prefix=ONLY-FIRST-ERR
140
141# ONLY-FIRST-ERR: error: the "FirstSec" key can't be used without the "LastSec" key
142
143## Check we create an empty segment when neither "FirstSec" nor "LastSec" are specified.
144
145# RUN: yaml2obj --docnum=2 %s -o %t3
146# RUN: llvm-readelf --program-headers %t3 | FileCheck %s --check-prefix=EMPTY-SEGMENT
147
148# EMPTY-SEGMENT:       Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz
149# EMPTY-SEGMENT:       LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000
150# EMPTY-SEGMENT-EMPTY:
151
152## Check that we include all sections between FirstSec and LastSec in the segment when both keys are used.
153
154# RUN: yaml2obj --docnum=2 -DFIRST=".foo" -DLAST=".zed" %s -o %t4
155# RUN: llvm-readelf --program-headers %t4 | FileCheck %s --check-prefix=BOTH
156
157# BOTH:      Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz
158# BOTH-NEXT: LOAD 0x000100 0x0000000000000000 0x0000000000000000 0x000230 0x000230
159
160## Check that we include fills that are between FirstSec and LastSec in the segment when both keys are used.
161
162# RUN: yaml2obj --docnum=2 -DFIRST=".foo" -DLAST=".fill1" %s -o %t5a
163# RUN: llvm-readelf --program-headers %t5a | FileCheck %s --check-prefix=FILL1
164
165# FILL1: Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz
166# FILL1: LOAD 0x000100 0x0000000000000000 0x0000000000000000 0x000340 0x000340
167
168# RUN: yaml2obj --docnum=2 -DFIRST=".foo" -DLAST=".fill2" %s -o %t5b
169# RUN: llvm-readelf --program-headers %t5b | FileCheck %s --check-prefix=FILL2
170
171# FILL2: Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz
172# FILL2: LOAD 0x000100 0x0000000000000000 0x0000000000000000 0x000450 0x000450
173