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