[lld] r277034 - Make CommonInputSection singleton class.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 28 14:05:04 PDT 2016


Author: ruiu
Date: Thu Jul 28 16:05:04 2016
New Revision: 277034

URL: http://llvm.org/viewvc/llvm-project?rev=277034&view=rev
Log:
Make CommonInputSection singleton class.

All other singleton instances are accessible globally.
CommonInputSection shouldn't be an exception.

Differential Revision: https://reviews.llvm.org/D22935

Modified:
    lld/trunk/ELF/InputSection.cpp
    lld/trunk/ELF/InputSection.h
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/LinkerScript.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=277034&r1=277033&r2=277034&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Thu Jul 28 16:05:04 2016
@@ -667,17 +667,9 @@ bool MipsOptionsInputSection<ELFT>::clas
   return S->SectionKind == InputSectionBase<ELFT>::MipsOptions;
 }
 
-// Due to initialization order in C++ memberwise initialization or
-// construction is invoked after base class construction. This helper
-// function is needed to zero initialize Elf_Shdr, before passing it
-// to InputSection<ELFT> constructor
-template <class T> static T *zero(T *Val) {
-  return static_cast<T *>(memset(Val, 0, sizeof(*Val)));
-}
-
 template <class ELFT>
 CommonInputSection<ELFT>::CommonInputSection()
-    : InputSection<ELFT>(nullptr, zero(&Hdr)) {
+    : InputSection<ELFT>(nullptr, &Hdr) {
   std::vector<DefinedCommon<ELFT> *> Symbols;
   Hdr.sh_size = 0;
   Hdr.sh_type = SHT_NOBITS;

Modified: lld/trunk/ELF/InputSection.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.h?rev=277034&r1=277033&r2=277034&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.h (original)
+++ lld/trunk/ELF/InputSection.h Thu Jul 28 16:05:04 2016
@@ -257,17 +257,26 @@ public:
   const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
 };
 
-// A special kind of section used to store common symbols
+// Common symbols don't belong to any section. But it is easier for us
+// to handle them as if they belong to some input section. So we defined
+// this class. CommonInputSection is a virtual singleton class that
+// "contains" all common symbols.
 template <class ELFT> class CommonInputSection : public InputSection<ELFT> {
   typedef typename ELFT::uint uintX_t;
 
 public:
   CommonInputSection();
 
+  // The singleton instance of this class.
+  static CommonInputSection<ELFT> *X;
+
 private:
-  typename ELFT::Shdr Hdr;
+  static typename ELFT::Shdr Hdr;
 };
 
+template <class ELFT> CommonInputSection<ELFT> *CommonInputSection<ELFT>::X;
+template <class ELFT> typename ELFT::Shdr CommonInputSection<ELFT>::Hdr;
+
 } // namespace elf
 } // namespace lld
 

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=277034&r1=277033&r2=277034&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Thu Jul 28 16:05:04 2016
@@ -95,8 +95,7 @@ LinkerScript<ELFT>::getSectionMap() {
 // Returns input sections filtered by given glob patterns.
 template <class ELFT>
 std::vector<InputSectionBase<ELFT> *>
-LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I,
-                                     CommonInputSection<ELFT> *Common) {
+LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) {
   ArrayRef<StringRef> Patterns = I->Patterns;
   ArrayRef<StringRef> ExcludedFiles = I->ExcludedFiles;
   std::vector<InputSectionBase<ELFT> *> Ret;
@@ -109,15 +108,14 @@ LinkerScript<ELFT>::getInputSections(con
           Ret.push_back(S);
 
   if ((llvm::find(Patterns, "COMMON") != Patterns.end()))
-    Ret.push_back(Common);
+    Ret.push_back(CommonInputSection<ELFT>::X);
 
   return Ret;
 }
 
 template <class ELFT>
 std::vector<OutputSectionBase<ELFT> *>
-LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory,
-                                   CommonInputSection<ELFT> *Common) {
+LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
   std::vector<OutputSectionBase<ELFT> *> Ret;
 
   // Add input section to output section. If there is no output section yet,
@@ -134,7 +132,7 @@ LinkerScript<ELFT>::createSections(Outpu
   for (auto &P : getSectionMap()) {
     StringRef OutputName = P.first;
     const InputSectionDescription *I = P.second;
-    for (InputSectionBase<ELFT> *S : getInputSections(I, Common)) {
+    for (InputSectionBase<ELFT> *S : getInputSections(I)) {
       if (OutputName == "/DISCARD/") {
         S->Live = false;
         reportDiscarded(S);

Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=277034&r1=277033&r2=277034&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Thu Jul 28 16:05:04 2016
@@ -24,7 +24,6 @@ template <class ELFT> class InputSection
 template <class ELFT> class OutputSectionBase;
 template <class ELFT> class OutputSectionFactory;
 template <class ELFT> class DefinedCommon;
-template <class ELFT> class CommonInputSection;
 
 typedef std::function<uint64_t(uint64_t)> Expr;
 
@@ -122,8 +121,7 @@ template <class ELFT> class LinkerScript
 
 public:
   std::vector<OutputSectionBase<ELFT> *>
-  createSections(OutputSectionFactory<ELFT> &Factory,
-                 CommonInputSection<ELFT> *Common);
+  createSections(OutputSectionFactory<ELFT> &Factory);
 
   std::vector<PhdrEntry<ELFT>>
   createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S);
@@ -140,7 +138,7 @@ private:
   getSectionMap();
 
   std::vector<InputSectionBase<ELFT> *>
-  getInputSections(const InputSectionDescription *, CommonInputSection<ELFT> *);
+  getInputSections(const InputSectionDescription *);
 
   // "ScriptConfig" is a bit too long, so define a short name for it.
   ScriptConfiguration &Opt = *ScriptConfig;

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=277034&r1=277033&r2=277034&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Jul 28 16:05:04 2016
@@ -72,7 +72,6 @@ private:
 
   BumpPtrAllocator Alloc;
   std::vector<OutputSectionBase<ELFT> *> OutputSections;
-  std::unique_ptr<CommonInputSection<ELFT>> CommonSection;
   OutputSectionFactory<ELFT> Factory;
 
   void addRelIpltSymbols();
@@ -222,10 +221,12 @@ template <class ELFT> void Writer<ELFT>:
     copyLocalSymbols();
   addReservedSymbols();
 
-  CommonSection = llvm::make_unique<CommonInputSection<ELFT>>();
+  CommonInputSection<ELFT> Common;
+  CommonInputSection<ELFT>::X = &Common;
+
   OutputSections =
       ScriptConfig->DoLayout
-          ? Script<ELFT>::X->createSections(Factory, CommonSection.get())
+          ? Script<ELFT>::X->createSections(Factory)
           : createSections();
   finalizeSections();
   if (HasError)
@@ -738,8 +739,8 @@ template <class ELFT> void Writer<ELFT>:
 
   // If linker script processor hasn't added common symbol section yet,
   // then add it to .bss now.
-  if (!CommonSection->OutSec) {
-    Out<ELFT>::Bss->addSection(CommonSection.get());
+  if (!CommonInputSection<ELFT>::X->OutSec) {
+    Out<ELFT>::Bss->addSection(CommonInputSection<ELFT>::X);
     Out<ELFT>::Bss->assignOffsets();
   }
 




More information about the llvm-commits mailing list