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; 2085868e8aSDimitry Andric 2185868e8aSDimitry Andric namespace lld { 2285868e8aSDimitry Andric namespace elf { 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric // Page(Expr) is the page address of the expression Expr, defined 250b57cec5SDimitry Andric // as (Expr & ~0xFFF). (This applies even if the machine page size 260b57cec5SDimitry Andric // supported by the platform has a different value.) 2785868e8aSDimitry Andric uint64_t getAArch64Page(uint64_t expr) { 280b57cec5SDimitry Andric return expr & ~static_cast<uint64_t>(0xFFF); 290b57cec5SDimitry Andric } 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric namespace { 320b57cec5SDimitry Andric class AArch64 : public TargetInfo { 330b57cec5SDimitry Andric public: 340b57cec5SDimitry Andric AArch64(); 350b57cec5SDimitry Andric RelExpr getRelExpr(RelType type, const Symbol &s, 360b57cec5SDimitry Andric const uint8_t *loc) const override; 370b57cec5SDimitry Andric RelType getDynRel(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; 40*480093f4SDimitry Andric void writePlt(uint8_t *buf, const Symbol &sym, 41*480093f4SDimitry Andric uint64_t pltEntryAddr) const override; 420b57cec5SDimitry Andric bool needsThunk(RelExpr expr, RelType type, const InputFile *file, 43*480093f4SDimitry Andric uint64_t branchAddr, const Symbol &s, 44*480093f4SDimitry 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; 480b57cec5SDimitry Andric void relocateOne(uint8_t *loc, RelType type, uint64_t val) const override; 490b57cec5SDimitry Andric RelExpr adjustRelaxExpr(RelType type, const uint8_t *data, 500b57cec5SDimitry Andric RelExpr expr) const override; 510b57cec5SDimitry Andric void relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const override; 520b57cec5SDimitry Andric void relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const override; 530b57cec5SDimitry Andric void relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const override; 540b57cec5SDimitry Andric }; 550b57cec5SDimitry Andric } // namespace 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric AArch64::AArch64() { 580b57cec5SDimitry Andric copyRel = R_AARCH64_COPY; 590b57cec5SDimitry Andric relativeRel = R_AARCH64_RELATIVE; 600b57cec5SDimitry Andric iRelativeRel = R_AARCH64_IRELATIVE; 610b57cec5SDimitry Andric gotRel = R_AARCH64_GLOB_DAT; 620b57cec5SDimitry Andric noneRel = R_AARCH64_NONE; 630b57cec5SDimitry Andric pltRel = R_AARCH64_JUMP_SLOT; 640b57cec5SDimitry Andric symbolicRel = R_AARCH64_ABS64; 650b57cec5SDimitry Andric tlsDescRel = R_AARCH64_TLSDESC; 660b57cec5SDimitry Andric tlsGotRel = R_AARCH64_TLS_TPREL64; 670b57cec5SDimitry Andric pltHeaderSize = 32; 68*480093f4SDimitry Andric pltEntrySize = 16; 69*480093f4SDimitry Andric ipltEntrySize = 16; 700b57cec5SDimitry Andric defaultMaxPageSize = 65536; 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric // Align to the 2 MiB page size (known as a superpage or huge page). 730b57cec5SDimitry Andric // FreeBSD automatically promotes 2 MiB-aligned allocations. 740b57cec5SDimitry Andric defaultImageBase = 0x200000; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric needsThunks = true; 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric RelExpr AArch64::getRelExpr(RelType type, const Symbol &s, 800b57cec5SDimitry Andric const uint8_t *loc) const { 810b57cec5SDimitry Andric switch (type) { 8285868e8aSDimitry Andric case R_AARCH64_ABS16: 8385868e8aSDimitry Andric case R_AARCH64_ABS32: 8485868e8aSDimitry Andric case R_AARCH64_ABS64: 8585868e8aSDimitry Andric case R_AARCH64_ADD_ABS_LO12_NC: 8685868e8aSDimitry Andric case R_AARCH64_LDST128_ABS_LO12_NC: 8785868e8aSDimitry Andric case R_AARCH64_LDST16_ABS_LO12_NC: 8885868e8aSDimitry Andric case R_AARCH64_LDST32_ABS_LO12_NC: 8985868e8aSDimitry Andric case R_AARCH64_LDST64_ABS_LO12_NC: 9085868e8aSDimitry Andric case R_AARCH64_LDST8_ABS_LO12_NC: 9185868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G0: 9285868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G1: 9385868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G2: 9485868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G0: 9585868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G0_NC: 9685868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G1: 9785868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G1_NC: 9885868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G2: 9985868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G2_NC: 10085868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G3: 10185868e8aSDimitry Andric return R_ABS; 1020b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21: 1030b57cec5SDimitry Andric return R_AARCH64_TLSDESC_PAGE; 1040b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 1050b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 1060b57cec5SDimitry Andric return R_TLSDESC; 1070b57cec5SDimitry Andric case R_AARCH64_TLSDESC_CALL: 1080b57cec5SDimitry Andric return R_TLSDESC_CALL; 1090b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_HI12: 1100b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: 1110b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC: 1120b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC: 1130b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC: 1140b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC: 1150b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC: 11685868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0: 11785868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC: 11885868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1: 11985868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC: 12085868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G2: 1210b57cec5SDimitry Andric return R_TLS; 1220b57cec5SDimitry Andric case R_AARCH64_CALL26: 1230b57cec5SDimitry Andric case R_AARCH64_CONDBR19: 1240b57cec5SDimitry Andric case R_AARCH64_JUMP26: 1250b57cec5SDimitry Andric case R_AARCH64_TSTBR14: 1260b57cec5SDimitry Andric return R_PLT_PC; 1270b57cec5SDimitry Andric case R_AARCH64_PREL16: 1280b57cec5SDimitry Andric case R_AARCH64_PREL32: 1290b57cec5SDimitry Andric case R_AARCH64_PREL64: 1300b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_LO21: 1310b57cec5SDimitry Andric case R_AARCH64_LD_PREL_LO19: 13285868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0: 13385868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0_NC: 13485868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1: 13585868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1_NC: 13685868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2: 13785868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2_NC: 13885868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G3: 1390b57cec5SDimitry Andric return R_PC; 1400b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21: 1410b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21_NC: 1420b57cec5SDimitry Andric return R_AARCH64_PAGE_PC; 1430b57cec5SDimitry Andric case R_AARCH64_LD64_GOT_LO12_NC: 1440b57cec5SDimitry Andric case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 1450b57cec5SDimitry Andric return R_GOT; 1460b57cec5SDimitry Andric case R_AARCH64_ADR_GOT_PAGE: 1470b57cec5SDimitry Andric case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: 1480b57cec5SDimitry Andric return R_AARCH64_GOT_PAGE_PC; 1490b57cec5SDimitry Andric case R_AARCH64_NONE: 1500b57cec5SDimitry Andric return R_NONE; 1510b57cec5SDimitry Andric default: 15285868e8aSDimitry Andric error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 15385868e8aSDimitry Andric ") against symbol " + toString(s)); 15485868e8aSDimitry Andric return R_NONE; 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric } 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric RelExpr AArch64::adjustRelaxExpr(RelType type, const uint8_t *data, 1590b57cec5SDimitry Andric RelExpr expr) const { 1600b57cec5SDimitry Andric if (expr == R_RELAX_TLS_GD_TO_IE) { 1610b57cec5SDimitry Andric if (type == R_AARCH64_TLSDESC_ADR_PAGE21) 1620b57cec5SDimitry Andric return R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC; 1630b57cec5SDimitry Andric return R_RELAX_TLS_GD_TO_IE_ABS; 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric return expr; 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric bool AArch64::usesOnlyLowPageBits(RelType type) const { 1690b57cec5SDimitry Andric switch (type) { 1700b57cec5SDimitry Andric default: 1710b57cec5SDimitry Andric return false; 1720b57cec5SDimitry Andric case R_AARCH64_ADD_ABS_LO12_NC: 1730b57cec5SDimitry Andric case R_AARCH64_LD64_GOT_LO12_NC: 1740b57cec5SDimitry Andric case R_AARCH64_LDST128_ABS_LO12_NC: 1750b57cec5SDimitry Andric case R_AARCH64_LDST16_ABS_LO12_NC: 1760b57cec5SDimitry Andric case R_AARCH64_LDST32_ABS_LO12_NC: 1770b57cec5SDimitry Andric case R_AARCH64_LDST64_ABS_LO12_NC: 1780b57cec5SDimitry Andric case R_AARCH64_LDST8_ABS_LO12_NC: 1790b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 1800b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 1810b57cec5SDimitry Andric case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 1820b57cec5SDimitry Andric return true; 1830b57cec5SDimitry Andric } 1840b57cec5SDimitry Andric } 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric RelType AArch64::getDynRel(RelType type) const { 1870b57cec5SDimitry Andric if (type == R_AARCH64_ABS64) 1880b57cec5SDimitry Andric return type; 1890b57cec5SDimitry Andric return R_AARCH64_NONE; 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric void AArch64::writeGotPlt(uint8_t *buf, const Symbol &) const { 1930b57cec5SDimitry Andric write64le(buf, in.plt->getVA()); 1940b57cec5SDimitry Andric } 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric void AArch64::writePltHeader(uint8_t *buf) const { 1970b57cec5SDimitry Andric const uint8_t pltData[] = { 1980b57cec5SDimitry Andric 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! 1990b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) 2000b57cec5SDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] 2010b57cec5SDimitry Andric 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) 2020b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6, // br x17 2030b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5, // nop 2040b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5, // nop 2050b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5 // nop 2060b57cec5SDimitry Andric }; 2070b57cec5SDimitry Andric memcpy(buf, pltData, sizeof(pltData)); 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric uint64_t got = in.gotPlt->getVA(); 2100b57cec5SDimitry Andric uint64_t plt = in.plt->getVA(); 2110b57cec5SDimitry Andric relocateOne(buf + 4, R_AARCH64_ADR_PREL_PG_HI21, 2120b57cec5SDimitry Andric getAArch64Page(got + 16) - getAArch64Page(plt + 4)); 2130b57cec5SDimitry Andric relocateOne(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16); 2140b57cec5SDimitry Andric relocateOne(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16); 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 217*480093f4SDimitry Andric void AArch64::writePlt(uint8_t *buf, const Symbol &sym, 218*480093f4SDimitry Andric uint64_t pltEntryAddr) const { 2190b57cec5SDimitry Andric const uint8_t inst[] = { 2200b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) 2210b57cec5SDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] 2220b57cec5SDimitry Andric 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n])) 2230b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6 // br x17 2240b57cec5SDimitry Andric }; 2250b57cec5SDimitry Andric memcpy(buf, inst, sizeof(inst)); 2260b57cec5SDimitry Andric 227*480093f4SDimitry Andric uint64_t gotPltEntryAddr = sym.getGotPltVA(); 2280b57cec5SDimitry Andric relocateOne(buf, R_AARCH64_ADR_PREL_PG_HI21, 2290b57cec5SDimitry Andric getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr)); 2300b57cec5SDimitry Andric relocateOne(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr); 2310b57cec5SDimitry Andric relocateOne(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr); 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric bool AArch64::needsThunk(RelExpr expr, RelType type, const InputFile *file, 235*480093f4SDimitry Andric uint64_t branchAddr, const Symbol &s, 236*480093f4SDimitry Andric int64_t a) const { 237*480093f4SDimitry Andric // If s is an undefined weak symbol and does not have a PLT entry then it 238*480093f4SDimitry Andric // will be resolved as a branch to the next instruction. 239*480093f4SDimitry Andric if (s.isUndefWeak() && !s.isInPlt()) 240*480093f4SDimitry Andric return false; 2410b57cec5SDimitry Andric // ELF for the ARM 64-bit architecture, section Call and Jump relocations 2420b57cec5SDimitry Andric // only permits range extension thunks for R_AARCH64_CALL26 and 2430b57cec5SDimitry Andric // R_AARCH64_JUMP26 relocation types. 2440b57cec5SDimitry Andric if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26) 2450b57cec5SDimitry Andric return false; 246*480093f4SDimitry Andric uint64_t dst = expr == R_PLT_PC ? s.getPltVA() : s.getVA(a); 2470b57cec5SDimitry Andric return !inBranchRange(type, branchAddr, dst); 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric uint32_t AArch64::getThunkSectionSpacing() const { 2510b57cec5SDimitry Andric // See comment in Arch/ARM.cpp for a more detailed explanation of 2520b57cec5SDimitry Andric // getThunkSectionSpacing(). For AArch64 the only branches we are permitted to 2530b57cec5SDimitry Andric // Thunk have a range of +/- 128 MiB 2540b57cec5SDimitry Andric return (128 * 1024 * 1024) - 0x30000; 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric bool AArch64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 2580b57cec5SDimitry Andric if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26) 2590b57cec5SDimitry Andric return true; 2600b57cec5SDimitry Andric // The AArch64 call and unconditional branch instructions have a range of 2610b57cec5SDimitry Andric // +/- 128 MiB. 2620b57cec5SDimitry Andric uint64_t range = 128 * 1024 * 1024; 2630b57cec5SDimitry Andric if (dst > src) { 2640b57cec5SDimitry Andric // Immediate of branch is signed. 2650b57cec5SDimitry Andric range -= 4; 2660b57cec5SDimitry Andric return dst - src <= range; 2670b57cec5SDimitry Andric } 2680b57cec5SDimitry Andric return src - dst <= range; 2690b57cec5SDimitry Andric } 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric static void write32AArch64Addr(uint8_t *l, uint64_t imm) { 2720b57cec5SDimitry Andric uint32_t immLo = (imm & 0x3) << 29; 2730b57cec5SDimitry Andric uint32_t immHi = (imm & 0x1FFFFC) << 3; 2740b57cec5SDimitry Andric uint64_t mask = (0x3 << 29) | (0x1FFFFC << 3); 2750b57cec5SDimitry Andric write32le(l, (read32le(l) & ~mask) | immLo | immHi); 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric // Return the bits [Start, End] from Val shifted Start bits. 2790b57cec5SDimitry Andric // For instance, getBits(0xF0, 4, 8) returns 0xF. 2800b57cec5SDimitry Andric static uint64_t getBits(uint64_t val, int start, int end) { 2810b57cec5SDimitry Andric uint64_t mask = ((uint64_t)1 << (end + 1 - start)) - 1; 2820b57cec5SDimitry Andric return (val >> start) & mask; 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric static void or32le(uint8_t *p, int32_t v) { write32le(p, read32le(p) | v); } 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric // Update the immediate field in a AARCH64 ldr, str, and add instruction. 2880b57cec5SDimitry Andric static void or32AArch64Imm(uint8_t *l, uint64_t imm) { 2890b57cec5SDimitry Andric or32le(l, (imm & 0xFFF) << 10); 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric 29285868e8aSDimitry Andric // Update the immediate field in an AArch64 movk, movn or movz instruction 29385868e8aSDimitry Andric // for a signed relocation, and update the opcode of a movn or movz instruction 29485868e8aSDimitry Andric // to match the sign of the operand. 29585868e8aSDimitry Andric static void writeSMovWImm(uint8_t *loc, uint32_t imm) { 29685868e8aSDimitry Andric uint32_t inst = read32le(loc); 29785868e8aSDimitry Andric // Opcode field is bits 30, 29, with 10 = movz, 00 = movn and 11 = movk. 29885868e8aSDimitry Andric if (!(inst & (1 << 29))) { 29985868e8aSDimitry Andric // movn or movz. 30085868e8aSDimitry Andric if (imm & 0x10000) { 30185868e8aSDimitry Andric // Change opcode to movn, which takes an inverted operand. 30285868e8aSDimitry Andric imm ^= 0xFFFF; 30385868e8aSDimitry Andric inst &= ~(1 << 30); 30485868e8aSDimitry Andric } else { 30585868e8aSDimitry Andric // Change opcode to movz. 30685868e8aSDimitry Andric inst |= 1 << 30; 30785868e8aSDimitry Andric } 30885868e8aSDimitry Andric } 30985868e8aSDimitry Andric write32le(loc, inst | ((imm & 0xFFFF) << 5)); 31085868e8aSDimitry Andric } 31185868e8aSDimitry Andric 3120b57cec5SDimitry Andric void AArch64::relocateOne(uint8_t *loc, RelType type, uint64_t val) const { 3130b57cec5SDimitry Andric switch (type) { 3140b57cec5SDimitry Andric case R_AARCH64_ABS16: 3150b57cec5SDimitry Andric case R_AARCH64_PREL16: 3160b57cec5SDimitry Andric checkIntUInt(loc, val, 16, type); 3170b57cec5SDimitry Andric write16le(loc, val); 3180b57cec5SDimitry Andric break; 3190b57cec5SDimitry Andric case R_AARCH64_ABS32: 3200b57cec5SDimitry Andric case R_AARCH64_PREL32: 3210b57cec5SDimitry Andric checkIntUInt(loc, val, 32, type); 3220b57cec5SDimitry Andric write32le(loc, val); 3230b57cec5SDimitry Andric break; 3240b57cec5SDimitry Andric case R_AARCH64_ABS64: 3250b57cec5SDimitry Andric case R_AARCH64_PREL64: 3260b57cec5SDimitry Andric write64le(loc, val); 3270b57cec5SDimitry Andric break; 3280b57cec5SDimitry Andric case R_AARCH64_ADD_ABS_LO12_NC: 3290b57cec5SDimitry Andric or32AArch64Imm(loc, val); 3300b57cec5SDimitry Andric break; 3310b57cec5SDimitry Andric case R_AARCH64_ADR_GOT_PAGE: 3320b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21: 3330b57cec5SDimitry Andric case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: 3340b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21: 3350b57cec5SDimitry Andric checkInt(loc, val, 33, type); 3360b57cec5SDimitry Andric LLVM_FALLTHROUGH; 3370b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_PG_HI21_NC: 3380b57cec5SDimitry Andric write32AArch64Addr(loc, val >> 12); 3390b57cec5SDimitry Andric break; 3400b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_LO21: 3410b57cec5SDimitry Andric checkInt(loc, val, 21, type); 3420b57cec5SDimitry Andric write32AArch64Addr(loc, val); 3430b57cec5SDimitry Andric break; 3440b57cec5SDimitry Andric case R_AARCH64_JUMP26: 3450b57cec5SDimitry Andric // Normally we would just write the bits of the immediate field, however 3460b57cec5SDimitry Andric // when patching instructions for the cpu errata fix -fix-cortex-a53-843419 3470b57cec5SDimitry Andric // we want to replace a non-branch instruction with a branch immediate 3480b57cec5SDimitry Andric // instruction. By writing all the bits of the instruction including the 3490b57cec5SDimitry Andric // opcode and the immediate (0 001 | 01 imm26) we can do this 3500b57cec5SDimitry Andric // transformation by placing a R_AARCH64_JUMP26 relocation at the offset of 3510b57cec5SDimitry Andric // the instruction we want to patch. 3520b57cec5SDimitry Andric write32le(loc, 0x14000000); 3530b57cec5SDimitry Andric LLVM_FALLTHROUGH; 3540b57cec5SDimitry Andric case R_AARCH64_CALL26: 3550b57cec5SDimitry Andric checkInt(loc, val, 28, type); 3560b57cec5SDimitry Andric or32le(loc, (val & 0x0FFFFFFC) >> 2); 3570b57cec5SDimitry Andric break; 3580b57cec5SDimitry Andric case R_AARCH64_CONDBR19: 3590b57cec5SDimitry Andric case R_AARCH64_LD_PREL_LO19: 3600b57cec5SDimitry Andric checkAlignment(loc, val, 4, type); 3610b57cec5SDimitry Andric checkInt(loc, val, 21, type); 3620b57cec5SDimitry Andric or32le(loc, (val & 0x1FFFFC) << 3); 3630b57cec5SDimitry Andric break; 3640b57cec5SDimitry Andric case R_AARCH64_LDST8_ABS_LO12_NC: 3650b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC: 3660b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 0, 11)); 3670b57cec5SDimitry Andric break; 3680b57cec5SDimitry Andric case R_AARCH64_LDST16_ABS_LO12_NC: 3690b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC: 3700b57cec5SDimitry Andric checkAlignment(loc, val, 2, type); 3710b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 1, 11)); 3720b57cec5SDimitry Andric break; 3730b57cec5SDimitry Andric case R_AARCH64_LDST32_ABS_LO12_NC: 3740b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC: 3750b57cec5SDimitry Andric checkAlignment(loc, val, 4, type); 3760b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 2, 11)); 3770b57cec5SDimitry Andric break; 3780b57cec5SDimitry Andric case R_AARCH64_LDST64_ABS_LO12_NC: 3790b57cec5SDimitry Andric case R_AARCH64_LD64_GOT_LO12_NC: 3800b57cec5SDimitry Andric case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 3810b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC: 3820b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 3830b57cec5SDimitry Andric checkAlignment(loc, val, 8, type); 3840b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 3, 11)); 3850b57cec5SDimitry Andric break; 3860b57cec5SDimitry Andric case R_AARCH64_LDST128_ABS_LO12_NC: 3870b57cec5SDimitry Andric case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC: 3880b57cec5SDimitry Andric checkAlignment(loc, val, 16, type); 3890b57cec5SDimitry Andric or32AArch64Imm(loc, getBits(val, 4, 11)); 3900b57cec5SDimitry Andric break; 39185868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G0: 39285868e8aSDimitry Andric checkUInt(loc, val, 16, type); 39385868e8aSDimitry Andric LLVM_FALLTHROUGH; 3940b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G0_NC: 3950b57cec5SDimitry Andric or32le(loc, (val & 0xFFFF) << 5); 3960b57cec5SDimitry Andric break; 39785868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G1: 39885868e8aSDimitry Andric checkUInt(loc, val, 32, type); 39985868e8aSDimitry Andric LLVM_FALLTHROUGH; 4000b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G1_NC: 4010b57cec5SDimitry Andric or32le(loc, (val & 0xFFFF0000) >> 11); 4020b57cec5SDimitry Andric break; 40385868e8aSDimitry Andric case R_AARCH64_MOVW_UABS_G2: 40485868e8aSDimitry Andric checkUInt(loc, val, 48, type); 40585868e8aSDimitry Andric LLVM_FALLTHROUGH; 4060b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G2_NC: 4070b57cec5SDimitry Andric or32le(loc, (val & 0xFFFF00000000) >> 27); 4080b57cec5SDimitry Andric break; 4090b57cec5SDimitry Andric case R_AARCH64_MOVW_UABS_G3: 4100b57cec5SDimitry Andric or32le(loc, (val & 0xFFFF000000000000) >> 43); 4110b57cec5SDimitry Andric break; 41285868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0: 41385868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G0: 41485868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0: 41585868e8aSDimitry Andric checkInt(loc, val, 17, type); 41685868e8aSDimitry Andric LLVM_FALLTHROUGH; 41785868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G0_NC: 41885868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC: 41985868e8aSDimitry Andric writeSMovWImm(loc, val); 42085868e8aSDimitry Andric break; 42185868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1: 42285868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G1: 42385868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1: 42485868e8aSDimitry Andric checkInt(loc, val, 33, type); 42585868e8aSDimitry Andric LLVM_FALLTHROUGH; 42685868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G1_NC: 42785868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC: 42885868e8aSDimitry Andric writeSMovWImm(loc, val >> 16); 42985868e8aSDimitry Andric break; 43085868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2: 43185868e8aSDimitry Andric case R_AARCH64_MOVW_SABS_G2: 43285868e8aSDimitry Andric case R_AARCH64_TLSLE_MOVW_TPREL_G2: 43385868e8aSDimitry Andric checkInt(loc, val, 49, type); 43485868e8aSDimitry Andric LLVM_FALLTHROUGH; 43585868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G2_NC: 43685868e8aSDimitry Andric writeSMovWImm(loc, val >> 32); 43785868e8aSDimitry Andric break; 43885868e8aSDimitry Andric case R_AARCH64_MOVW_PREL_G3: 43985868e8aSDimitry Andric writeSMovWImm(loc, val >> 48); 44085868e8aSDimitry Andric break; 4410b57cec5SDimitry Andric case R_AARCH64_TSTBR14: 4420b57cec5SDimitry Andric checkInt(loc, val, 16, type); 4430b57cec5SDimitry Andric or32le(loc, (val & 0xFFFC) << 3); 4440b57cec5SDimitry Andric break; 4450b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_HI12: 4460b57cec5SDimitry Andric checkUInt(loc, val, 24, type); 4470b57cec5SDimitry Andric or32AArch64Imm(loc, val >> 12); 4480b57cec5SDimitry Andric break; 4490b57cec5SDimitry Andric case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: 4500b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 4510b57cec5SDimitry Andric or32AArch64Imm(loc, val); 4520b57cec5SDimitry Andric break; 4530b57cec5SDimitry Andric default: 45485868e8aSDimitry Andric llvm_unreachable("unknown relocation"); 4550b57cec5SDimitry Andric } 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric void AArch64::relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const { 4590b57cec5SDimitry Andric // TLSDESC Global-Dynamic relocation are in the form: 4600b57cec5SDimitry Andric // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21] 4610b57cec5SDimitry Andric // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12] 4620b57cec5SDimitry Andric // add x0, x0, :tlsdesc_los:v [R_AARCH64_TLSDESC_ADD_LO12] 4630b57cec5SDimitry Andric // .tlsdesccall [R_AARCH64_TLSDESC_CALL] 4640b57cec5SDimitry Andric // blr x1 4650b57cec5SDimitry Andric // And it can optimized to: 4660b57cec5SDimitry Andric // movz x0, #0x0, lsl #16 4670b57cec5SDimitry Andric // movk x0, #0x10 4680b57cec5SDimitry Andric // nop 4690b57cec5SDimitry Andric // nop 4700b57cec5SDimitry Andric checkUInt(loc, val, 32, type); 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric switch (type) { 4730b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 4740b57cec5SDimitry Andric case R_AARCH64_TLSDESC_CALL: 4750b57cec5SDimitry Andric write32le(loc, 0xd503201f); // nop 4760b57cec5SDimitry Andric return; 4770b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21: 4780b57cec5SDimitry Andric write32le(loc, 0xd2a00000 | (((val >> 16) & 0xffff) << 5)); // movz 4790b57cec5SDimitry Andric return; 4800b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 4810b57cec5SDimitry Andric write32le(loc, 0xf2800000 | ((val & 0xffff) << 5)); // movk 4820b57cec5SDimitry Andric return; 4830b57cec5SDimitry Andric default: 4840b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 4850b57cec5SDimitry Andric } 4860b57cec5SDimitry Andric } 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric void AArch64::relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const { 4890b57cec5SDimitry Andric // TLSDESC Global-Dynamic relocation are in the form: 4900b57cec5SDimitry Andric // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21] 4910b57cec5SDimitry Andric // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12] 4920b57cec5SDimitry Andric // add x0, x0, :tlsdesc_los:v [R_AARCH64_TLSDESC_ADD_LO12] 4930b57cec5SDimitry Andric // .tlsdesccall [R_AARCH64_TLSDESC_CALL] 4940b57cec5SDimitry Andric // blr x1 4950b57cec5SDimitry Andric // And it can optimized to: 4960b57cec5SDimitry Andric // adrp x0, :gottprel:v 4970b57cec5SDimitry Andric // ldr x0, [x0, :gottprel_lo12:v] 4980b57cec5SDimitry Andric // nop 4990b57cec5SDimitry Andric // nop 5000b57cec5SDimitry Andric 5010b57cec5SDimitry Andric switch (type) { 5020b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADD_LO12: 5030b57cec5SDimitry Andric case R_AARCH64_TLSDESC_CALL: 5040b57cec5SDimitry Andric write32le(loc, 0xd503201f); // nop 5050b57cec5SDimitry Andric break; 5060b57cec5SDimitry Andric case R_AARCH64_TLSDESC_ADR_PAGE21: 5070b57cec5SDimitry Andric write32le(loc, 0x90000000); // adrp 5080b57cec5SDimitry Andric relocateOne(loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, val); 5090b57cec5SDimitry Andric break; 5100b57cec5SDimitry Andric case R_AARCH64_TLSDESC_LD64_LO12: 5110b57cec5SDimitry Andric write32le(loc, 0xf9400000); // ldr 5120b57cec5SDimitry Andric relocateOne(loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, val); 5130b57cec5SDimitry Andric break; 5140b57cec5SDimitry Andric default: 5150b57cec5SDimitry Andric llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 5160b57cec5SDimitry Andric } 5170b57cec5SDimitry Andric } 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric void AArch64::relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const { 5200b57cec5SDimitry Andric checkUInt(loc, val, 32, type); 5210b57cec5SDimitry Andric 5220b57cec5SDimitry Andric if (type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) { 5230b57cec5SDimitry Andric // Generate MOVZ. 5240b57cec5SDimitry Andric uint32_t regNo = read32le(loc) & 0x1f; 5250b57cec5SDimitry Andric write32le(loc, (0xd2a00000 | regNo) | (((val >> 16) & 0xffff) << 5)); 5260b57cec5SDimitry Andric return; 5270b57cec5SDimitry Andric } 5280b57cec5SDimitry Andric if (type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) { 5290b57cec5SDimitry Andric // Generate MOVK. 5300b57cec5SDimitry Andric uint32_t regNo = read32le(loc) & 0x1f; 5310b57cec5SDimitry Andric write32le(loc, (0xf2800000 | regNo) | ((val & 0xffff) << 5)); 5320b57cec5SDimitry Andric return; 5330b57cec5SDimitry Andric } 5340b57cec5SDimitry Andric llvm_unreachable("invalid relocation for TLS IE to LE relaxation"); 5350b57cec5SDimitry Andric } 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric // AArch64 may use security features in variant PLT sequences. These are: 5380b57cec5SDimitry Andric // Pointer Authentication (PAC), introduced in armv8.3-a and Branch Target 5390b57cec5SDimitry Andric // Indicator (BTI) introduced in armv8.5-a. The additional instructions used 5400b57cec5SDimitry Andric // in the variant Plt sequences are encoded in the Hint space so they can be 5410b57cec5SDimitry Andric // deployed on older architectures, which treat the instructions as a nop. 5420b57cec5SDimitry Andric // PAC and BTI can be combined leading to the following combinations: 5430b57cec5SDimitry Andric // writePltHeader 5440b57cec5SDimitry Andric // writePltHeaderBti (no PAC Header needed) 5450b57cec5SDimitry Andric // writePlt 5460b57cec5SDimitry Andric // writePltBti (BTI only) 5470b57cec5SDimitry Andric // writePltPac (PAC only) 5480b57cec5SDimitry Andric // writePltBtiPac (BTI and PAC) 5490b57cec5SDimitry Andric // 5500b57cec5SDimitry Andric // When PAC is enabled the dynamic loader encrypts the address that it places 5510b57cec5SDimitry Andric // in the .got.plt using the pacia1716 instruction which encrypts the value in 5520b57cec5SDimitry Andric // x17 using the modifier in x16. The static linker places autia1716 before the 5530b57cec5SDimitry Andric // indirect branch to x17 to authenticate the address in x17 with the modifier 5540b57cec5SDimitry Andric // in x16. This makes it more difficult for an attacker to modify the value in 5550b57cec5SDimitry Andric // the .got.plt. 5560b57cec5SDimitry Andric // 5570b57cec5SDimitry Andric // When BTI is enabled all indirect branches must land on a bti instruction. 5580b57cec5SDimitry Andric // The static linker must place a bti instruction at the start of any PLT entry 5590b57cec5SDimitry Andric // that may be the target of an indirect branch. As the PLT entries call the 5600b57cec5SDimitry Andric // lazy resolver indirectly this must have a bti instruction at start. In 5610b57cec5SDimitry Andric // general a bti instruction is not needed for a PLT entry as indirect calls 5620b57cec5SDimitry Andric // are resolved to the function address and not the PLT entry for the function. 5630b57cec5SDimitry Andric // There are a small number of cases where the PLT address can escape, such as 5640b57cec5SDimitry Andric // taking the address of a function or ifunc via a non got-generating 5650b57cec5SDimitry Andric // relocation, and a shared library refers to that symbol. 5660b57cec5SDimitry Andric // 5670b57cec5SDimitry Andric // We use the bti c variant of the instruction which permits indirect branches 5680b57cec5SDimitry Andric // (br) via x16/x17 and indirect function calls (blr) via any register. The ABI 5690b57cec5SDimitry Andric // guarantees that all indirect branches from code requiring BTI protection 5700b57cec5SDimitry Andric // will go via x16/x17 5710b57cec5SDimitry Andric 5720b57cec5SDimitry Andric namespace { 5730b57cec5SDimitry Andric class AArch64BtiPac final : public AArch64 { 5740b57cec5SDimitry Andric public: 5750b57cec5SDimitry Andric AArch64BtiPac(); 5760b57cec5SDimitry Andric void writePltHeader(uint8_t *buf) const override; 577*480093f4SDimitry Andric void writePlt(uint8_t *buf, const Symbol &sym, 578*480093f4SDimitry Andric uint64_t pltEntryAddr) const override; 5790b57cec5SDimitry Andric 5800b57cec5SDimitry Andric private: 5810b57cec5SDimitry Andric bool btiHeader; // bti instruction needed in PLT Header 5820b57cec5SDimitry Andric bool btiEntry; // bti instruction needed in PLT Entry 5830b57cec5SDimitry Andric bool pacEntry; // autia1716 instruction needed in PLT Entry 5840b57cec5SDimitry Andric }; 5850b57cec5SDimitry Andric } // namespace 5860b57cec5SDimitry Andric 5870b57cec5SDimitry Andric AArch64BtiPac::AArch64BtiPac() { 5880b57cec5SDimitry Andric btiHeader = (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI); 5890b57cec5SDimitry Andric // A BTI (Branch Target Indicator) Plt Entry is only required if the 5900b57cec5SDimitry Andric // address of the PLT entry can be taken by the program, which permits an 5910b57cec5SDimitry Andric // indirect jump to the PLT entry. This can happen when the address 5920b57cec5SDimitry Andric // of the PLT entry for a function is canonicalised due to the address of 5930b57cec5SDimitry Andric // the function in an executable being taken by a shared library. 5940b57cec5SDimitry Andric // FIXME: There is a potential optimization to omit the BTI if we detect 5950b57cec5SDimitry Andric // that the address of the PLT entry isn't taken. 5960b57cec5SDimitry Andric btiEntry = btiHeader && !config->shared; 5970b57cec5SDimitry Andric pacEntry = (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_PAC); 5980b57cec5SDimitry Andric 599*480093f4SDimitry Andric if (btiEntry || pacEntry) { 6000b57cec5SDimitry Andric pltEntrySize = 24; 601*480093f4SDimitry Andric ipltEntrySize = 24; 602*480093f4SDimitry Andric } 6030b57cec5SDimitry Andric } 6040b57cec5SDimitry Andric 6050b57cec5SDimitry Andric void AArch64BtiPac::writePltHeader(uint8_t *buf) const { 6060b57cec5SDimitry Andric const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c 6070b57cec5SDimitry Andric const uint8_t pltData[] = { 6080b57cec5SDimitry Andric 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! 6090b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) 6100b57cec5SDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] 6110b57cec5SDimitry Andric 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) 6120b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6, // br x17 6130b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5, // nop 6140b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5 // nop 6150b57cec5SDimitry Andric }; 6160b57cec5SDimitry Andric const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop 6170b57cec5SDimitry Andric 6180b57cec5SDimitry Andric uint64_t got = in.gotPlt->getVA(); 6190b57cec5SDimitry Andric uint64_t plt = in.plt->getVA(); 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric if (btiHeader) { 6220b57cec5SDimitry Andric // PltHeader is called indirectly by plt[N]. Prefix pltData with a BTI C 6230b57cec5SDimitry Andric // instruction. 6240b57cec5SDimitry Andric memcpy(buf, btiData, sizeof(btiData)); 6250b57cec5SDimitry Andric buf += sizeof(btiData); 6260b57cec5SDimitry Andric plt += sizeof(btiData); 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric memcpy(buf, pltData, sizeof(pltData)); 6290b57cec5SDimitry Andric 6300b57cec5SDimitry Andric relocateOne(buf + 4, R_AARCH64_ADR_PREL_PG_HI21, 6310b57cec5SDimitry Andric getAArch64Page(got + 16) - getAArch64Page(plt + 8)); 6320b57cec5SDimitry Andric relocateOne(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16); 6330b57cec5SDimitry Andric relocateOne(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16); 6340b57cec5SDimitry Andric if (!btiHeader) 6350b57cec5SDimitry Andric // We didn't add the BTI c instruction so round out size with NOP. 6360b57cec5SDimitry Andric memcpy(buf + sizeof(pltData), nopData, sizeof(nopData)); 6370b57cec5SDimitry Andric } 6380b57cec5SDimitry Andric 639*480093f4SDimitry Andric void AArch64BtiPac::writePlt(uint8_t *buf, const Symbol &sym, 640*480093f4SDimitry Andric uint64_t pltEntryAddr) const { 6410b57cec5SDimitry Andric // The PLT entry is of the form: 6420b57cec5SDimitry Andric // [btiData] addrInst (pacBr | stdBr) [nopData] 6430b57cec5SDimitry Andric const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c 6440b57cec5SDimitry Andric const uint8_t addrInst[] = { 6450b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) 6460b57cec5SDimitry Andric 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] 6470b57cec5SDimitry Andric 0x10, 0x02, 0x00, 0x91 // add x16, x16, Offset(&(.plt.got[n])) 6480b57cec5SDimitry Andric }; 6490b57cec5SDimitry Andric const uint8_t pacBr[] = { 6500b57cec5SDimitry Andric 0x9f, 0x21, 0x03, 0xd5, // autia1716 6510b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6 // br x17 6520b57cec5SDimitry Andric }; 6530b57cec5SDimitry Andric const uint8_t stdBr[] = { 6540b57cec5SDimitry Andric 0x20, 0x02, 0x1f, 0xd6, // br x17 6550b57cec5SDimitry Andric 0x1f, 0x20, 0x03, 0xd5 // nop 6560b57cec5SDimitry Andric }; 6570b57cec5SDimitry Andric const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andric if (btiEntry) { 6600b57cec5SDimitry Andric memcpy(buf, btiData, sizeof(btiData)); 6610b57cec5SDimitry Andric buf += sizeof(btiData); 6620b57cec5SDimitry Andric pltEntryAddr += sizeof(btiData); 6630b57cec5SDimitry Andric } 6640b57cec5SDimitry Andric 665*480093f4SDimitry Andric uint64_t gotPltEntryAddr = sym.getGotPltVA(); 6660b57cec5SDimitry Andric memcpy(buf, addrInst, sizeof(addrInst)); 6670b57cec5SDimitry Andric relocateOne(buf, R_AARCH64_ADR_PREL_PG_HI21, 6680b57cec5SDimitry Andric getAArch64Page(gotPltEntryAddr) - 6690b57cec5SDimitry Andric getAArch64Page(pltEntryAddr)); 6700b57cec5SDimitry Andric relocateOne(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr); 6710b57cec5SDimitry Andric relocateOne(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr); 6720b57cec5SDimitry Andric 6730b57cec5SDimitry Andric if (pacEntry) 6740b57cec5SDimitry Andric memcpy(buf + sizeof(addrInst), pacBr, sizeof(pacBr)); 6750b57cec5SDimitry Andric else 6760b57cec5SDimitry Andric memcpy(buf + sizeof(addrInst), stdBr, sizeof(stdBr)); 6770b57cec5SDimitry Andric if (!btiEntry) 6780b57cec5SDimitry Andric // We didn't add the BTI c instruction so round out size with NOP. 6790b57cec5SDimitry Andric memcpy(buf + sizeof(addrInst) + sizeof(stdBr), nopData, sizeof(nopData)); 6800b57cec5SDimitry Andric } 6810b57cec5SDimitry Andric 6820b57cec5SDimitry Andric static TargetInfo *getTargetInfo() { 6830b57cec5SDimitry Andric if (config->andFeatures & (GNU_PROPERTY_AARCH64_FEATURE_1_BTI | 6840b57cec5SDimitry Andric GNU_PROPERTY_AARCH64_FEATURE_1_PAC)) { 6850b57cec5SDimitry Andric static AArch64BtiPac t; 6860b57cec5SDimitry Andric return &t; 6870b57cec5SDimitry Andric } 6880b57cec5SDimitry Andric static AArch64 t; 6890b57cec5SDimitry Andric return &t; 6900b57cec5SDimitry Andric } 6910b57cec5SDimitry Andric 69285868e8aSDimitry Andric TargetInfo *getAArch64TargetInfo() { return getTargetInfo(); } 69385868e8aSDimitry Andric 69485868e8aSDimitry Andric } // namespace elf 69585868e8aSDimitry Andric } // namespace lld 696