[yaml2obj/obj2yaml] - Make RawContentSection::Content and RawContentSection::Size optional

This is a follow-up for D62809.

Content and Size fields should be optional as was discussed in comments
of the D62809's thread. With that, we can describe a specific string table and
symbol table sections in a more correct way and also show appropriate errors.

The patch adds lots of test cases where the behavior is described in details.

Differential revision: https://reviews.llvm.org/D62957

llvm-svn: 362931
This commit is contained in:
George Rimar 2019-06-10 12:43:18 +00:00
parent 1f73bbbd3a
commit 1e41007aeb
11 changed files with 810 additions and 54 deletions

View File

@ -151,8 +151,8 @@ struct DynamicSection : Section {
}; };
struct RawContentSection : Section { struct RawContentSection : Section {
yaml::BinaryRef Content; Optional<yaml::BinaryRef> Content;
llvm::yaml::Hex64 Size; Optional<llvm::yaml::Hex64> Size;
llvm::yaml::Hex64 Info; llvm::yaml::Hex64 Info;
RawContentSection() : Section(SectionKind::RawContent) {} RawContentSection() : Section(SectionKind::RawContent) {}

View File

@ -914,7 +914,7 @@ static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section) {
static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) { static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
commonSectionMapping(IO, Section); commonSectionMapping(IO, Section);
IO.mapOptional("Content", Section.Content); IO.mapOptional("Content", Section.Content);
IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size())); IO.mapOptional("Size", Section.Size);
IO.mapOptional("Info", Section.Info, Hex64(0)); IO.mapOptional("Info", Section.Info, Hex64(0));
} }
@ -1042,9 +1042,12 @@ void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping(
StringRef MappingTraits<std::unique_ptr<ELFYAML::Section>>::validate( StringRef MappingTraits<std::unique_ptr<ELFYAML::Section>>::validate(
IO &io, std::unique_ptr<ELFYAML::Section> &Section) { IO &io, std::unique_ptr<ELFYAML::Section> &Section) {
const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(Section.get()); const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(Section.get());
if (!RawSection || RawSection->Size >= RawSection->Content.binary_size()) if (!RawSection)
return StringRef(); return {};
return "Section size must be greater or equal to the content size"; if (RawSection->Size && RawSection->Content &&
(uint64_t)(*RawSection->Size) < RawSection->Content->binary_size())
return "Section size must be greater than or equal to the content size";
return {};
} }
namespace { namespace {

View File

@ -389,7 +389,6 @@ ELF-MIPSEL-NEXT: - Name: .data
ELF-MIPSEL-NEXT: Type: SHT_PROGBITS ELF-MIPSEL-NEXT: Type: SHT_PROGBITS
ELF-MIPSEL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ] ELF-MIPSEL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ]
ELF-MIPSEL-NEXT: AddressAlign: 0x0000000000000004 ELF-MIPSEL-NEXT: AddressAlign: 0x0000000000000004
ELF-MIPSEL-NEXT: Content: ''
ELF-MIPSEL-NEXT: - Name: .bss ELF-MIPSEL-NEXT: - Name: .bss
ELF-MIPSEL-NEXT: Type: SHT_NOBITS ELF-MIPSEL-NEXT: Type: SHT_NOBITS
ELF-MIPSEL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ] ELF-MIPSEL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ]
@ -398,7 +397,6 @@ ELF-MIPSEL-NEXT: Size: 0x0000000000000004
ELF-MIPSEL-NEXT: - Name: .mdebug.abi32 ELF-MIPSEL-NEXT: - Name: .mdebug.abi32
ELF-MIPSEL-NEXT: Type: SHT_PROGBITS ELF-MIPSEL-NEXT: Type: SHT_PROGBITS
ELF-MIPSEL-NEXT: AddressAlign: 0x0000000000000001 ELF-MIPSEL-NEXT: AddressAlign: 0x0000000000000001
ELF-MIPSEL-NEXT: Content: ''
ELF-MIPSEL-NEXT: - Name: .rodata.str1.1 ELF-MIPSEL-NEXT: - Name: .rodata.str1.1
ELF-MIPSEL-NEXT: Type: SHT_PROGBITS ELF-MIPSEL-NEXT: Type: SHT_PROGBITS
ELF-MIPSEL-NEXT: Flags: [ SHF_ALLOC, SHF_MERGE, SHF_STRINGS ] ELF-MIPSEL-NEXT: Flags: [ SHF_ALLOC, SHF_MERGE, SHF_STRINGS ]
@ -477,7 +475,6 @@ ELF-MIPS64EL-NEXT: - Name: .text
ELF-MIPS64EL-NEXT: Type: SHT_PROGBITS ELF-MIPS64EL-NEXT: Type: SHT_PROGBITS
ELF-MIPS64EL-NEXT: Flags: [ SHF_ALLOC, SHF_EXECINSTR ] ELF-MIPS64EL-NEXT: Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
ELF-MIPS64EL-NEXT: AddressAlign: 0x0000000000000010 ELF-MIPS64EL-NEXT: AddressAlign: 0x0000000000000010
ELF-MIPS64EL-NEXT: Content: ''
ELF-MIPS64EL-NEXT: - Name: .data ELF-MIPS64EL-NEXT: - Name: .data
ELF-MIPS64EL-NEXT: Type: SHT_PROGBITS ELF-MIPS64EL-NEXT: Type: SHT_PROGBITS
ELF-MIPS64EL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ] ELF-MIPS64EL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ]
@ -506,7 +503,6 @@ ELF-MIPS64EL-NEXT: Content: '012800000000000000000000000000000000000
ELF-MIPS64EL-NEXT: - Name: .pdr ELF-MIPS64EL-NEXT: - Name: .pdr
ELF-MIPS64EL-NEXT: Type: SHT_PROGBITS ELF-MIPS64EL-NEXT: Type: SHT_PROGBITS
ELF-MIPS64EL-NEXT: AddressAlign: 0x0000000000000004 ELF-MIPS64EL-NEXT: AddressAlign: 0x0000000000000004
ELF-MIPS64EL-NEXT: Content: ''
ELF-MIPS64EL-NEXT: Symbols: ELF-MIPS64EL-NEXT: Symbols:
ELF-MIPS64EL-NEXT: - Name: .text ELF-MIPS64EL-NEXT: - Name: .text
ELF-MIPS64EL-NEXT: Type: STT_SECTION ELF-MIPS64EL-NEXT: Type: STT_SECTION
@ -550,7 +546,6 @@ ELF-X86-64-NEXT: - Name: .note.GNU-stack
ELF-X86-64-NEXT: Type: SHT_PROGBITS ELF-X86-64-NEXT: Type: SHT_PROGBITS
ELF-X86-64-NEXT: Address: 0x0000000000000033 ELF-X86-64-NEXT: Address: 0x0000000000000033
ELF-X86-64-NEXT: AddressAlign: 0x0000000000000001 ELF-X86-64-NEXT: AddressAlign: 0x0000000000000001
ELF-X86-64-NEXT: Content: ''
ELF-X86-64-NEXT: - Name: .rela.text ELF-X86-64-NEXT: - Name: .rela.text
ELF-X86-64-NEXT: Type: SHT_RELA ELF-X86-64-NEXT: Type: SHT_RELA
ELF-X86-64-NEXT: Address: 0x0000000000000038 ELF-X86-64-NEXT: Address: 0x0000000000000038
@ -610,7 +605,6 @@ ELF-AVR-NEXT: Type: SHT_PROGBITS
ELF-AVR-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ] ELF-AVR-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ]
ELF-AVR-NEXT: Address: 0x0000000000800060 ELF-AVR-NEXT: Address: 0x0000000000800060
ELF-AVR-NEXT: AddressAlign: 0x0000000000000001 ELF-AVR-NEXT: AddressAlign: 0x0000000000000001
ELF-AVR-NEXT: Content: ''
ELF-AVR-NEXT: Symbols: ELF-AVR-NEXT: Symbols:
ELF-AVR-NEXT: - Name: .text ELF-AVR-NEXT: - Name: .text
ELF-AVR-NEXT: Type: STT_SECTION ELF-AVR-NEXT: Type: STT_SECTION
@ -706,7 +700,6 @@ ELF-X86-64-UNWIND: - Name: .eh_frame
ELF-X86-64-UNWIND-NEXT: Type: SHT_X86_64_UNWIND ELF-X86-64-UNWIND-NEXT: Type: SHT_X86_64_UNWIND
ELF-X86-64-UNWIND-NEXT: Flags: [ SHF_ALLOC ] ELF-X86-64-UNWIND-NEXT: Flags: [ SHF_ALLOC ]
ELF-X86-64-UNWIND-NEXT: AddressAlign: 0x0000000000000001 ELF-X86-64-UNWIND-NEXT: AddressAlign: 0x0000000000000001
ELF-X86-64-UNWIND-NEXT: Content: ''
RUN: not obj2yaml %t.blah 2>&1 | FileCheck --check-prefix=ENOENT %s RUN: not obj2yaml %t.blah 2>&1 | FileCheck --check-prefix=ENOENT %s
ENOENT: Error reading file: {{.*}}.blah: {{[Nn]}}o such file or directory ENOENT: Error reading file: {{.*}}.blah: {{[Nn]}}o such file or directory

