[llvm] 3cfd938 - [lib/ObjectYAML] - Simplify the code that handles Content/Size fields.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 16 00:06:43 PDT 2020


Author: Georgii Rymar
Date: 2020-10-16T09:57:27+03:00
New Revision: 3cfd9384bff476a4579c956e0c33697fee8bcf29

URL: https://github.com/llvm/llvm-project/commit/3cfd9384bff476a4579c956e0c33697fee8bcf29
DIFF: https://github.com/llvm/llvm-project/commit/3cfd9384bff476a4579c956e0c33697fee8bcf29.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/ObjectYAML/ELFEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index 155009bbb2ea..4474afbc3255 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -631,6 +631,22 @@ uint64_t ELFState<ELFT>::getSectionNameOffset(StringRef Name) {
   return DotShStrtab.getOffset(Name);
 }
 
+static uint64_t writeContent(ContiguousBlobAccumulator &CBA,
+                             const Optional<yaml::BinaryRef> &Content,
+                             const Optional<llvm::yaml::Hex64> &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 <class ELFT>
 void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
                                         ContiguousBlobAccumulator &CBA) {
@@ -690,7 +706,16 @@ void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
       }
       if (Sec->EntSize)
         SHeader.sh_entsize = *Sec->EntSize;
-    } else if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
+
+      LocationCounter += SHeader.sh_size;
+      overrideFields<ELFT>(Sec, SHeader);
+      continue;
+    }
+
+    if (!isa<ELFYAML::NoBitsSection>(Sec) && (Sec->Content || Sec->Size))
+      SHeader.sh_size = writeContent(CBA, Sec->Content, Sec->Size);
+
+    if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
       writeSectionContent(SHeader, *S, CBA);
     } else if (auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) {
       writeSectionContent(SHeader, *S, CBA);
@@ -769,22 +794,6 @@ static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
   return Symbols.size();
 }
 
-static uint64_t writeContent(ContiguousBlobAccumulator &CBA,
-                             const Optional<yaml::BinaryRef> &Content,
-                             const Optional<llvm::yaml::Hex64> &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 <class ELFT>
 std::vector<typename ELFT::Sym>
 ELFState<ELFT>::toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
@@ -1124,9 +1133,6 @@ template <class ELFT>
 void ELFState<ELFT>::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 <class ELFT>
 void ELFState<ELFT>::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<ELFT>::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<ELFT>::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<ELFT>::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<ELFT>::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 <class ELFT>
 void ELFState<ELFT>::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 <class ELFT>
 void ELFState<ELFT>::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 <class ELFT>
 void ELFState<ELFT>::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<ELFT>::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<ELFT>::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<ELFT>::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<ELFT>::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 <class ELFT>
 void ELFState<ELFT>::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<ELFT>::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<ELFT>::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 <class ELFT>
 void ELFState<ELFT>::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<ELFT>::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;


        


More information about the llvm-commits mailing list