[lld] r277103 - Remove dependency to SymbolTable from CommonInputSection.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 28 20:39:44 PDT 2016


Author: ruiu
Date: Thu Jul 28 22:39:44 2016
New Revision: 277103

URL: http://llvm.org/viewvc/llvm-project?rev=277103&view=rev
Log:
Remove dependency to SymbolTable from CommonInputSection.

Modified:
    lld/trunk/ELF/InputSection.cpp
    lld/trunk/ELF/InputSection.h
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=277103&r1=277102&r2=277103&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Thu Jul 28 22:39:44 2016
@@ -14,7 +14,6 @@
 #include "InputFiles.h"
 #include "LinkerScript.h"
 #include "OutputSections.h"
-#include "SymbolTable.h"
 #include "Target.h"
 #include "Thunks.h"
 
@@ -668,32 +667,28 @@ bool MipsOptionsInputSection<ELFT>::clas
 }
 
 template <class ELFT>
-CommonInputSection<ELFT>::CommonInputSection()
+CommonInputSection<ELFT>::CommonInputSection(
+    std::vector<DefinedCommon<ELFT> *> Syms)
     : InputSection<ELFT>(nullptr, &Hdr) {
-  std::vector<DefinedCommon<ELFT> *> Symbols;
   Hdr.sh_size = 0;
   Hdr.sh_type = SHT_NOBITS;
   Hdr.sh_flags = SHF_ALLOC | SHF_WRITE;
   this->Live = true;
 
-  for (Symbol *S : Symtab<ELFT>::X->getSymbols())
-    if (auto *C = dyn_cast<DefinedCommon<ELFT>>(S->body()))
-      Symbols.push_back(C);
-
-  std::stable_sort(
-      Symbols.begin(), Symbols.end(),
-      [](const DefinedCommon<ELFT> *A, const DefinedCommon<ELFT> *B) {
-        return A->Alignment > B->Alignment;
-      });
-
-  for (DefinedCommon<ELFT> *C : Symbols) {
-    this->Alignment = std::max<uintX_t>(this->Alignment, C->Alignment);
-    Hdr.sh_size = alignTo(Hdr.sh_size, C->Alignment);
+  // Sort the common symbols by alignment as an heuristic to pack them better.
+  std::stable_sort(Syms.begin(), Syms.end(), [](const DefinedCommon<ELFT> *A,
+                                                const DefinedCommon<ELFT> *B) {
+    return A->Alignment > B->Alignment;
+  });
+
+  for (DefinedCommon<ELFT> *Sym : Syms) {
+    this->Alignment = std::max<uintX_t>(this->Alignment, Sym->Alignment);
+    Hdr.sh_size = alignTo(Hdr.sh_size, Sym->Alignment);
 
     // Compute symbol offset relative to beginning of input section.
-    C->Offset = Hdr.sh_size;
-    C->Section = this;
-    Hdr.sh_size += C->Size;
+    Sym->Offset = Hdr.sh_size;
+    Sym->Section = this;
+    Hdr.sh_size += Sym->Size;
   }
 }
 

Modified: lld/trunk/ELF/InputSection.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.h?rev=277103&r1=277102&r2=277103&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.h (original)
+++ lld/trunk/ELF/InputSection.h Thu Jul 28 22:39:44 2016
@@ -265,7 +265,7 @@ template <class ELFT> class CommonInputS
   typedef typename ELFT::uint uintX_t;
 
 public:
-  CommonInputSection();
+  CommonInputSection(std::vector<DefinedCommon<ELFT> *> Syms);
 
   // The singleton instance of this class.
   static CommonInputSection<ELFT> *X;

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=277103&r1=277102&r2=277103&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Jul 28 22:39:44 2016
@@ -215,13 +215,22 @@ template <class ELFT> void elf::writeRes
   Writer<ELFT>(*Symtab).run();
 }
 
+template <class ELFT>
+static std::vector<DefinedCommon<ELFT> *> getCommonSymbols() {
+  std::vector<DefinedCommon<ELFT> *> V;
+  for (Symbol *S : Symtab<ELFT>::X->getSymbols())
+    if (auto *B = dyn_cast<DefinedCommon<ELFT>>(S->body()))
+      V.push_back(B);
+  return V;
+}
+
 // The main function of the writer.
 template <class ELFT> void Writer<ELFT>::run() {
   if (!Config->DiscardAll)
     copyLocalSymbols();
   addReservedSymbols();
 
-  CommonInputSection<ELFT> Common;
+  CommonInputSection<ELFT> Common(getCommonSymbols<ELFT>());
   CommonInputSection<ELFT>::X = &Common;
 
   OutputSections = ScriptConfig->HasContents




More information about the llvm-commits mailing list