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