From f302436a0a454e11502f7e57da5ac6afaefa3077 Mon Sep 17 00:00:00 2001 From: George Rimar Date: Wed, 25 Sep 2019 11:40:11 +0000 Subject: [yaml2obj] - Add a Size field for StackSizesSection. It is a follow-up requested in the review comment for D67757. Allows to use Content + Size or just Size when describing .stack_sizes sections in YAML document Differential revision: https://reviews.llvm.org/D67958 llvm-svn: 372845 --- llvm/lib/ObjectYAML/ELFEmitter.cpp | 28 ++++++++++++++-------------- llvm/lib/ObjectYAML/ELFYAML.cpp | 19 ++++++++++++++++--- 2 files changed, 30 insertions(+), 17 deletions(-) (limited to 'llvm/lib/ObjectYAML') diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp index c394763459d..0139c2d1144 100644 --- a/llvm/lib/ObjectYAML/ELFEmitter.cpp +++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp @@ -440,19 +440,20 @@ static size_t findFirstNonGlobal(ArrayRef Symbols) { return Symbols.size(); } -static uint64_t writeRawSectionData(raw_ostream &OS, - const ELFYAML::RawContentSection &RawSec) { +static uint64_t writeContent(raw_ostream &OS, + const Optional &Content, + const Optional &Size) { size_t ContentSize = 0; - if (RawSec.Content) { - RawSec.Content->writeAsBinary(OS); - ContentSize = RawSec.Content->binary_size(); + if (Content) { + Content->writeAsBinary(OS); + ContentSize = Content->binary_size(); } - if (!RawSec.Size) + if (!Size) return ContentSize; - OS.write_zeros(*RawSec.Size - ContentSize); - return *RawSec.Size; + OS.write_zeros(*Size - ContentSize); + return *Size; } template @@ -554,7 +555,7 @@ void ELFState::initSymtabSectionHeader(Elf_Shdr &SHeader, 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); + SHeader.sh_size = writeContent(OS, RawSec->Content, RawSec->Size); return; } @@ -579,7 +580,7 @@ void ELFState::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); if (RawSec && (RawSec->Content || RawSec->Size)) { - SHeader.sh_size = writeRawSectionData(OS, *RawSec); + SHeader.sh_size = writeContent(OS, RawSec->Content, RawSec->Size); } else { STB.write(OS); SHeader.sh_size = STB.getSize(); @@ -675,7 +676,7 @@ void ELFState::writeSectionContent( ContiguousBlobAccumulator &CBA) { raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); - SHeader.sh_size = writeRawSectionData(OS, Section); + SHeader.sh_size = writeContent(OS, Section.Content, Section.Size); if (Section.EntSize) SHeader.sh_entsize = *Section.EntSize; @@ -795,9 +796,8 @@ void ELFState::writeSectionContent( raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); - if (Section.Content) { - Section.Content->writeAsBinary(OS); - SHeader.sh_size = Section.Content->binary_size(); + if (Section.Content || Section.Size) { + SHeader.sh_size = writeContent(OS, Section.Content, Section.Size); return; } diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp index af795f9732c..d369f14be4b 100644 --- a/llvm/lib/ObjectYAML/ELFYAML.cpp +++ b/llvm/lib/ObjectYAML/ELFYAML.cpp @@ -1020,6 +1020,7 @@ static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) { static void sectionMapping(IO &IO, ELFYAML::StackSizesSection &Section) { commonSectionMapping(IO, Section); IO.mapOptional("Content", Section.Content); + IO.mapOptional("Size", Section.Size); IO.mapOptional("Entries", Section.Entries); } @@ -1176,10 +1177,22 @@ StringRef MappingTraits>::validate( } if (const auto *SS = dyn_cast(Section.get())) { - if (SS->Content && SS->Entries) + if (!SS->Entries && !SS->Content && !SS->Size) + return ".stack_sizes: one of Content, Entries and Size must be specified"; + + if (SS->Size && SS->Content && + (uint64_t)(*SS->Size) < SS->Content->binary_size()) + return ".stack_sizes: Size must be greater than or equal to the content " + "size"; + + // We accept Content, Size or both together when there are no Entries. + if (!SS->Entries) + return {}; + + if (SS->Size) + return ".stack_sizes: Size and Entries cannot be used together"; + if (SS->Content) return ".stack_sizes: Content and Entries cannot be used together"; - if (!SS->Content && !SS->Entries) - return ".stack_sizes: either Content or Entries tag must be specified"; return {}; } return {}; -- cgit v1.2.3