[lld] r250808 - [ELF2] - Lazy relocation support for x86_64.
Davide Italiano via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 29 15:36:02 PDT 2015
On Tue, Oct 20, 2015 at 1:54 AM, George Rimar via llvm-commits
<llvm-commits at lists.llvm.org> wrote:
> Author: grimar
> Date: Tue Oct 20 03:54:27 2015
> New Revision: 250808
>
> URL: http://llvm.org/viewvc/llvm-project?rev=250808&view=rev
> Log:
> [ELF2] - Lazy relocation support for x86_64.
>
> Target has supportsLazyRelocations() method which can switch lazy relocations on/off (currently all targets are OFF except x64 which is ON). So no any other targets are affected now.
>
> Differential Revision: http://reviews.llvm.org/D13856?id=37726
>
> Modified:
> lld/trunk/ELF/OutputSections.cpp
> lld/trunk/ELF/OutputSections.h
> lld/trunk/ELF/Symbols.h
> lld/trunk/ELF/Target.cpp
> lld/trunk/ELF/Target.h
> lld/trunk/ELF/Writer.cpp
> lld/trunk/test/elf2/dynamic-reloc-weak.s
> lld/trunk/test/elf2/plt.s
> lld/trunk/test/elf2/relocation.s
>
> Modified: lld/trunk/ELF/OutputSections.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=250808&r1=250807&r2=250808&view=diff
> ==============================================================================
> --- lld/trunk/ELF/OutputSections.cpp (original)
> +++ lld/trunk/ELF/OutputSections.cpp Tue Oct 20 03:54:27 2015
> @@ -30,6 +30,44 @@ OutputSectionBase<ELFT>::OutputSectionBa
> }
>
> template <class ELFT>
> +GotPltSection<ELFT>::GotPltSection()
> + : OutputSectionBase<ELFT>(".got.plt", llvm::ELF::SHT_PROGBITS,
> + llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
> + this->Header.sh_addralign = sizeof(uintX_t);
> + // .got.plt has 3 reserved entry
> + Entries.resize(3);
> +}
> +
> +template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) {
> + Sym->GotPltIndex = Entries.size();
> + Entries.push_back(Sym);
> +}
> +
> +template <class ELFT> bool GotPltSection<ELFT>::empty() const {
> + return Entries.size() == 3;
> +}
> +
> +template <class ELFT>
> +typename GotPltSection<ELFT>::uintX_t
> +GotPltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
> + return this->getVA() + B.GotPltIndex * sizeof(uintX_t);
> +}
> +
> +template <class ELFT> void GotPltSection<ELFT>::finalize() {
> + this->Header.sh_size = Entries.size() * sizeof(uintX_t);
> +}
> +
> +template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
> + write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(
> + Buf, Out<ELFT>::Dynamic->getVA());
> + for (const SymbolBody *B : Entries) {
> + if (B)
> + Target->writeGotPltEntry(Buf, Out<ELFT>::Plt->getEntryAddr(*B));
> + Buf += sizeof(uintX_t);
> + }
> +}
> +
> +template <class ELFT>
> GotSection<ELFT>::GotSection()
> : OutputSectionBase<ELFT>(".got", llvm::ELF::SHT_PROGBITS,
> llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
> @@ -67,10 +105,17 @@ PltSection<ELFT>::PltSection()
>
> template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
> size_t Off = 0;
> + bool LazyReloc = Target->supportsLazyRelocations();
> + if (LazyReloc) {
> + // First write PLT[0] entry which is special.
> + Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA());
> + Off += Target->getPltZeroEntrySize();
> + }
> for (const SymbolBody *E : Entries) {
> - uint64_t Got = Out<ELFT>::Got->getEntryAddr(*E);
> + uint64_t Got = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E)
> + : Out<ELFT>::Got->getEntryAddr(*E);
> uint64_t Plt = this->getVA() + Off;
> - Target->writePltEntry(Buf + Off, Got, Plt);
> + Target->writePltEntry(Buf + Off, Got, Plt, E->PltIndex);
> Off += Target->getPltEntrySize();
> }
> }
> @@ -83,17 +128,18 @@ template <class ELFT> void PltSection<EL
> template <class ELFT>
> typename PltSection<ELFT>::uintX_t
> PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
> - return this->getVA() + B.PltIndex * Target->getPltEntrySize();
> + return this->getVA() + Target->getPltZeroEntrySize() +
> + B.PltIndex * Target->getPltEntrySize();
> }
>
> -template <class ELFT>
> -void PltSection<ELFT>::finalize() {
> - this->Header.sh_size = Entries.size() * Target->getPltEntrySize();
> +template <class ELFT> void PltSection<ELFT>::finalize() {
> + this->Header.sh_size = Target->getPltZeroEntrySize() +
> + Entries.size() * Target->getPltEntrySize();
> }
>
> template <class ELFT>
> -RelocationSection<ELFT>::RelocationSection(bool IsRela)
> - : OutputSectionBase<ELFT>(IsRela ? ".rela.dyn" : ".rel.dyn",
> +RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
> + : OutputSectionBase<ELFT>(Name,
> IsRela ? llvm::ELF::SHT_RELA : llvm::ELF::SHT_REL,
> llvm::ELF::SHF_ALLOC),
> IsRela(IsRela) {
> @@ -118,11 +164,15 @@ template <class ELFT> void RelocationSec
> uint32_t Type = RI.getType(Config->Mips64EL);
> bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
> bool CanBePreempted = canBePreempted(Body, NeedsGot);
> + bool LazyReloc = Body && Target->supportsLazyRelocations() &&
> + Target->relocNeedsPlt(Type, *Body);
>
> if (CanBePreempted) {
> if (NeedsGot)
> P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
> - Target->getGotReloc(), Config->Mips64EL);
> + LazyReloc ? Target->getPltReloc()
> + : Target->getGotReloc(),
> + Config->Mips64EL);
> else
> P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type,
> Config->Mips64EL);
> @@ -130,10 +180,14 @@ template <class ELFT> void RelocationSec
> P->setSymbolAndType(0, Target->getRelativeReloc(), Config->Mips64EL);
> }
>
> - if (NeedsGot)
> - P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
> - else
> + if (NeedsGot) {
> + if (LazyReloc)
> + P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body);
> + else
> + P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
> + } else {
> P->r_offset = RI.r_offset + C.OutSec->getVA() + C.OutSecOff;
> + }
>
> uintX_t OrigAddend = 0;
> if (IsRela && !NeedsGot)
> @@ -256,6 +310,13 @@ template <class ELFT> void DynamicSectio
> ++NumEntries; // DT_RELASZ / DT_RELSZ
> ++NumEntries; // DT_RELAENT / DT_RELENT
> }
> + if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
> + ++NumEntries; // DT_JMPREL
> + ++NumEntries; // DT_PLTRELSZ
> + ++NumEntries; // DT_PLTGOT
> + ++NumEntries; // DT_PLTREL
> + }
> +
> ++NumEntries; // DT_SYMTAB
> ++NumEntries; // DT_SYMENT
> ++NumEntries; // DT_STRTAB
> @@ -325,6 +386,12 @@ template <class ELFT> void DynamicSectio
> WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
> IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
> }
> + if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
> + WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA());
> + WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize());
> + WritePtr(DT_PLTGOT, Out<ELFT>::GotPlt->getVA());
> + WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL);
> + }
>
> WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
> WritePtr(DT_SYMENT, sizeof(Elf_Sym));
> @@ -765,6 +832,11 @@ template class OutputSectionBase<ELF32BE
> template class OutputSectionBase<ELF64LE>;
> template class OutputSectionBase<ELF64BE>;
>
> +template class GotPltSection<ELF32LE>;
> +template class GotPltSection<ELF32BE>;
> +template class GotPltSection<ELF64LE>;
> +template class GotPltSection<ELF64BE>;
> +
> template class GotSection<ELF32LE>;
> template class GotSection<ELF32BE>;
> template class GotSection<ELF64LE>;
>
> Modified: lld/trunk/ELF/OutputSections.h
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=250808&r1=250807&r2=250808&view=diff
> ==============================================================================
> --- lld/trunk/ELF/OutputSections.h (original)
> +++ lld/trunk/ELF/OutputSections.h Tue Oct 20 03:54:27 2015
> @@ -122,6 +122,22 @@ private:
> std::vector<const SymbolBody *> Entries;
> };
>
> +template <class ELFT>
> +class GotPltSection final : public OutputSectionBase<ELFT> {
> + typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
> +
> +public:
> + GotPltSection();
> + void finalize() override;
> + void writeTo(uint8_t *Buf) override;
> + void addEntry(SymbolBody *Sym);
> + bool empty() const;
> + uintX_t getEntryAddr(const SymbolBody &B) const;
> +
> +private:
> + std::vector<const SymbolBody *> Entries;
> +};
> +
> template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
> typedef OutputSectionBase<ELFT> Base;
> typedef typename Base::uintX_t uintX_t;
> @@ -177,7 +193,7 @@ class RelocationSection final : public O
> typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
>
> public:
> - RelocationSection(bool IsRela);
> + RelocationSection(StringRef Name, bool IsRela);
> void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
> void finalize() override;
> void writeTo(uint8_t *Buf) override;
> @@ -294,6 +310,7 @@ private:
> // until Writer is initialized.
> template <class ELFT> struct Out {
> static DynamicSection<ELFT> *Dynamic;
> + static GotPltSection<ELFT> *GotPlt;
> static GotSection<ELFT> *Got;
> static HashTableSection<ELFT> *HashTab;
> static InterpSection<ELFT> *Interp;
> @@ -302,6 +319,7 @@ template <class ELFT> struct Out {
> static uint8_t *OpdBuf;
> static PltSection<ELFT> *Plt;
> static RelocationSection<ELFT> *RelaDyn;
> + static RelocationSection<ELFT> *RelaPlt;
> static StringTableSection<ELFT> *DynStrTab;
> static StringTableSection<ELFT> *StrTab;
> static SymbolTableSection<ELFT> *DynSymTab;
> @@ -309,6 +327,7 @@ template <class ELFT> struct Out {
> };
>
> template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
> +template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
> template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
> template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
> template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
> @@ -317,6 +336,7 @@ template <class ELFT> OutputSectionBase<
> template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
> template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
> template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
> +template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
> template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
> template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
> template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
>
> Modified: lld/trunk/ELF/Symbols.h
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.h?rev=250808&r1=250807&r2=250808&view=diff
> ==============================================================================
> --- lld/trunk/ELF/Symbols.h (original)
> +++ lld/trunk/ELF/Symbols.h Tue Oct 20 03:54:27 2015
> @@ -93,8 +93,10 @@ public:
> void setDynamicSymbolTableIndex(unsigned V) { DynamicSymbolTableIndex = V; }
>
> uint32_t GotIndex = -1;
> + uint32_t GotPltIndex = -1;
> uint32_t PltIndex = -1;
> bool isInGot() const { return GotIndex != -1U; }
> + bool isInGotPlt() const { return GotPltIndex != -1U; }
> bool isInPlt() const { return PltIndex != -1U; }
>
> // A SymbolBody has a backreference to a Symbol. Originally they are
>
> Modified: lld/trunk/ELF/Target.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Target.cpp?rev=250808&r1=250807&r2=250808&view=diff
> ==============================================================================
> --- lld/trunk/ELF/Target.cpp (original)
> +++ lld/trunk/ELF/Target.cpp Tue Oct 20 03:54:27 2015
> @@ -47,8 +47,11 @@ namespace {
> class X86TargetInfo final : public TargetInfo {
> public:
> X86TargetInfo();
> + void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
> + void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> + uint64_t PltEntryAddr) const override;
> void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> - uint64_t PltEntryAddr) const override;
> + uint64_t PltEntryAddr, int32_t Index) const override;
> bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
> bool relocPointsToGot(uint32_t Type) const override;
> bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
> @@ -61,8 +64,11 @@ class X86_64TargetInfo final : public Ta
> public:
> X86_64TargetInfo();
> unsigned getPLTRefReloc(unsigned Type) const override;
> + void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
> + void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> + uint64_t PltEntryAddr) const override;
> void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> - uint64_t PltEntryAddr) const override;
> + uint64_t PltEntryAddr, int32_t Index) const override;
> bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
> bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
> void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
> @@ -74,8 +80,11 @@ public:
> class PPC64TargetInfo final : public TargetInfo {
> public:
> PPC64TargetInfo();
> + void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
> + void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> + uint64_t PltEntryAddr) const override;
> void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> - uint64_t PltEntryAddr) const override;
> + uint64_t PltEntryAddr, int32_t Index) const override;
> bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
> bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
> void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
> @@ -87,8 +96,11 @@ public:
> class AArch64TargetInfo final : public TargetInfo {
> public:
> AArch64TargetInfo();
> + void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
> + void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> + uint64_t PltEntryAddr) const override;
> void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> - uint64_t PltEntryAddr) const override;
> + uint64_t PltEntryAddr, int32_t Index) const override;
> bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
> bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
> void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
> @@ -99,8 +111,11 @@ public:
> template <class ELFT> class MipsTargetInfo final : public TargetInfo {
> public:
> MipsTargetInfo();
> + void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
> + void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> + uint64_t PltEntryAddr) const override;
> void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> - uint64_t PltEntryAddr) const override;
> + uint64_t PltEntryAddr, int32_t Index) const override;
> bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
> bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
> void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
> @@ -144,10 +159,15 @@ X86TargetInfo::X86TargetInfo() {
> PCRelReloc = R_386_PC32;
> GotReloc = R_386_GLOB_DAT;
> GotRefReloc = R_386_GOT32;
> + PltReloc = R_386_JUMP_SLOT;
> }
>
> +void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
> +void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> + uint64_t PltEntryAddr) const {}
> +
> void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> - uint64_t PltEntryAddr) const {
> + uint64_t PltEntryAddr, int32_t Index) const {
> // jmpl *val; nop; nop
> const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
> memcpy(Buf, Inst, sizeof(Inst));
> @@ -194,19 +214,43 @@ X86_64TargetInfo::X86_64TargetInfo() {
> PCRelReloc = R_X86_64_PC32;
> GotReloc = R_X86_64_GLOB_DAT;
> GotRefReloc = R_X86_64_PC32;
> + PltReloc = R_X86_64_JUMP_SLOT;
> RelativeReloc = R_X86_64_RELATIVE;
> + LazyRelocations = true;
> + PltEntrySize = 16;
> + PltZeroEntrySize = 16;
> +}
> +
> +void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
> + // Skip 6 bytes of "jmpq *got(%rip)"
> + write32le(Buf, Plt + 6);
> +}
> +
> +void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> + uint64_t PltEntryAddr) const {
> + const uint8_t PltData[] = {
> + 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
> + 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
> + 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
> + };
> + memcpy(Buf, PltData, sizeof(PltData));
> + write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
> + write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
> }
Hi, sorry if I resurrect an old thread, but I have a comment on this.
It's not completely obvious for somebody without knowledge of the
linker why GotEntryAddr - PltEntryAddr {+ 2, + 4} should give you
GOT{+8, +16}. Do you mind to add a comment to elucidate?
>
> void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
> - uint64_t PltEntryAddr) const {
> - // jmpq *val(%rip); nop; nop
> - const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
> + uint64_t PltEntryAddr,
> + int32_t Index) const {
> + const uint8_t Inst[] = {
> + 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
> + 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
> + 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
> + };
> memcpy(Buf, Inst, sizeof(Inst));
>
> - uint64_t NextPC = PltEntryAddr + 6;
> - int64_t Delta = GotEntryAddr - NextPC;
> - assert(isInt<32>(Delta));
> - write32le(Buf + 2, Delta);
> + write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
Ditto for GotEntryAddr - PLTEntryAddr - 6.
Thanks,
--
Davide
More information about the llvm-commits
mailing list