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