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; 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; 475ffd83dbSDimitry Andric void relocate(uint8_t *loc, const Relocation &rel, 485ffd83dbSDimitry Andric uint64_t val) const override; 49*e8d8bef9SDimitry Andric RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override; 505ffd83dbSDimitry Andric void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 515ffd83dbSDimitry Andric uint64_t val) const override; 525ffd83dbSDimitry Andric void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 535ffd83dbSDimitry Andric uint64_t val) const override; 545ffd83dbSDimitry Andric void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 555ffd83dbSDimitry Andric uint64_t val) const override; 560b57cec5SDimitry Andric }; 570b57cec5SDimitry Andric } // namespace 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric AArch64::AArch64() { 600b57cec5SDimitry Andric copyRel = R_AARCH64_COPY; 610b57cec5SDimitry Andric relativeRel = R_AARCH64_RELATIVE; 620b57cec5SDimitry Andric iRelativeRel = R_AARCH64_IRELATIVE; 630b57cec5SDimitry Andric gotRel = R_AARCH64_GLOB_DAT; 640b57cec5SDimitry Andric noneRel = R_AARCH64_NONE; 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; 73*e8d8bef9SDimitry Andric gotBaseSymInGotPlt = false; 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: 124*e8d8bef9SDimitry Andric return R_TPREL; 1250b57cec5SDimitry Andric case R_AARCH64_CALL26: 1260b57cec5SDimitry Andric case R_AARCH64_CONDBR19: 1270b57cec5SDimitry Andric case R_AARCH64_JUMP26: 1280b57cec5SDimitry Andric case R_AARCH64_TSTBR14: 1295ffd83dbSDimitry 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; 150*e8d8bef9SDimitry Andric case R_AARCH64_LD64_GOTPAGE_LO15: 151*e8d8bef9SDimitry Andric return R_AARCH64_GOT_PAGE; 1520b57cec5SDimitry Andric case R_AARCH64_ADR_GOT_PAGE: 1530b57cec5SDimitry Andric case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: 1540b57cec5SDimitry Andric return R_AARCH64_GOT_PAGE_PC; 1550b57cec5SDimitry Andric case R_AARCH64_NONE: 1560b57cec5SDimitry Andric return R_NONE; 1570b57cec5SDimitry Andric default: 15885868e8aSDimitry Andric error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 15985868e8aSDimitry Andric ") against symbol " + toString(s)); 16085868e8aSDimitry Andric return R_NONE; 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric 164*e8d8bef9SDimitry Andric RelExpr AArch64::adjustTlsExpr(RelType type, RelExpr expr) const { 1650b57cec5SDimitry Andric if (expr == R_RELAX_TLS_GD_TO_IE) { 1660b57cec5SDimitry Andric if (type == R_AARCH64_TLSDESC_ADR_PAGE21) 1670b57cec5SDimitry Andric return R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC; 1680b57cec5SDimitry Andric return R_RELAX_TLS_GD_TO_IE_ABS; 1690b57cec5SDimitry Andric } 1700b57cec5SDimitry Andric return expr; 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric bool AArch64::usesOnlyLowPageBits(RelType type) const { 1740b57cec5SDimitry Andric switch (type) { 1750b57cec5SDimitry Andric default: 1760b57cec5SDimitry Andric return false; 1770b57cec5SDimitry Andric case R_AARCH64_ADD_ABS_LO12_NC: 1780b57cec5SDimitry Andric case R_AARCH64_LD64_GOT_LO12_NC: 1790b57cec5SDimitry Andric case R_AARCH64_LDST128_ABS_LO12_NC: 1800b57cec5SDimitry Andric case R_AARCH64_LDST16_ABS_LO12_NC: 1810b57cec5SDimitry Andric case R_AARCH64_LDST32_ABS_LO12_NC: 1820b57cec5SDimitry Andric case R_AARCH64_LDST64_ABS_LO12_NC: 1830b57cec5SDimitry Andric case R_AARCH64_LDST8_ABS_LO12_NC: 1840b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 1850b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 1860b57cec5SDimitry Andric case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 1870b57cec5SDimitry Andric return true; 1880b57cec5SDimitry Andric } 1890b57cec5SDimitry Andric } 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric RelType AArch64::getDynRel(RelType type) const { 1920b57cec5SDimitry Andric if (type == R_AARCH64_ABS64) 1930b57cec5SDimitry Andric return type; 1940b57cec5SDimitry Andric return R_AARCH64_NONE; 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric void AArch64::writeGotPlt(uint8_t *buf, const Symbol &) const { 1980b57cec5SDimitry Andric write64le(buf, in.plt->getVA()); 1990b57cec5SDimitry Andric } 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric void AArch64::writePltHeader(uint8_t *buf) const { 2020b57cec5SDimitry Andric const uint8_t pltData[] = { 2030b57cec5SDimitry Andric 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! 2040b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) 2050b57cec5SDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] 2060b57cec5SDimitry Andric 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) 2070b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6, // br x17 2080b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5, // nop 2090b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5, // nop 2100b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5 // nop 2110b57cec5SDimitry Andric }; 2120b57cec5SDimitry Andric memcpy(buf, pltData, sizeof(pltData)); 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric uint64_t got = in.gotPlt->getVA(); 2150b57cec5SDimitry Andric uint64_t plt = in.plt->getVA(); 2165ffd83dbSDimitry Andric relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21, 2170b57cec5SDimitry Andric getAArch64Page(got + 16) - getAArch64Page(plt + 4)); 2185ffd83dbSDimitry Andric relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16); 2195ffd83dbSDimitry Andric relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16); 2200b57cec5SDimitry Andric } 2210b57cec5SDimitry Andric 222480093f4SDimitry Andric void AArch64::writePlt(uint8_t *buf, const Symbol &sym, 223480093f4SDimitry Andric uint64_t pltEntryAddr) const { 2240b57cec5SDimitry Andric const uint8_t inst[] = { 2250b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) 2260b57cec5SDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] 2270b57cec5SDimitry Andric 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n])) 2280b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6 // br x17 2290b57cec5SDimitry Andric }; 2300b57cec5SDimitry Andric memcpy(buf, inst, sizeof(inst)); 2310b57cec5SDimitry Andric 232480093f4SDimitry Andric uint64_t gotPltEntryAddr = sym.getGotPltVA(); 2335ffd83dbSDimitry Andric relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21, 2340b57cec5SDimitry Andric getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr)); 2355ffd83dbSDimitry Andric relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr); 2365ffd83dbSDimitry Andric relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr); 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric bool AArch64::needsThunk(RelExpr expr, RelType type, const InputFile *file, 240480093f4SDimitry Andric uint64_t branchAddr, const Symbol &s, 241480093f4SDimitry Andric int64_t a) const { 242480093f4SDimitry Andric // If s is an undefined weak symbol and does not have a PLT entry then it 243480093f4SDimitry Andric // will be resolved as a branch to the next instruction. 244480093f4SDimitry Andric if (s.isUndefWeak() && !s.isInPlt()) 245480093f4SDimitry Andric return false; 2460b57cec5SDimitry Andric // ELF for the ARM 64-bit architecture, section Call and Jump relocations 2470b57cec5SDimitry Andric // only permits range extension thunks for R_AARCH64_CALL26 and 2480b57cec5SDimitry Andric // R_AARCH64_JUMP26 relocation types. 2495ffd83dbSDimitry Andric if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 && 2505ffd83dbSDimitry Andric type != R_AARCH64_PLT32) 2510b57cec5SDimitry Andric return false; 252480093f4SDimitry Andric uint64_t dst = expr == R_PLT_PC ? s.getPltVA() : s.getVA(a); 2530b57cec5SDimitry Andric return !inBranchRange(type, branchAddr, dst); 2540b57cec5SDimitry Andric } 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric uint32_t AArch64::getThunkSectionSpacing() const { 2570b57cec5SDimitry Andric // See comment in Arch/ARM.cpp for a more detailed explanation of 2580b57cec5SDimitry Andric // getThunkSectionSpacing(). For AArch64 the only branches we are permitted to 2590b57cec5SDimitry Andric // Thunk have a range of +/- 128 MiB 2600b57cec5SDimitry Andric return (128 * 1024 * 1024) - 0x30000; 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric bool AArch64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 2645ffd83dbSDimitry Andric if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 && 2655ffd83dbSDimitry Andric type != R_AARCH64_PLT32) 2660b57cec5SDimitry Andric return true; 2670b57cec5SDimitry Andric // The AArch64 call and unconditional branch instructions have a range of 2685ffd83dbSDimitry Andric // +/- 128 MiB. The PLT32 relocation supports a range up to +/- 2 GiB. 2695ffd83dbSDimitry Andric uint64_t range = 2705ffd83dbSDimitry Andric type == R_AARCH64_PLT32 ? (UINT64_C(1) << 31) : (128 * 1024 * 1024); 2710b57cec5SDimitry Andric if (dst > src) { 2720b57cec5SDimitry Andric // Immediate of branch is signed. 2730b57cec5SDimitry Andric range -= 4; 2740b57cec5SDimitry Andric return dst - src <= range; 2750b57cec5SDimitry Andric } 2760b57cec5SDimitry Andric return src - dst <= range; 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric static void write32AArch64Addr(uint8_t *l, uint64_t imm) { 2800b57cec5SDimitry Andric uint32_t immLo = (imm & 0x3) << 29; 2810b57cec5SDimitry Andric uint32_t immHi = (imm & 0x1FFFFC) << 3; 2820b57cec5SDimitry Andric uint64_t mask = (0x3 << 29) | (0x1FFFFC << 3); 2830b57cec5SDimitry Andric write32le(l, (read32le(l) & ~mask) | immLo | immHi); 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric // Return the bits [Start, End] from Val shifted Start bits. 2870b57cec5SDimitry Andric // For instance, getBits(0xF0, 4, 8) returns 0xF. 2880b57cec5SDimitry Andric static uint64_t getBits(uint64_t val, int start, int end) { 2890b57cec5SDimitry Andric uint64_t mask = ((uint64_t)1 << (end + 1 - start)) - 1; 2900b57cec5SDimitry Andric return (val >> start) & mask; 2910b57cec5SDimitry Andric } 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric static void or32le(uint8_t *p, int32_t v) { write32le(p, read32le(p) | v); } 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric // Update the immediate field in a AARCH64 ldr, str, and add instruction. 2960b57cec5SDimitry Andric static void or32AArch64Imm(uint8_t *l, uint64_t imm) { 2970b57cec5SDimitry Andric or32le(l, (imm & 0xFFF) << 10); 2980b57cec5SDimitry Andric } 2990b57cec5SDimitry Andric 30085868e8aSDimitry Andric // Update the immediate field in an AArch64 movk, movn or movz instruction 30185868e8aSDimitry Andric // for a signed relocation, and update the opcode of a movn or movz instruction 30285868e8aSDimitry Andric // to match the sign of the operand. 30385868e8aSDimitry Andric static void writeSMovWImm(uint8_t *loc, uint32_t imm) { 30485868e8aSDimitry Andric uint32_t inst = read32le(loc); 30585868e8aSDimitry Andric // Opcode field is bits 30, 29, with 10 = movz, 00 = movn and 11 = movk. 30685868e8aSDimitry Andric if (!(inst & (1 << 29))) { 30785868e8aSDimitry Andric // movn or movz. 30885868e8aSDimitry Andric if (imm & 0x10000) { 30985868e8aSDimitry Andric // Change opcode to movn, which takes an inverted operand. 31085868e8aSDimitry Andric imm ^= 0xFFFF; 31185868e8aSDimitry Andric inst &= ~(1 << 30); 31285868e8aSDimitry Andric } else { 31385868e8aSDimitry Andric // Change opcode to movz. 31485868e8aSDimitry Andric inst |= 1 << 30; 31585868e8aSDimitry Andric } 31685868e8aSDimitry Andric } 31785868e8aSDimitry Andric write32le(loc, inst | ((imm & 0xFFFF) << 5)); 31885868e8aSDimitry Andric } 31985868e8aSDimitry Andric 3205ffd83dbSDimitry Andric void AArch64::relocate(uint8_t *loc, const Relocation &rel, 3215ffd83dbSDimitry Andric uint64_t val) const { 3225ffd83dbSDimitry Andric switch (rel.type) { 3230b57cec5SDimitry Andric case R_AARCH64_ABS16: 3240b57cec5SDimitry Andric case R_AARCH64_PREL16: 3255ffd83dbSDimitry Andric checkIntUInt(loc, val, 16, rel); 3260b57cec5SDimitry Andric write16le(loc, val); 3270b57cec5SDimitry Andric break; 3280b57cec5SDimitry Andric case R_AARCH64_ABS32: 3290b57cec5SDimitry Andric case R_AARCH64_PREL32: 3305ffd83dbSDimitry Andric checkIntUInt(loc, val, 32, rel); 3315ffd83dbSDimitry Andric write32le(loc, val); 3325ffd83dbSDimitry Andric break; 3335ffd83dbSDimitry Andric case R_AARCH64_PLT32: 3345ffd83dbSDimitry Andric checkInt(loc, val, 32, rel); 3350b57cec5SDimitry Andric write32le(loc, val); 3360b57cec5SDimitry Andric break; 3370b57cec5SDimitry Andric case R_AARCH64_ABS64: 3380b57cec5SDimitry Andric case R_AARCH64_PREL64: 3390b57cec5SDimitry Andric write64le(loc, val); 3400b57cec5SDimitry Andric break; 3410b57cec5SDimitry Andric case R_AARCH64_ADD_ABS_LO12_NC: 3420b57cec5SDimitry Andric or32AArch64Imm(loc, val); 3430b57cec5SDimitry Andric break; 3440b57cec5SDimitry Andric case R_AARCH64_ADR_GOT_PAGE: 3450b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21: 3460b57cec5SDimitry Andric case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: 3470b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21: 3485ffd83dbSDimitry Andric checkInt(loc, val, 33, rel); 3490b57cec5SDimitry Andric LLVM_FALLTHROUGH; 3500b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21_NC: 3510b57cec5SDimitry Andric write32AArch64Addr(loc, val >> 12); 3520b57cec5SDimitry Andric break; 3530b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_LO21: 3545ffd83dbSDimitry Andric checkInt(loc, val, 21, rel); 3550b57cec5SDimitry Andric write32AArch64Addr(loc, val); 3560b57cec5SDimitry Andric break; 3570b57cec5SDimitry Andric case R_AARCH64_JUMP26: 3580b57cec5SDimitry Andric // Normally we would just write the bits of the immediate field, however 3590b57cec5SDimitry Andric // when patching instructions for the cpu errata fix -fix-cortex-a53-843419 3600b57cec5SDimitry Andric // we want to replace a non-branch instruction with a branch immediate 3610b57cec5SDimitry Andric // instruction. By writing all the bits of the instruction including the 3620b57cec5SDimitry Andric // opcode and the immediate (0 001 | 01 imm26) we can do this 3630b57cec5SDimitry Andric // transformation by placing a R_AARCH64_JUMP26 relocation at the offset of 3640b57cec5SDimitry Andric // the instruction we want to patch. 3650b57cec5SDimitry Andric write32le(loc, 0x14000000); 3660b57cec5SDimitry Andric LLVM_FALLTHROUGH; 3670b57cec5SDimitry Andric case R_AARCH64_CALL26: 3685ffd83dbSDimitry Andric checkInt(loc, val, 28, rel); 3690b57cec5SDimitry Andric or32le(loc, (val & 0x0FFFFFFC) >> 2); 3700b57cec5SDimitry Andric break; 3710b57cec5SDimitry Andric case R_AARCH64_CONDBR19: 3720b57cec5SDimitry Andric case R_AARCH64_LD_PREL_LO19: 3735ffd83dbSDimitry Andric checkAlignment(loc, val, 4, rel); 3745ffd83dbSDimitry Andric checkInt(loc, val, 21, rel); 3750b57cec5SDimitry Andric or32le(loc, (val & 0x1FFFFC) << 3); 3760b57cec5SDimitry Andric break; 3770b57cec5SDimitry Andric case R_AARCH64_LDST8_ABS_LO12_NC: 3780b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC: 3790b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 0, 11)); 3800b57cec5SDimitry Andric break; 3810b57cec5SDimitry Andric case R_AARCH64_LDST16_ABS_LO12_NC: 3820b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC: 3835ffd83dbSDimitry Andric checkAlignment(loc, val, 2, rel); 3840b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 1, 11)); 3850b57cec5SDimitry Andric break; 3860b57cec5SDimitry Andric case R_AARCH64_LDST32_ABS_LO12_NC: 3870b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC: 3885ffd83dbSDimitry Andric checkAlignment(loc, val, 4, rel); 3890b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 2, 11)); 3900b57cec5SDimitry Andric break; 3910b57cec5SDimitry Andric case R_AARCH64_LDST64_ABS_LO12_NC: 3920b57cec5SDimitry Andric case R_AARCH64_LD64_GOT_LO12_NC: 3930b57cec5SDimitry Andric case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 3940b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC: 3950b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 3965ffd83dbSDimitry Andric checkAlignment(loc, val, 8, rel); 3970b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 3, 11)); 3980b57cec5SDimitry Andric break; 3990b57cec5SDimitry Andric case R_AARCH64_LDST128_ABS_LO12_NC: 4000b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC: 4015ffd83dbSDimitry Andric checkAlignment(loc, val, 16, rel); 4020b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 4, 11)); 4030b57cec5SDimitry Andric break; 404*e8d8bef9SDimitry Andric case R_AARCH64_LD64_GOTPAGE_LO15: 405*e8d8bef9SDimitry Andric checkAlignment(loc, val, 8, rel); 406*e8d8bef9SDimitry Andric or32AArch64Imm(loc, getBits(val, 3, 14)); 407*e8d8bef9SDimitry Andric break; 40885868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G0: 4095ffd83dbSDimitry Andric checkUInt(loc, val, 16, rel); 41085868e8aSDimitry Andric LLVM_FALLTHROUGH; 4110b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G0_NC: 4120b57cec5SDimitry Andric or32le(loc, (val & 0xFFFF) << 5); 4130b57cec5SDimitry Andric break; 41485868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G1: 4155ffd83dbSDimitry Andric checkUInt(loc, val, 32, rel); 41685868e8aSDimitry Andric LLVM_FALLTHROUGH; 4170b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G1_NC: 4180b57cec5SDimitry Andric or32le(loc, (val & 0xFFFF0000) >> 11); 4190b57cec5SDimitry Andric break; 42085868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G2: 4215ffd83dbSDimitry Andric checkUInt(loc, val, 48, rel); 42285868e8aSDimitry Andric LLVM_FALLTHROUGH; 4230b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G2_NC: 4240b57cec5SDimitry Andric or32le(loc, (val & 0xFFFF00000000) >> 27); 4250b57cec5SDimitry Andric break; 4260b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G3: 4270b57cec5SDimitry Andric or32le(loc, (val & 0xFFFF000000000000) >> 43); 4280b57cec5SDimitry Andric break; 42985868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0: 43085868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G0: 43185868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0: 4325ffd83dbSDimitry Andric checkInt(loc, val, 17, rel); 43385868e8aSDimitry Andric LLVM_FALLTHROUGH; 43485868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0_NC: 43585868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC: 43685868e8aSDimitry Andric writeSMovWImm(loc, val); 43785868e8aSDimitry Andric break; 43885868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1: 43985868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G1: 44085868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1: 4415ffd83dbSDimitry Andric checkInt(loc, val, 33, rel); 44285868e8aSDimitry Andric LLVM_FALLTHROUGH; 44385868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1_NC: 44485868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC: 44585868e8aSDimitry Andric writeSMovWImm(loc, val >> 16); 44685868e8aSDimitry Andric break; 44785868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2: 44885868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G2: 44985868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G2: 4505ffd83dbSDimitry Andric checkInt(loc, val, 49, rel); 45185868e8aSDimitry Andric LLVM_FALLTHROUGH; 45285868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2_NC: 45385868e8aSDimitry Andric writeSMovWImm(loc, val >> 32); 45485868e8aSDimitry Andric break; 45585868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G3: 45685868e8aSDimitry Andric writeSMovWImm(loc, val >> 48); 45785868e8aSDimitry Andric break; 4580b57cec5SDimitry Andric case R_AARCH64_TSTBR14: 4595ffd83dbSDimitry Andric checkInt(loc, val, 16, rel); 4600b57cec5SDimitry Andric or32le(loc, (val & 0xFFFC) << 3); 4610b57cec5SDimitry Andric break; 4620b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_HI12: 4635ffd83dbSDimitry Andric checkUInt(loc, val, 24, rel); 4640b57cec5SDimitry Andric or32AArch64Imm(loc, val >> 12); 4650b57cec5SDimitry Andric break; 4660b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: 4670b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 4680b57cec5SDimitry Andric or32AArch64Imm(loc, val); 4690b57cec5SDimitry Andric break; 4700b57cec5SDimitry Andric default: 47185868e8aSDimitry Andric llvm_unreachable("unknown relocation"); 4720b57cec5SDimitry Andric } 4730b57cec5SDimitry Andric } 4740b57cec5SDimitry Andric 4755ffd83dbSDimitry Andric void AArch64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 4765ffd83dbSDimitry Andric uint64_t val) const { 4770b57cec5SDimitry Andric // TLSDESC Global-Dynamic relocation are in the form: 4780b57cec5SDimitry Andric // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21] 4790b57cec5SDimitry Andric // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12] 4800b57cec5SDimitry Andric // add x0, x0, :tlsdesc_los:v [R_AARCH64_TLSDESC_ADD_LO12] 4810b57cec5SDimitry Andric // .tlsdesccall [R_AARCH64_TLSDESC_CALL] 4820b57cec5SDimitry Andric // blr x1 4830b57cec5SDimitry Andric // And it can optimized to: 4840b57cec5SDimitry Andric // movz x0, #0x0, lsl #16 4850b57cec5SDimitry Andric // movk x0, #0x10 4860b57cec5SDimitry Andric // nop 4870b57cec5SDimitry Andric // nop 4885ffd83dbSDimitry Andric checkUInt(loc, val, 32, rel); 4890b57cec5SDimitry Andric 4905ffd83dbSDimitry Andric switch (rel.type) { 4910b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 4920b57cec5SDimitry Andric case R_AARCH64_TLSDESC_CALL: 4930b57cec5SDimitry Andric write32le(loc, 0xd503201f); // nop 4940b57cec5SDimitry Andric return; 4950b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21: 4960b57cec5SDimitry Andric write32le(loc, 0xd2a00000 | (((val >> 16) & 0xffff) << 5)); // movz 4970b57cec5SDimitry Andric return; 4980b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 4990b57cec5SDimitry Andric write32le(loc, 0xf2800000 | ((val & 0xffff) << 5)); // movk 5000b57cec5SDimitry Andric return; 5010b57cec5SDimitry Andric default: 5020b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric } 5050b57cec5SDimitry Andric 5065ffd83dbSDimitry Andric void AArch64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 5075ffd83dbSDimitry Andric uint64_t val) const { 5080b57cec5SDimitry Andric // TLSDESC Global-Dynamic relocation are in the form: 5090b57cec5SDimitry Andric // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21] 5100b57cec5SDimitry Andric // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12] 5110b57cec5SDimitry Andric // add x0, x0, :tlsdesc_los:v [R_AARCH64_TLSDESC_ADD_LO12] 5120b57cec5SDimitry Andric // .tlsdesccall [R_AARCH64_TLSDESC_CALL] 5130b57cec5SDimitry Andric // blr x1 5140b57cec5SDimitry Andric // And it can optimized to: 5150b57cec5SDimitry Andric // adrp x0, :gottprel:v 5160b57cec5SDimitry Andric // ldr x0, [x0, :gottprel_lo12:v] 5170b57cec5SDimitry Andric // nop 5180b57cec5SDimitry Andric // nop 5190b57cec5SDimitry Andric 5205ffd83dbSDimitry Andric switch (rel.type) { 5210b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 5220b57cec5SDimitry Andric case R_AARCH64_TLSDESC_CALL: 5230b57cec5SDimitry Andric write32le(loc, 0xd503201f); // nop 5240b57cec5SDimitry Andric break; 5250b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21: 5260b57cec5SDimitry Andric write32le(loc, 0x90000000); // adrp 5275ffd83dbSDimitry Andric relocateNoSym(loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, val); 5280b57cec5SDimitry Andric break; 5290b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 5300b57cec5SDimitry Andric write32le(loc, 0xf9400000); // ldr 5315ffd83dbSDimitry Andric relocateNoSym(loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, val); 5320b57cec5SDimitry Andric break; 5330b57cec5SDimitry Andric default: 5340b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 5350b57cec5SDimitry Andric } 5360b57cec5SDimitry Andric } 5370b57cec5SDimitry Andric 5385ffd83dbSDimitry Andric void AArch64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 5395ffd83dbSDimitry Andric uint64_t val) const { 5405ffd83dbSDimitry Andric checkUInt(loc, val, 32, rel); 5410b57cec5SDimitry Andric 5425ffd83dbSDimitry Andric if (rel.type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) { 5430b57cec5SDimitry Andric // Generate MOVZ. 5440b57cec5SDimitry Andric uint32_t regNo = read32le(loc) & 0x1f; 5450b57cec5SDimitry Andric write32le(loc, (0xd2a00000 | regNo) | (((val >> 16) & 0xffff) << 5)); 5460b57cec5SDimitry Andric return; 5470b57cec5SDimitry Andric } 5485ffd83dbSDimitry Andric if (rel.type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) { 5490b57cec5SDimitry Andric // Generate MOVK. 5500b57cec5SDimitry Andric uint32_t regNo = read32le(loc) & 0x1f; 5510b57cec5SDimitry Andric write32le(loc, (0xf2800000 | regNo) | ((val & 0xffff) << 5)); 5520b57cec5SDimitry Andric return; 5530b57cec5SDimitry Andric } 5540b57cec5SDimitry Andric llvm_unreachable("invalid relocation for TLS IE to LE relaxation"); 5550b57cec5SDimitry Andric } 5560b57cec5SDimitry Andric 5570b57cec5SDimitry Andric // AArch64 may use security features in variant PLT sequences. These are: 5580b57cec5SDimitry Andric // Pointer Authentication (PAC), introduced in armv8.3-a and Branch Target 5590b57cec5SDimitry Andric // Indicator (BTI) introduced in armv8.5-a. The additional instructions used 5600b57cec5SDimitry Andric // in the variant Plt sequences are encoded in the Hint space so they can be 5610b57cec5SDimitry Andric // deployed on older architectures, which treat the instructions as a nop. 5620b57cec5SDimitry Andric // PAC and BTI can be combined leading to the following combinations: 5630b57cec5SDimitry Andric // writePltHeader 5640b57cec5SDimitry Andric // writePltHeaderBti (no PAC Header needed) 5650b57cec5SDimitry Andric // writePlt 5660b57cec5SDimitry Andric // writePltBti (BTI only) 5670b57cec5SDimitry Andric // writePltPac (PAC only) 5680b57cec5SDimitry Andric // writePltBtiPac (BTI and PAC) 5690b57cec5SDimitry Andric // 5700b57cec5SDimitry Andric // When PAC is enabled the dynamic loader encrypts the address that it places 5710b57cec5SDimitry Andric // in the .got.plt using the pacia1716 instruction which encrypts the value in 5720b57cec5SDimitry Andric // x17 using the modifier in x16. The static linker places autia1716 before the 5730b57cec5SDimitry Andric // indirect branch to x17 to authenticate the address in x17 with the modifier 5740b57cec5SDimitry Andric // in x16. This makes it more difficult for an attacker to modify the value in 5750b57cec5SDimitry Andric // the .got.plt. 5760b57cec5SDimitry Andric // 5770b57cec5SDimitry Andric // When BTI is enabled all indirect branches must land on a bti instruction. 5780b57cec5SDimitry Andric // The static linker must place a bti instruction at the start of any PLT entry 5790b57cec5SDimitry Andric // that may be the target of an indirect branch. As the PLT entries call the 5800b57cec5SDimitry Andric // lazy resolver indirectly this must have a bti instruction at start. In 5810b57cec5SDimitry Andric // general a bti instruction is not needed for a PLT entry as indirect calls 5820b57cec5SDimitry Andric // are resolved to the function address and not the PLT entry for the function. 5830b57cec5SDimitry Andric // There are a small number of cases where the PLT address can escape, such as 5840b57cec5SDimitry Andric // taking the address of a function or ifunc via a non got-generating 5850b57cec5SDimitry Andric // relocation, and a shared library refers to that symbol. 5860b57cec5SDimitry Andric // 5870b57cec5SDimitry Andric // We use the bti c variant of the instruction which permits indirect branches 5880b57cec5SDimitry Andric // (br) via x16/x17 and indirect function calls (blr) via any register. The ABI 5890b57cec5SDimitry Andric // guarantees that all indirect branches from code requiring BTI protection 5900b57cec5SDimitry Andric // will go via x16/x17 5910b57cec5SDimitry Andric 5920b57cec5SDimitry Andric namespace { 5930b57cec5SDimitry Andric class AArch64BtiPac final : public AArch64 { 5940b57cec5SDimitry Andric public: 5950b57cec5SDimitry Andric AArch64BtiPac(); 5960b57cec5SDimitry Andric void writePltHeader(uint8_t *buf) const override; 597480093f4SDimitry Andric void writePlt(uint8_t *buf, const Symbol &sym, 598480093f4SDimitry Andric uint64_t pltEntryAddr) const override; 5990b57cec5SDimitry Andric 6000b57cec5SDimitry Andric private: 6010b57cec5SDimitry Andric bool btiHeader; // bti instruction needed in PLT Header 6020b57cec5SDimitry Andric bool btiEntry; // bti instruction needed in PLT Entry 6030b57cec5SDimitry Andric bool pacEntry; // autia1716 instruction needed in PLT Entry 6040b57cec5SDimitry Andric }; 6050b57cec5SDimitry Andric } // namespace 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric AArch64BtiPac::AArch64BtiPac() { 6080b57cec5SDimitry Andric btiHeader = (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI); 6090b57cec5SDimitry Andric // A BTI (Branch Target Indicator) Plt Entry is only required if the 6100b57cec5SDimitry Andric // address of the PLT entry can be taken by the program, which permits an 6110b57cec5SDimitry Andric // indirect jump to the PLT entry. This can happen when the address 6120b57cec5SDimitry Andric // of the PLT entry for a function is canonicalised due to the address of 6130b57cec5SDimitry Andric // the function in an executable being taken by a shared library. 6140b57cec5SDimitry Andric // FIXME: There is a potential optimization to omit the BTI if we detect 6150b57cec5SDimitry Andric // that the address of the PLT entry isn't taken. 6165ffd83dbSDimitry Andric // The PAC PLT entries require dynamic loader support and this isn't known 6175ffd83dbSDimitry Andric // from properties in the objects, so we use the command line flag. 6180b57cec5SDimitry Andric btiEntry = btiHeader && !config->shared; 6195ffd83dbSDimitry Andric pacEntry = config->zPacPlt; 6200b57cec5SDimitry Andric 621480093f4SDimitry Andric if (btiEntry || pacEntry) { 6220b57cec5SDimitry Andric pltEntrySize = 24; 623480093f4SDimitry Andric ipltEntrySize = 24; 624480093f4SDimitry Andric } 6250b57cec5SDimitry Andric } 6260b57cec5SDimitry Andric 6270b57cec5SDimitry Andric void AArch64BtiPac::writePltHeader(uint8_t *buf) const { 6280b57cec5SDimitry Andric const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c 6290b57cec5SDimitry Andric const uint8_t pltData[] = { 6300b57cec5SDimitry Andric 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! 6310b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) 6320b57cec5SDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] 6330b57cec5SDimitry Andric 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) 6340b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6, // br x17 6350b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5, // nop 6360b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5 // nop 6370b57cec5SDimitry Andric }; 6380b57cec5SDimitry Andric const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop 6390b57cec5SDimitry Andric 6400b57cec5SDimitry Andric uint64_t got = in.gotPlt->getVA(); 6410b57cec5SDimitry Andric uint64_t plt = in.plt->getVA(); 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric if (btiHeader) { 6440b57cec5SDimitry Andric // PltHeader is called indirectly by plt[N]. Prefix pltData with a BTI C 6450b57cec5SDimitry Andric // instruction. 6460b57cec5SDimitry Andric memcpy(buf, btiData, sizeof(btiData)); 6470b57cec5SDimitry Andric buf += sizeof(btiData); 6480b57cec5SDimitry Andric plt += sizeof(btiData); 6490b57cec5SDimitry Andric } 6500b57cec5SDimitry Andric memcpy(buf, pltData, sizeof(pltData)); 6510b57cec5SDimitry Andric 6525ffd83dbSDimitry Andric relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21, 6530b57cec5SDimitry Andric getAArch64Page(got + 16) - getAArch64Page(plt + 8)); 6545ffd83dbSDimitry Andric relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16); 6555ffd83dbSDimitry Andric relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16); 6560b57cec5SDimitry Andric if (!btiHeader) 6570b57cec5SDimitry Andric // We didn't add the BTI c instruction so round out size with NOP. 6580b57cec5SDimitry Andric memcpy(buf + sizeof(pltData), nopData, sizeof(nopData)); 6590b57cec5SDimitry Andric } 6600b57cec5SDimitry Andric 661480093f4SDimitry Andric void AArch64BtiPac::writePlt(uint8_t *buf, const Symbol &sym, 662480093f4SDimitry Andric uint64_t pltEntryAddr) const { 6630b57cec5SDimitry Andric // The PLT entry is of the form: 6640b57cec5SDimitry Andric // [btiData] addrInst (pacBr | stdBr) [nopData] 6650b57cec5SDimitry Andric const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c 6660b57cec5SDimitry Andric const uint8_t addrInst[] = { 6670b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) 6680b57cec5SDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] 6690b57cec5SDimitry Andric 0x10, 0x02, 0x00, 0x91 // add x16, x16, Offset(&(.plt.got[n])) 6700b57cec5SDimitry Andric }; 6710b57cec5SDimitry Andric const uint8_t pacBr[] = { 6720b57cec5SDimitry Andric 0x9f, 0x21, 0x03, 0xd5, // autia1716 6730b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6 // br x17 6740b57cec5SDimitry Andric }; 6750b57cec5SDimitry Andric const uint8_t stdBr[] = { 6760b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6, // br x17 6770b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5 // nop 6780b57cec5SDimitry Andric }; 6790b57cec5SDimitry Andric const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop 6800b57cec5SDimitry Andric 6810b57cec5SDimitry Andric if (btiEntry) { 6820b57cec5SDimitry Andric memcpy(buf, btiData, sizeof(btiData)); 6830b57cec5SDimitry Andric buf += sizeof(btiData); 6840b57cec5SDimitry Andric pltEntryAddr += sizeof(btiData); 6850b57cec5SDimitry Andric } 6860b57cec5SDimitry Andric 687480093f4SDimitry Andric uint64_t gotPltEntryAddr = sym.getGotPltVA(); 6880b57cec5SDimitry Andric memcpy(buf, addrInst, sizeof(addrInst)); 6895ffd83dbSDimitry Andric relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21, 6905ffd83dbSDimitry Andric getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr)); 6915ffd83dbSDimitry Andric relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr); 6925ffd83dbSDimitry Andric relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr); 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric if (pacEntry) 6950b57cec5SDimitry Andric memcpy(buf + sizeof(addrInst), pacBr, sizeof(pacBr)); 6960b57cec5SDimitry Andric else 6970b57cec5SDimitry Andric memcpy(buf + sizeof(addrInst), stdBr, sizeof(stdBr)); 6980b57cec5SDimitry Andric if (!btiEntry) 6990b57cec5SDimitry Andric // We didn't add the BTI c instruction so round out size with NOP. 7000b57cec5SDimitry Andric memcpy(buf + sizeof(addrInst) + sizeof(stdBr), nopData, sizeof(nopData)); 7010b57cec5SDimitry Andric } 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric static TargetInfo *getTargetInfo() { 7040b57cec5SDimitry Andric if (config->andFeatures & (GNU_PROPERTY_AARCH64_FEATURE_1_BTI | 7050b57cec5SDimitry Andric GNU_PROPERTY_AARCH64_FEATURE_1_PAC)) { 7060b57cec5SDimitry Andric static AArch64BtiPac t; 7070b57cec5SDimitry Andric return &t; 7080b57cec5SDimitry Andric } 7090b57cec5SDimitry Andric static AArch64 t; 7100b57cec5SDimitry Andric return &t; 7110b57cec5SDimitry Andric } 7120b57cec5SDimitry Andric 7135ffd83dbSDimitry Andric TargetInfo *elf::getAArch64TargetInfo() { return getTargetInfo(); } 714