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_DTPREL16: 194 case R_PPC_DTPREL16_HA: 195 case R_PPC_DTPREL16_HI: 196 case R_PPC_DTPREL16_LO: 197 case R_PPC_DTPREL32: 198 return R_DTPREL; 199 case R_PPC_REL14: 200 case R_PPC_REL32: 201 case R_PPC_LOCAL24PC: 202 case R_PPC_REL16_LO: 203 case R_PPC_REL16_HI: 204 case R_PPC_REL16_HA: 205 return R_PC; 206 case R_PPC_GOT16: 207 return R_GOT_OFF; 208 case R_PPC_REL24: 209 return R_PLT_PC; 210 case R_PPC_PLTREL24: 211 return R_PPC32_PLTREL; 212 case R_PPC_GOT_TLSGD16: 213 return R_TLSGD_GOT; 214 case R_PPC_GOT_TLSLD16: 215 return R_TLSLD_GOT; 216 case R_PPC_GOT_TPREL16: 217 return R_GOT_OFF; 218 case R_PPC_TLS: 219 return R_TLSIE_HINT; 220 case R_PPC_TLSGD: 221 return R_TLSDESC_CALL; 222 case R_PPC_TLSLD: 223 return R_TLSLD_HINT; 224 case R_PPC_TPREL16: 225 case R_PPC_TPREL16_HA: 226 case R_PPC_TPREL16_LO: 227 case R_PPC_TPREL16_HI: 228 return R_TLS; 229 default: 230 return R_ABS; 231 } 232 } 233 234 RelType PPC::getDynRel(RelType type) const { 235 if (type == R_PPC_ADDR32) 236 return type; 237 return R_PPC_NONE; 238 } 239 240 static std::pair<RelType, uint64_t> fromDTPREL(RelType type, uint64_t val) { 241 uint64_t dtpBiasedVal = val - 0x8000; 242 switch (type) { 243 case R_PPC_DTPREL16: 244 return {R_PPC64_ADDR16, dtpBiasedVal}; 245 case R_PPC_DTPREL16_HA: 246 return {R_PPC_ADDR16_HA, dtpBiasedVal}; 247 case R_PPC_DTPREL16_HI: 248 return {R_PPC_ADDR16_HI, dtpBiasedVal}; 249 case R_PPC_DTPREL16_LO: 250 return {R_PPC_ADDR16_LO, dtpBiasedVal}; 251 case R_PPC_DTPREL32: 252 return {R_PPC_ADDR32, dtpBiasedVal}; 253 default: 254 return {type, val}; 255 } 256 } 257 258 void PPC::relocateOne(uint8_t *loc, RelType type, uint64_t val) const { 259 RelType newType; 260 std::tie(newType, val) = fromDTPREL(type, val); 261 switch (newType) { 262 case R_PPC_ADDR16: 263 checkIntUInt(loc, val, 16, type); 264 write16(loc, val); 265 break; 266 case R_PPC_GOT16: 267 case R_PPC_GOT_TLSGD16: 268 case R_PPC_GOT_TLSLD16: 269 case R_PPC_GOT_TPREL16: 270 case R_PPC_TPREL16: 271 checkInt(loc, val, 16, type); 272 write16(loc, val); 273 break; 274 case R_PPC_ADDR16_HA: 275 case R_PPC_DTPREL16_HA: 276 case R_PPC_GOT_TLSGD16_HA: 277 case R_PPC_GOT_TLSLD16_HA: 278 case R_PPC_GOT_TPREL16_HA: 279 case R_PPC_REL16_HA: 280 case R_PPC_TPREL16_HA: 281 write16(loc, ha(val)); 282 break; 283 case R_PPC_ADDR16_HI: 284 case R_PPC_DTPREL16_HI: 285 case R_PPC_GOT_TLSGD16_HI: 286 case R_PPC_GOT_TLSLD16_HI: 287 case R_PPC_GOT_TPREL16_HI: 288 case R_PPC_REL16_HI: 289 case R_PPC_TPREL16_HI: 290 write16(loc, val >> 16); 291 break; 292 case R_PPC_ADDR16_LO: 293 case R_PPC_DTPREL16_LO: 294 case R_PPC_GOT_TLSGD16_LO: 295 case R_PPC_GOT_TLSLD16_LO: 296 case R_PPC_GOT_TPREL16_LO: 297 case R_PPC_REL16_LO: 298 case R_PPC_TPREL16_LO: 299 write16(loc, val); 300 break; 301 case R_PPC_ADDR32: 302 case R_PPC_REL32: 303 write32(loc, val); 304 break; 305 case R_PPC_REL14: { 306 uint32_t mask = 0x0000FFFC; 307 checkInt(loc, val, 16, type); 308 checkAlignment(loc, val, 4, type); 309 write32(loc, (read32(loc) & ~mask) | (val & mask)); 310 break; 311 } 312 case R_PPC_REL24: 313 case R_PPC_LOCAL24PC: 314 case R_PPC_PLTREL24: { 315 uint32_t mask = 0x03FFFFFC; 316 checkInt(loc, val, 26, type); 317 checkAlignment(loc, val, 4, type); 318 write32(loc, (read32(loc) & ~mask) | (val & mask)); 319 break; 320 } 321 default: 322 error(getErrorLocation(loc) + "unrecognized relocation " + toString(type)); 323 } 324 } 325 326 RelExpr PPC::adjustRelaxExpr(RelType type, const uint8_t *data, 327 RelExpr expr) const { 328 if (expr == R_RELAX_TLS_GD_TO_IE) 329 return R_RELAX_TLS_GD_TO_IE_GOT_OFF; 330 if (expr == R_RELAX_TLS_LD_TO_LE) 331 return R_RELAX_TLS_LD_TO_LE_ABS; 332 return expr; 333 } 334 335 int PPC::getTlsGdRelaxSkip(RelType type) const { 336 // A __tls_get_addr call instruction is marked with 2 relocations: 337 // 338 // R_PPC_TLSGD / R_PPC_TLSLD: marker relocation 339 // R_PPC_REL24: __tls_get_addr 340 // 341 // After the relaxation we no longer call __tls_get_addr and should skip both 342 // relocations to not create a false dependence on __tls_get_addr being 343 // defined. 344 if (type == R_PPC_TLSGD || type == R_PPC_TLSLD) 345 return 2; 346 return 1; 347 } 348 349 void PPC::relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const { 350 switch (type) { 351 case R_PPC_GOT_TLSGD16: { 352 // addi rT, rA, x@got@tlsgd --> lwz rT, x@got@tprel(rA) 353 uint32_t insn = readFromHalf16(loc); 354 writeFromHalf16(loc, 0x80000000 | (insn & 0x03ff0000)); 355 relocateOne(loc, R_PPC_GOT_TPREL16, val); 356 break; 357 } 358 case R_PPC_TLSGD: 359 // bl __tls_get_addr(x@tldgd) --> add r3, r3, r2 360 write32(loc, 0x7c631214); 361 break; 362 default: 363 llvm_unreachable("unsupported relocation for TLS GD to IE relaxation"); 364 } 365 } 366 367 void PPC::relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const { 368 switch (type) { 369 case R_PPC_GOT_TLSGD16: 370 // addi r3, r31, x@got@tlsgd --> addis r3, r2, x@tprel@ha 371 writeFromHalf16(loc, 0x3c620000 | ha(val)); 372 break; 373 case R_PPC_TLSGD: 374 // bl __tls_get_addr(x@tldgd) --> add r3, r3, x@tprel@l 375 write32(loc, 0x38630000 | lo(val)); 376 break; 377 default: 378 llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 379 } 380 } 381 382 void PPC::relaxTlsLdToLe(uint8_t *loc, RelType type, uint64_t val) const { 383 switch (type) { 384 case R_PPC_GOT_TLSLD16: 385 // addi r3, rA, x@got@tlsgd --> addis r3, r2, 0 386 writeFromHalf16(loc, 0x3c620000); 387 break; 388 case R_PPC_TLSLD: 389 // r3+x@dtprel computes r3+x-0x8000, while we want it to compute r3+x@tprel 390 // = r3+x-0x7000, so add 4096 to r3. 391 // bl __tls_get_addr(x@tlsld) --> addi r3, r3, 4096 392 write32(loc, 0x38631000); 393 break; 394 case R_PPC_DTPREL16: 395 case R_PPC_DTPREL16_HA: 396 case R_PPC_DTPREL16_HI: 397 case R_PPC_DTPREL16_LO: 398 relocateOne(loc, type, val); 399 break; 400 default: 401 llvm_unreachable("unsupported relocation for TLS LD to LE relaxation"); 402 } 403 } 404 405 void PPC::relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const { 406 switch (type) { 407 case R_PPC_GOT_TPREL16: { 408 // lwz rT, x@got@tprel(rA) --> addis rT, r2, x@tprel@ha 409 uint32_t rt = readFromHalf16(loc) & 0x03e00000; 410 writeFromHalf16(loc, 0x3c020000 | rt | ha(val)); 411 break; 412 } 413 case R_PPC_TLS: { 414 uint32_t insn = read32(loc); 415 if (insn >> 26 != 31) 416 error("unrecognized instruction for IE to LE R_PPC_TLS"); 417 // addi rT, rT, x@tls --> addi rT, rT, x@tprel@l 418 uint32_t dFormOp = getPPCDFormOp((read32(loc) & 0x000007fe) >> 1); 419 if (dFormOp == 0) 420 error("unrecognized instruction for IE to LE R_PPC_TLS"); 421 write32(loc, (dFormOp << 26) | (insn & 0x03ff0000) | lo(val)); 422 break; 423 } 424 default: 425 llvm_unreachable("unsupported relocation for TLS IE to LE relaxation"); 426 } 427 } 428 429 TargetInfo *elf::getPPCTargetInfo() { 430 static PPC target; 431 return ⌖ 432 } 433