10b57cec5SDimitry Andric //===- PPC.cpp ------------------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "OutputSections.h" 100b57cec5SDimitry Andric #include "Symbols.h" 110b57cec5SDimitry Andric #include "SyntheticSections.h" 120b57cec5SDimitry Andric #include "Target.h" 13480093f4SDimitry Andric #include "Thunks.h" 140b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h" 150b57cec5SDimitry Andric #include "llvm/Support/Endian.h" 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric using namespace llvm; 180b57cec5SDimitry Andric using namespace llvm::support::endian; 190b57cec5SDimitry Andric using namespace llvm::ELF; 2085868e8aSDimitry Andric 2185868e8aSDimitry Andric namespace lld { 2285868e8aSDimitry Andric namespace elf { 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric namespace { 250b57cec5SDimitry Andric class PPC final : public TargetInfo { 260b57cec5SDimitry Andric public: 270b57cec5SDimitry Andric PPC(); 280b57cec5SDimitry Andric RelExpr getRelExpr(RelType type, const Symbol &s, 290b57cec5SDimitry Andric const uint8_t *loc) const override; 300b57cec5SDimitry Andric RelType getDynRel(RelType type) const override; 310b57cec5SDimitry Andric void writeGotHeader(uint8_t *buf) const override; 320b57cec5SDimitry Andric void writePltHeader(uint8_t *buf) const override { 330b57cec5SDimitry Andric llvm_unreachable("should call writePPC32GlinkSection() instead"); 340b57cec5SDimitry Andric } 35480093f4SDimitry Andric void writePlt(uint8_t *buf, const Symbol &sym, 36480093f4SDimitry Andric uint64_t pltEntryAddr) const override { 370b57cec5SDimitry Andric llvm_unreachable("should call writePPC32GlinkSection() instead"); 380b57cec5SDimitry Andric } 39480093f4SDimitry Andric void writeIplt(uint8_t *buf, const Symbol &sym, 40480093f4SDimitry Andric uint64_t pltEntryAddr) const override; 410b57cec5SDimitry Andric void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 420b57cec5SDimitry Andric bool needsThunk(RelExpr expr, RelType relocType, const InputFile *file, 43480093f4SDimitry Andric uint64_t branchAddr, const Symbol &s, 44480093f4SDimitry Andric int64_t a) const override; 450b57cec5SDimitry Andric uint32_t getThunkSectionSpacing() const override; 460b57cec5SDimitry Andric bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override; 470b57cec5SDimitry Andric void relocateOne(uint8_t *loc, RelType type, uint64_t val) const override; 480b57cec5SDimitry Andric RelExpr adjustRelaxExpr(RelType type, const uint8_t *data, 490b57cec5SDimitry Andric RelExpr expr) const override; 500b57cec5SDimitry Andric int getTlsGdRelaxSkip(RelType type) const override; 510b57cec5SDimitry Andric void relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const override; 520b57cec5SDimitry Andric void relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const override; 530b57cec5SDimitry Andric void relaxTlsLdToLe(uint8_t *loc, RelType type, uint64_t val) const override; 540b57cec5SDimitry Andric void relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const override; 550b57cec5SDimitry Andric }; 560b57cec5SDimitry Andric } // namespace 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric static uint16_t lo(uint32_t v) { return v; } 590b57cec5SDimitry Andric static uint16_t ha(uint32_t v) { return (v + 0x8000) >> 16; } 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric static uint32_t readFromHalf16(const uint8_t *loc) { 620b57cec5SDimitry Andric return read32(config->isLE ? loc : loc - 2); 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric static void writeFromHalf16(uint8_t *loc, uint32_t insn) { 660b57cec5SDimitry Andric write32(config->isLE ? loc : loc - 2, insn); 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric 6985868e8aSDimitry Andric void writePPC32GlinkSection(uint8_t *buf, size_t numEntries) { 700b57cec5SDimitry Andric // On PPC Secure PLT ABI, bl foo@plt jumps to a call stub, which loads an 710b57cec5SDimitry Andric // absolute address from a specific .plt slot (usually called .got.plt on 720b57cec5SDimitry Andric // other targets) and jumps there. 730b57cec5SDimitry Andric // 740b57cec5SDimitry Andric // a) With immediate binding (BIND_NOW), the .plt entry is resolved at load 750b57cec5SDimitry Andric // time. The .glink section is not used. 760b57cec5SDimitry Andric // b) With lazy binding, the .plt entry points to a `b PLTresolve` 770b57cec5SDimitry Andric // instruction in .glink, filled in by PPC::writeGotPlt(). 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric // Write N `b PLTresolve` first. 800b57cec5SDimitry Andric for (size_t i = 0; i != numEntries; ++i) 810b57cec5SDimitry Andric write32(buf + 4 * i, 0x48000000 | 4 * (numEntries - i)); 820b57cec5SDimitry Andric buf += 4 * numEntries; 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric // Then write PLTresolve(), which has two forms: PIC and non-PIC. PLTresolve() 850b57cec5SDimitry Andric // computes the PLT index (by computing the distance from the landing b to 860b57cec5SDimitry Andric // itself) and calls _dl_runtime_resolve() (in glibc). 870b57cec5SDimitry Andric uint32_t got = in.got->getVA(); 880b57cec5SDimitry Andric uint32_t glink = in.plt->getVA(); // VA of .glink 890b57cec5SDimitry Andric const uint8_t *end = buf + 64; 900b57cec5SDimitry Andric if (config->isPic) { 910b57cec5SDimitry Andric uint32_t afterBcl = in.plt->getSize() - target->pltHeaderSize + 12; 920b57cec5SDimitry Andric uint32_t gotBcl = got + 4 - (glink + afterBcl); 930b57cec5SDimitry Andric write32(buf + 0, 0x3d6b0000 | ha(afterBcl)); // addis r11,r11,1f-glink@ha 940b57cec5SDimitry Andric write32(buf + 4, 0x7c0802a6); // mflr r0 950b57cec5SDimitry Andric write32(buf + 8, 0x429f0005); // bcl 20,30,.+4 960b57cec5SDimitry Andric write32(buf + 12, 0x396b0000 | lo(afterBcl)); // 1: addi r11,r11,1b-.glink@l 970b57cec5SDimitry Andric write32(buf + 16, 0x7d8802a6); // mflr r12 980b57cec5SDimitry Andric write32(buf + 20, 0x7c0803a6); // mtlr r0 990b57cec5SDimitry Andric write32(buf + 24, 0x7d6c5850); // sub r11,r11,r12 1000b57cec5SDimitry Andric write32(buf + 28, 0x3d8c0000 | ha(gotBcl)); // addis 12,12,GOT+4-1b@ha 1010b57cec5SDimitry Andric if (ha(gotBcl) == ha(gotBcl + 4)) { 1020b57cec5SDimitry Andric write32(buf + 32, 0x800c0000 | lo(gotBcl)); // lwz r0,r12,GOT+4-1b@l(r12) 1030b57cec5SDimitry Andric write32(buf + 36, 1040b57cec5SDimitry Andric 0x818c0000 | lo(gotBcl + 4)); // lwz r12,r12,GOT+8-1b@l(r12) 1050b57cec5SDimitry Andric } else { 1060b57cec5SDimitry Andric write32(buf + 32, 0x840c0000 | lo(gotBcl)); // lwzu r0,r12,GOT+4-1b@l(r12) 1070b57cec5SDimitry Andric write32(buf + 36, 0x818c0000 | 4); // lwz r12,r12,4(r12) 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric write32(buf + 40, 0x7c0903a6); // mtctr 0 1100b57cec5SDimitry Andric write32(buf + 44, 0x7c0b5a14); // add r0,11,11 1110b57cec5SDimitry Andric write32(buf + 48, 0x7d605a14); // add r11,0,11 1120b57cec5SDimitry Andric write32(buf + 52, 0x4e800420); // bctr 1130b57cec5SDimitry Andric buf += 56; 1140b57cec5SDimitry Andric } else { 1150b57cec5SDimitry Andric write32(buf + 0, 0x3d800000 | ha(got + 4)); // lis r12,GOT+4@ha 1160b57cec5SDimitry Andric write32(buf + 4, 0x3d6b0000 | ha(-glink)); // addis r11,r11,-Glink@ha 1170b57cec5SDimitry Andric if (ha(got + 4) == ha(got + 8)) 1180b57cec5SDimitry Andric write32(buf + 8, 0x800c0000 | lo(got + 4)); // lwz r0,GOT+4@l(r12) 1190b57cec5SDimitry Andric else 1200b57cec5SDimitry Andric write32(buf + 8, 0x840c0000 | lo(got + 4)); // lwzu r0,GOT+4@l(r12) 1210b57cec5SDimitry Andric write32(buf + 12, 0x396b0000 | lo(-glink)); // addi r11,r11,-Glink@l 1220b57cec5SDimitry Andric write32(buf + 16, 0x7c0903a6); // mtctr r0 1230b57cec5SDimitry Andric write32(buf + 20, 0x7c0b5a14); // add r0,r11,r11 1240b57cec5SDimitry Andric if (ha(got + 4) == ha(got + 8)) 1250b57cec5SDimitry Andric write32(buf + 24, 0x818c0000 | lo(got + 8)); // lwz r12,GOT+8@ha(r12) 1260b57cec5SDimitry Andric else 1270b57cec5SDimitry Andric write32(buf + 24, 0x818c0000 | 4); // lwz r12,4(r12) 1280b57cec5SDimitry Andric write32(buf + 28, 0x7d605a14); // add r11,r0,r11 1290b57cec5SDimitry Andric write32(buf + 32, 0x4e800420); // bctr 1300b57cec5SDimitry Andric buf += 36; 1310b57cec5SDimitry Andric } 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric // Pad with nop. They should not be executed. 1340b57cec5SDimitry Andric for (; buf < end; buf += 4) 1350b57cec5SDimitry Andric write32(buf, 0x60000000); 1360b57cec5SDimitry Andric } 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric PPC::PPC() { 139*55e4f9d5SDimitry Andric copyRel = R_PPC_COPY; 1400b57cec5SDimitry Andric gotRel = R_PPC_GLOB_DAT; 1410b57cec5SDimitry Andric noneRel = R_PPC_NONE; 1420b57cec5SDimitry Andric pltRel = R_PPC_JMP_SLOT; 1430b57cec5SDimitry Andric relativeRel = R_PPC_RELATIVE; 1440b57cec5SDimitry Andric iRelativeRel = R_PPC_IRELATIVE; 1450b57cec5SDimitry Andric symbolicRel = R_PPC_ADDR32; 1460b57cec5SDimitry Andric gotBaseSymInGotPlt = false; 1470b57cec5SDimitry Andric gotHeaderEntriesNum = 3; 1480b57cec5SDimitry Andric gotPltHeaderEntriesNum = 0; 1490b57cec5SDimitry Andric pltHeaderSize = 64; // size of PLTresolve in .glink 1500b57cec5SDimitry Andric pltEntrySize = 4; 151480093f4SDimitry Andric ipltEntrySize = 16; 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric needsThunks = true; 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric tlsModuleIndexRel = R_PPC_DTPMOD32; 1560b57cec5SDimitry Andric tlsOffsetRel = R_PPC_DTPREL32; 1570b57cec5SDimitry Andric tlsGotRel = R_PPC_TPREL32; 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric defaultMaxPageSize = 65536; 1600b57cec5SDimitry Andric defaultImageBase = 0x10000000; 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric write32(trapInstr.data(), 0x7fe00008); 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric 165480093f4SDimitry Andric void PPC::writeIplt(uint8_t *buf, const Symbol &sym, 166480093f4SDimitry Andric uint64_t /*pltEntryAddr*/) const { 167480093f4SDimitry Andric // In -pie or -shared mode, assume r30 points to .got2+0x8000, and use a 168480093f4SDimitry Andric // .got2.plt_pic32. thunk. 169480093f4SDimitry Andric writePPC32PltCallStub(buf, sym.getGotPltVA(), sym.file, 0x8000); 170480093f4SDimitry Andric } 171480093f4SDimitry Andric 1720b57cec5SDimitry Andric void PPC::writeGotHeader(uint8_t *buf) const { 1730b57cec5SDimitry Andric // _GLOBAL_OFFSET_TABLE_[0] = _DYNAMIC 1740b57cec5SDimitry Andric // glibc stores _dl_runtime_resolve in _GLOBAL_OFFSET_TABLE_[1], 1750b57cec5SDimitry Andric // link_map in _GLOBAL_OFFSET_TABLE_[2]. 1760b57cec5SDimitry Andric write32(buf, mainPart->dynamic->getVA()); 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric void PPC::writeGotPlt(uint8_t *buf, const Symbol &s) const { 1800b57cec5SDimitry Andric // Address of the symbol resolver stub in .glink . 1810b57cec5SDimitry Andric write32(buf, in.plt->getVA() + 4 * s.pltIndex); 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric bool PPC::needsThunk(RelExpr expr, RelType type, const InputFile *file, 185480093f4SDimitry Andric uint64_t branchAddr, const Symbol &s, int64_t /*a*/) const { 1860b57cec5SDimitry Andric if (type != R_PPC_REL24 && type != R_PPC_PLTREL24) 1870b57cec5SDimitry Andric return false; 1880b57cec5SDimitry Andric if (s.isInPlt()) 1890b57cec5SDimitry Andric return true; 1900b57cec5SDimitry Andric if (s.isUndefWeak()) 1910b57cec5SDimitry Andric return false; 1920b57cec5SDimitry Andric return !(expr == R_PC && PPC::inBranchRange(type, branchAddr, s.getVA())); 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric uint32_t PPC::getThunkSectionSpacing() const { return 0x2000000; } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric bool PPC::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 1980b57cec5SDimitry Andric uint64_t offset = dst - src; 1990b57cec5SDimitry Andric if (type == R_PPC_REL24 || type == R_PPC_PLTREL24) 2000b57cec5SDimitry Andric return isInt<26>(offset); 2010b57cec5SDimitry Andric llvm_unreachable("unsupported relocation type used in branch"); 2020b57cec5SDimitry Andric } 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric RelExpr PPC::getRelExpr(RelType type, const Symbol &s, 2050b57cec5SDimitry Andric const uint8_t *loc) const { 2060b57cec5SDimitry Andric switch (type) { 2070b57cec5SDimitry Andric case R_PPC_NONE: 2080b57cec5SDimitry Andric return R_NONE; 2090b57cec5SDimitry Andric case R_PPC_ADDR16_HA: 2100b57cec5SDimitry Andric case R_PPC_ADDR16_HI: 2110b57cec5SDimitry Andric case R_PPC_ADDR16_LO: 2120b57cec5SDimitry Andric case R_PPC_ADDR32: 2130b57cec5SDimitry Andric return R_ABS; 2140b57cec5SDimitry Andric case R_PPC_DTPREL16: 2150b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 2160b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 2170b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 2180b57cec5SDimitry Andric case R_PPC_DTPREL32: 2190b57cec5SDimitry Andric return R_DTPREL; 2200b57cec5SDimitry Andric case R_PPC_REL14: 2210b57cec5SDimitry Andric case R_PPC_REL32: 2220b57cec5SDimitry Andric case R_PPC_LOCAL24PC: 2230b57cec5SDimitry Andric case R_PPC_REL16_LO: 2240b57cec5SDimitry Andric case R_PPC_REL16_HI: 2250b57cec5SDimitry Andric case R_PPC_REL16_HA: 2260b57cec5SDimitry Andric return R_PC; 2270b57cec5SDimitry Andric case R_PPC_GOT16: 2280b57cec5SDimitry Andric return R_GOT_OFF; 2290b57cec5SDimitry Andric case R_PPC_REL24: 2300b57cec5SDimitry Andric return R_PLT_PC; 2310b57cec5SDimitry Andric case R_PPC_PLTREL24: 2320b57cec5SDimitry Andric return R_PPC32_PLTREL; 2330b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: 2340b57cec5SDimitry Andric return R_TLSGD_GOT; 2350b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16: 2360b57cec5SDimitry Andric return R_TLSLD_GOT; 2370b57cec5SDimitry Andric case R_PPC_GOT_TPREL16: 2380b57cec5SDimitry Andric return R_GOT_OFF; 2390b57cec5SDimitry Andric case R_PPC_TLS: 2400b57cec5SDimitry Andric return R_TLSIE_HINT; 2410b57cec5SDimitry Andric case R_PPC_TLSGD: 2420b57cec5SDimitry Andric return R_TLSDESC_CALL; 2430b57cec5SDimitry Andric case R_PPC_TLSLD: 2440b57cec5SDimitry Andric return R_TLSLD_HINT; 2450b57cec5SDimitry Andric case R_PPC_TPREL16: 2460b57cec5SDimitry Andric case R_PPC_TPREL16_HA: 2470b57cec5SDimitry Andric case R_PPC_TPREL16_LO: 2480b57cec5SDimitry Andric case R_PPC_TPREL16_HI: 2490b57cec5SDimitry Andric return R_TLS; 2500b57cec5SDimitry Andric default: 2510b57cec5SDimitry Andric error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 2520b57cec5SDimitry Andric ") against symbol " + toString(s)); 2530b57cec5SDimitry Andric return R_NONE; 2540b57cec5SDimitry Andric } 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric RelType PPC::getDynRel(RelType type) const { 2580b57cec5SDimitry Andric if (type == R_PPC_ADDR32) 2590b57cec5SDimitry Andric return type; 2600b57cec5SDimitry Andric return R_PPC_NONE; 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric static std::pair<RelType, uint64_t> fromDTPREL(RelType type, uint64_t val) { 2640b57cec5SDimitry Andric uint64_t dtpBiasedVal = val - 0x8000; 2650b57cec5SDimitry Andric switch (type) { 2660b57cec5SDimitry Andric case R_PPC_DTPREL16: 2670b57cec5SDimitry Andric return {R_PPC64_ADDR16, dtpBiasedVal}; 2680b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 2690b57cec5SDimitry Andric return {R_PPC_ADDR16_HA, dtpBiasedVal}; 2700b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 2710b57cec5SDimitry Andric return {R_PPC_ADDR16_HI, dtpBiasedVal}; 2720b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 2730b57cec5SDimitry Andric return {R_PPC_ADDR16_LO, dtpBiasedVal}; 2740b57cec5SDimitry Andric case R_PPC_DTPREL32: 2750b57cec5SDimitry Andric return {R_PPC_ADDR32, dtpBiasedVal}; 2760b57cec5SDimitry Andric default: 2770b57cec5SDimitry Andric return {type, val}; 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric void PPC::relocateOne(uint8_t *loc, RelType type, uint64_t val) const { 2820b57cec5SDimitry Andric RelType newType; 2830b57cec5SDimitry Andric std::tie(newType, val) = fromDTPREL(type, val); 2840b57cec5SDimitry Andric switch (newType) { 2850b57cec5SDimitry Andric case R_PPC_ADDR16: 2860b57cec5SDimitry Andric checkIntUInt(loc, val, 16, type); 2870b57cec5SDimitry Andric write16(loc, val); 2880b57cec5SDimitry Andric break; 2890b57cec5SDimitry Andric case R_PPC_GOT16: 2900b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: 2910b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16: 2920b57cec5SDimitry Andric case R_PPC_GOT_TPREL16: 2930b57cec5SDimitry Andric case R_PPC_TPREL16: 2940b57cec5SDimitry Andric checkInt(loc, val, 16, type); 2950b57cec5SDimitry Andric write16(loc, val); 2960b57cec5SDimitry Andric break; 2970b57cec5SDimitry Andric case R_PPC_ADDR16_HA: 2980b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 2990b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16_HA: 3000b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16_HA: 3010b57cec5SDimitry Andric case R_PPC_GOT_TPREL16_HA: 3020b57cec5SDimitry Andric case R_PPC_REL16_HA: 3030b57cec5SDimitry Andric case R_PPC_TPREL16_HA: 3040b57cec5SDimitry Andric write16(loc, ha(val)); 3050b57cec5SDimitry Andric break; 3060b57cec5SDimitry Andric case R_PPC_ADDR16_HI: 3070b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 3080b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16_HI: 3090b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16_HI: 3100b57cec5SDimitry Andric case R_PPC_GOT_TPREL16_HI: 3110b57cec5SDimitry Andric case R_PPC_REL16_HI: 3120b57cec5SDimitry Andric case R_PPC_TPREL16_HI: 3130b57cec5SDimitry Andric write16(loc, val >> 16); 3140b57cec5SDimitry Andric break; 3150b57cec5SDimitry Andric case R_PPC_ADDR16_LO: 3160b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 3170b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16_LO: 3180b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16_LO: 3190b57cec5SDimitry Andric case R_PPC_GOT_TPREL16_LO: 3200b57cec5SDimitry Andric case R_PPC_REL16_LO: 3210b57cec5SDimitry Andric case R_PPC_TPREL16_LO: 3220b57cec5SDimitry Andric write16(loc, val); 3230b57cec5SDimitry Andric break; 3240b57cec5SDimitry Andric case R_PPC_ADDR32: 3250b57cec5SDimitry Andric case R_PPC_REL32: 3260b57cec5SDimitry Andric write32(loc, val); 3270b57cec5SDimitry Andric break; 3280b57cec5SDimitry Andric case R_PPC_REL14: { 3290b57cec5SDimitry Andric uint32_t mask = 0x0000FFFC; 3300b57cec5SDimitry Andric checkInt(loc, val, 16, type); 3310b57cec5SDimitry Andric checkAlignment(loc, val, 4, type); 3320b57cec5SDimitry Andric write32(loc, (read32(loc) & ~mask) | (val & mask)); 3330b57cec5SDimitry Andric break; 3340b57cec5SDimitry Andric } 3350b57cec5SDimitry Andric case R_PPC_REL24: 3360b57cec5SDimitry Andric case R_PPC_LOCAL24PC: 3370b57cec5SDimitry Andric case R_PPC_PLTREL24: { 3380b57cec5SDimitry Andric uint32_t mask = 0x03FFFFFC; 3390b57cec5SDimitry Andric checkInt(loc, val, 26, type); 3400b57cec5SDimitry Andric checkAlignment(loc, val, 4, type); 3410b57cec5SDimitry Andric write32(loc, (read32(loc) & ~mask) | (val & mask)); 3420b57cec5SDimitry Andric break; 3430b57cec5SDimitry Andric } 3440b57cec5SDimitry Andric default: 3450b57cec5SDimitry Andric llvm_unreachable("unknown relocation"); 3460b57cec5SDimitry Andric } 3470b57cec5SDimitry Andric } 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric RelExpr PPC::adjustRelaxExpr(RelType type, const uint8_t *data, 3500b57cec5SDimitry Andric RelExpr expr) const { 3510b57cec5SDimitry Andric if (expr == R_RELAX_TLS_GD_TO_IE) 3520b57cec5SDimitry Andric return R_RELAX_TLS_GD_TO_IE_GOT_OFF; 3530b57cec5SDimitry Andric if (expr == R_RELAX_TLS_LD_TO_LE) 3540b57cec5SDimitry Andric return R_RELAX_TLS_LD_TO_LE_ABS; 3550b57cec5SDimitry Andric return expr; 3560b57cec5SDimitry Andric } 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric int PPC::getTlsGdRelaxSkip(RelType type) const { 3590b57cec5SDimitry Andric // A __tls_get_addr call instruction is marked with 2 relocations: 3600b57cec5SDimitry Andric // 3610b57cec5SDimitry Andric // R_PPC_TLSGD / R_PPC_TLSLD: marker relocation 3620b57cec5SDimitry Andric // R_PPC_REL24: __tls_get_addr 3630b57cec5SDimitry Andric // 3640b57cec5SDimitry Andric // After the relaxation we no longer call __tls_get_addr and should skip both 3650b57cec5SDimitry Andric // relocations to not create a false dependence on __tls_get_addr being 3660b57cec5SDimitry Andric // defined. 3670b57cec5SDimitry Andric if (type == R_PPC_TLSGD || type == R_PPC_TLSLD) 3680b57cec5SDimitry Andric return 2; 3690b57cec5SDimitry Andric return 1; 3700b57cec5SDimitry Andric } 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric void PPC::relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const { 3730b57cec5SDimitry Andric switch (type) { 3740b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: { 3750b57cec5SDimitry Andric // addi rT, rA, x@got@tlsgd --> lwz rT, x@got@tprel(rA) 3760b57cec5SDimitry Andric uint32_t insn = readFromHalf16(loc); 3770b57cec5SDimitry Andric writeFromHalf16(loc, 0x80000000 | (insn & 0x03ff0000)); 3780b57cec5SDimitry Andric relocateOne(loc, R_PPC_GOT_TPREL16, val); 3790b57cec5SDimitry Andric break; 3800b57cec5SDimitry Andric } 3810b57cec5SDimitry Andric case R_PPC_TLSGD: 3820b57cec5SDimitry Andric // bl __tls_get_addr(x@tldgd) --> add r3, r3, r2 3830b57cec5SDimitry Andric write32(loc, 0x7c631214); 3840b57cec5SDimitry Andric break; 3850b57cec5SDimitry Andric default: 3860b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to IE relaxation"); 3870b57cec5SDimitry Andric } 3880b57cec5SDimitry Andric } 3890b57cec5SDimitry Andric 3900b57cec5SDimitry Andric void PPC::relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const { 3910b57cec5SDimitry Andric switch (type) { 3920b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: 3930b57cec5SDimitry Andric // addi r3, r31, x@got@tlsgd --> addis r3, r2, x@tprel@ha 3940b57cec5SDimitry Andric writeFromHalf16(loc, 0x3c620000 | ha(val)); 3950b57cec5SDimitry Andric break; 3960b57cec5SDimitry Andric case R_PPC_TLSGD: 3970b57cec5SDimitry Andric // bl __tls_get_addr(x@tldgd) --> add r3, r3, x@tprel@l 3980b57cec5SDimitry Andric write32(loc, 0x38630000 | lo(val)); 3990b57cec5SDimitry Andric break; 4000b57cec5SDimitry Andric default: 4010b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 4020b57cec5SDimitry Andric } 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric void PPC::relaxTlsLdToLe(uint8_t *loc, RelType type, uint64_t val) const { 4060b57cec5SDimitry Andric switch (type) { 4070b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16: 4080b57cec5SDimitry Andric // addi r3, rA, x@got@tlsgd --> addis r3, r2, 0 4090b57cec5SDimitry Andric writeFromHalf16(loc, 0x3c620000); 4100b57cec5SDimitry Andric break; 4110b57cec5SDimitry Andric case R_PPC_TLSLD: 4120b57cec5SDimitry Andric // r3+x@dtprel computes r3+x-0x8000, while we want it to compute r3+x@tprel 4130b57cec5SDimitry Andric // = r3+x-0x7000, so add 4096 to r3. 4140b57cec5SDimitry Andric // bl __tls_get_addr(x@tlsld) --> addi r3, r3, 4096 4150b57cec5SDimitry Andric write32(loc, 0x38631000); 4160b57cec5SDimitry Andric break; 4170b57cec5SDimitry Andric case R_PPC_DTPREL16: 4180b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 4190b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 4200b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 4210b57cec5SDimitry Andric relocateOne(loc, type, val); 4220b57cec5SDimitry Andric break; 4230b57cec5SDimitry Andric default: 4240b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS LD to LE relaxation"); 4250b57cec5SDimitry Andric } 4260b57cec5SDimitry Andric } 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric void PPC::relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const { 4290b57cec5SDimitry Andric switch (type) { 4300b57cec5SDimitry Andric case R_PPC_GOT_TPREL16: { 4310b57cec5SDimitry Andric // lwz rT, x@got@tprel(rA) --> addis rT, r2, x@tprel@ha 4320b57cec5SDimitry Andric uint32_t rt = readFromHalf16(loc) & 0x03e00000; 4330b57cec5SDimitry Andric writeFromHalf16(loc, 0x3c020000 | rt | ha(val)); 4340b57cec5SDimitry Andric break; 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric case R_PPC_TLS: { 4370b57cec5SDimitry Andric uint32_t insn = read32(loc); 4380b57cec5SDimitry Andric if (insn >> 26 != 31) 4390b57cec5SDimitry Andric error("unrecognized instruction for IE to LE R_PPC_TLS"); 4400b57cec5SDimitry Andric // addi rT, rT, x@tls --> addi rT, rT, x@tprel@l 4410b57cec5SDimitry Andric uint32_t dFormOp = getPPCDFormOp((read32(loc) & 0x000007fe) >> 1); 4420b57cec5SDimitry Andric if (dFormOp == 0) 4430b57cec5SDimitry Andric error("unrecognized instruction for IE to LE R_PPC_TLS"); 4440b57cec5SDimitry Andric write32(loc, (dFormOp << 26) | (insn & 0x03ff0000) | lo(val)); 4450b57cec5SDimitry Andric break; 4460b57cec5SDimitry Andric } 4470b57cec5SDimitry Andric default: 4480b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS IE to LE relaxation"); 4490b57cec5SDimitry Andric } 4500b57cec5SDimitry Andric } 4510b57cec5SDimitry Andric 45285868e8aSDimitry Andric TargetInfo *getPPCTargetInfo() { 4530b57cec5SDimitry Andric static PPC target; 4540b57cec5SDimitry Andric return ⌖ 4550b57cec5SDimitry Andric } 45685868e8aSDimitry Andric 45785868e8aSDimitry Andric } // namespace elf 45885868e8aSDimitry Andric } // namespace lld 459