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