xref: /llvm-project/llvm/test/tools/llvm-objcopy/ELF/gap-fill.test (revision 535520c6636f9a2bd7cd8fcb221e598a372cae9e)
1# RUN: yaml2obj --docnum=1 %s -o %t
2
3# RUN: not llvm-objcopy --gap-fill 1 %t 2>&1 | FileCheck %s --check-prefix=NOT-BINARY
4# NOT-BINARY: error: '--gap-fill' is only supported for binary output
5
6# RUN: not llvm-objcopy -O binary --gap-fill= %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-FORMAT
7# BAD-FORMAT: error: --gap-fill: bad number:
8
9# RUN: not llvm-objcopy -O binary --gap-fill=x %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT
10# BAD-INPUT: error: --gap-fill: bad number: x
11
12# RUN: not llvm-objcopy -O binary --gap-fill=0x %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT2
13# BAD-INPUT2: error: --gap-fill: bad number: 0x
14
15# RUN: not llvm-objcopy -O binary --gap-fill=0x1G %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT3
16# BAD-INPUT3: error: --gap-fill: bad number: 0x1G
17
18# RUN: not llvm-objcopy -O binary --gap-fill=ff %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT4
19# BAD-INPUT4: error: --gap-fill: bad number: ff
20
21# RUN: not llvm-objcopy -O binary --gap-fill=0x1122 %t %t-val16 2>&1 | FileCheck %s --check-prefix=TRUNCATED-ERR
22# TRUNCATED-ERR: error: gap-fill value 0x1122 is out of range (0 to 0xff)
23
24## Test no gap fill with all allocatable output sections.
25# RUN: llvm-objcopy -O binary %t %t-default
26# RUN: od -v -Ax -t x1 %t-default | FileCheck %s --check-prefix=DEFAULT --ignore-case --match-full-lines
27# DEFAULT:      {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba 00 a1 b2
28# DEFAULT-NEXT: {{0*}}10 c3 d4 00 00 00 00 00 00 00 00 00 00 00 00 00 00
29# DEFAULT-NEXT: {{0*}}20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
30# DEFAULT-NEXT: {{0*}}30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
31# DEFAULT-NEXT: {{0*}}40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
32# DEFAULT-NEXT: {{0*}}50 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
33# DEFAULT-NEXT: {{0*}}60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
34# DEFAULT-NEXT: {{0*}}70 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
35# DEFAULT-NEXT: {{0*}}80 00 00 89 ab cd ef
36# DEFAULT-NEXT: {{0*}}86
37
38## Test gap fill with all allocatable output sections.
39# RUN: llvm-objcopy -O binary --gap-fill=0xe9 %t %t-filled
40# RUN: od -v -Ax -t x1 %t-filled | FileCheck %s --check-prefix=FULL --ignore-case --match-full-lines
41# FULL:      {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba e9 a1 b2
42# FULL-NEXT: {{0*}}10 c3 d4 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
43# FULL-NEXT: {{0*}}20 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
44# FULL-NEXT: {{0*}}30 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
45# FULL-NEXT: {{0*}}40 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
46# FULL-NEXT: {{0*}}50 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
47# FULL-NEXT: {{0*}}60 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
48# FULL-NEXT: {{0*}}70 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
49# FULL-NEXT: {{0*}}80 e9 e9 89 ab cd ef
50# FULL-NEXT: {{0*}}86
51
52## Test gap fill with a decimal value.
53# RUN: llvm-objcopy -O binary --gap-fill=99 %t %t-filled-decimal
54# RUN: od -v -Ax -t x1 %t-filled-decimal | FileCheck %s --check-prefix=DEC --ignore-case --match-full-lines
55# DEC:      {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba 63 a1 b2
56# DEC-NEXT: {{0*}}10 c3 d4 63 63 63 63 63 63 63 63 63 63 63 63 63 63
57# DEC-NEXT: {{0*}}20 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
58# DEC-NEXT: {{0*}}30 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
59# DEC-NEXT: {{0*}}40 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
60# DEC-NEXT: {{0*}}50 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
61# DEC-NEXT: {{0*}}60 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
62# DEC-NEXT: {{0*}}70 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
63# DEC-NEXT: {{0*}}80 63 63 89 ab cd ef
64# DEC-NEXT: {{0*}}86
65
66## Test gap fill with the last section removed, should be truncated.
67# RUN: llvm-objcopy -O binary --gap-fill=0xe9 --remove-section=.foo %t %t-filled
68# RUN: od -v -Ax -t x1 %t-filled | FileCheck %s --check-prefix=REMOVE-LAST-SECTION --ignore-case --match-full-lines
69# REMOVE-LAST-SECTION: {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba e9 a1 b2
70# REMOVE-LAST-SECTION-NEXT: {{0*}}10 c3 d4
71# REMOVE-LAST-SECTION-NEXT: {{0*}}12
72
73## Test gap fill with the middle section removed, should be filled.
74# RUN: llvm-objcopy -O binary --gap-fill=0xe9 --remove-section=.gap2 %t %t-filled
75# RUN: od -v -Ax -t x1 %t-filled | FileCheck %s --check-prefix=REMOVE-MIDDLE-SECTION --ignore-case --match-full-lines
76# REMOVE-MIDDLE-SECTION:      {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba e9 e9 e9
77# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}10 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
78# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}20 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
79# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}30 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
80# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}40 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
81# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}50 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
82# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}60 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
83# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}70 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
84# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}80 e9 e9 89 ab cd ef
85# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}86
86
87--- !ELF
88FileHeader:
89  Class:           ELFCLASS64
90  Data:            ELFDATA2LSB
91  Type:            ET_EXEC
92  Machine:         EM_X86_64
93Sections:
94  - Name:            .space1
95    Type:            Fill
96    Pattern:         'ABCD'
97    Size:            0x2
98  - Name:            .nogap
99    Type:            SHT_PROGBITS
100    Flags:           [ SHF_ALLOC ]
101    Address:         0x0102
102    Size:            0x6
103    Content:         'EEFF11223344'
104  - Name:            .gap1
105    Type:            SHT_PROGBITS
106    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
107    Address:         0x0108
108    Content:         'AABBCCDDFEDCBA'
109  - Name:            .zero_size
110    Type:            SHT_PROGBITS
111    Flags:           [ SHF_ALLOC ]
112    Address:         0x0110
113    Size:            0
114  - Name:            .space2
115    Type:            Fill
116    Pattern:         'DC'
117    Size:            1
118  - Name:            .gap2
119    Type:            SHT_PROGBITS
120    Flags:           [ SHF_ALLOC ]
121    Address:         0x0110
122    Content:         'A1B2C3D4'
123  - Name:            .space3
124    Type:            Fill
125    Pattern:         'FE'
126    Size:            0x1
127  - Name:            .nobit_tbss
128    Type:            SHT_NOBITS
129    Flags:           [ SHF_WRITE, SHF_ALLOC, SHF_TLS ]
130    Address:         0x0180
131    Size:            0x0018
132  - Name:            .space4
133    Type:            Fill
134    Pattern:         '01234567'
135    Size:            0x4
136  - Name:            .foo
137    Type:            SHT_PROGBITS
138    Flags:           [ SHF_WRITE, SHF_ALLOC ]
139    Address:         0x0184
140    Content:         '89ABCDEF'
141  - Name:            .nobit_bss
142    Type:            SHT_NOBITS
143    Flags:           [ SHF_WRITE, SHF_ALLOC ]
144    Address:         0x018A
145    Size:            0x0008
146  - Name:            .comment
147    Type:            SHT_PROGBITS
148    Flags:           [ SHF_MERGE, SHF_STRINGS ]
149    EntSize:         0x0001
150    Content:         4743433A
151
152## In this test, output sections are defined out of order with respect to their
153## load addresses. Verify that gaps are still correctly filled.
154
155# RUN: yaml2obj --docnum=2 %s -o %t.2
156# RUN: llvm-objcopy -O binary --gap-fill=0xe9 %t.2 %t.2.filled
157# RUN: od -v -Ax -t x1 %t.2.filled | FileCheck --ignore-case --match-full-lines %s
158# CHECK: {{0*}}00 aa bb cc dd e9 e9 e9 e9 11 22 33 44
159
160--- !ELF
161FileHeader:
162  Class:           ELFCLASS64
163  Data:            ELFDATA2LSB
164  Type:            ET_EXEC
165  Machine:         EM_X86_64
166Sections:
167  - Name:            .bss
168    Type:            SHT_NOBITS
169    Flags:           [ SHF_ALLOC, SHF_WRITE ]
170    Address:         0x0104
171    Size:            4
172  - Name:            .section1
173    Type:            SHT_PROGBITS
174    Flags:           [ SHF_ALLOC, SHF_WRITE ]
175    Address:         0x0108
176    Content:         '11223344'
177  - Name:            .section3
178    Type:            SHT_PROGBITS
179    Flags:           [ SHF_ALLOC, SHF_WRITE ]
180    Address:         0x0100
181    Content:         'AABBCCDD'
182