[lld] r267045 - Start adding support for internalizing shared libraries.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 21 13:35:25 PDT 2016


Author: rafael
Date: Thu Apr 21 15:35:25 2016
New Revision: 267045

URL: http://llvm.org/viewvc/llvm-project?rev=267045&view=rev
Log:
Start adding support for internalizing shared libraries.

Modified:
    lld/trunk/ELF/LTO.cpp
    lld/trunk/ELF/Symbols.cpp
    lld/trunk/ELF/Symbols.h
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/lto/internalize-exportdyn.ll

Modified: lld/trunk/ELF/LTO.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LTO.cpp?rev=267045&r1=267044&r2=267045&view=diff
==============================================================================
--- lld/trunk/ELF/LTO.cpp (original)
+++ lld/trunk/ELF/LTO.cpp Thu Apr 21 15:35:25 2016
@@ -74,6 +74,17 @@ static void runLTOPasses(Module &M, Targ
     saveBCFile(M, ".lto.opt.bc");
 }
 
+static bool shouldInternalize(const SmallPtrSet<GlobalValue *, 8> &Used,
+                              SymbolBody &B, GlobalValue *GV) {
+  if (B.isUsedInRegularObj())
+    return false;
+
+  if (Used.count(GV))
+    return false;
+
+  return !B.includeInDynsym();
+}
+
 void BitcodeCompiler::add(BitcodeFile &F) {
   std::unique_ptr<IRObjectFile> Obj =
       check(IRObjectFile::create(F.MB, Context));
@@ -121,13 +132,8 @@ void BitcodeCompiler::add(BitcodeFile &F
     // we imported the symbols and satisfied undefined references
     // to it. We can't just change linkage here because otherwise
     // the IRMover will just rename the symbol.
-    // Shared libraries need to be handled slightly differently.
-    // For now, let's be conservative and just never internalize
-    // symbols when creating a shared library.
-    if (!Config->Shared && !Config->ExportDynamic && !B->isUsedInRegularObj() &&
-        !B->MustBeInDynSym)
-      if (!Used.count(GV))
-        InternalizedSyms.insert(GV->getName());
+    if (shouldInternalize(Used, *B, GV))
+      InternalizedSyms.insert(GV->getName());
 
     Keep.push_back(GV);
   }

Modified: lld/trunk/ELF/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.cpp?rev=267045&r1=267044&r2=267045&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.cpp (original)
+++ lld/trunk/ELF/Symbols.cpp Thu Apr 21 15:35:25 2016
@@ -354,6 +354,15 @@ std::string elf::demangle(StringRef Name
 #endif
 }
 
+bool SymbolBody::includeInDynsym() const {
+  if (MustBeInDynSym)
+    return true;
+  uint8_t V = getVisibility();
+  if (V != STV_DEFAULT && V != STV_PROTECTED)
+    return false;
+  return Config->ExportDynamic || Config->Shared;
+}
+
 template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
 template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
 template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;

Modified: lld/trunk/ELF/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.h?rev=267045&r1=267044&r2=267045&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.h (original)
+++ lld/trunk/ELF/Symbols.h Thu Apr 21 15:35:25 2016
@@ -130,6 +130,8 @@ public:
   // they are duplicate (conflicting) symbols.
   int compare(SymbolBody *Other);
 
+  bool includeInDynsym() const;
+
 protected:
   SymbolBody(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther,
              uint8_t Type);

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=267045&r1=267044&r2=267045&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Apr 21 15:35:25 2016
@@ -1053,15 +1053,6 @@ template <class ELFT> static bool includ
   return true;
 }
 
-static bool includeInDynsym(const SymbolBody &B) {
-  if (B.MustBeInDynSym)
-    return true;
-  uint8_t V = B.getVisibility();
-  if (V != STV_DEFAULT && V != STV_PROTECTED)
-    return false;
-  return Config->ExportDynamic || Config->Shared;
-}
-
 // This class knows how to create an output section for a given
 // input section. Output section type is determined by various
 // factors, including input section's sh_flags, sh_type and
@@ -1334,7 +1325,7 @@ template <class ELFT> void Writer<ELFT>:
     if (Out<ELFT>::SymTab)
       Out<ELFT>::SymTab->addSymbol(Body);
 
-    if (isOutputDynamic() && includeInDynsym(*Body))
+    if (isOutputDynamic() && Body->includeInDynsym())
       Out<ELFT>::DynSymTab->addSymbol(Body);
   }
 

Modified: lld/trunk/test/ELF/lto/internalize-exportdyn.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/lto/internalize-exportdyn.ll?rev=267045&r1=267044&r2=267045&view=diff
==============================================================================
--- lld/trunk/test/ELF/lto/internalize-exportdyn.ll (original)
+++ lld/trunk/test/ELF/lto/internalize-exportdyn.ll Thu Apr 21 15:35:25 2016
@@ -10,10 +10,15 @@ define void @_start() {
   ret void
 }
 
-define hidden void @foo() {
+define void @foo() {
   ret void
 }
 
-; Check that _start and foo are not internalized.
+define hidden void @bar() {
+  ret void
+}
+
+; Check that _start and foo are not internalized, but bar is.
 ; CHECK: define void @_start()
-; CHECK: define hidden void @foo()
+; CHECK: define void @foo()
+; CHECK: define internal void @bar()




More information about the llvm-commits mailing list