1 # RUN: yaml2obj --docnum=1 %s -o %t1
2 # RUN: llvm-objcopy --only-keep-debug %t1 %t1.dbg
3 # RUN: llvm-readelf -S -l -x .note1 -x .note2 -x .debug_abbrev -x .debug_frame -x .debug_info %t1.dbg | FileCheck %s
4 ## --only-keep-debug suppresses the default --strip-all.
5 # RUN: llvm-strip --only-keep-debug %t1
6 # RUN: llvm-readelf -S -l -x .note1 -x .note2 -x .debug_abbrev -x .debug_frame -x .debug_info %t1 | FileCheck %s
8 ## Check that SHT_NOTE and .debug* are kept, but others are changed to SHT_NOBITS.
9 ## SHT_NOBITS sections do not occupy space in the output.
11 # CHECK: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
12 # CHECK: [ 1] .note1 NOTE 0000000000000400 000400 000001 00 A 0 0 1024
13 # CHECK-NEXT: [ 2] .note2 NOTE 0000000000000401 000401 000001 00 A 0 0 0
14 # CHECK-NEXT: [ 3] .text NOBITS 0000000000000402 000402 000001 00 AX 0 0 0
15 # CHECK-NEXT: [ 4] .tdata NOBITS 0000000000001480 000480 000007 00 WAT 0 0 128
16 # CHECK-NEXT: [ 5] .tbss NOBITS 0000000000001487 000480 000005 00 WAT 0 0 0
17 # CHECK-NEXT: [ 6] .bss NOBITS 00000000000014a0 000480 00003f 00 WA 0 0 32
18 ## objcopy sets sh_offset to 0x402. We don't do this to keep sh_offset non-decreasing.
19 # CHECK-NEXT: [ 7] .debug_abbrev PROGBITS 0000000000000000 000480 000001 00 0 0 0
20 # CHECK-NEXT: [ 8] .debug_frame PROGBITS 0000000000000000 000488 000001 00 0 0 8
21 # CHECK-NEXT: [ 9] .debug_info PROGBITS 0000000000000000 000489 000001 00 0 0 0
22 # CHECK-NEXT: [10] .strtab STRTAB 0000000000000000 00048a 000001 00 0 0 1
23 # CHECK-NEXT: [11] .shstrtab STRTAB 0000000000000000 00048b 000060 00 0 0 1
25 # CHECK: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
26 # CHECK-NEXT: LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x000402 0x000403 R E 0x1000
27 # CHECK-NEXT: LOAD 0x000480 0x0000000000001480 0x0000000000001480 0x000000 0x00005f RW 0x1000
28 # CHECK-NEXT: TLS 0x000480 0x0000000000001480 0x0000000000001480 0x000000 0x00000c RW 0x80
29 # CHECK-NEXT: NOTE 0x000400 0x0000000000000400 0x0000000000000400 0x000002 0x000002 0x400
31 ## Contents of SHT_NOTE and .debug* are kept.
33 # CHECK: Hex dump of section '.note1':
34 # CHECK-NEXT: 0x00000400 01
35 # CHECK: Hex dump of section '.note2':
36 # CHECK-NEXT: 0x00000401 02
37 # CHECK: Hex dump of section '.debug_abbrev':
38 # CHECK-NEXT: 0x00000000 03
39 # CHECK: Hex dump of section '.debug_frame':
40 # CHECK-NEXT: 0x00000000 04
41 # CHECK: Hex dump of section '.debug_info':
42 # CHECK-NEXT: 0x00000000 05
64 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
69 Flags: [ SHF_ALLOC, SHF_WRITE, SHF_TLS ]
70 Address: 0x1480 # Ensure Address=0x1000+Offset
72 # An arbitrary non-zero Size tests that .tdata does not occupy space
73 # and we can rewrite p_filesz of PT_TLS.
77 Flags: [ SHF_ALLOC, SHF_WRITE, SHF_TLS ]
78 Address: 0x1487 # Ensure Address=0x1000+Offset
82 Flags: [ SHF_ALLOC, SHF_WRITE ]
83 Address: 0x14a0 # Ensure Address=0x1000+Offset
85 # An arbitrary non-zero Size tests that .bss does not occupy space.
92 # AddressAlign tests the file offset assignment leaves a gap.
100 Flags: [ PF_R, PF_X ]
106 Flags: [ PF_R, PF_W ]
107 VAddr: 0x1480 # Ensure Offset=VAddr (mod Align) if Offset changes
112 Flags: [ PF_R, PF_W ]
113 VAddr: 0x1480 # Ensure Offset=VAddr (mod Align) if Offset changes
122 # RUN: yaml2obj --docnum=2 %s -o %t2
123 # RUN: llvm-objcopy --only-keep-debug %t2 %t2.dbg
124 # RUN: llvm-readelf -S -l %t2.dbg | FileCheck --check-prefix=CHECK2 %s
126 ## Only the tail of a segment can be trimmed. .text still occupies space because
127 ## it is followed by .note which is not SHT_NOBITS.
128 # CHECK2: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
129 # CHECK2: [ 1] .text NOBITS 0000000000000200 000200 000001 00 AX 0 0 512
130 # CHECK2-NEXT: [ 2] .note NOTE 0000000000000201 000201 000001 00 A 0 0 0
131 # CHECK2-NEXT: [ 3] .debug_info PROGBITS 0000000000000000 000220 000001 00 0 0 32
132 # CHECK2-NEXT: [ 4] .strtab STRTAB 0000000000000000 000221 000001 00 0 0 1
133 # CHECK2-NEXT: [ 5] .shstrtab STRTAB 0000000000000000 000222 00002b 00 0 0 1
135 ## Check that p_offset or p_filesz of PT_PHDR are not modified.
136 # CHECK2: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
137 # CHECK2-NEXT: PHDR 0x000040 0x0000000000000040 0x0000000000000040 0x0000a8 0x0000a8 R 0x8
138 # CHECK2-NEXT: LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x000202 0x000202 R E 0x1000
139 # CHECK2-NEXT: LOAD 0x000000 0x0000000000000202 0x0000000000000202 0x000000 0x00000e RW 0x1
150 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
168 # 3 * sizeof(Elf64_Phdr) = 0xa8
173 Flags: [ PF_R, PF_X ]
179 Flags: [ PF_R, PF_W ]
186 ## If .symtab or .strtab has the SHF_ALLOC flag, it will be changed to SHT_NOBITS.
187 # RUN: yaml2obj --docnum=3 %s -o %t3
188 # RUN: llvm-objcopy --only-keep-debug %t3 %t3.dbg
189 # RUN: llvm-readelf -S -l %t3.dbg | FileCheck --check-prefix=CHECK3 %s
191 # CHECK3: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
192 # CHECK3: [ 1] .dynsym NOBITS 0000000000000000 000040 000018 18 A 2 1 1024
193 # CHECK3-NEXT: [ 2] .dynstr NOBITS 0000000000000018 000040 000001 00 A 0 0 0
194 # CHECK3-NEXT: [ 3] .symtab NOBITS 0000000000000019 000040 000018 00 A 4 1 0
195 # CHECK3-NEXT: [ 4] .strtab NOBITS 0000000000000031 000040 000001 00 A 0 0 0
196 # CHECK3-NEXT: [ 5] .shstrtab STRTAB 0000000000000000 000040 00002b 00 0 0 1
214 ## TODO: this should be SHT_SYMTAB, but currently llvm-objcopy reports an error:
215 ## error: Symbol table has link index of 4 which is not a string table
225 ## PT_TLS and .tdata are empty. Test that we set its p_offset to the parent
226 ## segment's p_offset. If we don't rewrite the p_offset of PT_TLS and the deleted
227 ## bytes are large, p_offset can be larger than the file size, and trigger
228 ## validation errors with subsequent tools.
229 # RUN: yaml2obj --docnum=4 %s -o %t4
230 # RUN: llvm-objcopy --only-keep-debug %t4 %t4.dbg
231 # RUN: llvm-readelf -S -l %t4.dbg | FileCheck --check-prefix=CHECK4 %s
233 # CHECK4: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
234 # CHECK4: [ 1] .text NOBITS 0000000000000200 000200 000001 00 AX 0 0 0
235 # CHECK4-NEXT: [ 2] .tdata NOBITS 0000000000001240 000240 000000 00 WAT 0 0 64
236 # CHECK4-NEXT: [ 3] .got NOBITS 0000000000001240 000240 000008 00 WA 0 0 0
238 # CHECK4: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
239 # CHECK4-NEXT: LOAD 0x000200 0x0000000000000200 0x0000000000000200 0x000000 0x000001 R E 0x1000
240 # CHECK4-NEXT: LOAD 0x000240 0x0000000000001240 0x0000000000001240 0x000000 0x000008 RW 0x1000
241 # CHECK4-NEXT: TLS 0x000240 0x0000000000001240 0x0000000000001240 0x000000 0x000000 R 0x40
252 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
257 Flags: [ SHF_ALLOC, SHF_WRITE, SHF_TLS ]
258 Address: 0x1240 # Ensure Address=0x1000+Offset
262 Flags: [ SHF_ALLOC, SHF_WRITE ]
266 Flags: [ PF_R, PF_X ]
271 ## Add .got so that the PT_LOAD does not have zero p_memsz. We don't add
272 ## sections to zero-sized segments so zero-sized segments may have strange
273 ## offsets. In practice, the Linux kernel errors when mmapping a p_memsz
274 ## PT_LOAD,so for practical so this assumption can generally be made.
276 Flags: [ PF_R, PF_W ]
287 ## The offset and size fields of segments which contain no section and have no
288 ## parent segment are set to zeros, so that we can decrease the file size. Such
289 ## segments are not useful for debugging.
290 # RUN: yaml2obj --docnum=5 %s -o %t5
291 # RUN: llvm-objcopy --only-keep-debug %t5 %t5.dbg
292 # RUN: llvm-readelf -S -l %t5.dbg | FileCheck --check-prefix=CHECK5 %s
293 # RUN: llvm-objcopy --strip-sections %t5 %t5s
294 # RUN: llvm-objcopy --only-keep-debug %t5s %t5s.dbg
295 # RUN: llvm-readelf -S -l %t5s.dbg | FileCheck --check-prefix=CHECK5S %s
297 # CHECK5: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
298 # CHECK5: [ 1] .foo NOBITS 0000000000000000 000078 001000 00 A 0 0 0
299 # CHECK5-NEXT: [ 2] .strtab STRTAB 0000000000000000 000078 000001 00 0 0 1
300 # CHECK5-NEXT: [ 3] .shstrtab STRTAB 0000000000000000 000079 000018 00 0 0 1
302 # CHECK5: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
303 # CHECK5-NEXT: NULL 0x000000 0x0000000000000000 0x0000000000000000 0x000078 0x001000 0x1
306 # CHECK5S: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
307 # CHECK5S-NEXT: NULL 0x000000 0x0000000000000000 0x0000000000000000 0x000078 0x001000 0x1
325 ## Check that sections are placed correctly in a case when their order in the
326 ## section header table is different from their layout.
327 # RUN: yaml2obj --docnum=6 %s -o %t6
328 # RUN: llvm-objcopy --only-keep-debug %t6 %t6.dbg
329 # RUN: llvm-readelf -S -l %t6.dbg | FileCheck --check-prefix=CHECK6 %s
331 # CHECK6: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
332 # CHECK6: [ 1] foo NOBITS 0000000000000008 001000 000008 00 A 0 0 4
333 # CHECK6-NEXT: [ 2] bar NOBITS 0000000000000000 001000 000008 00 A 0 0 4
334 # CHECK6-NEXT: [ 3] baz NOTE 0000000000000018 001008 000008 00 A 0 0 0
335 # CHECK6-NEXT: [ 4] qux NOTE 0000000000000010 001000 000008 00 A 0 0 0
337 # CHECK6: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
338 # CHECK6-NEXT: LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000000 0x000010 R 0x1000
339 # CHECK6-NEXT: LOAD 0x001000 0x0000000000000010 0x0000000000000010 0x000010 0x000010 R 0x1
369 Content: 0000000000000000
376 Content: 0000000000000000
382 Content: 0000000000000000
388 Content: 0000000000000000
389 - Type: SectionHeaderTable
391 ## Note: the order of section headers differs from their layout.