[llvm] r359297 - [yaml2elf] - Cleanup the initSectionHeaders(). NFCI.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 26 05:15:33 PDT 2019


Author: grimar
Date: Fri Apr 26 05:15:32 2019
New Revision: 359297

URL: http://llvm.org/viewvc/llvm-project?rev=359297&view=rev
Log:
[yaml2elf] - Cleanup the initSectionHeaders(). NFCI.

This encapsulates the section specific code inside the
corresponding writeSectionContent methods.
Making the code a bit more consistent.

Modified:
    llvm/trunk/tools/yaml2obj/yaml2elf.cpp

Modified: llvm/trunk/tools/yaml2obj/yaml2elf.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/yaml2obj/yaml2elf.cpp?rev=359297&r1=359296&r2=359297&view=diff
==============================================================================
--- llvm/trunk/tools/yaml2obj/yaml2elf.cpp (original)
+++ llvm/trunk/tools/yaml2obj/yaml2elf.cpp Fri Apr 26 05:15:32 2019
@@ -147,7 +147,7 @@ class ELFState {
                               std::vector<Elf_Shdr> &SHeaders);
   void addSymbols(ArrayRef<ELFYAML::Symbol> Symbols, std::vector<Elf_Sym> &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<ELFT>::initSectionHeaders(
       SHeader.sh_link = Index;
     }
 
-    if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec.get()))
-      writeSectionContent(SHeader, *S, CBA);
-    else if (auto S = dyn_cast<ELFYAML::RelocationSection>(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<ELFYAML::RawContentSection>(Sec.get())) {
+      if (!writeSectionContent(SHeader, *S, CBA))
         return false;
-      SHeader.sh_info = Index;
+    } else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec.get())) {
       if (!writeSectionContent(SHeader, *S, CBA))
         return false;
     } else if (auto S = dyn_cast<ELFYAML::Group>(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<ELFYAML::MipsABIFlags>(Sec.get())) {
@@ -309,11 +293,14 @@ bool ELFState<ELFT>::initSectionHeaders(
       if (!writeSectionContent(SHeader, *S, CBA))
         return false;
     } else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec.get())) {
-      writeSectionContent(SHeader, *S, CBA);
+      if (!writeSectionContent(SHeader, *S, CBA))
+        return false;
     } else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec.get())) {
-      writeSectionContent(SHeader, *S, CBA);
+      if (!writeSectionContent(SHeader, *S, CBA))
+        return false;
     } else if (auto S = dyn_cast<ELFYAML::VerdefSection>(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<ELFT>::addSymbols(ArrayRef
 }
 
 template <class ELFT>
-void ELFState<ELFT>::writeSectionContent(
+bool ELFState<ELFT>::writeSectionContent(
     Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
     ContiguousBlobAccumulator &CBA) {
   assert(Section.Size >= Section.Content.binary_size() &&
@@ -521,6 +508,7 @@ void ELFState<ELFT>::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<ELFT>::writeSectionContent(Elf_
   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<ELFT>::writeSectionContent
   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 <class ELFT>
 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
                                          const ELFYAML::VerneedSection &Section,
                                          ContiguousBlobAccumulator &CBA) {
- typedef typename ELFT::Verneed Elf_Verneed;
- typedef typename ELFT::Vernaux Elf_Vernaux;
+  typedef typename ELFT::Verneed Elf_Verneed;
+  typedef typename ELFT::Vernaux Elf_Vernaux;
 
- auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
+  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;
+  uint64_t AuxCnt = 0;
+  for (size_t I = 0; I < Section.VerneedV.size(); ++I) {
+    const ELFYAML::VerneedEntry &VE = Section.VerneedV[I];
 
- return true;
+    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 <class ELFT>
@@ -883,7 +890,7 @@ int ELFState<ELFT>::writeELF(raw_ostream
   ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
 
   std::vector<Elf_Shdr> SHeaders;
-  if(!State.initSectionHeaders(SHeaders, CBA))
+  if (!State.initSectionHeaders(SHeaders, CBA))
     return 1;
 
   // Populate SHeaders with implicit sections not present in the Doc




More information about the llvm-commits mailing list