[lld] r298078 - [ELF] - Simplify logic of creating "COMMON" section.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 17 06:04:07 PDT 2017


Author: grimar
Date: Fri Mar 17 08:04:06 2017
New Revision: 298078

URL: http://llvm.org/viewvc/llvm-project?rev=298078&view=rev
Log:
[ELF] - Simplify logic of creating "COMMON" section.

Patch reuses BssSection section to simplify creation of 
COMMON section.

Differential revision: https://reviews.llvm.org/D30690

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

Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=298078&r1=298077&r2=298078&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Fri Mar 17 08:04:06 2017
@@ -63,33 +63,22 @@ template <class ELFT> static std::vector
 
 // Find all common symbols and allocate space for them.
 template <class ELFT> InputSection *elf::createCommonSection() {
-  auto *Ret = make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 1,
-                                 ArrayRef<uint8_t>(), "COMMON");
-  Ret->Live = true;
-
   if (!Config->DefineCommon)
-    return Ret;
+    return nullptr;
 
   // Sort the common symbols by alignment as an heuristic to pack them better.
   std::vector<DefinedCommon *> Syms = getCommonSymbols<ELFT>();
+  if (Syms.empty())
+    return nullptr;
+
   std::stable_sort(Syms.begin(), Syms.end(),
                    [](const DefinedCommon *A, const DefinedCommon *B) {
                      return A->Alignment > B->Alignment;
                    });
+  BssSection *Ret = make<BssSection>("COMMON");
+  for (DefinedCommon *Sym : Syms)
+    Sym->Offset = Ret->reserveSpace(Sym->Alignment, Sym->Size);
 
-  // Assign offsets to symbols.
-  size_t Size = 0;
-  uint32_t Alignment = 1;
-  for (DefinedCommon *Sym : Syms) {
-    Alignment = std::max(Alignment, Sym->Alignment);
-    Size = alignTo(Size, Sym->Alignment);
-
-    // Compute symbol offset relative to beginning of input section.
-    Sym->Offset = Size;
-    Size += Sym->Size;
-  }
-  Ret->Alignment = Alignment;
-  Ret->Data = makeArrayRef<uint8_t>(nullptr, Size);
   return Ret;
 }
 
@@ -380,7 +369,8 @@ BssSection::BssSection(StringRef Name)
     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 0, Name) {}
 
 size_t BssSection::reserveSpace(uint32_t Alignment, size_t Size) {
-  OutSec->updateAlignment(Alignment);
+  if (OutSec)
+    OutSec->updateAlignment(Alignment);
   this->Size = alignTo(this->Size, Alignment) + Size;
   this->Alignment = std::max<uint32_t>(this->Alignment, Alignment);
   return this->Size - Size;

Modified: lld/trunk/ELF/SyntheticSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.h?rev=298078&r1=298077&r2=298078&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.h (original)
+++ lld/trunk/ELF/SyntheticSections.h Fri Mar 17 08:04:06 2017
@@ -153,9 +153,10 @@ private:
   uint8_t *HashBuf;
 };
 
-// BssSection is used to reserve space for copy relocations. We create two
-// instances of this class for .bss and .bss.rel.ro. .bss is used for writable
-// symbols, and .bss.rel.ro is used for read-only symbols.
+// BssSection is used to reserve space for copy relocations and common symbols.
+// We create three instances of this class for .bss, .bss.rel.ro and "COMMON".
+// .bss is used for writable symbols, .bss.rel.ro is used for read-only symbols
+// and latter used for common symbols.
 class BssSection final : public SyntheticSection {
 public:
   BssSection(StringRef Name);

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=298078&r1=298077&r2=298078&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Fri Mar 17 08:04:06 2017
@@ -358,11 +358,8 @@ template <class ELFT> void Writer<ELFT>:
     Add(In<ELFT>::BuildId);
   }
 
-  InputSection *Common = createCommonSection<ELFT>();
-  if (!Common->Data.empty()) {
-    In<ELFT>::Common = Common;
-    Add(Common);
-  }
+  if (In<ELFT>::Common = createCommonSection<ELFT>())
+    Add(In<ELFT>::Common);
 
   In<ELFT>::Bss = make<BssSection>(".bss");
   Add(In<ELFT>::Bss);




More information about the llvm-commits mailing list