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 "Target.h" 32 #include "llvm/Support/Casting.h" 33 #include "llvm/Support/ELF.h" 34 #include "llvm/Support/Endian.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/MathExtras.h" 37 #include <cstdint> 38 #include <cstring> 39 40 using namespace llvm; 41 using namespace llvm::object; 42 using namespace llvm::support::endian; 43 using namespace llvm::ELF; 44 45 namespace lld { 46 namespace elf { 47 48 namespace { 49 50 // Specific ARM Thunk implementations. The naming convention is: 51 // Source State, TargetState, Target Requirement, ABS or PI, Range 52 template <class ELFT> 53 class ARMToThumbV7ABSLongThunk final : public Thunk<ELFT> { 54 public: 55 ARMToThumbV7ABSLongThunk(const SymbolBody &Dest, 56 const InputSection<ELFT> &Owner) 57 : Thunk<ELFT>(Dest, Owner) {} 58 59 uint32_t size() const override { return 12; } 60 void writeTo(uint8_t *Buf) const override; 61 }; 62 63 template <class ELFT> class ARMToThumbV7PILongThunk final : public Thunk<ELFT> { 64 public: 65 ARMToThumbV7PILongThunk(const SymbolBody &Dest, 66 const InputSection<ELFT> &Owner) 67 : Thunk<ELFT>(Dest, Owner) {} 68 69 uint32_t size() const override { return 16; } 70 void writeTo(uint8_t *Buf) const override; 71 }; 72 73 template <class ELFT> 74 class ThumbToARMV7ABSLongThunk final : public Thunk<ELFT> { 75 public: 76 ThumbToARMV7ABSLongThunk(const SymbolBody &Dest, 77 const InputSection<ELFT> &Owner) 78 : Thunk<ELFT>(Dest, Owner) {} 79 80 uint32_t size() const override { return 10; } 81 void writeTo(uint8_t *Buf) const override; 82 }; 83 84 template <class ELFT> class ThumbToARMV7PILongThunk final : public Thunk<ELFT> { 85 public: 86 ThumbToARMV7PILongThunk(const SymbolBody &Dest, 87 const InputSection<ELFT> &Owner) 88 : Thunk<ELFT>(Dest, Owner) {} 89 90 uint32_t size() const override { return 12; } 91 void writeTo(uint8_t *Buf) const override; 92 }; 93 94 // MIPS LA25 thunk 95 template <class ELFT> class MipsThunk final : public Thunk<ELFT> { 96 public: 97 MipsThunk(const SymbolBody &Dest, const InputSection<ELFT> &Owner) 98 : Thunk<ELFT>(Dest, Owner) {} 99 100 uint32_t size() const override { return 16; } 101 void writeTo(uint8_t *Buf) const override; 102 }; 103 104 } // end anonymous namespace 105 106 // ARM Target Thunks 107 template <class ELFT> static uint64_t getARMThunkDestVA(const SymbolBody &S) { 108 uint64_t V = S.isInPlt() ? S.getPltVA<ELFT>() : S.getVA<ELFT>(); 109 return SignExtend64<32>(V); 110 } 111 112 template <class ELFT> 113 void ARMToThumbV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf) const { 114 const uint8_t Data[] = { 115 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S 116 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S 117 0x1c, 0xff, 0x2f, 0xe1, // bx ip 118 }; 119 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination); 120 memcpy(Buf, Data, sizeof(Data)); 121 Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S); 122 Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S); 123 } 124 125 template <class ELFT> 126 void ThumbToARMV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf) const { 127 const uint8_t Data[] = { 128 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S 129 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S 130 0x60, 0x47, // bx ip 131 }; 132 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination); 133 memcpy(Buf, Data, sizeof(Data)); 134 Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S); 135 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S); 136 } 137 138 template <class ELFT> 139 void ARMToThumbV7PILongThunk<ELFT>::writeTo(uint8_t *Buf) const { 140 const uint8_t Data[] = { 141 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) +8) 142 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P+4) +8) 143 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc 144 0x1c, 0xff, 0x2f, 0xe1, // bx r12 145 }; 146 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination); 147 uint64_t P = this->getVA(); 148 memcpy(Buf, Data, sizeof(Data)); 149 Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, S - P - 16); 150 Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, S - P - 12); 151 } 152 153 template <class ELFT> 154 void ThumbToARMV7PILongThunk<ELFT>::writeTo(uint8_t *Buf) const { 155 const uint8_t Data[] = { 156 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4) 157 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P+4) + 4) 158 0xfc, 0x44, // L1: add r12, pc 159 0x60, 0x47, // bx r12 160 }; 161 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination); 162 uint64_t P = this->getVA(); 163 memcpy(Buf, Data, sizeof(Data)); 164 Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, S - P - 12); 165 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, S - P - 8); 166 } 167 168 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one. 169 template <class ELFT> void MipsThunk<ELFT>::writeTo(uint8_t *Buf) const { 170 const endianness E = ELFT::TargetEndianness; 171 172 uint64_t S = this->Destination.template getVA<ELFT>(); 173 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func) 174 write32<E>(Buf + 4, 0x08000000 | (S >> 2)); // j func 175 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func) 176 write32<E>(Buf + 12, 0x00000000); // nop 177 Target->relocateOne(Buf, R_MIPS_HI16, S); 178 Target->relocateOne(Buf + 8, R_MIPS_LO16, S); 179 } 180 181 template <class ELFT> 182 Thunk<ELFT>::Thunk(const SymbolBody &D, const InputSection<ELFT> &O) 183 : Destination(D), Owner(O), Offset(O.getThunkOff() + O.getThunksSize()) {} 184 185 template <class ELFT> typename ELFT::uint Thunk<ELFT>::getVA() const { 186 return Owner.OutSec->Addr + Owner.OutSecOff + Offset; 187 } 188 189 template <class ELFT> Thunk<ELFT>::~Thunk() = default; 190 191 // Creates a thunk for Thumb-ARM interworking. 192 template <class ELFT> 193 static Thunk<ELFT> *createThunkArm(uint32_t Reloc, SymbolBody &S, 194 InputSection<ELFT> &IS) { 195 // ARM relocations need ARM to Thumb interworking Thunks. 196 // Thumb relocations need Thumb to ARM relocations. 197 // Use position independent Thunks if we require position independent code. 198 switch (Reloc) { 199 case R_ARM_PC24: 200 case R_ARM_PLT32: 201 case R_ARM_JUMP24: 202 if (Config->Pic) 203 return new (BAlloc) ARMToThumbV7PILongThunk<ELFT>(S, IS); 204 return new (BAlloc) ARMToThumbV7ABSLongThunk<ELFT>(S, IS); 205 case R_ARM_THM_JUMP19: 206 case R_ARM_THM_JUMP24: 207 if (Config->Pic) 208 return new (BAlloc) ThumbToARMV7PILongThunk<ELFT>(S, IS); 209 return new (BAlloc) ThumbToARMV7ABSLongThunk<ELFT>(S, IS); 210 } 211 fatal("unrecognized relocation type"); 212 } 213 214 template <class ELFT> 215 static void addThunkARM(uint32_t Reloc, SymbolBody &S, InputSection<ELFT> &IS) { 216 // Only one Thunk supported per symbol. 217 if (S.hasThunk<ELFT>()) 218 return; 219 220 // ARM Thunks are added to the same InputSection as the relocation. This 221 // isn't strictly necessary but it makes it more likely that a limited range 222 // branch can reach the Thunk, and it makes Thunks to the PLT section easier 223 Thunk<ELFT> *T = createThunkArm(Reloc, S, IS); 224 IS.addThunk(T); 225 if (auto *Sym = dyn_cast<DefinedRegular<ELFT>>(&S)) 226 Sym->ThunkData = T; 227 else if (auto *Sym = dyn_cast<SharedSymbol<ELFT>>(&S)) 228 Sym->ThunkData = T; 229 else if (auto *Sym = dyn_cast<Undefined<ELFT>>(&S)) 230 Sym->ThunkData = T; 231 else 232 fatal("symbol not DefinedRegular or Shared"); 233 } 234 235 template <class ELFT> 236 static void addThunkMips(uint32_t RelocType, SymbolBody &S, 237 InputSection<ELFT> &IS) { 238 // Only one Thunk supported per symbol. 239 if (S.hasThunk<ELFT>()) 240 return; 241 242 // Mips Thunks are added to the InputSection defining S. 243 auto *R = cast<DefinedRegular<ELFT>>(&S); 244 auto *Sec = cast<InputSection<ELFT>>(R->Section); 245 auto *T = new (BAlloc) MipsThunk<ELFT>(S, *Sec); 246 Sec->addThunk(T); 247 R->ThunkData = T; 248 } 249 250 template <class ELFT> 251 void addThunk(uint32_t RelocType, SymbolBody &S, InputSection<ELFT> &IS) { 252 if (Config->EMachine == EM_ARM) 253 addThunkARM<ELFT>(RelocType, S, IS); 254 else if (Config->EMachine == EM_MIPS) 255 addThunkMips<ELFT>(RelocType, S, IS); 256 else 257 llvm_unreachable("add Thunk only supported for ARM and Mips"); 258 } 259 260 template void addThunk<ELF32LE>(uint32_t, SymbolBody &, 261 InputSection<ELF32LE> &); 262 template void addThunk<ELF32BE>(uint32_t, SymbolBody &, 263 InputSection<ELF32BE> &); 264 template void addThunk<ELF64LE>(uint32_t, SymbolBody &, 265 InputSection<ELF64LE> &); 266 template void addThunk<ELF64BE>(uint32_t, SymbolBody &, 267 InputSection<ELF64BE> &); 268 269 template class Thunk<ELF32LE>; 270 template class Thunk<ELF32BE>; 271 template class Thunk<ELF64LE>; 272 template class Thunk<ELF64BE>; 273 274 } // end namespace elf 275 } // end namespace lld 276