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