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