398 lines · plain
1# RUN: yaml2obj --docnum=1 %s -o %t12# RUN: llvm-objcopy --only-keep-debug %t1 %t1.dbg3# RUN: llvm-readelf -S -l -x .note1 -x .note2 -x .debug_abbrev -x .debug_frame -x .debug_info %t1.dbg | FileCheck %s4## --only-keep-debug suppresses the default --strip-all.5# RUN: llvm-strip --only-keep-debug %t16# RUN: llvm-readelf -S -l -x .note1 -x .note2 -x .debug_abbrev -x .debug_frame -x .debug_info %t1 | FileCheck %s7 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.10 11# CHECK: [Nr] Name Type Address Off Size ES Flg Lk Inf Al12# CHECK: [ 1] .note1 NOTE 0000000000000400 000400 000001 00 A 0 0 102413# CHECK-NEXT: [ 2] .note2 NOTE 0000000000000401 000401 000001 00 A 0 0 014# CHECK-NEXT: [ 3] .text NOBITS 0000000000000402 000402 000001 00 AX 0 0 015# CHECK-NEXT: [ 4] .tdata NOBITS 0000000000001480 000480 000007 00 WAT 0 0 12816# CHECK-NEXT: [ 5] .tbss NOBITS 0000000000001487 000480 000005 00 WAT 0 0 017# CHECK-NEXT: [ 6] .bss NOBITS 00000000000014a0 000480 00003f 00 WA 0 0 3218## 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 020# CHECK-NEXT: [ 8] .debug_frame PROGBITS 0000000000000000 000488 000001 00 0 0 821# CHECK-NEXT: [ 9] .debug_info PROGBITS 0000000000000000 000489 000001 00 0 0 022# CHECK-NEXT: [10] .strtab STRTAB 0000000000000000 00048a 000001 00 0 0 123# CHECK-NEXT: [11] .shstrtab STRTAB 0000000000000000 00048b 000060 00 0 0 124 25# CHECK: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align26# CHECK-NEXT: LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x000402 0x000403 R E 0x100027# CHECK-NEXT: LOAD 0x000480 0x0000000000001480 0x0000000000001480 0x000000 0x00005f RW 0x100028# CHECK-NEXT: TLS 0x000480 0x0000000000001480 0x0000000000001480 0x000000 0x00000c RW 0x8029# CHECK-NEXT: NOTE 0x000400 0x0000000000000400 0x0000000000000400 0x000002 0x000002 0x40030 31## Contents of SHT_NOTE and .debug* are kept.32 33# CHECK: Hex dump of section '.note1':34# CHECK-NEXT: 0x00000400 0135# CHECK: Hex dump of section '.note2':36# CHECK-NEXT: 0x00000401 0237# CHECK: Hex dump of section '.debug_abbrev':38# CHECK-NEXT: 0x00000000 0339# CHECK: Hex dump of section '.debug_frame':40# CHECK-NEXT: 0x00000000 0441# CHECK: Hex dump of section '.debug_info':42# CHECK-NEXT: 0x00000000 0543 44--- !ELF45FileHeader:46 Class: ELFCLASS6447 Data: ELFDATA2LSB48 Type: ET_DYN49 Machine: EM_X86_6450Sections:51 - Name: .note152 Type: SHT_NOTE53 Flags: [ SHF_ALLOC ]54 Address: 0x40055 AddressAlign: 0x40056 Content: 0157 - Name: .note258 Type: SHT_NOTE59 Flags: [ SHF_ALLOC ]60 Address: 0x40161 Content: 0262 - Name: .text63 Type: SHT_PROGBITS64 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]65 Address: 0x40266 Content: c367 - Name: .tdata68 Type: SHT_PROGBITS69 Flags: [ SHF_ALLOC, SHF_WRITE, SHF_TLS ]70 Address: 0x1480 # Ensure Address=0x1000+Offset71 AddressAlign: 0x8072 # An arbitrary non-zero Size tests that .tdata does not occupy space73 # and we can rewrite p_filesz of PT_TLS.74 Size: 775 - Name: .tbss76 Type: SHT_NOBITS77 Flags: [ SHF_ALLOC, SHF_WRITE, SHF_TLS ]78 Address: 0x1487 # Ensure Address=0x1000+Offset79 Size: 580 - Name: .bss81 Type: SHT_NOBITS82 Flags: [ SHF_ALLOC, SHF_WRITE ]83 Address: 0x14a0 # Ensure Address=0x1000+Offset84 AddressAlign: 0x2085 # An arbitrary non-zero Size tests that .bss does not occupy space.86 Size: 6387 - Name: .debug_abbrev88 Type: SHT_PROGBITS89 Content: 0390 - Name: .debug_frame91 Type: SHT_PROGBITS92 # AddressAlign tests the file offset assignment leaves a gap.93 AddressAlign: 0x894 Content: 0495 - Name: .debug_info96 Type: SHT_PROGBITS97 Content: 0598ProgramHeaders:99 - Type: PT_LOAD100 Flags: [ PF_R, PF_X ]101 Offset: 0102 Align: 0x1000103 FirstSec: .note1104 LastSec: .text105 - Type: PT_LOAD106 Flags: [ PF_R, PF_W ]107 VAddr: 0x1480 # Ensure Offset=VAddr (mod Align) if Offset changes108 Align: 0x1000109 FirstSec: .tdata110 LastSec: .bss111 - Type: PT_TLS112 Flags: [ PF_R, PF_W ]113 VAddr: 0x1480 # Ensure Offset=VAddr (mod Align) if Offset changes114 FirstSec: .tdata115 LastSec: .tbss116 - Type: PT_NOTE117 VAddr: 0x400118 FirstSec: .note1119 LastSec: .note2120...121 122# RUN: yaml2obj --docnum=2 %s -o %t2123# RUN: llvm-objcopy --only-keep-debug %t2 %t2.dbg124# RUN: llvm-readelf -S -l %t2.dbg | FileCheck --check-prefix=CHECK2 %s125 126## Only the tail of a segment can be trimmed. .text still occupies space because127## it is followed by .note which is not SHT_NOBITS.128# CHECK2: [Nr] Name Type Address Off Size ES Flg Lk Inf Al129# CHECK2: [ 1] .text NOBITS 0000000000000200 000200 000001 00 AX 0 0 512130# CHECK2-NEXT: [ 2] .note NOTE 0000000000000201 000201 000001 00 A 0 0 0131# CHECK2-NEXT: [ 3] .debug_info PROGBITS 0000000000000000 000220 000001 00 0 0 32132# CHECK2-NEXT: [ 4] .strtab STRTAB 0000000000000000 000221 000001 00 0 0 1133# CHECK2-NEXT: [ 5] .shstrtab STRTAB 0000000000000000 000222 00002b 00 0 0 1134 135## Check that p_offset or p_filesz of PT_PHDR are not modified.136# CHECK2: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align137# CHECK2-NEXT: PHDR 0x000040 0x0000000000000040 0x0000000000000040 0x0000a8 0x0000a8 R 0x8138# CHECK2-NEXT: LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x000202 0x000202 R E 0x1000139# CHECK2-NEXT: LOAD 0x000000 0x0000000000000202 0x0000000000000202 0x000000 0x00000e RW 0x1140 141--- !ELF142FileHeader:143 Class: ELFCLASS64144 Data: ELFDATA2LSB145 Type: ET_DYN146 Machine: EM_X86_64147Sections:148 - Name: .text149 Type: SHT_PROGBITS150 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]151 Address: 0x200152 AddressAlign: 0x200153 Content: c3154 - Name: .note155 Type: SHT_NOTE156 Flags: [ SHF_ALLOC ]157 Address: 0x201158 Content: 01159 - Name: .debug_info160 Type: SHT_PROGBITS161 AddressAlign: 0x20162 Content: 02163ProgramHeaders:164 - Type: PT_PHDR165 Flags: [ PF_R ]166 Offset: 0x40167 VAddr: 0x40168 # 3 * sizeof(Elf64_Phdr) = 0xa8169 FileSize: 0xa8170 MemSize: 0xa8171 Align: 8172 - Type: PT_LOAD173 Flags: [ PF_R, PF_X ]174 Offset: 0175 Align: 4096176 FirstSec: .text177 LastSec: .note178 - Type: PT_LOAD179 Flags: [ PF_R, PF_W ]180 Offset: 0x202181 VAddr: 0x202182 FileSize: 14183 MemSize: 14184...185 186## If .symtab or .strtab has the SHF_ALLOC flag, it will be changed to SHT_NOBITS.187# RUN: yaml2obj --docnum=3 %s -o %t3188# RUN: llvm-objcopy --only-keep-debug %t3 %t3.dbg189# RUN: llvm-readelf -S -l %t3.dbg | FileCheck --check-prefix=CHECK3 %s190 191# CHECK3: [Nr] Name Type Address Off Size ES Flg Lk Inf Al192# CHECK3: [ 1] .dynsym NOBITS 0000000000000000 000040 000018 18 A 2 1 1024193# CHECK3-NEXT: [ 2] .dynstr NOBITS 0000000000000018 000040 000001 00 A 0 0 0194# CHECK3-NEXT: [ 3] .symtab NOBITS 0000000000000019 000040 000018 00 A 4 1 0195# CHECK3-NEXT: [ 4] .strtab NOBITS 0000000000000031 000040 000001 00 A 0 0 0196# CHECK3-NEXT: [ 5] .shstrtab STRTAB 0000000000000000 000040 00002b 00 0 0 1197 198--- !ELF199FileHeader:200 Class: ELFCLASS64201 Data: ELFDATA2LSB202 Type: ET_DYN203 Machine: EM_X86_64204Sections:205 - Name: .dynsym206 Type: SHT_DYNSYM207 Flags: [ SHF_ALLOC ]208 Link: .dynstr209 AddressAlign: 0x400210 - Name: .dynstr211 Type: SHT_STRTAB212 Flags: [ SHF_ALLOC ]213 - Name: .symtab214## 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 table216 Type: SHT_STRTAB217 Flags: [ SHF_ALLOC ]218 Link: .strtab219 - Name: .strtab220 Type: SHT_STRTAB221 Flags: [ SHF_ALLOC ]222DynamicSymbols: []223Symbols: []224 225## PT_TLS and .tdata are empty. Test that we set its p_offset to the parent226## segment's p_offset. If we don't rewrite the p_offset of PT_TLS and the deleted227## bytes are large, p_offset can be larger than the file size, and trigger228## validation errors with subsequent tools.229# RUN: yaml2obj --docnum=4 %s -o %t4230# RUN: llvm-objcopy --only-keep-debug %t4 %t4.dbg231# RUN: llvm-readelf -S -l %t4.dbg | FileCheck --check-prefix=CHECK4 %s232 233# CHECK4: [Nr] Name Type Address Off Size ES Flg Lk Inf Al234# CHECK4: [ 1] .text NOBITS 0000000000000200 000200 000001 00 AX 0 0 0235# CHECK4-NEXT: [ 2] .tdata NOBITS 0000000000001240 000240 000000 00 WAT 0 0 64236# CHECK4-NEXT: [ 3] .got NOBITS 0000000000001240 000240 000008 00 WA 0 0 0237 238# CHECK4: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align239# CHECK4-NEXT: LOAD 0x000200 0x0000000000000200 0x0000000000000200 0x000000 0x000001 R E 0x1000240# CHECK4-NEXT: LOAD 0x000240 0x0000000000001240 0x0000000000001240 0x000000 0x000008 RW 0x1000241# CHECK4-NEXT: TLS 0x000240 0x0000000000001240 0x0000000000001240 0x000000 0x000000 R 0x40242 243--- !ELF244FileHeader:245 Class: ELFCLASS64246 Data: ELFDATA2LSB247 Type: ET_DYN248 Machine: EM_X86_64249Sections:250 - Name: .text251 Type: SHT_PROGBITS252 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]253 Address: 0x200254 Size: 1255 - Name: .tdata256 Type: SHT_PROGBITS257 Flags: [ SHF_ALLOC, SHF_WRITE, SHF_TLS ]258 Address: 0x1240 # Ensure Address=0x1000+Offset259 AddressAlign: 0x40260 - Name: .got261 Type: SHT_PROGBITS262 Flags: [ SHF_ALLOC, SHF_WRITE ]263 Size: 8264ProgramHeaders:265 - Type: PT_LOAD266 Flags: [ PF_R, PF_X ]267 VAddr: 0x200268 Align: 0x1000269 FirstSec: .text270 LastSec: .text271 ## Add .got so that the PT_LOAD does not have zero p_memsz. We don't add272 ## sections to zero-sized segments so zero-sized segments may have strange273 ## offsets. In practice, the Linux kernel errors when mmapping a p_memsz274 ## PT_LOAD,so for practical so this assumption can generally be made.275 - Type: PT_LOAD276 Flags: [ PF_R, PF_W ]277 VAddr: 0x1240278 Align: 0x1000279 FirstSec: .tdata280 LastSec: .got281 - Type: PT_TLS282 Flags: [ PF_R ]283 VAddr: 0x1240284 FirstSec: .tdata285 LastSec: .tdata286 287## The offset and size fields of segments which contain no section and have no288## parent segment are set to zeros, so that we can decrease the file size. Such289## segments are not useful for debugging.290# RUN: yaml2obj --docnum=5 %s -o %t5291# RUN: llvm-objcopy --only-keep-debug %t5 %t5.dbg292# RUN: llvm-readelf -S -l %t5.dbg | FileCheck --check-prefix=CHECK5 %s293# RUN: llvm-objcopy --strip-sections %t5 %t5s294# RUN: llvm-objcopy --only-keep-debug %t5s %t5s.dbg295# RUN: llvm-readelf -S -l %t5s.dbg | FileCheck --check-prefix=CHECK5S %s296 297# CHECK5: [Nr] Name Type Address Off Size ES Flg Lk Inf Al298# CHECK5: [ 1] .foo NOBITS 0000000000000000 000078 001000 00 A 0 0 0299# CHECK5-NEXT: [ 2] .strtab STRTAB 0000000000000000 000078 000001 00 0 0 1300# CHECK5-NEXT: [ 3] .shstrtab STRTAB 0000000000000000 000079 000018 00 0 0 1301 302# CHECK5: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align303# CHECK5-NEXT: NULL 0x000000 0x0000000000000000 0x0000000000000000 0x000078 0x001000 0x1304# CHECK5-EMPTY:305 306# CHECK5S: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align307# CHECK5S-NEXT: NULL 0x000000 0x0000000000000000 0x0000000000000000 0x000078 0x001000 0x1308# CHECK5S-EMPTY:309--- !ELF310FileHeader:311 Class: ELFCLASS64312 Data: ELFDATA2LSB313 Type: ET_DYN314Sections:315 - Name: .foo316 Type: SHT_PROGBITS317 Flags: [ SHF_ALLOC ]318 Size: 0x01000319ProgramHeaders:320 - Type: PT_NULL321 Flags: []322 FileSize: 0x01000323 MemSize: 0x01000324 325## Check that sections are placed correctly in a case when their order in the326## section header table is different from their layout.327# RUN: yaml2obj --docnum=6 %s -o %t6328# RUN: llvm-objcopy --only-keep-debug %t6 %t6.dbg329# RUN: llvm-readelf -S -l %t6.dbg | FileCheck --check-prefix=CHECK6 %s330 331# CHECK6: [Nr] Name Type Address Off Size ES Flg Lk Inf Al332# CHECK6: [ 1] foo NOBITS 0000000000000008 001000 000008 00 A 0 0 4333# CHECK6-NEXT: [ 2] bar NOBITS 0000000000000000 001000 000008 00 A 0 0 4334# CHECK6-NEXT: [ 3] baz NOTE 0000000000000018 001008 000008 00 A 0 0 0335# CHECK6-NEXT: [ 4] qux NOTE 0000000000000010 001000 000008 00 A 0 0 0336 337# CHECK6: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align338# CHECK6-NEXT: LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000000 0x000010 R 0x1000339# CHECK6-NEXT: LOAD 0x001000 0x0000000000000010 0x0000000000000010 0x000010 0x000010 R 0x1340# CHECK6-EMPTY:341 342--- !ELF343FileHeader:344 Class: ELFCLASS64345 Data: ELFDATA2LSB346 Type: ET_DYN347 Machine: EM_X86_64348ProgramHeaders:349 - Type: PT_LOAD350 Flags: [ PF_R ]351 Offset: 0x1000352 VAddr: 0x0353 Align: 0x1000354 FileSize: 0x10355 MemSize: 0x10356 - Type: PT_LOAD357 Flags: [ PF_R ]358 Offset: 0x1010359 VAddr: 0x10360 FileSize: 0x10361 MemSize: 0x10362Sections:363 - Name: bar364 Type: SHT_PROGBITS365 Flags: [ SHF_ALLOC ]366 Address: 0x0367 AddressAlign: 0x4368 Offset: 0x1000369 Content: 0000000000000000370 - Name: foo371 Type: SHT_PROGBITS372 Flags: [ SHF_ALLOC ]373 Address: 0x8374 AddressAlign: 0x4375 Offset: 0x1008376 Content: 0000000000000000377 - Name: qux378 Type: SHT_NOTE379 Flags: [ SHF_ALLOC ]380 Address: 0x10381 Offset: 0x1010382 Content: 0000000000000000383 - Name: baz384 Type: SHT_NOTE385 Flags: [ SHF_ALLOC ]386 Address: 0x18387 Offset: 0x1018388 Content: 0000000000000000389 - Type: SectionHeaderTable390 Sections:391## Note: the order of section headers differs from their layout.392 - Name: foo393 - Name: bar394 - Name: baz395 - Name: qux396 - Name: .shstrtab397 - Name: .strtab398