[lld] r204243 - [Mips] Move AHL addends calculation to the RelocationPass class.
Simon Atanasyan
simon at atanasyan.com
Wed Mar 19 08:45:55 PDT 2014
Author: atanasyan
Date: Wed Mar 19 10:45:55 2014
New Revision: 204243
URL: http://llvm.org/viewvc/llvm-project?rev=204243&view=rev
Log:
[Mips] Move AHL addends calculation to the RelocationPass class.
Modified:
lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp
lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.h
lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp
Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp?rev=204243&r1=204242&r2=204243&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp Wed Mar 19 10:45:55 2014
@@ -117,54 +117,6 @@ void relocLldLo16(uint8_t *location, uin
} // end anon namespace
-MipsTargetRelocationHandler::~MipsTargetRelocationHandler() {
- assert(_pairedRelocations.empty());
-}
-
-void
-MipsTargetRelocationHandler::savePairedRelocation(const lld::AtomLayout &atom,
- const Reference &ref) const {
- auto pi = _pairedRelocations.find(&atom);
- if (pi == _pairedRelocations.end())
- pi = _pairedRelocations.emplace(&atom, PairedRelocationsT()).first;
-
- pi->second.push_back(&ref);
-}
-
-void MipsTargetRelocationHandler::applyPairedRelocations(
- ELFWriter &writer, llvm::FileOutputBuffer &buf, const lld::AtomLayout &atom,
- int64_t gpAddr, int64_t loAddend) const {
- auto pi = _pairedRelocations.find(&atom);
- if (pi == _pairedRelocations.end())
- return;
-
- for (auto ri : pi->second) {
- uint8_t *atomContent = buf.getBufferStart() + atom._fileOffset;
- uint8_t *location = atomContent + ri->offsetInAtom();
- uint64_t targetVAddress = writer.addressOfAtom(ri->target());
- uint64_t relocVAddress = atom._virtualAddr + ri->offsetInAtom();
-
- int64_t ahl = calcAHL(ri->addend(), loAddend);
-
- if (ri->kindNamespace() != lld::Reference::KindNamespace::ELF)
- continue;
- assert(ri->kindArch() == Reference::KindArch::Mips);
- switch (ri->kindValue()) {
- case R_MIPS_HI16:
- relocHi16(location, relocVAddress, targetVAddress, ahl, gpAddr,
- ri->target()->name() == "_gp_disp");
- break;
- case R_MIPS_GOT16:
- relocGOT16(location, relocVAddress, targetVAddress, ahl, gpAddr);
- break;
- default:
- llvm_unreachable("Unknown type of paired relocation.");
- }
- }
-
- _pairedRelocations.erase(pi);
-}
-
error_code MipsTargetRelocationHandler::applyRelocation(
ELFWriter &writer, llvm::FileOutputBuffer &buf, const lld::AtomLayout &atom,
const Reference &ref) const {
@@ -190,15 +142,15 @@ error_code MipsTargetRelocationHandler::
reloc26loc(location, relocVAddress, targetVAddress, ref.addend());
break;
case R_MIPS_HI16:
- savePairedRelocation(atom, ref);
+ relocHi16(location, relocVAddress, targetVAddress, ref.addend(), gpAddr,
+ ref.target()->name() == "_gp_disp");
break;
case R_MIPS_LO16:
- relocLo16(location, relocVAddress, targetVAddress, calcAHL(0, ref.addend()),
- gpAddr, ref.target()->name() == "_gp_disp");
- applyPairedRelocations(writer, buf, atom, gpAddr, ref.addend());
+ relocLo16(location, relocVAddress, targetVAddress, ref.addend(), gpAddr,
+ ref.target()->name() == "_gp_disp");
break;
case R_MIPS_GOT16:
- savePairedRelocation(atom, ref);
+ relocGOT16(location, relocVAddress, targetVAddress, ref.addend(), gpAddr);
break;
case R_MIPS_CALL16:
relocCall16(location, relocVAddress, targetVAddress, ref.addend(), gpAddr);
Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.h?rev=204243&r1=204242&r2=204243&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.h Wed Mar 19 10:45:55 2014
@@ -22,25 +22,11 @@ public:
MipsTargetRelocationHandler(MipsTargetLayout<Mips32ElELFType> &layout)
: _mipsTargetLayout(layout) {}
- ~MipsTargetRelocationHandler();
-
error_code applyRelocation(ELFWriter &, llvm::FileOutputBuffer &,
const lld::AtomLayout &,
const Reference &) const override;
private:
- typedef std::vector<const Reference *> PairedRelocationsT;
- typedef std::unordered_map<const lld::AtomLayout *, PairedRelocationsT>
- PairedRelocationMapT;
-
- mutable PairedRelocationMapT _pairedRelocations;
-
- void savePairedRelocation(const lld::AtomLayout &atom,
- const Reference &ref) const;
- void applyPairedRelocations(ELFWriter &writer, llvm::FileOutputBuffer &buf,
- const lld::AtomLayout &atom, int64_t gpAddr,
- int64_t loAddend) const;
-
MipsTargetLayout<Mips32ElELFType> &_mipsTargetLayout;
};
Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp?rev=204243&r1=204242&r2=204243&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp Wed Mar 19 10:45:55 2014
@@ -152,6 +152,9 @@ private:
/// \brief Handle a specific reference.
void handleReference(const DefinedAtom &atom, const Reference &ref);
+ /// \brief Calculate AHL addendums for the atom's references.
+ void calculateAHLs(const DefinedAtom &atom);
+
void handlePlain(const Reference &ref);
void handlePLT(const Reference &ref);
void handleGOT(const Reference &ref);
@@ -172,6 +175,9 @@ RelocationPass::RelocationPass(MipsLinki
}
void RelocationPass::perform(std::unique_ptr<MutableFile> &mf) {
+ for (const auto &atom : mf->defined())
+ calculateAHLs(*atom);
+
// Process all references.
for (const auto &atom : mf->defined())
for (const auto &ref : *atom)
@@ -213,6 +219,41 @@ void RelocationPass::perform(std::unique
}
}
+/// \brief Calculate AHL value combines addends from 'hi' and 'lo' relocations.
+inline int64_t calcAHL(int64_t AHI, int64_t ALO) {
+ AHI &= 0xffff;
+ ALO &= 0xffff;
+ return (AHI << 16) + (int16_t)ALO;
+}
+
+void RelocationPass::calculateAHLs(const DefinedAtom &atom) {
+ std::vector<Reference *> references;
+ for (const auto &ref : atom) {
+ if (ref->kindNamespace() != lld::Reference::KindNamespace::ELF)
+ continue;
+ assert(ref->kindArch() == Reference::KindArch::Mips);
+ switch (ref->kindValue()) {
+ case R_MIPS_HI16:
+ references.push_back(const_cast<Reference *>(ref));
+ break;
+ case R_MIPS_GOT16:
+ if (isLocal(ref->target()))
+ references.push_back(const_cast<Reference *>(ref));
+ break;
+ case R_MIPS_LO16:
+ for (auto &sr : references)
+ sr->setAddend(calcAHL(sr->addend(), ref->addend()));
+ references.clear();
+ break;
+ }
+ }
+ if (!references.empty()) {
+ for (auto &sr : references)
+ sr->setAddend(0);
+ }
+ assert(references.empty());
+}
+
void RelocationPass::handleReference(const DefinedAtom &atom,
const Reference &ref) {
if (ref.kindNamespace() != lld::Reference::KindNamespace::ELF)
More information about the llvm-commits
mailing list