ELF: Make findAbsoluteAtom return AtomLayout* instead of an iterator.
authorRui Ueyama <ruiu@google.com>
Tue, 31 Mar 2015 22:37:59 +0000 (22:37 +0000)
committerRui Ueyama <ruiu@google.com>
Tue, 31 Mar 2015 22:37:59 +0000 (22:37 +0000)
All calls of findAbsoluteAtoms seem a bit awkward because of the type
of the function. It semantically returns a pointer to an AtomLayout or
nothing, so I made the function return AtomLayout*.

In this patch, I also expanded some "auto"s because their actual type
were not obvious in their contexts.

llvm-svn: 233769

lld/lib/ReaderWriter/ELF/ARM/ARMExecutableWriter.h
lld/lib/ReaderWriter/ELF/ARM/ARMTargetHandler.h
lld/lib/ReaderWriter/ELF/DefaultLayout.h
lld/lib/ReaderWriter/ELF/DynamicLibraryWriter.h
lld/lib/ReaderWriter/ELF/ExecutableWriter.h
lld/lib/ReaderWriter/ELF/Hexagon/HexagonExecutableWriter.h
lld/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h
lld/lib/ReaderWriter/ELF/Mips/MipsELFWriters.h
lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h
lld/lib/ReaderWriter/ELF/OutputELFWriter.h

