xref: /llvm-project/llvm/test/tools/yaml2obj/ELF/program-header-size-offset.yaml (revision a7a447be0fa934505f0c423fb97b91a68c1cc715)
1## Show that yaml2obj properly emits program headers with explicit file size,
2## memory size and offset parameters.
3
4# RUN: yaml2obj --docnum=1 %s -o %t1
5# RUN: llvm-readobj %t1 --program-headers | FileCheck %s
6
7# CHECK: ProgramHeaders [
8# CHECK:    Offset: 0x1234
9# CHECK:    FileSize: 1111
10# CHECK:    MemSize: 9999
11
12# CHECK:    Offset: 0x2000
13# CHECK:    FileSize: 6
14# CHECK:    MemSize: 4
15
16# CHECK:    Offset: 0x2000
17# CHECK:    FileSize: 4
18# CHECK:    MemSize: 6
19
20# CHECK:    Offset: 0x1FFF
21# CHECK:    FileSize: 5
22# CHECK:    MemSize: 5
23
24# CHECK:    Offset: 0xFFE
25# CHECK:    FileSize: 7
26# CHECK:    MemSize: 9
27
28# CHECK:    Offset: 0x3000
29# CHECK:    FileSize: 3
30# CHECK:    MemSize: 2
31
32# CHECK:    Offset: 0x2004
33## Offset of .nobits2 (0x2009) - offset of .data (0x2004) == 0x5.
34# CHECK:    FileSize: 5
35# CHECK:    MemSize: 6
36# CHECK: ]
37
38--- !ELF
39FileHeader:
40  Class: ELFCLASS64
41  Data:  ELFDATA2LSB
42  Type:  ET_EXEC
43Sections:
44  - Name: .text
45    Type: SHT_PROGBITS
46    Size: 4
47    ShOffset: 0x1000
48    AddressAlign: 0x1000
49  - Name: .rodata
50    Type: SHT_PROGBITS
51    Size: 4
52    ShOffset: 0x2000
53    AddressAlign: 0x1000
54  - Name: .data
55    Type: SHT_PROGBITS
56    ShOffset: 0x2004
57    Size: 4
58  - Name: .nobits1
59    Type: SHT_NOBITS
60    ShOffset: 0x2008
61    Size: 1
62  - Name: .nobits2
63    Type: SHT_NOBITS
64    # Intentionally set to 0x2009 though the previous section is SHT_NOBITS.
65    ShOffset: 0x2009
66    Size: 1
67ProgramHeaders:
68  # Program header with no sections.
69  - Type:     0x6abcdef0 # arbitrary type
70    Offset:   0x1234
71    FileSize: 1111
72    MemSize:  9999
73  # Program header with only file size set.
74  - Type:     0x6abcdef0
75    FileSize: 6
76    FirstSec: .rodata
77    LastSec:  .rodata
78  # Program header with only mem size set.
79  - Type:     0x6abcdef0
80    MemSize:  6
81    FirstSec: .rodata
82    LastSec:  .rodata
83  # Program header with only offset set.
84  - Type:     0x6abcdef0
85    Offset:   0x1fff
86    FirstSec: .rodata
87    LastSec:  .rodata
88  # Program header with sections, valid properties.
89  - Type:     0x6abcdef0
90    Offset:   0xffe
91    FileSize: 7
92    MemSize:  9
93    FirstSec: .text
94    LastSec:  .text
95  # Program header with invalid properties.
96  - Type:     0x6abcdef0
97    Offset:   0x3000
98    FileSize: 3
99    MemSize:  2
100  # Program header with 2 SHT_NOBITS sections.
101  - Type:     0x6abcdef0
102    Offset:   0x2004
103    FirstSec: .data
104    LastSec:  .nobits2
105
106## Test the "Offset" property.
107
108## Check that by default the p_offset field of a segment is set to the
109## offset of the section with the minimum offset.
110# RUN: yaml2obj --docnum=2 %s -o %t2
111# RUN: llvm-readelf %t2 --sections --program-headers | \
112# RUN:   FileCheck %s --check-prefixes=DEFAULT-OFFSET
113
114# DEFAULT-OFFSET:      [Nr] Name Type     Address          Off
115# DEFAULT-OFFSET:      [ 1] .foo PROGBITS 0000000000001000 0000b0
116# DEFAULT-OFFSET-NEXT: [ 2] .bar PROGBITS 0000000000001001 0000b1
117
118# DEFAULT-OFFSET:       Type Offset
119# DEFAULT-OFFSET-NEXT:  LOAD 0x0000b0
120# DEFAULT-OFFSET-NEXT:  LOAD 0x0000b1
121
122--- !ELF
123FileHeader:
124  Class: ELFCLASS64
125  Data:  ELFDATA2LSB
126  Type:  ET_EXEC
127Sections:
128  - Name:    .foo
129    Type:    SHT_PROGBITS
130    Flags:   [ SHF_ALLOC ]
131    Size:    0x1
132    Address: 0x1000
133  - Name:  .bar
134    Type:  SHT_PROGBITS
135    Flags: [ SHF_ALLOC ]
136    Size:  0x1
137ProgramHeaders:
138  - Type:     PT_LOAD
139    FirstSec: .foo
140    LastSec:  .bar
141  - Type:     PT_LOAD
142    FirstSec: .bar
143    LastSec:  .bar
144
145## Check we can set the "Offset" value explicitly to be less than or equal to
146## the offset of a section in the segment.
147# RUN: yaml2obj --docnum=3 -DOFFSET=0x77 %s -o %t3
148# RUN: llvm-readelf %t3 --sections --program-headers | \
149# RUN:   FileCheck %s --check-prefixes=VALID-OFFSET,VALID-OFFSET-LESS
150# RUN: yaml2obj --docnum=3 -DOFFSET=0x78 %s -o %t4
151# RUN: llvm-readelf %t4 --sections --program-headers | \
152# RUN:   FileCheck %s --check-prefixes=VALID-OFFSET,VALID-OFFSET-EQ
153
154# VALID-OFFSET: [Nr] Name Type     Address          Off
155# VALID-OFFSET: [ 1] .foo PROGBITS 0000000000000000 000078
156
157# VALID-OFFSET:      Type Offset
158# VALID-OFFSET-EQ:   LOAD 0x000078
159# VALID-OFFSET-LESS: LOAD 0x000077
160
161--- !ELF
162FileHeader:
163  Class: ELFCLASS64
164  Data:  ELFDATA2LSB
165  Type:  ET_EXEC
166Sections:
167  - Name:  .foo
168    Type:  SHT_PROGBITS
169    Flags: [ SHF_ALLOC ]
170    Size:  0x1
171ProgramHeaders:
172  - Type:    PT_LOAD
173    Offset:  [[OFFSET]]
174    FirstSec: .foo
175    LastSec:  .foo
176
177## Check we report an error when the "Offset" value is larger than the offset of a section in the segment.
178# RUN: not yaml2obj --docnum=3 -DOFFSET=0x79 %s -o /dev/null 2>&1 | \
179# RUN:   FileCheck %s --check-prefix=INVALID-OFFSET
180
181# INVALID-OFFSET: yaml2obj: error: 'Offset' for segment with index 1 must be less than or equal to the minimum file offset of all included sections (0x78)
182
183## Document that the "Offset" value is checked after the section offset is overriden using "ShOffset".
184# RUN: yaml2obj --docnum=4 %s -o %t5
185# RUN: llvm-readelf %t5 --sections --program-headers | FileCheck %s --check-prefix=SHOFFSET
186
187# SHOFFSET: [Nr] Name Type     Address          Off
188# SHOFFSET: [ 1] .foo PROGBITS 0000000000000000 ffffffff
189
190# SHOFFSET:      Type Offset
191# SHOFFSET-NEXT: LOAD 0xffffff00
192
193--- !ELF
194FileHeader:
195  Class: ELFCLASS64
196  Data:  ELFDATA2LSB
197  Type:  ET_EXEC
198Sections:
199  - Name:     .foo
200    Type:     SHT_PROGBITS
201    Flags:    [ SHF_ALLOC ]
202    Size:     0x1
203## Note: the real .foo offset is much less than 0xFFFFFFFF or
204##       0xFFFFFF00, but no error is reported.
205    ShOffset: 0xFFFFFFFF
206ProgramHeaders:
207  - Type:     PT_LOAD
208    Offset:   0xFFFFFF00
209    FirstSec: .foo
210    LastSec:  .foo
211