1 //===- ARM.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 "InputFiles.h" 10 #include "Symbols.h" 11 #include "SyntheticSections.h" 12 #include "Target.h" 13 #include "Thunks.h" 14 #include "lld/Common/ErrorHandler.h" 15 #include "llvm/Object/ELF.h" 16 #include "llvm/Support/Endian.h" 17 18 using namespace llvm; 19 using namespace llvm::support::endian; 20 using namespace llvm::ELF; 21 using namespace lld; 22 using namespace lld::elf; 23 24 namespace { 25 class ARM final : public TargetInfo { 26 public: 27 ARM(); 28 uint32_t calcEFlags() const override; 29 RelExpr getRelExpr(RelType type, const Symbol &s, 30 const uint8_t *loc) const override; 31 RelType getDynRel(RelType type) const override; 32 int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override; 33 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 34 void writeIgotPlt(uint8_t *buf, const Symbol &s) const override; 35 void writePltHeader(uint8_t *buf) const override; 36 void writePlt(uint8_t *buf, const Symbol &sym, 37 uint64_t pltEntryAddr) const override; 38 void addPltSymbols(InputSection &isec, uint64_t off) const override; 39 void addPltHeaderSymbols(InputSection &isd) const override; 40 bool needsThunk(RelExpr expr, RelType type, const InputFile *file, 41 uint64_t branchAddr, const Symbol &s, 42 int64_t a) const override; 43 uint32_t getThunkSectionSpacing() const override; 44 bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override; 45 void relocate(uint8_t *loc, const Relocation &rel, 46 uint64_t val) const override; 47 }; 48 } // namespace 49 50 ARM::ARM() { 51 copyRel = R_ARM_COPY; 52 relativeRel = R_ARM_RELATIVE; 53 iRelativeRel = R_ARM_IRELATIVE; 54 gotRel = R_ARM_GLOB_DAT; 55 noneRel = R_ARM_NONE; 56 pltRel = R_ARM_JUMP_SLOT; 57 symbolicRel = R_ARM_ABS32; 58 tlsGotRel = R_ARM_TLS_TPOFF32; 59 tlsModuleIndexRel = R_ARM_TLS_DTPMOD32; 60 tlsOffsetRel = R_ARM_TLS_DTPOFF32; 61 gotBaseSymInGotPlt = false; 62 pltHeaderSize = 32; 63 pltEntrySize = 16; 64 ipltEntrySize = 16; 65 trapInstr = {0xd4, 0xd4, 0xd4, 0xd4}; 66 needsThunks = true; 67 defaultMaxPageSize = 65536; 68 } 69 70 uint32_t ARM::calcEFlags() const { 71 // The ABIFloatType is used by loaders to detect the floating point calling 72 // convention. 73 uint32_t abiFloatType = 0; 74 if (config->armVFPArgs == ARMVFPArgKind::Base || 75 config->armVFPArgs == ARMVFPArgKind::Default) 76 abiFloatType = EF_ARM_ABI_FLOAT_SOFT; 77 else if (config->armVFPArgs == ARMVFPArgKind::VFP) 78 abiFloatType = EF_ARM_ABI_FLOAT_HARD; 79 80 // We don't currently use any features incompatible with EF_ARM_EABI_VER5, 81 // but we don't have any firm guarantees of conformance. Linux AArch64 82 // kernels (as of 2016) require an EABI version to be set. 83 return EF_ARM_EABI_VER5 | abiFloatType; 84 } 85 86 RelExpr ARM::getRelExpr(RelType type, const Symbol &s, 87 const uint8_t *loc) const { 88 switch (type) { 89 case R_ARM_THM_JUMP11: 90 return R_PC; 91 case R_ARM_CALL: 92 case R_ARM_JUMP24: 93 case R_ARM_PC24: 94 case R_ARM_PLT32: 95 case R_ARM_PREL31: 96 case R_ARM_THM_JUMP19: 97 case R_ARM_THM_JUMP24: 98 case R_ARM_THM_CALL: 99 return R_PLT_PC; 100 case R_ARM_GOTOFF32: 101 // (S + A) - GOT_ORG 102 return R_GOTREL; 103 case R_ARM_GOT_BREL: 104 // GOT(S) + A - GOT_ORG 105 return R_GOT_OFF; 106 case R_ARM_GOT_PREL: 107 case R_ARM_TLS_IE32: 108 // GOT(S) + A - P 109 return R_GOT_PC; 110 case R_ARM_SBREL32: 111 return R_ARM_SBREL; 112 case R_ARM_TARGET1: 113 return config->target1Rel ? R_PC : R_ABS; 114 case R_ARM_TARGET2: 115 if (config->target2 == Target2Policy::Rel) 116 return R_PC; 117 if (config->target2 == Target2Policy::Abs) 118 return R_ABS; 119 return R_GOT_PC; 120 case R_ARM_TLS_GD32: 121 return R_TLSGD_PC; 122 case R_ARM_TLS_LDM32: 123 return R_TLSLD_PC; 124 case R_ARM_BASE_PREL: 125 // B(S) + A - P 126 // FIXME: currently B(S) assumed to be .got, this may not hold for all 127 // platforms. 128 return R_GOTONLY_PC; 129 case R_ARM_MOVW_PREL_NC: 130 case R_ARM_MOVT_PREL: 131 case R_ARM_REL32: 132 case R_ARM_THM_MOVW_PREL_NC: 133 case R_ARM_THM_MOVT_PREL: 134 return R_PC; 135 case R_ARM_ALU_PC_G0: 136 case R_ARM_LDR_PC_G0: 137 case R_ARM_THM_ALU_PREL_11_0: 138 case R_ARM_THM_PC8: 139 case R_ARM_THM_PC12: 140 return R_ARM_PCA; 141 case R_ARM_MOVW_BREL_NC: 142 case R_ARM_MOVW_BREL: 143 case R_ARM_MOVT_BREL: 144 case R_ARM_THM_MOVW_BREL_NC: 145 case R_ARM_THM_MOVW_BREL: 146 case R_ARM_THM_MOVT_BREL: 147 return R_ARM_SBREL; 148 case R_ARM_NONE: 149 return R_NONE; 150 case R_ARM_TLS_LE32: 151 return R_TLS; 152 case R_ARM_V4BX: 153 // V4BX is just a marker to indicate there's a "bx rN" instruction at the 154 // given address. It can be used to implement a special linker mode which 155 // rewrites ARMv4T inputs to ARMv4. Since we support only ARMv4 input and 156 // not ARMv4 output, we can just ignore it. 157 return R_NONE; 158 default: 159 return R_ABS; 160 } 161 } 162 163 RelType ARM::getDynRel(RelType type) const { 164 if ((type == R_ARM_ABS32) || (type == R_ARM_TARGET1 && !config->target1Rel)) 165 return R_ARM_ABS32; 166 return R_ARM_NONE; 167 } 168 169 void ARM::writeGotPlt(uint8_t *buf, const Symbol &) const { 170 write32le(buf, in.plt->getVA()); 171 } 172 173 void ARM::writeIgotPlt(uint8_t *buf, const Symbol &s) const { 174 // An ARM entry is the address of the ifunc resolver function. 175 write32le(buf, s.getVA()); 176 } 177 178 // Long form PLT Header that does not have any restrictions on the displacement 179 // of the .plt from the .plt.got. 180 static void writePltHeaderLong(uint8_t *buf) { 181 const uint8_t pltData[] = { 182 0x04, 0xe0, 0x2d, 0xe5, // str lr, [sp,#-4]! 183 0x04, 0xe0, 0x9f, 0xe5, // ldr lr, L2 184 0x0e, 0xe0, 0x8f, 0xe0, // L1: add lr, pc, lr 185 0x08, 0xf0, 0xbe, 0xe5, // ldr pc, [lr, #8] 186 0x00, 0x00, 0x00, 0x00, // L2: .word &(.got.plt) - L1 - 8 187 0xd4, 0xd4, 0xd4, 0xd4, // Pad to 32-byte boundary 188 0xd4, 0xd4, 0xd4, 0xd4, // Pad to 32-byte boundary 189 0xd4, 0xd4, 0xd4, 0xd4}; 190 memcpy(buf, pltData, sizeof(pltData)); 191 uint64_t gotPlt = in.gotPlt->getVA(); 192 uint64_t l1 = in.plt->getVA() + 8; 193 write32le(buf + 16, gotPlt - l1 - 8); 194 } 195 196 // The default PLT header requires the .plt.got to be within 128 Mb of the 197 // .plt in the positive direction. 198 void ARM::writePltHeader(uint8_t *buf) const { 199 // Use a similar sequence to that in writePlt(), the difference is the calling 200 // conventions mean we use lr instead of ip. The PLT entry is responsible for 201 // saving lr on the stack, the dynamic loader is responsible for reloading 202 // it. 203 const uint32_t pltData[] = { 204 0xe52de004, // L1: str lr, [sp,#-4]! 205 0xe28fe600, // add lr, pc, #0x0NN00000 &(.got.plt - L1 - 4) 206 0xe28eea00, // add lr, lr, #0x000NN000 &(.got.plt - L1 - 4) 207 0xe5bef000, // ldr pc, [lr, #0x00000NNN] &(.got.plt -L1 - 4) 208 }; 209 210 uint64_t offset = in.gotPlt->getVA() - in.plt->getVA() - 4; 211 if (!llvm::isUInt<27>(offset)) { 212 // We cannot encode the Offset, use the long form. 213 writePltHeaderLong(buf); 214 return; 215 } 216 write32le(buf + 0, pltData[0]); 217 write32le(buf + 4, pltData[1] | ((offset >> 20) & 0xff)); 218 write32le(buf + 8, pltData[2] | ((offset >> 12) & 0xff)); 219 write32le(buf + 12, pltData[3] | (offset & 0xfff)); 220 memcpy(buf + 16, trapInstr.data(), 4); // Pad to 32-byte boundary 221 memcpy(buf + 20, trapInstr.data(), 4); 222 memcpy(buf + 24, trapInstr.data(), 4); 223 memcpy(buf + 28, trapInstr.data(), 4); 224 } 225 226 void ARM::addPltHeaderSymbols(InputSection &isec) const { 227 addSyntheticLocal("$a", STT_NOTYPE, 0, 0, isec); 228 addSyntheticLocal("$d", STT_NOTYPE, 16, 0, isec); 229 } 230 231 // Long form PLT entries that do not have any restrictions on the displacement 232 // of the .plt from the .plt.got. 233 static void writePltLong(uint8_t *buf, uint64_t gotPltEntryAddr, 234 uint64_t pltEntryAddr) { 235 const uint8_t pltData[] = { 236 0x04, 0xc0, 0x9f, 0xe5, // ldr ip, L2 237 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc 238 0x00, 0xf0, 0x9c, 0xe5, // ldr pc, [ip] 239 0x00, 0x00, 0x00, 0x00, // L2: .word Offset(&(.plt.got) - L1 - 8 240 }; 241 memcpy(buf, pltData, sizeof(pltData)); 242 uint64_t l1 = pltEntryAddr + 4; 243 write32le(buf + 12, gotPltEntryAddr - l1 - 8); 244 } 245 246 // The default PLT entries require the .plt.got to be within 128 Mb of the 247 // .plt in the positive direction. 248 void ARM::writePlt(uint8_t *buf, const Symbol &sym, 249 uint64_t pltEntryAddr) const { 250 // The PLT entry is similar to the example given in Appendix A of ELF for 251 // the Arm Architecture. Instead of using the Group Relocations to find the 252 // optimal rotation for the 8-bit immediate used in the add instructions we 253 // hard code the most compact rotations for simplicity. This saves a load 254 // instruction over the long plt sequences. 255 const uint32_t pltData[] = { 256 0xe28fc600, // L1: add ip, pc, #0x0NN00000 Offset(&(.plt.got) - L1 - 8 257 0xe28cca00, // add ip, ip, #0x000NN000 Offset(&(.plt.got) - L1 - 8 258 0xe5bcf000, // ldr pc, [ip, #0x00000NNN] Offset(&(.plt.got) - L1 - 8 259 }; 260 261 uint64_t offset = sym.getGotPltVA() - pltEntryAddr - 8; 262 if (!llvm::isUInt<27>(offset)) { 263 // We cannot encode the Offset, use the long form. 264 writePltLong(buf, sym.getGotPltVA(), pltEntryAddr); 265 return; 266 } 267 write32le(buf + 0, pltData[0] | ((offset >> 20) & 0xff)); 268 write32le(buf + 4, pltData[1] | ((offset >> 12) & 0xff)); 269 write32le(buf + 8, pltData[2] | (offset & 0xfff)); 270 memcpy(buf + 12, trapInstr.data(), 4); // Pad to 16-byte boundary 271 } 272 273 void ARM::addPltSymbols(InputSection &isec, uint64_t off) const { 274 addSyntheticLocal("$a", STT_NOTYPE, off, 0, isec); 275 addSyntheticLocal("$d", STT_NOTYPE, off + 12, 0, isec); 276 } 277 278 bool ARM::needsThunk(RelExpr expr, RelType type, const InputFile *file, 279 uint64_t branchAddr, const Symbol &s, 280 int64_t /*a*/) const { 281 // If S is an undefined weak symbol and does not have a PLT entry then it 282 // will be resolved as a branch to the next instruction. 283 if (s.isUndefWeak() && !s.isInPlt()) 284 return false; 285 // A state change from ARM to Thumb and vice versa must go through an 286 // interworking thunk if the relocation type is not R_ARM_CALL or 287 // R_ARM_THM_CALL. 288 switch (type) { 289 case R_ARM_PC24: 290 case R_ARM_PLT32: 291 case R_ARM_JUMP24: 292 // Source is ARM, all PLT entries are ARM so no interworking required. 293 // Otherwise we need to interwork if STT_FUNC Symbol has bit 0 set (Thumb). 294 if (s.isFunc() && expr == R_PC && (s.getVA() & 1)) 295 return true; 296 LLVM_FALLTHROUGH; 297 case R_ARM_CALL: { 298 uint64_t dst = (expr == R_PLT_PC) ? s.getPltVA() : s.getVA(); 299 return !inBranchRange(type, branchAddr, dst); 300 } 301 case R_ARM_THM_JUMP19: 302 case R_ARM_THM_JUMP24: 303 // Source is Thumb, all PLT entries are ARM so interworking is required. 304 // Otherwise we need to interwork if STT_FUNC Symbol has bit 0 clear (ARM). 305 if (expr == R_PLT_PC || (s.isFunc() && (s.getVA() & 1) == 0)) 306 return true; 307 LLVM_FALLTHROUGH; 308 case R_ARM_THM_CALL: { 309 uint64_t dst = (expr == R_PLT_PC) ? s.getPltVA() : s.getVA(); 310 return !inBranchRange(type, branchAddr, dst); 311 } 312 } 313 return false; 314 } 315 316 uint32_t ARM::getThunkSectionSpacing() const { 317 // The placing of pre-created ThunkSections is controlled by the value 318 // thunkSectionSpacing returned by getThunkSectionSpacing(). The aim is to 319 // place the ThunkSection such that all branches from the InputSections 320 // prior to the ThunkSection can reach a Thunk placed at the end of the 321 // ThunkSection. Graphically: 322 // | up to thunkSectionSpacing .text input sections | 323 // | ThunkSection | 324 // | up to thunkSectionSpacing .text input sections | 325 // | ThunkSection | 326 327 // Pre-created ThunkSections are spaced roughly 16MiB apart on ARMv7. This 328 // is to match the most common expected case of a Thumb 2 encoded BL, BLX or 329 // B.W: 330 // ARM B, BL, BLX range +/- 32MiB 331 // Thumb B.W, BL, BLX range +/- 16MiB 332 // Thumb B<cc>.W range +/- 1MiB 333 // If a branch cannot reach a pre-created ThunkSection a new one will be 334 // created so we can handle the rare cases of a Thumb 2 conditional branch. 335 // We intentionally use a lower size for thunkSectionSpacing than the maximum 336 // branch range so the end of the ThunkSection is more likely to be within 337 // range of the branch instruction that is furthest away. The value we shorten 338 // thunkSectionSpacing by is set conservatively to allow us to create 16,384 339 // 12 byte Thunks at any offset in a ThunkSection without risk of a branch to 340 // one of the Thunks going out of range. 341 342 // On Arm the thunkSectionSpacing depends on the range of the Thumb Branch 343 // range. On earlier Architectures such as ARMv4, ARMv5 and ARMv6 (except 344 // ARMv6T2) the range is +/- 4MiB. 345 346 return (config->armJ1J2BranchEncoding) ? 0x1000000 - 0x30000 347 : 0x400000 - 0x7500; 348 } 349 350 bool ARM::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 351 uint64_t range; 352 uint64_t instrSize; 353 354 switch (type) { 355 case R_ARM_PC24: 356 case R_ARM_PLT32: 357 case R_ARM_JUMP24: 358 case R_ARM_CALL: 359 range = 0x2000000; 360 instrSize = 4; 361 break; 362 case R_ARM_THM_JUMP19: 363 range = 0x100000; 364 instrSize = 2; 365 break; 366 case R_ARM_THM_JUMP24: 367 case R_ARM_THM_CALL: 368 range = config->armJ1J2BranchEncoding ? 0x1000000 : 0x400000; 369 instrSize = 2; 370 break; 371 default: 372 return true; 373 } 374 // PC at Src is 2 instructions ahead, immediate of branch is signed 375 if (src > dst) 376 range -= 2 * instrSize; 377 else 378 range += instrSize; 379 380 if ((dst & 0x1) == 0) 381 // Destination is ARM, if ARM caller then Src is already 4-byte aligned. 382 // If Thumb Caller (BLX) the Src address has bottom 2 bits cleared to ensure 383 // destination will be 4 byte aligned. 384 src &= ~0x3; 385 else 386 // Bit 0 == 1 denotes Thumb state, it is not part of the range 387 dst &= ~0x1; 388 389 uint64_t distance = (src > dst) ? src - dst : dst - src; 390 return distance <= range; 391 } 392 393 // Helper to produce message text when LLD detects that a CALL relocation to 394 // a non STT_FUNC symbol that may result in incorrect interworking between ARM 395 // or Thumb. 396 static void stateChangeWarning(uint8_t *loc, RelType relt, const Symbol &s) { 397 assert(!s.isFunc()); 398 if (s.isSection()) { 399 // Section symbols must be defined and in a section. Users cannot change 400 // the type. Use the section name as getName() returns an empty string. 401 warn(getErrorLocation(loc) + "branch and link relocation: " + 402 toString(relt) + " to STT_SECTION symbol " + 403 cast<Defined>(s).section->name + " ; interworking not performed"); 404 } else { 405 // Warn with hint on how to alter the symbol type. 406 warn(getErrorLocation(loc) + "branch and link relocation: " + 407 toString(relt) + " to non STT_FUNC symbol: " + s.getName() + 408 " interworking not performed; consider using directive '.type " + 409 s.getName() + 410 ", %function' to give symbol type STT_FUNC if" 411 " interworking between ARM and Thumb is required"); 412 } 413 } 414 415 // Utility functions taken from ARMAddressingModes.h, only changes are LLD 416 // coding style. 417 418 // Rotate a 32-bit unsigned value right by a specified amt of bits. 419 static uint32_t rotr32(uint32_t val, uint32_t amt) { 420 assert(amt < 32 && "Invalid rotate amount"); 421 return (val >> amt) | (val << ((32 - amt) & 31)); 422 } 423 424 // Rotate a 32-bit unsigned value left by a specified amt of bits. 425 static uint32_t rotl32(uint32_t val, uint32_t amt) { 426 assert(amt < 32 && "Invalid rotate amount"); 427 return (val << amt) | (val >> ((32 - amt) & 31)); 428 } 429 430 // Try to encode a 32-bit unsigned immediate imm with an immediate shifter 431 // operand, this form is an 8-bit immediate rotated right by an even number of 432 // bits. We compute the rotate amount to use. If this immediate value cannot be 433 // handled with a single shifter-op, determine a good rotate amount that will 434 // take a maximal chunk of bits out of the immediate. 435 static uint32_t getSOImmValRotate(uint32_t imm) { 436 // 8-bit (or less) immediates are trivially shifter_operands with a rotate 437 // of zero. 438 if ((imm & ~255U) == 0) 439 return 0; 440 441 // Use CTZ to compute the rotate amount. 442 unsigned tz = llvm::countTrailingZeros(imm); 443 444 // Rotate amount must be even. Something like 0x200 must be rotated 8 bits, 445 // not 9. 446 unsigned rotAmt = tz & ~1; 447 448 // If we can handle this spread, return it. 449 if ((rotr32(imm, rotAmt) & ~255U) == 0) 450 return (32 - rotAmt) & 31; // HW rotates right, not left. 451 452 // For values like 0xF000000F, we should ignore the low 6 bits, then 453 // retry the hunt. 454 if (imm & 63U) { 455 unsigned tz2 = countTrailingZeros(imm & ~63U); 456 unsigned rotAmt2 = tz2 & ~1; 457 if ((rotr32(imm, rotAmt2) & ~255U) == 0) 458 return (32 - rotAmt2) & 31; // HW rotates right, not left. 459 } 460 461 // Otherwise, we have no way to cover this span of bits with a single 462 // shifter_op immediate. Return a chunk of bits that will be useful to 463 // handle. 464 return (32 - rotAmt) & 31; // HW rotates right, not left. 465 } 466 467 void ARM::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 468 switch (rel.type) { 469 case R_ARM_ABS32: 470 case R_ARM_BASE_PREL: 471 case R_ARM_GOTOFF32: 472 case R_ARM_GOT_BREL: 473 case R_ARM_GOT_PREL: 474 case R_ARM_REL32: 475 case R_ARM_RELATIVE: 476 case R_ARM_SBREL32: 477 case R_ARM_TARGET1: 478 case R_ARM_TARGET2: 479 case R_ARM_TLS_GD32: 480 case R_ARM_TLS_IE32: 481 case R_ARM_TLS_LDM32: 482 case R_ARM_TLS_LDO32: 483 case R_ARM_TLS_LE32: 484 case R_ARM_TLS_TPOFF32: 485 case R_ARM_TLS_DTPOFF32: 486 write32le(loc, val); 487 break; 488 case R_ARM_PREL31: 489 checkInt(loc, val, 31, rel); 490 write32le(loc, (read32le(loc) & 0x80000000) | (val & ~0x80000000)); 491 break; 492 case R_ARM_CALL: { 493 // R_ARM_CALL is used for BL and BLX instructions, for symbols of type 494 // STT_FUNC we choose whether to write a BL or BLX depending on the 495 // value of bit 0 of Val. With bit 0 == 1 denoting Thumb. If the symbol is 496 // not of type STT_FUNC then we must preserve the original instruction. 497 // PLT entries are always ARM state so we know we don't need to interwork. 498 assert(rel.sym); // R_ARM_CALL is always reached via relocate(). 499 bool bit0Thumb = val & 1; 500 bool isBlx = (read32le(loc) & 0xfe000000) == 0xfa000000; 501 // lld 10.0 and before always used bit0Thumb when deciding to write a BLX 502 // even when type not STT_FUNC. 503 if (!rel.sym->isFunc() && isBlx != bit0Thumb) 504 stateChangeWarning(loc, rel.type, *rel.sym); 505 if (rel.sym->isFunc() ? bit0Thumb : isBlx) { 506 // The BLX encoding is 0xfa:H:imm24 where Val = imm24:H:'1' 507 checkInt(loc, val, 26, rel); 508 write32le(loc, 0xfa000000 | // opcode 509 ((val & 2) << 23) | // H 510 ((val >> 2) & 0x00ffffff)); // imm24 511 break; 512 } 513 // BLX (always unconditional) instruction to an ARM Target, select an 514 // unconditional BL. 515 write32le(loc, 0xeb000000 | (read32le(loc) & 0x00ffffff)); 516 // fall through as BL encoding is shared with B 517 } 518 LLVM_FALLTHROUGH; 519 case R_ARM_JUMP24: 520 case R_ARM_PC24: 521 case R_ARM_PLT32: 522 checkInt(loc, val, 26, rel); 523 write32le(loc, (read32le(loc) & ~0x00ffffff) | ((val >> 2) & 0x00ffffff)); 524 break; 525 case R_ARM_THM_JUMP11: 526 checkInt(loc, val, 12, rel); 527 write16le(loc, (read32le(loc) & 0xf800) | ((val >> 1) & 0x07ff)); 528 break; 529 case R_ARM_THM_JUMP19: 530 // Encoding T3: Val = S:J2:J1:imm6:imm11:0 531 checkInt(loc, val, 21, rel); 532 write16le(loc, 533 (read16le(loc) & 0xfbc0) | // opcode cond 534 ((val >> 10) & 0x0400) | // S 535 ((val >> 12) & 0x003f)); // imm6 536 write16le(loc + 2, 537 0x8000 | // opcode 538 ((val >> 8) & 0x0800) | // J2 539 ((val >> 5) & 0x2000) | // J1 540 ((val >> 1) & 0x07ff)); // imm11 541 break; 542 case R_ARM_THM_CALL: { 543 // R_ARM_THM_CALL is used for BL and BLX instructions, for symbols of type 544 // STT_FUNC we choose whether to write a BL or BLX depending on the 545 // value of bit 0 of Val. With bit 0 == 0 denoting ARM, if the symbol is 546 // not of type STT_FUNC then we must preserve the original instruction. 547 // PLT entries are always ARM state so we know we need to interwork. 548 assert(rel.sym); // R_ARM_THM_CALL is always reached via relocate(). 549 bool bit0Thumb = val & 1; 550 bool isBlx = (read16le(loc + 2) & 0x1000) == 0; 551 // lld 10.0 and before always used bit0Thumb when deciding to write a BLX 552 // even when type not STT_FUNC. PLT entries generated by LLD are always ARM. 553 if (!rel.sym->isFunc() && !rel.sym->isInPlt() && isBlx == bit0Thumb) 554 stateChangeWarning(loc, rel.type, *rel.sym); 555 if (rel.sym->isFunc() || rel.sym->isInPlt() ? !bit0Thumb : isBlx) { 556 // We are writing a BLX. Ensure BLX destination is 4-byte aligned. As 557 // the BLX instruction may only be two byte aligned. This must be done 558 // before overflow check. 559 val = alignTo(val, 4); 560 write16le(loc + 2, read16le(loc + 2) & ~0x1000); 561 } else { 562 write16le(loc + 2, (read16le(loc + 2) & ~0x1000) | 1 << 12); 563 } 564 if (!config->armJ1J2BranchEncoding) { 565 // Older Arm architectures do not support R_ARM_THM_JUMP24 and have 566 // different encoding rules and range due to J1 and J2 always being 1. 567 checkInt(loc, val, 23, rel); 568 write16le(loc, 569 0xf000 | // opcode 570 ((val >> 12) & 0x07ff)); // imm11 571 write16le(loc + 2, 572 (read16le(loc + 2) & 0xd000) | // opcode 573 0x2800 | // J1 == J2 == 1 574 ((val >> 1) & 0x07ff)); // imm11 575 break; 576 } 577 } 578 // Fall through as rest of encoding is the same as B.W 579 LLVM_FALLTHROUGH; 580 case R_ARM_THM_JUMP24: 581 // Encoding B T4, BL T1, BLX T2: Val = S:I1:I2:imm10:imm11:0 582 checkInt(loc, val, 25, rel); 583 write16le(loc, 584 0xf000 | // opcode 585 ((val >> 14) & 0x0400) | // S 586 ((val >> 12) & 0x03ff)); // imm10 587 write16le(loc + 2, 588 (read16le(loc + 2) & 0xd000) | // opcode 589 (((~(val >> 10)) ^ (val >> 11)) & 0x2000) | // J1 590 (((~(val >> 11)) ^ (val >> 13)) & 0x0800) | // J2 591 ((val >> 1) & 0x07ff)); // imm11 592 break; 593 case R_ARM_MOVW_ABS_NC: 594 case R_ARM_MOVW_PREL_NC: 595 case R_ARM_MOVW_BREL_NC: 596 write32le(loc, (read32le(loc) & ~0x000f0fff) | ((val & 0xf000) << 4) | 597 (val & 0x0fff)); 598 break; 599 case R_ARM_MOVT_ABS: 600 case R_ARM_MOVT_PREL: 601 case R_ARM_MOVT_BREL: 602 write32le(loc, (read32le(loc) & ~0x000f0fff) | 603 (((val >> 16) & 0xf000) << 4) | ((val >> 16) & 0xfff)); 604 break; 605 case R_ARM_THM_MOVT_ABS: 606 case R_ARM_THM_MOVT_PREL: 607 case R_ARM_THM_MOVT_BREL: 608 // Encoding T1: A = imm4:i:imm3:imm8 609 write16le(loc, 610 0xf2c0 | // opcode 611 ((val >> 17) & 0x0400) | // i 612 ((val >> 28) & 0x000f)); // imm4 613 write16le(loc + 2, 614 (read16le(loc + 2) & 0x8f00) | // opcode 615 ((val >> 12) & 0x7000) | // imm3 616 ((val >> 16) & 0x00ff)); // imm8 617 break; 618 case R_ARM_THM_MOVW_ABS_NC: 619 case R_ARM_THM_MOVW_PREL_NC: 620 case R_ARM_THM_MOVW_BREL_NC: 621 // Encoding T3: A = imm4:i:imm3:imm8 622 write16le(loc, 623 0xf240 | // opcode 624 ((val >> 1) & 0x0400) | // i 625 ((val >> 12) & 0x000f)); // imm4 626 write16le(loc + 2, 627 (read16le(loc + 2) & 0x8f00) | // opcode 628 ((val << 4) & 0x7000) | // imm3 629 (val & 0x00ff)); // imm8 630 break; 631 case R_ARM_ALU_PC_G0: { 632 // ADR (literal) add = bit23, sub = bit22 633 // literal is a 12-bit modified immediate, made up of a 4-bit even rotate 634 // right and an 8-bit immediate. The code-sequence here is derived from 635 // ARMAddressingModes.h in llvm/Target/ARM/MCTargetDesc. In our case we 636 // want to give an error if we cannot encode the constant. 637 uint32_t opcode = 0x00800000; 638 if (val >> 63) { 639 opcode = 0x00400000; 640 val = ~val + 1; 641 } 642 if ((val & ~255U) != 0) { 643 uint32_t rotAmt = getSOImmValRotate(val); 644 // Error if we cannot encode this with a single shift 645 if (rotr32(~255U, rotAmt) & val) 646 error(getErrorLocation(loc) + "unencodeable immediate " + 647 Twine(val).str() + " for relocation " + toString(rel.type)); 648 val = rotl32(val, rotAmt) | ((rotAmt >> 1) << 8); 649 } 650 write32le(loc, (read32le(loc) & 0xff0ff000) | opcode | val); 651 break; 652 } 653 case R_ARM_LDR_PC_G0: { 654 // R_ARM_LDR_PC_G0 is S + A - P, we have ((S + A) | T) - P, if S is a 655 // function then addr is 0 (modulo 2) and Pa is 0 (modulo 4) so we can clear 656 // bottom bit to recover S + A - P. 657 if (rel.sym->isFunc()) 658 val &= ~0x1; 659 // LDR (literal) u = bit23 660 int64_t imm = val; 661 uint32_t u = 0x00800000; 662 if (imm < 0) { 663 imm = -imm; 664 u = 0; 665 } 666 checkUInt(loc, imm, 12, rel); 667 write32le(loc, (read32le(loc) & 0xff7ff000) | u | imm); 668 break; 669 } 670 case R_ARM_THM_ALU_PREL_11_0: { 671 // ADR encoding T2 (sub), T3 (add) i:imm3:imm8 672 int64_t imm = val; 673 uint16_t sub = 0; 674 if (imm < 0) { 675 imm = -imm; 676 sub = 0x00a0; 677 } 678 checkUInt(loc, imm, 12, rel); 679 write16le(loc, (read16le(loc) & 0xfb0f) | sub | (imm & 0x800) >> 1); 680 write16le(loc + 2, 681 (read16le(loc + 2) & 0x8f00) | (imm & 0x700) << 4 | (imm & 0xff)); 682 break; 683 } 684 case R_ARM_THM_PC8: 685 // ADR and LDR literal encoding T1 positive offset only imm8:00 686 // R_ARM_THM_PC8 is S + A - Pa, we have ((S + A) | T) - Pa, if S is a 687 // function then addr is 0 (modulo 2) and Pa is 0 (modulo 4) so we can clear 688 // bottom bit to recover S + A - Pa. 689 if (rel.sym->isFunc()) 690 val &= ~0x1; 691 checkUInt(loc, val, 10, rel); 692 checkAlignment(loc, val, 4, rel); 693 write16le(loc, (read16le(loc) & 0xff00) | (val & 0x3fc) >> 2); 694 break; 695 case R_ARM_THM_PC12: { 696 // LDR (literal) encoding T2, add = (U == '1') imm12 697 // imm12 is unsigned 698 // R_ARM_THM_PC12 is S + A - Pa, we have ((S + A) | T) - Pa, if S is a 699 // function then addr is 0 (modulo 2) and Pa is 0 (modulo 4) so we can clear 700 // bottom bit to recover S + A - Pa. 701 if (rel.sym->isFunc()) 702 val &= ~0x1; 703 int64_t imm12 = val; 704 uint16_t u = 0x0080; 705 if (imm12 < 0) { 706 imm12 = -imm12; 707 u = 0; 708 } 709 checkUInt(loc, imm12, 12, rel); 710 write16le(loc, read16le(loc) | u); 711 write16le(loc + 2, (read16le(loc + 2) & 0xf000) | imm12); 712 break; 713 } 714 default: 715 error(getErrorLocation(loc) + "unrecognized relocation " + 716 toString(rel.type)); 717 } 718 } 719 720 int64_t ARM::getImplicitAddend(const uint8_t *buf, RelType type) const { 721 switch (type) { 722 default: 723 return 0; 724 case R_ARM_ABS32: 725 case R_ARM_BASE_PREL: 726 case R_ARM_GOTOFF32: 727 case R_ARM_GOT_BREL: 728 case R_ARM_GOT_PREL: 729 case R_ARM_REL32: 730 case R_ARM_TARGET1: 731 case R_ARM_TARGET2: 732 case R_ARM_TLS_GD32: 733 case R_ARM_TLS_LDM32: 734 case R_ARM_TLS_LDO32: 735 case R_ARM_TLS_IE32: 736 case R_ARM_TLS_LE32: 737 return SignExtend64<32>(read32le(buf)); 738 case R_ARM_PREL31: 739 return SignExtend64<31>(read32le(buf)); 740 case R_ARM_CALL: 741 case R_ARM_JUMP24: 742 case R_ARM_PC24: 743 case R_ARM_PLT32: 744 return SignExtend64<26>(read32le(buf) << 2); 745 case R_ARM_THM_JUMP11: 746 return SignExtend64<12>(read16le(buf) << 1); 747 case R_ARM_THM_JUMP19: { 748 // Encoding T3: A = S:J2:J1:imm10:imm6:0 749 uint16_t hi = read16le(buf); 750 uint16_t lo = read16le(buf + 2); 751 return SignExtend64<20>(((hi & 0x0400) << 10) | // S 752 ((lo & 0x0800) << 8) | // J2 753 ((lo & 0x2000) << 5) | // J1 754 ((hi & 0x003f) << 12) | // imm6 755 ((lo & 0x07ff) << 1)); // imm11:0 756 } 757 case R_ARM_THM_CALL: 758 if (!config->armJ1J2BranchEncoding) { 759 // Older Arm architectures do not support R_ARM_THM_JUMP24 and have 760 // different encoding rules and range due to J1 and J2 always being 1. 761 uint16_t hi = read16le(buf); 762 uint16_t lo = read16le(buf + 2); 763 return SignExtend64<22>(((hi & 0x7ff) << 12) | // imm11 764 ((lo & 0x7ff) << 1)); // imm11:0 765 break; 766 } 767 LLVM_FALLTHROUGH; 768 case R_ARM_THM_JUMP24: { 769 // Encoding B T4, BL T1, BLX T2: A = S:I1:I2:imm10:imm11:0 770 // I1 = NOT(J1 EOR S), I2 = NOT(J2 EOR S) 771 uint16_t hi = read16le(buf); 772 uint16_t lo = read16le(buf + 2); 773 return SignExtend64<24>(((hi & 0x0400) << 14) | // S 774 (~((lo ^ (hi << 3)) << 10) & 0x00800000) | // I1 775 (~((lo ^ (hi << 1)) << 11) & 0x00400000) | // I2 776 ((hi & 0x003ff) << 12) | // imm0 777 ((lo & 0x007ff) << 1)); // imm11:0 778 } 779 // ELF for the ARM Architecture 4.6.1.1 the implicit addend for MOVW and 780 // MOVT is in the range -32768 <= A < 32768 781 case R_ARM_MOVW_ABS_NC: 782 case R_ARM_MOVT_ABS: 783 case R_ARM_MOVW_PREL_NC: 784 case R_ARM_MOVT_PREL: 785 case R_ARM_MOVW_BREL_NC: 786 case R_ARM_MOVT_BREL: { 787 uint64_t val = read32le(buf) & 0x000f0fff; 788 return SignExtend64<16>(((val & 0x000f0000) >> 4) | (val & 0x00fff)); 789 } 790 case R_ARM_THM_MOVW_ABS_NC: 791 case R_ARM_THM_MOVT_ABS: 792 case R_ARM_THM_MOVW_PREL_NC: 793 case R_ARM_THM_MOVT_PREL: 794 case R_ARM_THM_MOVW_BREL_NC: 795 case R_ARM_THM_MOVT_BREL: { 796 // Encoding T3: A = imm4:i:imm3:imm8 797 uint16_t hi = read16le(buf); 798 uint16_t lo = read16le(buf + 2); 799 return SignExtend64<16>(((hi & 0x000f) << 12) | // imm4 800 ((hi & 0x0400) << 1) | // i 801 ((lo & 0x7000) >> 4) | // imm3 802 (lo & 0x00ff)); // imm8 803 } 804 case R_ARM_ALU_PC_G0: { 805 // 12-bit immediate is a modified immediate made up of a 4-bit even 806 // right rotation and 8-bit constant. After the rotation the value 807 // is zero-extended. When bit 23 is set the instruction is an add, when 808 // bit 22 is set it is a sub. 809 uint32_t instr = read32le(buf); 810 uint32_t val = rotr32(instr & 0xff, ((instr & 0xf00) >> 8) * 2); 811 return (instr & 0x00400000) ? -val : val; 812 } 813 case R_ARM_LDR_PC_G0: { 814 // ADR (literal) add = bit23, sub = bit22 815 // LDR (literal) u = bit23 unsigned imm12 816 bool u = read32le(buf) & 0x00800000; 817 uint32_t imm12 = read32le(buf) & 0xfff; 818 return u ? imm12 : -imm12; 819 } 820 case R_ARM_THM_ALU_PREL_11_0: { 821 // Thumb2 ADR, which is an alias for a sub or add instruction with an 822 // unsigned immediate. 823 // ADR encoding T2 (sub), T3 (add) i:imm3:imm8 824 uint16_t hi = read16le(buf); 825 uint16_t lo = read16le(buf + 2); 826 uint64_t imm = (hi & 0x0400) << 1 | // i 827 (lo & 0x7000) >> 4 | // imm3 828 (lo & 0x00ff); // imm8 829 // For sub, addend is negative, add is positive. 830 return (hi & 0x00f0) ? -imm : imm; 831 } 832 case R_ARM_THM_PC8: 833 // ADR and LDR (literal) encoding T1 834 // From ELF for the ARM Architecture the initial signed addend is formed 835 // from an unsigned field using expression (((imm8:00 + 4) & 0x3ff) – 4) 836 // this trick permits the PC bias of -4 to be encoded using imm8 = 0xff 837 return ((((read16le(buf) & 0xff) << 2) + 4) & 0x3ff) - 4; 838 case R_ARM_THM_PC12: { 839 // LDR (literal) encoding T2, add = (U == '1') imm12 840 bool u = read16le(buf) & 0x0080; 841 uint64_t imm12 = read16le(buf + 2) & 0x0fff; 842 return u ? imm12 : -imm12; 843 } 844 } 845 } 846 847 TargetInfo *elf::getARMTargetInfo() { 848 static ARM target; 849 return ⌖ 850 } 851