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