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 5## Check that SHT_NOTE and .debug* are kept, but others are changed to SHT_NOBITS. 6## SHT_NOBITS sections do not occupy space in the output. 7 8# CHECK: [Nr] Name Type Address Off Size ES Flg Lk Inf Al 9# CHECK: [ 1] .note1 NOTE 0000000000000400 000400 000001 00 A 0 0 1024 10# CHECK-NEXT: [ 2] .note2 NOTE 0000000000000401 000401 000001 00 A 0 0 0 11# CHECK-NEXT: [ 3] .text NOBITS 0000000000000402 000402 000001 00 AX 0 0 0 12# CHECK-NEXT: [ 4] .tdata NOBITS 0000000000001480 000480 000007 00 WAT 0 0 128 13# CHECK-NEXT: [ 5] .tbss NOBITS 0000000000001487 000480 000005 00 WAT 0 0 0 14# CHECK-NEXT: [ 6] .bss NOBITS 00000000000014a0 000480 00003f 00 WA 0 0 32 15## objcopy sets sh_offset to 0x402. We don't do this to keep sh_offset non-decreasing. 16# CHECK-NEXT: [ 7] .debug_abbrev PROGBITS 0000000000000000 000480 000001 00 0 0 0 17# CHECK-NEXT: [ 8] .debug_frame PROGBITS 0000000000000000 000488 000001 00 0 0 8 18# CHECK-NEXT: [ 9] .debug_info PROGBITS 0000000000000000 000489 000001 00 0 0 0 19# CHECK-NEXT: [10] .strtab STRTAB 0000000000000000 00048a 000001 00 0 0 1 20# CHECK-NEXT: [11] .shstrtab STRTAB 0000000000000000 00048b 000060 00 0 0 1 21 22# CHECK: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align 23# CHECK-NEXT: LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x000402 0x000403 R E 0x1000 24# CHECK-NEXT: LOAD 0x000480 0x0000000000001480 0x0000000000001480 0x000000 0x00005f RW 0x1000 25# CHECK-NEXT: TLS 0x000480 0x0000000000001480 0x0000000000001480 0x000000 0x00000c RW 0x80 26# CHECK-NEXT: NOTE 0x000400 0x0000000000000400 0x0000000000000400 0x000002 0x000002 0x400 27 28## Contents of SHT_NOTE and .debug* are kept. 29 30# CHECK: Hex dump of section '.note1': 31# CHECK-NEXT: 0x00000400 01 32# CHECK: Hex dump of section '.note2': 33# CHECK-NEXT: 0x00000401 02 34# CHECK: Hex dump of section '.debug_abbrev': 35# CHECK-NEXT: 0x00000000 03 36# CHECK: Hex dump of section '.debug_frame': 37# CHECK-NEXT: 0x00000000 04 38# CHECK: Hex dump of section '.debug_info': 39# CHECK-NEXT: 0x00000000 05 40 41--- !ELF 42FileHeader: 43 Class: ELFCLASS64 44 Data: ELFDATA2LSB 45 Type: ET_DYN 46 Machine: EM_X86_64 47Sections: 48 - Name: .note1 49 Type: SHT_NOTE 50 Flags: [ SHF_ALLOC ] 51 Address: 0x400 52 AddressAlign: 0x400 53 Content: 01 54 - Name: .note2 55 Type: SHT_NOTE 56 Flags: [ SHF_ALLOC ] 57 Address: 0x401 58 Content: 02 59 - Name: .text 60 Type: SHT_PROGBITS 61 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 62 Address: 0x402 63 Content: c3 64 - Name: .tdata 65 Type: SHT_PROGBITS 66 Flags: [ SHF_ALLOC, SHF_WRITE, SHF_TLS ] 67 Address: 0x1480 # Ensure Address=0x1000+Offset 68 AddressAlign: 0x80 69 # An arbitrary non-zero Size tests that .tdata does not occupy space 70 # and we can rewrite p_filesz of PT_TLS. 71 Size: 7 72 - Name: .tbss 73 Type: SHT_NOBITS 74 Flags: [ SHF_ALLOC, SHF_WRITE, SHF_TLS ] 75 Address: 0x1487 # Ensure Address=0x1000+Offset 76 Size: 5 77 - Name: .bss 78 Type: SHT_NOBITS 79 Flags: [ SHF_ALLOC, SHF_WRITE ] 80 Address: 0x14a0 # Ensure Address=0x1000+Offset 81 AddressAlign: 0x20 82 # An arbitrary non-zero Size tests that .bss does not occupy space. 83 Size: 63 84 - Name: .debug_abbrev 85 Type: SHT_PROGBITS 86 Content: 03 87 - Name: .debug_frame 88 Type: SHT_PROGBITS 89 # AddressAlign tests the file offset assignment leaves a gap. 90 AddressAlign: 0x8 91 Content: 04 92 - Name: .debug_info 93 Type: SHT_PROGBITS 94 Content: 05 95ProgramHeaders: 96 - Type: PT_LOAD 97 Flags: [ PF_R, PF_X ] 98 Offset: 0 99 Align: 0x1000 100 FirstSec: .note1 101 LastSec: .text 102 - Type: PT_LOAD 103 Flags: [ PF_R, PF_W ] 104 VAddr: 0x1480 # Ensure Offset=VAddr (mod Align) if Offset changes 105 Align: 0x1000 106 FirstSec: .tdata 107 LastSec: .bss 108 - Type: PT_TLS 109 Flags: [ PF_R, PF_W ] 110 VAddr: 0x1480 # Ensure Offset=VAddr (mod Align) if Offset changes 111 FirstSec: .tdata 112 LastSec: .tbss 113 - Type: PT_NOTE 114 VAddr: 0x400 115 FirstSec: .note1 116 LastSec: .note2 117... 118 119# RUN: yaml2obj --docnum=2 %s -o %t2 120# RUN: llvm-objcopy --only-keep-debug %t2 %t2.dbg 121# RUN: llvm-readelf -S -l %t2.dbg | FileCheck --check-prefix=CHECK2 %s 122 123## Only the tail of a segment can be trimmed. .text still occupies space because 124## it is followed by .note which is not SHT_NOBITS. 125# CHECK2: [Nr] Name Type Address Off Size ES Flg Lk Inf Al 126# CHECK2: [ 1] .text NOBITS 0000000000000200 000200 000001 00 AX 0 0 512 127# CHECK2-NEXT: [ 2] .note NOTE 0000000000000201 000201 000001 00 A 0 0 0 128# CHECK2-NEXT: [ 3] .debug_info PROGBITS 0000000000000000 000220 000001 00 0 0 32 129# CHECK2-NEXT: [ 4] .strtab STRTAB 0000000000000000 000221 000001 00 0 0 1 130# CHECK2-NEXT: [ 5] .shstrtab STRTAB 0000000000000000 000222 00002b 00 0 0 1 131 132## Check that p_offset or p_filesz of empty segments or PT_PHDR are not modified. 133# CHECK2: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align 134# CHECK2-NEXT: PHDR 0x000040 0x0000000000000040 0x0000000000000040 0x0000a8 0x0000a8 R 0x8 135# CHECK2-NEXT: LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x000202 0x000202 R E 0x1000 136# CHECK2-NEXT: LOAD 0x000202 0x0000000000000202 0x0000000000000202 0x00000e 0x00000e RW 0x1 137 138--- !ELF 139FileHeader: 140 Class: ELFCLASS64 141 Data: ELFDATA2LSB 142 Type: ET_DYN 143 Machine: EM_X86_64 144Sections: 145 - Name: .text 146 Type: SHT_PROGBITS 147 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 148 Address: 0x200 149 AddressAlign: 0x200 150 Content: c3 151 - Name: .note 152 Type: SHT_NOTE 153 Flags: [ SHF_ALLOC ] 154 Address: 0x201 155 Content: 01 156 - Name: .debug_info 157 Type: SHT_PROGBITS 158 AddressAlign: 0x20 159 Content: 02 160ProgramHeaders: 161 - Type: PT_PHDR 162 Flags: [ PF_R ] 163 Offset: 0x40 164 VAddr: 0x40 165 # 3 * sizeof(Elf64_Phdr) = 0xa8 166 FileSize: 0xa8 167 MemSize: 0xa8 168 Align: 8 169 - Type: PT_LOAD 170 Flags: [ PF_R, PF_X ] 171 Offset: 0 172 Align: 4096 173 FirstSec: .text 174 LastSec: .note 175 - Type: PT_LOAD 176 Flags: [ PF_R, PF_W ] 177 Offset: 0x202 178 VAddr: 0x202 179 FileSize: 14 180 MemSize: 14 181... 182 183## If .symtab or .strtab has the SHF_ALLOC flag, it will be changed to SHT_NOBITS. 184# RUN: yaml2obj --docnum=3 %s -o %t3 185# RUN: llvm-objcopy --only-keep-debug %t3 %t3.dbg 186# RUN: llvm-readelf -S -l %t3.dbg | FileCheck --check-prefix=CHECK3 %s 187 188# CHECK3: [Nr] Name Type Address Off Size ES Flg Lk Inf Al 189# CHECK3: [ 1] .dynsym NOBITS 0000000000000000 000040 000018 18 A 2 1 1024 190# CHECK3-NEXT: [ 2] .dynstr NOBITS 0000000000000018 000040 000001 00 A 0 0 0 191# CHECK3-NEXT: [ 3] .symtab NOBITS 0000000000000019 000040 000018 18 A 4 1 0 192# CHECK3-NEXT: [ 4] .strtab NOBITS 0000000000000031 000040 000001 00 A 0 0 0 193# CHECK3-NEXT: [ 5] .shstrtab STRTAB 0000000000000000 000040 00002b 00 0 0 1 194 195--- !ELF 196FileHeader: 197 Class: ELFCLASS64 198 Data: ELFDATA2LSB 199 Type: ET_DYN 200 Machine: EM_X86_64 201Sections: 202 - Name: .dynsym 203 Type: SHT_DYNSYM 204 Flags: [ SHF_ALLOC ] 205 Link: .dynstr 206 AddressAlign: 0x400 207 - Name: .dynstr 208 Type: SHT_STRTAB 209 Flags: [ SHF_ALLOC ] 210 - Name: .symtab 211 Type: SHT_STRTAB 212 Flags: [ SHF_ALLOC ] 213 Link: .strtab 214 - Name: .strtab 215 Type: SHT_STRTAB 216 Flags: [ SHF_ALLOC ] 217DynamicSymbols: [] 218Symbols: [] 219 220## PT_TLS and .tdata are empty. Test that we set its p_offset to the parent 221## segment's p_offset. If we don't rewrite the p_offset of PT_TLS and the deleted 222## bytes are large, p_offset can be larger than the file size, and trigger 223## validation errors with subsequent tools. 224# RUN: yaml2obj --docnum=4 %s -o %t4 225# RUN: llvm-objcopy --only-keep-debug %t4 %t4.dbg 226# RUN: llvm-readelf -S -l %t4.dbg | FileCheck --check-prefix=CHECK4 %s 227 228# CHECK4: [Nr] Name Type Address Off Size ES Flg Lk Inf Al 229# CHECK4: [ 1] .text NOBITS 0000000000000200 000200 000001 00 AX 0 0 0 230# CHECK4-NEXT: [ 2] .tdata NOBITS 0000000000001240 000240 000000 00 WAT 0 0 64 231# CHECK4-NEXT: [ 3] .got NOBITS 0000000000001240 000240 000008 00 WA 0 0 0 232 233# CHECK4: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align 234# CHECK4-NEXT: LOAD 0x000200 0x0000000000000200 0x0000000000000200 0x000000 0x000001 R E 0x1000 235# CHECK4-NEXT: LOAD 0x000240 0x0000000000001240 0x0000000000001240 0x000000 0x000008 RW 0x1000 236# CHECK4-NEXT: TLS 0x000240 0x0000000000001240 0x0000000000001240 0x000000 0x000000 R 0x40 237 238--- !ELF 239FileHeader: 240 Class: ELFCLASS64 241 Data: ELFDATA2LSB 242 Type: ET_DYN 243 Machine: EM_X86_64 244Sections: 245 - Name: .text 246 Type: SHT_PROGBITS 247 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 248 Address: 0x200 249 Size: 1 250 - Name: .tdata 251 Type: SHT_PROGBITS 252 Flags: [ SHF_ALLOC, SHF_WRITE, SHF_TLS ] 253 Address: 0x1240 # Ensure Address=0x1000+Offset 254 AddressAlign: 0x40 255 - Name: .got 256 Type: SHT_PROGBITS 257 Flags: [ SHF_ALLOC, SHF_WRITE ] 258 Size: 8 259ProgramHeaders: 260 - Type: PT_LOAD 261 Flags: [ PF_R, PF_X ] 262 VAddr: 0x200 263 Align: 0x1000 264 FirstSec: .text 265 LastSec: .text 266 ## Add .got so that the PT_LOAD does not have zero p_memsz. We don't add 267 ## sections to zero-sized segments so zero-sized segments may have strange 268 ## offsets. In practice, the Linux kernel errors when mmapping a p_memsz 269 ## PT_LOAD,so for practical so this assumption can generally be made. 270 - Type: PT_LOAD 271 Flags: [ PF_R, PF_W ] 272 VAddr: 0x1240 273 Align: 0x1000 274 FirstSec: .tdata 275 LastSec: .got 276 - Type: PT_TLS 277 Flags: [ PF_R ] 278 VAddr: 0x1240 279 FirstSec: .tdata 280 LastSec: .tdata 281