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