Move clearOutputSections earlier.

This moves clearOutputSections and OutputSectionCommands creation as
early as possible without changing other code.

llvm-svn: 304751
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index dd95c9a..9ffe6e9 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -258,13 +258,7 @@
   if (ErrorCount)
     return;
 
-  for (BaseCommand *Base : Script->Opt.Commands)
-    if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
-      OutputSectionCommands.push_back(Cmd);
-
-  clearOutputSections();
-
-  if (!Script->Opt.HasSections &&!Config->Relocatable)
+  if (!Script->Opt.HasSections && !Config->Relocatable)
     fixSectionAlignments();
 
   // If -compressed-debug-sections is specified, we need to compress
@@ -1267,6 +1261,9 @@
     Script->fabricateDefaultCommands();
   else
     Script->synchronize();
+  for (BaseCommand *Base : Script->Opt.Commands)
+    if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
+      OutputSectionCommands.push_back(Cmd);
 
   // Fill other section headers. The dynamic table is finalized
   // at the end because some tags like RELSZ depend on result
@@ -1277,6 +1274,8 @@
   // 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() {