diff options
Diffstat (limited to 'lld/ELF')
-rw-r--r-- | lld/ELF/OutputSections.cpp | 22 | ||||
-rw-r--r-- | lld/ELF/OutputSections.h | 3 | ||||
-rw-r--r-- | lld/ELF/Target.cpp | 107 | ||||
-rw-r--r-- | lld/ELF/Target.h | 5 |
4 files changed, 101 insertions, 36 deletions
diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 6563b1a61ca..0006ccf3c08 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -150,18 +150,25 @@ template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) { Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA()); Off += Target->getPltZeroEntrySize(); } - for (const SymbolBody *E : Entries) { - uint64_t Got = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E) - : Out<ELFT>::Got->getEntryAddr(*E); + for (std::pair<const SymbolBody *, unsigned> &I : Entries) { + const SymbolBody *E = I.first; + unsigned RelOff = I.second; + uint64_t GotVA = + LazyReloc ? Out<ELFT>::GotPlt->getVA() : Out<ELFT>::Got->getVA(); + uint64_t GotE = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E) + : Out<ELFT>::Got->getEntryAddr(*E); uint64_t Plt = this->getVA() + Off; - Target->writePltEntry(Buf + Off, Got, Plt, E->PltIndex); + Target->writePltEntry(Buf + Off, GotVA, GotE, Plt, E->PltIndex, RelOff); Off += Target->getPltEntrySize(); } } template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) { Sym->PltIndex = Entries.size(); - Entries.push_back(Sym); + unsigned RelOff = Target->supportsLazyRelocations() + ? Out<ELFT>::RelaPlt->getRelocOffset() + : Out<ELFT>::RelaDyn->getRelocOffset(); + Entries.push_back(std::make_pair(Sym, RelOff)); } template <class ELFT> @@ -281,6 +288,11 @@ template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) { } } +template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() { + const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); + return EntrySize * Relocs.size(); +} + template <class ELFT> void RelocationSection<ELFT>::finalize() { this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex; this->Header.sh_size = Relocs.size() * this->Header.sh_entsize; diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index f3266f5640d..27e6b68ab72 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -166,7 +166,7 @@ public: uintX_t getEntryAddr(const SymbolBody &B) const; private: - std::vector<const SymbolBody *> Entries; + std::vector<std::pair<const SymbolBody *, unsigned>> Entries; }; template <class ELFT> struct DynamicReloc { @@ -216,6 +216,7 @@ class RelocationSection final : public OutputSectionBase<ELFT> { public: RelocationSection(StringRef Name, bool IsRela); void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); } + unsigned getRelocOffset(); void finalize() override; void writeTo(uint8_t *Buf) override; bool hasRelocs() const { return !Relocs.empty(); } diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp index 78e5df6561b..bad344b0bc3 100644 --- a/lld/ELF/Target.cpp +++ b/lld/ELF/Target.cpp @@ -46,13 +46,15 @@ namespace { class X86TargetInfo final : public TargetInfo { public: X86TargetInfo(); + void writeGotPltHeaderEntries(uint8_t *Buf) const override; unsigned getDynReloc(unsigned Type) const override; bool isTlsDynReloc(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, int32_t Index) const override; + void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr, + uint64_t PltEntryAddr, int32_t Index, + unsigned RelOff) const override; bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override; bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; @@ -69,8 +71,9 @@ public: 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, int32_t Index) const override; + void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr, + uint64_t PltEntryAddr, int32_t Index, + unsigned RelOff) const override; bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override; bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; @@ -96,8 +99,9 @@ public: 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, int32_t Index) const override; + void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr, + uint64_t PltEntryAddr, int32_t Index, + unsigned RelOff) 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 *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, @@ -113,8 +117,9 @@ public: 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, int32_t Index) const override; + void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr, + uint64_t PltEntryAddr, int32_t Index, + unsigned RelOff) 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 *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, @@ -129,8 +134,9 @@ public: 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, int32_t Index) const override; + void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr, + uint64_t PltEntryAddr, int32_t Index, + unsigned RelOff) 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 *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, @@ -195,6 +201,18 @@ X86TargetInfo::X86TargetInfo() { GotReloc = R_386_GLOB_DAT; GotRefReloc = R_386_GOT32; PltReloc = R_386_JUMP_SLOT; + LazyRelocations = true; + PltEntrySize = 16; + PltZeroEntrySize = 16; +} + +void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const { + write32le(Buf, Out<ELF32LE>::Dynamic->getVA()); +} + +void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const { + // Skip 6 bytes of "pushl (GOT+4)" + write32le(Buf, Plt + 6); } unsigned X86TargetInfo::getDynReloc(unsigned Type) const { @@ -211,17 +229,44 @@ bool X86TargetInfo::isTlsDynReloc(unsigned Type) const { return false; } -void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, - uint64_t PltEntryAddr) const {} + uint64_t PltEntryAddr) const { + // Executable files and shared object files have + // separate procedure linkage tables. + if (Config->Shared) { + const uint8_t V[] = { + 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx + 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx) + 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop + }; + memcpy(Buf, V, sizeof(V)); + return; + } -void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, - uint64_t PltEntryAddr, int32_t Index) const { - // jmpl *val; nop; nop - const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; + const uint8_t PltData[] = { + 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4) + 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8) + 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop + }; + memcpy(Buf, PltData, sizeof(PltData)); + write32le(Buf + 2, GotEntryAddr + 4); // GOT+4 + write32le(Buf + 8, GotEntryAddr + 8); // GOT+8 +} + +void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr, + uint64_t GotEntryAddr, uint64_t PltEntryAddr, + int32_t Index, unsigned RelOff) const { + const uint8_t Inst[] = { + 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx) + 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset + 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC + }; memcpy(Buf, Inst, sizeof(Inst)); - assert(isUInt<32>(GotEntryAddr)); - write32le(Buf + 2, GotEntryAddr); + // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT + Buf[1] = Config->Shared ? 0xa3 : 0x25; + write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr); + write32le(Buf + 7, RelOff); + write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16); } bool X86TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const { @@ -303,9 +348,10 @@ void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16 } -void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, - uint64_t PltEntryAddr, - int32_t Index) const { +void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr, + uint64_t GotEntryAddr, + uint64_t PltEntryAddr, int32_t Index, + unsigned RelOff) const { const uint8_t Inst[] = { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip) 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index> @@ -604,8 +650,10 @@ uint64_t getPPC64TocBase() { void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr) const {} -void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, - uint64_t PltEntryAddr, int32_t Index) const { +void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr, + uint64_t GotEntryAddr, + uint64_t PltEntryAddr, int32_t Index, + unsigned RelOff) const { uint64_t Off = GotEntryAddr - getPPC64TocBase(); // FIXME: What we should do, in theory, is get the offset of the function @@ -816,9 +864,10 @@ void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, GotEntryAddr + 16); } -void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, - uint64_t PltEntryAddr, - int32_t Index) const { +void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr, + uint64_t GotEntryAddr, + uint64_t PltEntryAddr, int32_t Index, + unsigned RelOff) const { const uint8_t Inst[] = { 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] @@ -977,8 +1026,10 @@ template <class ELFT> void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr) const {} template <class ELFT> -void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, - uint64_t PltEntryAddr, int32_t Index) const {} +void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr, + uint64_t GotEntryAddr, + uint64_t PltEntryAddr, int32_t Index, + unsigned RelOff) const {} template <class ELFT> bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type, diff --git a/lld/ELF/Target.h b/lld/ELF/Target.h index d8132316bf8..939223414ec 100644 --- a/lld/ELF/Target.h +++ b/lld/ELF/Target.h @@ -50,8 +50,9 @@ public: virtual void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const = 0; virtual void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr) const = 0; - virtual void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, - uint64_t PltEntryAddr, int32_t Index) const = 0; + virtual void writePltEntry(uint8_t *Buf, uint64_t GotAddr, + uint64_t GotEntryAddr, uint64_t PltEntryAddr, + int32_t Index, unsigned RelOff) const = 0; virtual bool isRelRelative(uint32_t Type) const; virtual bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const; virtual bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const = 0; |