1 //===- PPC.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 "OutputSections.h" 10 #include "Symbols.h" 11 #include "SyntheticSections.h" 12 #include "Target.h" 13 #include "Thunks.h" 14 #include "lld/Common/ErrorHandler.h" 15 #include "llvm/Support/Endian.h" 16 17 using namespace llvm; 18 using namespace llvm::support::endian; 19 using namespace llvm::ELF; 20 using namespace lld; 21 using namespace lld::elf; 22 23 // Undefine the macro predefined by GCC powerpc32. 24 #undef PPC 25 26 namespace { 27 class PPC final : public TargetInfo { 28 public: 29 PPC(Ctx &); 30 RelExpr getRelExpr(RelType type, const Symbol &s, 31 const uint8_t *loc) const override; 32 RelType getDynRel(RelType type) const override; 33 int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override; 34 void writeGotHeader(uint8_t *buf) const override; 35 void writePltHeader(uint8_t *buf) const override { 36 llvm_unreachable("should call writePPC32GlinkSection() instead"); 37 } 38 void writePlt(uint8_t *buf, const Symbol &sym, 39 uint64_t pltEntryAddr) const override { 40 llvm_unreachable("should call writePPC32GlinkSection() instead"); 41 } 42 void writeIplt(uint8_t *buf, const Symbol &sym, 43 uint64_t pltEntryAddr) const override; 44 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 45 bool needsThunk(RelExpr expr, RelType relocType, const InputFile *file, 46 uint64_t branchAddr, const Symbol &s, 47 int64_t a) const override; 48 uint32_t getThunkSectionSpacing() const override; 49 bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override; 50 void relocate(uint8_t *loc, const Relocation &rel, 51 uint64_t val) const override; 52 RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override; 53 int getTlsGdRelaxSkip(RelType type) const override; 54 void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override; 55 56 private: 57 void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const; 58 void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const; 59 void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const; 60 void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const; 61 }; 62 } // namespace 63 64 static uint16_t lo(uint32_t v) { return v; } 65 static uint16_t ha(uint32_t v) { return (v + 0x8000) >> 16; } 66 67 static uint32_t readFromHalf16(Ctx &ctx, const uint8_t *loc) { 68 return read32(ctx, ctx.arg.isLE ? loc : loc - 2); 69 } 70 71 static void writeFromHalf16(Ctx &ctx, uint8_t *loc, uint32_t insn) { 72 write32(ctx, ctx.arg.isLE ? loc : loc - 2, insn); 73 } 74 75 void elf::writePPC32GlinkSection(Ctx &ctx, uint8_t *buf, size_t numEntries) { 76 // Create canonical PLT entries for non-PIE code. Compilers don't generate 77 // non-GOT-non-PLT relocations referencing external functions for -fpie/-fPIE. 78 uint32_t glink = ctx.in.plt->getVA(); // VA of .glink 79 if (!ctx.arg.isPic) { 80 for (const Symbol *sym : 81 cast<PPC32GlinkSection>(*ctx.in.plt).canonical_plts) { 82 writePPC32PltCallStub(ctx, buf, sym->getGotPltVA(ctx), nullptr, 0); 83 buf += 16; 84 glink += 16; 85 } 86 } 87 88 // On PPC Secure PLT ABI, bl foo@plt jumps to a call stub, which loads an 89 // absolute address from a specific .plt slot (usually called .got.plt on 90 // other targets) and jumps there. 91 // 92 // a) With immediate binding (BIND_NOW), the .plt entry is resolved at load 93 // time. The .glink section is not used. 94 // b) With lazy binding, the .plt entry points to a `b PLTresolve` 95 // instruction in .glink, filled in by PPC::writeGotPlt(). 96 97 // Write N `b PLTresolve` first. 98 for (size_t i = 0; i != numEntries; ++i) 99 write32(ctx, buf + 4 * i, 0x48000000 | 4 * (numEntries - i)); 100 buf += 4 * numEntries; 101 102 // Then write PLTresolve(), which has two forms: PIC and non-PIC. PLTresolve() 103 // computes the PLT index (by computing the distance from the landing b to 104 // itself) and calls _dl_runtime_resolve() (in glibc). 105 uint32_t got = ctx.in.got->getVA(); 106 const uint8_t *end = buf + 64; 107 if (ctx.arg.isPic) { 108 uint32_t afterBcl = 4 * ctx.in.plt->getNumEntries() + 12; 109 uint32_t gotBcl = got + 4 - (glink + afterBcl); 110 write32(ctx, buf + 0, 111 0x3d6b0000 | ha(afterBcl)); // addis r11,r11,1f-glink@ha 112 write32(ctx, buf + 4, 0x7c0802a6); // mflr r0 113 write32(ctx, buf + 8, 0x429f0005); // bcl 20,30,.+4 114 write32(ctx, buf + 12, 115 0x396b0000 | lo(afterBcl)); // 1: addi r11,r11,1b-glink@l 116 write32(ctx, buf + 16, 0x7d8802a6); // mflr r12 117 write32(ctx, buf + 20, 0x7c0803a6); // mtlr r0 118 write32(ctx, buf + 24, 0x7d6c5850); // sub r11,r11,r12 119 write32(ctx, buf + 28, 0x3d8c0000 | ha(gotBcl)); // addis 12,12,GOT+4-1b@ha 120 if (ha(gotBcl) == ha(gotBcl + 4)) { 121 write32(ctx, buf + 32, 122 0x800c0000 | lo(gotBcl)); // lwz r0,r12,GOT+4-1b@l(r12) 123 write32(ctx, buf + 36, 124 0x818c0000 | lo(gotBcl + 4)); // lwz r12,r12,GOT+8-1b@l(r12) 125 } else { 126 write32(ctx, buf + 32, 127 0x840c0000 | lo(gotBcl)); // lwzu r0,r12,GOT+4-1b@l(r12) 128 write32(ctx, buf + 36, 0x818c0000 | 4); // lwz r12,r12,4(r12) 129 } 130 write32(ctx, buf + 40, 0x7c0903a6); // mtctr 0 131 write32(ctx, buf + 44, 0x7c0b5a14); // add r0,11,11 132 write32(ctx, buf + 48, 0x7d605a14); // add r11,0,11 133 write32(ctx, buf + 52, 0x4e800420); // bctr 134 buf += 56; 135 } else { 136 write32(ctx, buf + 0, 0x3d800000 | ha(got + 4)); // lis r12,GOT+4@ha 137 write32(ctx, buf + 4, 0x3d6b0000 | ha(-glink)); // addis r11,r11,-glink@ha 138 if (ha(got + 4) == ha(got + 8)) 139 write32(ctx, buf + 8, 0x800c0000 | lo(got + 4)); // lwz r0,GOT+4@l(r12) 140 else 141 write32(ctx, buf + 8, 0x840c0000 | lo(got + 4)); // lwzu r0,GOT+4@l(r12) 142 write32(ctx, buf + 12, 0x396b0000 | lo(-glink)); // addi r11,r11,-glink@l 143 write32(ctx, buf + 16, 0x7c0903a6); // mtctr r0 144 write32(ctx, buf + 20, 0x7c0b5a14); // add r0,r11,r11 145 if (ha(got + 4) == ha(got + 8)) 146 write32(ctx, buf + 24, 0x818c0000 | lo(got + 8)); // lwz r12,GOT+8@l(r12) 147 else 148 write32(ctx, buf + 24, 0x818c0000 | 4); // lwz r12,4(r12) 149 write32(ctx, buf + 28, 0x7d605a14); // add r11,r0,r11 150 write32(ctx, buf + 32, 0x4e800420); // bctr 151 buf += 36; 152 } 153 154 // Pad with nop. They should not be executed. 155 for (; buf < end; buf += 4) 156 write32(ctx, buf, 0x60000000); 157 } 158 159 PPC::PPC(Ctx &ctx) : TargetInfo(ctx) { 160 copyRel = R_PPC_COPY; 161 gotRel = R_PPC_GLOB_DAT; 162 pltRel = R_PPC_JMP_SLOT; 163 relativeRel = R_PPC_RELATIVE; 164 iRelativeRel = R_PPC_IRELATIVE; 165 symbolicRel = R_PPC_ADDR32; 166 gotHeaderEntriesNum = 3; 167 gotPltHeaderEntriesNum = 0; 168 pltHeaderSize = 0; 169 pltEntrySize = 4; 170 ipltEntrySize = 16; 171 172 needsThunks = true; 173 174 tlsModuleIndexRel = R_PPC_DTPMOD32; 175 tlsOffsetRel = R_PPC_DTPREL32; 176 tlsGotRel = R_PPC_TPREL32; 177 178 defaultMaxPageSize = 65536; 179 defaultImageBase = 0x10000000; 180 181 write32(ctx, trapInstr.data(), 0x7fe00008); 182 } 183 184 void PPC::writeIplt(uint8_t *buf, const Symbol &sym, 185 uint64_t /*pltEntryAddr*/) const { 186 // In -pie or -shared mode, assume r30 points to .got2+0x8000, and use a 187 // .got2.plt_pic32. thunk. 188 writePPC32PltCallStub(ctx, buf, sym.getGotPltVA(ctx), sym.file, 0x8000); 189 } 190 191 void PPC::writeGotHeader(uint8_t *buf) const { 192 // _GLOBAL_OFFSET_TABLE_[0] = _DYNAMIC 193 // glibc stores _dl_runtime_resolve in _GLOBAL_OFFSET_TABLE_[1], 194 // link_map in _GLOBAL_OFFSET_TABLE_[2]. 195 write32(ctx, buf, ctx.mainPart->dynamic->getVA()); 196 } 197 198 void PPC::writeGotPlt(uint8_t *buf, const Symbol &s) const { 199 // Address of the symbol resolver stub in .glink . 200 write32(ctx, buf, 201 ctx.in.plt->getVA() + ctx.in.plt->headerSize + 4 * s.getPltIdx(ctx)); 202 } 203 204 bool PPC::needsThunk(RelExpr expr, RelType type, const InputFile *file, 205 uint64_t branchAddr, const Symbol &s, int64_t a) const { 206 if (type != R_PPC_LOCAL24PC && type != R_PPC_REL24 && type != R_PPC_PLTREL24) 207 return false; 208 if (s.isInPlt(ctx)) 209 return true; 210 if (s.isUndefWeak()) 211 return false; 212 return !PPC::inBranchRange(type, branchAddr, s.getVA(ctx, a)); 213 } 214 215 uint32_t PPC::getThunkSectionSpacing() const { return 0x2000000; } 216 217 bool PPC::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 218 uint64_t offset = dst - src; 219 if (type == R_PPC_LOCAL24PC || type == R_PPC_REL24 || type == R_PPC_PLTREL24) 220 return isInt<26>(offset); 221 llvm_unreachable("unsupported relocation type used in branch"); 222 } 223 224 RelExpr PPC::getRelExpr(RelType type, const Symbol &s, 225 const uint8_t *loc) const { 226 switch (type) { 227 case R_PPC_NONE: 228 return R_NONE; 229 case R_PPC_ADDR16_HA: 230 case R_PPC_ADDR16_HI: 231 case R_PPC_ADDR16_LO: 232 case R_PPC_ADDR24: 233 case R_PPC_ADDR32: 234 return R_ABS; 235 case R_PPC_DTPREL16: 236 case R_PPC_DTPREL16_HA: 237 case R_PPC_DTPREL16_HI: 238 case R_PPC_DTPREL16_LO: 239 case R_PPC_DTPREL32: 240 return R_DTPREL; 241 case R_PPC_REL14: 242 case R_PPC_REL32: 243 case R_PPC_REL16_LO: 244 case R_PPC_REL16_HI: 245 case R_PPC_REL16_HA: 246 return R_PC; 247 case R_PPC_GOT16: 248 return R_GOT_OFF; 249 case R_PPC_LOCAL24PC: 250 case R_PPC_REL24: 251 return R_PLT_PC; 252 case R_PPC_PLTREL24: 253 return RE_PPC32_PLTREL; 254 case R_PPC_GOT_TLSGD16: 255 return R_TLSGD_GOT; 256 case R_PPC_GOT_TLSLD16: 257 return R_TLSLD_GOT; 258 case R_PPC_GOT_TPREL16: 259 return R_GOT_OFF; 260 case R_PPC_TLS: 261 return R_TLSIE_HINT; 262 case R_PPC_TLSGD: 263 return R_TLSDESC_CALL; 264 case R_PPC_TLSLD: 265 return R_TLSLD_HINT; 266 case R_PPC_TPREL16: 267 case R_PPC_TPREL16_HA: 268 case R_PPC_TPREL16_LO: 269 case R_PPC_TPREL16_HI: 270 return R_TPREL; 271 default: 272 Err(ctx) << getErrorLoc(ctx, loc) << "unknown relocation (" << type.v 273 << ") against symbol " << &s; 274 return R_NONE; 275 } 276 } 277 278 RelType PPC::getDynRel(RelType type) const { 279 if (type == R_PPC_ADDR32) 280 return type; 281 return R_PPC_NONE; 282 } 283 284 int64_t PPC::getImplicitAddend(const uint8_t *buf, RelType type) const { 285 switch (type) { 286 case R_PPC_NONE: 287 case R_PPC_GLOB_DAT: 288 case R_PPC_JMP_SLOT: 289 return 0; 290 case R_PPC_ADDR32: 291 case R_PPC_REL32: 292 case R_PPC_RELATIVE: 293 case R_PPC_IRELATIVE: 294 case R_PPC_DTPMOD32: 295 case R_PPC_DTPREL32: 296 case R_PPC_TPREL32: 297 return SignExtend64<32>(read32(ctx, buf)); 298 default: 299 InternalErr(ctx, buf) << "cannot read addend for relocation " << type; 300 return 0; 301 } 302 } 303 304 static std::pair<RelType, uint64_t> fromDTPREL(RelType type, uint64_t val) { 305 uint64_t dtpBiasedVal = val - 0x8000; 306 switch (type) { 307 case R_PPC_DTPREL16: 308 return {R_PPC64_ADDR16, dtpBiasedVal}; 309 case R_PPC_DTPREL16_HA: 310 return {R_PPC_ADDR16_HA, dtpBiasedVal}; 311 case R_PPC_DTPREL16_HI: 312 return {R_PPC_ADDR16_HI, dtpBiasedVal}; 313 case R_PPC_DTPREL16_LO: 314 return {R_PPC_ADDR16_LO, dtpBiasedVal}; 315 case R_PPC_DTPREL32: 316 return {R_PPC_ADDR32, dtpBiasedVal}; 317 default: 318 return {type, val}; 319 } 320 } 321 322 void PPC::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 323 RelType newType; 324 std::tie(newType, val) = fromDTPREL(rel.type, val); 325 switch (newType) { 326 case R_PPC_ADDR16: 327 checkIntUInt(ctx, loc, val, 16, rel); 328 write16(ctx, loc, val); 329 break; 330 case R_PPC_GOT16: 331 case R_PPC_GOT_TLSGD16: 332 case R_PPC_GOT_TLSLD16: 333 case R_PPC_GOT_TPREL16: 334 case R_PPC_TPREL16: 335 checkInt(ctx, loc, val, 16, rel); 336 write16(ctx, loc, val); 337 break; 338 case R_PPC_ADDR16_HA: 339 case R_PPC_DTPREL16_HA: 340 case R_PPC_GOT_TLSGD16_HA: 341 case R_PPC_GOT_TLSLD16_HA: 342 case R_PPC_GOT_TPREL16_HA: 343 case R_PPC_REL16_HA: 344 case R_PPC_TPREL16_HA: 345 write16(ctx, loc, ha(val)); 346 break; 347 case R_PPC_ADDR16_HI: 348 case R_PPC_DTPREL16_HI: 349 case R_PPC_GOT_TLSGD16_HI: 350 case R_PPC_GOT_TLSLD16_HI: 351 case R_PPC_GOT_TPREL16_HI: 352 case R_PPC_REL16_HI: 353 case R_PPC_TPREL16_HI: 354 write16(ctx, loc, val >> 16); 355 break; 356 case R_PPC_ADDR16_LO: 357 case R_PPC_DTPREL16_LO: 358 case R_PPC_GOT_TLSGD16_LO: 359 case R_PPC_GOT_TLSLD16_LO: 360 case R_PPC_GOT_TPREL16_LO: 361 case R_PPC_REL16_LO: 362 case R_PPC_TPREL16_LO: 363 write16(ctx, loc, val); 364 break; 365 case R_PPC_ADDR32: 366 case R_PPC_REL32: 367 write32(ctx, loc, val); 368 break; 369 case R_PPC_REL14: { 370 uint32_t mask = 0x0000FFFC; 371 checkInt(ctx, loc, val, 16, rel); 372 checkAlignment(ctx, loc, val, 4, rel); 373 write32(ctx, loc, (read32(ctx, loc) & ~mask) | (val & mask)); 374 break; 375 } 376 case R_PPC_ADDR24: 377 case R_PPC_REL24: 378 case R_PPC_LOCAL24PC: 379 case R_PPC_PLTREL24: { 380 uint32_t mask = 0x03FFFFFC; 381 checkInt(ctx, loc, val, 26, rel); 382 checkAlignment(ctx, loc, val, 4, rel); 383 write32(ctx, loc, (read32(ctx, loc) & ~mask) | (val & mask)); 384 break; 385 } 386 default: 387 llvm_unreachable("unknown relocation"); 388 } 389 } 390 391 RelExpr PPC::adjustTlsExpr(RelType type, RelExpr expr) const { 392 if (expr == R_RELAX_TLS_GD_TO_IE) 393 return R_RELAX_TLS_GD_TO_IE_GOT_OFF; 394 if (expr == R_RELAX_TLS_LD_TO_LE) 395 return R_RELAX_TLS_LD_TO_LE_ABS; 396 return expr; 397 } 398 399 int PPC::getTlsGdRelaxSkip(RelType type) const { 400 // A __tls_get_addr call instruction is marked with 2 relocations: 401 // 402 // R_PPC_TLSGD / R_PPC_TLSLD: marker relocation 403 // R_PPC_REL24: __tls_get_addr 404 // 405 // After the relaxation we no longer call __tls_get_addr and should skip both 406 // relocations to not create a false dependence on __tls_get_addr being 407 // defined. 408 if (type == R_PPC_TLSGD || type == R_PPC_TLSLD) 409 return 2; 410 return 1; 411 } 412 413 void PPC::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 414 uint64_t val) const { 415 switch (rel.type) { 416 case R_PPC_GOT_TLSGD16: { 417 // addi rT, rA, x@got@tlsgd --> lwz rT, x@got@tprel(rA) 418 uint32_t insn = readFromHalf16(ctx, loc); 419 writeFromHalf16(ctx, loc, 0x80000000 | (insn & 0x03ff0000)); 420 relocateNoSym(loc, R_PPC_GOT_TPREL16, val); 421 break; 422 } 423 case R_PPC_TLSGD: 424 // bl __tls_get_addr(x@tldgd) --> add r3, r3, r2 425 write32(ctx, loc, 0x7c631214); 426 break; 427 default: 428 llvm_unreachable("unsupported relocation for TLS GD to IE relaxation"); 429 } 430 } 431 432 void PPC::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 433 uint64_t val) const { 434 switch (rel.type) { 435 case R_PPC_GOT_TLSGD16: 436 // addi r3, r31, x@got@tlsgd --> addis r3, r2, x@tprel@ha 437 writeFromHalf16(ctx, loc, 0x3c620000 | ha(val)); 438 break; 439 case R_PPC_TLSGD: 440 // bl __tls_get_addr(x@tldgd) --> add r3, r3, x@tprel@l 441 write32(ctx, loc, 0x38630000 | lo(val)); 442 break; 443 default: 444 llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 445 } 446 } 447 448 void PPC::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, 449 uint64_t val) const { 450 switch (rel.type) { 451 case R_PPC_GOT_TLSLD16: 452 // addi r3, rA, x@got@tlsgd --> addis r3, r2, 0 453 writeFromHalf16(ctx, loc, 0x3c620000); 454 break; 455 case R_PPC_TLSLD: 456 // r3+x@dtprel computes r3+x-0x8000, while we want it to compute r3+x@tprel 457 // = r3+x-0x7000, so add 4096 to r3. 458 // bl __tls_get_addr(x@tlsld) --> addi r3, r3, 4096 459 write32(ctx, loc, 0x38631000); 460 break; 461 case R_PPC_DTPREL16: 462 case R_PPC_DTPREL16_HA: 463 case R_PPC_DTPREL16_HI: 464 case R_PPC_DTPREL16_LO: 465 relocate(loc, rel, val); 466 break; 467 default: 468 llvm_unreachable("unsupported relocation for TLS LD to LE relaxation"); 469 } 470 } 471 472 void PPC::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 473 uint64_t val) const { 474 switch (rel.type) { 475 case R_PPC_GOT_TPREL16: { 476 // lwz rT, x@got@tprel(rA) --> addis rT, r2, x@tprel@ha 477 uint32_t rt = readFromHalf16(ctx, loc) & 0x03e00000; 478 writeFromHalf16(ctx, loc, 0x3c020000 | rt | ha(val)); 479 break; 480 } 481 case R_PPC_TLS: { 482 uint32_t insn = read32(ctx, loc); 483 if (insn >> 26 != 31) 484 ErrAlways(ctx) << "unrecognized instruction for IE to LE R_PPC_TLS"; 485 // addi rT, rT, x@tls --> addi rT, rT, x@tprel@l 486 unsigned secondaryOp = (read32(ctx, loc) & 0x000007fe) >> 1; 487 uint32_t dFormOp = getPPCDFormOp(secondaryOp); 488 if (dFormOp == 0) { // Expecting a DS-Form instruction. 489 dFormOp = getPPCDSFormOp(secondaryOp); 490 if (dFormOp == 0) 491 ErrAlways(ctx) << "unrecognized instruction for IE to LE R_PPC_TLS"; 492 } 493 write32(ctx, loc, (dFormOp | (insn & 0x03ff0000) | lo(val))); 494 break; 495 } 496 default: 497 llvm_unreachable("unsupported relocation for TLS IE to LE relaxation"); 498 } 499 } 500 501 void PPC::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const { 502 uint64_t secAddr = sec.getOutputSection()->addr; 503 if (auto *s = dyn_cast<InputSection>(&sec)) 504 secAddr += s->outSecOff; 505 for (const Relocation &rel : sec.relocs()) { 506 uint8_t *loc = buf + rel.offset; 507 const uint64_t val = 508 SignExtend64(sec.getRelocTargetVA(ctx, rel, secAddr + rel.offset), 32); 509 switch (rel.expr) { 510 case R_RELAX_TLS_GD_TO_IE_GOT_OFF: 511 relaxTlsGdToIe(loc, rel, val); 512 break; 513 case R_RELAX_TLS_GD_TO_LE: 514 relaxTlsGdToLe(loc, rel, val); 515 break; 516 case R_RELAX_TLS_LD_TO_LE_ABS: 517 relaxTlsLdToLe(loc, rel, val); 518 break; 519 case R_RELAX_TLS_IE_TO_LE: 520 relaxTlsIeToLe(loc, rel, val); 521 break; 522 default: 523 relocate(loc, rel, val); 524 break; 525 } 526 } 527 } 528 529 void elf::setPPCTargetInfo(Ctx &ctx) { ctx.target.reset(new PPC(ctx)); } 530