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) { 70*13138422SDimitry Andric // Create canonical PLT entries for non-PIE code. Compilers don't generate 71*13138422SDimitry Andric // non-GOT-non-PLT relocations referencing external functions for -fpie/-fPIE. 72*13138422SDimitry Andric uint32_t glink = in.plt->getVA(); // VA of .glink 73*13138422SDimitry Andric if (!config->isPic) { 74*13138422SDimitry Andric for (const Symbol *sym : in.plt->entries) 75*13138422SDimitry Andric if (sym->needsPltAddr) { 76*13138422SDimitry Andric writePPC32PltCallStub(buf, sym->getGotPltVA(), nullptr, 0); 77*13138422SDimitry Andric buf += 16; 78*13138422SDimitry Andric glink += 16; 79*13138422SDimitry Andric } 80*13138422SDimitry Andric } 81*13138422SDimitry Andric 820b57cec5SDimitry Andric // On PPC Secure PLT ABI, bl foo@plt jumps to a call stub, which loads an 830b57cec5SDimitry Andric // absolute address from a specific .plt slot (usually called .got.plt on 840b57cec5SDimitry Andric // other targets) and jumps there. 850b57cec5SDimitry Andric // 860b57cec5SDimitry Andric // a) With immediate binding (BIND_NOW), the .plt entry is resolved at load 870b57cec5SDimitry Andric // time. The .glink section is not used. 880b57cec5SDimitry Andric // b) With lazy binding, the .plt entry points to a `b PLTresolve` 890b57cec5SDimitry Andric // instruction in .glink, filled in by PPC::writeGotPlt(). 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric // Write N `b PLTresolve` first. 920b57cec5SDimitry Andric for (size_t i = 0; i != numEntries; ++i) 930b57cec5SDimitry Andric write32(buf + 4 * i, 0x48000000 | 4 * (numEntries - i)); 940b57cec5SDimitry Andric buf += 4 * numEntries; 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric // Then write PLTresolve(), which has two forms: PIC and non-PIC. PLTresolve() 970b57cec5SDimitry Andric // computes the PLT index (by computing the distance from the landing b to 980b57cec5SDimitry Andric // itself) and calls _dl_runtime_resolve() (in glibc). 990b57cec5SDimitry Andric uint32_t got = in.got->getVA(); 1000b57cec5SDimitry Andric const uint8_t *end = buf + 64; 1010b57cec5SDimitry Andric if (config->isPic) { 102*13138422SDimitry Andric uint32_t afterBcl = 4 * in.plt->getNumEntries() + 12; 1030b57cec5SDimitry Andric uint32_t gotBcl = got + 4 - (glink + afterBcl); 1040b57cec5SDimitry Andric write32(buf + 0, 0x3d6b0000 | ha(afterBcl)); // addis r11,r11,1f-glink@ha 1050b57cec5SDimitry Andric write32(buf + 4, 0x7c0802a6); // mflr r0 1060b57cec5SDimitry Andric write32(buf + 8, 0x429f0005); // bcl 20,30,.+4 107*13138422SDimitry Andric write32(buf + 12, 0x396b0000 | lo(afterBcl)); // 1: addi r11,r11,1b-glink@l 1080b57cec5SDimitry Andric write32(buf + 16, 0x7d8802a6); // mflr r12 1090b57cec5SDimitry Andric write32(buf + 20, 0x7c0803a6); // mtlr r0 1100b57cec5SDimitry Andric write32(buf + 24, 0x7d6c5850); // sub r11,r11,r12 1110b57cec5SDimitry Andric write32(buf + 28, 0x3d8c0000 | ha(gotBcl)); // addis 12,12,GOT+4-1b@ha 1120b57cec5SDimitry Andric if (ha(gotBcl) == ha(gotBcl + 4)) { 1130b57cec5SDimitry Andric write32(buf + 32, 0x800c0000 | lo(gotBcl)); // lwz r0,r12,GOT+4-1b@l(r12) 1140b57cec5SDimitry Andric write32(buf + 36, 1150b57cec5SDimitry Andric 0x818c0000 | lo(gotBcl + 4)); // lwz r12,r12,GOT+8-1b@l(r12) 1160b57cec5SDimitry Andric } else { 1170b57cec5SDimitry Andric write32(buf + 32, 0x840c0000 | lo(gotBcl)); // lwzu r0,r12,GOT+4-1b@l(r12) 1180b57cec5SDimitry Andric write32(buf + 36, 0x818c0000 | 4); // lwz r12,r12,4(r12) 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric write32(buf + 40, 0x7c0903a6); // mtctr 0 1210b57cec5SDimitry Andric write32(buf + 44, 0x7c0b5a14); // add r0,11,11 1220b57cec5SDimitry Andric write32(buf + 48, 0x7d605a14); // add r11,0,11 1230b57cec5SDimitry Andric write32(buf + 52, 0x4e800420); // bctr 1240b57cec5SDimitry Andric buf += 56; 1250b57cec5SDimitry Andric } else { 1260b57cec5SDimitry Andric write32(buf + 0, 0x3d800000 | ha(got + 4)); // lis r12,GOT+4@ha 127*13138422SDimitry Andric write32(buf + 4, 0x3d6b0000 | ha(-glink)); // addis r11,r11,-glink@ha 1280b57cec5SDimitry Andric if (ha(got + 4) == ha(got + 8)) 1290b57cec5SDimitry Andric write32(buf + 8, 0x800c0000 | lo(got + 4)); // lwz r0,GOT+4@l(r12) 1300b57cec5SDimitry Andric else 1310b57cec5SDimitry Andric write32(buf + 8, 0x840c0000 | lo(got + 4)); // lwzu r0,GOT+4@l(r12) 132*13138422SDimitry Andric write32(buf + 12, 0x396b0000 | lo(-glink)); // addi r11,r11,-glink@l 1330b57cec5SDimitry Andric write32(buf + 16, 0x7c0903a6); // mtctr r0 1340b57cec5SDimitry Andric write32(buf + 20, 0x7c0b5a14); // add r0,r11,r11 1350b57cec5SDimitry Andric if (ha(got + 4) == ha(got + 8)) 136*13138422SDimitry Andric write32(buf + 24, 0x818c0000 | lo(got + 8)); // lwz r12,GOT+8@l(r12) 1370b57cec5SDimitry Andric else 1380b57cec5SDimitry Andric write32(buf + 24, 0x818c0000 | 4); // lwz r12,4(r12) 1390b57cec5SDimitry Andric write32(buf + 28, 0x7d605a14); // add r11,r0,r11 1400b57cec5SDimitry Andric write32(buf + 32, 0x4e800420); // bctr 1410b57cec5SDimitry Andric buf += 36; 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric // Pad with nop. They should not be executed. 1450b57cec5SDimitry Andric for (; buf < end; buf += 4) 1460b57cec5SDimitry Andric write32(buf, 0x60000000); 1470b57cec5SDimitry Andric } 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric PPC::PPC() { 15055e4f9d5SDimitry Andric copyRel = R_PPC_COPY; 1510b57cec5SDimitry Andric gotRel = R_PPC_GLOB_DAT; 1520b57cec5SDimitry Andric noneRel = R_PPC_NONE; 1530b57cec5SDimitry Andric pltRel = R_PPC_JMP_SLOT; 1540b57cec5SDimitry Andric relativeRel = R_PPC_RELATIVE; 1550b57cec5SDimitry Andric iRelativeRel = R_PPC_IRELATIVE; 1560b57cec5SDimitry Andric symbolicRel = R_PPC_ADDR32; 1570b57cec5SDimitry Andric gotBaseSymInGotPlt = false; 1580b57cec5SDimitry Andric gotHeaderEntriesNum = 3; 1590b57cec5SDimitry Andric gotPltHeaderEntriesNum = 0; 160*13138422SDimitry Andric pltHeaderSize = 0; 1610b57cec5SDimitry Andric pltEntrySize = 4; 162480093f4SDimitry Andric ipltEntrySize = 16; 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric needsThunks = true; 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric tlsModuleIndexRel = R_PPC_DTPMOD32; 1670b57cec5SDimitry Andric tlsOffsetRel = R_PPC_DTPREL32; 1680b57cec5SDimitry Andric tlsGotRel = R_PPC_TPREL32; 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric defaultMaxPageSize = 65536; 1710b57cec5SDimitry Andric defaultImageBase = 0x10000000; 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric write32(trapInstr.data(), 0x7fe00008); 1740b57cec5SDimitry Andric } 1750b57cec5SDimitry Andric 176480093f4SDimitry Andric void PPC::writeIplt(uint8_t *buf, const Symbol &sym, 177480093f4SDimitry Andric uint64_t /*pltEntryAddr*/) const { 178480093f4SDimitry Andric // In -pie or -shared mode, assume r30 points to .got2+0x8000, and use a 179480093f4SDimitry Andric // .got2.plt_pic32. thunk. 180480093f4SDimitry Andric writePPC32PltCallStub(buf, sym.getGotPltVA(), sym.file, 0x8000); 181480093f4SDimitry Andric } 182480093f4SDimitry Andric 1830b57cec5SDimitry Andric void PPC::writeGotHeader(uint8_t *buf) const { 1840b57cec5SDimitry Andric // _GLOBAL_OFFSET_TABLE_[0] = _DYNAMIC 1850b57cec5SDimitry Andric // glibc stores _dl_runtime_resolve in _GLOBAL_OFFSET_TABLE_[1], 1860b57cec5SDimitry Andric // link_map in _GLOBAL_OFFSET_TABLE_[2]. 1870b57cec5SDimitry Andric write32(buf, mainPart->dynamic->getVA()); 1880b57cec5SDimitry Andric } 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric void PPC::writeGotPlt(uint8_t *buf, const Symbol &s) const { 1910b57cec5SDimitry Andric // Address of the symbol resolver stub in .glink . 192*13138422SDimitry Andric write32(buf, in.plt->getVA() + in.plt->headerSize + 4 * s.pltIndex); 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric bool PPC::needsThunk(RelExpr expr, RelType type, const InputFile *file, 196*13138422SDimitry Andric uint64_t branchAddr, const Symbol &s, int64_t a) const { 197*13138422SDimitry Andric if (type != R_PPC_LOCAL24PC && type != R_PPC_REL24 && type != R_PPC_PLTREL24) 1980b57cec5SDimitry Andric return false; 1990b57cec5SDimitry Andric if (s.isInPlt()) 2000b57cec5SDimitry Andric return true; 2010b57cec5SDimitry Andric if (s.isUndefWeak()) 2020b57cec5SDimitry Andric return false; 203*13138422SDimitry Andric return !PPC::inBranchRange(type, branchAddr, s.getVA(a)); 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric uint32_t PPC::getThunkSectionSpacing() const { return 0x2000000; } 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric bool PPC::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 2090b57cec5SDimitry Andric uint64_t offset = dst - src; 210*13138422SDimitry Andric if (type == R_PPC_LOCAL24PC || type == R_PPC_REL24 || type == R_PPC_PLTREL24) 2110b57cec5SDimitry Andric return isInt<26>(offset); 2120b57cec5SDimitry Andric llvm_unreachable("unsupported relocation type used in branch"); 2130b57cec5SDimitry Andric } 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric RelExpr PPC::getRelExpr(RelType type, const Symbol &s, 2160b57cec5SDimitry Andric const uint8_t *loc) const { 2170b57cec5SDimitry Andric switch (type) { 2180b57cec5SDimitry Andric case R_PPC_NONE: 2190b57cec5SDimitry Andric return R_NONE; 2200b57cec5SDimitry Andric case R_PPC_ADDR16_HA: 2210b57cec5SDimitry Andric case R_PPC_ADDR16_HI: 2220b57cec5SDimitry Andric case R_PPC_ADDR16_LO: 2230b57cec5SDimitry Andric case R_PPC_ADDR32: 2240b57cec5SDimitry Andric return R_ABS; 2250b57cec5SDimitry Andric case R_PPC_DTPREL16: 2260b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 2270b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 2280b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 2290b57cec5SDimitry Andric case R_PPC_DTPREL32: 2300b57cec5SDimitry Andric return R_DTPREL; 2310b57cec5SDimitry Andric case R_PPC_REL14: 2320b57cec5SDimitry Andric case R_PPC_REL32: 2330b57cec5SDimitry Andric case R_PPC_REL16_LO: 2340b57cec5SDimitry Andric case R_PPC_REL16_HI: 2350b57cec5SDimitry Andric case R_PPC_REL16_HA: 2360b57cec5SDimitry Andric return R_PC; 2370b57cec5SDimitry Andric case R_PPC_GOT16: 2380b57cec5SDimitry Andric return R_GOT_OFF; 239*13138422SDimitry Andric case R_PPC_LOCAL24PC: 2400b57cec5SDimitry Andric case R_PPC_REL24: 2410b57cec5SDimitry Andric return R_PLT_PC; 2420b57cec5SDimitry Andric case R_PPC_PLTREL24: 2430b57cec5SDimitry Andric return R_PPC32_PLTREL; 2440b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: 2450b57cec5SDimitry Andric return R_TLSGD_GOT; 2460b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16: 2470b57cec5SDimitry Andric return R_TLSLD_GOT; 2480b57cec5SDimitry Andric case R_PPC_GOT_TPREL16: 2490b57cec5SDimitry Andric return R_GOT_OFF; 2500b57cec5SDimitry Andric case R_PPC_TLS: 2510b57cec5SDimitry Andric return R_TLSIE_HINT; 2520b57cec5SDimitry Andric case R_PPC_TLSGD: 2530b57cec5SDimitry Andric return R_TLSDESC_CALL; 2540b57cec5SDimitry Andric case R_PPC_TLSLD: 2550b57cec5SDimitry Andric return R_TLSLD_HINT; 2560b57cec5SDimitry Andric case R_PPC_TPREL16: 2570b57cec5SDimitry Andric case R_PPC_TPREL16_HA: 2580b57cec5SDimitry Andric case R_PPC_TPREL16_LO: 2590b57cec5SDimitry Andric case R_PPC_TPREL16_HI: 2600b57cec5SDimitry Andric return R_TLS; 2610b57cec5SDimitry Andric default: 2620b57cec5SDimitry Andric error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 2630b57cec5SDimitry Andric ") against symbol " + toString(s)); 2640b57cec5SDimitry Andric return R_NONE; 2650b57cec5SDimitry Andric } 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric RelType PPC::getDynRel(RelType type) const { 2690b57cec5SDimitry Andric if (type == R_PPC_ADDR32) 2700b57cec5SDimitry Andric return type; 2710b57cec5SDimitry Andric return R_PPC_NONE; 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric static std::pair<RelType, uint64_t> fromDTPREL(RelType type, uint64_t val) { 2750b57cec5SDimitry Andric uint64_t dtpBiasedVal = val - 0x8000; 2760b57cec5SDimitry Andric switch (type) { 2770b57cec5SDimitry Andric case R_PPC_DTPREL16: 2780b57cec5SDimitry Andric return {R_PPC64_ADDR16, dtpBiasedVal}; 2790b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 2800b57cec5SDimitry Andric return {R_PPC_ADDR16_HA, dtpBiasedVal}; 2810b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 2820b57cec5SDimitry Andric return {R_PPC_ADDR16_HI, dtpBiasedVal}; 2830b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 2840b57cec5SDimitry Andric return {R_PPC_ADDR16_LO, dtpBiasedVal}; 2850b57cec5SDimitry Andric case R_PPC_DTPREL32: 2860b57cec5SDimitry Andric return {R_PPC_ADDR32, dtpBiasedVal}; 2870b57cec5SDimitry Andric default: 2880b57cec5SDimitry Andric return {type, val}; 2890b57cec5SDimitry Andric } 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric void PPC::relocateOne(uint8_t *loc, RelType type, uint64_t val) const { 2930b57cec5SDimitry Andric RelType newType; 2940b57cec5SDimitry Andric std::tie(newType, val) = fromDTPREL(type, val); 2950b57cec5SDimitry Andric switch (newType) { 2960b57cec5SDimitry Andric case R_PPC_ADDR16: 2970b57cec5SDimitry Andric checkIntUInt(loc, val, 16, type); 2980b57cec5SDimitry Andric write16(loc, val); 2990b57cec5SDimitry Andric break; 3000b57cec5SDimitry Andric case R_PPC_GOT16: 3010b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: 3020b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16: 3030b57cec5SDimitry Andric case R_PPC_GOT_TPREL16: 3040b57cec5SDimitry Andric case R_PPC_TPREL16: 3050b57cec5SDimitry Andric checkInt(loc, val, 16, type); 3060b57cec5SDimitry Andric write16(loc, val); 3070b57cec5SDimitry Andric break; 3080b57cec5SDimitry Andric case R_PPC_ADDR16_HA: 3090b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 3100b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16_HA: 3110b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16_HA: 3120b57cec5SDimitry Andric case R_PPC_GOT_TPREL16_HA: 3130b57cec5SDimitry Andric case R_PPC_REL16_HA: 3140b57cec5SDimitry Andric case R_PPC_TPREL16_HA: 3150b57cec5SDimitry Andric write16(loc, ha(val)); 3160b57cec5SDimitry Andric break; 3170b57cec5SDimitry Andric case R_PPC_ADDR16_HI: 3180b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 3190b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16_HI: 3200b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16_HI: 3210b57cec5SDimitry Andric case R_PPC_GOT_TPREL16_HI: 3220b57cec5SDimitry Andric case R_PPC_REL16_HI: 3230b57cec5SDimitry Andric case R_PPC_TPREL16_HI: 3240b57cec5SDimitry Andric write16(loc, val >> 16); 3250b57cec5SDimitry Andric break; 3260b57cec5SDimitry Andric case R_PPC_ADDR16_LO: 3270b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 3280b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16_LO: 3290b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16_LO: 3300b57cec5SDimitry Andric case R_PPC_GOT_TPREL16_LO: 3310b57cec5SDimitry Andric case R_PPC_REL16_LO: 3320b57cec5SDimitry Andric case R_PPC_TPREL16_LO: 3330b57cec5SDimitry Andric write16(loc, val); 3340b57cec5SDimitry Andric break; 3350b57cec5SDimitry Andric case R_PPC_ADDR32: 3360b57cec5SDimitry Andric case R_PPC_REL32: 3370b57cec5SDimitry Andric write32(loc, val); 3380b57cec5SDimitry Andric break; 3390b57cec5SDimitry Andric case R_PPC_REL14: { 3400b57cec5SDimitry Andric uint32_t mask = 0x0000FFFC; 3410b57cec5SDimitry Andric checkInt(loc, val, 16, type); 3420b57cec5SDimitry Andric checkAlignment(loc, val, 4, type); 3430b57cec5SDimitry Andric write32(loc, (read32(loc) & ~mask) | (val & mask)); 3440b57cec5SDimitry Andric break; 3450b57cec5SDimitry Andric } 3460b57cec5SDimitry Andric case R_PPC_REL24: 3470b57cec5SDimitry Andric case R_PPC_LOCAL24PC: 3480b57cec5SDimitry Andric case R_PPC_PLTREL24: { 3490b57cec5SDimitry Andric uint32_t mask = 0x03FFFFFC; 3500b57cec5SDimitry Andric checkInt(loc, val, 26, type); 3510b57cec5SDimitry Andric checkAlignment(loc, val, 4, type); 3520b57cec5SDimitry Andric write32(loc, (read32(loc) & ~mask) | (val & mask)); 3530b57cec5SDimitry Andric break; 3540b57cec5SDimitry Andric } 3550b57cec5SDimitry Andric default: 3560b57cec5SDimitry Andric llvm_unreachable("unknown relocation"); 3570b57cec5SDimitry Andric } 3580b57cec5SDimitry Andric } 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric RelExpr PPC::adjustRelaxExpr(RelType type, const uint8_t *data, 3610b57cec5SDimitry Andric RelExpr expr) const { 3620b57cec5SDimitry Andric if (expr == R_RELAX_TLS_GD_TO_IE) 3630b57cec5SDimitry Andric return R_RELAX_TLS_GD_TO_IE_GOT_OFF; 3640b57cec5SDimitry Andric if (expr == R_RELAX_TLS_LD_TO_LE) 3650b57cec5SDimitry Andric return R_RELAX_TLS_LD_TO_LE_ABS; 3660b57cec5SDimitry Andric return expr; 3670b57cec5SDimitry Andric } 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric int PPC::getTlsGdRelaxSkip(RelType type) const { 3700b57cec5SDimitry Andric // A __tls_get_addr call instruction is marked with 2 relocations: 3710b57cec5SDimitry Andric // 3720b57cec5SDimitry Andric // R_PPC_TLSGD / R_PPC_TLSLD: marker relocation 3730b57cec5SDimitry Andric // R_PPC_REL24: __tls_get_addr 3740b57cec5SDimitry Andric // 3750b57cec5SDimitry Andric // After the relaxation we no longer call __tls_get_addr and should skip both 3760b57cec5SDimitry Andric // relocations to not create a false dependence on __tls_get_addr being 3770b57cec5SDimitry Andric // defined. 3780b57cec5SDimitry Andric if (type == R_PPC_TLSGD || type == R_PPC_TLSLD) 3790b57cec5SDimitry Andric return 2; 3800b57cec5SDimitry Andric return 1; 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric void PPC::relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const { 3840b57cec5SDimitry Andric switch (type) { 3850b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: { 3860b57cec5SDimitry Andric // addi rT, rA, x@got@tlsgd --> lwz rT, x@got@tprel(rA) 3870b57cec5SDimitry Andric uint32_t insn = readFromHalf16(loc); 3880b57cec5SDimitry Andric writeFromHalf16(loc, 0x80000000 | (insn & 0x03ff0000)); 3890b57cec5SDimitry Andric relocateOne(loc, R_PPC_GOT_TPREL16, val); 3900b57cec5SDimitry Andric break; 3910b57cec5SDimitry Andric } 3920b57cec5SDimitry Andric case R_PPC_TLSGD: 3930b57cec5SDimitry Andric // bl __tls_get_addr(x@tldgd) --> add r3, r3, r2 3940b57cec5SDimitry Andric write32(loc, 0x7c631214); 3950b57cec5SDimitry Andric break; 3960b57cec5SDimitry Andric default: 3970b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to IE relaxation"); 3980b57cec5SDimitry Andric } 3990b57cec5SDimitry Andric } 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric void PPC::relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const { 4020b57cec5SDimitry Andric switch (type) { 4030b57cec5SDimitry Andric case R_PPC_GOT_TLSGD16: 4040b57cec5SDimitry Andric // addi r3, r31, x@got@tlsgd --> addis r3, r2, x@tprel@ha 4050b57cec5SDimitry Andric writeFromHalf16(loc, 0x3c620000 | ha(val)); 4060b57cec5SDimitry Andric break; 4070b57cec5SDimitry Andric case R_PPC_TLSGD: 4080b57cec5SDimitry Andric // bl __tls_get_addr(x@tldgd) --> add r3, r3, x@tprel@l 4090b57cec5SDimitry Andric write32(loc, 0x38630000 | lo(val)); 4100b57cec5SDimitry Andric break; 4110b57cec5SDimitry Andric default: 4120b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 4130b57cec5SDimitry Andric } 4140b57cec5SDimitry Andric } 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric void PPC::relaxTlsLdToLe(uint8_t *loc, RelType type, uint64_t val) const { 4170b57cec5SDimitry Andric switch (type) { 4180b57cec5SDimitry Andric case R_PPC_GOT_TLSLD16: 4190b57cec5SDimitry Andric // addi r3, rA, x@got@tlsgd --> addis r3, r2, 0 4200b57cec5SDimitry Andric writeFromHalf16(loc, 0x3c620000); 4210b57cec5SDimitry Andric break; 4220b57cec5SDimitry Andric case R_PPC_TLSLD: 4230b57cec5SDimitry Andric // r3+x@dtprel computes r3+x-0x8000, while we want it to compute r3+x@tprel 4240b57cec5SDimitry Andric // = r3+x-0x7000, so add 4096 to r3. 4250b57cec5SDimitry Andric // bl __tls_get_addr(x@tlsld) --> addi r3, r3, 4096 4260b57cec5SDimitry Andric write32(loc, 0x38631000); 4270b57cec5SDimitry Andric break; 4280b57cec5SDimitry Andric case R_PPC_DTPREL16: 4290b57cec5SDimitry Andric case R_PPC_DTPREL16_HA: 4300b57cec5SDimitry Andric case R_PPC_DTPREL16_HI: 4310b57cec5SDimitry Andric case R_PPC_DTPREL16_LO: 4320b57cec5SDimitry Andric relocateOne(loc, type, val); 4330b57cec5SDimitry Andric break; 4340b57cec5SDimitry Andric default: 4350b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS LD to LE relaxation"); 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric } 4380b57cec5SDimitry Andric 4390b57cec5SDimitry Andric void PPC::relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const { 4400b57cec5SDimitry Andric switch (type) { 4410b57cec5SDimitry Andric case R_PPC_GOT_TPREL16: { 4420b57cec5SDimitry Andric // lwz rT, x@got@tprel(rA) --> addis rT, r2, x@tprel@ha 4430b57cec5SDimitry Andric uint32_t rt = readFromHalf16(loc) & 0x03e00000; 4440b57cec5SDimitry Andric writeFromHalf16(loc, 0x3c020000 | rt | ha(val)); 4450b57cec5SDimitry Andric break; 4460b57cec5SDimitry Andric } 4470b57cec5SDimitry Andric case R_PPC_TLS: { 4480b57cec5SDimitry Andric uint32_t insn = read32(loc); 4490b57cec5SDimitry Andric if (insn >> 26 != 31) 4500b57cec5SDimitry Andric error("unrecognized instruction for IE to LE R_PPC_TLS"); 4510b57cec5SDimitry Andric // addi rT, rT, x@tls --> addi rT, rT, x@tprel@l 4520b57cec5SDimitry Andric uint32_t dFormOp = getPPCDFormOp((read32(loc) & 0x000007fe) >> 1); 4530b57cec5SDimitry Andric if (dFormOp == 0) 4540b57cec5SDimitry Andric error("unrecognized instruction for IE to LE R_PPC_TLS"); 4550b57cec5SDimitry Andric write32(loc, (dFormOp << 26) | (insn & 0x03ff0000) | lo(val)); 4560b57cec5SDimitry Andric break; 4570b57cec5SDimitry Andric } 4580b57cec5SDimitry Andric default: 4590b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS IE to LE relaxation"); 4600b57cec5SDimitry Andric } 4610b57cec5SDimitry Andric } 4620b57cec5SDimitry Andric 46385868e8aSDimitry Andric TargetInfo *getPPCTargetInfo() { 4640b57cec5SDimitry Andric static PPC target; 4650b57cec5SDimitry Andric return ⌖ 4660b57cec5SDimitry Andric } 46785868e8aSDimitry Andric 46885868e8aSDimitry Andric } // namespace elf 46985868e8aSDimitry Andric } // namespace lld 470