1 //===- Thunks.cpp --------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===---------------------------------------------------------------------===// 8 // 9 // This file contains Thunk subclasses. 10 // 11 // A thunk is a small piece of code written after an input section 12 // which is used to jump between "incompatible" functions 13 // such as MIPS PIC and non-PIC or ARM non-Thumb and Thumb functions. 14 // 15 // If a jump target is too far and its address doesn't fit to a 16 // short jump instruction, we need to create a thunk too, but we 17 // haven't supported it yet. 18 // 19 // i386 and x86-64 don't need thunks. 20 // 21 //===---------------------------------------------------------------------===// 22 23 #include "Thunks.h" 24 #include "Config.h" 25 #include "InputSection.h" 26 #include "OutputSections.h" 27 #include "Symbols.h" 28 #include "SyntheticSections.h" 29 #include "Target.h" 30 #include "lld/Common/CommonLinkerContext.h" 31 #include "llvm/BinaryFormat/ELF.h" 32 #include "llvm/Support/Casting.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/MathExtras.h" 35 #include <cstdint> 36 #include <cstring> 37 38 using namespace llvm; 39 using namespace llvm::object; 40 using namespace llvm::ELF; 41 using namespace lld; 42 using namespace lld::elf; 43 44 namespace { 45 46 // AArch64 long range Thunks 47 class AArch64ABSLongThunk final : public Thunk { 48 public: 49 AArch64ABSLongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {} 50 uint32_t size() override { return 16; } 51 void writeTo(uint8_t *buf) override; 52 void addSymbols(ThunkSection &isec) override; 53 }; 54 55 class AArch64ADRPThunk final : public Thunk { 56 public: 57 AArch64ADRPThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {} 58 uint32_t size() override { return 12; } 59 void writeTo(uint8_t *buf) override; 60 void addSymbols(ThunkSection &isec) override; 61 }; 62 63 // Base class for ARM thunks. 64 // 65 // An ARM thunk may be either short or long. A short thunk is simply a branch 66 // (B) instruction, and it may be used to call ARM functions when the distance 67 // from the thunk to the target is less than 32MB. Long thunks can branch to any 68 // virtual address and can switch between ARM and Thumb, and they are 69 // implemented in the derived classes. This class tries to create a short thunk 70 // if the target is in range, otherwise it creates a long thunk. 71 class ARMThunk : public Thunk { 72 public: 73 ARMThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {} 74 75 bool getMayUseShortThunk(); 76 uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); } 77 void writeTo(uint8_t *buf) override; 78 bool isCompatibleWith(const InputSection &isec, 79 const Relocation &rel) const override; 80 81 // Returns the size of a long thunk. 82 virtual uint32_t sizeLong() = 0; 83 84 // Writes a long thunk to Buf. 85 virtual void writeLong(uint8_t *buf) = 0; 86 87 private: 88 // This field tracks whether all previously considered layouts would allow 89 // this thunk to be short. If we have ever needed a long thunk, we always 90 // create a long thunk, even if the thunk may be short given the current 91 // distance to the target. We do this because transitioning from long to short 92 // can create layout oscillations in certain corner cases which would prevent 93 // the layout from converging. 94 bool mayUseShortThunk = true; 95 }; 96 97 // Base class for Thumb-2 thunks. 98 // 99 // This class is similar to ARMThunk, but it uses the Thumb-2 B.W instruction 100 // which has a range of 16MB. 101 class ThumbThunk : public Thunk { 102 public: 103 ThumbThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) { 104 alignment = 2; 105 } 106 107 bool getMayUseShortThunk(); 108 uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); } 109 void writeTo(uint8_t *buf) override; 110 bool isCompatibleWith(const InputSection &isec, 111 const Relocation &rel) const override; 112 113 // Returns the size of a long thunk. 114 virtual uint32_t sizeLong() = 0; 115 116 // Writes a long thunk to Buf. 117 virtual void writeLong(uint8_t *buf) = 0; 118 119 private: 120 // See comment in ARMThunk above. 121 bool mayUseShortThunk = true; 122 }; 123 124 // Specific ARM Thunk implementations. The naming convention is: 125 // Source State, TargetState, Target Requirement, ABS or PI, Range 126 class ARMV7ABSLongThunk final : public ARMThunk { 127 public: 128 ARMV7ABSLongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {} 129 130 uint32_t sizeLong() override { return 12; } 131 void writeLong(uint8_t *buf) override; 132 void addSymbols(ThunkSection &isec) override; 133 }; 134 135 class ARMV7PILongThunk final : public ARMThunk { 136 public: 137 ARMV7PILongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {} 138 139 uint32_t sizeLong() override { return 16; } 140 void writeLong(uint8_t *buf) override; 141 void addSymbols(ThunkSection &isec) override; 142 }; 143 144 class ThumbV7ABSLongThunk final : public ThumbThunk { 145 public: 146 ThumbV7ABSLongThunk(Symbol &dest, int64_t addend) 147 : ThumbThunk(dest, addend) {} 148 149 uint32_t sizeLong() override { return 10; } 150 void writeLong(uint8_t *buf) override; 151 void addSymbols(ThunkSection &isec) override; 152 }; 153 154 class ThumbV7PILongThunk final : public ThumbThunk { 155 public: 156 ThumbV7PILongThunk(Symbol &dest, int64_t addend) : ThumbThunk(dest, addend) {} 157 158 uint32_t sizeLong() override { return 12; } 159 void writeLong(uint8_t *buf) override; 160 void addSymbols(ThunkSection &isec) override; 161 }; 162 163 // Implementations of Thunks for older Arm architectures that do not support 164 // the movt/movw instructions. These thunks require at least Architecture v5 165 // as used on processors such as the Arm926ej-s. There are no Thumb entry 166 // points as there is no Thumb branch instruction on these architecture that 167 // can result in a thunk 168 class ARMV5ABSLongThunk final : public ARMThunk { 169 public: 170 ARMV5ABSLongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {} 171 172 uint32_t sizeLong() override { return 8; } 173 void writeLong(uint8_t *buf) override; 174 void addSymbols(ThunkSection &isec) override; 175 bool isCompatibleWith(const InputSection &isec, 176 const Relocation &rel) const override; 177 }; 178 179 class ARMV5PILongThunk final : public ARMThunk { 180 public: 181 ARMV5PILongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {} 182 183 uint32_t sizeLong() override { return 16; } 184 void writeLong(uint8_t *buf) override; 185 void addSymbols(ThunkSection &isec) override; 186 bool isCompatibleWith(const InputSection &isec, 187 const Relocation &rel) const override; 188 }; 189 190 // Implementations of Thunks for Arm v6-M. Only Thumb instructions are permitted 191 class ThumbV6MABSLongThunk final : public ThumbThunk { 192 public: 193 ThumbV6MABSLongThunk(Symbol &dest, int64_t addend) 194 : ThumbThunk(dest, addend) {} 195 196 uint32_t sizeLong() override { return 12; } 197 void writeLong(uint8_t *buf) override; 198 void addSymbols(ThunkSection &isec) override; 199 }; 200 201 class ThumbV6MPILongThunk final : public ThumbThunk { 202 public: 203 ThumbV6MPILongThunk(Symbol &dest, int64_t addend) 204 : ThumbThunk(dest, addend) {} 205 206 uint32_t sizeLong() override { return 16; } 207 void writeLong(uint8_t *buf) override; 208 void addSymbols(ThunkSection &isec) override; 209 }; 210 211 // MIPS LA25 thunk 212 class MipsThunk final : public Thunk { 213 public: 214 MipsThunk(Symbol &dest) : Thunk(dest, 0) {} 215 216 uint32_t size() override { return 16; } 217 void writeTo(uint8_t *buf) override; 218 void addSymbols(ThunkSection &isec) override; 219 InputSection *getTargetInputSection() const override; 220 }; 221 222 // microMIPS R2-R5 LA25 thunk 223 class MicroMipsThunk final : public Thunk { 224 public: 225 MicroMipsThunk(Symbol &dest) : Thunk(dest, 0) {} 226 227 uint32_t size() override { return 14; } 228 void writeTo(uint8_t *buf) override; 229 void addSymbols(ThunkSection &isec) override; 230 InputSection *getTargetInputSection() const override; 231 }; 232 233 // microMIPS R6 LA25 thunk 234 class MicroMipsR6Thunk final : public Thunk { 235 public: 236 MicroMipsR6Thunk(Symbol &dest) : Thunk(dest, 0) {} 237 238 uint32_t size() override { return 12; } 239 void writeTo(uint8_t *buf) override; 240 void addSymbols(ThunkSection &isec) override; 241 InputSection *getTargetInputSection() const override; 242 }; 243 244 class PPC32PltCallStub final : public Thunk { 245 public: 246 // For R_PPC_PLTREL24, Thunk::addend records the addend which will be used to 247 // decide the offsets in the call stub. 248 PPC32PltCallStub(const InputSection &isec, const Relocation &rel, 249 Symbol &dest) 250 : Thunk(dest, rel.addend), file(isec.file) {} 251 uint32_t size() override { return 16; } 252 void writeTo(uint8_t *buf) override; 253 void addSymbols(ThunkSection &isec) override; 254 bool isCompatibleWith(const InputSection &isec, const Relocation &rel) const override; 255 256 private: 257 // Records the call site of the call stub. 258 const InputFile *file; 259 }; 260 261 class PPC32LongThunk final : public Thunk { 262 public: 263 PPC32LongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {} 264 uint32_t size() override { return config->isPic ? 32 : 16; } 265 void writeTo(uint8_t *buf) override; 266 void addSymbols(ThunkSection &isec) override; 267 }; 268 269 // PPC64 Plt call stubs. 270 // Any call site that needs to call through a plt entry needs a call stub in 271 // the .text section. The call stub is responsible for: 272 // 1) Saving the toc-pointer to the stack. 273 // 2) Loading the target functions address from the procedure linkage table into 274 // r12 for use by the target functions global entry point, and into the count 275 // register. 276 // 3) Transferring control to the target function through an indirect branch. 277 class PPC64PltCallStub final : public Thunk { 278 public: 279 PPC64PltCallStub(Symbol &dest) : Thunk(dest, 0) {} 280 uint32_t size() override { return 20; } 281 void writeTo(uint8_t *buf) override; 282 void addSymbols(ThunkSection &isec) override; 283 bool isCompatibleWith(const InputSection &isec, 284 const Relocation &rel) const override; 285 }; 286 287 // PPC64 R2 Save Stub 288 // When the caller requires a valid R2 TOC pointer but the callee does not 289 // require a TOC pointer and the callee cannot guarantee that it doesn't 290 // clobber R2 then we need to save R2. This stub: 291 // 1) Saves the TOC pointer to the stack. 292 // 2) Tail calls the callee. 293 class PPC64R2SaveStub final : public Thunk { 294 public: 295 PPC64R2SaveStub(Symbol &dest, int64_t addend) : Thunk(dest, addend) { 296 alignment = 16; 297 } 298 299 // To prevent oscillations in layout when moving from short to long thunks 300 // we make sure that once a thunk has been set to long it cannot go back. 301 bool getMayUseShortThunk() { 302 if (!mayUseShortThunk) 303 return false; 304 if (!isInt<26>(computeOffset())) { 305 mayUseShortThunk = false; 306 return false; 307 } 308 return true; 309 } 310 uint32_t size() override { return getMayUseShortThunk() ? 8 : 32; } 311 void writeTo(uint8_t *buf) override; 312 void addSymbols(ThunkSection &isec) override; 313 bool isCompatibleWith(const InputSection &isec, 314 const Relocation &rel) const override; 315 316 private: 317 // Transitioning from long to short can create layout oscillations in 318 // certain corner cases which would prevent the layout from converging. 319 // This is similar to the handling for ARMThunk. 320 bool mayUseShortThunk = true; 321 int64_t computeOffset() const { 322 return destination.getVA() - (getThunkTargetSym()->getVA() + 4); 323 } 324 }; 325 326 // PPC64 R12 Setup Stub 327 // When a caller that does not maintain a toc-pointer performs a local call to 328 // a callee which requires a toc-pointer then we need this stub to place the 329 // callee's global entry point into r12 without a save of R2. 330 class PPC64R12SetupStub final : public Thunk { 331 public: 332 PPC64R12SetupStub(Symbol &dest) : Thunk(dest, 0) { alignment = 16; } 333 uint32_t size() override { return 32; } 334 void writeTo(uint8_t *buf) override; 335 void addSymbols(ThunkSection &isec) override; 336 bool isCompatibleWith(const InputSection &isec, 337 const Relocation &rel) const override; 338 }; 339 340 // PPC64 PC-relative PLT Stub 341 // When a caller that does not maintain a toc-pointer performs an extern call 342 // then this stub is needed for: 343 // 1) Loading the target functions address from the procedure linkage table into 344 // r12 for use by the target functions global entry point, and into the count 345 // register with pc-relative instructions. 346 // 2) Transferring control to the target function through an indirect branch. 347 class PPC64PCRelPLTStub final : public Thunk { 348 public: 349 PPC64PCRelPLTStub(Symbol &dest) : Thunk(dest, 0) { alignment = 16; } 350 uint32_t size() override { return 32; } 351 void writeTo(uint8_t *buf) override; 352 void addSymbols(ThunkSection &isec) override; 353 bool isCompatibleWith(const InputSection &isec, 354 const Relocation &rel) const override; 355 }; 356 357 // A bl instruction uses a signed 24 bit offset, with an implicit 4 byte 358 // alignment. This gives a possible 26 bits of 'reach'. If the call offset is 359 // larger than that we need to emit a long-branch thunk. The target address 360 // of the callee is stored in a table to be accessed TOC-relative. Since the 361 // call must be local (a non-local call will have a PltCallStub instead) the 362 // table stores the address of the callee's local entry point. For 363 // position-independent code a corresponding relative dynamic relocation is 364 // used. 365 class PPC64LongBranchThunk : public Thunk { 366 public: 367 uint32_t size() override { return 32; } 368 void writeTo(uint8_t *buf) override; 369 void addSymbols(ThunkSection &isec) override; 370 bool isCompatibleWith(const InputSection &isec, 371 const Relocation &rel) const override; 372 373 protected: 374 PPC64LongBranchThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {} 375 }; 376 377 class PPC64PILongBranchThunk final : public PPC64LongBranchThunk { 378 public: 379 PPC64PILongBranchThunk(Symbol &dest, int64_t addend) 380 : PPC64LongBranchThunk(dest, addend) { 381 assert(!dest.isPreemptible); 382 if (Optional<uint32_t> index = 383 in.ppc64LongBranchTarget->addEntry(&dest, addend)) { 384 mainPart->relaDyn->addRelativeReloc( 385 target->relativeRel, *in.ppc64LongBranchTarget, *index * UINT64_C(8), 386 dest, addend + getPPC64GlobalEntryToLocalEntryOffset(dest.stOther), 387 target->symbolicRel, R_ABS); 388 } 389 } 390 }; 391 392 class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk { 393 public: 394 PPC64PDLongBranchThunk(Symbol &dest, int64_t addend) 395 : PPC64LongBranchThunk(dest, addend) { 396 in.ppc64LongBranchTarget->addEntry(&dest, addend); 397 } 398 }; 399 400 } // end anonymous namespace 401 402 Defined *Thunk::addSymbol(StringRef name, uint8_t type, uint64_t value, 403 InputSectionBase §ion) { 404 Defined *d = addSyntheticLocal(name, type, value, /*size=*/0, section); 405 syms.push_back(d); 406 return d; 407 } 408 409 void Thunk::setOffset(uint64_t newOffset) { 410 for (Defined *d : syms) 411 d->value = d->value - offset + newOffset; 412 offset = newOffset; 413 } 414 415 // AArch64 long range Thunks 416 417 static uint64_t getAArch64ThunkDestVA(const Symbol &s, int64_t a) { 418 uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA(a); 419 return v; 420 } 421 422 void AArch64ABSLongThunk::writeTo(uint8_t *buf) { 423 const uint8_t data[] = { 424 0x50, 0x00, 0x00, 0x58, // ldr x16, L0 425 0x00, 0x02, 0x1f, 0xd6, // br x16 426 0x00, 0x00, 0x00, 0x00, // L0: .xword S 427 0x00, 0x00, 0x00, 0x00, 428 }; 429 uint64_t s = getAArch64ThunkDestVA(destination, addend); 430 memcpy(buf, data, sizeof(data)); 431 target->relocateNoSym(buf + 8, R_AARCH64_ABS64, s); 432 } 433 434 void AArch64ABSLongThunk::addSymbols(ThunkSection &isec) { 435 addSymbol(saver().save("__AArch64AbsLongThunk_" + destination.getName()), 436 STT_FUNC, 0, isec); 437 addSymbol("$x", STT_NOTYPE, 0, isec); 438 addSymbol("$d", STT_NOTYPE, 8, isec); 439 } 440 441 // This Thunk has a maximum range of 4Gb, this is sufficient for all programs 442 // using the small code model, including pc-relative ones. At time of writing 443 // clang and gcc do not support the large code model for position independent 444 // code so it is safe to use this for position independent thunks without 445 // worrying about the destination being more than 4Gb away. 446 void AArch64ADRPThunk::writeTo(uint8_t *buf) { 447 const uint8_t data[] = { 448 0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest) 449 0x10, 0x02, 0x00, 0x91, // add x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest) 450 0x00, 0x02, 0x1f, 0xd6, // br x16 451 }; 452 uint64_t s = getAArch64ThunkDestVA(destination, addend); 453 uint64_t p = getThunkTargetSym()->getVA(); 454 memcpy(buf, data, sizeof(data)); 455 target->relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21, 456 getAArch64Page(s) - getAArch64Page(p)); 457 target->relocateNoSym(buf + 4, R_AARCH64_ADD_ABS_LO12_NC, s); 458 } 459 460 void AArch64ADRPThunk::addSymbols(ThunkSection &isec) { 461 addSymbol(saver().save("__AArch64ADRPThunk_" + destination.getName()), 462 STT_FUNC, 0, isec); 463 addSymbol("$x", STT_NOTYPE, 0, isec); 464 } 465 466 // ARM Target Thunks 467 static uint64_t getARMThunkDestVA(const Symbol &s) { 468 uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA(); 469 return SignExtend64<32>(v); 470 } 471 472 // This function returns true if the target is not Thumb and is within 2^26, and 473 // it has not previously returned false (see comment for mayUseShortThunk). 474 bool ARMThunk::getMayUseShortThunk() { 475 if (!mayUseShortThunk) 476 return false; 477 uint64_t s = getARMThunkDestVA(destination); 478 if (s & 1) { 479 mayUseShortThunk = false; 480 return false; 481 } 482 uint64_t p = getThunkTargetSym()->getVA(); 483 int64_t offset = s - p - 8; 484 mayUseShortThunk = llvm::isInt<26>(offset); 485 return mayUseShortThunk; 486 } 487 488 void ARMThunk::writeTo(uint8_t *buf) { 489 if (!getMayUseShortThunk()) { 490 writeLong(buf); 491 return; 492 } 493 494 uint64_t s = getARMThunkDestVA(destination); 495 uint64_t p = getThunkTargetSym()->getVA(); 496 int64_t offset = s - p - 8; 497 const uint8_t data[] = { 498 0x00, 0x00, 0x00, 0xea, // b S 499 }; 500 memcpy(buf, data, sizeof(data)); 501 target->relocateNoSym(buf, R_ARM_JUMP24, offset); 502 } 503 504 bool ARMThunk::isCompatibleWith(const InputSection &isec, 505 const Relocation &rel) const { 506 // Thumb branch relocations can't use BLX 507 return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24; 508 } 509 510 // This function returns true if the target is Thumb and is within 2^25, and 511 // it has not previously returned false (see comment for mayUseShortThunk). 512 bool ThumbThunk::getMayUseShortThunk() { 513 if (!mayUseShortThunk) 514 return false; 515 uint64_t s = getARMThunkDestVA(destination); 516 if ((s & 1) == 0) { 517 mayUseShortThunk = false; 518 return false; 519 } 520 uint64_t p = getThunkTargetSym()->getVA() & ~1; 521 int64_t offset = s - p - 4; 522 mayUseShortThunk = llvm::isInt<25>(offset); 523 return mayUseShortThunk; 524 } 525 526 void ThumbThunk::writeTo(uint8_t *buf) { 527 if (!getMayUseShortThunk()) { 528 writeLong(buf); 529 return; 530 } 531 532 uint64_t s = getARMThunkDestVA(destination); 533 uint64_t p = getThunkTargetSym()->getVA(); 534 int64_t offset = s - p - 4; 535 const uint8_t data[] = { 536 0x00, 0xf0, 0x00, 0xb0, // b.w S 537 }; 538 memcpy(buf, data, sizeof(data)); 539 target->relocateNoSym(buf, R_ARM_THM_JUMP24, offset); 540 } 541 542 bool ThumbThunk::isCompatibleWith(const InputSection &isec, 543 const Relocation &rel) const { 544 // ARM branch relocations can't use BLX 545 return rel.type != R_ARM_JUMP24 && rel.type != R_ARM_PC24 && rel.type != R_ARM_PLT32; 546 } 547 548 void ARMV7ABSLongThunk::writeLong(uint8_t *buf) { 549 const uint8_t data[] = { 550 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S 551 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S 552 0x1c, 0xff, 0x2f, 0xe1, // bx ip 553 }; 554 uint64_t s = getARMThunkDestVA(destination); 555 memcpy(buf, data, sizeof(data)); 556 target->relocateNoSym(buf, R_ARM_MOVW_ABS_NC, s); 557 target->relocateNoSym(buf + 4, R_ARM_MOVT_ABS, s); 558 } 559 560 void ARMV7ABSLongThunk::addSymbols(ThunkSection &isec) { 561 addSymbol(saver().save("__ARMv7ABSLongThunk_" + destination.getName()), 562 STT_FUNC, 0, isec); 563 addSymbol("$a", STT_NOTYPE, 0, isec); 564 } 565 566 void ThumbV7ABSLongThunk::writeLong(uint8_t *buf) { 567 const uint8_t data[] = { 568 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S 569 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S 570 0x60, 0x47, // bx ip 571 }; 572 uint64_t s = getARMThunkDestVA(destination); 573 memcpy(buf, data, sizeof(data)); 574 target->relocateNoSym(buf, R_ARM_THM_MOVW_ABS_NC, s); 575 target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_ABS, s); 576 } 577 578 void ThumbV7ABSLongThunk::addSymbols(ThunkSection &isec) { 579 addSymbol(saver().save("__Thumbv7ABSLongThunk_" + destination.getName()), 580 STT_FUNC, 1, isec); 581 addSymbol("$t", STT_NOTYPE, 0, isec); 582 } 583 584 void ARMV7PILongThunk::writeLong(uint8_t *buf) { 585 const uint8_t data[] = { 586 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) + 8) 587 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P) + 8) 588 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc 589 0x1c, 0xff, 0x2f, 0xe1, // bx ip 590 }; 591 uint64_t s = getARMThunkDestVA(destination); 592 uint64_t p = getThunkTargetSym()->getVA(); 593 int64_t offset = s - p - 16; 594 memcpy(buf, data, sizeof(data)); 595 target->relocateNoSym(buf, R_ARM_MOVW_PREL_NC, offset); 596 target->relocateNoSym(buf + 4, R_ARM_MOVT_PREL, offset); 597 } 598 599 void ARMV7PILongThunk::addSymbols(ThunkSection &isec) { 600 addSymbol(saver().save("__ARMV7PILongThunk_" + destination.getName()), 601 STT_FUNC, 0, isec); 602 addSymbol("$a", STT_NOTYPE, 0, isec); 603 } 604 605 void ThumbV7PILongThunk::writeLong(uint8_t *buf) { 606 const uint8_t data[] = { 607 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4) 608 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P) + 4) 609 0xfc, 0x44, // L1: add ip, pc 610 0x60, 0x47, // bx ip 611 }; 612 uint64_t s = getARMThunkDestVA(destination); 613 uint64_t p = getThunkTargetSym()->getVA() & ~0x1; 614 int64_t offset = s - p - 12; 615 memcpy(buf, data, sizeof(data)); 616 target->relocateNoSym(buf, R_ARM_THM_MOVW_PREL_NC, offset); 617 target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_PREL, offset); 618 } 619 620 void ThumbV7PILongThunk::addSymbols(ThunkSection &isec) { 621 addSymbol(saver().save("__ThumbV7PILongThunk_" + destination.getName()), 622 STT_FUNC, 1, isec); 623 addSymbol("$t", STT_NOTYPE, 0, isec); 624 } 625 626 void ARMV5ABSLongThunk::writeLong(uint8_t *buf) { 627 const uint8_t data[] = { 628 0x04, 0xf0, 0x1f, 0xe5, // ldr pc, [pc,#-4] ; L1 629 0x00, 0x00, 0x00, 0x00, // L1: .word S 630 }; 631 memcpy(buf, data, sizeof(data)); 632 target->relocateNoSym(buf + 4, R_ARM_ABS32, getARMThunkDestVA(destination)); 633 } 634 635 void ARMV5ABSLongThunk::addSymbols(ThunkSection &isec) { 636 addSymbol(saver().save("__ARMv5ABSLongThunk_" + destination.getName()), 637 STT_FUNC, 0, isec); 638 addSymbol("$a", STT_NOTYPE, 0, isec); 639 addSymbol("$d", STT_NOTYPE, 4, isec); 640 } 641 642 bool ARMV5ABSLongThunk::isCompatibleWith(const InputSection &isec, 643 const Relocation &rel) const { 644 // Thumb branch relocations can't use BLX 645 return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24; 646 } 647 648 void ARMV5PILongThunk::writeLong(uint8_t *buf) { 649 const uint8_t data[] = { 650 0x04, 0xc0, 0x9f, 0xe5, // P: ldr ip, [pc,#4] ; L2 651 0x0c, 0xc0, 0x8f, 0xe0, // L1: add ip, pc, ip 652 0x1c, 0xff, 0x2f, 0xe1, // bx ip 653 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8) 654 }; 655 uint64_t s = getARMThunkDestVA(destination); 656 uint64_t p = getThunkTargetSym()->getVA() & ~0x1; 657 memcpy(buf, data, sizeof(data)); 658 target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12); 659 } 660 661 void ARMV5PILongThunk::addSymbols(ThunkSection &isec) { 662 addSymbol(saver().save("__ARMV5PILongThunk_" + destination.getName()), 663 STT_FUNC, 0, isec); 664 addSymbol("$a", STT_NOTYPE, 0, isec); 665 addSymbol("$d", STT_NOTYPE, 12, isec); 666 } 667 668 bool ARMV5PILongThunk::isCompatibleWith(const InputSection &isec, 669 const Relocation &rel) const { 670 // Thumb branch relocations can't use BLX 671 return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24; 672 } 673 674 void ThumbV6MABSLongThunk::writeLong(uint8_t *buf) { 675 // Most Thumb instructions cannot access the high registers r8 - r15. As the 676 // only register we can corrupt is r12 we must instead spill a low register 677 // to the stack to use as a scratch register. We push r1 even though we 678 // don't need to get some space to use for the return address. 679 const uint8_t data[] = { 680 0x03, 0xb4, // push {r0, r1} ; Obtain scratch registers 681 0x01, 0x48, // ldr r0, [pc, #4] ; L1 682 0x01, 0x90, // str r0, [sp, #4] ; SP + 4 = S 683 0x01, 0xbd, // pop {r0, pc} ; restore r0 and branch to dest 684 0x00, 0x00, 0x00, 0x00 // L1: .word S 685 }; 686 uint64_t s = getARMThunkDestVA(destination); 687 memcpy(buf, data, sizeof(data)); 688 target->relocateNoSym(buf + 8, R_ARM_ABS32, s); 689 } 690 691 void ThumbV6MABSLongThunk::addSymbols(ThunkSection &isec) { 692 addSymbol(saver().save("__Thumbv6MABSLongThunk_" + destination.getName()), 693 STT_FUNC, 1, isec); 694 addSymbol("$t", STT_NOTYPE, 0, isec); 695 addSymbol("$d", STT_NOTYPE, 8, isec); 696 } 697 698 void ThumbV6MPILongThunk::writeLong(uint8_t *buf) { 699 // Most Thumb instructions cannot access the high registers r8 - r15. As the 700 // only register we can corrupt is ip (r12) we must instead spill a low 701 // register to the stack to use as a scratch register. 702 const uint8_t data[] = { 703 0x01, 0xb4, // P: push {r0} ; Obtain scratch register 704 0x02, 0x48, // ldr r0, [pc, #8] ; L2 705 0x84, 0x46, // mov ip, r0 ; high to low register 706 0x01, 0xbc, // pop {r0} ; restore scratch register 707 0xe7, 0x44, // L1: add pc, ip ; transfer control 708 0xc0, 0x46, // nop ; pad to 4-byte boundary 709 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 4) 710 }; 711 uint64_t s = getARMThunkDestVA(destination); 712 uint64_t p = getThunkTargetSym()->getVA() & ~0x1; 713 memcpy(buf, data, sizeof(data)); 714 target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12); 715 } 716 717 void ThumbV6MPILongThunk::addSymbols(ThunkSection &isec) { 718 addSymbol(saver().save("__Thumbv6MPILongThunk_" + destination.getName()), 719 STT_FUNC, 1, isec); 720 addSymbol("$t", STT_NOTYPE, 0, isec); 721 addSymbol("$d", STT_NOTYPE, 12, isec); 722 } 723 724 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one. 725 void MipsThunk::writeTo(uint8_t *buf) { 726 uint64_t s = destination.getVA(); 727 write32(buf, 0x3c190000); // lui $25, %hi(func) 728 write32(buf + 4, 0x08000000 | (s >> 2)); // j func 729 write32(buf + 8, 0x27390000); // addiu $25, $25, %lo(func) 730 write32(buf + 12, 0x00000000); // nop 731 target->relocateNoSym(buf, R_MIPS_HI16, s); 732 target->relocateNoSym(buf + 8, R_MIPS_LO16, s); 733 } 734 735 void MipsThunk::addSymbols(ThunkSection &isec) { 736 addSymbol(saver().save("__LA25Thunk_" + destination.getName()), STT_FUNC, 0, 737 isec); 738 } 739 740 InputSection *MipsThunk::getTargetInputSection() const { 741 auto &dr = cast<Defined>(destination); 742 return dyn_cast<InputSection>(dr.section); 743 } 744 745 // Write microMIPS R2-R5 LA25 thunk code 746 // to call PIC function from the non-PIC one. 747 void MicroMipsThunk::writeTo(uint8_t *buf) { 748 uint64_t s = destination.getVA(); 749 write16(buf, 0x41b9); // lui $25, %hi(func) 750 write16(buf + 4, 0xd400); // j func 751 write16(buf + 8, 0x3339); // addiu $25, $25, %lo(func) 752 write16(buf + 12, 0x0c00); // nop 753 target->relocateNoSym(buf, R_MICROMIPS_HI16, s); 754 target->relocateNoSym(buf + 4, R_MICROMIPS_26_S1, s); 755 target->relocateNoSym(buf + 8, R_MICROMIPS_LO16, s); 756 } 757 758 void MicroMipsThunk::addSymbols(ThunkSection &isec) { 759 Defined *d = 760 addSymbol(saver().save("__microLA25Thunk_" + destination.getName()), 761 STT_FUNC, 0, isec); 762 d->stOther |= STO_MIPS_MICROMIPS; 763 } 764 765 InputSection *MicroMipsThunk::getTargetInputSection() const { 766 auto &dr = cast<Defined>(destination); 767 return dyn_cast<InputSection>(dr.section); 768 } 769 770 // Write microMIPS R6 LA25 thunk code 771 // to call PIC function from the non-PIC one. 772 void MicroMipsR6Thunk::writeTo(uint8_t *buf) { 773 uint64_t s = destination.getVA(); 774 uint64_t p = getThunkTargetSym()->getVA(); 775 write16(buf, 0x1320); // lui $25, %hi(func) 776 write16(buf + 4, 0x3339); // addiu $25, $25, %lo(func) 777 write16(buf + 8, 0x9400); // bc func 778 target->relocateNoSym(buf, R_MICROMIPS_HI16, s); 779 target->relocateNoSym(buf + 4, R_MICROMIPS_LO16, s); 780 target->relocateNoSym(buf + 8, R_MICROMIPS_PC26_S1, s - p - 12); 781 } 782 783 void MicroMipsR6Thunk::addSymbols(ThunkSection &isec) { 784 Defined *d = 785 addSymbol(saver().save("__microLA25Thunk_" + destination.getName()), 786 STT_FUNC, 0, isec); 787 d->stOther |= STO_MIPS_MICROMIPS; 788 } 789 790 InputSection *MicroMipsR6Thunk::getTargetInputSection() const { 791 auto &dr = cast<Defined>(destination); 792 return dyn_cast<InputSection>(dr.section); 793 } 794 795 void elf::writePPC32PltCallStub(uint8_t *buf, uint64_t gotPltVA, 796 const InputFile *file, int64_t addend) { 797 if (!config->isPic) { 798 write32(buf + 0, 0x3d600000 | (gotPltVA + 0x8000) >> 16); // lis r11,ha 799 write32(buf + 4, 0x816b0000 | (uint16_t)gotPltVA); // lwz r11,l(r11) 800 write32(buf + 8, 0x7d6903a6); // mtctr r11 801 write32(buf + 12, 0x4e800420); // bctr 802 return; 803 } 804 uint32_t offset; 805 if (addend >= 0x8000) { 806 // The stub loads an address relative to r30 (.got2+Addend). Addend is 807 // almost always 0x8000. The address of .got2 is different in another object 808 // file, so a stub cannot be shared. 809 offset = gotPltVA - 810 (in.ppc32Got2->getParent()->getVA() + 811 (file->ppc32Got2 ? file->ppc32Got2->outSecOff : 0) + addend); 812 } else { 813 // The stub loads an address relative to _GLOBAL_OFFSET_TABLE_ (which is 814 // currently the address of .got). 815 offset = gotPltVA - in.got->getVA(); 816 } 817 uint16_t ha = (offset + 0x8000) >> 16, l = (uint16_t)offset; 818 if (ha == 0) { 819 write32(buf + 0, 0x817e0000 | l); // lwz r11,l(r30) 820 write32(buf + 4, 0x7d6903a6); // mtctr r11 821 write32(buf + 8, 0x4e800420); // bctr 822 write32(buf + 12, 0x60000000); // nop 823 } else { 824 write32(buf + 0, 0x3d7e0000 | ha); // addis r11,r30,ha 825 write32(buf + 4, 0x816b0000 | l); // lwz r11,l(r11) 826 write32(buf + 8, 0x7d6903a6); // mtctr r11 827 write32(buf + 12, 0x4e800420); // bctr 828 } 829 } 830 831 void PPC32PltCallStub::writeTo(uint8_t *buf) { 832 writePPC32PltCallStub(buf, destination.getGotPltVA(), file, addend); 833 } 834 835 void PPC32PltCallStub::addSymbols(ThunkSection &isec) { 836 std::string buf; 837 raw_string_ostream os(buf); 838 os << format_hex_no_prefix(addend, 8); 839 if (!config->isPic) 840 os << ".plt_call32."; 841 else if (addend >= 0x8000) 842 os << ".got2.plt_pic32."; 843 else 844 os << ".plt_pic32."; 845 os << destination.getName(); 846 addSymbol(saver().save(os.str()), STT_FUNC, 0, isec); 847 } 848 849 bool PPC32PltCallStub::isCompatibleWith(const InputSection &isec, 850 const Relocation &rel) const { 851 return !config->isPic || (isec.file == file && rel.addend == addend); 852 } 853 854 void PPC32LongThunk::addSymbols(ThunkSection &isec) { 855 addSymbol(saver().save("__LongThunk_" + destination.getName()), STT_FUNC, 0, 856 isec); 857 } 858 859 void PPC32LongThunk::writeTo(uint8_t *buf) { 860 auto ha = [](uint32_t v) -> uint16_t { return (v + 0x8000) >> 16; }; 861 auto lo = [](uint32_t v) -> uint16_t { return v; }; 862 uint32_t d = destination.getVA(addend); 863 if (config->isPic) { 864 uint32_t off = d - (getThunkTargetSym()->getVA() + 8); 865 write32(buf + 0, 0x7c0802a6); // mflr r12,0 866 write32(buf + 4, 0x429f0005); // bcl r20,r31,.+4 867 write32(buf + 8, 0x7d8802a6); // mtctr r12 868 write32(buf + 12, 0x3d8c0000 | ha(off)); // addis r12,r12,off@ha 869 write32(buf + 16, 0x398c0000 | lo(off)); // addi r12,r12,off@l 870 write32(buf + 20, 0x7c0803a6); // mtlr r0 871 buf += 24; 872 } else { 873 write32(buf + 0, 0x3d800000 | ha(d)); // lis r12,d@ha 874 write32(buf + 4, 0x398c0000 | lo(d)); // addi r12,r12,d@l 875 buf += 8; 876 } 877 write32(buf + 0, 0x7d8903a6); // mtctr r12 878 write32(buf + 4, 0x4e800420); // bctr 879 } 880 881 void elf::writePPC64LoadAndBranch(uint8_t *buf, int64_t offset) { 882 uint16_t offHa = (offset + 0x8000) >> 16; 883 uint16_t offLo = offset & 0xffff; 884 885 write32(buf + 0, 0x3d820000 | offHa); // addis r12, r2, OffHa 886 write32(buf + 4, 0xe98c0000 | offLo); // ld r12, OffLo(r12) 887 write32(buf + 8, 0x7d8903a6); // mtctr r12 888 write32(buf + 12, 0x4e800420); // bctr 889 } 890 891 void PPC64PltCallStub::writeTo(uint8_t *buf) { 892 int64_t offset = destination.getGotPltVA() - getPPC64TocBase(); 893 // Save the TOC pointer to the save-slot reserved in the call frame. 894 write32(buf + 0, 0xf8410018); // std r2,24(r1) 895 writePPC64LoadAndBranch(buf + 4, offset); 896 } 897 898 void PPC64PltCallStub::addSymbols(ThunkSection &isec) { 899 Defined *s = addSymbol(saver().save("__plt_" + destination.getName()), 900 STT_FUNC, 0, isec); 901 s->needsTocRestore = true; 902 s->file = destination.file; 903 } 904 905 bool PPC64PltCallStub::isCompatibleWith(const InputSection &isec, 906 const Relocation &rel) const { 907 return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14; 908 } 909 910 void PPC64R2SaveStub::writeTo(uint8_t *buf) { 911 const int64_t offset = computeOffset(); 912 write32(buf + 0, 0xf8410018); // std r2,24(r1) 913 // The branch offset needs to fit in 26 bits. 914 if (getMayUseShortThunk()) { 915 write32(buf + 4, 0x48000000 | (offset & 0x03fffffc)); // b <offset> 916 } else if (isInt<34>(offset)) { 917 int nextInstOffset; 918 if (!config->power10Stubs) { 919 uint64_t tocOffset = destination.getVA() - getPPC64TocBase(); 920 if (tocOffset >> 16 > 0) { 921 const uint64_t addi = ADDI_R12_TO_R12_NO_DISP | (tocOffset & 0xffff); 922 const uint64_t addis = ADDIS_R12_TO_R2_NO_DISP | ((tocOffset >> 16) & 0xffff); 923 write32(buf + 4, addis); // addis r12, r2 , top of offset 924 write32(buf + 8, addi); // addi r12, r12, bottom of offset 925 nextInstOffset = 12; 926 } else { 927 const uint64_t addi = ADDI_R12_TO_R2_NO_DISP | (tocOffset & 0xffff); 928 write32(buf + 4, addi); // addi r12, r2, offset 929 nextInstOffset = 8; 930 } 931 } else { 932 const uint64_t paddi = PADDI_R12_NO_DISP | 933 (((offset >> 16) & 0x3ffff) << 32) | 934 (offset & 0xffff); 935 writePrefixedInstruction(buf + 4, paddi); // paddi r12, 0, func@pcrel, 1 936 nextInstOffset = 12; 937 } 938 write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12 939 write32(buf + nextInstOffset + 4, BCTR); // bctr 940 } else { 941 in.ppc64LongBranchTarget->addEntry(&destination, addend); 942 const int64_t offsetFromTOC = 943 in.ppc64LongBranchTarget->getEntryVA(&destination, addend) - 944 getPPC64TocBase(); 945 writePPC64LoadAndBranch(buf + 4, offsetFromTOC); 946 } 947 } 948 949 void PPC64R2SaveStub::addSymbols(ThunkSection &isec) { 950 Defined *s = addSymbol(saver().save("__toc_save_" + destination.getName()), 951 STT_FUNC, 0, isec); 952 s->needsTocRestore = true; 953 } 954 955 bool PPC64R2SaveStub::isCompatibleWith(const InputSection &isec, 956 const Relocation &rel) const { 957 return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14; 958 } 959 960 void PPC64R12SetupStub::writeTo(uint8_t *buf) { 961 int64_t offset = destination.getVA() - getThunkTargetSym()->getVA(); 962 if (!isInt<34>(offset)) 963 reportRangeError(buf, offset, 34, destination, "R12 setup stub offset"); 964 965 int nextInstOffset; 966 if (!config->power10Stubs) { 967 uint32_t off = destination.getVA(addend) - getThunkTargetSym()->getVA() - 8; 968 write32(buf + 0, 0x7c0802a6); // mflr r12 969 write32(buf + 4, 0x429f0005); // bcl 20,31,.+4 970 write32(buf + 8, 0x7d6802a6); // mflr r11 971 write32(buf + 12, 0x7d8803a6); // mtlr r12 972 write32(buf + 16, 0x3d8b0000 | computeHiBits(off));// addis r12,r11,off@ha 973 write32(buf + 20, 0x398c0000 | (off & 0xffff)); // addi r12,r12,off@l 974 nextInstOffset = 24; 975 } else { 976 uint64_t paddi = PADDI_R12_NO_DISP | (((offset >> 16) & 0x3ffff) << 32) | 977 (offset & 0xffff); 978 writePrefixedInstruction(buf + 0, paddi); // paddi r12, 0, func@pcrel, 1 979 nextInstOffset = 8; 980 } 981 write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12 982 write32(buf + nextInstOffset + 4, BCTR); // bctr 983 } 984 985 void PPC64R12SetupStub::addSymbols(ThunkSection &isec) { 986 addSymbol(saver().save("__gep_setup_" + destination.getName()), STT_FUNC, 0, 987 isec); 988 } 989 990 bool PPC64R12SetupStub::isCompatibleWith(const InputSection &isec, 991 const Relocation &rel) const { 992 return rel.type == R_PPC64_REL24_NOTOC; 993 } 994 995 void PPC64PCRelPLTStub::writeTo(uint8_t *buf) { 996 int nextInstOffset = 0; 997 int64_t offset = destination.getGotPltVA() - getThunkTargetSym()->getVA(); 998 999 if (config->power10Stubs) { 1000 if (!isInt<34>(offset)) 1001 reportRangeError(buf, offset, 34, destination, 1002 "PC-relative PLT stub offset"); 1003 const uint64_t pld = PLD_R12_NO_DISP | (((offset >> 16) & 0x3ffff) << 32) | 1004 (offset & 0xffff); 1005 writePrefixedInstruction(buf + 0, pld); // pld r12, func@plt@pcrel 1006 nextInstOffset = 8; 1007 } else { 1008 uint32_t off = destination.getVA(addend) - getThunkTargetSym()->getVA() - 8; 1009 write32(buf + 0, 0x7c0802a6); // mflr r12 1010 write32(buf + 4, 0x429f0005); // bcl 20,31,.+4 1011 write32(buf + 8, 0x7d6802a6); // mflr r11 1012 write32(buf + 12, 0x7d8803a6); // mtlr r12 1013 write32(buf + 16, 0x3d8b0000 | computeHiBits(off)); // addis r12,r11,off@ha 1014 write32(buf + 20, 0x398c0000 | (off & 0xffff)); // addi r12,r12,off@l 1015 nextInstOffset = 24; 1016 } 1017 write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12 1018 write32(buf + nextInstOffset + 4, BCTR); // bctr 1019 } 1020 1021 void PPC64PCRelPLTStub::addSymbols(ThunkSection &isec) { 1022 addSymbol(saver().save("__plt_pcrel_" + destination.getName()), STT_FUNC, 0, 1023 isec); 1024 } 1025 1026 bool PPC64PCRelPLTStub::isCompatibleWith(const InputSection &isec, 1027 const Relocation &rel) const { 1028 return rel.type == R_PPC64_REL24_NOTOC; 1029 } 1030 1031 void PPC64LongBranchThunk::writeTo(uint8_t *buf) { 1032 int64_t offset = in.ppc64LongBranchTarget->getEntryVA(&destination, addend) - 1033 getPPC64TocBase(); 1034 writePPC64LoadAndBranch(buf, offset); 1035 } 1036 1037 void PPC64LongBranchThunk::addSymbols(ThunkSection &isec) { 1038 addSymbol(saver().save("__long_branch_" + destination.getName()), STT_FUNC, 0, 1039 isec); 1040 } 1041 1042 bool PPC64LongBranchThunk::isCompatibleWith(const InputSection &isec, 1043 const Relocation &rel) const { 1044 return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14; 1045 } 1046 1047 Thunk::Thunk(Symbol &d, int64_t a) : destination(d), addend(a), offset(0) {} 1048 1049 Thunk::~Thunk() = default; 1050 1051 static Thunk *addThunkAArch64(RelType type, Symbol &s, int64_t a) { 1052 if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 && 1053 type != R_AARCH64_PLT32) 1054 fatal("unrecognized relocation type"); 1055 if (config->picThunk) 1056 return make<AArch64ADRPThunk>(s, a); 1057 return make<AArch64ABSLongThunk>(s, a); 1058 } 1059 1060 // Creates a thunk for Thumb-ARM interworking. 1061 // Arm Architectures v5 and v6 do not support Thumb2 technology. This means 1062 // - MOVT and MOVW instructions cannot be used 1063 // - Only Thumb relocation that can generate a Thunk is a BL, this can always 1064 // be transformed into a BLX 1065 static Thunk *addThunkPreArmv7(RelType reloc, Symbol &s, int64_t a) { 1066 switch (reloc) { 1067 case R_ARM_PC24: 1068 case R_ARM_PLT32: 1069 case R_ARM_JUMP24: 1070 case R_ARM_CALL: 1071 case R_ARM_THM_CALL: 1072 if (config->picThunk) 1073 return make<ARMV5PILongThunk>(s, a); 1074 return make<ARMV5ABSLongThunk>(s, a); 1075 } 1076 fatal("relocation " + toString(reloc) + " to " + toString(s) + 1077 " not supported for Armv5 or Armv6 targets"); 1078 } 1079 1080 // Create a thunk for Thumb long branch on V6-M. 1081 // Arm Architecture v6-M only supports Thumb instructions. This means 1082 // - MOVT and MOVW instructions cannot be used. 1083 // - Only a limited number of instructions can access registers r8 and above 1084 // - No interworking support is needed (all Thumb). 1085 static Thunk *addThunkV6M(RelType reloc, Symbol &s, int64_t a) { 1086 switch (reloc) { 1087 case R_ARM_THM_JUMP19: 1088 case R_ARM_THM_JUMP24: 1089 case R_ARM_THM_CALL: 1090 if (config->isPic) 1091 return make<ThumbV6MPILongThunk>(s, a); 1092 return make<ThumbV6MABSLongThunk>(s, a); 1093 } 1094 fatal("relocation " + toString(reloc) + " to " + toString(s) + 1095 " not supported for Armv6-M targets"); 1096 } 1097 1098 // Creates a thunk for Thumb-ARM interworking or branch range extension. 1099 static Thunk *addThunkArm(RelType reloc, Symbol &s, int64_t a) { 1100 // Decide which Thunk is needed based on: 1101 // Available instruction set 1102 // - An Arm Thunk can only be used if Arm state is available. 1103 // - A Thumb Thunk can only be used if Thumb state is available. 1104 // - Can only use a Thunk if it uses instructions that the Target supports. 1105 // Relocation is branch or branch and link 1106 // - Branch instructions cannot change state, can only select Thunk that 1107 // starts in the same state as the caller. 1108 // - Branch and link relocations can change state, can select Thunks from 1109 // either Arm or Thumb. 1110 // Position independent Thunks if we require position independent code. 1111 1112 // Handle architectures that have restrictions on the instructions that they 1113 // can use in Thunks. The flags below are set by reading the BuildAttributes 1114 // of the input objects. InputFiles.cpp contains the mapping from ARM 1115 // architecture to flag. 1116 if (!config->armHasMovtMovw) { 1117 if (!config->armJ1J2BranchEncoding) 1118 return addThunkPreArmv7(reloc, s, a); 1119 return addThunkV6M(reloc, s, a); 1120 } 1121 1122 switch (reloc) { 1123 case R_ARM_PC24: 1124 case R_ARM_PLT32: 1125 case R_ARM_JUMP24: 1126 case R_ARM_CALL: 1127 if (config->picThunk) 1128 return make<ARMV7PILongThunk>(s, a); 1129 return make<ARMV7ABSLongThunk>(s, a); 1130 case R_ARM_THM_JUMP19: 1131 case R_ARM_THM_JUMP24: 1132 case R_ARM_THM_CALL: 1133 if (config->picThunk) 1134 return make<ThumbV7PILongThunk>(s, a); 1135 return make<ThumbV7ABSLongThunk>(s, a); 1136 } 1137 fatal("unrecognized relocation type"); 1138 } 1139 1140 static Thunk *addThunkMips(RelType type, Symbol &s) { 1141 if ((s.stOther & STO_MIPS_MICROMIPS) && isMipsR6()) 1142 return make<MicroMipsR6Thunk>(s); 1143 if (s.stOther & STO_MIPS_MICROMIPS) 1144 return make<MicroMipsThunk>(s); 1145 return make<MipsThunk>(s); 1146 } 1147 1148 static Thunk *addThunkPPC32(const InputSection &isec, const Relocation &rel, 1149 Symbol &s) { 1150 assert((rel.type == R_PPC_LOCAL24PC || rel.type == R_PPC_REL24 || 1151 rel.type == R_PPC_PLTREL24) && 1152 "unexpected relocation type for thunk"); 1153 if (s.isInPlt()) 1154 return make<PPC32PltCallStub>(isec, rel, s); 1155 return make<PPC32LongThunk>(s, rel.addend); 1156 } 1157 1158 static Thunk *addThunkPPC64(RelType type, Symbol &s, int64_t a) { 1159 assert((type == R_PPC64_REL14 || type == R_PPC64_REL24 || 1160 type == R_PPC64_REL24_NOTOC) && 1161 "unexpected relocation type for thunk"); 1162 if (s.isInPlt()) 1163 return type == R_PPC64_REL24_NOTOC ? (Thunk *)make<PPC64PCRelPLTStub>(s) 1164 : (Thunk *)make<PPC64PltCallStub>(s); 1165 1166 // This check looks at the st_other bits of the callee. If the value is 1 1167 // then the callee clobbers the TOC and we need an R2 save stub when RelType 1168 // is R_PPC64_REL14 or R_PPC64_REL24. 1169 if ((type == R_PPC64_REL14 || type == R_PPC64_REL24) && (s.stOther >> 5) == 1) 1170 return make<PPC64R2SaveStub>(s, a); 1171 1172 if (type == R_PPC64_REL24_NOTOC) 1173 return make<PPC64R12SetupStub>(s); 1174 1175 if (config->picThunk) 1176 return make<PPC64PILongBranchThunk>(s, a); 1177 1178 return make<PPC64PDLongBranchThunk>(s, a); 1179 } 1180 1181 Thunk *elf::addThunk(const InputSection &isec, Relocation &rel) { 1182 Symbol &s = *rel.sym; 1183 int64_t a = rel.addend; 1184 1185 if (config->emachine == EM_AARCH64) 1186 return addThunkAArch64(rel.type, s, a); 1187 1188 if (config->emachine == EM_ARM) 1189 return addThunkArm(rel.type, s, a); 1190 1191 if (config->emachine == EM_MIPS) 1192 return addThunkMips(rel.type, s); 1193 1194 if (config->emachine == EM_PPC) 1195 return addThunkPPC32(isec, rel, s); 1196 1197 if (config->emachine == EM_PPC64) 1198 return addThunkPPC64(rel.type, s, a); 1199 1200 llvm_unreachable("add Thunk only supported for ARM, Mips and PowerPC"); 1201 } 1202