[lld] r234347 - [ELF] Simplify adding default atoms

Simon Atanasyan simon at atanasyan.com
Tue Apr 7 13:11:38 PDT 2015


Author: atanasyan
Date: Tue Apr  7 15:11:38 2015
New Revision: 234347

URL: http://llvm.org/viewvc/llvm-project?rev=234347&view=rev
Log:
[ELF] Simplify adding default atoms

Now 'writer' creates an instance of `RuntimeFile` in the constructor, then
populates the file in the virtual function `addDefaultAtoms`, then pass
owning of this file to the caller of virtual function `createImplicitFiles`.

First, we do not need to keep an instance of `RuntimeFile` so long. It is
enough to create the file, right after that populate it and pass the owning.

Second, relationship between `createImplicitFiles` and `addDefaultAtoms`
is complicated. The `createImplicitFiles` might call `addDefaultAtoms`,
overridden version of `addDefaultAtoms` might call base class `addDefaultAtoms`,
and overridden version of `createImplicitFiles` might call base class
`createImplicitFiles` as well as `addDefaultAtoms`.

The patch solves both problems above. It creates and populates runtime files
right in the createImplicitFiles(), removes `addDefaultAtoms` at all and
does not keep references to runtime files in class fields.

Modified:
    lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64DynamicLibraryWriter.h
    lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64ExecutableWriter.h
    lld/trunk/lib/ReaderWriter/ELF/ARM/ARMExecutableWriter.h
    lld/trunk/lib/ReaderWriter/ELF/DynamicLibraryWriter.h
    lld/trunk/lib/ReaderWriter/ELF/ExecutableWriter.h
    lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonDynamicLibraryWriter.h
    lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonExecutableWriter.h
    lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h
    lld/trunk/lib/ReaderWriter/ELF/X86/X86DynamicLibraryWriter.h
    lld/trunk/lib/ReaderWriter/ELF/X86/X86ExecutableWriter.h
    lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64DynamicLibraryWriter.h
    lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64ExecutableWriter.h

Modified: lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64DynamicLibraryWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64DynamicLibraryWriter.h?rev=234347&r1=234346&r2=234347&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64DynamicLibraryWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64DynamicLibraryWriter.h Tue Apr  7 15:11:38 2015
@@ -30,10 +30,6 @@ protected:
     return DynamicLibraryWriter<ELFT>::finalizeDefaultAtomValues();
   }
 
-  void addDefaultAtoms() override {
-    return DynamicLibraryWriter<ELFT>::addDefaultAtoms();
-  }
-
 private:
   class GOTFile : public SimpleFile {
   public:

Modified: lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64ExecutableWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64ExecutableWriter.h?rev=234347&r1=234346&r2=234347&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64ExecutableWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64ExecutableWriter.h Tue Apr  7 15:11:38 2015
@@ -29,10 +29,6 @@ protected:
     return ExecutableWriter<ELFT>::finalizeDefaultAtomValues();
   }
 
