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