View File

@ -20,7 +20,7 @@ Sections:
Content: 0000000000000000 Content: 0000000000000000
Size: 2 Size: 2
# CHECK: YAML:17:5: error: Section size must be greater or equal to the content size # CHECK: YAML:17:5: error: Section size must be greater than or equal to the content size
# CHECK-NEXT: - Name: .data # CHECK-NEXT: - Name: .data
# CHECK-NEXT: ^ # CHECK-NEXT: ^
# CHECK-NEXT: yaml2obj: Failed to parse YAML file! # CHECK-NEXT: yaml2obj: Failed to parse YAML file!

View File

@ -5,7 +5,6 @@
# CHECK: - Name: .test # CHECK: - Name: .test
# CHECK-NEXT: Type: SHT_PROGBITS # CHECK-NEXT: Type: SHT_PROGBITS
# CHECK-NEXT: Content: ''
# CHECK-NEXT: Info: 0x000000000000002A # CHECK-NEXT: Info: 0x000000000000002A
--- !ELF --- !ELF

View File

@ -0,0 +1,222 @@
## For implicit dynamic symbol table sections, `Size` and/or `Content`
## fields can also be specified in YAML. Here we test the behavior in
## different cases.
## When no `Size` or `Content` is specified for a dynamic symbol table section,
## yaml2obj writes the default content.
# RUN: yaml2obj --docnum=1 %s -o %t1
# RUN: llvm-readelf %t1 --dyn-symbols | FileCheck %s --check-prefix=CASE1
# CASE1: Symbol table '.dynsym' contains 2 entries:
# CASE1-NEXT: Num: Value Size Type Bind Vis Ndx Name
# CASE1-NEXT: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
# CASE1-NEXT: 1: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND foo
# CASE1-EMPTY:
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .dynsym
Type: SHT_DYNSYM
DynamicSymbols:
- Name: foo
Binding: STB_GLOBAL
## Specifying both `Size` and symbols at the same time is not allowed.
# RUN: not yaml2obj --docnum=2 %s -o %t2 2>&1 | FileCheck %s --check-prefix=CASE2
# CASE2: error: Cannot specify both `Size` and `DynamicSymbols` for symbol table section '.dynsym'.
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .dynsym
Type: SHT_DYNSYM
Size: 0x100
DynamicSymbols:
- Name: foo
Binding: STB_GLOBAL
## Specifying both `Content` and symbols at the same time is not allowed.
# RUN: not yaml2obj --docnum=3 %s -o %t3 2>&1 | FileCheck %s --check-prefix=CASE3
# CASE3: error: Cannot specify both `Content` and `DynamicSymbols` for symbol table section '.dynsym'.
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .dynsym
Type: SHT_DYNSYM
Content: "00"
DynamicSymbols:
- Name: foo
Binding: STB_GLOBAL
## Check we can use just `Content` to emit custom data in the symbol table section.
# RUN: yaml2obj --docnum=4 %s -o %t4
# RUN: llvm-readobj --section-data -S %t4 | FileCheck %s --check-prefix=CASE4
# CASE4: Name: .dynsym
# CASE4-NEXT: Type: SHT_DYNSYM
# CASE4-NEXT: Flags [
# CASE4-NEXT: SHF_ALLOC
# CASE4-NEXT: ]
# CASE4-NEXT: Address: 0x0
# CASE4-NEXT: Offset: 0x1C0
# CASE4-NEXT: Size: 2
# CASE4-NEXT: Link: 2
# CASE4-NEXT: Info: 0
# CASE4-NEXT: AddressAlignment: 0
# CASE4-NEXT: EntrySize: 24
# CASE4-NEXT: SectionData (
# CASE4-NEXT: 0000: 0123
# CASE4-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .dynsym
Type: SHT_DYNSYM
Content: "0123"
## TODO: .dynstr here is needed to set a proper Link for .dynsym.
## Without this, llvm-readobj reports an error.
## See https://bugs.llvm.org/show_bug.cgi?id=42215.
- Name: .dynstr
Type: SHT_STRTAB
Content: "00"
## Check we can use just `Size` to emit custom data filled with zeroes
## in the symbol table section.
# RUN: yaml2obj --docnum=5 %s -o %t5
# RUN: llvm-readobj --section-data -S %t5 | FileCheck %s --check-prefix=CASE5
# CASE5: Name: .dynsym
# CASE5-NEXT: Type: SHT_DYNSYM
# CASE5-NEXT: Flags [
# CASE5-NEXT: SHF_ALLOC
# CASE5-NEXT: ]
# CASE5-NEXT: Address: 0x0
# CASE5-NEXT: Offset: 0x1C0
# CASE5-NEXT: Size: 5
# CASE5-NEXT: Link: 2
# CASE5-NEXT: Info: 0
# CASE5-NEXT: AddressAlignment: 0
# CASE5-NEXT: EntrySize: 24
# CASE5-NEXT: SectionData (
# CASE5-NEXT: 0000: 00000000 00
# CASE5-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .dynsym
Type: SHT_DYNSYM
Size: 5
## TODO: .dynstr here is needed to set a proper Link for .dynsym.
## Without this, llvm-readobj reports an error.
## See https://bugs.llvm.org/show_bug.cgi?id=42215.
- Name: .dynstr
Type: SHT_STRTAB
Content: "00"
## Check we can specify both `Size` and `Content` when size is greater
## than content size. In this case zeroes are added as padding
## after after the specified content.
# RUN: yaml2obj --docnum=6 %s -o %t6
# RUN: llvm-readobj %t6 --section-data -S | FileCheck %s --check-prefix=CASE6
# CASE6: Name: .dynsym
# CASE6-NEXT: Type: SHT_DYNSYM
# CASE6-NEXT: Flags [
# CASE6-NEXT: SHF_ALLOC
# CASE6-NEXT: ]
# CASE6-NEXT: Address: 0x0
# CASE6-NEXT: Offset: 0x1C0
# CASE6-NEXT: Size: 4
# CASE6-NEXT: Link: 2
# CASE6-NEXT: Info: 0
# CASE6-NEXT: AddressAlignment: 0
# CASE6-NEXT: EntrySize: 24
# CASE6-NEXT: SectionData (
# CASE6-NEXT: 0000: 01230000
# CASE6-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .dynsym
Type: SHT_DYNSYM
Content: "0123"
Size: 4
## TODO: .dynstr here is needed to set a proper Link for .dynsym.
## Without this, llvm-readobj reports an error.
## See https://bugs.llvm.org/show_bug.cgi?id=42215.
- Name: .dynstr
Type: SHT_STRTAB
Content: "00"
## Check we can specify both `Size` and `Content` when size is
## equal to content size.
# RUN: yaml2obj --docnum=7 %s -o %t7
# RUN: llvm-readobj --section-data -S %t7 | FileCheck %s --check-prefix=CASE7
# CASE7: Name: .dynsym
# CASE7-NEXT: Type: SHT_DYNSYM
# CASE7-NEXT: Flags [
# CASE7-NEXT: SHF_ALLOC
# CASE7-NEXT: ]
# CASE7-NEXT: Address: 0x0
# CASE7-NEXT: Offset: 0x1C0
# CASE7-NEXT: Size: 2
# CASE7-NEXT: Link: 2
# CASE7-NEXT: Info: 0
# CASE7-NEXT: AddressAlignment: 0
# CASE7-NEXT: EntrySize: 24
# CASE7-NEXT: SectionData (
# CASE7-NEXT: 0000: 0123
# CASE7-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .dynsym
Type: SHT_DYNSYM
Content: "0123"
Size: 2
## TODO: .dynstr here is needed to set a proper Link for .dynsym.
## Without this, llvm-readobj reports an error.
## See https://bugs.llvm.org/show_bug.cgi?id=42215.
- Name: .dynstr
Type: SHT_STRTAB
Content: "00"