-  void addDefaultAtoms() override{
-    return ExecutableWriter<ELFT>::addDefaultAtoms();
-  }
-
 private:
   class GOTFile : public SimpleFile {
   public:

Modified: lld/trunk/lib/ReaderWriter/ELF/ARM/ARMExecutableWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/ARM/ARMExecutableWriter.h?rev=234347&r1=234346&r2=234347&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/ARM/ARMExecutableWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/ARM/ARMExecutableWriter.h Tue Apr  7 15:11:38 2015
@@ -33,10 +33,6 @@ protected:
 
   void finalizeDefaultAtomValues() override;
 
-  void addDefaultAtoms() override {
-    ExecutableWriter<ELFT>::addDefaultAtoms();
-  }
-
   /// \brief Create symbol table.
   unique_bump_ptr<SymbolTable<ELFT>> createSymbolTable() override;
 

Modified: lld/trunk/lib/ReaderWriter/ELF/DynamicLibraryWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/DynamicLibraryWriter.h?rev=234347&r1=234346&r2=234347&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/DynamicLibraryWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/DynamicLibraryWriter.h Tue Apr  7 15:11:38 2015
@@ -23,17 +23,12 @@ template<class ELFT>
 class DynamicLibraryWriter : public OutputELFWriter<ELFT> {
 public:
   DynamicLibraryWriter(ELFLinkingContext &ctx, TargetLayout<ELFT> &layout)
-      : OutputELFWriter<ELFT>(ctx, layout),
-        _runtimeFile(new RuntimeFile<ELFT>(ctx, "C runtime")) {}
+      : OutputELFWriter<ELFT>(ctx, layout) {}
 
 protected:
   void buildDynamicSymbolTable(const File &file) override;
-  void addDefaultAtoms() override;
   void createImplicitFiles(std::vector<std::unique_ptr<File>> &) override;
   void finalizeDefaultAtomValues() override;
-
-protected:
-  std::unique_ptr<RuntimeFile<ELFT> > _runtimeFile;
 };
 
 //===----------------------------------------------------------------------===//
@@ -59,19 +54,15 @@ void DynamicLibraryWriter<ELFT>::buildDy
   OutputELFWriter<ELFT>::buildDynamicSymbolTable(file);
 }
 
-template <class ELFT> void DynamicLibraryWriter<ELFT>::addDefaultAtoms() {
-  OutputELFWriter<ELFT>::addDefaultAtoms();
-  _runtimeFile->addAbsoluteAtom("_end");
-}
-
 /// \brief Hook in lld to add CRuntime file
 template <class ELFT>
 void DynamicLibraryWriter<ELFT>::createImplicitFiles(
     std::vector<std::unique_ptr<File> > &result) {
-  // Add the default atoms as defined by executables
-  DynamicLibraryWriter<ELFT>::addDefaultAtoms();
   OutputELFWriter<ELFT>::createImplicitFiles(result);
-  result.push_back(std::move(_runtimeFile));
+  // Add the default atoms as defined by executables
+  auto file = llvm::make_unique<RuntimeFile<ELFT>>(this->_ctx, "C runtime");
+  file->addAbsoluteAtom("_end");
+  result.push_back(std::move(file));
 }
 
 template <class ELFT>

Modified: lld/trunk/lib/ReaderWriter/ELF/ExecutableWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/ExecutableWriter.h?rev=234347&r1=234346&r2=234347&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/ExecutableWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/ExecutableWriter.h Tue Apr  7 15:11:38 2015
@@ -23,12 +23,10 @@ template<class ELFT>
 class ExecutableWriter : public OutputELFWriter<ELFT> {
 public:
   ExecutableWriter(ELFLinkingContext &ctx, TargetLayout<ELFT> &layout)
-      : OutputELFWriter<ELFT>(ctx, layout),
-        _runtimeFile(new RuntimeFile<ELFT>(ctx, "C runtime")) {}
+      : OutputELFWriter<ELFT>(ctx, layout) {}
 
 protected:
   void buildDynamicSymbolTable(const File &file) override;
-  void addDefaultAtoms() override;
   void createImplicitFiles(std::vector<std::unique_ptr<File>> &) override;
   void finalizeDefaultAtomValues() override;
   void createDefaultSections() override;
@@ -38,7 +36,9 @@ protected:
   }
 
   unique_bump_ptr<InterpSection<ELFT>> _interpSection;
-  std::unique_ptr<RuntimeFile<ELFT> > _runtimeFile;
+
+private:
+  std::unique_ptr<RuntimeFile<ELFT>> createRuntimeFile();
 };
 
 //===----------------------------------------------------------------------===//
@@ -73,39 +73,36 @@ void ExecutableWriter<ELFT>::buildDynami
   OutputELFWriter<ELFT>::buildDynamicSymbolTable(file);
 }
 
-/// \brief Add absolute symbols by default. These are linker added
-/// absolute symbols
 template<class ELFT>
