[lld] r265682 - Simplify dynamic relocation creation.
Rafael Espindola via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 7 08:20:57 PDT 2016
Author: rafael
Date: Thu Apr 7 10:20:56 2016
New Revision: 265682
URL: http://llvm.org/viewvc/llvm-project?rev=265682&view=rev
Log:
Simplify dynamic relocation creation.
The position of a relocation can always be expressed as an offset in an
output section.
Modified:
lld/trunk/ELF/OutputSections.cpp
lld/trunk/ELF/OutputSections.h
lld/trunk/ELF/Symbols.cpp
lld/trunk/ELF/Symbols.h
lld/trunk/ELF/Writer.cpp
Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=265682&r1=265681&r2=265682&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Thu Apr 7 10:20:56 2016
@@ -195,6 +195,12 @@ GotSection<ELFT>::getGlobalDynAddr(const
}
template <class ELFT>
+typename GotSection<ELFT>::uintX_t
+GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
+ return B.GlobalDynIndex * sizeof(uintX_t);
+}
+
+template <class ELFT>
const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
return Entries.empty() ? nullptr : Entries.front();
}
@@ -297,25 +303,6 @@ void RelocationSection<ELFT>::addReloc(c
Relocs.push_back(Reloc);
}
-template <class ELFT>
-typename ELFT::uint DynamicReloc<ELFT>::getOffset() const {
- switch (OKind) {
- case Off_GTlsIndex:
- return Out<ELFT>::Got->getGlobalDynAddr(*Sym);
- case Off_GTlsOffset:
- return Out<ELFT>::Got->getGlobalDynAddr(*Sym) + sizeof(uintX_t);
- case Off_LTlsIndex:
- return Out<ELFT>::Got->getTlsIndexVA();
- case Off_Sec:
- return OffsetInSec + OffsetSec->getVA();
- case Off_Got:
- return Sym->getGotVA<ELFT>();
- case Off_GotPlt:
- return Sym->getGotPltVA<ELFT>();
- }
- llvm_unreachable("invalid offset kind");
-}
-
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
for (const DynamicReloc<ELFT> &Rel : Relocs) {
auto *P = reinterpret_cast<Elf_Rela *>(Buf);
@@ -324,7 +311,7 @@ template <class ELFT> void RelocationSec
if (Config->Rela)
P->r_addend = Rel.UseSymVA ? Sym->getVA<ELFT>(Rel.Addend) : Rel.Addend;
- P->r_offset = Rel.getOffset();
+ P->r_offset = Rel.OffsetInSec + Rel.OffsetSec->getVA();
uint32_t SymIdx = (!Rel.UseSymVA && Sym) ? Sym->DynsymIndex : 0;
P->setSymbolAndType(SymIdx, Rel.Type, Config->Mips64EL);
}
Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=265682&r1=265681&r2=265682&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Thu Apr 7 10:20:56 2016
@@ -113,6 +113,7 @@ public:
uintX_t getMipsLocalFullAddr(const SymbolBody &B);
uintX_t getMipsLocalPageAddr(uintX_t Addr);
uintX_t getGlobalDynAddr(const SymbolBody &B) const;
+ uintX_t getGlobalDynOffset(const SymbolBody &B) const;
uintX_t getNumEntries() const { return Entries.size(); }
// Returns the symbol which corresponds to the first entry of the global part
@@ -126,6 +127,7 @@ public:
unsigned getMipsLocalEntriesNum() const;
uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
+ uint32_t getTlsIndexOff() { return TlsIndexOff; }
private:
std::vector<const SymbolBody *> Entries;
@@ -172,35 +174,17 @@ template <class ELFT> struct DynamicRelo
typedef typename ELFT::uint uintX_t;
uint32_t Type;
- // Where the relocation is.
- enum OffsetKind {
- Off_Got, // The got entry of Sym.
- Off_GotPlt, // The got.plt entry of Sym.
- Off_Sec, // The final position of the given input section and offset.
- Off_LTlsIndex, // The local tls index.
- Off_GTlsIndex, // The global tls index of Sym.
- Off_GTlsOffset // The global tls offset of Sym.
- } OKind;
-
SymbolBody *Sym = nullptr;
OutputSectionBase<ELFT> *OffsetSec = nullptr;
uintX_t OffsetInSec = 0;
bool UseSymVA = false;
uintX_t Addend = 0;
- DynamicReloc(uint32_t Type, OffsetKind OKind, SymbolBody *Sym)
- : Type(Type), OKind(OKind), Sym(Sym) {}
-
- DynamicReloc(uint32_t Type, OffsetKind OKind, bool UseSymVA, SymbolBody *Sym)
- : Type(Type), OKind(OKind), Sym(Sym), UseSymVA(UseSymVA) {}
-
DynamicReloc(uint32_t Type, OutputSectionBase<ELFT> *OffsetSec,
uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
uintX_t Addend)
- : Type(Type), OKind(Off_Sec), Sym(Sym), OffsetSec(OffsetSec),
- OffsetInSec(OffsetInSec), UseSymVA(UseSymVA), Addend(Addend) {}
-
- uintX_t getOffset() const;
+ : Type(Type), Sym(Sym), OffsetSec(OffsetSec), OffsetInSec(OffsetInSec),
+ UseSymVA(UseSymVA), Addend(Addend) {}
};
template <class ELFT>
Modified: lld/trunk/ELF/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.cpp?rev=265682&r1=265681&r2=265682&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.cpp (original)
+++ lld/trunk/ELF/Symbols.cpp Thu Apr 7 10:20:56 2016
@@ -150,13 +150,20 @@ typename ELFT::uint SymbolBody::getVA(ty
}
template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
- return Out<ELFT>::Got->getVA() +
- (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
- sizeof(typename ELFT::uint);
+ return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
+}
+
+template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
+ return (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
+ sizeof(typename ELFT::uint);
}
template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
- return Out<ELFT>::GotPlt->getVA() + GotPltIndex * sizeof(typename ELFT::uint);
+ return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
+}
+
+template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
+ return GotPltIndex * sizeof(typename ELFT::uint);
}
template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
@@ -340,11 +347,21 @@ template uint32_t SymbolBody::template g
template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
+template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
+template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
+template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
+template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
+
template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
+template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
+template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
+template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
+template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
+
template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
Modified: lld/trunk/ELF/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.h?rev=265682&r1=265681&r2=265682&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.h (original)
+++ lld/trunk/ELF/Symbols.h Thu Apr 7 10:20:56 2016
@@ -104,7 +104,9 @@ public:
template <class ELFT>
typename ELFT::uint getVA(typename ELFT::uint Addend = 0) const;
+ template <class ELFT> typename ELFT::uint getGotOffset() const;
template <class ELFT> typename ELFT::uint getGotVA() const;
+ template <class ELFT> typename ELFT::uint getGotPltOffset() const;
template <class ELFT> typename ELFT::uint getGotPltVA() const;
template <class ELFT> typename ELFT::uint getPltVA() const;
template <class ELFT> typename ELFT::uint getThunkVA() const;
Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=265682&r1=265681&r2=265682&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Apr 7 10:20:56 2016
@@ -266,13 +266,14 @@ template <bool Is64Bits> struct DenseMap
template <class ELFT, class RelT>
static unsigned handleTlsRelocation(uint32_t Type, SymbolBody &Body,
InputSectionBase<ELFT> &C, RelT &RI) {
+ typedef typename ELFT::uint uintX_t;
if (Target->pointsToLocalDynamicGotEntry(Type)) {
if (Target->canRelaxTls(Type, nullptr))
return 2;
if (Out<ELFT>::Got->addTlsIndex())
- Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel,
- DynamicReloc<ELFT>::Off_LTlsIndex,
- nullptr});
+ Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, Out<ELFT>::Got,
+ Out<ELFT>::Got->getTlsIndexOff(), false,
+ nullptr, 0});
return 1;
}
@@ -282,11 +283,12 @@ static unsigned handleTlsRelocation(uint
if (Target->isTlsGlobalDynamicRel(Type)) {
if (!Target->canRelaxTls(Type, &Body)) {
if (Out<ELFT>::Got->addDynTlsEntry(Body)) {
- Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel,
- DynamicReloc<ELFT>::Off_GTlsIndex,
- &Body});
+ uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body);
Out<ELFT>::RelaDyn->addReloc(
- {Target->TlsOffsetRel, DynamicReloc<ELFT>::Off_GTlsOffset, &Body});
+ {Target->TlsModuleIndexRel, Out<ELFT>::Got, Off, false, &Body, 0});
+ Out<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Out<ELFT>::Got,
+ Off + (uintX_t)sizeof(uintX_t), false,
+ &Body, 0});
}
return 1;
}
@@ -294,8 +296,9 @@ static unsigned handleTlsRelocation(uint
return 2;
if (!Body.isInGot()) {
Out<ELFT>::Got->addEntry(Body);
- Out<ELFT>::RelaDyn->addReloc(
- {Target->TlsGotRel, DynamicReloc<ELFT>::Off_Got, false, &Body});
+ Out<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, Out<ELFT>::Got,
+ Body.getGotOffset<ELFT>(), false, &Body,
+ 0});
}
return 2;
}
@@ -400,14 +403,16 @@ void Writer<ELFT>::scanRelocs(InputSecti
if (Target->UseLazyBinding) {
Out<ELFT>::GotPlt->addEntry(Body);
- Out<ELFT>::RelaPlt->addReloc(
- {Rel, DynamicReloc<ELFT>::Off_GotPlt, !Preemptible, &Body});
+ Out<ELFT>::RelaPlt->addReloc({Rel, Out<ELFT>::GotPlt,
+ Body.getGotPltOffset<ELFT>(),
+ !Preemptible, &Body, 0});
} else {
if (Body.isInGot())
continue;
Out<ELFT>::Got->addEntry(Body);
- Out<ELFT>::RelaDyn->addReloc(
- {Rel, DynamicReloc<ELFT>::Off_Got, !Preemptible, &Body});
+ Out<ELFT>::RelaDyn->addReloc({Rel, Out<ELFT>::Got,
+ Body.getGotOffset<ELFT>(), !Preemptible,
+ &Body, 0});
}
continue;
}
@@ -434,8 +439,9 @@ void Writer<ELFT>::scanRelocs(InputSecti
DynType = Target->GotRel;
else
DynType = Target->RelativeRel;
- Out<ELFT>::RelaDyn->addReloc(
- {DynType, DynamicReloc<ELFT>::Off_Got, !Preemptible, &Body});
+ Out<ELFT>::RelaDyn->addReloc({DynType, Out<ELFT>::Got,
+ Body.getGotOffset<ELFT>(), !Preemptible,
+ &Body, 0});
}
continue;
}
More information about the llvm-commits
mailing list