1 //===- PPC64.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 #include "InputFiles.h" 10 #include "OutputSections.h" 11 #include "SymbolTable.h" 12 #include "Symbols.h" 13 #include "SyntheticSections.h" 14 #include "Target.h" 15 #include "Thunks.h" 16 #include "lld/Common/CommonLinkerContext.h" 17 #include "llvm/Support/Endian.h" 18 19 using namespace llvm; 20 using namespace llvm::object; 21 using namespace llvm::support::endian; 22 using namespace llvm::ELF; 23 using namespace lld; 24 using namespace lld::elf; 25 26 constexpr uint64_t ppc64TocOffset = 0x8000; 27 constexpr uint64_t dynamicThreadPointerOffset = 0x8000; 28 29 // The instruction encoding of bits 21-30 from the ISA for the Xform and Dform 30 // instructions that can be used as part of the initial exec TLS sequence. 31 enum XFormOpcd { 32 LBZX = 87, 33 LHZX = 279, 34 LWZX = 23, 35 LDX = 21, 36 STBX = 215, 37 STHX = 407, 38 STWX = 151, 39 STDX = 149, 40 ADD = 266, 41 }; 42 43 enum DFormOpcd { 44 LBZ = 34, 45 LBZU = 35, 46 LHZ = 40, 47 LHZU = 41, 48 LHAU = 43, 49 LWZ = 32, 50 LWZU = 33, 51 LFSU = 49, 52 LD = 58, 53 LFDU = 51, 54 STB = 38, 55 STBU = 39, 56 STH = 44, 57 STHU = 45, 58 STW = 36, 59 STWU = 37, 60 STFSU = 53, 61 STFDU = 55, 62 STD = 62, 63 ADDI = 14 64 }; 65 66 constexpr uint32_t NOP = 0x60000000; 67 68 enum class PPCLegacyInsn : uint32_t { 69 NOINSN = 0, 70 // Loads. 71 LBZ = 0x88000000, 72 LHZ = 0xa0000000, 73 LWZ = 0x80000000, 74 LHA = 0xa8000000, 75 LWA = 0xe8000002, 76 LD = 0xe8000000, 77 LFS = 0xC0000000, 78 LXSSP = 0xe4000003, 79 LFD = 0xc8000000, 80 LXSD = 0xe4000002, 81 LXV = 0xf4000001, 82 LXVP = 0x18000000, 83 84 // Stores. 85 STB = 0x98000000, 86 STH = 0xb0000000, 87 STW = 0x90000000, 88 STD = 0xf8000000, 89 STFS = 0xd0000000, 90 STXSSP = 0xf4000003, 91 STFD = 0xd8000000, 92 STXSD = 0xf4000002, 93 STXV = 0xf4000005, 94 STXVP = 0x18000001 95 }; 96 enum class PPCPrefixedInsn : uint64_t { 97 NOINSN = 0, 98 PREFIX_MLS = 0x0610000000000000, 99 PREFIX_8LS = 0x0410000000000000, 100 101 // Loads. 102 PLBZ = PREFIX_MLS, 103 PLHZ = PREFIX_MLS, 104 PLWZ = PREFIX_MLS, 105 PLHA = PREFIX_MLS, 106 PLWA = PREFIX_8LS | 0xa4000000, 107 PLD = PREFIX_8LS | 0xe4000000, 108 PLFS = PREFIX_MLS, 109 PLXSSP = PREFIX_8LS | 0xac000000, 110 PLFD = PREFIX_MLS, 111 PLXSD = PREFIX_8LS | 0xa8000000, 112 PLXV = PREFIX_8LS | 0xc8000000, 113 PLXVP = PREFIX_8LS | 0xe8000000, 114 115 // Stores. 116 PSTB = PREFIX_MLS, 117 PSTH = PREFIX_MLS, 118 PSTW = PREFIX_MLS, 119 PSTD = PREFIX_8LS | 0xf4000000, 120 PSTFS = PREFIX_MLS, 121 PSTXSSP = PREFIX_8LS | 0xbc000000, 122 PSTFD = PREFIX_MLS, 123 PSTXSD = PREFIX_8LS | 0xb8000000, 124 PSTXV = PREFIX_8LS | 0xd8000000, 125 PSTXVP = PREFIX_8LS | 0xf8000000 126 }; 127 static bool checkPPCLegacyInsn(uint32_t encoding) { 128 PPCLegacyInsn insn = static_cast<PPCLegacyInsn>(encoding); 129 if (insn == PPCLegacyInsn::NOINSN) 130 return false; 131 #define PCREL_OPT(Legacy, PCRel, InsnMask) \ 132 if (insn == PPCLegacyInsn::Legacy) \ 133 return true; 134 #include "PPCInsns.def" 135 #undef PCREL_OPT 136 return false; 137 } 138 139 // Masks to apply to legacy instructions when converting them to prefixed, 140 // pc-relative versions. For the most part, the primary opcode is shared 141 // between the legacy instruction and the suffix of its prefixed version. 142 // However, there are some instances where that isn't the case (DS-Form and 143 // DQ-form instructions). 144 enum class LegacyToPrefixMask : uint64_t { 145 NOMASK = 0x0, 146 OPC_AND_RST = 0xffe00000, // Primary opc (0-5) and R[ST] (6-10). 147 ONLY_RST = 0x3e00000, // [RS]T (6-10). 148 ST_STX28_TO5 = 149 0x8000000003e00000, // S/T (6-10) - The [S/T]X bit moves from 28 to 5. 150 }; 151 152 namespace { 153 class PPC64 final : public TargetInfo { 154 public: 155 PPC64(); 156 int getTlsGdRelaxSkip(RelType type) const override; 157 uint32_t calcEFlags() const override; 158 RelExpr getRelExpr(RelType type, const Symbol &s, 159 const uint8_t *loc) const override; 160 RelType getDynRel(RelType type) const override; 161 int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override; 162 void writePltHeader(uint8_t *buf) const override; 163 void writePlt(uint8_t *buf, const Symbol &sym, 164 uint64_t pltEntryAddr) const override; 165 void writeIplt(uint8_t *buf, const Symbol &sym, 166 uint64_t pltEntryAddr) const override; 167 void relocate(uint8_t *loc, const Relocation &rel, 168 uint64_t val) const override; 169 void writeGotHeader(uint8_t *buf) const override; 170 bool needsThunk(RelExpr expr, RelType type, const InputFile *file, 171 uint64_t branchAddr, const Symbol &s, 172 int64_t a) const override; 173 uint32_t getThunkSectionSpacing() const override; 174 bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override; 175 RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override; 176 RelExpr adjustGotPcExpr(RelType type, int64_t addend, 177 const uint8_t *loc) const override; 178 void relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) const; 179 void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override; 180 181 bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end, 182 uint8_t stOther) const override; 183 184 private: 185 void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const; 186 void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const; 187 void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const; 188 void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const; 189 }; 190 } // namespace 191 192 uint64_t elf::getPPC64TocBase() { 193 // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The 194 // TOC starts where the first of these sections starts. We always create a 195 // .got when we see a relocation that uses it, so for us the start is always 196 // the .got. 197 uint64_t tocVA = in.got->getVA(); 198 199 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 200 // thus permitting a full 64 Kbytes segment. Note that the glibc startup 201 // code (crt1.o) assumes that you can get from the TOC base to the 202 // start of the .toc section with only a single (signed) 16-bit relocation. 203 return tocVA + ppc64TocOffset; 204 } 205 206 unsigned elf::getPPC64GlobalEntryToLocalEntryOffset(uint8_t stOther) { 207 // The offset is encoded into the 3 most significant bits of the st_other 208 // field, with some special values described in section 3.4.1 of the ABI: 209 // 0 --> Zero offset between the GEP and LEP, and the function does NOT use 210 // the TOC pointer (r2). r2 will hold the same value on returning from 211 // the function as it did on entering the function. 212 // 1 --> Zero offset between the GEP and LEP, and r2 should be treated as a 213 // caller-saved register for all callers. 214 // 2-6 --> The binary logarithm of the offset eg: 215 // 2 --> 2^2 = 4 bytes --> 1 instruction. 216 // 6 --> 2^6 = 64 bytes --> 16 instructions. 217 // 7 --> Reserved. 218 uint8_t gepToLep = (stOther >> 5) & 7; 219 if (gepToLep < 2) 220 return 0; 221 222 // The value encoded in the st_other bits is the 223 // log-base-2(offset). 224 if (gepToLep < 7) 225 return 1 << gepToLep; 226 227 error("reserved value of 7 in the 3 most-significant-bits of st_other"); 228 return 0; 229 } 230 231 void elf::writePrefixedInstruction(uint8_t *loc, uint64_t insn) { 232 insn = config->isLE ? insn << 32 | insn >> 32 : insn; 233 write64(loc, insn); 234 } 235 236 static bool addOptional(StringRef name, uint64_t value, 237 std::vector<Defined *> &defined) { 238 Symbol *sym = symtab.find(name); 239 if (!sym || sym->isDefined()) 240 return false; 241 sym->resolve(Defined{/*file=*/nullptr, StringRef(), STB_GLOBAL, STV_HIDDEN, 242 STT_FUNC, value, 243 /*size=*/0, /*section=*/nullptr}); 244 defined.push_back(cast<Defined>(sym)); 245 return true; 246 } 247 248 // If from is 14, write ${prefix}14: firstInsn; ${prefix}15: 249 // firstInsn+0x200008; ...; ${prefix}31: firstInsn+(31-14)*0x200008; $tail 250 // The labels are defined only if they exist in the symbol table. 251 static void writeSequence(MutableArrayRef<uint32_t> buf, const char *prefix, 252 int from, uint32_t firstInsn, 253 ArrayRef<uint32_t> tail) { 254 std::vector<Defined *> defined; 255 char name[16]; 256 int first; 257 uint32_t *ptr = buf.data(); 258 for (int r = from; r < 32; ++r) { 259 format("%s%d", prefix, r).snprint(name, sizeof(name)); 260 if (addOptional(name, 4 * (r - from), defined) && defined.size() == 1) 261 first = r - from; 262 write32(ptr++, firstInsn + 0x200008 * (r - from)); 263 } 264 for (uint32_t insn : tail) 265 write32(ptr++, insn); 266 assert(ptr == &*buf.end()); 267 268 if (defined.empty()) 269 return; 270 // The full section content has the extent of [begin, end). We drop unused 271 // instructions and write [first,end). 272 auto *sec = make<InputSection>( 273 nullptr, SHF_ALLOC, SHT_PROGBITS, 4, 274 ArrayRef(reinterpret_cast<uint8_t *>(buf.data() + first), 275 4 * (buf.size() - first)), 276 ".text"); 277 ctx.inputSections.push_back(sec); 278 for (Defined *sym : defined) { 279 sym->section = sec; 280 sym->value -= 4 * first; 281 } 282 } 283 284 // Implements some save and restore functions as described by ELF V2 ABI to be 285 // compatible with GCC. With GCC -Os, when the number of call-saved registers 286 // exceeds a certain threshold, GCC generates _savegpr0_* _restgpr0_* calls and 287 // expects the linker to define them. See 288 // https://sourceware.org/pipermail/binutils/2002-February/017444.html and 289 // https://sourceware.org/pipermail/binutils/2004-August/036765.html . This is 290 // weird because libgcc.a would be the natural place. The linker generation 291 // approach has the advantage that the linker can generate multiple copies to 292 // avoid long branch thunks. However, we don't consider the advantage 293 // significant enough to complicate our trunk implementation, so we take the 294 // simple approach and synthesize .text sections providing the implementation. 295 void elf::addPPC64SaveRestore() { 296 static uint32_t savegpr0[20], restgpr0[21], savegpr1[19], restgpr1[19]; 297 constexpr uint32_t blr = 0x4e800020, mtlr_0 = 0x7c0803a6; 298 299 // _restgpr0_14: ld 14, -144(1); _restgpr0_15: ld 15, -136(1); ... 300 // Tail: ld 0, 16(1); mtlr 0; blr 301 writeSequence(restgpr0, "_restgpr0_", 14, 0xe9c1ff70, 302 {0xe8010010, mtlr_0, blr}); 303 // _restgpr1_14: ld 14, -144(12); _restgpr1_15: ld 15, -136(12); ... 304 // Tail: blr 305 writeSequence(restgpr1, "_restgpr1_", 14, 0xe9ccff70, {blr}); 306 // _savegpr0_14: std 14, -144(1); _savegpr0_15: std 15, -136(1); ... 307 // Tail: std 0, 16(1); blr 308 writeSequence(savegpr0, "_savegpr0_", 14, 0xf9c1ff70, {0xf8010010, blr}); 309 // _savegpr1_14: std 14, -144(12); _savegpr1_15: std 15, -136(12); ... 310 // Tail: blr 311 writeSequence(savegpr1, "_savegpr1_", 14, 0xf9ccff70, {blr}); 312 } 313 314 // Find the R_PPC64_ADDR64 in .rela.toc with matching offset. 315 template <typename ELFT> 316 static std::pair<Defined *, int64_t> 317 getRelaTocSymAndAddend(InputSectionBase *tocSec, uint64_t offset) { 318 // .rela.toc contains exclusively R_PPC64_ADDR64 relocations sorted by 319 // r_offset: 0, 8, 16, etc. For a given Offset, Offset / 8 gives us the 320 // relocation index in most cases. 321 // 322 // In rare cases a TOC entry may store a constant that doesn't need an 323 // R_PPC64_ADDR64, the corresponding r_offset is therefore missing. Offset / 8 324 // points to a relocation with larger r_offset. Do a linear probe then. 325 // Constants are extremely uncommon in .toc and the extra number of array 326 // accesses can be seen as a small constant. 327 ArrayRef<typename ELFT::Rela> relas = 328 tocSec->template relsOrRelas<ELFT>().relas; 329 if (relas.empty()) 330 return {}; 331 uint64_t index = std::min<uint64_t>(offset / 8, relas.size() - 1); 332 for (;;) { 333 if (relas[index].r_offset == offset) { 334 Symbol &sym = tocSec->getFile<ELFT>()->getRelocTargetSym(relas[index]); 335 return {dyn_cast<Defined>(&sym), getAddend<ELFT>(relas[index])}; 336 } 337 if (relas[index].r_offset < offset || index == 0) 338 break; 339 --index; 340 } 341 return {}; 342 } 343 344 // When accessing a symbol defined in another translation unit, compilers 345 // reserve a .toc entry, allocate a local label and generate toc-indirect 346 // instructions: 347 // 348 // addis 3, 2, .LC0@toc@ha # R_PPC64_TOC16_HA 349 // ld 3, .LC0@toc@l(3) # R_PPC64_TOC16_LO_DS, load the address from a .toc entry 350 // ld/lwa 3, 0(3) # load the value from the address 351 // 352 // .section .toc,"aw",@progbits 353 // .LC0: .tc var[TC],var 354 // 355 // If var is defined, non-preemptable and addressable with a 32-bit signed 356 // offset from the toc base, the address of var can be computed by adding an 357 // offset to the toc base, saving a load. 358 // 359 // addis 3,2,var@toc@ha # this may be relaxed to a nop, 360 // addi 3,3,var@toc@l # then this becomes addi 3,2,var@toc 361 // ld/lwa 3, 0(3) # load the value from the address 362 // 363 // Returns true if the relaxation is performed. 364 static bool tryRelaxPPC64TocIndirection(const Relocation &rel, 365 uint8_t *bufLoc) { 366 assert(config->tocOptimize); 367 if (rel.addend < 0) 368 return false; 369 370 // If the symbol is not the .toc section, this isn't a toc-indirection. 371 Defined *defSym = dyn_cast<Defined>(rel.sym); 372 if (!defSym || !defSym->isSection() || defSym->section->name != ".toc") 373 return false; 374 375 Defined *d; 376 int64_t addend; 377 auto *tocISB = cast<InputSectionBase>(defSym->section); 378 std::tie(d, addend) = 379 config->isLE ? getRelaTocSymAndAddend<ELF64LE>(tocISB, rel.addend) 380 : getRelaTocSymAndAddend<ELF64BE>(tocISB, rel.addend); 381 382 // Only non-preemptable defined symbols can be relaxed. 383 if (!d || d->isPreemptible) 384 return false; 385 386 // R_PPC64_ADDR64 should have created a canonical PLT for the non-preemptable 387 // ifunc and changed its type to STT_FUNC. 388 assert(!d->isGnuIFunc()); 389 390 // Two instructions can materialize a 32-bit signed offset from the toc base. 391 uint64_t tocRelative = d->getVA(addend) - getPPC64TocBase(); 392 if (!isInt<32>(tocRelative)) 393 return false; 394 395 // Add PPC64TocOffset that will be subtracted by PPC64::relocate(). 396 static_cast<const PPC64 &>(*target).relaxGot(bufLoc, rel, 397 tocRelative + ppc64TocOffset); 398 return true; 399 } 400 401 // Relocation masks following the #lo(value), #hi(value), #ha(value), 402 // #higher(value), #highera(value), #highest(value), and #highesta(value) 403 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi 404 // document. 405 static uint16_t lo(uint64_t v) { return v; } 406 static uint16_t hi(uint64_t v) { return v >> 16; } 407 static uint64_t ha(uint64_t v) { return (v + 0x8000) >> 16; } 408 static uint16_t higher(uint64_t v) { return v >> 32; } 409 static uint16_t highera(uint64_t v) { return (v + 0x8000) >> 32; } 410 static uint16_t highest(uint64_t v) { return v >> 48; } 411 static uint16_t highesta(uint64_t v) { return (v + 0x8000) >> 48; } 412 413 // Extracts the 'PO' field of an instruction encoding. 414 static uint8_t getPrimaryOpCode(uint32_t encoding) { return (encoding >> 26); } 415 416 static bool isDQFormInstruction(uint32_t encoding) { 417 switch (getPrimaryOpCode(encoding)) { 418 default: 419 return false; 420 case 6: // Power10 paired loads/stores (lxvp, stxvp). 421 case 56: 422 // The only instruction with a primary opcode of 56 is `lq`. 423 return true; 424 case 61: 425 // There are both DS and DQ instruction forms with this primary opcode. 426 // Namely `lxv` and `stxv` are the DQ-forms that use it. 427 // The DS 'XO' bits being set to 01 is restricted to DQ form. 428 return (encoding & 3) == 0x1; 429 } 430 } 431 432 static bool isDSFormInstruction(PPCLegacyInsn insn) { 433 switch (insn) { 434 default: 435 return false; 436 case PPCLegacyInsn::LWA: 437 case PPCLegacyInsn::LD: 438 case PPCLegacyInsn::LXSD: 439 case PPCLegacyInsn::LXSSP: 440 case PPCLegacyInsn::STD: 441 case PPCLegacyInsn::STXSD: 442 case PPCLegacyInsn::STXSSP: 443 return true; 444 } 445 } 446 447 static PPCLegacyInsn getPPCLegacyInsn(uint32_t encoding) { 448 uint32_t opc = encoding & 0xfc000000; 449 450 // If the primary opcode is shared between multiple instructions, we need to 451 // fix it up to match the actual instruction we are after. 452 if ((opc == 0xe4000000 || opc == 0xe8000000 || opc == 0xf4000000 || 453 opc == 0xf8000000) && 454 !isDQFormInstruction(encoding)) 455 opc = encoding & 0xfc000003; 456 else if (opc == 0xf4000000) 457 opc = encoding & 0xfc000007; 458 else if (opc == 0x18000000) 459 opc = encoding & 0xfc00000f; 460 461 // If the value is not one of the enumerators in PPCLegacyInsn, we want to 462 // return PPCLegacyInsn::NOINSN. 463 if (!checkPPCLegacyInsn(opc)) 464 return PPCLegacyInsn::NOINSN; 465 return static_cast<PPCLegacyInsn>(opc); 466 } 467 468 static PPCPrefixedInsn getPCRelativeForm(PPCLegacyInsn insn) { 469 switch (insn) { 470 #define PCREL_OPT(Legacy, PCRel, InsnMask) \ 471 case PPCLegacyInsn::Legacy: \ 472 return PPCPrefixedInsn::PCRel 473 #include "PPCInsns.def" 474 #undef PCREL_OPT 475 } 476 return PPCPrefixedInsn::NOINSN; 477 } 478 479 static LegacyToPrefixMask getInsnMask(PPCLegacyInsn insn) { 480 switch (insn) { 481 #define PCREL_OPT(Legacy, PCRel, InsnMask) \ 482 case PPCLegacyInsn::Legacy: \ 483 return LegacyToPrefixMask::InsnMask 484 #include "PPCInsns.def" 485 #undef PCREL_OPT 486 } 487 return LegacyToPrefixMask::NOMASK; 488 } 489 static uint64_t getPCRelativeForm(uint32_t encoding) { 490 PPCLegacyInsn origInsn = getPPCLegacyInsn(encoding); 491 PPCPrefixedInsn pcrelInsn = getPCRelativeForm(origInsn); 492 if (pcrelInsn == PPCPrefixedInsn::NOINSN) 493 return UINT64_C(-1); 494 LegacyToPrefixMask origInsnMask = getInsnMask(origInsn); 495 uint64_t pcrelEncoding = 496 (uint64_t)pcrelInsn | (encoding & (uint64_t)origInsnMask); 497 498 // If the mask requires moving bit 28 to bit 5, do that now. 499 if (origInsnMask == LegacyToPrefixMask::ST_STX28_TO5) 500 pcrelEncoding |= (encoding & 0x8) << 23; 501 return pcrelEncoding; 502 } 503 504 static bool isInstructionUpdateForm(uint32_t encoding) { 505 switch (getPrimaryOpCode(encoding)) { 506 default: 507 return false; 508 case LBZU: 509 case LHAU: 510 case LHZU: 511 case LWZU: 512 case LFSU: 513 case LFDU: 514 case STBU: 515 case STHU: 516 case STWU: 517 case STFSU: 518 case STFDU: 519 return true; 520 // LWA has the same opcode as LD, and the DS bits is what differentiates 521 // between LD/LDU/LWA 522 case LD: 523 case STD: 524 return (encoding & 3) == 1; 525 } 526 } 527 528 // Compute the total displacement between the prefixed instruction that gets 529 // to the start of the data and the load/store instruction that has the offset 530 // into the data structure. 531 // For example: 532 // paddi 3, 0, 1000, 1 533 // lwz 3, 20(3) 534 // Should add up to 1020 for total displacement. 535 static int64_t getTotalDisp(uint64_t prefixedInsn, uint32_t accessInsn) { 536 int64_t disp34 = llvm::SignExtend64( 537 ((prefixedInsn & 0x3ffff00000000) >> 16) | (prefixedInsn & 0xffff), 34); 538 int32_t disp16 = llvm::SignExtend32(accessInsn & 0xffff, 16); 539 // For DS and DQ form instructions, we need to mask out the XO bits. 540 if (isDQFormInstruction(accessInsn)) 541 disp16 &= ~0xf; 542 else if (isDSFormInstruction(getPPCLegacyInsn(accessInsn))) 543 disp16 &= ~0x3; 544 return disp34 + disp16; 545 } 546 547 // There are a number of places when we either want to read or write an 548 // instruction when handling a half16 relocation type. On big-endian the buffer 549 // pointer is pointing into the middle of the word we want to extract, and on 550 // little-endian it is pointing to the start of the word. These 2 helpers are to 551 // simplify reading and writing in that context. 552 static void writeFromHalf16(uint8_t *loc, uint32_t insn) { 553 write32(config->isLE ? loc : loc - 2, insn); 554 } 555 556 static uint32_t readFromHalf16(const uint8_t *loc) { 557 return read32(config->isLE ? loc : loc - 2); 558 } 559 560 static uint64_t readPrefixedInstruction(const uint8_t *loc) { 561 uint64_t fullInstr = read64(loc); 562 return config->isLE ? (fullInstr << 32 | fullInstr >> 32) : fullInstr; 563 } 564 565 PPC64::PPC64() { 566 copyRel = R_PPC64_COPY; 567 gotRel = R_PPC64_GLOB_DAT; 568 pltRel = R_PPC64_JMP_SLOT; 569 relativeRel = R_PPC64_RELATIVE; 570 iRelativeRel = R_PPC64_IRELATIVE; 571 symbolicRel = R_PPC64_ADDR64; 572 pltHeaderSize = 60; 573 pltEntrySize = 4; 574 ipltEntrySize = 16; // PPC64PltCallStub::size 575 gotHeaderEntriesNum = 1; 576 gotPltHeaderEntriesNum = 2; 577 needsThunks = true; 578 579 tlsModuleIndexRel = R_PPC64_DTPMOD64; 580 tlsOffsetRel = R_PPC64_DTPREL64; 581 582 tlsGotRel = R_PPC64_TPREL64; 583 584 needsMoreStackNonSplit = false; 585 586 // We need 64K pages (at least under glibc/Linux, the loader won't 587 // set different permissions on a finer granularity than that). 588 defaultMaxPageSize = 65536; 589 590 // The PPC64 ELF ABI v1 spec, says: 591 // 592 // It is normally desirable to put segments with different characteristics 593 // in separate 256 Mbyte portions of the address space, to give the 594 // operating system full paging flexibility in the 64-bit address space. 595 // 596 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers 597 // use 0x10000000 as the starting address. 598 defaultImageBase = 0x10000000; 599 600 write32(trapInstr.data(), 0x7fe00008); 601 } 602 603 int PPC64::getTlsGdRelaxSkip(RelType type) const { 604 // A __tls_get_addr call instruction is marked with 2 relocations: 605 // 606 // R_PPC64_TLSGD / R_PPC64_TLSLD: marker relocation 607 // R_PPC64_REL24: __tls_get_addr 608 // 609 // After the relaxation we no longer call __tls_get_addr and should skip both 610 // relocations to not create a false dependence on __tls_get_addr being 611 // defined. 612 if (type == R_PPC64_TLSGD || type == R_PPC64_TLSLD) 613 return 2; 614 return 1; 615 } 616 617 static uint32_t getEFlags(InputFile *file) { 618 if (file->ekind == ELF64BEKind) 619 return cast<ObjFile<ELF64BE>>(file)->getObj().getHeader().e_flags; 620 return cast<ObjFile<ELF64LE>>(file)->getObj().getHeader().e_flags; 621 } 622 623 // This file implements v2 ABI. This function makes sure that all 624 // object files have v2 or an unspecified version as an ABI version. 625 uint32_t PPC64::calcEFlags() const { 626 for (InputFile *f : ctx.objectFiles) { 627 uint32_t flag = getEFlags(f); 628 if (flag == 1) 629 error(toString(f) + ": ABI version 1 is not supported"); 630 else if (flag > 2) 631 error(toString(f) + ": unrecognized e_flags: " + Twine(flag)); 632 } 633 return 2; 634 } 635 636 void PPC64::relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) const { 637 switch (rel.type) { 638 case R_PPC64_TOC16_HA: 639 // Convert "addis reg, 2, .LC0@toc@h" to "addis reg, 2, var@toc@h" or "nop". 640 relocate(loc, rel, val); 641 break; 642 case R_PPC64_TOC16_LO_DS: { 643 // Convert "ld reg, .LC0@toc@l(reg)" to "addi reg, reg, var@toc@l" or 644 // "addi reg, 2, var@toc". 645 uint32_t insn = readFromHalf16(loc); 646 if (getPrimaryOpCode(insn) != LD) 647 error("expected a 'ld' for got-indirect to toc-relative relaxing"); 648 writeFromHalf16(loc, (insn & 0x03ffffff) | 0x38000000); 649 relocateNoSym(loc, R_PPC64_TOC16_LO, val); 650 break; 651 } 652 case R_PPC64_GOT_PCREL34: { 653 // Clear the first 8 bits of the prefix and the first 6 bits of the 654 // instruction (the primary opcode). 655 uint64_t insn = readPrefixedInstruction(loc); 656 if ((insn & 0xfc000000) != 0xe4000000) 657 error("expected a 'pld' for got-indirect to pc-relative relaxing"); 658 insn &= ~0xff000000fc000000; 659 660 // Replace the cleared bits with the values for PADDI (0x600000038000000); 661 insn |= 0x600000038000000; 662 writePrefixedInstruction(loc, insn); 663 relocate(loc, rel, val); 664 break; 665 } 666 case R_PPC64_PCREL_OPT: { 667 // We can only relax this if the R_PPC64_GOT_PCREL34 at this offset can 668 // be relaxed. The eligibility for the relaxation needs to be determined 669 // on that relocation since this one does not relocate a symbol. 670 uint64_t insn = readPrefixedInstruction(loc); 671 uint32_t accessInsn = read32(loc + rel.addend); 672 uint64_t pcRelInsn = getPCRelativeForm(accessInsn); 673 674 // This error is not necessary for correctness but is emitted for now 675 // to ensure we don't miss these opportunities in real code. It can be 676 // removed at a later date. 677 if (pcRelInsn == UINT64_C(-1)) { 678 errorOrWarn( 679 "unrecognized instruction for R_PPC64_PCREL_OPT relaxation: 0x" + 680 Twine::utohexstr(accessInsn)); 681 break; 682 } 683 684 int64_t totalDisp = getTotalDisp(insn, accessInsn); 685 if (!isInt<34>(totalDisp)) 686 break; // Displacement doesn't fit. 687 // Convert the PADDI to the prefixed version of accessInsn and convert 688 // accessInsn to a nop. 689 writePrefixedInstruction(loc, pcRelInsn | 690 ((totalDisp & 0x3ffff0000) << 16) | 691 (totalDisp & 0xffff)); 692 write32(loc + rel.addend, NOP); // nop accessInsn. 693 break; 694 } 695 default: 696 llvm_unreachable("unexpected relocation type"); 697 } 698 } 699 700 void PPC64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 701 uint64_t val) const { 702 // Reference: 3.7.4.2 of the 64-bit ELF V2 abi supplement. 703 // The general dynamic code sequence for a global `x` will look like: 704 // Instruction Relocation Symbol 705 // addis r3, r2, x@got@tlsgd@ha R_PPC64_GOT_TLSGD16_HA x 706 // addi r3, r3, x@got@tlsgd@l R_PPC64_GOT_TLSGD16_LO x 707 // bl __tls_get_addr(x@tlsgd) R_PPC64_TLSGD x 708 // R_PPC64_REL24 __tls_get_addr 709 // nop None None 710 711 // Relaxing to local exec entails converting: 712 // addis r3, r2, x@got@tlsgd@ha into nop 713 // addi r3, r3, x@got@tlsgd@l into addis r3, r13, x@tprel@ha 714 // bl __tls_get_addr(x@tlsgd) into nop 715 // nop into addi r3, r3, x@tprel@l 716 717 switch (rel.type) { 718 case R_PPC64_GOT_TLSGD16_HA: 719 writeFromHalf16(loc, NOP); 720 break; 721 case R_PPC64_GOT_TLSGD16: 722 case R_PPC64_GOT_TLSGD16_LO: 723 writeFromHalf16(loc, 0x3c6d0000); // addis r3, r13 724 relocateNoSym(loc, R_PPC64_TPREL16_HA, val); 725 break; 726 case R_PPC64_GOT_TLSGD_PCREL34: 727 // Relax from paddi r3, 0, x@got@tlsgd@pcrel, 1 to 728 // paddi r3, r13, x@tprel, 0 729 writePrefixedInstruction(loc, 0x06000000386d0000); 730 relocateNoSym(loc, R_PPC64_TPREL34, val); 731 break; 732 case R_PPC64_TLSGD: { 733 // PC Relative Relaxation: 734 // Relax from bl __tls_get_addr@notoc(x@tlsgd) to 735 // nop 736 // TOC Relaxation: 737 // Relax from bl __tls_get_addr(x@tlsgd) 738 // nop 739 // to 740 // nop 741 // addi r3, r3, x@tprel@l 742 const uintptr_t locAsInt = reinterpret_cast<uintptr_t>(loc); 743 if (locAsInt % 4 == 0) { 744 write32(loc, NOP); // nop 745 write32(loc + 4, 0x38630000); // addi r3, r3 746 // Since we are relocating a half16 type relocation and Loc + 4 points to 747 // the start of an instruction we need to advance the buffer by an extra 748 // 2 bytes on BE. 749 relocateNoSym(loc + 4 + (config->ekind == ELF64BEKind ? 2 : 0), 750 R_PPC64_TPREL16_LO, val); 751 } else if (locAsInt % 4 == 1) { 752 write32(loc - 1, NOP); 753 } else { 754 errorOrWarn("R_PPC64_TLSGD has unexpected byte alignment"); 755 } 756 break; 757 } 758 default: 759 llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 760 } 761 } 762 763 void PPC64::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, 764 uint64_t val) const { 765 // Reference: 3.7.4.3 of the 64-bit ELF V2 abi supplement. 766 // The local dynamic code sequence for a global `x` will look like: 767 // Instruction Relocation Symbol 768 // addis r3, r2, x@got@tlsld@ha R_PPC64_GOT_TLSLD16_HA x 769 // addi r3, r3, x@got@tlsld@l R_PPC64_GOT_TLSLD16_LO x 770 // bl __tls_get_addr(x@tlsgd) R_PPC64_TLSLD x 771 // R_PPC64_REL24 __tls_get_addr 772 // nop None None 773 774 // Relaxing to local exec entails converting: 775 // addis r3, r2, x@got@tlsld@ha into nop 776 // addi r3, r3, x@got@tlsld@l into addis r3, r13, 0 777 // bl __tls_get_addr(x@tlsgd) into nop 778 // nop into addi r3, r3, 4096 779 780 switch (rel.type) { 781 case R_PPC64_GOT_TLSLD16_HA: 782 writeFromHalf16(loc, NOP); 783 break; 784 case R_PPC64_GOT_TLSLD16_LO: 785 writeFromHalf16(loc, 0x3c6d0000); // addis r3, r13, 0 786 break; 787 case R_PPC64_GOT_TLSLD_PCREL34: 788 // Relax from paddi r3, 0, x1@got@tlsld@pcrel, 1 to 789 // paddi r3, r13, 0x1000, 0 790 writePrefixedInstruction(loc, 0x06000000386d1000); 791 break; 792 case R_PPC64_TLSLD: { 793 // PC Relative Relaxation: 794 // Relax from bl __tls_get_addr@notoc(x@tlsld) 795 // to 796 // nop 797 // TOC Relaxation: 798 // Relax from bl __tls_get_addr(x@tlsld) 799 // nop 800 // to 801 // nop 802 // addi r3, r3, 4096 803 const uintptr_t locAsInt = reinterpret_cast<uintptr_t>(loc); 804 if (locAsInt % 4 == 0) { 805 write32(loc, NOP); 806 write32(loc + 4, 0x38631000); // addi r3, r3, 4096 807 } else if (locAsInt % 4 == 1) { 808 write32(loc - 1, NOP); 809 } else { 810 errorOrWarn("R_PPC64_TLSLD has unexpected byte alignment"); 811 } 812 break; 813 } 814 case R_PPC64_DTPREL16: 815 case R_PPC64_DTPREL16_HA: 816 case R_PPC64_DTPREL16_HI: 817 case R_PPC64_DTPREL16_DS: 818 case R_PPC64_DTPREL16_LO: 819 case R_PPC64_DTPREL16_LO_DS: 820 case R_PPC64_DTPREL34: 821 relocate(loc, rel, val); 822 break; 823 default: 824 llvm_unreachable("unsupported relocation for TLS LD to LE relaxation"); 825 } 826 } 827 828 unsigned elf::getPPCDFormOp(unsigned secondaryOp) { 829 switch (secondaryOp) { 830 case LBZX: 831 return LBZ; 832 case LHZX: 833 return LHZ; 834 case LWZX: 835 return LWZ; 836 case LDX: 837 return LD; 838 case STBX: 839 return STB; 840 case STHX: 841 return STH; 842 case STWX: 843 return STW; 844 case STDX: 845 return STD; 846 case ADD: 847 return ADDI; 848 default: 849 return 0; 850 } 851 } 852 853 void PPC64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 854 uint64_t val) const { 855 // The initial exec code sequence for a global `x` will look like: 856 // Instruction Relocation Symbol 857 // addis r9, r2, x@got@tprel@ha R_PPC64_GOT_TPREL16_HA x 858 // ld r9, x@got@tprel@l(r9) R_PPC64_GOT_TPREL16_LO_DS x 859 // add r9, r9, x@tls R_PPC64_TLS x 860 861 // Relaxing to local exec entails converting: 862 // addis r9, r2, x@got@tprel@ha into nop 863 // ld r9, x@got@tprel@l(r9) into addis r9, r13, x@tprel@ha 864 // add r9, r9, x@tls into addi r9, r9, x@tprel@l 865 866 // x@tls R_PPC64_TLS is a relocation which does not compute anything, 867 // it is replaced with r13 (thread pointer). 868 869 // The add instruction in the initial exec sequence has multiple variations 870 // that need to be handled. If we are building an address it will use an add 871 // instruction, if we are accessing memory it will use any of the X-form 872 // indexed load or store instructions. 873 874 unsigned offset = (config->ekind == ELF64BEKind) ? 2 : 0; 875 switch (rel.type) { 876 case R_PPC64_GOT_TPREL16_HA: 877 write32(loc - offset, NOP); 878 break; 879 case R_PPC64_GOT_TPREL16_LO_DS: 880 case R_PPC64_GOT_TPREL16_DS: { 881 uint32_t regNo = read32(loc - offset) & 0x03E00000; // bits 6-10 882 write32(loc - offset, 0x3C0D0000 | regNo); // addis RegNo, r13 883 relocateNoSym(loc, R_PPC64_TPREL16_HA, val); 884 break; 885 } 886 case R_PPC64_GOT_TPREL_PCREL34: { 887 const uint64_t pldRT = readPrefixedInstruction(loc) & 0x0000000003e00000; 888 // paddi RT(from pld), r13, symbol@tprel, 0 889 writePrefixedInstruction(loc, 0x06000000380d0000 | pldRT); 890 relocateNoSym(loc, R_PPC64_TPREL34, val); 891 break; 892 } 893 case R_PPC64_TLS: { 894 const uintptr_t locAsInt = reinterpret_cast<uintptr_t>(loc); 895 if (locAsInt % 4 == 0) { 896 uint32_t primaryOp = getPrimaryOpCode(read32(loc)); 897 if (primaryOp != 31) 898 error("unrecognized instruction for IE to LE R_PPC64_TLS"); 899 uint32_t secondaryOp = (read32(loc) & 0x000007FE) >> 1; // bits 21-30 900 uint32_t dFormOp = getPPCDFormOp(secondaryOp); 901 if (dFormOp == 0) 902 error("unrecognized instruction for IE to LE R_PPC64_TLS"); 903 write32(loc, ((dFormOp << 26) | (read32(loc) & 0x03FFFFFF))); 904 relocateNoSym(loc + offset, R_PPC64_TPREL16_LO, val); 905 } else if (locAsInt % 4 == 1) { 906 // If the offset is not 4 byte aligned then we have a PCRel type reloc. 907 // This version of the relocation is offset by one byte from the 908 // instruction it references. 909 uint32_t tlsInstr = read32(loc - 1); 910 uint32_t primaryOp = getPrimaryOpCode(tlsInstr); 911 if (primaryOp != 31) 912 errorOrWarn("unrecognized instruction for IE to LE R_PPC64_TLS"); 913 uint32_t secondaryOp = (tlsInstr & 0x000007FE) >> 1; // bits 21-30 914 // The add is a special case and should be turned into a nop. The paddi 915 // that comes before it will already have computed the address of the 916 // symbol. 917 if (secondaryOp == 266) { 918 // Check if the add uses the same result register as the input register. 919 uint32_t rt = (tlsInstr & 0x03E00000) >> 21; // bits 6-10 920 uint32_t ra = (tlsInstr & 0x001F0000) >> 16; // bits 11-15 921 if (ra == rt) { 922 write32(loc - 1, NOP); 923 } else { 924 // mr rt, ra 925 write32(loc - 1, 0x7C000378 | (rt << 16) | (ra << 21) | (ra << 11)); 926 } 927 } else { 928 uint32_t dFormOp = getPPCDFormOp(secondaryOp); 929 if (dFormOp == 0) 930 errorOrWarn("unrecognized instruction for IE to LE R_PPC64_TLS"); 931 write32(loc - 1, ((dFormOp << 26) | (tlsInstr & 0x03FF0000))); 932 } 933 } else { 934 errorOrWarn("R_PPC64_TLS must be either 4 byte aligned or one byte " 935 "offset from 4 byte aligned"); 936 } 937 break; 938 } 939 default: 940 llvm_unreachable("unknown relocation for IE to LE"); 941 break; 942 } 943 } 944 945 RelExpr PPC64::getRelExpr(RelType type, const Symbol &s, 946 const uint8_t *loc) const { 947 switch (type) { 948 case R_PPC64_NONE: 949 return R_NONE; 950 case R_PPC64_ADDR16: 951 case R_PPC64_ADDR16_DS: 952 case R_PPC64_ADDR16_HA: 953 case R_PPC64_ADDR16_HI: 954 case R_PPC64_ADDR16_HIGH: 955 case R_PPC64_ADDR16_HIGHER: 956 case R_PPC64_ADDR16_HIGHERA: 957 case R_PPC64_ADDR16_HIGHEST: 958 case R_PPC64_ADDR16_HIGHESTA: 959 case R_PPC64_ADDR16_LO: 960 case R_PPC64_ADDR16_LO_DS: 961 case R_PPC64_ADDR32: 962 case R_PPC64_ADDR64: 963 return R_ABS; 964 case R_PPC64_GOT16: 965 case R_PPC64_GOT16_DS: 966 case R_PPC64_GOT16_HA: 967 case R_PPC64_GOT16_HI: 968 case R_PPC64_GOT16_LO: 969 case R_PPC64_GOT16_LO_DS: 970 return R_GOT_OFF; 971 case R_PPC64_TOC16: 972 case R_PPC64_TOC16_DS: 973 case R_PPC64_TOC16_HI: 974 case R_PPC64_TOC16_LO: 975 return R_GOTREL; 976 case R_PPC64_GOT_PCREL34: 977 case R_PPC64_GOT_TPREL_PCREL34: 978 case R_PPC64_PCREL_OPT: 979 return R_GOT_PC; 980 case R_PPC64_TOC16_HA: 981 case R_PPC64_TOC16_LO_DS: 982 return config->tocOptimize ? R_PPC64_RELAX_TOC : R_GOTREL; 983 case R_PPC64_TOC: 984 return R_PPC64_TOCBASE; 985 case R_PPC64_REL14: 986 case R_PPC64_REL24: 987 return R_PPC64_CALL_PLT; 988 case R_PPC64_REL24_NOTOC: 989 return R_PLT_PC; 990 case R_PPC64_REL16_LO: 991 case R_PPC64_REL16_HA: 992 case R_PPC64_REL16_HI: 993 case R_PPC64_REL32: 994 case R_PPC64_REL64: 995 case R_PPC64_PCREL34: 996 return R_PC; 997 case R_PPC64_GOT_TLSGD16: 998 case R_PPC64_GOT_TLSGD16_HA: 999 case R_PPC64_GOT_TLSGD16_HI: 1000 case R_PPC64_GOT_TLSGD16_LO: 1001 return R_TLSGD_GOT; 1002 case R_PPC64_GOT_TLSGD_PCREL34: 1003 return R_TLSGD_PC; 1004 case R_PPC64_GOT_TLSLD16: 1005 case R_PPC64_GOT_TLSLD16_HA: 1006 case R_PPC64_GOT_TLSLD16_HI: 1007 case R_PPC64_GOT_TLSLD16_LO: 1008 return R_TLSLD_GOT; 1009 case R_PPC64_GOT_TLSLD_PCREL34: 1010 return R_TLSLD_PC; 1011 case R_PPC64_GOT_TPREL16_HA: 1012 case R_PPC64_GOT_TPREL16_LO_DS: 1013 case R_PPC64_GOT_TPREL16_DS: 1014 case R_PPC64_GOT_TPREL16_HI: 1015 return R_GOT_OFF; 1016 case R_PPC64_GOT_DTPREL16_HA: 1017 case R_PPC64_GOT_DTPREL16_LO_DS: 1018 case R_PPC64_GOT_DTPREL16_DS: 1019 case R_PPC64_GOT_DTPREL16_HI: 1020 return R_TLSLD_GOT_OFF; 1021 case R_PPC64_TPREL16: 1022 case R_PPC64_TPREL16_HA: 1023 case R_PPC64_TPREL16_LO: 1024 case R_PPC64_TPREL16_HI: 1025 case R_PPC64_TPREL16_DS: 1026 case R_PPC64_TPREL16_LO_DS: 1027 case R_PPC64_TPREL16_HIGHER: 1028 case R_PPC64_TPREL16_HIGHERA: 1029 case R_PPC64_TPREL16_HIGHEST: 1030 case R_PPC64_TPREL16_HIGHESTA: 1031 case R_PPC64_TPREL34: 1032 return R_TPREL; 1033 case R_PPC64_DTPREL16: 1034 case R_PPC64_DTPREL16_DS: 1035 case R_PPC64_DTPREL16_HA: 1036 case R_PPC64_DTPREL16_HI: 1037 case R_PPC64_DTPREL16_HIGHER: 1038 case R_PPC64_DTPREL16_HIGHERA: 1039 case R_PPC64_DTPREL16_HIGHEST: 1040 case R_PPC64_DTPREL16_HIGHESTA: 1041 case R_PPC64_DTPREL16_LO: 1042 case R_PPC64_DTPREL16_LO_DS: 1043 case R_PPC64_DTPREL64: 1044 case R_PPC64_DTPREL34: 1045 return R_DTPREL; 1046 case R_PPC64_TLSGD: 1047 return R_TLSDESC_CALL; 1048 case R_PPC64_TLSLD: 1049 return R_TLSLD_HINT; 1050 case R_PPC64_TLS: 1051 return R_TLSIE_HINT; 1052 default: 1053 error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 1054 ") against symbol " + toString(s)); 1055 return R_NONE; 1056 } 1057 } 1058 1059 RelType PPC64::getDynRel(RelType type) const { 1060 if (type == R_PPC64_ADDR64 || type == R_PPC64_TOC) 1061 return R_PPC64_ADDR64; 1062 return R_PPC64_NONE; 1063 } 1064 1065 int64_t PPC64::getImplicitAddend(const uint8_t *buf, RelType type) const { 1066 switch (type) { 1067 case R_PPC64_NONE: 1068 case R_PPC64_GLOB_DAT: 1069 case R_PPC64_JMP_SLOT: 1070 return 0; 1071 case R_PPC64_REL32: 1072 return SignExtend64<32>(read32(buf)); 1073 case R_PPC64_ADDR64: 1074 case R_PPC64_REL64: 1075 case R_PPC64_RELATIVE: 1076 case R_PPC64_IRELATIVE: 1077 case R_PPC64_DTPMOD64: 1078 case R_PPC64_DTPREL64: 1079 case R_PPC64_TPREL64: 1080 return read64(buf); 1081 default: 1082 internalLinkerError(getErrorLocation(buf), 1083 "cannot read addend for relocation " + toString(type)); 1084 return 0; 1085 } 1086 } 1087 1088 void PPC64::writeGotHeader(uint8_t *buf) const { 1089 write64(buf, getPPC64TocBase()); 1090 } 1091 1092 void PPC64::writePltHeader(uint8_t *buf) const { 1093 // The generic resolver stub goes first. 1094 write32(buf + 0, 0x7c0802a6); // mflr r0 1095 write32(buf + 4, 0x429f0005); // bcl 20,4*cr7+so,8 <_glink+0x8> 1096 write32(buf + 8, 0x7d6802a6); // mflr r11 1097 write32(buf + 12, 0x7c0803a6); // mtlr r0 1098 write32(buf + 16, 0x7d8b6050); // subf r12, r11, r12 1099 write32(buf + 20, 0x380cffcc); // subi r0,r12,52 1100 write32(buf + 24, 0x7800f082); // srdi r0,r0,62,2 1101 write32(buf + 28, 0xe98b002c); // ld r12,44(r11) 1102 write32(buf + 32, 0x7d6c5a14); // add r11,r12,r11 1103 write32(buf + 36, 0xe98b0000); // ld r12,0(r11) 1104 write32(buf + 40, 0xe96b0008); // ld r11,8(r11) 1105 write32(buf + 44, 0x7d8903a6); // mtctr r12 1106 write32(buf + 48, 0x4e800420); // bctr 1107 1108 // The 'bcl' instruction will set the link register to the address of the 1109 // following instruction ('mflr r11'). Here we store the offset from that 1110 // instruction to the first entry in the GotPlt section. 1111 int64_t gotPltOffset = in.gotPlt->getVA() - (in.plt->getVA() + 8); 1112 write64(buf + 52, gotPltOffset); 1113 } 1114 1115 void PPC64::writePlt(uint8_t *buf, const Symbol &sym, 1116 uint64_t /*pltEntryAddr*/) const { 1117 int32_t offset = pltHeaderSize + sym.getPltIdx() * pltEntrySize; 1118 // bl __glink_PLTresolve 1119 write32(buf, 0x48000000 | ((-offset) & 0x03FFFFFc)); 1120 } 1121 1122 void PPC64::writeIplt(uint8_t *buf, const Symbol &sym, 1123 uint64_t /*pltEntryAddr*/) const { 1124 writePPC64LoadAndBranch(buf, sym.getGotPltVA() - getPPC64TocBase()); 1125 } 1126 1127 static std::pair<RelType, uint64_t> toAddr16Rel(RelType type, uint64_t val) { 1128 // Relocations relative to the toc-base need to be adjusted by the Toc offset. 1129 uint64_t tocBiasedVal = val - ppc64TocOffset; 1130 // Relocations relative to dtv[dtpmod] need to be adjusted by the DTP offset. 1131 uint64_t dtpBiasedVal = val - dynamicThreadPointerOffset; 1132 1133 switch (type) { 1134 // TOC biased relocation. 1135 case R_PPC64_GOT16: 1136 case R_PPC64_GOT_TLSGD16: 1137 case R_PPC64_GOT_TLSLD16: 1138 case R_PPC64_TOC16: 1139 return {R_PPC64_ADDR16, tocBiasedVal}; 1140 case R_PPC64_GOT16_DS: 1141 case R_PPC64_TOC16_DS: 1142 case R_PPC64_GOT_TPREL16_DS: 1143 case R_PPC64_GOT_DTPREL16_DS: 1144 return {R_PPC64_ADDR16_DS, tocBiasedVal}; 1145 case R_PPC64_GOT16_HA: 1146 case R_PPC64_GOT_TLSGD16_HA: 1147 case R_PPC64_GOT_TLSLD16_HA: 1148 case R_PPC64_GOT_TPREL16_HA: 1149 case R_PPC64_GOT_DTPREL16_HA: 1150 case R_PPC64_TOC16_HA: 1151 return {R_PPC64_ADDR16_HA, tocBiasedVal}; 1152 case R_PPC64_GOT16_HI: 1153 case R_PPC64_GOT_TLSGD16_HI: 1154 case R_PPC64_GOT_TLSLD16_HI: 1155 case R_PPC64_GOT_TPREL16_HI: 1156 case R_PPC64_GOT_DTPREL16_HI: 1157 case R_PPC64_TOC16_HI: 1158 return {R_PPC64_ADDR16_HI, tocBiasedVal}; 1159 case R_PPC64_GOT16_LO: 1160 case R_PPC64_GOT_TLSGD16_LO: 1161 case R_PPC64_GOT_TLSLD16_LO: 1162 case R_PPC64_TOC16_LO: 1163 return {R_PPC64_ADDR16_LO, tocBiasedVal}; 1164 case R_PPC64_GOT16_LO_DS: 1165 case R_PPC64_TOC16_LO_DS: 1166 case R_PPC64_GOT_TPREL16_LO_DS: 1167 case R_PPC64_GOT_DTPREL16_LO_DS: 1168 return {R_PPC64_ADDR16_LO_DS, tocBiasedVal}; 1169 1170 // Dynamic Thread pointer biased relocation types. 1171 case R_PPC64_DTPREL16: 1172 return {R_PPC64_ADDR16, dtpBiasedVal}; 1173 case R_PPC64_DTPREL16_DS: 1174 return {R_PPC64_ADDR16_DS, dtpBiasedVal}; 1175 case R_PPC64_DTPREL16_HA: 1176 return {R_PPC64_ADDR16_HA, dtpBiasedVal}; 1177 case R_PPC64_DTPREL16_HI: 1178 return {R_PPC64_ADDR16_HI, dtpBiasedVal}; 1179 case R_PPC64_DTPREL16_HIGHER: 1180 return {R_PPC64_ADDR16_HIGHER, dtpBiasedVal}; 1181 case R_PPC64_DTPREL16_HIGHERA: 1182 return {R_PPC64_ADDR16_HIGHERA, dtpBiasedVal}; 1183 case R_PPC64_DTPREL16_HIGHEST: 1184 return {R_PPC64_ADDR16_HIGHEST, dtpBiasedVal}; 1185 case R_PPC64_DTPREL16_HIGHESTA: 1186 return {R_PPC64_ADDR16_HIGHESTA, dtpBiasedVal}; 1187 case R_PPC64_DTPREL16_LO: 1188 return {R_PPC64_ADDR16_LO, dtpBiasedVal}; 1189 case R_PPC64_DTPREL16_LO_DS: 1190 return {R_PPC64_ADDR16_LO_DS, dtpBiasedVal}; 1191 case R_PPC64_DTPREL64: 1192 return {R_PPC64_ADDR64, dtpBiasedVal}; 1193 1194 default: 1195 return {type, val}; 1196 } 1197 } 1198 1199 static bool isTocOptType(RelType type) { 1200 switch (type) { 1201 case R_PPC64_GOT16_HA: 1202 case R_PPC64_GOT16_LO_DS: 1203 case R_PPC64_TOC16_HA: 1204 case R_PPC64_TOC16_LO_DS: 1205 case R_PPC64_TOC16_LO: 1206 return true; 1207 default: 1208 return false; 1209 } 1210 } 1211 1212 void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 1213 RelType type = rel.type; 1214 bool shouldTocOptimize = isTocOptType(type); 1215 // For dynamic thread pointer relative, toc-relative, and got-indirect 1216 // relocations, proceed in terms of the corresponding ADDR16 relocation type. 1217 std::tie(type, val) = toAddr16Rel(type, val); 1218 1219 switch (type) { 1220 case R_PPC64_ADDR14: { 1221 checkAlignment(loc, val, 4, rel); 1222 // Preserve the AA/LK bits in the branch instruction 1223 uint8_t aalk = loc[3]; 1224 write16(loc + 2, (aalk & 3) | (val & 0xfffc)); 1225 break; 1226 } 1227 case R_PPC64_ADDR16: 1228 checkIntUInt(loc, val, 16, rel); 1229 write16(loc, val); 1230 break; 1231 case R_PPC64_ADDR32: 1232 checkIntUInt(loc, val, 32, rel); 1233 write32(loc, val); 1234 break; 1235 case R_PPC64_ADDR16_DS: 1236 case R_PPC64_TPREL16_DS: { 1237 checkInt(loc, val, 16, rel); 1238 // DQ-form instructions use bits 28-31 as part of the instruction encoding 1239 // DS-form instructions only use bits 30-31. 1240 uint16_t mask = isDQFormInstruction(readFromHalf16(loc)) ? 0xf : 0x3; 1241 checkAlignment(loc, lo(val), mask + 1, rel); 1242 write16(loc, (read16(loc) & mask) | lo(val)); 1243 } break; 1244 case R_PPC64_ADDR16_HA: 1245 case R_PPC64_REL16_HA: 1246 case R_PPC64_TPREL16_HA: 1247 if (config->tocOptimize && shouldTocOptimize && ha(val) == 0) 1248 writeFromHalf16(loc, NOP); 1249 else { 1250 checkInt(loc, val + 0x8000, 32, rel); 1251 write16(loc, ha(val)); 1252 } 1253 break; 1254 case R_PPC64_ADDR16_HI: 1255 case R_PPC64_REL16_HI: 1256 case R_PPC64_TPREL16_HI: 1257 checkInt(loc, val, 32, rel); 1258 write16(loc, hi(val)); 1259 break; 1260 case R_PPC64_ADDR16_HIGH: 1261 write16(loc, hi(val)); 1262 break; 1263 case R_PPC64_ADDR16_HIGHER: 1264 case R_PPC64_TPREL16_HIGHER: 1265 write16(loc, higher(val)); 1266 break; 1267 case R_PPC64_ADDR16_HIGHERA: 1268 case R_PPC64_TPREL16_HIGHERA: 1269 write16(loc, highera(val)); 1270 break; 1271 case R_PPC64_ADDR16_HIGHEST: 1272 case R_PPC64_TPREL16_HIGHEST: 1273 write16(loc, highest(val)); 1274 break; 1275 case R_PPC64_ADDR16_HIGHESTA: 1276 case R_PPC64_TPREL16_HIGHESTA: 1277 write16(loc, highesta(val)); 1278 break; 1279 case R_PPC64_ADDR16_LO: 1280 case R_PPC64_REL16_LO: 1281 case R_PPC64_TPREL16_LO: 1282 // When the high-adjusted part of a toc relocation evaluates to 0, it is 1283 // changed into a nop. The lo part then needs to be updated to use the 1284 // toc-pointer register r2, as the base register. 1285 if (config->tocOptimize && shouldTocOptimize && ha(val) == 0) { 1286 uint32_t insn = readFromHalf16(loc); 1287 if (isInstructionUpdateForm(insn)) 1288 error(getErrorLocation(loc) + 1289 "can't toc-optimize an update instruction: 0x" + 1290 utohexstr(insn)); 1291 writeFromHalf16(loc, (insn & 0xffe00000) | 0x00020000 | lo(val)); 1292 } else { 1293 write16(loc, lo(val)); 1294 } 1295 break; 1296 case R_PPC64_ADDR16_LO_DS: 1297 case R_PPC64_TPREL16_LO_DS: { 1298 // DQ-form instructions use bits 28-31 as part of the instruction encoding 1299 // DS-form instructions only use bits 30-31. 1300 uint32_t insn = readFromHalf16(loc); 1301 uint16_t mask = isDQFormInstruction(insn) ? 0xf : 0x3; 1302 checkAlignment(loc, lo(val), mask + 1, rel); 1303 if (config->tocOptimize && shouldTocOptimize && ha(val) == 0) { 1304 // When the high-adjusted part of a toc relocation evaluates to 0, it is 1305 // changed into a nop. The lo part then needs to be updated to use the toc 1306 // pointer register r2, as the base register. 1307 if (isInstructionUpdateForm(insn)) 1308 error(getErrorLocation(loc) + 1309 "Can't toc-optimize an update instruction: 0x" + 1310 Twine::utohexstr(insn)); 1311 insn &= 0xffe00000 | mask; 1312 writeFromHalf16(loc, insn | 0x00020000 | lo(val)); 1313 } else { 1314 write16(loc, (read16(loc) & mask) | lo(val)); 1315 } 1316 } break; 1317 case R_PPC64_TPREL16: 1318 checkInt(loc, val, 16, rel); 1319 write16(loc, val); 1320 break; 1321 case R_PPC64_REL32: 1322 checkInt(loc, val, 32, rel); 1323 write32(loc, val); 1324 break; 1325 case R_PPC64_ADDR64: 1326 case R_PPC64_REL64: 1327 case R_PPC64_TOC: 1328 write64(loc, val); 1329 break; 1330 case R_PPC64_REL14: { 1331 uint32_t mask = 0x0000FFFC; 1332 checkInt(loc, val, 16, rel); 1333 checkAlignment(loc, val, 4, rel); 1334 write32(loc, (read32(loc) & ~mask) | (val & mask)); 1335 break; 1336 } 1337 case R_PPC64_REL24: 1338 case R_PPC64_REL24_NOTOC: { 1339 uint32_t mask = 0x03FFFFFC; 1340 checkInt(loc, val, 26, rel); 1341 checkAlignment(loc, val, 4, rel); 1342 write32(loc, (read32(loc) & ~mask) | (val & mask)); 1343 break; 1344 } 1345 case R_PPC64_DTPREL64: 1346 write64(loc, val - dynamicThreadPointerOffset); 1347 break; 1348 case R_PPC64_DTPREL34: 1349 // The Dynamic Thread Vector actually points 0x8000 bytes past the start 1350 // of the TLS block. Therefore, in the case of R_PPC64_DTPREL34 we first 1351 // need to subtract that value then fallthrough to the general case. 1352 val -= dynamicThreadPointerOffset; 1353 [[fallthrough]]; 1354 case R_PPC64_PCREL34: 1355 case R_PPC64_GOT_PCREL34: 1356 case R_PPC64_GOT_TLSGD_PCREL34: 1357 case R_PPC64_GOT_TLSLD_PCREL34: 1358 case R_PPC64_GOT_TPREL_PCREL34: 1359 case R_PPC64_TPREL34: { 1360 const uint64_t si0Mask = 0x00000003ffff0000; 1361 const uint64_t si1Mask = 0x000000000000ffff; 1362 const uint64_t fullMask = 0x0003ffff0000ffff; 1363 checkInt(loc, val, 34, rel); 1364 1365 uint64_t instr = readPrefixedInstruction(loc) & ~fullMask; 1366 writePrefixedInstruction(loc, instr | ((val & si0Mask) << 16) | 1367 (val & si1Mask)); 1368 break; 1369 } 1370 // If we encounter a PCREL_OPT relocation that we won't optimize. 1371 case R_PPC64_PCREL_OPT: 1372 break; 1373 default: 1374 llvm_unreachable("unknown relocation"); 1375 } 1376 } 1377 1378 bool PPC64::needsThunk(RelExpr expr, RelType type, const InputFile *file, 1379 uint64_t branchAddr, const Symbol &s, int64_t a) const { 1380 if (type != R_PPC64_REL14 && type != R_PPC64_REL24 && 1381 type != R_PPC64_REL24_NOTOC) 1382 return false; 1383 1384 // If a function is in the Plt it needs to be called with a call-stub. 1385 if (s.isInPlt()) 1386 return true; 1387 1388 // This check looks at the st_other bits of the callee with relocation 1389 // R_PPC64_REL14 or R_PPC64_REL24. If the value is 1, then the callee 1390 // clobbers the TOC and we need an R2 save stub. 1391 if (type != R_PPC64_REL24_NOTOC && (s.stOther >> 5) == 1) 1392 return true; 1393 1394 if (type == R_PPC64_REL24_NOTOC && (s.stOther >> 5) > 1) 1395 return true; 1396 1397 // An undefined weak symbol not in a PLT does not need a thunk. If it is 1398 // hidden, its binding has been converted to local, so we just check 1399 // isUndefined() here. A undefined non-weak symbol has been errored. 1400 if (s.isUndefined()) 1401 return false; 1402 1403 // If the offset exceeds the range of the branch type then it will need 1404 // a range-extending thunk. 1405 // See the comment in getRelocTargetVA() about R_PPC64_CALL. 1406 return !inBranchRange(type, branchAddr, 1407 s.getVA(a) + 1408 getPPC64GlobalEntryToLocalEntryOffset(s.stOther)); 1409 } 1410 1411 uint32_t PPC64::getThunkSectionSpacing() const { 1412 // See comment in Arch/ARM.cpp for a more detailed explanation of 1413 // getThunkSectionSpacing(). For PPC64 we pick the constant here based on 1414 // R_PPC64_REL24, which is used by unconditional branch instructions. 1415 // 0x2000000 = (1 << 24-1) * 4 1416 return 0x2000000; 1417 } 1418 1419 bool PPC64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 1420 int64_t offset = dst - src; 1421 if (type == R_PPC64_REL14) 1422 return isInt<16>(offset); 1423 if (type == R_PPC64_REL24 || type == R_PPC64_REL24_NOTOC) 1424 return isInt<26>(offset); 1425 llvm_unreachable("unsupported relocation type used in branch"); 1426 } 1427 1428 RelExpr PPC64::adjustTlsExpr(RelType type, RelExpr expr) const { 1429 if (type != R_PPC64_GOT_TLSGD_PCREL34 && expr == R_RELAX_TLS_GD_TO_IE) 1430 return R_RELAX_TLS_GD_TO_IE_GOT_OFF; 1431 if (expr == R_RELAX_TLS_LD_TO_LE) 1432 return R_RELAX_TLS_LD_TO_LE_ABS; 1433 return expr; 1434 } 1435 1436 RelExpr PPC64::adjustGotPcExpr(RelType type, int64_t addend, 1437 const uint8_t *loc) const { 1438 if ((type == R_PPC64_GOT_PCREL34 || type == R_PPC64_PCREL_OPT) && 1439 config->pcRelOptimize) { 1440 // It only makes sense to optimize pld since paddi means that the address 1441 // of the object in the GOT is required rather than the object itself. 1442 if ((readPrefixedInstruction(loc) & 0xfc000000) == 0xe4000000) 1443 return R_PPC64_RELAX_GOT_PC; 1444 } 1445 return R_GOT_PC; 1446 } 1447 1448 // Reference: 3.7.4.1 of the 64-bit ELF V2 abi supplement. 1449 // The general dynamic code sequence for a global `x` uses 4 instructions. 1450 // Instruction Relocation Symbol 1451 // addis r3, r2, x@got@tlsgd@ha R_PPC64_GOT_TLSGD16_HA x 1452 // addi r3, r3, x@got@tlsgd@l R_PPC64_GOT_TLSGD16_LO x 1453 // bl __tls_get_addr(x@tlsgd) R_PPC64_TLSGD x 1454 // R_PPC64_REL24 __tls_get_addr 1455 // nop None None 1456 // 1457 // Relaxing to initial-exec entails: 1458 // 1) Convert the addis/addi pair that builds the address of the tls_index 1459 // struct for 'x' to an addis/ld pair that loads an offset from a got-entry. 1460 // 2) Convert the call to __tls_get_addr to a nop. 1461 // 3) Convert the nop following the call to an add of the loaded offset to the 1462 // thread pointer. 1463 // Since the nop must directly follow the call, the R_PPC64_TLSGD relocation is 1464 // used as the relaxation hint for both steps 2 and 3. 1465 void PPC64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 1466 uint64_t val) const { 1467 switch (rel.type) { 1468 case R_PPC64_GOT_TLSGD16_HA: 1469 // This is relaxed from addis rT, r2, sym@got@tlsgd@ha to 1470 // addis rT, r2, sym@got@tprel@ha. 1471 relocateNoSym(loc, R_PPC64_GOT_TPREL16_HA, val); 1472 return; 1473 case R_PPC64_GOT_TLSGD16: 1474 case R_PPC64_GOT_TLSGD16_LO: { 1475 // Relax from addi r3, rA, sym@got@tlsgd@l to 1476 // ld r3, sym@got@tprel@l(rA) 1477 uint32_t ra = (readFromHalf16(loc) & (0x1f << 16)); 1478 writeFromHalf16(loc, 0xe8600000 | ra); 1479 relocateNoSym(loc, R_PPC64_GOT_TPREL16_LO_DS, val); 1480 return; 1481 } 1482 case R_PPC64_GOT_TLSGD_PCREL34: { 1483 // Relax from paddi r3, 0, sym@got@tlsgd@pcrel, 1 to 1484 // pld r3, sym@got@tprel@pcrel 1485 writePrefixedInstruction(loc, 0x04100000e4600000); 1486 relocateNoSym(loc, R_PPC64_GOT_TPREL_PCREL34, val); 1487 return; 1488 } 1489 case R_PPC64_TLSGD: { 1490 // PC Relative Relaxation: 1491 // Relax from bl __tls_get_addr@notoc(x@tlsgd) to 1492 // nop 1493 // TOC Relaxation: 1494 // Relax from bl __tls_get_addr(x@tlsgd) 1495 // nop 1496 // to 1497 // nop 1498 // add r3, r3, r13 1499 const uintptr_t locAsInt = reinterpret_cast<uintptr_t>(loc); 1500 if (locAsInt % 4 == 0) { 1501 write32(loc, NOP); // bl __tls_get_addr(sym@tlsgd) --> nop 1502 write32(loc + 4, 0x7c636A14); // nop --> add r3, r3, r13 1503 } else if (locAsInt % 4 == 1) { 1504 // bl __tls_get_addr(sym@tlsgd) --> add r3, r3, r13 1505 write32(loc - 1, 0x7c636a14); 1506 } else { 1507 errorOrWarn("R_PPC64_TLSGD has unexpected byte alignment"); 1508 } 1509 return; 1510 } 1511 default: 1512 llvm_unreachable("unsupported relocation for TLS GD to IE relaxation"); 1513 } 1514 } 1515 1516 void PPC64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const { 1517 uint64_t secAddr = sec.getOutputSection()->addr; 1518 if (auto *s = dyn_cast<InputSection>(&sec)) 1519 secAddr += s->outSecOff; 1520 uint64_t lastPPCRelaxedRelocOff = -1; 1521 for (const Relocation &rel : sec.relocs()) { 1522 uint8_t *loc = buf + rel.offset; 1523 const uint64_t val = 1524 sec.getRelocTargetVA(sec.file, rel.type, rel.addend, 1525 secAddr + rel.offset, *rel.sym, rel.expr); 1526 switch (rel.expr) { 1527 case R_PPC64_RELAX_GOT_PC: { 1528 // The R_PPC64_PCREL_OPT relocation must appear immediately after 1529 // R_PPC64_GOT_PCREL34 in the relocations table at the same offset. 1530 // We can only relax R_PPC64_PCREL_OPT if we have also relaxed 1531 // the associated R_PPC64_GOT_PCREL34 since only the latter has an 1532 // associated symbol. So save the offset when relaxing R_PPC64_GOT_PCREL34 1533 // and only relax the other if the saved offset matches. 1534 if (rel.type == R_PPC64_GOT_PCREL34) 1535 lastPPCRelaxedRelocOff = rel.offset; 1536 if (rel.type == R_PPC64_PCREL_OPT && rel.offset != lastPPCRelaxedRelocOff) 1537 break; 1538 relaxGot(loc, rel, val); 1539 break; 1540 } 1541 case R_PPC64_RELAX_TOC: 1542 // rel.sym refers to the STT_SECTION symbol associated to the .toc input 1543 // section. If an R_PPC64_TOC16_LO (.toc + addend) references the TOC 1544 // entry, there may be R_PPC64_TOC16_HA not paired with 1545 // R_PPC64_TOC16_LO_DS. Don't relax. This loses some relaxation 1546 // opportunities but is safe. 1547 if (ppc64noTocRelax.count({rel.sym, rel.addend}) || 1548 !tryRelaxPPC64TocIndirection(rel, loc)) 1549 relocate(loc, rel, val); 1550 break; 1551 case R_PPC64_CALL: 1552 // If this is a call to __tls_get_addr, it may be part of a TLS 1553 // sequence that has been relaxed and turned into a nop. In this 1554 // case, we don't want to handle it as a call. 1555 if (read32(loc) == 0x60000000) // nop 1556 break; 1557 1558 // Patch a nop (0x60000000) to a ld. 1559 if (rel.sym->needsTocRestore) { 1560 // gcc/gfortran 5.4, 6.3 and earlier versions do not add nop for 1561 // recursive calls even if the function is preemptible. This is not 1562 // wrong in the common case where the function is not preempted at 1563 // runtime. Just ignore. 1564 if ((rel.offset + 8 > sec.content().size() || 1565 read32(loc + 4) != 0x60000000) && 1566 rel.sym->file != sec.file) { 1567 // Use substr(6) to remove the "__plt_" prefix. 1568 errorOrWarn(getErrorLocation(loc) + "call to " + 1569 lld::toString(*rel.sym).substr(6) + 1570 " lacks nop, can't restore toc"); 1571 break; 1572 } 1573 write32(loc + 4, 0xe8410018); // ld %r2, 24(%r1) 1574 } 1575 relocate(loc, rel, val); 1576 break; 1577 case R_RELAX_TLS_GD_TO_IE: 1578 case R_RELAX_TLS_GD_TO_IE_GOT_OFF: 1579 relaxTlsGdToIe(loc, rel, val); 1580 break; 1581 case R_RELAX_TLS_GD_TO_LE: 1582 relaxTlsGdToLe(loc, rel, val); 1583 break; 1584 case R_RELAX_TLS_LD_TO_LE_ABS: 1585 relaxTlsLdToLe(loc, rel, val); 1586 break; 1587 case R_RELAX_TLS_IE_TO_LE: 1588 relaxTlsIeToLe(loc, rel, val); 1589 break; 1590 default: 1591 relocate(loc, rel, val); 1592 break; 1593 } 1594 } 1595 } 1596 1597 // The prologue for a split-stack function is expected to look roughly 1598 // like this: 1599 // .Lglobal_entry_point: 1600 // # TOC pointer initialization. 1601 // ... 1602 // .Llocal_entry_point: 1603 // # load the __private_ss member of the threads tcbhead. 1604 // ld r0,-0x7000-64(r13) 1605 // # subtract the functions stack size from the stack pointer. 1606 // addis r12, r1, ha(-stack-frame size) 1607 // addi r12, r12, l(-stack-frame size) 1608 // # compare needed to actual and branch to allocate_more_stack if more 1609 // # space is needed, otherwise fallthrough to 'normal' function body. 1610 // cmpld cr7,r12,r0 1611 // blt- cr7, .Lallocate_more_stack 1612 // 1613 // -) The allocate_more_stack block might be placed after the split-stack 1614 // prologue and the `blt-` replaced with a `bge+ .Lnormal_func_body` 1615 // instead. 1616 // -) If either the addis or addi is not needed due to the stack size being 1617 // smaller then 32K or a multiple of 64K they will be replaced with a nop, 1618 // but there will always be 2 instructions the linker can overwrite for the 1619 // adjusted stack size. 1620 // 1621 // The linkers job here is to increase the stack size used in the addis/addi 1622 // pair by split-stack-size-adjust. 1623 // addis r12, r1, ha(-stack-frame size - split-stack-adjust-size) 1624 // addi r12, r12, l(-stack-frame size - split-stack-adjust-size) 1625 bool PPC64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end, 1626 uint8_t stOther) const { 1627 // If the caller has a global entry point adjust the buffer past it. The start 1628 // of the split-stack prologue will be at the local entry point. 1629 loc += getPPC64GlobalEntryToLocalEntryOffset(stOther); 1630 1631 // At the very least we expect to see a load of some split-stack data from the 1632 // tcb, and 2 instructions that calculate the ending stack address this 1633 // function will require. If there is not enough room for at least 3 1634 // instructions it can't be a split-stack prologue. 1635 if (loc + 12 >= end) 1636 return false; 1637 1638 // First instruction must be `ld r0, -0x7000-64(r13)` 1639 if (read32(loc) != 0xe80d8fc0) 1640 return false; 1641 1642 int16_t hiImm = 0; 1643 int16_t loImm = 0; 1644 // First instruction can be either an addis if the frame size is larger then 1645 // 32K, or an addi if the size is less then 32K. 1646 int32_t firstInstr = read32(loc + 4); 1647 if (getPrimaryOpCode(firstInstr) == 15) { 1648 hiImm = firstInstr & 0xFFFF; 1649 } else if (getPrimaryOpCode(firstInstr) == 14) { 1650 loImm = firstInstr & 0xFFFF; 1651 } else { 1652 return false; 1653 } 1654 1655 // Second instruction is either an addi or a nop. If the first instruction was 1656 // an addi then LoImm is set and the second instruction must be a nop. 1657 uint32_t secondInstr = read32(loc + 8); 1658 if (!loImm && getPrimaryOpCode(secondInstr) == 14) { 1659 loImm = secondInstr & 0xFFFF; 1660 } else if (secondInstr != NOP) { 1661 return false; 1662 } 1663 1664 // The register operands of the first instruction should be the stack-pointer 1665 // (r1) as the input (RA) and r12 as the output (RT). If the second 1666 // instruction is not a nop, then it should use r12 as both input and output. 1667 auto checkRegOperands = [](uint32_t instr, uint8_t expectedRT, 1668 uint8_t expectedRA) { 1669 return ((instr & 0x3E00000) >> 21 == expectedRT) && 1670 ((instr & 0x1F0000) >> 16 == expectedRA); 1671 }; 1672 if (!checkRegOperands(firstInstr, 12, 1)) 1673 return false; 1674 if (secondInstr != NOP && !checkRegOperands(secondInstr, 12, 12)) 1675 return false; 1676 1677 int32_t stackFrameSize = (hiImm * 65536) + loImm; 1678 // Check that the adjusted size doesn't overflow what we can represent with 2 1679 // instructions. 1680 if (stackFrameSize < config->splitStackAdjustSize + INT32_MIN) { 1681 error(getErrorLocation(loc) + "split-stack prologue adjustment overflows"); 1682 return false; 1683 } 1684 1685 int32_t adjustedStackFrameSize = 1686 stackFrameSize - config->splitStackAdjustSize; 1687 1688 loImm = adjustedStackFrameSize & 0xFFFF; 1689 hiImm = (adjustedStackFrameSize + 0x8000) >> 16; 1690 if (hiImm) { 1691 write32(loc + 4, 0x3D810000 | (uint16_t)hiImm); 1692 // If the low immediate is zero the second instruction will be a nop. 1693 secondInstr = loImm ? 0x398C0000 | (uint16_t)loImm : NOP; 1694 write32(loc + 8, secondInstr); 1695 } else { 1696 // addi r12, r1, imm 1697 write32(loc + 4, (0x39810000) | (uint16_t)loImm); 1698 write32(loc + 8, NOP); 1699 } 1700 1701 return true; 1702 } 1703 1704 TargetInfo *elf::getPPC64TargetInfo() { 1705 static PPC64 target; 1706 return ⌖ 1707 } 1708