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