-void ExecutableWriter<ELFT>::addDefaultAtoms() {
-  OutputELFWriter<ELFT>::addDefaultAtoms();
-  _runtimeFile->addUndefinedAtom(this->_ctx.entrySymbolName());
-  _runtimeFile->addAbsoluteAtom("__bss_start");
-  _runtimeFile->addAbsoluteAtom("__bss_end");
-  _runtimeFile->addAbsoluteAtom("_end");
-  _runtimeFile->addAbsoluteAtom("end");
-  _runtimeFile->addAbsoluteAtom("__preinit_array_start");
-  _runtimeFile->addAbsoluteAtom("__preinit_array_end");
-  _runtimeFile->addAbsoluteAtom("__init_array_start");
-  _runtimeFile->addAbsoluteAtom("__init_array_end");
+std::unique_ptr<RuntimeFile<ELFT>> ExecutableWriter<ELFT>::createRuntimeFile() {
+  auto file = llvm::make_unique<RuntimeFile<ELFT>>(this->_ctx, "C runtime");
+  file->addUndefinedAtom(this->_ctx.entrySymbolName());
+  file->addAbsoluteAtom("__bss_start");
+  file->addAbsoluteAtom("__bss_end");
+  file->addAbsoluteAtom("_end");
+  file->addAbsoluteAtom("end");
+  file->addAbsoluteAtom("__preinit_array_start");
+  file->addAbsoluteAtom("__preinit_array_end");
+  file->addAbsoluteAtom("__init_array_start");
+  file->addAbsoluteAtom("__init_array_end");
   if (this->_ctx.isRelaOutputFormat()) {
-    _runtimeFile->addAbsoluteAtom("__rela_iplt_start");
-    _runtimeFile->addAbsoluteAtom("__rela_iplt_end");
+    file->addAbsoluteAtom("__rela_iplt_start");
+    file->addAbsoluteAtom("__rela_iplt_end");
   } else {
-    _runtimeFile->addAbsoluteAtom("__rel_iplt_start");
-    _runtimeFile->addAbsoluteAtom("__rel_iplt_end");
+    file->addAbsoluteAtom("__rel_iplt_start");
+    file->addAbsoluteAtom("__rel_iplt_end");
   }
-  _runtimeFile->addAbsoluteAtom("__fini_array_start");
-  _runtimeFile->addAbsoluteAtom("__fini_array_end");
+  file->addAbsoluteAtom("__fini_array_start");
+  file->addAbsoluteAtom("__fini_array_end");
+  return file;
 }
 
 /// \brief Hook in lld to add CRuntime file
 template <class ELFT>
 void ExecutableWriter<ELFT>::createImplicitFiles(
     std::vector<std::unique_ptr<File> > &result) {
-  // Add the default atoms as defined by executables
-  ExecutableWriter<ELFT>::addDefaultAtoms();
   OutputELFWriter<ELFT>::createImplicitFiles(result);
-  result.push_back(std::move(_runtimeFile));
+  result.push_back(createRuntimeFile());
 }
 
 template <class ELFT> void ExecutableWriter<ELFT>::createDefaultSections() {

Modified: lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonDynamicLibraryWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonDynamicLibraryWriter.h?rev=234347&r1=234346&r2=234347&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonDynamicLibraryWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonDynamicLibraryWriter.h Tue Apr  7 15:11:38 2015
@@ -37,29 +37,25 @@ protected:
   }
 
 private:
-  void addDefaultAtoms() override {
-    _runtimeFile->addAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
-    _runtimeFile->addAbsoluteAtom("_DYNAMIC");
-  }
-
   HexagonLinkingContext &_ctx;
   HexagonTargetLayout<ELFT> &_targetLayout;
-  std::unique_ptr<HexagonRuntimeFile<ELFT>> _runtimeFile;
 };
 
 template <class ELFT>
 HexagonDynamicLibraryWriter<ELFT>::HexagonDynamicLibraryWriter(
     HexagonLinkingContext &ctx, HexagonTargetLayout<ELFT> &layout)
