1# We want to preserve areas in segments that are not covered by section headers.
2# This test shows that we do this for areas at the start of a segment, between
3# sections in a segment, and after all sections in a segment.
4# To create inputs with arbitrary data in segments, not covered by sections, we
5# use yaml2obj to create segments with sections covering all areas, then remove
6# some sections in those segments, and finally write over the areas of the
7# removed sections using python.
8
9# blob* sections are the sections that will be removed to create unlabelled
10# areas and then overwritten with data to show we preserve the data.
11
12# RUN: yaml2obj %s -o %t.base
13# RUN: llvm-objcopy %t.base %t.stripped --regex -R blob.*
14# Show that the removal leaves the bytes as zeroes, as desired, for all our
15# test cases.
16# RUN: od -t x1 -j 0x2000 -N 28 %t.stripped | FileCheck %s --ignore-case --check-prefix=CHECK1 -DPATTERN="00 00 00 00"
17# RUN: od -t x1 -j 0x2100 -N 12 %t.stripped | FileCheck %s --ignore-case --check-prefix=CHECK2 -DPATTERN="00 00 00 00"
18# RUN: od -t x1 -j 0x2200 -N 4  %t.stripped | FileCheck %s --ignore-case --check-prefix=CHECK3 -DPATTERN="00 00 00 00"
19# RUN: od -t x1 -j 0x2300 -N 12 %t.stripped | FileCheck %s --ignore-case --check-prefix=CHECK4 -DPATTERN="00 00 00 00"
20# RUN: od -t x1 -j 0x3000 -N 68 %t.stripped | FileCheck %s --ignore-case --check-prefix=CHECK5 -DPATTERN="00 00 00 00"
21# RUN: od -t x1 -j 0x4000 -N 60 %t.stripped | FileCheck %s --ignore-case --check-prefix=CHECK6 -DPATTERN="00 00 00 00"
22# RUN: od -t x1 -j 0x5000 -N 60 %t.stripped | FileCheck %s --ignore-case --check-prefix=CHECK7 -DPATTERN="00 00 00 00"
23
24# RUN: cp %t.stripped %t.in
25# RUN: echo "with open('%/t.in', 'rb+') as input:"                                 > %t.py
26# RUN: echo "  for offset in ["                                                   >> %t.py
27# RUN: echo "   0x2000, 0x2008, 0x200C, 0x2018, 0x2104, 0x2300,"                  >> %t.py
28# RUN: echo "   0x3008, 0x3010, 0x3018, 0x3020, 0x3028, 0x302C, 0x3034, 0x303C,"  >> %t.py
29# RUN: echo "   0x4000, 0x4008, 0x4010, 0x4014, 0x401C, 0x4024, 0x4034,"          >> %t.py
30# RUN: echo "   0x5000, 0x5008, 0x5010, 0x501C, 0x5024, 0x502C, 0x5030, 0x5038]:" >> %t.py
31# RUN: echo "    input.seek(offset)"                                              >> %t.py
32# RUN: echo "    input.write(bytearray.fromhex('DEADBEEF'))"                      >> %t.py
33# RUN: %python %t.py
34# RUN: llvm-objcopy %t.in %t.out
35# RUN: od -t x1 -j 0x2000 -N 28 %t.out | FileCheck %s --ignore-case --check-prefix=CHECK1 -DPATTERN="de ad be ef"
36# RUN: od -t x1 -j 0x2100 -N 12 %t.out | FileCheck %s --ignore-case --check-prefix=CHECK2 -DPATTERN="de ad be ef"
37# RUN: od -t x1 -j 0x2200 -N 4  %t.out | FileCheck %s --ignore-case --check-prefix=CHECK3 -DPATTERN="de ad be ef"
38# RUN: od -t x1 -j 0x2300 -N 12 %t.out | FileCheck %s --ignore-case --check-prefix=CHECK4 -DPATTERN="de ad be ef"
39# RUN: od -t x1 -j 0x3000 -N 68 %t.out | FileCheck %s --ignore-case --check-prefix=CHECK5 -DPATTERN="de ad be ef"
40# RUN: od -t x1 -j 0x4000 -N 60 %t.out | FileCheck %s --ignore-case --check-prefix=CHECK6 -DPATTERN="de ad be ef"
41# RUN: od -t x1 -j 0x5000 -N 60 %t.out | FileCheck %s --ignore-case --check-prefix=CHECK7 -DPATTERN="de ad be ef"
42
43# CHECK1:      [[PATTERN]] 11 22 33 44 [[PATTERN]] [[PATTERN]]
44# CHECK1-NEXT: 00 00 00 00 55 66 77 88 [[PATTERN]]
45# CHECK2:      99 00 aa bb [[PATTERN]] cc dd ee ff
46# CHECK3:      fe fe fe fe
47# CHECK4:      [[PATTERN]] 00 00 00 00 00 00 00 00
48# CHECK5:      ff ff ee ee dd dd cc cc [[PATTERN]] bb bb aa aa
49# CHECK5-NEXT: [[PATTERN]] 00 00 99 99 [[PATTERN]] 88 88 77 77
50# CHECK5-NEXT: [[PATTERN]] 66 66 55 55 [[PATTERN]] [[PATTERN]]
51# CHECK5-NEXT: 44 44 33 33 [[PATTERN]] 22 22 11 11 [[PATTERN]]
52# CHECK5-NEXT: 00 11 22 33
53# CHECK6:      [[PATTERN]] 44 55 66 77 [[PATTERN]] 88 99 aa bb
54# CHECK6-NEXT: [[PATTERN]] [[PATTERN]] cc dd ee ff [[PATTERN]]
55# CHECK6-NEXT: ff ee dd cc [[PATTERN]] bb aa 99 88 77 66 55 44
56# CHECK6-NEXT: 33 22 11 00 [[PATTERN]] 11 11 11 11
57# CHECK7:      [[PATTERN]] 12 34 56 78 [[PATTERN]] 90 ab cd ef
58# CHECK7-NEXT: [[PATTERN]] fe dc ba 09 87 65 43 21 [[PATTERN]]
59# CHECK7-NEXT: 22 22 22 22 [[PATTERN]] 33 33 33 33 [[PATTERN]]
60# CHECK7-NEXT: [[PATTERN]] 44 44 44 44 [[PATTERN]]
61
62--- !ELF
63FileHeader:
64  Class:   ELFCLASS64
65  Data:    ELFDATA2LSB
66  Type:    ET_EXEC
67  Machine: EM_X86_64
68Sections:
69  - Name: blob1
70    Type: SHT_PROGBITS
71    Content: 'abbababa'
72    Address: 0x2000
73    AddressAlign: 0x2000
74  - Name: section1
75    Type: SHT_PROGBITS
76    Address: 0x2004
77    Content: '11223344'
78  - Name: blob2
79    Type: SHT_PROGBITS
80    Content: 'abbababa'
81    Address: 0x2008
82  - Name: section2
83    Type: SHT_NOBITS
84    Size: 4
85    Address: 0x200C
86  - Name: blob3
87    Type: SHT_PROGBITS
88    Content: 'abbababa'
89    Address: 0x2010
90  - Name: section3
91    Type: SHT_PROGBITS
92    Content: '55667788'
93    Address: 0x2014
94  - Name: blob4
95    Type: SHT_PROGBITS
96    Content: 'abbababa'
97    Address: 0x2018
98  - Name: section4
99    Type: SHT_PROGBITS
100    Content: '9900aabb'
101    Address: 0x2100
102    AddressAlign: 0x100
103  - Name: blob5
104    Type: SHT_PROGBITS
105    Content: 'abbababa'
106    Address: 0x2104
107  - Name: section5
108    Type: SHT_PROGBITS
109    Address: 0x2108
110    Content: 'ccddeeff'
111  - Name: section6
112    Type: SHT_PROGBITS
113    Content: 'fefefefe'
114    Address: 0x2200
115    AddressAlign: 0x100
116  - Name: blob6
117    Type: SHT_PROGBITS
118    Content: 'abbababa'
119    Address: 0x2300
120    AddressAlign: 0x100
121  - Name: sectionA
122    Type: SHT_PROGBITS
123    Content: 'ffffeeee'
124    Address: 0x3000
125    AddressAlign: 0x1000
126  - Name: sectionB
127    Type: SHT_PROGBITS
128    Content: 'ddddcccc'
129    Address: 0x3004
130  - Name: blobA
131    Type: SHT_PROGBITS
132    Content: 'abbababa'
133    Address: 0x3008
134  - Name: sectionC
135    Type: SHT_PROGBITS
136    Content: 'bbbbaaaa'
137    Address: 0x300C
138  - Name: blobB
139    Type: SHT_PROGBITS
140    Content: 'abbababa'
141    Address: 0x3010
142  - Name: sectionD
143    Type: SHT_PROGBITS
144    Content: '00009999'
145    Address: 0x3014
146  - Name: blobC
147    Type: SHT_PROGBITS
148    Content: 'abbababa'
149    Address: 0x3018
150  - Name: sectionE
151    Type: SHT_PROGBITS
152    Content: '88887777'
153    Address: 0x301C
154  - Name: blobD
155    Type: SHT_PROGBITS
156    Content: 'abbababa'
157    Address: 0x3020
158  - Name: sectionF
159    Type: SHT_PROGBITS
160    Content: '66665555'
161    Address: 0x3024
162  - Name: blobE
163    Type: SHT_PROGBITS
164    Content: 'abbababa'
165    Address: 0x3028
166  - Name: blobF
167    Type: SHT_PROGBITS
168    Content: 'abbababa'
169    Address: 0x302C
170  - Name: sectionG
171    Type: SHT_PROGBITS
172    Content: '44443333'
173    Address: 0x3030
174  - Name: blobG
175    Type: SHT_PROGBITS
176    Content: 'abbababa'
177    Address: 0x3034
178  - Name: sectionH
179    Type: SHT_PROGBITS
180    Content: '22221111'
181    Address: 0x3038
182  - Name: blobH
183    Type: SHT_PROGBITS
184    Content: 'abbababa'
185    Address: 0x303C
186  - Name: sectionI
187    Type: SHT_PROGBITS
188    Content: '00112233'
189    Address: 0x3040
190  - Name: blobz
191    Type: SHT_PROGBITS
192    Content: 'abbababa'
193    Address: 0x4000
194    AddressAlign: 0x1000
195  - Name: sectionz
196    Type: SHT_PROGBITS
197    Content: '44556677'
198    Address: 0x4004
199  - Name: bloby
200    Type: SHT_PROGBITS
201    Content: 'abbababa'
202    Address: 0x4008
203  - Name: sectiony
204    Type: SHT_PROGBITS
205    Content: '8899aabb'
206    Address: 0x400C
207  - Name: blobx
208    Type: SHT_PROGBITS
209    Content: 'abbababa'
210    Address: 0x4010
211  - Name: blobw
212    Type: SHT_PROGBITS
213    Content: 'abbababa'
214    Address: 0x4014
215  - Name: sectionx
216    Type: SHT_PROGBITS
217    Content: 'ccddeeff'
218    Address: 0x4018
219  - Name: blobv
220    Type: SHT_PROGBITS
221    Content: 'abbababa'
222    Address: 0x401C
223  - Name: sectionw
224    Type: SHT_PROGBITS
225    Content: 'ffeeddcc'
226    Address: 0x4020
227  - Name: blobu
228    Type: SHT_PROGBITS
229    Content: 'abbababa'
230    Address: 0x4024
231  - Name: sectionv
232    Type: SHT_PROGBITS
233    Content: 'bbaa9988'
234    Address: 0x4028
235  - Name: sectionu
236    Type: SHT_PROGBITS
237    Content: '77665544'
238    Address: 0x402C
239  - Name: sectiont
240    Type: SHT_PROGBITS
241    Content: '33221100'
242    Address: 0x4030
243  - Name: blobt
244    Type: SHT_PROGBITS
245    Content: 'abbababa'
246    Address: 0x4034
247  - Name: sections
248    Type: SHT_PROGBITS
249    Content: '11111111'
250    Address: 0x4038
251  - Name: bloba
252    Type: SHT_PROGBITS
253    Content: 'abbababa'
254    Address: 0x5000
255    AddressAlign: 0x1000
256  - Name: sectiona
257    Type: SHT_PROGBITS
258    Content: '12345678'
259    Address: 0x5004
260  - Name: blobb
261    Type: SHT_PROGBITS
262    Content: 'abbababa'
263    Address: 0x5008
264  - Name: sectionb
265    Type: SHT_PROGBITS
266    Content: '90abcdef'
267    Address: 0x500C
268  - Name: blobc
269    Type: SHT_PROGBITS
270    Content: 'abbababa'
271    Address: 0x5010
272  - Name: sectionc
273    Type: SHT_PROGBITS
274    Content: 'fedcba09'
275    Address: 0x5014
276  - Name: sectiond
277    Type: SHT_PROGBITS
278    Content: '87654321'
279    Address: 0x5018
280  - Name: blobd
281    Type: SHT_PROGBITS
282    Content: 'abbababa'
283    Address: 0x501C
284  - Name: sectione
285    Type: SHT_PROGBITS
286    Content: '22222222'
287    Address: 0x5020
288  - Name: blobe
289    Type: SHT_PROGBITS
290    Content: 'abbababa'
291    Address: 0x5024
292  - Name: sectionf
293    Type: SHT_PROGBITS
294    Content: '33333333'
295    Address: 0x5028
296  - Name: blobf
297    Type: SHT_PROGBITS
298    Content: 'abbababa'
299    Address: 0x502C
300  - Name: blobg
301    Type: SHT_PROGBITS
302    Content: 'abbababa'
303    Address: 0x5030
304  - Name: sectiong
305    Type: SHT_PROGBITS
306    Content: '44444444'
307    Address: 0x5034
308  - Name: blobh
309    Type: SHT_PROGBITS
310    Content: 'abbababa'
311    Address: 0x5038
312ProgramHeaders:
313  # First segment has unlabelled space at start and end.
314  - Type:     0x6ABCDEF0 # Non-specific segment type.
315    VAddr:    0x2000
316    Align:    0x2000
317    FirstSec: blob1
318    LastSec:  blob4
319  # Second segment has sections at start and end.
320  - Type:     0x6ABCDEF0
321    VAddr:    0x2100
322    Align:    0x100
323    FirstSec: section4
324    LastSec:  section5
325  # Third segment is all covered by a section.
326  - Type:     0x6ABCDEF0
327    VAddr:    0x2200
328    Align:    0x100
329    FirstSec: section6
330    LastSec:  section6
331  # Fourth segment has no sections (after removing blob headers).
332  - Type:     0x6ABCDEF0
333    VAddr:    0x2300
334    Align:    0x100
335    FirstSec: blob6
336    LastSec:  blob6
337  # Fifth segment is empty.
338  - Type:   0x6ABCDEF0
339    VAddr:  0x2308
340    Offset: 0x2308
341  # The next few segments test behaviour of fully nested segments.
342  # Sixth segment is the "parent" segment.
343  - Type:     0x6ABCDEF0
344    VAddr:    0x3000
345    Align:    0x1000
346    FirstSec: sectionA
347    LastSec:  sectionI
348  # Seventh segment is empty and nested.
349  - Type:   0x6ABCDEF0
350    VAddr:  0x3002
351    Offset: 0x3002
352  # Eighth segment contains only a section and is nested.
353  - Type:     0x6ABCDEF0
354    VAddr:    0x3004
355    FirstSec: sectionB
356    LastSec:  sectionB
357  # Ninth segment contains only unlabelled space and is nested.
358  - Type:     0x6ABCDEF0
359    VAddr:    0x3008
360    FirstSec: blobA
361    LastSec:  blobA
362  # Tenth segment contains two sections with space between and is nested.
363  - Type:     0x6ABCDEF0
364    VAddr:    0x300C
365    FirstSec: sectionC
366    LastSec:  sectionD
367  # Eleventh segment contains two sections with space between and at ends and is nested.
368  - Type:     0x6ABCDEF0
369    VAddr:    0x3018
370    FirstSec: blobC
371    LastSec:  blobE
372  # Twelfth segment contains one section with space at ends adjacent to space in parent segment.
373  - Type:     0x6ABCDEF0
374    VAddr:    0x302E
375    Offset:   0x302E
376    FileSize: 8
377    FirstSec: sectionG
378    LastSec:  sectionG
379  # Thirteenth segment contains overlaps sections at either end in parent segment.
380  - Type:     0x6ABCDEF0
381    VAddr:    0x303A
382    Offset:   0x303A
383    FileSize: 0x8
384    FirstSec: blobH
385    LastSec:  blobH
386
387  # The next batch of segments are segments that only partially overlap other segments.
388
389  # Segment14: |-unlabelled-|-Sec-|
390  # Segment15:           |--|-Sec-|-unlabelled-|
391  - Type:     0x6ABCDEF0
392    VAddr:    0x4000
393    FirstSec: blobz
394    LastSec:  sectionz
395  - Type:     0x6ABCDEF0
396    VAddr:    0x4002
397    Offset:   0x4002
398    FirstSec: sectionz
399    LastSec:  bloby
400
401  # Segment16: |-Sec-|--|
402  # Segment17:    |--|----unlabelled---|
403  - Type:     0x6ABCDEF0
404    VAddr:    0x400C
405    FileSize: 6
406    FirstSec: sectiony
407    LastSec:  sectiony
408  - Type:     0x6ABCDEF0
409    VAddr:    0x400E
410    Offset:   0x400E
411    FirstSec: blobx
412    LastSec:  blobx
413
414  # Segment18: |-unlabelled-|-Sec-|
415  # Segment19:              |-Sec-|-unlabelled-|
416  - Type:     0x6ABCDEF0
417    VAddr:    0x4014
418    FirstSec: blobw
419    LastSec:  sectionx
420  - Type:     0x6ABCDEF0
421    VAddr:    0x4018
422    FirstSec: sectionx
423    LastSec:  blobv
424
425  # Segment20: |-Sec-|
426  # Segment21:    |--|-unlabelled-|-Sec-|
427  - Type:     0x6ABCDEF0
428    VAddr:    0x4020
429    FirstSec: sectionw
430    LastSec:  sectionw
431
432  - Type:   0x6ABCDEF0
433    VAddr:  0x4022
434    Offset: 0x4022
435    FirstSec: blobu
436    LastSec:  sectionv
437
438  # Segment22: |-Sec-|
439  # Segment23:    |--|-Sec-|
440  - Type:     0x6ABCDEF0
441    VAddr:    0x402C
442    FirstSec: sectionu
443    LastSec:  sectionu
444  - Type:     0x6ABCDEF0
445    VAddr:    0x402E
446    Offset:   0x402E
447    FirstSec: sectiont
448    LastSec:  sectiont
449
450  # Segment24: |-unlabelled-|--|
451  # Segment25:              |--Sec--|
452  - Type:     0x6ABCDEF0
453    VAddr:    0x4034
454    FileSize: 6
455    FirstSec: blobt
456    LastSec:  blobt
457  - Type:     0x6ABCDEF0
458    VAddr:    0x4038
459    FirstSec: sections
460    LastSec:  sections
461
462  # The next batch of segments represent groups of three nested/overlapping segments,
463  # with one parent segment containing two overlapping segments.
464
465  # Segment26: |-unlabelled-|-Sec-|-unlabelled-|
466  # Segment27: |------------|--|
467  # Segment28:              |-Sec-|------------|
468  - Type:     0x6ABCDEF0
469    VAddr:    0x5000
470    Align:    0x1000
471    FirstSec: bloba
472    LastSec:  blobb
473  - Type:     0x6ABCDEF0
474    VAddr:    0x5000
475    FileSize: 6
476    FirstSec: bloba
477    LastSec:  bloba
478  - Type:     0x6ABCDEF0
479    VAddr:    0x5004
480    FirstSec: sectiona
481    LastSec:  blobb
482
483  # Segment29: |-Sec-|-unlabelled-|-Sec-|
484  # Segment30: |-Sec-|--------|
485  # Segment31:          |---------|-Sec-|
486  - Type:     0x6ABCDEF0
487    VAddr:    0x500C
488    FirstSec: sectionb
489    LastSec:  sectionc
490  - Type:     0x6ABCDEF0
491    VAddr:    0x500C
492    FileSize: 7
493    FirstSec: sectionb
494    LastSec:  sectionb
495  - Type:     0x6ABCDEF0
496    VAddr:    0x5011
497    Offset:   0x5011
498    FirstSec: sectionc
499    LastSec:  sectionc
500
501  # Segment32: |-Sec-|-unlabelled-|-Sec-|
502  # Segment33: |-Sec-|------------|
503  # Segment34:       |------------|-Sec-|
504  - Type:    0x6ABCDEF0
505    VAddr:   0x5018
506    FirstSec: sectiond
507    LastSec:  sectione
508  - Type:     0x6ABCDEF0
509    VAddr:    0x5018
510    FirstSec: sectiond
511    LastSec:  blobd
512  - Type:     0x6ABCDEF0
513    VAddr:    0x501C
514    FirstSec: blobd
515    LastSec:  sectione
516
517  # Segment35: |-unlabelled-|-Sec-|-unlabelled-|
518  # Segment36: |------------|-Sec-|
519  # Segment37:              |-Sec-|------------|
520  - Type:     0x6ABCDEF0
521    VAddr:    0x5024
522    FirstSec: blobe
523    LastSec:  blobf
524  - Type:     0x6ABCDEF0
525    VAddr:    0x5024
526    FirstSec: blobe
527    LastSec:  sectionf
528  - Type:     0x6ABCDEF0
529    VAddr:    0x5028
530    FirstSec: sectionf
531    LastSec:  blobf
532
533  # Segment38: |-unlabelled-|-Sec-|-unlabelled-|
534  # Segment39: |------------|---|
535  # Segment40:                |---|------------|
536  - Type:     0x6ABCDEF0
537    VAddr:    0x5030
538    FirstSec: blobg
539    LastSec:  blobh
540  - Type:     0x6ABCDEF0
541    VAddr:    0x5030
542    FileSize: 7
543    FirstSec: blobg
544    LastSec:  blobg
545  - Type:     0x6ABCDEF0
546    VAddr:    0x5035
547    Offset:   0x5035
548    FirstSec: blobh
549    LastSec:  blobh
550