diff options
Diffstat (limited to 'contrib/llvm/tools/lld/ELF/Thunks.cpp')
-rw-r--r-- | contrib/llvm/tools/lld/ELF/Thunks.cpp | 236 |
1 files changed, 117 insertions, 119 deletions
diff --git a/contrib/llvm/tools/lld/ELF/Thunks.cpp b/contrib/llvm/tools/lld/ELF/Thunks.cpp index 397a0ee..07289d0 100644 --- a/contrib/llvm/tools/lld/ELF/Thunks.cpp +++ b/contrib/llvm/tools/lld/ELF/Thunks.cpp @@ -28,9 +28,10 @@ #include "Memory.h" #include "OutputSections.h" #include "Symbols.h" +#include "SyntheticSections.h" #include "Target.h" +#include "llvm/BinaryFormat/ELF.h" #include "llvm/Support/Casting.h" -#include "llvm/Support/ELF.h" #include "llvm/Support/Endian.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MathExtras.h" @@ -49,149 +50,195 @@ namespace { // Specific ARM Thunk implementations. The naming convention is: // Source State, TargetState, Target Requirement, ABS or PI, Range -template <class ELFT> -class ARMToThumbV7ABSLongThunk final : public Thunk<ELFT> { +class ARMV7ABSLongThunk final : public Thunk { public: - ARMToThumbV7ABSLongThunk(const SymbolBody &Dest, - const InputSection<ELFT> &Owner) - : Thunk<ELFT>(Dest, Owner) {} + ARMV7ABSLongThunk(const SymbolBody &Dest) : Thunk(Dest) {} uint32_t size() const override { return 12; } - void writeTo(uint8_t *Buf) const override; + void writeTo(uint8_t *Buf, ThunkSection &IS) const override; + void addSymbols(ThunkSection &IS) override; + bool isCompatibleWith(uint32_t RelocType) const override; }; -template <class ELFT> class ARMToThumbV7PILongThunk final : public Thunk<ELFT> { +class ARMV7PILongThunk final : public Thunk { public: - ARMToThumbV7PILongThunk(const SymbolBody &Dest, - const InputSection<ELFT> &Owner) - : Thunk<ELFT>(Dest, Owner) {} + ARMV7PILongThunk(const SymbolBody &Dest) : Thunk(Dest) {} uint32_t size() const override { return 16; } - void writeTo(uint8_t *Buf) const override; + void writeTo(uint8_t *Buf, ThunkSection &IS) const override; + void addSymbols(ThunkSection &IS) override; + bool isCompatibleWith(uint32_t RelocType) const override; }; -template <class ELFT> -class ThumbToARMV7ABSLongThunk final : public Thunk<ELFT> { +class ThumbV7ABSLongThunk final : public Thunk { public: - ThumbToARMV7ABSLongThunk(const SymbolBody &Dest, - const InputSection<ELFT> &Owner) - : Thunk<ELFT>(Dest, Owner) {} + ThumbV7ABSLongThunk(const SymbolBody &Dest) : Thunk(Dest) { Alignment = 2; } uint32_t size() const override { return 10; } - void writeTo(uint8_t *Buf) const override; + void writeTo(uint8_t *Buf, ThunkSection &IS) const override; + void addSymbols(ThunkSection &IS) override; + bool isCompatibleWith(uint32_t RelocType) const override; }; -template <class ELFT> class ThumbToARMV7PILongThunk final : public Thunk<ELFT> { +class ThumbV7PILongThunk final : public Thunk { public: - ThumbToARMV7PILongThunk(const SymbolBody &Dest, - const InputSection<ELFT> &Owner) - : Thunk<ELFT>(Dest, Owner) {} + ThumbV7PILongThunk(const SymbolBody &Dest) : Thunk(Dest) { Alignment = 2; } uint32_t size() const override { return 12; } - void writeTo(uint8_t *Buf) const override; + void writeTo(uint8_t *Buf, ThunkSection &IS) const override; + void addSymbols(ThunkSection &IS) override; + bool isCompatibleWith(uint32_t RelocType) const override; }; // MIPS LA25 thunk -template <class ELFT> class MipsThunk final : public Thunk<ELFT> { +class MipsThunk final : public Thunk { public: - MipsThunk(const SymbolBody &Dest, const InputSection<ELFT> &Owner) - : Thunk<ELFT>(Dest, Owner) {} + MipsThunk(const SymbolBody &Dest) : Thunk(Dest) {} uint32_t size() const override { return 16; } - void writeTo(uint8_t *Buf) const override; + void writeTo(uint8_t *Buf, ThunkSection &IS) const override; + void addSymbols(ThunkSection &IS) override; + InputSection *getTargetInputSection() const override; }; } // end anonymous namespace // ARM Target Thunks -template <class ELFT> static uint64_t getARMThunkDestVA(const SymbolBody &S) { - uint64_t V = S.isInPlt() ? S.getPltVA<ELFT>() : S.getVA<ELFT>(); +static uint64_t getARMThunkDestVA(const SymbolBody &S) { + uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA(); return SignExtend64<32>(V); } -template <class ELFT> -void ARMToThumbV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf) const { +void ARMV7ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const { const uint8_t Data[] = { 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S 0x1c, 0xff, 0x2f, 0xe1, // bx ip }; - uint64_t S = getARMThunkDestVA<ELFT>(this->Destination); + uint64_t S = getARMThunkDestVA(Destination); memcpy(Buf, Data, sizeof(Data)); Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S); Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S); } -template <class ELFT> -void ThumbToARMV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf) const { +void ARMV7ABSLongThunk::addSymbols(ThunkSection &IS) { + ThunkSym = addSyntheticLocal( + Saver.save("__ARMv7ABSLongThunk_" + Destination.getName()), STT_FUNC, + Offset, size(), &IS); + addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, &IS); +} + +bool ARMV7ABSLongThunk::isCompatibleWith(uint32_t RelocType) const { + // Thumb branch relocations can't use BLX + return RelocType != R_ARM_THM_JUMP19 && RelocType != R_ARM_THM_JUMP24; +} + +void ThumbV7ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const { const uint8_t Data[] = { 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S 0x60, 0x47, // bx ip }; - uint64_t S = getARMThunkDestVA<ELFT>(this->Destination); + uint64_t S = getARMThunkDestVA(Destination); memcpy(Buf, Data, sizeof(Data)); Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S); Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S); } -template <class ELFT> -void ARMToThumbV7PILongThunk<ELFT>::writeTo(uint8_t *Buf) const { +void ThumbV7ABSLongThunk::addSymbols(ThunkSection &IS) { + ThunkSym = addSyntheticLocal( + Saver.save("__Thumbv7ABSLongThunk_" + Destination.getName()), STT_FUNC, + Offset | 0x1, size(), &IS); + addSyntheticLocal("$t", STT_NOTYPE, Offset, 0, &IS); +} + +bool ThumbV7ABSLongThunk::isCompatibleWith(uint32_t RelocType) const { + // ARM branch relocations can't use BLX + return RelocType != R_ARM_JUMP24 && RelocType != R_ARM_PC24 && + RelocType != R_ARM_PLT32; +} + +void ARMV7PILongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const { const uint8_t Data[] = { 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) +8) 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P+4) +8) 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc 0x1c, 0xff, 0x2f, 0xe1, // bx r12 }; - uint64_t S = getARMThunkDestVA<ELFT>(this->Destination); - uint64_t P = this->getVA(); + uint64_t S = getARMThunkDestVA(Destination); + uint64_t P = ThunkSym->getVA(); memcpy(Buf, Data, sizeof(Data)); Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, S - P - 16); Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, S - P - 12); } -template <class ELFT> -void ThumbToARMV7PILongThunk<ELFT>::writeTo(uint8_t *Buf) const { +void ARMV7PILongThunk::addSymbols(ThunkSection &IS) { + ThunkSym = addSyntheticLocal( + Saver.save("__ARMV7PILongThunk_" + Destination.getName()), STT_FUNC, + Offset, size(), &IS); + addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, &IS); +} + +bool ARMV7PILongThunk::isCompatibleWith(uint32_t RelocType) const { + // Thumb branch relocations can't use BLX + return RelocType != R_ARM_THM_JUMP19 && RelocType != R_ARM_THM_JUMP24; +} + +void ThumbV7PILongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const { const uint8_t Data[] = { 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4) 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P+4) + 4) 0xfc, 0x44, // L1: add r12, pc 0x60, 0x47, // bx r12 }; - uint64_t S = getARMThunkDestVA<ELFT>(this->Destination); - uint64_t P = this->getVA(); + uint64_t S = getARMThunkDestVA(Destination); + uint64_t P = ThunkSym->getVA() & ~0x1; memcpy(Buf, Data, sizeof(Data)); Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, S - P - 12); Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, S - P - 8); } +void ThumbV7PILongThunk::addSymbols(ThunkSection &IS) { + ThunkSym = addSyntheticLocal( + Saver.save("__ThumbV7PILongThunk_" + Destination.getName()), STT_FUNC, + Offset | 0x1, size(), &IS); + addSyntheticLocal("$t", STT_NOTYPE, Offset, 0, &IS); +} + +bool ThumbV7PILongThunk::isCompatibleWith(uint32_t RelocType) const { + // ARM branch relocations can't use BLX + return RelocType != R_ARM_JUMP24 && RelocType != R_ARM_PC24 && + RelocType != R_ARM_PLT32; +} + // Write MIPS LA25 thunk code to call PIC function from the non-PIC one. -template <class ELFT> void MipsThunk<ELFT>::writeTo(uint8_t *Buf) const { - const endianness E = ELFT::TargetEndianness; - - uint64_t S = this->Destination.template getVA<ELFT>(); - write32<E>(Buf, 0x3c190000); // lui $25, %hi(func) - write32<E>(Buf + 4, 0x08000000 | (S >> 2)); // j func - write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func) - write32<E>(Buf + 12, 0x00000000); // nop +void MipsThunk::writeTo(uint8_t *Buf, ThunkSection &) const { + uint64_t S = Destination.getVA(); + write32(Buf, 0x3c190000, Config->Endianness); // lui $25, %hi(func) + write32(Buf + 4, 0x08000000 | (S >> 2), Config->Endianness); // j func + write32(Buf + 8, 0x27390000, Config->Endianness); // addiu $25, $25, %lo(func) + write32(Buf + 12, 0x00000000, Config->Endianness); // nop Target->relocateOne(Buf, R_MIPS_HI16, S); Target->relocateOne(Buf + 8, R_MIPS_LO16, S); } -template <class ELFT> -Thunk<ELFT>::Thunk(const SymbolBody &D, const InputSection<ELFT> &O) - : Destination(D), Owner(O), Offset(O.getThunkOff() + O.getThunksSize()) {} +void MipsThunk::addSymbols(ThunkSection &IS) { + ThunkSym = + addSyntheticLocal(Saver.save("__LA25Thunk_" + Destination.getName()), + STT_FUNC, Offset, size(), &IS); +} -template <class ELFT> typename ELFT::uint Thunk<ELFT>::getVA() const { - return Owner.OutSec->Addr + Owner.OutSecOff + Offset; +InputSection *MipsThunk::getTargetInputSection() const { + auto *DR = dyn_cast<DefinedRegular>(&Destination); + return dyn_cast<InputSection>(DR->Section); } -template <class ELFT> Thunk<ELFT>::~Thunk() = default; +Thunk::Thunk(const SymbolBody &D) : Destination(D), Offset(0) {} + +Thunk::~Thunk() = default; // Creates a thunk for Thumb-ARM interworking. -template <class ELFT> -static Thunk<ELFT> *createThunkArm(uint32_t Reloc, SymbolBody &S, - InputSection<ELFT> &IS) { +static Thunk *addThunkArm(uint32_t Reloc, SymbolBody &S) { // ARM relocations need ARM to Thumb interworking Thunks. // Thumb relocations need Thumb to ARM relocations. // Use position independent Thunks if we require position independent code. @@ -200,76 +247,27 @@ static Thunk<ELFT> *createThunkArm(uint32_t Reloc, SymbolBody &S, case R_ARM_PLT32: case R_ARM_JUMP24: if (Config->Pic) - return new (BAlloc) ARMToThumbV7PILongThunk<ELFT>(S, IS); - return new (BAlloc) ARMToThumbV7ABSLongThunk<ELFT>(S, IS); + return make<ARMV7PILongThunk>(S); + return make<ARMV7ABSLongThunk>(S); case R_ARM_THM_JUMP19: case R_ARM_THM_JUMP24: if (Config->Pic) - return new (BAlloc) ThumbToARMV7PILongThunk<ELFT>(S, IS); - return new (BAlloc) ThumbToARMV7ABSLongThunk<ELFT>(S, IS); + return make<ThumbV7PILongThunk>(S); + return make<ThumbV7ABSLongThunk>(S); } fatal("unrecognized relocation type"); } -template <class ELFT> -static void addThunkARM(uint32_t Reloc, SymbolBody &S, InputSection<ELFT> &IS) { - // Only one Thunk supported per symbol. - if (S.hasThunk<ELFT>()) - return; - - // ARM Thunks are added to the same InputSection as the relocation. This - // isn't strictly necessary but it makes it more likely that a limited range - // branch can reach the Thunk, and it makes Thunks to the PLT section easier - Thunk<ELFT> *T = createThunkArm(Reloc, S, IS); - IS.addThunk(T); - if (auto *Sym = dyn_cast<DefinedRegular<ELFT>>(&S)) - Sym->ThunkData = T; - else if (auto *Sym = dyn_cast<SharedSymbol<ELFT>>(&S)) - Sym->ThunkData = T; - else if (auto *Sym = dyn_cast<Undefined<ELFT>>(&S)) - Sym->ThunkData = T; - else - fatal("symbol not DefinedRegular or Shared"); -} +static Thunk *addThunkMips(SymbolBody &S) { return make<MipsThunk>(S); } -template <class ELFT> -static void addThunkMips(uint32_t RelocType, SymbolBody &S, - InputSection<ELFT> &IS) { - // Only one Thunk supported per symbol. - if (S.hasThunk<ELFT>()) - return; - - // Mips Thunks are added to the InputSection defining S. - auto *R = cast<DefinedRegular<ELFT>>(&S); - auto *Sec = cast<InputSection<ELFT>>(R->Section); - auto *T = new (BAlloc) MipsThunk<ELFT>(S, *Sec); - Sec->addThunk(T); - R->ThunkData = T; -} - -template <class ELFT> -void addThunk(uint32_t RelocType, SymbolBody &S, InputSection<ELFT> &IS) { +Thunk *addThunk(uint32_t RelocType, SymbolBody &S) { if (Config->EMachine == EM_ARM) - addThunkARM<ELFT>(RelocType, S, IS); + return addThunkArm(RelocType, S); else if (Config->EMachine == EM_MIPS) - addThunkMips<ELFT>(RelocType, S, IS); - else - llvm_unreachable("add Thunk only supported for ARM and Mips"); + return addThunkMips(S); + llvm_unreachable("add Thunk only supported for ARM and Mips"); + return nullptr; } -template void addThunk<ELF32LE>(uint32_t, SymbolBody &, - InputSection<ELF32LE> &); -template void addThunk<ELF32BE>(uint32_t, SymbolBody &, - InputSection<ELF32BE> &); -template void addThunk<ELF64LE>(uint32_t, SymbolBody &, - InputSection<ELF64LE> &); -template void addThunk<ELF64BE>(uint32_t, SymbolBody &, - InputSection<ELF64BE> &); - -template class Thunk<ELF32LE>; -template class Thunk<ELF32BE>; -template class Thunk<ELF64LE>; -template class Thunk<ELF64BE>; - } // end namespace elf } // end namespace lld |