[lld] r256411 - Split Writer::createSections().

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 24 23:38:59 PST 2015


Author: ruiu
Date: Fri Dec 25 01:38:58 2015
New Revision: 256411

URL: http://llvm.org/viewvc/llvm-project?rev=256411&view=rev
Log:
Split Writer::createSections().

This function was longer than 250 lines, which is way too long
in my own standard. This patch reduces the size. It is still
too long, but this patch should be toward the right direction.

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=256411&r1=256410&r2=256411&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Fri Dec 25 01:38:58 2015
@@ -43,9 +43,15 @@ public:
 private:
   void copyLocalSymbols();
   void createSections();
+
+  OutputSectionBase<ELFT> *createOutputSection(InputSectionBase<ELFT> *C,
+                                               StringRef Name, uintX_t Type,
+                                               uintX_t Flags);
+
   template <bool isRela>
   void scanRelocs(InputSectionBase<ELFT> &C,
                   iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels);
+
   void scanRelocs(InputSection<ELFT> &C);
   void scanRelocs(InputSectionBase<ELFT> &S, const Elf_Shdr &RelSec);
   void updateRelro(Elf_Phdr *Cur, Elf_Phdr *GnuRelroPhdr, uintX_t VA);
@@ -613,6 +619,23 @@ static bool includeInDynamicSymtab(const
   return B.isUsedInDynamicReloc();
 }
 
+template <class ELFT>
+OutputSectionBase<ELFT> *
+Writer<ELFT>::createOutputSection(InputSectionBase<ELFT> *C, StringRef Name,
+                                  uintX_t Type, uintX_t Flags) {
+  switch (C->SectionKind) {
+  case InputSectionBase<ELFT>::Regular:
+    return new (SecAlloc.Allocate()) OutputSection<ELFT>(Name, Type, Flags);
+  case InputSectionBase<ELFT>::EHFrame:
+    return new (EHSecAlloc.Allocate()) EHOutputSection<ELFT>(Name, Type, Flags);
+  case InputSectionBase<ELFT>::Merge:
+    return new (MSecAlloc.Allocate())
+        MergeOutputSection<ELFT>(Name, Type, Flags);
+  case InputSectionBase<ELFT>::MipsReginfo:
+    return new (MReginfoSecAlloc.Allocate()) MipsReginfoOutputSection<ELFT>();
+  }
+}
+
 // Create output section objects and add them to OutputSections.
 template <class ELFT> void Writer<ELFT>::createSections() {
   // .interp needs to be on the first page in the output file.
@@ -634,8 +657,7 @@ template <class ELFT> void Writer<ELFT>:
       // For SHF_MERGE we create different output sections for each sh_entsize.
       // This makes each output section simple and keeps a single level
       // mapping from input to output.
-      typename InputSectionBase<ELFT>::Kind K = C->SectionKind;
-      uintX_t EntSize = K != InputSectionBase<ELFT>::Merge ? 0 : H->sh_entsize;
+      uintX_t EntSize = isa<MergeInputSection<ELFT>>(C) ? H->sh_entsize : 0;
       uint32_t OutType = H->sh_type;
       if (OutType == SHT_PROGBITS && C->getSectionName() == ".eh_frame" &&
           Config->EMachine == EM_X86_64)
@@ -644,28 +666,11 @@ template <class ELFT> void Writer<ELFT>:
                                      OutType, OutFlags, EntSize};
       OutputSectionBase<ELFT> *&Sec = Map[Key];
       if (!Sec) {
-        switch (K) {
-        case InputSectionBase<ELFT>::Regular:
-          Sec = new (SecAlloc.Allocate())
-              OutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
-          break;
-        case InputSectionBase<ELFT>::EHFrame:
-          Sec = new (EHSecAlloc.Allocate())
-              EHOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
-          break;
-        case InputSectionBase<ELFT>::Merge:
-          Sec = new (MSecAlloc.Allocate())
-              MergeOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
-          break;
-        case InputSectionBase<ELFT>::MipsReginfo:
-          Sec = new (MReginfoSecAlloc.Allocate())
-              MipsReginfoOutputSection<ELFT>();
-          break;
-        }
+        Sec = createOutputSection(C, Key.Name, Key.Type, Key.Flags);
         OutputSections.push_back(Sec);
         RegularSections.push_back(Sec);
       }
-      switch (K) {
+      switch (C->SectionKind) {
       case InputSectionBase<ELFT>::Regular:
         static_cast<OutputSection<ELFT> *>(Sec)
             ->addSection(cast<InputSection<ELFT>>(C));




More information about the llvm-commits mailing list