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; 205ffd83dbSDimitry Andric using namespace lld; 215ffd83dbSDimitry Andric using namespace lld::elf; 220b57cec5SDimitry Andric 230eae32dcSDimitry Andric // Undefine the macro predefined by GCC powerpc32. 240eae32dcSDimitry Andric #undef PPC 250eae32dcSDimitry Andric 260b57cec5SDimitry Andric namespace { 270b57cec5SDimitry Andric class PPC final : public TargetInfo { 280b57cec5SDimitry Andric public: 290b57cec5SDimitry Andric PPC(); 300b57cec5SDimitry Andric RelExpr getRelExpr(RelType type, const Symbol &s, 310b57cec5SDimitry Andric const uint8_t *loc) const override; 320b57cec5SDimitry Andric RelType getDynRel(RelType type) const override; 33298c3e8dSDimitry Andric int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override; 340b57cec5SDimitry Andric void writeGotHeader(uint8_t *buf) const override; 350b57cec5SDimitry Andric void writePltHeader(uint8_t *buf) const override { 360b57cec5SDimitry Andric llvm_unreachable("should call writePPC32GlinkSection() instead"); 370b57cec5SDimitry Andric } 38480093f4SDimitry Andric void writePlt(uint8_t *buf, const Symbol &sym, 39480093f4SDimitry Andric uint64_t pltEntryAddr) const override { 400b57cec5SDimitry Andric llvm_unreachable("should call writePPC32GlinkSection() instead"); 410b57cec5SDimitry Andric } 42480093f4SDimitry Andric void writeIplt(uint8_t *buf, const Symbol &sym, 43480093f4SDimitry Andric uint64_t pltEntryAddr) const override; 440b57cec5SDimitry Andric void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 450b57cec5SDimitry Andric bool needsThunk(RelExpr expr, RelType relocType, const InputFile *file, 46480093f4SDimitry Andric uint64_t branchAddr, const Symbol &s, 47480093f4SDimitry Andric int64_t a) const override; 480b57cec5SDimitry Andric uint32_t getThunkSectionSpacing() const override; 490b57cec5SDimitry Andric bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override; 505ffd83dbSDimitry Andric void relocate(uint8_t *loc, const Relocation &rel, 515ffd83dbSDimitry Andric uint64_t val) const override; 52e8d8bef9SDimitry Andric RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override; 530b57cec5SDimitry Andric int getTlsGdRelaxSkip(RelType type) const override; 54*bdd1243dSDimitry Andric void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override; 55*bdd1243dSDimitry Andric 56*bdd1243dSDimitry Andric private: 57*bdd1243dSDimitry Andric void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const; 58*bdd1243dSDimitry Andric void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const; 59*bdd1243dSDimitry Andric void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const; 60*bdd1243dSDimitry Andric void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const; 610b57cec5SDimitry Andric }; 620b57cec5SDimitry Andric } // namespace 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric static uint16_t lo(uint32_t v) { return v; } 650b57cec5SDimitry Andric static uint16_t ha(uint32_t v) { return (v + 0x8000) >> 16; } 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric static uint32_t readFromHalf16(const uint8_t *loc) { 680b57cec5SDimitry Andric return read32(config->isLE ? loc : loc - 2); 690b57cec5SDimitry Andric } 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric static void writeFromHalf16(uint8_t *loc, uint32_t insn) { 720b57cec5SDimitry Andric write32(config->isLE ? loc : loc - 2, insn); 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric 755ffd83dbSDimitry Andric void elf::writePPC32GlinkSection(uint8_t *buf, size_t numEntries) { 7613138422SDimitry Andric // Create canonical PLT entries for non-PIE code. Compilers don't generate 7713138422SDimitry Andric // non-GOT-non-PLT relocations referencing external functions for -fpie/-fPIE. 7813138422SDimitry Andric uint32_t glink = in.plt->getVA(); // VA of .glink 7913138422SDimitry Andric if (!config->isPic) { 800eae32dcSDimitry Andric for (const Symbol *sym : cast<PPC32GlinkSection>(*in.plt).canonical_plts) { 8113138422SDimitry Andric writePPC32PltCallStub(buf, sym->getGotPltVA(), nullptr, 0); 8213138422SDimitry Andric buf += 16; 8313138422SDimitry Andric glink += 16; 8413138422SDimitry Andric } 8513138422SDimitry Andric } 8613138422SDimitry Andric 870b57cec5SDimitry Andric // On PPC Secure PLT ABI, bl foo@plt jumps to a call stub, which loads an 880b57cec5SDimitry Andric // absolute address from a specific .plt slot (usually called .got.plt on 890b57cec5SDimitry Andric // other targets) and jumps there. 900b57cec5SDimitry Andric // 910b57cec5SDimitry Andric // a) With immediate binding (BIND_NOW), the .plt entry is resolved at load 920b57cec5SDimitry Andric // time. The .glink section is not used. 930b57cec5SDimitry Andric // b) With lazy binding, the .plt entry points to a `b PLTresolve` 940b57cec5SDimitry Andric // instruction in .glink, filled in by PPC::writeGotPlt(). 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric // Write N `b PLTresolve` first. 970b57cec5SDimitry Andric for (size_t i = 0; i != numEntries; ++i) 980b57cec5SDimitry Andric write32(buf + 4 * i, 0x48000000 | 4 * (numEntries - i)); 990b57cec5SDimitry Andric buf += 4 * numEntries; 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric // Then write PLTresolve(), which has two forms: PIC and non-PIC. PLTresolve() 1020b57cec5SDimitry Andric // computes the PLT index (by computing the distance from the landing b to 1030b57cec5SDimitry Andric // itself) and calls _dl_runtime_resolve() (in glibc). 1040b57cec5SDimitry Andric uint32_t got = in.got->getVA(); 1050b57cec5SDimitry Andric const uint8_t *end = buf + 64; 1060b57cec5SDimitry Andric if (config->isPic) { 10713138422SDimitry Andric uint32_t afterBcl = 4 * in.plt->getNumEntries() + 12; 1080b57cec5SDimitry Andric uint32_t gotBcl = got + 4 - (glink + afterBcl); 1090b57cec5SDimitry Andric write32(buf + 0, 0x3d6b0000 | ha(afterBcl)); // addis r11,r11,1f-glink@ha 1100b57cec5SDimitry Andric write32(buf + 4, 0x7c0802a6); // mflr r0 1110b57cec5SDimitry Andric write32(buf + 8, 0x429f0005); // bcl 20,30,.+4 11213138422SDimitry Andric write32(buf + 12, 0x396b0000 | lo(afterBcl)); // 1: addi r11,r11,1b-glink@l 1130b57cec5SDimitry Andric write32(buf + 16, 0x7d8802a6); // mflr r12 1140b57cec5SDimitry Andric write32(buf + 20, 0x7c0803a6); // mtlr r0 1150b57cec5SDimitry Andric write32(buf + 24, 0x7d6c5850); // sub r11,r11,r12 1160b57cec5SDimitry Andric write32(buf + 28, 0x3d8c0000 | ha(gotBcl)); // addis 12,12,GOT+4-1b@ha 1170b57cec5SDimitry Andric if (ha(gotBcl) == ha(gotBcl + 4)) { 1180b57cec5SDimitry Andric write32(buf + 32, 0x800c0000 | lo(gotBcl)); // lwz r0,r12,GOT+4-1b@l(r12) 1190b57cec5SDimitry Andric write32(buf + 36, 1200b57cec5SDimitry Andric 0x818c0000 | lo(gotBcl + 4)); // lwz r12,r12,GOT+8-1b@l(r12) 1210b57cec5SDimitry Andric } else { 1220b57cec5SDimitry Andric write32(buf + 32, 0x840c0000 | lo(gotBcl)); // lwzu r0,r12,GOT+4-1b@l(r12) 1230b57cec5SDimitry Andric write32(buf + 36, 0x818c0000 | 4); // lwz r12,r12,4(r12) 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric write32(buf + 40, 0x7c0903a6); // mtctr 0 1260b57cec5SDimitry Andric write32(buf + 44, 0x7c0b5a14); // add r0,11,11 1270b57cec5SDimitry Andric write32(buf + 48, 0x7d605a14); // add r11,0,11 1280b57cec5SDimitry Andric write32(buf + 52, 0x4e800420); // bctr 1290b57cec5SDimitry Andric buf += 56; 1300b57cec5SDimitry Andric } else { 1310b57cec5SDimitry Andric write32(buf + 0, 0x3d800000 | ha(got + 4)); // lis r12,GOT+4@ha 13213138422SDimitry Andric write32(buf + 4, 0x3d6b0000 | ha(-glink)); // addis r11,r11,-glink@ha 1330b57cec5SDimitry Andric if (ha(got + 4) == ha(got + 8)) 1340b57cec5SDimitry Andric write32(buf + 8, 0x800c0000 | lo(got + 4)); // lwz r0,GOT+4@l(r12) 1350b57cec5SDimitry Andric else 1360b57cec5SDimitry Andric write32(buf + 8, 0x840c0000 | lo(got + 4)); // lwzu r0,GOT+4@l(r12) 13713138422SDimitry Andric write32(buf + 12, 0x396b0000 | lo(-glink)); // addi r11,r11,-glink@l 1380b57cec5SDimitry Andric write32(buf + 16, 0x7c0903a6); // mtctr r0 1390b57cec5SDimitry Andric write32(buf + 20, 0x7c0b5a14); // add r0,r11,r11 1400b57cec5SDimitry Andric if (ha(got + 4) == ha(got + 8)) 14113138422SDimitry Andric write32(buf + 24, 0x818c0000 | lo(got + 8)); // lwz r12,GOT+8@l(r12) 1420b57cec5SDimitry Andric else 1430b57cec5SDimitry Andric write32(buf + 24, 0x818c0000 | 4); // lwz r12,4(r12) 1440b57cec5SDimitry Andric write32(buf + 28, 0x7d605a14); // add r11,r0,r11 1450b57cec5SDimitry Andric write32(buf + 32, 0x4e800420); // bctr 1460b57cec5SDimitry Andric buf += 36; 1470b57cec5SDimitry Andric } 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric // Pad with nop. They should not be executed. 1500b57cec5SDimitry Andric for (; buf < end; buf += 4) 1510b57cec5SDimitry Andric write32(buf, 0x60000000); 1520b57cec5SDimitry Andric } 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric PPC::PPC() { 15555e4f9d5SDimitry Andric copyRel = R_PPC_COPY; 1560b57cec5SDimitry Andric gotRel = R_PPC_GLOB_DAT; 1570b57cec5SDimitry Andric pltRel = R_PPC_JMP_SLOT; 1580b57cec5SDimitry Andric relativeRel = R_PPC_RELATIVE; 1590b57cec5SDimitry Andric iRelativeRel = R_PPC_IRELATIVE; 1600b57cec5SDimitry Andric symbolicRel = R_PPC_ADDR32; 1610b57cec5SDimitry Andric gotHeaderEntriesNum = 3; 1620b57cec5SDimitry Andric gotPltHeaderEntriesNum = 0; 16313138422SDimitry Andric pltHeaderSize = 0; 1640b57cec5SDimitry Andric pltEntrySize = 4; 165480093f4SDimitry Andric ipltEntrySize = 16; 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric needsThunks = true; 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric tlsModuleIndexRel = R_PPC_DTPMOD32; 1700b57cec5SDimitry Andric tlsOffsetRel = R_PPC_DTPREL32; 1710b57cec5SDimitry Andric tlsGotRel = R_PPC_TPREL32; 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric defaultMaxPageSize = 65536; 1740b57cec5SDimitry Andric defaultImageBase = 0x10000000; 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric write32(trapInstr.data(), 0x7fe00008); 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric 179480093f4SDimitry Andric void PPC::writeIplt(uint8_t *buf, const Symbol &sym, 180480093f4SDimitry Andric uint64_t /*pltEntryAddr*/) const { 181480093f4SDimitry Andric // In -pie or -shared mode, assume r30 points to .got2+0x8000, and use a 182480093f4SDimitry Andric // .got2.plt_pic32. thunk. 183480093f4SDimitry Andric writePPC32PltCallStub(buf, sym.getGotPltVA(), sym.file, 0x8000); 184480093f4SDimitry Andric } 185480093f4SDimitry Andric 1860b57cec5SDimitry Andric void PPC::writeGotHeader(uint8_t *buf) const { 1870b57cec5SDimitry Andric // _GLOBAL_OFFSET_TABLE_[0] = _DYNAMIC 1880b57cec5SDimitry Andric // glibc stores _dl_runtime_resolve in _GLOBAL_OFFSET_TABLE_[1], 1890b57cec5SDimitry Andric // link_map in _GLOBAL_OFFSET_TABLE_[2]. 1900b57cec5SDimitry Andric write32(buf, mainPart->dynamic->getVA()); 1910b57cec5SDimitry Andric } 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric void PPC::writeGotPlt(uint8_t *buf, const Symbol &s) const { 1940b57cec5SDimitry Andric // Address of the symbol resolver stub in .glink . 19504eeddc0SDimitry Andric write32(buf, in.plt->getVA() + in.plt->headerSize + 4 * s.getPltIdx()); 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric bool PPC::needsThunk(RelExpr expr, RelType type, const InputFile *file, 19913138422SDimitry Andric uint64_t branchAddr, const Symbol &s, int64_t a) const { 20013138422SDimitry Andric if (type != R_PPC_LOCAL24PC && type != R_PPC_REL24 && type != R_PPC_PLTREL24) 2010b57cec5SDimitry Andric return false; 2020b57cec5SDimitry Andric if (s.isInPlt()) 2030b57cec5SDimitry Andric return true; 2040b57cec5SDimitry Andric if (s.isUndefWeak()) 2050b57cec5SDimitry Andric return false; 20613138422SDimitry Andric return !PPC::inBranchRange(type, branchAddr, s.getVA(a)); 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric uint32_t PPC::getThunkSectionSpacing() const { return 0x2000000; } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric bool PPC::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 2120b57cec5SDimitry Andric uint64_t offset = dst - src; 21313138422SDimitry Andric if (type == R_PPC_LOCAL24PC || type == R_PPC_REL24 || type == R_PPC_PLTREL24) 2140b57cec5SDimitry Andric return isInt<26>(offset); 2150b57cec5SDimitry Andric llvm_unreachable("unsupported relocation type used in branch"); 2160b57cec5SDimitry Andric } 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric RelExpr PPC::getRelExpr(RelType type, const Symbol &s, 2190b57cec5SDimitry Andric const uint8_t *loc) const { 2200b57cec5SDimitry Andric switch (type) { 2210b57cec5SDimitry Andric case R_PPC_NONE: 2220b57cec5SDimitry Andric return R_NONE; 2230b57cec5SDimitry Andric case R_PPC_ADDR16_HA: 2240b57cec5SDimitry Andric case R_PPC_ADDR16_HI: 2250b57cec5SDimitry Andric case R_PPC_ADDR16_LO: 226e8d8bef9SDimitry Andric case R_PPC_ADDR24: 2270b57cec5SDimitry Andric case R_PPC_ADDR32: 2280b57cec5SDimitry Andric return R_ABS; 2290b57cec5SDimitry Andric case R_PPC_DTPREL16: 2300b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 2310b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 2320b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 2330b57cec5SDimitry Andric case R_PPC_DTPREL32: 2340b57cec5SDimitry Andric return R_DTPREL; 2350b57cec5SDimitry Andric case R_PPC_REL14: 2360b57cec5SDimitry Andric case R_PPC_REL32: 2370b57cec5SDimitry Andric case R_PPC_REL16_LO: 2380b57cec5SDimitry Andric case R_PPC_REL16_HI: 2390b57cec5SDimitry Andric case R_PPC_REL16_HA: 2400b57cec5SDimitry Andric return R_PC; 2410b57cec5SDimitry Andric case R_PPC_GOT16: 2420b57cec5SDimitry Andric return R_GOT_OFF; 24313138422SDimitry Andric case R_PPC_LOCAL24PC: 2440b57cec5SDimitry Andric case R_PPC_REL24: 2450b57cec5SDimitry Andric return R_PLT_PC; 2460b57cec5SDimitry Andric case R_PPC_PLTREL24: 2470b57cec5SDimitry Andric return R_PPC32_PLTREL; 2480b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: 2490b57cec5SDimitry Andric return R_TLSGD_GOT; 2500b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16: 2510b57cec5SDimitry Andric return R_TLSLD_GOT; 2520b57cec5SDimitry Andric case R_PPC_GOT_TPREL16: 2530b57cec5SDimitry Andric return R_GOT_OFF; 2540b57cec5SDimitry Andric case R_PPC_TLS: 2550b57cec5SDimitry Andric return R_TLSIE_HINT; 2560b57cec5SDimitry Andric case R_PPC_TLSGD: 2570b57cec5SDimitry Andric return R_TLSDESC_CALL; 2580b57cec5SDimitry Andric case R_PPC_TLSLD: 2590b57cec5SDimitry Andric return R_TLSLD_HINT; 2600b57cec5SDimitry Andric case R_PPC_TPREL16: 2610b57cec5SDimitry Andric case R_PPC_TPREL16_HA: 2620b57cec5SDimitry Andric case R_PPC_TPREL16_LO: 2630b57cec5SDimitry Andric case R_PPC_TPREL16_HI: 264e8d8bef9SDimitry Andric return R_TPREL; 2650b57cec5SDimitry Andric default: 2660b57cec5SDimitry Andric error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 2670b57cec5SDimitry Andric ") against symbol " + toString(s)); 2680b57cec5SDimitry Andric return R_NONE; 2690b57cec5SDimitry Andric } 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric RelType PPC::getDynRel(RelType type) const { 2730b57cec5SDimitry Andric if (type == R_PPC_ADDR32) 2740b57cec5SDimitry Andric return type; 2750b57cec5SDimitry Andric return R_PPC_NONE; 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric 278298c3e8dSDimitry Andric int64_t PPC::getImplicitAddend(const uint8_t *buf, RelType type) const { 279298c3e8dSDimitry Andric switch (type) { 280298c3e8dSDimitry Andric case R_PPC_NONE: 281298c3e8dSDimitry Andric return 0; 282298c3e8dSDimitry Andric case R_PPC_ADDR32: 283298c3e8dSDimitry Andric case R_PPC_REL32: 284298c3e8dSDimitry Andric return SignExtend64<32>(read32(buf)); 285298c3e8dSDimitry Andric default: 286298c3e8dSDimitry Andric internalLinkerError(getErrorLocation(buf), 287298c3e8dSDimitry Andric "cannot read addend for relocation " + toString(type)); 288298c3e8dSDimitry Andric return 0; 289298c3e8dSDimitry Andric } 290298c3e8dSDimitry Andric } 291298c3e8dSDimitry Andric 2920b57cec5SDimitry Andric static std::pair<RelType, uint64_t> fromDTPREL(RelType type, uint64_t val) { 2930b57cec5SDimitry Andric uint64_t dtpBiasedVal = val - 0x8000; 2940b57cec5SDimitry Andric switch (type) { 2950b57cec5SDimitry Andric case R_PPC_DTPREL16: 2960b57cec5SDimitry Andric return {R_PPC64_ADDR16, dtpBiasedVal}; 2970b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 2980b57cec5SDimitry Andric return {R_PPC_ADDR16_HA, dtpBiasedVal}; 2990b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 3000b57cec5SDimitry Andric return {R_PPC_ADDR16_HI, dtpBiasedVal}; 3010b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 3020b57cec5SDimitry Andric return {R_PPC_ADDR16_LO, dtpBiasedVal}; 3030b57cec5SDimitry Andric case R_PPC_DTPREL32: 3040b57cec5SDimitry Andric return {R_PPC_ADDR32, dtpBiasedVal}; 3050b57cec5SDimitry Andric default: 3060b57cec5SDimitry Andric return {type, val}; 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric } 3090b57cec5SDimitry Andric 3105ffd83dbSDimitry Andric void PPC::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 3110b57cec5SDimitry Andric RelType newType; 3125ffd83dbSDimitry Andric std::tie(newType, val) = fromDTPREL(rel.type, val); 3130b57cec5SDimitry Andric switch (newType) { 3140b57cec5SDimitry Andric case R_PPC_ADDR16: 3155ffd83dbSDimitry Andric checkIntUInt(loc, val, 16, rel); 3160b57cec5SDimitry Andric write16(loc, val); 3170b57cec5SDimitry Andric break; 3180b57cec5SDimitry Andric case R_PPC_GOT16: 3190b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: 3200b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16: 3210b57cec5SDimitry Andric case R_PPC_GOT_TPREL16: 3220b57cec5SDimitry Andric case R_PPC_TPREL16: 3235ffd83dbSDimitry Andric checkInt(loc, val, 16, rel); 3240b57cec5SDimitry Andric write16(loc, val); 3250b57cec5SDimitry Andric break; 3260b57cec5SDimitry Andric case R_PPC_ADDR16_HA: 3270b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 3280b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16_HA: 3290b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16_HA: 3300b57cec5SDimitry Andric case R_PPC_GOT_TPREL16_HA: 3310b57cec5SDimitry Andric case R_PPC_REL16_HA: 3320b57cec5SDimitry Andric case R_PPC_TPREL16_HA: 3330b57cec5SDimitry Andric write16(loc, ha(val)); 3340b57cec5SDimitry Andric break; 3350b57cec5SDimitry Andric case R_PPC_ADDR16_HI: 3360b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 3370b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16_HI: 3380b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16_HI: 3390b57cec5SDimitry Andric case R_PPC_GOT_TPREL16_HI: 3400b57cec5SDimitry Andric case R_PPC_REL16_HI: 3410b57cec5SDimitry Andric case R_PPC_TPREL16_HI: 3420b57cec5SDimitry Andric write16(loc, val >> 16); 3430b57cec5SDimitry Andric break; 3440b57cec5SDimitry Andric case R_PPC_ADDR16_LO: 3450b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 3460b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16_LO: 3470b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16_LO: 3480b57cec5SDimitry Andric case R_PPC_GOT_TPREL16_LO: 3490b57cec5SDimitry Andric case R_PPC_REL16_LO: 3500b57cec5SDimitry Andric case R_PPC_TPREL16_LO: 3510b57cec5SDimitry Andric write16(loc, val); 3520b57cec5SDimitry Andric break; 3530b57cec5SDimitry Andric case R_PPC_ADDR32: 3540b57cec5SDimitry Andric case R_PPC_REL32: 3550b57cec5SDimitry Andric write32(loc, val); 3560b57cec5SDimitry Andric break; 3570b57cec5SDimitry Andric case R_PPC_REL14: { 3580b57cec5SDimitry Andric uint32_t mask = 0x0000FFFC; 3595ffd83dbSDimitry Andric checkInt(loc, val, 16, rel); 3605ffd83dbSDimitry Andric checkAlignment(loc, val, 4, rel); 3610b57cec5SDimitry Andric write32(loc, (read32(loc) & ~mask) | (val & mask)); 3620b57cec5SDimitry Andric break; 3630b57cec5SDimitry Andric } 364e8d8bef9SDimitry Andric case R_PPC_ADDR24: 3650b57cec5SDimitry Andric case R_PPC_REL24: 3660b57cec5SDimitry Andric case R_PPC_LOCAL24PC: 3670b57cec5SDimitry Andric case R_PPC_PLTREL24: { 3680b57cec5SDimitry Andric uint32_t mask = 0x03FFFFFC; 3695ffd83dbSDimitry Andric checkInt(loc, val, 26, rel); 3705ffd83dbSDimitry Andric checkAlignment(loc, val, 4, rel); 3710b57cec5SDimitry Andric write32(loc, (read32(loc) & ~mask) | (val & mask)); 3720b57cec5SDimitry Andric break; 3730b57cec5SDimitry Andric } 3740b57cec5SDimitry Andric default: 3750b57cec5SDimitry Andric llvm_unreachable("unknown relocation"); 3760b57cec5SDimitry Andric } 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric 379e8d8bef9SDimitry Andric RelExpr PPC::adjustTlsExpr(RelType type, RelExpr expr) const { 3800b57cec5SDimitry Andric if (expr == R_RELAX_TLS_GD_TO_IE) 3810b57cec5SDimitry Andric return R_RELAX_TLS_GD_TO_IE_GOT_OFF; 3820b57cec5SDimitry Andric if (expr == R_RELAX_TLS_LD_TO_LE) 3830b57cec5SDimitry Andric return R_RELAX_TLS_LD_TO_LE_ABS; 3840b57cec5SDimitry Andric return expr; 3850b57cec5SDimitry Andric } 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric int PPC::getTlsGdRelaxSkip(RelType type) const { 3880b57cec5SDimitry Andric // A __tls_get_addr call instruction is marked with 2 relocations: 3890b57cec5SDimitry Andric // 3900b57cec5SDimitry Andric // R_PPC_TLSGD / R_PPC_TLSLD: marker relocation 3910b57cec5SDimitry Andric // R_PPC_REL24: __tls_get_addr 3920b57cec5SDimitry Andric // 3930b57cec5SDimitry Andric // After the relaxation we no longer call __tls_get_addr and should skip both 3940b57cec5SDimitry Andric // relocations to not create a false dependence on __tls_get_addr being 3950b57cec5SDimitry Andric // defined. 3960b57cec5SDimitry Andric if (type == R_PPC_TLSGD || type == R_PPC_TLSLD) 3970b57cec5SDimitry Andric return 2; 3980b57cec5SDimitry Andric return 1; 3990b57cec5SDimitry Andric } 4000b57cec5SDimitry Andric 4015ffd83dbSDimitry Andric void PPC::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 4025ffd83dbSDimitry Andric uint64_t val) const { 4035ffd83dbSDimitry Andric switch (rel.type) { 4040b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: { 4050b57cec5SDimitry Andric // addi rT, rA, x@got@tlsgd --> lwz rT, x@got@tprel(rA) 4060b57cec5SDimitry Andric uint32_t insn = readFromHalf16(loc); 4070b57cec5SDimitry Andric writeFromHalf16(loc, 0x80000000 | (insn & 0x03ff0000)); 4085ffd83dbSDimitry Andric relocateNoSym(loc, R_PPC_GOT_TPREL16, val); 4090b57cec5SDimitry Andric break; 4100b57cec5SDimitry Andric } 4110b57cec5SDimitry Andric case R_PPC_TLSGD: 4120b57cec5SDimitry Andric // bl __tls_get_addr(x@tldgd) --> add r3, r3, r2 4130b57cec5SDimitry Andric write32(loc, 0x7c631214); 4140b57cec5SDimitry Andric break; 4150b57cec5SDimitry Andric default: 4160b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to IE relaxation"); 4170b57cec5SDimitry Andric } 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 4205ffd83dbSDimitry Andric void PPC::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 4215ffd83dbSDimitry Andric uint64_t val) const { 4225ffd83dbSDimitry Andric switch (rel.type) { 4230b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: 4240b57cec5SDimitry Andric // addi r3, r31, x@got@tlsgd --> addis r3, r2, x@tprel@ha 4250b57cec5SDimitry Andric writeFromHalf16(loc, 0x3c620000 | ha(val)); 4260b57cec5SDimitry Andric break; 4270b57cec5SDimitry Andric case R_PPC_TLSGD: 4280b57cec5SDimitry Andric // bl __tls_get_addr(x@tldgd) --> add r3, r3, x@tprel@l 4290b57cec5SDimitry Andric write32(loc, 0x38630000 | lo(val)); 4300b57cec5SDimitry Andric break; 4310b57cec5SDimitry Andric default: 4320b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 4330b57cec5SDimitry Andric } 4340b57cec5SDimitry Andric } 4350b57cec5SDimitry Andric 4365ffd83dbSDimitry Andric void PPC::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, 4375ffd83dbSDimitry Andric uint64_t val) const { 4385ffd83dbSDimitry Andric switch (rel.type) { 4390b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16: 4400b57cec5SDimitry Andric // addi r3, rA, x@got@tlsgd --> addis r3, r2, 0 4410b57cec5SDimitry Andric writeFromHalf16(loc, 0x3c620000); 4420b57cec5SDimitry Andric break; 4430b57cec5SDimitry Andric case R_PPC_TLSLD: 4440b57cec5SDimitry Andric // r3+x@dtprel computes r3+x-0x8000, while we want it to compute r3+x@tprel 4450b57cec5SDimitry Andric // = r3+x-0x7000, so add 4096 to r3. 4460b57cec5SDimitry Andric // bl __tls_get_addr(x@tlsld) --> addi r3, r3, 4096 4470b57cec5SDimitry Andric write32(loc, 0x38631000); 4480b57cec5SDimitry Andric break; 4490b57cec5SDimitry Andric case R_PPC_DTPREL16: 4500b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 4510b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 4520b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 4535ffd83dbSDimitry Andric relocate(loc, rel, val); 4540b57cec5SDimitry Andric break; 4550b57cec5SDimitry Andric default: 4560b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS LD to LE relaxation"); 4570b57cec5SDimitry Andric } 4580b57cec5SDimitry Andric } 4590b57cec5SDimitry Andric 4605ffd83dbSDimitry Andric void PPC::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 4615ffd83dbSDimitry Andric uint64_t val) const { 4625ffd83dbSDimitry Andric switch (rel.type) { 4630b57cec5SDimitry Andric case R_PPC_GOT_TPREL16: { 4640b57cec5SDimitry Andric // lwz rT, x@got@tprel(rA) --> addis rT, r2, x@tprel@ha 4650b57cec5SDimitry Andric uint32_t rt = readFromHalf16(loc) & 0x03e00000; 4660b57cec5SDimitry Andric writeFromHalf16(loc, 0x3c020000 | rt | ha(val)); 4670b57cec5SDimitry Andric break; 4680b57cec5SDimitry Andric } 4690b57cec5SDimitry Andric case R_PPC_TLS: { 4700b57cec5SDimitry Andric uint32_t insn = read32(loc); 4710b57cec5SDimitry Andric if (insn >> 26 != 31) 4720b57cec5SDimitry Andric error("unrecognized instruction for IE to LE R_PPC_TLS"); 4730b57cec5SDimitry Andric // addi rT, rT, x@tls --> addi rT, rT, x@tprel@l 4740b57cec5SDimitry Andric uint32_t dFormOp = getPPCDFormOp((read32(loc) & 0x000007fe) >> 1); 4750b57cec5SDimitry Andric if (dFormOp == 0) 4760b57cec5SDimitry Andric error("unrecognized instruction for IE to LE R_PPC_TLS"); 4770b57cec5SDimitry Andric write32(loc, (dFormOp << 26) | (insn & 0x03ff0000) | lo(val)); 4780b57cec5SDimitry Andric break; 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric default: 4810b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS IE to LE relaxation"); 4820b57cec5SDimitry Andric } 4830b57cec5SDimitry Andric } 4840b57cec5SDimitry Andric 485*bdd1243dSDimitry Andric void PPC::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const { 486*bdd1243dSDimitry Andric uint64_t secAddr = sec.getOutputSection()->addr; 487*bdd1243dSDimitry Andric if (auto *s = dyn_cast<InputSection>(&sec)) 488*bdd1243dSDimitry Andric secAddr += s->outSecOff; 489*bdd1243dSDimitry Andric for (const Relocation &rel : sec.relocs()) { 490*bdd1243dSDimitry Andric uint8_t *loc = buf + rel.offset; 491*bdd1243dSDimitry Andric const uint64_t val = SignExtend64( 492*bdd1243dSDimitry Andric sec.getRelocTargetVA(sec.file, rel.type, rel.addend, 493*bdd1243dSDimitry Andric secAddr + rel.offset, *rel.sym, rel.expr), 494*bdd1243dSDimitry Andric 32); 495*bdd1243dSDimitry Andric switch (rel.expr) { 496*bdd1243dSDimitry Andric case R_RELAX_TLS_GD_TO_IE_GOT_OFF: 497*bdd1243dSDimitry Andric relaxTlsGdToIe(loc, rel, val); 498*bdd1243dSDimitry Andric break; 499*bdd1243dSDimitry Andric case R_RELAX_TLS_GD_TO_LE: 500*bdd1243dSDimitry Andric relaxTlsGdToLe(loc, rel, val); 501*bdd1243dSDimitry Andric break; 502*bdd1243dSDimitry Andric case R_RELAX_TLS_LD_TO_LE_ABS: 503*bdd1243dSDimitry Andric relaxTlsLdToLe(loc, rel, val); 504*bdd1243dSDimitry Andric break; 505*bdd1243dSDimitry Andric case R_RELAX_TLS_IE_TO_LE: 506*bdd1243dSDimitry Andric relaxTlsIeToLe(loc, rel, val); 507*bdd1243dSDimitry Andric break; 508*bdd1243dSDimitry Andric default: 509*bdd1243dSDimitry Andric relocate(loc, rel, val); 510*bdd1243dSDimitry Andric break; 511*bdd1243dSDimitry Andric } 512*bdd1243dSDimitry Andric } 513*bdd1243dSDimitry Andric } 514*bdd1243dSDimitry Andric 5155ffd83dbSDimitry Andric TargetInfo *elf::getPPCTargetInfo() { 5160b57cec5SDimitry Andric static PPC target; 5170b57cec5SDimitry Andric return ⌖ 5180b57cec5SDimitry Andric } 519