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