Skip to content

Commit

Permalink
[ELF] - Detemplate MipsGotSection<ELFT>
Browse files Browse the repository at this point in the history
This continues detemplation process.

Detemplating MipsGotSection<ELFT> is helpfull because can
help to detemplate getRelocTargetVA. (one more change is required)
It opens road to detemplation of GotSection<ELFT> and probably
something else after that.

Differential revision: https://reviews.llvm.org/D31090

llvm-svn: 298272
George Rimar committed Mar 20, 2017
1 parent 25a17ba commit 14534eb
Showing 3 changed files with 63 additions and 83 deletions.
123 changes: 53 additions & 70 deletions lld/ELF/SyntheticSections.cpp
Original file line number Diff line number Diff line change
@@ -665,14 +665,11 @@ template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
this->template relocate<ELFT>(Buf, Buf + Size);
}

template <class ELFT>
MipsGotSection<ELFT>::MipsGotSection()
MipsGotSection::MipsGotSection()
: SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16,
".got") {}

template <class ELFT>
void MipsGotSection<ELFT>::addEntry(SymbolBody &Sym, int64_t Addend,
RelExpr Expr) {
void MipsGotSection::addEntry(SymbolBody &Sym, int64_t Addend, RelExpr Expr) {
// For "true" local symbols which can be referenced from the same module
// only compiler creates two instructions for address loading:
//
@@ -715,7 +712,7 @@ void MipsGotSection<ELFT>::addEntry(SymbolBody &Sym, int64_t Addend,
TlsEntries.push_back(&Sym);
return;
}
auto AddEntry = [&](SymbolBody &S, uintX_t A, GotEntries &Items) {
auto AddEntry = [&](SymbolBody &S, uint64_t A, GotEntries &Items) {
if (S.isInGot() && !A)
return;
size_t NewIndex = Items.size();
@@ -740,8 +737,7 @@ void MipsGotSection<ELFT>::addEntry(SymbolBody &Sym, int64_t Addend,
}
}

template <class ELFT>
bool MipsGotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
bool MipsGotSection::addDynTlsEntry(SymbolBody &Sym) {
if (Sym.GlobalDynIndex != -1U)
return false;
Sym.GlobalDynIndex = TlsEntries.size();
@@ -753,10 +749,10 @@ bool MipsGotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {

// Reserves TLS entries for a TLS module ID and a TLS block offset.
// In total it takes two GOT slots.
template <class ELFT> bool MipsGotSection<ELFT>::addTlsIndex() {
bool MipsGotSection::addTlsIndex() {
if (TlsIndexOff != uint32_t(-1))
return false;
TlsIndexOff = TlsEntries.size() * sizeof(uintX_t);
TlsIndexOff = TlsEntries.size() * Config->Wordsize;
TlsEntries.push_back(nullptr);
TlsEntries.push_back(nullptr);
return true;
@@ -770,25 +766,21 @@ static uint64_t getMipsPageCount(uint64_t Size) {
return (Size + 0xfffe) / 0xffff + 1;
}

template <class ELFT>
typename MipsGotSection<ELFT>::uintX_t
MipsGotSection<ELFT>::getPageEntryOffset(const SymbolBody &B,
int64_t Addend) const {
uint64_t MipsGotSection::getPageEntryOffset(const SymbolBody &B,
int64_t Addend) const {
const OutputSection *OutSec =
cast<DefinedRegular>(&B)->Section->getOutputSection();
uintX_t SecAddr = getMipsPageAddr(OutSec->Addr);
uintX_t SymAddr = getMipsPageAddr(B.getVA(Addend));
uintX_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff;
uint64_t SecAddr = getMipsPageAddr(OutSec->Addr);
uint64_t SymAddr = getMipsPageAddr(B.getVA(Addend));
uint64_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff;
assert(Index < PageEntriesNum);
return (HeaderEntriesNum + Index) * sizeof(uintX_t);
return (HeaderEntriesNum + Index) * Config->Wordsize;
}

template <class ELFT>
typename MipsGotSection<ELFT>::uintX_t
MipsGotSection<ELFT>::getBodyEntryOffset(const SymbolBody &B,
int64_t Addend) const {
uint64_t MipsGotSection::getBodyEntryOffset(const SymbolBody &B,
int64_t Addend) const {
// Calculate offset of the GOT entries block: TLS, global, local.
uintX_t Index = HeaderEntriesNum + PageEntriesNum;
uint64_t Index = HeaderEntriesNum + PageEntriesNum;
if (B.isTls())
Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size();
else if (B.IsInGlobalMipsGot)
@@ -803,37 +795,31 @@ MipsGotSection<ELFT>::getBodyEntryOffset(const SymbolBody &B,
assert(It != EntryIndexMap.end());
Index += It->second;
}
return Index * sizeof(uintX_t);
return Index * Config->Wordsize;
}

template <class ELFT>
typename MipsGotSection<ELFT>::uintX_t
MipsGotSection<ELFT>::getTlsOffset() const {
return (getLocalEntriesNum() + GlobalEntries.size()) * sizeof(uintX_t);
uint64_t MipsGotSection::getTlsOffset() const {
return (getLocalEntriesNum() + GlobalEntries.size()) * Config->Wordsize;
}

template <class ELFT>
typename MipsGotSection<ELFT>::uintX_t
MipsGotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
return B.GlobalDynIndex * sizeof(uintX_t);
uint64_t MipsGotSection::getGlobalDynOffset(const SymbolBody &B) const {
return B.GlobalDynIndex * Config->Wordsize;
}

template <class ELFT>
const SymbolBody *MipsGotSection<ELFT>::getFirstGlobalEntry() const {
const SymbolBody *MipsGotSection::getFirstGlobalEntry() const {
return GlobalEntries.empty() ? nullptr : GlobalEntries.front().first;
}

template <class ELFT>
unsigned MipsGotSection<ELFT>::getLocalEntriesNum() const {
unsigned MipsGotSection::getLocalEntriesNum() const {
return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() +
LocalEntries32.size();
}

template <class ELFT> void MipsGotSection<ELFT>::finalizeContents() {
void MipsGotSection::finalizeContents() {
updateAllocSize();
}

template <class ELFT> void MipsGotSection<ELFT>::updateAllocSize() {
void MipsGotSection::updateAllocSize() {
PageEntriesNum = 0;
for (std::pair<const OutputSection *, size_t> &P : PageIndexMap) {
// For each output section referenced by GOT page relocations calculate
@@ -846,27 +832,29 @@ template <class ELFT> void MipsGotSection<ELFT>::updateAllocSize() {
PageEntriesNum += getMipsPageCount(P.first->Size);
}
Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) *
sizeof(uintX_t);
Config->Wordsize;
}

template <class ELFT> bool MipsGotSection<ELFT>::empty() const {
bool MipsGotSection::empty() const {
// We add the .got section to the result for dynamic MIPS target because
// its address and properties are mentioned in the .dynamic section.
return Config->Relocatable;
}

template <class ELFT>
typename MipsGotSection<ELFT>::uintX_t MipsGotSection<ELFT>::getGp() const {
uint64_t MipsGotSection::getGp() const {
return ElfSym::MipsGp->getVA(0);
}

template <class ELFT>
static void writeUint(uint8_t *Buf, typename ELFT::uint Val) {
typedef typename ELFT::uint uintX_t;
write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val);
static void writeUint(uint8_t *Buf, uint64_t Val) {
support::endianness E =
Config->IsLE ? support::endianness::little : support::endianness::big;
if (Config->Wordsize == 0)
write64(Buf, Val, E);
else
write32(Buf, Val, E);
}

template <class ELFT> void MipsGotSection<ELFT>::writeTo(uint8_t *Buf) {
void MipsGotSection::writeTo(uint8_t *Buf) {
// Set the MSB of the second GOT slot. This is not required by any
// MIPS ABI documentation, though.
//
@@ -881,25 +869,24 @@ template <class ELFT> void MipsGotSection<ELFT>::writeTo(uint8_t *Buf) {
// we've been doing this for years, it is probably a safe bet to
// keep doing this for now. We really need to revisit this to see
// if we had to do this.
auto *P = reinterpret_cast<typename ELFT::Off *>(Buf);
P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Buf += HeaderEntriesNum * sizeof(uintX_t);
writeUint(Buf + Config->Wordsize, (uint64_t)1 << (Config->Wordsize * 8 - 1));
Buf += HeaderEntriesNum * Config->Wordsize;
// Write 'page address' entries to the local part of the GOT.
for (std::pair<const OutputSection *, size_t> &L : PageIndexMap) {
size_t PageCount = getMipsPageCount(L.first->Size);
uintX_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
uint64_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
for (size_t PI = 0; PI < PageCount; ++PI) {
uint8_t *Entry = Buf + (L.second + PI) * sizeof(uintX_t);
writeUint<ELFT>(Entry, FirstPageAddr + PI * 0x10000);
uint8_t *Entry = Buf + (L.second + PI) * Config->Wordsize;
writeUint(Entry, FirstPageAddr + PI * 0x10000);
}
}
Buf += PageEntriesNum * sizeof(uintX_t);
Buf += PageEntriesNum * Config->Wordsize;
auto AddEntry = [&](const GotEntry &SA) {
uint8_t *Entry = Buf;
Buf += sizeof(uintX_t);
Buf += Config->Wordsize;
const SymbolBody *Body = SA.first;
uintX_t VA = Body->getVA(SA.second);
writeUint<ELFT>(Entry, VA);
uint64_t VA = Body->getVA(SA.second);
writeUint(Entry, VA);
};
std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry);
std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry);
@@ -910,20 +897,20 @@ template <class ELFT> void MipsGotSection<ELFT>::writeTo(uint8_t *Buf) {
// for thread-local storage.
// https://www.linux-mips.org/wiki/NPTL
if (TlsIndexOff != -1U && !Config->Pic)
writeUint<ELFT>(Buf + TlsIndexOff, 1);
writeUint(Buf + TlsIndexOff, 1);
for (const SymbolBody *B : TlsEntries) {
if (!B || B->isPreemptible())
continue;
uintX_t VA = B->getVA();
uint64_t VA = B->getVA();
if (B->GotIndex != -1U) {
uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t);
writeUint<ELFT>(Entry, VA - 0x7000);
uint8_t *Entry = Buf + B->GotIndex * Config->Wordsize;
writeUint(Entry, VA - 0x7000);
}
if (B->GlobalDynIndex != -1U) {
uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t);
writeUint<ELFT>(Entry, 1);
Entry += sizeof(uintX_t);
writeUint<ELFT>(Entry, VA - 0x8000);
uint8_t *Entry = Buf + B->GlobalDynIndex * Config->Wordsize;
writeUint(Entry, 1);
Entry += Config->Wordsize;
writeUint(Entry, VA - 0x8000);
}
}
}
@@ -2254,6 +2241,7 @@ StringTableSection *InX::DynStrTab;
InputSection *InX::Interp;
GotPltSection *InX::GotPlt;
IgotPltSection *InX::IgotPlt;
MipsGotSection *InX::MipsGot;
MipsRldMapSection *InX::MipsRldMap;
PltSection *InX::Plt;
PltSection *InX::Iplt;
@@ -2308,11 +2296,6 @@ template class elf::GotSection<ELF32BE>;
template class elf::GotSection<ELF64LE>;
template class elf::GotSection<ELF64BE>;

template class elf::MipsGotSection<ELF32LE>;
template class elf::MipsGotSection<ELF32BE>;
template class elf::MipsGotSection<ELF64LE>;
template class elf::MipsGotSection<ELF64BE>;

template class elf::DynamicSection<ELF32LE>;
template class elf::DynamicSection<ELF32BE>;
template class elf::DynamicSection<ELF64LE>;
21 changes: 9 additions & 12 deletions lld/ELF/SyntheticSections.h
Original file line number Diff line number Diff line change
@@ -169,9 +169,7 @@ class BssSection final : public SyntheticSection {
size_t Size = 0;
};

template <class ELFT> class MipsGotSection final : public SyntheticSection {
typedef typename ELFT::uint uintX_t;

class MipsGotSection final : public SyntheticSection {
public:
MipsGotSection();
void writeTo(uint8_t *Buf) override;
@@ -182,9 +180,9 @@ template <class ELFT> class MipsGotSection final : public SyntheticSection {
void addEntry(SymbolBody &Sym, int64_t Addend, RelExpr Expr);
bool addDynTlsEntry(SymbolBody &Sym);
bool addTlsIndex();
uintX_t getPageEntryOffset(const SymbolBody &B, int64_t Addend) const;
uintX_t getBodyEntryOffset(const SymbolBody &B, int64_t Addend) const;
uintX_t getGlobalDynOffset(const SymbolBody &B) const;
uint64_t getPageEntryOffset(const SymbolBody &B, int64_t Addend) const;
uint64_t getBodyEntryOffset(const SymbolBody &B, int64_t Addend) const;
uint64_t getGlobalDynOffset(const SymbolBody &B) const;

// Returns the symbol which corresponds to the first entry of the global part
// of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
@@ -198,11 +196,11 @@ template <class ELFT> class MipsGotSection final : public SyntheticSection {

// Returns offset of TLS part of the MIPS GOT table. This part goes
// after 'local' and 'global' entries.
uintX_t getTlsOffset() const;
uint64_t getTlsOffset() const;

uint32_t getTlsIndexOff() const { return TlsIndexOff; }

uintX_t getGp() const;
uint64_t getGp() const;

private:
// MIPS GOT consists of three parts: local, global and tls. Each part
@@ -250,7 +248,7 @@ template <class ELFT> class MipsGotSection final : public SyntheticSection {
// to the first index of "Page" entries allocated for this section.
llvm::SmallMapVector<const OutputSection *, size_t, 16> PageIndexMap;

typedef std::pair<const SymbolBody *, uintX_t> GotEntry;
typedef std::pair<const SymbolBody *, uint64_t> GotEntry;
typedef std::vector<GotEntry> GotEntries;
// Map from Symbol-Addend pair to the GOT index.
llvm::DenseMap<GotEntry, size_t> EntryIndexMap;
@@ -266,7 +264,7 @@ template <class ELFT> class MipsGotSection final : public SyntheticSection {
std::vector<const SymbolBody *> TlsEntries;

uint32_t TlsIndexOff = -1;
uintX_t Size = 0;
uint64_t Size = 0;
};

class GotPltSection final : public SyntheticSection {
@@ -766,6 +764,7 @@ struct InX {
static InputSection *Interp;
static GotPltSection *GotPlt;
static IgotPltSection *IgotPlt;
static MipsGotSection *MipsGot;
static MipsRldMapSection *MipsRldMap;
static PltSection *Plt;
static PltSection *Iplt;
@@ -781,7 +780,6 @@ template <class ELFT> struct In : public InX {
static GdbIndexSection<ELFT> *GdbIndex;
static GotSection<ELFT> *Got;
static EhFrameSection<ELFT> *EhFrame;
static MipsGotSection<ELFT> *MipsGot;
static HashTableSection<ELFT> *HashTab;
static RelocationSection<ELFT> *RelaDyn;
static RelocationSection<ELFT> *RelaPlt;
@@ -799,7 +797,6 @@ template <class ELFT> GdbIndexSection<ELFT> *In<ELFT>::GdbIndex;
template <class ELFT> GnuHashTableSection<ELFT> *In<ELFT>::GnuHashTab;
template <class ELFT> GotSection<ELFT> *In<ELFT>::Got;
template <class ELFT> EhFrameSection<ELFT> *In<ELFT>::EhFrame;
template <class ELFT> MipsGotSection<ELFT> *In<ELFT>::MipsGot;
template <class ELFT> HashTableSection<ELFT> *In<ELFT>::HashTab;
template <class ELFT> RelocationSection<ELFT> *In<ELFT>::RelaDyn;
template <class ELFT> RelocationSection<ELFT> *In<ELFT>::RelaPlt;
2 changes: 1 addition & 1 deletion lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
@@ -415,7 +415,7 @@ template <class ELFT> void Writer<ELFT>::createSyntheticSections() {
// Add .got. MIPS' .got is so different from the other archs,
// it has its own class.
if (Config->EMachine == EM_MIPS) {
In<ELFT>::MipsGot = make<MipsGotSection<ELFT>>();
In<ELFT>::MipsGot = make<MipsGotSection>();
Add(In<ELFT>::MipsGot);
} else {
In<ELFT>::Got = make<GotSection<ELFT>>();

0 comments on commit 14534eb

Please sign in to comment.