xref: /llvm-project-15.0.7/lld/ELF/Target.cpp (revision 981986cd)
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 varaibles, 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 
33 #include "llvm/ADT/ArrayRef.h"
34 #include "llvm/Object/ELF.h"
35 #include "llvm/Support/Endian.h"
36 #include "llvm/Support/ELF.h"
37 
38 using namespace llvm;
39 using namespace llvm::object;
40 using namespace llvm::support::endian;
41 using namespace llvm::ELF;
42 
43 namespace lld {
44 namespace elf {
45 
46 TargetInfo *Target;
47 
48 static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
49 
50 template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
51   if (isInt<N>(V))
52     return;
53   StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
54   error("relocation " + S + " out of range");
55 }
56 
57 template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
58   if (isUInt<N>(V))
59     return;
60   StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
61   error("relocation " + S + " out of range");
62 }
63 
64 template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
65   if (isInt<N>(V) || isUInt<N>(V))
66     return;
67   StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
68   error("relocation " + S + " out of range");
69 }
70 
71 template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
72   if ((V & (N - 1)) == 0)
73     return;
74   StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
75   error("improper alignment for relocation " + S);
76 }
77 
78 namespace {
79 class X86TargetInfo final : public TargetInfo {
80 public:
81   X86TargetInfo();
82   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
83   uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
84   void writeGotPltHeader(uint8_t *Buf) const override;
85   uint32_t getDynRel(uint32_t Type) const override;
86   bool isTlsLocalDynamicRel(uint32_t Type) const override;
87   bool isTlsGlobalDynamicRel(uint32_t Type) const override;
88   bool isTlsInitialExecRel(uint32_t Type) const override;
89   void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
90   void writePltZero(uint8_t *Buf) const override;
91   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
92                 int32_t Index, unsigned RelOff) const override;
93   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
94 
95   void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
96   void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
97   void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
98   void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
99 };
100 
101 class X86_64TargetInfo final : public TargetInfo {
102 public:
103   X86_64TargetInfo();
104   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
105   uint32_t getDynRel(uint32_t Type) const override;
106   bool isTlsLocalDynamicRel(uint32_t Type) const override;
107   bool isTlsGlobalDynamicRel(uint32_t Type) const override;
108   bool isTlsInitialExecRel(uint32_t Type) const override;
109   void writeGotPltHeader(uint8_t *Buf) const override;
110   void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
111   void writePltZero(uint8_t *Buf) const override;
112   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
113                 int32_t Index, unsigned RelOff) const override;
114   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
115 
116   RelExpr adjustRelaxGotExpr(uint32_t Type, const uint8_t *Data,
117                              RelExpr Expr) const override;
118   void relaxGot(uint8_t *Loc, uint64_t Val) const override;
119   void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
120   void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
121   void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
122   void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
123 
124 private:
125   void relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
126                      uint8_t ModRm) const;
127 };
128 
129 class PPCTargetInfo final : public TargetInfo {
130 public:
131   PPCTargetInfo();
132   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
133   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
134 };
135 
136 class PPC64TargetInfo final : public TargetInfo {
137 public:
138   PPC64TargetInfo();
139   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
140   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
141                 int32_t Index, unsigned RelOff) const override;
142   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
143 };
144 
145 class AArch64TargetInfo final : public TargetInfo {
146 public:
147   AArch64TargetInfo();
148   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
149   uint32_t getDynRel(uint32_t Type) const override;
150   bool isTlsInitialExecRel(uint32_t Type) const override;
151   void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
152   void writePltZero(uint8_t *Buf) const override;
153   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
154                 int32_t Index, unsigned RelOff) const override;
155   bool usesOnlyLowPageBits(uint32_t Type) const override;
156   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
157   void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
158   void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
159 };
160 
161 class AMDGPUTargetInfo final : public TargetInfo {
162 public:
163   AMDGPUTargetInfo() {}
164   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
165   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
166 };
167 
168 template <class ELFT> class MipsTargetInfo final : public TargetInfo {
169 public:
170   MipsTargetInfo();
171   RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
172   uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
173   uint32_t getDynRel(uint32_t Type) const override;
174   void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
175   void writePltZero(uint8_t *Buf) const override;
176   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
177                 int32_t Index, unsigned RelOff) const override;
178   void writeThunk(uint8_t *Buf, uint64_t S) const override;
179   bool needsThunk(uint32_t Type, const InputFile &File,
180                   const SymbolBody &S) const override;
181   void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
182   bool usesOnlyLowPageBits(uint32_t Type) const override;
183 };
184 } // anonymous namespace
185 
186 TargetInfo *createTarget() {
187   switch (Config->EMachine) {
188   case EM_386:
189     return new X86TargetInfo();
190   case EM_AARCH64:
191     return new AArch64TargetInfo();
192   case EM_AMDGPU:
193     return new AMDGPUTargetInfo();
194   case EM_MIPS:
195     switch (Config->EKind) {
196     case ELF32LEKind:
197       return new MipsTargetInfo<ELF32LE>();
198     case ELF32BEKind:
199       return new MipsTargetInfo<ELF32BE>();
200     case ELF64LEKind:
201       return new MipsTargetInfo<ELF64LE>();
202     case ELF64BEKind:
203       return new MipsTargetInfo<ELF64BE>();
204     default:
205       fatal("unsupported MIPS target");
206     }
207   case EM_PPC:
208     return new PPCTargetInfo();
209   case EM_PPC64:
210     return new PPC64TargetInfo();
211   case EM_X86_64:
212     return new X86_64TargetInfo();
213   }
214   fatal("unknown target machine");
215 }
216 
217 TargetInfo::~TargetInfo() {}
218 
219 uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
220                                        uint32_t Type) const {
221   return 0;
222 }
223 
224 uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
225 
226 bool TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { return false; }
227 
228 bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
229                             const SymbolBody &S) const {
230   return false;
231 }
232 
233 bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
234 
235 bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
236 
237 bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
238   return false;
239 }
240 
241 RelExpr TargetInfo::adjustRelaxGotExpr(uint32_t Type, const uint8_t *Data,
242                                        RelExpr Expr) const {
243   return Expr;
244 }
245 
246 void TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
247   llvm_unreachable("Should not have claimed to be relaxable");
248 }
249 
250 void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
251                                 uint64_t Val) const {
252   llvm_unreachable("Should not have claimed to be relaxable");
253 }
254 
255 void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
256                                 uint64_t Val) const {
257   llvm_unreachable("Should not have claimed to be relaxable");
258 }
259 
260 void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
261                                 uint64_t Val) const {
262   llvm_unreachable("Should not have claimed to be relaxable");
263 }
264 
265 void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
266                                 uint64_t Val) const {
267   llvm_unreachable("Should not have claimed to be relaxable");
268 }
269 
270 X86TargetInfo::X86TargetInfo() {
271   CopyRel = R_386_COPY;
272   GotRel = R_386_GLOB_DAT;
273   PltRel = R_386_JUMP_SLOT;
274   IRelativeRel = R_386_IRELATIVE;
275   RelativeRel = R_386_RELATIVE;
276   TlsGotRel = R_386_TLS_TPOFF;
277   TlsModuleIndexRel = R_386_TLS_DTPMOD32;
278   TlsOffsetRel = R_386_TLS_DTPOFF32;
279   PltEntrySize = 16;
280   PltZeroSize = 16;
281   TlsGdToLeSkip = 2;
282 }
283 
284 RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
285   switch (Type) {
286   default:
287     return R_ABS;
288   case R_386_TLS_GD:
289     return R_TLSGD;
290   case R_386_TLS_LDM:
291     return R_TLSLD;
292   case R_386_PLT32:
293     return R_PLT_PC;
294   case R_386_PC32:
295     return R_PC;
296   case R_386_GOTPC:
297     return R_GOTONLY_PC;
298   case R_386_TLS_IE:
299     return R_GOT;
300   case R_386_GOT32:
301   case R_386_TLS_GOTIE:
302     return R_GOT_FROM_END;
303   case R_386_GOTOFF:
304     return R_GOTREL;
305   case R_386_TLS_LE:
306     return R_TLS;
307   case R_386_TLS_LE_32:
308     return R_NEG_TLS;
309   }
310 }
311 
312 void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
313   write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
314 }
315 
316 void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
317   // Entries in .got.plt initially points back to the corresponding
318   // PLT entries with a fixed offset to skip the first instruction.
319   write32le(Buf, Plt + 6);
320 }
321 
322 uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
323   if (Type == R_386_TLS_LE)
324     return R_386_TLS_TPOFF;
325   if (Type == R_386_TLS_LE_32)
326     return R_386_TLS_TPOFF32;
327   return Type;
328 }
329 
330 bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
331   return Type == R_386_TLS_GD;
332 }
333 
334 bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
335   return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
336 }
337 
338 bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
339   return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
340 }
341 
342 void X86TargetInfo::writePltZero(uint8_t *Buf) const {
343   // Executable files and shared object files have
344   // separate procedure linkage tables.
345   if (Config->Pic) {
346     const uint8_t V[] = {
347         0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
348         0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp   *8(%ebx)
349         0x90, 0x90, 0x90, 0x90              // nop; nop; nop; nop
350     };
351     memcpy(Buf, V, sizeof(V));
352     return;
353   }
354 
355   const uint8_t PltData[] = {
356       0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
357       0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp   *(GOT+8)
358       0x90, 0x90, 0x90, 0x90              // nop; nop; nop; nop
359   };
360   memcpy(Buf, PltData, sizeof(PltData));
361   uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
362   write32le(Buf + 2, Got + 4);
363   write32le(Buf + 8, Got + 8);
364 }
365 
366 void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
367                              uint64_t PltEntryAddr, int32_t Index,
368                              unsigned RelOff) const {
369   const uint8_t Inst[] = {
370       0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
371       0x68, 0x00, 0x00, 0x00, 0x00,       // pushl $reloc_offset
372       0xe9, 0x00, 0x00, 0x00, 0x00        // jmp .PLT0@PC
373   };
374   memcpy(Buf, Inst, sizeof(Inst));
375 
376   // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
377   Buf[1] = Config->Pic ? 0xa3 : 0x25;
378   uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
379   write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
380   write32le(Buf + 7, RelOff);
381   write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
382 }
383 
384 uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
385                                           uint32_t Type) const {
386   switch (Type) {
387   default:
388     return 0;
389   case R_386_32:
390   case R_386_GOT32:
391   case R_386_GOTOFF:
392   case R_386_GOTPC:
393   case R_386_PC32:
394   case R_386_PLT32:
395     return read32le(Buf);
396   }
397 }
398 
399 void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
400                                 uint64_t Val) const {
401   checkInt<32>(Val, Type);
402   write32le(Loc, Val);
403 }
404 
405 void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
406                                    uint64_t Val) const {
407   // Convert
408   //   leal x@tlsgd(, %ebx, 1),
409   //   call __tls_get_addr@plt
410   // to
411   //   movl %gs:0,%eax
412   //   subl $x@ntpoff,%eax
413   const uint8_t Inst[] = {
414       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
415       0x81, 0xe8, 0x00, 0x00, 0x00, 0x00  // subl 0(%ebx), %eax
416   };
417   memcpy(Loc - 3, Inst, sizeof(Inst));
418   relocateOne(Loc + 5, R_386_32, Val);
419 }
420 
421 void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
422                                    uint64_t Val) const {
423   // Convert
424   //   leal x@tlsgd(, %ebx, 1),
425   //   call __tls_get_addr@plt
426   // to
427   //   movl %gs:0, %eax
428   //   addl x@gotntpoff(%ebx), %eax
429   const uint8_t Inst[] = {
430       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
431       0x03, 0x83, 0x00, 0x00, 0x00, 0x00  // addl 0(%ebx), %eax
432   };
433   memcpy(Loc - 3, Inst, sizeof(Inst));
434   relocateOne(Loc + 5, R_386_32, Val);
435 }
436 
437 // In some conditions, relocations can be optimized to avoid using GOT.
438 // This function does that for Initial Exec to Local Exec case.
439 void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
440                                    uint64_t Val) const {
441   // Ulrich's document section 6.2 says that @gotntpoff can
442   // be used with MOVL or ADDL instructions.
443   // @indntpoff is similar to @gotntpoff, but for use in
444   // position dependent code.
445   uint8_t *Inst = Loc - 2;
446   uint8_t *Op = Loc - 1;
447   uint8_t Reg = (Loc[-1] >> 3) & 7;
448   bool IsMov = *Inst == 0x8b;
449   if (Type == R_386_TLS_IE) {
450     // For R_386_TLS_IE relocation we perform the next transformations:
451     // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
452     // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
453     // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
454     // First one is special because when EAX is used the sequence is 5 bytes
455     // long, otherwise it is 6 bytes.
456     if (*Op == 0xa1) {
457       *Op = 0xb8;
458     } else {
459       *Inst = IsMov ? 0xc7 : 0x81;
460       *Op = 0xc0 | ((*Op >> 3) & 7);
461     }
462   } else {
463     // R_386_TLS_GOTIE relocation can be optimized to
464     // R_386_TLS_LE so that it does not use GOT.
465     // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
466     // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
467     // Note: gold converts to ADDL instead of LEAL.
468     *Inst = IsMov ? 0xc7 : 0x8d;
469     if (IsMov)
470       *Op = 0xc0 | ((*Op >> 3) & 7);
471     else
472       *Op = 0x80 | Reg | (Reg << 3);
473   }
474   relocateOne(Loc, R_386_TLS_LE, Val);
475 }
476 
477 void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
478                                    uint64_t Val) const {
479   if (Type == R_386_TLS_LDO_32) {
480     relocateOne(Loc, R_386_TLS_LE, Val);
481     return;
482   }
483 
484   // Convert
485   //   leal foo(%reg),%eax
486   //   call ___tls_get_addr
487   // to
488   //   movl %gs:0,%eax
489   //   nop
490   //   leal 0(%esi,1),%esi
491   const uint8_t Inst[] = {
492       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
493       0x90,                               // nop
494       0x8d, 0x74, 0x26, 0x00              // leal 0(%esi,1),%esi
495   };
496   memcpy(Loc - 2, Inst, sizeof(Inst));
497 }
498 
499 X86_64TargetInfo::X86_64TargetInfo() {
500   CopyRel = R_X86_64_COPY;
501   GotRel = R_X86_64_GLOB_DAT;
502   PltRel = R_X86_64_JUMP_SLOT;
503   RelativeRel = R_X86_64_RELATIVE;
504   IRelativeRel = R_X86_64_IRELATIVE;
505   TlsGotRel = R_X86_64_TPOFF64;
506   TlsModuleIndexRel = R_X86_64_DTPMOD64;
507   TlsOffsetRel = R_X86_64_DTPOFF64;
508   PltEntrySize = 16;
509   PltZeroSize = 16;
510   TlsGdToLeSkip = 2;
511 }
512 
513 RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
514   switch (Type) {
515   default:
516     return R_ABS;
517   case R_X86_64_TPOFF32:
518     return R_TLS;
519   case R_X86_64_TLSLD:
520     return R_TLSLD_PC;
521   case R_X86_64_TLSGD:
522     return R_TLSGD_PC;
523   case R_X86_64_SIZE32:
524   case R_X86_64_SIZE64:
525     return R_SIZE;
526   case R_X86_64_PLT32:
527     return R_PLT_PC;
528   case R_X86_64_PC32:
529   case R_X86_64_PC64:
530     return R_PC;
531   case R_X86_64_GOT32:
532     return R_GOT_FROM_END;
533   case R_X86_64_GOTPCREL:
534   case R_X86_64_GOTPCRELX:
535   case R_X86_64_REX_GOTPCRELX:
536   case R_X86_64_GOTTPOFF:
537     return R_GOT_PC;
538   }
539 }
540 
541 void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
542   // The first entry holds the value of _DYNAMIC. It is not clear why that is
543   // required, but it is documented in the psabi and the glibc dynamic linker
544   // seems to use it (note that this is relevant for linking ld.so, not any
545   // other program).
546   write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
547 }
548 
549 void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
550   // See comments in X86TargetInfo::writeGotPlt.
551   write32le(Buf, Plt + 6);
552 }
553 
554 void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
555   const uint8_t PltData[] = {
556       0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
557       0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
558       0x0f, 0x1f, 0x40, 0x00              // nopl 0x0(rax)
559   };
560   memcpy(Buf, PltData, sizeof(PltData));
561   uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
562   uint64_t Plt = Out<ELF64LE>::Plt->getVA();
563   write32le(Buf + 2, Got - Plt + 2); // GOT+8
564   write32le(Buf + 8, Got - Plt + 4); // GOT+16
565 }
566 
567 void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
568                                 uint64_t PltEntryAddr, int32_t Index,
569                                 unsigned RelOff) const {
570   const uint8_t Inst[] = {
571       0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
572       0x68, 0x00, 0x00, 0x00, 0x00,       // pushq <relocation index>
573       0xe9, 0x00, 0x00, 0x00, 0x00        // jmpq plt[0]
574   };
575   memcpy(Buf, Inst, sizeof(Inst));
576 
577   write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
578   write32le(Buf + 7, Index);
579   write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
580 }
581 
582 uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
583   if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
584     if (Config->Shared)
585       error(getELFRelocationTypeName(EM_X86_64, Type) +
586             " cannot be a dynamic relocation");
587   return Type;
588 }
589 
590 bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
591   return Type == R_X86_64_GOTTPOFF;
592 }
593 
594 bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
595   return Type == R_X86_64_TLSGD;
596 }
597 
598 bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
599   return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
600          Type == R_X86_64_TLSLD;
601 }
602 
603 void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
604                                       uint64_t Val) const {
605   // Convert
606   //   .byte 0x66
607   //   leaq x@tlsgd(%rip), %rdi
608   //   .word 0x6666
609   //   rex64
610   //   call __tls_get_addr@plt
611   // to
612   //   mov %fs:0x0,%rax
613   //   lea x@tpoff,%rax
614   const uint8_t Inst[] = {
615       0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
616       0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00              // lea x@tpoff,%rax
617   };
618   memcpy(Loc - 4, Inst, sizeof(Inst));
619   // The original code used a pc relative relocation and so we have to
620   // compensate for the -4 in had in the addend.
621   relocateOne(Loc + 8, R_X86_64_TPOFF32, Val + 4);
622 }
623 
624 void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
625                                       uint64_t Val) const {
626   // Convert
627   //   .byte 0x66
628   //   leaq x@tlsgd(%rip), %rdi
629   //   .word 0x6666
630   //   rex64
631   //   call __tls_get_addr@plt
632   // to
633   //   mov %fs:0x0,%rax
634   //   addq x@tpoff,%rax
635   const uint8_t Inst[] = {
636       0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
637       0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00              // addq x@tpoff,%rax
638   };
639   memcpy(Loc - 4, Inst, sizeof(Inst));
640   // Both code sequences are PC relatives, but since we are moving the constant
641   // forward by 8 bytes we have to subtract the value by 8.
642   relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
643 }
644 
645 // In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
646 // R_X86_64_TPOFF32 so that it does not use GOT.
647 void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
648                                       uint64_t Val) const {
649   // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
650   // used in MOVQ or ADDQ instructions only.
651   // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
652   // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
653   // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
654   // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
655   uint8_t *Prefix = Loc - 3;
656   uint8_t *Inst = Loc - 2;
657   uint8_t *RegSlot = Loc - 1;
658   uint8_t Reg = Loc[-1] >> 3;
659   bool IsMov = *Inst == 0x8b;
660   bool RspAdd = !IsMov && Reg == 4;
661 
662   // r12 and rsp registers requires special handling.
663   // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
664   // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
665   // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
666   // The same true for rsp. So we convert to addq for them, saving 1 byte that
667   // we dont have.
668   if (RspAdd)
669     *Inst = 0x81;
670   else
671     *Inst = IsMov ? 0xc7 : 0x8d;
672   if (*Prefix == 0x4c)
673     *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
674   *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
675   // The original code used a pc relative relocation and so we have to
676   // compensate for the -4 in had in the addend.
677   relocateOne(Loc, R_X86_64_TPOFF32, Val + 4);
678 }
679 
680 void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
681                                       uint64_t Val) const {
682   // Convert
683   //   leaq bar@tlsld(%rip), %rdi
684   //   callq __tls_get_addr@PLT
685   //   leaq bar@dtpoff(%rax), %rcx
686   // to
687   //   .word 0x6666
688   //   .byte 0x66
689   //   mov %fs:0,%rax
690   //   leaq bar@tpoff(%rax), %rcx
691   if (Type == R_X86_64_DTPOFF64) {
692     write64le(Loc, Val);
693     return;
694   }
695   if (Type == R_X86_64_DTPOFF32) {
696     relocateOne(Loc, R_X86_64_TPOFF32, Val);
697     return;
698   }
699 
700   const uint8_t Inst[] = {
701       0x66, 0x66,                                          // .word 0x6666
702       0x66,                                                // .byte 0x66
703       0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
704   };
705   memcpy(Loc - 3, Inst, sizeof(Inst));
706 }
707 
708 void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
709                                    uint64_t Val) const {
710   switch (Type) {
711   case R_X86_64_32:
712     checkUInt<32>(Val, Type);
713     write32le(Loc, Val);
714     break;
715   case R_X86_64_32S:
716   case R_X86_64_TPOFF32:
717   case R_X86_64_GOT32:
718   case R_X86_64_GOTPCREL:
719   case R_X86_64_GOTPCRELX:
720   case R_X86_64_REX_GOTPCRELX:
721   case R_X86_64_PC32:
722   case R_X86_64_GOTTPOFF:
723   case R_X86_64_PLT32:
724   case R_X86_64_TLSGD:
725   case R_X86_64_TLSLD:
726   case R_X86_64_DTPOFF32:
727   case R_X86_64_SIZE32:
728     checkInt<32>(Val, Type);
729     write32le(Loc, Val);
730     break;
731   case R_X86_64_64:
732   case R_X86_64_DTPOFF64:
733   case R_X86_64_SIZE64:
734   case R_X86_64_PC64:
735     write64le(Loc, Val);
736     break;
737   default:
738     fatal("unrecognized reloc " + Twine(Type));
739   }
740 }
741 
742 RelExpr X86_64TargetInfo::adjustRelaxGotExpr(uint32_t Type, const uint8_t *Data,
743                                              RelExpr RelExpr) const {
744   if (Type != R_X86_64_GOTPCRELX && Type != R_X86_64_REX_GOTPCRELX)
745     return RelExpr;
746   const uint8_t Op = Data[-2];
747   const uint8_t ModRm = Data[-1];
748   // FIXME: When PIC is disabled and foo is defined locally in the
749   // lower 32 bit address space, memory operand in mov can be converted into
750   // immediate operand. Otherwise, mov must be changed to lea. We support only
751   // latter relaxation at this moment.
752   if (Op == 0x8b)
753     return R_RELAX_GOT_PC;
754   // Relax call and jmp.
755   if (Op == 0xff && (ModRm == 0x15 || ModRm == 0x25))
756     return R_RELAX_GOT_PC;
757 
758   // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor.
759   // If PIC then no relaxation is available.
760   // We also don't relax test/binop instructions without REX byte,
761   // they are 32bit operations and not common to have.
762   assert(Type == R_X86_64_REX_GOTPCRELX);
763   return Config->Pic ? RelExpr : R_RELAX_GOT_PC_NOPIC;
764 }
765 
766 // A subset of relaxations can only be applied for no-PIC. This method
767 // handles such relaxations. Instructions encoding information was taken from:
768 // "Intel 64 and IA-32 Architectures Software Developer's Manual V2"
769 // (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
770 //    64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
771 void X86_64TargetInfo::relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
772                                      uint8_t ModRm) const {
773   const uint8_t Rex = Loc[-3];
774   // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg".
775   if (Op == 0x85) {
776     // See "TEST-Logical Compare" (4-428 Vol. 2B),
777     // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension).
778 
779     // ModR/M byte has form XX YYY ZZZ, where
780     // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1).
781     // XX has different meanings:
782     // 00: The operand's memory address is in reg1.
783     // 01: The operand's memory address is reg1 + a byte-sized displacement.
784     // 10: The operand's memory address is reg1 + a word-sized displacement.
785     // 11: The operand is reg1 itself.
786     // If an instruction requires only one operand, the unused reg2 field
787     // holds extra opcode bits rather than a register code
788     // 0xC0 == 11 000 000 binary.
789     // 0x38 == 00 111 000 binary.
790     // We transfer reg2 to reg1 here as operand.
791     // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3).
792     *(Loc - 1) = 0xc0 | (ModRm & 0x38) >> 3; // ModR/M byte.
793 
794     // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32
795     // See "TEST-Logical Compare" (4-428 Vol. 2B).
796     *(Loc - 2) = 0xf7;
797 
798     // Move R bit to the B bit in REX byte.
799     // REX byte is encoded as 0100WRXB, where
800     // 0100 is 4bit fixed pattern.
801     // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the
802     //   default operand size is used (which is 32-bit for most but not all
803     //   instructions).
804     // REX.R This 1-bit value is an extension to the MODRM.reg field.
805     // REX.X This 1-bit value is an extension to the SIB.index field.
806     // REX.B This 1-bit value is an extension to the MODRM.rm field or the
807     // SIB.base field.
808     // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A).
809     *(Loc - 3) = (Rex & ~0x4) | (Rex & 0x4) >> 2;
810     relocateOne(Loc, R_X86_64_PC32, Val);
811     return;
812   }
813 
814   // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub
815   // or xor operations.
816 
817   // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg".
818   // Logic is close to one for test instruction above, but we also
819   // write opcode extension here, see below for details.
820   *(Loc - 1) = 0xc0 | (ModRm & 0x38) >> 3 | (Op & 0x3c); // ModR/M byte.
821 
822   // Primary opcode is 0x81, opcode extension is one of:
823   // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB,
824   // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP.
825   // This value was wrote to MODRM.reg in a line above.
826   // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15),
827   // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for
828   // descriptions about each operation.
829   *(Loc - 2) = 0x81;
830   *(Loc - 3) = (Rex & ~0x4) | (Rex & 0x4) >> 2;
831   relocateOne(Loc, R_X86_64_PC32, Val);
832 }
833 
834 void X86_64TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
835   const uint8_t Op = Loc[-2];
836   const uint8_t ModRm = Loc[-1];
837 
838   // Convert mov foo@GOTPCREL(%rip), %reg to lea foo(%rip), %reg.
839   if (Op == 0x8b) {
840     *(Loc - 2) = 0x8d;
841     relocateOne(Loc, R_X86_64_PC32, Val);
842     return;
843   }
844 
845   // Convert call/jmp instructions.
846   if (Op == 0xff) {
847     if (ModRm == 0x15) {
848       // ABI says we can convert call *foo@GOTPCREL(%rip) to nop call foo.
849       // Instead we convert to addr32 call foo, where addr32 is instruction
850       // prefix. That makes result expression to be a single instruction.
851       *(Loc - 2) = 0x67; // addr32 prefix
852       *(Loc - 1) = 0xe8; // call
853     } else {
854       assert(ModRm == 0x25);
855       // Convert jmp *foo@GOTPCREL(%rip) to jmp foo nop.
856       // jmp doesn't return, so it is fine to use nop here, it is just a stub.
857       *(Loc - 2) = 0xe9; // jmp
858       *(Loc + 3) = 0x90; // nop
859       Loc -= 1;
860       Val += 1;
861     }
862     relocateOne(Loc, R_X86_64_PC32, Val);
863     return;
864   }
865 
866   assert(!Config->Pic);
867   // We are relaxing a rip relative to an absolute, so compensate
868   // for the old -4 addend.
869   relaxGotNoPic(Loc, Val + 4, Op, ModRm);
870 }
871 
872 // Relocation masks following the #lo(value), #hi(value), #ha(value),
873 // #higher(value), #highera(value), #highest(value), and #highesta(value)
874 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
875 // document.
876 static uint16_t applyPPCLo(uint64_t V) { return V; }
877 static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
878 static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
879 static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
880 static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
881 static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
882 static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
883 
884 PPCTargetInfo::PPCTargetInfo() {}
885 
886 void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
887                                 uint64_t Val) const {
888   switch (Type) {
889   case R_PPC_ADDR16_HA:
890     write16be(Loc, applyPPCHa(Val));
891     break;
892   case R_PPC_ADDR16_LO:
893     write16be(Loc, applyPPCLo(Val));
894     break;
895   default:
896     fatal("unrecognized reloc " + Twine(Type));
897   }
898 }
899 
900 RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
901   return R_ABS;
902 }
903 
904 PPC64TargetInfo::PPC64TargetInfo() {
905   PltRel = GotRel = R_PPC64_GLOB_DAT;
906   RelativeRel = R_PPC64_RELATIVE;
907   PltEntrySize = 32;
908 
909   // We need 64K pages (at least under glibc/Linux, the loader won't
910   // set different permissions on a finer granularity than that).
911   PageSize = 65536;
912 
913   // The PPC64 ELF ABI v1 spec, says:
914   //
915   //   It is normally desirable to put segments with different characteristics
916   //   in separate 256 Mbyte portions of the address space, to give the
917   //   operating system full paging flexibility in the 64-bit address space.
918   //
919   // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
920   // use 0x10000000 as the starting address.
921   VAStart = 0x10000000;
922 }
923 
924 static uint64_t PPC64TocOffset = 0x8000;
925 
926 uint64_t getPPC64TocBase() {
927   // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
928   // TOC starts where the first of these sections starts. We always create a
929   // .got when we see a relocation that uses it, so for us the start is always
930   // the .got.
931   uint64_t TocVA = Out<ELF64BE>::Got->getVA();
932 
933   // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
934   // thus permitting a full 64 Kbytes segment. Note that the glibc startup
935   // code (crt1.o) assumes that you can get from the TOC base to the
936   // start of the .toc section with only a single (signed) 16-bit relocation.
937   return TocVA + PPC64TocOffset;
938 }
939 
940 RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
941   switch (Type) {
942   default:
943     return R_ABS;
944   case R_PPC64_TOC16:
945   case R_PPC64_TOC16_DS:
946   case R_PPC64_TOC16_HA:
947   case R_PPC64_TOC16_HI:
948   case R_PPC64_TOC16_LO:
949   case R_PPC64_TOC16_LO_DS:
950     return R_GOTREL;
951   case R_PPC64_TOC:
952     return R_PPC_TOC;
953   case R_PPC64_REL24:
954     return R_PPC_PLT_OPD;
955   }
956 }
957 
958 void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
959                                uint64_t PltEntryAddr, int32_t Index,
960                                unsigned RelOff) const {
961   uint64_t Off = GotEntryAddr - getPPC64TocBase();
962 
963   // FIXME: What we should do, in theory, is get the offset of the function
964   // descriptor in the .opd section, and use that as the offset from %r2 (the
965   // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
966   // be a pointer to the function descriptor in the .opd section. Using
967   // this scheme is simpler, but requires an extra indirection per PLT dispatch.
968 
969   write32be(Buf,      0xf8410028);                   // std %r2, 40(%r1)
970   write32be(Buf + 4,  0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
971   write32be(Buf + 8,  0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
972   write32be(Buf + 12, 0xe96c0000);                   // ld %r11,0(%r12)
973   write32be(Buf + 16, 0x7d6903a6);                   // mtctr %r11
974   write32be(Buf + 20, 0xe84c0008);                   // ld %r2,8(%r12)
975   write32be(Buf + 24, 0xe96c0010);                   // ld %r11,16(%r12)
976   write32be(Buf + 28, 0x4e800420);                   // bctr
977 }
978 
979 void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
980                                   uint64_t Val) const {
981   uint64_t TO = PPC64TocOffset;
982 
983   // For a TOC-relative relocation,  proceed in terms of the corresponding
984   // ADDR16 relocation type.
985   switch (Type) {
986   case R_PPC64_TOC16:       Type = R_PPC64_ADDR16;       Val -= TO; break;
987   case R_PPC64_TOC16_DS:    Type = R_PPC64_ADDR16_DS;    Val -= TO; break;
988   case R_PPC64_TOC16_HA:    Type = R_PPC64_ADDR16_HA;    Val -= TO; break;
989   case R_PPC64_TOC16_HI:    Type = R_PPC64_ADDR16_HI;    Val -= TO; break;
990   case R_PPC64_TOC16_LO:    Type = R_PPC64_ADDR16_LO;    Val -= TO; break;
991   case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TO; break;
992   default: break;
993   }
994 
995   switch (Type) {
996   case R_PPC64_ADDR14: {
997     checkAlignment<4>(Val, Type);
998     // Preserve the AA/LK bits in the branch instruction
999     uint8_t AALK = Loc[3];
1000     write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
1001     break;
1002   }
1003   case R_PPC64_ADDR16:
1004     checkInt<16>(Val, Type);
1005     write16be(Loc, Val);
1006     break;
1007   case R_PPC64_ADDR16_DS:
1008     checkInt<16>(Val, Type);
1009     write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
1010     break;
1011   case R_PPC64_ADDR16_HA:
1012     write16be(Loc, applyPPCHa(Val));
1013     break;
1014   case R_PPC64_ADDR16_HI:
1015     write16be(Loc, applyPPCHi(Val));
1016     break;
1017   case R_PPC64_ADDR16_HIGHER:
1018     write16be(Loc, applyPPCHigher(Val));
1019     break;
1020   case R_PPC64_ADDR16_HIGHERA:
1021     write16be(Loc, applyPPCHighera(Val));
1022     break;
1023   case R_PPC64_ADDR16_HIGHEST:
1024     write16be(Loc, applyPPCHighest(Val));
1025     break;
1026   case R_PPC64_ADDR16_HIGHESTA:
1027     write16be(Loc, applyPPCHighesta(Val));
1028     break;
1029   case R_PPC64_ADDR16_LO:
1030     write16be(Loc, applyPPCLo(Val));
1031     break;
1032   case R_PPC64_ADDR16_LO_DS:
1033     write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
1034     break;
1035   case R_PPC64_ADDR32:
1036     checkInt<32>(Val, Type);
1037     write32be(Loc, Val);
1038     break;
1039   case R_PPC64_ADDR64:
1040     write64be(Loc, Val);
1041     break;
1042   case R_PPC64_REL16_HA:
1043     write16be(Loc, applyPPCHa(Val));
1044     break;
1045   case R_PPC64_REL16_HI:
1046     write16be(Loc, applyPPCHi(Val));
1047     break;
1048   case R_PPC64_REL16_LO:
1049     write16be(Loc, applyPPCLo(Val));
1050     break;
1051   case R_PPC64_REL24: {
1052     uint32_t Mask = 0x03FFFFFC;
1053     checkInt<24>(Val, Type);
1054     write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
1055     break;
1056   }
1057   case R_PPC64_REL32:
1058     checkInt<32>(Val, Type);
1059     write32be(Loc, Val);
1060     break;
1061   case R_PPC64_REL64:
1062     write64be(Loc, Val);
1063     break;
1064   case R_PPC64_TOC:
1065     write64be(Loc, Val);
1066     break;
1067   default:
1068     fatal("unrecognized reloc " + Twine(Type));
1069   }
1070 }
1071 
1072 AArch64TargetInfo::AArch64TargetInfo() {
1073   CopyRel = R_AARCH64_COPY;
1074   RelativeRel = R_AARCH64_RELATIVE;
1075   IRelativeRel = R_AARCH64_IRELATIVE;
1076   GotRel = R_AARCH64_GLOB_DAT;
1077   PltRel = R_AARCH64_JUMP_SLOT;
1078   TlsDescRel = R_AARCH64_TLSDESC;
1079   TlsGotRel = R_AARCH64_TLS_TPREL64;
1080   PltEntrySize = 16;
1081   PltZeroSize = 32;
1082 
1083   // It doesn't seem to be documented anywhere, but tls on aarch64 uses variant
1084   // 1 of the tls structures and the tcb size is 16.
1085   TcbSize = 16;
1086 }
1087 
1088 RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
1089                                       const SymbolBody &S) const {
1090   switch (Type) {
1091   default:
1092     return R_ABS;
1093 
1094   case R_AARCH64_TLSDESC_ADR_PAGE21:
1095     return R_TLSDESC_PAGE;
1096 
1097   case R_AARCH64_TLSDESC_LD64_LO12_NC:
1098   case R_AARCH64_TLSDESC_ADD_LO12_NC:
1099     return R_TLSDESC;
1100 
1101   case R_AARCH64_TLSDESC_CALL:
1102     return R_HINT;
1103 
1104   case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1105   case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1106     return R_TLS;
1107 
1108   case R_AARCH64_CALL26:
1109   case R_AARCH64_CONDBR19:
1110   case R_AARCH64_JUMP26:
1111   case R_AARCH64_TSTBR14:
1112     return R_PLT_PC;
1113 
1114   case R_AARCH64_PREL16:
1115   case R_AARCH64_PREL32:
1116   case R_AARCH64_PREL64:
1117   case R_AARCH64_ADR_PREL_LO21:
1118     return R_PC;
1119   case R_AARCH64_ADR_PREL_PG_HI21:
1120     return R_PAGE_PC;
1121   case R_AARCH64_LD64_GOT_LO12_NC:
1122   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1123     return R_GOT;
1124   case R_AARCH64_ADR_GOT_PAGE:
1125   case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1126     return R_GOT_PAGE_PC;
1127   }
1128 }
1129 
1130 bool AArch64TargetInfo::usesOnlyLowPageBits(uint32_t Type) const {
1131   switch (Type) {
1132   default:
1133     return false;
1134   case R_AARCH64_ADD_ABS_LO12_NC:
1135   case R_AARCH64_LD64_GOT_LO12_NC:
1136   case R_AARCH64_LDST128_ABS_LO12_NC:
1137   case R_AARCH64_LDST16_ABS_LO12_NC:
1138   case R_AARCH64_LDST32_ABS_LO12_NC:
1139   case R_AARCH64_LDST64_ABS_LO12_NC:
1140   case R_AARCH64_LDST8_ABS_LO12_NC:
1141   case R_AARCH64_TLSDESC_ADD_LO12_NC:
1142   case R_AARCH64_TLSDESC_LD64_LO12_NC:
1143   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1144     return true;
1145   }
1146 }
1147 
1148 bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
1149   return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1150          Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1151 }
1152 
1153 uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
1154   if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1155     return Type;
1156   StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
1157   error("relocation " + S + " cannot be used when making a shared object; "
1158                             "recompile with -fPIC.");
1159   // Keep it going with a dummy value so that we can find more reloc errors.
1160   return R_AARCH64_ABS32;
1161 }
1162 
1163 void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1164   write64le(Buf, Out<ELF64LE>::Plt->getVA());
1165 }
1166 
1167 static uint64_t getAArch64Page(uint64_t Expr) {
1168   return Expr & (~static_cast<uint64_t>(0xFFF));
1169 }
1170 
1171 void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
1172   const uint8_t PltData[] = {
1173       0xf0, 0x7b, 0xbf, 0xa9, // stp	x16, x30, [sp,#-16]!
1174       0x10, 0x00, 0x00, 0x90, // adrp	x16, Page(&(.plt.got[2]))
1175       0x11, 0x02, 0x40, 0xf9, // ldr	x17, [x16, Offset(&(.plt.got[2]))]
1176       0x10, 0x02, 0x00, 0x91, // add	x16, x16, Offset(&(.plt.got[2]))
1177       0x20, 0x02, 0x1f, 0xd6, // br	x17
1178       0x1f, 0x20, 0x03, 0xd5, // nop
1179       0x1f, 0x20, 0x03, 0xd5, // nop
1180       0x1f, 0x20, 0x03, 0xd5  // nop
1181   };
1182   memcpy(Buf, PltData, sizeof(PltData));
1183 
1184   uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1185   uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1186   relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1187               getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1188   relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1189   relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
1190 }
1191 
1192 void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1193                                  uint64_t PltEntryAddr, int32_t Index,
1194                                  unsigned RelOff) const {
1195   const uint8_t Inst[] = {
1196       0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1197       0x11, 0x02, 0x40, 0xf9, // ldr  x17, [x16, Offset(&(.plt.got[n]))]
1198       0x10, 0x02, 0x00, 0x91, // add  x16, x16, Offset(&(.plt.got[n]))
1199       0x20, 0x02, 0x1f, 0xd6  // br   x17
1200   };
1201   memcpy(Buf, Inst, sizeof(Inst));
1202 
1203   relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1204               getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1205   relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1206   relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
1207 }
1208 
1209 static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
1210   uint32_t ImmLo = (Imm & 0x3) << 29;
1211   uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
1212   uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
1213   write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
1214 }
1215 
1216 static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1217   or32le(L, (Imm & 0xFFF) << 10);
1218 }
1219 
1220 void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1221                                     uint64_t Val) const {
1222   switch (Type) {
1223   case R_AARCH64_ABS16:
1224   case R_AARCH64_PREL16:
1225     checkIntUInt<16>(Val, Type);
1226     write16le(Loc, Val);
1227     break;
1228   case R_AARCH64_ABS32:
1229   case R_AARCH64_PREL32:
1230     checkIntUInt<32>(Val, Type);
1231     write32le(Loc, Val);
1232     break;
1233   case R_AARCH64_ABS64:
1234   case R_AARCH64_PREL64:
1235     write64le(Loc, Val);
1236     break;
1237   case R_AARCH64_ADD_ABS_LO12_NC:
1238     // This relocation stores 12 bits and there's no instruction
1239     // to do it. Instead, we do a 32 bits store of the value
1240     // of r_addend bitwise-or'ed Loc. This assumes that the addend
1241     // bits in Loc are zero.
1242     or32le(Loc, (Val & 0xFFF) << 10);
1243     break;
1244   case R_AARCH64_ADR_GOT_PAGE:
1245   case R_AARCH64_ADR_PREL_PG_HI21:
1246   case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1247   case R_AARCH64_TLSDESC_ADR_PAGE21:
1248     checkInt<33>(Val, Type);
1249     updateAArch64Addr(Loc, Val >> 12);
1250     break;
1251   case R_AARCH64_ADR_PREL_LO21:
1252     checkInt<21>(Val, Type);
1253     updateAArch64Addr(Loc, Val);
1254     break;
1255   case R_AARCH64_CALL26:
1256   case R_AARCH64_JUMP26:
1257     checkInt<28>(Val, Type);
1258     or32le(Loc, (Val & 0x0FFFFFFC) >> 2);
1259     break;
1260   case R_AARCH64_CONDBR19:
1261     checkInt<21>(Val, Type);
1262     or32le(Loc, (Val & 0x1FFFFC) << 3);
1263     break;
1264   case R_AARCH64_LD64_GOT_LO12_NC:
1265   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1266   case R_AARCH64_TLSDESC_LD64_LO12_NC:
1267     checkAlignment<8>(Val, Type);
1268     or32le(Loc, (Val & 0xFF8) << 7);
1269     break;
1270   case R_AARCH64_LDST128_ABS_LO12_NC:
1271     or32le(Loc, (Val & 0x0FF8) << 6);
1272     break;
1273   case R_AARCH64_LDST16_ABS_LO12_NC:
1274     or32le(Loc, (Val & 0x0FFC) << 9);
1275     break;
1276   case R_AARCH64_LDST8_ABS_LO12_NC:
1277     or32le(Loc, (Val & 0xFFF) << 10);
1278     break;
1279   case R_AARCH64_LDST32_ABS_LO12_NC:
1280     or32le(Loc, (Val & 0xFFC) << 8);
1281     break;
1282   case R_AARCH64_LDST64_ABS_LO12_NC:
1283     or32le(Loc, (Val & 0xFF8) << 7);
1284     break;
1285   case R_AARCH64_TSTBR14:
1286     checkInt<16>(Val, Type);
1287     or32le(Loc, (Val & 0xFFFC) << 3);
1288     break;
1289   case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1290     checkInt<24>(Val, Type);
1291     updateAArch64Add(Loc, Val >> 12);
1292     break;
1293   case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1294   case R_AARCH64_TLSDESC_ADD_LO12_NC:
1295     updateAArch64Add(Loc, Val);
1296     break;
1297   default:
1298     fatal("unrecognized reloc " + Twine(Type));
1299   }
1300 }
1301 
1302 void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1303                                        uint64_t Val) const {
1304   // TLSDESC Global-Dynamic relocation are in the form:
1305   //   adrp    x0, :tlsdesc:v             [R_AARCH64_TLSDESC_ADR_PAGE21]
1306   //   ldr     x1, [x0, #:tlsdesc_lo12:v  [R_AARCH64_TLSDESC_LD64_LO12_NC]
1307   //   add     x0, x0, :tlsdesc_los:v     [_AARCH64_TLSDESC_ADD_LO12_NC]
1308   //   .tlsdesccall                       [R_AARCH64_TLSDESC_CALL]
1309   // And it can optimized to:
1310   //   movz    x0, #0x0, lsl #16
1311   //   movk    x0, #0x10
1312   //   nop
1313   //   nop
1314   checkUInt<32>(Val, Type);
1315 
1316   uint32_t NewInst;
1317   switch (Type) {
1318   case R_AARCH64_TLSDESC_ADD_LO12_NC:
1319   case R_AARCH64_TLSDESC_CALL:
1320     // nop
1321     NewInst = 0xd503201f;
1322     break;
1323   case R_AARCH64_TLSDESC_ADR_PAGE21:
1324     // movz
1325     NewInst = 0xd2a00000 | (((Val >> 16) & 0xffff) << 5);
1326     break;
1327   case R_AARCH64_TLSDESC_LD64_LO12_NC:
1328     // movk
1329     NewInst = 0xf2800000 | ((Val & 0xffff) << 5);
1330     break;
1331   default:
1332     llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
1333   }
1334   write32le(Loc, NewInst);
1335 }
1336 
1337 void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1338                                        uint64_t Val) const {
1339   checkUInt<32>(Val, Type);
1340 
1341   uint32_t Inst = read32le(Loc);
1342   uint32_t NewInst;
1343   if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1344     // Generate movz.
1345     unsigned RegNo = (Inst & 0x1f);
1346     NewInst = (0xd2a00000 | RegNo) | (((Val >> 16) & 0xffff) << 5);
1347   } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1348     // Generate movk
1349     unsigned RegNo = (Inst & 0x1f);
1350     NewInst = (0xf2800000 | RegNo) | ((Val & 0xffff) << 5);
1351   } else {
1352     llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
1353   }
1354   write32le(Loc, NewInst);
1355 }
1356 
1357 // Implementing relocations for AMDGPU is low priority since most
1358 // programs don't use relocations now. Thus, this function is not
1359 // actually called (relocateOne is called for each relocation).
1360 // That's why the AMDGPU port works without implementing this function.
1361 void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1362                                    uint64_t Val) const {
1363   llvm_unreachable("not implemented");
1364 }
1365 
1366 RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
1367   llvm_unreachable("not implemented");
1368 }
1369 
1370 template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
1371   GotPltHeaderEntriesNum = 2;
1372   PageSize = 65536;
1373   PltEntrySize = 16;
1374   PltZeroSize = 32;
1375   ThunkSize = 16;
1376   CopyRel = R_MIPS_COPY;
1377   PltRel = R_MIPS_JUMP_SLOT;
1378   if (ELFT::Is64Bits)
1379     RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
1380   else
1381     RelativeRel = R_MIPS_REL32;
1382 }
1383 
1384 template <class ELFT>
1385 RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1386                                          const SymbolBody &S) const {
1387   if (ELFT::Is64Bits)
1388     // See comment in the calculateMips64RelChain.
1389     Type &= 0xff;
1390   switch (Type) {
1391   default:
1392     return R_ABS;
1393   case R_MIPS_JALR:
1394     return R_HINT;
1395   case R_MIPS_GPREL16:
1396   case R_MIPS_GPREL32:
1397     return R_GOTREL;
1398   case R_MIPS_26:
1399     return R_PLT;
1400   case R_MIPS_HI16:
1401   case R_MIPS_LO16:
1402   case R_MIPS_GOT_OFST:
1403     // MIPS _gp_disp designates offset between start of function and 'gp'
1404     // pointer into GOT. __gnu_local_gp is equal to the current value of
1405     // the 'gp'. Therefore any relocations against them do not require
1406     // dynamic relocation.
1407     if (&S == ElfSym<ELFT>::MipsGpDisp)
1408       return R_PC;
1409     return R_ABS;
1410   case R_MIPS_PC32:
1411   case R_MIPS_PC16:
1412   case R_MIPS_PC19_S2:
1413   case R_MIPS_PC21_S2:
1414   case R_MIPS_PC26_S2:
1415   case R_MIPS_PCHI16:
1416   case R_MIPS_PCLO16:
1417     return R_PC;
1418   case R_MIPS_GOT16:
1419     if (S.isLocal())
1420       return R_MIPS_GOT_LOCAL_PAGE;
1421   // fallthrough
1422   case R_MIPS_CALL16:
1423   case R_MIPS_GOT_DISP:
1424     if (!S.isPreemptible())
1425       return R_MIPS_GOT_LOCAL;
1426     return R_GOT_OFF;
1427   case R_MIPS_GOT_PAGE:
1428     return R_MIPS_GOT_LOCAL_PAGE;
1429   }
1430 }
1431 
1432 template <class ELFT>
1433 uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
1434   if (Type == R_MIPS_32 || Type == R_MIPS_64)
1435     return RelativeRel;
1436   StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
1437   error("relocation " + S + " cannot be used when making a shared object; "
1438                             "recompile with -fPIC.");
1439   // Keep it going with a dummy value so that we can find more reloc errors.
1440   return R_MIPS_32;
1441 }
1442 
1443 template <class ELFT>
1444 void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1445   write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
1446 }
1447 
1448 static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
1449 
1450 template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
1451 static int64_t getPcRelocAddend(const uint8_t *Loc) {
1452   uint32_t Instr = read32<E>(Loc);
1453   uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1454   return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1455 }
1456 
1457 template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
1458 static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
1459   uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1460   uint32_t Instr = read32<E>(Loc);
1461   if (SHIFT > 0)
1462     checkAlignment<(1 << SHIFT)>(V, Type);
1463   checkInt<BSIZE + SHIFT>(V, Type);
1464   write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
1465 }
1466 
1467 template <endianness E>
1468 static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
1469   uint32_t Instr = read32<E>(Loc);
1470   write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
1471 }
1472 
1473 template <endianness E>
1474 static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1475   uint32_t Instr = read32<E>(Loc);
1476   write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1477 }
1478 
1479 template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
1480   return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1481 }
1482 
1483 template <class ELFT>
1484 void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1485   const endianness E = ELFT::TargetEndianness;
1486   write32<E>(Buf, 0x3c1c0000);      // lui   $28, %hi(&GOTPLT[0])
1487   write32<E>(Buf + 4, 0x8f990000);  // lw    $25, %lo(&GOTPLT[0])($28)
1488   write32<E>(Buf + 8, 0x279c0000);  // addiu $28, $28, %lo(&GOTPLT[0])
1489   write32<E>(Buf + 12, 0x031cc023); // subu  $24, $24, $28
1490   write32<E>(Buf + 16, 0x03e07825); // move  $15, $31
1491   write32<E>(Buf + 20, 0x0018c082); // srl   $24, $24, 2
1492   write32<E>(Buf + 24, 0x0320f809); // jalr  $25
1493   write32<E>(Buf + 28, 0x2718fffe); // subu  $24, $24, 2
1494   uint64_t Got = Out<ELFT>::GotPlt->getVA();
1495   writeMipsHi16<E>(Buf, Got);
1496   writeMipsLo16<E>(Buf + 4, Got);
1497   writeMipsLo16<E>(Buf + 8, Got);
1498 }
1499 
1500 template <class ELFT>
1501 void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1502                                     uint64_t PltEntryAddr, int32_t Index,
1503                                     unsigned RelOff) const {
1504   const endianness E = ELFT::TargetEndianness;
1505   write32<E>(Buf, 0x3c0f0000);      // lui   $15, %hi(.got.plt entry)
1506   write32<E>(Buf + 4, 0x8df90000);  // l[wd] $25, %lo(.got.plt entry)($15)
1507   write32<E>(Buf + 8, 0x03200008);  // jr    $25
1508   write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
1509   writeMipsHi16<E>(Buf, GotEntryAddr);
1510   writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1511   writeMipsLo16<E>(Buf + 12, GotEntryAddr);
1512 }
1513 
1514 template <class ELFT>
1515 void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1516   // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1517   // See MipsTargetInfo::writeThunk for details.
1518   const endianness E = ELFT::TargetEndianness;
1519   write32<E>(Buf, 0x3c190000);      // lui   $25, %hi(func)
1520   write32<E>(Buf + 4, 0x08000000);  // j     func
1521   write32<E>(Buf + 8, 0x27390000);  // addiu $25, $25, %lo(func)
1522   write32<E>(Buf + 12, 0x00000000); // nop
1523   writeMipsHi16<E>(Buf, S);
1524   write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1525   writeMipsLo16<E>(Buf + 8, S);
1526 }
1527 
1528 template <class ELFT>
1529 bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1530                                       const SymbolBody &S) const {
1531   // Any MIPS PIC code function is invoked with its address in register $t9.
1532   // So if we have a branch instruction from non-PIC code to the PIC one
1533   // we cannot make the jump directly and need to create a small stubs
1534   // to save the target function address.
1535   // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1536   if (Type != R_MIPS_26)
1537     return false;
1538   auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1539   if (!F)
1540     return false;
1541   // If current file has PIC code, LA25 stub is not required.
1542   if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1543     return false;
1544   auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1545   if (!D || !D->Section)
1546     return false;
1547   // LA25 is required if target file has PIC code
1548   // or target symbol is a PIC symbol.
1549   return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
1550          (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
1551 }
1552 
1553 template <class ELFT>
1554 uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
1555                                                  uint32_t Type) const {
1556   const endianness E = ELFT::TargetEndianness;
1557   switch (Type) {
1558   default:
1559     return 0;
1560   case R_MIPS_32:
1561   case R_MIPS_GPREL32:
1562     return read32<E>(Buf);
1563   case R_MIPS_26:
1564     // FIXME (simon): If the relocation target symbol is not a PLT entry
1565     // we should use another expression for calculation:
1566     // ((A << 2) | (P & 0xf0000000)) >> 2
1567     return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1568   case R_MIPS_GPREL16:
1569   case R_MIPS_LO16:
1570   case R_MIPS_PCLO16:
1571   case R_MIPS_TLS_DTPREL_HI16:
1572   case R_MIPS_TLS_DTPREL_LO16:
1573   case R_MIPS_TLS_TPREL_HI16:
1574   case R_MIPS_TLS_TPREL_LO16:
1575     return readSignedLo16<E>(Buf);
1576   case R_MIPS_PC16:
1577     return getPcRelocAddend<E, 16, 2>(Buf);
1578   case R_MIPS_PC19_S2:
1579     return getPcRelocAddend<E, 19, 2>(Buf);
1580   case R_MIPS_PC21_S2:
1581     return getPcRelocAddend<E, 21, 2>(Buf);
1582   case R_MIPS_PC26_S2:
1583     return getPcRelocAddend<E, 26, 2>(Buf);
1584   case R_MIPS_PC32:
1585     return getPcRelocAddend<E, 32, 0>(Buf);
1586   }
1587 }
1588 
1589 static std::pair<uint32_t, uint64_t> calculateMips64RelChain(uint32_t Type,
1590                                                              uint64_t Val) {
1591   // MIPS N64 ABI packs multiple relocations into the single relocation
1592   // record. In general, all up to three relocations can have arbitrary
1593   // types. In fact, Clang and GCC uses only a few combinations. For now,
1594   // we support two of them. That is allow to pass at least all LLVM
1595   // test suite cases.
1596   // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
1597   // <any relocation> / R_MIPS_64 / R_MIPS_NONE
1598   // The first relocation is a 'real' relocation which is calculated
1599   // using the corresponding symbol's value. The second and the third
1600   // relocations used to modify result of the first one: extend it to
1601   // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
1602   // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
1603   uint32_t Type2 = (Type >> 8) & 0xff;
1604   uint32_t Type3 = (Type >> 16) & 0xff;
1605   if (Type2 == R_MIPS_NONE && Type3 == R_MIPS_NONE)
1606     return std::make_pair(Type, Val);
1607   if (Type2 == R_MIPS_64 && Type3 == R_MIPS_NONE)
1608     return std::make_pair(Type2, Val);
1609   if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16))
1610     return std::make_pair(Type3, -Val);
1611   error("unsupported relocations combination " + Twine(Type));
1612   return std::make_pair(Type & 0xff, Val);
1613 }
1614 
1615 template <class ELFT>
1616 void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1617                                        uint64_t Val) const {
1618   const endianness E = ELFT::TargetEndianness;
1619   // Thread pointer and DRP offsets from the start of TLS data area.
1620   // https://www.linux-mips.org/wiki/NPTL
1621   if (Type == R_MIPS_TLS_DTPREL_HI16 || Type == R_MIPS_TLS_DTPREL_LO16)
1622     Val -= 0x8000;
1623   else if (Type == R_MIPS_TLS_TPREL_HI16 || Type == R_MIPS_TLS_TPREL_LO16)
1624     Val -= 0x7000;
1625   if (ELFT::Is64Bits)
1626     std::tie(Type, Val) = calculateMips64RelChain(Type, Val);
1627   switch (Type) {
1628   case R_MIPS_32:
1629   case R_MIPS_GPREL32:
1630     write32<E>(Loc, Val);
1631     break;
1632   case R_MIPS_64:
1633     write64<E>(Loc, Val);
1634     break;
1635   case R_MIPS_26:
1636     write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | (Val >> 2));
1637     break;
1638   case R_MIPS_GOT_DISP:
1639   case R_MIPS_GOT_PAGE:
1640   case R_MIPS_GOT16:
1641   case R_MIPS_GPREL16:
1642     checkInt<16>(Val, Type);
1643   // fallthrough
1644   case R_MIPS_CALL16:
1645   case R_MIPS_GOT_OFST:
1646   case R_MIPS_LO16:
1647   case R_MIPS_PCLO16:
1648   case R_MIPS_TLS_DTPREL_LO16:
1649   case R_MIPS_TLS_TPREL_LO16:
1650     writeMipsLo16<E>(Loc, Val);
1651     break;
1652   case R_MIPS_HI16:
1653   case R_MIPS_PCHI16:
1654   case R_MIPS_TLS_DTPREL_HI16:
1655   case R_MIPS_TLS_TPREL_HI16:
1656     writeMipsHi16<E>(Loc, Val);
1657     break;
1658   case R_MIPS_JALR:
1659     // Ignore this optimization relocation for now
1660     break;
1661   case R_MIPS_PC16:
1662     applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
1663     break;
1664   case R_MIPS_PC19_S2:
1665     applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
1666     break;
1667   case R_MIPS_PC21_S2:
1668     applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
1669     break;
1670   case R_MIPS_PC26_S2:
1671     applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
1672     break;
1673   case R_MIPS_PC32:
1674     applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
1675     break;
1676   default:
1677     fatal("unrecognized reloc " + Twine(Type));
1678   }
1679 }
1680 
1681 template <class ELFT>
1682 bool MipsTargetInfo<ELFT>::usesOnlyLowPageBits(uint32_t Type) const {
1683   return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST;
1684 }
1685 }
1686 }
1687