xref: /llvm-project-15.0.7/lld/ELF/Target.cpp (revision c744e120)
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. SA is S+A.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "Target.h"
19 #include "Error.h"
20 #include "OutputSections.h"
21 #include "Symbols.h"
22 
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/Object/ELF.h"
25 #include "llvm/Support/Endian.h"
26 #include "llvm/Support/ELF.h"
27 
28 using namespace llvm;
29 using namespace llvm::object;
30 using namespace llvm::support::endian;
31 using namespace llvm::ELF;
32 
33 namespace lld {
34 namespace elf2 {
35 
36 std::unique_ptr<TargetInfo> Target;
37 
38 template <endianness E> static void add32(void *P, int32_t V) {
39   write32<E>(P, read32<E>(P) + V);
40 }
41 
42 static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); }
43 static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
44 
45 namespace {
46 class X86TargetInfo final : public TargetInfo {
47 public:
48   X86TargetInfo();
49   void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
50   void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
51                          uint64_t PltEntryAddr) const override;
52   void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
53                      uint64_t PltEntryAddr, int32_t Index) const override;
54   bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
55   bool relocPointsToGot(uint32_t Type) const override;
56   bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
57   void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
58                    uint64_t SA) const override;
59 };
60 
61 class X86_64TargetInfo final : public TargetInfo {
62 public:
63   X86_64TargetInfo();
64   unsigned getPLTRefReloc(unsigned Type) const override;
65   void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
66   void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
67                          uint64_t PltEntryAddr) const override;
68   void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
69                      uint64_t PltEntryAddr, int32_t Index) const override;
70   bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
71   bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
72   bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
73   void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
74                    uint64_t SA) const override;
75   bool isRelRelative(uint32_t Type) const override;
76 };
77 
78 class PPC64TargetInfo final : public TargetInfo {
79 public:
80   PPC64TargetInfo();
81   void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
82   void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
83                          uint64_t PltEntryAddr) const override;
84   void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
85                      uint64_t PltEntryAddr, int32_t Index) const override;
86   bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
87   bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
88   void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
89                    uint64_t SA) const override;
90   bool isRelRelative(uint32_t Type) const override;
91 };
92 
93 class AArch64TargetInfo final : public TargetInfo {
94 public:
95   AArch64TargetInfo();
96   void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
97   void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
98                          uint64_t PltEntryAddr) const override;
99   void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
100                      uint64_t PltEntryAddr, int32_t Index) const override;
101   bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
102   bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
103   void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
104                    uint64_t SA) const override;
105 };
106 
107 template <class ELFT> class MipsTargetInfo final : public TargetInfo {
108 public:
109   MipsTargetInfo();
110   void writeGotHeaderEntries(uint8_t *Buf) const override;
111   void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
112   void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
113                          uint64_t PltEntryAddr) const override;
114   void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
115                      uint64_t PltEntryAddr, int32_t Index) const override;
116   bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
117   bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
118   void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
119                    uint64_t SA) const override;
120 };
121 } // anonymous namespace
122 
123 TargetInfo *createTarget() {
124   switch (Config->EMachine) {
125   case EM_386:
126     return new X86TargetInfo();
127   case EM_AARCH64:
128     return new AArch64TargetInfo();
129   case EM_MIPS:
130     switch (Config->EKind) {
131     case ELF32LEKind:
132       return new MipsTargetInfo<ELF32LE>();
133     case ELF32BEKind:
134       return new MipsTargetInfo<ELF32BE>();
135     default:
136       error("Unsupported MIPS target");
137     }
138   case EM_PPC64:
139     return new PPC64TargetInfo();
140   case EM_X86_64:
141     return new X86_64TargetInfo();
142   }
143   error("Unknown target machine");
144 }
145 
146 TargetInfo::~TargetInfo() {}
147 
148 uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
149 
150 bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
151   return false;
152 }
153 
154 unsigned TargetInfo::getPLTRefReloc(unsigned Type) const { return PCRelReloc; }
155 
156 bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; }
157 
158 bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
159 
160 void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
161 
162 X86TargetInfo::X86TargetInfo() {
163   PCRelReloc = R_386_PC32;
164   GotReloc = R_386_GLOB_DAT;
165   GotRefReloc = R_386_GOT32;
166   PltReloc = R_386_JUMP_SLOT;
167 }
168 
169 void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
170 void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
171                                       uint64_t PltEntryAddr) const {}
172 
173 void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
174                                   uint64_t PltEntryAddr, int32_t Index) const {
175   // jmpl *val; nop; nop
176   const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
177   memcpy(Buf, Inst, sizeof(Inst));
178   assert(isUInt<32>(GotEntryAddr));
179   write32le(Buf + 2, GotEntryAddr);
180 }
181 
182 bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
183   return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
184 }
185 
186 bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
187   return Type == R_386_GOTPC;
188 }
189 
190 bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
191   return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
192 }
193 
194 void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
195                                 uint64_t P, uint64_t SA) const {
196   switch (Type) {
197   case R_386_GOT32:
198     add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
199     break;
200   case R_386_PC32:
201     add32le(Loc, SA - P);
202     break;
203   case R_386_32:
204     add32le(Loc, SA);
205     break;
206   default:
207     error("unrecognized reloc " + Twine(Type));
208   }
209 }
210 
211 X86_64TargetInfo::X86_64TargetInfo() {
212   CopyReloc = R_X86_64_COPY;
213   PCRelReloc = R_X86_64_PC32;
214   GotReloc = R_X86_64_GLOB_DAT;
215   GotRefReloc = R_X86_64_PC32;
216   PltReloc = R_X86_64_JUMP_SLOT;
217   RelativeReloc = R_X86_64_RELATIVE;
218   TlsLocalDynamicReloc = R_X86_64_TLSLD;
219   TlsModuleIndexReloc = R_X86_64_DTPMOD64;
220   LazyRelocations = true;
221   PltEntrySize = 16;
222   PltZeroEntrySize = 16;
223 }
224 
225 void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
226   // Skip 6 bytes of "jmpq *got(%rip)"
227   write32le(Buf, Plt + 6);
228 }
229 
230 void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
231                                          uint64_t PltEntryAddr) const {
232   const uint8_t PltData[] = {
233       0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
234       0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
235       0x0f, 0x1f, 0x40, 0x00              // nopl 0x0(rax)
236   };
237   memcpy(Buf, PltData, sizeof(PltData));
238   write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
239   write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
240 }
241 
242 void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
243                                      uint64_t PltEntryAddr,
244                                      int32_t Index) const {
245   const uint8_t Inst[] = {
246       0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
247       0x68, 0x00, 0x00, 0x00, 0x00,       // pushq <relocation index>
248       0xe9, 0x00, 0x00, 0x00, 0x00        // jmpq plt[0]
249   };
250   memcpy(Buf, Inst, sizeof(Inst));
251 
252   write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
253   write32le(Buf + 7, Index);
254   write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
255 }
256 
257 bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
258                                       const SymbolBody &S) const {
259   if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
260       Type == R_X86_64_64)
261     if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
262       return SS->Sym.getType() == STT_OBJECT;
263   return false;
264 }
265 
266 bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
267   return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
268 }
269 
270 unsigned X86_64TargetInfo::getPLTRefReloc(unsigned Type) const {
271   if (Type == R_X86_64_PLT32)
272     return R_X86_64_PC32;
273   return Type;
274 }
275 
276 bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
277   if (relocNeedsCopy(Type, S))
278     return false;
279 
280   switch (Type) {
281   default:
282     return false;
283   case R_X86_64_32:
284   case R_X86_64_64:
285   case R_X86_64_PC32:
286     // This relocation is defined to have a value of (S + A - P).
287     // The problems start when a non PIC program calls a function in a shared
288     // library.
289     // In an ideal world, we could just report an error saying the relocation
290     // can overflow at runtime.
291     // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
292     // libc.so.
293     //
294     // The general idea on how to handle such cases is to create a PLT entry
295     // and use that as the function value.
296     //
297     // For the static linking part, we just return true and everything else
298     // will use the the PLT entry as the address.
299     //
300     // The remaining (unimplemented) problem is making sure pointer equality
301     // still works. We need the help of the dynamic linker for that. We
302     // let it know that we have a direct reference to a so symbol by creating
303     // an undefined symbol with a non zero st_value. Seeing that, the
304     // dynamic linker resolves the symbol to the value of the symbol we created.
305     // This is true even for got entries, so pointer equality is maintained.
306     // To avoid an infinite loop, the only entry that points to the
307     // real function is a dedicated got entry used by the plt. That is
308     // identified by special relocation types (R_X86_64_JUMP_SLOT,
309     // R_386_JMP_SLOT, etc).
310     return S.isShared();
311   case R_X86_64_PLT32:
312     return canBePreempted(&S, true);
313   }
314 }
315 
316 bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
317   switch (Type) {
318   default:
319     return false;
320   case R_X86_64_PC64:
321   case R_X86_64_PC32:
322   case R_X86_64_PC16:
323   case R_X86_64_PC8:
324   case R_X86_64_PLT32:
325   case R_X86_64_DTPOFF32:
326   case R_X86_64_DTPOFF64:
327     return true;
328   }
329 }
330 
331 void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
332                                    uint64_t P, uint64_t SA) const {
333   switch (Type) {
334   case R_X86_64_PC32:
335   case R_X86_64_GOTPCREL:
336   case R_X86_64_PLT32:
337   case R_X86_64_TLSLD:
338     write32le(Loc, SA - P);
339     break;
340   case R_X86_64_64:
341   case R_X86_64_DTPOFF64:
342     write64le(Loc, SA);
343     break;
344   case R_X86_64_32:
345   case R_X86_64_32S:
346     if (Type == R_X86_64_32 && !isUInt<32>(SA))
347       error("R_X86_64_32 out of range");
348     else if (!isInt<32>(SA))
349       error("R_X86_64_32S out of range");
350     write32le(Loc, SA);
351     break;
352   case R_X86_64_DTPOFF32:
353     write32le(Loc, SA);
354     break;
355   case R_X86_64_TPOFF32: {
356     uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
357     if (!isInt<32>(Val))
358       error("R_X86_64_TPOFF32 out of range");
359     write32le(Loc, Val);
360     break;
361   }
362   default:
363     error("unrecognized reloc " + Twine(Type));
364   }
365 }
366 
367 // Relocation masks following the #lo(value), #hi(value), #ha(value),
368 // #higher(value), #highera(value), #highest(value), and #highesta(value)
369 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
370 // document.
371 static uint16_t applyPPCLo(uint64_t V) { return V; }
372 static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
373 static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
374 static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
375 static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
376 static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
377 static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
378 
379 PPC64TargetInfo::PPC64TargetInfo() {
380   PCRelReloc = R_PPC64_REL24;
381   GotReloc = R_PPC64_GLOB_DAT;
382   GotRefReloc = R_PPC64_REL64;
383   RelativeReloc = R_PPC64_RELATIVE;
384   PltEntrySize = 32;
385 
386   // We need 64K pages (at least under glibc/Linux, the loader won't
387   // set different permissions on a finer granularity than that).
388   PageSize = 65536;
389 
390   // The PPC64 ELF ABI v1 spec, says:
391   //
392   //   It is normally desirable to put segments with different characteristics
393   //   in separate 256 Mbyte portions of the address space, to give the
394   //   operating system full paging flexibility in the 64-bit address space.
395   //
396   // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
397   // use 0x10000000 as the starting address.
398   VAStart = 0x10000000;
399 }
400 
401 uint64_t getPPC64TocBase() {
402   // The TOC consists of sections .got, .toc, .tocbss, .plt in that
403   // order. The TOC starts where the first of these sections starts.
404 
405   // FIXME: This obviously does not do the right thing when there is no .got
406   // section, but there is a .toc or .tocbss section.
407   uint64_t TocVA = Out<ELF64BE>::Got->getVA();
408   if (!TocVA)
409     TocVA = Out<ELF64BE>::Plt->getVA();
410 
411   // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
412   // thus permitting a full 64 Kbytes segment. Note that the glibc startup
413   // code (crt1.o) assumes that you can get from the TOC base to the
414   // start of the .toc section with only a single (signed) 16-bit relocation.
415   return TocVA + 0x8000;
416 }
417 
418 void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
419 void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
420                                         uint64_t PltEntryAddr) const {}
421 void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
422                                     uint64_t PltEntryAddr, int32_t Index) const {
423   uint64_t Off = GotEntryAddr - getPPC64TocBase();
424 
425   // FIXME: What we should do, in theory, is get the offset of the function
426   // descriptor in the .opd section, and use that as the offset from %r2 (the
427   // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
428   // be a pointer to the function descriptor in the .opd section. Using
429   // this scheme is simpler, but requires an extra indirection per PLT dispatch.
430 
431   write32be(Buf,      0xf8410028);                   // std %r2, 40(%r1)
432   write32be(Buf + 4,  0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
433   write32be(Buf + 8,  0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
434   write32be(Buf + 12, 0xe96c0000);                   // ld %r11,0(%r12)
435   write32be(Buf + 16, 0x7d6903a6);                   // mtctr %r11
436   write32be(Buf + 20, 0xe84c0008);                   // ld %r2,8(%r12)
437   write32be(Buf + 24, 0xe96c0010);                   // ld %r11,16(%r12)
438   write32be(Buf + 28, 0x4e800420);                   // bctr
439 }
440 
441 bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
442   if (relocNeedsPlt(Type, S))
443     return true;
444 
445   switch (Type) {
446   default: return false;
447   case R_PPC64_GOT16:
448   case R_PPC64_GOT16_LO:
449   case R_PPC64_GOT16_HI:
450   case R_PPC64_GOT16_HA:
451   case R_PPC64_GOT16_DS:
452   case R_PPC64_GOT16_LO_DS:
453     return true;
454   }
455 }
456 
457 bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
458   // These are function calls that need to be redirected through a PLT stub.
459   return Type == R_PPC64_REL24 && canBePreempted(&S, false);
460 }
461 
462 bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
463   switch (Type) {
464   default:
465     return true;
466   case R_PPC64_TOC:
467   case R_PPC64_ADDR64:
468     return false;
469   }
470 }
471 
472 void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
473                                   uint64_t P, uint64_t SA) const {
474   uint64_t TB = getPPC64TocBase();
475 
476   // For a TOC-relative relocation, adjust the addend and proceed in terms of
477   // the corresponding ADDR16 relocation type.
478   switch (Type) {
479   case R_PPC64_TOC16:       Type = R_PPC64_ADDR16;       SA -= TB; break;
480   case R_PPC64_TOC16_DS:    Type = R_PPC64_ADDR16_DS;    SA -= TB; break;
481   case R_PPC64_TOC16_LO:    Type = R_PPC64_ADDR16_LO;    SA -= TB; break;
482   case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
483   case R_PPC64_TOC16_HI:    Type = R_PPC64_ADDR16_HI;    SA -= TB; break;
484   case R_PPC64_TOC16_HA:    Type = R_PPC64_ADDR16_HA;    SA -= TB; break;
485   default: break;
486   }
487 
488   switch (Type) {
489   case R_PPC64_ADDR16:
490     if (!isInt<16>(SA))
491       error("Relocation R_PPC64_ADDR16 overflow");
492     write16be(Loc, SA);
493     break;
494   case R_PPC64_ADDR16_DS:
495     if (!isInt<16>(SA))
496       error("Relocation R_PPC64_ADDR16_DS overflow");
497     write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
498     break;
499   case R_PPC64_ADDR16_LO:
500     write16be(Loc, applyPPCLo(SA));
501     break;
502   case R_PPC64_ADDR16_LO_DS:
503     write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
504     break;
505   case R_PPC64_ADDR16_HI:
506     write16be(Loc, applyPPCHi(SA));
507     break;
508   case R_PPC64_ADDR16_HA:
509     write16be(Loc, applyPPCHa(SA));
510     break;
511   case R_PPC64_ADDR16_HIGHER:
512     write16be(Loc, applyPPCHigher(SA));
513     break;
514   case R_PPC64_ADDR16_HIGHERA:
515     write16be(Loc, applyPPCHighera(SA));
516     break;
517   case R_PPC64_ADDR16_HIGHEST:
518     write16be(Loc, applyPPCHighest(SA));
519     break;
520   case R_PPC64_ADDR16_HIGHESTA:
521     write16be(Loc, applyPPCHighesta(SA));
522     break;
523   case R_PPC64_ADDR14: {
524     if ((SA & 3) != 0)
525       error("Improper alignment for relocation R_PPC64_ADDR14");
526 
527     // Preserve the AA/LK bits in the branch instruction
528     uint8_t AALK = Loc[3];
529     write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
530     break;
531   }
532   case R_PPC64_REL16_LO:
533     write16be(Loc, applyPPCLo(SA - P));
534     break;
535   case R_PPC64_REL16_HI:
536     write16be(Loc, applyPPCHi(SA - P));
537     break;
538   case R_PPC64_REL16_HA:
539     write16be(Loc, applyPPCHa(SA - P));
540     break;
541   case R_PPC64_ADDR32:
542     if (!isInt<32>(SA))
543       error("Relocation R_PPC64_ADDR32 overflow");
544     write32be(Loc, SA);
545     break;
546   case R_PPC64_REL24: {
547     // If we have an undefined weak symbol, we might get here with a symbol
548     // address of zero. That could overflow, but the code must be unreachable,
549     // so don't bother doing anything at all.
550     if (!SA)
551       break;
552 
553     uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
554     uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
555     bool InPlt = PltStart <= SA && SA < PltEnd;
556 
557     if (!InPlt && Out<ELF64BE>::Opd) {
558       // If this is a local call, and we currently have the address of a
559       // function-descriptor, get the underlying code address instead.
560       uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
561       uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
562       bool InOpd = OpdStart <= SA && SA < OpdEnd;
563 
564       if (InOpd)
565         SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
566     }
567 
568     uint32_t Mask = 0x03FFFFFC;
569     if (!isInt<24>(SA - P))
570       error("Relocation R_PPC64_REL24 overflow");
571     write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
572 
573     uint32_t Nop = 0x60000000;
574     if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
575       write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
576     break;
577   }
578   case R_PPC64_REL32:
579     if (!isInt<32>(SA - P))
580       error("Relocation R_PPC64_REL32 overflow");
581     write32be(Loc, SA - P);
582     break;
583   case R_PPC64_REL64:
584     write64be(Loc, SA - P);
585     break;
586   case R_PPC64_ADDR64:
587   case R_PPC64_TOC:
588     write64be(Loc, SA);
589     break;
590   default:
591     error("unrecognized reloc " + Twine(Type));
592   }
593 }
594 
595 AArch64TargetInfo::AArch64TargetInfo() {}
596 
597 void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
598 void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
599                                           uint64_t PltEntryAddr) const {}
600 void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
601                                       uint64_t PltEntryAddr, int32_t Index) const {}
602 bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
603                                       const SymbolBody &S) const {
604   return false;
605 }
606 bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
607                                       const SymbolBody &S) const {
608   return false;
609 }
610 
611 static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
612   uint32_t ImmLo = (Imm & 0x3) << 29;
613   uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
614   uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
615   write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
616 }
617 
618 // Page(Expr) is the page address of the expression Expr, defined
619 // as (Expr & ~0xFFF). (This applies even if the machine page size
620 // supported by the platform has a different value.)
621 static uint64_t getAArch64Page(uint64_t Expr) {
622   return Expr & (~static_cast<uint64_t>(0xFFF));
623 }
624 
625 void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
626                                     uint32_t Type, uint64_t P,
627                                     uint64_t SA) const {
628   switch (Type) {
629   case R_AARCH64_ABS16:
630     if (!isInt<16>(SA))
631       error("Relocation R_AARCH64_ABS16 out of range");
632     write16le(Loc, SA);
633     break;
634   case R_AARCH64_ABS32:
635     if (!isInt<32>(SA))
636       error("Relocation R_AARCH64_ABS32 out of range");
637     write32le(Loc, SA);
638     break;
639   case R_AARCH64_ABS64:
640     // No overflow check needed.
641     write64le(Loc, SA);
642     break;
643   case R_AARCH64_ADD_ABS_LO12_NC:
644     // No overflow check needed.
645     // This relocation stores 12 bits and there's no instruction
646     // to do it. Instead, we do a 32 bits store of the value
647     // of r_addend bitwise-or'ed Loc. This assumes that the addend
648     // bits in Loc are zero.
649     or32le(Loc, (SA & 0xFFF) << 10);
650     break;
651   case R_AARCH64_ADR_PREL_LO21: {
652     uint64_t X = SA - P;
653     if (!isInt<21>(X))
654       error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
655     updateAArch64Adr(Loc, X & 0x1FFFFF);
656     break;
657   }
658   case R_AARCH64_ADR_PREL_PG_HI21: {
659     uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
660     if (!isInt<33>(X))
661       error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range");
662     updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
663     break;
664   }
665   case R_AARCH64_LDST64_ABS_LO12_NC:
666     // No overflow check needed.
667     or32le(Loc, (SA & 0xFF8) << 7);
668     break;
669   case R_AARCH64_PREL16:
670     if (!isInt<16>(SA))
671       error("Relocation R_AARCH64_PREL16 out of range");
672     write16le(Loc, SA - P);
673     break;
674   case R_AARCH64_PREL32:
675     if (!isInt<32>(SA))
676       error("Relocation R_AARCH64_PREL32 out of range");
677     write32le(Loc, SA - P);
678     break;
679   case R_AARCH64_PREL64:
680     // No overflow check needed.
681     write64le(Loc, SA - P);
682     break;
683   default:
684     error("unrecognized reloc " + Twine(Type));
685   }
686 }
687 
688 template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
689   PageSize = 65536;
690   GotRefReloc = R_MIPS_GOT16;
691   GotHeaderEntriesNum = 2;
692 }
693 
694 template <class ELFT>
695 void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
696   typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
697   auto *P = reinterpret_cast<Elf_Off *>(Buf);
698   // Module pointer
699   P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
700 }
701 
702 template <class ELFT>
703 void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
704 template <class ELFT>
705 void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
706                                        uint64_t PltEntryAddr) const {}
707 template <class ELFT>
708 void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
709                                          uint64_t PltEntryAddr, int32_t Index) const {}
710 
711 template <class ELFT>
712 bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
713                                          const SymbolBody &S) const {
714   return Type == R_MIPS_GOT16;
715 }
716 
717 template <class ELFT>
718 bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
719                                          const SymbolBody &S) const {
720   return false;
721 }
722 
723 template <class ELFT>
724 void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
725                                        uint32_t Type, uint64_t P,
726                                        uint64_t SA) const {
727   const endianness E = ELFT::TargetEndianness;
728   switch (Type) {
729   case R_MIPS_32:
730     add32<E>(Loc, SA);
731     break;
732   case R_MIPS_GOT16: {
733     int64_t V = SA - getMipsGpAddr<ELFT>();
734     if (!isInt<16>(V))
735       error("Relocation R_MIPS_GOT16 out of range");
736     write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
737     break;
738   }
739   default:
740     error("unrecognized reloc " + Twine(Type));
741   }
742 }
743 
744 template <class ELFT>
745 typename llvm::object::ELFFile<ELFT>::uintX_t getMipsGpAddr() {
746   const unsigned GPOffset = 0x7ff0;
747   return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
748 }
749 
750 template uint32_t getMipsGpAddr<ELF32LE>();
751 template uint32_t getMipsGpAddr<ELF32BE>();
752 template uint64_t getMipsGpAddr<ELF64LE>();
753 template uint64_t getMipsGpAddr<ELF64BE>();
754 }
755 }
756