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