Index: include/llvm/Object/ELFYAML.h =================================================================== --- include/llvm/Object/ELFYAML.h +++ include/llvm/Object/ELFYAML.h @@ -83,6 +83,7 @@ }; struct RawContentSection : Section { object::yaml::BinaryRef Content; + llvm::yaml::Hex64 Size; RawContentSection() : Section(SectionKind::RawContent) {} static bool classof(const Section *S) { return S->Kind == SectionKind::RawContent; @@ -193,6 +194,7 @@ template <> struct MappingTraits> { static void mapping(IO &IO, std::unique_ptr &Section); + static StringRef validate(IO &io, std::unique_ptr &Section); }; template <> Index: lib/Object/ELFYAML.cpp =================================================================== --- lib/Object/ELFYAML.cpp +++ lib/Object/ELFYAML.cpp @@ -658,6 +658,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())); } static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) { @@ -687,6 +688,14 @@ } } +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"; +} + void MappingTraits::mapping(IO &IO, ELFYAML::Relocation &Rel) { IO.mapRequired("Offset", Rel.Offset); Index: test/Object/yaml2obj-elf-section-basic.yaml =================================================================== --- test/Object/yaml2obj-elf-section-basic.yaml +++ test/Object/yaml2obj-elf-section-basic.yaml @@ -17,6 +17,14 @@ Content: EBFE AddressAlign: 2 + - Name: .data + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + Address: 0xCAFECAFE + Content: FEBF + Size: 8 + AddressAlign: 2 + # CHECK: Section { # CHECK: Index: 0 # CHECK: Type: SHT_NULL (0x0) @@ -38,6 +46,23 @@ # CHECK-NEXT: ) # # CHECK: Section { +# CHECK: Name: .data +# CHECK-NEXT: Type: SHT_PROGBITS (0x1) +# CHECK-NEXT: Flags [ (0x2) +# CHECK-NEXT: SHF_ALLOC (0x2) +# CHECK-NEXT: ] +# CHECK-NEXT: Address: 0xCAFECAFE +# CHECK-NEXT: Offset: 0x1D0 +# CHECK-NEXT: Size: 8 +# CHECK-NEXT: Link: 0 +# CHECK-NEXT: Info: 0 +# CHECK-NEXT: AddressAlignment: 2 +# CHECK-NEXT: EntrySize: 0 +# CHECK-NEXT: SectionData ( +# CHECK-NEXT: 0000: FEBF0000 00000000 |........| +# CHECK-NEXT: ) +# +# CHECK: Section { # CHECK: Name: .symtab (25) # CHECK: Type: SHT_SYMTAB (0x2) # CHECK: } Index: test/Object/yaml2obj-elf-section-invalid-size.yaml =================================================================== --- /dev/null +++ test/Object/yaml2obj-elf-section-invalid-size.yaml @@ -0,0 +1,26 @@ +# RUN: not yaml2obj -format=elf -o %t %s 2>&1 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 + +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Content: EBFE + AddressAlign: 2 + + - Name: .data + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + Content: 0000000000000000 + Size: 2 + +# CHECK: YAML:17:5: error: Section size must be greater or equal to the content size +# CHECK-NEXT: - Name: .data +# CHECK-NEXT: ^ +# CHECK-NEXT: yaml2obj: Failed to parse YAML file! Index: tools/obj2yaml/elf2yaml.cpp =================================================================== --- tools/obj2yaml/elf2yaml.cpp +++ tools/obj2yaml/elf2yaml.cpp @@ -254,6 +254,7 @@ if (error_code EC = ContentOrErr.getError()) return EC; S->Content = object::yaml::BinaryRef(ContentOrErr.get()); + S->Size = S->Content.binary_size(); return S.release(); } Index: tools/yaml2obj/yaml2elf.cpp =================================================================== --- tools/yaml2obj/yaml2elf.cpp +++ tools/yaml2obj/yaml2elf.cpp @@ -314,9 +314,14 @@ ELFState::writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, ContiguousBlobAccumulator &CBA) { - Section.Content.writeAsBinary(CBA.getOSAndAlignedOffset(SHeader.sh_offset)); + assert(Section.Size >= Section.Content.binary_size() && + "Section size and section content are inconsistent"); + raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset); + Section.Content.writeAsBinary(OS); + for (auto i = Section.Content.binary_size(); i < Section.Size; ++i) + OS.write(0); SHeader.sh_entsize = 0; - SHeader.sh_size = Section.Content.binary_size(); + SHeader.sh_size = Section.Size; } template