Move clearOutputSections earlier. NFC.

Now addSectionSymbols operates on the linker script.

llvm-svn: 307102
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 8dbfabd..f790ccf 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -197,11 +197,11 @@
   if (Config->Discard != DiscardPolicy::All)
     copyLocalSymbols();
 
+  clearOutputSections();
+
   if (Config->CopyRelocs)
     addSectionSymbols();
 
-  clearOutputSections();
-
   // Now that we have a complete set of output sections. This function
   // completes section contents. For example, we need to add strings
   // to the string table, and add entries to .got and .plt.
@@ -501,11 +501,18 @@
 template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
   // Create one STT_SECTION symbol for each output section we might
   // have a relocation with.
-  for (OutputSection *Sec : OutputSections) {
-    if (Sec->Sections.empty())
+  for (BaseCommand *Base : Script->Opt.Commands) {
+    auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
+    if (!Cmd)
       continue;
-
-    InputSection *IS = Sec->Sections[0];
+    auto I = llvm::find_if(Cmd->Commands, [](BaseCommand *Base) {
+      if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
+        return !ISD->Sections.empty();
+      return false;
+    });
+    if (I == Cmd->Commands.end())
+      continue;
+    InputSection *IS = cast<InputSectionDescription>(*I)->Sections[0];
     if (isa<SyntheticSection>(IS) || IS->Type == SHT_REL ||
         IS->Type == SHT_RELA)
       continue;