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; 20*5ffd83dbSDimitry Andric using namespace lld; 21*5ffd83dbSDimitry 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.) 26*5ffd83dbSDimitry 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; 370b57cec5SDimitry Andric void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 380b57cec5SDimitry Andric void writePltHeader(uint8_t *buf) const override; 39480093f4SDimitry Andric void writePlt(uint8_t *buf, const Symbol &sym, 40480093f4SDimitry Andric uint64_t pltEntryAddr) const override; 410b57cec5SDimitry Andric bool needsThunk(RelExpr expr, RelType type, const InputFile *file, 42480093f4SDimitry Andric uint64_t branchAddr, const Symbol &s, 43480093f4SDimitry Andric int64_t a) const override; 440b57cec5SDimitry Andric uint32_t getThunkSectionSpacing() const override; 450b57cec5SDimitry Andric bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override; 460b57cec5SDimitry Andric bool usesOnlyLowPageBits(RelType type) const override; 47*5ffd83dbSDimitry Andric void relocate(uint8_t *loc, const Relocation &rel, 48*5ffd83dbSDimitry Andric uint64_t val) const override; 490b57cec5SDimitry Andric RelExpr adjustRelaxExpr(RelType type, const uint8_t *data, 500b57cec5SDimitry Andric RelExpr expr) const override; 51*5ffd83dbSDimitry Andric void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 52*5ffd83dbSDimitry Andric uint64_t val) const override; 53*5ffd83dbSDimitry Andric void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 54*5ffd83dbSDimitry Andric uint64_t val) const override; 55*5ffd83dbSDimitry Andric void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 56*5ffd83dbSDimitry 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 noneRel = R_AARCH64_NONE; 660b57cec5SDimitry Andric pltRel = R_AARCH64_JUMP_SLOT; 670b57cec5SDimitry Andric symbolicRel = R_AARCH64_ABS64; 680b57cec5SDimitry Andric tlsDescRel = R_AARCH64_TLSDESC; 690b57cec5SDimitry Andric tlsGotRel = R_AARCH64_TLS_TPREL64; 700b57cec5SDimitry Andric pltHeaderSize = 32; 71480093f4SDimitry Andric pltEntrySize = 16; 72480093f4SDimitry Andric ipltEntrySize = 16; 730b57cec5SDimitry Andric defaultMaxPageSize = 65536; 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric // Align to the 2 MiB page size (known as a superpage or huge page). 760b57cec5SDimitry Andric // FreeBSD automatically promotes 2 MiB-aligned allocations. 770b57cec5SDimitry Andric defaultImageBase = 0x200000; 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric needsThunks = true; 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric RelExpr AArch64::getRelExpr(RelType type, const Symbol &s, 830b57cec5SDimitry Andric const uint8_t *loc) const { 840b57cec5SDimitry Andric switch (type) { 8585868e8aSDimitry Andric case R_AARCH64_ABS16: 8685868e8aSDimitry Andric case R_AARCH64_ABS32: 8785868e8aSDimitry Andric case R_AARCH64_ABS64: 8885868e8aSDimitry Andric case R_AARCH64_ADD_ABS_LO12_NC: 8985868e8aSDimitry Andric case R_AARCH64_LDST128_ABS_LO12_NC: 9085868e8aSDimitry Andric case R_AARCH64_LDST16_ABS_LO12_NC: 9185868e8aSDimitry Andric case R_AARCH64_LDST32_ABS_LO12_NC: 9285868e8aSDimitry Andric case R_AARCH64_LDST64_ABS_LO12_NC: 9385868e8aSDimitry Andric case R_AARCH64_LDST8_ABS_LO12_NC: 9485868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G0: 9585868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G1: 9685868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G2: 9785868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G0: 9885868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G0_NC: 9985868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G1: 10085868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G1_NC: 10185868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G2: 10285868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G2_NC: 10385868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G3: 10485868e8aSDimitry Andric return R_ABS; 1050b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21: 1060b57cec5SDimitry Andric return R_AARCH64_TLSDESC_PAGE; 1070b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 1080b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 1090b57cec5SDimitry Andric return R_TLSDESC; 1100b57cec5SDimitry Andric case R_AARCH64_TLSDESC_CALL: 1110b57cec5SDimitry Andric return R_TLSDESC_CALL; 1120b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_HI12: 1130b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: 1140b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC: 1150b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC: 1160b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC: 1170b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC: 1180b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC: 11985868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0: 12085868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC: 12185868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1: 12285868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC: 12385868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G2: 1240b57cec5SDimitry Andric return R_TLS; 1250b57cec5SDimitry Andric case R_AARCH64_CALL26: 1260b57cec5SDimitry Andric case R_AARCH64_CONDBR19: 1270b57cec5SDimitry Andric case R_AARCH64_JUMP26: 1280b57cec5SDimitry Andric case R_AARCH64_TSTBR14: 129*5ffd83dbSDimitry Andric case R_AARCH64_PLT32: 1300b57cec5SDimitry Andric return R_PLT_PC; 1310b57cec5SDimitry Andric case R_AARCH64_PREL16: 1320b57cec5SDimitry Andric case R_AARCH64_PREL32: 1330b57cec5SDimitry Andric case R_AARCH64_PREL64: 1340b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_LO21: 1350b57cec5SDimitry Andric case R_AARCH64_LD_PREL_LO19: 13685868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0: 13785868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0_NC: 13885868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1: 13985868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1_NC: 14085868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2: 14185868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2_NC: 14285868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G3: 1430b57cec5SDimitry Andric return R_PC; 1440b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21: 1450b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21_NC: 1460b57cec5SDimitry Andric return R_AARCH64_PAGE_PC; 1470b57cec5SDimitry Andric case R_AARCH64_LD64_GOT_LO12_NC: 1480b57cec5SDimitry Andric case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 1490b57cec5SDimitry Andric return R_GOT; 1500b57cec5SDimitry Andric case R_AARCH64_ADR_GOT_PAGE: 1510b57cec5SDimitry Andric case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: 1520b57cec5SDimitry Andric return R_AARCH64_GOT_PAGE_PC; 1530b57cec5SDimitry Andric case R_AARCH64_NONE: 1540b57cec5SDimitry Andric return R_NONE; 1550b57cec5SDimitry Andric default: 15685868e8aSDimitry Andric error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 15785868e8aSDimitry Andric ") against symbol " + toString(s)); 15885868e8aSDimitry Andric return R_NONE; 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric RelExpr AArch64::adjustRelaxExpr(RelType type, const uint8_t *data, 1630b57cec5SDimitry Andric 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 1960b57cec5SDimitry Andric void AArch64::writeGotPlt(uint8_t *buf, const Symbol &) const { 1970b57cec5SDimitry Andric write64le(buf, in.plt->getVA()); 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric void AArch64::writePltHeader(uint8_t *buf) const { 2010b57cec5SDimitry Andric const uint8_t pltData[] = { 2020b57cec5SDimitry Andric 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! 2030b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) 2040b57cec5SDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] 2050b57cec5SDimitry Andric 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) 2060b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6, // br x17 2070b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5, // nop 2080b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5, // nop 2090b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5 // nop 2100b57cec5SDimitry Andric }; 2110b57cec5SDimitry Andric memcpy(buf, pltData, sizeof(pltData)); 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric uint64_t got = in.gotPlt->getVA(); 2140b57cec5SDimitry Andric uint64_t plt = in.plt->getVA(); 215*5ffd83dbSDimitry Andric relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21, 2160b57cec5SDimitry Andric getAArch64Page(got + 16) - getAArch64Page(plt + 4)); 217*5ffd83dbSDimitry Andric relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16); 218*5ffd83dbSDimitry Andric relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16); 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 221480093f4SDimitry Andric void AArch64::writePlt(uint8_t *buf, const Symbol &sym, 222480093f4SDimitry Andric uint64_t pltEntryAddr) const { 2230b57cec5SDimitry Andric const uint8_t inst[] = { 2240b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) 2250b57cec5SDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] 2260b57cec5SDimitry Andric 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n])) 2270b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6 // br x17 2280b57cec5SDimitry Andric }; 2290b57cec5SDimitry Andric memcpy(buf, inst, sizeof(inst)); 2300b57cec5SDimitry Andric 231480093f4SDimitry Andric uint64_t gotPltEntryAddr = sym.getGotPltVA(); 232*5ffd83dbSDimitry Andric relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21, 2330b57cec5SDimitry Andric getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr)); 234*5ffd83dbSDimitry Andric relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr); 235*5ffd83dbSDimitry Andric relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr); 2360b57cec5SDimitry Andric } 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric bool AArch64::needsThunk(RelExpr expr, RelType type, const InputFile *file, 239480093f4SDimitry Andric uint64_t branchAddr, const Symbol &s, 240480093f4SDimitry Andric int64_t a) const { 241480093f4SDimitry Andric // If s is an undefined weak symbol and does not have a PLT entry then it 242480093f4SDimitry Andric // will be resolved as a branch to the next instruction. 243480093f4SDimitry Andric if (s.isUndefWeak() && !s.isInPlt()) 244480093f4SDimitry Andric return false; 2450b57cec5SDimitry Andric // ELF for the ARM 64-bit architecture, section Call and Jump relocations 2460b57cec5SDimitry Andric // only permits range extension thunks for R_AARCH64_CALL26 and 2470b57cec5SDimitry Andric // R_AARCH64_JUMP26 relocation types. 248*5ffd83dbSDimitry Andric if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 && 249*5ffd83dbSDimitry Andric type != R_AARCH64_PLT32) 2500b57cec5SDimitry Andric return false; 251480093f4SDimitry Andric uint64_t dst = expr == R_PLT_PC ? s.getPltVA() : s.getVA(a); 2520b57cec5SDimitry Andric return !inBranchRange(type, branchAddr, dst); 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric uint32_t AArch64::getThunkSectionSpacing() const { 2560b57cec5SDimitry Andric // See comment in Arch/ARM.cpp for a more detailed explanation of 2570b57cec5SDimitry Andric // getThunkSectionSpacing(). For AArch64 the only branches we are permitted to 2580b57cec5SDimitry Andric // Thunk have a range of +/- 128 MiB 2590b57cec5SDimitry Andric return (128 * 1024 * 1024) - 0x30000; 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric bool AArch64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 263*5ffd83dbSDimitry Andric if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 && 264*5ffd83dbSDimitry Andric type != R_AARCH64_PLT32) 2650b57cec5SDimitry Andric return true; 2660b57cec5SDimitry Andric // The AArch64 call and unconditional branch instructions have a range of 267*5ffd83dbSDimitry Andric // +/- 128 MiB. The PLT32 relocation supports a range up to +/- 2 GiB. 268*5ffd83dbSDimitry Andric uint64_t range = 269*5ffd83dbSDimitry Andric type == R_AARCH64_PLT32 ? (UINT64_C(1) << 31) : (128 * 1024 * 1024); 2700b57cec5SDimitry Andric if (dst > src) { 2710b57cec5SDimitry Andric // Immediate of branch is signed. 2720b57cec5SDimitry Andric range -= 4; 2730b57cec5SDimitry Andric return dst - src <= range; 2740b57cec5SDimitry Andric } 2750b57cec5SDimitry Andric return src - dst <= range; 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric static void write32AArch64Addr(uint8_t *l, uint64_t imm) { 2790b57cec5SDimitry Andric uint32_t immLo = (imm & 0x3) << 29; 2800b57cec5SDimitry Andric uint32_t immHi = (imm & 0x1FFFFC) << 3; 2810b57cec5SDimitry Andric uint64_t mask = (0x3 << 29) | (0x1FFFFC << 3); 2820b57cec5SDimitry Andric write32le(l, (read32le(l) & ~mask) | immLo | immHi); 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric // Return the bits [Start, End] from Val shifted Start bits. 2860b57cec5SDimitry Andric // For instance, getBits(0xF0, 4, 8) returns 0xF. 2870b57cec5SDimitry Andric static uint64_t getBits(uint64_t val, int start, int end) { 2880b57cec5SDimitry Andric uint64_t mask = ((uint64_t)1 << (end + 1 - start)) - 1; 2890b57cec5SDimitry Andric return (val >> start) & mask; 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric static void or32le(uint8_t *p, int32_t v) { write32le(p, read32le(p) | v); } 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric // Update the immediate field in a AARCH64 ldr, str, and add instruction. 2950b57cec5SDimitry Andric static void or32AArch64Imm(uint8_t *l, uint64_t imm) { 2960b57cec5SDimitry Andric or32le(l, (imm & 0xFFF) << 10); 2970b57cec5SDimitry Andric } 2980b57cec5SDimitry Andric 29985868e8aSDimitry Andric // Update the immediate field in an AArch64 movk, movn or movz instruction 30085868e8aSDimitry Andric // for a signed relocation, and update the opcode of a movn or movz instruction 30185868e8aSDimitry Andric // to match the sign of the operand. 30285868e8aSDimitry Andric static void writeSMovWImm(uint8_t *loc, uint32_t imm) { 30385868e8aSDimitry Andric uint32_t inst = read32le(loc); 30485868e8aSDimitry Andric // Opcode field is bits 30, 29, with 10 = movz, 00 = movn and 11 = movk. 30585868e8aSDimitry Andric if (!(inst & (1 << 29))) { 30685868e8aSDimitry Andric // movn or movz. 30785868e8aSDimitry Andric if (imm & 0x10000) { 30885868e8aSDimitry Andric // Change opcode to movn, which takes an inverted operand. 30985868e8aSDimitry Andric imm ^= 0xFFFF; 31085868e8aSDimitry Andric inst &= ~(1 << 30); 31185868e8aSDimitry Andric } else { 31285868e8aSDimitry Andric // Change opcode to movz. 31385868e8aSDimitry Andric inst |= 1 << 30; 31485868e8aSDimitry Andric } 31585868e8aSDimitry Andric } 31685868e8aSDimitry Andric write32le(loc, inst | ((imm & 0xFFFF) << 5)); 31785868e8aSDimitry Andric } 31885868e8aSDimitry Andric 319*5ffd83dbSDimitry Andric void AArch64::relocate(uint8_t *loc, const Relocation &rel, 320*5ffd83dbSDimitry Andric uint64_t val) const { 321*5ffd83dbSDimitry Andric switch (rel.type) { 3220b57cec5SDimitry Andric case R_AARCH64_ABS16: 3230b57cec5SDimitry Andric case R_AARCH64_PREL16: 324*5ffd83dbSDimitry Andric checkIntUInt(loc, val, 16, rel); 3250b57cec5SDimitry Andric write16le(loc, val); 3260b57cec5SDimitry Andric break; 3270b57cec5SDimitry Andric case R_AARCH64_ABS32: 3280b57cec5SDimitry Andric case R_AARCH64_PREL32: 329*5ffd83dbSDimitry Andric checkIntUInt(loc, val, 32, rel); 330*5ffd83dbSDimitry Andric write32le(loc, val); 331*5ffd83dbSDimitry Andric break; 332*5ffd83dbSDimitry Andric case R_AARCH64_PLT32: 333*5ffd83dbSDimitry Andric checkInt(loc, val, 32, rel); 3340b57cec5SDimitry Andric write32le(loc, val); 3350b57cec5SDimitry Andric break; 3360b57cec5SDimitry Andric case R_AARCH64_ABS64: 3370b57cec5SDimitry Andric case R_AARCH64_PREL64: 3380b57cec5SDimitry Andric write64le(loc, val); 3390b57cec5SDimitry Andric break; 3400b57cec5SDimitry Andric case R_AARCH64_ADD_ABS_LO12_NC: 3410b57cec5SDimitry Andric or32AArch64Imm(loc, val); 3420b57cec5SDimitry Andric break; 3430b57cec5SDimitry Andric case R_AARCH64_ADR_GOT_PAGE: 3440b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21: 3450b57cec5SDimitry Andric case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: 3460b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21: 347*5ffd83dbSDimitry Andric checkInt(loc, val, 33, rel); 3480b57cec5SDimitry Andric LLVM_FALLTHROUGH; 3490b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21_NC: 3500b57cec5SDimitry Andric write32AArch64Addr(loc, val >> 12); 3510b57cec5SDimitry Andric break; 3520b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_LO21: 353*5ffd83dbSDimitry Andric checkInt(loc, val, 21, rel); 3540b57cec5SDimitry Andric write32AArch64Addr(loc, val); 3550b57cec5SDimitry Andric break; 3560b57cec5SDimitry Andric case R_AARCH64_JUMP26: 3570b57cec5SDimitry Andric // Normally we would just write the bits of the immediate field, however 3580b57cec5SDimitry Andric // when patching instructions for the cpu errata fix -fix-cortex-a53-843419 3590b57cec5SDimitry Andric // we want to replace a non-branch instruction with a branch immediate 3600b57cec5SDimitry Andric // instruction. By writing all the bits of the instruction including the 3610b57cec5SDimitry Andric // opcode and the immediate (0 001 | 01 imm26) we can do this 3620b57cec5SDimitry Andric // transformation by placing a R_AARCH64_JUMP26 relocation at the offset of 3630b57cec5SDimitry Andric // the instruction we want to patch. 3640b57cec5SDimitry Andric write32le(loc, 0x14000000); 3650b57cec5SDimitry Andric LLVM_FALLTHROUGH; 3660b57cec5SDimitry Andric case R_AARCH64_CALL26: 367*5ffd83dbSDimitry Andric checkInt(loc, val, 28, rel); 3680b57cec5SDimitry Andric or32le(loc, (val & 0x0FFFFFFC) >> 2); 3690b57cec5SDimitry Andric break; 3700b57cec5SDimitry Andric case R_AARCH64_CONDBR19: 3710b57cec5SDimitry Andric case R_AARCH64_LD_PREL_LO19: 372*5ffd83dbSDimitry Andric checkAlignment(loc, val, 4, rel); 373*5ffd83dbSDimitry Andric checkInt(loc, val, 21, rel); 3740b57cec5SDimitry Andric or32le(loc, (val & 0x1FFFFC) << 3); 3750b57cec5SDimitry Andric break; 3760b57cec5SDimitry Andric case R_AARCH64_LDST8_ABS_LO12_NC: 3770b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC: 3780b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 0, 11)); 3790b57cec5SDimitry Andric break; 3800b57cec5SDimitry Andric case R_AARCH64_LDST16_ABS_LO12_NC: 3810b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC: 382*5ffd83dbSDimitry Andric checkAlignment(loc, val, 2, rel); 3830b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 1, 11)); 3840b57cec5SDimitry Andric break; 3850b57cec5SDimitry Andric case R_AARCH64_LDST32_ABS_LO12_NC: 3860b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC: 387*5ffd83dbSDimitry Andric checkAlignment(loc, val, 4, rel); 3880b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 2, 11)); 3890b57cec5SDimitry Andric break; 3900b57cec5SDimitry Andric case R_AARCH64_LDST64_ABS_LO12_NC: 3910b57cec5SDimitry Andric case R_AARCH64_LD64_GOT_LO12_NC: 3920b57cec5SDimitry Andric case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 3930b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC: 3940b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 395*5ffd83dbSDimitry Andric checkAlignment(loc, val, 8, rel); 3960b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 3, 11)); 3970b57cec5SDimitry Andric break; 3980b57cec5SDimitry Andric case R_AARCH64_LDST128_ABS_LO12_NC: 3990b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC: 400*5ffd83dbSDimitry Andric checkAlignment(loc, val, 16, rel); 4010b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 4, 11)); 4020b57cec5SDimitry Andric break; 40385868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G0: 404*5ffd83dbSDimitry Andric checkUInt(loc, val, 16, rel); 40585868e8aSDimitry Andric LLVM_FALLTHROUGH; 4060b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G0_NC: 4070b57cec5SDimitry Andric or32le(loc, (val & 0xFFFF) << 5); 4080b57cec5SDimitry Andric break; 40985868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G1: 410*5ffd83dbSDimitry Andric checkUInt(loc, val, 32, rel); 41185868e8aSDimitry Andric LLVM_FALLTHROUGH; 4120b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G1_NC: 4130b57cec5SDimitry Andric or32le(loc, (val & 0xFFFF0000) >> 11); 4140b57cec5SDimitry Andric break; 41585868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G2: 416*5ffd83dbSDimitry Andric checkUInt(loc, val, 48, rel); 41785868e8aSDimitry Andric LLVM_FALLTHROUGH; 4180b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G2_NC: 4190b57cec5SDimitry Andric or32le(loc, (val & 0xFFFF00000000) >> 27); 4200b57cec5SDimitry Andric break; 4210b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G3: 4220b57cec5SDimitry Andric or32le(loc, (val & 0xFFFF000000000000) >> 43); 4230b57cec5SDimitry Andric break; 42485868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0: 42585868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G0: 42685868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0: 427*5ffd83dbSDimitry Andric checkInt(loc, val, 17, rel); 42885868e8aSDimitry Andric LLVM_FALLTHROUGH; 42985868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0_NC: 43085868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC: 43185868e8aSDimitry Andric writeSMovWImm(loc, val); 43285868e8aSDimitry Andric break; 43385868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1: 43485868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G1: 43585868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1: 436*5ffd83dbSDimitry Andric checkInt(loc, val, 33, rel); 43785868e8aSDimitry Andric LLVM_FALLTHROUGH; 43885868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1_NC: 43985868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC: 44085868e8aSDimitry Andric writeSMovWImm(loc, val >> 16); 44185868e8aSDimitry Andric break; 44285868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2: 44385868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G2: 44485868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G2: 445*5ffd83dbSDimitry Andric checkInt(loc, val, 49, rel); 44685868e8aSDimitry Andric LLVM_FALLTHROUGH; 44785868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2_NC: 44885868e8aSDimitry Andric writeSMovWImm(loc, val >> 32); 44985868e8aSDimitry Andric break; 45085868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G3: 45185868e8aSDimitry Andric writeSMovWImm(loc, val >> 48); 45285868e8aSDimitry Andric break; 4530b57cec5SDimitry Andric case R_AARCH64_TSTBR14: 454*5ffd83dbSDimitry Andric checkInt(loc, val, 16, rel); 4550b57cec5SDimitry Andric or32le(loc, (val & 0xFFFC) << 3); 4560b57cec5SDimitry Andric break; 4570b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_HI12: 458*5ffd83dbSDimitry Andric checkUInt(loc, val, 24, rel); 4590b57cec5SDimitry Andric or32AArch64Imm(loc, val >> 12); 4600b57cec5SDimitry Andric break; 4610b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: 4620b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 4630b57cec5SDimitry Andric or32AArch64Imm(loc, val); 4640b57cec5SDimitry Andric break; 4650b57cec5SDimitry Andric default: 46685868e8aSDimitry Andric llvm_unreachable("unknown relocation"); 4670b57cec5SDimitry Andric } 4680b57cec5SDimitry Andric } 4690b57cec5SDimitry Andric 470*5ffd83dbSDimitry Andric void AArch64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 471*5ffd83dbSDimitry Andric uint64_t val) const { 4720b57cec5SDimitry Andric // TLSDESC Global-Dynamic relocation are in the form: 4730b57cec5SDimitry Andric // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21] 4740b57cec5SDimitry Andric // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12] 4750b57cec5SDimitry Andric // add x0, x0, :tlsdesc_los:v [R_AARCH64_TLSDESC_ADD_LO12] 4760b57cec5SDimitry Andric // .tlsdesccall [R_AARCH64_TLSDESC_CALL] 4770b57cec5SDimitry Andric // blr x1 4780b57cec5SDimitry Andric // And it can optimized to: 4790b57cec5SDimitry Andric // movz x0, #0x0, lsl #16 4800b57cec5SDimitry Andric // movk x0, #0x10 4810b57cec5SDimitry Andric // nop 4820b57cec5SDimitry Andric // nop 483*5ffd83dbSDimitry Andric checkUInt(loc, val, 32, rel); 4840b57cec5SDimitry Andric 485*5ffd83dbSDimitry Andric switch (rel.type) { 4860b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 4870b57cec5SDimitry Andric case R_AARCH64_TLSDESC_CALL: 4880b57cec5SDimitry Andric write32le(loc, 0xd503201f); // nop 4890b57cec5SDimitry Andric return; 4900b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21: 4910b57cec5SDimitry Andric write32le(loc, 0xd2a00000 | (((val >> 16) & 0xffff) << 5)); // movz 4920b57cec5SDimitry Andric return; 4930b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 4940b57cec5SDimitry Andric write32le(loc, 0xf2800000 | ((val & 0xffff) << 5)); // movk 4950b57cec5SDimitry Andric return; 4960b57cec5SDimitry Andric default: 4970b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 4980b57cec5SDimitry Andric } 4990b57cec5SDimitry Andric } 5000b57cec5SDimitry Andric 501*5ffd83dbSDimitry Andric void AArch64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 502*5ffd83dbSDimitry Andric uint64_t val) const { 5030b57cec5SDimitry Andric // TLSDESC Global-Dynamic relocation are in the form: 5040b57cec5SDimitry Andric // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21] 5050b57cec5SDimitry Andric // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12] 5060b57cec5SDimitry Andric // add x0, x0, :tlsdesc_los:v [R_AARCH64_TLSDESC_ADD_LO12] 5070b57cec5SDimitry Andric // .tlsdesccall [R_AARCH64_TLSDESC_CALL] 5080b57cec5SDimitry Andric // blr x1 5090b57cec5SDimitry Andric // And it can optimized to: 5100b57cec5SDimitry Andric // adrp x0, :gottprel:v 5110b57cec5SDimitry Andric // ldr x0, [x0, :gottprel_lo12:v] 5120b57cec5SDimitry Andric // nop 5130b57cec5SDimitry Andric // nop 5140b57cec5SDimitry Andric 515*5ffd83dbSDimitry Andric switch (rel.type) { 5160b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 5170b57cec5SDimitry Andric case R_AARCH64_TLSDESC_CALL: 5180b57cec5SDimitry Andric write32le(loc, 0xd503201f); // nop 5190b57cec5SDimitry Andric break; 5200b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21: 5210b57cec5SDimitry Andric write32le(loc, 0x90000000); // adrp 522*5ffd83dbSDimitry Andric relocateNoSym(loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, val); 5230b57cec5SDimitry Andric break; 5240b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 5250b57cec5SDimitry Andric write32le(loc, 0xf9400000); // ldr 526*5ffd83dbSDimitry Andric relocateNoSym(loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, val); 5270b57cec5SDimitry Andric break; 5280b57cec5SDimitry Andric default: 5290b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 5300b57cec5SDimitry Andric } 5310b57cec5SDimitry Andric } 5320b57cec5SDimitry Andric 533*5ffd83dbSDimitry Andric void AArch64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 534*5ffd83dbSDimitry Andric uint64_t val) const { 535*5ffd83dbSDimitry Andric checkUInt(loc, val, 32, rel); 5360b57cec5SDimitry Andric 537*5ffd83dbSDimitry Andric if (rel.type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) { 5380b57cec5SDimitry Andric // Generate MOVZ. 5390b57cec5SDimitry Andric uint32_t regNo = read32le(loc) & 0x1f; 5400b57cec5SDimitry Andric write32le(loc, (0xd2a00000 | regNo) | (((val >> 16) & 0xffff) << 5)); 5410b57cec5SDimitry Andric return; 5420b57cec5SDimitry Andric } 543*5ffd83dbSDimitry Andric if (rel.type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) { 5440b57cec5SDimitry Andric // Generate MOVK. 5450b57cec5SDimitry Andric uint32_t regNo = read32le(loc) & 0x1f; 5460b57cec5SDimitry Andric write32le(loc, (0xf2800000 | regNo) | ((val & 0xffff) << 5)); 5470b57cec5SDimitry Andric return; 5480b57cec5SDimitry Andric } 5490b57cec5SDimitry Andric llvm_unreachable("invalid relocation for TLS IE to LE relaxation"); 5500b57cec5SDimitry Andric } 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric // AArch64 may use security features in variant PLT sequences. These are: 5530b57cec5SDimitry Andric // Pointer Authentication (PAC), introduced in armv8.3-a and Branch Target 5540b57cec5SDimitry Andric // Indicator (BTI) introduced in armv8.5-a. The additional instructions used 5550b57cec5SDimitry Andric // in the variant Plt sequences are encoded in the Hint space so they can be 5560b57cec5SDimitry Andric // deployed on older architectures, which treat the instructions as a nop. 5570b57cec5SDimitry Andric // PAC and BTI can be combined leading to the following combinations: 5580b57cec5SDimitry Andric // writePltHeader 5590b57cec5SDimitry Andric // writePltHeaderBti (no PAC Header needed) 5600b57cec5SDimitry Andric // writePlt 5610b57cec5SDimitry Andric // writePltBti (BTI only) 5620b57cec5SDimitry Andric // writePltPac (PAC only) 5630b57cec5SDimitry Andric // writePltBtiPac (BTI and PAC) 5640b57cec5SDimitry Andric // 5650b57cec5SDimitry Andric // When PAC is enabled the dynamic loader encrypts the address that it places 5660b57cec5SDimitry Andric // in the .got.plt using the pacia1716 instruction which encrypts the value in 5670b57cec5SDimitry Andric // x17 using the modifier in x16. The static linker places autia1716 before the 5680b57cec5SDimitry Andric // indirect branch to x17 to authenticate the address in x17 with the modifier 5690b57cec5SDimitry Andric // in x16. This makes it more difficult for an attacker to modify the value in 5700b57cec5SDimitry Andric // the .got.plt. 5710b57cec5SDimitry Andric // 5720b57cec5SDimitry Andric // When BTI is enabled all indirect branches must land on a bti instruction. 5730b57cec5SDimitry Andric // The static linker must place a bti instruction at the start of any PLT entry 5740b57cec5SDimitry Andric // that may be the target of an indirect branch. As the PLT entries call the 5750b57cec5SDimitry Andric // lazy resolver indirectly this must have a bti instruction at start. In 5760b57cec5SDimitry Andric // general a bti instruction is not needed for a PLT entry as indirect calls 5770b57cec5SDimitry Andric // are resolved to the function address and not the PLT entry for the function. 5780b57cec5SDimitry Andric // There are a small number of cases where the PLT address can escape, such as 5790b57cec5SDimitry Andric // taking the address of a function or ifunc via a non got-generating 5800b57cec5SDimitry Andric // relocation, and a shared library refers to that symbol. 5810b57cec5SDimitry Andric // 5820b57cec5SDimitry Andric // We use the bti c variant of the instruction which permits indirect branches 5830b57cec5SDimitry Andric // (br) via x16/x17 and indirect function calls (blr) via any register. The ABI 5840b57cec5SDimitry Andric // guarantees that all indirect branches from code requiring BTI protection 5850b57cec5SDimitry Andric // will go via x16/x17 5860b57cec5SDimitry Andric 5870b57cec5SDimitry Andric namespace { 5880b57cec5SDimitry Andric class AArch64BtiPac final : public AArch64 { 5890b57cec5SDimitry Andric public: 5900b57cec5SDimitry Andric AArch64BtiPac(); 5910b57cec5SDimitry Andric void writePltHeader(uint8_t *buf) const override; 592480093f4SDimitry Andric void writePlt(uint8_t *buf, const Symbol &sym, 593480093f4SDimitry Andric uint64_t pltEntryAddr) const override; 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric private: 5960b57cec5SDimitry Andric bool btiHeader; // bti instruction needed in PLT Header 5970b57cec5SDimitry Andric bool btiEntry; // bti instruction needed in PLT Entry 5980b57cec5SDimitry Andric bool pacEntry; // autia1716 instruction needed in PLT Entry 5990b57cec5SDimitry Andric }; 6000b57cec5SDimitry Andric } // namespace 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric AArch64BtiPac::AArch64BtiPac() { 6030b57cec5SDimitry Andric btiHeader = (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI); 6040b57cec5SDimitry Andric // A BTI (Branch Target Indicator) Plt Entry is only required if the 6050b57cec5SDimitry Andric // address of the PLT entry can be taken by the program, which permits an 6060b57cec5SDimitry Andric // indirect jump to the PLT entry. This can happen when the address 6070b57cec5SDimitry Andric // of the PLT entry for a function is canonicalised due to the address of 6080b57cec5SDimitry Andric // the function in an executable being taken by a shared library. 6090b57cec5SDimitry Andric // FIXME: There is a potential optimization to omit the BTI if we detect 6100b57cec5SDimitry Andric // that the address of the PLT entry isn't taken. 611*5ffd83dbSDimitry Andric // The PAC PLT entries require dynamic loader support and this isn't known 612*5ffd83dbSDimitry Andric // from properties in the objects, so we use the command line flag. 6130b57cec5SDimitry Andric btiEntry = btiHeader && !config->shared; 614*5ffd83dbSDimitry Andric pacEntry = config->zPacPlt; 6150b57cec5SDimitry Andric 616480093f4SDimitry Andric if (btiEntry || pacEntry) { 6170b57cec5SDimitry Andric pltEntrySize = 24; 618480093f4SDimitry Andric ipltEntrySize = 24; 619480093f4SDimitry Andric } 6200b57cec5SDimitry Andric } 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric void AArch64BtiPac::writePltHeader(uint8_t *buf) const { 6230b57cec5SDimitry Andric const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c 6240b57cec5SDimitry Andric const uint8_t pltData[] = { 6250b57cec5SDimitry Andric 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! 6260b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) 6270b57cec5SDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] 6280b57cec5SDimitry Andric 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) 6290b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6, // br x17 6300b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5, // nop 6310b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5 // nop 6320b57cec5SDimitry Andric }; 6330b57cec5SDimitry Andric const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric uint64_t got = in.gotPlt->getVA(); 6360b57cec5SDimitry Andric uint64_t plt = in.plt->getVA(); 6370b57cec5SDimitry Andric 6380b57cec5SDimitry Andric if (btiHeader) { 6390b57cec5SDimitry Andric // PltHeader is called indirectly by plt[N]. Prefix pltData with a BTI C 6400b57cec5SDimitry Andric // instruction. 6410b57cec5SDimitry Andric memcpy(buf, btiData, sizeof(btiData)); 6420b57cec5SDimitry Andric buf += sizeof(btiData); 6430b57cec5SDimitry Andric plt += sizeof(btiData); 6440b57cec5SDimitry Andric } 6450b57cec5SDimitry Andric memcpy(buf, pltData, sizeof(pltData)); 6460b57cec5SDimitry Andric 647*5ffd83dbSDimitry Andric relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21, 6480b57cec5SDimitry Andric getAArch64Page(got + 16) - getAArch64Page(plt + 8)); 649*5ffd83dbSDimitry Andric relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16); 650*5ffd83dbSDimitry Andric relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16); 6510b57cec5SDimitry Andric if (!btiHeader) 6520b57cec5SDimitry Andric // We didn't add the BTI c instruction so round out size with NOP. 6530b57cec5SDimitry Andric memcpy(buf + sizeof(pltData), nopData, sizeof(nopData)); 6540b57cec5SDimitry Andric } 6550b57cec5SDimitry Andric 656480093f4SDimitry Andric void AArch64BtiPac::writePlt(uint8_t *buf, const Symbol &sym, 657480093f4SDimitry Andric uint64_t pltEntryAddr) const { 6580b57cec5SDimitry Andric // The PLT entry is of the form: 6590b57cec5SDimitry Andric // [btiData] addrInst (pacBr | stdBr) [nopData] 6600b57cec5SDimitry Andric const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c 6610b57cec5SDimitry Andric const uint8_t addrInst[] = { 6620b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) 6630b57cec5SDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] 6640b57cec5SDimitry Andric 0x10, 0x02, 0x00, 0x91 // add x16, x16, Offset(&(.plt.got[n])) 6650b57cec5SDimitry Andric }; 6660b57cec5SDimitry Andric const uint8_t pacBr[] = { 6670b57cec5SDimitry Andric 0x9f, 0x21, 0x03, 0xd5, // autia1716 6680b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6 // br x17 6690b57cec5SDimitry Andric }; 6700b57cec5SDimitry Andric const uint8_t stdBr[] = { 6710b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6, // br x17 6720b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5 // nop 6730b57cec5SDimitry Andric }; 6740b57cec5SDimitry Andric const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop 6750b57cec5SDimitry Andric 6760b57cec5SDimitry Andric if (btiEntry) { 6770b57cec5SDimitry Andric memcpy(buf, btiData, sizeof(btiData)); 6780b57cec5SDimitry Andric buf += sizeof(btiData); 6790b57cec5SDimitry Andric pltEntryAddr += sizeof(btiData); 6800b57cec5SDimitry Andric } 6810b57cec5SDimitry Andric 682480093f4SDimitry Andric uint64_t gotPltEntryAddr = sym.getGotPltVA(); 6830b57cec5SDimitry Andric memcpy(buf, addrInst, sizeof(addrInst)); 684*5ffd83dbSDimitry Andric relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21, 685*5ffd83dbSDimitry Andric getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr)); 686*5ffd83dbSDimitry Andric relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr); 687*5ffd83dbSDimitry Andric relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr); 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric if (pacEntry) 6900b57cec5SDimitry Andric memcpy(buf + sizeof(addrInst), pacBr, sizeof(pacBr)); 6910b57cec5SDimitry Andric else 6920b57cec5SDimitry Andric memcpy(buf + sizeof(addrInst), stdBr, sizeof(stdBr)); 6930b57cec5SDimitry Andric if (!btiEntry) 6940b57cec5SDimitry Andric // We didn't add the BTI c instruction so round out size with NOP. 6950b57cec5SDimitry Andric memcpy(buf + sizeof(addrInst) + sizeof(stdBr), nopData, sizeof(nopData)); 6960b57cec5SDimitry Andric } 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric static TargetInfo *getTargetInfo() { 6990b57cec5SDimitry Andric if (config->andFeatures & (GNU_PROPERTY_AARCH64_FEATURE_1_BTI | 7000b57cec5SDimitry Andric GNU_PROPERTY_AARCH64_FEATURE_1_PAC)) { 7010b57cec5SDimitry Andric static AArch64BtiPac t; 7020b57cec5SDimitry Andric return &t; 7030b57cec5SDimitry Andric } 7040b57cec5SDimitry Andric static AArch64 t; 7050b57cec5SDimitry Andric return &t; 7060b57cec5SDimitry Andric } 7070b57cec5SDimitry Andric 708*5ffd83dbSDimitry Andric TargetInfo *elf::getAArch64TargetInfo() { return getTargetInfo(); } 709