1 //===- Thunks.cpp --------------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===---------------------------------------------------------------------===// 9 // 10 // This file contains Thunk subclasses. 11 // 12 // A thunk is a small piece of code written after an input section 13 // which is used to jump between "incompatible" functions 14 // such as MIPS PIC and non-PIC or ARM non-Thumb and Thumb functions. 15 // 16 // If a jump target is too far and its address doesn't fit to a 17 // short jump instruction, we need to create a thunk too, but we 18 // haven't supported it yet. 19 // 20 // i386 and x86-64 don't need thunks. 21 // 22 //===---------------------------------------------------------------------===// 23 24 #include "Thunks.h" 25 #include "Config.h" 26 #include "Error.h" 27 #include "InputSection.h" 28 #include "Memory.h" 29 #include "OutputSections.h" 30 #include "Symbols.h" 31 #include "SyntheticSections.h" 32 #include "Target.h" 33 #include "llvm/Support/Casting.h" 34 #include "llvm/Support/ELF.h" 35 #include "llvm/Support/Endian.h" 36 #include "llvm/Support/ErrorHandling.h" 37 #include "llvm/Support/MathExtras.h" 38 #include <cstdint> 39 #include <cstring> 40 41 using namespace llvm; 42 using namespace llvm::object; 43 using namespace llvm::support::endian; 44 using namespace llvm::ELF; 45 46 namespace lld { 47 namespace elf { 48 49 namespace { 50 51 // Specific ARM Thunk implementations. The naming convention is: 52 // Source State, TargetState, Target Requirement, ABS or PI, Range 53 template <class ELFT> class ARMToThumbV7ABSLongThunk final : public Thunk { 54 public: 55 ARMToThumbV7ABSLongThunk(const SymbolBody &Dest) : Thunk(Dest) {} 56 57 uint32_t size() const override { return 12; } 58 void writeTo(uint8_t *Buf, ThunkSection &IS) const override; 59 void addSymbols(ThunkSection &IS) override; 60 }; 61 62 template <class ELFT> class ARMToThumbV7PILongThunk final : public Thunk { 63 public: 64 ARMToThumbV7PILongThunk(const SymbolBody &Dest) : Thunk(Dest) {} 65 66 uint32_t size() const override { return 16; } 67 void writeTo(uint8_t *Buf, ThunkSection &IS) const override; 68 void addSymbols(ThunkSection &IS) override; 69 }; 70 71 template <class ELFT> class ThumbToARMV7ABSLongThunk final : public Thunk { 72 public: 73 ThumbToARMV7ABSLongThunk(const SymbolBody &Dest) : Thunk(Dest) { 74 this->alignment = 2; 75 } 76 77 uint32_t size() const override { return 10; } 78 void writeTo(uint8_t *Buf, ThunkSection &IS) const override; 79 void addSymbols(ThunkSection &IS) override; 80 }; 81 82 template <class ELFT> class ThumbToARMV7PILongThunk final : public Thunk { 83 public: 84 ThumbToARMV7PILongThunk(const SymbolBody &Dest) : Thunk(Dest) { 85 this->alignment = 2; 86 } 87 88 uint32_t size() const override { return 12; } 89 void writeTo(uint8_t *Buf, ThunkSection &IS) const override; 90 void addSymbols(ThunkSection &IS) override; 91 }; 92 93 // MIPS LA25 thunk 94 template <class ELFT> class MipsThunk final : public Thunk { 95 public: 96 MipsThunk(const SymbolBody &Dest) : Thunk(Dest) {} 97 98 uint32_t size() const override { return 16; } 99 void writeTo(uint8_t *Buf, ThunkSection &IS) const override; 100 void addSymbols(ThunkSection &IS) override; 101 InputSection *getTargetInputSection() const override; 102 }; 103 104 } // end anonymous namespace 105 106 // ARM Target Thunks 107 static uint64_t getARMThunkDestVA(const SymbolBody &S) { 108 uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA(); 109 return SignExtend64<32>(V); 110 } 111 112 template <class ELFT> 113 void ARMToThumbV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf, 114 ThunkSection &IS) const { 115 const uint8_t Data[] = { 116 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S 117 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S 118 0x1c, 0xff, 0x2f, 0xe1, // bx ip 119 }; 120 uint64_t S = getARMThunkDestVA(this->Destination); 121 memcpy(Buf, Data, sizeof(Data)); 122 Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S); 123 Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S); 124 } 125 126 template <class ELFT> 127 void ARMToThumbV7ABSLongThunk<ELFT>::addSymbols(ThunkSection &IS) { 128 this->ThunkSym = addSyntheticLocal<ELFT>( 129 Saver.save("__ARMToThumbv7ABSLongThunk_" + this->Destination.getName()), 130 STT_FUNC, this->Offset, size(), &IS); 131 addSyntheticLocal<ELFT>("$a", STT_NOTYPE, this->Offset, 0, &IS); 132 } 133 134 template <class ELFT> 135 void ThumbToARMV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf, 136 ThunkSection &IS) const { 137 const uint8_t Data[] = { 138 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S 139 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S 140 0x60, 0x47, // bx ip 141 }; 142 uint64_t S = getARMThunkDestVA(this->Destination); 143 memcpy(Buf, Data, sizeof(Data)); 144 Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S); 145 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S); 146 } 147 148 template <class ELFT> 149 void ThumbToARMV7ABSLongThunk<ELFT>::addSymbols(ThunkSection &IS) { 150 this->ThunkSym = addSyntheticLocal<ELFT>( 151 Saver.save("__ThumbToARMv7ABSLongThunk_" + this->Destination.getName()), 152 STT_FUNC, this->Offset, size(), &IS); 153 addSyntheticLocal<ELFT>("$t", STT_NOTYPE, this->Offset, 0, &IS); 154 } 155 156 template <class ELFT> 157 void ARMToThumbV7PILongThunk<ELFT>::writeTo(uint8_t *Buf, 158 ThunkSection &IS) const { 159 const uint8_t Data[] = { 160 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) +8) 161 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P+4) +8) 162 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc 163 0x1c, 0xff, 0x2f, 0xe1, // bx r12 164 }; 165 uint64_t S = getARMThunkDestVA(this->Destination); 166 uint64_t P = this->ThunkSym->getVA(); 167 memcpy(Buf, Data, sizeof(Data)); 168 Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, S - P - 16); 169 Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, S - P - 12); 170 } 171 172 template <class ELFT> 173 void ARMToThumbV7PILongThunk<ELFT>::addSymbols(ThunkSection &IS) { 174 this->ThunkSym = addSyntheticLocal<ELFT>( 175 Saver.save("__ARMToThumbV7PILongThunk_" + this->Destination.getName()), 176 STT_FUNC, this->Offset, size(), &IS); 177 addSyntheticLocal<ELFT>("$a", STT_NOTYPE, this->Offset, 0, &IS); 178 } 179 180 template <class ELFT> 181 void ThumbToARMV7PILongThunk<ELFT>::writeTo(uint8_t *Buf, 182 ThunkSection &IS) const { 183 const uint8_t Data[] = { 184 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4) 185 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P+4) + 4) 186 0xfc, 0x44, // L1: add r12, pc 187 0x60, 0x47, // bx r12 188 }; 189 uint64_t S = getARMThunkDestVA(this->Destination); 190 uint64_t P = this->ThunkSym->getVA(); 191 memcpy(Buf, Data, sizeof(Data)); 192 Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, S - P - 12); 193 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, S - P - 8); 194 } 195 196 template <class ELFT> 197 void ThumbToARMV7PILongThunk<ELFT>::addSymbols(ThunkSection &IS) { 198 this->ThunkSym = addSyntheticLocal<ELFT>( 199 Saver.save("__ThumbToARMV7PILongThunk_" + this->Destination.getName()), 200 STT_FUNC, this->Offset, size(), &IS); 201 addSyntheticLocal<ELFT>("$t", STT_NOTYPE, this->Offset, 0, &IS); 202 } 203 204 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one. 205 template <class ELFT> 206 void MipsThunk<ELFT>::writeTo(uint8_t *Buf, ThunkSection &) const { 207 const endianness E = ELFT::TargetEndianness; 208 209 uint64_t S = this->Destination.getVA(); 210 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func) 211 write32<E>(Buf + 4, 0x08000000 | (S >> 2)); // j func 212 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func) 213 write32<E>(Buf + 12, 0x00000000); // nop 214 Target->relocateOne(Buf, R_MIPS_HI16, S); 215 Target->relocateOne(Buf + 8, R_MIPS_LO16, S); 216 } 217 218 template <class ELFT> void MipsThunk<ELFT>::addSymbols(ThunkSection &IS) { 219 this->ThunkSym = addSyntheticLocal<ELFT>( 220 Saver.save("__LA25Thunk_" + this->Destination.getName()), STT_FUNC, 221 this->Offset, size(), &IS); 222 } 223 224 template <class ELFT> 225 InputSection *MipsThunk<ELFT>::getTargetInputSection() const { 226 auto *DR = dyn_cast<DefinedRegular>(&this->Destination); 227 return dyn_cast<InputSection>(DR->Section); 228 } 229 230 Thunk::Thunk(const SymbolBody &D) : Destination(D), Offset(0) {} 231 232 Thunk::~Thunk() = default; 233 234 // Creates a thunk for Thumb-ARM interworking. 235 template <class ELFT> 236 static Thunk *addThunkArm(uint32_t Reloc, SymbolBody &S) { 237 // ARM relocations need ARM to Thumb interworking Thunks. 238 // Thumb relocations need Thumb to ARM relocations. 239 // Use position independent Thunks if we require position independent code. 240 switch (Reloc) { 241 case R_ARM_PC24: 242 case R_ARM_PLT32: 243 case R_ARM_JUMP24: 244 if (Config->Pic) 245 return make<ARMToThumbV7PILongThunk<ELFT>>(S); 246 return make<ARMToThumbV7ABSLongThunk<ELFT>>(S); 247 case R_ARM_THM_JUMP19: 248 case R_ARM_THM_JUMP24: 249 if (Config->Pic) 250 return make<ThumbToARMV7PILongThunk<ELFT>>(S); 251 return make<ThumbToARMV7ABSLongThunk<ELFT>>(S); 252 } 253 fatal("unrecognized relocation type"); 254 } 255 256 template <class ELFT> static Thunk *addThunkMips(SymbolBody &S) { 257 return make<MipsThunk<ELFT>>(S); 258 } 259 260 template <class ELFT> Thunk *addThunk(uint32_t RelocType, SymbolBody &S) { 261 if (Config->EMachine == EM_ARM) 262 return addThunkArm<ELFT>(RelocType, S); 263 else if (Config->EMachine == EM_MIPS) 264 return addThunkMips<ELFT>(S); 265 llvm_unreachable("add Thunk only supported for ARM and Mips"); 266 return nullptr; 267 } 268 269 template Thunk *addThunk<ELF32LE>(uint32_t, SymbolBody &); 270 template Thunk *addThunk<ELF32BE>(uint32_t, SymbolBody &); 271 template Thunk *addThunk<ELF64LE>(uint32_t, SymbolBody &); 272 template Thunk *addThunk<ELF64BE>(uint32_t, SymbolBody &); 273 } // end namespace elf 274 } // end namespace lld 275