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;
writePltHeader(uint8_t * buf) const350b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override {
360b57cec5SDimitry Andric     llvm_unreachable("should call writePPC32GlinkSection() instead");
370b57cec5SDimitry Andric   }
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const38480093f4SDimitry 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;
54bdd1243dSDimitry Andric   void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
55bdd1243dSDimitry Andric 
56bdd1243dSDimitry Andric private:
57bdd1243dSDimitry Andric   void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
58bdd1243dSDimitry Andric   void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
59bdd1243dSDimitry Andric   void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
60bdd1243dSDimitry Andric   void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
610b57cec5SDimitry Andric };
620b57cec5SDimitry Andric } // namespace
630b57cec5SDimitry Andric 
lo(uint32_t v)640b57cec5SDimitry Andric static uint16_t lo(uint32_t v) { return v; }
ha(uint32_t v)650b57cec5SDimitry Andric static uint16_t ha(uint32_t v) { return (v + 0x8000) >> 16; }
660b57cec5SDimitry Andric 
readFromHalf16(const uint8_t * loc)670b57cec5SDimitry Andric static uint32_t readFromHalf16(const uint8_t *loc) {
680b57cec5SDimitry Andric   return read32(config->isLE ? loc : loc - 2);
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric 
writeFromHalf16(uint8_t * loc,uint32_t insn)710b57cec5SDimitry Andric static void writeFromHalf16(uint8_t *loc, uint32_t insn) {
720b57cec5SDimitry Andric   write32(config->isLE ? loc : loc - 2, insn);
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
writePPC32GlinkSection(uint8_t * buf,size_t numEntries)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 
PPC()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 
writeIplt(uint8_t * buf,const Symbol & sym,uint64_t) const179480093f4SDimitry 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 
writeGotHeader(uint8_t * buf) const1860b57cec5SDimitry 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 
writeGotPlt(uint8_t * buf,const Symbol & s) const1930b57cec5SDimitry 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 
needsThunk(RelExpr expr,RelType type,const InputFile * file,uint64_t branchAddr,const Symbol & s,int64_t a) const1980b57cec5SDimitry 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 
getThunkSectionSpacing() const2090b57cec5SDimitry Andric uint32_t PPC::getThunkSectionSpacing() const { return 0x2000000; }
2100b57cec5SDimitry Andric 
inBranchRange(RelType type,uint64_t src,uint64_t dst) const2110b57cec5SDimitry 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 
getRelExpr(RelType type,const Symbol & s,const uint8_t * loc) const2180b57cec5SDimitry 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 
getDynRel(RelType type) const2720b57cec5SDimitry 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 
getImplicitAddend(const uint8_t * buf,RelType type) const278298c3e8dSDimitry Andric int64_t PPC::getImplicitAddend(const uint8_t *buf, RelType type) const {
279298c3e8dSDimitry Andric   switch (type) {
280298c3e8dSDimitry Andric   case R_PPC_NONE:
2815f757f3fSDimitry Andric   case R_PPC_GLOB_DAT:
2825f757f3fSDimitry Andric   case R_PPC_JMP_SLOT:
283298c3e8dSDimitry Andric     return 0;
284298c3e8dSDimitry Andric   case R_PPC_ADDR32:
285298c3e8dSDimitry Andric   case R_PPC_REL32:
2865f757f3fSDimitry Andric   case R_PPC_RELATIVE:
2875f757f3fSDimitry Andric   case R_PPC_IRELATIVE:
2885f757f3fSDimitry Andric   case R_PPC_DTPMOD32:
2895f757f3fSDimitry Andric   case R_PPC_DTPREL32:
2905f757f3fSDimitry Andric   case R_PPC_TPREL32:
291298c3e8dSDimitry Andric     return SignExtend64<32>(read32(buf));
292298c3e8dSDimitry Andric   default:
293298c3e8dSDimitry Andric     internalLinkerError(getErrorLocation(buf),
294298c3e8dSDimitry Andric                         "cannot read addend for relocation " + toString(type));
295298c3e8dSDimitry Andric     return 0;
296298c3e8dSDimitry Andric   }
297298c3e8dSDimitry Andric }
298298c3e8dSDimitry Andric 
fromDTPREL(RelType type,uint64_t val)2990b57cec5SDimitry Andric static std::pair<RelType, uint64_t> fromDTPREL(RelType type, uint64_t val) {
3000b57cec5SDimitry Andric   uint64_t dtpBiasedVal = val - 0x8000;
3010b57cec5SDimitry Andric   switch (type) {
3020b57cec5SDimitry Andric   case R_PPC_DTPREL16:
3030b57cec5SDimitry Andric     return {R_PPC64_ADDR16, dtpBiasedVal};
3040b57cec5SDimitry Andric   case R_PPC_DTPREL16_HA:
3050b57cec5SDimitry Andric     return {R_PPC_ADDR16_HA, dtpBiasedVal};
3060b57cec5SDimitry Andric   case R_PPC_DTPREL16_HI:
3070b57cec5SDimitry Andric     return {R_PPC_ADDR16_HI, dtpBiasedVal};
3080b57cec5SDimitry Andric   case R_PPC_DTPREL16_LO:
3090b57cec5SDimitry Andric     return {R_PPC_ADDR16_LO, dtpBiasedVal};
3100b57cec5SDimitry Andric   case R_PPC_DTPREL32:
3110b57cec5SDimitry Andric     return {R_PPC_ADDR32, dtpBiasedVal};
3120b57cec5SDimitry Andric   default:
3130b57cec5SDimitry Andric     return {type, val};
3140b57cec5SDimitry Andric   }
3150b57cec5SDimitry Andric }
3160b57cec5SDimitry Andric 
relocate(uint8_t * loc,const Relocation & rel,uint64_t val) const3175ffd83dbSDimitry Andric void PPC::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
3180b57cec5SDimitry Andric   RelType newType;
3195ffd83dbSDimitry Andric   std::tie(newType, val) = fromDTPREL(rel.type, val);
3200b57cec5SDimitry Andric   switch (newType) {
3210b57cec5SDimitry Andric   case R_PPC_ADDR16:
3225ffd83dbSDimitry Andric     checkIntUInt(loc, val, 16, rel);
3230b57cec5SDimitry Andric     write16(loc, val);
3240b57cec5SDimitry Andric     break;
3250b57cec5SDimitry Andric   case R_PPC_GOT16:
3260b57cec5SDimitry Andric   case R_PPC_GOT_TLSGD16:
3270b57cec5SDimitry Andric   case R_PPC_GOT_TLSLD16:
3280b57cec5SDimitry Andric   case R_PPC_GOT_TPREL16:
3290b57cec5SDimitry Andric   case R_PPC_TPREL16:
3305ffd83dbSDimitry Andric     checkInt(loc, val, 16, rel);
3310b57cec5SDimitry Andric     write16(loc, val);
3320b57cec5SDimitry Andric     break;
3330b57cec5SDimitry Andric   case R_PPC_ADDR16_HA:
3340b57cec5SDimitry Andric   case R_PPC_DTPREL16_HA:
3350b57cec5SDimitry Andric   case R_PPC_GOT_TLSGD16_HA:
3360b57cec5SDimitry Andric   case R_PPC_GOT_TLSLD16_HA:
3370b57cec5SDimitry Andric   case R_PPC_GOT_TPREL16_HA:
3380b57cec5SDimitry Andric   case R_PPC_REL16_HA:
3390b57cec5SDimitry Andric   case R_PPC_TPREL16_HA:
3400b57cec5SDimitry Andric     write16(loc, ha(val));
3410b57cec5SDimitry Andric     break;
3420b57cec5SDimitry Andric   case R_PPC_ADDR16_HI:
3430b57cec5SDimitry Andric   case R_PPC_DTPREL16_HI:
3440b57cec5SDimitry Andric   case R_PPC_GOT_TLSGD16_HI:
3450b57cec5SDimitry Andric   case R_PPC_GOT_TLSLD16_HI:
3460b57cec5SDimitry Andric   case R_PPC_GOT_TPREL16_HI:
3470b57cec5SDimitry Andric   case R_PPC_REL16_HI:
3480b57cec5SDimitry Andric   case R_PPC_TPREL16_HI:
3490b57cec5SDimitry Andric     write16(loc, val >> 16);
3500b57cec5SDimitry Andric     break;
3510b57cec5SDimitry Andric   case R_PPC_ADDR16_LO:
3520b57cec5SDimitry Andric   case R_PPC_DTPREL16_LO:
3530b57cec5SDimitry Andric   case R_PPC_GOT_TLSGD16_LO:
3540b57cec5SDimitry Andric   case R_PPC_GOT_TLSLD16_LO:
3550b57cec5SDimitry Andric   case R_PPC_GOT_TPREL16_LO:
3560b57cec5SDimitry Andric   case R_PPC_REL16_LO:
3570b57cec5SDimitry Andric   case R_PPC_TPREL16_LO:
3580b57cec5SDimitry Andric     write16(loc, val);
3590b57cec5SDimitry Andric     break;
3600b57cec5SDimitry Andric   case R_PPC_ADDR32:
3610b57cec5SDimitry Andric   case R_PPC_REL32:
3620b57cec5SDimitry Andric     write32(loc, val);
3630b57cec5SDimitry Andric     break;
3640b57cec5SDimitry Andric   case R_PPC_REL14: {
3650b57cec5SDimitry Andric     uint32_t mask = 0x0000FFFC;
3665ffd83dbSDimitry Andric     checkInt(loc, val, 16, rel);
3675ffd83dbSDimitry Andric     checkAlignment(loc, val, 4, rel);
3680b57cec5SDimitry Andric     write32(loc, (read32(loc) & ~mask) | (val & mask));
3690b57cec5SDimitry Andric     break;
3700b57cec5SDimitry Andric   }
371e8d8bef9SDimitry Andric   case R_PPC_ADDR24:
3720b57cec5SDimitry Andric   case R_PPC_REL24:
3730b57cec5SDimitry Andric   case R_PPC_LOCAL24PC:
3740b57cec5SDimitry Andric   case R_PPC_PLTREL24: {
3750b57cec5SDimitry Andric     uint32_t mask = 0x03FFFFFC;
3765ffd83dbSDimitry Andric     checkInt(loc, val, 26, rel);
3775ffd83dbSDimitry Andric     checkAlignment(loc, val, 4, rel);
3780b57cec5SDimitry Andric     write32(loc, (read32(loc) & ~mask) | (val & mask));
3790b57cec5SDimitry Andric     break;
3800b57cec5SDimitry Andric   }
3810b57cec5SDimitry Andric   default:
3820b57cec5SDimitry Andric     llvm_unreachable("unknown relocation");
3830b57cec5SDimitry Andric   }
3840b57cec5SDimitry Andric }
3850b57cec5SDimitry Andric 
adjustTlsExpr(RelType type,RelExpr expr) const386e8d8bef9SDimitry Andric RelExpr PPC::adjustTlsExpr(RelType type, RelExpr expr) const {
3870b57cec5SDimitry Andric   if (expr == R_RELAX_TLS_GD_TO_IE)
3880b57cec5SDimitry Andric     return R_RELAX_TLS_GD_TO_IE_GOT_OFF;
3890b57cec5SDimitry Andric   if (expr == R_RELAX_TLS_LD_TO_LE)
3900b57cec5SDimitry Andric     return R_RELAX_TLS_LD_TO_LE_ABS;
3910b57cec5SDimitry Andric   return expr;
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric 
getTlsGdRelaxSkip(RelType type) const3940b57cec5SDimitry Andric int PPC::getTlsGdRelaxSkip(RelType type) const {
3950b57cec5SDimitry Andric   // A __tls_get_addr call instruction is marked with 2 relocations:
3960b57cec5SDimitry Andric   //
3970b57cec5SDimitry Andric   //   R_PPC_TLSGD / R_PPC_TLSLD: marker relocation
3980b57cec5SDimitry Andric   //   R_PPC_REL24: __tls_get_addr
3990b57cec5SDimitry Andric   //
4000b57cec5SDimitry Andric   // After the relaxation we no longer call __tls_get_addr and should skip both
4010b57cec5SDimitry Andric   // relocations to not create a false dependence on __tls_get_addr being
4020b57cec5SDimitry Andric   // defined.
4030b57cec5SDimitry Andric   if (type == R_PPC_TLSGD || type == R_PPC_TLSLD)
4040b57cec5SDimitry Andric     return 2;
4050b57cec5SDimitry Andric   return 1;
4060b57cec5SDimitry Andric }
4070b57cec5SDimitry Andric 
relaxTlsGdToIe(uint8_t * loc,const Relocation & rel,uint64_t val) const4085ffd83dbSDimitry Andric void PPC::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
4095ffd83dbSDimitry Andric                          uint64_t val) const {
4105ffd83dbSDimitry Andric   switch (rel.type) {
4110b57cec5SDimitry Andric   case R_PPC_GOT_TLSGD16: {
4120b57cec5SDimitry Andric     // addi rT, rA, x@got@tlsgd --> lwz rT, x@got@tprel(rA)
4130b57cec5SDimitry Andric     uint32_t insn = readFromHalf16(loc);
4140b57cec5SDimitry Andric     writeFromHalf16(loc, 0x80000000 | (insn & 0x03ff0000));
4155ffd83dbSDimitry Andric     relocateNoSym(loc, R_PPC_GOT_TPREL16, val);
4160b57cec5SDimitry Andric     break;
4170b57cec5SDimitry Andric   }
4180b57cec5SDimitry Andric   case R_PPC_TLSGD:
4190b57cec5SDimitry Andric     // bl __tls_get_addr(x@tldgd) --> add r3, r3, r2
4200b57cec5SDimitry Andric     write32(loc, 0x7c631214);
4210b57cec5SDimitry Andric     break;
4220b57cec5SDimitry Andric   default:
4230b57cec5SDimitry Andric     llvm_unreachable("unsupported relocation for TLS GD to IE relaxation");
4240b57cec5SDimitry Andric   }
4250b57cec5SDimitry Andric }
4260b57cec5SDimitry Andric 
relaxTlsGdToLe(uint8_t * loc,const Relocation & rel,uint64_t val) const4275ffd83dbSDimitry Andric void PPC::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
4285ffd83dbSDimitry Andric                          uint64_t val) const {
4295ffd83dbSDimitry Andric   switch (rel.type) {
4300b57cec5SDimitry Andric   case R_PPC_GOT_TLSGD16:
4310b57cec5SDimitry Andric     // addi r3, r31, x@got@tlsgd --> addis r3, r2, x@tprel@ha
4320b57cec5SDimitry Andric     writeFromHalf16(loc, 0x3c620000 | ha(val));
4330b57cec5SDimitry Andric     break;
4340b57cec5SDimitry Andric   case R_PPC_TLSGD:
4350b57cec5SDimitry Andric     // bl __tls_get_addr(x@tldgd) --> add r3, r3, x@tprel@l
4360b57cec5SDimitry Andric     write32(loc, 0x38630000 | lo(val));
4370b57cec5SDimitry Andric     break;
4380b57cec5SDimitry Andric   default:
4390b57cec5SDimitry Andric     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
4400b57cec5SDimitry Andric   }
4410b57cec5SDimitry Andric }
4420b57cec5SDimitry Andric 
relaxTlsLdToLe(uint8_t * loc,const Relocation & rel,uint64_t val) const4435ffd83dbSDimitry Andric void PPC::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
4445ffd83dbSDimitry Andric                          uint64_t val) const {
4455ffd83dbSDimitry Andric   switch (rel.type) {
4460b57cec5SDimitry Andric   case R_PPC_GOT_TLSLD16:
4470b57cec5SDimitry Andric     // addi r3, rA, x@got@tlsgd --> addis r3, r2, 0
4480b57cec5SDimitry Andric     writeFromHalf16(loc, 0x3c620000);
4490b57cec5SDimitry Andric     break;
4500b57cec5SDimitry Andric   case R_PPC_TLSLD:
4510b57cec5SDimitry Andric     // r3+x@dtprel computes r3+x-0x8000, while we want it to compute r3+x@tprel
4520b57cec5SDimitry Andric     // = r3+x-0x7000, so add 4096 to r3.
4530b57cec5SDimitry Andric     // bl __tls_get_addr(x@tlsld) --> addi r3, r3, 4096
4540b57cec5SDimitry Andric     write32(loc, 0x38631000);
4550b57cec5SDimitry Andric     break;
4560b57cec5SDimitry Andric   case R_PPC_DTPREL16:
4570b57cec5SDimitry Andric   case R_PPC_DTPREL16_HA:
4580b57cec5SDimitry Andric   case R_PPC_DTPREL16_HI:
4590b57cec5SDimitry Andric   case R_PPC_DTPREL16_LO:
4605ffd83dbSDimitry Andric     relocate(loc, rel, val);
4610b57cec5SDimitry Andric     break;
4620b57cec5SDimitry Andric   default:
4630b57cec5SDimitry Andric     llvm_unreachable("unsupported relocation for TLS LD to LE relaxation");
4640b57cec5SDimitry Andric   }
4650b57cec5SDimitry Andric }
4660b57cec5SDimitry Andric 
relaxTlsIeToLe(uint8_t * loc,const Relocation & rel,uint64_t val) const4675ffd83dbSDimitry Andric void PPC::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
4685ffd83dbSDimitry Andric                          uint64_t val) const {
4695ffd83dbSDimitry Andric   switch (rel.type) {
4700b57cec5SDimitry Andric   case R_PPC_GOT_TPREL16: {
4710b57cec5SDimitry Andric     // lwz rT, x@got@tprel(rA) --> addis rT, r2, x@tprel@ha
4720b57cec5SDimitry Andric     uint32_t rt = readFromHalf16(loc) & 0x03e00000;
4730b57cec5SDimitry Andric     writeFromHalf16(loc, 0x3c020000 | rt | ha(val));
4740b57cec5SDimitry Andric     break;
4750b57cec5SDimitry Andric   }
4760b57cec5SDimitry Andric   case R_PPC_TLS: {
4770b57cec5SDimitry Andric     uint32_t insn = read32(loc);
4780b57cec5SDimitry Andric     if (insn >> 26 != 31)
4790b57cec5SDimitry Andric       error("unrecognized instruction for IE to LE R_PPC_TLS");
4800b57cec5SDimitry Andric     // addi rT, rT, x@tls --> addi rT, rT, x@tprel@l
4818a4dda33SDimitry Andric     unsigned secondaryOp = (read32(loc) & 0x000007fe) >> 1;
4828a4dda33SDimitry Andric     uint32_t dFormOp = getPPCDFormOp(secondaryOp);
4838a4dda33SDimitry Andric     if (dFormOp == 0) { // Expecting a DS-Form instruction.
4848a4dda33SDimitry Andric       dFormOp = getPPCDSFormOp(secondaryOp);
4850b57cec5SDimitry Andric       if (dFormOp == 0)
4860b57cec5SDimitry Andric         error("unrecognized instruction for IE to LE R_PPC_TLS");
4878a4dda33SDimitry Andric     }
4888a4dda33SDimitry Andric     write32(loc, (dFormOp | (insn & 0x03ff0000) | lo(val)));
4890b57cec5SDimitry Andric     break;
4900b57cec5SDimitry Andric   }
4910b57cec5SDimitry Andric   default:
4920b57cec5SDimitry Andric     llvm_unreachable("unsupported relocation for TLS IE to LE relaxation");
4930b57cec5SDimitry Andric   }
4940b57cec5SDimitry Andric }
4950b57cec5SDimitry Andric 
relocateAlloc(InputSectionBase & sec,uint8_t * buf) const496bdd1243dSDimitry Andric void PPC::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
497bdd1243dSDimitry Andric   uint64_t secAddr = sec.getOutputSection()->addr;
498bdd1243dSDimitry Andric   if (auto *s = dyn_cast<InputSection>(&sec))
499bdd1243dSDimitry Andric     secAddr += s->outSecOff;
500bdd1243dSDimitry Andric   for (const Relocation &rel : sec.relocs()) {
501bdd1243dSDimitry Andric     uint8_t *loc = buf + rel.offset;
502bdd1243dSDimitry Andric     const uint64_t val = SignExtend64(
503bdd1243dSDimitry Andric         sec.getRelocTargetVA(sec.file, rel.type, rel.addend,
504bdd1243dSDimitry Andric                              secAddr + rel.offset, *rel.sym, rel.expr),
505bdd1243dSDimitry Andric         32);
506bdd1243dSDimitry Andric     switch (rel.expr) {
507bdd1243dSDimitry Andric     case R_RELAX_TLS_GD_TO_IE_GOT_OFF:
508bdd1243dSDimitry Andric       relaxTlsGdToIe(loc, rel, val);
509bdd1243dSDimitry Andric       break;
510bdd1243dSDimitry Andric     case R_RELAX_TLS_GD_TO_LE:
511bdd1243dSDimitry Andric       relaxTlsGdToLe(loc, rel, val);
512bdd1243dSDimitry Andric       break;
513bdd1243dSDimitry Andric     case R_RELAX_TLS_LD_TO_LE_ABS:
514bdd1243dSDimitry Andric       relaxTlsLdToLe(loc, rel, val);
515bdd1243dSDimitry Andric       break;
516bdd1243dSDimitry Andric     case R_RELAX_TLS_IE_TO_LE:
517bdd1243dSDimitry Andric       relaxTlsIeToLe(loc, rel, val);
518bdd1243dSDimitry Andric       break;
519bdd1243dSDimitry Andric     default:
520bdd1243dSDimitry Andric       relocate(loc, rel, val);
521bdd1243dSDimitry Andric       break;
522bdd1243dSDimitry Andric     }
523bdd1243dSDimitry Andric   }
524bdd1243dSDimitry Andric }
525bdd1243dSDimitry Andric 
getPPCTargetInfo()5265ffd83dbSDimitry Andric TargetInfo *elf::getPPCTargetInfo() {
5270b57cec5SDimitry Andric   static PPC target;
5280b57cec5SDimitry Andric   return &target;
5290b57cec5SDimitry Andric }
530