From 3af86648587d6bcc8b04a7a53b908c9507f3f388 Mon Sep 17 00:00:00 2001 From: Simon Atanasyan Date: Sun, 9 Mar 2014 13:19:29 +0000 Subject: [PATCH] [ELF] Factor out the code creates a Reference for the specified symbol and relocation entry into the two virtual functions. llvm-svn: 203408 --- lld/lib/ReaderWriter/ELF/ELFFile.h | 61 +++++++++++++++++++----------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/lld/lib/ReaderWriter/ELF/ELFFile.h b/lld/lib/ReaderWriter/ELF/ELFFile.h index 8f56a310f276..a15d0c629b32 100644 --- a/lld/lib/ReaderWriter/ELF/ELFFile.h +++ b/lld/lib/ReaderWriter/ELF/ELFFile.h @@ -166,6 +166,16 @@ protected: StringRef symbolName, StringRef sectionName, const Elf_Sym *symbol, const Elf_Shdr *section, ArrayRef content); + /// \brief Create a reference for the Elf_Sym symbol + /// and Elf_Rela relocation entry. + virtual ELFReference *createRelocationReference(const Elf_Sym &symbol, + const Elf_Rela &rai); + /// \brief Create a reference for the Elf_Sym symbol + /// and Elf_Rel relocation entry. + virtual ELFReference * + createRelocationReference(const Elf_Sym &symbol, const Elf_Rel &ri, + ArrayRef content); + /// \brief After all the Atoms and References are created, update each /// Reference's target with the Atom pointer it refers to. virtual void updateReferences(); @@ -747,15 +757,9 @@ ELFDefinedAtom *ELFFile::createDefinedAtomAndAssignRelocations( auto rari = _relocationAddendReferences.find(sectionName); if (rari != _relocationAddendReferences.end()) { for (const Elf_Rela &rai : rari->second) { - if (rai.r_offset < symbol->st_value || - symbol->st_value + content.size() <= rai.r_offset) - continue; - bool isMips64EL = _objFile->isMips64EL(); - uint32_t symbolIndex = rai.getSymbol(isMips64EL); - auto *ERef = new (_readerStorage) - ELFReference(&rai, rai.r_offset - symbol->st_value, kindArch(), - rai.getType(isMips64EL), symbolIndex); - _references.push_back(ERef); + if (symbol->st_value <= rai.r_offset && + rai.r_offset < symbol->st_value + content.size()) + _references.push_back(createRelocationReference(*symbol, rai)); } } @@ -763,20 +767,9 @@ ELFDefinedAtom *ELFFile::createDefinedAtomAndAssignRelocations( auto rri = _relocationReferences.find(sectionName); if (rri != _relocationReferences.end()) { for (const Elf_Rel &ri : rri->second) { - if (ri.r_offset < symbol->st_value || - symbol->st_value + content.size() <= ri.r_offset) - continue; - bool isMips64EL = _objFile->isMips64EL(); - uint32_t symbolIndex = ri.getSymbol(isMips64EL); - auto *ERef = new (_readerStorage) - ELFReference(&ri, ri.r_offset - symbol->st_value, kindArch(), - ri.getType(isMips64EL), symbolIndex); - // Read the addend from the section contents - // TODO : We should move the way lld reads relocations totally from - // ELFFile - int32_t addend = *(content.data() + ri.r_offset - symbol->st_value); - ERef->setAddend(addend); - _references.push_back(ERef); + if (symbol->st_value <= ri.r_offset && + ri.r_offset < symbol->st_value + content.size()) + _references.push_back(createRelocationReference(*symbol, ri, content)); } } @@ -785,6 +778,28 @@ ELFDefinedAtom *ELFFile::createDefinedAtomAndAssignRelocations( referenceStart, _references.size(), _references); } +template +ELFReference * +ELFFile::createRelocationReference(const Elf_Sym &symbol, + const Elf_Rela &rai) { + bool isMips64EL = _objFile->isMips64EL(); + return new (_readerStorage) + ELFReference(&rai, rai.r_offset - symbol.st_value, kindArch(), + rai.getType(isMips64EL), rai.getSymbol(isMips64EL)); +} + +template +ELFReference *ELFFile::createRelocationReference( + const Elf_Sym &symbol, const Elf_Rel &ri, ArrayRef content) { + bool isMips64EL = _objFile->isMips64EL(); + auto *ref = new (_readerStorage) + ELFReference(&ri, ri.r_offset - symbol.st_value, kindArch(), + ri.getType(isMips64EL), ri.getSymbol(isMips64EL)); + int32_t addend = *(content.data() + ri.r_offset - symbol.st_value); + ref->setAddend(addend); + return ref; +} + template int64_t ELFFile::defaultRelocAddend(const Reference &) const { return 0;