[lld] r249118 - ELF2: Merge duplicates using lambdas. NFC.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 1 18:33:32 PDT 2015


Author: ruiu
Date: Thu Oct  1 20:33:31 2015
New Revision: 249118

URL: http://llvm.org/viewvc/llvm-project?rev=249118&view=rev
Log:
ELF2: Merge duplicates using lambdas. NFC.

Modified:
    lld/trunk/ELF/OutputSections.cpp

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=249118&r1=249117&r2=249118&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Thu Oct  1 20:33:31 2015
@@ -227,64 +227,44 @@ template <class ELFT> void DynamicSectio
       Elf_Dyn;
   auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
 
-  if (RelaDynSec.hasRelocs()) {
-    bool IsRela = RelaDynSec.isRela();
-    P->d_tag = IsRela ? DT_RELA : DT_REL;
-    P->d_un.d_ptr = RelaDynSec.getVA();
+  auto WritePtr = [&](int32_t Tag, uint64_t Val) {
+    P->d_tag = Tag;
+    P->d_un.d_ptr = Val;
     ++P;
+  };
 
-    P->d_tag = IsRela ? DT_RELASZ : DT_RELSZ;
-    P->d_un.d_val = RelaDynSec.getSize();
+  auto WriteVal = [&](int32_t Tag, uint32_t Val) {
+    P->d_tag = Tag;
+    P->d_un.d_val = Val;
     ++P;
+  };
 
-    P->d_tag = IsRela ? DT_RELAENT : DT_RELENT;
-    P->d_un.d_val = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
-    ++P;
+  if (RelaDynSec.hasRelocs()) {
+    bool IsRela = RelaDynSec.isRela();
+    WritePtr(IsRela ? DT_RELA : DT_REL, RelaDynSec.getVA());
+    WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, RelaDynSec.getSize());
+    WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
+             IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
   }
 
-  P->d_tag = DT_SYMTAB;
-  P->d_un.d_ptr = DynSymSec.getVA();
-  ++P;
-
-  P->d_tag = DT_SYMENT;
-  P->d_un.d_ptr = sizeof(Elf_Sym);
-  ++P;
-
-  P->d_tag = DT_STRTAB;
-  P->d_un.d_ptr = DynStrSec.getVA();
-  ++P;
-
-  P->d_tag = DT_STRSZ;
-  P->d_un.d_val = DynStrSec.data().size();
-  ++P;
-
-  P->d_tag = DT_HASH;
-  P->d_un.d_ptr = HashSec.getVA();
-  ++P;
-
-  if (!Config->RPath.empty()) {
-    P->d_tag = DT_RUNPATH;
-    P->d_un.d_val = DynStrSec.getFileOff(Config->RPath);
-    ++P;
-  }
+  WritePtr(DT_SYMTAB, DynSymSec.getVA());
+  WritePtr(DT_SYMENT, sizeof(Elf_Sym));
+  WritePtr(DT_STRTAB, DynStrSec.getVA());
+  WriteVal(DT_STRSZ, DynStrSec.data().size());
+  WritePtr(DT_HASH, HashSec.getVA());
 
-  if (!Config->SoName.empty()) {
-    P->d_tag = DT_SONAME;
-    P->d_un.d_val = DynStrSec.getFileOff(Config->SoName);
-    ++P;
-  }
+  if (!Config->RPath.empty())
+    WriteVal(DT_RUNPATH, DynStrSec.getFileOff(Config->RPath));
+
+  if (!Config->SoName.empty())
+    WriteVal(DT_SONAME, DynStrSec.getFileOff(Config->SoName));
 
   const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
       SymTab.getSharedFiles();
-  for (const std::unique_ptr<SharedFileBase> &File : SharedFiles) {
-    P->d_tag = DT_NEEDED;
-    P->d_un.d_val = DynStrSec.getFileOff(File->getSoName());
-    ++P;
-  }
+  for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
+    WriteVal(DT_NEEDED, DynStrSec.getFileOff(File->getSoName()));
 
-  P->d_tag = DT_NULL;
-  P->d_un.d_val = 0;
-  ++P;
+  WriteVal(DT_NULL, 0);
 }
 
 template <class ELFT>




More information about the llvm-commits mailing list