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