1## In this test we check that we allocate file space for SHT_NOBITS sections when
2## there are non-nobits sections in the same segment after them. When an object has
3## multiple segments, we check each and allocate the space if at least one matches the
4## condition mentioned.
5
6## Case A. In this case there are no non-nobits sections after SHT_NOBITS sections in segments.
7##         Because of this the file space for SHT_NOBITS sections is not allocated.
8# RUN: yaml2obj %s -D SEC1=.data.before -D SEC2=.nobits.1 -o %t1
9# RUN: llvm-readelf --sections --segments %t1 | FileCheck %s --check-prefix=NO-ALLOC
10
11# NO-ALLOC:      [Nr] Name         Type     Address          Off    Size
12# NO-ALLOC:      [ 1] .data.before PROGBITS 0000000000000000 0000b0 000001
13# NO-ALLOC-NEXT: [ 2] .nobits.1    NOBITS   0000000000000001 0000b1 000002
14# NO-ALLOC-NEXT: [ 3] .data.after  PROGBITS 0000000000000003 0000b1 000003
15# NO-ALLOC-NEXT: [ 4] .nobits.2    NOBITS   0000000000000006 0000b4 000004
16## .fill of size 0x5 is placed here.
17# NO-ALLOC-NEXT: [ 5] .data.last   PROGBITS 000000000000000f 0000b9 000006
18
19# NO-ALLOC:       Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz
20# NO-ALLOC-NEXT:  LOAD 0x0000b0 0x0000000000000000 0x0000000000000000 0x000001 0x000003
21# NO-ALLOC-NEXT:  LOAD 0x0000b0 0x0000000000000000 0x0000000000000000 0x000001 0x000003
22
23## Case B. We have a segment that has a non-nobits section after the SHT_NOBITS section ".nobits.1".
24##         The file space is allocated for it, but not for ".nobits.2",
25##         which does not belong to any segment.
26# RUN: yaml2obj %s -D SEC1=.nobits.1 -D SEC2=.data.after -o %t2
27# RUN: llvm-readelf --sections --segments %t2 | FileCheck %s --check-prefix=ALLOC-FIRST
28
29# ALLOC-FIRST:      [Nr] Name         Type     Address          Off    Size
30# ALLOC-FIRST:      [ 1] .data.before PROGBITS 0000000000000000 0000b0 000001
31# ALLOC-FIRST-NEXT: [ 2] .nobits.1    NOBITS   0000000000000001 0000b1 000002
32# ALLOC-FIRST-NEXT: [ 3] .data.after  PROGBITS 0000000000000003 0000b3 000003
33# ALLOC-FIRST-NEXT: [ 4] .nobits.2    NOBITS   0000000000000006 0000b6 000004
34## .fill of size 0x5 is placed here.
35# ALLOC-FIRST-NEXT: [ 5] .data.last   PROGBITS 000000000000000f 0000bb 000006
36
37# ALLOC-FIRST:       Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz
38# ALLOC-FIRST-NEXT:  LOAD 0x0000b0 0x0000000000000000 0x0000000000000000 0x000001 0x000003
39# ALLOC-FIRST-NEXT:  LOAD 0x0000b1 0x0000000000000000 0x0000000000000000 0x000005 0x000005
40
41## Case C. We have a Fill after the ".nobits.2" section. In this case the file space is
42##         allocated for it, because fills are handled just like any other non-nobits sections.
43# RUN: yaml2obj %s -D SEC1=.nobits.2 -D SEC2=.fill -o %t3
44# RUN: llvm-readelf --sections --segments %t3 | FileCheck %s --check-prefix=FILL-AT-END
45
46# FILL-AT-END: [Nr] Name       Type     Address          Off    Size
47# FILL-AT-END: [ 4] .nobits.2  NOBITS   0000000000000006 0000b4 000004
48## .fill of size 0x5 is placed here.
49# FILL-AT-END: [ 5] .data.last PROGBITS 000000000000000f 0000bd 000006
50
51# FILL-AT-END:      Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz
52# FILL-AT-END-NEXT: LOAD 0x0000b0 0x0000000000000000 0x0000000000000000 0x000001 0x000003
53# FILL-AT-END-NEXT: LOAD 0x0000b4 0x0000000000000000 0x0000000000000000 0x000009 0x000009
54
55--- !ELF
56FileHeader:
57  Class: ELFCLASS64
58  Data:  ELFDATA2LSB
59  Type:  ET_EXEC
60Sections:
61  - Name:  .data.before
62    Type:  SHT_PROGBITS
63    Flags: [ SHF_ALLOC ]
64    Size:  0x1
65  - Name:  .nobits.1
66    Type:  SHT_NOBITS
67    Flags: [ SHF_ALLOC ]
68    Size:  0x2
69  - Name:  .data.after
70    Type:  SHT_PROGBITS
71    Flags: [ SHF_ALLOC ]
72    Size:  0x3
73  - Name:  .nobits.2
74    Type:  SHT_NOBITS
75    Flags: [ SHF_ALLOC ]
76    Size:  0x4
77  - Type:    Fill
78    Name:    .fill
79    Pattern: "00"
80    Size:    5
81  - Name:  .data.last
82    Type:  SHT_PROGBITS
83    Flags: [ SHF_ALLOC ]
84    Size:  0x6
85ProgramHeaders:
86## We have 2 segments, the first is predefined and the second can be customized.
87## We want to have more than one segment to show we check all of them when
88## trying to find a non-nobits section after a nobits one.
89  - Type:     PT_LOAD
90    Flags:    [ PF_R ]
91    FirstSec: .data.before
92    LastSec:  .nobits.1
93  - Type:     PT_LOAD
94    Flags:    [ PF_R ]
95    FirstSec: [[SEC1]]
96    LastSec:  [[SEC2]]
97
98## Case D. We have a segment with SHT_NOBITS sections on its borders and one
99##         non-nobits in the middle. Check we allocate the file space only for
100##         the first nobits section.
101
102# RUN: yaml2obj --docnum=2 %s -o %t4
103# RUN: llvm-readelf --sections --segments %t4 | FileCheck %s --check-prefix=MIDDLE
104
105# MIDDLE:      [Nr] Name      Type     Address          Off    Size
106# MIDDLE:      [ 1] .nobits.1 NOBITS   0000000000000000 000078 000001
107# MIDDLE-NEXT: [ 2] .data     PROGBITS 0000000000000001 000079 000010
108# MIDDLE-NEXT: [ 3] .nobits.2 NOBITS   0000000000000011 000089 000100
109# MIDDLE-NEXT: [ 4] .strtab   STRTAB   0000000000000000 000089 000001
110
111# MIDDLE:      Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz
112# MIDDLE-NEXT: LOAD 0x000078 0x0000000000000000 0x0000000000000000 0x000011 0x000111
113
114--- !ELF
115FileHeader:
116  Class: ELFCLASS64
117  Data:  ELFDATA2LSB
118  Type:  ET_EXEC
119Sections:
120  - Name:  .nobits.1
121    Type:  SHT_NOBITS
122    Flags: [ SHF_ALLOC ]
123    Size:  0x1
124  - Name:  .data
125    Type:  SHT_PROGBITS
126    Flags: [ SHF_ALLOC ]
127    Size:  0x10
128  - Name:  .nobits.2
129    Type:  SHT_NOBITS
130    Flags: [ SHF_ALLOC ]
131    Size:  0x100
132ProgramHeaders:
133  - Type:     PT_LOAD
134    Flags:    [ PF_R ]
135    FirstSec: .nobits.1
136    LastSec:  .nobits.2
137