1## Check we are able to set an offset field for sections using the 'Offset' key. 2 3## Show how the 'Offset' field key can be used. 4## Show that it can affect the layout of the rest of the file. 5 6# RUN: yaml2obj --docnum=1 %s -o %t1 7# RUN: llvm-readelf --sections %t1 | FileCheck %s --check-prefix=DEFAULT 8 9# DEFAULT: [Nr] Name Type Address Off 10# DEFAULT: [ 1] .foo PROGBITS 0000000000000000 000040 11# DEFAULT-NEXT: [ 2] .bar PROGBITS 0000000000000000 000048 12# DEFAULT-NEXT: [ 3] .strtab STRTAB 0000000000000000 000058 13# DEFAULT-NEXT: [ 4] .shstrtab STRTAB 0000000000000000 000059 14 15--- !ELF 16FileHeader: 17 Class: ELFCLASS64 18 Data: ELFDATA2LSB 19 Type: ET_REL 20Sections: 21 - Name: .foo 22 Type: SHT_PROGBITS 23 Size: 0x8 24 - Name: .bar 25 Type: SHT_PROGBITS 26 Size: 0x10 27## It is a no-op. We set it to reduce amount 28## of differences with the second YAML below. 29 AddressAlign: 0x0 30 31## The same as previous, but an arbitrary 'Offset' is set for the .bar section. 32# RUN: yaml2obj --docnum=2 %s -o %t2 -DOFFSET=0x50 -DALIGN=0x0 33# RUN: llvm-readelf --sections %t2 | FileCheck %s --check-prefix=OFFSET 34 35# OFFSET: [Nr] Name Type Address Off 36# OFFSET: [ 1] .foo PROGBITS 0000000000000000 000040 37# OFFSET-NEXT: [ 2] .bar PROGBITS 0000000000000000 000050 38# OFFSET-NEXT: [ 3] .strtab STRTAB 0000000000000000 000060 39# OFFSET-NEXT: [ 4] .shstrtab STRTAB 0000000000000000 000061 40 41--- !ELF 42FileHeader: 43 Class: ELFCLASS64 44 Data: ELFDATA2LSB 45 Type: ET_REL 46Sections: 47 - Name: .foo 48 Type: SHT_PROGBITS 49 Size: 0x8 50 - Name: .bar 51 Type: SHT_PROGBITS 52 Size: 0x10 53 Offset: [[OFFSET]] 54 AddressAlign: [[ALIGN]] 55 56## Set the 'Offset' to the same value as was set by default to show 57## that there is no difference in the output in this case. 58# RUN: yaml2obj --docnum=2 %s -o %t3 -DOFFSET=0x48 -DALIGN=0x0 59# RUN: cmp %t1 %t3 60 61## Show that we can set an offset and an address alignment independently for a section. 62# RUN: yaml2obj --docnum=2 %s -o %t4 -DOFFSET=0x48 -DALIGN=0x5 63# RUN: llvm-readelf --sections %t4 | FileCheck %s --check-prefix=OFFSET-AND-ALIGN 64 65# OFFSET-AND-ALIGN: [Nr] Name Type Address Off Size ES Flg Lk Inf Al 66# OFFSET-AND-ALIGN: [ 2] .bar PROGBITS 0000000000000000 000048 000010 00 0 0 5 67 68## Show we do not allow an 'Offset' to go backward. 69# RUN: not yaml2obj --docnum=2 %s -DOFFSET=0x47 -DALIGN=0x0 2>&1 | \ 70# RUN: FileCheck %s --check-prefix=ERR-BACKWARD 71 72# ERR-BACKWARD: error: the 'Offset' value (0x47) goes backward 73 74## Show that the 'Offset' key can be used together with the 'ShOffset' key. 75 76## Case 1: set the same value for 'Offset' and 'ShOffset' keys. 77# RUN: yaml2obj --docnum=3 %s -o %t5 -DSHOFFSET=0x100 -DOFFSET=0x100 78# RUN: llvm-readelf --headers --sections %t5 | FileCheck %s --check-prefix=BOTH-SAME 79 80## The same offset as in the Case 3. 81# BOTH-SAME: Start of section headers: 288 (bytes into file) 82 83# BOTH-SAME: [Nr] Name Type Address Off 84# BOTH-SAME: [ 1] .foo PROGBITS 0000000000000000 000100 85# BOTH-SAME-NEXT: [ 2] .bar PROGBITS 0000000000000000 000101 86# BOTH-SAME-NEXT: [ 3] .strtab STRTAB 0000000000000000 000102 87 88## Case 2: set the 'Offset' value to be less than the 'ShOffset'. 89 90# RUN: yaml2obj --docnum=3 %s -o %t6 -DSHOFFSET=0x100 -DOFFSET=0x90 91# RUN: llvm-readelf --headers --sections %t6 | FileCheck %s --check-prefix=BOTH-A 92 93## 176 < 288 (start of section headers in Case 1). 94# BOTH-A: Start of section headers: 176 (bytes into file) 95 96## Show that the 'Offset' field sets the physical offset in a file and the `ShOffset` 97## field only overrides the sh_offset value of the .foo section. 98# BOTH-A: [Nr] Name Type Address Off 99# BOTH-A: [ 1] .foo PROGBITS 0000000000000000 000100 100# BOTH-A-NEXT: [ 2] .bar PROGBITS 0000000000000000 000091 101# BOTH-A-NEXT: [ 3] .strtab STRTAB 0000000000000000 000092 102 103## Case 3: set the 'Offset' value to be greater than the 'ShOffset' value. 104 105# RUN: yaml2obj --docnum=3 %s -o %t7 -DSHOFFSET=0x90 -DOFFSET=0x100 106# RUN: llvm-readelf --sections --headers %t7 | FileCheck %s --check-prefix=BOTH-B 107 108## The same offset as in Case 1. 109# BOTH-B: Start of section headers: 288 (bytes into file) 110 111## Show that the 'Offset' field sets the physical offset in file and `ShOffset` 112## field only affects the sh_offset value of the .foo section (overrides it). 113# BOTH-B: [Nr] Name Type Address Off 114# BOTH-B: [ 1] .foo PROGBITS 0000000000000000 000090 115# BOTH-B-NEXT: [ 2] .bar PROGBITS 0000000000000000 000101 116# BOTH-B-NEXT: [ 3] .strtab STRTAB 0000000000000000 000102 117 118--- !ELF 119FileHeader: 120 Class: ELFCLASS64 121 Data: ELFDATA2LSB 122 Type: ET_REL 123Sections: 124 - Name: .foo 125 Type: SHT_PROGBITS 126 Size: 0x1 127 ShOffset: [[SHOFFSET]] 128 Offset: [[OFFSET]] 129 - Name: .bar 130 Type: SHT_PROGBITS 131 Size: 0x1 132