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