View File

@ -0,0 +1,158 @@
## For regular sections, it is common to specify `Size` and/or `Content` fields in YAML.
## Here we test the behavior in different cases.
## In this case, we have both `Content` and `Size` fields specified and `Size`
## is less than content size. Check we report an error.
# RUN: not yaml2obj --docnum=1 %s -o %t1 2>&1 | FileCheck %s --check-prefix=ERR
# ERR: error: Section size must be greater than or equal to the content size
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .foo
Type: SHT_PROGBITS
Content: "FF"
Size: 0
## In this case, we have both `Content` and `Size` fields specified and
## `Size` is equal to the content size. We check that this is allowed and
## that the output section has a correct size value.
# RUN: yaml2obj --docnum=2 %s -o %t2
# RUN: llvm-readobj --section-data -S %t2 | FileCheck %s --check-prefix=CASE2
# CASE2: Name: .foo
# CASE2-NEXT: Type: SHT_PROGBITS
# CASE2-NEXT: Flags [
# CASE2-NEXT: ]
# CASE2-NEXT: Address: 0x0
# CASE2-NEXT: Offset: 0x180
# CASE2-NEXT: Size: 1
# CASE2-NEXT: Link: 0
# CASE2-NEXT: Info: 0
# CASE2-NEXT: AddressAlignment: 0
# CASE2-NEXT: EntrySize: 0
# CASE2-NEXT: SectionData (
# CASE2-NEXT: 0000: FF
# CASE2-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .foo
Type: SHT_PROGBITS
Content: "FF"
Size: 1
## Check we can specify only `Content`.
# RUN: yaml2obj --docnum=3 %s -o %t3
# RUN: llvm-readobj --section-data -S %t3 | FileCheck %s --check-prefix=CASE2
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .foo
Type: SHT_PROGBITS
Content: "FF"
## Check we can specify only `Size`.
# RUN: yaml2obj --docnum=4 %s -o %t4
# RUN: llvm-readobj --section-data -S %t4 | FileCheck %s --check-prefix=CASE3
# CASE3: Name: .foo
# CASE3-NEXT: Type: SHT_PROGBITS
# CASE3-NEXT: Flags [
# CASE3-NEXT: ]
# CASE3-NEXT: Address: 0x0
# CASE3-NEXT: Offset: 0x180
# CASE3-NEXT: Size: 1
# CASE3-NEXT: Link: 0
# CASE3-NEXT: Info: 0
# CASE3-NEXT: AddressAlignment: 0
# CASE3-NEXT: EntrySize: 0
# CASE3-NEXT: SectionData (
# CASE3-NEXT: 0000: 00
# CASE3-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .foo
Type: SHT_PROGBITS
Size: 1
## Check we can specify both `Size` and `Content` when size is greater
## than content size. In this case zeroes are added as padding
## after the specified content.
# RUN: yaml2obj --docnum=5 %s -o %t5
# RUN: llvm-readobj --section-data -S %t5 | FileCheck %s --check-prefix=CASE4
# CASE4: Name: .foo
# CASE4-NEXT: Type: SHT_PROGBITS
# CASE4-NEXT: Flags [
# CASE4-NEXT: ]
# CASE4-NEXT: Address: 0x0
# CASE4-NEXT: Offset: 0x180
# CASE4-NEXT: Size: 3
# CASE4-NEXT: Link: 0
# CASE4-NEXT: Info: 0
# CASE4-NEXT: AddressAlignment: 0
# CASE4-NEXT: EntrySize: 0
# CASE4-NEXT: SectionData (
# CASE4-NEXT: 0000: FF0000
# CASE4-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .foo
Type: SHT_PROGBITS
Content: "FF"
Size: 3
## Check we emit an empty section if neither 'Content' nor 'Size' were set.
# RUN: yaml2obj --docnum=6 %s -o %t6
# RUN: llvm-readobj %t6 --sections | FileCheck %s --check-prefix=CASE5
# CASE5: Name: .foo
# CASE5-NEXT: Type: SHT_PROGBITS
# CASE5-NEXT: Flags [
# CASE5-NEXT: ]
# CASE5-NEXT: Address: 0x0
# CASE5-NEXT: Offset: 0x180
# CASE5-NEXT: Size: 0
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .foo
Type: SHT_PROGBITS

