From: George Rimar Date: Fri, 26 Apr 2019 12:15:32 +0000 (+0000) Subject: [yaml2elf] - Cleanup the initSectionHeaders(). NFCI. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8399432f4d8e24d9c7255e8cff13e781a2ac3e96;p=llvm [yaml2elf] - Cleanup the initSectionHeaders(). NFCI. This encapsulates the section specific code inside the corresponding writeSectionContent methods. Making the code a bit more consistent. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359297 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/tools/yaml2obj/yaml2elf.cpp b/tools/yaml2obj/yaml2elf.cpp index 974acbd40c9..96761259c4e 100644 --- a/tools/yaml2obj/yaml2elf.cpp +++ b/tools/yaml2obj/yaml2elf.cpp @@ -147,7 +147,7 @@ class ELFState { std::vector &SHeaders); void addSymbols(ArrayRef Symbols, std::vector &Syms, const StringTableBuilder &Strtab); - void writeSectionContent(Elf_Shdr &SHeader, + bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, ContiguousBlobAccumulator &CBA); bool writeSectionContent(Elf_Shdr &SHeader, @@ -271,29 +271,13 @@ bool ELFState::initSectionHeaders(std::vector &SHeaders, SHeader.sh_link = Index; } - if (auto S = dyn_cast(Sec.get())) - writeSectionContent(SHeader, *S, CBA); - else if (auto S = dyn_cast(Sec.get())) { - if (S->Link.empty()) - // For relocation section set link to .symtab by default. - SHeader.sh_link = getDotSymTabSecNo(); - - unsigned Index = 0; - if (!S->RelocatableSec.empty() && - !convertSectionIndex(SN2I, S->Name, S->RelocatableSec, Index)) + if (auto S = dyn_cast(Sec.get())) { + if (!writeSectionContent(SHeader, *S, CBA)) return false; - SHeader.sh_info = Index; + } else if (auto S = dyn_cast(Sec.get())) { if (!writeSectionContent(SHeader, *S, CBA)) return false; } else if (auto S = dyn_cast(Sec.get())) { - unsigned SymIdx; - if (SymN2I.lookup(S->Signature, SymIdx) && - !to_integer(S->Signature, SymIdx)) { - WithColor::error() << "Unknown symbol referenced: '" << S->Signature - << "' at YAML section '" << S->Name << "'.\n"; - return false; - } - SHeader.sh_info = SymIdx; if (!writeSectionContent(SHeader, *S, CBA)) return false; } else if (auto S = dyn_cast(Sec.get())) { @@ -309,11 +293,14 @@ bool ELFState::initSectionHeaders(std::vector &SHeaders, if (!writeSectionContent(SHeader, *S, CBA)) return false; } else if (auto S = dyn_cast(Sec.get())) { - writeSectionContent(SHeader, *S, CBA); + if (!writeSectionContent(SHeader, *S, CBA)) + return false; } else if (auto S = dyn_cast(Sec.get())) { - writeSectionContent(SHeader, *S, CBA); + if (!writeSectionContent(SHeader, *S, CBA)) + return false; } else if (auto S = dyn_cast(Sec.get())) { - writeSectionContent(SHeader, *S, CBA); + if (!writeSectionContent(SHeader, *S, CBA)) + return false; } else llvm_unreachable("Unknown section type"); @@ -503,7 +490,7 @@ void ELFState::addSymbols(ArrayRef Symbols, } template -void ELFState::writeSectionContent( +bool ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, ContiguousBlobAccumulator &CBA) { assert(Section.Size >= Section.Content.binary_size() && @@ -521,6 +508,7 @@ void ELFState::writeSectionContent( SHeader.sh_entsize = 0; SHeader.sh_size = Section.Size; SHeader.sh_info = Section.Info; + return true; } static bool isMips64EL(const ELFYAML::Object &Doc) { @@ -542,6 +530,16 @@ ELFState::writeSectionContent(Elf_Shdr &SHeader, SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size(); + // For relocation section set link to .symtab by default. + if (Section.Link.empty()) + SHeader.sh_link = getDotSymTabSecNo(); + + unsigned Index = 0; + if (!Section.RelocatableSec.empty() && + !convertSectionIndex(SN2I, Section.Name, Section.RelocatableSec, Index)) + return false; + SHeader.sh_info = Index; + auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); for (const auto &Rel : Section.Relocations) { @@ -583,6 +581,15 @@ bool ELFState::writeSectionContent(Elf_Shdr &SHeader, SHeader.sh_entsize = 4; SHeader.sh_size = SHeader.sh_entsize * Section.Members.size(); + unsigned SymIdx; + if (SymN2I.lookup(Section.Signature, SymIdx) && + !to_integer(Section.Signature, SymIdx)) { + WithColor::error() << "Unknown symbol referenced: '" << Section.Signature + << "' at YAML section '" << Section.Name << "'.\n"; + return false; + } + SHeader.sh_info = SymIdx; + raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); @@ -661,48 +668,48 @@ template bool ELFState::writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::VerneedSection &Section, ContiguousBlobAccumulator &CBA) { - typedef typename ELFT::Verneed Elf_Verneed; - typedef typename ELFT::Vernaux Elf_Vernaux; - - auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); - - uint64_t AuxCnt = 0; - for (size_t I = 0; I < Section.VerneedV.size(); ++I) { - const ELFYAML::VerneedEntry &VE = Section.VerneedV[I]; - - Elf_Verneed VerNeed; - VerNeed.vn_version = VE.Version; - VerNeed.vn_file = DotDynstr.getOffset(VE.File); - if (I == Section.VerneedV.size() - 1) - VerNeed.vn_next = 0; - else - VerNeed.vn_next = - sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux); - VerNeed.vn_cnt = VE.AuxV.size(); - VerNeed.vn_aux = sizeof(Elf_Verneed); - OS.write((const char *)&VerNeed, sizeof(Elf_Verneed)); - - for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) { - const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J]; - - Elf_Vernaux VernAux; - VernAux.vna_hash = VAuxE.Hash; - VernAux.vna_flags = VAuxE.Flags; - VernAux.vna_other = VAuxE.Other; - VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name); - if (J == VE.AuxV.size() - 1) - VernAux.vna_next = 0; - else - VernAux.vna_next = sizeof(Elf_Vernaux); - OS.write((const char *)&VernAux, sizeof(Elf_Vernaux)); - } - } - - SHeader.sh_size = Section.VerneedV.size() * sizeof(Elf_Verneed) + - AuxCnt * sizeof(Elf_Vernaux); - SHeader.sh_info = Section.Info; - - return true; + typedef typename ELFT::Verneed Elf_Verneed; + typedef typename ELFT::Vernaux Elf_Vernaux; + + auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); + + uint64_t AuxCnt = 0; + for (size_t I = 0; I < Section.VerneedV.size(); ++I) { + const ELFYAML::VerneedEntry &VE = Section.VerneedV[I]; + + Elf_Verneed VerNeed; + VerNeed.vn_version = VE.Version; + VerNeed.vn_file = DotDynstr.getOffset(VE.File); + if (I == Section.VerneedV.size() - 1) + VerNeed.vn_next = 0; + else + VerNeed.vn_next = + sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux); + VerNeed.vn_cnt = VE.AuxV.size(); + VerNeed.vn_aux = sizeof(Elf_Verneed); + OS.write((const char *)&VerNeed, sizeof(Elf_Verneed)); + + for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) { + const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J]; + + Elf_Vernaux VernAux; + VernAux.vna_hash = VAuxE.Hash; + VernAux.vna_flags = VAuxE.Flags; + VernAux.vna_other = VAuxE.Other; + VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name); + if (J == VE.AuxV.size() - 1) + VernAux.vna_next = 0; + else + VernAux.vna_next = sizeof(Elf_Vernaux); + OS.write((const char *)&VernAux, sizeof(Elf_Vernaux)); + } + } + + SHeader.sh_size = Section.VerneedV.size() * sizeof(Elf_Verneed) + + AuxCnt * sizeof(Elf_Vernaux); + SHeader.sh_info = Section.Info; + + return true; } template @@ -883,7 +890,7 @@ int ELFState::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) { ContiguousBlobAccumulator CBA(SectionContentBeginOffset); std::vector SHeaders; - if(!State.initSectionHeaders(SHeaders, CBA)) + if (!State.initSectionHeaders(SHeaders, CBA)) return 1; // Populate SHeaders with implicit sections not present in the Doc