Index: lib/ReaderWriter/ELF/Atoms.h =================================================================== --- lib/ReaderWriter/ELF/Atoms.h +++ lib/ReaderWriter/ELF/Atoms.h @@ -38,24 +38,22 @@ typedef llvm::object::Elf_Sym_Impl Elf_Sym; public: - ELFReference(const Elf_Sym *sym, const Elf_Rela *rela, uint64_t off, - Reference::KindArch arch, Reference::KindValue relocType, - uint32_t idx) - : Reference(Reference::KindNamespace::ELF, arch, relocType), _sym(sym), + ELFReference(const Elf_Rela *rela, uint64_t off, Reference::KindArch arch, + Reference::KindValue relocType, uint32_t idx) + : Reference(Reference::KindNamespace::ELF, arch, relocType), _target(nullptr), _targetSymbolIndex(idx), _offsetInAtom(off), _addend(rela->r_addend) {} - ELFReference(const Elf_Sym *sym, uint64_t off, Reference::KindArch arch, + ELFReference(uint64_t off, Reference::KindArch arch, Reference::KindValue relocType, uint32_t idx) - : Reference(Reference::KindNamespace::ELF, arch, relocType), _sym(sym), + : Reference(Reference::KindNamespace::ELF, arch, relocType), _target(nullptr), _targetSymbolIndex(idx), _offsetInAtom(off), _addend(0) {} ELFReference(uint32_t edgeKind) : Reference(Reference::KindNamespace::all, Reference::KindArch::all, edgeKind), - _sym(nullptr), _target(nullptr), _targetSymbolIndex(0), - _offsetInAtom(0), _addend(0) {} + _target(nullptr), _targetSymbolIndex(0), _offsetInAtom(0), _addend(0) {} uint64_t offsetInAtom() const override { return _offsetInAtom; } @@ -74,12 +72,7 @@ void setTarget(const Atom *newAtom) override { _target = newAtom; } - /// Is used for lookup the symbol or section that refers is in the same group - /// or not in a group. - const Elf_Sym *symbol() const { return _sym; } - private: - const Elf_Sym *_sym; const Atom *_target; uint64_t _targetSymbolIndex; uint64_t _offsetInAtom; Index: lib/ReaderWriter/ELF/ELFFile.h =================================================================== --- lib/ReaderWriter/ELF/ELFFile.h +++ lib/ReaderWriter/ELF/ELFFile.h @@ -388,6 +388,18 @@ bool redirectReferenceUsingUndefAtom(const Elf_Sym *sourceSymbol, const Elf_Sym *targetSymbol) const; + void addReferenceToSymbol(const ELFReference *r, const Elf_Sym *sym) { + _referenceToSymbol[r] = sym; + } + + const Elf_Sym *findSymbolForReference(const ELFReference *r) const { + auto elfReferenceToSymbol = _referenceToSymbol.find(r); + if (elfReferenceToSymbol != _referenceToSymbol.end()) + return elfReferenceToSymbol->second; + ; + return nullptr; + } + llvm::BumpPtrAllocator _readerStorage; std::unique_ptr > _objFile; atom_collection_vector _definedAtoms; @@ -405,6 +417,8 @@ std::unordered_map> _relocationReferences; std::vector *> _references; llvm::DenseMap _symbolToAtomMapping; + llvm::DenseMap *, const Elf_Sym *> + _referenceToSymbol; // Group child atoms have a pair corresponding to the signature and the // section header of the section that was used for generating the signature. llvm::DenseMap> @@ -1016,9 +1030,11 @@ if (rel.r_offset < symValue || symValue + content.size() <= rel.r_offset) continue; - _references.push_back(new (_readerStorage) ELFReference( - symbol, &rel, rel.r_offset - symValue, kindArch(), - rel.getType(isMips64EL), rel.getSymbol(isMips64EL))); + auto elfRelocation = new (_readerStorage) + ELFReference(&rel, rel.r_offset - symValue, kindArch(), + rel.getType(isMips64EL), rel.getSymbol(isMips64EL)); + addReferenceToSymbol(elfRelocation, symbol); + _references.push_back(elfRelocation); } } @@ -1033,11 +1049,13 @@ if (rel.r_offset < symValue || symValue + symContent.size() <= rel.r_offset) continue; - _references.push_back(new (_readerStorage) ELFReference( - symbol, rel.r_offset - symValue, kindArch(), rel.getType(isMips64EL), - rel.getSymbol(isMips64EL))); + auto elfRelocation = new (_readerStorage) + ELFReference(rel.r_offset - symValue, kindArch(), + rel.getType(isMips64EL), rel.getSymbol(isMips64EL)); int32_t addend = *(symContent.data() + rel.r_offset - symValue); - _references.back()->setAddend(addend); + elfRelocation->setAddend(addend); + addReferenceToSymbol(elfRelocation, symbol); + _references.push_back(elfRelocation); } } @@ -1079,7 +1097,7 @@ // If the atom is not in mergeable string section, the target atom is // simply that atom. if (!isMergeableStringSection(shdr)) { - ri->setTarget(findAtom(ri->symbol(), symbol)); + ri->setTarget(findAtom(findSymbolForReference(ri), symbol)); continue; } updateReferenceForMergeStringAccess(ri, symbol, shdr); Index: lib/ReaderWriter/ELF/Mips/MipsELFFile.h =================================================================== --- lib/ReaderWriter/ELF/Mips/MipsELFFile.h +++ lib/ReaderWriter/ELF/Mips/MipsELFFile.h @@ -208,9 +208,11 @@ symbol->st_value + symContent.size() <= rit->r_offset) continue; - this->_references.push_back(new (this->_readerStorage) ELFReference( - symbol, rit->r_offset - symbol->st_value, this->kindArch(), - rit->getType(isMips64EL()), rit->getSymbol(isMips64EL()))); + auto elfReference = new (this->_readerStorage) ELFReference( + rit->r_offset - symbol->st_value, this->kindArch(), + rit->getType(isMips64EL()), rit->getSymbol(isMips64EL())); + ELFFile::addReferenceToSymbol(elfReference, symbol); + this->_references.push_back(elfReference); auto addend = getAddend(*rit, secContent); auto pairRelType = getPairRelocation(*rit);