[lld] 25c7ec4 - [ELF] Simplify OutputSection::sectionIndex assignment. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 28 14:56:35 PST 2021


Author: Fangrui Song
Date: 2021-11-28T14:56:29-08:00
New Revision: 25c7ec4fc622b73f02840daf2ebaf4a5dabb9e1a

URL: https://github.com/llvm/llvm-project/commit/25c7ec4fc622b73f02840daf2ebaf4a5dabb9e1a
DIFF: https://github.com/llvm/llvm-project/commit/25c7ec4fc622b73f02840daf2ebaf4a5dabb9e1a.diff

LOG: [ELF] Simplify OutputSection::sectionIndex assignment. NFC

And improve comments.

Added: 
    

Modified: 
    lld/ELF/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index adceb3022460..42e63ee0e98a 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -2031,11 +2031,14 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
 
   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 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
   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.


        


More information about the llvm-commits mailing list