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