View File

@ -0,0 +1,167 @@
## For implicit string table sections, `Size` and/or `Content` fields can also
## be specified in YAML. Here we test the behavior in different cases.
## When no `Size` or `Content` is specified for a string table section,
## yaml2obj writes the default content.
# RUN: yaml2obj --docnum=1 %s -o %t1
# RUN: llvm-objdump %t1 -s | FileCheck %s --check-prefix=CASE1
# CASE1: Contents of section .strtab:
# CASE1-NEXT: 0000 00666f6f 00 .foo.
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .strtab
Type: SHT_STRTAB
## Used to trigger adding string `foo` to the string table section.
Symbols:
- Name: foo
## For string table sections, `Size` can be used to override the
## implicit string table data. The content is filled with zeroes in this case.
# RUN: yaml2obj --docnum=2 %s -o %t2
# RUN: llvm-readobj --section-data -S %t2 | FileCheck %s --check-prefix=CASE2
# CASE2: Name: .strtab
# CASE2-NEXT: Type: SHT_STRTAB
# CASE2-NEXT: Flags [
# CASE2-NEXT: ]
# CASE2-NEXT: Address: 0x0
# CASE2-NEXT: Offset: 0x140
# CASE2-NEXT: Size: 2
# CASE2-NEXT: Link: 0
# CASE2-NEXT: Info: 0
# CASE2-NEXT: AddressAlignment: 0
# CASE2-NEXT: EntrySize: 0
# CASE2-NEXT: SectionData (
# CASE2-NEXT: 0000: 0000
# CASE2-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .strtab
Type: SHT_STRTAB
Size: 2
## Used to trigger adding string `foo` to the string table section.
Symbols:
- Name: foo
## For string table sections, `Content` can be used to override the
## implicit string table data.
# RUN: yaml2obj --docnum=3 %s -o %t3
# RUN: llvm-readobj --section-data -S %t3 | FileCheck %s --check-prefix=CASE3
# CASE3: Name: .strtab
# CASE3-NEXT: Type: SHT_STRTAB
# CASE3-NEXT: Flags [
# CASE3-NEXT: ]
# CASE3-NEXT: Address: 0x0
# CASE3-NEXT: Offset: 0x140
# CASE3-NEXT: Size: 2
# CASE3-NEXT: Link: 0
# CASE3-NEXT: Info: 0
# CASE3-NEXT: AddressAlignment: 0
# CASE3-NEXT: EntrySize: 0
# CASE3-NEXT: SectionData (
# CASE3-NEXT: 0000: 0102
# CASE3-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .strtab
Type: SHT_STRTAB
Content: "0102"
## Used to trigger adding string `foo` to the string table section.
Symbols:
- Name: foo
## For string table sections, check we can specify both `Size` and `Content`
## when size is greater than content size. In this case zeroes are
## added as padding after the specified content.
# RUN: yaml2obj --docnum=4 %s -o %t4
# RUN: llvm-readobj --section-data -S %t4 | FileCheck %s --check-prefix=CASE4
# CASE4: Name: .strtab
# CASE4-NEXT: Type: SHT_STRTAB
# CASE4-NEXT: Flags [
# CASE4-NEXT: ]
# CASE4-NEXT: Address: 0x0
# CASE4-NEXT: Offset: 0x140
# CASE4-NEXT: Size: 3
# CASE4-NEXT: Link: 0
# CASE4-NEXT: Info: 0
# CASE4-NEXT: AddressAlignment: 0
# CASE4-NEXT: EntrySize: 0
# CASE4-NEXT: SectionData (
# CASE4-NEXT: 0000: 010200
# CASE4-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .strtab
Type: SHT_STRTAB
Content: "0102"
Size: 3
## Used to trigger adding string `foo` to the string table section.
Symbols:
- Name: foo
## For string table sections, check we can specify both `Size` and `Content`
## when size is equal to content size.
# RUN: yaml2obj --docnum=5 %s -o %t5
# RUN: llvm-readobj --section-data -S %t5 | FileCheck %s --check-prefix=CASE5
# CASE5: Name: .strtab
# CASE5-NEXT: Type: SHT_STRTAB
# CASE5-NEXT: Flags [ (0x0)
# CASE5-NEXT: ]
# CASE5-NEXT: Address: 0x0
# CASE5-NEXT: Offset: 0x140
# CASE5-NEXT: Size: 2
# CASE5-NEXT: Link: 0
# CASE5-NEXT: Info: 0
# CASE5-NEXT: AddressAlignment: 0
# CASE5-NEXT: EntrySize: 0
# CASE5-NEXT: SectionData (
# CASE5-NEXT: 0000: 0102
# CASE5-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .strtab
Type: SHT_STRTAB
Content: "0102"
Size: 2
## Used to trigger adding string `foo` to the string table section.
Symbols:
- Name: foo

