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