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