xref: /freebsd/contrib/llvm-project/lld/ELF/Arch/AArch64.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
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 
90b57cec5SDimitry Andric #include "Symbols.h"
100b57cec5SDimitry Andric #include "SyntheticSections.h"
110b57cec5SDimitry Andric #include "Target.h"
120b57cec5SDimitry Andric #include "Thunks.h"
130b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h"
140b57cec5SDimitry Andric #include "llvm/Object/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;
390b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
40480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
41480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
420b57cec5SDimitry Andric   bool needsThunk(RelExpr expr, RelType type, const InputFile *file,
43480093f4SDimitry Andric                   uint64_t branchAddr, const Symbol &s,
44480093f4SDimitry Andric                   int64_t a) const override;
450b57cec5SDimitry Andric   uint32_t getThunkSectionSpacing() const override;
460b57cec5SDimitry Andric   bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
470b57cec5SDimitry Andric   bool usesOnlyLowPageBits(RelType type) const override;
485ffd83dbSDimitry Andric   void relocate(uint8_t *loc, const Relocation &rel,
495ffd83dbSDimitry Andric                 uint64_t val) const override;
50e8d8bef9SDimitry Andric   RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
515ffd83dbSDimitry Andric   void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
525ffd83dbSDimitry Andric                       uint64_t val) const override;
535ffd83dbSDimitry Andric   void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
545ffd83dbSDimitry Andric                       uint64_t val) const override;
555ffd83dbSDimitry Andric   void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
565ffd83dbSDimitry Andric                       uint64_t val) const override;
570b57cec5SDimitry Andric };
580b57cec5SDimitry Andric } // namespace
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric AArch64::AArch64() {
610b57cec5SDimitry Andric   copyRel = R_AARCH64_COPY;
620b57cec5SDimitry Andric   relativeRel = R_AARCH64_RELATIVE;
630b57cec5SDimitry Andric   iRelativeRel = R_AARCH64_IRELATIVE;
640b57cec5SDimitry Andric   gotRel = R_AARCH64_GLOB_DAT;
650b57cec5SDimitry Andric   pltRel = R_AARCH64_JUMP_SLOT;
660b57cec5SDimitry Andric   symbolicRel = R_AARCH64_ABS64;
670b57cec5SDimitry Andric   tlsDescRel = R_AARCH64_TLSDESC;
680b57cec5SDimitry Andric   tlsGotRel = R_AARCH64_TLS_TPREL64;
690b57cec5SDimitry Andric   pltHeaderSize = 32;
70480093f4SDimitry Andric   pltEntrySize = 16;
71480093f4SDimitry Andric   ipltEntrySize = 16;
720b57cec5SDimitry Andric   defaultMaxPageSize = 65536;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   // Align to the 2 MiB page size (known as a superpage or huge page).
750b57cec5SDimitry Andric   // FreeBSD automatically promotes 2 MiB-aligned allocations.
760b57cec5SDimitry Andric   defaultImageBase = 0x200000;
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   needsThunks = true;
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
820b57cec5SDimitry Andric                             const uint8_t *loc) const {
830b57cec5SDimitry Andric   switch (type) {
8485868e8aSDimitry Andric   case R_AARCH64_ABS16:
8585868e8aSDimitry Andric   case R_AARCH64_ABS32:
8685868e8aSDimitry Andric   case R_AARCH64_ABS64:
8785868e8aSDimitry Andric   case R_AARCH64_ADD_ABS_LO12_NC:
8885868e8aSDimitry Andric   case R_AARCH64_LDST128_ABS_LO12_NC:
8985868e8aSDimitry Andric   case R_AARCH64_LDST16_ABS_LO12_NC:
9085868e8aSDimitry Andric   case R_AARCH64_LDST32_ABS_LO12_NC:
9185868e8aSDimitry Andric   case R_AARCH64_LDST64_ABS_LO12_NC:
9285868e8aSDimitry Andric   case R_AARCH64_LDST8_ABS_LO12_NC:
9385868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G0:
9485868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G1:
9585868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G2:
9685868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G0:
9785868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G0_NC:
9885868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G1:
9985868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G1_NC:
10085868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G2:
10185868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G2_NC:
10285868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G3:
10385868e8aSDimitry Andric     return R_ABS;
1040b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADR_PAGE21:
1050b57cec5SDimitry Andric     return R_AARCH64_TLSDESC_PAGE;
1060b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
1070b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
1080b57cec5SDimitry Andric     return R_TLSDESC;
1090b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_CALL:
1100b57cec5SDimitry Andric     return R_TLSDESC_CALL;
1110b57cec5SDimitry Andric   case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1120b57cec5SDimitry Andric   case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1130b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
1140b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
1150b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
1160b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
1170b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC:
11885868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G0:
11985868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
12085868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G1:
12185868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
12285868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G2:
123e8d8bef9SDimitry Andric     return R_TPREL;
1240b57cec5SDimitry Andric   case R_AARCH64_CALL26:
1250b57cec5SDimitry Andric   case R_AARCH64_CONDBR19:
1260b57cec5SDimitry Andric   case R_AARCH64_JUMP26:
1270b57cec5SDimitry Andric   case R_AARCH64_TSTBR14:
1285ffd83dbSDimitry Andric   case R_AARCH64_PLT32:
1290b57cec5SDimitry Andric     return R_PLT_PC;
1300b57cec5SDimitry Andric   case R_AARCH64_PREL16:
1310b57cec5SDimitry Andric   case R_AARCH64_PREL32:
1320b57cec5SDimitry Andric   case R_AARCH64_PREL64:
1330b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_LO21:
1340b57cec5SDimitry Andric   case R_AARCH64_LD_PREL_LO19:
13585868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G0:
13685868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G0_NC:
13785868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G1:
13885868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G1_NC:
13985868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G2:
14085868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G2_NC:
14185868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G3:
1420b57cec5SDimitry Andric     return R_PC;
1430b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_PG_HI21:
1440b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_PG_HI21_NC:
1450b57cec5SDimitry Andric     return R_AARCH64_PAGE_PC;
1460b57cec5SDimitry Andric   case R_AARCH64_LD64_GOT_LO12_NC:
1470b57cec5SDimitry Andric   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1480b57cec5SDimitry Andric     return R_GOT;
149e8d8bef9SDimitry Andric   case R_AARCH64_LD64_GOTPAGE_LO15:
150e8d8bef9SDimitry Andric     return R_AARCH64_GOT_PAGE;
1510b57cec5SDimitry Andric   case R_AARCH64_ADR_GOT_PAGE:
1520b57cec5SDimitry Andric   case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1530b57cec5SDimitry Andric     return R_AARCH64_GOT_PAGE_PC;
1540b57cec5SDimitry Andric   case R_AARCH64_NONE:
1550b57cec5SDimitry Andric     return R_NONE;
1560b57cec5SDimitry Andric   default:
15785868e8aSDimitry Andric     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
15885868e8aSDimitry Andric           ") against symbol " + toString(s));
15985868e8aSDimitry Andric     return R_NONE;
1600b57cec5SDimitry Andric   }
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric 
163e8d8bef9SDimitry Andric RelExpr AArch64::adjustTlsExpr(RelType type, RelExpr expr) const {
1640b57cec5SDimitry Andric   if (expr == R_RELAX_TLS_GD_TO_IE) {
1650b57cec5SDimitry Andric     if (type == R_AARCH64_TLSDESC_ADR_PAGE21)
1660b57cec5SDimitry Andric       return R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC;
1670b57cec5SDimitry Andric     return R_RELAX_TLS_GD_TO_IE_ABS;
1680b57cec5SDimitry Andric   }
1690b57cec5SDimitry Andric   return expr;
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric bool AArch64::usesOnlyLowPageBits(RelType type) const {
1730b57cec5SDimitry Andric   switch (type) {
1740b57cec5SDimitry Andric   default:
1750b57cec5SDimitry Andric     return false;
1760b57cec5SDimitry Andric   case R_AARCH64_ADD_ABS_LO12_NC:
1770b57cec5SDimitry Andric   case R_AARCH64_LD64_GOT_LO12_NC:
1780b57cec5SDimitry Andric   case R_AARCH64_LDST128_ABS_LO12_NC:
1790b57cec5SDimitry Andric   case R_AARCH64_LDST16_ABS_LO12_NC:
1800b57cec5SDimitry Andric   case R_AARCH64_LDST32_ABS_LO12_NC:
1810b57cec5SDimitry Andric   case R_AARCH64_LDST64_ABS_LO12_NC:
1820b57cec5SDimitry Andric   case R_AARCH64_LDST8_ABS_LO12_NC:
1830b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
1840b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
1850b57cec5SDimitry Andric   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1860b57cec5SDimitry Andric     return true;
1870b57cec5SDimitry Andric   }
1880b57cec5SDimitry Andric }
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric RelType AArch64::getDynRel(RelType type) const {
1910b57cec5SDimitry Andric   if (type == R_AARCH64_ABS64)
1920b57cec5SDimitry Andric     return type;
1930b57cec5SDimitry Andric   return R_AARCH64_NONE;
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric 
196fe6060f1SDimitry Andric int64_t AArch64::getImplicitAddend(const uint8_t *buf, RelType type) const {
197fe6060f1SDimitry Andric   switch (type) {
198fe6060f1SDimitry Andric   case R_AARCH64_TLSDESC:
199fe6060f1SDimitry Andric     return read64(buf + 8);
200298c3e8dSDimitry Andric   case R_AARCH64_NONE:
201298c3e8dSDimitry Andric     return 0;
202298c3e8dSDimitry Andric   case R_AARCH64_PREL32:
203298c3e8dSDimitry Andric     return SignExtend64<32>(read32(buf));
204298c3e8dSDimitry Andric   case R_AARCH64_ABS64:
205298c3e8dSDimitry Andric   case R_AARCH64_PREL64:
206298c3e8dSDimitry Andric     return read64(buf);
207fe6060f1SDimitry Andric   default:
208fe6060f1SDimitry Andric     internalLinkerError(getErrorLocation(buf),
209fe6060f1SDimitry Andric                         "cannot read addend for relocation " + toString(type));
210fe6060f1SDimitry Andric     return 0;
211fe6060f1SDimitry Andric   }
212fe6060f1SDimitry Andric }
213fe6060f1SDimitry Andric 
2140b57cec5SDimitry Andric void AArch64::writeGotPlt(uint8_t *buf, const Symbol &) const {
215fe6060f1SDimitry Andric   write64(buf, in.plt->getVA());
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric void AArch64::writePltHeader(uint8_t *buf) const {
2190b57cec5SDimitry Andric   const uint8_t pltData[] = {
2200b57cec5SDimitry Andric       0xf0, 0x7b, 0xbf, 0xa9, // stp    x16, x30, [sp,#-16]!
2210b57cec5SDimitry Andric       0x10, 0x00, 0x00, 0x90, // adrp   x16, Page(&(.plt.got[2]))
2220b57cec5SDimitry Andric       0x11, 0x02, 0x40, 0xf9, // ldr    x17, [x16, Offset(&(.plt.got[2]))]
2230b57cec5SDimitry Andric       0x10, 0x02, 0x00, 0x91, // add    x16, x16, Offset(&(.plt.got[2]))
2240b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6, // br     x17
2250b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5, // nop
2260b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5, // nop
2270b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5  // nop
2280b57cec5SDimitry Andric   };
2290b57cec5SDimitry Andric   memcpy(buf, pltData, sizeof(pltData));
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric   uint64_t got = in.gotPlt->getVA();
2320b57cec5SDimitry Andric   uint64_t plt = in.plt->getVA();
2335ffd83dbSDimitry Andric   relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
2340b57cec5SDimitry Andric                 getAArch64Page(got + 16) - getAArch64Page(plt + 4));
2355ffd83dbSDimitry Andric   relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16);
2365ffd83dbSDimitry Andric   relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16);
2370b57cec5SDimitry Andric }
2380b57cec5SDimitry Andric 
239480093f4SDimitry Andric void AArch64::writePlt(uint8_t *buf, const Symbol &sym,
240480093f4SDimitry Andric                        uint64_t pltEntryAddr) const {
2410b57cec5SDimitry Andric   const uint8_t inst[] = {
2420b57cec5SDimitry Andric       0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
2430b57cec5SDimitry Andric       0x11, 0x02, 0x40, 0xf9, // ldr  x17, [x16, Offset(&(.plt.got[n]))]
2440b57cec5SDimitry Andric       0x10, 0x02, 0x00, 0x91, // add  x16, x16, Offset(&(.plt.got[n]))
2450b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6  // br   x17
2460b57cec5SDimitry Andric   };
2470b57cec5SDimitry Andric   memcpy(buf, inst, sizeof(inst));
2480b57cec5SDimitry Andric 
249480093f4SDimitry Andric   uint64_t gotPltEntryAddr = sym.getGotPltVA();
2505ffd83dbSDimitry Andric   relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
2510b57cec5SDimitry Andric                 getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr));
2525ffd83dbSDimitry Andric   relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr);
2535ffd83dbSDimitry Andric   relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr);
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric bool AArch64::needsThunk(RelExpr expr, RelType type, const InputFile *file,
257480093f4SDimitry Andric                          uint64_t branchAddr, const Symbol &s,
258480093f4SDimitry Andric                          int64_t a) const {
259480093f4SDimitry Andric   // If s is an undefined weak symbol and does not have a PLT entry then it
260480093f4SDimitry Andric   // will be resolved as a branch to the next instruction.
261480093f4SDimitry Andric   if (s.isUndefWeak() && !s.isInPlt())
262480093f4SDimitry Andric     return false;
2630b57cec5SDimitry Andric   // ELF for the ARM 64-bit architecture, section Call and Jump relocations
2640b57cec5SDimitry Andric   // only permits range extension thunks for R_AARCH64_CALL26 and
2650b57cec5SDimitry Andric   // R_AARCH64_JUMP26 relocation types.
2665ffd83dbSDimitry Andric   if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
2675ffd83dbSDimitry Andric       type != R_AARCH64_PLT32)
2680b57cec5SDimitry Andric     return false;
269480093f4SDimitry Andric   uint64_t dst = expr == R_PLT_PC ? s.getPltVA() : s.getVA(a);
2700b57cec5SDimitry Andric   return !inBranchRange(type, branchAddr, dst);
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric uint32_t AArch64::getThunkSectionSpacing() const {
2740b57cec5SDimitry Andric   // See comment in Arch/ARM.cpp for a more detailed explanation of
2750b57cec5SDimitry Andric   // getThunkSectionSpacing(). For AArch64 the only branches we are permitted to
2760b57cec5SDimitry Andric   // Thunk have a range of +/- 128 MiB
2770b57cec5SDimitry Andric   return (128 * 1024 * 1024) - 0x30000;
2780b57cec5SDimitry Andric }
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric bool AArch64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
2815ffd83dbSDimitry Andric   if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
2825ffd83dbSDimitry Andric       type != R_AARCH64_PLT32)
2830b57cec5SDimitry Andric     return true;
2840b57cec5SDimitry Andric   // The AArch64 call and unconditional branch instructions have a range of
2855ffd83dbSDimitry Andric   // +/- 128 MiB. The PLT32 relocation supports a range up to +/- 2 GiB.
2865ffd83dbSDimitry Andric   uint64_t range =
2875ffd83dbSDimitry Andric       type == R_AARCH64_PLT32 ? (UINT64_C(1) << 31) : (128 * 1024 * 1024);
2880b57cec5SDimitry Andric   if (dst > src) {
2890b57cec5SDimitry Andric     // Immediate of branch is signed.
2900b57cec5SDimitry Andric     range -= 4;
2910b57cec5SDimitry Andric     return dst - src <= range;
2920b57cec5SDimitry Andric   }
2930b57cec5SDimitry Andric   return src - dst <= range;
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric static void write32AArch64Addr(uint8_t *l, uint64_t imm) {
2970b57cec5SDimitry Andric   uint32_t immLo = (imm & 0x3) << 29;
2980b57cec5SDimitry Andric   uint32_t immHi = (imm & 0x1FFFFC) << 3;
2990b57cec5SDimitry Andric   uint64_t mask = (0x3 << 29) | (0x1FFFFC << 3);
3000b57cec5SDimitry Andric   write32le(l, (read32le(l) & ~mask) | immLo | immHi);
3010b57cec5SDimitry Andric }
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric // Return the bits [Start, End] from Val shifted Start bits.
3040b57cec5SDimitry Andric // For instance, getBits(0xF0, 4, 8) returns 0xF.
3050b57cec5SDimitry Andric static uint64_t getBits(uint64_t val, int start, int end) {
3060b57cec5SDimitry Andric   uint64_t mask = ((uint64_t)1 << (end + 1 - start)) - 1;
3070b57cec5SDimitry Andric   return (val >> start) & mask;
3080b57cec5SDimitry Andric }
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric static void or32le(uint8_t *p, int32_t v) { write32le(p, read32le(p) | v); }
3110b57cec5SDimitry Andric 
3120b57cec5SDimitry Andric // Update the immediate field in a AARCH64 ldr, str, and add instruction.
3130b57cec5SDimitry Andric static void or32AArch64Imm(uint8_t *l, uint64_t imm) {
3140b57cec5SDimitry Andric   or32le(l, (imm & 0xFFF) << 10);
3150b57cec5SDimitry Andric }
3160b57cec5SDimitry Andric 
31785868e8aSDimitry Andric // Update the immediate field in an AArch64 movk, movn or movz instruction
31885868e8aSDimitry Andric // for a signed relocation, and update the opcode of a movn or movz instruction
31985868e8aSDimitry Andric // to match the sign of the operand.
32085868e8aSDimitry Andric static void writeSMovWImm(uint8_t *loc, uint32_t imm) {
32185868e8aSDimitry Andric   uint32_t inst = read32le(loc);
32285868e8aSDimitry Andric   // Opcode field is bits 30, 29, with 10 = movz, 00 = movn and 11 = movk.
32385868e8aSDimitry Andric   if (!(inst & (1 << 29))) {
32485868e8aSDimitry Andric     // movn or movz.
32585868e8aSDimitry Andric     if (imm & 0x10000) {
32685868e8aSDimitry Andric       // Change opcode to movn, which takes an inverted operand.
32785868e8aSDimitry Andric       imm ^= 0xFFFF;
32885868e8aSDimitry Andric       inst &= ~(1 << 30);
32985868e8aSDimitry Andric     } else {
33085868e8aSDimitry Andric       // Change opcode to movz.
33185868e8aSDimitry Andric       inst |= 1 << 30;
33285868e8aSDimitry Andric     }
33385868e8aSDimitry Andric   }
33485868e8aSDimitry Andric   write32le(loc, inst | ((imm & 0xFFFF) << 5));
33585868e8aSDimitry Andric }
33685868e8aSDimitry Andric 
3375ffd83dbSDimitry Andric void AArch64::relocate(uint8_t *loc, const Relocation &rel,
3385ffd83dbSDimitry Andric                        uint64_t val) const {
3395ffd83dbSDimitry Andric   switch (rel.type) {
3400b57cec5SDimitry Andric   case R_AARCH64_ABS16:
3410b57cec5SDimitry Andric   case R_AARCH64_PREL16:
3425ffd83dbSDimitry Andric     checkIntUInt(loc, val, 16, rel);
343fe6060f1SDimitry Andric     write16(loc, val);
3440b57cec5SDimitry Andric     break;
3450b57cec5SDimitry Andric   case R_AARCH64_ABS32:
3460b57cec5SDimitry Andric   case R_AARCH64_PREL32:
3475ffd83dbSDimitry Andric     checkIntUInt(loc, val, 32, rel);
348fe6060f1SDimitry Andric     write32(loc, val);
3495ffd83dbSDimitry Andric     break;
3505ffd83dbSDimitry Andric   case R_AARCH64_PLT32:
3515ffd83dbSDimitry Andric     checkInt(loc, val, 32, rel);
352fe6060f1SDimitry Andric     write32(loc, val);
3530b57cec5SDimitry Andric     break;
3540b57cec5SDimitry Andric   case R_AARCH64_ABS64:
3550b57cec5SDimitry Andric   case R_AARCH64_PREL64:
356fe6060f1SDimitry Andric     write64(loc, val);
3570b57cec5SDimitry Andric     break;
3580b57cec5SDimitry Andric   case R_AARCH64_ADD_ABS_LO12_NC:
3590b57cec5SDimitry Andric     or32AArch64Imm(loc, val);
3600b57cec5SDimitry Andric     break;
3610b57cec5SDimitry Andric   case R_AARCH64_ADR_GOT_PAGE:
3620b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_PG_HI21:
3630b57cec5SDimitry Andric   case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3640b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADR_PAGE21:
3655ffd83dbSDimitry Andric     checkInt(loc, val, 33, rel);
3660b57cec5SDimitry Andric     LLVM_FALLTHROUGH;
3670b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_PG_HI21_NC:
3680b57cec5SDimitry Andric     write32AArch64Addr(loc, val >> 12);
3690b57cec5SDimitry Andric     break;
3700b57cec5SDimitry Andric   case R_AARCH64_ADR_PREL_LO21:
3715ffd83dbSDimitry Andric     checkInt(loc, val, 21, rel);
3720b57cec5SDimitry Andric     write32AArch64Addr(loc, val);
3730b57cec5SDimitry Andric     break;
3740b57cec5SDimitry Andric   case R_AARCH64_JUMP26:
3750b57cec5SDimitry Andric     // Normally we would just write the bits of the immediate field, however
3760b57cec5SDimitry Andric     // when patching instructions for the cpu errata fix -fix-cortex-a53-843419
3770b57cec5SDimitry Andric     // we want to replace a non-branch instruction with a branch immediate
3780b57cec5SDimitry Andric     // instruction. By writing all the bits of the instruction including the
3790b57cec5SDimitry Andric     // opcode and the immediate (0 001 | 01 imm26) we can do this
3800b57cec5SDimitry Andric     // transformation by placing a R_AARCH64_JUMP26 relocation at the offset of
3810b57cec5SDimitry Andric     // the instruction we want to patch.
3820b57cec5SDimitry Andric     write32le(loc, 0x14000000);
3830b57cec5SDimitry Andric     LLVM_FALLTHROUGH;
3840b57cec5SDimitry Andric   case R_AARCH64_CALL26:
3855ffd83dbSDimitry Andric     checkInt(loc, val, 28, rel);
3860b57cec5SDimitry Andric     or32le(loc, (val & 0x0FFFFFFC) >> 2);
3870b57cec5SDimitry Andric     break;
3880b57cec5SDimitry Andric   case R_AARCH64_CONDBR19:
3890b57cec5SDimitry Andric   case R_AARCH64_LD_PREL_LO19:
3905ffd83dbSDimitry Andric     checkAlignment(loc, val, 4, rel);
3915ffd83dbSDimitry Andric     checkInt(loc, val, 21, rel);
3920b57cec5SDimitry Andric     or32le(loc, (val & 0x1FFFFC) << 3);
3930b57cec5SDimitry Andric     break;
3940b57cec5SDimitry Andric   case R_AARCH64_LDST8_ABS_LO12_NC:
3950b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
3960b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 0, 11));
3970b57cec5SDimitry Andric     break;
3980b57cec5SDimitry Andric   case R_AARCH64_LDST16_ABS_LO12_NC:
3990b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
4005ffd83dbSDimitry Andric     checkAlignment(loc, val, 2, rel);
4010b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 1, 11));
4020b57cec5SDimitry Andric     break;
4030b57cec5SDimitry Andric   case R_AARCH64_LDST32_ABS_LO12_NC:
4040b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
4055ffd83dbSDimitry Andric     checkAlignment(loc, val, 4, rel);
4060b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 2, 11));
4070b57cec5SDimitry Andric     break;
4080b57cec5SDimitry Andric   case R_AARCH64_LDST64_ABS_LO12_NC:
4090b57cec5SDimitry Andric   case R_AARCH64_LD64_GOT_LO12_NC:
4100b57cec5SDimitry Andric   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4110b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
4120b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
4135ffd83dbSDimitry Andric     checkAlignment(loc, val, 8, rel);
4140b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 3, 11));
4150b57cec5SDimitry Andric     break;
4160b57cec5SDimitry Andric   case R_AARCH64_LDST128_ABS_LO12_NC:
4170b57cec5SDimitry Andric   case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC:
4185ffd83dbSDimitry Andric     checkAlignment(loc, val, 16, rel);
4190b57cec5SDimitry Andric     or32AArch64Imm(loc, getBits(val, 4, 11));
4200b57cec5SDimitry Andric     break;
421e8d8bef9SDimitry Andric   case R_AARCH64_LD64_GOTPAGE_LO15:
422e8d8bef9SDimitry Andric     checkAlignment(loc, val, 8, rel);
423e8d8bef9SDimitry Andric     or32AArch64Imm(loc, getBits(val, 3, 14));
424e8d8bef9SDimitry Andric     break;
42585868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G0:
4265ffd83dbSDimitry Andric     checkUInt(loc, val, 16, rel);
42785868e8aSDimitry Andric     LLVM_FALLTHROUGH;
4280b57cec5SDimitry Andric   case R_AARCH64_MOVW_UABS_G0_NC:
4290b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFF) << 5);
4300b57cec5SDimitry Andric     break;
43185868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G1:
4325ffd83dbSDimitry Andric     checkUInt(loc, val, 32, rel);
43385868e8aSDimitry Andric     LLVM_FALLTHROUGH;
4340b57cec5SDimitry Andric   case R_AARCH64_MOVW_UABS_G1_NC:
4350b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFF0000) >> 11);
4360b57cec5SDimitry Andric     break;
43785868e8aSDimitry Andric   case R_AARCH64_MOVW_UABS_G2:
4385ffd83dbSDimitry Andric     checkUInt(loc, val, 48, rel);
43985868e8aSDimitry Andric     LLVM_FALLTHROUGH;
4400b57cec5SDimitry Andric   case R_AARCH64_MOVW_UABS_G2_NC:
4410b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFF00000000) >> 27);
4420b57cec5SDimitry Andric     break;
4430b57cec5SDimitry Andric   case R_AARCH64_MOVW_UABS_G3:
4440b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFF000000000000) >> 43);
4450b57cec5SDimitry Andric     break;
44685868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G0:
44785868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G0:
44885868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G0:
4495ffd83dbSDimitry Andric     checkInt(loc, val, 17, rel);
45085868e8aSDimitry Andric     LLVM_FALLTHROUGH;
45185868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G0_NC:
45285868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
45385868e8aSDimitry Andric     writeSMovWImm(loc, val);
45485868e8aSDimitry Andric     break;
45585868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G1:
45685868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G1:
45785868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G1:
4585ffd83dbSDimitry Andric     checkInt(loc, val, 33, rel);
45985868e8aSDimitry Andric     LLVM_FALLTHROUGH;
46085868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G1_NC:
46185868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
46285868e8aSDimitry Andric     writeSMovWImm(loc, val >> 16);
46385868e8aSDimitry Andric     break;
46485868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G2:
46585868e8aSDimitry Andric   case R_AARCH64_MOVW_SABS_G2:
46685868e8aSDimitry Andric   case R_AARCH64_TLSLE_MOVW_TPREL_G2:
4675ffd83dbSDimitry Andric     checkInt(loc, val, 49, rel);
46885868e8aSDimitry Andric     LLVM_FALLTHROUGH;
46985868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G2_NC:
47085868e8aSDimitry Andric     writeSMovWImm(loc, val >> 32);
47185868e8aSDimitry Andric     break;
47285868e8aSDimitry Andric   case R_AARCH64_MOVW_PREL_G3:
47385868e8aSDimitry Andric     writeSMovWImm(loc, val >> 48);
47485868e8aSDimitry Andric     break;
4750b57cec5SDimitry Andric   case R_AARCH64_TSTBR14:
4765ffd83dbSDimitry Andric     checkInt(loc, val, 16, rel);
4770b57cec5SDimitry Andric     or32le(loc, (val & 0xFFFC) << 3);
4780b57cec5SDimitry Andric     break;
4790b57cec5SDimitry Andric   case R_AARCH64_TLSLE_ADD_TPREL_HI12:
4805ffd83dbSDimitry Andric     checkUInt(loc, val, 24, rel);
4810b57cec5SDimitry Andric     or32AArch64Imm(loc, val >> 12);
4820b57cec5SDimitry Andric     break;
4830b57cec5SDimitry Andric   case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
4840b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
4850b57cec5SDimitry Andric     or32AArch64Imm(loc, val);
4860b57cec5SDimitry Andric     break;
487fe6060f1SDimitry Andric   case R_AARCH64_TLSDESC:
488fe6060f1SDimitry Andric     // For R_AARCH64_TLSDESC the addend is stored in the second 64-bit word.
489fe6060f1SDimitry Andric     write64(loc + 8, val);
490fe6060f1SDimitry Andric     break;
4910b57cec5SDimitry Andric   default:
49285868e8aSDimitry Andric     llvm_unreachable("unknown relocation");
4930b57cec5SDimitry Andric   }
4940b57cec5SDimitry Andric }
4950b57cec5SDimitry Andric 
4965ffd83dbSDimitry Andric void AArch64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
4975ffd83dbSDimitry Andric                              uint64_t val) const {
4980b57cec5SDimitry Andric   // TLSDESC Global-Dynamic relocation are in the form:
4990b57cec5SDimitry Andric   //   adrp    x0, :tlsdesc:v             [R_AARCH64_TLSDESC_ADR_PAGE21]
5000b57cec5SDimitry Andric   //   ldr     x1, [x0, #:tlsdesc_lo12:v  [R_AARCH64_TLSDESC_LD64_LO12]
5010b57cec5SDimitry Andric   //   add     x0, x0, :tlsdesc_los:v     [R_AARCH64_TLSDESC_ADD_LO12]
5020b57cec5SDimitry Andric   //   .tlsdesccall                       [R_AARCH64_TLSDESC_CALL]
5030b57cec5SDimitry Andric   //   blr     x1
5040b57cec5SDimitry Andric   // And it can optimized to:
5050b57cec5SDimitry Andric   //   movz    x0, #0x0, lsl #16
5060b57cec5SDimitry Andric   //   movk    x0, #0x10
5070b57cec5SDimitry Andric   //   nop
5080b57cec5SDimitry Andric   //   nop
5095ffd83dbSDimitry Andric   checkUInt(loc, val, 32, rel);
5100b57cec5SDimitry Andric 
5115ffd83dbSDimitry Andric   switch (rel.type) {
5120b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
5130b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_CALL:
5140b57cec5SDimitry Andric     write32le(loc, 0xd503201f); // nop
5150b57cec5SDimitry Andric     return;
5160b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADR_PAGE21:
5170b57cec5SDimitry Andric     write32le(loc, 0xd2a00000 | (((val >> 16) & 0xffff) << 5)); // movz
5180b57cec5SDimitry Andric     return;
5190b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
5200b57cec5SDimitry Andric     write32le(loc, 0xf2800000 | ((val & 0xffff) << 5)); // movk
5210b57cec5SDimitry Andric     return;
5220b57cec5SDimitry Andric   default:
5230b57cec5SDimitry Andric     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
5240b57cec5SDimitry Andric   }
5250b57cec5SDimitry Andric }
5260b57cec5SDimitry Andric 
5275ffd83dbSDimitry Andric void AArch64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
5285ffd83dbSDimitry Andric                              uint64_t val) const {
5290b57cec5SDimitry Andric   // TLSDESC Global-Dynamic relocation are in the form:
5300b57cec5SDimitry Andric   //   adrp    x0, :tlsdesc:v             [R_AARCH64_TLSDESC_ADR_PAGE21]
5310b57cec5SDimitry Andric   //   ldr     x1, [x0, #:tlsdesc_lo12:v  [R_AARCH64_TLSDESC_LD64_LO12]
5320b57cec5SDimitry Andric   //   add     x0, x0, :tlsdesc_los:v     [R_AARCH64_TLSDESC_ADD_LO12]
5330b57cec5SDimitry Andric   //   .tlsdesccall                       [R_AARCH64_TLSDESC_CALL]
5340b57cec5SDimitry Andric   //   blr     x1
5350b57cec5SDimitry Andric   // And it can optimized to:
5360b57cec5SDimitry Andric   //   adrp    x0, :gottprel:v
5370b57cec5SDimitry Andric   //   ldr     x0, [x0, :gottprel_lo12:v]
5380b57cec5SDimitry Andric   //   nop
5390b57cec5SDimitry Andric   //   nop
5400b57cec5SDimitry Andric 
5415ffd83dbSDimitry Andric   switch (rel.type) {
5420b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADD_LO12:
5430b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_CALL:
5440b57cec5SDimitry Andric     write32le(loc, 0xd503201f); // nop
5450b57cec5SDimitry Andric     break;
5460b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_ADR_PAGE21:
5470b57cec5SDimitry Andric     write32le(loc, 0x90000000); // adrp
5485ffd83dbSDimitry Andric     relocateNoSym(loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, val);
5490b57cec5SDimitry Andric     break;
5500b57cec5SDimitry Andric   case R_AARCH64_TLSDESC_LD64_LO12:
5510b57cec5SDimitry Andric     write32le(loc, 0xf9400000); // ldr
5525ffd83dbSDimitry Andric     relocateNoSym(loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, val);
5530b57cec5SDimitry Andric     break;
5540b57cec5SDimitry Andric   default:
5550b57cec5SDimitry Andric     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
5560b57cec5SDimitry Andric   }
5570b57cec5SDimitry Andric }
5580b57cec5SDimitry Andric 
5595ffd83dbSDimitry Andric void AArch64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
5605ffd83dbSDimitry Andric                              uint64_t val) const {
5615ffd83dbSDimitry Andric   checkUInt(loc, val, 32, rel);
5620b57cec5SDimitry Andric 
5635ffd83dbSDimitry Andric   if (rel.type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
5640b57cec5SDimitry Andric     // Generate MOVZ.
5650b57cec5SDimitry Andric     uint32_t regNo = read32le(loc) & 0x1f;
5660b57cec5SDimitry Andric     write32le(loc, (0xd2a00000 | regNo) | (((val >> 16) & 0xffff) << 5));
5670b57cec5SDimitry Andric     return;
5680b57cec5SDimitry Andric   }
5695ffd83dbSDimitry Andric   if (rel.type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
5700b57cec5SDimitry Andric     // Generate MOVK.
5710b57cec5SDimitry Andric     uint32_t regNo = read32le(loc) & 0x1f;
5720b57cec5SDimitry Andric     write32le(loc, (0xf2800000 | regNo) | ((val & 0xffff) << 5));
5730b57cec5SDimitry Andric     return;
5740b57cec5SDimitry Andric   }
5750b57cec5SDimitry Andric   llvm_unreachable("invalid relocation for TLS IE to LE relaxation");
5760b57cec5SDimitry Andric }
5770b57cec5SDimitry Andric 
578*04eeddc0SDimitry Andric AArch64Relaxer::AArch64Relaxer(ArrayRef<Relocation> relocs) {
579*04eeddc0SDimitry Andric   if (!config->relax || config->emachine != EM_AARCH64) {
580*04eeddc0SDimitry Andric     safeToRelaxAdrpLdr = false;
581*04eeddc0SDimitry Andric     return;
582*04eeddc0SDimitry Andric   }
583*04eeddc0SDimitry Andric   // Check if R_AARCH64_ADR_GOT_PAGE and R_AARCH64_LD64_GOT_LO12_NC
584*04eeddc0SDimitry Andric   // always appear in pairs.
585*04eeddc0SDimitry Andric   size_t i = 0;
586*04eeddc0SDimitry Andric   const size_t size = relocs.size();
587*04eeddc0SDimitry Andric   for (; i != size; ++i) {
588*04eeddc0SDimitry Andric     if (relocs[i].type == R_AARCH64_ADR_GOT_PAGE) {
589*04eeddc0SDimitry Andric       if (i + 1 < size && relocs[i + 1].type == R_AARCH64_LD64_GOT_LO12_NC) {
590*04eeddc0SDimitry Andric         ++i;
591*04eeddc0SDimitry Andric         continue;
592*04eeddc0SDimitry Andric       }
593*04eeddc0SDimitry Andric       break;
594*04eeddc0SDimitry Andric     } else if (relocs[i].type == R_AARCH64_LD64_GOT_LO12_NC) {
595*04eeddc0SDimitry Andric       break;
596*04eeddc0SDimitry Andric     }
597*04eeddc0SDimitry Andric   }
598*04eeddc0SDimitry Andric   safeToRelaxAdrpLdr = i == size;
599*04eeddc0SDimitry Andric }
600*04eeddc0SDimitry Andric 
601*04eeddc0SDimitry Andric bool AArch64Relaxer::tryRelaxAdrpLdr(const Relocation &adrpRel,
602*04eeddc0SDimitry Andric                                      const Relocation &ldrRel, uint64_t secAddr,
603*04eeddc0SDimitry Andric                                      uint8_t *buf) const {
604*04eeddc0SDimitry Andric   if (!safeToRelaxAdrpLdr)
605*04eeddc0SDimitry Andric     return false;
606*04eeddc0SDimitry Andric 
607*04eeddc0SDimitry Andric   // When the definition of sym is not preemptible then we may
608*04eeddc0SDimitry Andric   // be able to relax
609*04eeddc0SDimitry Andric   // ADRP xn, :got: sym
610*04eeddc0SDimitry Andric   // LDR xn, [ xn :got_lo12: sym]
611*04eeddc0SDimitry Andric   // to
612*04eeddc0SDimitry Andric   // ADRP xn, sym
613*04eeddc0SDimitry Andric   // ADD xn, xn, :lo_12: sym
614*04eeddc0SDimitry Andric 
615*04eeddc0SDimitry Andric   if (adrpRel.type != R_AARCH64_ADR_GOT_PAGE ||
616*04eeddc0SDimitry Andric       ldrRel.type != R_AARCH64_LD64_GOT_LO12_NC)
617*04eeddc0SDimitry Andric     return false;
618*04eeddc0SDimitry Andric   // Check if the relocations apply to consecutive instructions.
619*04eeddc0SDimitry Andric   if (adrpRel.offset + 4 != ldrRel.offset)
620*04eeddc0SDimitry Andric     return false;
621*04eeddc0SDimitry Andric   // Check if the relocations reference the same symbol and
622*04eeddc0SDimitry Andric   // skip undefined, preemptible and STT_GNU_IFUNC symbols.
623*04eeddc0SDimitry Andric   if (!adrpRel.sym || adrpRel.sym != ldrRel.sym || !adrpRel.sym->isDefined() ||
624*04eeddc0SDimitry Andric       adrpRel.sym->isPreemptible || adrpRel.sym->isGnuIFunc())
625*04eeddc0SDimitry Andric     return false;
626*04eeddc0SDimitry Andric   // Check if the addends of the both relocations are zero.
627*04eeddc0SDimitry Andric   if (adrpRel.addend != 0 || ldrRel.addend != 0)
628*04eeddc0SDimitry Andric     return false;
629*04eeddc0SDimitry Andric   uint32_t adrpInstr = read32le(buf + adrpRel.offset);
630*04eeddc0SDimitry Andric   uint32_t ldrInstr = read32le(buf + ldrRel.offset);
631*04eeddc0SDimitry Andric   // Check if the first instruction is ADRP and the second instruction is LDR.
632*04eeddc0SDimitry Andric   if ((adrpInstr & 0x9f000000) != 0x90000000 ||
633*04eeddc0SDimitry Andric       (ldrInstr & 0x3b000000) != 0x39000000)
634*04eeddc0SDimitry Andric     return false;
635*04eeddc0SDimitry Andric   // Check the value of the sf bit.
636*04eeddc0SDimitry Andric   if (!(ldrInstr >> 31))
637*04eeddc0SDimitry Andric     return false;
638*04eeddc0SDimitry Andric   uint32_t adrpDestReg = adrpInstr & 0x1f;
639*04eeddc0SDimitry Andric   uint32_t ldrDestReg = ldrInstr & 0x1f;
640*04eeddc0SDimitry Andric   uint32_t ldrSrcReg = (ldrInstr >> 5) & 0x1f;
641*04eeddc0SDimitry Andric   // Check if ADPR and LDR use the same register.
642*04eeddc0SDimitry Andric   if (adrpDestReg != ldrDestReg || adrpDestReg != ldrSrcReg)
643*04eeddc0SDimitry Andric     return false;
644*04eeddc0SDimitry Andric 
645*04eeddc0SDimitry Andric   Symbol &sym = *adrpRel.sym;
646*04eeddc0SDimitry Andric   // Check if the address difference is within 4GB range.
647*04eeddc0SDimitry Andric   int64_t val =
648*04eeddc0SDimitry Andric       getAArch64Page(sym.getVA()) - getAArch64Page(secAddr + adrpRel.offset);
649*04eeddc0SDimitry Andric   if (val != llvm::SignExtend64(val, 33))
650*04eeddc0SDimitry Andric     return false;
651*04eeddc0SDimitry Andric 
652*04eeddc0SDimitry Andric   Relocation adrpSymRel = {R_AARCH64_PAGE_PC, R_AARCH64_ADR_PREL_PG_HI21,
653*04eeddc0SDimitry Andric                            adrpRel.offset, /*addend=*/0, &sym};
654*04eeddc0SDimitry Andric   Relocation addRel = {R_ABS, R_AARCH64_ADD_ABS_LO12_NC, ldrRel.offset,
655*04eeddc0SDimitry Andric                        /*addend=*/0, &sym};
656*04eeddc0SDimitry Andric 
657*04eeddc0SDimitry Andric   // adrp x_<dest_reg>
658*04eeddc0SDimitry Andric   write32le(buf + adrpSymRel.offset, 0x90000000 | adrpDestReg);
659*04eeddc0SDimitry Andric   // add x_<dest reg>, x_<dest reg>
660*04eeddc0SDimitry Andric   write32le(buf + addRel.offset, 0x91000000 | adrpDestReg | (adrpDestReg << 5));
661*04eeddc0SDimitry Andric 
662*04eeddc0SDimitry Andric   target->relocate(buf + adrpSymRel.offset, adrpSymRel,
663*04eeddc0SDimitry Andric                    SignExtend64(getAArch64Page(sym.getVA()) -
664*04eeddc0SDimitry Andric                                     getAArch64Page(secAddr + adrpSymRel.offset),
665*04eeddc0SDimitry Andric                                 64));
666*04eeddc0SDimitry Andric   target->relocate(buf + addRel.offset, addRel, SignExtend64(sym.getVA(), 64));
667*04eeddc0SDimitry Andric   return true;
668*04eeddc0SDimitry Andric }
669*04eeddc0SDimitry Andric 
6700b57cec5SDimitry Andric // AArch64 may use security features in variant PLT sequences. These are:
6710b57cec5SDimitry Andric // Pointer Authentication (PAC), introduced in armv8.3-a and Branch Target
6720b57cec5SDimitry Andric // Indicator (BTI) introduced in armv8.5-a. The additional instructions used
6730b57cec5SDimitry Andric // in the variant Plt sequences are encoded in the Hint space so they can be
6740b57cec5SDimitry Andric // deployed on older architectures, which treat the instructions as a nop.
6750b57cec5SDimitry Andric // PAC and BTI can be combined leading to the following combinations:
6760b57cec5SDimitry Andric // writePltHeader
6770b57cec5SDimitry Andric // writePltHeaderBti (no PAC Header needed)
6780b57cec5SDimitry Andric // writePlt
6790b57cec5SDimitry Andric // writePltBti (BTI only)
6800b57cec5SDimitry Andric // writePltPac (PAC only)
6810b57cec5SDimitry Andric // writePltBtiPac (BTI and PAC)
6820b57cec5SDimitry Andric //
6830b57cec5SDimitry Andric // When PAC is enabled the dynamic loader encrypts the address that it places
6840b57cec5SDimitry Andric // in the .got.plt using the pacia1716 instruction which encrypts the value in
6850b57cec5SDimitry Andric // x17 using the modifier in x16. The static linker places autia1716 before the
6860b57cec5SDimitry Andric // indirect branch to x17 to authenticate the address in x17 with the modifier
6870b57cec5SDimitry Andric // in x16. This makes it more difficult for an attacker to modify the value in
6880b57cec5SDimitry Andric // the .got.plt.
6890b57cec5SDimitry Andric //
6900b57cec5SDimitry Andric // When BTI is enabled all indirect branches must land on a bti instruction.
6910b57cec5SDimitry Andric // The static linker must place a bti instruction at the start of any PLT entry
6920b57cec5SDimitry Andric // that may be the target of an indirect branch. As the PLT entries call the
6930b57cec5SDimitry Andric // lazy resolver indirectly this must have a bti instruction at start. In
6940b57cec5SDimitry Andric // general a bti instruction is not needed for a PLT entry as indirect calls
6950b57cec5SDimitry Andric // are resolved to the function address and not the PLT entry for the function.
6960b57cec5SDimitry Andric // There are a small number of cases where the PLT address can escape, such as
6970b57cec5SDimitry Andric // taking the address of a function or ifunc via a non got-generating
6980b57cec5SDimitry Andric // relocation, and a shared library refers to that symbol.
6990b57cec5SDimitry Andric //
7000b57cec5SDimitry Andric // We use the bti c variant of the instruction which permits indirect branches
7010b57cec5SDimitry Andric // (br) via x16/x17 and indirect function calls (blr) via any register. The ABI
7020b57cec5SDimitry Andric // guarantees that all indirect branches from code requiring BTI protection
7030b57cec5SDimitry Andric // will go via x16/x17
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric namespace {
7060b57cec5SDimitry Andric class AArch64BtiPac final : public AArch64 {
7070b57cec5SDimitry Andric public:
7080b57cec5SDimitry Andric   AArch64BtiPac();
7090b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
710480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
711480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
7120b57cec5SDimitry Andric 
7130b57cec5SDimitry Andric private:
714349cc55cSDimitry Andric   bool btiHeader; // bti instruction needed in PLT Header and Entry
7150b57cec5SDimitry Andric   bool pacEntry;  // autia1716 instruction needed in PLT Entry
7160b57cec5SDimitry Andric };
7170b57cec5SDimitry Andric } // namespace
7180b57cec5SDimitry Andric 
7190b57cec5SDimitry Andric AArch64BtiPac::AArch64BtiPac() {
7200b57cec5SDimitry Andric   btiHeader = (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI);
7210b57cec5SDimitry Andric   // A BTI (Branch Target Indicator) Plt Entry is only required if the
7220b57cec5SDimitry Andric   // address of the PLT entry can be taken by the program, which permits an
7230b57cec5SDimitry Andric   // indirect jump to the PLT entry. This can happen when the address
7240b57cec5SDimitry Andric   // of the PLT entry for a function is canonicalised due to the address of
725349cc55cSDimitry Andric   // the function in an executable being taken by a shared library, or
726349cc55cSDimitry Andric   // non-preemptible ifunc referenced by non-GOT-generating, non-PLT-generating
727349cc55cSDimitry Andric   // relocations.
7285ffd83dbSDimitry Andric   // The PAC PLT entries require dynamic loader support and this isn't known
7295ffd83dbSDimitry Andric   // from properties in the objects, so we use the command line flag.
7305ffd83dbSDimitry Andric   pacEntry = config->zPacPlt;
7310b57cec5SDimitry Andric 
732349cc55cSDimitry Andric   if (btiHeader || pacEntry) {
7330b57cec5SDimitry Andric     pltEntrySize = 24;
734480093f4SDimitry Andric     ipltEntrySize = 24;
735480093f4SDimitry Andric   }
7360b57cec5SDimitry Andric }
7370b57cec5SDimitry Andric 
7380b57cec5SDimitry Andric void AArch64BtiPac::writePltHeader(uint8_t *buf) const {
7390b57cec5SDimitry Andric   const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c
7400b57cec5SDimitry Andric   const uint8_t pltData[] = {
7410b57cec5SDimitry Andric       0xf0, 0x7b, 0xbf, 0xa9, // stp    x16, x30, [sp,#-16]!
7420b57cec5SDimitry Andric       0x10, 0x00, 0x00, 0x90, // adrp   x16, Page(&(.plt.got[2]))
7430b57cec5SDimitry Andric       0x11, 0x02, 0x40, 0xf9, // ldr    x17, [x16, Offset(&(.plt.got[2]))]
7440b57cec5SDimitry Andric       0x10, 0x02, 0x00, 0x91, // add    x16, x16, Offset(&(.plt.got[2]))
7450b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6, // br     x17
7460b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5, // nop
7470b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5  // nop
7480b57cec5SDimitry Andric   };
7490b57cec5SDimitry Andric   const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop
7500b57cec5SDimitry Andric 
7510b57cec5SDimitry Andric   uint64_t got = in.gotPlt->getVA();
7520b57cec5SDimitry Andric   uint64_t plt = in.plt->getVA();
7530b57cec5SDimitry Andric 
7540b57cec5SDimitry Andric   if (btiHeader) {
7550b57cec5SDimitry Andric     // PltHeader is called indirectly by plt[N]. Prefix pltData with a BTI C
7560b57cec5SDimitry Andric     // instruction.
7570b57cec5SDimitry Andric     memcpy(buf, btiData, sizeof(btiData));
7580b57cec5SDimitry Andric     buf += sizeof(btiData);
7590b57cec5SDimitry Andric     plt += sizeof(btiData);
7600b57cec5SDimitry Andric   }
7610b57cec5SDimitry Andric   memcpy(buf, pltData, sizeof(pltData));
7620b57cec5SDimitry Andric 
7635ffd83dbSDimitry Andric   relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
7640b57cec5SDimitry Andric                 getAArch64Page(got + 16) - getAArch64Page(plt + 8));
7655ffd83dbSDimitry Andric   relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16);
7665ffd83dbSDimitry Andric   relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16);
7670b57cec5SDimitry Andric   if (!btiHeader)
7680b57cec5SDimitry Andric     // We didn't add the BTI c instruction so round out size with NOP.
7690b57cec5SDimitry Andric     memcpy(buf + sizeof(pltData), nopData, sizeof(nopData));
7700b57cec5SDimitry Andric }
7710b57cec5SDimitry Andric 
772480093f4SDimitry Andric void AArch64BtiPac::writePlt(uint8_t *buf, const Symbol &sym,
773480093f4SDimitry Andric                              uint64_t pltEntryAddr) const {
7740b57cec5SDimitry Andric   // The PLT entry is of the form:
7750b57cec5SDimitry Andric   // [btiData] addrInst (pacBr | stdBr) [nopData]
7760b57cec5SDimitry Andric   const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c
7770b57cec5SDimitry Andric   const uint8_t addrInst[] = {
7780b57cec5SDimitry Andric       0x10, 0x00, 0x00, 0x90,  // adrp x16, Page(&(.plt.got[n]))
7790b57cec5SDimitry Andric       0x11, 0x02, 0x40, 0xf9,  // ldr  x17, [x16, Offset(&(.plt.got[n]))]
7800b57cec5SDimitry Andric       0x10, 0x02, 0x00, 0x91   // add  x16, x16, Offset(&(.plt.got[n]))
7810b57cec5SDimitry Andric   };
7820b57cec5SDimitry Andric   const uint8_t pacBr[] = {
7830b57cec5SDimitry Andric       0x9f, 0x21, 0x03, 0xd5,  // autia1716
7840b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6   // br   x17
7850b57cec5SDimitry Andric   };
7860b57cec5SDimitry Andric   const uint8_t stdBr[] = {
7870b57cec5SDimitry Andric       0x20, 0x02, 0x1f, 0xd6,  // br   x17
7880b57cec5SDimitry Andric       0x1f, 0x20, 0x03, 0xd5   // nop
7890b57cec5SDimitry Andric   };
7900b57cec5SDimitry Andric   const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop
7910b57cec5SDimitry Andric 
7920eae32dcSDimitry Andric   // needsCopy indicates a non-ifunc canonical PLT entry whose address may
793349cc55cSDimitry Andric   // escape to shared objects. isInIplt indicates a non-preemptible ifunc. Its
794349cc55cSDimitry Andric   // address may escape if referenced by a direct relocation. The condition is
795349cc55cSDimitry Andric   // conservative.
7960eae32dcSDimitry Andric   bool hasBti = btiHeader && (sym.needsCopy || sym.isInIplt);
797349cc55cSDimitry Andric   if (hasBti) {
7980b57cec5SDimitry Andric     memcpy(buf, btiData, sizeof(btiData));
7990b57cec5SDimitry Andric     buf += sizeof(btiData);
8000b57cec5SDimitry Andric     pltEntryAddr += sizeof(btiData);
8010b57cec5SDimitry Andric   }
8020b57cec5SDimitry Andric 
803480093f4SDimitry Andric   uint64_t gotPltEntryAddr = sym.getGotPltVA();
8040b57cec5SDimitry Andric   memcpy(buf, addrInst, sizeof(addrInst));
8055ffd83dbSDimitry Andric   relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
8065ffd83dbSDimitry Andric                 getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr));
8075ffd83dbSDimitry Andric   relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr);
8085ffd83dbSDimitry Andric   relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr);
8090b57cec5SDimitry Andric 
8100b57cec5SDimitry Andric   if (pacEntry)
8110b57cec5SDimitry Andric     memcpy(buf + sizeof(addrInst), pacBr, sizeof(pacBr));
8120b57cec5SDimitry Andric   else
8130b57cec5SDimitry Andric     memcpy(buf + sizeof(addrInst), stdBr, sizeof(stdBr));
814349cc55cSDimitry Andric   if (!hasBti)
8150b57cec5SDimitry Andric     // We didn't add the BTI c instruction so round out size with NOP.
8160b57cec5SDimitry Andric     memcpy(buf + sizeof(addrInst) + sizeof(stdBr), nopData, sizeof(nopData));
8170b57cec5SDimitry Andric }
8180b57cec5SDimitry Andric 
8190b57cec5SDimitry Andric static TargetInfo *getTargetInfo() {
8200b57cec5SDimitry Andric   if (config->andFeatures & (GNU_PROPERTY_AARCH64_FEATURE_1_BTI |
8210b57cec5SDimitry Andric                              GNU_PROPERTY_AARCH64_FEATURE_1_PAC)) {
8220b57cec5SDimitry Andric     static AArch64BtiPac t;
8230b57cec5SDimitry Andric     return &t;
8240b57cec5SDimitry Andric   }
8250b57cec5SDimitry Andric   static AArch64 t;
8260b57cec5SDimitry Andric   return &t;
8270b57cec5SDimitry Andric }
8280b57cec5SDimitry Andric 
8295ffd83dbSDimitry Andric TargetInfo *elf::getAArch64TargetInfo() { return getTargetInfo(); }
830