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 "Symbols.h" 10 #include "SyntheticSections.h" 11 #include "Target.h" 12 #include "Thunks.h" 13 #include "lld/Common/ErrorHandler.h" 14 #include "llvm/Support/Endian.h" 15 16 using namespace llvm; 17 using namespace llvm::object; 18 using namespace llvm::support::endian; 19 using namespace llvm::ELF; 20 21 namespace lld { 22 namespace elf { 23 24 static uint64_t ppc64TocOffset = 0x8000; 25 static uint64_t dynamicThreadPointerOffset = 0x8000; 26 27 // The instruction encoding of bits 21-30 from the ISA for the Xform and Dform 28 // instructions that can be used as part of the initial exec TLS sequence. 29 enum XFormOpcd { 30 LBZX = 87, 31 LHZX = 279, 32 LWZX = 23, 33 LDX = 21, 34 STBX = 215, 35 STHX = 407, 36 STWX = 151, 37 STDX = 149, 38 ADD = 266, 39 }; 40 41 enum DFormOpcd { 42 LBZ = 34, 43 LBZU = 35, 44 LHZ = 40, 45 LHZU = 41, 46 LHAU = 43, 47 LWZ = 32, 48 LWZU = 33, 49 LFSU = 49, 50 LD = 58, 51 LFDU = 51, 52 STB = 38, 53 STBU = 39, 54 STH = 44, 55 STHU = 45, 56 STW = 36, 57 STWU = 37, 58 STFSU = 53, 59 STFDU = 55, 60 STD = 62, 61 ADDI = 14 62 }; 63 64 uint64_t getPPC64TocBase() { 65 // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The 66 // TOC starts where the first of these sections starts. We always create a 67 // .got when we see a relocation that uses it, so for us the start is always 68 // the .got. 69 uint64_t tocVA = in.got->getVA(); 70 71 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 72 // thus permitting a full 64 Kbytes segment. Note that the glibc startup 73 // code (crt1.o) assumes that you can get from the TOC base to the 74 // start of the .toc section with only a single (signed) 16-bit relocation. 75 return tocVA + ppc64TocOffset; 76 } 77 78 unsigned getPPC64GlobalEntryToLocalEntryOffset(uint8_t stOther) { 79 // The offset is encoded into the 3 most significant bits of the st_other 80 // field, with some special values described in section 3.4.1 of the ABI: 81 // 0 --> Zero offset between the GEP and LEP, and the function does NOT use 82 // the TOC pointer (r2). r2 will hold the same value on returning from 83 // the function as it did on entering the function. 84 // 1 --> Zero offset between the GEP and LEP, and r2 should be treated as a 85 // caller-saved register for all callers. 86 // 2-6 --> The binary logarithm of the offset eg: 87 // 2 --> 2^2 = 4 bytes --> 1 instruction. 88 // 6 --> 2^6 = 64 bytes --> 16 instructions. 89 // 7 --> Reserved. 90 uint8_t gepToLep = (stOther >> 5) & 7; 91 if (gepToLep < 2) 92 return 0; 93 94 // The value encoded in the st_other bits is the 95 // log-base-2(offset). 96 if (gepToLep < 7) 97 return 1 << gepToLep; 98 99 error("reserved value of 7 in the 3 most-significant-bits of st_other"); 100 return 0; 101 } 102 103 bool isPPC64SmallCodeModelTocReloc(RelType type) { 104 // The only small code model relocations that access the .toc section. 105 return type == R_PPC64_TOC16 || type == R_PPC64_TOC16_DS; 106 } 107 108 // Find the R_PPC64_ADDR64 in .rela.toc with matching offset. 109 template <typename ELFT> 110 static std::pair<Defined *, int64_t> 111 getRelaTocSymAndAddend(InputSectionBase *tocSec, uint64_t offset) { 112 if (tocSec->numRelocations == 0) 113 return {}; 114 115 // .rela.toc contains exclusively R_PPC64_ADDR64 relocations sorted by 116 // r_offset: 0, 8, 16, etc. For a given Offset, Offset / 8 gives us the 117 // relocation index in most cases. 118 // 119 // In rare cases a TOC entry may store a constant that doesn't need an 120 // R_PPC64_ADDR64, the corresponding r_offset is therefore missing. Offset / 8 121 // points to a relocation with larger r_offset. Do a linear probe then. 122 // Constants are extremely uncommon in .toc and the extra number of array 123 // accesses can be seen as a small constant. 124 ArrayRef<typename ELFT::Rela> relas = tocSec->template relas<ELFT>(); 125 uint64_t index = std::min<uint64_t>(offset / 8, relas.size() - 1); 126 for (;;) { 127 if (relas[index].r_offset == offset) { 128 Symbol &sym = tocSec->getFile<ELFT>()->getRelocTargetSym(relas[index]); 129 return {dyn_cast<Defined>(&sym), getAddend<ELFT>(relas[index])}; 130 } 131 if (relas[index].r_offset < offset || index == 0) 132 break; 133 --index; 134 } 135 return {}; 136 } 137 138 // When accessing a symbol defined in another translation unit, compilers 139 // reserve a .toc entry, allocate a local label and generate toc-indirect 140 // instructions: 141 // 142 // addis 3, 2, .LC0@toc@ha # R_PPC64_TOC16_HA 143 // ld 3, .LC0@toc@l(3) # R_PPC64_TOC16_LO_DS, load the address from a .toc entry 144 // ld/lwa 3, 0(3) # load the value from the address 145 // 146 // .section .toc,"aw",@progbits 147 // .LC0: .tc var[TC],var 148 // 149 // If var is defined, non-preemptable and addressable with a 32-bit signed 150 // offset from the toc base, the address of var can be computed by adding an 151 // offset to the toc base, saving a load. 152 // 153 // addis 3,2,var@toc@ha # this may be relaxed to a nop, 154 // addi 3,3,var@toc@l # then this becomes addi 3,2,var@toc 155 // ld/lwa 3, 0(3) # load the value from the address 156 // 157 // Returns true if the relaxation is performed. 158 bool tryRelaxPPC64TocIndirection(const Relocation &rel, uint8_t *bufLoc) { 159 assert(config->tocOptimize); 160 if (rel.addend < 0) 161 return false; 162 163 // If the symbol is not the .toc section, this isn't a toc-indirection. 164 Defined *defSym = dyn_cast<Defined>(rel.sym); 165 if (!defSym || !defSym->isSection() || defSym->section->name != ".toc") 166 return false; 167 168 Defined *d; 169 int64_t addend; 170 auto *tocISB = cast<InputSectionBase>(defSym->section); 171 std::tie(d, addend) = 172 config->isLE ? getRelaTocSymAndAddend<ELF64LE>(tocISB, rel.addend) 173 : getRelaTocSymAndAddend<ELF64BE>(tocISB, rel.addend); 174 175 // Only non-preemptable defined symbols can be relaxed. 176 if (!d || d->isPreemptible) 177 return false; 178 179 // R_PPC64_ADDR64 should have created a canonical PLT for the non-preemptable 180 // ifunc and changed its type to STT_FUNC. 181 assert(!d->isGnuIFunc()); 182 183 // Two instructions can materialize a 32-bit signed offset from the toc base. 184 uint64_t tocRelative = d->getVA(addend) - getPPC64TocBase(); 185 if (!isInt<32>(tocRelative)) 186 return false; 187 188 // Add PPC64TocOffset that will be subtracted by PPC64::relocate(). 189 target->relaxGot(bufLoc, rel, tocRelative + ppc64TocOffset); 190 return true; 191 } 192 193 namespace { 194 class PPC64 final : public TargetInfo { 195 public: 196 PPC64(); 197 int getTlsGdRelaxSkip(RelType type) const override; 198 uint32_t calcEFlags() const override; 199 RelExpr getRelExpr(RelType type, const Symbol &s, 200 const uint8_t *loc) const override; 201 RelType getDynRel(RelType type) const override; 202 void writePltHeader(uint8_t *buf) const override; 203 void writePlt(uint8_t *buf, const Symbol &sym, 204 uint64_t pltEntryAddr) const override; 205 void writeIplt(uint8_t *buf, const Symbol &sym, 206 uint64_t pltEntryAddr) const override; 207 void relocate(uint8_t *loc, const Relocation &rel, 208 uint64_t val) const override; 209 void writeGotHeader(uint8_t *buf) const override; 210 bool needsThunk(RelExpr expr, RelType type, const InputFile *file, 211 uint64_t branchAddr, const Symbol &s, 212 int64_t a) const override; 213 uint32_t getThunkSectionSpacing() const override; 214 bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override; 215 RelExpr adjustRelaxExpr(RelType type, const uint8_t *data, 216 RelExpr expr) const override; 217 void relaxGot(uint8_t *loc, const Relocation &rel, 218 uint64_t val) const override; 219 void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 220 uint64_t val) const override; 221 void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 222 uint64_t val) const override; 223 void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, 224 uint64_t val) const override; 225 void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 226 uint64_t val) const override; 227 228 bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end, 229 uint8_t stOther) const override; 230 }; 231 } // namespace 232 233 // Relocation masks following the #lo(value), #hi(value), #ha(value), 234 // #higher(value), #highera(value), #highest(value), and #highesta(value) 235 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi 236 // document. 237 static uint16_t lo(uint64_t v) { return v; } 238 static uint16_t hi(uint64_t v) { return v >> 16; } 239 static uint16_t ha(uint64_t v) { return (v + 0x8000) >> 16; } 240 static uint16_t higher(uint64_t v) { return v >> 32; } 241 static uint16_t highera(uint64_t v) { return (v + 0x8000) >> 32; } 242 static uint16_t highest(uint64_t v) { return v >> 48; } 243 static uint16_t highesta(uint64_t v) { return (v + 0x8000) >> 48; } 244 245 // Extracts the 'PO' field of an instruction encoding. 246 static uint8_t getPrimaryOpCode(uint32_t encoding) { return (encoding >> 26); } 247 248 static bool isDQFormInstruction(uint32_t encoding) { 249 switch (getPrimaryOpCode(encoding)) { 250 default: 251 return false; 252 case 56: 253 // The only instruction with a primary opcode of 56 is `lq`. 254 return true; 255 case 61: 256 // There are both DS and DQ instruction forms with this primary opcode. 257 // Namely `lxv` and `stxv` are the DQ-forms that use it. 258 // The DS 'XO' bits being set to 01 is restricted to DQ form. 259 return (encoding & 3) == 0x1; 260 } 261 } 262 263 static bool isInstructionUpdateForm(uint32_t encoding) { 264 switch (getPrimaryOpCode(encoding)) { 265 default: 266 return false; 267 case LBZU: 268 case LHAU: 269 case LHZU: 270 case LWZU: 271 case LFSU: 272 case LFDU: 273 case STBU: 274 case STHU: 275 case STWU: 276 case STFSU: 277 case STFDU: 278 return true; 279 // LWA has the same opcode as LD, and the DS bits is what differentiates 280 // between LD/LDU/LWA 281 case LD: 282 case STD: 283 return (encoding & 3) == 1; 284 } 285 } 286 287 // There are a number of places when we either want to read or write an 288 // instruction when handling a half16 relocation type. On big-endian the buffer 289 // pointer is pointing into the middle of the word we want to extract, and on 290 // little-endian it is pointing to the start of the word. These 2 helpers are to 291 // simplify reading and writing in that context. 292 static void writeFromHalf16(uint8_t *loc, uint32_t insn) { 293 write32(config->isLE ? loc : loc - 2, insn); 294 } 295 296 static uint32_t readFromHalf16(const uint8_t *loc) { 297 return read32(config->isLE ? loc : loc - 2); 298 } 299 300 PPC64::PPC64() { 301 copyRel = R_PPC64_COPY; 302 gotRel = R_PPC64_GLOB_DAT; 303 noneRel = R_PPC64_NONE; 304 pltRel = R_PPC64_JMP_SLOT; 305 relativeRel = R_PPC64_RELATIVE; 306 iRelativeRel = R_PPC64_IRELATIVE; 307 symbolicRel = R_PPC64_ADDR64; 308 pltHeaderSize = 60; 309 pltEntrySize = 4; 310 ipltEntrySize = 16; // PPC64PltCallStub::size 311 gotBaseSymInGotPlt = false; 312 gotHeaderEntriesNum = 1; 313 gotPltHeaderEntriesNum = 2; 314 needsThunks = true; 315 316 tlsModuleIndexRel = R_PPC64_DTPMOD64; 317 tlsOffsetRel = R_PPC64_DTPREL64; 318 319 tlsGotRel = R_PPC64_TPREL64; 320 321 needsMoreStackNonSplit = false; 322 323 // We need 64K pages (at least under glibc/Linux, the loader won't 324 // set different permissions on a finer granularity than that). 325 defaultMaxPageSize = 65536; 326 327 // The PPC64 ELF ABI v1 spec, says: 328 // 329 // It is normally desirable to put segments with different characteristics 330 // in separate 256 Mbyte portions of the address space, to give the 331 // operating system full paging flexibility in the 64-bit address space. 332 // 333 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers 334 // use 0x10000000 as the starting address. 335 defaultImageBase = 0x10000000; 336 337 write32(trapInstr.data(), 0x7fe00008); 338 } 339 340 int PPC64::getTlsGdRelaxSkip(RelType type) const { 341 // A __tls_get_addr call instruction is marked with 2 relocations: 342 // 343 // R_PPC64_TLSGD / R_PPC64_TLSLD: marker relocation 344 // R_PPC64_REL24: __tls_get_addr 345 // 346 // After the relaxation we no longer call __tls_get_addr and should skip both 347 // relocations to not create a false dependence on __tls_get_addr being 348 // defined. 349 if (type == R_PPC64_TLSGD || type == R_PPC64_TLSLD) 350 return 2; 351 return 1; 352 } 353 354 static uint32_t getEFlags(InputFile *file) { 355 if (config->ekind == ELF64BEKind) 356 return cast<ObjFile<ELF64BE>>(file)->getObj().getHeader()->e_flags; 357 return cast<ObjFile<ELF64LE>>(file)->getObj().getHeader()->e_flags; 358 } 359 360 // This file implements v2 ABI. This function makes sure that all 361 // object files have v2 or an unspecified version as an ABI version. 362 uint32_t PPC64::calcEFlags() const { 363 for (InputFile *f : objectFiles) { 364 uint32_t flag = getEFlags(f); 365 if (flag == 1) 366 error(toString(f) + ": ABI version 1 is not supported"); 367 else if (flag > 2) 368 error(toString(f) + ": unrecognized e_flags: " + Twine(flag)); 369 } 370 return 2; 371 } 372 373 void PPC64::relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) const { 374 switch (rel.type) { 375 case R_PPC64_TOC16_HA: 376 // Convert "addis reg, 2, .LC0@toc@h" to "addis reg, 2, var@toc@h" or "nop". 377 relocate(loc, rel, val); 378 break; 379 case R_PPC64_TOC16_LO_DS: { 380 // Convert "ld reg, .LC0@toc@l(reg)" to "addi reg, reg, var@toc@l" or 381 // "addi reg, 2, var@toc". 382 uint32_t insn = readFromHalf16(loc); 383 if (getPrimaryOpCode(insn) != LD) 384 error("expected a 'ld' for got-indirect to toc-relative relaxing"); 385 writeFromHalf16(loc, (insn & 0x03ffffff) | 0x38000000); 386 relocateNoSym(loc, R_PPC64_TOC16_LO, val); 387 break; 388 } 389 default: 390 llvm_unreachable("unexpected relocation type"); 391 } 392 } 393 394 void PPC64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 395 uint64_t val) const { 396 // Reference: 3.7.4.2 of the 64-bit ELF V2 abi supplement. 397 // The general dynamic code sequence for a global `x` will look like: 398 // Instruction Relocation Symbol 399 // addis r3, r2, x@got@tlsgd@ha R_PPC64_GOT_TLSGD16_HA x 400 // addi r3, r3, x@got@tlsgd@l R_PPC64_GOT_TLSGD16_LO x 401 // bl __tls_get_addr(x@tlsgd) R_PPC64_TLSGD x 402 // R_PPC64_REL24 __tls_get_addr 403 // nop None None 404 405 // Relaxing to local exec entails converting: 406 // addis r3, r2, x@got@tlsgd@ha into nop 407 // addi r3, r3, x@got@tlsgd@l into addis r3, r13, x@tprel@ha 408 // bl __tls_get_addr(x@tlsgd) into nop 409 // nop into addi r3, r3, x@tprel@l 410 411 switch (rel.type) { 412 case R_PPC64_GOT_TLSGD16_HA: 413 writeFromHalf16(loc, 0x60000000); // nop 414 break; 415 case R_PPC64_GOT_TLSGD16: 416 case R_PPC64_GOT_TLSGD16_LO: 417 writeFromHalf16(loc, 0x3c6d0000); // addis r3, r13 418 relocateNoSym(loc, R_PPC64_TPREL16_HA, val); 419 break; 420 case R_PPC64_TLSGD: 421 write32(loc, 0x60000000); // nop 422 write32(loc + 4, 0x38630000); // addi r3, r3 423 // Since we are relocating a half16 type relocation and Loc + 4 points to 424 // the start of an instruction we need to advance the buffer by an extra 425 // 2 bytes on BE. 426 relocateNoSym(loc + 4 + (config->ekind == ELF64BEKind ? 2 : 0), 427 R_PPC64_TPREL16_LO, val); 428 break; 429 default: 430 llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 431 } 432 } 433 434 void PPC64::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, 435 uint64_t val) const { 436 // Reference: 3.7.4.3 of the 64-bit ELF V2 abi supplement. 437 // The local dynamic code sequence for a global `x` will look like: 438 // Instruction Relocation Symbol 439 // addis r3, r2, x@got@tlsld@ha R_PPC64_GOT_TLSLD16_HA x 440 // addi r3, r3, x@got@tlsld@l R_PPC64_GOT_TLSLD16_LO x 441 // bl __tls_get_addr(x@tlsgd) R_PPC64_TLSLD x 442 // R_PPC64_REL24 __tls_get_addr 443 // nop None None 444 445 // Relaxing to local exec entails converting: 446 // addis r3, r2, x@got@tlsld@ha into nop 447 // addi r3, r3, x@got@tlsld@l into addis r3, r13, 0 448 // bl __tls_get_addr(x@tlsgd) into nop 449 // nop into addi r3, r3, 4096 450 451 switch (rel.type) { 452 case R_PPC64_GOT_TLSLD16_HA: 453 writeFromHalf16(loc, 0x60000000); // nop 454 break; 455 case R_PPC64_GOT_TLSLD16_LO: 456 writeFromHalf16(loc, 0x3c6d0000); // addis r3, r13, 0 457 break; 458 case R_PPC64_TLSLD: 459 write32(loc, 0x60000000); // nop 460 write32(loc + 4, 0x38631000); // addi r3, r3, 4096 461 break; 462 case R_PPC64_DTPREL16: 463 case R_PPC64_DTPREL16_HA: 464 case R_PPC64_DTPREL16_HI: 465 case R_PPC64_DTPREL16_DS: 466 case R_PPC64_DTPREL16_LO: 467 case R_PPC64_DTPREL16_LO_DS: 468 relocate(loc, rel, val); 469 break; 470 default: 471 llvm_unreachable("unsupported relocation for TLS LD to LE relaxation"); 472 } 473 } 474 475 unsigned getPPCDFormOp(unsigned secondaryOp) { 476 switch (secondaryOp) { 477 case LBZX: 478 return LBZ; 479 case LHZX: 480 return LHZ; 481 case LWZX: 482 return LWZ; 483 case LDX: 484 return LD; 485 case STBX: 486 return STB; 487 case STHX: 488 return STH; 489 case STWX: 490 return STW; 491 case STDX: 492 return STD; 493 case ADD: 494 return ADDI; 495 default: 496 return 0; 497 } 498 } 499 500 void PPC64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 501 uint64_t val) const { 502 // The initial exec code sequence for a global `x` will look like: 503 // Instruction Relocation Symbol 504 // addis r9, r2, x@got@tprel@ha R_PPC64_GOT_TPREL16_HA x 505 // ld r9, x@got@tprel@l(r9) R_PPC64_GOT_TPREL16_LO_DS x 506 // add r9, r9, x@tls R_PPC64_TLS x 507 508 // Relaxing to local exec entails converting: 509 // addis r9, r2, x@got@tprel@ha into nop 510 // ld r9, x@got@tprel@l(r9) into addis r9, r13, x@tprel@ha 511 // add r9, r9, x@tls into addi r9, r9, x@tprel@l 512 513 // x@tls R_PPC64_TLS is a relocation which does not compute anything, 514 // it is replaced with r13 (thread pointer). 515 516 // The add instruction in the initial exec sequence has multiple variations 517 // that need to be handled. If we are building an address it will use an add 518 // instruction, if we are accessing memory it will use any of the X-form 519 // indexed load or store instructions. 520 521 unsigned offset = (config->ekind == ELF64BEKind) ? 2 : 0; 522 switch (rel.type) { 523 case R_PPC64_GOT_TPREL16_HA: 524 write32(loc - offset, 0x60000000); // nop 525 break; 526 case R_PPC64_GOT_TPREL16_LO_DS: 527 case R_PPC64_GOT_TPREL16_DS: { 528 uint32_t regNo = read32(loc - offset) & 0x03E00000; // bits 6-10 529 write32(loc - offset, 0x3C0D0000 | regNo); // addis RegNo, r13 530 relocateNoSym(loc, R_PPC64_TPREL16_HA, val); 531 break; 532 } 533 case R_PPC64_TLS: { 534 uint32_t primaryOp = getPrimaryOpCode(read32(loc)); 535 if (primaryOp != 31) 536 error("unrecognized instruction for IE to LE R_PPC64_TLS"); 537 uint32_t secondaryOp = (read32(loc) & 0x000007FE) >> 1; // bits 21-30 538 uint32_t dFormOp = getPPCDFormOp(secondaryOp); 539 if (dFormOp == 0) 540 error("unrecognized instruction for IE to LE R_PPC64_TLS"); 541 write32(loc, ((dFormOp << 26) | (read32(loc) & 0x03FFFFFF))); 542 relocateNoSym(loc + offset, R_PPC64_TPREL16_LO, val); 543 break; 544 } 545 default: 546 llvm_unreachable("unknown relocation for IE to LE"); 547 break; 548 } 549 } 550 551 RelExpr PPC64::getRelExpr(RelType type, const Symbol &s, 552 const uint8_t *loc) const { 553 switch (type) { 554 case R_PPC64_NONE: 555 return R_NONE; 556 case R_PPC64_ADDR16: 557 case R_PPC64_ADDR16_DS: 558 case R_PPC64_ADDR16_HA: 559 case R_PPC64_ADDR16_HI: 560 case R_PPC64_ADDR16_HIGHER: 561 case R_PPC64_ADDR16_HIGHERA: 562 case R_PPC64_ADDR16_HIGHEST: 563 case R_PPC64_ADDR16_HIGHESTA: 564 case R_PPC64_ADDR16_LO: 565 case R_PPC64_ADDR16_LO_DS: 566 case R_PPC64_ADDR32: 567 case R_PPC64_ADDR64: 568 return R_ABS; 569 case R_PPC64_GOT16: 570 case R_PPC64_GOT16_DS: 571 case R_PPC64_GOT16_HA: 572 case R_PPC64_GOT16_HI: 573 case R_PPC64_GOT16_LO: 574 case R_PPC64_GOT16_LO_DS: 575 return R_GOT_OFF; 576 case R_PPC64_TOC16: 577 case R_PPC64_TOC16_DS: 578 case R_PPC64_TOC16_HI: 579 case R_PPC64_TOC16_LO: 580 return R_GOTREL; 581 case R_PPC64_TOC16_HA: 582 case R_PPC64_TOC16_LO_DS: 583 return config->tocOptimize ? R_PPC64_RELAX_TOC : R_GOTREL; 584 case R_PPC64_TOC: 585 return R_PPC64_TOCBASE; 586 case R_PPC64_REL14: 587 case R_PPC64_REL24: 588 return R_PPC64_CALL_PLT; 589 case R_PPC64_REL16_LO: 590 case R_PPC64_REL16_HA: 591 case R_PPC64_REL16_HI: 592 case R_PPC64_REL32: 593 case R_PPC64_REL64: 594 return R_PC; 595 case R_PPC64_GOT_TLSGD16: 596 case R_PPC64_GOT_TLSGD16_HA: 597 case R_PPC64_GOT_TLSGD16_HI: 598 case R_PPC64_GOT_TLSGD16_LO: 599 return R_TLSGD_GOT; 600 case R_PPC64_GOT_TLSLD16: 601 case R_PPC64_GOT_TLSLD16_HA: 602 case R_PPC64_GOT_TLSLD16_HI: 603 case R_PPC64_GOT_TLSLD16_LO: 604 return R_TLSLD_GOT; 605 case R_PPC64_GOT_TPREL16_HA: 606 case R_PPC64_GOT_TPREL16_LO_DS: 607 case R_PPC64_GOT_TPREL16_DS: 608 case R_PPC64_GOT_TPREL16_HI: 609 return R_GOT_OFF; 610 case R_PPC64_GOT_DTPREL16_HA: 611 case R_PPC64_GOT_DTPREL16_LO_DS: 612 case R_PPC64_GOT_DTPREL16_DS: 613 case R_PPC64_GOT_DTPREL16_HI: 614 return R_TLSLD_GOT_OFF; 615 case R_PPC64_TPREL16: 616 case R_PPC64_TPREL16_HA: 617 case R_PPC64_TPREL16_LO: 618 case R_PPC64_TPREL16_HI: 619 case R_PPC64_TPREL16_DS: 620 case R_PPC64_TPREL16_LO_DS: 621 case R_PPC64_TPREL16_HIGHER: 622 case R_PPC64_TPREL16_HIGHERA: 623 case R_PPC64_TPREL16_HIGHEST: 624 case R_PPC64_TPREL16_HIGHESTA: 625 return R_TLS; 626 case R_PPC64_DTPREL16: 627 case R_PPC64_DTPREL16_DS: 628 case R_PPC64_DTPREL16_HA: 629 case R_PPC64_DTPREL16_HI: 630 case R_PPC64_DTPREL16_HIGHER: 631 case R_PPC64_DTPREL16_HIGHERA: 632 case R_PPC64_DTPREL16_HIGHEST: 633 case R_PPC64_DTPREL16_HIGHESTA: 634 case R_PPC64_DTPREL16_LO: 635 case R_PPC64_DTPREL16_LO_DS: 636 case R_PPC64_DTPREL64: 637 return R_DTPREL; 638 case R_PPC64_TLSGD: 639 return R_TLSDESC_CALL; 640 case R_PPC64_TLSLD: 641 return R_TLSLD_HINT; 642 case R_PPC64_TLS: 643 return R_TLSIE_HINT; 644 default: 645 error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 646 ") against symbol " + toString(s)); 647 return R_NONE; 648 } 649 } 650 651 RelType PPC64::getDynRel(RelType type) const { 652 if (type == R_PPC64_ADDR64 || type == R_PPC64_TOC) 653 return R_PPC64_ADDR64; 654 return R_PPC64_NONE; 655 } 656 657 void PPC64::writeGotHeader(uint8_t *buf) const { 658 write64(buf, getPPC64TocBase()); 659 } 660 661 void PPC64::writePltHeader(uint8_t *buf) const { 662 // The generic resolver stub goes first. 663 write32(buf + 0, 0x7c0802a6); // mflr r0 664 write32(buf + 4, 0x429f0005); // bcl 20,4*cr7+so,8 <_glink+0x8> 665 write32(buf + 8, 0x7d6802a6); // mflr r11 666 write32(buf + 12, 0x7c0803a6); // mtlr r0 667 write32(buf + 16, 0x7d8b6050); // subf r12, r11, r12 668 write32(buf + 20, 0x380cffcc); // subi r0,r12,52 669 write32(buf + 24, 0x7800f082); // srdi r0,r0,62,2 670 write32(buf + 28, 0xe98b002c); // ld r12,44(r11) 671 write32(buf + 32, 0x7d6c5a14); // add r11,r12,r11 672 write32(buf + 36, 0xe98b0000); // ld r12,0(r11) 673 write32(buf + 40, 0xe96b0008); // ld r11,8(r11) 674 write32(buf + 44, 0x7d8903a6); // mtctr r12 675 write32(buf + 48, 0x4e800420); // bctr 676 677 // The 'bcl' instruction will set the link register to the address of the 678 // following instruction ('mflr r11'). Here we store the offset from that 679 // instruction to the first entry in the GotPlt section. 680 int64_t gotPltOffset = in.gotPlt->getVA() - (in.plt->getVA() + 8); 681 write64(buf + 52, gotPltOffset); 682 } 683 684 void PPC64::writePlt(uint8_t *buf, const Symbol &sym, 685 uint64_t /*pltEntryAddr*/) const { 686 int32_t offset = pltHeaderSize + sym.pltIndex * pltEntrySize; 687 // bl __glink_PLTresolve 688 write32(buf, 0x48000000 | ((-offset) & 0x03FFFFFc)); 689 } 690 691 void PPC64::writeIplt(uint8_t *buf, const Symbol &sym, 692 uint64_t /*pltEntryAddr*/) const { 693 writePPC64LoadAndBranch(buf, sym.getGotPltVA() - getPPC64TocBase()); 694 } 695 696 static std::pair<RelType, uint64_t> toAddr16Rel(RelType type, uint64_t val) { 697 // Relocations relative to the toc-base need to be adjusted by the Toc offset. 698 uint64_t tocBiasedVal = val - ppc64TocOffset; 699 // Relocations relative to dtv[dtpmod] need to be adjusted by the DTP offset. 700 uint64_t dtpBiasedVal = val - dynamicThreadPointerOffset; 701 702 switch (type) { 703 // TOC biased relocation. 704 case R_PPC64_GOT16: 705 case R_PPC64_GOT_TLSGD16: 706 case R_PPC64_GOT_TLSLD16: 707 case R_PPC64_TOC16: 708 return {R_PPC64_ADDR16, tocBiasedVal}; 709 case R_PPC64_GOT16_DS: 710 case R_PPC64_TOC16_DS: 711 case R_PPC64_GOT_TPREL16_DS: 712 case R_PPC64_GOT_DTPREL16_DS: 713 return {R_PPC64_ADDR16_DS, tocBiasedVal}; 714 case R_PPC64_GOT16_HA: 715 case R_PPC64_GOT_TLSGD16_HA: 716 case R_PPC64_GOT_TLSLD16_HA: 717 case R_PPC64_GOT_TPREL16_HA: 718 case R_PPC64_GOT_DTPREL16_HA: 719 case R_PPC64_TOC16_HA: 720 return {R_PPC64_ADDR16_HA, tocBiasedVal}; 721 case R_PPC64_GOT16_HI: 722 case R_PPC64_GOT_TLSGD16_HI: 723 case R_PPC64_GOT_TLSLD16_HI: 724 case R_PPC64_GOT_TPREL16_HI: 725 case R_PPC64_GOT_DTPREL16_HI: 726 case R_PPC64_TOC16_HI: 727 return {R_PPC64_ADDR16_HI, tocBiasedVal}; 728 case R_PPC64_GOT16_LO: 729 case R_PPC64_GOT_TLSGD16_LO: 730 case R_PPC64_GOT_TLSLD16_LO: 731 case R_PPC64_TOC16_LO: 732 return {R_PPC64_ADDR16_LO, tocBiasedVal}; 733 case R_PPC64_GOT16_LO_DS: 734 case R_PPC64_TOC16_LO_DS: 735 case R_PPC64_GOT_TPREL16_LO_DS: 736 case R_PPC64_GOT_DTPREL16_LO_DS: 737 return {R_PPC64_ADDR16_LO_DS, tocBiasedVal}; 738 739 // Dynamic Thread pointer biased relocation types. 740 case R_PPC64_DTPREL16: 741 return {R_PPC64_ADDR16, dtpBiasedVal}; 742 case R_PPC64_DTPREL16_DS: 743 return {R_PPC64_ADDR16_DS, dtpBiasedVal}; 744 case R_PPC64_DTPREL16_HA: 745 return {R_PPC64_ADDR16_HA, dtpBiasedVal}; 746 case R_PPC64_DTPREL16_HI: 747 return {R_PPC64_ADDR16_HI, dtpBiasedVal}; 748 case R_PPC64_DTPREL16_HIGHER: 749 return {R_PPC64_ADDR16_HIGHER, dtpBiasedVal}; 750 case R_PPC64_DTPREL16_HIGHERA: 751 return {R_PPC64_ADDR16_HIGHERA, dtpBiasedVal}; 752 case R_PPC64_DTPREL16_HIGHEST: 753 return {R_PPC64_ADDR16_HIGHEST, dtpBiasedVal}; 754 case R_PPC64_DTPREL16_HIGHESTA: 755 return {R_PPC64_ADDR16_HIGHESTA, dtpBiasedVal}; 756 case R_PPC64_DTPREL16_LO: 757 return {R_PPC64_ADDR16_LO, dtpBiasedVal}; 758 case R_PPC64_DTPREL16_LO_DS: 759 return {R_PPC64_ADDR16_LO_DS, dtpBiasedVal}; 760 case R_PPC64_DTPREL64: 761 return {R_PPC64_ADDR64, dtpBiasedVal}; 762 763 default: 764 return {type, val}; 765 } 766 } 767 768 static bool isTocOptType(RelType type) { 769 switch (type) { 770 case R_PPC64_GOT16_HA: 771 case R_PPC64_GOT16_LO_DS: 772 case R_PPC64_TOC16_HA: 773 case R_PPC64_TOC16_LO_DS: 774 case R_PPC64_TOC16_LO: 775 return true; 776 default: 777 return false; 778 } 779 } 780 781 void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 782 RelType type = rel.type; 783 bool shouldTocOptimize = isTocOptType(type); 784 // For dynamic thread pointer relative, toc-relative, and got-indirect 785 // relocations, proceed in terms of the corresponding ADDR16 relocation type. 786 std::tie(type, val) = toAddr16Rel(type, val); 787 788 switch (type) { 789 case R_PPC64_ADDR14: { 790 checkAlignment(loc, val, 4, rel); 791 // Preserve the AA/LK bits in the branch instruction 792 uint8_t aalk = loc[3]; 793 write16(loc + 2, (aalk & 3) | (val & 0xfffc)); 794 break; 795 } 796 case R_PPC64_ADDR16: 797 checkIntUInt(loc, val, 16, rel); 798 write16(loc, val); 799 break; 800 case R_PPC64_ADDR32: 801 checkIntUInt(loc, val, 32, rel); 802 write32(loc, val); 803 break; 804 case R_PPC64_ADDR16_DS: 805 case R_PPC64_TPREL16_DS: { 806 checkInt(loc, val, 16, rel); 807 // DQ-form instructions use bits 28-31 as part of the instruction encoding 808 // DS-form instructions only use bits 30-31. 809 uint16_t mask = isDQFormInstruction(readFromHalf16(loc)) ? 0xf : 0x3; 810 checkAlignment(loc, lo(val), mask + 1, rel); 811 write16(loc, (read16(loc) & mask) | lo(val)); 812 } break; 813 case R_PPC64_ADDR16_HA: 814 case R_PPC64_REL16_HA: 815 case R_PPC64_TPREL16_HA: 816 if (config->tocOptimize && shouldTocOptimize && ha(val) == 0) 817 writeFromHalf16(loc, 0x60000000); 818 else 819 write16(loc, ha(val)); 820 break; 821 case R_PPC64_ADDR16_HI: 822 case R_PPC64_REL16_HI: 823 case R_PPC64_TPREL16_HI: 824 write16(loc, hi(val)); 825 break; 826 case R_PPC64_ADDR16_HIGHER: 827 case R_PPC64_TPREL16_HIGHER: 828 write16(loc, higher(val)); 829 break; 830 case R_PPC64_ADDR16_HIGHERA: 831 case R_PPC64_TPREL16_HIGHERA: 832 write16(loc, highera(val)); 833 break; 834 case R_PPC64_ADDR16_HIGHEST: 835 case R_PPC64_TPREL16_HIGHEST: 836 write16(loc, highest(val)); 837 break; 838 case R_PPC64_ADDR16_HIGHESTA: 839 case R_PPC64_TPREL16_HIGHESTA: 840 write16(loc, highesta(val)); 841 break; 842 case R_PPC64_ADDR16_LO: 843 case R_PPC64_REL16_LO: 844 case R_PPC64_TPREL16_LO: 845 // When the high-adjusted part of a toc relocation evaluates to 0, it is 846 // changed into a nop. The lo part then needs to be updated to use the 847 // toc-pointer register r2, as the base register. 848 if (config->tocOptimize && shouldTocOptimize && ha(val) == 0) { 849 uint32_t insn = readFromHalf16(loc); 850 if (isInstructionUpdateForm(insn)) 851 error(getErrorLocation(loc) + 852 "can't toc-optimize an update instruction: 0x" + 853 utohexstr(insn)); 854 writeFromHalf16(loc, (insn & 0xffe00000) | 0x00020000 | lo(val)); 855 } else { 856 write16(loc, lo(val)); 857 } 858 break; 859 case R_PPC64_ADDR16_LO_DS: 860 case R_PPC64_TPREL16_LO_DS: { 861 // DQ-form instructions use bits 28-31 as part of the instruction encoding 862 // DS-form instructions only use bits 30-31. 863 uint32_t insn = readFromHalf16(loc); 864 uint16_t mask = isDQFormInstruction(insn) ? 0xf : 0x3; 865 checkAlignment(loc, lo(val), mask + 1, rel); 866 if (config->tocOptimize && shouldTocOptimize && ha(val) == 0) { 867 // When the high-adjusted part of a toc relocation evaluates to 0, it is 868 // changed into a nop. The lo part then needs to be updated to use the toc 869 // pointer register r2, as the base register. 870 if (isInstructionUpdateForm(insn)) 871 error(getErrorLocation(loc) + 872 "Can't toc-optimize an update instruction: 0x" + 873 Twine::utohexstr(insn)); 874 insn &= 0xffe00000 | mask; 875 writeFromHalf16(loc, insn | 0x00020000 | lo(val)); 876 } else { 877 write16(loc, (read16(loc) & mask) | lo(val)); 878 } 879 } break; 880 case R_PPC64_TPREL16: 881 checkInt(loc, val, 16, rel); 882 write16(loc, val); 883 break; 884 case R_PPC64_REL32: 885 checkInt(loc, val, 32, rel); 886 write32(loc, val); 887 break; 888 case R_PPC64_ADDR64: 889 case R_PPC64_REL64: 890 case R_PPC64_TOC: 891 write64(loc, val); 892 break; 893 case R_PPC64_REL14: { 894 uint32_t mask = 0x0000FFFC; 895 checkInt(loc, val, 16, rel); 896 checkAlignment(loc, val, 4, rel); 897 write32(loc, (read32(loc) & ~mask) | (val & mask)); 898 break; 899 } 900 case R_PPC64_REL24: { 901 uint32_t mask = 0x03FFFFFC; 902 checkInt(loc, val, 26, rel); 903 checkAlignment(loc, val, 4, rel); 904 write32(loc, (read32(loc) & ~mask) | (val & mask)); 905 break; 906 } 907 case R_PPC64_DTPREL64: 908 write64(loc, val - dynamicThreadPointerOffset); 909 break; 910 default: 911 llvm_unreachable("unknown relocation"); 912 } 913 } 914 915 bool PPC64::needsThunk(RelExpr expr, RelType type, const InputFile *file, 916 uint64_t branchAddr, const Symbol &s, int64_t a) const { 917 if (type != R_PPC64_REL14 && type != R_PPC64_REL24) 918 return false; 919 920 // If a function is in the Plt it needs to be called with a call-stub. 921 if (s.isInPlt()) 922 return true; 923 924 // If a symbol is a weak undefined and we are compiling an executable 925 // it doesn't need a range-extending thunk since it can't be called. 926 if (s.isUndefWeak() && !config->shared) 927 return false; 928 929 // If the offset exceeds the range of the branch type then it will need 930 // a range-extending thunk. 931 // See the comment in getRelocTargetVA() about R_PPC64_CALL. 932 return !inBranchRange(type, branchAddr, 933 s.getVA(a) + 934 getPPC64GlobalEntryToLocalEntryOffset(s.stOther)); 935 } 936 937 uint32_t PPC64::getThunkSectionSpacing() const { 938 // See comment in Arch/ARM.cpp for a more detailed explanation of 939 // getThunkSectionSpacing(). For PPC64 we pick the constant here based on 940 // R_PPC64_REL24, which is used by unconditional branch instructions. 941 // 0x2000000 = (1 << 24-1) * 4 942 return 0x2000000; 943 } 944 945 bool PPC64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 946 int64_t offset = dst - src; 947 if (type == R_PPC64_REL14) 948 return isInt<16>(offset); 949 if (type == R_PPC64_REL24) 950 return isInt<26>(offset); 951 llvm_unreachable("unsupported relocation type used in branch"); 952 } 953 954 RelExpr PPC64::adjustRelaxExpr(RelType type, const uint8_t *data, 955 RelExpr expr) const { 956 if (expr == R_RELAX_TLS_GD_TO_IE) 957 return R_RELAX_TLS_GD_TO_IE_GOT_OFF; 958 if (expr == R_RELAX_TLS_LD_TO_LE) 959 return R_RELAX_TLS_LD_TO_LE_ABS; 960 return expr; 961 } 962 963 // Reference: 3.7.4.1 of the 64-bit ELF V2 abi supplement. 964 // The general dynamic code sequence for a global `x` uses 4 instructions. 965 // Instruction Relocation Symbol 966 // addis r3, r2, x@got@tlsgd@ha R_PPC64_GOT_TLSGD16_HA x 967 // addi r3, r3, x@got@tlsgd@l R_PPC64_GOT_TLSGD16_LO x 968 // bl __tls_get_addr(x@tlsgd) R_PPC64_TLSGD x 969 // R_PPC64_REL24 __tls_get_addr 970 // nop None None 971 // 972 // Relaxing to initial-exec entails: 973 // 1) Convert the addis/addi pair that builds the address of the tls_index 974 // struct for 'x' to an addis/ld pair that loads an offset from a got-entry. 975 // 2) Convert the call to __tls_get_addr to a nop. 976 // 3) Convert the nop following the call to an add of the loaded offset to the 977 // thread pointer. 978 // Since the nop must directly follow the call, the R_PPC64_TLSGD relocation is 979 // used as the relaxation hint for both steps 2 and 3. 980 void PPC64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 981 uint64_t val) const { 982 switch (rel.type) { 983 case R_PPC64_GOT_TLSGD16_HA: 984 // This is relaxed from addis rT, r2, sym@got@tlsgd@ha to 985 // addis rT, r2, sym@got@tprel@ha. 986 relocateNoSym(loc, R_PPC64_GOT_TPREL16_HA, val); 987 return; 988 case R_PPC64_GOT_TLSGD16: 989 case R_PPC64_GOT_TLSGD16_LO: { 990 // Relax from addi r3, rA, sym@got@tlsgd@l to 991 // ld r3, sym@got@tprel@l(rA) 992 uint32_t ra = (readFromHalf16(loc) & (0x1f << 16)); 993 writeFromHalf16(loc, 0xe8600000 | ra); 994 relocateNoSym(loc, R_PPC64_GOT_TPREL16_LO_DS, val); 995 return; 996 } 997 case R_PPC64_TLSGD: 998 write32(loc, 0x60000000); // bl __tls_get_addr(sym@tlsgd) --> nop 999 write32(loc + 4, 0x7c636A14); // nop --> add r3, r3, r13 1000 return; 1001 default: 1002 llvm_unreachable("unsupported relocation for TLS GD to IE relaxation"); 1003 } 1004 } 1005 1006 // The prologue for a split-stack function is expected to look roughly 1007 // like this: 1008 // .Lglobal_entry_point: 1009 // # TOC pointer initialization. 1010 // ... 1011 // .Llocal_entry_point: 1012 // # load the __private_ss member of the threads tcbhead. 1013 // ld r0,-0x7000-64(r13) 1014 // # subtract the functions stack size from the stack pointer. 1015 // addis r12, r1, ha(-stack-frame size) 1016 // addi r12, r12, l(-stack-frame size) 1017 // # compare needed to actual and branch to allocate_more_stack if more 1018 // # space is needed, otherwise fallthrough to 'normal' function body. 1019 // cmpld cr7,r12,r0 1020 // blt- cr7, .Lallocate_more_stack 1021 // 1022 // -) The allocate_more_stack block might be placed after the split-stack 1023 // prologue and the `blt-` replaced with a `bge+ .Lnormal_func_body` 1024 // instead. 1025 // -) If either the addis or addi is not needed due to the stack size being 1026 // smaller then 32K or a multiple of 64K they will be replaced with a nop, 1027 // but there will always be 2 instructions the linker can overwrite for the 1028 // adjusted stack size. 1029 // 1030 // The linkers job here is to increase the stack size used in the addis/addi 1031 // pair by split-stack-size-adjust. 1032 // addis r12, r1, ha(-stack-frame size - split-stack-adjust-size) 1033 // addi r12, r12, l(-stack-frame size - split-stack-adjust-size) 1034 bool PPC64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end, 1035 uint8_t stOther) const { 1036 // If the caller has a global entry point adjust the buffer past it. The start 1037 // of the split-stack prologue will be at the local entry point. 1038 loc += getPPC64GlobalEntryToLocalEntryOffset(stOther); 1039 1040 // At the very least we expect to see a load of some split-stack data from the 1041 // tcb, and 2 instructions that calculate the ending stack address this 1042 // function will require. If there is not enough room for at least 3 1043 // instructions it can't be a split-stack prologue. 1044 if (loc + 12 >= end) 1045 return false; 1046 1047 // First instruction must be `ld r0, -0x7000-64(r13)` 1048 if (read32(loc) != 0xe80d8fc0) 1049 return false; 1050 1051 int16_t hiImm = 0; 1052 int16_t loImm = 0; 1053 // First instruction can be either an addis if the frame size is larger then 1054 // 32K, or an addi if the size is less then 32K. 1055 int32_t firstInstr = read32(loc + 4); 1056 if (getPrimaryOpCode(firstInstr) == 15) { 1057 hiImm = firstInstr & 0xFFFF; 1058 } else if (getPrimaryOpCode(firstInstr) == 14) { 1059 loImm = firstInstr & 0xFFFF; 1060 } else { 1061 return false; 1062 } 1063 1064 // Second instruction is either an addi or a nop. If the first instruction was 1065 // an addi then LoImm is set and the second instruction must be a nop. 1066 uint32_t secondInstr = read32(loc + 8); 1067 if (!loImm && getPrimaryOpCode(secondInstr) == 14) { 1068 loImm = secondInstr & 0xFFFF; 1069 } else if (secondInstr != 0x60000000) { 1070 return false; 1071 } 1072 1073 // The register operands of the first instruction should be the stack-pointer 1074 // (r1) as the input (RA) and r12 as the output (RT). If the second 1075 // instruction is not a nop, then it should use r12 as both input and output. 1076 auto checkRegOperands = [](uint32_t instr, uint8_t expectedRT, 1077 uint8_t expectedRA) { 1078 return ((instr & 0x3E00000) >> 21 == expectedRT) && 1079 ((instr & 0x1F0000) >> 16 == expectedRA); 1080 }; 1081 if (!checkRegOperands(firstInstr, 12, 1)) 1082 return false; 1083 if (secondInstr != 0x60000000 && !checkRegOperands(secondInstr, 12, 12)) 1084 return false; 1085 1086 int32_t stackFrameSize = (hiImm * 65536) + loImm; 1087 // Check that the adjusted size doesn't overflow what we can represent with 2 1088 // instructions. 1089 if (stackFrameSize < config->splitStackAdjustSize + INT32_MIN) { 1090 error(getErrorLocation(loc) + "split-stack prologue adjustment overflows"); 1091 return false; 1092 } 1093 1094 int32_t adjustedStackFrameSize = 1095 stackFrameSize - config->splitStackAdjustSize; 1096 1097 loImm = adjustedStackFrameSize & 0xFFFF; 1098 hiImm = (adjustedStackFrameSize + 0x8000) >> 16; 1099 if (hiImm) { 1100 write32(loc + 4, 0x3D810000 | (uint16_t)hiImm); 1101 // If the low immediate is zero the second instruction will be a nop. 1102 secondInstr = loImm ? 0x398C0000 | (uint16_t)loImm : 0x60000000; 1103 write32(loc + 8, secondInstr); 1104 } else { 1105 // addi r12, r1, imm 1106 write32(loc + 4, (0x39810000) | (uint16_t)loImm); 1107 write32(loc + 8, 0x60000000); 1108 } 1109 1110 return true; 1111 } 1112 1113 TargetInfo *getPPC64TargetInfo() { 1114 static PPC64 target; 1115 return ⌖ 1116 } 1117 1118 } // namespace elf 1119 } // namespace lld 1120