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> 54 class ARMToThumbV7ABSLongThunk final : public Thunk<ELFT> { 55 public: 56 ARMToThumbV7ABSLongThunk(const SymbolBody &Dest) : Thunk<ELFT>(Dest) {} 57 58 uint32_t size() const override { return 12; } 59 void writeTo(uint8_t *Buf, ThunkSection<ELFT> &IS) const override; 60 void addSymbols(ThunkSection<ELFT> &IS) override; 61 }; 62 63 template <class ELFT> class ARMToThumbV7PILongThunk final : public Thunk<ELFT> { 64 public: 65 ARMToThumbV7PILongThunk(const SymbolBody &Dest) : Thunk<ELFT>(Dest) {} 66 67 uint32_t size() const override { return 16; } 68 void writeTo(uint8_t *Buf, ThunkSection<ELFT> &IS) const override; 69 void addSymbols(ThunkSection<ELFT> &IS) override; 70 }; 71 72 template <class ELFT> 73 class ThumbToARMV7ABSLongThunk final : public Thunk<ELFT> { 74 public: 75 ThumbToARMV7ABSLongThunk(const SymbolBody &Dest) : Thunk<ELFT>(Dest) { 76 this->alignment = 2; 77 } 78 79 uint32_t size() const override { return 10; } 80 void writeTo(uint8_t *Buf, ThunkSection<ELFT> &IS) const override; 81 void addSymbols(ThunkSection<ELFT> &IS) override; 82 }; 83 84 template <class ELFT> class ThumbToARMV7PILongThunk final : public Thunk<ELFT> { 85 public: 86 ThumbToARMV7PILongThunk(const SymbolBody &Dest) : Thunk<ELFT>(Dest) { 87 this->alignment = 2; 88 } 89 90 uint32_t size() const override { return 12; } 91 void writeTo(uint8_t *Buf, ThunkSection<ELFT> &IS) const override; 92 void addSymbols(ThunkSection<ELFT> &IS) override; 93 }; 94 95 // MIPS LA25 thunk 96 template <class ELFT> class MipsThunk final : public Thunk<ELFT> { 97 public: 98 MipsThunk(const SymbolBody &Dest) : Thunk<ELFT>(Dest) {} 99 100 uint32_t size() const override { return 16; } 101 void writeTo(uint8_t *Buf, ThunkSection<ELFT> &IS) const override; 102 void addSymbols(ThunkSection<ELFT> &IS) override; 103 InputSection *getTargetInputSection() const override; 104 }; 105 106 } // end anonymous namespace 107 108 // ARM Target Thunks 109 template <class ELFT> static uint64_t getARMThunkDestVA(const SymbolBody &S) { 110 uint64_t V = S.isInPlt() ? S.getPltVA<ELFT>() : S.getVA<ELFT>(); 111 return SignExtend64<32>(V); 112 } 113 114 template <class ELFT> 115 void ARMToThumbV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf, 116 ThunkSection<ELFT> &IS) const { 117 const uint8_t Data[] = { 118 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S 119 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S 120 0x1c, 0xff, 0x2f, 0xe1, // bx ip 121 }; 122 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination); 123 memcpy(Buf, Data, sizeof(Data)); 124 Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S); 125 Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S); 126 } 127 128 template <class ELFT> 129 void ARMToThumbV7ABSLongThunk<ELFT>::addSymbols(ThunkSection<ELFT> &IS) { 130 this->ThunkSym = addSyntheticLocal<ELFT>( 131 Saver.save("__ARMToThumbv7ABSLongThunk_" + this->Destination.getName()), 132 STT_FUNC, this->Offset, size(), &IS); 133 addSyntheticLocal<ELFT>("$a", STT_NOTYPE, this->Offset, 0, &IS); 134 } 135 136 template <class ELFT> 137 void ThumbToARMV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf, 138 ThunkSection<ELFT> &IS) const { 139 const uint8_t Data[] = { 140 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S 141 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S 142 0x60, 0x47, // bx ip 143 }; 144 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination); 145 memcpy(Buf, Data, sizeof(Data)); 146 Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S); 147 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S); 148 } 149 150 template <class ELFT> 151 void ThumbToARMV7ABSLongThunk<ELFT>::addSymbols(ThunkSection<ELFT> &IS) { 152 this->ThunkSym = addSyntheticLocal<ELFT>( 153 Saver.save("__ThumbToARMv7ABSLongThunk_" + this->Destination.getName()), 154 STT_FUNC, this->Offset, size(), &IS); 155 addSyntheticLocal<ELFT>("$t", STT_NOTYPE, this->Offset, 0, &IS); 156 } 157 158 template <class ELFT> 159 void ARMToThumbV7PILongThunk<ELFT>::writeTo(uint8_t *Buf, 160 ThunkSection<ELFT> &IS) const { 161 const uint8_t Data[] = { 162 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) +8) 163 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P+4) +8) 164 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc 165 0x1c, 0xff, 0x2f, 0xe1, // bx r12 166 }; 167 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination); 168 uint64_t P = this->ThunkSym->template getVA<ELFT>(); 169 memcpy(Buf, Data, sizeof(Data)); 170 Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, S - P - 16); 171 Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, S - P - 12); 172 } 173 174 template <class ELFT> 175 void ARMToThumbV7PILongThunk<ELFT>::addSymbols(ThunkSection<ELFT> &IS) { 176 this->ThunkSym = addSyntheticLocal<ELFT>( 177 Saver.save("__ARMToThumbV7PILongThunk_" + this->Destination.getName()), 178 STT_FUNC, this->Offset, size(), &IS); 179 addSyntheticLocal<ELFT>("$a", STT_NOTYPE, this->Offset, 0, &IS); 180 } 181 182 template <class ELFT> 183 void ThumbToARMV7PILongThunk<ELFT>::writeTo(uint8_t *Buf, 184 ThunkSection<ELFT> &IS) const { 185 const uint8_t Data[] = { 186 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4) 187 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P+4) + 4) 188 0xfc, 0x44, // L1: add r12, pc 189 0x60, 0x47, // bx r12 190 }; 191 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination); 192 uint64_t P = this->ThunkSym->template getVA<ELFT>(); 193 memcpy(Buf, Data, sizeof(Data)); 194 Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, S - P - 12); 195 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, S - P - 8); 196 } 197 198 template <class ELFT> 199 void ThumbToARMV7PILongThunk<ELFT>::addSymbols(ThunkSection<ELFT> &IS) { 200 this->ThunkSym = addSyntheticLocal<ELFT>( 201 Saver.save("__ThumbToARMV7PILongThunk_" + this->Destination.getName()), 202 STT_FUNC, this->Offset, size(), &IS); 203 addSyntheticLocal<ELFT>("$t", STT_NOTYPE, this->Offset, 0, &IS); 204 } 205 206 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one. 207 template <class ELFT> 208 void MipsThunk<ELFT>::writeTo(uint8_t *Buf, ThunkSection<ELFT> &) const { 209 const endianness E = ELFT::TargetEndianness; 210 211 uint64_t S = this->Destination.template getVA<ELFT>(); 212 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func) 213 write32<E>(Buf + 4, 0x08000000 | (S >> 2)); // j func 214 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func) 215 write32<E>(Buf + 12, 0x00000000); // nop 216 Target->relocateOne(Buf, R_MIPS_HI16, S); 217 Target->relocateOne(Buf + 8, R_MIPS_LO16, S); 218 } 219 220 template <class ELFT> void MipsThunk<ELFT>::addSymbols(ThunkSection<ELFT> &IS) { 221 this->ThunkSym = addSyntheticLocal<ELFT>( 222 Saver.save("__LA25Thunk_" + this->Destination.getName()), STT_FUNC, 223 this->Offset, size(), &IS); 224 } 225 226 template <class ELFT> 227 InputSection *MipsThunk<ELFT>::getTargetInputSection() const { 228 auto *DR = dyn_cast<DefinedRegular>(&this->Destination); 229 return dyn_cast<InputSection>(DR->Section); 230 } 231 232 template <class ELFT> 233 Thunk<ELFT>::Thunk(const SymbolBody &D) : Destination(D), Offset(0) {} 234 235 template <class ELFT> Thunk<ELFT>::~Thunk() = default; 236 237 // Creates a thunk for Thumb-ARM interworking. 238 template <class ELFT> 239 static Thunk<ELFT> *addThunkArm(uint32_t Reloc, SymbolBody &S) { 240 // ARM relocations need ARM to Thumb interworking Thunks. 241 // Thumb relocations need Thumb to ARM relocations. 242 // Use position independent Thunks if we require position independent code. 243 switch (Reloc) { 244 case R_ARM_PC24: 245 case R_ARM_PLT32: 246 case R_ARM_JUMP24: 247 if (Config->pic()) 248 return make<ARMToThumbV7PILongThunk<ELFT>>(S); 249 return make<ARMToThumbV7ABSLongThunk<ELFT>>(S); 250 case R_ARM_THM_JUMP19: 251 case R_ARM_THM_JUMP24: 252 if (Config->pic()) 253 return make<ThumbToARMV7PILongThunk<ELFT>>(S); 254 return make<ThumbToARMV7ABSLongThunk<ELFT>>(S); 255 } 256 fatal("unrecognized relocation type"); 257 } 258 259 template <class ELFT> static Thunk<ELFT> *addThunkMips(SymbolBody &S) { 260 return make<MipsThunk<ELFT>>(S); 261 } 262 263 template <class ELFT> Thunk<ELFT> *addThunk(uint32_t RelocType, SymbolBody &S) { 264 if (Config->EMachine == EM_ARM) 265 return addThunkArm<ELFT>(RelocType, S); 266 else if (Config->EMachine == EM_MIPS) 267 return addThunkMips<ELFT>(S); 268 llvm_unreachable("add Thunk only supported for ARM and Mips"); 269 return nullptr; 270 } 271 272 template Thunk<ELF32LE> *addThunk<ELF32LE>(uint32_t, SymbolBody &); 273 template Thunk<ELF32BE> *addThunk<ELF32BE>(uint32_t, SymbolBody &); 274 template Thunk<ELF64LE> *addThunk<ELF64LE>(uint32_t, SymbolBody &); 275 template Thunk<ELF64BE> *addThunk<ELF64BE>(uint32_t, SymbolBody &); 276 277 template class Thunk<ELF32LE>; 278 template class Thunk<ELF32BE>; 279 template class Thunk<ELF64LE>; 280 template class Thunk<ELF64BE>; 281 282 } // end namespace elf 283 } // end namespace lld 284