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