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