xref: /openbsd-src/gnu/llvm/lld/ELF/Arch/PPC.cpp (revision 05edf1c10c70aea023b49c9190728f24a4673803)
1ece8a530Spatrick //===- PPC.cpp ------------------------------------------------------------===//
2ece8a530Spatrick //
3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information.
5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ece8a530Spatrick //
7ece8a530Spatrick //===----------------------------------------------------------------------===//
8ece8a530Spatrick 
9ece8a530Spatrick #include "OutputSections.h"
10ece8a530Spatrick #include "Symbols.h"
11ece8a530Spatrick #include "SyntheticSections.h"
12ece8a530Spatrick #include "Target.h"
13ece8a530Spatrick #include "Thunks.h"
14ece8a530Spatrick #include "lld/Common/ErrorHandler.h"
15ece8a530Spatrick #include "llvm/Support/Endian.h"
16ece8a530Spatrick 
17ece8a530Spatrick using namespace llvm;
18ece8a530Spatrick using namespace llvm::support::endian;
19ece8a530Spatrick using namespace llvm::ELF;
20adae0cfdSpatrick using namespace lld;
21adae0cfdSpatrick using namespace lld::elf;
22ece8a530Spatrick 
23*05edf1c1Srobert // Undefine the macro predefined by GCC powerpc32.
24*05edf1c1Srobert #undef PPC
25*05edf1c1Srobert 
26ece8a530Spatrick namespace {
27ece8a530Spatrick class PPC final : public TargetInfo {
28ece8a530Spatrick public:
29ece8a530Spatrick   PPC();
30ece8a530Spatrick   RelExpr getRelExpr(RelType type, const Symbol &s,
31ece8a530Spatrick                      const uint8_t *loc) const override;
32ece8a530Spatrick   RelType getDynRel(RelType type) const override;
33*05edf1c1Srobert   int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
34ece8a530Spatrick   void writeGotHeader(uint8_t *buf) const override;
writePltHeader(uint8_t * buf) const35ece8a530Spatrick   void writePltHeader(uint8_t *buf) const override {
36ece8a530Spatrick     llvm_unreachable("should call writePPC32GlinkSection() instead");
37ece8a530Spatrick   }
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const38ece8a530Spatrick   void writePlt(uint8_t *buf, const Symbol &sym,
39ece8a530Spatrick                 uint64_t pltEntryAddr) const override {
40ece8a530Spatrick     llvm_unreachable("should call writePPC32GlinkSection() instead");
41ece8a530Spatrick   }
42ece8a530Spatrick   void writeIplt(uint8_t *buf, const Symbol &sym,
43ece8a530Spatrick                  uint64_t pltEntryAddr) const override;
44ece8a530Spatrick   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
45ece8a530Spatrick   bool needsThunk(RelExpr expr, RelType relocType, const InputFile *file,
46ece8a530Spatrick                   uint64_t branchAddr, const Symbol &s,
47ece8a530Spatrick                   int64_t a) const override;
48ece8a530Spatrick   uint32_t getThunkSectionSpacing() const override;
49ece8a530Spatrick   bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
50adae0cfdSpatrick   void relocate(uint8_t *loc, const Relocation &rel,
51adae0cfdSpatrick                 uint64_t val) const override;
52a0747c9fSpatrick   RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
53ece8a530Spatrick   int getTlsGdRelaxSkip(RelType type) const override;
54*05edf1c1Srobert   void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
55*05edf1c1Srobert 
56*05edf1c1Srobert private:
57*05edf1c1Srobert   void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
58*05edf1c1Srobert   void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
59*05edf1c1Srobert   void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
60*05edf1c1Srobert   void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
61ece8a530Spatrick };
62ece8a530Spatrick } // namespace
63ece8a530Spatrick 
lo(uint32_t v)64ece8a530Spatrick static uint16_t lo(uint32_t v) { return v; }
ha(uint32_t v)65ece8a530Spatrick static uint16_t ha(uint32_t v) { return (v + 0x8000) >> 16; }
66ece8a530Spatrick 
readFromHalf16(const uint8_t * loc)67ece8a530Spatrick static uint32_t readFromHalf16(const uint8_t *loc) {
68ece8a530Spatrick   return read32(config->isLE ? loc : loc - 2);
69ece8a530Spatrick }
70ece8a530Spatrick 
writeFromHalf16(uint8_t * loc,uint32_t insn)71ece8a530Spatrick static void writeFromHalf16(uint8_t *loc, uint32_t insn) {
72ece8a530Spatrick   write32(config->isLE ? loc : loc - 2, insn);
73ece8a530Spatrick }
74ece8a530Spatrick 
writePPC32GlinkSection(uint8_t * buf,size_t numEntries)75adae0cfdSpatrick void elf::writePPC32GlinkSection(uint8_t *buf, size_t numEntries) {
76ece8a530Spatrick   // Create canonical PLT entries for non-PIE code. Compilers don't generate
77ece8a530Spatrick   // non-GOT-non-PLT relocations referencing external functions for -fpie/-fPIE.
78ece8a530Spatrick   uint32_t glink = in.plt->getVA(); // VA of .glink
79ece8a530Spatrick   if (!config->isPic) {
80*05edf1c1Srobert     for (const Symbol *sym : cast<PPC32GlinkSection>(*in.plt).canonical_plts) {
81ece8a530Spatrick       writePPC32PltCallStub(buf, sym->getGotPltVA(), nullptr, 0);
82ece8a530Spatrick       buf += 16;
83ece8a530Spatrick       glink += 16;
84ece8a530Spatrick     }
85ece8a530Spatrick   }
86ece8a530Spatrick 
87ece8a530Spatrick   // On PPC Secure PLT ABI, bl foo@plt jumps to a call stub, which loads an
88ece8a530Spatrick   // absolute address from a specific .plt slot (usually called .got.plt on
89ece8a530Spatrick   // other targets) and jumps there.
90ece8a530Spatrick   //
91ece8a530Spatrick   // a) With immediate binding (BIND_NOW), the .plt entry is resolved at load
92ece8a530Spatrick   // time. The .glink section is not used.
93ece8a530Spatrick   // b) With lazy binding, the .plt entry points to a `b PLTresolve`
94ece8a530Spatrick   // instruction in .glink, filled in by PPC::writeGotPlt().
95ece8a530Spatrick 
96ece8a530Spatrick   // Write N `b PLTresolve` first.
97ece8a530Spatrick   for (size_t i = 0; i != numEntries; ++i)
98ece8a530Spatrick     write32(buf + 4 * i, 0x48000000 | 4 * (numEntries - i));
99ece8a530Spatrick   buf += 4 * numEntries;
100ece8a530Spatrick 
101ece8a530Spatrick   // Then write PLTresolve(), which has two forms: PIC and non-PIC. PLTresolve()
102ece8a530Spatrick   // computes the PLT index (by computing the distance from the landing b to
103ece8a530Spatrick   // itself) and calls _dl_runtime_resolve() (in glibc).
104ece8a530Spatrick   uint32_t got = in.got->getVA();
105ece8a530Spatrick   const uint8_t *end = buf + 64;
106ece8a530Spatrick   if (config->isPic) {
107ece8a530Spatrick     uint32_t afterBcl = 4 * in.plt->getNumEntries() + 12;
108ece8a530Spatrick     uint32_t gotBcl = got + 4 - (glink + afterBcl);
109ece8a530Spatrick     write32(buf + 0, 0x3d6b0000 | ha(afterBcl));  // addis r11,r11,1f-glink@ha
110ece8a530Spatrick     write32(buf + 4, 0x7c0802a6);                 // mflr r0
111ece8a530Spatrick     write32(buf + 8, 0x429f0005);                 // bcl 20,30,.+4
112ece8a530Spatrick     write32(buf + 12, 0x396b0000 | lo(afterBcl)); // 1: addi r11,r11,1b-glink@l
113ece8a530Spatrick     write32(buf + 16, 0x7d8802a6);                // mflr r12
114ece8a530Spatrick     write32(buf + 20, 0x7c0803a6);                // mtlr r0
115ece8a530Spatrick     write32(buf + 24, 0x7d6c5850);                // sub r11,r11,r12
116ece8a530Spatrick     write32(buf + 28, 0x3d8c0000 | ha(gotBcl));   // addis 12,12,GOT+4-1b@ha
117ece8a530Spatrick     if (ha(gotBcl) == ha(gotBcl + 4)) {
118ece8a530Spatrick       write32(buf + 32, 0x800c0000 | lo(gotBcl)); // lwz r0,r12,GOT+4-1b@l(r12)
119ece8a530Spatrick       write32(buf + 36,
120ece8a530Spatrick               0x818c0000 | lo(gotBcl + 4));       // lwz r12,r12,GOT+8-1b@l(r12)
121ece8a530Spatrick     } else {
122ece8a530Spatrick       write32(buf + 32, 0x840c0000 | lo(gotBcl)); // lwzu r0,r12,GOT+4-1b@l(r12)
123ece8a530Spatrick       write32(buf + 36, 0x818c0000 | 4);          // lwz r12,r12,4(r12)
124ece8a530Spatrick     }
125ece8a530Spatrick     write32(buf + 40, 0x7c0903a6);                // mtctr 0
126ece8a530Spatrick     write32(buf + 44, 0x7c0b5a14);                // add r0,11,11
127ece8a530Spatrick     write32(buf + 48, 0x7d605a14);                // add r11,0,11
128ece8a530Spatrick     write32(buf + 52, 0x4e800420);                // bctr
129ece8a530Spatrick     buf += 56;
130ece8a530Spatrick   } else {
131ece8a530Spatrick     write32(buf + 0, 0x3d800000 | ha(got + 4));   // lis     r12,GOT+4@ha
132ece8a530Spatrick     write32(buf + 4, 0x3d6b0000 | ha(-glink));    // addis   r11,r11,-glink@ha
133ece8a530Spatrick     if (ha(got + 4) == ha(got + 8))
134ece8a530Spatrick       write32(buf + 8, 0x800c0000 | lo(got + 4)); // lwz r0,GOT+4@l(r12)
135ece8a530Spatrick     else
136ece8a530Spatrick       write32(buf + 8, 0x840c0000 | lo(got + 4)); // lwzu r0,GOT+4@l(r12)
137ece8a530Spatrick     write32(buf + 12, 0x396b0000 | lo(-glink));   // addi    r11,r11,-glink@l
138ece8a530Spatrick     write32(buf + 16, 0x7c0903a6);                // mtctr   r0
139ece8a530Spatrick     write32(buf + 20, 0x7c0b5a14);                // add     r0,r11,r11
140ece8a530Spatrick     if (ha(got + 4) == ha(got + 8))
141ece8a530Spatrick       write32(buf + 24, 0x818c0000 | lo(got + 8)); // lwz r12,GOT+8@l(r12)
142ece8a530Spatrick     else
143ece8a530Spatrick       write32(buf + 24, 0x818c0000 | 4);          // lwz r12,4(r12)
144ece8a530Spatrick     write32(buf + 28, 0x7d605a14);                // add     r11,r0,r11
145ece8a530Spatrick     write32(buf + 32, 0x4e800420);                // bctr
146ece8a530Spatrick     buf += 36;
147ece8a530Spatrick   }
148ece8a530Spatrick 
149ece8a530Spatrick   // Pad with nop. They should not be executed.
150ece8a530Spatrick   for (; buf < end; buf += 4)
151ece8a530Spatrick     write32(buf, 0x60000000);
152ece8a530Spatrick }
153ece8a530Spatrick 
PPC()154ece8a530Spatrick PPC::PPC() {
155ece8a530Spatrick   copyRel = R_PPC_COPY;
156ece8a530Spatrick   gotRel = R_PPC_GLOB_DAT;
157ece8a530Spatrick   pltRel = R_PPC_JMP_SLOT;
158ece8a530Spatrick   relativeRel = R_PPC_RELATIVE;
159ece8a530Spatrick   iRelativeRel = R_PPC_IRELATIVE;
160ece8a530Spatrick   symbolicRel = R_PPC_ADDR32;
161ece8a530Spatrick   gotHeaderEntriesNum = 3;
162ece8a530Spatrick   gotPltHeaderEntriesNum = 0;
163ece8a530Spatrick   pltHeaderSize = 0;
164ece8a530Spatrick   pltEntrySize = 4;
165ece8a530Spatrick   ipltEntrySize = 16;
166ece8a530Spatrick 
167ece8a530Spatrick   needsThunks = true;
168ece8a530Spatrick 
169ece8a530Spatrick   tlsModuleIndexRel = R_PPC_DTPMOD32;
170ece8a530Spatrick   tlsOffsetRel = R_PPC_DTPREL32;
171ece8a530Spatrick   tlsGotRel = R_PPC_TPREL32;
172ece8a530Spatrick 
173ece8a530Spatrick   defaultMaxPageSize = 65536;
174ece8a530Spatrick   defaultImageBase = 0x10000000;
175ece8a530Spatrick 
176ece8a530Spatrick   write32(trapInstr.data(), 0x7fe00008);
177ece8a530Spatrick }
178ece8a530Spatrick 
writeIplt(uint8_t * buf,const Symbol & sym,uint64_t) const179ece8a530Spatrick void PPC::writeIplt(uint8_t *buf, const Symbol &sym,
180ece8a530Spatrick                     uint64_t /*pltEntryAddr*/) const {
181ece8a530Spatrick   // In -pie or -shared mode, assume r30 points to .got2+0x8000, and use a
182ece8a530Spatrick   // .got2.plt_pic32. thunk.
183ece8a530Spatrick   writePPC32PltCallStub(buf, sym.getGotPltVA(), sym.file, 0x8000);
184ece8a530Spatrick }
185ece8a530Spatrick 
writeGotHeader(uint8_t * buf) const186ece8a530Spatrick void PPC::writeGotHeader(uint8_t *buf) const {
187ece8a530Spatrick   // _GLOBAL_OFFSET_TABLE_[0] = _DYNAMIC
188ece8a530Spatrick   // glibc stores _dl_runtime_resolve in _GLOBAL_OFFSET_TABLE_[1],
189ece8a530Spatrick   // link_map in _GLOBAL_OFFSET_TABLE_[2].
190ece8a530Spatrick   write32(buf, mainPart->dynamic->getVA());
191ece8a530Spatrick }
192ece8a530Spatrick 
writeGotPlt(uint8_t * buf,const Symbol & s) const193ece8a530Spatrick void PPC::writeGotPlt(uint8_t *buf, const Symbol &s) const {
194ece8a530Spatrick   // Address of the symbol resolver stub in .glink .
195*05edf1c1Srobert   write32(buf, in.plt->getVA() + in.plt->headerSize + 4 * s.getPltIdx());
196ece8a530Spatrick }
197ece8a530Spatrick 
needsThunk(RelExpr expr,RelType type,const InputFile * file,uint64_t branchAddr,const Symbol & s,int64_t a) const198ece8a530Spatrick bool PPC::needsThunk(RelExpr expr, RelType type, const InputFile *file,
199ece8a530Spatrick                      uint64_t branchAddr, const Symbol &s, int64_t a) const {
200ece8a530Spatrick   if (type != R_PPC_LOCAL24PC && type != R_PPC_REL24 && type != R_PPC_PLTREL24)
201ece8a530Spatrick     return false;
202ece8a530Spatrick   if (s.isInPlt())
203ece8a530Spatrick     return true;
204ece8a530Spatrick   if (s.isUndefWeak())
205ece8a530Spatrick     return false;
206ece8a530Spatrick   return !PPC::inBranchRange(type, branchAddr, s.getVA(a));
207ece8a530Spatrick }
208ece8a530Spatrick 
getThunkSectionSpacing() const209ece8a530Spatrick uint32_t PPC::getThunkSectionSpacing() const { return 0x2000000; }
210ece8a530Spatrick 
inBranchRange(RelType type,uint64_t src,uint64_t dst) const211ece8a530Spatrick bool PPC::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
212ece8a530Spatrick   uint64_t offset = dst - src;
213ece8a530Spatrick   if (type == R_PPC_LOCAL24PC || type == R_PPC_REL24 || type == R_PPC_PLTREL24)
214ece8a530Spatrick     return isInt<26>(offset);
215ece8a530Spatrick   llvm_unreachable("unsupported relocation type used in branch");
216ece8a530Spatrick }
217ece8a530Spatrick 
getRelExpr(RelType type,const Symbol & s,const uint8_t * loc) const218ece8a530Spatrick RelExpr PPC::getRelExpr(RelType type, const Symbol &s,
219ece8a530Spatrick                         const uint8_t *loc) const {
220ece8a530Spatrick   switch (type) {
221ece8a530Spatrick   case R_PPC_NONE:
222ece8a530Spatrick     return R_NONE;
223ece8a530Spatrick   case R_PPC_ADDR16_HA:
224ece8a530Spatrick   case R_PPC_ADDR16_HI:
225ece8a530Spatrick   case R_PPC_ADDR16_LO:
2266dfb2addSgkoehler   case R_PPC_ADDR24:
227ece8a530Spatrick   case R_PPC_ADDR32:
228ece8a530Spatrick     return R_ABS;
229ece8a530Spatrick   case R_PPC_DTPREL16:
230ece8a530Spatrick   case R_PPC_DTPREL16_HA:
231ece8a530Spatrick   case R_PPC_DTPREL16_HI:
232ece8a530Spatrick   case R_PPC_DTPREL16_LO:
233ece8a530Spatrick   case R_PPC_DTPREL32:
234ece8a530Spatrick     return R_DTPREL;
235ece8a530Spatrick   case R_PPC_REL14:
236ece8a530Spatrick   case R_PPC_REL32:
237ece8a530Spatrick   case R_PPC_REL16_LO:
238ece8a530Spatrick   case R_PPC_REL16_HI:
239ece8a530Spatrick   case R_PPC_REL16_HA:
240ece8a530Spatrick     return R_PC;
241ece8a530Spatrick   case R_PPC_GOT16:
242ece8a530Spatrick     return R_GOT_OFF;
243ece8a530Spatrick   case R_PPC_LOCAL24PC:
244ece8a530Spatrick   case R_PPC_REL24:
245ece8a530Spatrick     return R_PLT_PC;
246ece8a530Spatrick   case R_PPC_PLTREL24:
247ece8a530Spatrick     return R_PPC32_PLTREL;
248ece8a530Spatrick   case R_PPC_GOT_TLSGD16:
249ece8a530Spatrick     return R_TLSGD_GOT;
250ece8a530Spatrick   case R_PPC_GOT_TLSLD16:
251ece8a530Spatrick     return R_TLSLD_GOT;
252ece8a530Spatrick   case R_PPC_GOT_TPREL16:
253ece8a530Spatrick     return R_GOT_OFF;
254ece8a530Spatrick   case R_PPC_TLS:
255ece8a530Spatrick     return R_TLSIE_HINT;
256ece8a530Spatrick   case R_PPC_TLSGD:
257ece8a530Spatrick     return R_TLSDESC_CALL;
258ece8a530Spatrick   case R_PPC_TLSLD:
259ece8a530Spatrick     return R_TLSLD_HINT;
260ece8a530Spatrick   case R_PPC_TPREL16:
261ece8a530Spatrick   case R_PPC_TPREL16_HA:
262ece8a530Spatrick   case R_PPC_TPREL16_LO:
263ece8a530Spatrick   case R_PPC_TPREL16_HI:
264a0747c9fSpatrick     return R_TPREL;
265ece8a530Spatrick   default:
266ece8a530Spatrick     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
267ece8a530Spatrick           ") against symbol " + toString(s));
268ece8a530Spatrick     return R_NONE;
269ece8a530Spatrick   }
270ece8a530Spatrick }
271ece8a530Spatrick 
getDynRel(RelType type) const272ece8a530Spatrick RelType PPC::getDynRel(RelType type) const {
273ece8a530Spatrick   if (type == R_PPC_ADDR32)
274ece8a530Spatrick     return type;
275ece8a530Spatrick   return R_PPC_NONE;
276ece8a530Spatrick }
277ece8a530Spatrick 
getImplicitAddend(const uint8_t * buf,RelType type) const278*05edf1c1Srobert int64_t PPC::getImplicitAddend(const uint8_t *buf, RelType type) const {
279*05edf1c1Srobert   switch (type) {
280*05edf1c1Srobert   case R_PPC_NONE:
281*05edf1c1Srobert     return 0;
282*05edf1c1Srobert   case R_PPC_ADDR32:
283*05edf1c1Srobert   case R_PPC_REL32:
284*05edf1c1Srobert     return SignExtend64<32>(read32(buf));
285*05edf1c1Srobert   default:
286*05edf1c1Srobert     internalLinkerError(getErrorLocation(buf),
287*05edf1c1Srobert                         "cannot read addend for relocation " + toString(type));
288*05edf1c1Srobert     return 0;
289*05edf1c1Srobert   }
290*05edf1c1Srobert }
291*05edf1c1Srobert 
fromDTPREL(RelType type,uint64_t val)292ece8a530Spatrick static std::pair<RelType, uint64_t> fromDTPREL(RelType type, uint64_t val) {
293ece8a530Spatrick   uint64_t dtpBiasedVal = val - 0x8000;
294ece8a530Spatrick   switch (type) {
295ece8a530Spatrick   case R_PPC_DTPREL16:
296ece8a530Spatrick     return {R_PPC64_ADDR16, dtpBiasedVal};
297ece8a530Spatrick   case R_PPC_DTPREL16_HA:
298ece8a530Spatrick     return {R_PPC_ADDR16_HA, dtpBiasedVal};
299ece8a530Spatrick   case R_PPC_DTPREL16_HI:
300ece8a530Spatrick     return {R_PPC_ADDR16_HI, dtpBiasedVal};
301ece8a530Spatrick   case R_PPC_DTPREL16_LO:
302ece8a530Spatrick     return {R_PPC_ADDR16_LO, dtpBiasedVal};
303ece8a530Spatrick   case R_PPC_DTPREL32:
304ece8a530Spatrick     return {R_PPC_ADDR32, dtpBiasedVal};
305ece8a530Spatrick   default:
306ece8a530Spatrick     return {type, val};
307ece8a530Spatrick   }
308ece8a530Spatrick }
309ece8a530Spatrick 
relocate(uint8_t * loc,const Relocation & rel,uint64_t val) const310adae0cfdSpatrick void PPC::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
311ece8a530Spatrick   RelType newType;
312adae0cfdSpatrick   std::tie(newType, val) = fromDTPREL(rel.type, val);
313ece8a530Spatrick   switch (newType) {
314ece8a530Spatrick   case R_PPC_ADDR16:
315adae0cfdSpatrick     checkIntUInt(loc, val, 16, rel);
316ece8a530Spatrick     write16(loc, val);
317ece8a530Spatrick     break;
318ece8a530Spatrick   case R_PPC_GOT16:
319ece8a530Spatrick   case R_PPC_GOT_TLSGD16:
320ece8a530Spatrick   case R_PPC_GOT_TLSLD16:
321ece8a530Spatrick   case R_PPC_GOT_TPREL16:
322ece8a530Spatrick   case R_PPC_TPREL16:
323adae0cfdSpatrick     checkInt(loc, val, 16, rel);
324ece8a530Spatrick     write16(loc, val);
325ece8a530Spatrick     break;
326ece8a530Spatrick   case R_PPC_ADDR16_HA:
327ece8a530Spatrick   case R_PPC_DTPREL16_HA:
328ece8a530Spatrick   case R_PPC_GOT_TLSGD16_HA:
329ece8a530Spatrick   case R_PPC_GOT_TLSLD16_HA:
330ece8a530Spatrick   case R_PPC_GOT_TPREL16_HA:
331ece8a530Spatrick   case R_PPC_REL16_HA:
332ece8a530Spatrick   case R_PPC_TPREL16_HA:
333ece8a530Spatrick     write16(loc, ha(val));
334ece8a530Spatrick     break;
335ece8a530Spatrick   case R_PPC_ADDR16_HI:
336ece8a530Spatrick   case R_PPC_DTPREL16_HI:
337ece8a530Spatrick   case R_PPC_GOT_TLSGD16_HI:
338ece8a530Spatrick   case R_PPC_GOT_TLSLD16_HI:
339ece8a530Spatrick   case R_PPC_GOT_TPREL16_HI:
340ece8a530Spatrick   case R_PPC_REL16_HI:
341ece8a530Spatrick   case R_PPC_TPREL16_HI:
342ece8a530Spatrick     write16(loc, val >> 16);
343ece8a530Spatrick     break;
344ece8a530Spatrick   case R_PPC_ADDR16_LO:
345ece8a530Spatrick   case R_PPC_DTPREL16_LO:
346ece8a530Spatrick   case R_PPC_GOT_TLSGD16_LO:
347ece8a530Spatrick   case R_PPC_GOT_TLSLD16_LO:
348ece8a530Spatrick   case R_PPC_GOT_TPREL16_LO:
349ece8a530Spatrick   case R_PPC_REL16_LO:
350ece8a530Spatrick   case R_PPC_TPREL16_LO:
351ece8a530Spatrick     write16(loc, val);
352ece8a530Spatrick     break;
353ece8a530Spatrick   case R_PPC_ADDR32:
354ece8a530Spatrick   case R_PPC_REL32:
355ece8a530Spatrick     write32(loc, val);
356ece8a530Spatrick     break;
357ece8a530Spatrick   case R_PPC_REL14: {
358ece8a530Spatrick     uint32_t mask = 0x0000FFFC;
359adae0cfdSpatrick     checkInt(loc, val, 16, rel);
360adae0cfdSpatrick     checkAlignment(loc, val, 4, rel);
361ece8a530Spatrick     write32(loc, (read32(loc) & ~mask) | (val & mask));
362ece8a530Spatrick     break;
363ece8a530Spatrick   }
3646dfb2addSgkoehler   case R_PPC_ADDR24:
365a0747c9fSpatrick   case R_PPC_REL24:
366ece8a530Spatrick   case R_PPC_LOCAL24PC:
367ece8a530Spatrick   case R_PPC_PLTREL24: {
368ece8a530Spatrick     uint32_t mask = 0x03FFFFFC;
369adae0cfdSpatrick     checkInt(loc, val, 26, rel);
370adae0cfdSpatrick     checkAlignment(loc, val, 4, rel);
371ece8a530Spatrick     write32(loc, (read32(loc) & ~mask) | (val & mask));
372ece8a530Spatrick     break;
373ece8a530Spatrick   }
374ece8a530Spatrick   default:
375ece8a530Spatrick     llvm_unreachable("unknown relocation");
376ece8a530Spatrick   }
377ece8a530Spatrick }
378ece8a530Spatrick 
adjustTlsExpr(RelType type,RelExpr expr) const379a0747c9fSpatrick RelExpr PPC::adjustTlsExpr(RelType type, RelExpr expr) const {
380ece8a530Spatrick   if (expr == R_RELAX_TLS_GD_TO_IE)
381ece8a530Spatrick     return R_RELAX_TLS_GD_TO_IE_GOT_OFF;
382ece8a530Spatrick   if (expr == R_RELAX_TLS_LD_TO_LE)
383ece8a530Spatrick     return R_RELAX_TLS_LD_TO_LE_ABS;
384ece8a530Spatrick   return expr;
385ece8a530Spatrick }
386ece8a530Spatrick 
getTlsGdRelaxSkip(RelType type) const387ece8a530Spatrick int PPC::getTlsGdRelaxSkip(RelType type) const {
388ece8a530Spatrick   // A __tls_get_addr call instruction is marked with 2 relocations:
389ece8a530Spatrick   //
390ece8a530Spatrick   //   R_PPC_TLSGD / R_PPC_TLSLD: marker relocation
391ece8a530Spatrick   //   R_PPC_REL24: __tls_get_addr
392ece8a530Spatrick   //
393ece8a530Spatrick   // After the relaxation we no longer call __tls_get_addr and should skip both
394ece8a530Spatrick   // relocations to not create a false dependence on __tls_get_addr being
395ece8a530Spatrick   // defined.
396ece8a530Spatrick   if (type == R_PPC_TLSGD || type == R_PPC_TLSLD)
397ece8a530Spatrick     return 2;
398ece8a530Spatrick   return 1;
399ece8a530Spatrick }
400ece8a530Spatrick 
relaxTlsGdToIe(uint8_t * loc,const Relocation & rel,uint64_t val) const401adae0cfdSpatrick void PPC::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
402adae0cfdSpatrick                          uint64_t val) const {
403adae0cfdSpatrick   switch (rel.type) {
404ece8a530Spatrick   case R_PPC_GOT_TLSGD16: {
405ece8a530Spatrick     // addi rT, rA, x@got@tlsgd --> lwz rT, x@got@tprel(rA)
406ece8a530Spatrick     uint32_t insn = readFromHalf16(loc);
407ece8a530Spatrick     writeFromHalf16(loc, 0x80000000 | (insn & 0x03ff0000));
408adae0cfdSpatrick     relocateNoSym(loc, R_PPC_GOT_TPREL16, val);
409ece8a530Spatrick     break;
410ece8a530Spatrick   }
411ece8a530Spatrick   case R_PPC_TLSGD:
412ece8a530Spatrick     // bl __tls_get_addr(x@tldgd) --> add r3, r3, r2
413ece8a530Spatrick     write32(loc, 0x7c631214);
414ece8a530Spatrick     break;
415ece8a530Spatrick   default:
416ece8a530Spatrick     llvm_unreachable("unsupported relocation for TLS GD to IE relaxation");
417ece8a530Spatrick   }
418ece8a530Spatrick }
419ece8a530Spatrick 
relaxTlsGdToLe(uint8_t * loc,const Relocation & rel,uint64_t val) const420adae0cfdSpatrick void PPC::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
421adae0cfdSpatrick                          uint64_t val) const {
422adae0cfdSpatrick   switch (rel.type) {
423ece8a530Spatrick   case R_PPC_GOT_TLSGD16:
424ece8a530Spatrick     // addi r3, r31, x@got@tlsgd --> addis r3, r2, x@tprel@ha
425ece8a530Spatrick     writeFromHalf16(loc, 0x3c620000 | ha(val));
426ece8a530Spatrick     break;
427ece8a530Spatrick   case R_PPC_TLSGD:
428ece8a530Spatrick     // bl __tls_get_addr(x@tldgd) --> add r3, r3, x@tprel@l
429ece8a530Spatrick     write32(loc, 0x38630000 | lo(val));
430ece8a530Spatrick     break;
431ece8a530Spatrick   default:
432ece8a530Spatrick     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
433ece8a530Spatrick   }
434ece8a530Spatrick }
435ece8a530Spatrick 
relaxTlsLdToLe(uint8_t * loc,const Relocation & rel,uint64_t val) const436adae0cfdSpatrick void PPC::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
437adae0cfdSpatrick                          uint64_t val) const {
438adae0cfdSpatrick   switch (rel.type) {
439ece8a530Spatrick   case R_PPC_GOT_TLSLD16:
440ece8a530Spatrick     // addi r3, rA, x@got@tlsgd --> addis r3, r2, 0
441ece8a530Spatrick     writeFromHalf16(loc, 0x3c620000);
442ece8a530Spatrick     break;
443ece8a530Spatrick   case R_PPC_TLSLD:
444ece8a530Spatrick     // r3+x@dtprel computes r3+x-0x8000, while we want it to compute r3+x@tprel
445ece8a530Spatrick     // = r3+x-0x7000, so add 4096 to r3.
446ece8a530Spatrick     // bl __tls_get_addr(x@tlsld) --> addi r3, r3, 4096
447ece8a530Spatrick     write32(loc, 0x38631000);
448ece8a530Spatrick     break;
449ece8a530Spatrick   case R_PPC_DTPREL16:
450ece8a530Spatrick   case R_PPC_DTPREL16_HA:
451ece8a530Spatrick   case R_PPC_DTPREL16_HI:
452ece8a530Spatrick   case R_PPC_DTPREL16_LO:
453adae0cfdSpatrick     relocate(loc, rel, val);
454ece8a530Spatrick     break;
455ece8a530Spatrick   default:
456ece8a530Spatrick     llvm_unreachable("unsupported relocation for TLS LD to LE relaxation");
457ece8a530Spatrick   }
458ece8a530Spatrick }
459ece8a530Spatrick 
relaxTlsIeToLe(uint8_t * loc,const Relocation & rel,uint64_t val) const460adae0cfdSpatrick void PPC::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
461adae0cfdSpatrick                          uint64_t val) const {
462adae0cfdSpatrick   switch (rel.type) {
463ece8a530Spatrick   case R_PPC_GOT_TPREL16: {
464ece8a530Spatrick     // lwz rT, x@got@tprel(rA) --> addis rT, r2, x@tprel@ha
465ece8a530Spatrick     uint32_t rt = readFromHalf16(loc) & 0x03e00000;
466ece8a530Spatrick     writeFromHalf16(loc, 0x3c020000 | rt | ha(val));
467ece8a530Spatrick     break;
468ece8a530Spatrick   }
469ece8a530Spatrick   case R_PPC_TLS: {
470ece8a530Spatrick     uint32_t insn = read32(loc);
471ece8a530Spatrick     if (insn >> 26 != 31)
472ece8a530Spatrick       error("unrecognized instruction for IE to LE R_PPC_TLS");
473ece8a530Spatrick     // addi rT, rT, x@tls --> addi rT, rT, x@tprel@l
474ece8a530Spatrick     uint32_t dFormOp = getPPCDFormOp((read32(loc) & 0x000007fe) >> 1);
475ece8a530Spatrick     if (dFormOp == 0)
476ece8a530Spatrick       error("unrecognized instruction for IE to LE R_PPC_TLS");
477ece8a530Spatrick     write32(loc, (dFormOp << 26) | (insn & 0x03ff0000) | lo(val));
478ece8a530Spatrick     break;
479ece8a530Spatrick   }
480ece8a530Spatrick   default:
481ece8a530Spatrick     llvm_unreachable("unsupported relocation for TLS IE to LE relaxation");
482ece8a530Spatrick   }
483ece8a530Spatrick }
484ece8a530Spatrick 
relocateAlloc(InputSectionBase & sec,uint8_t * buf) const485*05edf1c1Srobert void PPC::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
486*05edf1c1Srobert   uint64_t secAddr = sec.getOutputSection()->addr;
487*05edf1c1Srobert   if (auto *s = dyn_cast<InputSection>(&sec))
488*05edf1c1Srobert     secAddr += s->outSecOff;
489*05edf1c1Srobert   for (const Relocation &rel : sec.relocs()) {
490*05edf1c1Srobert     uint8_t *loc = buf + rel.offset;
491*05edf1c1Srobert     const uint64_t val = SignExtend64(
492*05edf1c1Srobert         sec.getRelocTargetVA(sec.file, rel.type, rel.addend,
493*05edf1c1Srobert                              secAddr + rel.offset, *rel.sym, rel.expr),
494*05edf1c1Srobert         32);
495*05edf1c1Srobert     switch (rel.expr) {
496*05edf1c1Srobert     case R_RELAX_TLS_GD_TO_IE_GOT_OFF:
497*05edf1c1Srobert       relaxTlsGdToIe(loc, rel, val);
498*05edf1c1Srobert       break;
499*05edf1c1Srobert     case R_RELAX_TLS_GD_TO_LE:
500*05edf1c1Srobert       relaxTlsGdToLe(loc, rel, val);
501*05edf1c1Srobert       break;
502*05edf1c1Srobert     case R_RELAX_TLS_LD_TO_LE_ABS:
503*05edf1c1Srobert       relaxTlsLdToLe(loc, rel, val);
504*05edf1c1Srobert       break;
505*05edf1c1Srobert     case R_RELAX_TLS_IE_TO_LE:
506*05edf1c1Srobert       relaxTlsIeToLe(loc, rel, val);
507*05edf1c1Srobert       break;
508*05edf1c1Srobert     default:
509*05edf1c1Srobert       relocate(loc, rel, val);
510*05edf1c1Srobert       break;
511*05edf1c1Srobert     }
512*05edf1c1Srobert   }
513*05edf1c1Srobert }
514*05edf1c1Srobert 
getPPCTargetInfo()515adae0cfdSpatrick TargetInfo *elf::getPPCTargetInfo() {
516ece8a530Spatrick   static PPC target;
517ece8a530Spatrick   return &target;
518ece8a530Spatrick }
519