Skip to content

Commit 6503aad

Browse files
committed
[llvm-objcopy][test] Improve empty section tests
empty-sections.test: add two tests adapted from @jhenderson's https://reviews.llvm.org/D74755#1882221 strip-non-alloc.test: improve. D74755 will change the attribution of an empty section. They mostly test the behavior of Object.cpp:sectionWithinSegment : how we attribute sections to segments. `ParentSegment` can affect some subtle layout decisions. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D74879
1 parent 4231940 commit 6503aad

File tree

2 files changed

+125
-60
lines changed

2 files changed

+125
-60
lines changed
Lines changed: 87 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,94 @@
1-
# RUN: yaml2obj %s -o %t
2-
# RUN: llvm-objcopy %t %t2
3-
# RUN: llvm-readobj --sections %t2 | FileCheck %s
1+
## .empty is not covered by a segment. Its offset is moved to 0x200 to fill the gap.
2+
# RUN: yaml2obj --docnum=1 %s -o %t1
3+
# RUN: llvm-objcopy %t1 %t1.out
4+
# RUN: llvm-readelf -S %t1.out | FileCheck --check-prefix=CHECK1 %s
45

5-
!ELF
6+
# CHECK1: Name Type Address Off
7+
# CHECK1-NEXT: NULL 0000000000000000 000000
8+
# CHECK1-NEXT: .foo PROGBITS 0000000000000000 000100
9+
# CHECK1-NEXT: .empty PROGBITS 0000000000001000 000200
10+
# CHECK1-NEXT: .baz PROGBITS 0000000000001000 001000
11+
12+
--- !ELF
613
FileHeader:
7-
Class: ELFCLASS64
8-
Data: ELFDATA2LSB
9-
Type: ET_EXEC
10-
Machine: EM_X86_64
14+
Class: ELFCLASS64
15+
Data: ELFDATA2LSB
16+
Type: ET_EXEC
17+
Machine: EM_X86_64
1118
Sections:
12-
- Name: .text
13-
Type: SHT_PROGBITS
14-
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
15-
Address: 0x0
16-
AddressAlign: 0x0000000000001000
17-
Content: "00000000"
18-
- Name: .empty
19-
Type: SHT_PROGBITS
20-
Flags: [ SHF_ALLOC ]
21-
Address: 0x1000
22-
AddressAlign: 0x0000000000001000
23-
Content: ""
24-
- Name: .data
25-
Type: SHT_PROGBITS
26-
Flags: [ SHF_ALLOC ]
27-
Address: 0x1000
28-
AddressAlign: 0x0000000000001000
29-
Content: "00000000"
19+
- Name: gap
20+
Type: Fill
21+
Size: 0xE00
22+
- Name: .foo
23+
Type: SHT_PROGBITS
24+
Flags: [ SHF_ALLOC ]
25+
AddressAlign: 0x100
26+
Address: 0
27+
Size: 0x100
28+
- Name: .empty
29+
Type: SHT_PROGBITS
30+
Flags: [ SHF_ALLOC ]
31+
Address: 0x1000
32+
- Name: .baz
33+
Type: SHT_PROGBITS
34+
Flags: [ SHF_ALLOC ]
35+
AddressAlign: 0x1000
36+
Address: 0x1000
37+
Size: 0x100
3038

39+
## Test that we attribute .empty to the second segment. Its offset is assigned according to
40+
## the p_offset of the second segment.
41+
# RUN: yaml2obj --docnum=2 %s -o %t2
42+
# RUN: llvm-objcopy %t2 %t2.out
43+
# RUN: llvm-readelf -S -l %t2.out | FileCheck --check-prefix=CHECK2 %s
3144

32-
# CHECK: Name: .text
33-
# CHECK-NEXT: Type: SHT_PROGBITS
34-
# CHECK-NEXT: Flags [
35-
# CHECK-NEXT: SHF_ALLOC
36-
# CHECK-NEXT: SHF_EXECINSTR
37-
# CHECK-NEXT: ]
45+
# CHECK2: Name Type Address Off
46+
# CHECK2-NEXT: NULL 0000000000000000 000000
47+
# CHECK2-NEXT: .foo PROGBITS 0000000000000000 000100
48+
# CHECK2-NEXT: .empty PROGBITS 0000000000001000 001000
49+
# CHECK2-NEXT: .baz PROGBITS 0000000000001000 001000
3850

39-
# CHECK: Name: .empty
40-
# CHECK-NEXT: Type: SHT_PROGBITS
41-
# CHECK-NEXT: Flags [
42-
# CHECK-NEXT: SHF_ALLOC
43-
# CHECK-NEXT: ]
44-
# CHECK-NEXT: Address: 0x1000
45-
# CHECK-NEXT: Offset: 0x2000
46-
# CHECK-NEXT: Size: 0
51+
# CHECK2: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
52+
# CHECK2-NEXT: LOAD 0x000100 0x0000000000000000 0x0000000000000000 0x000100 0x000100 0x100
53+
# CHECK2-NEXT: LOAD 0x001000 0x0000000000001000 0x0000000000001000 0x000100 0x000100 0x1000
4754

