xref: /llvm-project-15.0.7/lld/ELF/Target.cpp (revision 498ee00a)
1 //===- Target.cpp ---------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Machine-specific things, such as applying relocations, creation of
11 // GOT or PLT entries, etc., are handled in this file.
12 //
13 // Refer the ELF spec for the single letter variables, S, A or P, used
14 // in this file.
15 //
16 // Some functions defined in this file has "relaxTls" as part of their names.
17 // They do peephole optimization for TLS variables by rewriting instructions.
18 // They are not part of the ABI but optional optimization, so you can skip
19 // them if you are not interested in how TLS variables are optimized.
20 // See the following paper for the details.
21 //
22 //   Ulrich Drepper, ELF Handling For Thread-Local Storage
23 //   http://www.akkadia.org/drepper/tls.pdf
24 //
25 //===----------------------------------------------------------------------===//
26 
27 #include "Target.h"
28 #include "Error.h"
29 #include "InputFiles.h"
30 #include "OutputSections.h"
31 #include "Symbols.h"
32 #include "Thunks.h"
33 
34 #include "llvm/ADT/ArrayRef.h"
35 #include "llvm/Object/ELF.h"
36 #include "llvm/Support/Endian.h"
37 #include "llvm/Support/ELF.h"
38 
39 using namespace llvm;
40 using namespace llvm::object;
41 using namespace llvm::support::endian;
42 using namespace llvm::ELF;
43 
44 namespace lld {
45 namespace elf {
46 
47 TargetInfo *Target;
48 
49 static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
50 
51 StringRef getRelName(uint32_t Type) {
52   return getELFRelocationTypeName(Config->EMachine, Type);
53 }
54 
55 template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
56   if (!isInt<N>(V))
57     error("relocation " + getRelName(Type) + " out of range");
58 }
59 
60 template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
61   if (!isUInt<N>(V))
62     error("relocation " + getRelName(Type) + " out of range");
63 }
64 
65 template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
66   if (!isInt<N>(V) && !isUInt<N>(V))
67     error("relocation " + getRelName(Type) + " out of range");
68 }
69 
70 template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
71   if ((V & (N - 1)) != 0)
72     error("improper alignment for relocation " + getRelName(Type));
73 }
74 
75 static void errorDynRel(uint32_t Type) {
76   error("relocation " + getRelName(Type) +
77         " cannot be used against shared object; recompile with -fPIC.");
78 }
79 
80 namespace {
81 class X86TargetInfo final : public TargetInfo {
82 public:
83   X86TargetInfo();
84   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
85   uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
86   void writeGotPltHeader(uint8_t *Buf) const override;
87   uint32_t getDynRel(uint32_t Type) const override;
88   bool isTlsLocalDynamicRel(uint32_t Type) const override;
89   bool isTlsGlobalDynamicRel(uint32_t Type) const override;
90   bool isTlsInitialExecRel(uint32_t Type) const override;
91   void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
92   void writePltHeader(uint8_t *Buf) const override;
93   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
94                 int32_t Index, unsigned RelOff) const override;
95   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
96 
97   RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
98                           RelExpr Expr) const override;
99   void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
100   void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
101   void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
102   void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
103 };
104 
105 template <class ELFT> class X86_64TargetInfo final : public TargetInfo {
106 public:
107   X86_64TargetInfo();
108   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
109   uint32_t getDynRel(uint32_t Type) const override;
110   bool isTlsLocalDynamicRel(uint32_t Type) const override;
111   bool isTlsGlobalDynamicRel(uint32_t Type) const override;
112   bool isTlsInitialExecRel(uint32_t Type) const override;
113   void writeGotPltHeader(uint8_t *Buf) const override;
114   void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
115   void writePltHeader(uint8_t *Buf) const override;
116   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
117                 int32_t Index, unsigned RelOff) const override;
118   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
119 
120   RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
121                           RelExpr Expr) const override;
122   void relaxGot(uint8_t *Loc, uint64_t Val) const override;
123   void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
124   void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
125   void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
126   void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
127 
128 private:
129   void relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
130                      uint8_t ModRm) const;
131 };
132 
133 class PPCTargetInfo final : public TargetInfo {
134 public:
135   PPCTargetInfo();
136   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
137   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
138 };
139 
140 class PPC64TargetInfo final : public TargetInfo {
141 public:
142   PPC64TargetInfo();
143   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
144   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
145                 int32_t Index, unsigned RelOff) const override;
146   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
147 };
148 
149 class AArch64TargetInfo final : public TargetInfo {
150 public:
151   AArch64TargetInfo();
152   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
153   uint32_t getDynRel(uint32_t Type) const override;
154   bool isTlsInitialExecRel(uint32_t Type) const override;
155   void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
156   void writePltHeader(uint8_t *Buf) const override;
157   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
158                 int32_t Index, unsigned RelOff) const override;
159   bool usesOnlyLowPageBits(uint32_t Type) const override;
160   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
161   RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
162                           RelExpr Expr) const override;
163   void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
164   void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
165   void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
166 };
167 
168 class AMDGPUTargetInfo final : public TargetInfo {
169 public:
170   AMDGPUTargetInfo();
171   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
172   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
173 };
174 
175 class ARMTargetInfo final : public TargetInfo {
176 public:
177   ARMTargetInfo();
178   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
179   uint32_t getDynRel(uint32_t Type) const override;
180   uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
181   bool isTlsLocalDynamicRel(uint32_t Type) const override;
182   bool isTlsGlobalDynamicRel(uint32_t Type) const override;
183   bool isTlsInitialExecRel(uint32_t Type) const override;
184   void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
185   void writePltHeader(uint8_t *Buf) const override;
186   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
187                 int32_t Index, unsigned RelOff) const override;
188   RelExpr getThunkExpr(RelExpr Expr, uint32_t RelocType,
189                        const InputFile &File,
190                        const SymbolBody &S) const override;
191   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
192 };
193 
194 template <class ELFT> class MipsTargetInfo final : public TargetInfo {
195 public:
196   MipsTargetInfo();
197   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
198   uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
199   uint32_t getDynRel(uint32_t Type) const override;
200   bool isTlsLocalDynamicRel(uint32_t Type) const override;
201   bool isTlsGlobalDynamicRel(uint32_t Type) const override;
202   void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
203   void writePltHeader(uint8_t *Buf) const override;
204   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
205                 int32_t Index, unsigned RelOff) const override;
206   RelExpr getThunkExpr(RelExpr Expr, uint32_t RelocType,
207                        const InputFile &File,
208                        const SymbolBody &S) const override;
209   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
210   bool usesOnlyLowPageBits(uint32_t Type) const override;
211 };
212 } // anonymous namespace
213 
214 TargetInfo *createTarget() {
215   switch (Config->EMachine) {
216   case EM_386:
217   case EM_IAMCU:
218     return new X86TargetInfo();
219   case EM_AARCH64:
220     return new AArch64TargetInfo();
221   case EM_AMDGPU:
222     return new AMDGPUTargetInfo();
223   case EM_ARM:
224     return new ARMTargetInfo();
225   case EM_MIPS:
226     switch (Config->EKind) {
227     case ELF32LEKind:
228       return new MipsTargetInfo<ELF32LE>();
229     case ELF32BEKind:
230       return new MipsTargetInfo<ELF32BE>();
231     case ELF64LEKind:
232       return new MipsTargetInfo<ELF64LE>();
233     case ELF64BEKind:
234       return new MipsTargetInfo<ELF64BE>();
235     default:
236       fatal("unsupported MIPS target");
237     }
238   case EM_PPC:
239     return new PPCTargetInfo();
240   case EM_PPC64:
241     return new PPC64TargetInfo();
242   case EM_X86_64:
243     if (Config->EKind == ELF32LEKind)
244       return new X86_64TargetInfo<ELF32LE>();
245     return new X86_64TargetInfo<ELF64LE>();
246   }
247   fatal("unknown target machine");
248 }
249 
250 TargetInfo::~TargetInfo() {}
251 
252 uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
253                                        uint32_t Type) const {
254   return 0;
255 }
256 
257 bool TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { return false; }
258 
259 RelExpr TargetInfo::getThunkExpr(RelExpr Expr, uint32_t RelocType,
260                                  const InputFile &File,
261                                  const SymbolBody &S) const {
262   return Expr;
263 }
264 
265 bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
266 
267 bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
268 
269 bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
270   return false;
271 }
272 
273 RelExpr TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
274                                     RelExpr Expr) const {
275   return Expr;
276 }
277 
278 void TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
279   llvm_unreachable("Should not have claimed to be relaxable");
280 }
281 
282 void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
283                                 uint64_t Val) const {
284   llvm_unreachable("Should not have claimed to be relaxable");
285 }
286 
287 void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
288                                 uint64_t Val) const {
289   llvm_unreachable("Should not have claimed to be relaxable");
290 }
291 
292 void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
293                                 uint64_t Val) const {
294   llvm_unreachable("Should not have claimed to be relaxable");
295 }
296 
297 void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
298                                 uint64_t Val) const {
299   llvm_unreachable("Should not have claimed to be relaxable");
300 }
301 
302 X86TargetInfo::X86TargetInfo() {
303   CopyRel = R_386_COPY;
304   GotRel = R_386_GLOB_DAT;
305   PltRel = R_386_JUMP_SLOT;
306   IRelativeRel = R_386_IRELATIVE;
307   RelativeRel = R_386_RELATIVE;
308   TlsGotRel = R_386_TLS_TPOFF;
309   TlsModuleIndexRel = R_386_TLS_DTPMOD32;
310   TlsOffsetRel = R_386_TLS_DTPOFF32;
311   GotEntrySize = 4;
312   GotPltEntrySize = 4;
313   PltEntrySize = 16;
314   PltHeaderSize = 16;
315   TlsGdRelaxSkip = 2;
316 }
317 
318 RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
319   switch (Type) {
320   default:
321     return R_ABS;
322   case R_386_TLS_GD:
323     return R_TLSGD;
324   case R_386_TLS_LDM:
325     return R_TLSLD;
326   case R_386_PLT32:
327     return R_PLT_PC;
328   case R_386_PC32:
329     return R_PC;
330   case R_386_GOTPC:
331     return R_GOTONLY_PC_FROM_END;
332   case R_386_TLS_IE:
333     return R_GOT;
334   case R_386_GOT32:
335   case R_386_GOT32X:
336   case R_386_TLS_GOTIE:
337     return R_GOT_FROM_END;
338   case R_386_GOTOFF:
339     return R_GOTREL_FROM_END;
340   case R_386_TLS_LE:
341     return R_TLS;
342   case R_386_TLS_LE_32:
343     return R_NEG_TLS;
344   }
345 }
346 
347 RelExpr X86TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
348                                        RelExpr Expr) const {
349   switch (Expr) {
350   default:
351     return Expr;
352   case R_RELAX_TLS_GD_TO_IE:
353     return R_RELAX_TLS_GD_TO_IE_END;
354   case R_RELAX_TLS_GD_TO_LE:
355     return R_RELAX_TLS_GD_TO_LE_NEG;
356   }
357 }
358 
359 void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
360   write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
361 }
362 
363 void X86TargetInfo::writeGotPlt(uint8_t *Buf, const SymbolBody &S) const {
364   // Entries in .got.plt initially points back to the corresponding
365   // PLT entries with a fixed offset to skip the first instruction.
366   write32le(Buf, S.getPltVA<ELF32LE>() + 6);
367 }
368 
369 uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
370   if (Type == R_386_TLS_LE)
371     return R_386_TLS_TPOFF;
372   if (Type == R_386_TLS_LE_32)
373     return R_386_TLS_TPOFF32;
374   return Type;
375 }
376 
377 bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
378   return Type == R_386_TLS_GD;
379 }
380 
381 bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
382   return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
383 }
384 
385 bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
386   return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
387 }
388 
389 void X86TargetInfo::writePltHeader(uint8_t *Buf) const {
390   // Executable files and shared object files have
391   // separate procedure linkage tables.
392   if (Config->Pic) {
393     const uint8_t V[] = {
394         0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
395         0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp   *8(%ebx)
396         0x90, 0x90, 0x90, 0x90              // nop; nop; nop; nop
397     };
398     memcpy(Buf, V, sizeof(V));
399     return;
400   }
401 
402   const uint8_t PltData[] = {
403       0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
404       0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp   *(GOT+8)
405       0x90, 0x90, 0x90, 0x90              // nop; nop; nop; nop
406   };
407   memcpy(Buf, PltData, sizeof(PltData));
408   uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
409   write32le(Buf + 2, Got + 4);
410   write32le(Buf + 8, Got + 8);
411 }
412 
413 void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
414                              uint64_t PltEntryAddr, int32_t Index,
415                              unsigned RelOff) const {
416   const uint8_t Inst[] = {
417       0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
418       0x68, 0x00, 0x00, 0x00, 0x00,       // pushl $reloc_offset
419       0xe9, 0x00, 0x00, 0x00, 0x00        // jmp .PLT0@PC
420   };
421   memcpy(Buf, Inst, sizeof(Inst));
422 
423   // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
424   Buf[1] = Config->Pic ? 0xa3 : 0x25;
425   uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
426   write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
427   write32le(Buf + 7, RelOff);
428   write32le(Buf + 12, -Index * PltEntrySize - PltHeaderSize - 16);
429 }
430 
431 uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
432                                           uint32_t Type) const {
433   switch (Type) {
434   default:
435     return 0;
436   case R_386_32:
437   case R_386_GOT32:
438   case R_386_GOT32X:
439   case R_386_GOTOFF:
440   case R_386_GOTPC:
441   case R_386_PC32:
442   case R_386_PLT32:
443   case R_386_TLS_LE:
444     return read32le(Buf);
445   }
446 }
447 
448 void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
449                                 uint64_t Val) const {
450   checkInt<32>(Val, Type);
451   write32le(Loc, Val);
452 }
453 
454 void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
455                                    uint64_t Val) const {
456   // Convert
457   //   leal x@tlsgd(, %ebx, 1),
458   //   call __tls_get_addr@plt
459   // to
460   //   movl %gs:0,%eax
461   //   subl $x@ntpoff,%eax
462   const uint8_t Inst[] = {
463       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
464       0x81, 0xe8, 0x00, 0x00, 0x00, 0x00  // subl 0(%ebx), %eax
465   };
466   memcpy(Loc - 3, Inst, sizeof(Inst));
467   relocateOne(Loc + 5, R_386_32, Val);
468 }
469 
470 void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
471                                    uint64_t Val) const {
472   // Convert
473   //   leal x@tlsgd(, %ebx, 1),
474   //   call __tls_get_addr@plt
475   // to
476   //   movl %gs:0, %eax
477   //   addl x@gotntpoff(%ebx), %eax
478   const uint8_t Inst[] = {
479       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
480       0x03, 0x83, 0x00, 0x00, 0x00, 0x00  // addl 0(%ebx), %eax
481   };
482   memcpy(Loc - 3, Inst, sizeof(Inst));
483   relocateOne(Loc + 5, R_386_32, Val);
484 }
485 
486 // In some conditions, relocations can be optimized to avoid using GOT.
487 // This function does that for Initial Exec to Local Exec case.
488 void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
489                                    uint64_t Val) const {
490   // Ulrich's document section 6.2 says that @gotntpoff can
491   // be used with MOVL or ADDL instructions.
492   // @indntpoff is similar to @gotntpoff, but for use in
493   // position dependent code.
494   uint8_t Reg = (Loc[-1] >> 3) & 7;
495 
496   if (Type == R_386_TLS_IE) {
497     if (Loc[-1] == 0xa1) {
498       // "movl foo@indntpoff,%eax" -> "movl $foo,%eax"
499       // This case is different from the generic case below because
500       // this is a 5 byte instruction while below is 6 bytes.
501       Loc[-1] = 0xb8;
502     } else if (Loc[-2] == 0x8b) {
503       // "movl foo@indntpoff,%reg" -> "movl $foo,%reg"
504       Loc[-2] = 0xc7;
505       Loc[-1] = 0xc0 | Reg;
506     } else {
507       // "addl foo@indntpoff,%reg" -> "addl $foo,%reg"
508       Loc[-2] = 0x81;
509       Loc[-1] = 0xc0 | Reg;
510     }
511   } else {
512     assert(Type == R_386_TLS_GOTIE);
513     if (Loc[-2] == 0x8b) {
514       // "movl foo@gottpoff(%rip),%reg" -> "movl $foo,%reg"
515       Loc[-2] = 0xc7;
516       Loc[-1] = 0xc0 | Reg;
517     } else {
518       // "addl foo@gotntpoff(%rip),%reg" -> "leal foo(%reg),%reg"
519       Loc[-2] = 0x8d;
520       Loc[-1] = 0x80 | (Reg << 3) | Reg;
521     }
522   }
523   relocateOne(Loc, R_386_TLS_LE, Val);
524 }
525 
526 void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
527                                    uint64_t Val) const {
528   if (Type == R_386_TLS_LDO_32) {
529     relocateOne(Loc, R_386_TLS_LE, Val);
530     return;
531   }
532 
533   // Convert
534   //   leal foo(%reg),%eax
535   //   call ___tls_get_addr
536   // to
537   //   movl %gs:0,%eax
538   //   nop
539   //   leal 0(%esi,1),%esi
540   const uint8_t Inst[] = {
541       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
542       0x90,                               // nop
543       0x8d, 0x74, 0x26, 0x00              // leal 0(%esi,1),%esi
544   };
545   memcpy(Loc - 2, Inst, sizeof(Inst));
546 }
547 
548 template <class ELFT> X86_64TargetInfo<ELFT>::X86_64TargetInfo() {
549   CopyRel = R_X86_64_COPY;
550   GotRel = R_X86_64_GLOB_DAT;
551   PltRel = R_X86_64_JUMP_SLOT;
552   RelativeRel = R_X86_64_RELATIVE;
553   IRelativeRel = R_X86_64_IRELATIVE;
554   TlsGotRel = R_X86_64_TPOFF64;
555   TlsModuleIndexRel = R_X86_64_DTPMOD64;
556   TlsOffsetRel = R_X86_64_DTPOFF64;
557   GotEntrySize = 8;
558   GotPltEntrySize = 8;
559   PltEntrySize = 16;
560   PltHeaderSize = 16;
561   TlsGdRelaxSkip = 2;
562 }
563 
564 template <class ELFT>
565 RelExpr X86_64TargetInfo<ELFT>::getRelExpr(uint32_t Type,
566                                            const SymbolBody &S) const {
567   switch (Type) {
568   default:
569     return R_ABS;
570   case R_X86_64_TPOFF32:
571     return R_TLS;
572   case R_X86_64_TLSLD:
573     return R_TLSLD_PC;
574   case R_X86_64_TLSGD:
575     return R_TLSGD_PC;
576   case R_X86_64_SIZE32:
577   case R_X86_64_SIZE64:
578     return R_SIZE;
579   case R_X86_64_PLT32:
580     return R_PLT_PC;
581   case R_X86_64_PC32:
582   case R_X86_64_PC64:
583     return R_PC;
584   case R_X86_64_GOT32:
585     return R_GOT_FROM_END;
586   case R_X86_64_GOTPCREL:
587   case R_X86_64_GOTPCRELX:
588   case R_X86_64_REX_GOTPCRELX:
589   case R_X86_64_GOTTPOFF:
590     return R_GOT_PC;
591   }
592 }
593 
594 template <class ELFT>
595 void X86_64TargetInfo<ELFT>::writeGotPltHeader(uint8_t *Buf) const {
596   // The first entry holds the value of _DYNAMIC. It is not clear why that is
597   // required, but it is documented in the psabi and the glibc dynamic linker
598   // seems to use it (note that this is relevant for linking ld.so, not any
599   // other program).
600   write64le(Buf, Out<ELFT>::Dynamic->getVA());
601 }
602 
603 template <class ELFT>
604 void X86_64TargetInfo<ELFT>::writeGotPlt(uint8_t *Buf,
605                                          const SymbolBody &S) const {
606   // See comments in X86TargetInfo::writeGotPlt.
607   write32le(Buf, S.getPltVA<ELFT>() + 6);
608 }
609 
610 template <class ELFT>
611 void X86_64TargetInfo<ELFT>::writePltHeader(uint8_t *Buf) const {
612   const uint8_t PltData[] = {
613       0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
614       0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
615       0x0f, 0x1f, 0x40, 0x00              // nopl 0x0(rax)
616   };
617   memcpy(Buf, PltData, sizeof(PltData));
618   uint64_t Got = Out<ELFT>::GotPlt->getVA();
619   uint64_t Plt = Out<ELFT>::Plt->getVA();
620   write32le(Buf + 2, Got - Plt + 2); // GOT+8
621   write32le(Buf + 8, Got - Plt + 4); // GOT+16
622 }
623 
624 template <class ELFT>
625 void X86_64TargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
626                                       uint64_t PltEntryAddr, int32_t Index,
627                                       unsigned RelOff) const {
628   const uint8_t Inst[] = {
629       0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
630       0x68, 0x00, 0x00, 0x00, 0x00,       // pushq <relocation index>
631       0xe9, 0x00, 0x00, 0x00, 0x00        // jmpq plt[0]
632   };
633   memcpy(Buf, Inst, sizeof(Inst));
634 
635   write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
636   write32le(Buf + 7, Index);
637   write32le(Buf + 12, -Index * PltEntrySize - PltHeaderSize - 16);
638 }
639 
640 template <class ELFT>
641 uint32_t X86_64TargetInfo<ELFT>::getDynRel(uint32_t Type) const {
642   if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
643     errorDynRel(Type);
644   return Type;
645 }
646 
647 template <class ELFT>
648 bool X86_64TargetInfo<ELFT>::isTlsInitialExecRel(uint32_t Type) const {
649   return Type == R_X86_64_GOTTPOFF;
650 }
651 
652 template <class ELFT>
653 bool X86_64TargetInfo<ELFT>::isTlsGlobalDynamicRel(uint32_t Type) const {
654   return Type == R_X86_64_TLSGD;
655 }
656 
657 template <class ELFT>
658 bool X86_64TargetInfo<ELFT>::isTlsLocalDynamicRel(uint32_t Type) const {
659   return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
660          Type == R_X86_64_TLSLD;
661 }
662 
663 template <class ELFT>
664 void X86_64TargetInfo<ELFT>::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
665                                             uint64_t Val) const {
666   // Convert
667   //   .byte 0x66
668   //   leaq x@tlsgd(%rip), %rdi
669   //   .word 0x6666
670   //   rex64
671   //   call __tls_get_addr@plt
672   // to
673   //   mov %fs:0x0,%rax
674   //   lea x@tpoff,%rax
675   const uint8_t Inst[] = {
676       0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
677       0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00              // lea x@tpoff,%rax
678   };
679   memcpy(Loc - 4, Inst, sizeof(Inst));
680   // The original code used a pc relative relocation and so we have to
681   // compensate for the -4 in had in the addend.
682   relocateOne(Loc + 8, R_X86_64_TPOFF32, Val + 4);
683 }
684 
685 template <class ELFT>
686 void X86_64TargetInfo<ELFT>::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
687                                             uint64_t Val) const {
688   // Convert
689   //   .byte 0x66
690   //   leaq x@tlsgd(%rip), %rdi
691   //   .word 0x6666
692   //   rex64
693   //   call __tls_get_addr@plt
694   // to
695   //   mov %fs:0x0,%rax
696   //   addq x@tpoff,%rax
697   const uint8_t Inst[] = {
698       0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
699       0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00              // addq x@tpoff,%rax
700   };
701   memcpy(Loc - 4, Inst, sizeof(Inst));
702   // Both code sequences are PC relatives, but since we are moving the constant
703   // forward by 8 bytes we have to subtract the value by 8.
704   relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
705 }
706 
707 // In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
708 // R_X86_64_TPOFF32 so that it does not use GOT.
709 template <class ELFT>
710 void X86_64TargetInfo<ELFT>::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
711                                             uint64_t Val) const {
712   uint8_t *Inst = Loc - 3;
713   uint8_t Reg = Loc[-1] >> 3;
714   uint8_t *RegSlot = Loc - 1;
715 
716   // Note that ADD with RSP or R12 is converted to ADD instead of LEA
717   // because LEA with these registers needs 4 bytes to encode and thus
718   // wouldn't fit the space.
719 
720   if (memcmp(Inst, "\x48\x03\x25", 3) == 0) {
721     // "addq foo@gottpoff(%rip),%rsp" -> "addq $foo,%rsp"
722     memcpy(Inst, "\x48\x81\xc4", 3);
723   } else if (memcmp(Inst, "\x4c\x03\x25", 3) == 0) {
724     // "addq foo@gottpoff(%rip),%r12" -> "addq $foo,%r12"
725     memcpy(Inst, "\x49\x81\xc4", 3);
726   } else if (memcmp(Inst, "\x4c\x03", 2) == 0) {
727     // "addq foo@gottpoff(%rip),%r[8-15]" -> "leaq foo(%r[8-15]),%r[8-15]"
728     memcpy(Inst, "\x4d\x8d", 2);
729     *RegSlot = 0x80 | (Reg << 3) | Reg;
730   } else if (memcmp(Inst, "\x48\x03", 2) == 0) {
731     // "addq foo@gottpoff(%rip),%reg -> "leaq foo(%reg),%reg"
732     memcpy(Inst, "\x48\x8d", 2);
733     *RegSlot = 0x80 | (Reg << 3) | Reg;
734   } else if (memcmp(Inst, "\x4c\x8b", 2) == 0) {
735     // "movq foo@gottpoff(%rip),%r[8-15]" -> "movq $foo,%r[8-15]"
736     memcpy(Inst, "\x49\xc7", 2);
737     *RegSlot = 0xc0 | Reg;
738   } else if (memcmp(Inst, "\x48\x8b", 2) == 0) {
739     // "movq foo@gottpoff(%rip),%reg" -> "movq $foo,%reg"
740     memcpy(Inst, "\x48\xc7", 2);
741     *RegSlot = 0xc0 | Reg;
742   } else {
743     fatal("R_X86_64_GOTTPOFF must be used in MOVQ or ADDQ instructions only");
744   }
745 
746   // The original code used a PC relative relocation.
747   // Need to compensate for the -4 it had in the addend.
748   relocateOne(Loc, R_X86_64_TPOFF32, Val + 4);
749 }
750 
751 template <class ELFT>
752 void X86_64TargetInfo<ELFT>::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
753                                             uint64_t Val) const {
754   // Convert
755   //   leaq bar@tlsld(%rip), %rdi
756   //   callq __tls_get_addr@PLT
757   //   leaq bar@dtpoff(%rax), %rcx
758   // to
759   //   .word 0x6666
760   //   .byte 0x66
761   //   mov %fs:0,%rax
762   //   leaq bar@tpoff(%rax), %rcx
763   if (Type == R_X86_64_DTPOFF64) {
764     write64le(Loc, Val);
765     return;
766   }
767   if (Type == R_X86_64_DTPOFF32) {
768     relocateOne(Loc, R_X86_64_TPOFF32, Val);
769     return;
770   }
771 
772   const uint8_t Inst[] = {
773       0x66, 0x66,                                          // .word 0x6666
774       0x66,                                                // .byte 0x66
775       0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
776   };
777   memcpy(Loc - 3, Inst, sizeof(Inst));
778 }
779 
780 template <class ELFT>
781 void X86_64TargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
782                                          uint64_t Val) const {
783   switch (Type) {
784   case R_X86_64_32:
785     checkUInt<32>(Val, Type);
786     write32le(Loc, Val);
787     break;
788   case R_X86_64_32S:
789   case R_X86_64_TPOFF32:
790   case R_X86_64_GOT32:
791   case R_X86_64_GOTPCREL:
792   case R_X86_64_GOTPCRELX:
793   case R_X86_64_REX_GOTPCRELX:
794   case R_X86_64_PC32:
795   case R_X86_64_GOTTPOFF:
796   case R_X86_64_PLT32:
797   case R_X86_64_TLSGD:
798   case R_X86_64_TLSLD:
799   case R_X86_64_DTPOFF32:
800   case R_X86_64_SIZE32:
801     checkInt<32>(Val, Type);
802     write32le(Loc, Val);
803     break;
804   case R_X86_64_64:
805   case R_X86_64_DTPOFF64:
806   case R_X86_64_SIZE64:
807   case R_X86_64_PC64:
808     write64le(Loc, Val);
809     break;
810   default:
811     fatal("unrecognized reloc " + Twine(Type));
812   }
813 }
814 
815 template <class ELFT>
816 RelExpr X86_64TargetInfo<ELFT>::adjustRelaxExpr(uint32_t Type,
817                                                 const uint8_t *Data,
818                                                 RelExpr RelExpr) const {
819   if (Type != R_X86_64_GOTPCRELX && Type != R_X86_64_REX_GOTPCRELX)
820     return RelExpr;
821   const uint8_t Op = Data[-2];
822   const uint8_t ModRm = Data[-1];
823   // FIXME: When PIC is disabled and foo is defined locally in the
824   // lower 32 bit address space, memory operand in mov can be converted into
825   // immediate operand. Otherwise, mov must be changed to lea. We support only
826   // latter relaxation at this moment.
827   if (Op == 0x8b)
828     return R_RELAX_GOT_PC;
829   // Relax call and jmp.
830   if (Op == 0xff && (ModRm == 0x15 || ModRm == 0x25))
831     return R_RELAX_GOT_PC;
832 
833   // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor.
834   // If PIC then no relaxation is available.
835   // We also don't relax test/binop instructions without REX byte,
836   // they are 32bit operations and not common to have.
837   assert(Type == R_X86_64_REX_GOTPCRELX);
838   return Config->Pic ? RelExpr : R_RELAX_GOT_PC_NOPIC;
839 }
840 
841 // A subset of relaxations can only be applied for no-PIC. This method
842 // handles such relaxations. Instructions encoding information was taken from:
843 // "Intel 64 and IA-32 Architectures Software Developer's Manual V2"
844 // (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
845 //    64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
846 template <class ELFT>
847 void X86_64TargetInfo<ELFT>::relaxGotNoPic(uint8_t *Loc, uint64_t Val,
848                                            uint8_t Op, uint8_t ModRm) const {
849   const uint8_t Rex = Loc[-3];
850   // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg".
851   if (Op == 0x85) {
852     // See "TEST-Logical Compare" (4-428 Vol. 2B),
853     // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension).
854 
855     // ModR/M byte has form XX YYY ZZZ, where
856     // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1).
857     // XX has different meanings:
858     // 00: The operand's memory address is in reg1.
859     // 01: The operand's memory address is reg1 + a byte-sized displacement.
860     // 10: The operand's memory address is reg1 + a word-sized displacement.
861     // 11: The operand is reg1 itself.
862     // If an instruction requires only one operand, the unused reg2 field
863     // holds extra opcode bits rather than a register code
864     // 0xC0 == 11 000 000 binary.
865     // 0x38 == 00 111 000 binary.
866     // We transfer reg2 to reg1 here as operand.
867     // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3).
868     Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3; // ModR/M byte.
869 
870     // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32
871     // See "TEST-Logical Compare" (4-428 Vol. 2B).
872     Loc[-2] = 0xf7;
873 
874     // Move R bit to the B bit in REX byte.
875     // REX byte is encoded as 0100WRXB, where
876     // 0100 is 4bit fixed pattern.
877     // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the
878     //   default operand size is used (which is 32-bit for most but not all
879     //   instructions).
880     // REX.R This 1-bit value is an extension to the MODRM.reg field.
881     // REX.X This 1-bit value is an extension to the SIB.index field.
882     // REX.B This 1-bit value is an extension to the MODRM.rm field or the
883     // SIB.base field.
884     // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A).
885     Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2;
886     relocateOne(Loc, R_X86_64_PC32, Val);
887     return;
888   }
889 
890   // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub
891   // or xor operations.
892 
893   // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg".
894   // Logic is close to one for test instruction above, but we also
895   // write opcode extension here, see below for details.
896   Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3 | (Op & 0x3c); // ModR/M byte.
897 
898   // Primary opcode is 0x81, opcode extension is one of:
899   // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB,
900   // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP.
901   // This value was wrote to MODRM.reg in a line above.
902   // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15),
903   // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for
904   // descriptions about each operation.
905   Loc[-2] = 0x81;
906   Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2;
907   relocateOne(Loc, R_X86_64_PC32, Val);
908 }
909 
910 template <class ELFT>
911 void X86_64TargetInfo<ELFT>::relaxGot(uint8_t *Loc, uint64_t Val) const {
912   const uint8_t Op = Loc[-2];
913   const uint8_t ModRm = Loc[-1];
914 
915   // Convert "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
916   if (Op == 0x8b) {
917     Loc[-2] = 0x8d;
918     relocateOne(Loc, R_X86_64_PC32, Val);
919     return;
920   }
921 
922   if (Op != 0xff) {
923     // We are relaxing a rip relative to an absolute, so compensate
924     // for the old -4 addend.
925     assert(!Config->Pic);
926     relaxGotNoPic(Loc, Val + 4, Op, ModRm);
927     return;
928   }
929 
930   // Convert call/jmp instructions.
931   if (ModRm == 0x15) {
932     // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call foo".
933     // Instead we convert to "addr32 call foo" where addr32 is an instruction
934     // prefix. That makes result expression to be a single instruction.
935     Loc[-2] = 0x67; // addr32 prefix
936     Loc[-1] = 0xe8; // call
937     relocateOne(Loc, R_X86_64_PC32, Val);
938     return;
939   }
940 
941   // Convert "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop".
942   // jmp doesn't return, so it is fine to use nop here, it is just a stub.
943   assert(ModRm == 0x25);
944   Loc[-2] = 0xe9; // jmp
945   Loc[3] = 0x90;  // nop
946   relocateOne(Loc - 1, R_X86_64_PC32, Val + 1);
947 }
948 
949 // Relocation masks following the #lo(value), #hi(value), #ha(value),
950 // #higher(value), #highera(value), #highest(value), and #highesta(value)
951 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
952 // document.
953 static uint16_t applyPPCLo(uint64_t V) { return V; }
954 static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
955 static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
956 static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
957 static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
958 static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
959 static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
960 
961 PPCTargetInfo::PPCTargetInfo() {}
962 
963 void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
964                                 uint64_t Val) const {
965   switch (Type) {
966   case R_PPC_ADDR16_HA:
967     write16be(Loc, applyPPCHa(Val));
968     break;
969   case R_PPC_ADDR16_LO:
970     write16be(Loc, applyPPCLo(Val));
971     break;
972   default:
973     fatal("unrecognized reloc " + Twine(Type));
974   }
975 }
976 
977 RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
978   return R_ABS;
979 }
980 
981 PPC64TargetInfo::PPC64TargetInfo() {
982   PltRel = GotRel = R_PPC64_GLOB_DAT;
983   RelativeRel = R_PPC64_RELATIVE;
984   GotEntrySize = 8;
985   GotPltEntrySize = 8;
986   PltEntrySize = 32;
987   PltHeaderSize = 0;
988 
989   // We need 64K pages (at least under glibc/Linux, the loader won't
990   // set different permissions on a finer granularity than that).
991   MaxPageSize = 65536;
992 
993   // The PPC64 ELF ABI v1 spec, says:
994   //
995   //   It is normally desirable to put segments with different characteristics
996   //   in separate 256 Mbyte portions of the address space, to give the
997   //   operating system full paging flexibility in the 64-bit address space.
998   //
999   // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1000   // use 0x10000000 as the starting address.
1001   DefaultImageBase = 0x10000000;
1002 }
1003 
1004 static uint64_t PPC64TocOffset = 0x8000;
1005 
1006 uint64_t getPPC64TocBase() {
1007   // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
1008   // TOC starts where the first of these sections starts. We always create a
1009   // .got when we see a relocation that uses it, so for us the start is always
1010   // the .got.
1011   uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1012 
1013   // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1014   // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1015   // code (crt1.o) assumes that you can get from the TOC base to the
1016   // start of the .toc section with only a single (signed) 16-bit relocation.
1017   return TocVA + PPC64TocOffset;
1018 }
1019 
1020 RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
1021   switch (Type) {
1022   default:
1023     return R_ABS;
1024   case R_PPC64_TOC16:
1025   case R_PPC64_TOC16_DS:
1026   case R_PPC64_TOC16_HA:
1027   case R_PPC64_TOC16_HI:
1028   case R_PPC64_TOC16_LO:
1029   case R_PPC64_TOC16_LO_DS:
1030     return R_GOTREL;
1031   case R_PPC64_TOC:
1032     return R_PPC_TOC;
1033   case R_PPC64_REL24:
1034     return R_PPC_PLT_OPD;
1035   }
1036 }
1037 
1038 void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1039                                uint64_t PltEntryAddr, int32_t Index,
1040                                unsigned RelOff) const {
1041   uint64_t Off = GotEntryAddr - getPPC64TocBase();
1042 
1043   // FIXME: What we should do, in theory, is get the offset of the function
1044   // descriptor in the .opd section, and use that as the offset from %r2 (the
1045   // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1046   // be a pointer to the function descriptor in the .opd section. Using
1047   // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1048 
1049   write32be(Buf,      0xf8410028);                   // std %r2, 40(%r1)
1050   write32be(Buf + 4,  0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1051   write32be(Buf + 8,  0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1052   write32be(Buf + 12, 0xe96c0000);                   // ld %r11,0(%r12)
1053   write32be(Buf + 16, 0x7d6903a6);                   // mtctr %r11
1054   write32be(Buf + 20, 0xe84c0008);                   // ld %r2,8(%r12)
1055   write32be(Buf + 24, 0xe96c0010);                   // ld %r11,16(%r12)
1056   write32be(Buf + 28, 0x4e800420);                   // bctr
1057 }
1058 
1059 static std::pair<uint32_t, uint64_t> toAddr16Rel(uint32_t Type, uint64_t Val) {
1060   uint64_t V = Val - PPC64TocOffset;
1061   switch (Type) {
1062   case R_PPC64_TOC16: return {R_PPC64_ADDR16, V};
1063   case R_PPC64_TOC16_DS: return {R_PPC64_ADDR16_DS, V};
1064   case R_PPC64_TOC16_HA: return {R_PPC64_ADDR16_HA, V};
1065   case R_PPC64_TOC16_HI: return {R_PPC64_ADDR16_HI, V};
1066   case R_PPC64_TOC16_LO: return {R_PPC64_ADDR16_LO, V};
1067   case R_PPC64_TOC16_LO_DS: return {R_PPC64_ADDR16_LO_DS, V};
1068   default: return {Type, Val};
1069   }
1070 }
1071 
1072 void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1073                                   uint64_t Val) const {
1074   // For a TOC-relative relocation, proceed in terms of the corresponding
1075   // ADDR16 relocation type.
1076   std::tie(Type, Val) = toAddr16Rel(Type, Val);
1077 
1078   switch (Type) {
1079   case R_PPC64_ADDR14: {
1080     checkAlignment<4>(Val, Type);
1081     // Preserve the AA/LK bits in the branch instruction
1082     uint8_t AALK = Loc[3];
1083     write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
1084     break;
1085   }
1086   case R_PPC64_ADDR16:
1087     checkInt<16>(Val, Type);
1088     write16be(Loc, Val);
1089     break;
1090   case R_PPC64_ADDR16_DS:
1091     checkInt<16>(Val, Type);
1092     write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
1093     break;
1094   case R_PPC64_ADDR16_HA:
1095   case R_PPC64_REL16_HA:
1096     write16be(Loc, applyPPCHa(Val));
1097     break;
1098   case R_PPC64_ADDR16_HI:
1099   case R_PPC64_REL16_HI:
1100     write16be(Loc, applyPPCHi(Val));
1101     break;
1102   case R_PPC64_ADDR16_HIGHER:
1103     write16be(Loc, applyPPCHigher(Val));
1104     break;
1105   case R_PPC64_ADDR16_HIGHERA:
1106     write16be(Loc, applyPPCHighera(Val));
1107     break;
1108   case R_PPC64_ADDR16_HIGHEST:
1109     write16be(Loc, applyPPCHighest(Val));
1110     break;
1111   case R_PPC64_ADDR16_HIGHESTA:
1112     write16be(Loc, applyPPCHighesta(Val));
1113     break;
1114   case R_PPC64_ADDR16_LO:
1115     write16be(Loc, applyPPCLo(Val));
1116     break;
1117   case R_PPC64_ADDR16_LO_DS:
1118   case R_PPC64_REL16_LO:
1119     write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
1120     break;
1121   case R_PPC64_ADDR32:
1122   case R_PPC64_REL32:
1123     checkInt<32>(Val, Type);
1124     write32be(Loc, Val);
1125     break;
1126   case R_PPC64_ADDR64:
1127   case R_PPC64_REL64:
1128   case R_PPC64_TOC:
1129     write64be(Loc, Val);
1130     break;
1131   case R_PPC64_REL24: {
1132     uint32_t Mask = 0x03FFFFFC;
1133     checkInt<24>(Val, Type);
1134     write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
1135     break;
1136   }
1137   default:
1138     fatal("unrecognized reloc " + Twine(Type));
1139   }
1140 }
1141 
1142 AArch64TargetInfo::AArch64TargetInfo() {
1143   CopyRel = R_AARCH64_COPY;
1144   RelativeRel = R_AARCH64_RELATIVE;
1145   IRelativeRel = R_AARCH64_IRELATIVE;
1146   GotRel = R_AARCH64_GLOB_DAT;
1147   PltRel = R_AARCH64_JUMP_SLOT;
1148   TlsDescRel = R_AARCH64_TLSDESC;
1149   TlsGotRel = R_AARCH64_TLS_TPREL64;
1150   GotEntrySize = 8;
1151   GotPltEntrySize = 8;
1152   PltEntrySize = 16;
1153   PltHeaderSize = 32;
1154   MaxPageSize = 65536;
1155 
1156   // It doesn't seem to be documented anywhere, but tls on aarch64 uses variant
1157   // 1 of the tls structures and the tcb size is 16.
1158   TcbSize = 16;
1159 }
1160 
1161 RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
1162                                       const SymbolBody &S) const {
1163   switch (Type) {
1164   default:
1165     return R_ABS;
1166   case R_AARCH64_TLSDESC_ADR_PAGE21:
1167     return R_TLSDESC_PAGE;
1168   case R_AARCH64_TLSDESC_LD64_LO12_NC:
1169   case R_AARCH64_TLSDESC_ADD_LO12_NC:
1170     return R_TLSDESC;
1171   case R_AARCH64_TLSDESC_CALL:
1172     return R_HINT;
1173   case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1174   case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1175     return R_TLS;
1176   case R_AARCH64_CALL26:
1177   case R_AARCH64_CONDBR19:
1178   case R_AARCH64_JUMP26:
1179   case R_AARCH64_TSTBR14:
1180     return R_PLT_PC;
1181   case R_AARCH64_PREL16:
1182   case R_AARCH64_PREL32:
1183   case R_AARCH64_PREL64:
1184   case R_AARCH64_ADR_PREL_LO21:
1185     return R_PC;
1186   case R_AARCH64_ADR_PREL_PG_HI21:
1187     return R_PAGE_PC;
1188   case R_AARCH64_LD64_GOT_LO12_NC:
1189   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1190     return R_GOT;
1191   case R_AARCH64_ADR_GOT_PAGE:
1192   case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1193     return R_GOT_PAGE_PC;
1194   }
1195 }
1196 
1197 RelExpr AArch64TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
1198                                            RelExpr Expr) const {
1199   if (Expr == R_RELAX_TLS_GD_TO_IE) {
1200     if (Type == R_AARCH64_TLSDESC_ADR_PAGE21)
1201       return R_RELAX_TLS_GD_TO_IE_PAGE_PC;
1202     return R_RELAX_TLS_GD_TO_IE_ABS;
1203   }
1204   return Expr;
1205 }
1206 
1207 bool AArch64TargetInfo::usesOnlyLowPageBits(uint32_t Type) const {
1208   switch (Type) {
1209   default:
1210     return false;
1211   case R_AARCH64_ADD_ABS_LO12_NC:
1212   case R_AARCH64_LD64_GOT_LO12_NC:
1213   case R_AARCH64_LDST128_ABS_LO12_NC:
1214   case R_AARCH64_LDST16_ABS_LO12_NC:
1215   case R_AARCH64_LDST32_ABS_LO12_NC:
1216   case R_AARCH64_LDST64_ABS_LO12_NC:
1217   case R_AARCH64_LDST8_ABS_LO12_NC:
1218   case R_AARCH64_TLSDESC_ADD_LO12_NC:
1219   case R_AARCH64_TLSDESC_LD64_LO12_NC:
1220   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1221     return true;
1222   }
1223 }
1224 
1225 bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
1226   return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1227          Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1228 }
1229 
1230 uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
1231   if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1232     return Type;
1233   // Keep it going with a dummy value so that we can find more reloc errors.
1234   errorDynRel(Type);
1235   return R_AARCH64_ABS32;
1236 }
1237 
1238 void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, const SymbolBody &) const {
1239   write64le(Buf, Out<ELF64LE>::Plt->getVA());
1240 }
1241 
1242 static uint64_t getAArch64Page(uint64_t Expr) {
1243   return Expr & (~static_cast<uint64_t>(0xFFF));
1244 }
1245 
1246 void AArch64TargetInfo::writePltHeader(uint8_t *Buf) const {
1247   const uint8_t PltData[] = {
1248       0xf0, 0x7b, 0xbf, 0xa9, // stp	x16, x30, [sp,#-16]!
1249       0x10, 0x00, 0x00, 0x90, // adrp	x16, Page(&(.plt.got[2]))
1250       0x11, 0x02, 0x40, 0xf9, // ldr	x17, [x16, Offset(&(.plt.got[2]))]
1251       0x10, 0x02, 0x00, 0x91, // add	x16, x16, Offset(&(.plt.got[2]))
1252       0x20, 0x02, 0x1f, 0xd6, // br	x17
1253       0x1f, 0x20, 0x03, 0xd5, // nop
1254       0x1f, 0x20, 0x03, 0xd5, // nop
1255       0x1f, 0x20, 0x03, 0xd5  // nop
1256   };
1257   memcpy(Buf, PltData, sizeof(PltData));
1258 
1259   uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1260   uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1261   relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1262               getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1263   relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1264   relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
1265 }
1266 
1267 void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1268                                  uint64_t PltEntryAddr, int32_t Index,
1269                                  unsigned RelOff) const {
1270   const uint8_t Inst[] = {
1271       0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1272       0x11, 0x02, 0x40, 0xf9, // ldr  x17, [x16, Offset(&(.plt.got[n]))]
1273       0x10, 0x02, 0x00, 0x91, // add  x16, x16, Offset(&(.plt.got[n]))
1274       0x20, 0x02, 0x1f, 0xd6  // br   x17
1275   };
1276   memcpy(Buf, Inst, sizeof(Inst));
1277 
1278   relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1279               getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1280   relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1281   relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
1282 }
1283 
1284 static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
1285   uint32_t ImmLo = (Imm & 0x3) << 29;
1286   uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
1287   uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
1288   write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
1289 }
1290 
1291 static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1292   or32le(L, (Imm & 0xFFF) << 10);
1293 }
1294 
1295 void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1296                                     uint64_t Val) const {
1297   switch (Type) {
1298   case R_AARCH64_ABS16:
1299   case R_AARCH64_PREL16:
1300     checkIntUInt<16>(Val, Type);
1301     write16le(Loc, Val);
1302     break;
1303   case R_AARCH64_ABS32:
1304   case R_AARCH64_PREL32:
1305     checkIntUInt<32>(Val, Type);
1306     write32le(Loc, Val);
1307     break;
1308   case R_AARCH64_ABS64:
1309   case R_AARCH64_PREL64:
1310     write64le(Loc, Val);
1311     break;
1312   case R_AARCH64_ADD_ABS_LO12_NC:
1313     // This relocation stores 12 bits and there's no instruction
1314     // to do it. Instead, we do a 32 bits store of the value
1315     // of r_addend bitwise-or'ed Loc. This assumes that the addend
1316     // bits in Loc are zero.
1317     or32le(Loc, (Val & 0xFFF) << 10);
1318     break;
1319   case R_AARCH64_ADR_GOT_PAGE:
1320   case R_AARCH64_ADR_PREL_PG_HI21:
1321   case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1322   case R_AARCH64_TLSDESC_ADR_PAGE21:
1323     checkInt<33>(Val, Type);
1324     updateAArch64Addr(Loc, Val >> 12);
1325     break;
1326   case R_AARCH64_ADR_PREL_LO21:
1327     checkInt<21>(Val, Type);
1328     updateAArch64Addr(Loc, Val);
1329     break;
1330   case R_AARCH64_CALL26:
1331   case R_AARCH64_JUMP26:
1332     checkInt<28>(Val, Type);
1333     or32le(Loc, (Val & 0x0FFFFFFC) >> 2);
1334     break;
1335   case R_AARCH64_CONDBR19:
1336     checkInt<21>(Val, Type);
1337     or32le(Loc, (Val & 0x1FFFFC) << 3);
1338     break;
1339   case R_AARCH64_LD64_GOT_LO12_NC:
1340   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1341   case R_AARCH64_TLSDESC_LD64_LO12_NC:
1342     checkAlignment<8>(Val, Type);
1343     or32le(Loc, (Val & 0xFF8) << 7);
1344     break;
1345   case R_AARCH64_LDST128_ABS_LO12_NC:
1346     or32le(Loc, (Val & 0x0FF8) << 6);
1347     break;
1348   case R_AARCH64_LDST16_ABS_LO12_NC:
1349     or32le(Loc, (Val & 0x0FFC) << 9);
1350     break;
1351   case R_AARCH64_LDST8_ABS_LO12_NC:
1352     or32le(Loc, (Val & 0xFFF) << 10);
1353     break;
1354   case R_AARCH64_LDST32_ABS_LO12_NC:
1355     or32le(Loc, (Val & 0xFFC) << 8);
1356     break;
1357   case R_AARCH64_LDST64_ABS_LO12_NC:
1358     or32le(Loc, (Val & 0xFF8) << 7);
1359     break;
1360   case R_AARCH64_MOVW_UABS_G0_NC:
1361     or32le(Loc, (Val & 0xFFFF) << 5);
1362     break;
1363   case R_AARCH64_MOVW_UABS_G1_NC:
1364     or32le(Loc, (Val & 0xFFFF0000) >> 11);
1365     break;
1366   case R_AARCH64_MOVW_UABS_G2_NC:
1367     or32le(Loc, (Val & 0xFFFF00000000) >> 27);
1368     break;
1369   case R_AARCH64_MOVW_UABS_G3:
1370     or32le(Loc, (Val & 0xFFFF000000000000) >> 43);
1371     break;
1372   case R_AARCH64_TSTBR14:
1373     checkInt<16>(Val, Type);
1374     or32le(Loc, (Val & 0xFFFC) << 3);
1375     break;
1376   case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1377     checkInt<24>(Val, Type);
1378     updateAArch64Add(Loc, Val >> 12);
1379     break;
1380   case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1381   case R_AARCH64_TLSDESC_ADD_LO12_NC:
1382     updateAArch64Add(Loc, Val);
1383     break;
1384   default:
1385     fatal("unrecognized reloc " + Twine(Type));
1386   }
1387 }
1388 
1389 void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1390                                        uint64_t Val) const {
1391   // TLSDESC Global-Dynamic relocation are in the form:
1392   //   adrp    x0, :tlsdesc:v             [R_AARCH64_TLSDESC_ADR_PAGE21]
1393   //   ldr     x1, [x0, #:tlsdesc_lo12:v  [R_AARCH64_TLSDESC_LD64_LO12_NC]
1394   //   add     x0, x0, :tlsdesc_los:v     [_AARCH64_TLSDESC_ADD_LO12_NC]
1395   //   .tlsdesccall                       [R_AARCH64_TLSDESC_CALL]
1396   //   blr     x1
1397   // And it can optimized to:
1398   //   movz    x0, #0x0, lsl #16
1399   //   movk    x0, #0x10
1400   //   nop
1401   //   nop
1402   checkUInt<32>(Val, Type);
1403 
1404   switch (Type) {
1405   case R_AARCH64_TLSDESC_ADD_LO12_NC:
1406   case R_AARCH64_TLSDESC_CALL:
1407     write32le(Loc, 0xd503201f); // nop
1408     return;
1409   case R_AARCH64_TLSDESC_ADR_PAGE21:
1410     write32le(Loc, 0xd2a00000 | (((Val >> 16) & 0xffff) << 5)); // movz
1411     return;
1412   case R_AARCH64_TLSDESC_LD64_LO12_NC:
1413     write32le(Loc, 0xf2800000 | ((Val & 0xffff) << 5)); // movk
1414     return;
1415   default:
1416     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
1417   }
1418 }
1419 
1420 void AArch64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
1421                                        uint64_t Val) const {
1422   // TLSDESC Global-Dynamic relocation are in the form:
1423   //   adrp    x0, :tlsdesc:v             [R_AARCH64_TLSDESC_ADR_PAGE21]
1424   //   ldr     x1, [x0, #:tlsdesc_lo12:v  [R_AARCH64_TLSDESC_LD64_LO12_NC]
1425   //   add     x0, x0, :tlsdesc_los:v     [_AARCH64_TLSDESC_ADD_LO12_NC]
1426   //   .tlsdesccall                       [R_AARCH64_TLSDESC_CALL]
1427   //   blr     x1
1428   // And it can optimized to:
1429   //   adrp    x0, :gottprel:v
1430   //   ldr     x0, [x0, :gottprel_lo12:v]
1431   //   nop
1432   //   nop
1433 
1434   switch (Type) {
1435   case R_AARCH64_TLSDESC_ADD_LO12_NC:
1436   case R_AARCH64_TLSDESC_CALL:
1437     write32le(Loc, 0xd503201f); // nop
1438     break;
1439   case R_AARCH64_TLSDESC_ADR_PAGE21:
1440     write32le(Loc, 0x90000000); // adrp
1441     relocateOne(Loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, Val);
1442     break;
1443   case R_AARCH64_TLSDESC_LD64_LO12_NC:
1444     write32le(Loc, 0xf9400000); // ldr
1445     relocateOne(Loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, Val);
1446     break;
1447   default:
1448     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
1449   }
1450 }
1451 
1452 void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1453                                        uint64_t Val) const {
1454   checkUInt<32>(Val, Type);
1455 
1456   if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1457     // Generate MOVZ.
1458     uint32_t RegNo = read32le(Loc) & 0x1f;
1459     write32le(Loc, (0xd2a00000 | RegNo) | (((Val >> 16) & 0xffff) << 5));
1460     return;
1461   }
1462   if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1463     // Generate MOVK.
1464     uint32_t RegNo = read32le(Loc) & 0x1f;
1465     write32le(Loc, (0xf2800000 | RegNo) | ((Val & 0xffff) << 5));
1466     return;
1467   }
1468   llvm_unreachable("invalid relocation for TLS IE to LE relaxation");
1469 }
1470 
1471 AMDGPUTargetInfo::AMDGPUTargetInfo() {
1472   RelativeRel = R_AMDGPU_REL64;
1473   GotRel = R_AMDGPU_ABS64;
1474   GotEntrySize = 8;
1475 }
1476 
1477 void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1478                                    uint64_t Val) const {
1479   switch (Type) {
1480   case R_AMDGPU_ABS32:
1481   case R_AMDGPU_GOTPCREL:
1482   case R_AMDGPU_GOTPCREL32_LO:
1483   case R_AMDGPU_REL32:
1484   case R_AMDGPU_REL32_LO:
1485     write32le(Loc, Val);
1486     break;
1487   case R_AMDGPU_GOTPCREL32_HI:
1488   case R_AMDGPU_REL32_HI:
1489     write32le(Loc, Val >> 32);
1490     break;
1491   default:
1492     fatal("unrecognized reloc " + Twine(Type));
1493   }
1494 }
1495 
1496 RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
1497   switch (Type) {
1498   case R_AMDGPU_ABS32:
1499     return R_ABS;
1500   case R_AMDGPU_REL32:
1501   case R_AMDGPU_REL32_LO:
1502   case R_AMDGPU_REL32_HI:
1503     return R_PC;
1504   case R_AMDGPU_GOTPCREL:
1505   case R_AMDGPU_GOTPCREL32_LO:
1506   case R_AMDGPU_GOTPCREL32_HI:
1507     return R_GOT_PC;
1508   default:
1509     fatal("do not know how to handle relocation " + Twine(Type));
1510   }
1511 }
1512 
1513 ARMTargetInfo::ARMTargetInfo() {
1514   CopyRel = R_ARM_COPY;
1515   RelativeRel = R_ARM_RELATIVE;
1516   IRelativeRel = R_ARM_IRELATIVE;
1517   GotRel = R_ARM_GLOB_DAT;
1518   PltRel = R_ARM_JUMP_SLOT;
1519   TlsGotRel = R_ARM_TLS_TPOFF32;
1520   TlsModuleIndexRel = R_ARM_TLS_DTPMOD32;
1521   TlsOffsetRel = R_ARM_TLS_DTPOFF32;
1522   GotEntrySize = 4;
1523   GotPltEntrySize = 4;
1524   PltEntrySize = 16;
1525   PltHeaderSize = 20;
1526   // ARM uses Variant 1 TLS
1527   TcbSize = 8;
1528   NeedsThunks = true;
1529 }
1530 
1531 RelExpr ARMTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
1532   switch (Type) {
1533   default:
1534     return R_ABS;
1535   case R_ARM_THM_JUMP11:
1536     return R_PC;
1537   case R_ARM_CALL:
1538   case R_ARM_JUMP24:
1539   case R_ARM_PC24:
1540   case R_ARM_PLT32:
1541   case R_ARM_THM_JUMP19:
1542   case R_ARM_THM_JUMP24:
1543   case R_ARM_THM_CALL:
1544     return R_PLT_PC;
1545   case R_ARM_GOTOFF32:
1546     // (S + A) - GOT_ORG
1547     return R_GOTREL;
1548   case R_ARM_GOT_BREL:
1549     // GOT(S) + A - GOT_ORG
1550     return R_GOT_OFF;
1551   case R_ARM_GOT_PREL:
1552   case R_ARM_TLS_IE32:
1553     // GOT(S) + A - P
1554     return R_GOT_PC;
1555   case R_ARM_TARGET1:
1556     return Config->Target1Rel ? R_PC : R_ABS;
1557   case R_ARM_TLS_GD32:
1558     return R_TLSGD_PC;
1559   case R_ARM_TLS_LDM32:
1560     return R_TLSLD_PC;
1561   case R_ARM_BASE_PREL:
1562     // B(S) + A - P
1563     // FIXME: currently B(S) assumed to be .got, this may not hold for all
1564     // platforms.
1565     return R_GOTONLY_PC;
1566   case R_ARM_MOVW_PREL_NC:
1567   case R_ARM_MOVT_PREL:
1568   case R_ARM_PREL31:
1569   case R_ARM_REL32:
1570   case R_ARM_THM_MOVW_PREL_NC:
1571   case R_ARM_THM_MOVT_PREL:
1572     return R_PC;
1573   case R_ARM_TLS_LE32:
1574     return R_TLS;
1575   }
1576 }
1577 
1578 uint32_t ARMTargetInfo::getDynRel(uint32_t Type) const {
1579   if (Type == R_ARM_TARGET1 && !Config->Target1Rel)
1580     return R_ARM_ABS32;
1581   if (Type == R_ARM_ABS32)
1582     return Type;
1583   // Keep it going with a dummy value so that we can find more reloc errors.
1584   errorDynRel(Type);
1585   return R_ARM_ABS32;
1586 }
1587 
1588 void ARMTargetInfo::writeGotPlt(uint8_t *Buf, const SymbolBody &) const {
1589   write32le(Buf, Out<ELF32LE>::Plt->getVA());
1590 }
1591 
1592 void ARMTargetInfo::writePltHeader(uint8_t *Buf) const {
1593   const uint8_t PltData[] = {
1594       0x04, 0xe0, 0x2d, 0xe5, //     str lr, [sp,#-4]!
1595       0x04, 0xe0, 0x9f, 0xe5, //     ldr lr, L2
1596       0x0e, 0xe0, 0x8f, 0xe0, // L1: add lr, pc, lr
1597       0x08, 0xf0, 0xbe, 0xe5, //     ldr pc, [lr, #8]
1598       0x00, 0x00, 0x00, 0x00, // L2: .word   &(.got.plt) - L1 - 8
1599   };
1600   memcpy(Buf, PltData, sizeof(PltData));
1601   uint64_t GotPlt = Out<ELF32LE>::GotPlt->getVA();
1602   uint64_t L1 = Out<ELF32LE>::Plt->getVA() + 8;
1603   write32le(Buf + 16, GotPlt - L1 - 8);
1604 }
1605 
1606 void ARMTargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1607                              uint64_t PltEntryAddr, int32_t Index,
1608                              unsigned RelOff) const {
1609   // FIXME: Using simple code sequence with simple relocations.
1610   // There is a more optimal sequence but it requires support for the group
1611   // relocations. See ELF for the ARM Architecture Appendix A.3
1612   const uint8_t PltData[] = {
1613       0x04, 0xc0, 0x9f, 0xe5, //     ldr ip, L2
1614       0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
1615       0x00, 0xf0, 0x9c, 0xe5, //     ldr pc, [ip]
1616       0x00, 0x00, 0x00, 0x00, // L2: .word   Offset(&(.plt.got) - L1 - 8
1617   };
1618   memcpy(Buf, PltData, sizeof(PltData));
1619   uint64_t L1 = PltEntryAddr + 4;
1620   write32le(Buf + 12, GotEntryAddr - L1 - 8);
1621 }
1622 
1623 RelExpr ARMTargetInfo::getThunkExpr(RelExpr Expr, uint32_t RelocType,
1624                                     const InputFile &File,
1625                                     const SymbolBody &S) const {
1626   // A state change from ARM to Thumb and vice versa must go through an
1627   // interworking thunk if the relocation type is not R_ARM_CALL or
1628   // R_ARM_THM_CALL.
1629   switch (RelocType) {
1630   case R_ARM_PC24:
1631   case R_ARM_PLT32:
1632   case R_ARM_JUMP24:
1633     // Source is ARM, all PLT entries are ARM so no interworking required.
1634     // Otherwise we need to interwork if Symbol has bit 0 set (Thumb).
1635     if (Expr == R_PC && ((S.getVA<ELF32LE>() & 1) == 1))
1636       return R_THUNK_PC;
1637     break;
1638   case R_ARM_THM_JUMP19:
1639   case R_ARM_THM_JUMP24:
1640     // Source is Thumb, all PLT entries are ARM so interworking is required.
1641     // Otherwise we need to interwork if Symbol has bit 0 clear (ARM).
1642     if (Expr == R_PLT_PC)
1643       return R_THUNK_PLT_PC;
1644     if ((S.getVA<ELF32LE>() & 1) == 0)
1645       return R_THUNK_PC;
1646     break;
1647   }
1648   return Expr;
1649 }
1650 
1651 void ARMTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1652                                 uint64_t Val) const {
1653   switch (Type) {
1654   case R_ARM_NONE:
1655     break;
1656   case R_ARM_ABS32:
1657   case R_ARM_BASE_PREL:
1658   case R_ARM_GOTOFF32:
1659   case R_ARM_GOT_BREL:
1660   case R_ARM_GOT_PREL:
1661   case R_ARM_REL32:
1662   case R_ARM_TARGET1:
1663   case R_ARM_TLS_GD32:
1664   case R_ARM_TLS_IE32:
1665   case R_ARM_TLS_LDM32:
1666   case R_ARM_TLS_LDO32:
1667   case R_ARM_TLS_LE32:
1668     write32le(Loc, Val);
1669     break;
1670   case R_ARM_PREL31:
1671     checkInt<31>(Val, Type);
1672     write32le(Loc, (read32le(Loc) & 0x80000000) | (Val & ~0x80000000));
1673     break;
1674   case R_ARM_CALL:
1675     // R_ARM_CALL is used for BL and BLX instructions, depending on the
1676     // value of bit 0 of Val, we must select a BL or BLX instruction
1677     if (Val & 1) {
1678       // If bit 0 of Val is 1 the target is Thumb, we must select a BLX.
1679       // The BLX encoding is 0xfa:H:imm24 where Val = imm24:H:'1'
1680       checkInt<26>(Val, Type);
1681       write32le(Loc, 0xfa000000 |                    // opcode
1682                          ((Val & 2) << 23) |         // H
1683                          ((Val >> 2) & 0x00ffffff)); // imm24
1684       break;
1685     }
1686     if ((read32le(Loc) & 0xfe000000) == 0xfa000000)
1687       // BLX (always unconditional) instruction to an ARM Target, select an
1688       // unconditional BL.
1689       write32le(Loc, 0xeb000000 | (read32le(Loc) & 0x00ffffff));
1690     // fall through as BL encoding is shared with B
1691   case R_ARM_JUMP24:
1692   case R_ARM_PC24:
1693   case R_ARM_PLT32:
1694     checkInt<26>(Val, Type);
1695     write32le(Loc, (read32le(Loc) & ~0x00ffffff) | ((Val >> 2) & 0x00ffffff));
1696     break;
1697   case R_ARM_THM_JUMP11:
1698     checkInt<12>(Val, Type);
1699     write16le(Loc, (read32le(Loc) & 0xf800) | ((Val >> 1) & 0x07ff));
1700     break;
1701   case R_ARM_THM_JUMP19:
1702     // Encoding T3: Val = S:J2:J1:imm6:imm11:0
1703     checkInt<21>(Val, Type);
1704     write16le(Loc,
1705               (read16le(Loc) & 0xfbc0) |   // opcode cond
1706                   ((Val >> 10) & 0x0400) | // S
1707                   ((Val >> 12) & 0x003f)); // imm6
1708     write16le(Loc + 2,
1709               0x8000 |                    // opcode
1710                   ((Val >> 8) & 0x0800) | // J2
1711                   ((Val >> 5) & 0x2000) | // J1
1712                   ((Val >> 1) & 0x07ff)); // imm11
1713     break;
1714   case R_ARM_THM_CALL:
1715     // R_ARM_THM_CALL is used for BL and BLX instructions, depending on the
1716     // value of bit 0 of Val, we must select a BL or BLX instruction
1717     if ((Val & 1) == 0) {
1718       // Ensure BLX destination is 4-byte aligned. As BLX instruction may
1719       // only be two byte aligned. This must be done before overflow check
1720       Val = alignTo(Val, 4);
1721     }
1722     // Bit 12 is 0 for BLX, 1 for BL
1723     write16le(Loc + 2, (read16le(Loc + 2) & ~0x1000) | (Val & 1) << 12);
1724     // Fall through as rest of encoding is the same as B.W
1725   case R_ARM_THM_JUMP24:
1726     // Encoding B  T4, BL T1, BLX T2: Val = S:I1:I2:imm10:imm11:0
1727     // FIXME: Use of I1 and I2 require v6T2ops
1728     checkInt<25>(Val, Type);
1729     write16le(Loc,
1730               0xf000 |                     // opcode
1731                   ((Val >> 14) & 0x0400) | // S
1732                   ((Val >> 12) & 0x03ff)); // imm10
1733     write16le(Loc + 2,
1734               (read16le(Loc + 2) & 0xd000) |                  // opcode
1735                   (((~(Val >> 10)) ^ (Val >> 11)) & 0x2000) | // J1
1736                   (((~(Val >> 11)) ^ (Val >> 13)) & 0x0800) | // J2
1737                   ((Val >> 1) & 0x07ff));                     // imm11
1738     break;
1739   case R_ARM_MOVW_ABS_NC:
1740   case R_ARM_MOVW_PREL_NC:
1741     write32le(Loc, (read32le(Loc) & ~0x000f0fff) | ((Val & 0xf000) << 4) |
1742                        (Val & 0x0fff));
1743     break;
1744   case R_ARM_MOVT_ABS:
1745   case R_ARM_MOVT_PREL:
1746     checkInt<32>(Val, Type);
1747     write32le(Loc, (read32le(Loc) & ~0x000f0fff) |
1748                        (((Val >> 16) & 0xf000) << 4) | ((Val >> 16) & 0xfff));
1749     break;
1750   case R_ARM_THM_MOVT_ABS:
1751   case R_ARM_THM_MOVT_PREL:
1752     // Encoding T1: A = imm4:i:imm3:imm8
1753     checkInt<32>(Val, Type);
1754     write16le(Loc,
1755               0xf2c0 |                     // opcode
1756                   ((Val >> 17) & 0x0400) | // i
1757                   ((Val >> 28) & 0x000f)); // imm4
1758     write16le(Loc + 2,
1759               (read16le(Loc + 2) & 0x8f00) | // opcode
1760                   ((Val >> 12) & 0x7000) |   // imm3
1761                   ((Val >> 16) & 0x00ff));   // imm8
1762     break;
1763   case R_ARM_THM_MOVW_ABS_NC:
1764   case R_ARM_THM_MOVW_PREL_NC:
1765     // Encoding T3: A = imm4:i:imm3:imm8
1766     write16le(Loc,
1767               0xf240 |                     // opcode
1768                   ((Val >> 1) & 0x0400) |  // i
1769                   ((Val >> 12) & 0x000f)); // imm4
1770     write16le(Loc + 2,
1771               (read16le(Loc + 2) & 0x8f00) | // opcode
1772                   ((Val << 4) & 0x7000) |    // imm3
1773                   (Val & 0x00ff));           // imm8
1774     break;
1775   default:
1776     fatal("unrecognized reloc " + Twine(Type));
1777   }
1778 }
1779 
1780 uint64_t ARMTargetInfo::getImplicitAddend(const uint8_t *Buf,
1781                                           uint32_t Type) const {
1782   switch (Type) {
1783   default:
1784     return 0;
1785   case R_ARM_ABS32:
1786   case R_ARM_BASE_PREL:
1787   case R_ARM_GOTOFF32:
1788   case R_ARM_GOT_BREL:
1789   case R_ARM_GOT_PREL:
1790   case R_ARM_REL32:
1791   case R_ARM_TARGET1:
1792   case R_ARM_TLS_GD32:
1793   case R_ARM_TLS_LDM32:
1794   case R_ARM_TLS_LDO32:
1795   case R_ARM_TLS_IE32:
1796   case R_ARM_TLS_LE32:
1797     return SignExtend64<32>(read32le(Buf));
1798   case R_ARM_PREL31:
1799     return SignExtend64<31>(read32le(Buf));
1800   case R_ARM_CALL:
1801   case R_ARM_JUMP24:
1802   case R_ARM_PC24:
1803   case R_ARM_PLT32:
1804     return SignExtend64<26>(read32le(Buf) << 2);
1805   case R_ARM_THM_JUMP11:
1806     return SignExtend64<12>(read16le(Buf) << 1);
1807   case R_ARM_THM_JUMP19: {
1808     // Encoding T3: A = S:J2:J1:imm10:imm6:0
1809     uint16_t Hi = read16le(Buf);
1810     uint16_t Lo = read16le(Buf + 2);
1811     return SignExtend64<20>(((Hi & 0x0400) << 10) | // S
1812                             ((Lo & 0x0800) << 8) |  // J2
1813                             ((Lo & 0x2000) << 5) |  // J1
1814                             ((Hi & 0x003f) << 12) | // imm6
1815                             ((Lo & 0x07ff) << 1));  // imm11:0
1816   }
1817   case R_ARM_THM_CALL:
1818   case R_ARM_THM_JUMP24: {
1819     // Encoding B T4, BL T1, BLX T2: A = S:I1:I2:imm10:imm11:0
1820     // I1 = NOT(J1 EOR S), I2 = NOT(J2 EOR S)
1821     // FIXME: I1 and I2 require v6T2ops
1822     uint16_t Hi = read16le(Buf);
1823     uint16_t Lo = read16le(Buf + 2);
1824     return SignExtend64<24>(((Hi & 0x0400) << 14) |                    // S
1825                             (~((Lo ^ (Hi << 3)) << 10) & 0x00800000) | // I1
1826                             (~((Lo ^ (Hi << 1)) << 11) & 0x00400000) | // I2
1827                             ((Hi & 0x003ff) << 12) |                   // imm0
1828                             ((Lo & 0x007ff) << 1)); // imm11:0
1829   }
1830   // ELF for the ARM Architecture 4.6.1.1 the implicit addend for MOVW and
1831   // MOVT is in the range -32768 <= A < 32768
1832   case R_ARM_MOVW_ABS_NC:
1833   case R_ARM_MOVT_ABS:
1834   case R_ARM_MOVW_PREL_NC:
1835   case R_ARM_MOVT_PREL: {
1836     uint64_t Val = read32le(Buf) & 0x000f0fff;
1837     return SignExtend64<16>(((Val & 0x000f0000) >> 4) | (Val & 0x00fff));
1838   }
1839   case R_ARM_THM_MOVW_ABS_NC:
1840   case R_ARM_THM_MOVT_ABS:
1841   case R_ARM_THM_MOVW_PREL_NC:
1842   case R_ARM_THM_MOVT_PREL: {
1843     // Encoding T3: A = imm4:i:imm3:imm8
1844     uint16_t Hi = read16le(Buf);
1845     uint16_t Lo = read16le(Buf + 2);
1846     return SignExtend64<16>(((Hi & 0x000f) << 12) | // imm4
1847                             ((Hi & 0x0400) << 1) |  // i
1848                             ((Lo & 0x7000) >> 4) |  // imm3
1849                             (Lo & 0x00ff));         // imm8
1850   }
1851   }
1852 }
1853 
1854 bool ARMTargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
1855   return Type == R_ARM_TLS_LDO32 || Type == R_ARM_TLS_LDM32;
1856 }
1857 
1858 bool ARMTargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
1859   return Type == R_ARM_TLS_GD32;
1860 }
1861 
1862 bool ARMTargetInfo::isTlsInitialExecRel(uint32_t Type) const {
1863   return Type == R_ARM_TLS_IE32;
1864 }
1865 
1866 template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
1867   GotPltHeaderEntriesNum = 2;
1868   MaxPageSize = 65536;
1869   GotEntrySize = sizeof(typename ELFT::uint);
1870   GotPltEntrySize = sizeof(typename ELFT::uint);
1871   PltEntrySize = 16;
1872   PltHeaderSize = 32;
1873   CopyRel = R_MIPS_COPY;
1874   PltRel = R_MIPS_JUMP_SLOT;
1875   NeedsThunks = true;
1876   if (ELFT::Is64Bits) {
1877     RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
1878     TlsGotRel = R_MIPS_TLS_TPREL64;
1879     TlsModuleIndexRel = R_MIPS_TLS_DTPMOD64;
1880     TlsOffsetRel = R_MIPS_TLS_DTPREL64;
1881   } else {
1882     RelativeRel = R_MIPS_REL32;
1883     TlsGotRel = R_MIPS_TLS_TPREL32;
1884     TlsModuleIndexRel = R_MIPS_TLS_DTPMOD32;
1885     TlsOffsetRel = R_MIPS_TLS_DTPREL32;
1886   }
1887 }
1888 
1889 template <class ELFT>
1890 RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1891                                          const SymbolBody &S) const {
1892   if (ELFT::Is64Bits)
1893     // See comment in the calculateMips64RelChain.
1894     Type &= 0xff;
1895   switch (Type) {
1896   default:
1897     return R_ABS;
1898   case R_MIPS_JALR:
1899     return R_HINT;
1900   case R_MIPS_GPREL16:
1901   case R_MIPS_GPREL32:
1902     return R_GOTREL;
1903   case R_MIPS_26:
1904     return R_PLT;
1905   case R_MIPS_HI16:
1906   case R_MIPS_LO16:
1907   case R_MIPS_GOT_OFST:
1908     // MIPS _gp_disp designates offset between start of function and 'gp'
1909     // pointer into GOT. __gnu_local_gp is equal to the current value of
1910     // the 'gp'. Therefore any relocations against them do not require
1911     // dynamic relocation.
1912     if (&S == ElfSym<ELFT>::MipsGpDisp)
1913       return R_PC;
1914     return R_ABS;
1915   case R_MIPS_PC32:
1916   case R_MIPS_PC16:
1917   case R_MIPS_PC19_S2:
1918   case R_MIPS_PC21_S2:
1919   case R_MIPS_PC26_S2:
1920   case R_MIPS_PCHI16:
1921   case R_MIPS_PCLO16:
1922     return R_PC;
1923   case R_MIPS_GOT16:
1924     if (S.isLocal())
1925       return R_MIPS_GOT_LOCAL_PAGE;
1926   // fallthrough
1927   case R_MIPS_CALL16:
1928   case R_MIPS_CALL_HI16:
1929   case R_MIPS_CALL_LO16:
1930   case R_MIPS_GOT_DISP:
1931   case R_MIPS_GOT_HI16:
1932   case R_MIPS_GOT_LO16:
1933   case R_MIPS_TLS_GOTTPREL:
1934     return R_MIPS_GOT_OFF;
1935   case R_MIPS_GOT_PAGE:
1936     return R_MIPS_GOT_LOCAL_PAGE;
1937   case R_MIPS_TLS_GD:
1938     return R_MIPS_TLSGD;
1939   case R_MIPS_TLS_LDM:
1940     return R_MIPS_TLSLD;
1941   }
1942 }
1943 
1944 template <class ELFT>
1945 uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
1946   if (Type == R_MIPS_32 || Type == R_MIPS_64)
1947     return RelativeRel;
1948   // Keep it going with a dummy value so that we can find more reloc errors.
1949   errorDynRel(Type);
1950   return R_MIPS_32;
1951 }
1952 
1953 template <class ELFT>
1954 bool MipsTargetInfo<ELFT>::isTlsLocalDynamicRel(uint32_t Type) const {
1955   return Type == R_MIPS_TLS_LDM;
1956 }
1957 
1958 template <class ELFT>
1959 bool MipsTargetInfo<ELFT>::isTlsGlobalDynamicRel(uint32_t Type) const {
1960   return Type == R_MIPS_TLS_GD;
1961 }
1962 
1963 template <class ELFT>
1964 void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, const SymbolBody &) const {
1965   write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
1966 }
1967 
1968 template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
1969 static int64_t getPcRelocAddend(const uint8_t *Loc) {
1970   uint32_t Instr = read32<E>(Loc);
1971   uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1972   return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1973 }
1974 
1975 template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
1976 static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
1977   uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1978   uint32_t Instr = read32<E>(Loc);
1979   if (SHIFT > 0)
1980     checkAlignment<(1 << SHIFT)>(V, Type);
1981   checkInt<BSIZE + SHIFT>(V, Type);
1982   write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
1983 }
1984 
1985 template <endianness E>
1986 static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
1987   uint32_t Instr = read32<E>(Loc);
1988   uint16_t Res = ((V + 0x8000) >> 16) & 0xffff;
1989   write32<E>(Loc, (Instr & 0xffff0000) | Res);
1990 }
1991 
1992 template <endianness E>
1993 static void writeMipsHigher(uint8_t *Loc, uint64_t V) {
1994   uint32_t Instr = read32<E>(Loc);
1995   uint16_t Res = ((V + 0x80008000) >> 32) & 0xffff;
1996   write32<E>(Loc, (Instr & 0xffff0000) | Res);
1997 }
1998 
1999 template <endianness E>
2000 static void writeMipsHighest(uint8_t *Loc, uint64_t V) {
2001   uint32_t Instr = read32<E>(Loc);
2002   uint16_t Res = ((V + 0x800080008000) >> 48) & 0xffff;
2003   write32<E>(Loc, (Instr & 0xffff0000) | Res);
2004 }
2005 
2006 template <endianness E>
2007 static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
2008   uint32_t Instr = read32<E>(Loc);
2009   write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
2010 }
2011 
2012 template <class ELFT> static bool isMipsR6() {
2013   const auto &FirstObj = cast<ELFFileBase<ELFT>>(*Config->FirstElf);
2014   uint32_t Arch = FirstObj.getObj().getHeader()->e_flags & EF_MIPS_ARCH;
2015   return Arch == EF_MIPS_ARCH_32R6 || Arch == EF_MIPS_ARCH_64R6;
2016 }
2017 
2018 template <class ELFT>
2019 void MipsTargetInfo<ELFT>::writePltHeader(uint8_t *Buf) const {
2020   const endianness E = ELFT::TargetEndianness;
2021   write32<E>(Buf, 0x3c1c0000);      // lui   $28, %hi(&GOTPLT[0])
2022   write32<E>(Buf + 4, 0x8f990000);  // lw    $25, %lo(&GOTPLT[0])($28)
2023   write32<E>(Buf + 8, 0x279c0000);  // addiu $28, $28, %lo(&GOTPLT[0])
2024   write32<E>(Buf + 12, 0x031cc023); // subu  $24, $24, $28
2025   write32<E>(Buf + 16, 0x03e07825); // move  $15, $31
2026   write32<E>(Buf + 20, 0x0018c082); // srl   $24, $24, 2
2027   write32<E>(Buf + 24, 0x0320f809); // jalr  $25
2028   write32<E>(Buf + 28, 0x2718fffe); // subu  $24, $24, 2
2029   uint64_t Got = Out<ELFT>::GotPlt->getVA();
2030   writeMipsHi16<E>(Buf, Got);
2031   writeMipsLo16<E>(Buf + 4, Got);
2032   writeMipsLo16<E>(Buf + 8, Got);
2033 }
2034 
2035 template <class ELFT>
2036 void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
2037                                     uint64_t PltEntryAddr, int32_t Index,
2038                                     unsigned RelOff) const {
2039   const endianness E = ELFT::TargetEndianness;
2040   write32<E>(Buf, 0x3c0f0000);      // lui   $15, %hi(.got.plt entry)
2041   write32<E>(Buf + 4, 0x8df90000);  // l[wd] $25, %lo(.got.plt entry)($15)
2042                                     // jr    $25
2043   write32<E>(Buf + 8, isMipsR6<ELFT>() ? 0x03200009 : 0x03200008);
2044   write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
2045   writeMipsHi16<E>(Buf, GotEntryAddr);
2046   writeMipsLo16<E>(Buf + 4, GotEntryAddr);
2047   writeMipsLo16<E>(Buf + 12, GotEntryAddr);
2048 }
2049 
2050 template <class ELFT>
2051 RelExpr MipsTargetInfo<ELFT>::getThunkExpr(RelExpr Expr, uint32_t Type,
2052                                            const InputFile &File,
2053                                            const SymbolBody &S) const {
2054   // Any MIPS PIC code function is invoked with its address in register $t9.
2055   // So if we have a branch instruction from non-PIC code to the PIC one
2056   // we cannot make the jump directly and need to create a small stubs
2057   // to save the target function address.
2058   // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2059   if (Type != R_MIPS_26)
2060     return Expr;
2061   auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
2062   if (!F)
2063     return Expr;
2064   // If current file has PIC code, LA25 stub is not required.
2065   if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
2066     return Expr;
2067   auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
2068   // LA25 is required if target file has PIC code
2069   // or target symbol is a PIC symbol.
2070   return D && D->isMipsPIC() ? R_THUNK_ABS : Expr;
2071 }
2072 
2073 template <class ELFT>
2074 uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
2075                                                  uint32_t Type) const {
2076   const endianness E = ELFT::TargetEndianness;
2077   switch (Type) {
2078   default:
2079     return 0;
2080   case R_MIPS_32:
2081   case R_MIPS_GPREL32:
2082   case R_MIPS_TLS_DTPREL32:
2083   case R_MIPS_TLS_TPREL32:
2084     return read32<E>(Buf);
2085   case R_MIPS_26:
2086     // FIXME (simon): If the relocation target symbol is not a PLT entry
2087     // we should use another expression for calculation:
2088     // ((A << 2) | (P & 0xf0000000)) >> 2
2089     return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
2090   case R_MIPS_GPREL16:
2091   case R_MIPS_LO16:
2092   case R_MIPS_PCLO16:
2093   case R_MIPS_TLS_DTPREL_HI16:
2094   case R_MIPS_TLS_DTPREL_LO16:
2095   case R_MIPS_TLS_TPREL_HI16:
2096   case R_MIPS_TLS_TPREL_LO16:
2097     return SignExtend64<16>(read32<E>(Buf));
2098   case R_MIPS_PC16:
2099     return getPcRelocAddend<E, 16, 2>(Buf);
2100   case R_MIPS_PC19_S2:
2101     return getPcRelocAddend<E, 19, 2>(Buf);
2102   case R_MIPS_PC21_S2:
2103     return getPcRelocAddend<E, 21, 2>(Buf);
2104   case R_MIPS_PC26_S2:
2105     return getPcRelocAddend<E, 26, 2>(Buf);
2106   case R_MIPS_PC32:
2107     return getPcRelocAddend<E, 32, 0>(Buf);
2108   }
2109 }
2110 
2111 static std::pair<uint32_t, uint64_t> calculateMips64RelChain(uint32_t Type,
2112                                                              uint64_t Val) {
2113   // MIPS N64 ABI packs multiple relocations into the single relocation
2114   // record. In general, all up to three relocations can have arbitrary
2115   // types. In fact, Clang and GCC uses only a few combinations. For now,
2116   // we support two of them. That is allow to pass at least all LLVM
2117   // test suite cases.
2118   // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
2119   // <any relocation> / R_MIPS_64 / R_MIPS_NONE
2120   // The first relocation is a 'real' relocation which is calculated
2121   // using the corresponding symbol's value. The second and the third
2122   // relocations used to modify result of the first one: extend it to
2123   // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
2124   // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
2125   uint32_t Type2 = (Type >> 8) & 0xff;
2126   uint32_t Type3 = (Type >> 16) & 0xff;
2127   if (Type2 == R_MIPS_NONE && Type3 == R_MIPS_NONE)
2128     return std::make_pair(Type, Val);
2129   if (Type2 == R_MIPS_64 && Type3 == R_MIPS_NONE)
2130     return std::make_pair(Type2, Val);
2131   if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16))
2132     return std::make_pair(Type3, -Val);
2133   error("unsupported relocations combination " + Twine(Type));
2134   return std::make_pair(Type & 0xff, Val);
2135 }
2136 
2137 template <class ELFT>
2138 void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
2139                                        uint64_t Val) const {
2140   const endianness E = ELFT::TargetEndianness;
2141   // Thread pointer and DRP offsets from the start of TLS data area.
2142   // https://www.linux-mips.org/wiki/NPTL
2143   if (Type == R_MIPS_TLS_DTPREL_HI16 || Type == R_MIPS_TLS_DTPREL_LO16 ||
2144       Type == R_MIPS_TLS_DTPREL32 || Type == R_MIPS_TLS_DTPREL64)
2145     Val -= 0x8000;
2146   else if (Type == R_MIPS_TLS_TPREL_HI16 || Type == R_MIPS_TLS_TPREL_LO16 ||
2147            Type == R_MIPS_TLS_TPREL32 || Type == R_MIPS_TLS_TPREL64)
2148     Val -= 0x7000;
2149   if (ELFT::Is64Bits)
2150     std::tie(Type, Val) = calculateMips64RelChain(Type, Val);
2151   switch (Type) {
2152   case R_MIPS_32:
2153   case R_MIPS_GPREL32:
2154   case R_MIPS_TLS_DTPREL32:
2155   case R_MIPS_TLS_TPREL32:
2156     write32<E>(Loc, Val);
2157     break;
2158   case R_MIPS_64:
2159   case R_MIPS_TLS_DTPREL64:
2160   case R_MIPS_TLS_TPREL64:
2161     write64<E>(Loc, Val);
2162     break;
2163   case R_MIPS_26:
2164     write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | ((Val >> 2) & 0x3ffffff));
2165     break;
2166   case R_MIPS_GOT_DISP:
2167   case R_MIPS_GOT_PAGE:
2168   case R_MIPS_GOT16:
2169   case R_MIPS_GPREL16:
2170   case R_MIPS_TLS_GD:
2171   case R_MIPS_TLS_LDM:
2172     checkInt<16>(Val, Type);
2173   // fallthrough
2174   case R_MIPS_CALL16:
2175   case R_MIPS_CALL_LO16:
2176   case R_MIPS_GOT_LO16:
2177   case R_MIPS_GOT_OFST:
2178   case R_MIPS_LO16:
2179   case R_MIPS_PCLO16:
2180   case R_MIPS_TLS_DTPREL_LO16:
2181   case R_MIPS_TLS_GOTTPREL:
2182   case R_MIPS_TLS_TPREL_LO16:
2183     writeMipsLo16<E>(Loc, Val);
2184     break;
2185   case R_MIPS_CALL_HI16:
2186   case R_MIPS_GOT_HI16:
2187   case R_MIPS_HI16:
2188   case R_MIPS_PCHI16:
2189   case R_MIPS_TLS_DTPREL_HI16:
2190   case R_MIPS_TLS_TPREL_HI16:
2191     writeMipsHi16<E>(Loc, Val);
2192     break;
2193   case R_MIPS_HIGHER:
2194     writeMipsHigher<E>(Loc, Val);
2195     break;
2196   case R_MIPS_HIGHEST:
2197     writeMipsHighest<E>(Loc, Val);
2198     break;
2199   case R_MIPS_JALR:
2200     // Ignore this optimization relocation for now
2201     break;
2202   case R_MIPS_PC16:
2203     applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
2204     break;
2205   case R_MIPS_PC19_S2:
2206     applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
2207     break;
2208   case R_MIPS_PC21_S2:
2209     applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
2210     break;
2211   case R_MIPS_PC26_S2:
2212     applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
2213     break;
2214   case R_MIPS_PC32:
2215     applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
2216     break;
2217   default:
2218     fatal("unrecognized reloc " + Twine(Type));
2219   }
2220 }
2221 
2222 template <class ELFT>
2223 bool MipsTargetInfo<ELFT>::usesOnlyLowPageBits(uint32_t Type) const {
2224   return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST;
2225 }
2226 }
2227 }
2228