[lld] r219449 - Reapply [ELF] Only mark as DT_NEEDED libs that are strictly necessary (r219353)

Rafael Auler rafaelauler at gmail.com
Thu Oct 9 15:06:38 PDT 2014


Author: rafauler
Date: Thu Oct  9 17:06:38 2014
New Revision: 219449

URL: http://llvm.org/viewvc/llvm-project?rev=219449&view=rev
Log:
Reapply [ELF] Only mark as DT_NEEDED libs that are strictly necessary (r219353)

When committed in r219353, this patch originally caused problems because it was
not tested in debug build. In such scenarios, Driver.cpp adds two additional
passes. These passes serialize all atoms via YAML and reads it back. Since the
patch changed ObjectAtom to hold a new reference, the serialization was removing
the extra data.

This commit implements r219853 in another way, similar to the original MIPS way,
by using a StringSet that holds the names of all copied atoms instead of
directly holding a reference to the copied atom. In this way, this commit is
simpler and eliminate the necessity of changing the DefinedAtom hierarchy to
hold a new data.

Reviewers: shankarke

http://reviews.llvm.org/D5713

Modified:
    lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h
    lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64LinkingContext.h
    lld/trunk/lib/ReaderWriter/ELF/DefaultLayout.h
    lld/trunk/lib/ReaderWriter/ELF/ExecutableWriter.h
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsDynamicLibraryWriter.h
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsELFWriters.h
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsExecutableWriter.h
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h
    lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h
    lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64LinkingContext.h

Modified: lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h?rev=219449&r1=219448&r2=219449&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h Thu Oct  9 17:06:38 2014
@@ -90,6 +90,15 @@ public:
                                    const Reference &) const {
     return false;
   }
+
+  /// \brief Is this a copy relocation?
+  ///
+  /// If this is a copy relocation, its target must be an ObjectAtom. We must
+  /// include in DT_NEEDED the name of the library where this object came from.
+  virtual bool isCopyRelocation(const Reference &) const {
+    return false;
+  }
+
   bool validateImpl(raw_ostream &diagnostics) override;
 
   /// \brief Does the linker allow dynamic libraries to be linked with?

Modified: lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64LinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64LinkingContext.h?rev=219449&r1=219448&r2=219449&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64LinkingContext.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64LinkingContext.h Thu Oct  9 17:06:38 2014
@@ -59,6 +59,15 @@ public:
     }
   }
 
+  bool isCopyRelocation(const Reference &r) const override {
+    if (r.kindNamespace() != Reference::KindNamespace::ELF)
+      return false;
+    assert(r.kindArch() == Reference::KindArch::AArch64);
+    if (r.kindValue() == llvm::ELF::R_AARCH64_COPY)
+      return true;
+    return false;
+  }
+
   bool isPLTRelocation(const DefinedAtom &,
                                const Reference &r) const override {
     if (r.kindNamespace() != Reference::KindNamespace::ELF)

Modified: lld/trunk/lib/ReaderWriter/ELF/DefaultLayout.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/DefaultLayout.h?rev=219449&r1=219448&r2=219449&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/DefaultLayout.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/DefaultLayout.h Thu Oct  9 17:06:38 2014
@@ -10,6 +10,7 @@
 #ifndef LLD_READER_WRITER_ELF_DEFAULT_LAYOUT_H
 #define LLD_READER_WRITER_ELF_DEFAULT_LAYOUT_H
 
+#include "Atoms.h"
 #include "Chunk.h"
 #include "HeaderChunks.h"
 #include "Layout.h"
@@ -303,6 +304,10 @@ public:
     return _referencedDynAtoms.count(a);
   }
 
+  bool isCopied(const SharedLibraryAtom *sla) const {
+    return _copiedDynSymNames.count(sla->name());
+  }
+
 protected:
   /// \brief Allocate a new section.
   virtual AtomSection<ELFT> *createSection(
@@ -325,6 +330,7 @@ protected:
   LLD_UNIQUE_BUMP_PTR(RelocationTable<ELFT>) _pltRelocationTable;
   std::vector<lld::AtomLayout *> _absoluteAtoms;
   AtomSetT _referencedDynAtoms;
+  llvm::StringSet<> _copiedDynSymNames;
   const ELFLinkingContext &_context;
 };
 
