xref: /llvm-project-15.0.7/lld/ELF/Arch/PPC64.cpp (revision ba03bcbc)
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 //===----------------------------------------------------------------------===//
8 
9 #include "Symbols.h"
10 #include "SyntheticSections.h"
11 #include "Target.h"
12 #include "Thunks.h"
13 #include "lld/Common/ErrorHandler.h"
14 #include "llvm/Support/Endian.h"
15 
16 using namespace llvm;
17 using namespace llvm::object;
18 using namespace llvm::support::endian;
19 using namespace llvm::ELF;
20 using namespace lld;
21 using namespace lld::elf;
22 
23 static uint64_t ppc64TocOffset = 0x8000;
24 static uint64_t dynamicThreadPointerOffset = 0x8000;
25 
26 // The instruction encoding of bits 21-30 from the ISA for the Xform and Dform
27 // instructions that can be used as part of the initial exec TLS sequence.
28 enum XFormOpcd {
29   LBZX = 87,
30   LHZX = 279,
31   LWZX = 23,
32   LDX = 21,
33   STBX = 215,
34   STHX = 407,
35   STWX = 151,
36   STDX = 149,
37   ADD = 266,
38 };
39 
40 enum DFormOpcd {
41   LBZ = 34,
42   LBZU = 35,
43   LHZ = 40,
44   LHZU = 41,
45   LHAU = 43,
46   LWZ = 32,
47   LWZU = 33,
48   LFSU = 49,
49   LD = 58,
50   LFDU = 51,
51   STB = 38,
52   STBU = 39,
53   STH = 44,
54   STHU = 45,
55   STW = 36,
56   STWU = 37,
57   STFSU = 53,
58   STFDU = 55,
59   STD = 62,
60   ADDI = 14
61 };
62 
63 uint64_t elf::getPPC64TocBase() {
64   // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
65   // TOC starts where the first of these sections starts. We always create a
66   // .got when we see a relocation that uses it, so for us the start is always
67   // the .got.
68   uint64_t tocVA = in.got->getVA();
69 
70   // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
71   // thus permitting a full 64 Kbytes segment. Note that the glibc startup
72   // code (crt1.o) assumes that you can get from the TOC base to the
73   // start of the .toc section with only a single (signed) 16-bit relocation.
74   return tocVA + ppc64TocOffset;
75 }
76 
77 unsigned elf::getPPC64GlobalEntryToLocalEntryOffset(uint8_t stOther) {
78   // The offset is encoded into the 3 most significant bits of the st_other
79   // field, with some special values described in section 3.4.1 of the ABI:
80   // 0   --> Zero offset between the GEP and LEP, and the function does NOT use
81   //         the TOC pointer (r2). r2 will hold the same value on returning from
82   //         the function as it did on entering the function.
83   // 1   --> Zero offset between the GEP and LEP, and r2 should be treated as a
84   //         caller-saved register for all callers.
85   // 2-6 --> The  binary logarithm of the offset eg:
86   //         2 --> 2^2 = 4 bytes -->  1 instruction.
87   //         6 --> 2^6 = 64 bytes --> 16 instructions.
88   // 7   --> Reserved.
89   uint8_t gepToLep = (stOther >> 5) & 7;
90   if (gepToLep < 2)
91     return 0;
92 
93   // The value encoded in the st_other bits is the
94   // log-base-2(offset).
95   if (gepToLep < 7)
96     return 1 << gepToLep;
97 
98   error("reserved value of 7 in the 3 most-significant-bits of st_other");
99   return 0;
100 }
101 
102 bool elf::isPPC64SmallCodeModelTocReloc(RelType type) {
103   // The only small code model relocations that access the .toc section.
104   return type == R_PPC64_TOC16 || type == R_PPC64_TOC16_DS;
105 }
106 
107 // Find the R_PPC64_ADDR64 in .rela.toc with matching offset.
108 template <typename ELFT>
109 static std::pair<Defined *, int64_t>
110 getRelaTocSymAndAddend(InputSectionBase *tocSec, uint64_t offset) {
111   if (tocSec->numRelocations == 0)
112     return {};
113 
114   // .rela.toc contains exclusively R_PPC64_ADDR64 relocations sorted by
115   // r_offset: 0, 8, 16, etc. For a given Offset, Offset / 8 gives us the
116   // relocation index in most cases.
117   //
118   // In rare cases a TOC entry may store a constant that doesn't need an
119   // R_PPC64_ADDR64, the corresponding r_offset is therefore missing. Offset / 8
120   // points to a relocation with larger r_offset. Do a linear probe then.
121   // Constants are extremely uncommon in .toc and the extra number of array
122   // accesses can be seen as a small constant.
123   ArrayRef<typename ELFT::Rela> relas = tocSec->template relas<ELFT>();
124   uint64_t index = std::min<uint64_t>(offset / 8, relas.size() - 1);
125   for (;;) {
126     if (relas[index].r_offset == offset) {
127       Symbol &sym = tocSec->getFile<ELFT>()->getRelocTargetSym(relas[index]);
128       return {dyn_cast<Defined>(&sym), getAddend<ELFT>(relas[index])};
129     }
130     if (relas[index].r_offset < offset || index == 0)
131       break;
132     --index;
133   }
134   return {};
135 }
136 
137 // When accessing a symbol defined in another translation unit, compilers
138 // reserve a .toc entry, allocate a local label and generate toc-indirect
139 // instructions:
140 //
141 //   addis 3, 2, .LC0@toc@ha  # R_PPC64_TOC16_HA
142 //   ld    3, .LC0@toc@l(3)   # R_PPC64_TOC16_LO_DS, load the address from a .toc entry
143 //   ld/lwa 3, 0(3)           # load the value from the address
144 //
145 //   .section .toc,"aw",@progbits
146 //   .LC0: .tc var[TC],var
147 //
148 // If var is defined, non-preemptable and addressable with a 32-bit signed
149 // offset from the toc base, the address of var can be computed by adding an
150 // offset to the toc base, saving a load.
151 //
152 //   addis 3,2,var@toc@ha     # this may be relaxed to a nop,
153 //   addi  3,3,var@toc@l      # then this becomes addi 3,2,var@toc
154 //   ld/lwa 3, 0(3)           # load the value from the address
155 //
156 // Returns true if the relaxation is performed.
157 bool elf::tryRelaxPPC64TocIndirection(const Relocation &rel, uint8_t *bufLoc) {
158   assert(config->tocOptimize);
159   if (rel.addend < 0)
160     return false;
161 
162   // If the symbol is not the .toc section, this isn't a toc-indirection.
163   Defined *defSym = dyn_cast<Defined>(rel.sym);
164   if (!defSym || !defSym->isSection() || defSym->section->name != ".toc")
165     return false;
166 
167   Defined *d;
168   int64_t addend;
169   auto *tocISB = cast<InputSectionBase>(defSym->section);
170   std::tie(d, addend) =
171       config->isLE ? getRelaTocSymAndAddend<ELF64LE>(tocISB, rel.addend)
172                    : getRelaTocSymAndAddend<ELF64BE>(tocISB, rel.addend);
173 
174   // Only non-preemptable defined symbols can be relaxed.
175   if (!d || d->isPreemptible)
176     return false;
177 
178   // R_PPC64_ADDR64 should have created a canonical PLT for the non-preemptable
179   // ifunc and changed its type to STT_FUNC.
180   assert(!d->isGnuIFunc());
181 
182   // Two instructions can materialize a 32-bit signed offset from the toc base.
183   uint64_t tocRelative = d->getVA(addend) - getPPC64TocBase();
184   if (!isInt<32>(tocRelative))
185     return false;
186 
187   // Add PPC64TocOffset that will be subtracted by PPC64::relocate().
188   target->relaxGot(bufLoc, rel, tocRelative + ppc64TocOffset);
189   return true;
190 }
191 
192 namespace {
193 class PPC64 final : public TargetInfo {
194 public:
195   PPC64();
196   int getTlsGdRelaxSkip(RelType type) const override;
197   uint32_t calcEFlags() const override;
198   RelExpr getRelExpr(RelType type, const Symbol &s,
199                      const uint8_t *loc) const override;
200   RelType getDynRel(RelType type) const override;
201   void writePltHeader(uint8_t *buf) const override;
202   void writePlt(uint8_t *buf, const Symbol &sym,
203                 uint64_t pltEntryAddr) const override;
204   void writeIplt(uint8_t *buf, const Symbol &sym,
205                  uint64_t pltEntryAddr) const override;
206   void relocate(uint8_t *loc, const Relocation &rel,
207                 uint64_t val) const override;
208   void writeGotHeader(uint8_t *buf) const override;
209   bool needsThunk(RelExpr expr, RelType type, const InputFile *file,
210                   uint64_t branchAddr, const Symbol &s,
211                   int64_t a) const override;
212   uint32_t getThunkSectionSpacing() const override;
213   bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
214   RelExpr adjustRelaxExpr(RelType type, const uint8_t *data,
215                           RelExpr expr) const override;
216   void relaxGot(uint8_t *loc, const Relocation &rel,
217                 uint64_t val) const override;
218   void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
219                       uint64_t val) const override;
220   void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
221                       uint64_t val) const override;
222   void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
223                       uint64_t val) const override;
224   void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
225                       uint64_t val) const override;
226 
227   bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
228                                         uint8_t stOther) const override;
229 };
230 } // namespace
231 
232 // Relocation masks following the #lo(value), #hi(value), #ha(value),
233 // #higher(value), #highera(value), #highest(value), and #highesta(value)
234 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
235 // document.
236 static uint16_t lo(uint64_t v) { return v; }
237 static uint16_t hi(uint64_t v) { return v >> 16; }
238 static uint16_t ha(uint64_t v) { return (v + 0x8000) >> 16; }
239 static uint16_t higher(uint64_t v) { return v >> 32; }
240 static uint16_t highera(uint64_t v) { return (v + 0x8000) >> 32; }
241 static uint16_t highest(uint64_t v) { return v >> 48; }
242 static uint16_t highesta(uint64_t v) { return (v + 0x8000) >> 48; }
243 
244 // Extracts the 'PO' field of an instruction encoding.
245 static uint8_t getPrimaryOpCode(uint32_t encoding) { return (encoding >> 26); }
246 
247 static bool isDQFormInstruction(uint32_t encoding) {
248   switch (getPrimaryOpCode(encoding)) {
249   default:
250     return false;
251   case 56:
252     // The only instruction with a primary opcode of 56 is `lq`.
253     return true;
254   case 61:
255     // There are both DS and DQ instruction forms with this primary opcode.
256     // Namely `lxv` and `stxv` are the DQ-forms that use it.
257     // The DS 'XO' bits being set to 01 is restricted to DQ form.
258     return (encoding & 3) == 0x1;
259   }
260 }
261 
262 static bool isInstructionUpdateForm(uint32_t encoding) {
263   switch (getPrimaryOpCode(encoding)) {
264   default:
265     return false;
266   case LBZU:
267   case LHAU:
268   case LHZU:
269   case LWZU:
270   case LFSU:
271   case LFDU:
272   case STBU:
273   case STHU:
274   case STWU:
275   case STFSU:
276   case STFDU:
277     return true;
278     // LWA has the same opcode as LD, and the DS bits is what differentiates
279     // between LD/LDU/LWA
280   case LD:
281   case STD:
282     return (encoding & 3) == 1;
283   }
284 }
285 
286 // There are a number of places when we either want to read or write an
287 // instruction when handling a half16 relocation type. On big-endian the buffer
288 // pointer is pointing into the middle of the word we want to extract, and on
289 // little-endian it is pointing to the start of the word. These 2 helpers are to
290 // simplify reading and writing in that context.
291 static void writeFromHalf16(uint8_t *loc, uint32_t insn) {
292   write32(config->isLE ? loc : loc - 2, insn);
293 }
294 
295 static uint32_t readFromHalf16(const uint8_t *loc) {
296   return read32(config->isLE ? loc : loc - 2);
297 }
298 
299 PPC64::PPC64() {
300   copyRel = R_PPC64_COPY;
301   gotRel = R_PPC64_GLOB_DAT;
302   noneRel = R_PPC64_NONE;
303   pltRel = R_PPC64_JMP_SLOT;
304   relativeRel = R_PPC64_RELATIVE;
305   iRelativeRel = R_PPC64_IRELATIVE;
306   symbolicRel = R_PPC64_ADDR64;
307   pltHeaderSize = 60;
308   pltEntrySize = 4;
309   ipltEntrySize = 16; // PPC64PltCallStub::size
310   gotBaseSymInGotPlt = false;
311   gotHeaderEntriesNum = 1;
312   gotPltHeaderEntriesNum = 2;
313   needsThunks = true;
314 
315   tlsModuleIndexRel = R_PPC64_DTPMOD64;
316   tlsOffsetRel = R_PPC64_DTPREL64;
317 
318   tlsGotRel = R_PPC64_TPREL64;
319 
320   needsMoreStackNonSplit = false;
321 
322   // We need 64K pages (at least under glibc/Linux, the loader won't
323   // set different permissions on a finer granularity than that).
324   defaultMaxPageSize = 65536;
325 
326   // The PPC64 ELF ABI v1 spec, says:
327   //
328   //   It is normally desirable to put segments with different characteristics
329   //   in separate 256 Mbyte portions of the address space, to give the
330   //   operating system full paging flexibility in the 64-bit address space.
331   //
332   // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
333   // use 0x10000000 as the starting address.
334   defaultImageBase = 0x10000000;
335 
336   write32(trapInstr.data(), 0x7fe00008);
337 }
338 
339 int PPC64::getTlsGdRelaxSkip(RelType type) const {
340   // A __tls_get_addr call instruction is marked with 2 relocations:
341   //
342   //   R_PPC64_TLSGD / R_PPC64_TLSLD: marker relocation
343   //   R_PPC64_REL24: __tls_get_addr
344   //
345   // After the relaxation we no longer call __tls_get_addr and should skip both
346   // relocations to not create a false dependence on __tls_get_addr being
347   // defined.
348   if (type == R_PPC64_TLSGD || type == R_PPC64_TLSLD)
349     return 2;
350   return 1;
351 }
352 
353 static uint32_t getEFlags(InputFile *file) {
354   if (config->ekind == ELF64BEKind)
355     return cast<ObjFile<ELF64BE>>(file)->getObj().getHeader()->e_flags;
356   return cast<ObjFile<ELF64LE>>(file)->getObj().getHeader()->e_flags;
357 }
358 
359 // This file implements v2 ABI. This function makes sure that all
360 // object files have v2 or an unspecified version as an ABI version.
361 uint32_t PPC64::calcEFlags() const {
362   for (InputFile *f : objectFiles) {
363     uint32_t flag = getEFlags(f);
364     if (flag == 1)
365       error(toString(f) + ": ABI version 1 is not supported");
366     else if (flag > 2)
367       error(toString(f) + ": unrecognized e_flags: " + Twine(flag));
368   }
369   return 2;
370 }
371 
372 void PPC64::relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) const {
373   switch (rel.type) {
374   case R_PPC64_TOC16_HA:
375     // Convert "addis reg, 2, .LC0@toc@h" to "addis reg, 2, var@toc@h" or "nop".
376     relocate(loc, rel, val);
377     break;
378   case R_PPC64_TOC16_LO_DS: {
379     // Convert "ld reg, .LC0@toc@l(reg)" to "addi reg, reg, var@toc@l" or
380     // "addi reg, 2, var@toc".
381     uint32_t insn = readFromHalf16(loc);
382     if (getPrimaryOpCode(insn) != LD)
383       error("expected a 'ld' for got-indirect to toc-relative relaxing");
384     writeFromHalf16(loc, (insn & 0x03ffffff) | 0x38000000);
385     relocateNoSym(loc, R_PPC64_TOC16_LO, val);
386     break;
387   }
388   default:
389     llvm_unreachable("unexpected relocation type");
390   }
391 }
392 
393 void PPC64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
394                            uint64_t val) const {
395   // Reference: 3.7.4.2 of the 64-bit ELF V2 abi supplement.
396   // The general dynamic code sequence for a global `x` will look like:
397   // Instruction                    Relocation                Symbol
398   // addis r3, r2, x@got@tlsgd@ha   R_PPC64_GOT_TLSGD16_HA      x
399   // addi  r3, r3, x@got@tlsgd@l    R_PPC64_GOT_TLSGD16_LO      x
400   // bl __tls_get_addr(x@tlsgd)     R_PPC64_TLSGD               x
401   //                                R_PPC64_REL24               __tls_get_addr
402   // nop                            None                       None
403 
404   // Relaxing to local exec entails converting:
405   // addis r3, r2, x@got@tlsgd@ha    into      nop
406   // addi  r3, r3, x@got@tlsgd@l     into      addis r3, r13, x@tprel@ha
407   // bl __tls_get_addr(x@tlsgd)      into      nop
408   // nop                             into      addi r3, r3, x@tprel@l
409 
410   switch (rel.type) {
411   case R_PPC64_GOT_TLSGD16_HA:
412     writeFromHalf16(loc, 0x60000000); // nop
413     break;
414   case R_PPC64_GOT_TLSGD16:
415   case R_PPC64_GOT_TLSGD16_LO:
416     writeFromHalf16(loc, 0x3c6d0000); // addis r3, r13
417     relocateNoSym(loc, R_PPC64_TPREL16_HA, val);
418     break;
419   case R_PPC64_TLSGD:
420     write32(loc, 0x60000000);     // nop
421     write32(loc + 4, 0x38630000); // addi r3, r3
422     // Since we are relocating a half16 type relocation and Loc + 4 points to
423     // the start of an instruction we need to advance the buffer by an extra
424     // 2 bytes on BE.
425     relocateNoSym(loc + 4 + (config->ekind == ELF64BEKind ? 2 : 0),
426                   R_PPC64_TPREL16_LO, val);
427     break;
428   default:
429     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
430   }
431 }
432 
433 void PPC64::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
434                            uint64_t val) const {
435   // Reference: 3.7.4.3 of the 64-bit ELF V2 abi supplement.
436   // The local dynamic code sequence for a global `x` will look like:
437   // Instruction                    Relocation                Symbol
438   // addis r3, r2, x@got@tlsld@ha   R_PPC64_GOT_TLSLD16_HA      x
439   // addi  r3, r3, x@got@tlsld@l    R_PPC64_GOT_TLSLD16_LO      x
440   // bl __tls_get_addr(x@tlsgd)     R_PPC64_TLSLD               x
441   //                                R_PPC64_REL24               __tls_get_addr
442   // nop                            None                       None
443 
444   // Relaxing to local exec entails converting:
445   // addis r3, r2, x@got@tlsld@ha   into      nop
446   // addi  r3, r3, x@got@tlsld@l    into      addis r3, r13, 0
447   // bl __tls_get_addr(x@tlsgd)     into      nop
448   // nop                            into      addi r3, r3, 4096
449 
450   switch (rel.type) {
451   case R_PPC64_GOT_TLSLD16_HA:
452     writeFromHalf16(loc, 0x60000000); // nop
453     break;
454   case R_PPC64_GOT_TLSLD16_LO:
455     writeFromHalf16(loc, 0x3c6d0000); // addis r3, r13, 0
456     break;
457   case R_PPC64_TLSLD:
458     write32(loc, 0x60000000);     // nop
459     write32(loc + 4, 0x38631000); // addi r3, r3, 4096
460     break;
461   case R_PPC64_DTPREL16:
462   case R_PPC64_DTPREL16_HA:
463   case R_PPC64_DTPREL16_HI:
464   case R_PPC64_DTPREL16_DS:
465   case R_PPC64_DTPREL16_LO:
466   case R_PPC64_DTPREL16_LO_DS:
467     relocate(loc, rel, val);
468     break;
469   default:
470     llvm_unreachable("unsupported relocation for TLS LD to LE relaxation");
471   }
472 }
473 
474 unsigned elf::getPPCDFormOp(unsigned secondaryOp) {
475   switch (secondaryOp) {
476   case LBZX:
477     return LBZ;
478   case LHZX:
479     return LHZ;
480   case LWZX:
481     return LWZ;
482   case LDX:
483     return LD;
484   case STBX:
485     return STB;
486   case STHX:
487     return STH;
488   case STWX:
489     return STW;
490   case STDX:
491     return STD;
492   case ADD:
493     return ADDI;
494   default:
495     return 0;
496   }
497 }
498 
499 void PPC64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
500                            uint64_t val) const {
501   // The initial exec code sequence for a global `x` will look like:
502   // Instruction                    Relocation                Symbol
503   // addis r9, r2, x@got@tprel@ha   R_PPC64_GOT_TPREL16_HA      x
504   // ld    r9, x@got@tprel@l(r9)    R_PPC64_GOT_TPREL16_LO_DS   x
505   // add r9, r9, x@tls              R_PPC64_TLS                 x
506 
507   // Relaxing to local exec entails converting:
508   // addis r9, r2, x@got@tprel@ha       into        nop
509   // ld r9, x@got@tprel@l(r9)           into        addis r9, r13, x@tprel@ha
510   // add r9, r9, x@tls                  into        addi r9, r9, x@tprel@l
511 
512   // x@tls R_PPC64_TLS is a relocation which does not compute anything,
513   // it is replaced with r13 (thread pointer).
514 
515   // The add instruction in the initial exec sequence has multiple variations
516   // that need to be handled. If we are building an address it will use an add
517   // instruction, if we are accessing memory it will use any of the X-form
518   // indexed load or store instructions.
519 
520   unsigned offset = (config->ekind == ELF64BEKind) ? 2 : 0;
521   switch (rel.type) {
522   case R_PPC64_GOT_TPREL16_HA:
523     write32(loc - offset, 0x60000000); // nop
524     break;
525   case R_PPC64_GOT_TPREL16_LO_DS:
526   case R_PPC64_GOT_TPREL16_DS: {
527     uint32_t regNo = read32(loc - offset) & 0x03E00000; // bits 6-10
528     write32(loc - offset, 0x3C0D0000 | regNo);          // addis RegNo, r13
529     relocateNoSym(loc, R_PPC64_TPREL16_HA, val);
530     break;
531   }
532   case R_PPC64_TLS: {
533     uint32_t primaryOp = getPrimaryOpCode(read32(loc));
534     if (primaryOp != 31)
535       error("unrecognized instruction for IE to LE R_PPC64_TLS");
536     uint32_t secondaryOp = (read32(loc) & 0x000007FE) >> 1; // bits 21-30
537     uint32_t dFormOp = getPPCDFormOp(secondaryOp);
538     if (dFormOp == 0)
539       error("unrecognized instruction for IE to LE R_PPC64_TLS");
540     write32(loc, ((dFormOp << 26) | (read32(loc) & 0x03FFFFFF)));
541     relocateNoSym(loc + offset, R_PPC64_TPREL16_LO, val);
542     break;
543   }
544   default:
545     llvm_unreachable("unknown relocation for IE to LE");
546     break;
547   }
548 }
549 
550 RelExpr PPC64::getRelExpr(RelType type, const Symbol &s,
551                           const uint8_t *loc) const {
552   switch (type) {
553   case R_PPC64_NONE:
554     return R_NONE;
555   case R_PPC64_ADDR16:
556   case R_PPC64_ADDR16_DS:
557   case R_PPC64_ADDR16_HA:
558   case R_PPC64_ADDR16_HI:
559   case R_PPC64_ADDR16_HIGHER:
560   case R_PPC64_ADDR16_HIGHERA:
561   case R_PPC64_ADDR16_HIGHEST:
562   case R_PPC64_ADDR16_HIGHESTA:
563   case R_PPC64_ADDR16_LO:
564   case R_PPC64_ADDR16_LO_DS:
565   case R_PPC64_ADDR32:
566   case R_PPC64_ADDR64:
567     return R_ABS;
568   case R_PPC64_GOT16:
569   case R_PPC64_GOT16_DS:
570   case R_PPC64_GOT16_HA:
571   case R_PPC64_GOT16_HI:
572   case R_PPC64_GOT16_LO:
573   case R_PPC64_GOT16_LO_DS:
574     return R_GOT_OFF;
575   case R_PPC64_TOC16:
576   case R_PPC64_TOC16_DS:
577   case R_PPC64_TOC16_HI:
578   case R_PPC64_TOC16_LO:
579     return R_GOTREL;
580   case R_PPC64_TOC16_HA:
581   case R_PPC64_TOC16_LO_DS:
582     return config->tocOptimize ? R_PPC64_RELAX_TOC : R_GOTREL;
583   case R_PPC64_TOC:
584     return R_PPC64_TOCBASE;
585   case R_PPC64_REL14:
586   case R_PPC64_REL24:
587     return R_PPC64_CALL_PLT;
588   case R_PPC64_REL16_LO:
589   case R_PPC64_REL16_HA:
590   case R_PPC64_REL16_HI:
591   case R_PPC64_REL32:
592   case R_PPC64_REL64:
593     return R_PC;
594   case R_PPC64_GOT_TLSGD16:
595   case R_PPC64_GOT_TLSGD16_HA:
596   case R_PPC64_GOT_TLSGD16_HI:
597   case R_PPC64_GOT_TLSGD16_LO:
598     return R_TLSGD_GOT;
599   case R_PPC64_GOT_TLSLD16:
600   case R_PPC64_GOT_TLSLD16_HA:
601   case R_PPC64_GOT_TLSLD16_HI:
602   case R_PPC64_GOT_TLSLD16_LO:
603     return R_TLSLD_GOT;
604   case R_PPC64_GOT_TPREL16_HA:
605   case R_PPC64_GOT_TPREL16_LO_DS:
606   case R_PPC64_GOT_TPREL16_DS:
607   case R_PPC64_GOT_TPREL16_HI:
608     return R_GOT_OFF;
609   case R_PPC64_GOT_DTPREL16_HA:
610   case R_PPC64_GOT_DTPREL16_LO_DS:
611   case R_PPC64_GOT_DTPREL16_DS:
612   case R_PPC64_GOT_DTPREL16_HI:
613     return R_TLSLD_GOT_OFF;
614   case R_PPC64_TPREL16:
615   case R_PPC64_TPREL16_HA:
616   case R_PPC64_TPREL16_LO:
617   case R_PPC64_TPREL16_HI:
618   case R_PPC64_TPREL16_DS:
619   case R_PPC64_TPREL16_LO_DS:
620   case R_PPC64_TPREL16_HIGHER:
621   case R_PPC64_TPREL16_HIGHERA:
622   case R_PPC64_TPREL16_HIGHEST:
623   case R_PPC64_TPREL16_HIGHESTA:
624     return R_TLS;
625   case R_PPC64_DTPREL16:
626   case R_PPC64_DTPREL16_DS:
627   case R_PPC64_DTPREL16_HA:
628   case R_PPC64_DTPREL16_HI:
629   case R_PPC64_DTPREL16_HIGHER:
630   case R_PPC64_DTPREL16_HIGHERA:
631   case R_PPC64_DTPREL16_HIGHEST:
632   case R_PPC64_DTPREL16_HIGHESTA:
633   case R_PPC64_DTPREL16_LO:
634   case R_PPC64_DTPREL16_LO_DS:
635   case R_PPC64_DTPREL64:
636     return R_DTPREL;
637   case R_PPC64_TLSGD:
638     return R_TLSDESC_CALL;
639   case R_PPC64_TLSLD:
640     return R_TLSLD_HINT;
641   case R_PPC64_TLS:
642     return R_TLSIE_HINT;
643   default:
644     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
645           ") against symbol " + toString(s));
646     return R_NONE;
647   }
648 }
649 
650 RelType PPC64::getDynRel(RelType type) const {
651   if (type == R_PPC64_ADDR64 || type == R_PPC64_TOC)
652     return R_PPC64_ADDR64;
653   return R_PPC64_NONE;
654 }
655 
656 void PPC64::writeGotHeader(uint8_t *buf) const {
657   write64(buf, getPPC64TocBase());
658 }
659 
660 void PPC64::writePltHeader(uint8_t *buf) const {
661   // The generic resolver stub goes first.
662   write32(buf +  0, 0x7c0802a6); // mflr r0
663   write32(buf +  4, 0x429f0005); // bcl  20,4*cr7+so,8 <_glink+0x8>
664   write32(buf +  8, 0x7d6802a6); // mflr r11
665   write32(buf + 12, 0x7c0803a6); // mtlr r0
666   write32(buf + 16, 0x7d8b6050); // subf r12, r11, r12
667   write32(buf + 20, 0x380cffcc); // subi r0,r12,52
668   write32(buf + 24, 0x7800f082); // srdi r0,r0,62,2
669   write32(buf + 28, 0xe98b002c); // ld   r12,44(r11)
670   write32(buf + 32, 0x7d6c5a14); // add  r11,r12,r11
671   write32(buf + 36, 0xe98b0000); // ld   r12,0(r11)
672   write32(buf + 40, 0xe96b0008); // ld   r11,8(r11)
673   write32(buf + 44, 0x7d8903a6); // mtctr   r12
674   write32(buf + 48, 0x4e800420); // bctr
675 
676   // The 'bcl' instruction will set the link register to the address of the
677   // following instruction ('mflr r11'). Here we store the offset from that
678   // instruction  to the first entry in the GotPlt section.
679   int64_t gotPltOffset = in.gotPlt->getVA() - (in.plt->getVA() + 8);
680   write64(buf + 52, gotPltOffset);
681 }
682 
683 void PPC64::writePlt(uint8_t *buf, const Symbol &sym,
684                      uint64_t /*pltEntryAddr*/) const {
685   int32_t offset = pltHeaderSize + sym.pltIndex * pltEntrySize;
686   // bl __glink_PLTresolve
687   write32(buf, 0x48000000 | ((-offset) & 0x03FFFFFc));
688 }
689 
690 void PPC64::writeIplt(uint8_t *buf, const Symbol &sym,
691                       uint64_t /*pltEntryAddr*/) const {
692   writePPC64LoadAndBranch(buf, sym.getGotPltVA() - getPPC64TocBase());
693 }
694 
695 static std::pair<RelType, uint64_t> toAddr16Rel(RelType type, uint64_t val) {
696   // Relocations relative to the toc-base need to be adjusted by the Toc offset.
697   uint64_t tocBiasedVal = val - ppc64TocOffset;
698   // Relocations relative to dtv[dtpmod] need to be adjusted by the DTP offset.
699   uint64_t dtpBiasedVal = val - dynamicThreadPointerOffset;
700 
701   switch (type) {
702   // TOC biased relocation.
703   case R_PPC64_GOT16:
704   case R_PPC64_GOT_TLSGD16:
705   case R_PPC64_GOT_TLSLD16:
706   case R_PPC64_TOC16:
707     return {R_PPC64_ADDR16, tocBiasedVal};
708   case R_PPC64_GOT16_DS:
709   case R_PPC64_TOC16_DS:
710   case R_PPC64_GOT_TPREL16_DS:
711   case R_PPC64_GOT_DTPREL16_DS:
712     return {R_PPC64_ADDR16_DS, tocBiasedVal};
713   case R_PPC64_GOT16_HA:
714   case R_PPC64_GOT_TLSGD16_HA:
715   case R_PPC64_GOT_TLSLD16_HA:
716   case R_PPC64_GOT_TPREL16_HA:
717   case R_PPC64_GOT_DTPREL16_HA:
718   case R_PPC64_TOC16_HA:
719     return {R_PPC64_ADDR16_HA, tocBiasedVal};
720   case R_PPC64_GOT16_HI:
721   case R_PPC64_GOT_TLSGD16_HI:
722   case R_PPC64_GOT_TLSLD16_HI:
723   case R_PPC64_GOT_TPREL16_HI:
724   case R_PPC64_GOT_DTPREL16_HI:
725   case R_PPC64_TOC16_HI:
726     return {R_PPC64_ADDR16_HI, tocBiasedVal};
727   case R_PPC64_GOT16_LO:
728   case R_PPC64_GOT_TLSGD16_LO:
729   case R_PPC64_GOT_TLSLD16_LO:
730   case R_PPC64_TOC16_LO:
731     return {R_PPC64_ADDR16_LO, tocBiasedVal};
732   case R_PPC64_GOT16_LO_DS:
733   case R_PPC64_TOC16_LO_DS:
734   case R_PPC64_GOT_TPREL16_LO_DS:
735   case R_PPC64_GOT_DTPREL16_LO_DS:
736     return {R_PPC64_ADDR16_LO_DS, tocBiasedVal};
737 
738   // Dynamic Thread pointer biased relocation types.
739   case R_PPC64_DTPREL16:
740     return {R_PPC64_ADDR16, dtpBiasedVal};
741   case R_PPC64_DTPREL16_DS:
742     return {R_PPC64_ADDR16_DS, dtpBiasedVal};
743   case R_PPC64_DTPREL16_HA:
744     return {R_PPC64_ADDR16_HA, dtpBiasedVal};
745   case R_PPC64_DTPREL16_HI:
746     return {R_PPC64_ADDR16_HI, dtpBiasedVal};
747   case R_PPC64_DTPREL16_HIGHER:
748     return {R_PPC64_ADDR16_HIGHER, dtpBiasedVal};
749   case R_PPC64_DTPREL16_HIGHERA:
750     return {R_PPC64_ADDR16_HIGHERA, dtpBiasedVal};
751   case R_PPC64_DTPREL16_HIGHEST:
752     return {R_PPC64_ADDR16_HIGHEST, dtpBiasedVal};
753   case R_PPC64_DTPREL16_HIGHESTA:
754     return {R_PPC64_ADDR16_HIGHESTA, dtpBiasedVal};
755   case R_PPC64_DTPREL16_LO:
756     return {R_PPC64_ADDR16_LO, dtpBiasedVal};
757   case R_PPC64_DTPREL16_LO_DS:
758     return {R_PPC64_ADDR16_LO_DS, dtpBiasedVal};
759   case R_PPC64_DTPREL64:
760     return {R_PPC64_ADDR64, dtpBiasedVal};
761 
762   default:
763     return {type, val};
764   }
765 }
766 
767 static bool isTocOptType(RelType type) {
768   switch (type) {
769   case R_PPC64_GOT16_HA:
770   case R_PPC64_GOT16_LO_DS:
771   case R_PPC64_TOC16_HA:
772   case R_PPC64_TOC16_LO_DS:
773   case R_PPC64_TOC16_LO:
774     return true;
775   default:
776     return false;
777   }
778 }
779 
780 void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
781   RelType type = rel.type;
782   bool shouldTocOptimize =  isTocOptType(type);
783   // For dynamic thread pointer relative, toc-relative, and got-indirect
784   // relocations, proceed in terms of the corresponding ADDR16 relocation type.
785   std::tie(type, val) = toAddr16Rel(type, val);
786 
787   switch (type) {
788   case R_PPC64_ADDR14: {
789     checkAlignment(loc, val, 4, rel);
790     // Preserve the AA/LK bits in the branch instruction
791     uint8_t aalk = loc[3];
792     write16(loc + 2, (aalk & 3) | (val & 0xfffc));
793     break;
794   }
795   case R_PPC64_ADDR16:
796     checkIntUInt(loc, val, 16, rel);
797     write16(loc, val);
798     break;
799   case R_PPC64_ADDR32:
800     checkIntUInt(loc, val, 32, rel);
801     write32(loc, val);
802     break;
803   case R_PPC64_ADDR16_DS:
804   case R_PPC64_TPREL16_DS: {
805     checkInt(loc, val, 16, rel);
806     // DQ-form instructions use bits 28-31 as part of the instruction encoding
807     // DS-form instructions only use bits 30-31.
808     uint16_t mask = isDQFormInstruction(readFromHalf16(loc)) ? 0xf : 0x3;
809     checkAlignment(loc, lo(val), mask + 1, rel);
810     write16(loc, (read16(loc) & mask) | lo(val));
811   } break;
812   case R_PPC64_ADDR16_HA:
813   case R_PPC64_REL16_HA:
814   case R_PPC64_TPREL16_HA:
815     if (config->tocOptimize && shouldTocOptimize && ha(val) == 0)
816       writeFromHalf16(loc, 0x60000000);
817     else
818       write16(loc, ha(val));
819     break;
820   case R_PPC64_ADDR16_HI:
821   case R_PPC64_REL16_HI:
822   case R_PPC64_TPREL16_HI:
823     write16(loc, hi(val));
824     break;
825   case R_PPC64_ADDR16_HIGHER:
826   case R_PPC64_TPREL16_HIGHER:
827     write16(loc, higher(val));
828     break;
829   case R_PPC64_ADDR16_HIGHERA:
830   case R_PPC64_TPREL16_HIGHERA:
831     write16(loc, highera(val));
832     break;
833   case R_PPC64_ADDR16_HIGHEST:
834   case R_PPC64_TPREL16_HIGHEST:
835     write16(loc, highest(val));
836     break;
837   case R_PPC64_ADDR16_HIGHESTA:
838   case R_PPC64_TPREL16_HIGHESTA:
839     write16(loc, highesta(val));
840     break;
841   case R_PPC64_ADDR16_LO:
842   case R_PPC64_REL16_LO:
843   case R_PPC64_TPREL16_LO:
844     // When the high-adjusted part of a toc relocation evaluates to 0, it is
845     // changed into a nop. The lo part then needs to be updated to use the
846     // toc-pointer register r2, as the base register.
847     if (config->tocOptimize && shouldTocOptimize && ha(val) == 0) {
848       uint32_t insn = readFromHalf16(loc);
849       if (isInstructionUpdateForm(insn))
850         error(getErrorLocation(loc) +
851               "can't toc-optimize an update instruction: 0x" +
852               utohexstr(insn));
853       writeFromHalf16(loc, (insn & 0xffe00000) | 0x00020000 | lo(val));
854     } else {
855       write16(loc, lo(val));
856     }
857     break;
858   case R_PPC64_ADDR16_LO_DS:
859   case R_PPC64_TPREL16_LO_DS: {
860     // DQ-form instructions use bits 28-31 as part of the instruction encoding
861     // DS-form instructions only use bits 30-31.
862     uint32_t insn = readFromHalf16(loc);
863     uint16_t mask = isDQFormInstruction(insn) ? 0xf : 0x3;
864     checkAlignment(loc, lo(val), mask + 1, rel);
865     if (config->tocOptimize && shouldTocOptimize && ha(val) == 0) {
866       // When the high-adjusted part of a toc relocation evaluates to 0, it is
867       // changed into a nop. The lo part then needs to be updated to use the toc
868       // pointer register r2, as the base register.
869       if (isInstructionUpdateForm(insn))
870         error(getErrorLocation(loc) +
871               "Can't toc-optimize an update instruction: 0x" +
872               Twine::utohexstr(insn));
873       insn &= 0xffe00000 | mask;
874       writeFromHalf16(loc, insn | 0x00020000 | lo(val));
875     } else {
876       write16(loc, (read16(loc) & mask) | lo(val));
877     }
878   } break;
879   case R_PPC64_TPREL16:
880     checkInt(loc, val, 16, rel);
881     write16(loc, val);
882     break;
883   case R_PPC64_REL32:
884     checkInt(loc, val, 32, rel);
885     write32(loc, val);
886     break;
887   case R_PPC64_ADDR64:
888   case R_PPC64_REL64:
889   case R_PPC64_TOC:
890     write64(loc, val);
891     break;
892   case R_PPC64_REL14: {
893     uint32_t mask = 0x0000FFFC;
894     checkInt(loc, val, 16, rel);
895     checkAlignment(loc, val, 4, rel);
896     write32(loc, (read32(loc) & ~mask) | (val & mask));
897     break;
898   }
899   case R_PPC64_REL24: {
900     uint32_t mask = 0x03FFFFFC;
901     checkInt(loc, val, 26, rel);
902     checkAlignment(loc, val, 4, rel);
903     write32(loc, (read32(loc) & ~mask) | (val & mask));
904     break;
905   }
906   case R_PPC64_DTPREL64:
907     write64(loc, val - dynamicThreadPointerOffset);
908     break;
909   default:
910     llvm_unreachable("unknown relocation");
911   }
912 }
913 
914 bool PPC64::needsThunk(RelExpr expr, RelType type, const InputFile *file,
915                        uint64_t branchAddr, const Symbol &s, int64_t a) const {
916   if (type != R_PPC64_REL14 && type != R_PPC64_REL24)
917     return false;
918 
919   // If a function is in the Plt it needs to be called with a call-stub.
920   if (s.isInPlt())
921     return true;
922 
923   // If a symbol is a weak undefined and we are compiling an executable
924   // it doesn't need a range-extending thunk since it can't be called.
925   if (s.isUndefWeak() && !config->shared)
926     return false;
927 
928   // If the offset exceeds the range of the branch type then it will need
929   // a range-extending thunk.
930   // See the comment in getRelocTargetVA() about R_PPC64_CALL.
931   return !inBranchRange(type, branchAddr,
932                         s.getVA(a) +
933                             getPPC64GlobalEntryToLocalEntryOffset(s.stOther));
934 }
935 
936 uint32_t PPC64::getThunkSectionSpacing() const {
937   // See comment in Arch/ARM.cpp for a more detailed explanation of
938   // getThunkSectionSpacing(). For PPC64 we pick the constant here based on
939   // R_PPC64_REL24, which is used by unconditional branch instructions.
940   // 0x2000000 = (1 << 24-1) * 4
941   return 0x2000000;
942 }
943 
944 bool PPC64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
945   int64_t offset = dst - src;
946   if (type == R_PPC64_REL14)
947     return isInt<16>(offset);
948   if (type == R_PPC64_REL24)
949     return isInt<26>(offset);
950   llvm_unreachable("unsupported relocation type used in branch");
951 }
952 
953 RelExpr PPC64::adjustRelaxExpr(RelType type, const uint8_t *data,
954                                RelExpr expr) const {
955   if (expr == R_RELAX_TLS_GD_TO_IE)
956     return R_RELAX_TLS_GD_TO_IE_GOT_OFF;
957   if (expr == R_RELAX_TLS_LD_TO_LE)
958     return R_RELAX_TLS_LD_TO_LE_ABS;
959   return expr;
960 }
961 
962 // Reference: 3.7.4.1 of the 64-bit ELF V2 abi supplement.
963 // The general dynamic code sequence for a global `x` uses 4 instructions.
964 // Instruction                    Relocation                Symbol
965 // addis r3, r2, x@got@tlsgd@ha   R_PPC64_GOT_TLSGD16_HA      x
966 // addi  r3, r3, x@got@tlsgd@l    R_PPC64_GOT_TLSGD16_LO      x
967 // bl __tls_get_addr(x@tlsgd)     R_PPC64_TLSGD               x
968 //                                R_PPC64_REL24               __tls_get_addr
969 // nop                            None                       None
970 //
971 // Relaxing to initial-exec entails:
972 // 1) Convert the addis/addi pair that builds the address of the tls_index
973 //    struct for 'x' to an addis/ld pair that loads an offset from a got-entry.
974 // 2) Convert the call to __tls_get_addr to a nop.
975 // 3) Convert the nop following the call to an add of the loaded offset to the
976 //    thread pointer.
977 // Since the nop must directly follow the call, the R_PPC64_TLSGD relocation is
978 // used as the relaxation hint for both steps 2 and 3.
979 void PPC64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
980                            uint64_t val) const {
981   switch (rel.type) {
982   case R_PPC64_GOT_TLSGD16_HA:
983     // This is relaxed from addis rT, r2, sym@got@tlsgd@ha to
984     //                      addis rT, r2, sym@got@tprel@ha.
985     relocateNoSym(loc, R_PPC64_GOT_TPREL16_HA, val);
986     return;
987   case R_PPC64_GOT_TLSGD16:
988   case R_PPC64_GOT_TLSGD16_LO: {
989     // Relax from addi  r3, rA, sym@got@tlsgd@l to
990     //            ld r3, sym@got@tprel@l(rA)
991     uint32_t ra = (readFromHalf16(loc) & (0x1f << 16));
992     writeFromHalf16(loc, 0xe8600000 | ra);
993     relocateNoSym(loc, R_PPC64_GOT_TPREL16_LO_DS, val);
994     return;
995   }
996   case R_PPC64_TLSGD:
997     write32(loc, 0x60000000);     // bl __tls_get_addr(sym@tlsgd) --> nop
998     write32(loc + 4, 0x7c636A14); // nop --> add r3, r3, r13
999     return;
1000   default:
1001     llvm_unreachable("unsupported relocation for TLS GD to IE relaxation");
1002   }
1003 }
1004 
1005 // The prologue for a split-stack function is expected to look roughly
1006 // like this:
1007 //    .Lglobal_entry_point:
1008 //      # TOC pointer initialization.
1009 //      ...
1010 //    .Llocal_entry_point:
1011 //      # load the __private_ss member of the threads tcbhead.
1012 //      ld r0,-0x7000-64(r13)
1013 //      # subtract the functions stack size from the stack pointer.
1014 //      addis r12, r1, ha(-stack-frame size)
1015 //      addi  r12, r12, l(-stack-frame size)
1016 //      # compare needed to actual and branch to allocate_more_stack if more
1017 //      # space is needed, otherwise fallthrough to 'normal' function body.
1018 //      cmpld cr7,r12,r0
1019 //      blt- cr7, .Lallocate_more_stack
1020 //
1021 // -) The allocate_more_stack block might be placed after the split-stack
1022 //    prologue and the `blt-` replaced with a `bge+ .Lnormal_func_body`
1023 //    instead.
1024 // -) If either the addis or addi is not needed due to the stack size being
1025 //    smaller then 32K or a multiple of 64K they will be replaced with a nop,
1026 //    but there will always be 2 instructions the linker can overwrite for the
1027 //    adjusted stack size.
1028 //
1029 // The linkers job here is to increase the stack size used in the addis/addi
1030 // pair by split-stack-size-adjust.
1031 // addis r12, r1, ha(-stack-frame size - split-stack-adjust-size)
1032 // addi  r12, r12, l(-stack-frame size - split-stack-adjust-size)
1033 bool PPC64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
1034                                              uint8_t stOther) const {
1035   // If the caller has a global entry point adjust the buffer past it. The start
1036   // of the split-stack prologue will be at the local entry point.
1037   loc += getPPC64GlobalEntryToLocalEntryOffset(stOther);
1038 
1039   // At the very least we expect to see a load of some split-stack data from the
1040   // tcb, and 2 instructions that calculate the ending stack address this
1041   // function will require. If there is not enough room for at least 3
1042   // instructions it can't be a split-stack prologue.
1043   if (loc + 12 >= end)
1044     return false;
1045 
1046   // First instruction must be `ld r0, -0x7000-64(r13)`
1047   if (read32(loc) != 0xe80d8fc0)
1048     return false;
1049 
1050   int16_t hiImm = 0;
1051   int16_t loImm = 0;
1052   // First instruction can be either an addis if the frame size is larger then
1053   // 32K, or an addi if the size is less then 32K.
1054   int32_t firstInstr = read32(loc + 4);
1055   if (getPrimaryOpCode(firstInstr) == 15) {
1056     hiImm = firstInstr & 0xFFFF;
1057   } else if (getPrimaryOpCode(firstInstr) == 14) {
1058     loImm = firstInstr & 0xFFFF;
1059   } else {
1060     return false;
1061   }
1062 
1063   // Second instruction is either an addi or a nop. If the first instruction was
1064   // an addi then LoImm is set and the second instruction must be a nop.
1065   uint32_t secondInstr = read32(loc + 8);
1066   if (!loImm && getPrimaryOpCode(secondInstr) == 14) {
1067     loImm = secondInstr & 0xFFFF;
1068   } else if (secondInstr != 0x60000000) {
1069     return false;
1070   }
1071 
1072   // The register operands of the first instruction should be the stack-pointer
1073   // (r1) as the input (RA) and r12 as the output (RT). If the second
1074   // instruction is not a nop, then it should use r12 as both input and output.
1075   auto checkRegOperands = [](uint32_t instr, uint8_t expectedRT,
1076                              uint8_t expectedRA) {
1077     return ((instr & 0x3E00000) >> 21 == expectedRT) &&
1078            ((instr & 0x1F0000) >> 16 == expectedRA);
1079   };
1080   if (!checkRegOperands(firstInstr, 12, 1))
1081     return false;
1082   if (secondInstr != 0x60000000 && !checkRegOperands(secondInstr, 12, 12))
1083     return false;
1084 
1085   int32_t stackFrameSize = (hiImm * 65536) + loImm;
1086   // Check that the adjusted size doesn't overflow what we can represent with 2
1087   // instructions.
1088   if (stackFrameSize < config->splitStackAdjustSize + INT32_MIN) {
1089     error(getErrorLocation(loc) + "split-stack prologue adjustment overflows");
1090     return false;
1091   }
1092 
1093   int32_t adjustedStackFrameSize =
1094       stackFrameSize - config->splitStackAdjustSize;
1095 
1096   loImm = adjustedStackFrameSize & 0xFFFF;
1097   hiImm = (adjustedStackFrameSize + 0x8000) >> 16;
1098   if (hiImm) {
1099     write32(loc + 4, 0x3D810000 | (uint16_t)hiImm);
1100     // If the low immediate is zero the second instruction will be a nop.
1101     secondInstr = loImm ? 0x398C0000 | (uint16_t)loImm : 0x60000000;
1102     write32(loc + 8, secondInstr);
1103   } else {
1104     // addi r12, r1, imm
1105     write32(loc + 4, (0x39810000) | (uint16_t)loImm);
1106     write32(loc + 8, 0x60000000);
1107   }
1108 
1109   return true;
1110 }
1111 
1112 TargetInfo *elf::getPPC64TargetInfo() {
1113   static PPC64 target;
1114   return &target;
1115 }
1116