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; 545ffd83dbSDimitry Andric void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 555ffd83dbSDimitry Andric uint64_t val) const override; 565ffd83dbSDimitry Andric void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 575ffd83dbSDimitry Andric uint64_t val) const override; 585ffd83dbSDimitry Andric void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, 595ffd83dbSDimitry Andric uint64_t val) const override; 605ffd83dbSDimitry Andric void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 615ffd83dbSDimitry Andric uint64_t val) const override; 620b57cec5SDimitry Andric }; 630b57cec5SDimitry Andric } // namespace 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric static uint16_t lo(uint32_t v) { return v; } 660b57cec5SDimitry Andric static uint16_t ha(uint32_t v) { return (v + 0x8000) >> 16; } 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric static uint32_t readFromHalf16(const uint8_t *loc) { 690b57cec5SDimitry Andric return read32(config->isLE ? loc : loc - 2); 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric static void writeFromHalf16(uint8_t *loc, uint32_t insn) { 730b57cec5SDimitry Andric write32(config->isLE ? loc : loc - 2, insn); 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric 765ffd83dbSDimitry Andric void elf::writePPC32GlinkSection(uint8_t *buf, size_t numEntries) { 7713138422SDimitry Andric // Create canonical PLT entries for non-PIE code. Compilers don't generate 7813138422SDimitry Andric // non-GOT-non-PLT relocations referencing external functions for -fpie/-fPIE. 7913138422SDimitry Andric uint32_t glink = in.plt->getVA(); // VA of .glink 8013138422SDimitry Andric if (!config->isPic) { 810eae32dcSDimitry Andric for (const Symbol *sym : cast<PPC32GlinkSection>(*in.plt).canonical_plts) { 8213138422SDimitry Andric writePPC32PltCallStub(buf, sym->getGotPltVA(), nullptr, 0); 8313138422SDimitry Andric buf += 16; 8413138422SDimitry Andric glink += 16; 8513138422SDimitry Andric } 8613138422SDimitry Andric } 8713138422SDimitry Andric 880b57cec5SDimitry Andric // On PPC Secure PLT ABI, bl foo@plt jumps to a call stub, which loads an 890b57cec5SDimitry Andric // absolute address from a specific .plt slot (usually called .got.plt on 900b57cec5SDimitry Andric // other targets) and jumps there. 910b57cec5SDimitry Andric // 920b57cec5SDimitry Andric // a) With immediate binding (BIND_NOW), the .plt entry is resolved at load 930b57cec5SDimitry Andric // time. The .glink section is not used. 940b57cec5SDimitry Andric // b) With lazy binding, the .plt entry points to a `b PLTresolve` 950b57cec5SDimitry Andric // instruction in .glink, filled in by PPC::writeGotPlt(). 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric // Write N `b PLTresolve` first. 980b57cec5SDimitry Andric for (size_t i = 0; i != numEntries; ++i) 990b57cec5SDimitry Andric write32(buf + 4 * i, 0x48000000 | 4 * (numEntries - i)); 1000b57cec5SDimitry Andric buf += 4 * numEntries; 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric // Then write PLTresolve(), which has two forms: PIC and non-PIC. PLTresolve() 1030b57cec5SDimitry Andric // computes the PLT index (by computing the distance from the landing b to 1040b57cec5SDimitry Andric // itself) and calls _dl_runtime_resolve() (in glibc). 1050b57cec5SDimitry Andric uint32_t got = in.got->getVA(); 1060b57cec5SDimitry Andric const uint8_t *end = buf + 64; 1070b57cec5SDimitry Andric if (config->isPic) { 10813138422SDimitry Andric uint32_t afterBcl = 4 * in.plt->getNumEntries() + 12; 1090b57cec5SDimitry Andric uint32_t gotBcl = got + 4 - (glink + afterBcl); 1100b57cec5SDimitry Andric write32(buf + 0, 0x3d6b0000 | ha(afterBcl)); // addis r11,r11,1f-glink@ha 1110b57cec5SDimitry Andric write32(buf + 4, 0x7c0802a6); // mflr r0 1120b57cec5SDimitry Andric write32(buf + 8, 0x429f0005); // bcl 20,30,.+4 11313138422SDimitry Andric write32(buf + 12, 0x396b0000 | lo(afterBcl)); // 1: addi r11,r11,1b-glink@l 1140b57cec5SDimitry Andric write32(buf + 16, 0x7d8802a6); // mflr r12 1150b57cec5SDimitry Andric write32(buf + 20, 0x7c0803a6); // mtlr r0 1160b57cec5SDimitry Andric write32(buf + 24, 0x7d6c5850); // sub r11,r11,r12 1170b57cec5SDimitry Andric write32(buf + 28, 0x3d8c0000 | ha(gotBcl)); // addis 12,12,GOT+4-1b@ha 1180b57cec5SDimitry Andric if (ha(gotBcl) == ha(gotBcl + 4)) { 1190b57cec5SDimitry Andric write32(buf + 32, 0x800c0000 | lo(gotBcl)); // lwz r0,r12,GOT+4-1b@l(r12) 1200b57cec5SDimitry Andric write32(buf + 36, 1210b57cec5SDimitry Andric 0x818c0000 | lo(gotBcl + 4)); // lwz r12,r12,GOT+8-1b@l(r12) 1220b57cec5SDimitry Andric } else { 1230b57cec5SDimitry Andric write32(buf + 32, 0x840c0000 | lo(gotBcl)); // lwzu r0,r12,GOT+4-1b@l(r12) 1240b57cec5SDimitry Andric write32(buf + 36, 0x818c0000 | 4); // lwz r12,r12,4(r12) 1250b57cec5SDimitry Andric } 1260b57cec5SDimitry Andric write32(buf + 40, 0x7c0903a6); // mtctr 0 1270b57cec5SDimitry Andric write32(buf + 44, 0x7c0b5a14); // add r0,11,11 1280b57cec5SDimitry Andric write32(buf + 48, 0x7d605a14); // add r11,0,11 1290b57cec5SDimitry Andric write32(buf + 52, 0x4e800420); // bctr 1300b57cec5SDimitry Andric buf += 56; 1310b57cec5SDimitry Andric } else { 1320b57cec5SDimitry Andric write32(buf + 0, 0x3d800000 | ha(got + 4)); // lis r12,GOT+4@ha 13313138422SDimitry Andric write32(buf + 4, 0x3d6b0000 | ha(-glink)); // addis r11,r11,-glink@ha 1340b57cec5SDimitry Andric if (ha(got + 4) == ha(got + 8)) 1350b57cec5SDimitry Andric write32(buf + 8, 0x800c0000 | lo(got + 4)); // lwz r0,GOT+4@l(r12) 1360b57cec5SDimitry Andric else 1370b57cec5SDimitry Andric write32(buf + 8, 0x840c0000 | lo(got + 4)); // lwzu r0,GOT+4@l(r12) 13813138422SDimitry Andric write32(buf + 12, 0x396b0000 | lo(-glink)); // addi r11,r11,-glink@l 1390b57cec5SDimitry Andric write32(buf + 16, 0x7c0903a6); // mtctr r0 1400b57cec5SDimitry Andric write32(buf + 20, 0x7c0b5a14); // add r0,r11,r11 1410b57cec5SDimitry Andric if (ha(got + 4) == ha(got + 8)) 14213138422SDimitry Andric write32(buf + 24, 0x818c0000 | lo(got + 8)); // lwz r12,GOT+8@l(r12) 1430b57cec5SDimitry Andric else 1440b57cec5SDimitry Andric write32(buf + 24, 0x818c0000 | 4); // lwz r12,4(r12) 1450b57cec5SDimitry Andric write32(buf + 28, 0x7d605a14); // add r11,r0,r11 1460b57cec5SDimitry Andric write32(buf + 32, 0x4e800420); // bctr 1470b57cec5SDimitry Andric buf += 36; 1480b57cec5SDimitry Andric } 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric // Pad with nop. They should not be executed. 1510b57cec5SDimitry Andric for (; buf < end; buf += 4) 1520b57cec5SDimitry Andric write32(buf, 0x60000000); 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric PPC::PPC() { 15655e4f9d5SDimitry Andric copyRel = R_PPC_COPY; 1570b57cec5SDimitry Andric gotRel = R_PPC_GLOB_DAT; 1580b57cec5SDimitry Andric pltRel = R_PPC_JMP_SLOT; 1590b57cec5SDimitry Andric relativeRel = R_PPC_RELATIVE; 1600b57cec5SDimitry Andric iRelativeRel = R_PPC_IRELATIVE; 1610b57cec5SDimitry Andric symbolicRel = R_PPC_ADDR32; 1620b57cec5SDimitry Andric gotHeaderEntriesNum = 3; 1630b57cec5SDimitry Andric gotPltHeaderEntriesNum = 0; 16413138422SDimitry Andric pltHeaderSize = 0; 1650b57cec5SDimitry Andric pltEntrySize = 4; 166480093f4SDimitry Andric ipltEntrySize = 16; 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric needsThunks = true; 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric tlsModuleIndexRel = R_PPC_DTPMOD32; 1710b57cec5SDimitry Andric tlsOffsetRel = R_PPC_DTPREL32; 1720b57cec5SDimitry Andric tlsGotRel = R_PPC_TPREL32; 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric defaultMaxPageSize = 65536; 1750b57cec5SDimitry Andric defaultImageBase = 0x10000000; 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric write32(trapInstr.data(), 0x7fe00008); 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric 180480093f4SDimitry Andric void PPC::writeIplt(uint8_t *buf, const Symbol &sym, 181480093f4SDimitry Andric uint64_t /*pltEntryAddr*/) const { 182480093f4SDimitry Andric // In -pie or -shared mode, assume r30 points to .got2+0x8000, and use a 183480093f4SDimitry Andric // .got2.plt_pic32. thunk. 184480093f4SDimitry Andric writePPC32PltCallStub(buf, sym.getGotPltVA(), sym.file, 0x8000); 185480093f4SDimitry Andric } 186480093f4SDimitry Andric 1870b57cec5SDimitry Andric void PPC::writeGotHeader(uint8_t *buf) const { 1880b57cec5SDimitry Andric // _GLOBAL_OFFSET_TABLE_[0] = _DYNAMIC 1890b57cec5SDimitry Andric // glibc stores _dl_runtime_resolve in _GLOBAL_OFFSET_TABLE_[1], 1900b57cec5SDimitry Andric // link_map in _GLOBAL_OFFSET_TABLE_[2]. 1910b57cec5SDimitry Andric write32(buf, mainPart->dynamic->getVA()); 1920b57cec5SDimitry Andric } 1930b57cec5SDimitry Andric 1940b57cec5SDimitry Andric void PPC::writeGotPlt(uint8_t *buf, const Symbol &s) const { 1950b57cec5SDimitry Andric // Address of the symbol resolver stub in .glink . 196*04eeddc0SDimitry Andric write32(buf, in.plt->getVA() + in.plt->headerSize + 4 * s.getPltIdx()); 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric bool PPC::needsThunk(RelExpr expr, RelType type, const InputFile *file, 20013138422SDimitry Andric uint64_t branchAddr, const Symbol &s, int64_t a) const { 20113138422SDimitry Andric if (type != R_PPC_LOCAL24PC && type != R_PPC_REL24 && type != R_PPC_PLTREL24) 2020b57cec5SDimitry Andric return false; 2030b57cec5SDimitry Andric if (s.isInPlt()) 2040b57cec5SDimitry Andric return true; 2050b57cec5SDimitry Andric if (s.isUndefWeak()) 2060b57cec5SDimitry Andric return false; 20713138422SDimitry Andric return !PPC::inBranchRange(type, branchAddr, s.getVA(a)); 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric uint32_t PPC::getThunkSectionSpacing() const { return 0x2000000; } 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric bool PPC::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 2130b57cec5SDimitry Andric uint64_t offset = dst - src; 21413138422SDimitry Andric if (type == R_PPC_LOCAL24PC || type == R_PPC_REL24 || type == R_PPC_PLTREL24) 2150b57cec5SDimitry Andric return isInt<26>(offset); 2160b57cec5SDimitry Andric llvm_unreachable("unsupported relocation type used in branch"); 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric RelExpr PPC::getRelExpr(RelType type, const Symbol &s, 2200b57cec5SDimitry Andric const uint8_t *loc) const { 2210b57cec5SDimitry Andric switch (type) { 2220b57cec5SDimitry Andric case R_PPC_NONE: 2230b57cec5SDimitry Andric return R_NONE; 2240b57cec5SDimitry Andric case R_PPC_ADDR16_HA: 2250b57cec5SDimitry Andric case R_PPC_ADDR16_HI: 2260b57cec5SDimitry Andric case R_PPC_ADDR16_LO: 227e8d8bef9SDimitry Andric case R_PPC_ADDR24: 2280b57cec5SDimitry Andric case R_PPC_ADDR32: 2290b57cec5SDimitry Andric return R_ABS; 2300b57cec5SDimitry Andric case R_PPC_DTPREL16: 2310b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 2320b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 2330b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 2340b57cec5SDimitry Andric case R_PPC_DTPREL32: 2350b57cec5SDimitry Andric return R_DTPREL; 2360b57cec5SDimitry Andric case R_PPC_REL14: 2370b57cec5SDimitry Andric case R_PPC_REL32: 2380b57cec5SDimitry Andric case R_PPC_REL16_LO: 2390b57cec5SDimitry Andric case R_PPC_REL16_HI: 2400b57cec5SDimitry Andric case R_PPC_REL16_HA: 2410b57cec5SDimitry Andric return R_PC; 2420b57cec5SDimitry Andric case R_PPC_GOT16: 2430b57cec5SDimitry Andric return R_GOT_OFF; 24413138422SDimitry Andric case R_PPC_LOCAL24PC: 2450b57cec5SDimitry Andric case R_PPC_REL24: 2460b57cec5SDimitry Andric return R_PLT_PC; 2470b57cec5SDimitry Andric case R_PPC_PLTREL24: 2480b57cec5SDimitry Andric return R_PPC32_PLTREL; 2490b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: 2500b57cec5SDimitry Andric return R_TLSGD_GOT; 2510b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16: 2520b57cec5SDimitry Andric return R_TLSLD_GOT; 2530b57cec5SDimitry Andric case R_PPC_GOT_TPREL16: 2540b57cec5SDimitry Andric return R_GOT_OFF; 2550b57cec5SDimitry Andric case R_PPC_TLS: 2560b57cec5SDimitry Andric return R_TLSIE_HINT; 2570b57cec5SDimitry Andric case R_PPC_TLSGD: 2580b57cec5SDimitry Andric return R_TLSDESC_CALL; 2590b57cec5SDimitry Andric case R_PPC_TLSLD: 2600b57cec5SDimitry Andric return R_TLSLD_HINT; 2610b57cec5SDimitry Andric case R_PPC_TPREL16: 2620b57cec5SDimitry Andric case R_PPC_TPREL16_HA: 2630b57cec5SDimitry Andric case R_PPC_TPREL16_LO: 2640b57cec5SDimitry Andric case R_PPC_TPREL16_HI: 265e8d8bef9SDimitry Andric return R_TPREL; 2660b57cec5SDimitry Andric default: 2670b57cec5SDimitry Andric error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 2680b57cec5SDimitry Andric ") against symbol " + toString(s)); 2690b57cec5SDimitry Andric return R_NONE; 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric } 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric RelType PPC::getDynRel(RelType type) const { 2740b57cec5SDimitry Andric if (type == R_PPC_ADDR32) 2750b57cec5SDimitry Andric return type; 2760b57cec5SDimitry Andric return R_PPC_NONE; 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric 279298c3e8dSDimitry Andric int64_t PPC::getImplicitAddend(const uint8_t *buf, RelType type) const { 280298c3e8dSDimitry Andric switch (type) { 281298c3e8dSDimitry Andric case R_PPC_NONE: 282298c3e8dSDimitry Andric return 0; 283298c3e8dSDimitry Andric case R_PPC_ADDR32: 284298c3e8dSDimitry Andric case R_PPC_REL32: 285298c3e8dSDimitry Andric return SignExtend64<32>(read32(buf)); 286298c3e8dSDimitry Andric default: 287298c3e8dSDimitry Andric internalLinkerError(getErrorLocation(buf), 288298c3e8dSDimitry Andric "cannot read addend for relocation " + toString(type)); 289298c3e8dSDimitry Andric return 0; 290298c3e8dSDimitry Andric } 291298c3e8dSDimitry Andric } 292298c3e8dSDimitry Andric 2930b57cec5SDimitry Andric static std::pair<RelType, uint64_t> fromDTPREL(RelType type, uint64_t val) { 2940b57cec5SDimitry Andric uint64_t dtpBiasedVal = val - 0x8000; 2950b57cec5SDimitry Andric switch (type) { 2960b57cec5SDimitry Andric case R_PPC_DTPREL16: 2970b57cec5SDimitry Andric return {R_PPC64_ADDR16, dtpBiasedVal}; 2980b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 2990b57cec5SDimitry Andric return {R_PPC_ADDR16_HA, dtpBiasedVal}; 3000b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 3010b57cec5SDimitry Andric return {R_PPC_ADDR16_HI, dtpBiasedVal}; 3020b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 3030b57cec5SDimitry Andric return {R_PPC_ADDR16_LO, dtpBiasedVal}; 3040b57cec5SDimitry Andric case R_PPC_DTPREL32: 3050b57cec5SDimitry Andric return {R_PPC_ADDR32, dtpBiasedVal}; 3060b57cec5SDimitry Andric default: 3070b57cec5SDimitry Andric return {type, val}; 3080b57cec5SDimitry Andric } 3090b57cec5SDimitry Andric } 3100b57cec5SDimitry Andric 3115ffd83dbSDimitry Andric void PPC::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 3120b57cec5SDimitry Andric RelType newType; 3135ffd83dbSDimitry Andric std::tie(newType, val) = fromDTPREL(rel.type, val); 3140b57cec5SDimitry Andric switch (newType) { 3150b57cec5SDimitry Andric case R_PPC_ADDR16: 3165ffd83dbSDimitry Andric checkIntUInt(loc, val, 16, rel); 3170b57cec5SDimitry Andric write16(loc, val); 3180b57cec5SDimitry Andric break; 3190b57cec5SDimitry Andric case R_PPC_GOT16: 3200b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: 3210b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16: 3220b57cec5SDimitry Andric case R_PPC_GOT_TPREL16: 3230b57cec5SDimitry Andric case R_PPC_TPREL16: 3245ffd83dbSDimitry Andric checkInt(loc, val, 16, rel); 3250b57cec5SDimitry Andric write16(loc, val); 3260b57cec5SDimitry Andric break; 3270b57cec5SDimitry Andric case R_PPC_ADDR16_HA: 3280b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 3290b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16_HA: 3300b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16_HA: 3310b57cec5SDimitry Andric case R_PPC_GOT_TPREL16_HA: 3320b57cec5SDimitry Andric case R_PPC_REL16_HA: 3330b57cec5SDimitry Andric case R_PPC_TPREL16_HA: 3340b57cec5SDimitry Andric write16(loc, ha(val)); 3350b57cec5SDimitry Andric break; 3360b57cec5SDimitry Andric case R_PPC_ADDR16_HI: 3370b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 3380b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16_HI: 3390b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16_HI: 3400b57cec5SDimitry Andric case R_PPC_GOT_TPREL16_HI: 3410b57cec5SDimitry Andric case R_PPC_REL16_HI: 3420b57cec5SDimitry Andric case R_PPC_TPREL16_HI: 3430b57cec5SDimitry Andric write16(loc, val >> 16); 3440b57cec5SDimitry Andric break; 3450b57cec5SDimitry Andric case R_PPC_ADDR16_LO: 3460b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 3470b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16_LO: 3480b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16_LO: 3490b57cec5SDimitry Andric case R_PPC_GOT_TPREL16_LO: 3500b57cec5SDimitry Andric case R_PPC_REL16_LO: 3510b57cec5SDimitry Andric case R_PPC_TPREL16_LO: 3520b57cec5SDimitry Andric write16(loc, val); 3530b57cec5SDimitry Andric break; 3540b57cec5SDimitry Andric case R_PPC_ADDR32: 3550b57cec5SDimitry Andric case R_PPC_REL32: 3560b57cec5SDimitry Andric write32(loc, val); 3570b57cec5SDimitry Andric break; 3580b57cec5SDimitry Andric case R_PPC_REL14: { 3590b57cec5SDimitry Andric uint32_t mask = 0x0000FFFC; 3605ffd83dbSDimitry Andric checkInt(loc, val, 16, rel); 3615ffd83dbSDimitry Andric checkAlignment(loc, val, 4, rel); 3620b57cec5SDimitry Andric write32(loc, (read32(loc) & ~mask) | (val & mask)); 3630b57cec5SDimitry Andric break; 3640b57cec5SDimitry Andric } 365e8d8bef9SDimitry Andric case R_PPC_ADDR24: 3660b57cec5SDimitry Andric case R_PPC_REL24: 3670b57cec5SDimitry Andric case R_PPC_LOCAL24PC: 3680b57cec5SDimitry Andric case R_PPC_PLTREL24: { 3690b57cec5SDimitry Andric uint32_t mask = 0x03FFFFFC; 3705ffd83dbSDimitry Andric checkInt(loc, val, 26, rel); 3715ffd83dbSDimitry Andric checkAlignment(loc, val, 4, rel); 3720b57cec5SDimitry Andric write32(loc, (read32(loc) & ~mask) | (val & mask)); 3730b57cec5SDimitry Andric break; 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric default: 3760b57cec5SDimitry Andric llvm_unreachable("unknown relocation"); 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric 380e8d8bef9SDimitry Andric RelExpr PPC::adjustTlsExpr(RelType type, RelExpr expr) const { 3810b57cec5SDimitry Andric if (expr == R_RELAX_TLS_GD_TO_IE) 3820b57cec5SDimitry Andric return R_RELAX_TLS_GD_TO_IE_GOT_OFF; 3830b57cec5SDimitry Andric if (expr == R_RELAX_TLS_LD_TO_LE) 3840b57cec5SDimitry Andric return R_RELAX_TLS_LD_TO_LE_ABS; 3850b57cec5SDimitry Andric return expr; 3860b57cec5SDimitry Andric } 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric int PPC::getTlsGdRelaxSkip(RelType type) const { 3890b57cec5SDimitry Andric // A __tls_get_addr call instruction is marked with 2 relocations: 3900b57cec5SDimitry Andric // 3910b57cec5SDimitry Andric // R_PPC_TLSGD / R_PPC_TLSLD: marker relocation 3920b57cec5SDimitry Andric // R_PPC_REL24: __tls_get_addr 3930b57cec5SDimitry Andric // 3940b57cec5SDimitry Andric // After the relaxation we no longer call __tls_get_addr and should skip both 3950b57cec5SDimitry Andric // relocations to not create a false dependence on __tls_get_addr being 3960b57cec5SDimitry Andric // defined. 3970b57cec5SDimitry Andric if (type == R_PPC_TLSGD || type == R_PPC_TLSLD) 3980b57cec5SDimitry Andric return 2; 3990b57cec5SDimitry Andric return 1; 4000b57cec5SDimitry Andric } 4010b57cec5SDimitry Andric 4025ffd83dbSDimitry Andric void PPC::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 4035ffd83dbSDimitry Andric uint64_t val) const { 4045ffd83dbSDimitry Andric switch (rel.type) { 4050b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: { 4060b57cec5SDimitry Andric // addi rT, rA, x@got@tlsgd --> lwz rT, x@got@tprel(rA) 4070b57cec5SDimitry Andric uint32_t insn = readFromHalf16(loc); 4080b57cec5SDimitry Andric writeFromHalf16(loc, 0x80000000 | (insn & 0x03ff0000)); 4095ffd83dbSDimitry Andric relocateNoSym(loc, R_PPC_GOT_TPREL16, val); 4100b57cec5SDimitry Andric break; 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric case R_PPC_TLSGD: 4130b57cec5SDimitry Andric // bl __tls_get_addr(x@tldgd) --> add r3, r3, r2 4140b57cec5SDimitry Andric write32(loc, 0x7c631214); 4150b57cec5SDimitry Andric break; 4160b57cec5SDimitry Andric default: 4170b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to IE relaxation"); 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric } 4200b57cec5SDimitry Andric 4215ffd83dbSDimitry Andric void PPC::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 4225ffd83dbSDimitry Andric uint64_t val) const { 4235ffd83dbSDimitry Andric switch (rel.type) { 4240b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: 4250b57cec5SDimitry Andric // addi r3, r31, x@got@tlsgd --> addis r3, r2, x@tprel@ha 4260b57cec5SDimitry Andric writeFromHalf16(loc, 0x3c620000 | ha(val)); 4270b57cec5SDimitry Andric break; 4280b57cec5SDimitry Andric case R_PPC_TLSGD: 4290b57cec5SDimitry Andric // bl __tls_get_addr(x@tldgd) --> add r3, r3, x@tprel@l 4300b57cec5SDimitry Andric write32(loc, 0x38630000 | lo(val)); 4310b57cec5SDimitry Andric break; 4320b57cec5SDimitry Andric default: 4330b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 4340b57cec5SDimitry Andric } 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric 4375ffd83dbSDimitry Andric void PPC::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, 4385ffd83dbSDimitry Andric uint64_t val) const { 4395ffd83dbSDimitry Andric switch (rel.type) { 4400b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16: 4410b57cec5SDimitry Andric // addi r3, rA, x@got@tlsgd --> addis r3, r2, 0 4420b57cec5SDimitry Andric writeFromHalf16(loc, 0x3c620000); 4430b57cec5SDimitry Andric break; 4440b57cec5SDimitry Andric case R_PPC_TLSLD: 4450b57cec5SDimitry Andric // r3+x@dtprel computes r3+x-0x8000, while we want it to compute r3+x@tprel 4460b57cec5SDimitry Andric // = r3+x-0x7000, so add 4096 to r3. 4470b57cec5SDimitry Andric // bl __tls_get_addr(x@tlsld) --> addi r3, r3, 4096 4480b57cec5SDimitry Andric write32(loc, 0x38631000); 4490b57cec5SDimitry Andric break; 4500b57cec5SDimitry Andric case R_PPC_DTPREL16: 4510b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 4520b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 4530b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 4545ffd83dbSDimitry Andric relocate(loc, rel, val); 4550b57cec5SDimitry Andric break; 4560b57cec5SDimitry Andric default: 4570b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS LD to LE relaxation"); 4580b57cec5SDimitry Andric } 4590b57cec5SDimitry Andric } 4600b57cec5SDimitry Andric 4615ffd83dbSDimitry Andric void PPC::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 4625ffd83dbSDimitry Andric uint64_t val) const { 4635ffd83dbSDimitry Andric switch (rel.type) { 4640b57cec5SDimitry Andric case R_PPC_GOT_TPREL16: { 4650b57cec5SDimitry Andric // lwz rT, x@got@tprel(rA) --> addis rT, r2, x@tprel@ha 4660b57cec5SDimitry Andric uint32_t rt = readFromHalf16(loc) & 0x03e00000; 4670b57cec5SDimitry Andric writeFromHalf16(loc, 0x3c020000 | rt | ha(val)); 4680b57cec5SDimitry Andric break; 4690b57cec5SDimitry Andric } 4700b57cec5SDimitry Andric case R_PPC_TLS: { 4710b57cec5SDimitry Andric uint32_t insn = read32(loc); 4720b57cec5SDimitry Andric if (insn >> 26 != 31) 4730b57cec5SDimitry Andric error("unrecognized instruction for IE to LE R_PPC_TLS"); 4740b57cec5SDimitry Andric // addi rT, rT, x@tls --> addi rT, rT, x@tprel@l 4750b57cec5SDimitry Andric uint32_t dFormOp = getPPCDFormOp((read32(loc) & 0x000007fe) >> 1); 4760b57cec5SDimitry Andric if (dFormOp == 0) 4770b57cec5SDimitry Andric error("unrecognized instruction for IE to LE R_PPC_TLS"); 4780b57cec5SDimitry Andric write32(loc, (dFormOp << 26) | (insn & 0x03ff0000) | lo(val)); 4790b57cec5SDimitry Andric break; 4800b57cec5SDimitry Andric } 4810b57cec5SDimitry Andric default: 4820b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS IE to LE relaxation"); 4830b57cec5SDimitry Andric } 4840b57cec5SDimitry Andric } 4850b57cec5SDimitry Andric 4865ffd83dbSDimitry Andric TargetInfo *elf::getPPCTargetInfo() { 4870b57cec5SDimitry Andric static PPC target; 4880b57cec5SDimitry Andric return ⌖ 4890b57cec5SDimitry Andric } 490