Move clearOutputSections earlier. NFC.

This now just requires not calling assignOffsets after it.

llvm-svn: 304861
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 444384d..5ee7e20 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1115,10 +1115,8 @@
 static void applySynthetic(const std::vector<SyntheticSection *> &Sections,
                            std::function<void(SyntheticSection *)> Fn) {
   for (SyntheticSection *SS : Sections)
-    if (SS && SS->getParent() && !SS->empty()) {
+    if (SS && SS->getParent() && !SS->empty())
       Fn(SS);
-      SS->getParent()->assignOffsets();
-    }
 }
 
 // We need to add input synthetic sections early in createSyntheticSections()
@@ -1233,6 +1231,12 @@
     Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
   }
 
+  // Compute the size of .rela.dyn and .rela.plt early since we need
+  // them to populate .dynamic.
+  for (SyntheticSection *SS : {In<ELFT>::RelaDyn, In<ELFT>::RelaPlt})
+    if (SS->getParent() && !SS->empty())
+      SS->getParent()->assignOffsets();
+
   // Dynamic section must be the last one in this list and dynamic
   // symbol table section (DynSymTab) must be the first one.
   applySynthetic({InX::DynSymTab,    InX::Bss,           InX::BssRelRo,
@@ -1268,6 +1272,7 @@
   for (BaseCommand *Base : Script->Opt.Commands)
     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
       OutputSectionCommands.push_back(Cmd);
+  clearOutputSections();
 
   // Fill other section headers. The dynamic table is finalized
   // at the end because some tags like RELSZ depend on result
@@ -1278,8 +1283,6 @@
   // createThunks may have added local symbols to the static symbol table
   applySynthetic({InX::SymTab, InX::ShStrTab, InX::StrTab},
                  [](SyntheticSection *SS) { SS->postThunkContents(); });
-
-  clearOutputSections();
 }
 
 template <class ELFT> void Writer<ELFT>::addPredefinedSections() {