index 401bc33d851359c4dc56f3fd08296416e9106e40..1fe7f29c876fed1e258f869d88edb6abfaa139c1 100644 (file)
@@ -66,9 +66,8 @@ template <class ELFT>
 void ARMExecutableWriter<ELFT>::finalizeDefaultAtomValues() {
   // Finalize the atom values that are part of the parent.
   ExecutableWriter<ELFT>::finalizeDefaultAtomValues();
-  auto gotAtomIter = _armLayout.findAbsoluteAtom(gotSymbol);
-  if (gotAtomIter != _armLayout.absoluteAtoms().end()) {
-    auto *gotAtom = *gotAtomIter;
+  AtomLayout *gotAtom = _armLayout.findAbsoluteAtom(gotSymbol);
+  if (gotAtom) {
     if (auto gotpltSection = _armLayout.findOutputSection(".got.plt"))
       gotAtom->_virtualAddr = gotpltSection->virtualAddr();
     else if (auto gotSection = _armLayout.findOutputSection(".got"))
index 47ae46eb80d7e22271fd4681e7db2ea46e3600cd..2ff8dc015929e644f96ff3e6b68eda3955cfd248 100644 (file)
@@ -27,10 +27,8 @@ public:
 
   uint64_t getGOTSymAddr() {
     if (!_gotSymAddr.hasValue()) {
-      auto gotAtomIter = this->findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
-      _gotSymAddr = (gotAtomIter != this->absoluteAtoms().end())
-                        ? (*gotAtomIter)->_virtualAddr
-                        : 0;
+      AtomLayout *gotAtom = this->findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
+      _gotSymAddr = gotAtom ? gotAtom->_virtualAddr : 0;
     }
     return *_gotSymAddr;
   }
index f9807a94e39dbc9a21e7c97935c8ad3e86777b02..24c625298e9213a8dd99a39f05d0ee9046abb1da 100644 (file)
@@ -204,10 +204,13 @@ public:
   }
 
   /// \brief find a absolute atom given a name
-  AbsoluteAtomIterT findAbsoluteAtom(StringRef name) {
-    return std::find_if(
+  lld::AtomLayout *findAbsoluteAtom(StringRef name) {
+    auto iter = std::find_if(
         _absoluteAtoms.begin(), _absoluteAtoms.end(),
         [=](const AtomLayout *a) { return a->_atom->name() == name; });
+    if (iter == _absoluteAtoms.end())
+      return nullptr;
+    return *iter;
   }
 
   // Output sections with the same name into a OutputSection
index 88211660200f28000449574494d6009b5b36aa65..2f2f0da7a9954796e80b45a6a8dc35af17bdab5d 100644 (file)
@@ -79,13 +79,14 @@ bool DynamicLibraryWriter<ELFT>::createImplicitFiles(
 
 template <class ELFT>
 void DynamicLibraryWriter<ELFT>::finalizeDefaultAtomValues() {
-  auto underScoreEndAtomIter = this->_layout.findAbsoluteAtom("_end");
+  lld::AtomLayout *underScoreEndAtom = this->_layout.findAbsoluteAtom("_end");
+  assert(underScoreEndAtom);
 
   if (auto bssSection = this->_layout.findOutputSection(".bss")) {
-    (*underScoreEndAtomIter)->_virtualAddr =
+    underScoreEndAtom->_virtualAddr =
         bssSection->virtualAddr() + bssSection->memSize();
   } else if (auto dataSection = this->_layout.findOutputSection(".data")) {
-    (*underScoreEndAtomIter)->_virtualAddr =
+    underScoreEndAtom->_virtualAddr =
         dataSection->virtualAddr() + dataSection->memSize();
   }
 }
index e014cbee59ef39215347e2f111b59815eeb4a079..b281b09c9c272006a780ffa829b9b0273fe4fa85 100644 (file)
@@ -126,23 +126,26 @@ template <class ELFT> void ExecutableWriter<ELFT>::createDefaultSections() {
 /// created
 template <class ELFT> void ExecutableWriter<ELFT>::finalizeDefaultAtomValues() {
   OutputELFWriter<ELFT>::finalizeDefaultAtomValues();
-  auto bssStartAtomIter = this->_layout.findAbsoluteAtom("__bss_start");
-  auto bssEndAtomIter = this->_layout.findAbsoluteAtom("__bss_end");
-  auto underScoreEndAtomIter = this->_layout.findAbsoluteAtom("_end");
-  auto endAtomIter = this->_layout.findAbsoluteAtom("end");
+  AtomLayout *bssStartAtom = this->_layout.findAbsoluteAtom("__bss_start");
+  AtomLayout *bssEndAtom = this->_layout.findAbsoluteAtom("__bss_end");
+  AtomLayout *underScoreEndAtom = this->_layout.findAbsoluteAtom("_end");
+  AtomLayout *endAtom = this->_layout.findAbsoluteAtom("end");
+
+  assert((bssStartAtom || bssEndAtom || underScoreEndAtom || endAtom) &&
+         "Unable to find the absolute atoms that have been added by lld");
 
   auto startEnd = [&](StringRef sym, StringRef sec) -> void {
     std::string start = ("__" + sym + "_start").str();
     std::string end = ("__" + sym + "_end").str();
-    auto s = this->_layout.findAbsoluteAtom(start);
-    auto e = this->_layout.findAbsoluteAtom(end);
-    auto section = this->_layout.findOutputSection(sec);
+    AtomLayout *s = this->_layout.findAbsoluteAtom(start);
+    AtomLayout *e = this->_layout.findAbsoluteAtom(end);
+    OutputSection<ELFT> *section = this->_layout.findOutputSection(sec);
     if (section) {
-      (*s)->_virtualAddr = section->virtualAddr();
-      (*e)->_virtualAddr = section->virtualAddr() + section->memSize();
+      s->_virtualAddr = section->virtualAddr();
+      e->_virtualAddr = section->virtualAddr() + section->memSize();
     } else {
-      (*s)->_virtualAddr = 0;
-      (*e)->_virtualAddr = 0;
+      s->_virtualAddr = 0;
+      e->_virtualAddr = 0;
     }
   };
 
@@ -154,25 +157,19 @@ template <class ELFT> void ExecutableWriter<ELFT>::finalizeDefaultAtomValues() {
     startEnd("rel_iplt", ".rel.plt");
   startEnd("fini_array", ".fini_array");
 
-  assert(!(bssStartAtomIter == this->_layout.absoluteAtoms().end() ||
-           bssEndAtomIter == this->_layout.absoluteAtoms().end() ||
-           underScoreEndAtomIter == this->_layout.absoluteAtoms().end() ||
-           endAtomIter == this->_layout.absoluteAtoms().end()) &&
-         "Unable to find the absolute atoms that have been added by lld");
-
   auto bssSection = this->_layout.findOutputSection(".bss");
 
   // If we don't find a bss section, then don't set these values
   if (bssSection) {
-    (*bssStartAtomIter)->_virtualAddr = bssSection->virtualAddr();
-    (*bssEndAtomIter)->_virtualAddr =
+    bssStartAtom->_virtualAddr = bssSection->virtualAddr();
+    bssEndAtom->_virtualAddr =
         bssSection->virtualAddr() + bssSection->memSize();
-    (*underScoreEndAtomIter)->_virtualAddr = (*bssEndAtomIter)->_virtualAddr;
-    (*endAtomIter)->_virtualAddr = (*bssEndAtomIter)->_virtualAddr;
+    underScoreEndAtom->_virtualAddr = bssEndAtom->_virtualAddr;
+    endAtom->_virtualAddr = bssEndAtom->_virtualAddr;
   } else if (auto dataSection = this->_layout.findOutputSection(".data")) {
-    (*underScoreEndAtomIter)->_virtualAddr =
+    underScoreEndAtom->_virtualAddr =
         dataSection->virtualAddr() + dataSection->memSize();
-    (*endAtomIter)->_virtualAddr = (*underScoreEndAtomIter)->_virtualAddr;
+    endAtom->_virtualAddr = underScoreEndAtom->_virtualAddr;
   }
 }
 
index 366ec1a2d0d5f31fe1d77256e2df56c5bfe03f1e..d89fbaf0b94e3a8abb3b53d93ad6f43e3d8fd4c8 100644 (file)
@@ -70,9 +70,8 @@ template <class ELFT>
 void HexagonExecutableWriter<ELFT>::finalizeDefaultAtomValues() {
   // Finalize the atom values that are part of the parent.
   ExecutableWriter<ELFT>::finalizeDefaultAtomValues();
-  auto sdabaseAtomIter = _targetLayout.findAbsoluteAtom("_SDA_BASE_");
-  (*sdabaseAtomIter)->_virtualAddr =
-      _targetLayout.getSDataSection()->virtualAddr();
+  AtomLayout *sdabaseAtom = _targetLayout.findAbsoluteAtom("_SDA_BASE_");
+  sdabaseAtom->_virtualAddr = _targetLayout.getSDataSection()->virtualAddr();
   if (_ctx.isDynamic())
     finalizeHexagonRuntimeAtomValues(_targetLayout);
 }
index 290f4af4180827928adcf08758b22c3df08d2b1f..11360588bbb952f7286ddb10cf24c6528a1f7df9 100644 (file)
@@ -83,10 +83,8 @@ public:
   }
 
   uint64_t getGOTSymAddr() {
-    if (!_gotSymAtom.hasValue()) {
-      auto iter = this->findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
-      _gotSymAtom = (iter == this->absoluteAtoms().end()) ? nullptr : *iter;
-    }
+    if (!_gotSymAtom.hasValue())
+      _gotSymAtom = this->findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
     if (*_gotSymAtom)
       return (*_gotSymAtom)->_virtualAddr;
     return 0;
@@ -131,18 +129,18 @@ private:
 
 template <class ELFT>
 void finalizeHexagonRuntimeAtomValues(HexagonTargetLayout<ELFT> &layout) {
-  auto gotAtomIter = layout.findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
-  auto gotpltSection = layout.findOutputSection(".got.plt");
+  AtomLayout *gotAtom = layout.findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
+  OutputSection<ELFT> *gotpltSection = layout.findOutputSection(".got.plt");
   if (gotpltSection)
-    (*gotAtomIter)->_virtualAddr = gotpltSection->virtualAddr();
+    gotAtom->_virtualAddr = gotpltSection->virtualAddr();
   else
-    (*gotAtomIter)->_virtualAddr = 0;
-  auto dynamicAtomIter = layout.findAbsoluteAtom("_DYNAMIC");
-  auto dynamicSection = layout.findOutputSection(".dynamic");
+    gotAtom->_virtualAddr = 0;
+  AtomLayout *dynamicAtom = layout.findAbsoluteAtom("_DYNAMIC");
+  OutputSection<ELFT> *dynamicSection = layout.findOutputSection(".dynamic");
   if (dynamicSection)
-    (*dynamicAtomIter)->_virtualAddr = dynamicSection->virtualAddr();
+    dynamicAtom->_virtualAddr = dynamicSection->virtualAddr();
   else
-    (*dynamicAtomIter)->_virtualAddr = 0;
+    dynamicAtom->_virtualAddr = 0;
 }
 
 } // end namespace elf
index 110f05d7edd2ba62f46b1b5b54bbd52387c58de1..25e1f32f5acdcbc68e583d67cf7834d7eb229d4e 100644 (file)
@@ -68,9 +68,9 @@ private:
   MipsTargetLayout<ELFT> &_targetLayout;
 
   void setAtomValue(StringRef name, uint64_t value) {
-    auto atom = _targetLayout.findAbsoluteAtom(name);
-    assert(atom != _targetLayout.absoluteAtoms().end());
-    (*atom)->_virtualAddr = value;
+    AtomLayout *atom = _targetLayout.findAbsoluteAtom(name);
+    assert(atom);
+    atom->_virtualAddr = value;
   }
 };
 
index 4c76770730eb44bd5aefacd33cd33d00f16bfb43..5e52b17fcdb51f8026406c78871be30d80b44802 100644 (file)
@@ -48,19 +48,15 @@ public:
 
   /// \brief Get '_gp' symbol atom layout.
   AtomLayout *getGP() {
-    if (!_gpAtom.hasValue()) {
-      auto atom = this->findAbsoluteAtom("_gp");
-      _gpAtom = atom != this->absoluteAtoms().end() ? *atom : nullptr;
-    }
+    if (!_gpAtom.hasValue())
+      _gpAtom = this->findAbsoluteAtom("_gp");
     return *_gpAtom;
   }
 
   /// \brief Get '_gp_disp' symbol atom layout.
   AtomLayout *getGPDisp() {
-    if (!_gpDispAtom.hasValue()) {
-      auto atom = this->findAbsoluteAtom("_gp_disp");
-      _gpDispAtom = atom != this->absoluteAtoms().end() ? *atom : nullptr;
-    }
+    if (!_gpDispAtom.hasValue())
+      _gpDispAtom = this->findAbsoluteAtom("_gp_disp");
     return *_gpDispAtom;
   }
 
index 59e3425df4e2d875c3cab1c49e084cbb1d6c79a9..53c941d49a57ae4c521780b857da4ac99e074810 100644 (file)
@@ -384,8 +384,9 @@ void OutputELFWriter<ELFT>::finalizeDefaultAtomValues() {
   for (auto &sym : symbols) {
     uint64_t res =
         _ctx.linkerScriptSema().getLinkerScriptExprValue(sym.getKey());
-    auto a = _layout.findAbsoluteAtom(sym.getKey());
-    (*a)->_virtualAddr = res;
+    AtomLayout *a = _layout.findAbsoluteAtom(sym.getKey());
+    assert(a);
+    a->_virtualAddr = res;
   }
 }