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