1## The computed LMA of a section in a PT_LOAD equals sh_offset-p_offset+p_paddr. 2## The byte offset difference between two sections equals the difference between their LMAs. 3 4## Corollary: if two sections are in the same PT_LOAD, the byte offset 5## difference equals the difference between their sh_addr fields. 6 7# RUN: yaml2obj --docnum=1 %s -o %t1 8# RUN: llvm-objcopy -O binary %t1 %t1.out 9# RUN: od -A x -t x2 %t1.out | FileCheck %s --check-prefix=CHECK1 --ignore-case 10# RUN: wc -c %t1.out | FileCheck %s --check-prefix=SIZE1 11 12# CHECK1: 000000 c3c3 c3c3 0000 0000 0000 0000 0000 0000 13# CHECK1-NEXT: 000010 0000 0000 0000 0000 0000 0000 0000 0000 14# CHECK1-NEXT: * 15# CHECK1-NEXT: 001000 3232 16# SIZE1: 4098 17 18--- !ELF 19FileHeader: 20 Class: ELFCLASS64 21 Data: ELFDATA2LSB 22 Type: ET_EXEC 23 Machine: EM_X86_64 24Sections: 25 - Name: .text 26 Type: SHT_PROGBITS 27 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 28 Address: 0x1000 29 AddressAlign: 0x1000 30 Content: "c3c3c3c3" 31 - Name: .data 32 Type: SHT_PROGBITS 33 Flags: [ SHF_ALLOC, SHF_WRITE ] 34 Address: 0x2000 35 AddressAlign: 0x1000 36 Content: "3232" 37ProgramHeaders: 38 - Type: PT_LOAD 39 Flags: [ PF_R, PF_W ] 40 FirstSec: .text 41 LastSec: .data 42 43## The computed LMA of a section not in a PT_LOAD equals its sh_addr. 44 45# RUN: yaml2obj --docnum=2 %s -o %t2 46# RUN: llvm-objcopy -O binary %t2 %t2.out 47# RUN: od -A x -t x2 %t2.out | FileCheck %s --check-prefix=CHECK2 --ignore-case 48# RUN: wc -c %t2.out | FileCheck %s --check-prefix=SIZE2 49 50## The computed LMA of .data is 0x4000. The minimum LMA of all non-empty sections is 0x1000. 51## The content of .data will be written at 0x4000-0x1000 = 0x3000. 52# CHECK2: 000000 c3c3 c3c3 0000 0000 0000 0000 0000 0000 53# CHECK2-NEXT: 000010 0000 0000 0000 0000 0000 0000 0000 0000 54# CHECK2-NEXT: * 55# CHECK2-NEXT: 003000 3232 56# SIZE2: 12290 57 58--- !ELF 59FileHeader: 60 Class: ELFCLASS64 61 Data: ELFDATA2LSB 62 Type: ET_EXEC 63 Machine: EM_X86_64 64Sections: 65 - Name: .text 66 Type: SHT_PROGBITS 67 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 68 ## Not in a PT_LOAD. LMA = sh_addr = 0x1000. 69 Address: 0x1000 70 AddressAlign: 0x1000 71 Content: "c3c3c3c3" 72 - Name: .data 73 Type: SHT_PROGBITS 74 Flags: [ SHF_ALLOC, SHF_WRITE ] 75 ## LMA = sh_offset-p_offset+p_paddr = 0x2000-0x2000+0x4000 = 0x4000. 76 Address: 0x2000 77 AddressAlign: 0x1000 78 Content: "3232" 79ProgramHeaders: 80 - Type: PT_LOAD 81 Flags: [ PF_R, PF_W ] 82 VAddr: 0x2000 83 ## p_vaddr is increased from 0x2000 to 0x4000. 84 PAddr: 0x4000 85 FirstSec: .data 86 LastSec: .data 87 88## Check that we use sh_offset instead of sh_addr to decide where to write section contents. 89 90# RUN: yaml2obj --docnum=3 %s -o %t3 91# RUN: llvm-objcopy -O binary %t3 %t3.out 92# RUN: od -A x -t x2 %t3.out | FileCheck %s --check-prefix=CHECK3 --ignore-case 93# RUN: wc -c %t3.out | FileCheck %s --check-prefix=SIZE3 94 95## The minimum LMA of all non-empty sections is 0x1000. 96## The content of .data will be written at 0x3000-0x1000 = 0x2000. 97# CHECK3: 000000 c3c3 c3c3 0000 0000 0000 0000 0000 0000 98# CHECK3-NEXT: 000010 0000 0000 0000 0000 0000 0000 0000 0000 99# CHECK3-NEXT: * 100# CHECK3-NEXT: 002000 3232 101# SIZE3: 8194 102 103--- !ELF 104FileHeader: 105 Class: ELFCLASS64 106 Data: ELFDATA2LSB 107 Type: ET_EXEC 108 Machine: EM_X86_64 109Sections: 110 - Name: .text 111 Type: SHT_PROGBITS 112 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 113 ## Not in a PT_LOAD. LMA = sh_addr = 0x1000. 114 Address: 0x1000 115 AddressAlign: 0x1000 116 Content: "c3c3c3c3" 117 - Name: .data 118 Type: SHT_PROGBITS 119 Flags: [ SHF_ALLOC, SHF_WRITE ] 120 ## sh_addr is increased from 0x2000 to 0x3000, but it is ignored. 121 ## LMA = sh_offset-p_offset+p_paddr = 0x2000-0x2000+0x3000 = 0x3000. 122 Address: 0x3000 123 AddressAlign: 0x1000 124 Content: "3232" 125ProgramHeaders: 126 - Type: PT_LOAD 127 Flags: [ PF_R, PF_W ] 128 VAddr: 0x3000 129 FirstSec: .data 130 LastSec: .data 131 132## The first section (.text) is empty. Test that we skip its LMA until the first 133## non-empty section, otherwise we would leave a large number of leading zeroes. 134# RUN: yaml2obj --docnum=4 %s -o %t4 135# RUN: llvm-objcopy -O binary %t4 %t4.out 136# RUN: od -A x -t x2 %t4.out | FileCheck %s --check-prefix=SKIPEMPTY 137 138# SKIPEMPTY: 000000 3232 139# SKIPEMPTY-NEXT: 000002 140 141--- !ELF 142FileHeader: 143 Class: ELFCLASS64 144 Data: ELFDATA2LSB 145 Type: ET_EXEC 146 Machine: EM_X86_64 147Sections: 148 - Name: .text 149 Type: SHT_PROGBITS 150 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 151 Address: 0x1000 152 AddressAlign: 0x1000 153 - Name: gap 154 Type: Fill 155 Size: 0x1000 156 - Name: .data 157 Type: SHT_PROGBITS 158 Flags: [ SHF_ALLOC, SHF_WRITE ] 159 Content: "3232" 160 161## The last section (.data) is empty. Test that we stop dumping after the last 162## non-empty section, otherwise we would leave a large number of trailing zeroes. 163# RUN: yaml2obj --docnum=5 %s -o %t5 164# RUN: llvm-objcopy -O binary %t5 %t5.out 165# RUN: od -A x -t x2 %t5.out | FileCheck %s --check-prefix=SKIPEMPTY 166 167--- !ELF 168FileHeader: 169 Class: ELFCLASS64 170 Data: ELFDATA2LSB 171 Type: ET_EXEC 172 Machine: EM_X86_64 173Sections: 174 - Name: .text 175 Type: SHT_PROGBITS 176 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 177 Address: 0x1000 178 AddressAlign: 0x1000 179 Content: "3232" 180 - Name: gap 181 Type: Fill 182 Size: 0xffd 183 - Name: .data 184 Type: SHT_PROGBITS 185 Flags: [ SHF_ALLOC, SHF_WRITE ] 186 187## NOBITS sections should not appear in output. 188# RUN: yaml2obj --docnum=6 %s -o %t6 189# RUN: llvm-objcopy -O binary %t6 %t6.out 190# RUN: od -A x -t x2 %t6.out | FileCheck %s --check-prefix=SKIPNOBITS --ignore-case 191 192# SKIPNOBITS: 000000 c3c3 c3c3 193# SKIPNOBITS-NEXT: 000004 194 195--- !ELF 196FileHeader: 197 Class: ELFCLASS64 198 Data: ELFDATA2LSB 199 Type: ET_EXEC 200 Machine: EM_X86_64 201Sections: 202 - Name: .bss 203 Type: SHT_NOBITS 204 Flags: [ SHF_ALLOC ] 205 Address: 0x1000 206 AddressAlign: 0x1000 207 Size: 0x123 208 - Name: gap 209 Type: Fill 210 Size: 0xffd 211 - Name: .text 212 Type: SHT_PROGBITS 213 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 214 Address: 0x4000 215 AddressAlign: 0x1000 216 Content: "c3c3c3c3" 217 218## If .bss is converted to non-SHT_NOBITS, align its new offset. This may affect 219## the output size. 220# RUN: yaml2obj --docnum=7 %s -o %t7 221# RUN: llvm-objcopy -O binary --set-section-flags .bss=alloc,contents %t7 %t7.out 222# RUN: od -A x -t x2 %t7.out | FileCheck %s --check-prefix=FILLNOBITS --ignore-case 223 224# FILLNOBITS: 000000 c3c3 0000 0000 0000 0000 0000 0000 0000 225# FILLNOBITS-NEXT: 000010 0000 00 226# FILLNOBITS-NEXT: 000013 227 228--- !ELF 229FileHeader: 230 Class: ELFCLASS64 231 Data: ELFDATA2LSB 232 Type: ET_EXEC 233 Machine: EM_X86_64 234Sections: 235 - Name: .text 236 Type: SHT_PROGBITS 237 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 238 Address: 0x1000 239 AddressAlign: 0x1000 240 Content: "c3c3" 241 - Name: .bss 242 Type: SHT_NOBITS 243 Flags: [ SHF_ALLOC ] 244 ## sh_offset is not aligned. 245 ShOffset: 0x1002 246 Size: 0x3 247 AddressAlign: 0x10 248ProgramHeaders: 249 - Type: PT_LOAD 250 Flags: [ PF_R, PF_W, PF_X ] 251 Offset: 0x1000 252 VAddr: 0x1000 253 PAddr: 0x1000 254 FileSize: 0x2 255 MemSize: 0x13 256 Align: 0x1000 257 258## Test that sections do not move relative to their physical addresses if 259## the physical address is not aligned. This behavior matches GNU objcopy 260## and is important for embedded images where the physical address is 261## used to store the initial data image. Without converting the type of a 262## NOBITS section, don't align the offset. This matches GNU objcopy when 263## the physical address of a section is not aligned. 264 265# RUN: yaml2obj --docnum=8 %s -o %t8 266# RUN: llvm-objcopy -O binary --only-section=.text --only-section=.data %t8 %t8.out 267# RUN: od -A x -t x1 %t8.out | FileCheck %s --check-prefix=PHYSUNALIGNED --ignore-case 268 269# PHYSUNALIGNED: 000000 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 00 270# PHYSUNALIGNED-NEXT: 000010 11 22 33 44 bd ac cd ab 271 272--- !ELF 273FileHeader: 274 Class: ELFCLASS64 275 Data: ELFDATA2LSB 276 Type: ET_EXEC 277 Machine: EM_X86_64 278ProgramHeaders: 279 - Type: PT_LOAD 280 Flags: [ PF_X, PF_R ] 281 FirstSec: .text 282 LastSec: .text 283 VAddr: 0x200000 284 PAddr: 0x200000 285 Align: 0x1000 286 - Type: PT_LOAD 287 Flags: [ PF_W, PF_R ] 288 FirstSec: .data 289 LastSec: .data 290 VAddr: 0x400000 291 PAddr: 0x200014 292## PAddr is not aligned by sh_addralign(.data) 293 Align: 0x1000 294Sections: 295 - Name: .text 296 Type: SHT_PROGBITS 297 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 298 Address: 0x200000 299 AddressAlign: 0x1 300 Offset: 0x1000 301 Content: 112233445566778899aabbccddeeff0011223344 302 - Name: .data 303 Type: SHT_PROGBITS 304 Flags: [ SHF_WRITE, SHF_ALLOC ] 305 Address: 0x400000 306 AddressAlign: 0x8 307 Offset: 0x2000 308 Content: BDACCDAB 309