[lld] r266666 - Refactor LinkerScript::assignAddresses. NFC.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 18 14:00:41 PDT 2016


Author: ruiu
Date: Mon Apr 18 16:00:40 2016
New Revision: 266666

URL: http://llvm.org/viewvc/llvm-project?rev=266666&view=rev
Log:
Refactor LinkerScript::assignAddresses. NFC.

Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/LinkerScript.h

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=266666&r1=266665&r2=266666&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Mon Apr 18 16:00:40 2016
@@ -90,30 +90,33 @@ template <class ELFT> bool LinkerScript:
   return R && R->Keep;
 }
 
-// This method finalizes the Locations list. Adds neccesary locations for
-// orphan sections, what prepares it for futher use without
-// changes in LinkerScript::assignAddresses().
 template <class ELFT>
-void LinkerScript::fixupLocations(std::vector<OutputSectionBase<ELFT> *> &S) {
+static OutputSectionBase<ELFT> *
+findSection(std::vector<OutputSectionBase<ELFT> *> &V, StringRef Name) {
+  for (OutputSectionBase<ELFT> *Sec : V)
+    if (Sec->getName() == Name)
+      return Sec;
+  return nullptr;
+}
+
+template <class ELFT>
+void LinkerScript::assignAddresses(
+    std::vector<OutputSectionBase<ELFT> *> &Sections) {
+  typedef typename ELFT::uint uintX_t;
+
   // Orphan sections are sections present in the input files which
-  // are not explicitly placed into the output file by the linker
-  // script. We place orphan sections at end of file. Other linkers places
-  // them using some heuristics as described in
+  // are not explicitly placed into the output file by the linker script.
+  // We place orphan sections at end of file.
+  // Other linkers places them using some heuristics as described in
   // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
-  for (OutputSectionBase<ELFT> *Sec : S) {
+  for (OutputSectionBase<ELFT> *Sec : Sections) {
     StringRef Name = Sec->getName();
     auto I = std::find(SectionOrder.begin(), SectionOrder.end(), Name);
     if (I == SectionOrder.end())
       Locations.push_back({Command::Section, {}, {Name}});
   }
-}
-
-template <class ELFT>
-void LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELFT> *> &S) {
-  typedef typename ELFT::uint uintX_t;
-
-  Script->fixupLocations(S);
 
+  // Assign addresses as instructed by linker script SECTIONS sub-commands.
   uintX_t ThreadBssOffset = 0;
   uintX_t VA =
       Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
@@ -124,25 +127,20 @@ void LinkerScript::assignAddresses(std::
       continue;
     }
 
-    auto I =
-        std::find_if(S.begin(), S.end(), [&](OutputSectionBase<ELFT> *Sec) {
-          return Sec->getName() == Node.SectionName;
-        });
-    if (I == S.end())
+    OutputSectionBase<ELFT> *Sec = findSection(Sections, Node.SectionName);
+    if (!Sec)
       continue;
 
-    OutputSectionBase<ELFT> *Sec = *I;
-    uintX_t Align = Sec->getAlign();
     if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
       uintX_t TVA = VA + ThreadBssOffset;
-      TVA = alignTo(TVA, Align);
+      TVA = alignTo(TVA, Sec->getAlign());
       Sec->setVA(TVA);
       ThreadBssOffset = TVA - VA + Sec->getSize();
       continue;
     }
 
     if (Sec->getFlags() & SHF_ALLOC) {
-      VA = alignTo(VA, Align);
+      VA = alignTo(VA, Sec->getAlign());
       Sec->setVA(VA);
       VA += Sec->getSize();
       continue;

Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=266666&r1=266665&r2=266666&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Mon Apr 18 16:00:40 2016
@@ -72,8 +72,6 @@ public:
   bool DoLayout = false;
 
 private:
-  template <class ELFT>
-  void fixupLocations(std::vector<OutputSectionBase<ELFT> *> &);
   uint64_t evaluate(std::vector<StringRef> &Tokens, uint64_t LocCounter);
   template <class ELFT> SectionRule *find(InputSectionBase<ELFT> *S);
 




More information about the llvm-commits mailing list