xref: /freebsd/contrib/llvm-project/lld/ELF/Arch/AArch64.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===- AArch64.cpp --------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
9bdd1243dSDimitry Andric #include "OutputSections.h"
100b57cec5SDimitry Andric #include "Symbols.h"
110b57cec5SDimitry Andric #include "SyntheticSections.h"
120b57cec5SDimitry Andric #include "Target.h"
130b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h"
1481ad6265SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
150b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric using namespace llvm;
180b57cec5SDimitry Andric using namespace llvm::support::endian;
190b57cec5SDimitry Andric using namespace llvm::ELF;
205ffd83dbSDimitry Andric using namespace lld;
215ffd83dbSDimitry Andric using namespace lld::elf;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric // Page(Expr) is the page address of the expression Expr, defined
240b57cec5SDimitry Andric // as (Expr & ~0xFFF). (This applies even if the machine page size
250b57cec5SDimitry Andric // supported by the platform has a different value.)
265ffd83dbSDimitry Andric uint64_t elf::getAArch64Page(uint64_t expr) {
270b57cec5SDimitry Andric   return expr & ~static_cast<uint64_t>(0xFFF);
280b57cec5SDimitry Andric }
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric namespace {
310b57cec5SDimitry Andric class AArch64 : public TargetInfo {
320b57cec5SDimitry Andric public:
330b57cec5SDimitry Andric   AArch64();
340b57cec5SDimitry Andric   RelExpr getRelExpr(RelType type, const Symbol &s,
350b57cec5SDimitry Andric                      const uint8_t *loc) const override;
360b57cec5SDimitry Andric   RelType getDynRel(RelType type) const override;
37fe6060f1SDimitry Andric   int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
380b57cec5SDimitry Andric   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
39bdd1243dSDimitry Andric   void writeIgotPlt(uint8_t *buf, const Symbol &s) const override;
400b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
41480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
42480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
430b57cec5SDimitry Andric   bool needsThunk(RelExpr expr, RelType type, const InputFile *file,
44480093f4SDimitry Andric                   uint64_t branchAddr, const Symbol &s,
45480093f4SDimitry Andric                   int64_t a) const override;
460b57cec5SDimitry Andric   uint32_t getThunkSectionSpacing() const override;
470b57cec5SDimitry Andric   bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
480b57cec5SDimitry Andric   bool usesOnlyLowPageBits(RelType type) const override;
495ffd83dbSDimitry Andric   void relocate(uint8_t *loc, const Relocation &rel,
505ffd83dbSDimitry Andric                 uint64_t val) const override;
51e8d8bef9SDimitry Andric   RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
52bdd1243dSDimitry Andric   void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
53bdd1243dSDimitry Andric 
54bdd1243dSDimitry Andric private:
55bdd1243dSDimitry Andric   void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
56bdd1243dSDimitry Andric   void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
57bdd1243dSDimitry Andric   void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
58bdd1243dSDimitry Andric };
59bdd1243dSDimitry Andric 
60bdd1243dSDimitry Andric struct AArch64Relaxer {
61bdd1243dSDimitry Andric   bool safeToRelaxAdrpLdr = false;
62bdd1243dSDimitry Andric 
63bdd1243dSDimitry Andric   AArch64Relaxer(ArrayRef<Relocation> relocs);
64bdd1243dSDimitry Andric   bool tryRelaxAdrpAdd(const Relocation &adrpRel, const Relocation &addRel,
65bdd1243dSDimitry Andric                        uint64_t secAddr, uint8_t *buf) const;
66bdd1243dSDimitry Andric   bool tryRelaxAdrpLdr(const Relocation &adrpRel, const Relocation &ldrRel,
67bdd1243dSDimitry Andric                        uint64_t secAddr, uint8_t *buf) const;
680b57cec5SDimitry Andric };
690b57cec5SDimitry Andric } // namespace
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric AArch64::AArch64() {
720b57cec5SDimitry Andric   copyRel = R_AARCH64_COPY;
730b57cec5SDimitry Andric   relativeRel = R_AARCH64_RELATIVE;
740b57cec5SDimitry Andric   iRelativeRel = R_AARCH64_IRELATIVE;
750b57cec5SDimitry Andric   gotRel = R_AARCH64_GLOB_DAT;
760b57cec5SDimitry Andric   pltRel = R_AARCH64_JUMP_SLOT;
770b57cec5SDimitry Andric   symbolicRel = R_AARCH64_ABS64;
780b57cec5SDimitry Andric   tlsDescRel = R_AARCH64_TLSDESC;
790b57cec5SDimitry Andric   tlsGotRel = R_AARCH64_TLS_TPREL64;
800b57cec5SDimitry Andric   pltHeaderSize = 32;
81480093f4SDimitry Andric   pltEntrySize = 16;
82480093f4SDimitry Andric   ipltEntrySize = 16;
830b57cec5SDimitry Andric   defaultMaxPageSize = 65536;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   // Align to the 2 MiB page size (known as a superpage or huge page).
860b57cec5SDimitry Andric   // FreeBSD automatically promotes 2 MiB-aligned allocations.
870b57cec5SDimitry Andric   defaultImageBase = 0x200000;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   needsThunks = true;
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
930b57cec5SDimitry Andric                             const uint8_t *loc) const {
940b57cec5SDimitry Andric   switch (type) {
9585868e8aSDimitry Andric   case R_AARCH64_ABS16:
9685868e8aSDimitry Andric   case R_AARCH64_ABS32:
9785868e8aSDimitry Andric   case R_AARCH64_ABS64:
9885868e8aSDimitry Andric   case R_AARCH64_ADD_ABS_LO12_NC:
9985868e8aSDimitry Andric   case R_AARCH64_LDST128_ABS_LO12_NC:
10085868e8aSDimitry Andric   case R_AARCH64_LDST16_ABS_LO12_NC:
10185868e8aSDimitry Andric   case R_AARCH64_LDST32_ABS_LO12_NC:
10285868e8aSDimitry Andric   case R_AARCH64_LDST64_ABS_LO12_NC:
10385868e8aSDimitry Andric   case R_AARCH64_LDST8_ABS_LO12_NC:
10485868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G0:
10585868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G1:
10685868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G2:
10785868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G0:
10885868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G0_NC:
10985868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G1:
11085868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G1_NC:
11185868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G2:
11285868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G2_NC:
11385868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G3:
11485868e8aSDimitry Andric     return R_ABS;
1150b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADR_PAGE21:
1160b57cec5SDimitry Andric     return R_AARCH64_TLSDESC_PAGE;
1170b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
1180b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
1190b57cec5SDimitry Andric     return R_TLSDESC;
1200b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_CALL:
1210b57cec5SDimitry Andric     return R_TLSDESC_CALL;
1220b57cec5SDimitry Andric   case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1230b57cec5SDimitry Andric   case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1240b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
1250b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
1260b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
1270b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
1280b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC:
12985868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G0:
13085868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
13185868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G1:
13285868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
13385868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G2:
134e8d8bef9SDimitry Andric     return R_TPREL;
1350b57cec5SDimitry Andric   case R_AARCH64_CALL26:
1360b57cec5SDimitry Andric   case R_AARCH64_CONDBR19:
1370b57cec5SDimitry Andric   case R_AARCH64_JUMP26:
1380b57cec5SDimitry Andric   case R_AARCH64_TSTBR14:
139*06c3fb27SDimitry Andric     return R_PLT_PC;
1405ffd83dbSDimitry Andric   case R_AARCH64_PLT32:
141*06c3fb27SDimitry Andric     const_cast<Symbol &>(s).thunkAccessed = true;
1420b57cec5SDimitry Andric     return R_PLT_PC;
1430b57cec5SDimitry Andric   case R_AARCH64_PREL16:
1440b57cec5SDimitry Andric   case R_AARCH64_PREL32:
1450b57cec5SDimitry Andric   case R_AARCH64_PREL64:
1460b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_LO21:
1470b57cec5SDimitry Andric   case R_AARCH64_LD_PREL_LO19:
14885868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G0:
14985868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G0_NC:
15085868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G1:
15185868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G1_NC:
15285868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G2:
15385868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G2_NC:
15485868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G3:
1550b57cec5SDimitry Andric     return R_PC;
1560b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_PG_HI21:
1570b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_PG_HI21_NC:
1580b57cec5SDimitry Andric     return R_AARCH64_PAGE_PC;
1590b57cec5SDimitry Andric   case R_AARCH64_LD64_GOT_LO12_NC:
1600b57cec5SDimitry Andric   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1610b57cec5SDimitry Andric     return R_GOT;
162e8d8bef9SDimitry Andric   case R_AARCH64_LD64_GOTPAGE_LO15:
163e8d8bef9SDimitry Andric     return R_AARCH64_GOT_PAGE;
1640b57cec5SDimitry Andric   case R_AARCH64_ADR_GOT_PAGE:
1650b57cec5SDimitry Andric   case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1660b57cec5SDimitry Andric     return R_AARCH64_GOT_PAGE_PC;
1670b57cec5SDimitry Andric   case R_AARCH64_NONE:
1680b57cec5SDimitry Andric     return R_NONE;
1690b57cec5SDimitry Andric   default:
17085868e8aSDimitry Andric     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
17185868e8aSDimitry Andric           ") against symbol " + toString(s));
17285868e8aSDimitry Andric     return R_NONE;
1730b57cec5SDimitry Andric   }
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
176e8d8bef9SDimitry Andric RelExpr AArch64::adjustTlsExpr(RelType type, RelExpr expr) const {
1770b57cec5SDimitry Andric   if (expr == R_RELAX_TLS_GD_TO_IE) {
1780b57cec5SDimitry Andric     if (type == R_AARCH64_TLSDESC_ADR_PAGE21)
1790b57cec5SDimitry Andric       return R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC;
1800b57cec5SDimitry Andric     return R_RELAX_TLS_GD_TO_IE_ABS;
1810b57cec5SDimitry Andric   }
1820b57cec5SDimitry Andric   return expr;
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric bool AArch64::usesOnlyLowPageBits(RelType type) const {
1860b57cec5SDimitry Andric   switch (type) {
1870b57cec5SDimitry Andric   default:
1880b57cec5SDimitry Andric     return false;
1890b57cec5SDimitry Andric   case R_AARCH64_ADD_ABS_LO12_NC:
1900b57cec5SDimitry Andric   case R_AARCH64_LD64_GOT_LO12_NC:
1910b57cec5SDimitry Andric   case R_AARCH64_LDST128_ABS_LO12_NC:
1920b57cec5SDimitry Andric   case R_AARCH64_LDST16_ABS_LO12_NC:
1930b57cec5SDimitry Andric   case R_AARCH64_LDST32_ABS_LO12_NC:
1940b57cec5SDimitry Andric   case R_AARCH64_LDST64_ABS_LO12_NC:
1950b57cec5SDimitry Andric   case R_AARCH64_LDST8_ABS_LO12_NC:
1960b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
1970b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
1980b57cec5SDimitry Andric   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1990b57cec5SDimitry Andric     return true;
2000b57cec5SDimitry Andric   }
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric RelType AArch64::getDynRel(RelType type) const {
2040b57cec5SDimitry Andric   if (type == R_AARCH64_ABS64)
2050b57cec5SDimitry Andric     return type;
2060b57cec5SDimitry Andric   return R_AARCH64_NONE;
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric 
209fe6060f1SDimitry Andric int64_t AArch64::getImplicitAddend(const uint8_t *buf, RelType type) const {
210fe6060f1SDimitry Andric   switch (type) {
211fe6060f1SDimitry Andric   case R_AARCH64_TLSDESC:
212fe6060f1SDimitry Andric     return read64(buf + 8);
213298c3e8dSDimitry Andric   case R_AARCH64_NONE:
214bdd1243dSDimitry Andric   case R_AARCH64_GLOB_DAT:
215bdd1243dSDimitry Andric   case R_AARCH64_JUMP_SLOT:
216298c3e8dSDimitry Andric     return 0;
217298c3e8dSDimitry Andric   case R_AARCH64_PREL32:
218298c3e8dSDimitry Andric     return SignExtend64<32>(read32(buf));
219298c3e8dSDimitry Andric   case R_AARCH64_ABS64:
220298c3e8dSDimitry Andric   case R_AARCH64_PREL64:
221bdd1243dSDimitry Andric   case R_AARCH64_RELATIVE:
222bdd1243dSDimitry Andric   case R_AARCH64_IRELATIVE:
223bdd1243dSDimitry Andric   case R_AARCH64_TLS_TPREL64:
224298c3e8dSDimitry Andric     return read64(buf);
225fe6060f1SDimitry Andric   default:
226fe6060f1SDimitry Andric     internalLinkerError(getErrorLocation(buf),
227fe6060f1SDimitry Andric                         "cannot read addend for relocation " + toString(type));
228fe6060f1SDimitry Andric     return 0;
229fe6060f1SDimitry Andric   }
230fe6060f1SDimitry Andric }
231fe6060f1SDimitry Andric 
2320b57cec5SDimitry Andric void AArch64::writeGotPlt(uint8_t *buf, const Symbol &) const {
233fe6060f1SDimitry Andric   write64(buf, in.plt->getVA());
2340b57cec5SDimitry Andric }
2350b57cec5SDimitry Andric 
236bdd1243dSDimitry Andric void AArch64::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
237bdd1243dSDimitry Andric   if (config->writeAddends)
238bdd1243dSDimitry Andric     write64(buf, s.getVA());
239bdd1243dSDimitry Andric }
240bdd1243dSDimitry Andric 
2410b57cec5SDimitry Andric void AArch64::writePltHeader(uint8_t *buf) const {
2420b57cec5SDimitry Andric   const uint8_t pltData[] = {
2430b57cec5SDimitry Andric       0xf0, 0x7b, 0xbf, 0xa9, // stp    x16, x30, [sp,#-16]!
244bdd1243dSDimitry Andric       0x10, 0x00, 0x00, 0x90, // adrp   x16, Page(&(.got.plt[2]))
245bdd1243dSDimitry Andric       0x11, 0x02, 0x40, 0xf9, // ldr    x17, [x16, Offset(&(.got.plt[2]))]
246bdd1243dSDimitry Andric       0x10, 0x02, 0x00, 0x91, // add    x16, x16, Offset(&(.got.plt[2]))
2470b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6, // br     x17
2480b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5, // nop
2490b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5, // nop
2500b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5  // nop
2510b57cec5SDimitry Andric   };
2520b57cec5SDimitry Andric   memcpy(buf, pltData, sizeof(pltData));
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric   uint64_t got = in.gotPlt->getVA();
2550b57cec5SDimitry Andric   uint64_t plt = in.plt->getVA();
2565ffd83dbSDimitry Andric   relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
2570b57cec5SDimitry Andric                 getAArch64Page(got + 16) - getAArch64Page(plt + 4));
2585ffd83dbSDimitry Andric   relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16);
2595ffd83dbSDimitry Andric   relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16);
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric 
262480093f4SDimitry Andric void AArch64::writePlt(uint8_t *buf, const Symbol &sym,
263480093f4SDimitry Andric                        uint64_t pltEntryAddr) const {
2640b57cec5SDimitry Andric   const uint8_t inst[] = {
265bdd1243dSDimitry Andric       0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.got.plt[n]))
266bdd1243dSDimitry Andric       0x11, 0x02, 0x40, 0xf9, // ldr  x17, [x16, Offset(&(.got.plt[n]))]
267bdd1243dSDimitry Andric       0x10, 0x02, 0x00, 0x91, // add  x16, x16, Offset(&(.got.plt[n]))
2680b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6  // br   x17
2690b57cec5SDimitry Andric   };
2700b57cec5SDimitry Andric   memcpy(buf, inst, sizeof(inst));
2710b57cec5SDimitry Andric 
272480093f4SDimitry Andric   uint64_t gotPltEntryAddr = sym.getGotPltVA();
2735ffd83dbSDimitry Andric   relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
2740b57cec5SDimitry Andric                 getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr));
2755ffd83dbSDimitry Andric   relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr);
2765ffd83dbSDimitry Andric   relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr);
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric bool AArch64::needsThunk(RelExpr expr, RelType type, const InputFile *file,
280480093f4SDimitry Andric                          uint64_t branchAddr, const Symbol &s,
281480093f4SDimitry Andric                          int64_t a) const {
2822a66634dSDimitry Andric   // If s is an undefined weak symbol and does not have a PLT entry then it will
2832a66634dSDimitry Andric   // be resolved as a branch to the next instruction. If it is hidden, its
2842a66634dSDimitry Andric   // binding has been converted to local, so we just check isUndefined() here. A
2852a66634dSDimitry Andric   // undefined non-weak symbol will have been errored.
2862a66634dSDimitry Andric   if (s.isUndefined() && !s.isInPlt())
287480093f4SDimitry Andric     return false;
2880b57cec5SDimitry Andric   // ELF for the ARM 64-bit architecture, section Call and Jump relocations
2890b57cec5SDimitry Andric   // only permits range extension thunks for R_AARCH64_CALL26 and
2900b57cec5SDimitry Andric   // R_AARCH64_JUMP26 relocation types.
2915ffd83dbSDimitry Andric   if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
2925ffd83dbSDimitry Andric       type != R_AARCH64_PLT32)
2930b57cec5SDimitry Andric     return false;
294480093f4SDimitry Andric   uint64_t dst = expr == R_PLT_PC ? s.getPltVA() : s.getVA(a);
2950b57cec5SDimitry Andric   return !inBranchRange(type, branchAddr, dst);
2960b57cec5SDimitry Andric }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric uint32_t AArch64::getThunkSectionSpacing() const {
2990b57cec5SDimitry Andric   // See comment in Arch/ARM.cpp for a more detailed explanation of
3000b57cec5SDimitry Andric   // getThunkSectionSpacing(). For AArch64 the only branches we are permitted to
3010b57cec5SDimitry Andric   // Thunk have a range of +/- 128 MiB
3020b57cec5SDimitry Andric   return (128 * 1024 * 1024) - 0x30000;
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric bool AArch64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
3065ffd83dbSDimitry Andric   if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
3075ffd83dbSDimitry Andric       type != R_AARCH64_PLT32)
3080b57cec5SDimitry Andric     return true;
3090b57cec5SDimitry Andric   // The AArch64 call and unconditional branch instructions have a range of
3105ffd83dbSDimitry Andric   // +/- 128 MiB. The PLT32 relocation supports a range up to +/- 2 GiB.
3115ffd83dbSDimitry Andric   uint64_t range =
3125ffd83dbSDimitry Andric       type == R_AARCH64_PLT32 ? (UINT64_C(1) << 31) : (128 * 1024 * 1024);
3130b57cec5SDimitry Andric   if (dst > src) {
3140b57cec5SDimitry Andric     // Immediate of branch is signed.
3150b57cec5SDimitry Andric     range -= 4;
3160b57cec5SDimitry Andric     return dst - src <= range;
3170b57cec5SDimitry Andric   }
3180b57cec5SDimitry Andric   return src - dst <= range;
3190b57cec5SDimitry Andric }
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric static void write32AArch64Addr(uint8_t *l, uint64_t imm) {
3220b57cec5SDimitry Andric   uint32_t immLo = (imm & 0x3) << 29;
3230b57cec5SDimitry Andric   uint32_t immHi = (imm & 0x1FFFFC) << 3;
3240b57cec5SDimitry Andric   uint64_t mask = (0x3 << 29) | (0x1FFFFC << 3);
3250b57cec5SDimitry Andric   write32le(l, (read32le(l) & ~mask) | immLo | immHi);
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric // Return the bits [Start, End] from Val shifted Start bits.
3290b57cec5SDimitry Andric // For instance, getBits(0xF0, 4, 8) returns 0xF.
3300b57cec5SDimitry Andric static uint64_t getBits(uint64_t val, int start, int end) {
3310b57cec5SDimitry Andric   uint64_t mask = ((uint64_t)1 << (end + 1 - start)) - 1;
3320b57cec5SDimitry Andric   return (val >> start) & mask;
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric static void or32le(uint8_t *p, int32_t v) { write32le(p, read32le(p) | v); }
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric // Update the immediate field in a AARCH64 ldr, str, and add instruction.
3380b57cec5SDimitry Andric static void or32AArch64Imm(uint8_t *l, uint64_t imm) {
3390b57cec5SDimitry Andric   or32le(l, (imm & 0xFFF) << 10);
3400b57cec5SDimitry Andric }
3410b57cec5SDimitry Andric 
34285868e8aSDimitry Andric // Update the immediate field in an AArch64 movk, movn or movz instruction
34385868e8aSDimitry Andric // for a signed relocation, and update the opcode of a movn or movz instruction
34485868e8aSDimitry Andric // to match the sign of the operand.
34585868e8aSDimitry Andric static void writeSMovWImm(uint8_t *loc, uint32_t imm) {
34685868e8aSDimitry Andric   uint32_t inst = read32le(loc);
34785868e8aSDimitry Andric   // Opcode field is bits 30, 29, with 10 = movz, 00 = movn and 11 = movk.
34885868e8aSDimitry Andric   if (!(inst & (1 << 29))) {
34985868e8aSDimitry Andric     // movn or movz.
35085868e8aSDimitry Andric     if (imm & 0x10000) {
35185868e8aSDimitry Andric       // Change opcode to movn, which takes an inverted operand.
35285868e8aSDimitry Andric       imm ^= 0xFFFF;
35385868e8aSDimitry Andric       inst &= ~(1 << 30);
35485868e8aSDimitry Andric     } else {
35585868e8aSDimitry Andric       // Change opcode to movz.
35685868e8aSDimitry Andric       inst |= 1 << 30;
35785868e8aSDimitry Andric     }
35885868e8aSDimitry Andric   }
35985868e8aSDimitry Andric   write32le(loc, inst | ((imm & 0xFFFF) << 5));
36085868e8aSDimitry Andric }
36185868e8aSDimitry Andric 
3625ffd83dbSDimitry Andric void AArch64::relocate(uint8_t *loc, const Relocation &rel,
3635ffd83dbSDimitry Andric                        uint64_t val) const {
3645ffd83dbSDimitry Andric   switch (rel.type) {
3650b57cec5SDimitry Andric   case R_AARCH64_ABS16:
3660b57cec5SDimitry Andric   case R_AARCH64_PREL16:
3675ffd83dbSDimitry Andric     checkIntUInt(loc, val, 16, rel);
368fe6060f1SDimitry Andric     write16(loc, val);
3690b57cec5SDimitry Andric     break;
3700b57cec5SDimitry Andric   case R_AARCH64_ABS32:
3710b57cec5SDimitry Andric   case R_AARCH64_PREL32:
3725ffd83dbSDimitry Andric     checkIntUInt(loc, val, 32, rel);
373fe6060f1SDimitry Andric     write32(loc, val);
3745ffd83dbSDimitry Andric     break;
3755ffd83dbSDimitry Andric   case R_AARCH64_PLT32:
3765ffd83dbSDimitry Andric     checkInt(loc, val, 32, rel);
377fe6060f1SDimitry Andric     write32(loc, val);
3780b57cec5SDimitry Andric     break;
3790b57cec5SDimitry Andric   case R_AARCH64_ABS64:
3800b57cec5SDimitry Andric   case R_AARCH64_PREL64:
381fe6060f1SDimitry Andric     write64(loc, val);
3820b57cec5SDimitry Andric     break;
3830b57cec5SDimitry Andric   case R_AARCH64_ADD_ABS_LO12_NC:
3840b57cec5SDimitry Andric     or32AArch64Imm(loc, val);
3850b57cec5SDimitry Andric     break;
3860b57cec5SDimitry Andric   case R_AARCH64_ADR_GOT_PAGE:
3870b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_PG_HI21:
3880b57cec5SDimitry Andric   case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3890b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADR_PAGE21:
3905ffd83dbSDimitry Andric     checkInt(loc, val, 33, rel);
391bdd1243dSDimitry Andric     [[fallthrough]];
3920b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_PG_HI21_NC:
3930b57cec5SDimitry Andric     write32AArch64Addr(loc, val >> 12);
3940b57cec5SDimitry Andric     break;
3950b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_LO21:
3965ffd83dbSDimitry Andric     checkInt(loc, val, 21, rel);
3970b57cec5SDimitry Andric     write32AArch64Addr(loc, val);
3980b57cec5SDimitry Andric     break;
3990b57cec5SDimitry Andric   case R_AARCH64_JUMP26:
4000b57cec5SDimitry Andric     // Normally we would just write the bits of the immediate field, however
4010b57cec5SDimitry Andric     // when patching instructions for the cpu errata fix -fix-cortex-a53-843419
4020b57cec5SDimitry Andric     // we want to replace a non-branch instruction with a branch immediate
4030b57cec5SDimitry Andric     // instruction. By writing all the bits of the instruction including the
4040b57cec5SDimitry Andric     // opcode and the immediate (0 001 | 01 imm26) we can do this
4050b57cec5SDimitry Andric     // transformation by placing a R_AARCH64_JUMP26 relocation at the offset of
4060b57cec5SDimitry Andric     // the instruction we want to patch.
4070b57cec5SDimitry Andric     write32le(loc, 0x14000000);
408bdd1243dSDimitry Andric     [[fallthrough]];
4090b57cec5SDimitry Andric   case R_AARCH64_CALL26:
4105ffd83dbSDimitry Andric     checkInt(loc, val, 28, rel);
4110b57cec5SDimitry Andric     or32le(loc, (val & 0x0FFFFFFC) >> 2);
4120b57cec5SDimitry Andric     break;
4130b57cec5SDimitry Andric   case R_AARCH64_CONDBR19:
4140b57cec5SDimitry Andric   case R_AARCH64_LD_PREL_LO19:
4155ffd83dbSDimitry Andric     checkAlignment(loc, val, 4, rel);
4165ffd83dbSDimitry Andric     checkInt(loc, val, 21, rel);
4170b57cec5SDimitry Andric     or32le(loc, (val & 0x1FFFFC) << 3);
4180b57cec5SDimitry Andric     break;
4190b57cec5SDimitry Andric   case R_AARCH64_LDST8_ABS_LO12_NC:
4200b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
4210b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 0, 11));
4220b57cec5SDimitry Andric     break;
4230b57cec5SDimitry Andric   case R_AARCH64_LDST16_ABS_LO12_NC:
4240b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
4255ffd83dbSDimitry Andric     checkAlignment(loc, val, 2, rel);
4260b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 1, 11));
4270b57cec5SDimitry Andric     break;
4280b57cec5SDimitry Andric   case R_AARCH64_LDST32_ABS_LO12_NC:
4290b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
4305ffd83dbSDimitry Andric     checkAlignment(loc, val, 4, rel);
4310b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 2, 11));
4320b57cec5SDimitry Andric     break;
4330b57cec5SDimitry Andric   case R_AARCH64_LDST64_ABS_LO12_NC:
4340b57cec5SDimitry Andric   case R_AARCH64_LD64_GOT_LO12_NC:
4350b57cec5SDimitry Andric   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4360b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
4370b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
4385ffd83dbSDimitry Andric     checkAlignment(loc, val, 8, rel);
4390b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 3, 11));
4400b57cec5SDimitry Andric     break;
4410b57cec5SDimitry Andric   case R_AARCH64_LDST128_ABS_LO12_NC:
4420b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC:
4435ffd83dbSDimitry Andric     checkAlignment(loc, val, 16, rel);
4440b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 4, 11));
4450b57cec5SDimitry Andric     break;
446e8d8bef9SDimitry Andric   case R_AARCH64_LD64_GOTPAGE_LO15:
447e8d8bef9SDimitry Andric     checkAlignment(loc, val, 8, rel);
448e8d8bef9SDimitry Andric     or32AArch64Imm(loc, getBits(val, 3, 14));
449e8d8bef9SDimitry Andric     break;
45085868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G0:
4515ffd83dbSDimitry Andric     checkUInt(loc, val, 16, rel);
452bdd1243dSDimitry Andric     [[fallthrough]];
4530b57cec5SDimitry Andric   case R_AARCH64_MOVW_UABS_G0_NC:
4540b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFF) << 5);
4550b57cec5SDimitry Andric     break;
45685868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G1:
4575ffd83dbSDimitry Andric     checkUInt(loc, val, 32, rel);
458bdd1243dSDimitry Andric     [[fallthrough]];
4590b57cec5SDimitry Andric   case R_AARCH64_MOVW_UABS_G1_NC:
4600b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFF0000) >> 11);
4610b57cec5SDimitry Andric     break;
46285868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G2:
4635ffd83dbSDimitry Andric     checkUInt(loc, val, 48, rel);
464bdd1243dSDimitry Andric     [[fallthrough]];
4650b57cec5SDimitry Andric   case R_AARCH64_MOVW_UABS_G2_NC:
4660b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFF00000000) >> 27);
4670b57cec5SDimitry Andric     break;
4680b57cec5SDimitry Andric   case R_AARCH64_MOVW_UABS_G3:
4690b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFF000000000000) >> 43);
4700b57cec5SDimitry Andric     break;
47185868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G0:
47285868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G0:
47385868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G0:
4745ffd83dbSDimitry Andric     checkInt(loc, val, 17, rel);
475bdd1243dSDimitry Andric     [[fallthrough]];
47685868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G0_NC:
47785868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
47885868e8aSDimitry Andric     writeSMovWImm(loc, val);
47985868e8aSDimitry Andric     break;
48085868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G1:
48185868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G1:
48285868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G1:
4835ffd83dbSDimitry Andric     checkInt(loc, val, 33, rel);
484bdd1243dSDimitry Andric     [[fallthrough]];
48585868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G1_NC:
48685868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
48785868e8aSDimitry Andric     writeSMovWImm(loc, val >> 16);
48885868e8aSDimitry Andric     break;
48985868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G2:
49085868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G2:
49185868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G2:
4925ffd83dbSDimitry Andric     checkInt(loc, val, 49, rel);
493bdd1243dSDimitry Andric     [[fallthrough]];
49485868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G2_NC:
49585868e8aSDimitry Andric     writeSMovWImm(loc, val >> 32);
49685868e8aSDimitry Andric     break;
49785868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G3:
49885868e8aSDimitry Andric     writeSMovWImm(loc, val >> 48);
49985868e8aSDimitry Andric     break;
5000b57cec5SDimitry Andric   case R_AARCH64_TSTBR14:
5015ffd83dbSDimitry Andric     checkInt(loc, val, 16, rel);
5020b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFC) << 3);
5030b57cec5SDimitry Andric     break;
5040b57cec5SDimitry Andric   case R_AARCH64_TLSLE_ADD_TPREL_HI12:
5055ffd83dbSDimitry Andric     checkUInt(loc, val, 24, rel);
5060b57cec5SDimitry Andric     or32AArch64Imm(loc, val >> 12);
5070b57cec5SDimitry Andric     break;
5080b57cec5SDimitry Andric   case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5090b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
5100b57cec5SDimitry Andric     or32AArch64Imm(loc, val);
5110b57cec5SDimitry Andric     break;
512fe6060f1SDimitry Andric   case R_AARCH64_TLSDESC:
513fe6060f1SDimitry Andric     // For R_AARCH64_TLSDESC the addend is stored in the second 64-bit word.
514fe6060f1SDimitry Andric     write64(loc + 8, val);
515fe6060f1SDimitry Andric     break;
5160b57cec5SDimitry Andric   default:
51785868e8aSDimitry Andric     llvm_unreachable("unknown relocation");
5180b57cec5SDimitry Andric   }
5190b57cec5SDimitry Andric }
5200b57cec5SDimitry Andric 
5215ffd83dbSDimitry Andric void AArch64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
5225ffd83dbSDimitry Andric                              uint64_t val) const {
5230b57cec5SDimitry Andric   // TLSDESC Global-Dynamic relocation are in the form:
5240b57cec5SDimitry Andric   //   adrp    x0, :tlsdesc:v             [R_AARCH64_TLSDESC_ADR_PAGE21]
5250b57cec5SDimitry Andric   //   ldr     x1, [x0, #:tlsdesc_lo12:v  [R_AARCH64_TLSDESC_LD64_LO12]
5260b57cec5SDimitry Andric   //   add     x0, x0, :tlsdesc_los:v     [R_AARCH64_TLSDESC_ADD_LO12]
5270b57cec5SDimitry Andric   //   .tlsdesccall                       [R_AARCH64_TLSDESC_CALL]
5280b57cec5SDimitry Andric   //   blr     x1
5290b57cec5SDimitry Andric   // And it can optimized to:
5300b57cec5SDimitry Andric   //   movz    x0, #0x0, lsl #16
5310b57cec5SDimitry Andric   //   movk    x0, #0x10
5320b57cec5SDimitry Andric   //   nop
5330b57cec5SDimitry Andric   //   nop
5345ffd83dbSDimitry Andric   checkUInt(loc, val, 32, rel);
5350b57cec5SDimitry Andric 
5365ffd83dbSDimitry Andric   switch (rel.type) {
5370b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
5380b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_CALL:
5390b57cec5SDimitry Andric     write32le(loc, 0xd503201f); // nop
5400b57cec5SDimitry Andric     return;
5410b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADR_PAGE21:
5420b57cec5SDimitry Andric     write32le(loc, 0xd2a00000 | (((val >> 16) & 0xffff) << 5)); // movz
5430b57cec5SDimitry Andric     return;
5440b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
5450b57cec5SDimitry Andric     write32le(loc, 0xf2800000 | ((val & 0xffff) << 5)); // movk
5460b57cec5SDimitry Andric     return;
5470b57cec5SDimitry Andric   default:
5480b57cec5SDimitry Andric     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
5490b57cec5SDimitry Andric   }
5500b57cec5SDimitry Andric }
5510b57cec5SDimitry Andric 
5525ffd83dbSDimitry Andric void AArch64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
5535ffd83dbSDimitry Andric                              uint64_t val) const {
5540b57cec5SDimitry Andric   // TLSDESC Global-Dynamic relocation are in the form:
5550b57cec5SDimitry Andric   //   adrp    x0, :tlsdesc:v             [R_AARCH64_TLSDESC_ADR_PAGE21]
5560b57cec5SDimitry Andric   //   ldr     x1, [x0, #:tlsdesc_lo12:v  [R_AARCH64_TLSDESC_LD64_LO12]
5570b57cec5SDimitry Andric   //   add     x0, x0, :tlsdesc_los:v     [R_AARCH64_TLSDESC_ADD_LO12]
5580b57cec5SDimitry Andric   //   .tlsdesccall                       [R_AARCH64_TLSDESC_CALL]
5590b57cec5SDimitry Andric   //   blr     x1
5600b57cec5SDimitry Andric   // And it can optimized to:
5610b57cec5SDimitry Andric   //   adrp    x0, :gottprel:v
5620b57cec5SDimitry Andric   //   ldr     x0, [x0, :gottprel_lo12:v]
5630b57cec5SDimitry Andric   //   nop
5640b57cec5SDimitry Andric   //   nop
5650b57cec5SDimitry Andric 
5665ffd83dbSDimitry Andric   switch (rel.type) {
5670b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
5680b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_CALL:
5690b57cec5SDimitry Andric     write32le(loc, 0xd503201f); // nop
5700b57cec5SDimitry Andric     break;
5710b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADR_PAGE21:
5720b57cec5SDimitry Andric     write32le(loc, 0x90000000); // adrp
5735ffd83dbSDimitry Andric     relocateNoSym(loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, val);
5740b57cec5SDimitry Andric     break;
5750b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
5760b57cec5SDimitry Andric     write32le(loc, 0xf9400000); // ldr
5775ffd83dbSDimitry Andric     relocateNoSym(loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, val);
5780b57cec5SDimitry Andric     break;
5790b57cec5SDimitry Andric   default:
5800b57cec5SDimitry Andric     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
5810b57cec5SDimitry Andric   }
5820b57cec5SDimitry Andric }
5830b57cec5SDimitry Andric 
5845ffd83dbSDimitry Andric void AArch64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
5855ffd83dbSDimitry Andric                              uint64_t val) const {
5865ffd83dbSDimitry Andric   checkUInt(loc, val, 32, rel);
5870b57cec5SDimitry Andric 
5885ffd83dbSDimitry Andric   if (rel.type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
5890b57cec5SDimitry Andric     // Generate MOVZ.
5900b57cec5SDimitry Andric     uint32_t regNo = read32le(loc) & 0x1f;
5910b57cec5SDimitry Andric     write32le(loc, (0xd2a00000 | regNo) | (((val >> 16) & 0xffff) << 5));
5920b57cec5SDimitry Andric     return;
5930b57cec5SDimitry Andric   }
5945ffd83dbSDimitry Andric   if (rel.type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
5950b57cec5SDimitry Andric     // Generate MOVK.
5960b57cec5SDimitry Andric     uint32_t regNo = read32le(loc) & 0x1f;
5970b57cec5SDimitry Andric     write32le(loc, (0xf2800000 | regNo) | ((val & 0xffff) << 5));
5980b57cec5SDimitry Andric     return;
5990b57cec5SDimitry Andric   }
6000b57cec5SDimitry Andric   llvm_unreachable("invalid relocation for TLS IE to LE relaxation");
6010b57cec5SDimitry Andric }
6020b57cec5SDimitry Andric 
60304eeddc0SDimitry Andric AArch64Relaxer::AArch64Relaxer(ArrayRef<Relocation> relocs) {
604bdd1243dSDimitry Andric   if (!config->relax)
60504eeddc0SDimitry Andric     return;
60604eeddc0SDimitry Andric   // Check if R_AARCH64_ADR_GOT_PAGE and R_AARCH64_LD64_GOT_LO12_NC
60704eeddc0SDimitry Andric   // always appear in pairs.
60804eeddc0SDimitry Andric   size_t i = 0;
60904eeddc0SDimitry Andric   const size_t size = relocs.size();
61004eeddc0SDimitry Andric   for (; i != size; ++i) {
61104eeddc0SDimitry Andric     if (relocs[i].type == R_AARCH64_ADR_GOT_PAGE) {
61204eeddc0SDimitry Andric       if (i + 1 < size && relocs[i + 1].type == R_AARCH64_LD64_GOT_LO12_NC) {
61304eeddc0SDimitry Andric         ++i;
61404eeddc0SDimitry Andric         continue;
61504eeddc0SDimitry Andric       }
61604eeddc0SDimitry Andric       break;
61704eeddc0SDimitry Andric     } else if (relocs[i].type == R_AARCH64_LD64_GOT_LO12_NC) {
61804eeddc0SDimitry Andric       break;
61904eeddc0SDimitry Andric     }
62004eeddc0SDimitry Andric   }
62104eeddc0SDimitry Andric   safeToRelaxAdrpLdr = i == size;
62204eeddc0SDimitry Andric }
62304eeddc0SDimitry Andric 
6241fd87a68SDimitry Andric bool AArch64Relaxer::tryRelaxAdrpAdd(const Relocation &adrpRel,
6251fd87a68SDimitry Andric                                      const Relocation &addRel, uint64_t secAddr,
6261fd87a68SDimitry Andric                                      uint8_t *buf) const {
6271fd87a68SDimitry Andric   // When the address of sym is within the range of ADR then
6281fd87a68SDimitry Andric   // we may relax
6291fd87a68SDimitry Andric   // ADRP xn, sym
6301fd87a68SDimitry Andric   // ADD  xn, xn, :lo12: sym
6311fd87a68SDimitry Andric   // to
6321fd87a68SDimitry Andric   // NOP
6331fd87a68SDimitry Andric   // ADR xn, sym
6341fd87a68SDimitry Andric   if (!config->relax || adrpRel.type != R_AARCH64_ADR_PREL_PG_HI21 ||
6351fd87a68SDimitry Andric       addRel.type != R_AARCH64_ADD_ABS_LO12_NC)
6361fd87a68SDimitry Andric     return false;
6371fd87a68SDimitry Andric   // Check if the relocations apply to consecutive instructions.
6381fd87a68SDimitry Andric   if (adrpRel.offset + 4 != addRel.offset)
6391fd87a68SDimitry Andric     return false;
6401fd87a68SDimitry Andric   if (adrpRel.sym != addRel.sym)
6411fd87a68SDimitry Andric     return false;
6421fd87a68SDimitry Andric   if (adrpRel.addend != 0 || addRel.addend != 0)
6431fd87a68SDimitry Andric     return false;
6441fd87a68SDimitry Andric 
6451fd87a68SDimitry Andric   uint32_t adrpInstr = read32le(buf + adrpRel.offset);
6461fd87a68SDimitry Andric   uint32_t addInstr = read32le(buf + addRel.offset);
6471fd87a68SDimitry Andric   // Check if the first instruction is ADRP and the second instruction is ADD.
6481fd87a68SDimitry Andric   if ((adrpInstr & 0x9f000000) != 0x90000000 ||
6491fd87a68SDimitry Andric       (addInstr & 0xffc00000) != 0x91000000)
6501fd87a68SDimitry Andric     return false;
6511fd87a68SDimitry Andric   uint32_t adrpDestReg = adrpInstr & 0x1f;
6521fd87a68SDimitry Andric   uint32_t addDestReg = addInstr & 0x1f;
6531fd87a68SDimitry Andric   uint32_t addSrcReg = (addInstr >> 5) & 0x1f;
6541fd87a68SDimitry Andric   if (adrpDestReg != addDestReg || adrpDestReg != addSrcReg)
6551fd87a68SDimitry Andric     return false;
6561fd87a68SDimitry Andric 
6571fd87a68SDimitry Andric   Symbol &sym = *adrpRel.sym;
6581fd87a68SDimitry Andric   // Check if the address difference is within 1MiB range.
6591fd87a68SDimitry Andric   int64_t val = sym.getVA() - (secAddr + addRel.offset);
6601fd87a68SDimitry Andric   if (val < -1024 * 1024 || val >= 1024 * 1024)
6611fd87a68SDimitry Andric     return false;
6621fd87a68SDimitry Andric 
6631fd87a68SDimitry Andric   Relocation adrRel = {R_ABS, R_AARCH64_ADR_PREL_LO21, addRel.offset,
6641fd87a68SDimitry Andric                        /*addend=*/0, &sym};
6651fd87a68SDimitry Andric   // nop
6661fd87a68SDimitry Andric   write32le(buf + adrpRel.offset, 0xd503201f);
6671fd87a68SDimitry Andric   // adr x_<dest_reg>
6681fd87a68SDimitry Andric   write32le(buf + adrRel.offset, 0x10000000 | adrpDestReg);
6691fd87a68SDimitry Andric   target->relocate(buf + adrRel.offset, adrRel, val);
6701fd87a68SDimitry Andric   return true;
6711fd87a68SDimitry Andric }
6721fd87a68SDimitry Andric 
67304eeddc0SDimitry Andric bool AArch64Relaxer::tryRelaxAdrpLdr(const Relocation &adrpRel,
67404eeddc0SDimitry Andric                                      const Relocation &ldrRel, uint64_t secAddr,
67504eeddc0SDimitry Andric                                      uint8_t *buf) const {
67604eeddc0SDimitry Andric   if (!safeToRelaxAdrpLdr)
67704eeddc0SDimitry Andric     return false;
67804eeddc0SDimitry Andric 
67904eeddc0SDimitry Andric   // When the definition of sym is not preemptible then we may
68004eeddc0SDimitry Andric   // be able to relax
68104eeddc0SDimitry Andric   // ADRP xn, :got: sym
68204eeddc0SDimitry Andric   // LDR xn, [ xn :got_lo12: sym]
68304eeddc0SDimitry Andric   // to
68404eeddc0SDimitry Andric   // ADRP xn, sym
68504eeddc0SDimitry Andric   // ADD xn, xn, :lo_12: sym
68604eeddc0SDimitry Andric 
68704eeddc0SDimitry Andric   if (adrpRel.type != R_AARCH64_ADR_GOT_PAGE ||
68804eeddc0SDimitry Andric       ldrRel.type != R_AARCH64_LD64_GOT_LO12_NC)
68904eeddc0SDimitry Andric     return false;
69004eeddc0SDimitry Andric   // Check if the relocations apply to consecutive instructions.
69104eeddc0SDimitry Andric   if (adrpRel.offset + 4 != ldrRel.offset)
69204eeddc0SDimitry Andric     return false;
69304eeddc0SDimitry Andric   // Check if the relocations reference the same symbol and
69404eeddc0SDimitry Andric   // skip undefined, preemptible and STT_GNU_IFUNC symbols.
69504eeddc0SDimitry Andric   if (!adrpRel.sym || adrpRel.sym != ldrRel.sym || !adrpRel.sym->isDefined() ||
69604eeddc0SDimitry Andric       adrpRel.sym->isPreemptible || adrpRel.sym->isGnuIFunc())
69704eeddc0SDimitry Andric     return false;
69804eeddc0SDimitry Andric   // Check if the addends of the both relocations are zero.
69904eeddc0SDimitry Andric   if (adrpRel.addend != 0 || ldrRel.addend != 0)
70004eeddc0SDimitry Andric     return false;
70104eeddc0SDimitry Andric   uint32_t adrpInstr = read32le(buf + adrpRel.offset);
70204eeddc0SDimitry Andric   uint32_t ldrInstr = read32le(buf + ldrRel.offset);
70304eeddc0SDimitry Andric   // Check if the first instruction is ADRP and the second instruction is LDR.
70404eeddc0SDimitry Andric   if ((adrpInstr & 0x9f000000) != 0x90000000 ||
70504eeddc0SDimitry Andric       (ldrInstr & 0x3b000000) != 0x39000000)
70604eeddc0SDimitry Andric     return false;
70704eeddc0SDimitry Andric   // Check the value of the sf bit.
70804eeddc0SDimitry Andric   if (!(ldrInstr >> 31))
70904eeddc0SDimitry Andric     return false;
71004eeddc0SDimitry Andric   uint32_t adrpDestReg = adrpInstr & 0x1f;
71104eeddc0SDimitry Andric   uint32_t ldrDestReg = ldrInstr & 0x1f;
71204eeddc0SDimitry Andric   uint32_t ldrSrcReg = (ldrInstr >> 5) & 0x1f;
71304eeddc0SDimitry Andric   // Check if ADPR and LDR use the same register.
71404eeddc0SDimitry Andric   if (adrpDestReg != ldrDestReg || adrpDestReg != ldrSrcReg)
71504eeddc0SDimitry Andric     return false;
71604eeddc0SDimitry Andric 
71704eeddc0SDimitry Andric   Symbol &sym = *adrpRel.sym;
71881ad6265SDimitry Andric   // GOT references to absolute symbols can't be relaxed to use ADRP/ADD in
71981ad6265SDimitry Andric   // position-independent code because these instructions produce a relative
72081ad6265SDimitry Andric   // address.
72181ad6265SDimitry Andric   if (config->isPic && !cast<Defined>(sym).section)
72281ad6265SDimitry Andric     return false;
72304eeddc0SDimitry Andric   // Check if the address difference is within 4GB range.
72404eeddc0SDimitry Andric   int64_t val =
72504eeddc0SDimitry Andric       getAArch64Page(sym.getVA()) - getAArch64Page(secAddr + adrpRel.offset);
72604eeddc0SDimitry Andric   if (val != llvm::SignExtend64(val, 33))
72704eeddc0SDimitry Andric     return false;
72804eeddc0SDimitry Andric 
72904eeddc0SDimitry Andric   Relocation adrpSymRel = {R_AARCH64_PAGE_PC, R_AARCH64_ADR_PREL_PG_HI21,
73004eeddc0SDimitry Andric                            adrpRel.offset, /*addend=*/0, &sym};
73104eeddc0SDimitry Andric   Relocation addRel = {R_ABS, R_AARCH64_ADD_ABS_LO12_NC, ldrRel.offset,
73204eeddc0SDimitry Andric                        /*addend=*/0, &sym};
73304eeddc0SDimitry Andric 
73404eeddc0SDimitry Andric   // adrp x_<dest_reg>
73504eeddc0SDimitry Andric   write32le(buf + adrpSymRel.offset, 0x90000000 | adrpDestReg);
73604eeddc0SDimitry Andric   // add x_<dest reg>, x_<dest reg>
73704eeddc0SDimitry Andric   write32le(buf + addRel.offset, 0x91000000 | adrpDestReg | (adrpDestReg << 5));
73804eeddc0SDimitry Andric 
73904eeddc0SDimitry Andric   target->relocate(buf + adrpSymRel.offset, adrpSymRel,
74004eeddc0SDimitry Andric                    SignExtend64(getAArch64Page(sym.getVA()) -
74104eeddc0SDimitry Andric                                     getAArch64Page(secAddr + adrpSymRel.offset),
74204eeddc0SDimitry Andric                                 64));
74304eeddc0SDimitry Andric   target->relocate(buf + addRel.offset, addRel, SignExtend64(sym.getVA(), 64));
7441fd87a68SDimitry Andric   tryRelaxAdrpAdd(adrpSymRel, addRel, secAddr, buf);
74504eeddc0SDimitry Andric   return true;
74604eeddc0SDimitry Andric }
74704eeddc0SDimitry Andric 
748bdd1243dSDimitry Andric void AArch64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
749bdd1243dSDimitry Andric   uint64_t secAddr = sec.getOutputSection()->addr;
750bdd1243dSDimitry Andric   if (auto *s = dyn_cast<InputSection>(&sec))
751bdd1243dSDimitry Andric     secAddr += s->outSecOff;
752bdd1243dSDimitry Andric   AArch64Relaxer relaxer(sec.relocs());
753bdd1243dSDimitry Andric   for (size_t i = 0, size = sec.relocs().size(); i != size; ++i) {
754bdd1243dSDimitry Andric     const Relocation &rel = sec.relocs()[i];
755bdd1243dSDimitry Andric     uint8_t *loc = buf + rel.offset;
756bdd1243dSDimitry Andric     const uint64_t val =
757bdd1243dSDimitry Andric         sec.getRelocTargetVA(sec.file, rel.type, rel.addend,
758bdd1243dSDimitry Andric                              secAddr + rel.offset, *rel.sym, rel.expr);
759bdd1243dSDimitry Andric     switch (rel.expr) {
760bdd1243dSDimitry Andric     case R_AARCH64_GOT_PAGE_PC:
761bdd1243dSDimitry Andric       if (i + 1 < size &&
762bdd1243dSDimitry Andric           relaxer.tryRelaxAdrpLdr(rel, sec.relocs()[i + 1], secAddr, buf)) {
763bdd1243dSDimitry Andric         ++i;
764bdd1243dSDimitry Andric         continue;
765bdd1243dSDimitry Andric       }
766bdd1243dSDimitry Andric       break;
767bdd1243dSDimitry Andric     case R_AARCH64_PAGE_PC:
768bdd1243dSDimitry Andric       if (i + 1 < size &&
769bdd1243dSDimitry Andric           relaxer.tryRelaxAdrpAdd(rel, sec.relocs()[i + 1], secAddr, buf)) {
770bdd1243dSDimitry Andric         ++i;
771bdd1243dSDimitry Andric         continue;
772bdd1243dSDimitry Andric       }
773bdd1243dSDimitry Andric       break;
774bdd1243dSDimitry Andric     case R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC:
775bdd1243dSDimitry Andric     case R_RELAX_TLS_GD_TO_IE_ABS:
776bdd1243dSDimitry Andric       relaxTlsGdToIe(loc, rel, val);
777bdd1243dSDimitry Andric       continue;
778bdd1243dSDimitry Andric     case R_RELAX_TLS_GD_TO_LE:
779bdd1243dSDimitry Andric       relaxTlsGdToLe(loc, rel, val);
780bdd1243dSDimitry Andric       continue;
781bdd1243dSDimitry Andric     case R_RELAX_TLS_IE_TO_LE:
782bdd1243dSDimitry Andric       relaxTlsIeToLe(loc, rel, val);
783bdd1243dSDimitry Andric       continue;
784bdd1243dSDimitry Andric     default:
785bdd1243dSDimitry Andric       break;
786bdd1243dSDimitry Andric     }
787bdd1243dSDimitry Andric     relocate(loc, rel, val);
788bdd1243dSDimitry Andric   }
789bdd1243dSDimitry Andric }
790bdd1243dSDimitry Andric 
7910b57cec5SDimitry Andric // AArch64 may use security features in variant PLT sequences. These are:
7920b57cec5SDimitry Andric // Pointer Authentication (PAC), introduced in armv8.3-a and Branch Target
7930b57cec5SDimitry Andric // Indicator (BTI) introduced in armv8.5-a. The additional instructions used
7940b57cec5SDimitry Andric // in the variant Plt sequences are encoded in the Hint space so they can be
7950b57cec5SDimitry Andric // deployed on older architectures, which treat the instructions as a nop.
7960b57cec5SDimitry Andric // PAC and BTI can be combined leading to the following combinations:
7970b57cec5SDimitry Andric // writePltHeader
7980b57cec5SDimitry Andric // writePltHeaderBti (no PAC Header needed)
7990b57cec5SDimitry Andric // writePlt
8000b57cec5SDimitry Andric // writePltBti (BTI only)
8010b57cec5SDimitry Andric // writePltPac (PAC only)
8020b57cec5SDimitry Andric // writePltBtiPac (BTI and PAC)
8030b57cec5SDimitry Andric //
8040b57cec5SDimitry Andric // When PAC is enabled the dynamic loader encrypts the address that it places
8050b57cec5SDimitry Andric // in the .got.plt using the pacia1716 instruction which encrypts the value in
8060b57cec5SDimitry Andric // x17 using the modifier in x16. The static linker places autia1716 before the
8070b57cec5SDimitry Andric // indirect branch to x17 to authenticate the address in x17 with the modifier
8080b57cec5SDimitry Andric // in x16. This makes it more difficult for an attacker to modify the value in
8090b57cec5SDimitry Andric // the .got.plt.
8100b57cec5SDimitry Andric //
8110b57cec5SDimitry Andric // When BTI is enabled all indirect branches must land on a bti instruction.
8120b57cec5SDimitry Andric // The static linker must place a bti instruction at the start of any PLT entry
8130b57cec5SDimitry Andric // that may be the target of an indirect branch. As the PLT entries call the
8140b57cec5SDimitry Andric // lazy resolver indirectly this must have a bti instruction at start. In
8150b57cec5SDimitry Andric // general a bti instruction is not needed for a PLT entry as indirect calls
8160b57cec5SDimitry Andric // are resolved to the function address and not the PLT entry for the function.
8170b57cec5SDimitry Andric // There are a small number of cases where the PLT address can escape, such as
8180b57cec5SDimitry Andric // taking the address of a function or ifunc via a non got-generating
8190b57cec5SDimitry Andric // relocation, and a shared library refers to that symbol.
8200b57cec5SDimitry Andric //
8210b57cec5SDimitry Andric // We use the bti c variant of the instruction which permits indirect branches
8220b57cec5SDimitry Andric // (br) via x16/x17 and indirect function calls (blr) via any register. The ABI
8230b57cec5SDimitry Andric // guarantees that all indirect branches from code requiring BTI protection
8240b57cec5SDimitry Andric // will go via x16/x17
8250b57cec5SDimitry Andric 
8260b57cec5SDimitry Andric namespace {
8270b57cec5SDimitry Andric class AArch64BtiPac final : public AArch64 {
8280b57cec5SDimitry Andric public:
8290b57cec5SDimitry Andric   AArch64BtiPac();
8300b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
831480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
832480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
8330b57cec5SDimitry Andric 
8340b57cec5SDimitry Andric private:
835349cc55cSDimitry Andric   bool btiHeader; // bti instruction needed in PLT Header and Entry
8360b57cec5SDimitry Andric   bool pacEntry;  // autia1716 instruction needed in PLT Entry
8370b57cec5SDimitry Andric };
8380b57cec5SDimitry Andric } // namespace
8390b57cec5SDimitry Andric 
8400b57cec5SDimitry Andric AArch64BtiPac::AArch64BtiPac() {
8410b57cec5SDimitry Andric   btiHeader = (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI);
8420b57cec5SDimitry Andric   // A BTI (Branch Target Indicator) Plt Entry is only required if the
8430b57cec5SDimitry Andric   // address of the PLT entry can be taken by the program, which permits an
8440b57cec5SDimitry Andric   // indirect jump to the PLT entry. This can happen when the address
8450b57cec5SDimitry Andric   // of the PLT entry for a function is canonicalised due to the address of
846349cc55cSDimitry Andric   // the function in an executable being taken by a shared library, or
847349cc55cSDimitry Andric   // non-preemptible ifunc referenced by non-GOT-generating, non-PLT-generating
848349cc55cSDimitry Andric   // relocations.
8495ffd83dbSDimitry Andric   // The PAC PLT entries require dynamic loader support and this isn't known
8505ffd83dbSDimitry Andric   // from properties in the objects, so we use the command line flag.
8515ffd83dbSDimitry Andric   pacEntry = config->zPacPlt;
8520b57cec5SDimitry Andric 
853349cc55cSDimitry Andric   if (btiHeader || pacEntry) {
8540b57cec5SDimitry Andric     pltEntrySize = 24;
855480093f4SDimitry Andric     ipltEntrySize = 24;
856480093f4SDimitry Andric   }
8570b57cec5SDimitry Andric }
8580b57cec5SDimitry Andric 
8590b57cec5SDimitry Andric void AArch64BtiPac::writePltHeader(uint8_t *buf) const {
8600b57cec5SDimitry Andric   const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c
8610b57cec5SDimitry Andric   const uint8_t pltData[] = {
8620b57cec5SDimitry Andric       0xf0, 0x7b, 0xbf, 0xa9, // stp    x16, x30, [sp,#-16]!
863bdd1243dSDimitry Andric       0x10, 0x00, 0x00, 0x90, // adrp   x16, Page(&(.got.plt[2]))
864bdd1243dSDimitry Andric       0x11, 0x02, 0x40, 0xf9, // ldr    x17, [x16, Offset(&(.got.plt[2]))]
865bdd1243dSDimitry Andric       0x10, 0x02, 0x00, 0x91, // add    x16, x16, Offset(&(.got.plt[2]))
8660b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6, // br     x17
8670b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5, // nop
8680b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5  // nop
8690b57cec5SDimitry Andric   };
8700b57cec5SDimitry Andric   const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop
8710b57cec5SDimitry Andric 
8720b57cec5SDimitry Andric   uint64_t got = in.gotPlt->getVA();
8730b57cec5SDimitry Andric   uint64_t plt = in.plt->getVA();
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric   if (btiHeader) {
8760b57cec5SDimitry Andric     // PltHeader is called indirectly by plt[N]. Prefix pltData with a BTI C
8770b57cec5SDimitry Andric     // instruction.
8780b57cec5SDimitry Andric     memcpy(buf, btiData, sizeof(btiData));
8790b57cec5SDimitry Andric     buf += sizeof(btiData);
8800b57cec5SDimitry Andric     plt += sizeof(btiData);
8810b57cec5SDimitry Andric   }
8820b57cec5SDimitry Andric   memcpy(buf, pltData, sizeof(pltData));
8830b57cec5SDimitry Andric 
8845ffd83dbSDimitry Andric   relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
8850b57cec5SDimitry Andric                 getAArch64Page(got + 16) - getAArch64Page(plt + 8));
8865ffd83dbSDimitry Andric   relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16);
8875ffd83dbSDimitry Andric   relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16);
8880b57cec5SDimitry Andric   if (!btiHeader)
8890b57cec5SDimitry Andric     // We didn't add the BTI c instruction so round out size with NOP.
8900b57cec5SDimitry Andric     memcpy(buf + sizeof(pltData), nopData, sizeof(nopData));
8910b57cec5SDimitry Andric }
8920b57cec5SDimitry Andric 
893480093f4SDimitry Andric void AArch64BtiPac::writePlt(uint8_t *buf, const Symbol &sym,
894480093f4SDimitry Andric                              uint64_t pltEntryAddr) const {
8950b57cec5SDimitry Andric   // The PLT entry is of the form:
8960b57cec5SDimitry Andric   // [btiData] addrInst (pacBr | stdBr) [nopData]
8970b57cec5SDimitry Andric   const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c
8980b57cec5SDimitry Andric   const uint8_t addrInst[] = {
899bdd1243dSDimitry Andric       0x10, 0x00, 0x00, 0x90,  // adrp x16, Page(&(.got.plt[n]))
900bdd1243dSDimitry Andric       0x11, 0x02, 0x40, 0xf9,  // ldr  x17, [x16, Offset(&(.got.plt[n]))]
901bdd1243dSDimitry Andric       0x10, 0x02, 0x00, 0x91   // add  x16, x16, Offset(&(.got.plt[n]))
9020b57cec5SDimitry Andric   };
9030b57cec5SDimitry Andric   const uint8_t pacBr[] = {
9040b57cec5SDimitry Andric       0x9f, 0x21, 0x03, 0xd5,  // autia1716
9050b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6   // br   x17
9060b57cec5SDimitry Andric   };
9070b57cec5SDimitry Andric   const uint8_t stdBr[] = {
9080b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6,  // br   x17
9090b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5   // nop
9100b57cec5SDimitry Andric   };
9110b57cec5SDimitry Andric   const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop
9120b57cec5SDimitry Andric 
913bdd1243dSDimitry Andric   // NEEDS_COPY indicates a non-ifunc canonical PLT entry whose address may
914349cc55cSDimitry Andric   // escape to shared objects. isInIplt indicates a non-preemptible ifunc. Its
915*06c3fb27SDimitry Andric   // address may escape if referenced by a direct relocation. If relative
916*06c3fb27SDimitry Andric   // vtables are used then if the vtable is in a shared object the offsets will
917*06c3fb27SDimitry Andric   // be to the PLT entry. The condition is conservative.
918*06c3fb27SDimitry Andric   bool hasBti = btiHeader &&
919*06c3fb27SDimitry Andric                 (sym.hasFlag(NEEDS_COPY) || sym.isInIplt || sym.thunkAccessed);
920349cc55cSDimitry Andric   if (hasBti) {
9210b57cec5SDimitry Andric     memcpy(buf, btiData, sizeof(btiData));
9220b57cec5SDimitry Andric     buf += sizeof(btiData);
9230b57cec5SDimitry Andric     pltEntryAddr += sizeof(btiData);
9240b57cec5SDimitry Andric   }
9250b57cec5SDimitry Andric 
926480093f4SDimitry Andric   uint64_t gotPltEntryAddr = sym.getGotPltVA();
9270b57cec5SDimitry Andric   memcpy(buf, addrInst, sizeof(addrInst));
9285ffd83dbSDimitry Andric   relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
9295ffd83dbSDimitry Andric                 getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr));
9305ffd83dbSDimitry Andric   relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr);
9315ffd83dbSDimitry Andric   relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr);
9320b57cec5SDimitry Andric 
9330b57cec5SDimitry Andric   if (pacEntry)
9340b57cec5SDimitry Andric     memcpy(buf + sizeof(addrInst), pacBr, sizeof(pacBr));
9350b57cec5SDimitry Andric   else
9360b57cec5SDimitry Andric     memcpy(buf + sizeof(addrInst), stdBr, sizeof(stdBr));
937349cc55cSDimitry Andric   if (!hasBti)
9380b57cec5SDimitry Andric     // We didn't add the BTI c instruction so round out size with NOP.
9390b57cec5SDimitry Andric     memcpy(buf + sizeof(addrInst) + sizeof(stdBr), nopData, sizeof(nopData));
9400b57cec5SDimitry Andric }
9410b57cec5SDimitry Andric 
9420b57cec5SDimitry Andric static TargetInfo *getTargetInfo() {
94361cfbce3SDimitry Andric   if ((config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) ||
94461cfbce3SDimitry Andric       config->zPacPlt) {
9450b57cec5SDimitry Andric     static AArch64BtiPac t;
9460b57cec5SDimitry Andric     return &t;
9470b57cec5SDimitry Andric   }
9480b57cec5SDimitry Andric   static AArch64 t;
9490b57cec5SDimitry Andric   return &t;
9500b57cec5SDimitry Andric }
9510b57cec5SDimitry Andric 
9525ffd83dbSDimitry Andric TargetInfo *elf::getAArch64TargetInfo() { return getTargetInfo(); }
953