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