1 //===- PPC.cpp ------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "OutputSections.h"
11 #include "SyntheticSections.h"
13 #include "lld/Common/ErrorHandler.h"
14 #include "llvm/Support/Endian.h"
17 using namespace llvm::support::endian;
18 using namespace llvm::ELF;
20 using namespace lld::elf;
23 class PPC final : public TargetInfo {
26 RelExpr getRelExpr(RelType type, const Symbol &s,
27 const uint8_t *loc) const override;
28 RelType getDynRel(RelType type) const override;
29 void writeGotHeader(uint8_t *buf) const override;
30 void writePltHeader(uint8_t *buf) const override {
31 llvm_unreachable("should call writePPC32GlinkSection() instead");
33 void writePlt(uint8_t *buf, uint64_t gotPltEntryAddr, uint64_t pltEntryAddr,
34 int32_t index, unsigned relOff) const override {
35 llvm_unreachable("should call writePPC32GlinkSection() instead");
37 void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
38 bool needsThunk(RelExpr expr, RelType relocType, const InputFile *file,
39 uint64_t branchAddr, const Symbol &s) const override;
40 uint32_t getThunkSectionSpacing() const override;
41 bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
42 void relocateOne(uint8_t *loc, RelType type, uint64_t val) const override;
43 RelExpr adjustRelaxExpr(RelType type, const uint8_t *data,
44 RelExpr expr) const override;
45 int getTlsGdRelaxSkip(RelType type) const override;
46 void relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const override;
47 void relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const override;
48 void relaxTlsLdToLe(uint8_t *loc, RelType type, uint64_t val) const override;
49 void relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const override;
53 static uint16_t lo(uint32_t v) { return v; }
54 static uint16_t ha(uint32_t v) { return (v + 0x8000) >> 16; }
56 static uint32_t readFromHalf16(const uint8_t *loc) {
57 return read32(config->isLE ? loc : loc - 2);
60 static void writeFromHalf16(uint8_t *loc, uint32_t insn) {
61 write32(config->isLE ? loc : loc - 2, insn);
64 void elf::writePPC32GlinkSection(uint8_t *buf, size_t numEntries) {
65 // On PPC Secure PLT ABI, bl foo@plt jumps to a call stub, which loads an
66 // absolute address from a specific .plt slot (usually called .got.plt on
67 // other targets) and jumps there.
69 // a) With immediate binding (BIND_NOW), the .plt entry is resolved at load
70 // time. The .glink section is not used.
71 // b) With lazy binding, the .plt entry points to a `b PLTresolve`
72 // instruction in .glink, filled in by PPC::writeGotPlt().
74 // Write N `b PLTresolve` first.
75 for (size_t i = 0; i != numEntries; ++i)
76 write32(buf + 4 * i, 0x48000000 | 4 * (numEntries - i));
77 buf += 4 * numEntries;
79 // Then write PLTresolve(), which has two forms: PIC and non-PIC. PLTresolve()
80 // computes the PLT index (by computing the distance from the landing b to
81 // itself) and calls _dl_runtime_resolve() (in glibc).
82 uint32_t got = in.got->getVA();
83 uint32_t glink = in.plt->getVA(); // VA of .glink
84 const uint8_t *end = buf + 64;
86 uint32_t afterBcl = in.plt->getSize() - target->pltHeaderSize + 12;
87 uint32_t gotBcl = got + 4 - (glink + afterBcl);
88 write32(buf + 0, 0x3d6b0000 | ha(afterBcl)); // addis r11,r11,1f-glink@ha
89 write32(buf + 4, 0x7c0802a6); // mflr r0
90 write32(buf + 8, 0x429f0005); // bcl 20,30,.+4
91 write32(buf + 12, 0x396b0000 | lo(afterBcl)); // 1: addi r11,r11,1b-.glink@l
92 write32(buf + 16, 0x7d8802a6); // mflr r12
93 write32(buf + 20, 0x7c0803a6); // mtlr r0
94 write32(buf + 24, 0x7d6c5850); // sub r11,r11,r12
95 write32(buf + 28, 0x3d8c0000 | ha(gotBcl)); // addis 12,12,GOT+4-1b@ha
96 if (ha(gotBcl) == ha(gotBcl + 4)) {
97 write32(buf + 32, 0x800c0000 | lo(gotBcl)); // lwz r0,r12,GOT+4-1b@l(r12)
99 0x818c0000 | lo(gotBcl + 4)); // lwz r12,r12,GOT+8-1b@l(r12)
101 write32(buf + 32, 0x840c0000 | lo(gotBcl)); // lwzu r0,r12,GOT+4-1b@l(r12)
102 write32(buf + 36, 0x818c0000 | 4); // lwz r12,r12,4(r12)
104 write32(buf + 40, 0x7c0903a6); // mtctr 0
105 write32(buf + 44, 0x7c0b5a14); // add r0,11,11
106 write32(buf + 48, 0x7d605a14); // add r11,0,11
107 write32(buf + 52, 0x4e800420); // bctr
110 write32(buf + 0, 0x3d800000 | ha(got + 4)); // lis r12,GOT+4@ha
111 write32(buf + 4, 0x3d6b0000 | ha(-glink)); // addis r11,r11,-Glink@ha
112 if (ha(got + 4) == ha(got + 8))
113 write32(buf + 8, 0x800c0000 | lo(got + 4)); // lwz r0,GOT+4@l(r12)
115 write32(buf + 8, 0x840c0000 | lo(got + 4)); // lwzu r0,GOT+4@l(r12)
116 write32(buf + 12, 0x396b0000 | lo(-glink)); // addi r11,r11,-Glink@l
117 write32(buf + 16, 0x7c0903a6); // mtctr r0
118 write32(buf + 20, 0x7c0b5a14); // add r0,r11,r11
119 if (ha(got + 4) == ha(got + 8))
120 write32(buf + 24, 0x818c0000 | lo(got + 8)); // lwz r12,GOT+8@ha(r12)
122 write32(buf + 24, 0x818c0000 | 4); // lwz r12,4(r12)
123 write32(buf + 28, 0x7d605a14); // add r11,r0,r11
124 write32(buf + 32, 0x4e800420); // bctr
128 // Pad with nop. They should not be executed.
129 for (; buf < end; buf += 4)
130 write32(buf, 0x60000000);
134 gotRel = R_PPC_GLOB_DAT;
135 noneRel = R_PPC_NONE;
136 pltRel = R_PPC_JMP_SLOT;
137 relativeRel = R_PPC_RELATIVE;
138 iRelativeRel = R_PPC_IRELATIVE;
139 symbolicRel = R_PPC_ADDR32;
140 gotBaseSymInGotPlt = false;
141 gotHeaderEntriesNum = 3;
142 gotPltHeaderEntriesNum = 0;
143 pltHeaderSize = 64; // size of PLTresolve in .glink
148 tlsModuleIndexRel = R_PPC_DTPMOD32;
149 tlsOffsetRel = R_PPC_DTPREL32;
150 tlsGotRel = R_PPC_TPREL32;
152 defaultMaxPageSize = 65536;
153 defaultImageBase = 0x10000000;
155 write32(trapInstr.data(), 0x7fe00008);
158 void PPC::writeGotHeader(uint8_t *buf) const {
159 // _GLOBAL_OFFSET_TABLE_[0] = _DYNAMIC
160 // glibc stores _dl_runtime_resolve in _GLOBAL_OFFSET_TABLE_[1],
161 // link_map in _GLOBAL_OFFSET_TABLE_[2].
162 write32(buf, mainPart->dynamic->getVA());
165 void PPC::writeGotPlt(uint8_t *buf, const Symbol &s) const {
166 // Address of the symbol resolver stub in .glink .
167 write32(buf, in.plt->getVA() + 4 * s.pltIndex);
170 bool PPC::needsThunk(RelExpr expr, RelType type, const InputFile *file,
171 uint64_t branchAddr, const Symbol &s) const {
172 if (type != R_PPC_REL24 && type != R_PPC_PLTREL24)
178 return !(expr == R_PC && PPC::inBranchRange(type, branchAddr, s.getVA()));
181 uint32_t PPC::getThunkSectionSpacing() const { return 0x2000000; }
183 bool PPC::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
184 uint64_t offset = dst - src;
185 if (type == R_PPC_REL24 || type == R_PPC_PLTREL24)
186 return isInt<26>(offset);
187 llvm_unreachable("unsupported relocation type used in branch");
190 RelExpr PPC::getRelExpr(RelType type, const Symbol &s,
191 const uint8_t *loc) const {
195 case R_PPC_ADDR16_HA:
196 case R_PPC_ADDR16_HI:
197 case R_PPC_ADDR16_LO:
201 case R_PPC_DTPREL16_HA:
202 case R_PPC_DTPREL16_HI:
203 case R_PPC_DTPREL16_LO:
208 case R_PPC_LOCAL24PC:
218 return R_PPC32_PLTREL;
219 case R_PPC_GOT_TLSGD16:
221 case R_PPC_GOT_TLSLD16:
223 case R_PPC_GOT_TPREL16:
228 return R_TLSDESC_CALL;
232 case R_PPC_TPREL16_HA:
233 case R_PPC_TPREL16_LO:
234 case R_PPC_TPREL16_HI:
237 error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
238 ") against symbol " + toString(s));
243 RelType PPC::getDynRel(RelType type) const {
244 if (type == R_PPC_ADDR32)
249 static std::pair<RelType, uint64_t> fromDTPREL(RelType type, uint64_t val) {
250 uint64_t dtpBiasedVal = val - 0x8000;
253 return {R_PPC64_ADDR16, dtpBiasedVal};
254 case R_PPC_DTPREL16_HA:
255 return {R_PPC_ADDR16_HA, dtpBiasedVal};
256 case R_PPC_DTPREL16_HI:
257 return {R_PPC_ADDR16_HI, dtpBiasedVal};
258 case R_PPC_DTPREL16_LO:
259 return {R_PPC_ADDR16_LO, dtpBiasedVal};
261 return {R_PPC_ADDR32, dtpBiasedVal};
267 void PPC::relocateOne(uint8_t *loc, RelType type, uint64_t val) const {
269 std::tie(newType, val) = fromDTPREL(type, val);
272 checkIntUInt(loc, val, 16, type);
276 case R_PPC_GOT_TLSGD16:
277 case R_PPC_GOT_TLSLD16:
278 case R_PPC_GOT_TPREL16:
280 checkInt(loc, val, 16, type);
283 case R_PPC_ADDR16_HA:
284 case R_PPC_DTPREL16_HA:
285 case R_PPC_GOT_TLSGD16_HA:
286 case R_PPC_GOT_TLSLD16_HA:
287 case R_PPC_GOT_TPREL16_HA:
289 case R_PPC_TPREL16_HA:
290 write16(loc, ha(val));
292 case R_PPC_ADDR16_HI:
293 case R_PPC_DTPREL16_HI:
294 case R_PPC_GOT_TLSGD16_HI:
295 case R_PPC_GOT_TLSLD16_HI:
296 case R_PPC_GOT_TPREL16_HI:
298 case R_PPC_TPREL16_HI:
299 write16(loc, val >> 16);
301 case R_PPC_ADDR16_LO:
302 case R_PPC_DTPREL16_LO:
303 case R_PPC_GOT_TLSGD16_LO:
304 case R_PPC_GOT_TLSLD16_LO:
305 case R_PPC_GOT_TPREL16_LO:
307 case R_PPC_TPREL16_LO:
315 uint32_t mask = 0x0000FFFC;
316 checkInt(loc, val, 16, type);
317 checkAlignment(loc, val, 4, type);
318 write32(loc, (read32(loc) & ~mask) | (val & mask));
322 case R_PPC_LOCAL24PC:
323 case R_PPC_PLTREL24: {
324 uint32_t mask = 0x03FFFFFC;
325 checkInt(loc, val, 26, type);
326 checkAlignment(loc, val, 4, type);
327 write32(loc, (read32(loc) & ~mask) | (val & mask));
331 llvm_unreachable("unknown relocation");
335 RelExpr PPC::adjustRelaxExpr(RelType type, const uint8_t *data,
336 RelExpr expr) const {
337 if (expr == R_RELAX_TLS_GD_TO_IE)
338 return R_RELAX_TLS_GD_TO_IE_GOT_OFF;
339 if (expr == R_RELAX_TLS_LD_TO_LE)
340 return R_RELAX_TLS_LD_TO_LE_ABS;
344 int PPC::getTlsGdRelaxSkip(RelType type) const {
345 // A __tls_get_addr call instruction is marked with 2 relocations:
347 // R_PPC_TLSGD / R_PPC_TLSLD: marker relocation
348 // R_PPC_REL24: __tls_get_addr
350 // After the relaxation we no longer call __tls_get_addr and should skip both
351 // relocations to not create a false dependence on __tls_get_addr being
353 if (type == R_PPC_TLSGD || type == R_PPC_TLSLD)
358 void PPC::relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const {
360 case R_PPC_GOT_TLSGD16: {
361 // addi rT, rA, x@got@tlsgd --> lwz rT, x@got@tprel(rA)
362 uint32_t insn = readFromHalf16(loc);
363 writeFromHalf16(loc, 0x80000000 | (insn & 0x03ff0000));
364 relocateOne(loc, R_PPC_GOT_TPREL16, val);
368 // bl __tls_get_addr(x@tldgd) --> add r3, r3, r2
369 write32(loc, 0x7c631214);
372 llvm_unreachable("unsupported relocation for TLS GD to IE relaxation");
376 void PPC::relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const {
378 case R_PPC_GOT_TLSGD16:
379 // addi r3, r31, x@got@tlsgd --> addis r3, r2, x@tprel@ha
380 writeFromHalf16(loc, 0x3c620000 | ha(val));
383 // bl __tls_get_addr(x@tldgd) --> add r3, r3, x@tprel@l
384 write32(loc, 0x38630000 | lo(val));
387 llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
391 void PPC::relaxTlsLdToLe(uint8_t *loc, RelType type, uint64_t val) const {
393 case R_PPC_GOT_TLSLD16:
394 // addi r3, rA, x@got@tlsgd --> addis r3, r2, 0
395 writeFromHalf16(loc, 0x3c620000);
398 // r3+x@dtprel computes r3+x-0x8000, while we want it to compute r3+x@tprel
399 // = r3+x-0x7000, so add 4096 to r3.
400 // bl __tls_get_addr(x@tlsld) --> addi r3, r3, 4096
401 write32(loc, 0x38631000);
404 case R_PPC_DTPREL16_HA:
405 case R_PPC_DTPREL16_HI:
406 case R_PPC_DTPREL16_LO:
407 relocateOne(loc, type, val);
410 llvm_unreachable("unsupported relocation for TLS LD to LE relaxation");
414 void PPC::relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const {
416 case R_PPC_GOT_TPREL16: {
417 // lwz rT, x@got@tprel(rA) --> addis rT, r2, x@tprel@ha
418 uint32_t rt = readFromHalf16(loc) & 0x03e00000;
419 writeFromHalf16(loc, 0x3c020000 | rt | ha(val));
423 uint32_t insn = read32(loc);
424 if (insn >> 26 != 31)
425 error("unrecognized instruction for IE to LE R_PPC_TLS");
426 // addi rT, rT, x@tls --> addi rT, rT, x@tprel@l
427 uint32_t dFormOp = getPPCDFormOp((read32(loc) & 0x000007fe) >> 1);
429 error("unrecognized instruction for IE to LE R_PPC_TLS");
430 write32(loc, (dFormOp << 26) | (insn & 0x03ff0000) | lo(val));
434 llvm_unreachable("unsupported relocation for TLS IE to LE relaxation");
438 TargetInfo *elf::getPPCTargetInfo() {