Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lld / ELF / Arch / PPC64.cpp
blob097a57514770aaf8f2b29504bbdedb6127bff322
1 //===- PPC64.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 //===----------------------------------------------------------------------===//
9 #include "InputFiles.h"
10 #include "OutputSections.h"
11 #include "SymbolTable.h"
12 #include "Symbols.h"
13 #include "SyntheticSections.h"
14 #include "Target.h"
15 #include "Thunks.h"
16 #include "lld/Common/CommonLinkerContext.h"
17 #include "llvm/Support/Endian.h"
19 using namespace llvm;
20 using namespace llvm::object;
21 using namespace llvm::support::endian;
22 using namespace llvm::ELF;
23 using namespace lld;
24 using namespace lld::elf;
26 constexpr uint64_t ppc64TocOffset = 0x8000;
27 constexpr uint64_t dynamicThreadPointerOffset = 0x8000;
29 // The instruction encoding of bits 21-30 from the ISA for the Xform and Dform
30 // instructions that can be used as part of the initial exec TLS sequence.
31 enum XFormOpcd {
32 LBZX = 87,
33 LHZX = 279,
34 LWZX = 23,
35 LDX = 21,
36 STBX = 215,
37 STHX = 407,
38 STWX = 151,
39 STDX = 149,
40 LHAX = 343,
41 LWAX = 341,
42 LFSX = 535,
43 LFDX = 599,
44 STFSX = 663,
45 STFDX = 727,
46 ADD = 266,
49 enum DFormOpcd {
50 LBZ = 34,
51 LBZU = 35,
52 LHZ = 40,
53 LHZU = 41,
54 LHAU = 43,
55 LWZ = 32,
56 LWZU = 33,
57 LFSU = 49,
58 LFDU = 51,
59 STB = 38,
60 STBU = 39,
61 STH = 44,
62 STHU = 45,
63 STW = 36,
64 STWU = 37,
65 STFSU = 53,
66 STFDU = 55,
67 LHA = 42,
68 LFS = 48,
69 LFD = 50,
70 STFS = 52,
71 STFD = 54,
72 ADDI = 14
75 enum DSFormOpcd {
76 LD = 58,
77 LWA = 58,
78 STD = 62
81 constexpr uint32_t NOP = 0x60000000;
83 enum class PPCLegacyInsn : uint32_t {
84 NOINSN = 0,
85 // Loads.
86 LBZ = 0x88000000,
87 LHZ = 0xa0000000,
88 LWZ = 0x80000000,
89 LHA = 0xa8000000,
90 LWA = 0xe8000002,
91 LD = 0xe8000000,
92 LFS = 0xC0000000,
93 LXSSP = 0xe4000003,
94 LFD = 0xc8000000,
95 LXSD = 0xe4000002,
96 LXV = 0xf4000001,
97 LXVP = 0x18000000,
99 // Stores.
100 STB = 0x98000000,
101 STH = 0xb0000000,
102 STW = 0x90000000,
103 STD = 0xf8000000,
104 STFS = 0xd0000000,
105 STXSSP = 0xf4000003,
106 STFD = 0xd8000000,
107 STXSD = 0xf4000002,
108 STXV = 0xf4000005,
109 STXVP = 0x18000001
111 enum class PPCPrefixedInsn : uint64_t {
112 NOINSN = 0,
113 PREFIX_MLS = 0x0610000000000000,
114 PREFIX_8LS = 0x0410000000000000,
116 // Loads.
117 PLBZ = PREFIX_MLS,
118 PLHZ = PREFIX_MLS,
119 PLWZ = PREFIX_MLS,
120 PLHA = PREFIX_MLS,
121 PLWA = PREFIX_8LS | 0xa4000000,
122 PLD = PREFIX_8LS | 0xe4000000,
123 PLFS = PREFIX_MLS,
124 PLXSSP = PREFIX_8LS | 0xac000000,
125 PLFD = PREFIX_MLS,
126 PLXSD = PREFIX_8LS | 0xa8000000,
127 PLXV = PREFIX_8LS | 0xc8000000,
128 PLXVP = PREFIX_8LS | 0xe8000000,
130 // Stores.
131 PSTB = PREFIX_MLS,
132 PSTH = PREFIX_MLS,
133 PSTW = PREFIX_MLS,
134 PSTD = PREFIX_8LS | 0xf4000000,
135 PSTFS = PREFIX_MLS,
136 PSTXSSP = PREFIX_8LS | 0xbc000000,
137 PSTFD = PREFIX_MLS,
138 PSTXSD = PREFIX_8LS | 0xb8000000,
139 PSTXV = PREFIX_8LS | 0xd8000000,
140 PSTXVP = PREFIX_8LS | 0xf8000000
142 static bool checkPPCLegacyInsn(uint32_t encoding) {
143 PPCLegacyInsn insn = static_cast<PPCLegacyInsn>(encoding);
144 if (insn == PPCLegacyInsn::NOINSN)
145 return false;
146 #define PCREL_OPT(Legacy, PCRel, InsnMask) \
147 if (insn == PPCLegacyInsn::Legacy) \
148 return true;
149 #include "PPCInsns.def"
150 #undef PCREL_OPT
151 return false;
154 // Masks to apply to legacy instructions when converting them to prefixed,
155 // pc-relative versions. For the most part, the primary opcode is shared
156 // between the legacy instruction and the suffix of its prefixed version.
157 // However, there are some instances where that isn't the case (DS-Form and
158 // DQ-form instructions).
159 enum class LegacyToPrefixMask : uint64_t {
160 NOMASK = 0x0,
161 OPC_AND_RST = 0xffe00000, // Primary opc (0-5) and R[ST] (6-10).
162 ONLY_RST = 0x3e00000, // [RS]T (6-10).
163 ST_STX28_TO5 =
164 0x8000000003e00000, // S/T (6-10) - The [S/T]X bit moves from 28 to 5.
167 namespace {
168 class PPC64 final : public TargetInfo {
169 public:
170 PPC64();
171 int getTlsGdRelaxSkip(RelType type) const override;
172 uint32_t calcEFlags() const override;
173 RelExpr getRelExpr(RelType type, const Symbol &s,
174 const uint8_t *loc) const override;
175 RelType getDynRel(RelType type) const override;
176 int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
177 void writePltHeader(uint8_t *buf) const override;
178 void writePlt(uint8_t *buf, const Symbol &sym,
179 uint64_t pltEntryAddr) const override;
180 void writeIplt(uint8_t *buf, const Symbol &sym,
181 uint64_t pltEntryAddr) const override;
182 void relocate(uint8_t *loc, const Relocation &rel,
183 uint64_t val) const override;
184 void writeGotHeader(uint8_t *buf) const override;
185 bool needsThunk(RelExpr expr, RelType type, const InputFile *file,
186 uint64_t branchAddr, const Symbol &s,
187 int64_t a) const override;
188 uint32_t getThunkSectionSpacing() const override;
189 bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
190 RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
191 RelExpr adjustGotPcExpr(RelType type, int64_t addend,
192 const uint8_t *loc) const override;
193 void relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) const;
194 void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
196 bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
197 uint8_t stOther) const override;
199 private:
200 void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
201 void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
202 void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
203 void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
205 } // namespace
207 uint64_t elf::getPPC64TocBase() {
208 // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
209 // TOC starts where the first of these sections starts. We always create a
210 // .got when we see a relocation that uses it, so for us the start is always
211 // the .got.
212 uint64_t tocVA = in.got->getVA();
214 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
215 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
216 // code (crt1.o) assumes that you can get from the TOC base to the
217 // start of the .toc section with only a single (signed) 16-bit relocation.
218 return tocVA + ppc64TocOffset;
221 unsigned elf::getPPC64GlobalEntryToLocalEntryOffset(uint8_t stOther) {
222 // The offset is encoded into the 3 most significant bits of the st_other
223 // field, with some special values described in section 3.4.1 of the ABI:
224 // 0 --> Zero offset between the GEP and LEP, and the function does NOT use
225 // the TOC pointer (r2). r2 will hold the same value on returning from
226 // the function as it did on entering the function.
227 // 1 --> Zero offset between the GEP and LEP, and r2 should be treated as a
228 // caller-saved register for all callers.
229 // 2-6 --> The binary logarithm of the offset eg:
230 // 2 --> 2^2 = 4 bytes --> 1 instruction.
231 // 6 --> 2^6 = 64 bytes --> 16 instructions.
232 // 7 --> Reserved.
233 uint8_t gepToLep = (stOther >> 5) & 7;
234 if (gepToLep < 2)
235 return 0;
237 // The value encoded in the st_other bits is the
238 // log-base-2(offset).
239 if (gepToLep < 7)
240 return 1 << gepToLep;
242 error("reserved value of 7 in the 3 most-significant-bits of st_other");
243 return 0;
246 void elf::writePrefixedInstruction(uint8_t *loc, uint64_t insn) {
247 insn = config->isLE ? insn << 32 | insn >> 32 : insn;
248 write64(loc, insn);
251 static bool addOptional(StringRef name, uint64_t value,
252 std::vector<Defined *> &defined) {
253 Symbol *sym = symtab.find(name);
254 if (!sym || sym->isDefined())
255 return false;
256 sym->resolve(Defined{/*file=*/nullptr, StringRef(), STB_GLOBAL, STV_HIDDEN,
257 STT_FUNC, value,
258 /*size=*/0, /*section=*/nullptr});
259 defined.push_back(cast<Defined>(sym));
260 return true;
263 // If from is 14, write ${prefix}14: firstInsn; ${prefix}15:
264 // firstInsn+0x200008; ...; ${prefix}31: firstInsn+(31-14)*0x200008; $tail
265 // The labels are defined only if they exist in the symbol table.
266 static void writeSequence(MutableArrayRef<uint32_t> buf, const char *prefix,
267 int from, uint32_t firstInsn,
268 ArrayRef<uint32_t> tail) {
269 std::vector<Defined *> defined;
270 char name[16];
271 int first;
272 uint32_t *ptr = buf.data();
273 for (int r = from; r < 32; ++r) {
274 format("%s%d", prefix, r).snprint(name, sizeof(name));
275 if (addOptional(name, 4 * (r - from), defined) && defined.size() == 1)
276 first = r - from;
277 write32(ptr++, firstInsn + 0x200008 * (r - from));
279 for (uint32_t insn : tail)
280 write32(ptr++, insn);
281 assert(ptr == &*buf.end());
283 if (defined.empty())
284 return;
285 // The full section content has the extent of [begin, end). We drop unused
286 // instructions and write [first,end).
287 auto *sec = make<InputSection>(
288 nullptr, SHF_ALLOC, SHT_PROGBITS, 4,
289 ArrayRef(reinterpret_cast<uint8_t *>(buf.data() + first),
290 4 * (buf.size() - first)),
291 ".text");
292 ctx.inputSections.push_back(sec);
293 for (Defined *sym : defined) {
294 sym->section = sec;
295 sym->value -= 4 * first;
299 // Implements some save and restore functions as described by ELF V2 ABI to be
300 // compatible with GCC. With GCC -Os, when the number of call-saved registers
301 // exceeds a certain threshold, GCC generates _savegpr0_* _restgpr0_* calls and
302 // expects the linker to define them. See
303 // https://sourceware.org/pipermail/binutils/2002-February/017444.html and
304 // https://sourceware.org/pipermail/binutils/2004-August/036765.html . This is
305 // weird because libgcc.a would be the natural place. The linker generation
306 // approach has the advantage that the linker can generate multiple copies to
307 // avoid long branch thunks. However, we don't consider the advantage
308 // significant enough to complicate our trunk implementation, so we take the
309 // simple approach and synthesize .text sections providing the implementation.
310 void elf::addPPC64SaveRestore() {
311 static uint32_t savegpr0[20], restgpr0[21], savegpr1[19], restgpr1[19];
312 constexpr uint32_t blr = 0x4e800020, mtlr_0 = 0x7c0803a6;
314 // _restgpr0_14: ld 14, -144(1); _restgpr0_15: ld 15, -136(1); ...
315 // Tail: ld 0, 16(1); mtlr 0; blr
316 writeSequence(restgpr0, "_restgpr0_", 14, 0xe9c1ff70,
317 {0xe8010010, mtlr_0, blr});
318 // _restgpr1_14: ld 14, -144(12); _restgpr1_15: ld 15, -136(12); ...
319 // Tail: blr
320 writeSequence(restgpr1, "_restgpr1_", 14, 0xe9ccff70, {blr});
321 // _savegpr0_14: std 14, -144(1); _savegpr0_15: std 15, -136(1); ...
322 // Tail: std 0, 16(1); blr
323 writeSequence(savegpr0, "_savegpr0_", 14, 0xf9c1ff70, {0xf8010010, blr});
324 // _savegpr1_14: std 14, -144(12); _savegpr1_15: std 15, -136(12); ...
325 // Tail: blr
326 writeSequence(savegpr1, "_savegpr1_", 14, 0xf9ccff70, {blr});
329 // Find the R_PPC64_ADDR64 in .rela.toc with matching offset.
330 template <typename ELFT>
331 static std::pair<Defined *, int64_t>
332 getRelaTocSymAndAddend(InputSectionBase *tocSec, uint64_t offset) {
333 // .rela.toc contains exclusively R_PPC64_ADDR64 relocations sorted by
334 // r_offset: 0, 8, 16, etc. For a given Offset, Offset / 8 gives us the
335 // relocation index in most cases.
337 // In rare cases a TOC entry may store a constant that doesn't need an
338 // R_PPC64_ADDR64, the corresponding r_offset is therefore missing. Offset / 8
339 // points to a relocation with larger r_offset. Do a linear probe then.
340 // Constants are extremely uncommon in .toc and the extra number of array
341 // accesses can be seen as a small constant.
342 ArrayRef<typename ELFT::Rela> relas =
343 tocSec->template relsOrRelas<ELFT>().relas;
344 if (relas.empty())
345 return {};
346 uint64_t index = std::min<uint64_t>(offset / 8, relas.size() - 1);
347 for (;;) {
348 if (relas[index].r_offset == offset) {
349 Symbol &sym = tocSec->getFile<ELFT>()->getRelocTargetSym(relas[index]);
350 return {dyn_cast<Defined>(&sym), getAddend<ELFT>(relas[index])};
352 if (relas[index].r_offset < offset || index == 0)
353 break;
354 --index;
356 return {};
359 // When accessing a symbol defined in another translation unit, compilers
360 // reserve a .toc entry, allocate a local label and generate toc-indirect
361 // instructions:
363 // addis 3, 2, .LC0@toc@ha # R_PPC64_TOC16_HA
364 // ld 3, .LC0@toc@l(3) # R_PPC64_TOC16_LO_DS, load the address from a .toc entry
365 // ld/lwa 3, 0(3) # load the value from the address
367 // .section .toc,"aw",@progbits
368 // .LC0: .tc var[TC],var
370 // If var is defined, non-preemptable and addressable with a 32-bit signed
371 // offset from the toc base, the address of var can be computed by adding an
372 // offset to the toc base, saving a load.
374 // addis 3,2,var@toc@ha # this may be relaxed to a nop,
375 // addi 3,3,var@toc@l # then this becomes addi 3,2,var@toc
376 // ld/lwa 3, 0(3) # load the value from the address
378 // Returns true if the relaxation is performed.
379 static bool tryRelaxPPC64TocIndirection(const Relocation &rel,
380 uint8_t *bufLoc) {
381 assert(config->tocOptimize);
382 if (rel.addend < 0)
383 return false;
385 // If the symbol is not the .toc section, this isn't a toc-indirection.
386 Defined *defSym = dyn_cast<Defined>(rel.sym);
387 if (!defSym || !defSym->isSection() || defSym->section->name != ".toc")
388 return false;
390 Defined *d;
391 int64_t addend;
392 auto *tocISB = cast<InputSectionBase>(defSym->section);
393 std::tie(d, addend) =
394 config->isLE ? getRelaTocSymAndAddend<ELF64LE>(tocISB, rel.addend)
395 : getRelaTocSymAndAddend<ELF64BE>(tocISB, rel.addend);
397 // Only non-preemptable defined symbols can be relaxed.
398 if (!d || d->isPreemptible)
399 return false;
401 // R_PPC64_ADDR64 should have created a canonical PLT for the non-preemptable
402 // ifunc and changed its type to STT_FUNC.
403 assert(!d->isGnuIFunc());
405 // Two instructions can materialize a 32-bit signed offset from the toc base.
406 uint64_t tocRelative = d->getVA(addend) - getPPC64TocBase();
407 if (!isInt<32>(tocRelative))
408 return false;
410 // Add PPC64TocOffset that will be subtracted by PPC64::relocate().
411 static_cast<const PPC64 &>(*target).relaxGot(bufLoc, rel,
412 tocRelative + ppc64TocOffset);
413 return true;
416 // Relocation masks following the #lo(value), #hi(value), #ha(value),
417 // #higher(value), #highera(value), #highest(value), and #highesta(value)
418 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
419 // document.
420 static uint16_t lo(uint64_t v) { return v; }
421 static uint16_t hi(uint64_t v) { return v >> 16; }
422 static uint64_t ha(uint64_t v) { return (v + 0x8000) >> 16; }
423 static uint16_t higher(uint64_t v) { return v >> 32; }
424 static uint16_t highera(uint64_t v) { return (v + 0x8000) >> 32; }
425 static uint16_t highest(uint64_t v) { return v >> 48; }
426 static uint16_t highesta(uint64_t v) { return (v + 0x8000) >> 48; }
428 // Extracts the 'PO' field of an instruction encoding.
429 static uint8_t getPrimaryOpCode(uint32_t encoding) { return (encoding >> 26); }
431 static bool isDQFormInstruction(uint32_t encoding) {
432 switch (getPrimaryOpCode(encoding)) {
433 default:
434 return false;
435 case 6: // Power10 paired loads/stores (lxvp, stxvp).
436 case 56:
437 // The only instruction with a primary opcode of 56 is `lq`.
438 return true;
439 case 61:
440 // There are both DS and DQ instruction forms with this primary opcode.
441 // Namely `lxv` and `stxv` are the DQ-forms that use it.
442 // The DS 'XO' bits being set to 01 is restricted to DQ form.
443 return (encoding & 3) == 0x1;
447 static bool isDSFormInstruction(PPCLegacyInsn insn) {
448 switch (insn) {
449 default:
450 return false;
451 case PPCLegacyInsn::LWA:
452 case PPCLegacyInsn::LD:
453 case PPCLegacyInsn::LXSD:
454 case PPCLegacyInsn::LXSSP:
455 case PPCLegacyInsn::STD:
456 case PPCLegacyInsn::STXSD:
457 case PPCLegacyInsn::STXSSP:
458 return true;
462 static PPCLegacyInsn getPPCLegacyInsn(uint32_t encoding) {
463 uint32_t opc = encoding & 0xfc000000;
465 // If the primary opcode is shared between multiple instructions, we need to
466 // fix it up to match the actual instruction we are after.
467 if ((opc == 0xe4000000 || opc == 0xe8000000 || opc == 0xf4000000 ||
468 opc == 0xf8000000) &&
469 !isDQFormInstruction(encoding))
470 opc = encoding & 0xfc000003;
471 else if (opc == 0xf4000000)
472 opc = encoding & 0xfc000007;
473 else if (opc == 0x18000000)
474 opc = encoding & 0xfc00000f;
476 // If the value is not one of the enumerators in PPCLegacyInsn, we want to
477 // return PPCLegacyInsn::NOINSN.
478 if (!checkPPCLegacyInsn(opc))
479 return PPCLegacyInsn::NOINSN;
480 return static_cast<PPCLegacyInsn>(opc);
483 static PPCPrefixedInsn getPCRelativeForm(PPCLegacyInsn insn) {
484 switch (insn) {
485 #define PCREL_OPT(Legacy, PCRel, InsnMask) \
486 case PPCLegacyInsn::Legacy: \
487 return PPCPrefixedInsn::PCRel
488 #include "PPCInsns.def"
489 #undef PCREL_OPT
491 return PPCPrefixedInsn::NOINSN;
494 static LegacyToPrefixMask getInsnMask(PPCLegacyInsn insn) {
495 switch (insn) {
496 #define PCREL_OPT(Legacy, PCRel, InsnMask) \
497 case PPCLegacyInsn::Legacy: \
498 return LegacyToPrefixMask::InsnMask
499 #include "PPCInsns.def"
500 #undef PCREL_OPT
502 return LegacyToPrefixMask::NOMASK;
504 static uint64_t getPCRelativeForm(uint32_t encoding) {
505 PPCLegacyInsn origInsn = getPPCLegacyInsn(encoding);
506 PPCPrefixedInsn pcrelInsn = getPCRelativeForm(origInsn);
507 if (pcrelInsn == PPCPrefixedInsn::NOINSN)
508 return UINT64_C(-1);
509 LegacyToPrefixMask origInsnMask = getInsnMask(origInsn);
510 uint64_t pcrelEncoding =
511 (uint64_t)pcrelInsn | (encoding & (uint64_t)origInsnMask);
513 // If the mask requires moving bit 28 to bit 5, do that now.
514 if (origInsnMask == LegacyToPrefixMask::ST_STX28_TO5)
515 pcrelEncoding |= (encoding & 0x8) << 23;
516 return pcrelEncoding;
519 static bool isInstructionUpdateForm(uint32_t encoding) {
520 switch (getPrimaryOpCode(encoding)) {
521 default:
522 return false;
523 case LBZU:
524 case LHAU:
525 case LHZU:
526 case LWZU:
527 case LFSU:
528 case LFDU:
529 case STBU:
530 case STHU:
531 case STWU:
532 case STFSU:
533 case STFDU:
534 return true;
535 // LWA has the same opcode as LD, and the DS bits is what differentiates
536 // between LD/LDU/LWA
537 case LD:
538 case STD:
539 return (encoding & 3) == 1;
543 // Compute the total displacement between the prefixed instruction that gets
544 // to the start of the data and the load/store instruction that has the offset
545 // into the data structure.
546 // For example:
547 // paddi 3, 0, 1000, 1
548 // lwz 3, 20(3)
549 // Should add up to 1020 for total displacement.
550 static int64_t getTotalDisp(uint64_t prefixedInsn, uint32_t accessInsn) {
551 int64_t disp34 = llvm::SignExtend64(
552 ((prefixedInsn & 0x3ffff00000000) >> 16) | (prefixedInsn & 0xffff), 34);
553 int32_t disp16 = llvm::SignExtend32(accessInsn & 0xffff, 16);
554 // For DS and DQ form instructions, we need to mask out the XO bits.
555 if (isDQFormInstruction(accessInsn))
556 disp16 &= ~0xf;
557 else if (isDSFormInstruction(getPPCLegacyInsn(accessInsn)))
558 disp16 &= ~0x3;
559 return disp34 + disp16;
562 // There are a number of places when we either want to read or write an
563 // instruction when handling a half16 relocation type. On big-endian the buffer
564 // pointer is pointing into the middle of the word we want to extract, and on
565 // little-endian it is pointing to the start of the word. These 2 helpers are to
566 // simplify reading and writing in that context.
567 static void writeFromHalf16(uint8_t *loc, uint32_t insn) {
568 write32(config->isLE ? loc : loc - 2, insn);
571 static uint32_t readFromHalf16(const uint8_t *loc) {
572 return read32(config->isLE ? loc : loc - 2);
575 static uint64_t readPrefixedInstruction(const uint8_t *loc) {
576 uint64_t fullInstr = read64(loc);
577 return config->isLE ? (fullInstr << 32 | fullInstr >> 32) : fullInstr;
580 PPC64::PPC64() {
581 copyRel = R_PPC64_COPY;
582 gotRel = R_PPC64_GLOB_DAT;
583 pltRel = R_PPC64_JMP_SLOT;
584 relativeRel = R_PPC64_RELATIVE;
585 iRelativeRel = R_PPC64_IRELATIVE;
586 symbolicRel = R_PPC64_ADDR64;
587 pltHeaderSize = 60;
588 pltEntrySize = 4;
589 ipltEntrySize = 16; // PPC64PltCallStub::size
590 gotHeaderEntriesNum = 1;
591 gotPltHeaderEntriesNum = 2;
592 needsThunks = true;
594 tlsModuleIndexRel = R_PPC64_DTPMOD64;
595 tlsOffsetRel = R_PPC64_DTPREL64;
597 tlsGotRel = R_PPC64_TPREL64;
599 needsMoreStackNonSplit = false;
601 // We need 64K pages (at least under glibc/Linux, the loader won't
602 // set different permissions on a finer granularity than that).
603 defaultMaxPageSize = 65536;
605 // The PPC64 ELF ABI v1 spec, says:
607 // It is normally desirable to put segments with different characteristics
608 // in separate 256 Mbyte portions of the address space, to give the
609 // operating system full paging flexibility in the 64-bit address space.
611 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
612 // use 0x10000000 as the starting address.
613 defaultImageBase = 0x10000000;
615 write32(trapInstr.data(), 0x7fe00008);
618 int PPC64::getTlsGdRelaxSkip(RelType type) const {
619 // A __tls_get_addr call instruction is marked with 2 relocations:
621 // R_PPC64_TLSGD / R_PPC64_TLSLD: marker relocation
622 // R_PPC64_REL24: __tls_get_addr
624 // After the relaxation we no longer call __tls_get_addr and should skip both
625 // relocations to not create a false dependence on __tls_get_addr being
626 // defined.
627 if (type == R_PPC64_TLSGD || type == R_PPC64_TLSLD)
628 return 2;
629 return 1;
632 static uint32_t getEFlags(InputFile *file) {
633 if (file->ekind == ELF64BEKind)
634 return cast<ObjFile<ELF64BE>>(file)->getObj().getHeader().e_flags;
635 return cast<ObjFile<ELF64LE>>(file)->getObj().getHeader().e_flags;
638 // This file implements v2 ABI. This function makes sure that all
639 // object files have v2 or an unspecified version as an ABI version.
640 uint32_t PPC64::calcEFlags() const {
641 for (InputFile *f : ctx.objectFiles) {
642 uint32_t flag = getEFlags(f);
643 if (flag == 1)
644 error(toString(f) + ": ABI version 1 is not supported");
645 else if (flag > 2)
646 error(toString(f) + ": unrecognized e_flags: " + Twine(flag));
648 return 2;
651 void PPC64::relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) const {
652 switch (rel.type) {
653 case R_PPC64_TOC16_HA:
654 // Convert "addis reg, 2, .LC0@toc@h" to "addis reg, 2, var@toc@h" or "nop".
655 relocate(loc, rel, val);
656 break;
657 case R_PPC64_TOC16_LO_DS: {
658 // Convert "ld reg, .LC0@toc@l(reg)" to "addi reg, reg, var@toc@l" or
659 // "addi reg, 2, var@toc".
660 uint32_t insn = readFromHalf16(loc);
661 if (getPrimaryOpCode(insn) != LD)
662 error("expected a 'ld' for got-indirect to toc-relative relaxing");
663 writeFromHalf16(loc, (insn & 0x03ffffff) | 0x38000000);
664 relocateNoSym(loc, R_PPC64_TOC16_LO, val);
665 break;
667 case R_PPC64_GOT_PCREL34: {
668 // Clear the first 8 bits of the prefix and the first 6 bits of the
669 // instruction (the primary opcode).
670 uint64_t insn = readPrefixedInstruction(loc);
671 if ((insn & 0xfc000000) != 0xe4000000)
672 error("expected a 'pld' for got-indirect to pc-relative relaxing");
673 insn &= ~0xff000000fc000000;
675 // Replace the cleared bits with the values for PADDI (0x600000038000000);
676 insn |= 0x600000038000000;
677 writePrefixedInstruction(loc, insn);
678 relocate(loc, rel, val);
679 break;
681 case R_PPC64_PCREL_OPT: {
682 // We can only relax this if the R_PPC64_GOT_PCREL34 at this offset can
683 // be relaxed. The eligibility for the relaxation needs to be determined
684 // on that relocation since this one does not relocate a symbol.
685 uint64_t insn = readPrefixedInstruction(loc);
686 uint32_t accessInsn = read32(loc + rel.addend);
687 uint64_t pcRelInsn = getPCRelativeForm(accessInsn);
689 // This error is not necessary for correctness but is emitted for now
690 // to ensure we don't miss these opportunities in real code. It can be
691 // removed at a later date.
692 if (pcRelInsn == UINT64_C(-1)) {
693 errorOrWarn(
694 "unrecognized instruction for R_PPC64_PCREL_OPT relaxation: 0x" +
695 Twine::utohexstr(accessInsn));
696 break;
699 int64_t totalDisp = getTotalDisp(insn, accessInsn);
700 if (!isInt<34>(totalDisp))
701 break; // Displacement doesn't fit.
702 // Convert the PADDI to the prefixed version of accessInsn and convert
703 // accessInsn to a nop.
704 writePrefixedInstruction(loc, pcRelInsn |
705 ((totalDisp & 0x3ffff0000) << 16) |
706 (totalDisp & 0xffff));
707 write32(loc + rel.addend, NOP); // nop accessInsn.
708 break;
710 default:
711 llvm_unreachable("unexpected relocation type");
715 void PPC64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
716 uint64_t val) const {
717 // Reference: 3.7.4.2 of the 64-bit ELF V2 abi supplement.
718 // The general dynamic code sequence for a global `x` will look like:
719 // Instruction Relocation Symbol
720 // addis r3, r2, x@got@tlsgd@ha R_PPC64_GOT_TLSGD16_HA x
721 // addi r3, r3, x@got@tlsgd@l R_PPC64_GOT_TLSGD16_LO x
722 // bl __tls_get_addr(x@tlsgd) R_PPC64_TLSGD x
723 // R_PPC64_REL24 __tls_get_addr
724 // nop None None
726 // Relaxing to local exec entails converting:
727 // addis r3, r2, x@got@tlsgd@ha into nop
728 // addi r3, r3, x@got@tlsgd@l into addis r3, r13, x@tprel@ha
729 // bl __tls_get_addr(x@tlsgd) into nop
730 // nop into addi r3, r3, x@tprel@l
732 switch (rel.type) {
733 case R_PPC64_GOT_TLSGD16_HA:
734 writeFromHalf16(loc, NOP);
735 break;
736 case R_PPC64_GOT_TLSGD16:
737 case R_PPC64_GOT_TLSGD16_LO:
738 writeFromHalf16(loc, 0x3c6d0000); // addis r3, r13
739 relocateNoSym(loc, R_PPC64_TPREL16_HA, val);
740 break;
741 case R_PPC64_GOT_TLSGD_PCREL34:
742 // Relax from paddi r3, 0, x@got@tlsgd@pcrel, 1 to
743 // paddi r3, r13, x@tprel, 0
744 writePrefixedInstruction(loc, 0x06000000386d0000);
745 relocateNoSym(loc, R_PPC64_TPREL34, val);
746 break;
747 case R_PPC64_TLSGD: {
748 // PC Relative Relaxation:
749 // Relax from bl __tls_get_addr@notoc(x@tlsgd) to
750 // nop
751 // TOC Relaxation:
752 // Relax from bl __tls_get_addr(x@tlsgd)
753 // nop
754 // to
755 // nop
756 // addi r3, r3, x@tprel@l
757 const uintptr_t locAsInt = reinterpret_cast<uintptr_t>(loc);
758 if (locAsInt % 4 == 0) {
759 write32(loc, NOP); // nop
760 write32(loc + 4, 0x38630000); // addi r3, r3
761 // Since we are relocating a half16 type relocation and Loc + 4 points to
762 // the start of an instruction we need to advance the buffer by an extra
763 // 2 bytes on BE.
764 relocateNoSym(loc + 4 + (config->ekind == ELF64BEKind ? 2 : 0),
765 R_PPC64_TPREL16_LO, val);
766 } else if (locAsInt % 4 == 1) {
767 write32(loc - 1, NOP);
768 } else {
769 errorOrWarn("R_PPC64_TLSGD has unexpected byte alignment");
771 break;
773 default:
774 llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
778 void PPC64::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
779 uint64_t val) const {
780 // Reference: 3.7.4.3 of the 64-bit ELF V2 abi supplement.
781 // The local dynamic code sequence for a global `x` will look like:
782 // Instruction Relocation Symbol
783 // addis r3, r2, x@got@tlsld@ha R_PPC64_GOT_TLSLD16_HA x
784 // addi r3, r3, x@got@tlsld@l R_PPC64_GOT_TLSLD16_LO x
785 // bl __tls_get_addr(x@tlsgd) R_PPC64_TLSLD x
786 // R_PPC64_REL24 __tls_get_addr
787 // nop None None
789 // Relaxing to local exec entails converting:
790 // addis r3, r2, x@got@tlsld@ha into nop
791 // addi r3, r3, x@got@tlsld@l into addis r3, r13, 0
792 // bl __tls_get_addr(x@tlsgd) into nop
793 // nop into addi r3, r3, 4096
795 switch (rel.type) {
796 case R_PPC64_GOT_TLSLD16_HA:
797 writeFromHalf16(loc, NOP);
798 break;
799 case R_PPC64_GOT_TLSLD16_LO:
800 writeFromHalf16(loc, 0x3c6d0000); // addis r3, r13, 0
801 break;
802 case R_PPC64_GOT_TLSLD_PCREL34:
803 // Relax from paddi r3, 0, x1@got@tlsld@pcrel, 1 to
804 // paddi r3, r13, 0x1000, 0
805 writePrefixedInstruction(loc, 0x06000000386d1000);
806 break;
807 case R_PPC64_TLSLD: {
808 // PC Relative Relaxation:
809 // Relax from bl __tls_get_addr@notoc(x@tlsld)
810 // to
811 // nop
812 // TOC Relaxation:
813 // Relax from bl __tls_get_addr(x@tlsld)
814 // nop
815 // to
816 // nop
817 // addi r3, r3, 4096
818 const uintptr_t locAsInt = reinterpret_cast<uintptr_t>(loc);
819 if (locAsInt % 4 == 0) {
820 write32(loc, NOP);
821 write32(loc + 4, 0x38631000); // addi r3, r3, 4096
822 } else if (locAsInt % 4 == 1) {
823 write32(loc - 1, NOP);
824 } else {
825 errorOrWarn("R_PPC64_TLSLD has unexpected byte alignment");
827 break;
829 case R_PPC64_DTPREL16:
830 case R_PPC64_DTPREL16_HA:
831 case R_PPC64_DTPREL16_HI:
832 case R_PPC64_DTPREL16_DS:
833 case R_PPC64_DTPREL16_LO:
834 case R_PPC64_DTPREL16_LO_DS:
835 case R_PPC64_DTPREL34:
836 relocate(loc, rel, val);
837 break;
838 default:
839 llvm_unreachable("unsupported relocation for TLS LD to LE relaxation");
843 // Map X-Form instructions to their DS-Form counterparts, if applicable.
844 // The full encoding is returned here to distinguish between the different
845 // DS-Form instructions.
846 unsigned elf::getPPCDSFormOp(unsigned secondaryOp) {
847 switch (secondaryOp) {
848 case LWAX:
849 return (LWA << 26) | 0x2;
850 case LDX:
851 return LD << 26;
852 case STDX:
853 return STD << 26;
854 default:
855 return 0;
859 unsigned elf::getPPCDFormOp(unsigned secondaryOp) {
860 switch (secondaryOp) {
861 case LBZX:
862 return LBZ << 26;
863 case LHZX:
864 return LHZ << 26;
865 case LWZX:
866 return LWZ << 26;
867 case STBX:
868 return STB << 26;
869 case STHX:
870 return STH << 26;
871 case STWX:
872 return STW << 26;
873 case LHAX:
874 return LHA << 26;
875 case LFSX:
876 return LFS << 26;
877 case LFDX:
878 return LFD << 26;
879 case STFSX:
880 return STFS << 26;
881 case STFDX:
882 return STFD << 26;
883 case ADD:
884 return ADDI << 26;
885 default:
886 return 0;
890 void PPC64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
891 uint64_t val) const {
892 // The initial exec code sequence for a global `x` will look like:
893 // Instruction Relocation Symbol
894 // addis r9, r2, x@got@tprel@ha R_PPC64_GOT_TPREL16_HA x
895 // ld r9, x@got@tprel@l(r9) R_PPC64_GOT_TPREL16_LO_DS x
896 // add r9, r9, x@tls R_PPC64_TLS x
898 // Relaxing to local exec entails converting:
899 // addis r9, r2, x@got@tprel@ha into nop
900 // ld r9, x@got@tprel@l(r9) into addis r9, r13, x@tprel@ha
901 // add r9, r9, x@tls into addi r9, r9, x@tprel@l
903 // x@tls R_PPC64_TLS is a relocation which does not compute anything,
904 // it is replaced with r13 (thread pointer).
906 // The add instruction in the initial exec sequence has multiple variations
907 // that need to be handled. If we are building an address it will use an add
908 // instruction, if we are accessing memory it will use any of the X-form
909 // indexed load or store instructions.
911 unsigned offset = (config->ekind == ELF64BEKind) ? 2 : 0;
912 switch (rel.type) {
913 case R_PPC64_GOT_TPREL16_HA:
914 write32(loc - offset, NOP);
915 break;
916 case R_PPC64_GOT_TPREL16_LO_DS:
917 case R_PPC64_GOT_TPREL16_DS: {
918 uint32_t regNo = read32(loc - offset) & 0x03E00000; // bits 6-10
919 write32(loc - offset, 0x3C0D0000 | regNo); // addis RegNo, r13
920 relocateNoSym(loc, R_PPC64_TPREL16_HA, val);
921 break;
923 case R_PPC64_GOT_TPREL_PCREL34: {
924 const uint64_t pldRT = readPrefixedInstruction(loc) & 0x0000000003e00000;
925 // paddi RT(from pld), r13, symbol@tprel, 0
926 writePrefixedInstruction(loc, 0x06000000380d0000 | pldRT);
927 relocateNoSym(loc, R_PPC64_TPREL34, val);
928 break;
930 case R_PPC64_TLS: {
931 const uintptr_t locAsInt = reinterpret_cast<uintptr_t>(loc);
932 if (locAsInt % 4 == 0) {
933 uint32_t primaryOp = getPrimaryOpCode(read32(loc));
934 if (primaryOp != 31)
935 error("unrecognized instruction for IE to LE R_PPC64_TLS");
936 uint32_t secondaryOp = (read32(loc) & 0x000007FE) >> 1; // bits 21-30
937 uint32_t dFormOp = getPPCDFormOp(secondaryOp);
938 uint32_t finalReloc;
939 if (dFormOp == 0) { // Expecting a DS-Form instruction.
940 dFormOp = getPPCDSFormOp(secondaryOp);
941 if (dFormOp == 0)
942 error("unrecognized instruction for IE to LE R_PPC64_TLS");
943 finalReloc = R_PPC64_TPREL16_LO_DS;
944 } else
945 finalReloc = R_PPC64_TPREL16_LO;
946 write32(loc, dFormOp | (read32(loc) & 0x03ff0000));
947 relocateNoSym(loc + offset, finalReloc, val);
948 } else if (locAsInt % 4 == 1) {
949 // If the offset is not 4 byte aligned then we have a PCRel type reloc.
950 // This version of the relocation is offset by one byte from the
951 // instruction it references.
952 uint32_t tlsInstr = read32(loc - 1);
953 uint32_t primaryOp = getPrimaryOpCode(tlsInstr);
954 if (primaryOp != 31)
955 errorOrWarn("unrecognized instruction for IE to LE R_PPC64_TLS");
956 uint32_t secondaryOp = (tlsInstr & 0x000007FE) >> 1; // bits 21-30
957 // The add is a special case and should be turned into a nop. The paddi
958 // that comes before it will already have computed the address of the
959 // symbol.
960 if (secondaryOp == 266) {
961 // Check if the add uses the same result register as the input register.
962 uint32_t rt = (tlsInstr & 0x03E00000) >> 21; // bits 6-10
963 uint32_t ra = (tlsInstr & 0x001F0000) >> 16; // bits 11-15
964 if (ra == rt) {
965 write32(loc - 1, NOP);
966 } else {
967 // mr rt, ra
968 write32(loc - 1, 0x7C000378 | (rt << 16) | (ra << 21) | (ra << 11));
970 } else {
971 uint32_t dFormOp = getPPCDFormOp(secondaryOp);
972 if (dFormOp == 0) { // Expecting a DS-Form instruction.
973 dFormOp = getPPCDSFormOp(secondaryOp);
974 if (dFormOp == 0)
975 errorOrWarn("unrecognized instruction for IE to LE R_PPC64_TLS");
977 write32(loc - 1, (dFormOp | (tlsInstr & 0x03ff0000)));
979 } else {
980 errorOrWarn("R_PPC64_TLS must be either 4 byte aligned or one byte "
981 "offset from 4 byte aligned");
983 break;
985 default:
986 llvm_unreachable("unknown relocation for IE to LE");
987 break;
991 RelExpr PPC64::getRelExpr(RelType type, const Symbol &s,
992 const uint8_t *loc) const {
993 switch (type) {
994 case R_PPC64_NONE:
995 return R_NONE;
996 case R_PPC64_ADDR16:
997 case R_PPC64_ADDR16_DS:
998 case R_PPC64_ADDR16_HA:
999 case R_PPC64_ADDR16_HI:
1000 case R_PPC64_ADDR16_HIGH:
1001 case R_PPC64_ADDR16_HIGHER:
1002 case R_PPC64_ADDR16_HIGHERA:
1003 case R_PPC64_ADDR16_HIGHEST:
1004 case R_PPC64_ADDR16_HIGHESTA:
1005 case R_PPC64_ADDR16_LO:
1006 case R_PPC64_ADDR16_LO_DS:
1007 case R_PPC64_ADDR32:
1008 case R_PPC64_ADDR64:
1009 return R_ABS;
1010 case R_PPC64_GOT16:
1011 case R_PPC64_GOT16_DS:
1012 case R_PPC64_GOT16_HA:
1013 case R_PPC64_GOT16_HI:
1014 case R_PPC64_GOT16_LO:
1015 case R_PPC64_GOT16_LO_DS:
1016 return R_GOT_OFF;
1017 case R_PPC64_TOC16:
1018 case R_PPC64_TOC16_DS:
1019 case R_PPC64_TOC16_HI:
1020 case R_PPC64_TOC16_LO:
1021 return R_GOTREL;
1022 case R_PPC64_GOT_PCREL34:
1023 case R_PPC64_GOT_TPREL_PCREL34:
1024 case R_PPC64_PCREL_OPT:
1025 return R_GOT_PC;
1026 case R_PPC64_TOC16_HA:
1027 case R_PPC64_TOC16_LO_DS:
1028 return config->tocOptimize ? R_PPC64_RELAX_TOC : R_GOTREL;
1029 case R_PPC64_TOC:
1030 return R_PPC64_TOCBASE;
1031 case R_PPC64_REL14:
1032 case R_PPC64_REL24:
1033 return R_PPC64_CALL_PLT;
1034 case R_PPC64_REL24_NOTOC:
1035 return R_PLT_PC;
1036 case R_PPC64_REL16_LO:
1037 case R_PPC64_REL16_HA:
1038 case R_PPC64_REL16_HI:
1039 case R_PPC64_REL32:
1040 case R_PPC64_REL64:
1041 case R_PPC64_PCREL34:
1042 return R_PC;
1043 case R_PPC64_GOT_TLSGD16:
1044 case R_PPC64_GOT_TLSGD16_HA:
1045 case R_PPC64_GOT_TLSGD16_HI:
1046 case R_PPC64_GOT_TLSGD16_LO:
1047 return R_TLSGD_GOT;
1048 case R_PPC64_GOT_TLSGD_PCREL34:
1049 return R_TLSGD_PC;
1050 case R_PPC64_GOT_TLSLD16:
1051 case R_PPC64_GOT_TLSLD16_HA:
1052 case R_PPC64_GOT_TLSLD16_HI:
1053 case R_PPC64_GOT_TLSLD16_LO:
1054 return R_TLSLD_GOT;
1055 case R_PPC64_GOT_TLSLD_PCREL34:
1056 return R_TLSLD_PC;
1057 case R_PPC64_GOT_TPREL16_HA:
1058 case R_PPC64_GOT_TPREL16_LO_DS:
1059 case R_PPC64_GOT_TPREL16_DS:
1060 case R_PPC64_GOT_TPREL16_HI:
1061 return R_GOT_OFF;
1062 case R_PPC64_GOT_DTPREL16_HA:
1063 case R_PPC64_GOT_DTPREL16_LO_DS:
1064 case R_PPC64_GOT_DTPREL16_DS:
1065 case R_PPC64_GOT_DTPREL16_HI:
1066 return R_TLSLD_GOT_OFF;
1067 case R_PPC64_TPREL16:
1068 case R_PPC64_TPREL16_HA:
1069 case R_PPC64_TPREL16_LO:
1070 case R_PPC64_TPREL16_HI:
1071 case R_PPC64_TPREL16_DS:
1072 case R_PPC64_TPREL16_LO_DS:
1073 case R_PPC64_TPREL16_HIGHER:
1074 case R_PPC64_TPREL16_HIGHERA:
1075 case R_PPC64_TPREL16_HIGHEST:
1076 case R_PPC64_TPREL16_HIGHESTA:
1077 case R_PPC64_TPREL34:
1078 return R_TPREL;
1079 case R_PPC64_DTPREL16:
1080 case R_PPC64_DTPREL16_DS:
1081 case R_PPC64_DTPREL16_HA:
1082 case R_PPC64_DTPREL16_HI:
1083 case R_PPC64_DTPREL16_HIGHER:
1084 case R_PPC64_DTPREL16_HIGHERA:
1085 case R_PPC64_DTPREL16_HIGHEST:
1086 case R_PPC64_DTPREL16_HIGHESTA:
1087 case R_PPC64_DTPREL16_LO:
1088 case R_PPC64_DTPREL16_LO_DS:
1089 case R_PPC64_DTPREL64:
1090 case R_PPC64_DTPREL34:
1091 return R_DTPREL;
1092 case R_PPC64_TLSGD:
1093 return R_TLSDESC_CALL;
1094 case R_PPC64_TLSLD:
1095 return R_TLSLD_HINT;
1096 case R_PPC64_TLS:
1097 return R_TLSIE_HINT;
1098 default:
1099 error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
1100 ") against symbol " + toString(s));
1101 return R_NONE;
1105 RelType PPC64::getDynRel(RelType type) const {
1106 if (type == R_PPC64_ADDR64 || type == R_PPC64_TOC)
1107 return R_PPC64_ADDR64;
1108 return R_PPC64_NONE;
1111 int64_t PPC64::getImplicitAddend(const uint8_t *buf, RelType type) const {
1112 switch (type) {
1113 case R_PPC64_NONE:
1114 case R_PPC64_GLOB_DAT:
1115 case R_PPC64_JMP_SLOT:
1116 return 0;
1117 case R_PPC64_REL32:
1118 return SignExtend64<32>(read32(buf));
1119 case R_PPC64_ADDR64:
1120 case R_PPC64_REL64:
1121 case R_PPC64_RELATIVE:
1122 case R_PPC64_IRELATIVE:
1123 case R_PPC64_DTPMOD64:
1124 case R_PPC64_DTPREL64:
1125 case R_PPC64_TPREL64:
1126 return read64(buf);
1127 default:
1128 internalLinkerError(getErrorLocation(buf),
1129 "cannot read addend for relocation " + toString(type));
1130 return 0;
1134 void PPC64::writeGotHeader(uint8_t *buf) const {
1135 write64(buf, getPPC64TocBase());
1138 void PPC64::writePltHeader(uint8_t *buf) const {
1139 // The generic resolver stub goes first.
1140 write32(buf + 0, 0x7c0802a6); // mflr r0
1141 write32(buf + 4, 0x429f0005); // bcl 20,4*cr7+so,8 <_glink+0x8>
1142 write32(buf + 8, 0x7d6802a6); // mflr r11
1143 write32(buf + 12, 0x7c0803a6); // mtlr r0
1144 write32(buf + 16, 0x7d8b6050); // subf r12, r11, r12
1145 write32(buf + 20, 0x380cffcc); // subi r0,r12,52
1146 write32(buf + 24, 0x7800f082); // srdi r0,r0,62,2
1147 write32(buf + 28, 0xe98b002c); // ld r12,44(r11)
1148 write32(buf + 32, 0x7d6c5a14); // add r11,r12,r11
1149 write32(buf + 36, 0xe98b0000); // ld r12,0(r11)
1150 write32(buf + 40, 0xe96b0008); // ld r11,8(r11)
1151 write32(buf + 44, 0x7d8903a6); // mtctr r12
1152 write32(buf + 48, 0x4e800420); // bctr
1154 // The 'bcl' instruction will set the link register to the address of the
1155 // following instruction ('mflr r11'). Here we store the offset from that
1156 // instruction to the first entry in the GotPlt section.
1157 int64_t gotPltOffset = in.gotPlt->getVA() - (in.plt->getVA() + 8);
1158 write64(buf + 52, gotPltOffset);
1161 void PPC64::writePlt(uint8_t *buf, const Symbol &sym,
1162 uint64_t /*pltEntryAddr*/) const {
1163 int32_t offset = pltHeaderSize + sym.getPltIdx() * pltEntrySize;
1164 // bl __glink_PLTresolve
1165 write32(buf, 0x48000000 | ((-offset) & 0x03FFFFFc));
1168 void PPC64::writeIplt(uint8_t *buf, const Symbol &sym,
1169 uint64_t /*pltEntryAddr*/) const {
1170 writePPC64LoadAndBranch(buf, sym.getGotPltVA() - getPPC64TocBase());
1173 static std::pair<RelType, uint64_t> toAddr16Rel(RelType type, uint64_t val) {
1174 // Relocations relative to the toc-base need to be adjusted by the Toc offset.
1175 uint64_t tocBiasedVal = val - ppc64TocOffset;
1176 // Relocations relative to dtv[dtpmod] need to be adjusted by the DTP offset.
1177 uint64_t dtpBiasedVal = val - dynamicThreadPointerOffset;
1179 switch (type) {
1180 // TOC biased relocation.
1181 case R_PPC64_GOT16:
1182 case R_PPC64_GOT_TLSGD16:
1183 case R_PPC64_GOT_TLSLD16:
1184 case R_PPC64_TOC16:
1185 return {R_PPC64_ADDR16, tocBiasedVal};
1186 case R_PPC64_GOT16_DS:
1187 case R_PPC64_TOC16_DS:
1188 case R_PPC64_GOT_TPREL16_DS:
1189 case R_PPC64_GOT_DTPREL16_DS:
1190 return {R_PPC64_ADDR16_DS, tocBiasedVal};
1191 case R_PPC64_GOT16_HA:
1192 case R_PPC64_GOT_TLSGD16_HA:
1193 case R_PPC64_GOT_TLSLD16_HA:
1194 case R_PPC64_GOT_TPREL16_HA:
1195 case R_PPC64_GOT_DTPREL16_HA:
1196 case R_PPC64_TOC16_HA:
1197 return {R_PPC64_ADDR16_HA, tocBiasedVal};
1198 case R_PPC64_GOT16_HI:
1199 case R_PPC64_GOT_TLSGD16_HI:
1200 case R_PPC64_GOT_TLSLD16_HI:
1201 case R_PPC64_GOT_TPREL16_HI:
1202 case R_PPC64_GOT_DTPREL16_HI:
1203 case R_PPC64_TOC16_HI:
1204 return {R_PPC64_ADDR16_HI, tocBiasedVal};
1205 case R_PPC64_GOT16_LO:
1206 case R_PPC64_GOT_TLSGD16_LO:
1207 case R_PPC64_GOT_TLSLD16_LO:
1208 case R_PPC64_TOC16_LO:
1209 return {R_PPC64_ADDR16_LO, tocBiasedVal};
1210 case R_PPC64_GOT16_LO_DS:
1211 case R_PPC64_TOC16_LO_DS:
1212 case R_PPC64_GOT_TPREL16_LO_DS:
1213 case R_PPC64_GOT_DTPREL16_LO_DS:
1214 return {R_PPC64_ADDR16_LO_DS, tocBiasedVal};
1216 // Dynamic Thread pointer biased relocation types.
1217 case R_PPC64_DTPREL16:
1218 return {R_PPC64_ADDR16, dtpBiasedVal};
1219 case R_PPC64_DTPREL16_DS:
1220 return {R_PPC64_ADDR16_DS, dtpBiasedVal};
1221 case R_PPC64_DTPREL16_HA:
1222 return {R_PPC64_ADDR16_HA, dtpBiasedVal};
1223 case R_PPC64_DTPREL16_HI:
1224 return {R_PPC64_ADDR16_HI, dtpBiasedVal};
1225 case R_PPC64_DTPREL16_HIGHER:
1226 return {R_PPC64_ADDR16_HIGHER, dtpBiasedVal};
1227 case R_PPC64_DTPREL16_HIGHERA:
1228 return {R_PPC64_ADDR16_HIGHERA, dtpBiasedVal};
1229 case R_PPC64_DTPREL16_HIGHEST:
1230 return {R_PPC64_ADDR16_HIGHEST, dtpBiasedVal};
1231 case R_PPC64_DTPREL16_HIGHESTA:
1232 return {R_PPC64_ADDR16_HIGHESTA, dtpBiasedVal};
1233 case R_PPC64_DTPREL16_LO:
1234 return {R_PPC64_ADDR16_LO, dtpBiasedVal};
1235 case R_PPC64_DTPREL16_LO_DS:
1236 return {R_PPC64_ADDR16_LO_DS, dtpBiasedVal};
1237 case R_PPC64_DTPREL64:
1238 return {R_PPC64_ADDR64, dtpBiasedVal};
1240 default:
1241 return {type, val};
1245 static bool isTocOptType(RelType type) {
1246 switch (type) {
1247 case R_PPC64_GOT16_HA:
1248 case R_PPC64_GOT16_LO_DS:
1249 case R_PPC64_TOC16_HA:
1250 case R_PPC64_TOC16_LO_DS:
1251 case R_PPC64_TOC16_LO:
1252 return true;
1253 default:
1254 return false;
1258 void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
1259 RelType type = rel.type;
1260 bool shouldTocOptimize = isTocOptType(type);
1261 // For dynamic thread pointer relative, toc-relative, and got-indirect
1262 // relocations, proceed in terms of the corresponding ADDR16 relocation type.
1263 std::tie(type, val) = toAddr16Rel(type, val);
1265 switch (type) {
1266 case R_PPC64_ADDR14: {
1267 checkAlignment(loc, val, 4, rel);
1268 // Preserve the AA/LK bits in the branch instruction
1269 uint8_t aalk = loc[3];
1270 write16(loc + 2, (aalk & 3) | (val & 0xfffc));
1271 break;
1273 case R_PPC64_ADDR16:
1274 checkIntUInt(loc, val, 16, rel);
1275 write16(loc, val);
1276 break;
1277 case R_PPC64_ADDR32:
1278 checkIntUInt(loc, val, 32, rel);
1279 write32(loc, val);
1280 break;
1281 case R_PPC64_ADDR16_DS:
1282 case R_PPC64_TPREL16_DS: {
1283 checkInt(loc, val, 16, rel);
1284 // DQ-form instructions use bits 28-31 as part of the instruction encoding
1285 // DS-form instructions only use bits 30-31.
1286 uint16_t mask = isDQFormInstruction(readFromHalf16(loc)) ? 0xf : 0x3;
1287 checkAlignment(loc, lo(val), mask + 1, rel);
1288 write16(loc, (read16(loc) & mask) | lo(val));
1289 } break;
1290 case R_PPC64_ADDR16_HA:
1291 case R_PPC64_REL16_HA:
1292 case R_PPC64_TPREL16_HA:
1293 if (config->tocOptimize && shouldTocOptimize && ha(val) == 0)
1294 writeFromHalf16(loc, NOP);
1295 else {
1296 checkInt(loc, val + 0x8000, 32, rel);
1297 write16(loc, ha(val));
1299 break;
1300 case R_PPC64_ADDR16_HI:
1301 case R_PPC64_REL16_HI:
1302 case R_PPC64_TPREL16_HI:
1303 checkInt(loc, val, 32, rel);
1304 write16(loc, hi(val));
1305 break;
1306 case R_PPC64_ADDR16_HIGH:
1307 write16(loc, hi(val));
1308 break;
1309 case R_PPC64_ADDR16_HIGHER:
1310 case R_PPC64_TPREL16_HIGHER:
1311 write16(loc, higher(val));
1312 break;
1313 case R_PPC64_ADDR16_HIGHERA:
1314 case R_PPC64_TPREL16_HIGHERA:
1315 write16(loc, highera(val));
1316 break;
1317 case R_PPC64_ADDR16_HIGHEST:
1318 case R_PPC64_TPREL16_HIGHEST:
1319 write16(loc, highest(val));
1320 break;
1321 case R_PPC64_ADDR16_HIGHESTA:
1322 case R_PPC64_TPREL16_HIGHESTA:
1323 write16(loc, highesta(val));
1324 break;
1325 case R_PPC64_ADDR16_LO:
1326 case R_PPC64_REL16_LO:
1327 case R_PPC64_TPREL16_LO:
1328 // When the high-adjusted part of a toc relocation evaluates to 0, it is
1329 // changed into a nop. The lo part then needs to be updated to use the
1330 // toc-pointer register r2, as the base register.
1331 if (config->tocOptimize && shouldTocOptimize && ha(val) == 0) {
1332 uint32_t insn = readFromHalf16(loc);
1333 if (isInstructionUpdateForm(insn))
1334 error(getErrorLocation(loc) +
1335 "can't toc-optimize an update instruction: 0x" +
1336 utohexstr(insn));
1337 writeFromHalf16(loc, (insn & 0xffe00000) | 0x00020000 | lo(val));
1338 } else {
1339 write16(loc, lo(val));
1341 break;
1342 case R_PPC64_ADDR16_LO_DS:
1343 case R_PPC64_TPREL16_LO_DS: {
1344 // DQ-form instructions use bits 28-31 as part of the instruction encoding
1345 // DS-form instructions only use bits 30-31.
1346 uint32_t insn = readFromHalf16(loc);
1347 uint16_t mask = isDQFormInstruction(insn) ? 0xf : 0x3;
1348 checkAlignment(loc, lo(val), mask + 1, rel);
1349 if (config->tocOptimize && shouldTocOptimize && ha(val) == 0) {
1350 // When the high-adjusted part of a toc relocation evaluates to 0, it is
1351 // changed into a nop. The lo part then needs to be updated to use the toc
1352 // pointer register r2, as the base register.
1353 if (isInstructionUpdateForm(insn))
1354 error(getErrorLocation(loc) +
1355 "Can't toc-optimize an update instruction: 0x" +
1356 Twine::utohexstr(insn));
1357 insn &= 0xffe00000 | mask;
1358 writeFromHalf16(loc, insn | 0x00020000 | lo(val));
1359 } else {
1360 write16(loc, (read16(loc) & mask) | lo(val));
1362 } break;
1363 case R_PPC64_TPREL16:
1364 checkInt(loc, val, 16, rel);
1365 write16(loc, val);
1366 break;
1367 case R_PPC64_REL32:
1368 checkInt(loc, val, 32, rel);
1369 write32(loc, val);
1370 break;
1371 case R_PPC64_ADDR64:
1372 case R_PPC64_REL64:
1373 case R_PPC64_TOC:
1374 write64(loc, val);
1375 break;
1376 case R_PPC64_REL14: {
1377 uint32_t mask = 0x0000FFFC;
1378 checkInt(loc, val, 16, rel);
1379 checkAlignment(loc, val, 4, rel);
1380 write32(loc, (read32(loc) & ~mask) | (val & mask));
1381 break;
1383 case R_PPC64_REL24:
1384 case R_PPC64_REL24_NOTOC: {
1385 uint32_t mask = 0x03FFFFFC;
1386 checkInt(loc, val, 26, rel);
1387 checkAlignment(loc, val, 4, rel);
1388 write32(loc, (read32(loc) & ~mask) | (val & mask));
1389 break;
1391 case R_PPC64_DTPREL64:
1392 write64(loc, val - dynamicThreadPointerOffset);
1393 break;
1394 case R_PPC64_DTPREL34:
1395 // The Dynamic Thread Vector actually points 0x8000 bytes past the start
1396 // of the TLS block. Therefore, in the case of R_PPC64_DTPREL34 we first
1397 // need to subtract that value then fallthrough to the general case.
1398 val -= dynamicThreadPointerOffset;
1399 [[fallthrough]];
1400 case R_PPC64_PCREL34:
1401 case R_PPC64_GOT_PCREL34:
1402 case R_PPC64_GOT_TLSGD_PCREL34:
1403 case R_PPC64_GOT_TLSLD_PCREL34:
1404 case R_PPC64_GOT_TPREL_PCREL34:
1405 case R_PPC64_TPREL34: {
1406 const uint64_t si0Mask = 0x00000003ffff0000;
1407 const uint64_t si1Mask = 0x000000000000ffff;
1408 const uint64_t fullMask = 0x0003ffff0000ffff;
1409 checkInt(loc, val, 34, rel);
1411 uint64_t instr = readPrefixedInstruction(loc) & ~fullMask;
1412 writePrefixedInstruction(loc, instr | ((val & si0Mask) << 16) |
1413 (val & si1Mask));
1414 break;
1416 // If we encounter a PCREL_OPT relocation that we won't optimize.
1417 case R_PPC64_PCREL_OPT:
1418 break;
1419 default:
1420 llvm_unreachable("unknown relocation");
1424 bool PPC64::needsThunk(RelExpr expr, RelType type, const InputFile *file,
1425 uint64_t branchAddr, const Symbol &s, int64_t a) const {
1426 if (type != R_PPC64_REL14 && type != R_PPC64_REL24 &&
1427 type != R_PPC64_REL24_NOTOC)
1428 return false;
1430 // If a function is in the Plt it needs to be called with a call-stub.
1431 if (s.isInPlt())
1432 return true;
1434 // This check looks at the st_other bits of the callee with relocation
1435 // R_PPC64_REL14 or R_PPC64_REL24. If the value is 1, then the callee
1436 // clobbers the TOC and we need an R2 save stub.
1437 if (type != R_PPC64_REL24_NOTOC && (s.stOther >> 5) == 1)
1438 return true;
1440 if (type == R_PPC64_REL24_NOTOC && (s.stOther >> 5) > 1)
1441 return true;
1443 // An undefined weak symbol not in a PLT does not need a thunk. If it is
1444 // hidden, its binding has been converted to local, so we just check
1445 // isUndefined() here. A undefined non-weak symbol has been errored.
1446 if (s.isUndefined())
1447 return false;
1449 // If the offset exceeds the range of the branch type then it will need
1450 // a range-extending thunk.
1451 // See the comment in getRelocTargetVA() about R_PPC64_CALL.
1452 return !inBranchRange(type, branchAddr,
1453 s.getVA(a) +
1454 getPPC64GlobalEntryToLocalEntryOffset(s.stOther));
1457 uint32_t PPC64::getThunkSectionSpacing() const {
1458 // See comment in Arch/ARM.cpp for a more detailed explanation of
1459 // getThunkSectionSpacing(). For PPC64 we pick the constant here based on
1460 // R_PPC64_REL24, which is used by unconditional branch instructions.
1461 // 0x2000000 = (1 << 24-1) * 4
1462 return 0x2000000;
1465 bool PPC64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
1466 int64_t offset = dst - src;
1467 if (type == R_PPC64_REL14)
1468 return isInt<16>(offset);
1469 if (type == R_PPC64_REL24 || type == R_PPC64_REL24_NOTOC)
1470 return isInt<26>(offset);
1471 llvm_unreachable("unsupported relocation type used in branch");
1474 RelExpr PPC64::adjustTlsExpr(RelType type, RelExpr expr) const {
1475 if (type != R_PPC64_GOT_TLSGD_PCREL34 && expr == R_RELAX_TLS_GD_TO_IE)
1476 return R_RELAX_TLS_GD_TO_IE_GOT_OFF;
1477 if (expr == R_RELAX_TLS_LD_TO_LE)
1478 return R_RELAX_TLS_LD_TO_LE_ABS;
1479 return expr;
1482 RelExpr PPC64::adjustGotPcExpr(RelType type, int64_t addend,
1483 const uint8_t *loc) const {
1484 if ((type == R_PPC64_GOT_PCREL34 || type == R_PPC64_PCREL_OPT) &&
1485 config->pcRelOptimize) {
1486 // It only makes sense to optimize pld since paddi means that the address
1487 // of the object in the GOT is required rather than the object itself.
1488 if ((readPrefixedInstruction(loc) & 0xfc000000) == 0xe4000000)
1489 return R_PPC64_RELAX_GOT_PC;
1491 return R_GOT_PC;
1494 // Reference: 3.7.4.1 of the 64-bit ELF V2 abi supplement.
1495 // The general dynamic code sequence for a global `x` uses 4 instructions.
1496 // Instruction Relocation Symbol
1497 // addis r3, r2, x@got@tlsgd@ha R_PPC64_GOT_TLSGD16_HA x
1498 // addi r3, r3, x@got@tlsgd@l R_PPC64_GOT_TLSGD16_LO x
1499 // bl __tls_get_addr(x@tlsgd) R_PPC64_TLSGD x
1500 // R_PPC64_REL24 __tls_get_addr
1501 // nop None None
1503 // Relaxing to initial-exec entails:
1504 // 1) Convert the addis/addi pair that builds the address of the tls_index
1505 // struct for 'x' to an addis/ld pair that loads an offset from a got-entry.
1506 // 2) Convert the call to __tls_get_addr to a nop.
1507 // 3) Convert the nop following the call to an add of the loaded offset to the
1508 // thread pointer.
1509 // Since the nop must directly follow the call, the R_PPC64_TLSGD relocation is
1510 // used as the relaxation hint for both steps 2 and 3.
1511 void PPC64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
1512 uint64_t val) const {
1513 switch (rel.type) {
1514 case R_PPC64_GOT_TLSGD16_HA:
1515 // This is relaxed from addis rT, r2, sym@got@tlsgd@ha to
1516 // addis rT, r2, sym@got@tprel@ha.
1517 relocateNoSym(loc, R_PPC64_GOT_TPREL16_HA, val);
1518 return;
1519 case R_PPC64_GOT_TLSGD16:
1520 case R_PPC64_GOT_TLSGD16_LO: {
1521 // Relax from addi r3, rA, sym@got@tlsgd@l to
1522 // ld r3, sym@got@tprel@l(rA)
1523 uint32_t ra = (readFromHalf16(loc) & (0x1f << 16));
1524 writeFromHalf16(loc, 0xe8600000 | ra);
1525 relocateNoSym(loc, R_PPC64_GOT_TPREL16_LO_DS, val);
1526 return;
1528 case R_PPC64_GOT_TLSGD_PCREL34: {
1529 // Relax from paddi r3, 0, sym@got@tlsgd@pcrel, 1 to
1530 // pld r3, sym@got@tprel@pcrel
1531 writePrefixedInstruction(loc, 0x04100000e4600000);
1532 relocateNoSym(loc, R_PPC64_GOT_TPREL_PCREL34, val);
1533 return;
1535 case R_PPC64_TLSGD: {
1536 // PC Relative Relaxation:
1537 // Relax from bl __tls_get_addr@notoc(x@tlsgd) to
1538 // nop
1539 // TOC Relaxation:
1540 // Relax from bl __tls_get_addr(x@tlsgd)
1541 // nop
1542 // to
1543 // nop
1544 // add r3, r3, r13
1545 const uintptr_t locAsInt = reinterpret_cast<uintptr_t>(loc);
1546 if (locAsInt % 4 == 0) {
1547 write32(loc, NOP); // bl __tls_get_addr(sym@tlsgd) --> nop
1548 write32(loc + 4, 0x7c636A14); // nop --> add r3, r3, r13
1549 } else if (locAsInt % 4 == 1) {
1550 // bl __tls_get_addr(sym@tlsgd) --> add r3, r3, r13
1551 write32(loc - 1, 0x7c636a14);
1552 } else {
1553 errorOrWarn("R_PPC64_TLSGD has unexpected byte alignment");
1555 return;
1557 default:
1558 llvm_unreachable("unsupported relocation for TLS GD to IE relaxation");
1562 void PPC64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
1563 uint64_t secAddr = sec.getOutputSection()->addr;
1564 if (auto *s = dyn_cast<InputSection>(&sec))
1565 secAddr += s->outSecOff;
1566 else if (auto *ehIn = dyn_cast<EhInputSection>(&sec))
1567 secAddr += ehIn->getParent()->outSecOff;
1568 uint64_t lastPPCRelaxedRelocOff = -1;
1569 for (const Relocation &rel : sec.relocs()) {
1570 uint8_t *loc = buf + rel.offset;
1571 const uint64_t val =
1572 sec.getRelocTargetVA(sec.file, rel.type, rel.addend,
1573 secAddr + rel.offset, *rel.sym, rel.expr);
1574 switch (rel.expr) {
1575 case R_PPC64_RELAX_GOT_PC: {
1576 // The R_PPC64_PCREL_OPT relocation must appear immediately after
1577 // R_PPC64_GOT_PCREL34 in the relocations table at the same offset.
1578 // We can only relax R_PPC64_PCREL_OPT if we have also relaxed
1579 // the associated R_PPC64_GOT_PCREL34 since only the latter has an
1580 // associated symbol. So save the offset when relaxing R_PPC64_GOT_PCREL34
1581 // and only relax the other if the saved offset matches.
1582 if (rel.type == R_PPC64_GOT_PCREL34)
1583 lastPPCRelaxedRelocOff = rel.offset;
1584 if (rel.type == R_PPC64_PCREL_OPT && rel.offset != lastPPCRelaxedRelocOff)
1585 break;
1586 relaxGot(loc, rel, val);
1587 break;
1589 case R_PPC64_RELAX_TOC:
1590 // rel.sym refers to the STT_SECTION symbol associated to the .toc input
1591 // section. If an R_PPC64_TOC16_LO (.toc + addend) references the TOC
1592 // entry, there may be R_PPC64_TOC16_HA not paired with
1593 // R_PPC64_TOC16_LO_DS. Don't relax. This loses some relaxation
1594 // opportunities but is safe.
1595 if (ppc64noTocRelax.count({rel.sym, rel.addend}) ||
1596 !tryRelaxPPC64TocIndirection(rel, loc))
1597 relocate(loc, rel, val);
1598 break;
1599 case R_PPC64_CALL:
1600 // If this is a call to __tls_get_addr, it may be part of a TLS
1601 // sequence that has been relaxed and turned into a nop. In this
1602 // case, we don't want to handle it as a call.
1603 if (read32(loc) == 0x60000000) // nop
1604 break;
1606 // Patch a nop (0x60000000) to a ld.
1607 if (rel.sym->needsTocRestore()) {
1608 // gcc/gfortran 5.4, 6.3 and earlier versions do not add nop for
1609 // recursive calls even if the function is preemptible. This is not
1610 // wrong in the common case where the function is not preempted at
1611 // runtime. Just ignore.
1612 if ((rel.offset + 8 > sec.content().size() ||
1613 read32(loc + 4) != 0x60000000) &&
1614 rel.sym->file != sec.file) {
1615 // Use substr(6) to remove the "__plt_" prefix.
1616 errorOrWarn(getErrorLocation(loc) + "call to " +
1617 lld::toString(*rel.sym).substr(6) +
1618 " lacks nop, can't restore toc");
1619 break;
1621 write32(loc + 4, 0xe8410018); // ld %r2, 24(%r1)
1623 relocate(loc, rel, val);
1624 break;
1625 case R_RELAX_TLS_GD_TO_IE:
1626 case R_RELAX_TLS_GD_TO_IE_GOT_OFF:
1627 relaxTlsGdToIe(loc, rel, val);
1628 break;
1629 case R_RELAX_TLS_GD_TO_LE:
1630 relaxTlsGdToLe(loc, rel, val);
1631 break;
1632 case R_RELAX_TLS_LD_TO_LE_ABS:
1633 relaxTlsLdToLe(loc, rel, val);
1634 break;
1635 case R_RELAX_TLS_IE_TO_LE:
1636 relaxTlsIeToLe(loc, rel, val);
1637 break;
1638 default:
1639 relocate(loc, rel, val);
1640 break;
1645 // The prologue for a split-stack function is expected to look roughly
1646 // like this:
1647 // .Lglobal_entry_point:
1648 // # TOC pointer initialization.
1649 // ...
1650 // .Llocal_entry_point:
1651 // # load the __private_ss member of the threads tcbhead.
1652 // ld r0,-0x7000-64(r13)
1653 // # subtract the functions stack size from the stack pointer.
1654 // addis r12, r1, ha(-stack-frame size)
1655 // addi r12, r12, l(-stack-frame size)
1656 // # compare needed to actual and branch to allocate_more_stack if more
1657 // # space is needed, otherwise fallthrough to 'normal' function body.
1658 // cmpld cr7,r12,r0
1659 // blt- cr7, .Lallocate_more_stack
1661 // -) The allocate_more_stack block might be placed after the split-stack
1662 // prologue and the `blt-` replaced with a `bge+ .Lnormal_func_body`
1663 // instead.
1664 // -) If either the addis or addi is not needed due to the stack size being
1665 // smaller then 32K or a multiple of 64K they will be replaced with a nop,
1666 // but there will always be 2 instructions the linker can overwrite for the
1667 // adjusted stack size.
1669 // The linkers job here is to increase the stack size used in the addis/addi
1670 // pair by split-stack-size-adjust.
1671 // addis r12, r1, ha(-stack-frame size - split-stack-adjust-size)
1672 // addi r12, r12, l(-stack-frame size - split-stack-adjust-size)
1673 bool PPC64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
1674 uint8_t stOther) const {
1675 // If the caller has a global entry point adjust the buffer past it. The start
1676 // of the split-stack prologue will be at the local entry point.
1677 loc += getPPC64GlobalEntryToLocalEntryOffset(stOther);
1679 // At the very least we expect to see a load of some split-stack data from the
1680 // tcb, and 2 instructions that calculate the ending stack address this
1681 // function will require. If there is not enough room for at least 3
1682 // instructions it can't be a split-stack prologue.
1683 if (loc + 12 >= end)
1684 return false;
1686 // First instruction must be `ld r0, -0x7000-64(r13)`
1687 if (read32(loc) != 0xe80d8fc0)
1688 return false;
1690 int16_t hiImm = 0;
1691 int16_t loImm = 0;
1692 // First instruction can be either an addis if the frame size is larger then
1693 // 32K, or an addi if the size is less then 32K.
1694 int32_t firstInstr = read32(loc + 4);
1695 if (getPrimaryOpCode(firstInstr) == 15) {
1696 hiImm = firstInstr & 0xFFFF;
1697 } else if (getPrimaryOpCode(firstInstr) == 14) {
1698 loImm = firstInstr & 0xFFFF;
1699 } else {
1700 return false;
1703 // Second instruction is either an addi or a nop. If the first instruction was
1704 // an addi then LoImm is set and the second instruction must be a nop.
1705 uint32_t secondInstr = read32(loc + 8);
1706 if (!loImm && getPrimaryOpCode(secondInstr) == 14) {
1707 loImm = secondInstr & 0xFFFF;
1708 } else if (secondInstr != NOP) {
1709 return false;
1712 // The register operands of the first instruction should be the stack-pointer
1713 // (r1) as the input (RA) and r12 as the output (RT). If the second
1714 // instruction is not a nop, then it should use r12 as both input and output.
1715 auto checkRegOperands = [](uint32_t instr, uint8_t expectedRT,
1716 uint8_t expectedRA) {
1717 return ((instr & 0x3E00000) >> 21 == expectedRT) &&
1718 ((instr & 0x1F0000) >> 16 == expectedRA);
1720 if (!checkRegOperands(firstInstr, 12, 1))
1721 return false;
1722 if (secondInstr != NOP && !checkRegOperands(secondInstr, 12, 12))
1723 return false;
1725 int32_t stackFrameSize = (hiImm * 65536) + loImm;
1726 // Check that the adjusted size doesn't overflow what we can represent with 2
1727 // instructions.
1728 if (stackFrameSize < config->splitStackAdjustSize + INT32_MIN) {
1729 error(getErrorLocation(loc) + "split-stack prologue adjustment overflows");
1730 return false;
1733 int32_t adjustedStackFrameSize =
1734 stackFrameSize - config->splitStackAdjustSize;
1736 loImm = adjustedStackFrameSize & 0xFFFF;
1737 hiImm = (adjustedStackFrameSize + 0x8000) >> 16;
1738 if (hiImm) {
1739 write32(loc + 4, 0x3D810000 | (uint16_t)hiImm);
1740 // If the low immediate is zero the second instruction will be a nop.
1741 secondInstr = loImm ? 0x398C0000 | (uint16_t)loImm : NOP;
1742 write32(loc + 8, secondInstr);
1743 } else {
1744 // addi r12, r1, imm
1745 write32(loc + 4, (0x39810000) | (uint16_t)loImm);
1746 write32(loc + 8, NOP);
1749 return true;
1752 TargetInfo *elf::getPPC64TargetInfo() {
1753 static PPC64 target;
1754 return &target;