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