48-
# CHECK: Name: .data
49-
# CHECK-NEXT: Type: SHT_PROGBITS
50-
# CHECK-NEXT: Flags [
51-
# CHECK-NEXT: SHF_ALLOC
52-
# CHECK-NEXT: ]
53-
# CHECK-NEXT: Address: 0x1000
54-
# CHECK-NEXT: Offset: 0x2000
55-
# CHECK-NEXT: Size: 4
55+
--- !ELF
56+
FileHeader:
57+
Class: ELFCLASS64
58+
Data: ELFDATA2LSB
59+
Type: ET_EXEC
60+
Machine: EM_X86_64
61+
Sections:
62+
- Name: gap
63+
Type: Fill
64+
Size: 0xE00
65+
- Name: .foo
66+
Type: SHT_PROGBITS
67+
Flags: [ SHF_ALLOC ]
68+
AddressAlign: 0x100
69+
Address: 0
70+
Size: 0x100
71+
- Name: .empty
72+
Type: SHT_PROGBITS
73+
Flags: [ SHF_ALLOC ]
74+
Address: 0x1000
75+
- Name: .baz
76+
Type: SHT_PROGBITS
77+
Flags: [ SHF_ALLOC ]
78+
AddressAlign: 0x1000
79+
Address: 0x1000
80+
Size: 0x100
81+
ProgramHeaders:
82+
- Type: PT_LOAD
83+
VAddr: 0
84+
PAddr: 0
85+
Align: 0x100
86+
Sections:
87+
- Section: .foo
88+
- Type: PT_LOAD
89+
VAddr: 0x1000
90+
PAddr: 0x1000
91+
Align: 0x1000
92+
Sections:
93+
- Section: .empty
94+
- Section: .baz
Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
# RUN: yaml2obj %s -o %t
2-
# RUN: llvm-objcopy --strip-non-alloc %t %t2
3-
# RUN: llvm-readobj --file-headers --sections %t2 | FileCheck %s
1+
# RUN: yaml2obj --docnum=1 %s -o %t
2+
# RUN: llvm-objcopy --strip-non-alloc %t %t.out
3+
# RUN: llvm-readobj --file-headers --sections %t.out | FileCheck %s
44

5-
!ELF
5+
# CHECK: SectionHeaderCount: 5
6+
# CHECK: Name: non_alloc_in_segment
7+
# CHECK: Name: .bss
8+
# CHECK: Name: .text
9+
# CHECK: Name: .shstrtab
10+
11+
--- !ELF
612
FileHeader:
713
Class: ELFCLASS64
814
Data: ELFDATA2LSB
@@ -19,18 +25,38 @@ Sections:
1925
- Name: .text
2026
Type: SHT_PROGBITS
2127
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
22-
- Name: .blarg
23-
Type: SHT_PROGBITS
24-
Flags: [ ]
2528
ProgramHeaders:
2629
# Use an arbitrary segment type to show that the segment type is unimportant.
2730
- Type: 0x61234567
2831
Sections:
2932
- Section: non_alloc_in_segment
3033

31-
# CHECK: SectionHeaderCount: 5
34+
# RUN: yaml2obj --docnum=2 %s -o %t2
35+
# RUN: llvm-objcopy --strip-non-alloc %t2 %t2.out
36+
# RUN: llvm-readobj --file-headers --sections %t2.out | FileCheck --check-prefix=CHECK2 %s
3237

33-
# CHECK: Name: non_alloc_in_segment
34-
# CHECK: Name: .bss
35-
# CHECK: Name: .text
36-
# CHECK: Name: .shstrtab
38+
# CHECK2: SectionHeaderCount: 3
39+
# CHECK2: Name: .text
40+
# CHECK2: Name: .shstrtab
41+
42+
--- !ELF
43+
FileHeader:
44+
Class: ELFCLASS64
45+
Data: ELFDATA2LSB
46+
Type: ET_REL
47+
Machine: EM_X86_64
48+
Sections:
49+
- Name: .text
50+
Type: SHT_PROGBITS
51+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
52+
## empty_trailing_non_alloc is considered included by the segment,
53+
## so it will be retained.
54+
- Name: empty_trailing_non_alloc
55+
Type: SHT_PROGBITS
56+
- Name: trailing_non_alloc
57+
Type: SHT_PROGBITS
58+
Content: 00
59+
ProgramHeaders:
60+
- Type: 0x61234567
61+
Sections:
62+
- Section: .text

0 commit comments

Comments
 (0)