Index: llvm/trunk/include/llvm/ObjectYAML/ELFYAML.h =================================================================== --- llvm/trunk/include/llvm/ObjectYAML/ELFYAML.h +++ llvm/trunk/include/llvm/ObjectYAML/ELFYAML.h @@ -151,8 +151,8 @@ }; struct RawContentSection : Section { - yaml::BinaryRef Content; - llvm::yaml::Hex64 Size; + Optional Content; + Optional Size; llvm::yaml::Hex64 Info; RawContentSection() : Section(SectionKind::RawContent) {} Index: llvm/trunk/lib/ObjectYAML/ELFYAML.cpp =================================================================== --- llvm/trunk/lib/ObjectYAML/ELFYAML.cpp +++ llvm/trunk/lib/ObjectYAML/ELFYAML.cpp @@ -914,7 +914,7 @@ static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) { commonSectionMapping(IO, Section); 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)); } @@ -1042,9 +1042,12 @@ StringRef MappingTraits>::validate( IO &io, std::unique_ptr &Section) { const auto *RawSection = dyn_cast(Section.get()); - if (!RawSection || RawSection->Size >= RawSection->Content.binary_size()) - return StringRef(); - return "Section size must be greater or equal to the content size"; + if (!RawSection) + return {}; + 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 { Index: llvm/trunk/test/Object/obj2yaml.test =================================================================== --- llvm/trunk/test/Object/obj2yaml.test +++ llvm/trunk/test/Object/obj2yaml.test @@ -389,7 +389,6 @@ ELF-MIPSEL-NEXT: Type: SHT_PROGBITS ELF-MIPSEL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ] ELF-MIPSEL-NEXT: AddressAlign: 0x0000000000000004 -ELF-MIPSEL-NEXT: Content: '' ELF-MIPSEL-NEXT: - Name: .bss ELF-MIPSEL-NEXT: Type: SHT_NOBITS ELF-MIPSEL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ] @@ -398,7 +397,6 @@ ELF-MIPSEL-NEXT: - Name: .mdebug.abi32 ELF-MIPSEL-NEXT: Type: SHT_PROGBITS ELF-MIPSEL-NEXT: AddressAlign: 0x0000000000000001 -ELF-MIPSEL-NEXT: Content: '' ELF-MIPSEL-NEXT: - Name: .rodata.str1.1 ELF-MIPSEL-NEXT: Type: SHT_PROGBITS ELF-MIPSEL-NEXT: Flags: [ SHF_ALLOC, SHF_MERGE, SHF_STRINGS ] @@ -477,7 +475,6 @@ ELF-MIPS64EL-NEXT: Type: SHT_PROGBITS ELF-MIPS64EL-NEXT: Flags: [ SHF_ALLOC, SHF_EXECINSTR ] ELF-MIPS64EL-NEXT: AddressAlign: 0x0000000000000010 -ELF-MIPS64EL-NEXT: Content: '' ELF-MIPS64EL-NEXT: - Name: .data ELF-MIPS64EL-NEXT: Type: SHT_PROGBITS ELF-MIPS64EL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ] @@ -506,7 +503,6 @@ ELF-MIPS64EL-NEXT: - Name: .pdr ELF-MIPS64EL-NEXT: Type: SHT_PROGBITS ELF-MIPS64EL-NEXT: AddressAlign: 0x0000000000000004 -ELF-MIPS64EL-NEXT: Content: '' ELF-MIPS64EL-NEXT: Symbols: ELF-MIPS64EL-NEXT: - Name: .text ELF-MIPS64EL-NEXT: Type: STT_SECTION @@ -550,7 +546,6 @@ ELF-X86-64-NEXT: Type: SHT_PROGBITS ELF-X86-64-NEXT: Address: 0x0000000000000033 ELF-X86-64-NEXT: AddressAlign: 0x0000000000000001 -ELF-X86-64-NEXT: Content: '' ELF-X86-64-NEXT: - Name: .rela.text ELF-X86-64-NEXT: Type: SHT_RELA ELF-X86-64-NEXT: Address: 0x0000000000000038 @@ -610,7 +605,6 @@ ELF-AVR-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ] ELF-AVR-NEXT: Address: 0x0000000000800060 ELF-AVR-NEXT: AddressAlign: 0x0000000000000001 -ELF-AVR-NEXT: Content: '' ELF-AVR-NEXT: Symbols: ELF-AVR-NEXT: - Name: .text ELF-AVR-NEXT: Type: STT_SECTION @@ -706,7 +700,6 @@ ELF-X86-64-UNWIND-NEXT: Type: SHT_X86_64_UNWIND ELF-X86-64-UNWIND-NEXT: Flags: [ SHF_ALLOC ] 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 ENOENT: Error reading file: {{.*}}.blah: {{[Nn]}}o such file or directory Index: llvm/trunk/test/Object/yaml2obj-elf-section-invalid-size.yaml =================================================================== --- llvm/trunk/test/Object/yaml2obj-elf-section-invalid-size.yaml +++ llvm/trunk/test/Object/yaml2obj-elf-section-invalid-size.yaml @@ -20,7 +20,7 @@ Content: 0000000000000000 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: ^ # CHECK-NEXT: yaml2obj: Failed to parse YAML file! Index: llvm/trunk/test/tools/obj2yaml/elf-shinfo.yaml =================================================================== --- llvm/trunk/test/tools/obj2yaml/elf-shinfo.yaml +++ llvm/trunk/test/tools/obj2yaml/elf-shinfo.yaml @@ -5,7 +5,6 @@ # CHECK: - Name: .test # CHECK-NEXT: Type: SHT_PROGBITS -# CHECK-NEXT: Content: '' # CHECK-NEXT: Info: 0x000000000000002A --- !ELF Index: llvm/trunk/test/tools/yaml2obj/dynsymtab-implicit-sections-size-content.yaml =================================================================== --- llvm/trunk/test/tools/yaml2obj/dynsymtab-implicit-sections-size-content.yaml +++ llvm/trunk/test/tools/yaml2obj/dynsymtab-implicit-sections-size-content.yaml @@ -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" Index: llvm/trunk/test/tools/yaml2obj/section-size-content.yaml =================================================================== --- llvm/trunk/test/tools/yaml2obj/section-size-content.yaml +++ llvm/trunk/test/tools/yaml2obj/section-size-content.yaml @@ -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 Index: llvm/trunk/test/tools/yaml2obj/strtab-implicit-sections-size-content.yaml =================================================================== --- llvm/trunk/test/tools/yaml2obj/strtab-implicit-sections-size-content.yaml +++ llvm/trunk/test/tools/yaml2obj/strtab-implicit-sections-size-content.yaml @@ -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 Index: llvm/trunk/test/tools/yaml2obj/symtab-implicit-sections-size-content.yaml =================================================================== --- llvm/trunk/test/tools/yaml2obj/symtab-implicit-sections-size-content.yaml +++ llvm/trunk/test/tools/yaml2obj/symtab-implicit-sections-size-content.yaml @@ -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 Index: llvm/trunk/tools/obj2yaml/elf2yaml.cpp =================================================================== --- llvm/trunk/tools/obj2yaml/elf2yaml.cpp +++ llvm/trunk/tools/obj2yaml/elf2yaml.cpp @@ -438,10 +438,10 @@ auto ContentOrErr = Obj.getSectionContents(Shdr); if (!ContentOrErr) return errorToErrorCode(ContentOrErr.takeError()); - S->Content = yaml::BinaryRef(ContentOrErr.get()); - S->Size = S->Content.binary_size(); + ArrayRef Content = *ContentOrErr; + if (!Content.empty()) + S->Content = yaml::BinaryRef(Content); S->Info = Shdr->sh_info; - return S.release(); } Index: llvm/trunk/tools/yaml2obj/yaml2elf.cpp =================================================================== --- llvm/trunk/tools/yaml2obj/yaml2elf.cpp +++ llvm/trunk/tools/yaml2obj/yaml2elf.cpp @@ -358,13 +358,49 @@ 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 void ELFState::initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType, ContiguousBlobAccumulator &CBA, ELFYAML::Section *YAMLSec) { - zero(SHeader); + bool IsStatic = STType == SymtabType::Static; + const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols; + + ELFYAML::RawContentSection *RawSec = + dyn_cast_or_null(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_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM; @@ -382,13 +418,8 @@ if (!IsStatic) 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 // then we should set the fields requested. - ELFYAML::RawContentSection *RawSec = - dyn_cast_or_null(YAMLSec); SHeader.sh_info = RawSec ? (unsigned)RawSec->Info : findFirstNonGlobal(Symbols) + 1; SHeader.sh_entsize = (YAMLSec && YAMLSec->EntSize) @@ -397,26 +428,24 @@ SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8; SHeader.sh_addr = YAMLSec ? (uint64_t)YAMLSec->Address : 0; - if (RawSec && RawSec->Content.binary_size()) { - RawSec->Content.writeAsBinary( - CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign)); - SHeader.sh_size = RawSec->Size; - } else { - std::vector 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)); - } + auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); + if (RawSec && (RawSec->Content || RawSec->Size)) { + assert(Symbols.empty()); + SHeader.sh_size = writeRawSectionData(OS, *RawSec); + return; + } + + std::vector 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 @@ -431,13 +460,12 @@ ELFYAML::RawContentSection *RawSec = dyn_cast_or_null(YAMLSec); - if (RawSec && RawSec->Content.binary_size()) { - RawSec->Content.writeAsBinary( - CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign)); - SHeader.sh_size = RawSec->Size; + + auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); + if (RawSec && (RawSec->Content || RawSec->Size)) { + SHeader.sh_size = writeRawSectionData(OS, *RawSec); } else { - STB.write( - CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign)); + STB.write(OS); SHeader.sh_size = STB.getSize(); } @@ -569,12 +597,9 @@ bool ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, ContiguousBlobAccumulator &CBA) { - assert(Section.Size >= Section.Content.binary_size() && - "Section size and section content are inconsistent"); raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); - Section.Content.writeAsBinary(OS); - OS.write_zeros(Section.Size - Section.Content.binary_size()); + SHeader.sh_size = writeRawSectionData(OS, Section); if (Section.EntSize) SHeader.sh_entsize = *Section.EntSize; @@ -582,7 +607,6 @@ SHeader.sh_entsize = sizeof(Elf_Relr); else SHeader.sh_entsize = 0; - SHeader.sh_size = Section.Size; SHeader.sh_info = Section.Info; return true; }