View File

@ -0,0 +1,190 @@
## For implicit symbol table sections, `Size` and/or `Content` fields can also
## be specified in YAML. Here we test the behavior in different cases.
## When no `Size` or `Content` is specified for a symbol table section,
## yaml2obj writes the default content.
# RUN: yaml2obj --docnum=1 %s -o %t1
# RUN: llvm-readelf %t1 -s | FileCheck %s --check-prefix=CASE1
# CASE1: Symbol table '.symtab' contains 2 entries:
# CASE1-NEXT: Num: Value Size Type Bind Vis Ndx Name
# CASE1-NEXT: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
# CASE1-NEXT: 1: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND foo
# CASE1-EMPTY:
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .symtab
Type: SHT_SYMTAB
Symbols:
- Name: foo
## Specifying both `Size` and symbols at the same time is not allowed.
# RUN: not yaml2obj --docnum=2 %s -o %t2 2>&1 | FileCheck %s --check-prefix=CASE2
# CASE2: error: Cannot specify both `Size` and `Symbols` for symbol table section '.symtab'.
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .symtab
Type: SHT_SYMTAB
Size: 0x100
Symbols:
- Name: foo
## Specifying both `Content` and symbols at the same time is not allowed.
# RUN: not yaml2obj --docnum=3 %s -o %t3 2>&1 | FileCheck %s --check-prefix=CASE3
# CASE3: error: Cannot specify both `Content` and `Symbols` for symbol table section '.symtab'.
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .symtab
Type: SHT_SYMTAB
Content: "00"
Symbols:
- Name: foo
## Check we can use just `Content` to emit custom data in the symbol table section.
# RUN: yaml2obj --docnum=4 %s -o %t4
# RUN: llvm-readobj --section-data -S %t4 | FileCheck %s --check-prefix=CASE4
# CASE4: Name: .symtab
# CASE4-NEXT: Type: SHT_SYMTAB
# CASE4-NEXT: Flags [ (0x0)
# CASE4-NEXT: ]
# CASE4-NEXT: Address: 0x0
# CASE4-NEXT: Offset: 0x140
# CASE4-NEXT: Size: 2
# CASE4-NEXT: Link: 2
# CASE4-NEXT: Info: 0
# CASE4-NEXT: AddressAlignment: 0
# CASE4-NEXT: EntrySize: 24
# CASE4-NEXT: SectionData (
# CASE4-NEXT: 0000: 0123
# CASE4-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .symtab
Type: SHT_SYMTAB
Content: "0123"
## Check we can use just `Size` to emit custom data filled with zeroes
## in the symbol table section.
# RUN: yaml2obj --docnum=5 %s -o %t5
# RUN: llvm-readobj --section-data -S %t5 | FileCheck %s --check-prefix=CASE5
# CASE5: Name: .symtab (19)
# CASE5-NEXT: Type: SHT_SYMTAB (0x2)
# CASE5-NEXT: Flags [ (0x0)
# CASE5-NEXT: ]
# CASE5-NEXT: Address: 0x0
# CASE5-NEXT: Offset: 0x140
# CASE5-NEXT: Size: 5
# CASE5-NEXT: Link: 2
# CASE5-NEXT: Info: 0
# CASE5-NEXT: AddressAlignment: 0
# CASE5-NEXT: EntrySize: 24
# CASE5-NEXT: SectionData (
# CASE5-NEXT: 0000: 00000000 00
# CASE5-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .symtab
Type: SHT_SYMTAB
Size: 5
## Check we can specify both `Size` and `Content` when size is greater
## than content size. In this case zeroes are added as padding
## after the specified content.
# RUN: yaml2obj --docnum=6 %s -o %t6
# RUN: llvm-readobj %t6 --section-data -S | FileCheck %s --check-prefix=CASE6
# CASE6: Name: .symtab
# CASE6-NEXT: Type: SHT_SYMTAB
# CASE6-NEXT: Flags [
# CASE6-NEXT: ]
# CASE6-NEXT: Address: 0x0
# CASE6-NEXT: Offset: 0x140
# CASE6-NEXT: Size: 4
# CASE6-NEXT: Link: 2
# CASE6-NEXT: Info: 0
# CASE6-NEXT: AddressAlignment: 0
# CASE6-NEXT: EntrySize: 24
# CASE6-NEXT: SectionData (
# CASE6-NEXT: 0000: 01230000
# CASE6-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .symtab
Type: SHT_SYMTAB
Content: "0123"
Size: 4
## Check we can specify both `Size` and `Content` when size is
## equal to content size.
# RUN: yaml2obj --docnum=7 %s -o %t7
# RUN: llvm-readobj --section-data -S %t7 | FileCheck %s --check-prefix=CASE7
# CASE7: Name: .symtab
# CASE7-NEXT: Type: SHT_SYMTAB
# CASE7-NEXT: Flags [
# CASE7-NEXT: ]
# CASE7-NEXT: Address: 0x0
# CASE7-NEXT: Offset: 0x140
# CASE7-NEXT: Size: 2
# CASE7-NEXT: Link: 2
# CASE7-NEXT: Info: 0
# CASE7-NEXT: AddressAlignment: 0
# CASE7-NEXT: EntrySize: 24
# CASE7-NEXT: SectionData (
# CASE7-NEXT: 0000: 0123
# CASE7-NEXT: )
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .symtab
Type: SHT_SYMTAB
Content: "0123"
Size: 2

