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