Split MergeOutputSection::finalize.
authorRui Ueyama <ruiu@google.com>
Sat, 26 Nov 2016 15:09:58 +0000 (15:09 +0000)
committerRui Ueyama <ruiu@google.com>
Sat, 26 Nov 2016 15:09:58 +0000 (15:09 +0000)
llvm-svn: 287977

lld/ELF/OutputSections.cpp
lld/ELF/OutputSections.h

index 241f93d..e4536ef 100644 (file)
@@ -489,39 +489,46 @@ template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
   return (this->Flags & SHF_STRINGS) && Config->Optimize >= 2;
 }
 
-template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
+template <class ELFT> void MergeOutputSection<ELFT>::finalizeTailMerge() {
   // Add all string pieces to the string table builder to create section
-  // contents. If we are not tail-optimizing, offsets of strings are fixed
-  // when they are added to the builder (string table builder contains a
-  // hash table from strings to offsets), so we record them if available.
-  for (MergeInputSection<ELFT> *Sec : Sections) {
-    for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) {
-      if (!Sec->Pieces[I].Live)
-        continue;
-      uint32_t OutputOffset = Builder.add(Sec->getData(I));
-
-      // Save the offset in the generated string table.
-      if (!shouldTailMerge())
-        Sec->Pieces[I].OutputOff = OutputOffset;
-    }
-  }
-
-  // Fix the string table content. After this, the contents
-  // will never change.
-  if (shouldTailMerge())
-    Builder.finalize();
-  else
-    Builder.finalizeInOrder();
+  // contents.
+  for (MergeInputSection<ELFT> *Sec : Sections)
+    for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
+      if (Sec->Pieces[I].Live)
+        Builder.add(Sec->getData(I));
+
+  // Fix the string table content. After this, the contents will never change.
+  Builder.finalize();
   this->Size = Builder.getSize();
 
   // finalize() fixed tail-optimized strings, so we can now get
   // offsets of strings. Get an offset for each string and save it
   // to a corresponding StringPiece for easy access.
+  for (MergeInputSection<ELFT> *Sec : Sections)
+    for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
+      if (Sec->Pieces[I].Live)
+        Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I));
+}
+
+template <class ELFT> void MergeOutputSection<ELFT>::finalizeNoTailMerge() {
+  // Add all string pieces to the string table builder to create section
+  // contents. Because we are not tail-optimizing, offsets of strings are
+  // fixed when they are added to the builder (string table builder contains
+  // a hash table from strings to offsets).
+  for (MergeInputSection<ELFT> *Sec : Sections)
+    for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
+      if (Sec->Pieces[I].Live)
+        Sec->Pieces[I].OutputOff = Builder.add(Sec->getData(I));
+
+  Builder.finalizeInOrder();
+  this->Size = Builder.getSize();
+}
+
+template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
   if (shouldTailMerge())
-    for (MergeInputSection<ELFT> *Sec : Sections)
-      for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
-        if (Sec->Pieces[I].Live)
-          Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I));
+    finalizeTailMerge();
+  else
+    finalizeNoTailMerge();
 }
 
 template <class ELFT>
index edb028f..2dd213d 100644 (file)
@@ -140,6 +140,9 @@ public:
   }
 
 private:
+  void finalizeTailMerge();
+  void finalizeNoTailMerge();
+
   llvm::StringTableBuilder Builder;
   std::vector<MergeInputSection<ELFT> *> Sections;
 };