457 lines · yaml
1## Check we can use "SectionHeaderTable" tag to reorder section header entries.2 3## This is a general test that has sections with unique prefixes, a fill and a4## section without the unique prefix. The section header table describes sections5## in the same order they are listed in the YAML.6# RUN: yaml2obj %s --docnum=1 -o %t1 -DSEC1=".section (1)" -DSEC2=".section (2)" -DSEC3=".section.foo"7# RUN: llvm-readelf --section-headers %t1 | FileCheck %s --check-prefix=NO-OP8 9# NO-OP: Section Headers:10# NO-OP-NEXT: [Nr] Name Type Address Off Size ES Flg Lk Inf Al11# NO-OP-NEXT: [ 0] NULL 0000000000000000 000000 000000 00 0 0 012# NO-OP-NEXT: [ 1] .section PROGBITS 0000000000000000 000040 000010 00 0 0 013# NO-OP-NEXT: [ 2] .section PROGBITS 0000000000000000 000050 000020 00 0 0 014# NO-OP-NEXT: [ 3] .section.foo PROGBITS 0000000000000000 0000a0 000040 00 0 0 015# NO-OP-NEXT: [ 4] .strtab STRTAB 0000000000000000 0000e0 000001 00 0 0 116# NO-OP-NEXT: [ 5] .shstrtab STRTAB 0000000000000000 0000e1 000029 00 0 0 117 18--- !ELF19FileHeader:20 Class: ELFCLASS6421 Data: ELFDATA2LSB22 Type: ET_REL23Sections:24 - Name: .section (1)25 Type: SHT_PROGBITS26 Size: 0x1027 - Name: .section (2)28 Type: SHT_PROGBITS29 Size: 0x2030 - Type: Fill31 Name: .filler32 Size: 0x3033 Pattern: ""34 - Name: .section.foo35 Type: SHT_PROGBITS36 Size: 0x4037 - Type: SectionHeaderTable38 Sections:39 - Name: [[SEC1]]40 - Name: [[SEC2]]41 - Name: [[SEC3]]42 - Name: .strtab43 - Name: .shstrtab44 45## Show we are able to reorder sections.46# RUN: yaml2obj %s -o %t2 -DSEC3=".section (1)" -DSEC2=".section (2)" -DSEC1=".section.foo"47# RUN: llvm-readelf --section-headers %t2 | FileCheck %s --check-prefix=REORDERED48 49# REORDERED: Section Headers:50# REORDERED-NEXT: [Nr] Name Type Address Off Size ES Flg Lk Inf Al51# REORDERED-NEXT: [ 0] NULL 0000000000000000 000000 000000 00 0 0 052# REORDERED-NEXT: [ 1] .section.foo PROGBITS 0000000000000000 0000a0 000040 00 0 0 053# REORDERED-NEXT: [ 2] .section PROGBITS 0000000000000000 000050 000020 00 0 0 054# REORDERED-NEXT: [ 3] .section PROGBITS 0000000000000000 000040 000010 00 0 0 055# REORDERED-NEXT: [ 4] .strtab STRTAB 0000000000000000 0000e0 000001 00 0 0 156# REORDERED-NEXT: [ 5] .shstrtab STRTAB 0000000000000000 0000e1 000029 00 0 0 157 58## Show we report proper errors when the section header description:59## a) contains a repeated section name.60## b) omits any section that exists.61## c) contains a non-existent section.62# RUN: not yaml2obj %s -o /dev/null -DSEC1=".section.foo" -DSEC2="unknown" -DSEC3=".section.foo" 2>&1 | \63# RUN: FileCheck %s --check-prefix=ERR164# d) contains a repeated implicit section name.65# e) contains a fill name.66# RUN: not yaml2obj %s -o /dev/null -DSEC1=".strtab" -DSEC2=".shstrtab" -DSEC3=".filler" 2>&1 | \67# RUN: FileCheck %s --check-prefix=ERR268 69# ERR1: error: repeated section name: '.section.foo' in the section header description70# ERR1-NEXT: error: section '.section (1)' should be present in the 'Sections' or 'Excluded' lists71# ERR1-NEXT: error: section '.section (2)' should be present in the 'Sections' or 'Excluded' lists72# ERR1-NEXT: error: section header contains undefined section 'unknown'73 74# ERR2: error: repeated section name: '.strtab' in the section header description75# ERR2-NEXT: error: repeated section name: '.shstrtab' in the section header description76# ERR2-NEXT: error: section '.section (1)' should be present in the 'Sections' or 'Excluded' lists77# ERR2-NEXT: error: section '.section (2)' should be present in the 'Sections' or 'Excluded' lists78# ERR2-NEXT: error: section '.section.foo' should be present in the 'Sections' or 'Excluded' lists79# ERR2-NEXT: error: section header contains undefined section '.filler'80 81## Check that when the an empty "Sections" list is used, we do not create an empty section header table.82# RUN: not yaml2obj %s --docnum=2 -o /dev/null 2>&1 | FileCheck %s --check-prefix=EMPTY-SECTIONS83 84# EMPTY-SECTIONS: error: section '.foo' should be present in the 'Sections' or 'Excluded' lists85# EMPTY-SECTIONS-NEXT: error: section '.strtab' should be present in the 'Sections' or 'Excluded' lists86# EMPTY-SECTIONS-NEXT: error: section '.shstrtab' should be present in the 'Sections' or 'Excluded' lists87 88--- !ELF89FileHeader:90 Class: ELFCLASS6491 Data: ELFDATA2LSB92 Type: ET_REL93Sections:94 - Name: .foo95 Type: SHT_PROGBITS96 - Type: SectionHeaderTable97 Sections: []98 99## Test that we are able to use "NoHeaders" property to produce an empty section header table.100# RUN: yaml2obj %s --docnum=3 -DNOHEADERS=true -o %t3.1101# RUN: llvm-readelf --file-headers %t3.1 | FileCheck %s --check-prefix=NO-HEADERS-TRUE102 103# NO-HEADERS-TRUE: Start of section headers: 0 (bytes into file)104# NO-HEADERS-TRUE: Size of section headers: 64 (bytes)105# NO-HEADERS-TRUE: Number of section headers: 0106# NO-HEADERS-TRUE: Section header string table index: 0107 108--- !ELF109FileHeader:110 Class: ELFCLASS64111 Data: ELFDATA2LSB112 Type: ET_REL113Sections:114 - Name: .foo115 Type: SHT_PROGBITS116 - Type: SectionHeaderTable117 NoHeaders: [[NOHEADERS]]118 119## Test that we are able to set NoHeaders to false. In this case the tool produces an output120## as if there were no `SectionHeaderTable` key at all.121# RUN: yaml2obj %s --docnum=3 -DNOHEADERS=false -o %t3.2122# RUN: llvm-readelf --file-headers --sections %t3.2 | FileCheck %s --check-prefix=NO-HEADERS-FALSE123 124# NO-HEADERS-FALSE: Start of section headers: 96 (bytes into file)125# NO-HEADERS-FALSE: Size of section headers: 64 (bytes)126# NO-HEADERS-FALSE: Number of section headers: 4127# NO-HEADERS-FALSE: Section header string table index: 3128 129# NO-HEADERS-FALSE: Section Headers:130# NO-HEADERS-FALSE-NEXT: [Nr] Name Type Address Off Size ES Flg Lk Inf Al131# NO-HEADERS-FALSE-NEXT: [ 0] NULL 0000000000000000 000000 000000 00 0 0 0132# NO-HEADERS-FALSE-NEXT: [ 1] .foo PROGBITS 0000000000000000 000040 000000 00 0 0 0133# NO-HEADERS-FALSE-NEXT: [ 2] .strtab STRTAB 0000000000000000 000040 000001 00 0 0 1134# NO-HEADERS-FALSE-NEXT: [ 3] .shstrtab STRTAB 0000000000000000 000041 000018 00 0 0 1135 136## Check we do not allow using "Sections" together with "NoHeaders".137# RUN: not yaml2obj %s --docnum=4 -DNOHEADERS=true -o /dev/null 2>&1 | FileCheck %s --check-prefix=SECTIONS-NO-HEADERS138# RUN: not yaml2obj %s --docnum=4 -DNOHEADERS=false -o /dev/null 2>&1 | FileCheck %s --check-prefix=SECTIONS-NO-HEADERS139 140# SECTIONS-NO-HEADERS: error: NoHeaders can't be used together with Offset/Sections/Excluded141 142--- !ELF143FileHeader:144 Class: ELFCLASS64145 Data: ELFDATA2LSB146 Type: ET_REL147Sections:148 - Name: .foo149 Type: SHT_PROGBITS150 - Type: SectionHeaderTable151 Sections: []152 NoHeaders: [[NOHEADERS]]153 154## Check that we allow using an empty SectionHeaderTable definition.155## It can be used to emit the default section header table at an arbitrary position.156 157# RUN: yaml2obj %s --docnum=5 -o %t5.novalues158# RUN: llvm-readelf --sections %t5.novalues | \159# RUN: FileCheck %s --check-prefix=NO-VALUES160 161## Check we placed the section header table before the .foo section.162 163# NO-VALUES: There are 4 section headers, starting at offset 0x40:164# NO-VALUES: [Nr] Name Type Address Off Size165# NO-VALUES: [ 1] .foo PROGBITS 0000000000000000 000140 000000166 167--- !ELF168FileHeader:169 Class: ELFCLASS64170 Data: ELFDATA2LSB171 Type: ET_REL172Sections:173 - Type: SectionHeaderTable174 - Name: .foo175 Type: SHT_PROGBITS176 177## Test that we are still able to override e_shoff, e_shnum and e_shstrndx178## fields even when we do not produce section headers.179# RUN: yaml2obj %s --docnum=6 -o %t4180# RUN: not llvm-readelf --file-headers %t4 | FileCheck %s --check-prefix=NO-HEADERS-OVERRIDE181 182# NO-HEADERS-OVERRIDE: Start of section headers: 2 (bytes into file)183# NO-HEADERS-OVERRIDE: Number of section headers: 3184# NO-HEADERS-OVERRIDE: Section header string table index: 4185 186--- !ELF187FileHeader:188 Class: ELFCLASS64189 Data: ELFDATA2LSB190 Type: ET_REL191 EShOff: 0x2192 EShNum: 0x3193 EShStrNdx: 0x4194Sections:195 - Type: SectionHeaderTable196 NoHeaders: true197 198## Check that section indices are updated properly in other places when we199## reorder sections in the section header table.200# RUN: yaml2obj %s --docnum=7 -o %t5 -DSEC1=".foo" -DSEC2=".bar"201# RUN: llvm-readelf --section-headers --symbols %t5 | FileCheck %s --check-prefix=INDICES-A202# RUN: yaml2obj %s --docnum=7 -o %t6 -DSEC2=".foo" -DSEC1=".bar"203# RUN: llvm-readelf --section-headers --symbols %t6 | FileCheck %s --check-prefix=INDICES-B204 205# INDICES-A: [Nr] Name Type Address Off Size ES Flg Lk206# INDICES-A: [ 1] .foo PROGBITS 0000000000000000 000040 000000 00 0207# INDICES-A-NEXT: [ 2] .bar PROGBITS 0000000000000000 000040 000000 00 0208# INDICES-A-NEXT: [ 3] .another.1 PROGBITS 0000000000000000 000040 000000 00 1209# INDICES-A-NEXT: [ 4] .another.2 PROGBITS 0000000000000000 000040 000000 00 2210 211# INDICES-A: Num: Value Size Type Bind Vis Ndx Name212# INDICES-A: 1: 0000000000000000 0 NOTYPE LOCAL DEFAULT 1 foo213# INDICES-A-NEXT: 2: 0000000000000000 0 NOTYPE LOCAL DEFAULT 2 bar214 215# INDICES-B: [ 1] .bar PROGBITS 0000000000000000 000040 000000 00 0216# INDICES-B-NEXT: [ 2] .foo PROGBITS 0000000000000000 000040 000000 00 0217# INDICES-B-NEXT: [ 3] .another.1 PROGBITS 0000000000000000 000040 000000 00 2218# INDICES-B-NEXT: [ 4] .another.2 PROGBITS 0000000000000000 000040 000000 00 1219 220# INDICES-B: Num: Value Size Type Bind Vis Ndx Name221# INDICES-B: 1: 0000000000000000 0 NOTYPE LOCAL DEFAULT 2 foo222# INDICES-B-NEXT: 2: 0000000000000000 0 NOTYPE LOCAL DEFAULT 1 bar223 224--- !ELF225FileHeader:226 Class: ELFCLASS64227 Data: ELFDATA2LSB228 Type: ET_REL229Sections:230 - Name: .foo231 Type: SHT_PROGBITS232 - Name: .bar233 Type: SHT_PROGBITS234 - Name: .another.1235 Link: .foo236 Type: SHT_PROGBITS237 - Name: .another.2238 Link: .bar239 Type: SHT_PROGBITS240 - Type: SectionHeaderTable241 Sections:242 - Name: [[SEC1]]243 - Name: [[SEC2]]244 - Name: .another.1245 - Name: .another.2246 - Name: .symtab247 - Name: .strtab248 - Name: .shstrtab249Symbols:250 - Name: foo251 Section: .foo252 - Name: bar253 Section: .bar254 255## Check that when "NoHeaders" is set to "true" then we don't emit256## the .shstrtab section implicitly and don't write the data of the257## section header table to the file.258 259# RUN: yaml2obj %s --docnum=8 -o %t8260# RUN: wc -c < %t8 | FileCheck %s --check-prefix=SIZE261 262# SIZE: 511{{$}}263 264--- !ELF265FileHeader:266 Class: ELFCLASS64267 Data: ELFDATA2LSB268 Type: ET_REL269Sections:270## We don't want any implicit sections to be added after the .foo section,271## so add them here explicitly.272 - Name: .strtab273 Type: SHT_STRTAB274## Nothing should be emitted after the following section.275## So we know that the expected file size is 0x100 + 0xFF == 0x1FF == 511.276 - Name: .foo277 Type: SHT_PROGBITS278## Unaligned size. Used to make sure that we don't try to align the file offset279## for writing the section header table.280 Size: 0xFF281 Offset: 0x100282 - Type: SectionHeaderTable283 NoHeaders: true284 285## Check we do not allow using "Offset" together with "NoHeaders".286# RUN: not yaml2obj %s --docnum=9 -DNOHEADERS=true -o /dev/null 2>&1 | FileCheck %s --check-prefix=NO-HEADERS-OFFSET287# RUN: not yaml2obj %s --docnum=9 -DNOHEADERS=false -o /dev/null 2>&1 | FileCheck %s --check-prefix=NO-HEADERS-OFFSET288 289# NO-HEADERS-OFFSET: error: NoHeaders can't be used together with Offset/Sections/Excluded290 291--- !ELF292FileHeader:293 Class: ELFCLASS64294 Data: ELFDATA2LSB295 Type: ET_REL296Sections:297 - Name: .foo298 Type: SHT_PROGBITS299 - Type: SectionHeaderTable300 Offset: 0x1000301 NoHeaders: [[NOHEADERS]]302 303## Check we can place the section header table before other sections.304 305# RUN: yaml2obj %s --docnum=10 -o %t10306# RUN: llvm-readelf --file-headers --sections %t10 | FileCheck %s --check-prefix=PLACE-BEFORE307 308# PLACE-BEFORE: Start of section headers: 64 (bytes into file)309# PLACE-BEFORE: Size of section headers: 64 (bytes)310# PLACE-BEFORE: Number of section headers: 4311 312# PLACE-BEFORE: Section Headers:313# PLACE-BEFORE: [Nr] Name Type Address Off Size314## 0x140 == Start of section headers (64) + Size of section headers (64) * Number of section headers (4);315# PLACE-BEFORE: [ 1] .foo PROGBITS 0000000000000000 000140 000000316 317--- !ELF318FileHeader:319 Class: ELFCLASS64320 Data: ELFDATA2LSB321 Type: ET_REL322Sections:323 - Type: SectionHeaderTable324 Offset: [[OFFSET=<none>]]325 Sections:326 - Name: .foo327 - Name: .strtab328 - Name: .shstrtab329 - Name: .foo330 Type: SHT_PROGBITS331 332## Check we can use the Offset key to place the section header table at a different location.333 334# RUN: yaml2obj %s --docnum=10 -DOFFSET=0x1000 -o %t10.offset335# RUN: llvm-readelf --file-headers --sections %t10.offset | \336# RUN: FileCheck %s --check-prefix=PLACE-BEFORE-OFFSET337 338# PLACE-BEFORE-OFFSET: Start of section headers: 4096 (bytes into file)339# PLACE-BEFORE-OFFSET: Size of section headers: 64 (bytes)340# PLACE-BEFORE-OFFSET: Number of section headers: 4341 342# PLACE-BEFORE-OFFSET: Section Headers:343# PLACE-BEFORE-OFFSET: [Nr] Name Type Address Off Size344## 0x1100 == Start of section headers (0x1000) + Size of section headers (64) * Number of section headers (4);345# PLACE-BEFORE-OFFSET: [ 1] .foo PROGBITS 0000000000000000 001100 000000346 347## Check we can place the section header table somewhere in the middle of the sections list.348 349# RUN: yaml2obj %s --docnum=11 -o %t11350# RUN: llvm-readelf --sections %t11 | FileCheck %s --check-prefix=PLACE-AT-MIDDLE351 352# PLACE-AT-MIDDLE: There are 5 section headers, starting at offset 0x140:353# PLACE-AT-MIDDLE: [Nr] Name Type Address Off Size354# PLACE-AT-MIDDLE: [ 1] .foo PROGBITS 0000000000000000 000040 000100355## The section header table is placed here, at 0x40 + 0x100 == 0x140.356## The size of the table is 5 * 64 == 320 == 0x140.357## 0x140 + 0x140 == offset of the .bar section.358# PLACE-AT-MIDDLE-NEXT: [ 2] .bar PROGBITS 0000000000000000 000280 000200359# PLACE-AT-MIDDLE-NEXT: [ 3] .strtab STRTAB 0000000000000000 000480 000001360# PLACE-AT-MIDDLE-NEXT: [ 4] .shstrtab STRTAB 0000000000000000 000481 00001d361 362--- !ELF363FileHeader:364 Class: ELFCLASS64365 Data: ELFDATA2LSB366 Type: ET_REL367Sections:368 - Name: .foo369 Type: SHT_PROGBITS370 Size: 0x100371 - Type: SectionHeaderTable372 Sections:373 - Name: .foo374 - Name: .bar375 - Name: .strtab376 - Name: .shstrtab377 - Name: .bar378 Type: SHT_PROGBITS379 Size: 0x200380 381## Check we don`t allow having multiple SectionHeaderTable chunks.382 383# RUN: not yaml2obj %s --docnum=12 -o /dev/null 2>&1 | FileCheck %s --check-prefix=MULTIPLE384 385# MULTIPLE: error: multiple section header tables are not allowed386 387--- !ELF388FileHeader:389 Class: ELFCLASS64390 Data: ELFDATA2LSB391 Type: ET_REL392Sections:393 - Type: SectionHeaderTable394 Sections: []395 - Type: SectionHeaderTable396 Sections: []397 398## Check that we place implicit sections before the section header table399## when it is placed at the end explicitly.400 401# RUN: yaml2obj %s --docnum=13 -o %t13402# RUN: llvm-readelf --sections %t13 | FileCheck %s --check-prefix=IMPLICIT403 404# IMPLICIT: There are 3 section headers, starting at offset 0x58:405# IMPLICIT: [Nr] Name Type Address Off Size406# IMPLICIT: [ 1] .strtab STRTAB 0000000000000000 000040 000001407# IMPLICIT-NEXT: [ 2] .shstrtab STRTAB 0000000000000000 000041 000013408## The section header table is placed here, at 0x58.409## alignTo(8, 0x41 + 0x13) == 0x58.410 411--- !ELF412FileHeader:413 Class: ELFCLASS64414 Data: ELFDATA2LSB415 Type: ET_REL416Sections:417 - Type: SectionHeaderTable418 Sections:419 - Name: .strtab420 - Name: .shstrtab421 422## Show that we can reference both the first and last section in the section423## header table, via sh_link.424 425# RUN: yaml2obj %s --docnum=14 -o %t14426# RUN: llvm-readelf --sections %t14 | FileCheck %s --check-prefix=LINK427 428--- !ELF429FileHeader:430 Class: ELFCLASS64431 Data: ELFDATA2LSB432 Type: ET_REL433Sections:434 - Name: .strtab435 Type: SHT_STRTAB436 - Type: SectionHeaderTable437 Sections:438 - Name: .strtab439 - Name: .ref.first440 - Name: .ref.last441 - Name: .shstrtab442 - Name: .ref.first443 Type: SHT_PROGBITS444 Link: .strtab445 - Name: .ref.last446 Type: SHT_PROGBITS447 Link: .shstrtab448 - Name: .shstrtab449 Type: SHT_STRTAB450 451# LINK: There are 5 section headers452# LINK: [Nr] Name Type Address Off Size ES Flg Lk453# LINK: [ 1] .strtab454# LINK-NEXT: [ 2] .ref.first PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] [[#%x,]] 1455# LINK-NEXT: [ 3] .ref.last PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] [[#%x,]] 4456# LINK-NEXT: [ 4] .shstrtab457