-    : DynamicLibraryWriter<ELFT>(ctx, layout), _ctx(ctx), _targetLayout(layout),
-      _runtimeFile(new HexagonRuntimeFile<ELFT>(ctx)) {}
+    : DynamicLibraryWriter<ELFT>(ctx, layout), _ctx(ctx),
+      _targetLayout(layout) {}
 
 template <class ELFT>
 void HexagonDynamicLibraryWriter<ELFT>::createImplicitFiles(
     std::vector<std::unique_ptr<File>> &result) {
   DynamicLibraryWriter<ELFT>::createImplicitFiles(result);
   // Add the default atoms as defined for hexagon
-  addDefaultAtoms();
-  result.push_back(std::move(_runtimeFile));
+  auto file = llvm::make_unique<HexagonRuntimeFile<ELFT>>(_ctx);
+  file->addAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
+  file->addAbsoluteAtom("_DYNAMIC");
+  result.push_back(std::move(file));
 }
 
 template <class ELFT>

Modified: lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonExecutableWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonExecutableWriter.h?rev=234347&r1=234346&r2=234347&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonExecutableWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonExecutableWriter.h Tue Apr  7 15:11:38 2015
@@ -37,32 +37,27 @@ protected:
   }
 
 private:
-  void addDefaultAtoms() override {
-    _runtimeFile->addAbsoluteAtom("_SDA_BASE_");
-    if (this->_ctx.isDynamic()) {
-      _runtimeFile->addAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
-      _runtimeFile->addAbsoluteAtom("_DYNAMIC");
-    }
-  }
-
   HexagonLinkingContext &_ctx;
   HexagonTargetLayout<ELFT> &_targetLayout;
-  std::unique_ptr<HexagonRuntimeFile<ELFT>> _runtimeFile;
 };
 
 template <class ELFT>
 HexagonExecutableWriter<ELFT>::HexagonExecutableWriter(
     HexagonLinkingContext &ctx, HexagonTargetLayout<ELFT> &layout)
-    : ExecutableWriter<ELFT>(ctx, layout), _ctx(ctx), _targetLayout(layout),
-      _runtimeFile(new HexagonRuntimeFile<ELFT>(ctx)) {}
+    : ExecutableWriter<ELFT>(ctx, layout), _ctx(ctx), _targetLayout(layout) {}
 
 template <class ELFT>
 void HexagonExecutableWriter<ELFT>::createImplicitFiles(
     std::vector<std::unique_ptr<File>> &result) {
   ExecutableWriter<ELFT>::createImplicitFiles(result);
   // Add the default atoms as defined for hexagon
-  addDefaultAtoms();
-  result.push_back(std::move(_runtimeFile));
+  auto file = llvm::make_unique<HexagonRuntimeFile<ELFT>>(_ctx);
+  file->addAbsoluteAtom("_SDA_BASE_");
+  if (this->_ctx.isDynamic()) {
+    file->addAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
+    file->addAbsoluteAtom("_DYNAMIC");
+  }
+  result.push_back(std::move(file));
 }
 
 template <class ELFT>

Modified: lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h?rev=234347&r1=234346&r2=234347&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h Tue Apr  7 15:11:38 2015
@@ -129,9 +129,6 @@ protected:
   // section header table, string table etc
   virtual void assignSectionsWithNoSegments();
 
-  // Add default atoms that need to be present in the output file
-  virtual void addDefaultAtoms();
-
   // Add any runtime files and their atoms to the output
   void createImplicitFiles(std::vector<std::unique_ptr<File>> &) override;
 
@@ -194,7 +191,6 @@ protected:
   unique_bump_ptr<HashSection<ELFT>> _hashTable;
   llvm::StringSet<> _soNeeded;
   /// @}
-  std::unique_ptr<RuntimeFile<ELFT>> _scriptFile;
 
 private:
   static StringRef maybeGetSOName(Node *node);
@@ -206,8 +202,7 @@ private:
 template <class ELFT>
 OutputELFWriter<ELFT>::OutputELFWriter(ELFLinkingContext &ctx,
                                        TargetLayout<ELFT> &layout)
