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