1 //===- AArch64.cpp --------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "Symbols.h" 10 #include "SyntheticSections.h" 11 #include "Target.h" 12 #include "Thunks.h" 13 #include "lld/Common/ErrorHandler.h" 14 #include "llvm/Object/ELF.h" 15 #include "llvm/Support/Endian.h" 16 17 using namespace llvm; 18 using namespace llvm::support::endian; 19 using namespace llvm::ELF; 20 using namespace lld; 21 using namespace lld::elf; 22 23 // Page(Expr) is the page address of the expression Expr, defined 24 // as (Expr & ~0xFFF). (This applies even if the machine page size 25 // supported by the platform has a different value.) 26 uint64_t elf::getAArch64Page(uint64_t expr) { 27 return expr & ~static_cast<uint64_t>(0xFFF); 28 } 29 30 namespace { 31 class AArch64 : public TargetInfo { 32 public: 33 AArch64(); 34 RelExpr getRelExpr(RelType type, const Symbol &s, 35 const uint8_t *loc) const override; 36 RelType getDynRel(RelType type) const override; 37 int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override; 38 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 39 void writePltHeader(uint8_t *buf) const override; 40 void writePlt(uint8_t *buf, const Symbol &sym, 41 uint64_t pltEntryAddr) const override; 42 bool needsThunk(RelExpr expr, RelType type, const InputFile *file, 43 uint64_t branchAddr, const Symbol &s, 44 int64_t a) const override; 45 uint32_t getThunkSectionSpacing() const override; 46 bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override; 47 bool usesOnlyLowPageBits(RelType type) const override; 48 void relocate(uint8_t *loc, const Relocation &rel, 49 uint64_t val) const override; 50 RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override; 51 void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 52 uint64_t val) const override; 53 void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 54 uint64_t val) const override; 55 void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 56 uint64_t val) const override; 57 }; 58 } // namespace 59 60 AArch64::AArch64() { 61 copyRel = R_AARCH64_COPY; 62 relativeRel = R_AARCH64_RELATIVE; 63 iRelativeRel = R_AARCH64_IRELATIVE; 64 gotRel = R_AARCH64_GLOB_DAT; 65 pltRel = R_AARCH64_JUMP_SLOT; 66 symbolicRel = R_AARCH64_ABS64; 67 tlsDescRel = R_AARCH64_TLSDESC; 68 tlsGotRel = R_AARCH64_TLS_TPREL64; 69 pltHeaderSize = 32; 70 pltEntrySize = 16; 71 ipltEntrySize = 16; 72 defaultMaxPageSize = 65536; 73 74 // Align to the 2 MiB page size (known as a superpage or huge page). 75 // FreeBSD automatically promotes 2 MiB-aligned allocations. 76 defaultImageBase = 0x200000; 77 78 needsThunks = true; 79 } 80 81 RelExpr AArch64::getRelExpr(RelType type, const Symbol &s, 82 const uint8_t *loc) const { 83 switch (type) { 84 case R_AARCH64_ABS16: 85 case R_AARCH64_ABS32: 86 case R_AARCH64_ABS64: 87 case R_AARCH64_ADD_ABS_LO12_NC: 88 case R_AARCH64_LDST128_ABS_LO12_NC: 89 case R_AARCH64_LDST16_ABS_LO12_NC: 90 case R_AARCH64_LDST32_ABS_LO12_NC: 91 case R_AARCH64_LDST64_ABS_LO12_NC: 92 case R_AARCH64_LDST8_ABS_LO12_NC: 93 case R_AARCH64_MOVW_SABS_G0: 94 case R_AARCH64_MOVW_SABS_G1: 95 case R_AARCH64_MOVW_SABS_G2: 96 case R_AARCH64_MOVW_UABS_G0: 97 case R_AARCH64_MOVW_UABS_G0_NC: 98 case R_AARCH64_MOVW_UABS_G1: 99 case R_AARCH64_MOVW_UABS_G1_NC: 100 case R_AARCH64_MOVW_UABS_G2: 101 case R_AARCH64_MOVW_UABS_G2_NC: 102 case R_AARCH64_MOVW_UABS_G3: 103 return R_ABS; 104 case R_AARCH64_TLSDESC_ADR_PAGE21: 105 return R_AARCH64_TLSDESC_PAGE; 106 case R_AARCH64_TLSDESC_LD64_LO12: 107 case R_AARCH64_TLSDESC_ADD_LO12: 108 return R_TLSDESC; 109 case R_AARCH64_TLSDESC_CALL: 110 return R_TLSDESC_CALL; 111 case R_AARCH64_TLSLE_ADD_TPREL_HI12: 112 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: 113 case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC: 114 case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC: 115 case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC: 116 case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC: 117 case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC: 118 case R_AARCH64_TLSLE_MOVW_TPREL_G0: 119 case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC: 120 case R_AARCH64_TLSLE_MOVW_TPREL_G1: 121 case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC: 122 case R_AARCH64_TLSLE_MOVW_TPREL_G2: 123 return R_TPREL; 124 case R_AARCH64_CALL26: 125 case R_AARCH64_CONDBR19: 126 case R_AARCH64_JUMP26: 127 case R_AARCH64_TSTBR14: 128 case R_AARCH64_PLT32: 129 return R_PLT_PC; 130 case R_AARCH64_PREL16: 131 case R_AARCH64_PREL32: 132 case R_AARCH64_PREL64: 133 case R_AARCH64_ADR_PREL_LO21: 134 case R_AARCH64_LD_PREL_LO19: 135 case R_AARCH64_MOVW_PREL_G0: 136 case R_AARCH64_MOVW_PREL_G0_NC: 137 case R_AARCH64_MOVW_PREL_G1: 138 case R_AARCH64_MOVW_PREL_G1_NC: 139 case R_AARCH64_MOVW_PREL_G2: 140 case R_AARCH64_MOVW_PREL_G2_NC: 141 case R_AARCH64_MOVW_PREL_G3: 142 return R_PC; 143 case R_AARCH64_ADR_PREL_PG_HI21: 144 case R_AARCH64_ADR_PREL_PG_HI21_NC: 145 return R_AARCH64_PAGE_PC; 146 case R_AARCH64_LD64_GOT_LO12_NC: 147 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 148 return R_GOT; 149 case R_AARCH64_LD64_GOTPAGE_LO15: 150 return R_AARCH64_GOT_PAGE; 151 case R_AARCH64_ADR_GOT_PAGE: 152 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: 153 return R_AARCH64_GOT_PAGE_PC; 154 case R_AARCH64_NONE: 155 return R_NONE; 156 default: 157 error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 158 ") against symbol " + toString(s)); 159 return R_NONE; 160 } 161 } 162 163 RelExpr AArch64::adjustTlsExpr(RelType type, RelExpr expr) const { 164 if (expr == R_RELAX_TLS_GD_TO_IE) { 165 if (type == R_AARCH64_TLSDESC_ADR_PAGE21) 166 return R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC; 167 return R_RELAX_TLS_GD_TO_IE_ABS; 168 } 169 return expr; 170 } 171 172 bool AArch64::usesOnlyLowPageBits(RelType type) const { 173 switch (type) { 174 default: 175 return false; 176 case R_AARCH64_ADD_ABS_LO12_NC: 177 case R_AARCH64_LD64_GOT_LO12_NC: 178 case R_AARCH64_LDST128_ABS_LO12_NC: 179 case R_AARCH64_LDST16_ABS_LO12_NC: 180 case R_AARCH64_LDST32_ABS_LO12_NC: 181 case R_AARCH64_LDST64_ABS_LO12_NC: 182 case R_AARCH64_LDST8_ABS_LO12_NC: 183 case R_AARCH64_TLSDESC_ADD_LO12: 184 case R_AARCH64_TLSDESC_LD64_LO12: 185 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 186 return true; 187 } 188 } 189 190 RelType AArch64::getDynRel(RelType type) const { 191 if (type == R_AARCH64_ABS64) 192 return type; 193 return R_AARCH64_NONE; 194 } 195 196 int64_t AArch64::getImplicitAddend(const uint8_t *buf, RelType type) const { 197 switch (type) { 198 case R_AARCH64_TLSDESC: 199 return read64(buf + 8); 200 default: 201 internalLinkerError(getErrorLocation(buf), 202 "cannot read addend for relocation " + toString(type)); 203 return 0; 204 } 205 } 206 207 void AArch64::writeGotPlt(uint8_t *buf, const Symbol &) const { 208 write64(buf, in.plt->getVA()); 209 } 210 211 void AArch64::writePltHeader(uint8_t *buf) const { 212 const uint8_t pltData[] = { 213 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! 214 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) 215 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] 216 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) 217 0x20, 0x02, 0x1f, 0xd6, // br x17 218 0x1f, 0x20, 0x03, 0xd5, // nop 219 0x1f, 0x20, 0x03, 0xd5, // nop 220 0x1f, 0x20, 0x03, 0xd5 // nop 221 }; 222 memcpy(buf, pltData, sizeof(pltData)); 223 224 uint64_t got = in.gotPlt->getVA(); 225 uint64_t plt = in.plt->getVA(); 226 relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21, 227 getAArch64Page(got + 16) - getAArch64Page(plt + 4)); 228 relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16); 229 relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16); 230 } 231 232 void AArch64::writePlt(uint8_t *buf, const Symbol &sym, 233 uint64_t pltEntryAddr) const { 234 const uint8_t inst[] = { 235 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) 236 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] 237 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n])) 238 0x20, 0x02, 0x1f, 0xd6 // br x17 239 }; 240 memcpy(buf, inst, sizeof(inst)); 241 242 uint64_t gotPltEntryAddr = sym.getGotPltVA(); 243 relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21, 244 getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr)); 245 relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr); 246 relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr); 247 } 248 249 bool AArch64::needsThunk(RelExpr expr, RelType type, const InputFile *file, 250 uint64_t branchAddr, const Symbol &s, 251 int64_t a) const { 252 // If s is an undefined weak symbol and does not have a PLT entry then it 253 // will be resolved as a branch to the next instruction. 254 if (s.isUndefWeak() && !s.isInPlt()) 255 return false; 256 // ELF for the ARM 64-bit architecture, section Call and Jump relocations 257 // only permits range extension thunks for R_AARCH64_CALL26 and 258 // R_AARCH64_JUMP26 relocation types. 259 if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 && 260 type != R_AARCH64_PLT32) 261 return false; 262 uint64_t dst = expr == R_PLT_PC ? s.getPltVA() : s.getVA(a); 263 return !inBranchRange(type, branchAddr, dst); 264 } 265 266 uint32_t AArch64::getThunkSectionSpacing() const { 267 // See comment in Arch/ARM.cpp for a more detailed explanation of 268 // getThunkSectionSpacing(). For AArch64 the only branches we are permitted to 269 // Thunk have a range of +/- 128 MiB 270 return (128 * 1024 * 1024) - 0x30000; 271 } 272 273 bool AArch64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 274 if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 && 275 type != R_AARCH64_PLT32) 276 return true; 277 // The AArch64 call and unconditional branch instructions have a range of 278 // +/- 128 MiB. The PLT32 relocation supports a range up to +/- 2 GiB. 279 uint64_t range = 280 type == R_AARCH64_PLT32 ? (UINT64_C(1) << 31) : (128 * 1024 * 1024); 281 if (dst > src) { 282 // Immediate of branch is signed. 283 range -= 4; 284 return dst - src <= range; 285 } 286 return src - dst <= range; 287 } 288 289 static void write32AArch64Addr(uint8_t *l, uint64_t imm) { 290 uint32_t immLo = (imm & 0x3) << 29; 291 uint32_t immHi = (imm & 0x1FFFFC) << 3; 292 uint64_t mask = (0x3 << 29) | (0x1FFFFC << 3); 293 write32le(l, (read32le(l) & ~mask) | immLo | immHi); 294 } 295 296 // Return the bits [Start, End] from Val shifted Start bits. 297 // For instance, getBits(0xF0, 4, 8) returns 0xF. 298 static uint64_t getBits(uint64_t val, int start, int end) { 299 uint64_t mask = ((uint64_t)1 << (end + 1 - start)) - 1; 300 return (val >> start) & mask; 301 } 302 303 static void or32le(uint8_t *p, int32_t v) { write32le(p, read32le(p) | v); } 304 305 // Update the immediate field in a AARCH64 ldr, str, and add instruction. 306 static void or32AArch64Imm(uint8_t *l, uint64_t imm) { 307 or32le(l, (imm & 0xFFF) << 10); 308 } 309 310 // Update the immediate field in an AArch64 movk, movn or movz instruction 311 // for a signed relocation, and update the opcode of a movn or movz instruction 312 // to match the sign of the operand. 313 static void writeSMovWImm(uint8_t *loc, uint32_t imm) { 314 uint32_t inst = read32le(loc); 315 // Opcode field is bits 30, 29, with 10 = movz, 00 = movn and 11 = movk. 316 if (!(inst & (1 << 29))) { 317 // movn or movz. 318 if (imm & 0x10000) { 319 // Change opcode to movn, which takes an inverted operand. 320 imm ^= 0xFFFF; 321 inst &= ~(1 << 30); 322 } else { 323 // Change opcode to movz. 324 inst |= 1 << 30; 325 } 326 } 327 write32le(loc, inst | ((imm & 0xFFFF) << 5)); 328 } 329 330 void AArch64::relocate(uint8_t *loc, const Relocation &rel, 331 uint64_t val) const { 332 switch (rel.type) { 333 case R_AARCH64_ABS16: 334 case R_AARCH64_PREL16: 335 checkIntUInt(loc, val, 16, rel); 336 write16(loc, val); 337 break; 338 case R_AARCH64_ABS32: 339 case R_AARCH64_PREL32: 340 checkIntUInt(loc, val, 32, rel); 341 write32(loc, val); 342 break; 343 case R_AARCH64_PLT32: 344 checkInt(loc, val, 32, rel); 345 write32(loc, val); 346 break; 347 case R_AARCH64_ABS64: 348 case R_AARCH64_PREL64: 349 write64(loc, val); 350 break; 351 case R_AARCH64_ADD_ABS_LO12_NC: 352 or32AArch64Imm(loc, val); 353 break; 354 case R_AARCH64_ADR_GOT_PAGE: 355 case R_AARCH64_ADR_PREL_PG_HI21: 356 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: 357 case R_AARCH64_TLSDESC_ADR_PAGE21: 358 checkInt(loc, val, 33, rel); 359 LLVM_FALLTHROUGH; 360 case R_AARCH64_ADR_PREL_PG_HI21_NC: 361 write32AArch64Addr(loc, val >> 12); 362 break; 363 case R_AARCH64_ADR_PREL_LO21: 364 checkInt(loc, val, 21, rel); 365 write32AArch64Addr(loc, val); 366 break; 367 case R_AARCH64_JUMP26: 368 // Normally we would just write the bits of the immediate field, however 369 // when patching instructions for the cpu errata fix -fix-cortex-a53-843419 370 // we want to replace a non-branch instruction with a branch immediate 371 // instruction. By writing all the bits of the instruction including the 372 // opcode and the immediate (0 001 | 01 imm26) we can do this 373 // transformation by placing a R_AARCH64_JUMP26 relocation at the offset of 374 // the instruction we want to patch. 375 write32le(loc, 0x14000000); 376 LLVM_FALLTHROUGH; 377 case R_AARCH64_CALL26: 378 checkInt(loc, val, 28, rel); 379 or32le(loc, (val & 0x0FFFFFFC) >> 2); 380 break; 381 case R_AARCH64_CONDBR19: 382 case R_AARCH64_LD_PREL_LO19: 383 checkAlignment(loc, val, 4, rel); 384 checkInt(loc, val, 21, rel); 385 or32le(loc, (val & 0x1FFFFC) << 3); 386 break; 387 case R_AARCH64_LDST8_ABS_LO12_NC: 388 case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC: 389 or32AArch64Imm(loc, getBits(val, 0, 11)); 390 break; 391 case R_AARCH64_LDST16_ABS_LO12_NC: 392 case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC: 393 checkAlignment(loc, val, 2, rel); 394 or32AArch64Imm(loc, getBits(val, 1, 11)); 395 break; 396 case R_AARCH64_LDST32_ABS_LO12_NC: 397 case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC: 398 checkAlignment(loc, val, 4, rel); 399 or32AArch64Imm(loc, getBits(val, 2, 11)); 400 break; 401 case R_AARCH64_LDST64_ABS_LO12_NC: 402 case R_AARCH64_LD64_GOT_LO12_NC: 403 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 404 case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC: 405 case R_AARCH64_TLSDESC_LD64_LO12: 406 checkAlignment(loc, val, 8, rel); 407 or32AArch64Imm(loc, getBits(val, 3, 11)); 408 break; 409 case R_AARCH64_LDST128_ABS_LO12_NC: 410 case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC: 411 checkAlignment(loc, val, 16, rel); 412 or32AArch64Imm(loc, getBits(val, 4, 11)); 413 break; 414 case R_AARCH64_LD64_GOTPAGE_LO15: 415 checkAlignment(loc, val, 8, rel); 416 or32AArch64Imm(loc, getBits(val, 3, 14)); 417 break; 418 case R_AARCH64_MOVW_UABS_G0: 419 checkUInt(loc, val, 16, rel); 420 LLVM_FALLTHROUGH; 421 case R_AARCH64_MOVW_UABS_G0_NC: 422 or32le(loc, (val & 0xFFFF) << 5); 423 break; 424 case R_AARCH64_MOVW_UABS_G1: 425 checkUInt(loc, val, 32, rel); 426 LLVM_FALLTHROUGH; 427 case R_AARCH64_MOVW_UABS_G1_NC: 428 or32le(loc, (val & 0xFFFF0000) >> 11); 429 break; 430 case R_AARCH64_MOVW_UABS_G2: 431 checkUInt(loc, val, 48, rel); 432 LLVM_FALLTHROUGH; 433 case R_AARCH64_MOVW_UABS_G2_NC: 434 or32le(loc, (val & 0xFFFF00000000) >> 27); 435 break; 436 case R_AARCH64_MOVW_UABS_G3: 437 or32le(loc, (val & 0xFFFF000000000000) >> 43); 438 break; 439 case R_AARCH64_MOVW_PREL_G0: 440 case R_AARCH64_MOVW_SABS_G0: 441 case R_AARCH64_TLSLE_MOVW_TPREL_G0: 442 checkInt(loc, val, 17, rel); 443 LLVM_FALLTHROUGH; 444 case R_AARCH64_MOVW_PREL_G0_NC: 445 case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC: 446 writeSMovWImm(loc, val); 447 break; 448 case R_AARCH64_MOVW_PREL_G1: 449 case R_AARCH64_MOVW_SABS_G1: 450 case R_AARCH64_TLSLE_MOVW_TPREL_G1: 451 checkInt(loc, val, 33, rel); 452 LLVM_FALLTHROUGH; 453 case R_AARCH64_MOVW_PREL_G1_NC: 454 case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC: 455 writeSMovWImm(loc, val >> 16); 456 break; 457 case R_AARCH64_MOVW_PREL_G2: 458 case R_AARCH64_MOVW_SABS_G2: 459 case R_AARCH64_TLSLE_MOVW_TPREL_G2: 460 checkInt(loc, val, 49, rel); 461 LLVM_FALLTHROUGH; 462 case R_AARCH64_MOVW_PREL_G2_NC: 463 writeSMovWImm(loc, val >> 32); 464 break; 465 case R_AARCH64_MOVW_PREL_G3: 466 writeSMovWImm(loc, val >> 48); 467 break; 468 case R_AARCH64_TSTBR14: 469 checkInt(loc, val, 16, rel); 470 or32le(loc, (val & 0xFFFC) << 3); 471 break; 472 case R_AARCH64_TLSLE_ADD_TPREL_HI12: 473 checkUInt(loc, val, 24, rel); 474 or32AArch64Imm(loc, val >> 12); 475 break; 476 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: 477 case R_AARCH64_TLSDESC_ADD_LO12: 478 or32AArch64Imm(loc, val); 479 break; 480 case R_AARCH64_TLSDESC: 481 // For R_AARCH64_TLSDESC the addend is stored in the second 64-bit word. 482 write64(loc + 8, val); 483 break; 484 default: 485 llvm_unreachable("unknown relocation"); 486 } 487 } 488 489 void AArch64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 490 uint64_t val) const { 491 // TLSDESC Global-Dynamic relocation are in the form: 492 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21] 493 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12] 494 // add x0, x0, :tlsdesc_los:v [R_AARCH64_TLSDESC_ADD_LO12] 495 // .tlsdesccall [R_AARCH64_TLSDESC_CALL] 496 // blr x1 497 // And it can optimized to: 498 // movz x0, #0x0, lsl #16 499 // movk x0, #0x10 500 // nop 501 // nop 502 checkUInt(loc, val, 32, rel); 503 504 switch (rel.type) { 505 case R_AARCH64_TLSDESC_ADD_LO12: 506 case R_AARCH64_TLSDESC_CALL: 507 write32le(loc, 0xd503201f); // nop 508 return; 509 case R_AARCH64_TLSDESC_ADR_PAGE21: 510 write32le(loc, 0xd2a00000 | (((val >> 16) & 0xffff) << 5)); // movz 511 return; 512 case R_AARCH64_TLSDESC_LD64_LO12: 513 write32le(loc, 0xf2800000 | ((val & 0xffff) << 5)); // movk 514 return; 515 default: 516 llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 517 } 518 } 519 520 void AArch64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 521 uint64_t val) const { 522 // TLSDESC Global-Dynamic relocation are in the form: 523 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21] 524 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12] 525 // add x0, x0, :tlsdesc_los:v [R_AARCH64_TLSDESC_ADD_LO12] 526 // .tlsdesccall [R_AARCH64_TLSDESC_CALL] 527 // blr x1 528 // And it can optimized to: 529 // adrp x0, :gottprel:v 530 // ldr x0, [x0, :gottprel_lo12:v] 531 // nop 532 // nop 533 534 switch (rel.type) { 535 case R_AARCH64_TLSDESC_ADD_LO12: 536 case R_AARCH64_TLSDESC_CALL: 537 write32le(loc, 0xd503201f); // nop 538 break; 539 case R_AARCH64_TLSDESC_ADR_PAGE21: 540 write32le(loc, 0x90000000); // adrp 541 relocateNoSym(loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, val); 542 break; 543 case R_AARCH64_TLSDESC_LD64_LO12: 544 write32le(loc, 0xf9400000); // ldr 545 relocateNoSym(loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, val); 546 break; 547 default: 548 llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); 549 } 550 } 551 552 void AArch64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 553 uint64_t val) const { 554 checkUInt(loc, val, 32, rel); 555 556 if (rel.type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) { 557 // Generate MOVZ. 558 uint32_t regNo = read32le(loc) & 0x1f; 559 write32le(loc, (0xd2a00000 | regNo) | (((val >> 16) & 0xffff) << 5)); 560 return; 561 } 562 if (rel.type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) { 563 // Generate MOVK. 564 uint32_t regNo = read32le(loc) & 0x1f; 565 write32le(loc, (0xf2800000 | regNo) | ((val & 0xffff) << 5)); 566 return; 567 } 568 llvm_unreachable("invalid relocation for TLS IE to LE relaxation"); 569 } 570 571 // AArch64 may use security features in variant PLT sequences. These are: 572 // Pointer Authentication (PAC), introduced in armv8.3-a and Branch Target 573 // Indicator (BTI) introduced in armv8.5-a. The additional instructions used 574 // in the variant Plt sequences are encoded in the Hint space so they can be 575 // deployed on older architectures, which treat the instructions as a nop. 576 // PAC and BTI can be combined leading to the following combinations: 577 // writePltHeader 578 // writePltHeaderBti (no PAC Header needed) 579 // writePlt 580 // writePltBti (BTI only) 581 // writePltPac (PAC only) 582 // writePltBtiPac (BTI and PAC) 583 // 584 // When PAC is enabled the dynamic loader encrypts the address that it places 585 // in the .got.plt using the pacia1716 instruction which encrypts the value in 586 // x17 using the modifier in x16. The static linker places autia1716 before the 587 // indirect branch to x17 to authenticate the address in x17 with the modifier 588 // in x16. This makes it more difficult for an attacker to modify the value in 589 // the .got.plt. 590 // 591 // When BTI is enabled all indirect branches must land on a bti instruction. 592 // The static linker must place a bti instruction at the start of any PLT entry 593 // that may be the target of an indirect branch. As the PLT entries call the 594 // lazy resolver indirectly this must have a bti instruction at start. In 595 // general a bti instruction is not needed for a PLT entry as indirect calls 596 // are resolved to the function address and not the PLT entry for the function. 597 // There are a small number of cases where the PLT address can escape, such as 598 // taking the address of a function or ifunc via a non got-generating 599 // relocation, and a shared library refers to that symbol. 600 // 601 // We use the bti c variant of the instruction which permits indirect branches 602 // (br) via x16/x17 and indirect function calls (blr) via any register. The ABI 603 // guarantees that all indirect branches from code requiring BTI protection 604 // will go via x16/x17 605 606 namespace { 607 class AArch64BtiPac final : public AArch64 { 608 public: 609 AArch64BtiPac(); 610 void writePltHeader(uint8_t *buf) const override; 611 void writePlt(uint8_t *buf, const Symbol &sym, 612 uint64_t pltEntryAddr) const override; 613 614 private: 615 bool btiHeader; // bti instruction needed in PLT Header and Entry 616 bool pacEntry; // autia1716 instruction needed in PLT Entry 617 }; 618 } // namespace 619 620 AArch64BtiPac::AArch64BtiPac() { 621 btiHeader = (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI); 622 // A BTI (Branch Target Indicator) Plt Entry is only required if the 623 // address of the PLT entry can be taken by the program, which permits an 624 // indirect jump to the PLT entry. This can happen when the address 625 // of the PLT entry for a function is canonicalised due to the address of 626 // the function in an executable being taken by a shared library, or 627 // non-preemptible ifunc referenced by non-GOT-generating, non-PLT-generating 628 // relocations. 629 // The PAC PLT entries require dynamic loader support and this isn't known 630 // from properties in the objects, so we use the command line flag. 631 pacEntry = config->zPacPlt; 632 633 if (btiHeader || pacEntry) { 634 pltEntrySize = 24; 635 ipltEntrySize = 24; 636 } 637 } 638 639 void AArch64BtiPac::writePltHeader(uint8_t *buf) const { 640 const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c 641 const uint8_t pltData[] = { 642 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! 643 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) 644 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] 645 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) 646 0x20, 0x02, 0x1f, 0xd6, // br x17 647 0x1f, 0x20, 0x03, 0xd5, // nop 648 0x1f, 0x20, 0x03, 0xd5 // nop 649 }; 650 const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop 651 652 uint64_t got = in.gotPlt->getVA(); 653 uint64_t plt = in.plt->getVA(); 654 655 if (btiHeader) { 656 // PltHeader is called indirectly by plt[N]. Prefix pltData with a BTI C 657 // instruction. 658 memcpy(buf, btiData, sizeof(btiData)); 659 buf += sizeof(btiData); 660 plt += sizeof(btiData); 661 } 662 memcpy(buf, pltData, sizeof(pltData)); 663 664 relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21, 665 getAArch64Page(got + 16) - getAArch64Page(plt + 8)); 666 relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16); 667 relocateNoSym(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16); 668 if (!btiHeader) 669 // We didn't add the BTI c instruction so round out size with NOP. 670 memcpy(buf + sizeof(pltData), nopData, sizeof(nopData)); 671 } 672 673 void AArch64BtiPac::writePlt(uint8_t *buf, const Symbol &sym, 674 uint64_t pltEntryAddr) const { 675 // The PLT entry is of the form: 676 // [btiData] addrInst (pacBr | stdBr) [nopData] 677 const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c 678 const uint8_t addrInst[] = { 679 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) 680 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] 681 0x10, 0x02, 0x00, 0x91 // add x16, x16, Offset(&(.plt.got[n])) 682 }; 683 const uint8_t pacBr[] = { 684 0x9f, 0x21, 0x03, 0xd5, // autia1716 685 0x20, 0x02, 0x1f, 0xd6 // br x17 686 }; 687 const uint8_t stdBr[] = { 688 0x20, 0x02, 0x1f, 0xd6, // br x17 689 0x1f, 0x20, 0x03, 0xd5 // nop 690 }; 691 const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop 692 693 // needsPltAddr indicates a non-ifunc canonical PLT entry whose address may 694 // escape to shared objects. isInIplt indicates a non-preemptible ifunc. Its 695 // address may escape if referenced by a direct relocation. The condition is 696 // conservative. 697 bool hasBti = btiHeader && (sym.needsPltAddr || sym.isInIplt); 698 if (hasBti) { 699 memcpy(buf, btiData, sizeof(btiData)); 700 buf += sizeof(btiData); 701 pltEntryAddr += sizeof(btiData); 702 } 703 704 uint64_t gotPltEntryAddr = sym.getGotPltVA(); 705 memcpy(buf, addrInst, sizeof(addrInst)); 706 relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21, 707 getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr)); 708 relocateNoSym(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr); 709 relocateNoSym(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr); 710 711 if (pacEntry) 712 memcpy(buf + sizeof(addrInst), pacBr, sizeof(pacBr)); 713 else 714 memcpy(buf + sizeof(addrInst), stdBr, sizeof(stdBr)); 715 if (!hasBti) 716 // We didn't add the BTI c instruction so round out size with NOP. 717 memcpy(buf + sizeof(addrInst) + sizeof(stdBr), nopData, sizeof(nopData)); 718 } 719 720 static TargetInfo *getTargetInfo() { 721 if (config->andFeatures & (GNU_PROPERTY_AARCH64_FEATURE_1_BTI | 722 GNU_PROPERTY_AARCH64_FEATURE_1_PAC)) { 723 static AArch64BtiPac t; 724 return &t; 725 } 726 static AArch64 t; 727 return &t; 728 } 729 730 TargetInfo *elf::getAArch64TargetInfo() { return getTargetInfo(); } 731