[ELF] Simplify OutputSection::sectionIndex assignment. NFC

And improve comments.

GitOrigin-RevId: 25c7ec4fc622b73f02840daf2ebaf4a5dabb9e1a
diff --git a/ELF/Writer.cpp b/ELF/Writer.cpp
index adceb30..42e63ee 100644
--- a/ELF/Writer.cpp
+++ b/ELF/Writer.cpp
@@ -2031,11 +2031,14 @@
 
   sortSections();
 
-  // Now that we have the final list, create a list of all the
-  // OutputSections for convenience.
+  // Create a list of OutputSections, assign sectionIndex, and populate
+  // in.shStrTab.
   for (SectionCommand *cmd : script->sectionCommands)
-    if (auto *sec = dyn_cast<OutputSection>(cmd))
-      outputSections.push_back(sec);
+    if (auto *osec = dyn_cast<OutputSection>(cmd)) {
+      outputSections.push_back(osec);
+      osec->sectionIndex = outputSections.size();
+      osec->shName = in.shStrTab->addString(osec->name);
+    }
 
   // Prefer command line supplied address over other constraints.
   for (OutputSection *sec : outputSections) {
@@ -2059,12 +2062,6 @@
   Out::elfHeader->sectionIndex = 1;
   Out::elfHeader->size = sizeof(typename ELFT::Ehdr);
 
-  for (size_t i = 0, e = outputSections.size(); i != e; ++i) {
-    OutputSection *sec = outputSections[i];
-    sec->sectionIndex = i + 1;
-    sec->shName = in.shStrTab->addString(sec->name);
-  }
-
   // Binary and relocatable output does not have PHDRS.
   // The headers have to be created before finalize as that can influence the
   // image base and the dynamic section on mips includes the image base.