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
This commit is contained in:
Rui Ueyama 2015-03-31 22:37:59 +00:00
parent 0ddb72263f
commit 5d2939302a
10 changed files with 55 additions and 63 deletions

View 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"))

View 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;
}

View 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

View 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();
}
}

View 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;
}
}

View 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);
}

View 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

View 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;
}
};

View 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;
}

View 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;
}
}