From: Georgii Rymar Date: Fri, 9 Oct 2020 11:57:02 +0000 (+0300) Subject: [lib/ObjectYAML] - Simplify the code that handles Content/Size fields. X-Git-Tag: llvmorg-13-init~9118 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3cfd9384bff476a4579c956e0c33697fee8bcf29;p=platform%2Fupstream%2Fllvm.git [lib/ObjectYAML] - Simplify the code that handles Content/Size fields. This is a follow-up for D89039 patch, which adds a support for `Content`/`Size` for all sections. Assuming that all of sections have a support of these 2 fields, we can simplify and generalize the code. Depends on D89039 Differential revision: https://reviews.llvm.org/D89120 --- diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp index 155009b..4474afb 100644 --- a/llvm/lib/ObjectYAML/ELFEmitter.cpp +++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp @@ -631,6 +631,22 @@ uint64_t ELFState::getSectionNameOffset(StringRef Name) { return DotShStrtab.getOffset(Name); } +static uint64_t writeContent(ContiguousBlobAccumulator &CBA, + const Optional &Content, + const Optional &Size) { + size_t ContentSize = 0; + if (Content) { + CBA.writeAsBinary(*Content); + ContentSize = Content->binary_size(); + } + + if (!Size) + return ContentSize; + + CBA.writeZeros(*Size - ContentSize); + return *Size; +} + template void ELFState::initSectionHeaders(std::vector &SHeaders, ContiguousBlobAccumulator &CBA) { @@ -690,7 +706,16 @@ void ELFState::initSectionHeaders(std::vector &SHeaders, } if (Sec->EntSize) SHeader.sh_entsize = *Sec->EntSize; - } else if (auto S = dyn_cast(Sec)) { + + LocationCounter += SHeader.sh_size; + overrideFields(Sec, SHeader); + continue; + } + + if (!isa(Sec) && (Sec->Content || Sec->Size)) + SHeader.sh_size = writeContent(CBA, Sec->Content, Sec->Size); + + if (auto S = dyn_cast(Sec)) { writeSectionContent(SHeader, *S, CBA); } else if (auto S = dyn_cast(Sec)) { writeSectionContent(SHeader, *S, CBA); @@ -769,22 +794,6 @@ static size_t findFirstNonGlobal(ArrayRef Symbols) { return Symbols.size(); } -static uint64_t writeContent(ContiguousBlobAccumulator &CBA, - const Optional &Content, - const Optional &Size) { - size_t ContentSize = 0; - if (Content) { - CBA.writeAsBinary(*Content); - ContentSize = Content->binary_size(); - } - - if (!Size) - return ContentSize; - - CBA.writeZeros(*Size - ContentSize); - return *Size; -} - template std::vector ELFState::toELFSymbols(ArrayRef Symbols, @@ -1124,9 +1133,6 @@ template void ELFState::writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::NoBitsSection &S, ContiguousBlobAccumulator &CBA) { - // SHT_NOBITS sections do not have any content to write. - SHeader.sh_entsize = 0; - if (!S.Size) return; @@ -1143,8 +1149,6 @@ template void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, ContiguousBlobAccumulator &CBA) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - if (Section.EntSize) SHeader.sh_entsize = *Section.EntSize; @@ -1181,11 +1185,6 @@ void ELFState::writeSectionContent( if (!Section.RelocatableSec.empty()) SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name); - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Relocations) return; @@ -1220,11 +1219,6 @@ void ELFState::writeSectionContent(Elf_Shdr &SHeader, SHeader.sh_entsize = Section.EntSize ? uint64_t(*Section.EntSize) : sizeof(Elf_Relr); - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Entries) return; @@ -1275,11 +1269,6 @@ void ELFState::writeSectionContent(Elf_Shdr &SHeader, SHeader.sh_info = toSymbolIndex(*Section.Signature, Section.Name, /*IsDynamic=*/false); - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Members) return; @@ -1300,11 +1289,6 @@ void ELFState::writeSectionContent(Elf_Shdr &SHeader, ContiguousBlobAccumulator &CBA) { SHeader.sh_entsize = Section.EntSize ? (uint64_t)*Section.EntSize : 2; - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Entries) return; @@ -1317,10 +1301,8 @@ template void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::StackSizesSection &Section, ContiguousBlobAccumulator &CBA) { - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); + if (!Section.Entries) return; - } if (!Section.Entries) return; @@ -1335,11 +1317,6 @@ template void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::LinkerOptionsSection &Section, ContiguousBlobAccumulator &CBA) { - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Options) return; @@ -1356,11 +1333,6 @@ template void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::DependentLibrariesSection &Section, ContiguousBlobAccumulator &CBA) { - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Libs) return; @@ -1409,11 +1381,6 @@ void ELFState::writeSectionContent( SN2I.lookup(".symtab", Link)) SHeader.sh_link = Link; - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Entries) return; @@ -1442,10 +1409,8 @@ void ELFState::writeSectionContent(Elf_Shdr &SHeader, else SHeader.sh_entsize = sizeof(typename ELFT::Word); - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); + if (!Section.Bucket) return; - } if (!Section.Bucket) return; @@ -1474,11 +1439,6 @@ void ELFState::writeSectionContent(Elf_Shdr &SHeader, SHeader.sh_info = Section.Info; - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Entries) return; @@ -1524,11 +1484,6 @@ void ELFState::writeSectionContent(Elf_Shdr &SHeader, SHeader.sh_info = Section.Info; - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.VerneedV) return; @@ -1572,11 +1527,6 @@ template void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::ARMIndexTableSection &Section, ContiguousBlobAccumulator &CBA) { - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Entries) return; @@ -1625,11 +1575,6 @@ void ELFState::writeSectionContent(Elf_Shdr &SHeader, else SHeader.sh_entsize = 2 * sizeof(uintX_t); - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Entries) return; @@ -1649,10 +1594,8 @@ void ELFState::writeSectionContent(Elf_Shdr &SHeader, SN2I.lookup(".symtab", Link)) SHeader.sh_link = Link; - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); + if (!Section.Symbols) return; - } if (!Section.Symbols) return; @@ -1666,15 +1609,10 @@ template void ELFState::writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::NoteSection &Section, ContiguousBlobAccumulator &CBA) { - uint64_t Offset = CBA.tell(); - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); - return; - } - if (!Section.Notes) return; + uint64_t Offset = CBA.tell(); for (const ELFYAML::NoteEntry &NE : *Section.Notes) { // Write name size. if (NE.Name.empty()) @@ -1717,10 +1655,8 @@ void ELFState::writeSectionContent(Elf_Shdr &SHeader, SN2I.lookup(".dynsym", Link)) SHeader.sh_link = Link; - if (Section.Content || Section.Size) { - SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size); + if (!Section.HashBuckets) return; - } if (!Section.Header) return;