[lld] r285766 - Split writeResult. NFC.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 1 16:17:45 PDT 2016


Author: ruiu
Date: Tue Nov  1 18:17:45 2016
New Revision: 285766

URL: http://llvm.org/viewvc/llvm-project?rev=285766&view=rev
Log:
Split writeResult. NFC.

This is now doable because this code doesn't have to be in the
dynamic scope of Writer::run().

Modified:
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=285766&r1=285765&r2=285766&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Nov  1 18:17:45 2016
@@ -49,6 +49,7 @@ public:
 private:
   typedef PhdrEntry<ELFT> Phdr;
 
+  void createSyntheticSections();
   void copyLocalSymbols();
   void addReservedSymbols();
   void addInputSec(InputSectionBase<ELFT> *S);
@@ -126,83 +127,6 @@ template <class ELFT> static bool needsI
 }
 
 template <class ELFT> void elf::writeResult() {
-  typedef typename ELFT::uint uintX_t;
-  typedef typename ELFT::Ehdr Elf_Ehdr;
-
-  // Initialize all pointers with NULL.
-  memset(&Out<ELFT>::First, 0, sizeof(Out<ELFT>));
-
-  // Create singleton output sections.
-  Out<ELFT>::Bss =
-      make<OutputSection<ELFT>>(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
-  Out<ELFT>::Dynamic = make<DynamicSection<ELFT>>();
-  Out<ELFT>::EhFrame = make<EhOutputSection<ELFT>>();
-  Out<ELFT>::Got = make<GotSection<ELFT>>();
-  Out<ELFT>::Plt = make<PltSection<ELFT>>();
-  Out<ELFT>::RelaDyn = make<RelocationSection<ELFT>>(
-      Config->Rela ? ".rela.dyn" : ".rel.dyn", Config->ZCombreloc);
-  Out<ELFT>::ShStrTab = make<StringTableSection<ELFT>>(".shstrtab", false);
-  Out<ELFT>::VerSym = make<VersionTableSection<ELFT>>();
-  Out<ELFT>::VerNeed = make<VersionNeedSection<ELFT>>();
-
-  Out<ELFT>::ElfHeader = make<OutputSectionBase<ELFT>>("", 0, SHF_ALLOC);
-  Out<ELFT>::ElfHeader->setSize(sizeof(Elf_Ehdr));
-  Out<ELFT>::ProgramHeaders = make<OutputSectionBase<ELFT>>("", 0, SHF_ALLOC);
-  Out<ELFT>::ProgramHeaders->updateAlignment(sizeof(uintX_t));
-
-  if (needsInterpSection<ELFT>())
-    Out<ELFT>::Interp = make<InterpSection<ELFT>>();
-
-  if (!Symtab<ELFT>::X->getSharedFiles().empty() || Config->Pic) {
-    Out<ELFT>::DynStrTab = make<StringTableSection<ELFT>>(".dynstr", true);
-    Out<ELFT>::DynSymTab =
-        make<SymbolTableSection<ELFT>>(*Out<ELFT>::DynStrTab);
-  }
-
-  if (Config->EhFrameHdr)
-    Out<ELFT>::EhFrameHdr = make<EhFrameHeader<ELFT>>();
-
-  if (Config->GnuHash)
-    Out<ELFT>::GnuHashTab = make<GnuHashTableSection<ELFT>>();
-  if (Config->SysvHash)
-    Out<ELFT>::HashTab = make<HashTableSection<ELFT>>();
-  if (Config->GdbIndex)
-    Out<ELFT>::GdbIndex = make<GdbIndexSection<ELFT>>();
-
-  StringRef S = Config->Rela ? ".rela.plt" : ".rel.plt";
-  Out<ELFT>::GotPlt = make<GotPltSection<ELFT>>();
-  Out<ELFT>::RelaPlt = make<RelocationSection<ELFT>>(S, false /*Sort*/);
-  if (Config->Strip != StripPolicy::All) {
-    Out<ELFT>::StrTab = make<StringTableSection<ELFT>>(".strtab", false);
-    Out<ELFT>::SymTab = make<SymbolTableSection<ELFT>>(*Out<ELFT>::StrTab);
-  }
-
-  if (Config->EMachine == EM_MIPS && !Config->Shared) {
-    // This is a MIPS specific section to hold a space within the data segment
-    // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
-    // See "Dynamic section" in Chapter 5 in the following document:
-    // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
-    Out<ELFT>::MipsRldMap = make<OutputSection<ELFT>>(".rld_map", SHT_PROGBITS,
-                                                      SHF_ALLOC | SHF_WRITE);
-    Out<ELFT>::MipsRldMap->setSize(sizeof(uintX_t));
-    Out<ELFT>::MipsRldMap->updateAlignment(sizeof(uintX_t));
-  }
-  if (!Config->VersionDefinitions.empty())
-    Out<ELFT>::VerDef = make<VersionDefinitionSection<ELFT>>();
-
-  // Initialize linker generated sections
-  if (Config->BuildId == BuildIdKind::Fast)
-    In<ELFT>::BuildId = make<BuildIdFastHash<ELFT>>();
-  else if (Config->BuildId == BuildIdKind::Md5)
-    In<ELFT>::BuildId = make<BuildIdMd5<ELFT>>();
-  else if (Config->BuildId == BuildIdKind::Sha1)
-    In<ELFT>::BuildId = make<BuildIdSha1<ELFT>>();
-  else if (Config->BuildId == BuildIdKind::Uuid)
-    In<ELFT>::BuildId = make<BuildIdUuid<ELFT>>();
-  else if (Config->BuildId == BuildIdKind::Hexstring)
-    In<ELFT>::BuildId = make<BuildIdHexstring<ELFT>>();
-  In<ELFT>::Sections = {In<ELFT>::BuildId};
-
   Writer<ELFT>().run();
 }
 
@@ -216,6 +140,7 @@ template <class ELFT> static std::vector
 
 // The main function of the writer.
 template <class ELFT> void Writer<ELFT>::run() {
+  createSyntheticSections();
   addReservedSymbols();
 
   if (Target->NeedsThunks)
@@ -284,6 +209,84 @@ template <class ELFT> void Writer<ELFT>:
   }
 }
 
