Path: blob/main/contrib/llvm-project/lld/ELF/Arch/PPC.cpp
34878 views
//===- PPC.cpp ------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "OutputSections.h"9#include "Symbols.h"10#include "SyntheticSections.h"11#include "Target.h"12#include "Thunks.h"13#include "lld/Common/ErrorHandler.h"14#include "llvm/Support/Endian.h"1516using namespace llvm;17using namespace llvm::support::endian;18using namespace llvm::ELF;19using namespace lld;20using namespace lld::elf;2122// Undefine the macro predefined by GCC powerpc32.23#undef PPC2425namespace {26class PPC final : public TargetInfo {27public:28PPC();29RelExpr getRelExpr(RelType type, const Symbol &s,30const uint8_t *loc) const override;31RelType getDynRel(RelType type) const override;32int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;33void writeGotHeader(uint8_t *buf) const override;34void writePltHeader(uint8_t *buf) const override {35llvm_unreachable("should call writePPC32GlinkSection() instead");36}37void writePlt(uint8_t *buf, const Symbol &sym,38uint64_t pltEntryAddr) const override {39llvm_unreachable("should call writePPC32GlinkSection() instead");40}41void writeIplt(uint8_t *buf, const Symbol &sym,42uint64_t pltEntryAddr) const override;43void writeGotPlt(uint8_t *buf, const Symbol &s) const override;44bool needsThunk(RelExpr expr, RelType relocType, const InputFile *file,45uint64_t branchAddr, const Symbol &s,46int64_t a) const override;47uint32_t getThunkSectionSpacing() const override;48bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;49void relocate(uint8_t *loc, const Relocation &rel,50uint64_t val) const override;51RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;52int getTlsGdRelaxSkip(RelType type) const override;53void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;5455private:56void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;57void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;58void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;59void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;60};61} // namespace6263static uint16_t lo(uint32_t v) { return v; }64static uint16_t ha(uint32_t v) { return (v + 0x8000) >> 16; }6566static uint32_t readFromHalf16(const uint8_t *loc) {67return read32(config->isLE ? loc : loc - 2);68}6970static void writeFromHalf16(uint8_t *loc, uint32_t insn) {71write32(config->isLE ? loc : loc - 2, insn);72}7374void elf::writePPC32GlinkSection(uint8_t *buf, size_t numEntries) {75// Create canonical PLT entries for non-PIE code. Compilers don't generate76// non-GOT-non-PLT relocations referencing external functions for -fpie/-fPIE.77uint32_t glink = in.plt->getVA(); // VA of .glink78if (!config->isPic) {79for (const Symbol *sym : cast<PPC32GlinkSection>(*in.plt).canonical_plts) {80writePPC32PltCallStub(buf, sym->getGotPltVA(), nullptr, 0);81buf += 16;82glink += 16;83}84}8586// On PPC Secure PLT ABI, bl foo@plt jumps to a call stub, which loads an87// absolute address from a specific .plt slot (usually called .got.plt on88// other targets) and jumps there.89//90// a) With immediate binding (BIND_NOW), the .plt entry is resolved at load91// time. The .glink section is not used.92// b) With lazy binding, the .plt entry points to a `b PLTresolve`93// instruction in .glink, filled in by PPC::writeGotPlt().9495// Write N `b PLTresolve` first.96for (size_t i = 0; i != numEntries; ++i)97write32(buf + 4 * i, 0x48000000 | 4 * (numEntries - i));98buf += 4 * numEntries;99100// Then write PLTresolve(), which has two forms: PIC and non-PIC. PLTresolve()101// computes the PLT index (by computing the distance from the landing b to102// itself) and calls _dl_runtime_resolve() (in glibc).103uint32_t got = in.got->getVA();104const uint8_t *end = buf + 64;105if (config->isPic) {106uint32_t afterBcl = 4 * in.plt->getNumEntries() + 12;107uint32_t gotBcl = got + 4 - (glink + afterBcl);108write32(buf + 0, 0x3d6b0000 | ha(afterBcl)); // addis r11,r11,1f-glink@ha109write32(buf + 4, 0x7c0802a6); // mflr r0110write32(buf + 8, 0x429f0005); // bcl 20,30,.+4111write32(buf + 12, 0x396b0000 | lo(afterBcl)); // 1: addi r11,r11,1b-glink@l112write32(buf + 16, 0x7d8802a6); // mflr r12113write32(buf + 20, 0x7c0803a6); // mtlr r0114write32(buf + 24, 0x7d6c5850); // sub r11,r11,r12115write32(buf + 28, 0x3d8c0000 | ha(gotBcl)); // addis 12,12,GOT+4-1b@ha116if (ha(gotBcl) == ha(gotBcl + 4)) {117write32(buf + 32, 0x800c0000 | lo(gotBcl)); // lwz r0,r12,GOT+4-1b@l(r12)118write32(buf + 36,1190x818c0000 | lo(gotBcl + 4)); // lwz r12,r12,GOT+8-1b@l(r12)120} else {121write32(buf + 32, 0x840c0000 | lo(gotBcl)); // lwzu r0,r12,GOT+4-1b@l(r12)122write32(buf + 36, 0x818c0000 | 4); // lwz r12,r12,4(r12)123}124write32(buf + 40, 0x7c0903a6); // mtctr 0125write32(buf + 44, 0x7c0b5a14); // add r0,11,11126write32(buf + 48, 0x7d605a14); // add r11,0,11127write32(buf + 52, 0x4e800420); // bctr128buf += 56;129} else {130write32(buf + 0, 0x3d800000 | ha(got + 4)); // lis r12,GOT+4@ha131write32(buf + 4, 0x3d6b0000 | ha(-glink)); // addis r11,r11,-glink@ha132if (ha(got + 4) == ha(got + 8))133write32(buf + 8, 0x800c0000 | lo(got + 4)); // lwz r0,GOT+4@l(r12)134else135write32(buf + 8, 0x840c0000 | lo(got + 4)); // lwzu r0,GOT+4@l(r12)136write32(buf + 12, 0x396b0000 | lo(-glink)); // addi r11,r11,-glink@l137write32(buf + 16, 0x7c0903a6); // mtctr r0138write32(buf + 20, 0x7c0b5a14); // add r0,r11,r11139if (ha(got + 4) == ha(got + 8))140write32(buf + 24, 0x818c0000 | lo(got + 8)); // lwz r12,GOT+8@l(r12)141else142write32(buf + 24, 0x818c0000 | 4); // lwz r12,4(r12)143write32(buf + 28, 0x7d605a14); // add r11,r0,r11144write32(buf + 32, 0x4e800420); // bctr145buf += 36;146}147148// Pad with nop. They should not be executed.149for (; buf < end; buf += 4)150write32(buf, 0x60000000);151}152153PPC::PPC() {154copyRel = R_PPC_COPY;155gotRel = R_PPC_GLOB_DAT;156pltRel = R_PPC_JMP_SLOT;157relativeRel = R_PPC_RELATIVE;158iRelativeRel = R_PPC_IRELATIVE;159symbolicRel = R_PPC_ADDR32;160gotHeaderEntriesNum = 3;161gotPltHeaderEntriesNum = 0;162pltHeaderSize = 0;163pltEntrySize = 4;164ipltEntrySize = 16;165166needsThunks = true;167168tlsModuleIndexRel = R_PPC_DTPMOD32;169tlsOffsetRel = R_PPC_DTPREL32;170tlsGotRel = R_PPC_TPREL32;171172defaultMaxPageSize = 65536;173defaultImageBase = 0x10000000;174175write32(trapInstr.data(), 0x7fe00008);176}177178void PPC::writeIplt(uint8_t *buf, const Symbol &sym,179uint64_t /*pltEntryAddr*/) const {180// In -pie or -shared mode, assume r30 points to .got2+0x8000, and use a181// .got2.plt_pic32. thunk.182writePPC32PltCallStub(buf, sym.getGotPltVA(), sym.file, 0x8000);183}184185void PPC::writeGotHeader(uint8_t *buf) const {186// _GLOBAL_OFFSET_TABLE_[0] = _DYNAMIC187// glibc stores _dl_runtime_resolve in _GLOBAL_OFFSET_TABLE_[1],188// link_map in _GLOBAL_OFFSET_TABLE_[2].189write32(buf, mainPart->dynamic->getVA());190}191192void PPC::writeGotPlt(uint8_t *buf, const Symbol &s) const {193// Address of the symbol resolver stub in .glink .194write32(buf, in.plt->getVA() + in.plt->headerSize + 4 * s.getPltIdx());195}196197bool PPC::needsThunk(RelExpr expr, RelType type, const InputFile *file,198uint64_t branchAddr, const Symbol &s, int64_t a) const {199if (type != R_PPC_LOCAL24PC && type != R_PPC_REL24 && type != R_PPC_PLTREL24)200return false;201if (s.isInPlt())202return true;203if (s.isUndefWeak())204return false;205return !PPC::inBranchRange(type, branchAddr, s.getVA(a));206}207208uint32_t PPC::getThunkSectionSpacing() const { return 0x2000000; }209210bool PPC::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {211uint64_t offset = dst - src;212if (type == R_PPC_LOCAL24PC || type == R_PPC_REL24 || type == R_PPC_PLTREL24)213return isInt<26>(offset);214llvm_unreachable("unsupported relocation type used in branch");215}216217RelExpr PPC::getRelExpr(RelType type, const Symbol &s,218const uint8_t *loc) const {219switch (type) {220case R_PPC_NONE:221return R_NONE;222case R_PPC_ADDR16_HA:223case R_PPC_ADDR16_HI:224case R_PPC_ADDR16_LO:225case R_PPC_ADDR24:226case R_PPC_ADDR32:227return R_ABS;228case R_PPC_DTPREL16:229case R_PPC_DTPREL16_HA:230case R_PPC_DTPREL16_HI:231case R_PPC_DTPREL16_LO:232case R_PPC_DTPREL32:233return R_DTPREL;234case R_PPC_REL14:235case R_PPC_REL32:236case R_PPC_REL16_LO:237case R_PPC_REL16_HI:238case R_PPC_REL16_HA:239return R_PC;240case R_PPC_GOT16:241return R_GOT_OFF;242case R_PPC_LOCAL24PC:243case R_PPC_REL24:244return R_PLT_PC;245case R_PPC_PLTREL24:246return R_PPC32_PLTREL;247case R_PPC_GOT_TLSGD16:248return R_TLSGD_GOT;249case R_PPC_GOT_TLSLD16:250return R_TLSLD_GOT;251case R_PPC_GOT_TPREL16:252return R_GOT_OFF;253case R_PPC_TLS:254return R_TLSIE_HINT;255case R_PPC_TLSGD:256return R_TLSDESC_CALL;257case R_PPC_TLSLD:258return R_TLSLD_HINT;259case R_PPC_TPREL16:260case R_PPC_TPREL16_HA:261case R_PPC_TPREL16_LO:262case R_PPC_TPREL16_HI:263return R_TPREL;264default:265error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +266") against symbol " + toString(s));267return R_NONE;268}269}270271RelType PPC::getDynRel(RelType type) const {272if (type == R_PPC_ADDR32)273return type;274return R_PPC_NONE;275}276277int64_t PPC::getImplicitAddend(const uint8_t *buf, RelType type) const {278switch (type) {279case R_PPC_NONE:280case R_PPC_GLOB_DAT:281case R_PPC_JMP_SLOT:282return 0;283case R_PPC_ADDR32:284case R_PPC_REL32:285case R_PPC_RELATIVE:286case R_PPC_IRELATIVE:287case R_PPC_DTPMOD32:288case R_PPC_DTPREL32:289case R_PPC_TPREL32:290return SignExtend64<32>(read32(buf));291default:292internalLinkerError(getErrorLocation(buf),293"cannot read addend for relocation " + toString(type));294return 0;295}296}297298static std::pair<RelType, uint64_t> fromDTPREL(RelType type, uint64_t val) {299uint64_t dtpBiasedVal = val - 0x8000;300switch (type) {301case R_PPC_DTPREL16:302return {R_PPC64_ADDR16, dtpBiasedVal};303case R_PPC_DTPREL16_HA:304return {R_PPC_ADDR16_HA, dtpBiasedVal};305case R_PPC_DTPREL16_HI:306return {R_PPC_ADDR16_HI, dtpBiasedVal};307case R_PPC_DTPREL16_LO:308return {R_PPC_ADDR16_LO, dtpBiasedVal};309case R_PPC_DTPREL32:310return {R_PPC_ADDR32, dtpBiasedVal};311default:312return {type, val};313}314}315316void PPC::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {317RelType newType;318std::tie(newType, val) = fromDTPREL(rel.type, val);319switch (newType) {320case R_PPC_ADDR16:321checkIntUInt(loc, val, 16, rel);322write16(loc, val);323break;324case R_PPC_GOT16:325case R_PPC_GOT_TLSGD16:326case R_PPC_GOT_TLSLD16:327case R_PPC_GOT_TPREL16:328case R_PPC_TPREL16:329checkInt(loc, val, 16, rel);330write16(loc, val);331break;332case R_PPC_ADDR16_HA:333case R_PPC_DTPREL16_HA:334case R_PPC_GOT_TLSGD16_HA:335case R_PPC_GOT_TLSLD16_HA:336case R_PPC_GOT_TPREL16_HA:337case R_PPC_REL16_HA:338case R_PPC_TPREL16_HA:339write16(loc, ha(val));340break;341case R_PPC_ADDR16_HI:342case R_PPC_DTPREL16_HI:343case R_PPC_GOT_TLSGD16_HI:344case R_PPC_GOT_TLSLD16_HI:345case R_PPC_GOT_TPREL16_HI:346case R_PPC_REL16_HI:347case R_PPC_TPREL16_HI:348write16(loc, val >> 16);349break;350case R_PPC_ADDR16_LO:351case R_PPC_DTPREL16_LO:352case R_PPC_GOT_TLSGD16_LO:353case R_PPC_GOT_TLSLD16_LO:354case R_PPC_GOT_TPREL16_LO:355case R_PPC_REL16_LO:356case R_PPC_TPREL16_LO:357write16(loc, val);358break;359case R_PPC_ADDR32:360case R_PPC_REL32:361write32(loc, val);362break;363case R_PPC_REL14: {364uint32_t mask = 0x0000FFFC;365checkInt(loc, val, 16, rel);366checkAlignment(loc, val, 4, rel);367write32(loc, (read32(loc) & ~mask) | (val & mask));368break;369}370case R_PPC_ADDR24:371case R_PPC_REL24:372case R_PPC_LOCAL24PC:373case R_PPC_PLTREL24: {374uint32_t mask = 0x03FFFFFC;375checkInt(loc, val, 26, rel);376checkAlignment(loc, val, 4, rel);377write32(loc, (read32(loc) & ~mask) | (val & mask));378break;379}380default:381llvm_unreachable("unknown relocation");382}383}384385RelExpr PPC::adjustTlsExpr(RelType type, RelExpr expr) const {386if (expr == R_RELAX_TLS_GD_TO_IE)387return R_RELAX_TLS_GD_TO_IE_GOT_OFF;388if (expr == R_RELAX_TLS_LD_TO_LE)389return R_RELAX_TLS_LD_TO_LE_ABS;390return expr;391}392393int PPC::getTlsGdRelaxSkip(RelType type) const {394// A __tls_get_addr call instruction is marked with 2 relocations:395//396// R_PPC_TLSGD / R_PPC_TLSLD: marker relocation397// R_PPC_REL24: __tls_get_addr398//399// After the relaxation we no longer call __tls_get_addr and should skip both400// relocations to not create a false dependence on __tls_get_addr being401// defined.402if (type == R_PPC_TLSGD || type == R_PPC_TLSLD)403return 2;404return 1;405}406407void PPC::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,408uint64_t val) const {409switch (rel.type) {410case R_PPC_GOT_TLSGD16: {411// addi rT, rA, x@got@tlsgd --> lwz rT, x@got@tprel(rA)412uint32_t insn = readFromHalf16(loc);413writeFromHalf16(loc, 0x80000000 | (insn & 0x03ff0000));414relocateNoSym(loc, R_PPC_GOT_TPREL16, val);415break;416}417case R_PPC_TLSGD:418// bl __tls_get_addr(x@tldgd) --> add r3, r3, r2419write32(loc, 0x7c631214);420break;421default:422llvm_unreachable("unsupported relocation for TLS GD to IE relaxation");423}424}425426void PPC::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,427uint64_t val) const {428switch (rel.type) {429case R_PPC_GOT_TLSGD16:430// addi r3, r31, x@got@tlsgd --> addis r3, r2, x@tprel@ha431writeFromHalf16(loc, 0x3c620000 | ha(val));432break;433case R_PPC_TLSGD:434// bl __tls_get_addr(x@tldgd) --> add r3, r3, x@tprel@l435write32(loc, 0x38630000 | lo(val));436break;437default:438llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");439}440}441442void PPC::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,443uint64_t val) const {444switch (rel.type) {445case R_PPC_GOT_TLSLD16:446// addi r3, rA, x@got@tlsgd --> addis r3, r2, 0447writeFromHalf16(loc, 0x3c620000);448break;449case R_PPC_TLSLD:450// r3+x@dtprel computes r3+x-0x8000, while we want it to compute r3+x@tprel451// = r3+x-0x7000, so add 4096 to r3.452// bl __tls_get_addr(x@tlsld) --> addi r3, r3, 4096453write32(loc, 0x38631000);454break;455case R_PPC_DTPREL16:456case R_PPC_DTPREL16_HA:457case R_PPC_DTPREL16_HI:458case R_PPC_DTPREL16_LO:459relocate(loc, rel, val);460break;461default:462llvm_unreachable("unsupported relocation for TLS LD to LE relaxation");463}464}465466void PPC::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,467uint64_t val) const {468switch (rel.type) {469case R_PPC_GOT_TPREL16: {470// lwz rT, x@got@tprel(rA) --> addis rT, r2, x@tprel@ha471uint32_t rt = readFromHalf16(loc) & 0x03e00000;472writeFromHalf16(loc, 0x3c020000 | rt | ha(val));473break;474}475case R_PPC_TLS: {476uint32_t insn = read32(loc);477if (insn >> 26 != 31)478error("unrecognized instruction for IE to LE R_PPC_TLS");479// addi rT, rT, x@tls --> addi rT, rT, x@tprel@l480unsigned secondaryOp = (read32(loc) & 0x000007fe) >> 1;481uint32_t dFormOp = getPPCDFormOp(secondaryOp);482if (dFormOp == 0) { // Expecting a DS-Form instruction.483dFormOp = getPPCDSFormOp(secondaryOp);484if (dFormOp == 0)485error("unrecognized instruction for IE to LE R_PPC_TLS");486}487write32(loc, (dFormOp | (insn & 0x03ff0000) | lo(val)));488break;489}490default:491llvm_unreachable("unsupported relocation for TLS IE to LE relaxation");492}493}494495void PPC::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {496uint64_t secAddr = sec.getOutputSection()->addr;497if (auto *s = dyn_cast<InputSection>(&sec))498secAddr += s->outSecOff;499for (const Relocation &rel : sec.relocs()) {500uint8_t *loc = buf + rel.offset;501const uint64_t val = SignExtend64(502sec.getRelocTargetVA(sec.file, rel.type, rel.addend,503secAddr + rel.offset, *rel.sym, rel.expr),50432);505switch (rel.expr) {506case R_RELAX_TLS_GD_TO_IE_GOT_OFF:507relaxTlsGdToIe(loc, rel, val);508break;509case R_RELAX_TLS_GD_TO_LE:510relaxTlsGdToLe(loc, rel, val);511break;512case R_RELAX_TLS_LD_TO_LE_ABS:513relaxTlsLdToLe(loc, rel, val);514break;515case R_RELAX_TLS_IE_TO_LE:516relaxTlsIeToLe(loc, rel, val);517break;518default:519relocate(loc, rel, val);520break;521}522}523}524525TargetInfo *elf::getPPCTargetInfo() {526static PPC target;527return ⌖528}529530531