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