+// Initialize Out<ELFT> members.
+template <class ELFT> void Writer<ELFT>::createSyntheticSections() {
+  // Initialize all pointers with NULL. This is needed because
+  // you can call lld::elf::main more than once as a library.
+  memset(&Out<ELFT>::First, 0, sizeof(Out<ELFT>));
+
+  // Create singleton output sections.
+  Out<ELFT>::Bss =
+      make<OutputSection<ELFT>>(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
+  Out<ELFT>::Dynamic = make<DynamicSection<ELFT>>();
+  Out<ELFT>::EhFrame = make<EhOutputSection<ELFT>>();
+  Out<ELFT>::Got = make<GotSection<ELFT>>();
+  Out<ELFT>::Plt = make<PltSection<ELFT>>();
+  Out<ELFT>::RelaDyn = make<RelocationSection<ELFT>>(
+      Config->Rela ? ".rela.dyn" : ".rel.dyn", Config->ZCombreloc);
+  Out<ELFT>::ShStrTab = make<StringTableSection<ELFT>>(".shstrtab", false);
+  Out<ELFT>::VerSym = make<VersionTableSection<ELFT>>();
+  Out<ELFT>::VerNeed = make<VersionNeedSection<ELFT>>();
+
+  Out<ELFT>::ElfHeader = make<OutputSectionBase<ELFT>>("", 0, SHF_ALLOC);
+  Out<ELFT>::ElfHeader->setSize(sizeof(Elf_Ehdr));
+  Out<ELFT>::ProgramHeaders = make<OutputSectionBase<ELFT>>("", 0, SHF_ALLOC);
+  Out<ELFT>::ProgramHeaders->updateAlignment(sizeof(uintX_t));
+
+  if (needsInterpSection<ELFT>())
+    Out<ELFT>::Interp = make<InterpSection<ELFT>>();
+
+  if (!Symtab<ELFT>::X->getSharedFiles().empty() || Config->Pic) {
+    Out<ELFT>::DynStrTab = make<StringTableSection<ELFT>>(".dynstr", true);
+    Out<ELFT>::DynSymTab =
+        make<SymbolTableSection<ELFT>>(*Out<ELFT>::DynStrTab);
+  }
+
+  if (Config->EhFrameHdr)
+    Out<ELFT>::EhFrameHdr = make<EhFrameHeader<ELFT>>();
+
+  if (Config->GnuHash)
+    Out<ELFT>::GnuHashTab = make<GnuHashTableSection<ELFT>>();
+  if (Config->SysvHash)
+    Out<ELFT>::HashTab = make<HashTableSection<ELFT>>();
+  if (Config->GdbIndex)
+    Out<ELFT>::GdbIndex = make<GdbIndexSection<ELFT>>();
+
+  StringRef S = Config->Rela ? ".rela.plt" : ".rel.plt";
+  Out<ELFT>::GotPlt = make<GotPltSection<ELFT>>();
+  Out<ELFT>::RelaPlt = make<RelocationSection<ELFT>>(S, false /*Sort*/);
+  if (Config->Strip != StripPolicy::All) {
+    Out<ELFT>::StrTab = make<StringTableSection<ELFT>>(".strtab", false);
+    Out<ELFT>::SymTab = make<SymbolTableSection<ELFT>>(*Out<ELFT>::StrTab);
+  }
+
+  if (Config->EMachine == EM_MIPS && !Config->Shared) {
+    // This is a MIPS specific section to hold a space within the data segment
+    // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
+    // See "Dynamic section" in Chapter 5 in the following document:
+    // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
+    Out<ELFT>::MipsRldMap = make<OutputSection<ELFT>>(".rld_map", SHT_PROGBITS,
+                                                      SHF_ALLOC | SHF_WRITE);
+    Out<ELFT>::MipsRldMap->setSize(sizeof(uintX_t));
+    Out<ELFT>::MipsRldMap->updateAlignment(sizeof(uintX_t));
+  }
+  if (!Config->VersionDefinitions.empty())
+    Out<ELFT>::VerDef = make<VersionDefinitionSection<ELFT>>();
+
+  // Initialize linker generated sections
+  if (Config->BuildId == BuildIdKind::Fast)
+    In<ELFT>::BuildId = make<BuildIdFastHash<ELFT>>();
+  else if (Config->BuildId == BuildIdKind::Md5)
+    In<ELFT>::BuildId = make<BuildIdMd5<ELFT>>();
+  else if (Config->BuildId == BuildIdKind::Sha1)
+    In<ELFT>::BuildId = make<BuildIdSha1<ELFT>>();
+  else if (Config->BuildId == BuildIdKind::Uuid)
+    In<ELFT>::BuildId = make<BuildIdUuid<ELFT>>();
+  else if (Config->BuildId == BuildIdKind::Hexstring)
+    In<ELFT>::BuildId = make<BuildIdHexstring<ELFT>>();
+  In<ELFT>::Sections = {In<ELFT>::BuildId};
+}
+
 template <class ELFT>
 static bool shouldKeepInSymtab(InputSectionBase<ELFT> *Sec, StringRef SymName,
                                const SymbolBody &B) {




More information about the llvm-commits mailing list