[ELF] Convert DynamicSection to input section.

This patch introduces the following changes:
- DynamicSection now inherits InputSection<ELFT> and was moved
  to SyntheticSections.h/.cpp.
- Link and Entsize fields of DynamicSection are propagated to 
  its output section
- In<ELFT>::SyntheticSections was removed.
- Finalization of synthetic sections was removed from 
  OutputSection<ELFT>::finalize. Now finalizeSyntheticSections is
  used instead.

Differential revision: https://reviews.llvm.org/D26603

llvm-svn: 286950
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 15b4b32..f8600ed 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -210,7 +210,7 @@
   Out<ELFT>::Bss =
       make<OutputSection<ELFT>>(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
   In<ELFT>::DynStrTab = make<StringTableSection<ELFT>>(".dynstr", true);
-  Out<ELFT>::Dynamic = make<DynamicSection<ELFT>>();
+  In<ELFT>::Dynamic = make<DynamicSection<ELFT>>();
   Out<ELFT>::EhFrame = make<EhOutputSection<ELFT>>();
   Out<ELFT>::Plt = make<PltSection<ELFT>>();
   Out<ELFT>::RelaDyn = make<RelocationSection<ELFT>>(
@@ -314,13 +314,6 @@
 
   In<ELFT>::Got = make<GotSection<ELFT>>();
   In<ELFT>::GotPlt = make<GotPltSection<ELFT>>();
-
-  // These sections are filled after createSections() is called.
-  // We use this list to fixup size of output sections, when they
-  // are finalized.
-  In<ELFT>::SyntheticSections = {In<ELFT>::ShStrTab, In<ELFT>::StrTab,
-                                 In<ELFT>::DynStrTab, In<ELFT>::Got,
-                                 In<ELFT>::GotPlt};
 }
 
 template <class ELFT>
@@ -434,7 +427,7 @@
     return true;
   if (Sec == In<ELFT>::GotPlt->OutSec)
     return Config->ZNow;
-  if (Sec == Out<ELFT>::Dynamic || Sec == In<ELFT>::Got->OutSec)
+  if (Sec == In<ELFT>::Dynamic->OutSec || Sec == In<ELFT>::Got->OutSec)
     return true;
   StringRef S = Sec->getName();
   return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" ||
@@ -861,6 +854,17 @@
   Script<ELFT>::X->adjustSectionsAfterSorting();
 }
 
+template <class ELFT>
+static void
+finalizeSynthetic(const std::vector<SyntheticSection<ELFT> *> &Sections) {
+  for (SyntheticSection<ELFT> *SS : Sections)
+    if (SS && SS->OutSec) {
+      SS->finalize();
+      SS->OutSec->Size = 0;
+      SS->OutSec->assignOffsets();
+    }
+}
+
 // Create output section objects and add them to OutputSections.
 template <class ELFT> void Writer<ELFT>::finalizeSections() {
   Out<ELFT>::DebugInfo = findSection(".debug_info");
@@ -882,8 +886,7 @@
   // Even the author of gold doesn't remember why gold behaves that way.
   // https://sourceware.org/ml/binutils/2002-03/msg00360.html
   if (Out<ELFT>::DynSymTab)
-    Symtab<ELFT>::X->addSynthetic("_DYNAMIC", Out<ELFT>::Dynamic, 0,
-                                  STV_HIDDEN);
+    addRegular("_DYNAMIC", In<ELFT>::Dynamic, 0);
 
   // Define __rel[a]_iplt_{start,end} symbols if needed.
   addRelIpltSymbols();
@@ -940,11 +943,11 @@
   // at the end because some tags like RELSZ depend on result
   // of finalizing other sections.
   for (OutputSectionBase *Sec : OutputSections)
-    if (Sec != Out<ELFT>::Dynamic)
-      Sec->finalize();
+    Sec->finalize();
 
-  if (Out<ELFT>::DynSymTab)
-    Out<ELFT>::Dynamic->finalize();
+  finalizeSynthetic<ELFT>({In<ELFT>::ShStrTab, In<ELFT>::StrTab,
+                           In<ELFT>::DynStrTab, In<ELFT>::Got, In<ELFT>::GotPlt,
+                           In<ELFT>::Dynamic});
 
   // Now that all output offsets are fixed. Finalize mergeable sections
   // to fix their maps from input offsets to output offsets.
@@ -992,7 +995,7 @@
 
     Add(Out<ELFT>::GnuHashTab);
     Add(Out<ELFT>::HashTab);
-    Add(Out<ELFT>::Dynamic);
+    addInputSec(In<ELFT>::Dynamic);
     addInputSec(In<ELFT>::DynStrTab);
     if (Out<ELFT>::RelaDyn->hasRelocs())
       Add(Out<ELFT>::RelaDyn);
@@ -1006,10 +1009,8 @@
 
   // We fill .got and .got.plt sections in scanRelocs(). This is the
   // reason we don't add it earlier in createSections().
-  if (needsGot()) {
-    In<ELFT>::Got->finalize();
+  if (needsGot())
     addInputSec(In<ELFT>::Got);
-  }
 
   if (!In<ELFT>::GotPlt->empty())
     addInputSec(In<ELFT>::GotPlt);
@@ -1158,8 +1159,8 @@
 
   // Add an entry for .dynamic.
   if (Out<ELFT>::DynSymTab) {
-    Phdr &H = *AddHdr(PT_DYNAMIC, Out<ELFT>::Dynamic->getPhdrFlags());
-    H.add(Out<ELFT>::Dynamic);
+    Phdr &H = *AddHdr(PT_DYNAMIC, In<ELFT>::Dynamic->OutSec->getPhdrFlags());
+    H.add(In<ELFT>::Dynamic->OutSec);
   }
 
   // PT_GNU_RELRO includes all sections that should be marked as