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 22 namespace lld { 23 namespace elf { 24 25 namespace { 26 class ARM final : public TargetInfo { 27 public: 28 ARM(); 29 uint32_t calcEFlags() const override; 30 RelExpr getRelExpr(RelType type, const Symbol &s, 31 const uint8_t *loc) const override; 32 RelType getDynRel(RelType type) const override; 33 int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override; 34 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 35 void writeIgotPlt(uint8_t *buf, const Symbol &s) const override; 36 void writePltHeader(uint8_t *buf) const override; 37 void writePlt(uint8_t *buf, const Symbol &sym, 38 uint64_t pltEntryAddr) const override; 39 void addPltSymbols(InputSection &isec, uint64_t off) const override; 40 void addPltHeaderSymbols(InputSection &isd) const override; 41 bool needsThunk(RelExpr expr, RelType type, const InputFile *file, 42 uint64_t branchAddr, const Symbol &s, 43 int64_t a) const override; 44 uint32_t getThunkSectionSpacing() const override; 45 bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override; 46 void relocate(uint8_t *loc, const Relocation &rel, 47 uint64_t val) const override; 48 }; 49 } // namespace 50 51 ARM::ARM() { 52 copyRel = R_ARM_COPY; 53 relativeRel = R_ARM_RELATIVE; 54 iRelativeRel = R_ARM_IRELATIVE; 55 gotRel = R_ARM_GLOB_DAT; 56 noneRel = R_ARM_NONE; 57 pltRel = R_ARM_JUMP_SLOT; 58 symbolicRel = R_ARM_ABS32; 59 tlsGotRel = R_ARM_TLS_TPOFF32; 60 tlsModuleIndexRel = R_ARM_TLS_DTPMOD32; 61 tlsOffsetRel = R_ARM_TLS_DTPOFF32; 62 gotBaseSymInGotPlt = false; 63 pltHeaderSize = 32; 64 pltEntrySize = 16; 65 ipltEntrySize = 16; 66 trapInstr = {0xd4, 0xd4, 0xd4, 0xd4}; 67 needsThunks = true; 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_NONE: 136 return R_NONE; 137 case R_ARM_TLS_LE32: 138 return R_TLS; 139 case R_ARM_V4BX: 140 // V4BX is just a marker to indicate there's a "bx rN" instruction at the 141 // given address. It can be used to implement a special linker mode which 142 // rewrites ARMv4T inputs to ARMv4. Since we support only ARMv4 input and 143 // not ARMv4 output, we can just ignore it. 144 return R_NONE; 145 default: 146 return R_ABS; 147 } 148 } 149 150 RelType ARM::getDynRel(RelType type) const { 151 if ((type == R_ARM_ABS32) || (type == R_ARM_TARGET1 && !config->target1Rel)) 152 return R_ARM_ABS32; 153 return R_ARM_NONE; 154 } 155 156 void ARM::writeGotPlt(uint8_t *buf, const Symbol &) const { 157 write32le(buf, in.plt->getVA()); 158 } 159 160 void ARM::writeIgotPlt(uint8_t *buf, const Symbol &s) const { 161 // An ARM entry is the address of the ifunc resolver function. 162 write32le(buf, s.getVA()); 163 } 164 165 // Long form PLT Header that does not have any restrictions on the displacement 166 // of the .plt from the .plt.got. 167 static void writePltHeaderLong(uint8_t *buf) { 168 const uint8_t pltData[] = { 169 0x04, 0xe0, 0x2d, 0xe5, // str lr, [sp,#-4]! 170 0x04, 0xe0, 0x9f, 0xe5, // ldr lr, L2 171 0x0e, 0xe0, 0x8f, 0xe0, // L1: add lr, pc, lr 172 0x08, 0xf0, 0xbe, 0xe5, // ldr pc, [lr, #8] 173 0x00, 0x00, 0x00, 0x00, // L2: .word &(.got.plt) - L1 - 8 174 0xd4, 0xd4, 0xd4, 0xd4, // Pad to 32-byte boundary 175 0xd4, 0xd4, 0xd4, 0xd4, // Pad to 32-byte boundary 176 0xd4, 0xd4, 0xd4, 0xd4}; 177 memcpy(buf, pltData, sizeof(pltData)); 178 uint64_t gotPlt = in.gotPlt->getVA(); 179 uint64_t l1 = in.plt->getVA() + 8; 180 write32le(buf + 16, gotPlt - l1 - 8); 181 } 182 183 // The default PLT header requires the .plt.got to be within 128 Mb of the 184 // .plt in the positive direction. 185 void ARM::writePltHeader(uint8_t *buf) const { 186 // Use a similar sequence to that in writePlt(), the difference is the calling 187 // conventions mean we use lr instead of ip. The PLT entry is responsible for 188 // saving lr on the stack, the dynamic loader is responsible for reloading 189 // it. 190 const uint32_t pltData[] = { 191 0xe52de004, // L1: str lr, [sp,#-4]! 192 0xe28fe600, // add lr, pc, #0x0NN00000 &(.got.plt - L1 - 4) 193 0xe28eea00, // add lr, lr, #0x000NN000 &(.got.plt - L1 - 4) 194 0xe5bef000, // ldr pc, [lr, #0x00000NNN] &(.got.plt -L1 - 4) 195 }; 196 197 uint64_t offset = in.gotPlt->getVA() - in.plt->getVA() - 4; 198 if (!llvm::isUInt<27>(offset)) { 199 // We cannot encode the Offset, use the long form. 200 writePltHeaderLong(buf); 201 return; 202 } 203 write32le(buf + 0, pltData[0]); 204 write32le(buf + 4, pltData[1] | ((offset >> 20) & 0xff)); 205 write32le(buf + 8, pltData[2] | ((offset >> 12) & 0xff)); 206 write32le(buf + 12, pltData[3] | (offset & 0xfff)); 207 memcpy(buf + 16, trapInstr.data(), 4); // Pad to 32-byte boundary 208 memcpy(buf + 20, trapInstr.data(), 4); 209 memcpy(buf + 24, trapInstr.data(), 4); 210 memcpy(buf + 28, trapInstr.data(), 4); 211 } 212 213 void ARM::addPltHeaderSymbols(InputSection &isec) const { 214 addSyntheticLocal("$a", STT_NOTYPE, 0, 0, isec); 215 addSyntheticLocal("$d", STT_NOTYPE, 16, 0, isec); 216 } 217 218 // Long form PLT entries that do not have any restrictions on the displacement 219 // of the .plt from the .plt.got. 220 static void writePltLong(uint8_t *buf, uint64_t gotPltEntryAddr, 221 uint64_t pltEntryAddr) { 222 const uint8_t pltData[] = { 223 0x04, 0xc0, 0x9f, 0xe5, // ldr ip, L2 224 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc 225 0x00, 0xf0, 0x9c, 0xe5, // ldr pc, [ip] 226 0x00, 0x00, 0x00, 0x00, // L2: .word Offset(&(.plt.got) - L1 - 8 227 }; 228 memcpy(buf, pltData, sizeof(pltData)); 229 uint64_t l1 = pltEntryAddr + 4; 230 write32le(buf + 12, gotPltEntryAddr - l1 - 8); 231 } 232 233 // The default PLT entries require the .plt.got to be within 128 Mb of the 234 // .plt in the positive direction. 235 void ARM::writePlt(uint8_t *buf, const Symbol &sym, 236 uint64_t pltEntryAddr) const { 237 // The PLT entry is similar to the example given in Appendix A of ELF for 238 // the Arm Architecture. Instead of using the Group Relocations to find the 239 // optimal rotation for the 8-bit immediate used in the add instructions we 240 // hard code the most compact rotations for simplicity. This saves a load 241 // instruction over the long plt sequences. 242 const uint32_t pltData[] = { 243 0xe28fc600, // L1: add ip, pc, #0x0NN00000 Offset(&(.plt.got) - L1 - 8 244 0xe28cca00, // add ip, ip, #0x000NN000 Offset(&(.plt.got) - L1 - 8 245 0xe5bcf000, // ldr pc, [ip, #0x00000NNN] Offset(&(.plt.got) - L1 - 8 246 }; 247 248 uint64_t offset = sym.getGotPltVA() - pltEntryAddr - 8; 249 if (!llvm::isUInt<27>(offset)) { 250 // We cannot encode the Offset, use the long form. 251 writePltLong(buf, sym.getGotPltVA(), pltEntryAddr); 252 return; 253 } 254 write32le(buf + 0, pltData[0] | ((offset >> 20) & 0xff)); 255 write32le(buf + 4, pltData[1] | ((offset >> 12) & 0xff)); 256 write32le(buf + 8, pltData[2] | (offset & 0xfff)); 257 memcpy(buf + 12, trapInstr.data(), 4); // Pad to 16-byte boundary 258 } 259 260 void ARM::addPltSymbols(InputSection &isec, uint64_t off) const { 261 addSyntheticLocal("$a", STT_NOTYPE, off, 0, isec); 262 addSyntheticLocal("$d", STT_NOTYPE, off + 12, 0, isec); 263 } 264 265 bool ARM::needsThunk(RelExpr expr, RelType type, const InputFile *file, 266 uint64_t branchAddr, const Symbol &s, 267 int64_t /*a*/) const { 268 // If S is an undefined weak symbol and does not have a PLT entry then it 269 // will be resolved as a branch to the next instruction. 270 if (s.isUndefWeak() && !s.isInPlt()) 271 return false; 272 // A state change from ARM to Thumb and vice versa must go through an 273 // interworking thunk if the relocation type is not R_ARM_CALL or 274 // R_ARM_THM_CALL. 275 switch (type) { 276 case R_ARM_PC24: 277 case R_ARM_PLT32: 278 case R_ARM_JUMP24: 279 // Source is ARM, all PLT entries are ARM so no interworking required. 280 // Otherwise we need to interwork if STT_FUNC Symbol has bit 0 set (Thumb). 281 if (s.isFunc() && expr == R_PC && (s.getVA() & 1)) 282 return true; 283 LLVM_FALLTHROUGH; 284 case R_ARM_CALL: { 285 uint64_t dst = (expr == R_PLT_PC) ? s.getPltVA() : s.getVA(); 286 return !inBranchRange(type, branchAddr, dst); 287 } 288 case R_ARM_THM_JUMP19: 289 case R_ARM_THM_JUMP24: 290 // Source is Thumb, all PLT entries are ARM so interworking is required. 291 // Otherwise we need to interwork if STT_FUNC Symbol has bit 0 clear (ARM). 292 if (expr == R_PLT_PC || (s.isFunc() && (s.getVA() & 1) == 0)) 293 return true; 294 LLVM_FALLTHROUGH; 295 case R_ARM_THM_CALL: { 296 uint64_t dst = (expr == R_PLT_PC) ? s.getPltVA() : s.getVA(); 297 return !inBranchRange(type, branchAddr, dst); 298 } 299 } 300 return false; 301 } 302 303 uint32_t ARM::getThunkSectionSpacing() const { 304 // The placing of pre-created ThunkSections is controlled by the value 305 // thunkSectionSpacing returned by getThunkSectionSpacing(). The aim is to 306 // place the ThunkSection such that all branches from the InputSections 307 // prior to the ThunkSection can reach a Thunk placed at the end of the 308 // ThunkSection. Graphically: 309 // | up to thunkSectionSpacing .text input sections | 310 // | ThunkSection | 311 // | up to thunkSectionSpacing .text input sections | 312 // | ThunkSection | 313 314 // Pre-created ThunkSections are spaced roughly 16MiB apart on ARMv7. This 315 // is to match the most common expected case of a Thumb 2 encoded BL, BLX or 316 // B.W: 317 // ARM B, BL, BLX range +/- 32MiB 318 // Thumb B.W, BL, BLX range +/- 16MiB 319 // Thumb B<cc>.W range +/- 1MiB 320 // If a branch cannot reach a pre-created ThunkSection a new one will be 321 // created so we can handle the rare cases of a Thumb 2 conditional branch. 322 // We intentionally use a lower size for thunkSectionSpacing than the maximum 323 // branch range so the end of the ThunkSection is more likely to be within 324 // range of the branch instruction that is furthest away. The value we shorten 325 // thunkSectionSpacing by is set conservatively to allow us to create 16,384 326 // 12 byte Thunks at any offset in a ThunkSection without risk of a branch to 327 // one of the Thunks going out of range. 328 329 // On Arm the thunkSectionSpacing depends on the range of the Thumb Branch 330 // range. On earlier Architectures such as ARMv4, ARMv5 and ARMv6 (except 331 // ARMv6T2) the range is +/- 4MiB. 332 333 return (config->armJ1J2BranchEncoding) ? 0x1000000 - 0x30000 334 : 0x400000 - 0x7500; 335 } 336 337 bool ARM::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { 338 uint64_t range; 339 uint64_t instrSize; 340 341 switch (type) { 342 case R_ARM_PC24: 343 case R_ARM_PLT32: 344 case R_ARM_JUMP24: 345 case R_ARM_CALL: 346 range = 0x2000000; 347 instrSize = 4; 348 break; 349 case R_ARM_THM_JUMP19: 350 range = 0x100000; 351 instrSize = 2; 352 break; 353 case R_ARM_THM_JUMP24: 354 case R_ARM_THM_CALL: 355 range = config->armJ1J2BranchEncoding ? 0x1000000 : 0x400000; 356 instrSize = 2; 357 break; 358 default: 359 return true; 360 } 361 // PC at Src is 2 instructions ahead, immediate of branch is signed 362 if (src > dst) 363 range -= 2 * instrSize; 364 else 365 range += instrSize; 366 367 if ((dst & 0x1) == 0) 368 // Destination is ARM, if ARM caller then Src is already 4-byte aligned. 369 // If Thumb Caller (BLX) the Src address has bottom 2 bits cleared to ensure 370 // destination will be 4 byte aligned. 371 src &= ~0x3; 372 else 373 // Bit 0 == 1 denotes Thumb state, it is not part of the range 374 dst &= ~0x1; 375 376 uint64_t distance = (src > dst) ? src - dst : dst - src; 377 return distance <= range; 378 } 379 380 void ARM::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 381 switch (rel.type) { 382 case R_ARM_ABS32: 383 case R_ARM_BASE_PREL: 384 case R_ARM_GOTOFF32: 385 case R_ARM_GOT_BREL: 386 case R_ARM_GOT_PREL: 387 case R_ARM_REL32: 388 case R_ARM_RELATIVE: 389 case R_ARM_SBREL32: 390 case R_ARM_TARGET1: 391 case R_ARM_TARGET2: 392 case R_ARM_TLS_GD32: 393 case R_ARM_TLS_IE32: 394 case R_ARM_TLS_LDM32: 395 case R_ARM_TLS_LDO32: 396 case R_ARM_TLS_LE32: 397 case R_ARM_TLS_TPOFF32: 398 case R_ARM_TLS_DTPOFF32: 399 write32le(loc, val); 400 break; 401 case R_ARM_PREL31: 402 checkInt(loc, val, 31, rel); 403 write32le(loc, (read32le(loc) & 0x80000000) | (val & ~0x80000000)); 404 break; 405 case R_ARM_CALL: { 406 // R_ARM_CALL is used for BL and BLX instructions, for symbols of type 407 // STT_FUNC we choose whether to write a BL or BLX depending on the 408 // value of bit 0 of Val. With bit 0 == 1 denoting Thumb. If the symbol is 409 // not of type STT_FUNC then we must preserve the original instruction. 410 // PLT entries are always ARM state so we know we don't need to interwork. 411 bool isBlx = (read32le(loc) & 0xfe000000) == 0xfa000000; 412 bool interwork = rel.sym && rel.sym->isFunc() && rel.type != R_PLT_PC; 413 if (interwork ? val & 1 : isBlx) { 414 // The BLX encoding is 0xfa:H:imm24 where Val = imm24:H:'1' 415 checkInt(loc, val, 26, rel); 416 write32le(loc, 0xfa000000 | // opcode 417 ((val & 2) << 23) | // H 418 ((val >> 2) & 0x00ffffff)); // imm24 419 break; 420 } 421 // BLX (always unconditional) instruction to an ARM Target, select an 422 // unconditional BL. 423 write32le(loc, 0xeb000000 | (read32le(loc) & 0x00ffffff)); 424 // fall through as BL encoding is shared with B 425 } 426 LLVM_FALLTHROUGH; 427 case R_ARM_JUMP24: 428 case R_ARM_PC24: 429 case R_ARM_PLT32: 430 checkInt(loc, val, 26, rel); 431 write32le(loc, (read32le(loc) & ~0x00ffffff) | ((val >> 2) & 0x00ffffff)); 432 break; 433 case R_ARM_THM_JUMP11: 434 checkInt(loc, val, 12, rel); 435 write16le(loc, (read32le(loc) & 0xf800) | ((val >> 1) & 0x07ff)); 436 break; 437 case R_ARM_THM_JUMP19: 438 // Encoding T3: Val = S:J2:J1:imm6:imm11:0 439 checkInt(loc, val, 21, rel); 440 write16le(loc, 441 (read16le(loc) & 0xfbc0) | // opcode cond 442 ((val >> 10) & 0x0400) | // S 443 ((val >> 12) & 0x003f)); // imm6 444 write16le(loc + 2, 445 0x8000 | // opcode 446 ((val >> 8) & 0x0800) | // J2 447 ((val >> 5) & 0x2000) | // J1 448 ((val >> 1) & 0x07ff)); // imm11 449 break; 450 case R_ARM_THM_CALL: { 451 // R_ARM_THM_CALL is used for BL and BLX instructions, for symbols of type 452 // STT_FUNC we choose whether to write a BL or BLX depending on the 453 // value of bit 0 of Val. With bit 0 == 0 denoting ARM, if the symbol is 454 // not of type STT_FUNC then we must preserve the original instruction. 455 // PLT entries are always ARM state so we know we need to interwork. 456 bool isBlx = (read16le(loc + 2) & 0x1000) == 0; 457 bool interwork = (rel.sym && rel.sym->isFunc()) || rel.type == R_PLT_PC; 458 if (interwork ? (val & 1) == 0 : isBlx) { 459 // We are writing a BLX. Ensure BLX destination is 4-byte aligned. As 460 // the BLX instruction may only be two byte aligned. This must be done 461 // before overflow check. 462 val = alignTo(val, 4); 463 write16le(loc + 2, read16le(loc + 2) & ~0x1000); 464 } else { 465 write16le(loc + 2, (read16le(loc + 2) & ~0x1000) | 1 << 12); 466 } 467 if (!config->armJ1J2BranchEncoding) { 468 // Older Arm architectures do not support R_ARM_THM_JUMP24 and have 469 // different encoding rules and range due to J1 and J2 always being 1. 470 checkInt(loc, val, 23, rel); 471 write16le(loc, 472 0xf000 | // opcode 473 ((val >> 12) & 0x07ff)); // imm11 474 write16le(loc + 2, 475 (read16le(loc + 2) & 0xd000) | // opcode 476 0x2800 | // J1 == J2 == 1 477 ((val >> 1) & 0x07ff)); // imm11 478 break; 479 } 480 } 481 // Fall through as rest of encoding is the same as B.W 482 LLVM_FALLTHROUGH; 483 case R_ARM_THM_JUMP24: 484 // Encoding B T4, BL T1, BLX T2: Val = S:I1:I2:imm10:imm11:0 485 checkInt(loc, val, 25, rel); 486 write16le(loc, 487 0xf000 | // opcode 488 ((val >> 14) & 0x0400) | // S 489 ((val >> 12) & 0x03ff)); // imm10 490 write16le(loc + 2, 491 (read16le(loc + 2) & 0xd000) | // opcode 492 (((~(val >> 10)) ^ (val >> 11)) & 0x2000) | // J1 493 (((~(val >> 11)) ^ (val >> 13)) & 0x0800) | // J2 494 ((val >> 1) & 0x07ff)); // imm11 495 break; 496 case R_ARM_MOVW_ABS_NC: 497 case R_ARM_MOVW_PREL_NC: 498 write32le(loc, (read32le(loc) & ~0x000f0fff) | ((val & 0xf000) << 4) | 499 (val & 0x0fff)); 500 break; 501 case R_ARM_MOVT_ABS: 502 case R_ARM_MOVT_PREL: 503 write32le(loc, (read32le(loc) & ~0x000f0fff) | 504 (((val >> 16) & 0xf000) << 4) | ((val >> 16) & 0xfff)); 505 break; 506 case R_ARM_THM_MOVT_ABS: 507 case R_ARM_THM_MOVT_PREL: 508 // Encoding T1: A = imm4:i:imm3:imm8 509 write16le(loc, 510 0xf2c0 | // opcode 511 ((val >> 17) & 0x0400) | // i 512 ((val >> 28) & 0x000f)); // imm4 513 write16le(loc + 2, 514 (read16le(loc + 2) & 0x8f00) | // opcode 515 ((val >> 12) & 0x7000) | // imm3 516 ((val >> 16) & 0x00ff)); // imm8 517 break; 518 case R_ARM_THM_MOVW_ABS_NC: 519 case R_ARM_THM_MOVW_PREL_NC: 520 // Encoding T3: A = imm4:i:imm3:imm8 521 write16le(loc, 522 0xf240 | // opcode 523 ((val >> 1) & 0x0400) | // i 524 ((val >> 12) & 0x000f)); // imm4 525 write16le(loc + 2, 526 (read16le(loc + 2) & 0x8f00) | // opcode 527 ((val << 4) & 0x7000) | // imm3 528 (val & 0x00ff)); // imm8 529 break; 530 default: 531 error(getErrorLocation(loc) + "unrecognized relocation " + 532 toString(rel.type)); 533 } 534 } 535 536 int64_t ARM::getImplicitAddend(const uint8_t *buf, RelType type) const { 537 switch (type) { 538 default: 539 return 0; 540 case R_ARM_ABS32: 541 case R_ARM_BASE_PREL: 542 case R_ARM_GOTOFF32: 543 case R_ARM_GOT_BREL: 544 case R_ARM_GOT_PREL: 545 case R_ARM_REL32: 546 case R_ARM_TARGET1: 547 case R_ARM_TARGET2: 548 case R_ARM_TLS_GD32: 549 case R_ARM_TLS_LDM32: 550 case R_ARM_TLS_LDO32: 551 case R_ARM_TLS_IE32: 552 case R_ARM_TLS_LE32: 553 return SignExtend64<32>(read32le(buf)); 554 case R_ARM_PREL31: 555 return SignExtend64<31>(read32le(buf)); 556 case R_ARM_CALL: 557 case R_ARM_JUMP24: 558 case R_ARM_PC24: 559 case R_ARM_PLT32: 560 return SignExtend64<26>(read32le(buf) << 2); 561 case R_ARM_THM_JUMP11: 562 return SignExtend64<12>(read16le(buf) << 1); 563 case R_ARM_THM_JUMP19: { 564 // Encoding T3: A = S:J2:J1:imm10:imm6:0 565 uint16_t hi = read16le(buf); 566 uint16_t lo = read16le(buf + 2); 567 return SignExtend64<20>(((hi & 0x0400) << 10) | // S 568 ((lo & 0x0800) << 8) | // J2 569 ((lo & 0x2000) << 5) | // J1 570 ((hi & 0x003f) << 12) | // imm6 571 ((lo & 0x07ff) << 1)); // imm11:0 572 } 573 case R_ARM_THM_CALL: 574 if (!config->armJ1J2BranchEncoding) { 575 // Older Arm architectures do not support R_ARM_THM_JUMP24 and have 576 // different encoding rules and range due to J1 and J2 always being 1. 577 uint16_t hi = read16le(buf); 578 uint16_t lo = read16le(buf + 2); 579 return SignExtend64<22>(((hi & 0x7ff) << 12) | // imm11 580 ((lo & 0x7ff) << 1)); // imm11:0 581 break; 582 } 583 LLVM_FALLTHROUGH; 584 case R_ARM_THM_JUMP24: { 585 // Encoding B T4, BL T1, BLX T2: A = S:I1:I2:imm10:imm11:0 586 // I1 = NOT(J1 EOR S), I2 = NOT(J2 EOR S) 587 uint16_t hi = read16le(buf); 588 uint16_t lo = read16le(buf + 2); 589 return SignExtend64<24>(((hi & 0x0400) << 14) | // S 590 (~((lo ^ (hi << 3)) << 10) & 0x00800000) | // I1 591 (~((lo ^ (hi << 1)) << 11) & 0x00400000) | // I2 592 ((hi & 0x003ff) << 12) | // imm0 593 ((lo & 0x007ff) << 1)); // imm11:0 594 } 595 // ELF for the ARM Architecture 4.6.1.1 the implicit addend for MOVW and 596 // MOVT is in the range -32768 <= A < 32768 597 case R_ARM_MOVW_ABS_NC: 598 case R_ARM_MOVT_ABS: 599 case R_ARM_MOVW_PREL_NC: 600 case R_ARM_MOVT_PREL: { 601 uint64_t val = read32le(buf) & 0x000f0fff; 602 return SignExtend64<16>(((val & 0x000f0000) >> 4) | (val & 0x00fff)); 603 } 604 case R_ARM_THM_MOVW_ABS_NC: 605 case R_ARM_THM_MOVT_ABS: 606 case R_ARM_THM_MOVW_PREL_NC: 607 case R_ARM_THM_MOVT_PREL: { 608 // Encoding T3: A = imm4:i:imm3:imm8 609 uint16_t hi = read16le(buf); 610 uint16_t lo = read16le(buf + 2); 611 return SignExtend64<16>(((hi & 0x000f) << 12) | // imm4 612 ((hi & 0x0400) << 1) | // i 613 ((lo & 0x7000) >> 4) | // imm3 614 (lo & 0x00ff)); // imm8 615 } 616 } 617 } 618 619 TargetInfo *getARMTargetInfo() { 620 static ARM target; 621 return ⌖ 622 } 623 624 } // namespace elf 625 } // namespace lld 626