View File

@ -438,10 +438,10 @@ ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
auto ContentOrErr = Obj.getSectionContents(Shdr); auto ContentOrErr = Obj.getSectionContents(Shdr);
if (!ContentOrErr) if (!ContentOrErr)
return errorToErrorCode(ContentOrErr.takeError()); return errorToErrorCode(ContentOrErr.takeError());
S->Content = yaml::BinaryRef(ContentOrErr.get()); ArrayRef<uint8_t> Content = *ContentOrErr;
S->Size = S->Content.binary_size(); if (!Content.empty())
S->Content = yaml::BinaryRef(Content);
S->Info = Shdr->sh_info; S->Info = Shdr->sh_info;
return S.release(); return S.release();
} }

View File

@ -358,13 +358,49 @@ static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
return Symbols.size(); return Symbols.size();
} }
static uint64_t writeRawSectionData(raw_ostream &OS,
const ELFYAML::RawContentSection &RawSec) {
size_t ContentSize = 0;
if (RawSec.Content) {
RawSec.Content->writeAsBinary(OS);
ContentSize = RawSec.Content->binary_size();
}
if (!RawSec.Size)
return ContentSize;
OS.write_zeros(*RawSec.Size - ContentSize);
return *RawSec.Size;
}
template <class ELFT> template <class ELFT>
void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader, void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
SymtabType STType, SymtabType STType,
ContiguousBlobAccumulator &CBA, ContiguousBlobAccumulator &CBA,
ELFYAML::Section *YAMLSec) { ELFYAML::Section *YAMLSec) {
zero(SHeader);
bool IsStatic = STType == SymtabType::Static; bool IsStatic = STType == SymtabType::Static;
const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
ELFYAML::RawContentSection *RawSec =
dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
if (RawSec && !Symbols.empty() && (RawSec->Content || RawSec->Size)) {
if (RawSec->Content)
WithColor::error() << "Cannot specify both `Content` and " +
(IsStatic ? Twine("`Symbols`")
: Twine("`DynamicSymbols`")) +
" for symbol table section '"
<< RawSec->Name << "'.\n";
if (RawSec->Size)
WithColor::error() << "Cannot specify both `Size` and " +
(IsStatic ? Twine("`Symbols`")
: Twine("`DynamicSymbols`")) +
" for symbol table section '"
<< RawSec->Name << "'.\n";
exit(1);
}
zero(SHeader);
SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym"); SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym");
SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM; SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
@ -382,13 +418,8 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
if (!IsStatic) if (!IsStatic)
SHeader.sh_flags |= ELF::SHF_ALLOC; SHeader.sh_flags |= ELF::SHF_ALLOC;
// One greater than symbol table index of the last local symbol.
const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
// If the symbol table section is explicitly described in the YAML // If the symbol table section is explicitly described in the YAML
// then we should set the fields requested. // then we should set the fields requested.
ELFYAML::RawContentSection *RawSec =
dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
SHeader.sh_info = SHeader.sh_info =
RawSec ? (unsigned)RawSec->Info : findFirstNonGlobal(Symbols) + 1; RawSec ? (unsigned)RawSec->Info : findFirstNonGlobal(Symbols) + 1;
SHeader.sh_entsize = (YAMLSec && YAMLSec->EntSize) SHeader.sh_entsize = (YAMLSec && YAMLSec->EntSize)
@ -397,26 +428,24 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8; SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8;
SHeader.sh_addr = YAMLSec ? (uint64_t)YAMLSec->Address : 0; SHeader.sh_addr = YAMLSec ? (uint64_t)YAMLSec->Address : 0;
if (RawSec && RawSec->Content.binary_size()) { auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
RawSec->Content.writeAsBinary( if (RawSec && (RawSec->Content || RawSec->Size)) {
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign)); assert(Symbols.empty());
SHeader.sh_size = RawSec->Size; SHeader.sh_size = writeRawSectionData(OS, *RawSec);
} else { return;
std::vector<Elf_Sym> Syms;
{
// Ensure STN_UNDEF is present
Elf_Sym Sym;
zero(Sym);
Syms.push_back(Sym);
}
addSymbols(Symbols, Syms, IsStatic ? DotStrtab : DotDynstr);
writeArrayData(
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign),
makeArrayRef(Syms));
SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
} }
std::vector<Elf_Sym> Syms;
{
// Ensure STN_UNDEF is present
Elf_Sym Sym;
zero(Sym);
Syms.push_back(Sym);
}
addSymbols(Symbols, Syms, IsStatic ? DotStrtab : DotDynstr);
writeArrayData(OS, makeArrayRef(Syms));
SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
} }
template <class ELFT> template <class ELFT>
@ -431,13 +460,12 @@ void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
ELFYAML::RawContentSection *RawSec = ELFYAML::RawContentSection *RawSec =
dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec); dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
if (RawSec && RawSec->Content.binary_size()) {
RawSec->Content.writeAsBinary( auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign)); if (RawSec && (RawSec->Content || RawSec->Size)) {
SHeader.sh_size = RawSec->Size; SHeader.sh_size = writeRawSectionData(OS, *RawSec);
} else { } else {
STB.write( STB.write(OS);
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign));
SHeader.sh_size = STB.getSize(); SHeader.sh_size = STB.getSize();
} }
@ -569,12 +597,9 @@ template <class ELFT>
bool ELFState<ELFT>::writeSectionContent( bool ELFState<ELFT>::writeSectionContent(
Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
ContiguousBlobAccumulator &CBA) { ContiguousBlobAccumulator &CBA) {
assert(Section.Size >= Section.Content.binary_size() &&
"Section size and section content are inconsistent");
raw_ostream &OS = raw_ostream &OS =
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Section.Content.writeAsBinary(OS); SHeader.sh_size = writeRawSectionData(OS, Section);
OS.write_zeros(Section.Size - Section.Content.binary_size());
if (Section.EntSize) if (Section.EntSize)
SHeader.sh_entsize = *Section.EntSize; SHeader.sh_entsize = *Section.EntSize;
@ -582,7 +607,6 @@ bool ELFState<ELFT>::writeSectionContent(
SHeader.sh_entsize = sizeof(Elf_Relr); SHeader.sh_entsize = sizeof(Elf_Relr);
else else
SHeader.sh_entsize = 0; SHeader.sh_entsize = 0;
SHeader.sh_size = Section.Size;
SHeader.sh_info = Section.Info; SHeader.sh_info = Section.Info;
return true; return true;
} }