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