@@ -582,6 +588,11 @@ ErrorOr<const lld::AtomLayout &> Default
       if (isa<UndefinedAtom>(reloc->target()) && isLocalReloc)
         continue;
 
+      if (_context.isCopyRelocation(*reloc)) {
+        _copiedDynSymNames.insert(definedAtom->name());
+        continue;
+      }
+
       _referencedDynAtoms.insert(reloc->target());
     }
 

Modified: lld/trunk/lib/ReaderWriter/ELF/ExecutableWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/ExecutableWriter.h?rev=219449&r1=219448&r2=219449&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/ExecutableWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/ExecutableWriter.h Thu Oct  9 17:06:38 2014
@@ -35,6 +35,11 @@ protected:
   virtual bool createImplicitFiles(std::vector<std::unique_ptr<File> > &);
   virtual void finalizeDefaultAtomValues();
   virtual void createDefaultSections();
+
+  virtual bool isNeededTagRequired(const SharedLibraryAtom *sla) const {
+    return this->_layout.isCopied(sla);
+  }
+
   LLD_UNIQUE_BUMP_PTR(InterpSection<ELFT>) _interpSection;
   std::unique_ptr<CRuntimeFile<ELFT> > _runtimeFile;
 };

Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsDynamicLibraryWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsDynamicLibraryWriter.h?rev=219449&r1=219448&r2=219449&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsDynamicLibraryWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsDynamicLibraryWriter.h Thu Oct  9 17:06:38 2014
@@ -37,10 +37,6 @@ protected:
     return std::error_code();
   }
 
-  bool isNeededTagRequired(const SharedLibraryAtom *sla) const override {
-    return _writeHelper.isNeededTagRequired(sla);
-  }
-
   LLD_UNIQUE_BUMP_PTR(DynamicTable<ELFT>) createDynamicTable();
 
   LLD_UNIQUE_BUMP_PTR(DynamicSymbolTable<ELFT>) createDynamicSymbolTable();

Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsELFWriters.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsELFWriters.h?rev=219449&r1=219448&r2=219449&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsELFWriters.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsELFWriters.h Thu Oct  9 17:06:38 2014
@@ -69,11 +69,6 @@ public:
     return file;
   }
 
-  bool isNeededTagRequired(const SharedLibraryAtom *sla) const {
-    return _targetLayout.isReferencedByDefinedAtom(sla) ||
-           _targetLayout.isCopied(sla);
-  }
-
 private:
   MipsLinkingContext &_ctx;
   MipsTargetLayout<ELFT> &_targetLayout;

Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsExecutableWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsExecutableWriter.h?rev=219449&r1=219448&r2=219449&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsExecutableWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsExecutableWriter.h Thu Oct  9 17:06:38 2014
@@ -38,10 +38,6 @@ protected:
     return std::error_code();
   }
 
-  bool isNeededTagRequired(const SharedLibraryAtom *sla) const override {
-    return _writeHelper.isNeededTagRequired(sla);
-  }
-
   LLD_UNIQUE_BUMP_PTR(DynamicTable<ELFT>) createDynamicTable();
 
   LLD_UNIQUE_BUMP_PTR(DynamicSymbolTable<ELFT>) createDynamicSymbolTable();

Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp?rev=219449&r1=219448&r2=219449&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp Thu Oct  9 17:06:38 2014
@@ -65,6 +65,15 @@ bool MipsLinkingContext::isDynamicReloca
   }
 }
 
