xref: /freebsd/contrib/llvm-project/lld/ELF/Arch/AArch64.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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 
9*bdd1243dSDimitry 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;
39*bdd1243dSDimitry 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;
52*bdd1243dSDimitry Andric   void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
53*bdd1243dSDimitry Andric 
54*bdd1243dSDimitry Andric private:
55*bdd1243dSDimitry Andric   void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
56*bdd1243dSDimitry Andric   void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
57*bdd1243dSDimitry Andric   void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
58*bdd1243dSDimitry Andric };
59*bdd1243dSDimitry Andric 
60*bdd1243dSDimitry Andric struct AArch64Relaxer {
61*bdd1243dSDimitry Andric   bool safeToRelaxAdrpLdr = false;
62*bdd1243dSDimitry Andric 
63*bdd1243dSDimitry Andric   AArch64Relaxer(ArrayRef<Relocation> relocs);
64*bdd1243dSDimitry Andric   bool tryRelaxAdrpAdd(const Relocation &adrpRel, const Relocation &addRel,
65*bdd1243dSDimitry Andric                        uint64_t secAddr, uint8_t *buf) const;
66*bdd1243dSDimitry Andric   bool tryRelaxAdrpLdr(const Relocation &adrpRel, const Relocation &ldrRel,
67*bdd1243dSDimitry 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:
1395ffd83dbSDimitry Andric   case R_AARCH64_PLT32:
1400b57cec5SDimitry Andric     return R_PLT_PC;
1410b57cec5SDimitry Andric   case R_AARCH64_PREL16:
1420b57cec5SDimitry Andric   case R_AARCH64_PREL32:
1430b57cec5SDimitry Andric   case R_AARCH64_PREL64:
1440b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_LO21:
1450b57cec5SDimitry Andric   case R_AARCH64_LD_PREL_LO19:
14685868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G0:
14785868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G0_NC:
14885868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G1:
14985868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G1_NC:
15085868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G2:
15185868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G2_NC:
15285868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G3:
1530b57cec5SDimitry Andric     return R_PC;
1540b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_PG_HI21:
1550b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_PG_HI21_NC:
1560b57cec5SDimitry Andric     return R_AARCH64_PAGE_PC;
1570b57cec5SDimitry Andric   case R_AARCH64_LD64_GOT_LO12_NC:
1580b57cec5SDimitry Andric   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1590b57cec5SDimitry Andric     return R_GOT;
160e8d8bef9SDimitry Andric   case R_AARCH64_LD64_GOTPAGE_LO15:
161e8d8bef9SDimitry Andric     return R_AARCH64_GOT_PAGE;
1620b57cec5SDimitry Andric   case R_AARCH64_ADR_GOT_PAGE:
1630b57cec5SDimitry Andric   case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1640b57cec5SDimitry Andric     return R_AARCH64_GOT_PAGE_PC;
1650b57cec5SDimitry Andric   case R_AARCH64_NONE:
1660b57cec5SDimitry Andric     return R_NONE;
1670b57cec5SDimitry Andric   default:
16885868e8aSDimitry Andric     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
16985868e8aSDimitry Andric           ") against symbol " + toString(s));
17085868e8aSDimitry Andric     return R_NONE;
1710b57cec5SDimitry Andric   }
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric 
174e8d8bef9SDimitry Andric RelExpr AArch64::adjustTlsExpr(RelType type, RelExpr expr) const {
1750b57cec5SDimitry Andric   if (expr == R_RELAX_TLS_GD_TO_IE) {
1760b57cec5SDimitry Andric     if (type == R_AARCH64_TLSDESC_ADR_PAGE21)
1770b57cec5SDimitry Andric       return R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC;
1780b57cec5SDimitry Andric     return R_RELAX_TLS_GD_TO_IE_ABS;
1790b57cec5SDimitry Andric   }
1800b57cec5SDimitry Andric   return expr;
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric bool AArch64::usesOnlyLowPageBits(RelType type) const {
1840b57cec5SDimitry Andric   switch (type) {
1850b57cec5SDimitry Andric   default:
1860b57cec5SDimitry Andric     return false;
1870b57cec5SDimitry Andric   case R_AARCH64_ADD_ABS_LO12_NC:
1880b57cec5SDimitry Andric   case R_AARCH64_LD64_GOT_LO12_NC:
1890b57cec5SDimitry Andric   case R_AARCH64_LDST128_ABS_LO12_NC:
1900b57cec5SDimitry Andric   case R_AARCH64_LDST16_ABS_LO12_NC:
1910b57cec5SDimitry Andric   case R_AARCH64_LDST32_ABS_LO12_NC:
1920b57cec5SDimitry Andric   case R_AARCH64_LDST64_ABS_LO12_NC:
1930b57cec5SDimitry Andric   case R_AARCH64_LDST8_ABS_LO12_NC:
1940b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
1950b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
1960b57cec5SDimitry Andric   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1970b57cec5SDimitry Andric     return true;
1980b57cec5SDimitry Andric   }
1990b57cec5SDimitry Andric }
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric RelType AArch64::getDynRel(RelType type) const {
2020b57cec5SDimitry Andric   if (type == R_AARCH64_ABS64)
2030b57cec5SDimitry Andric     return type;
2040b57cec5SDimitry Andric   return R_AARCH64_NONE;
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric 
207fe6060f1SDimitry Andric int64_t AArch64::getImplicitAddend(const uint8_t *buf, RelType type) const {
208fe6060f1SDimitry Andric   switch (type) {
209fe6060f1SDimitry Andric   case R_AARCH64_TLSDESC:
210fe6060f1SDimitry Andric     return read64(buf + 8);
211298c3e8dSDimitry Andric   case R_AARCH64_NONE:
212*bdd1243dSDimitry Andric   case R_AARCH64_GLOB_DAT:
213*bdd1243dSDimitry Andric   case R_AARCH64_JUMP_SLOT:
214298c3e8dSDimitry Andric     return 0;
215298c3e8dSDimitry Andric   case R_AARCH64_PREL32:
216298c3e8dSDimitry Andric     return SignExtend64<32>(read32(buf));
217298c3e8dSDimitry Andric   case R_AARCH64_ABS64:
218298c3e8dSDimitry Andric   case R_AARCH64_PREL64:
219*bdd1243dSDimitry Andric   case R_AARCH64_RELATIVE:
220*bdd1243dSDimitry Andric   case R_AARCH64_IRELATIVE:
221*bdd1243dSDimitry Andric   case R_AARCH64_TLS_TPREL64:
222298c3e8dSDimitry Andric     return read64(buf);
223fe6060f1SDimitry Andric   default:
224fe6060f1SDimitry Andric     internalLinkerError(getErrorLocation(buf),
225fe6060f1SDimitry Andric                         "cannot read addend for relocation " + toString(type));
226fe6060f1SDimitry Andric     return 0;
227fe6060f1SDimitry Andric   }
228fe6060f1SDimitry Andric }
229fe6060f1SDimitry Andric 
2300b57cec5SDimitry Andric void AArch64::writeGotPlt(uint8_t *buf, const Symbol &) const {
231fe6060f1SDimitry Andric   write64(buf, in.plt->getVA());
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric 
234*bdd1243dSDimitry Andric void AArch64::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
235*bdd1243dSDimitry Andric   if (config->writeAddends)
236*bdd1243dSDimitry Andric     write64(buf, s.getVA());
237*bdd1243dSDimitry Andric }
238*bdd1243dSDimitry Andric 
2390b57cec5SDimitry Andric void AArch64::writePltHeader(uint8_t *buf) const {
2400b57cec5SDimitry Andric   const uint8_t pltData[] = {
2410b57cec5SDimitry Andric       0xf0, 0x7b, 0xbf, 0xa9, // stp    x16, x30, [sp,#-16]!
242*bdd1243dSDimitry Andric       0x10, 0x00, 0x00, 0x90, // adrp   x16, Page(&(.got.plt[2]))
243*bdd1243dSDimitry Andric       0x11, 0x02, 0x40, 0xf9, // ldr    x17, [x16, Offset(&(.got.plt[2]))]
244*bdd1243dSDimitry Andric       0x10, 0x02, 0x00, 0x91, // add    x16, x16, Offset(&(.got.plt[2]))
2450b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6, // br     x17
2460b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5, // nop
2470b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5, // nop
2480b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5  // nop
2490b57cec5SDimitry Andric   };
2500b57cec5SDimitry Andric   memcpy(buf, pltData, sizeof(pltData));
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   uint64_t got = in.gotPlt->getVA();
2530b57cec5SDimitry Andric   uint64_t plt = in.plt->getVA();
2545ffd83dbSDimitry Andric   relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
2550b57cec5SDimitry Andric                 getAArch64Page(got + 16) - getAArch64Page(plt + 4));
2565ffd83dbSDimitry Andric   relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16);
2575ffd83dbSDimitry Andric   relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16);
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric 
260480093f4SDimitry Andric void AArch64::writePlt(uint8_t *buf, const Symbol &sym,
261480093f4SDimitry Andric                        uint64_t pltEntryAddr) const {
2620b57cec5SDimitry Andric   const uint8_t inst[] = {
263*bdd1243dSDimitry Andric       0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.got.plt[n]))
264*bdd1243dSDimitry Andric       0x11, 0x02, 0x40, 0xf9, // ldr  x17, [x16, Offset(&(.got.plt[n]))]
265*bdd1243dSDimitry Andric       0x10, 0x02, 0x00, 0x91, // add  x16, x16, Offset(&(.got.plt[n]))
2660b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6  // br   x17
2670b57cec5SDimitry Andric   };
2680b57cec5SDimitry Andric   memcpy(buf, inst, sizeof(inst));
2690b57cec5SDimitry Andric 
270480093f4SDimitry Andric   uint64_t gotPltEntryAddr = sym.getGotPltVA();
2715ffd83dbSDimitry Andric   relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
2720b57cec5SDimitry Andric                 getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr));
2735ffd83dbSDimitry Andric   relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr);
2745ffd83dbSDimitry Andric   relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr);
2750b57cec5SDimitry Andric }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric bool AArch64::needsThunk(RelExpr expr, RelType type, const InputFile *file,
278480093f4SDimitry Andric                          uint64_t branchAddr, const Symbol &s,
279480093f4SDimitry Andric                          int64_t a) const {
2802a66634dSDimitry Andric   // If s is an undefined weak symbol and does not have a PLT entry then it will
2812a66634dSDimitry Andric   // be resolved as a branch to the next instruction. If it is hidden, its
2822a66634dSDimitry Andric   // binding has been converted to local, so we just check isUndefined() here. A
2832a66634dSDimitry Andric   // undefined non-weak symbol will have been errored.
2842a66634dSDimitry Andric   if (s.isUndefined() && !s.isInPlt())
285480093f4SDimitry Andric     return false;
2860b57cec5SDimitry Andric   // ELF for the ARM 64-bit architecture, section Call and Jump relocations
2870b57cec5SDimitry Andric   // only permits range extension thunks for R_AARCH64_CALL26 and
2880b57cec5SDimitry Andric   // R_AARCH64_JUMP26 relocation types.
2895ffd83dbSDimitry Andric   if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
2905ffd83dbSDimitry Andric       type != R_AARCH64_PLT32)
2910b57cec5SDimitry Andric     return false;
292480093f4SDimitry Andric   uint64_t dst = expr == R_PLT_PC ? s.getPltVA() : s.getVA(a);
2930b57cec5SDimitry Andric   return !inBranchRange(type, branchAddr, dst);
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric uint32_t AArch64::getThunkSectionSpacing() const {
2970b57cec5SDimitry Andric   // See comment in Arch/ARM.cpp for a more detailed explanation of
2980b57cec5SDimitry Andric   // getThunkSectionSpacing(). For AArch64 the only branches we are permitted to
2990b57cec5SDimitry Andric   // Thunk have a range of +/- 128 MiB
3000b57cec5SDimitry Andric   return (128 * 1024 * 1024) - 0x30000;
3010b57cec5SDimitry Andric }
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric bool AArch64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
3045ffd83dbSDimitry Andric   if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
3055ffd83dbSDimitry Andric       type != R_AARCH64_PLT32)
3060b57cec5SDimitry Andric     return true;
3070b57cec5SDimitry Andric   // The AArch64 call and unconditional branch instructions have a range of
3085ffd83dbSDimitry Andric   // +/- 128 MiB. The PLT32 relocation supports a range up to +/- 2 GiB.
3095ffd83dbSDimitry Andric   uint64_t range =
3105ffd83dbSDimitry Andric       type == R_AARCH64_PLT32 ? (UINT64_C(1) << 31) : (128 * 1024 * 1024);
3110b57cec5SDimitry Andric   if (dst > src) {
3120b57cec5SDimitry Andric     // Immediate of branch is signed.
3130b57cec5SDimitry Andric     range -= 4;
3140b57cec5SDimitry Andric     return dst - src <= range;
3150b57cec5SDimitry Andric   }
3160b57cec5SDimitry Andric   return src - dst <= range;
3170b57cec5SDimitry Andric }
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric static void write32AArch64Addr(uint8_t *l, uint64_t imm) {
3200b57cec5SDimitry Andric   uint32_t immLo = (imm & 0x3) << 29;
3210b57cec5SDimitry Andric   uint32_t immHi = (imm & 0x1FFFFC) << 3;
3220b57cec5SDimitry Andric   uint64_t mask = (0x3 << 29) | (0x1FFFFC << 3);
3230b57cec5SDimitry Andric   write32le(l, (read32le(l) & ~mask) | immLo | immHi);
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric // Return the bits [Start, End] from Val shifted Start bits.
3270b57cec5SDimitry Andric // For instance, getBits(0xF0, 4, 8) returns 0xF.
3280b57cec5SDimitry Andric static uint64_t getBits(uint64_t val, int start, int end) {
3290b57cec5SDimitry Andric   uint64_t mask = ((uint64_t)1 << (end + 1 - start)) - 1;
3300b57cec5SDimitry Andric   return (val >> start) & mask;
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric static void or32le(uint8_t *p, int32_t v) { write32le(p, read32le(p) | v); }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric // Update the immediate field in a AARCH64 ldr, str, and add instruction.
3360b57cec5SDimitry Andric static void or32AArch64Imm(uint8_t *l, uint64_t imm) {
3370b57cec5SDimitry Andric   or32le(l, (imm & 0xFFF) << 10);
3380b57cec5SDimitry Andric }
3390b57cec5SDimitry Andric 
34085868e8aSDimitry Andric // Update the immediate field in an AArch64 movk, movn or movz instruction
34185868e8aSDimitry Andric // for a signed relocation, and update the opcode of a movn or movz instruction
34285868e8aSDimitry Andric // to match the sign of the operand.
34385868e8aSDimitry Andric static void writeSMovWImm(uint8_t *loc, uint32_t imm) {
34485868e8aSDimitry Andric   uint32_t inst = read32le(loc);
34585868e8aSDimitry Andric   // Opcode field is bits 30, 29, with 10 = movz, 00 = movn and 11 = movk.
34685868e8aSDimitry Andric   if (!(inst & (1 << 29))) {
34785868e8aSDimitry Andric     // movn or movz.
34885868e8aSDimitry Andric     if (imm & 0x10000) {
34985868e8aSDimitry Andric       // Change opcode to movn, which takes an inverted operand.
35085868e8aSDimitry Andric       imm ^= 0xFFFF;
35185868e8aSDimitry Andric       inst &= ~(1 << 30);
35285868e8aSDimitry Andric     } else {
35385868e8aSDimitry Andric       // Change opcode to movz.
35485868e8aSDimitry Andric       inst |= 1 << 30;
35585868e8aSDimitry Andric     }
35685868e8aSDimitry Andric   }
35785868e8aSDimitry Andric   write32le(loc, inst | ((imm & 0xFFFF) << 5));
35885868e8aSDimitry Andric }
35985868e8aSDimitry Andric 
3605ffd83dbSDimitry Andric void AArch64::relocate(uint8_t *loc, const Relocation &rel,
3615ffd83dbSDimitry Andric                        uint64_t val) const {
3625ffd83dbSDimitry Andric   switch (rel.type) {
3630b57cec5SDimitry Andric   case R_AARCH64_ABS16:
3640b57cec5SDimitry Andric   case R_AARCH64_PREL16:
3655ffd83dbSDimitry Andric     checkIntUInt(loc, val, 16, rel);
366fe6060f1SDimitry Andric     write16(loc, val);
3670b57cec5SDimitry Andric     break;
3680b57cec5SDimitry Andric   case R_AARCH64_ABS32:
3690b57cec5SDimitry Andric   case R_AARCH64_PREL32:
3705ffd83dbSDimitry Andric     checkIntUInt(loc, val, 32, rel);
371fe6060f1SDimitry Andric     write32(loc, val);
3725ffd83dbSDimitry Andric     break;
3735ffd83dbSDimitry Andric   case R_AARCH64_PLT32:
3745ffd83dbSDimitry Andric     checkInt(loc, val, 32, rel);
375fe6060f1SDimitry Andric     write32(loc, val);
3760b57cec5SDimitry Andric     break;
3770b57cec5SDimitry Andric   case R_AARCH64_ABS64:
3780b57cec5SDimitry Andric   case R_AARCH64_PREL64:
379fe6060f1SDimitry Andric     write64(loc, val);
3800b57cec5SDimitry Andric     break;
3810b57cec5SDimitry Andric   case R_AARCH64_ADD_ABS_LO12_NC:
3820b57cec5SDimitry Andric     or32AArch64Imm(loc, val);
3830b57cec5SDimitry Andric     break;
3840b57cec5SDimitry Andric   case R_AARCH64_ADR_GOT_PAGE:
3850b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_PG_HI21:
3860b57cec5SDimitry Andric   case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3870b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADR_PAGE21:
3885ffd83dbSDimitry Andric     checkInt(loc, val, 33, rel);
389*bdd1243dSDimitry Andric     [[fallthrough]];
3900b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_PG_HI21_NC:
3910b57cec5SDimitry Andric     write32AArch64Addr(loc, val >> 12);
3920b57cec5SDimitry Andric     break;
3930b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_LO21:
3945ffd83dbSDimitry Andric     checkInt(loc, val, 21, rel);
3950b57cec5SDimitry Andric     write32AArch64Addr(loc, val);
3960b57cec5SDimitry Andric     break;
3970b57cec5SDimitry Andric   case R_AARCH64_JUMP26:
3980b57cec5SDimitry Andric     // Normally we would just write the bits of the immediate field, however
3990b57cec5SDimitry Andric     // when patching instructions for the cpu errata fix -fix-cortex-a53-843419
4000b57cec5SDimitry Andric     // we want to replace a non-branch instruction with a branch immediate
4010b57cec5SDimitry Andric     // instruction. By writing all the bits of the instruction including the
4020b57cec5SDimitry Andric     // opcode and the immediate (0 001 | 01 imm26) we can do this
4030b57cec5SDimitry Andric     // transformation by placing a R_AARCH64_JUMP26 relocation at the offset of
4040b57cec5SDimitry Andric     // the instruction we want to patch.
4050b57cec5SDimitry Andric     write32le(loc, 0x14000000);
406*bdd1243dSDimitry Andric     [[fallthrough]];
4070b57cec5SDimitry Andric   case R_AARCH64_CALL26:
4085ffd83dbSDimitry Andric     checkInt(loc, val, 28, rel);
4090b57cec5SDimitry Andric     or32le(loc, (val & 0x0FFFFFFC) >> 2);
4100b57cec5SDimitry Andric     break;
4110b57cec5SDimitry Andric   case R_AARCH64_CONDBR19:
4120b57cec5SDimitry Andric   case R_AARCH64_LD_PREL_LO19:
4135ffd83dbSDimitry Andric     checkAlignment(loc, val, 4, rel);
4145ffd83dbSDimitry Andric     checkInt(loc, val, 21, rel);
4150b57cec5SDimitry Andric     or32le(loc, (val & 0x1FFFFC) << 3);
4160b57cec5SDimitry Andric     break;
4170b57cec5SDimitry Andric   case R_AARCH64_LDST8_ABS_LO12_NC:
4180b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
4190b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 0, 11));
4200b57cec5SDimitry Andric     break;
4210b57cec5SDimitry Andric   case R_AARCH64_LDST16_ABS_LO12_NC:
4220b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
4235ffd83dbSDimitry Andric     checkAlignment(loc, val, 2, rel);
4240b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 1, 11));
4250b57cec5SDimitry Andric     break;
4260b57cec5SDimitry Andric   case R_AARCH64_LDST32_ABS_LO12_NC:
4270b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
4285ffd83dbSDimitry Andric     checkAlignment(loc, val, 4, rel);
4290b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 2, 11));
4300b57cec5SDimitry Andric     break;
4310b57cec5SDimitry Andric   case R_AARCH64_LDST64_ABS_LO12_NC:
4320b57cec5SDimitry Andric   case R_AARCH64_LD64_GOT_LO12_NC:
4330b57cec5SDimitry Andric   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4340b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
4350b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
4365ffd83dbSDimitry Andric     checkAlignment(loc, val, 8, rel);
4370b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 3, 11));
4380b57cec5SDimitry Andric     break;
4390b57cec5SDimitry Andric   case R_AARCH64_LDST128_ABS_LO12_NC:
4400b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC:
4415ffd83dbSDimitry Andric     checkAlignment(loc, val, 16, rel);
4420b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 4, 11));
4430b57cec5SDimitry Andric     break;
444e8d8bef9SDimitry Andric   case R_AARCH64_LD64_GOTPAGE_LO15:
445e8d8bef9SDimitry Andric     checkAlignment(loc, val, 8, rel);
446e8d8bef9SDimitry Andric     or32AArch64Imm(loc, getBits(val, 3, 14));
447e8d8bef9SDimitry Andric     break;
44885868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G0:
4495ffd83dbSDimitry Andric     checkUInt(loc, val, 16, rel);
450*bdd1243dSDimitry Andric     [[fallthrough]];
4510b57cec5SDimitry Andric   case R_AARCH64_MOVW_UABS_G0_NC:
4520b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFF) << 5);
4530b57cec5SDimitry Andric     break;
45485868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G1:
4555ffd83dbSDimitry Andric     checkUInt(loc, val, 32, rel);
456*bdd1243dSDimitry Andric     [[fallthrough]];
4570b57cec5SDimitry Andric   case R_AARCH64_MOVW_UABS_G1_NC:
4580b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFF0000) >> 11);
4590b57cec5SDimitry Andric     break;
46085868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G2:
4615ffd83dbSDimitry Andric     checkUInt(loc, val, 48, rel);
462*bdd1243dSDimitry Andric     [[fallthrough]];
4630b57cec5SDimitry Andric   case R_AARCH64_MOVW_UABS_G2_NC:
4640b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFF00000000) >> 27);
4650b57cec5SDimitry Andric     break;
4660b57cec5SDimitry Andric   case R_AARCH64_MOVW_UABS_G3:
4670b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFF000000000000) >> 43);
4680b57cec5SDimitry Andric     break;
46985868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G0:
47085868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G0:
47185868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G0:
4725ffd83dbSDimitry Andric     checkInt(loc, val, 17, rel);
473*bdd1243dSDimitry Andric     [[fallthrough]];
47485868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G0_NC:
47585868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
47685868e8aSDimitry Andric     writeSMovWImm(loc, val);
47785868e8aSDimitry Andric     break;
47885868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G1:
47985868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G1:
48085868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G1:
4815ffd83dbSDimitry Andric     checkInt(loc, val, 33, rel);
482*bdd1243dSDimitry Andric     [[fallthrough]];
48385868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G1_NC:
48485868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
48585868e8aSDimitry Andric     writeSMovWImm(loc, val >> 16);
48685868e8aSDimitry Andric     break;
48785868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G2:
48885868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G2:
48985868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G2:
4905ffd83dbSDimitry Andric     checkInt(loc, val, 49, rel);
491*bdd1243dSDimitry Andric     [[fallthrough]];
49285868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G2_NC:
49385868e8aSDimitry Andric     writeSMovWImm(loc, val >> 32);
49485868e8aSDimitry Andric     break;
49585868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G3:
49685868e8aSDimitry Andric     writeSMovWImm(loc, val >> 48);
49785868e8aSDimitry Andric     break;
4980b57cec5SDimitry Andric   case R_AARCH64_TSTBR14:
4995ffd83dbSDimitry Andric     checkInt(loc, val, 16, rel);
5000b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFC) << 3);
5010b57cec5SDimitry Andric     break;
5020b57cec5SDimitry Andric   case R_AARCH64_TLSLE_ADD_TPREL_HI12:
5035ffd83dbSDimitry Andric     checkUInt(loc, val, 24, rel);
5040b57cec5SDimitry Andric     or32AArch64Imm(loc, val >> 12);
5050b57cec5SDimitry Andric     break;
5060b57cec5SDimitry Andric   case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5070b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
5080b57cec5SDimitry Andric     or32AArch64Imm(loc, val);
5090b57cec5SDimitry Andric     break;
510fe6060f1SDimitry Andric   case R_AARCH64_TLSDESC:
511fe6060f1SDimitry Andric     // For R_AARCH64_TLSDESC the addend is stored in the second 64-bit word.
512fe6060f1SDimitry Andric     write64(loc + 8, val);
513fe6060f1SDimitry Andric     break;
5140b57cec5SDimitry Andric   default:
51585868e8aSDimitry Andric     llvm_unreachable("unknown relocation");
5160b57cec5SDimitry Andric   }
5170b57cec5SDimitry Andric }
5180b57cec5SDimitry Andric 
5195ffd83dbSDimitry Andric void AArch64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
5205ffd83dbSDimitry Andric                              uint64_t val) const {
5210b57cec5SDimitry Andric   // TLSDESC Global-Dynamic relocation are in the form:
5220b57cec5SDimitry Andric   //   adrp    x0, :tlsdesc:v             [R_AARCH64_TLSDESC_ADR_PAGE21]
5230b57cec5SDimitry Andric   //   ldr     x1, [x0, #:tlsdesc_lo12:v  [R_AARCH64_TLSDESC_LD64_LO12]
5240b57cec5SDimitry Andric   //   add     x0, x0, :tlsdesc_los:v     [R_AARCH64_TLSDESC_ADD_LO12]
5250b57cec5SDimitry Andric   //   .tlsdesccall                       [R_AARCH64_TLSDESC_CALL]
5260b57cec5SDimitry Andric   //   blr     x1
5270b57cec5SDimitry Andric   // And it can optimized to:
5280b57cec5SDimitry Andric   //   movz    x0, #0x0, lsl #16
5290b57cec5SDimitry Andric   //   movk    x0, #0x10
5300b57cec5SDimitry Andric   //   nop
5310b57cec5SDimitry Andric   //   nop
5325ffd83dbSDimitry Andric   checkUInt(loc, val, 32, rel);
5330b57cec5SDimitry Andric 
5345ffd83dbSDimitry Andric   switch (rel.type) {
5350b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
5360b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_CALL:
5370b57cec5SDimitry Andric     write32le(loc, 0xd503201f); // nop
5380b57cec5SDimitry Andric     return;
5390b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADR_PAGE21:
5400b57cec5SDimitry Andric     write32le(loc, 0xd2a00000 | (((val >> 16) & 0xffff) << 5)); // movz
5410b57cec5SDimitry Andric     return;
5420b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
5430b57cec5SDimitry Andric     write32le(loc, 0xf2800000 | ((val & 0xffff) << 5)); // movk
5440b57cec5SDimitry Andric     return;
5450b57cec5SDimitry Andric   default:
5460b57cec5SDimitry Andric     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
5470b57cec5SDimitry Andric   }
5480b57cec5SDimitry Andric }
5490b57cec5SDimitry Andric 
5505ffd83dbSDimitry Andric void AArch64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
5515ffd83dbSDimitry Andric                              uint64_t val) const {
5520b57cec5SDimitry Andric   // TLSDESC Global-Dynamic relocation are in the form:
5530b57cec5SDimitry Andric   //   adrp    x0, :tlsdesc:v             [R_AARCH64_TLSDESC_ADR_PAGE21]
5540b57cec5SDimitry Andric   //   ldr     x1, [x0, #:tlsdesc_lo12:v  [R_AARCH64_TLSDESC_LD64_LO12]
5550b57cec5SDimitry Andric   //   add     x0, x0, :tlsdesc_los:v     [R_AARCH64_TLSDESC_ADD_LO12]
5560b57cec5SDimitry Andric   //   .tlsdesccall                       [R_AARCH64_TLSDESC_CALL]
5570b57cec5SDimitry Andric   //   blr     x1
5580b57cec5SDimitry Andric   // And it can optimized to:
5590b57cec5SDimitry Andric   //   adrp    x0, :gottprel:v
5600b57cec5SDimitry Andric   //   ldr     x0, [x0, :gottprel_lo12:v]
5610b57cec5SDimitry Andric   //   nop
5620b57cec5SDimitry Andric   //   nop
5630b57cec5SDimitry Andric 
5645ffd83dbSDimitry Andric   switch (rel.type) {
5650b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
5660b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_CALL:
5670b57cec5SDimitry Andric     write32le(loc, 0xd503201f); // nop
5680b57cec5SDimitry Andric     break;
5690b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADR_PAGE21:
5700b57cec5SDimitry Andric     write32le(loc, 0x90000000); // adrp
5715ffd83dbSDimitry Andric     relocateNoSym(loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, val);
5720b57cec5SDimitry Andric     break;
5730b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
5740b57cec5SDimitry Andric     write32le(loc, 0xf9400000); // ldr
5755ffd83dbSDimitry Andric     relocateNoSym(loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, val);
5760b57cec5SDimitry Andric     break;
5770b57cec5SDimitry Andric   default:
5780b57cec5SDimitry Andric     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
5790b57cec5SDimitry Andric   }
5800b57cec5SDimitry Andric }
5810b57cec5SDimitry Andric 
5825ffd83dbSDimitry Andric void AArch64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
5835ffd83dbSDimitry Andric                              uint64_t val) const {
5845ffd83dbSDimitry Andric   checkUInt(loc, val, 32, rel);
5850b57cec5SDimitry Andric 
5865ffd83dbSDimitry Andric   if (rel.type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
5870b57cec5SDimitry Andric     // Generate MOVZ.
5880b57cec5SDimitry Andric     uint32_t regNo = read32le(loc) & 0x1f;
5890b57cec5SDimitry Andric     write32le(loc, (0xd2a00000 | regNo) | (((val >> 16) & 0xffff) << 5));
5900b57cec5SDimitry Andric     return;
5910b57cec5SDimitry Andric   }
5925ffd83dbSDimitry Andric   if (rel.type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
5930b57cec5SDimitry Andric     // Generate MOVK.
5940b57cec5SDimitry Andric     uint32_t regNo = read32le(loc) & 0x1f;
5950b57cec5SDimitry Andric     write32le(loc, (0xf2800000 | regNo) | ((val & 0xffff) << 5));
5960b57cec5SDimitry Andric     return;
5970b57cec5SDimitry Andric   }
5980b57cec5SDimitry Andric   llvm_unreachable("invalid relocation for TLS IE to LE relaxation");
5990b57cec5SDimitry Andric }
6000b57cec5SDimitry Andric 
60104eeddc0SDimitry Andric AArch64Relaxer::AArch64Relaxer(ArrayRef<Relocation> relocs) {
602*bdd1243dSDimitry Andric   if (!config->relax)
60304eeddc0SDimitry Andric     return;
60404eeddc0SDimitry Andric   // Check if R_AARCH64_ADR_GOT_PAGE and R_AARCH64_LD64_GOT_LO12_NC
60504eeddc0SDimitry Andric   // always appear in pairs.
60604eeddc0SDimitry Andric   size_t i = 0;
60704eeddc0SDimitry Andric   const size_t size = relocs.size();
60804eeddc0SDimitry Andric   for (; i != size; ++i) {
60904eeddc0SDimitry Andric     if (relocs[i].type == R_AARCH64_ADR_GOT_PAGE) {
61004eeddc0SDimitry Andric       if (i + 1 < size && relocs[i + 1].type == R_AARCH64_LD64_GOT_LO12_NC) {
61104eeddc0SDimitry Andric         ++i;
61204eeddc0SDimitry Andric         continue;
61304eeddc0SDimitry Andric       }
61404eeddc0SDimitry Andric       break;
61504eeddc0SDimitry Andric     } else if (relocs[i].type == R_AARCH64_LD64_GOT_LO12_NC) {
61604eeddc0SDimitry Andric       break;
61704eeddc0SDimitry Andric     }
61804eeddc0SDimitry Andric   }
61904eeddc0SDimitry Andric   safeToRelaxAdrpLdr = i == size;
62004eeddc0SDimitry Andric }
62104eeddc0SDimitry Andric 
6221fd87a68SDimitry Andric bool AArch64Relaxer::tryRelaxAdrpAdd(const Relocation &adrpRel,
6231fd87a68SDimitry Andric                                      const Relocation &addRel, uint64_t secAddr,
6241fd87a68SDimitry Andric                                      uint8_t *buf) const {
6251fd87a68SDimitry Andric   // When the address of sym is within the range of ADR then
6261fd87a68SDimitry Andric   // we may relax
6271fd87a68SDimitry Andric   // ADRP xn, sym
6281fd87a68SDimitry Andric   // ADD  xn, xn, :lo12: sym
6291fd87a68SDimitry Andric   // to
6301fd87a68SDimitry Andric   // NOP
6311fd87a68SDimitry Andric   // ADR xn, sym
6321fd87a68SDimitry Andric   if (!config->relax || adrpRel.type != R_AARCH64_ADR_PREL_PG_HI21 ||
6331fd87a68SDimitry Andric       addRel.type != R_AARCH64_ADD_ABS_LO12_NC)
6341fd87a68SDimitry Andric     return false;
6351fd87a68SDimitry Andric   // Check if the relocations apply to consecutive instructions.
6361fd87a68SDimitry Andric   if (adrpRel.offset + 4 != addRel.offset)
6371fd87a68SDimitry Andric     return false;
6381fd87a68SDimitry Andric   if (adrpRel.sym != addRel.sym)
6391fd87a68SDimitry Andric     return false;
6401fd87a68SDimitry Andric   if (adrpRel.addend != 0 || addRel.addend != 0)
6411fd87a68SDimitry Andric     return false;
6421fd87a68SDimitry Andric 
6431fd87a68SDimitry Andric   uint32_t adrpInstr = read32le(buf + adrpRel.offset);
6441fd87a68SDimitry Andric   uint32_t addInstr = read32le(buf + addRel.offset);
6451fd87a68SDimitry Andric   // Check if the first instruction is ADRP and the second instruction is ADD.
6461fd87a68SDimitry Andric   if ((adrpInstr & 0x9f000000) != 0x90000000 ||
6471fd87a68SDimitry Andric       (addInstr & 0xffc00000) != 0x91000000)
6481fd87a68SDimitry Andric     return false;
6491fd87a68SDimitry Andric   uint32_t adrpDestReg = adrpInstr & 0x1f;
6501fd87a68SDimitry Andric   uint32_t addDestReg = addInstr & 0x1f;
6511fd87a68SDimitry Andric   uint32_t addSrcReg = (addInstr >> 5) & 0x1f;
6521fd87a68SDimitry Andric   if (adrpDestReg != addDestReg || adrpDestReg != addSrcReg)
6531fd87a68SDimitry Andric     return false;
6541fd87a68SDimitry Andric 
6551fd87a68SDimitry Andric   Symbol &sym = *adrpRel.sym;
6561fd87a68SDimitry Andric   // Check if the address difference is within 1MiB range.
6571fd87a68SDimitry Andric   int64_t val = sym.getVA() - (secAddr + addRel.offset);
6581fd87a68SDimitry Andric   if (val < -1024 * 1024 || val >= 1024 * 1024)
6591fd87a68SDimitry Andric     return false;
6601fd87a68SDimitry Andric 
6611fd87a68SDimitry Andric   Relocation adrRel = {R_ABS, R_AARCH64_ADR_PREL_LO21, addRel.offset,
6621fd87a68SDimitry Andric                        /*addend=*/0, &sym};
6631fd87a68SDimitry Andric   // nop
6641fd87a68SDimitry Andric   write32le(buf + adrpRel.offset, 0xd503201f);
6651fd87a68SDimitry Andric   // adr x_<dest_reg>
6661fd87a68SDimitry Andric   write32le(buf + adrRel.offset, 0x10000000 | adrpDestReg);
6671fd87a68SDimitry Andric   target->relocate(buf + adrRel.offset, adrRel, val);
6681fd87a68SDimitry Andric   return true;
6691fd87a68SDimitry Andric }
6701fd87a68SDimitry Andric 
67104eeddc0SDimitry Andric bool AArch64Relaxer::tryRelaxAdrpLdr(const Relocation &adrpRel,
67204eeddc0SDimitry Andric                                      const Relocation &ldrRel, uint64_t secAddr,
67304eeddc0SDimitry Andric                                      uint8_t *buf) const {
67404eeddc0SDimitry Andric   if (!safeToRelaxAdrpLdr)
67504eeddc0SDimitry Andric     return false;
67604eeddc0SDimitry Andric 
67704eeddc0SDimitry Andric   // When the definition of sym is not preemptible then we may
67804eeddc0SDimitry Andric   // be able to relax
67904eeddc0SDimitry Andric   // ADRP xn, :got: sym
68004eeddc0SDimitry Andric   // LDR xn, [ xn :got_lo12: sym]
68104eeddc0SDimitry Andric   // to
68204eeddc0SDimitry Andric   // ADRP xn, sym
68304eeddc0SDimitry Andric   // ADD xn, xn, :lo_12: sym
68404eeddc0SDimitry Andric 
68504eeddc0SDimitry Andric   if (adrpRel.type != R_AARCH64_ADR_GOT_PAGE ||
68604eeddc0SDimitry Andric       ldrRel.type != R_AARCH64_LD64_GOT_LO12_NC)
68704eeddc0SDimitry Andric     return false;
68804eeddc0SDimitry Andric   // Check if the relocations apply to consecutive instructions.
68904eeddc0SDimitry Andric   if (adrpRel.offset + 4 != ldrRel.offset)
69004eeddc0SDimitry Andric     return false;
69104eeddc0SDimitry Andric   // Check if the relocations reference the same symbol and
69204eeddc0SDimitry Andric   // skip undefined, preemptible and STT_GNU_IFUNC symbols.
69304eeddc0SDimitry Andric   if (!adrpRel.sym || adrpRel.sym != ldrRel.sym || !adrpRel.sym->isDefined() ||
69404eeddc0SDimitry Andric       adrpRel.sym->isPreemptible || adrpRel.sym->isGnuIFunc())
69504eeddc0SDimitry Andric     return false;
69604eeddc0SDimitry Andric   // Check if the addends of the both relocations are zero.
69704eeddc0SDimitry Andric   if (adrpRel.addend != 0 || ldrRel.addend != 0)
69804eeddc0SDimitry Andric     return false;
69904eeddc0SDimitry Andric   uint32_t adrpInstr = read32le(buf + adrpRel.offset);
70004eeddc0SDimitry Andric   uint32_t ldrInstr = read32le(buf + ldrRel.offset);
70104eeddc0SDimitry Andric   // Check if the first instruction is ADRP and the second instruction is LDR.
70204eeddc0SDimitry Andric   if ((adrpInstr & 0x9f000000) != 0x90000000 ||
70304eeddc0SDimitry Andric       (ldrInstr & 0x3b000000) != 0x39000000)
70404eeddc0SDimitry Andric     return false;
70504eeddc0SDimitry Andric   // Check the value of the sf bit.
70604eeddc0SDimitry Andric   if (!(ldrInstr >> 31))
70704eeddc0SDimitry Andric     return false;
70804eeddc0SDimitry Andric   uint32_t adrpDestReg = adrpInstr & 0x1f;
70904eeddc0SDimitry Andric   uint32_t ldrDestReg = ldrInstr & 0x1f;
71004eeddc0SDimitry Andric   uint32_t ldrSrcReg = (ldrInstr >> 5) & 0x1f;
71104eeddc0SDimitry Andric   // Check if ADPR and LDR use the same register.
71204eeddc0SDimitry Andric   if (adrpDestReg != ldrDestReg || adrpDestReg != ldrSrcReg)
71304eeddc0SDimitry Andric     return false;
71404eeddc0SDimitry Andric 
71504eeddc0SDimitry Andric   Symbol &sym = *adrpRel.sym;
71681ad6265SDimitry Andric   // GOT references to absolute symbols can't be relaxed to use ADRP/ADD in
71781ad6265SDimitry Andric   // position-independent code because these instructions produce a relative
71881ad6265SDimitry Andric   // address.
71981ad6265SDimitry Andric   if (config->isPic && !cast<Defined>(sym).section)
72081ad6265SDimitry Andric     return false;
72104eeddc0SDimitry Andric   // Check if the address difference is within 4GB range.
72204eeddc0SDimitry Andric   int64_t val =
72304eeddc0SDimitry Andric       getAArch64Page(sym.getVA()) - getAArch64Page(secAddr + adrpRel.offset);
72404eeddc0SDimitry Andric   if (val != llvm::SignExtend64(val, 33))
72504eeddc0SDimitry Andric     return false;
72604eeddc0SDimitry Andric 
72704eeddc0SDimitry Andric   Relocation adrpSymRel = {R_AARCH64_PAGE_PC, R_AARCH64_ADR_PREL_PG_HI21,
72804eeddc0SDimitry Andric                            adrpRel.offset, /*addend=*/0, &sym};
72904eeddc0SDimitry Andric   Relocation addRel = {R_ABS, R_AARCH64_ADD_ABS_LO12_NC, ldrRel.offset,
73004eeddc0SDimitry Andric                        /*addend=*/0, &sym};
73104eeddc0SDimitry Andric 
73204eeddc0SDimitry Andric   // adrp x_<dest_reg>
73304eeddc0SDimitry Andric   write32le(buf + adrpSymRel.offset, 0x90000000 | adrpDestReg);
73404eeddc0SDimitry Andric   // add x_<dest reg>, x_<dest reg>
73504eeddc0SDimitry Andric   write32le(buf + addRel.offset, 0x91000000 | adrpDestReg | (adrpDestReg << 5));
73604eeddc0SDimitry Andric 
73704eeddc0SDimitry Andric   target->relocate(buf + adrpSymRel.offset, adrpSymRel,
73804eeddc0SDimitry Andric                    SignExtend64(getAArch64Page(sym.getVA()) -
73904eeddc0SDimitry Andric                                     getAArch64Page(secAddr + adrpSymRel.offset),
74004eeddc0SDimitry Andric                                 64));
74104eeddc0SDimitry Andric   target->relocate(buf + addRel.offset, addRel, SignExtend64(sym.getVA(), 64));
7421fd87a68SDimitry Andric   tryRelaxAdrpAdd(adrpSymRel, addRel, secAddr, buf);
74304eeddc0SDimitry Andric   return true;
74404eeddc0SDimitry Andric }
74504eeddc0SDimitry Andric 
746*bdd1243dSDimitry Andric void AArch64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
747*bdd1243dSDimitry Andric   uint64_t secAddr = sec.getOutputSection()->addr;
748*bdd1243dSDimitry Andric   if (auto *s = dyn_cast<InputSection>(&sec))
749*bdd1243dSDimitry Andric     secAddr += s->outSecOff;
750*bdd1243dSDimitry Andric   AArch64Relaxer relaxer(sec.relocs());
751*bdd1243dSDimitry Andric   for (size_t i = 0, size = sec.relocs().size(); i != size; ++i) {
752*bdd1243dSDimitry Andric     const Relocation &rel = sec.relocs()[i];
753*bdd1243dSDimitry Andric     uint8_t *loc = buf + rel.offset;
754*bdd1243dSDimitry Andric     const uint64_t val =
755*bdd1243dSDimitry Andric         sec.getRelocTargetVA(sec.file, rel.type, rel.addend,
756*bdd1243dSDimitry Andric                              secAddr + rel.offset, *rel.sym, rel.expr);
757*bdd1243dSDimitry Andric     switch (rel.expr) {
758*bdd1243dSDimitry Andric     case R_AARCH64_GOT_PAGE_PC:
759*bdd1243dSDimitry Andric       if (i + 1 < size &&
760*bdd1243dSDimitry Andric           relaxer.tryRelaxAdrpLdr(rel, sec.relocs()[i + 1], secAddr, buf)) {
761*bdd1243dSDimitry Andric         ++i;
762*bdd1243dSDimitry Andric         continue;
763*bdd1243dSDimitry Andric       }
764*bdd1243dSDimitry Andric       break;
765*bdd1243dSDimitry Andric     case R_AARCH64_PAGE_PC:
766*bdd1243dSDimitry Andric       if (i + 1 < size &&
767*bdd1243dSDimitry Andric           relaxer.tryRelaxAdrpAdd(rel, sec.relocs()[i + 1], secAddr, buf)) {
768*bdd1243dSDimitry Andric         ++i;
769*bdd1243dSDimitry Andric         continue;
770*bdd1243dSDimitry Andric       }
771*bdd1243dSDimitry Andric       break;
772*bdd1243dSDimitry Andric     case R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC:
773*bdd1243dSDimitry Andric     case R_RELAX_TLS_GD_TO_IE_ABS:
774*bdd1243dSDimitry Andric       relaxTlsGdToIe(loc, rel, val);
775*bdd1243dSDimitry Andric       continue;
776*bdd1243dSDimitry Andric     case R_RELAX_TLS_GD_TO_LE:
777*bdd1243dSDimitry Andric       relaxTlsGdToLe(loc, rel, val);
778*bdd1243dSDimitry Andric       continue;
779*bdd1243dSDimitry Andric     case R_RELAX_TLS_IE_TO_LE:
780*bdd1243dSDimitry Andric       relaxTlsIeToLe(loc, rel, val);
781*bdd1243dSDimitry Andric       continue;
782*bdd1243dSDimitry Andric     default:
783*bdd1243dSDimitry Andric       break;
784*bdd1243dSDimitry Andric     }
785*bdd1243dSDimitry Andric     relocate(loc, rel, val);
786*bdd1243dSDimitry Andric   }
787*bdd1243dSDimitry Andric }
788*bdd1243dSDimitry Andric 
7890b57cec5SDimitry Andric // AArch64 may use security features in variant PLT sequences. These are:
7900b57cec5SDimitry Andric // Pointer Authentication (PAC), introduced in armv8.3-a and Branch Target
7910b57cec5SDimitry Andric // Indicator (BTI) introduced in armv8.5-a. The additional instructions used
7920b57cec5SDimitry Andric // in the variant Plt sequences are encoded in the Hint space so they can be
7930b57cec5SDimitry Andric // deployed on older architectures, which treat the instructions as a nop.
7940b57cec5SDimitry Andric // PAC and BTI can be combined leading to the following combinations:
7950b57cec5SDimitry Andric // writePltHeader
7960b57cec5SDimitry Andric // writePltHeaderBti (no PAC Header needed)
7970b57cec5SDimitry Andric // writePlt
7980b57cec5SDimitry Andric // writePltBti (BTI only)
7990b57cec5SDimitry Andric // writePltPac (PAC only)
8000b57cec5SDimitry Andric // writePltBtiPac (BTI and PAC)
8010b57cec5SDimitry Andric //
8020b57cec5SDimitry Andric // When PAC is enabled the dynamic loader encrypts the address that it places
8030b57cec5SDimitry Andric // in the .got.plt using the pacia1716 instruction which encrypts the value in
8040b57cec5SDimitry Andric // x17 using the modifier in x16. The static linker places autia1716 before the
8050b57cec5SDimitry Andric // indirect branch to x17 to authenticate the address in x17 with the modifier
8060b57cec5SDimitry Andric // in x16. This makes it more difficult for an attacker to modify the value in
8070b57cec5SDimitry Andric // the .got.plt.
8080b57cec5SDimitry Andric //
8090b57cec5SDimitry Andric // When BTI is enabled all indirect branches must land on a bti instruction.
8100b57cec5SDimitry Andric // The static linker must place a bti instruction at the start of any PLT entry
8110b57cec5SDimitry Andric // that may be the target of an indirect branch. As the PLT entries call the
8120b57cec5SDimitry Andric // lazy resolver indirectly this must have a bti instruction at start. In
8130b57cec5SDimitry Andric // general a bti instruction is not needed for a PLT entry as indirect calls
8140b57cec5SDimitry Andric // are resolved to the function address and not the PLT entry for the function.
8150b57cec5SDimitry Andric // There are a small number of cases where the PLT address can escape, such as
8160b57cec5SDimitry Andric // taking the address of a function or ifunc via a non got-generating
8170b57cec5SDimitry Andric // relocation, and a shared library refers to that symbol.
8180b57cec5SDimitry Andric //
8190b57cec5SDimitry Andric // We use the bti c variant of the instruction which permits indirect branches
8200b57cec5SDimitry Andric // (br) via x16/x17 and indirect function calls (blr) via any register. The ABI
8210b57cec5SDimitry Andric // guarantees that all indirect branches from code requiring BTI protection
8220b57cec5SDimitry Andric // will go via x16/x17
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric namespace {
8250b57cec5SDimitry Andric class AArch64BtiPac final : public AArch64 {
8260b57cec5SDimitry Andric public:
8270b57cec5SDimitry Andric   AArch64BtiPac();
8280b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
829480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
830480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
8310b57cec5SDimitry Andric 
8320b57cec5SDimitry Andric private:
833349cc55cSDimitry Andric   bool btiHeader; // bti instruction needed in PLT Header and Entry
8340b57cec5SDimitry Andric   bool pacEntry;  // autia1716 instruction needed in PLT Entry
8350b57cec5SDimitry Andric };
8360b57cec5SDimitry Andric } // namespace
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric AArch64BtiPac::AArch64BtiPac() {
8390b57cec5SDimitry Andric   btiHeader = (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI);
8400b57cec5SDimitry Andric   // A BTI (Branch Target Indicator) Plt Entry is only required if the
8410b57cec5SDimitry Andric   // address of the PLT entry can be taken by the program, which permits an
8420b57cec5SDimitry Andric   // indirect jump to the PLT entry. This can happen when the address
8430b57cec5SDimitry Andric   // of the PLT entry for a function is canonicalised due to the address of
844349cc55cSDimitry Andric   // the function in an executable being taken by a shared library, or
845349cc55cSDimitry Andric   // non-preemptible ifunc referenced by non-GOT-generating, non-PLT-generating
846349cc55cSDimitry Andric   // relocations.
8475ffd83dbSDimitry Andric   // The PAC PLT entries require dynamic loader support and this isn't known
8485ffd83dbSDimitry Andric   // from properties in the objects, so we use the command line flag.
8495ffd83dbSDimitry Andric   pacEntry = config->zPacPlt;
8500b57cec5SDimitry Andric 
851349cc55cSDimitry Andric   if (btiHeader || pacEntry) {
8520b57cec5SDimitry Andric     pltEntrySize = 24;
853480093f4SDimitry Andric     ipltEntrySize = 24;
854480093f4SDimitry Andric   }
8550b57cec5SDimitry Andric }
8560b57cec5SDimitry Andric 
8570b57cec5SDimitry Andric void AArch64BtiPac::writePltHeader(uint8_t *buf) const {
8580b57cec5SDimitry Andric   const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c
8590b57cec5SDimitry Andric   const uint8_t pltData[] = {
8600b57cec5SDimitry Andric       0xf0, 0x7b, 0xbf, 0xa9, // stp    x16, x30, [sp,#-16]!
861*bdd1243dSDimitry Andric       0x10, 0x00, 0x00, 0x90, // adrp   x16, Page(&(.got.plt[2]))
862*bdd1243dSDimitry Andric       0x11, 0x02, 0x40, 0xf9, // ldr    x17, [x16, Offset(&(.got.plt[2]))]
863*bdd1243dSDimitry Andric       0x10, 0x02, 0x00, 0x91, // add    x16, x16, Offset(&(.got.plt[2]))
8640b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6, // br     x17
8650b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5, // nop
8660b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5  // nop
8670b57cec5SDimitry Andric   };
8680b57cec5SDimitry Andric   const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop
8690b57cec5SDimitry Andric 
8700b57cec5SDimitry Andric   uint64_t got = in.gotPlt->getVA();
8710b57cec5SDimitry Andric   uint64_t plt = in.plt->getVA();
8720b57cec5SDimitry Andric 
8730b57cec5SDimitry Andric   if (btiHeader) {
8740b57cec5SDimitry Andric     // PltHeader is called indirectly by plt[N]. Prefix pltData with a BTI C
8750b57cec5SDimitry Andric     // instruction.
8760b57cec5SDimitry Andric     memcpy(buf, btiData, sizeof(btiData));
8770b57cec5SDimitry Andric     buf += sizeof(btiData);
8780b57cec5SDimitry Andric     plt += sizeof(btiData);
8790b57cec5SDimitry Andric   }
8800b57cec5SDimitry Andric   memcpy(buf, pltData, sizeof(pltData));
8810b57cec5SDimitry Andric 
8825ffd83dbSDimitry Andric   relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
8830b57cec5SDimitry Andric                 getAArch64Page(got + 16) - getAArch64Page(plt + 8));
8845ffd83dbSDimitry Andric   relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16);
8855ffd83dbSDimitry Andric   relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16);
8860b57cec5SDimitry Andric   if (!btiHeader)
8870b57cec5SDimitry Andric     // We didn't add the BTI c instruction so round out size with NOP.
8880b57cec5SDimitry Andric     memcpy(buf + sizeof(pltData), nopData, sizeof(nopData));
8890b57cec5SDimitry Andric }
8900b57cec5SDimitry Andric 
891480093f4SDimitry Andric void AArch64BtiPac::writePlt(uint8_t *buf, const Symbol &sym,
892480093f4SDimitry Andric                              uint64_t pltEntryAddr) const {
8930b57cec5SDimitry Andric   // The PLT entry is of the form:
8940b57cec5SDimitry Andric   // [btiData] addrInst (pacBr | stdBr) [nopData]
8950b57cec5SDimitry Andric   const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c
8960b57cec5SDimitry Andric   const uint8_t addrInst[] = {
897*bdd1243dSDimitry Andric       0x10, 0x00, 0x00, 0x90,  // adrp x16, Page(&(.got.plt[n]))
898*bdd1243dSDimitry Andric       0x11, 0x02, 0x40, 0xf9,  // ldr  x17, [x16, Offset(&(.got.plt[n]))]
899*bdd1243dSDimitry Andric       0x10, 0x02, 0x00, 0x91   // add  x16, x16, Offset(&(.got.plt[n]))
9000b57cec5SDimitry Andric   };
9010b57cec5SDimitry Andric   const uint8_t pacBr[] = {
9020b57cec5SDimitry Andric       0x9f, 0x21, 0x03, 0xd5,  // autia1716
9030b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6   // br   x17
9040b57cec5SDimitry Andric   };
9050b57cec5SDimitry Andric   const uint8_t stdBr[] = {
9060b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6,  // br   x17
9070b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5   // nop
9080b57cec5SDimitry Andric   };
9090b57cec5SDimitry Andric   const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop
9100b57cec5SDimitry Andric 
911*bdd1243dSDimitry Andric   // NEEDS_COPY indicates a non-ifunc canonical PLT entry whose address may
912349cc55cSDimitry Andric   // escape to shared objects. isInIplt indicates a non-preemptible ifunc. Its
913349cc55cSDimitry Andric   // address may escape if referenced by a direct relocation. The condition is
914349cc55cSDimitry Andric   // conservative.
915*bdd1243dSDimitry Andric   bool hasBti = btiHeader && (sym.hasFlag(NEEDS_COPY) || sym.isInIplt);
916349cc55cSDimitry Andric   if (hasBti) {
9170b57cec5SDimitry Andric     memcpy(buf, btiData, sizeof(btiData));
9180b57cec5SDimitry Andric     buf += sizeof(btiData);
9190b57cec5SDimitry Andric     pltEntryAddr += sizeof(btiData);
9200b57cec5SDimitry Andric   }
9210b57cec5SDimitry Andric 
922480093f4SDimitry Andric   uint64_t gotPltEntryAddr = sym.getGotPltVA();
9230b57cec5SDimitry Andric   memcpy(buf, addrInst, sizeof(addrInst));
9245ffd83dbSDimitry Andric   relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
9255ffd83dbSDimitry Andric                 getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr));
9265ffd83dbSDimitry Andric   relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr);
9275ffd83dbSDimitry Andric   relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr);
9280b57cec5SDimitry Andric 
9290b57cec5SDimitry Andric   if (pacEntry)
9300b57cec5SDimitry Andric     memcpy(buf + sizeof(addrInst), pacBr, sizeof(pacBr));
9310b57cec5SDimitry Andric   else
9320b57cec5SDimitry Andric     memcpy(buf + sizeof(addrInst), stdBr, sizeof(stdBr));
933349cc55cSDimitry Andric   if (!hasBti)
9340b57cec5SDimitry Andric     // We didn't add the BTI c instruction so round out size with NOP.
9350b57cec5SDimitry Andric     memcpy(buf + sizeof(addrInst) + sizeof(stdBr), nopData, sizeof(nopData));
9360b57cec5SDimitry Andric }
9370b57cec5SDimitry Andric 
9380b57cec5SDimitry Andric static TargetInfo *getTargetInfo() {
93961cfbce3SDimitry Andric   if ((config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) ||
94061cfbce3SDimitry Andric       config->zPacPlt) {
9410b57cec5SDimitry Andric     static AArch64BtiPac t;
9420b57cec5SDimitry Andric     return &t;
9430b57cec5SDimitry Andric   }
9440b57cec5SDimitry Andric   static AArch64 t;
9450b57cec5SDimitry Andric   return &t;
9460b57cec5SDimitry Andric }
9470b57cec5SDimitry Andric 
9485ffd83dbSDimitry Andric TargetInfo *elf::getAArch64TargetInfo() { return getTargetInfo(); }
949