[lld] r295650 - [ELF] Allow local symbols to be added after global symbols

Peter Smith via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 20 03:12:33 PST 2017


Author: psmith
Date: Mon Feb 20 05:12:33 2017
New Revision: 295650

URL: http://llvm.org/viewvc/llvm-project?rev=295650&view=rev
Log:
[ELF] Allow local symbols to be added after global symbols
    
This change moves the SymbolBodies with isLocal() == true before the global
symbols then calculating NumLocals rather than assuming all locals are
added before globals and the first NumLocals have isLocal() == true. This
permits Thunks to be moved after the pass that adds global symbols from
synthetics to the symbol table.

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


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

Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=295650&r1=295649&r2=295650&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Mon Feb 20 05:12:33 2017
@@ -1085,9 +1085,17 @@ static bool sortMipsSymbols(const Symbol
 
 template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
   this->OutSec->Link = this->Link = StrTabSec.OutSec->SectionIndex;
-  this->OutSec->Info = this->Info = NumLocals + 1;
   this->OutSec->Entsize = this->Entsize;
 
+  if (!StrTabSec.isDynamic()) {
+    // All explictly added STB_LOCAL symbols without a Symbol are first
+    auto It = std::stable_partition(
+        Symbols.begin(), Symbols.end(),
+        [](const SymbolTableEntry &S) { return S.Symbol->isLocal(); });
+    NumLocals = It - Symbols.begin();
+  }
+  this->OutSec->Info = this->Info = 1 + NumLocals;
+
   if (Config->Relocatable)
     return;
 
@@ -1122,7 +1130,6 @@ template <class ELFT> void SymbolTableSe
 
 template <class ELFT> void SymbolTableSection<ELFT>::addLocal(SymbolBody *B) {
   assert(!StrTabSec.isDynamic());
-  ++NumLocals;
   Symbols.push_back({B, StrTabSec.addString(B->getName())});
 }
 

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=295650&r1=295649&r2=295650&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Feb 20 05:12:33 2017
@@ -1103,13 +1103,7 @@ template <class ELFT> void Writer<ELFT>:
   if (In<ELFT>::Iplt && !In<ELFT>::Iplt->empty())
     In<ELFT>::Iplt->addSymbols();
 
-  // Some architectures use small displacements for jump instructions.
-  // It is linker's responsibility to create thunks containing long
-  // jump instructions if jump targets are too far. Create thunks.
-  if (Target->NeedsThunks)
-    createThunks<ELFT>(OutputSections);
-
-  // Now that we have defined all possible symbols including linker-
+  // Now that we have defined all possible global symbols including linker-
   // synthesized ones. Visit all symbols to give the finishing touches.
   for (Symbol *S : Symtab<ELFT>::X->getSymbols()) {
     SymbolBody *Body = S->body();
@@ -1159,6 +1153,12 @@ template <class ELFT> void Writer<ELFT>:
     fixHeaders();
   }
 
+  // Some architectures use small displacements for jump instructions.
+  // It is linker's responsibility to create thunks containing long
+  // jump instructions if jump targets are too far. Create thunks.
+  if (Target->NeedsThunks)
+    createThunks<ELFT>(OutputSections);
+
   // Fill other section headers. The dynamic table is finalized
   // at the end because some tags like RELSZ depend on result
   // of finalizing other sections.




More information about the llvm-commits mailing list