+bool MipsLinkingContext::isCopyRelocation(const Reference &r) const {
+  if (r.kindNamespace() != Reference::KindNamespace::ELF)
+    return false;
+  assert(r.kindArch() == Reference::KindArch::Mips);
+  if (r.kindValue() == llvm::ELF::R_MIPS_COPY)
+    return true;
+  return false;
+}
+
 bool MipsLinkingContext::isPLTRelocation(const DefinedAtom &,
                                          const Reference &r) const {
   if (r.kindNamespace() != Reference::KindNamespace::ELF)

Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h?rev=219449&r1=219448&r2=219449&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h Thu Oct  9 17:06:38 2014
@@ -47,6 +47,7 @@ public:
   bool isRelaOutputFormat() const override { return false; }
   bool isDynamicRelocation(const DefinedAtom &,
                            const Reference &r) const override;
+  bool isCopyRelocation(const Reference &r) const override;
   bool isPLTRelocation(const DefinedAtom &, const Reference &r) const override;
 };
 

Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h?rev=219449&r1=219448&r2=219449&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h Thu Oct  9 17:06:38 2014
@@ -62,26 +62,6 @@ public:
     }
   }
 
-  ErrorOr<const lld::AtomLayout &> addAtom(const Atom *atom) override {
-    // Maintain:
-    // 1. Set of shared library atoms referenced by regular defined atoms.
-    // 2. Set of shared library atoms have corresponding R_MIPS_COPY copies.
-    if (const auto *da = dyn_cast<DefinedAtom>(atom))
-      for (const Reference *ref : *da) {
-        if (ref->kindNamespace() == lld::Reference::KindNamespace::ELF) {
-          assert(ref->kindArch() == Reference::KindArch::Mips);
-          if (ref->kindValue() == llvm::ELF::R_MIPS_COPY)
-            _copiedDynSymNames.insert(atom->name());
-        }
-      }
-
-    return TargetLayout<ELFType>::addAtom(atom);
-  }
-
-  bool isCopied(const SharedLibraryAtom *sla) const {
-    return _copiedDynSymNames.count(sla->name());
-  }
-
   /// \brief GP offset relative to .got section.
   uint64_t getGPOffset() const { return 0x7FF0; }
 
@@ -109,7 +89,6 @@ private:
   MipsPLTSection<ELFType> *_pltSection;
   llvm::Optional<AtomLayout *> _gpAtom;
   llvm::Optional<AtomLayout *> _gpDispAtom;
-  llvm::StringSet<> _copiedDynSymNames;
 };
 
 /// \brief Mips Runtime file.

Modified: lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h?rev=219449&r1=219448&r2=219449&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h Thu Oct  9 17:06:38 2014
@@ -116,7 +116,7 @@ protected:
 
   /// \brief Create DT_NEEDED dynamic tage for the shared library.
   virtual bool isNeededTagRequired(const SharedLibraryAtom *sla) const {
-    return true;
+    return false;
   }
 
   llvm::BumpPtrAllocator _alloc;
@@ -180,8 +180,11 @@ template <class ELFT>
 void OutputELFWriter<ELFT>::buildDynamicSymbolTable(const File &file) {
   ScopedTask task(getDefaultDomain(), "buildDynamicSymbolTable");
   for (const auto &sla : file.sharedLibrary()) {
-    if (isDynSymEntryRequired(sla))
+    if (isDynSymEntryRequired(sla)) {
       _dynamicSymbolTable->addSymbol(sla, ELF::SHN_UNDEF);
+      _soNeeded.insert(sla->loadName());
+      continue;
+    }
     if (isNeededTagRequired(sla))
       _soNeeded.insert(sla->loadName());
   }

Modified: lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64LinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64LinkingContext.h?rev=219449&r1=219448&r2=219449&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64LinkingContext.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64LinkingContext.h Thu Oct  9 17:06:38 2014
@@ -58,6 +58,15 @@ public:
     }
   }
 
+  bool isCopyRelocation(const Reference &r) const override {
+    if (r.kindNamespace() != Reference::KindNamespace::ELF)
+      return false;
+    assert(r.kindArch() == Reference::KindArch::x86_64);
+    if (r.kindValue() == llvm::ELF::R_X86_64_COPY)
+      return true;
+    return false;
+  }
+
   virtual bool isPLTRelocation(const DefinedAtom &,
                                const Reference &r) const override {
     if (r.kindNamespace() != Reference::KindNamespace::ELF)





More information about the llvm-commits mailing list