From 5d2939302a4ea7ae0df14afc9618ee22b8d47bad Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 31 Mar 2015 22:37:59 +0000 Subject: [PATCH] ELF: Make findAbsoluteAtom return AtomLayout* instead of an iterator. 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 --- .../ELF/ARM/ARMExecutableWriter.h | 5 +-- .../ReaderWriter/ELF/ARM/ARMTargetHandler.h | 6 +-- lld/lib/ReaderWriter/ELF/DefaultLayout.h | 7 ++- .../ReaderWriter/ELF/DynamicLibraryWriter.h | 7 +-- lld/lib/ReaderWriter/ELF/ExecutableWriter.h | 43 +++++++++---------- .../ELF/Hexagon/HexagonExecutableWriter.h | 5 +-- .../ELF/Hexagon/HexagonTargetHandler.h | 22 +++++----- .../ReaderWriter/ELF/Mips/MipsELFWriters.h | 6 +-- .../ReaderWriter/ELF/Mips/MipsTargetHandler.h | 12 ++---- lld/lib/ReaderWriter/ELF/OutputELFWriter.h | 5 ++- 10 files changed, 55 insertions(+), 63 deletions(-) diff --git a/lld/lib/ReaderWriter/ELF/ARM/ARMExecutableWriter.h b/lld/lib/ReaderWriter/ELF/ARM/ARMExecutableWriter.h index 401bc33d8513..1fe7f29c876f 100644 --- a/lld/lib/ReaderWriter/ELF/ARM/ARMExecutableWriter.h +++ b/lld/lib/ReaderWriter/ELF/ARM/ARMExecutableWriter.h @@ -66,9 +66,8 @@ template void ARMExecutableWriter::finalizeDefaultAtomValues() { // Finalize the atom values that are part of the parent. ExecutableWriter::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")) diff --git a/lld/lib/ReaderWriter/ELF/ARM/ARMTargetHandler.h b/lld/lib/ReaderWriter/ELF/ARM/ARMTargetHandler.h index 47ae46eb80d7..2ff8dc015929 100644 --- a/lld/lib/ReaderWriter/ELF/ARM/ARMTargetHandler.h +++ b/lld/lib/ReaderWriter/ELF/ARM/ARMTargetHandler.h @@ -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; } diff --git a/lld/lib/ReaderWriter/ELF/DefaultLayout.h b/lld/lib/ReaderWriter/ELF/DefaultLayout.h index f9807a94e39d..24c625298e92 100644 --- a/lld/lib/ReaderWriter/ELF/DefaultLayout.h +++ b/lld/lib/ReaderWriter/ELF/DefaultLayout.h @@ -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 diff --git a/lld/lib/ReaderWriter/ELF/DynamicLibraryWriter.h b/lld/lib/ReaderWriter/ELF/DynamicLibraryWriter.h index 88211660200f..2f2f0da7a995 100644 --- a/lld/lib/ReaderWriter/ELF/DynamicLibraryWriter.h +++ b/lld/lib/ReaderWriter/ELF/DynamicLibraryWriter.h @@ -79,13 +79,14 @@ bool DynamicLibraryWriter::createImplicitFiles( template void DynamicLibraryWriter::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(); } } diff --git a/lld/lib/ReaderWriter/ELF/ExecutableWriter.h b/lld/lib/ReaderWriter/ELF/ExecutableWriter.h index e014cbee59ef..b281b09c9c27 100644 --- a/lld/lib/ReaderWriter/ELF/ExecutableWriter.h +++ b/lld/lib/ReaderWriter/ELF/ExecutableWriter.h @@ -126,23 +126,26 @@ template void ExecutableWriter::createDefaultSections() { /// created template void ExecutableWriter::finalizeDefaultAtomValues() { OutputELFWriter::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 *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 void ExecutableWriter::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; } } diff --git a/lld/lib/ReaderWriter/ELF/Hexagon/HexagonExecutableWriter.h b/lld/lib/ReaderWriter/ELF/Hexagon/HexagonExecutableWriter.h index 366ec1a2d0d5..d89fbaf0b94e 100644 --- a/lld/lib/ReaderWriter/ELF/Hexagon/HexagonExecutableWriter.h +++ b/lld/lib/ReaderWriter/ELF/Hexagon/HexagonExecutableWriter.h @@ -70,9 +70,8 @@ template void HexagonExecutableWriter::finalizeDefaultAtomValues() { // Finalize the atom values that are part of the parent. ExecutableWriter::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); } diff --git a/lld/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h b/lld/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h index 290f4af41808..11360588bbb9 100644 --- a/lld/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h +++ b/lld/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h @@ -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 void finalizeHexagonRuntimeAtomValues(HexagonTargetLayout &layout) { - auto gotAtomIter = layout.findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_"); - auto gotpltSection = layout.findOutputSection(".got.plt"); + AtomLayout *gotAtom = layout.findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_"); + OutputSection *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 *dynamicSection = layout.findOutputSection(".dynamic"); if (dynamicSection) - (*dynamicAtomIter)->_virtualAddr = dynamicSection->virtualAddr(); + dynamicAtom->_virtualAddr = dynamicSection->virtualAddr(); else - (*dynamicAtomIter)->_virtualAddr = 0; + dynamicAtom->_virtualAddr = 0; } } // end namespace elf diff --git a/lld/lib/ReaderWriter/ELF/Mips/MipsELFWriters.h b/lld/lib/ReaderWriter/ELF/Mips/MipsELFWriters.h index 110f05d7edd2..25e1f32f5acd 100644 --- a/lld/lib/ReaderWriter/ELF/Mips/MipsELFWriters.h +++ b/lld/lib/ReaderWriter/ELF/Mips/MipsELFWriters.h @@ -68,9 +68,9 @@ private: MipsTargetLayout &_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; } }; diff --git a/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h b/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h index 4c76770730eb..5e52b17fcdb5 100644 --- a/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h +++ b/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h @@ -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; } diff --git a/lld/lib/ReaderWriter/ELF/OutputELFWriter.h b/lld/lib/ReaderWriter/ELF/OutputELFWriter.h index 59e3425df4e2..53c941d49a57 100644 --- a/lld/lib/ReaderWriter/ELF/OutputELFWriter.h +++ b/lld/lib/ReaderWriter/ELF/OutputELFWriter.h @@ -384,8 +384,9 @@ void OutputELFWriter::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; } }