-    : _ctx(ctx), _targetHandler(ctx.getTargetHandler()), _layout(layout),
-      _scriptFile(new RuntimeFile<ELFT>(ctx, "Linker script runtime")) {}
+    : _ctx(ctx), _targetHandler(ctx.getTargetHandler()), _layout(layout) {}
 
 template <class ELFT>
 void OutputELFWriter<ELFT>::buildChunks(const File &file) {
@@ -355,13 +350,6 @@ void OutputELFWriter<ELFT>::assignSectio
         _shdrtab->updateSection(section);
 }
 
-template <class ELFT> void OutputELFWriter<ELFT>::addDefaultAtoms() {
-  const llvm::StringSet<> &symbols =
-      _ctx.linkerScriptSema().getScriptDefinedSymbols();
-  for (auto &sym : symbols)
-    _scriptFile->addAbsoluteAtom(sym.getKey());
-}
-
 template <class ELFT>
 void OutputELFWriter<ELFT>::createImplicitFiles(
     std::vector<std::unique_ptr<File>> &result) {
@@ -373,7 +361,11 @@ void OutputELFWriter<ELFT>::createImplic
   _ctx.setUndefinesResolver(
       llvm::make_unique<DynamicSymbolFile<ELFT>>(_ctx, std::move(callback)));
   // Add script defined symbols
-  result.push_back(std::move(_scriptFile));
+  auto file =
+      llvm::make_unique<RuntimeFile<ELFT>>(_ctx, "Linker script runtime");
+  for (auto &sym : this->_ctx.linkerScriptSema().getScriptDefinedSymbols())
+    file->addAbsoluteAtom(sym.getKey());
+  result.push_back(std::move(file));
 }
 
 template <class ELFT>

Modified: lld/trunk/lib/ReaderWriter/ELF/X86/X86DynamicLibraryWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/X86/X86DynamicLibraryWriter.h?rev=234347&r1=234346&r2=234347&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/X86/X86DynamicLibraryWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/X86/X86DynamicLibraryWriter.h Tue Apr  7 15:11:38 2015
@@ -28,10 +28,6 @@ protected:
     return DynamicLibraryWriter<ELFT>::finalizeDefaultAtomValues();
   }
 
-  void addDefaultAtoms() override {
-    return DynamicLibraryWriter<ELFT>::addDefaultAtoms();
-  }
-
 private:
   class GOTFile : public SimpleFile {
   public:

Modified: lld/trunk/lib/ReaderWriter/ELF/X86/X86ExecutableWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/X86/X86ExecutableWriter.h?rev=234347&r1=234346&r2=234347&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/X86/X86ExecutableWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/X86/X86ExecutableWriter.h Tue Apr  7 15:11:38 2015
@@ -28,10 +28,6 @@ protected:
     return ExecutableWriter<ELFT>::finalizeDefaultAtomValues();
   }
 
-  void addDefaultAtoms() override {
-    return ExecutableWriter<ELFT>::addDefaultAtoms();
-  }
-
 private:
   X86LinkingContext &_ctx;
   TargetLayout<ELFT> &_layout;

Modified: lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64DynamicLibraryWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64DynamicLibraryWriter.h?rev=234347&r1=234346&r2=234347&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64DynamicLibraryWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64DynamicLibraryWriter.h Tue Apr  7 15:11:38 2015
@@ -30,10 +30,6 @@ protected:
     return DynamicLibraryWriter::finalizeDefaultAtomValues();
   }
 
-  void addDefaultAtoms() override {
-    return DynamicLibraryWriter::addDefaultAtoms();
-  }
-
 private:
   class GOTFile : public SimpleFile {
   public:

Modified: lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64ExecutableWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64ExecutableWriter.h?rev=234347&r1=234346&r2=234347&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64ExecutableWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64ExecutableWriter.h Tue Apr  7 15:11:38 2015
@@ -37,10 +37,6 @@ protected:
     return ExecutableWriter::finalizeDefaultAtomValues();
   }
 
-  void addDefaultAtoms() override {
-    return ExecutableWriter::addDefaultAtoms();
-  }
-
 private:
   class GOTFile : public SimpleFile {
   public:





More information about the llvm-commits mailing list