1 //===- ARM.cpp ------------------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "InputFiles.h" 11 #include "Symbols.h" 12 #include "SyntheticSections.h" 13 #include "Target.h" 14 #include "Thunks.h" 15 #include "lld/Common/ErrorHandler.h" 16 #include "llvm/Object/ELF.h" 17 #include "llvm/Support/Endian.h" 18 19 using namespace llvm; 20 using namespace llvm::support::endian; 21 using namespace llvm::ELF; 22 using namespace lld; 23 using namespace lld::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, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr, 38 int32_t Index, unsigned RelOff) const override; 39 void addPltSymbols(InputSection &IS, 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) const override; 43 uint32_t getThunkSectionSpacing() const override; 44 bool inBranchRange(RelType Type, uint64_t Src, uint64_t Dst) const override; 45 void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override; 46 }; 47 } // namespace 48 49 ARM::ARM() { 50 CopyRel = R_ARM_COPY; 51 RelativeRel = R_ARM_RELATIVE; 52 IRelativeRel = R_ARM_IRELATIVE; 53 GotRel = R_ARM_GLOB_DAT; 54 NoneRel = R_ARM_NONE; 55 PltRel = R_ARM_JUMP_SLOT; 56 TlsGotRel = R_ARM_TLS_TPOFF32; 57 TlsModuleIndexRel = R_ARM_TLS_DTPMOD32; 58 TlsOffsetRel = R_ARM_TLS_DTPOFF32; 59 GotBaseSymInGotPlt = false; 60 GotEntrySize = 4; 61 GotPltEntrySize = 4; 62 PltEntrySize = 16; 63 PltHeaderSize = 32; 64 TrapInstr = {0xd4, 0xd4, 0xd4, 0xd4}; 65 NeedsThunks = true; 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_THM_JUMP11: 88 return R_PC; 89 case R_ARM_CALL: 90 case R_ARM_JUMP24: 91 case R_ARM_PC24: 92 case R_ARM_PLT32: 93 case R_ARM_PREL31: 94 case R_ARM_THM_JUMP19: 95 case R_ARM_THM_JUMP24: 96 case R_ARM_THM_CALL: 97 return R_PLT_PC; 98 case R_ARM_GOTOFF32: 99 // (S + A) - GOT_ORG 100 return R_GOTREL; 101 case R_ARM_GOT_BREL: 102 // GOT(S) + A - GOT_ORG 103 return R_GOT_OFF; 104 case R_ARM_GOT_PREL: 105 case R_ARM_TLS_IE32: 106 // GOT(S) + A - P 107 return R_GOT_PC; 108 case R_ARM_SBREL32: 109 return R_ARM_SBREL; 110 case R_ARM_TARGET1: 111 return Config->Target1Rel ? R_PC : R_ABS; 112 case R_ARM_TARGET2: 113 if (Config->Target2 == Target2Policy::Rel) 114 return R_PC; 115 if (Config->Target2 == Target2Policy::Abs) 116 return R_ABS; 117 return R_GOT_PC; 118 case R_ARM_TLS_GD32: 119 return R_TLSGD_PC; 120 case R_ARM_TLS_LDM32: 121 return R_TLSLD_PC; 122 case R_ARM_BASE_PREL: 123 // B(S) + A - P 124 // FIXME: currently B(S) assumed to be .got, this may not hold for all 125 // platforms. 126 return R_GOTONLY_PC; 127 case R_ARM_MOVW_PREL_NC: 128 case R_ARM_MOVT_PREL: 129 case R_ARM_REL32: 130 case R_ARM_THM_MOVW_PREL_NC: 131 case R_ARM_THM_MOVT_PREL: 132 return R_PC; 133 case R_ARM_NONE: 134 return R_NONE; 135 case R_ARM_TLS_LE32: 136 return R_TLS; 137 default: 138 return R_ABS; 139 } 140 } 141 142 RelType ARM::getDynRel(RelType Type) const { 143 if ((Type == R_ARM_ABS32) || (Type == R_ARM_TARGET1 && !Config->Target1Rel)) 144 return R_ARM_ABS32; 145 return R_ARM_NONE; 146 } 147 148 void ARM::writeGotPlt(uint8_t *Buf, const Symbol &) const { 149 write32le(Buf, In.Plt->getVA()); 150 } 151 152 void ARM::writeIgotPlt(uint8_t *Buf, const Symbol &S) const { 153 // An ARM entry is the address of the ifunc resolver function. 154 write32le(Buf, S.getVA()); 155 } 156 157 // Long form PLT Header that does not have any restrictions on the displacement 158 // of the .plt from the .plt.got. 159 static void writePltHeaderLong(uint8_t *Buf) { 160 const uint8_t PltData[] = { 161 0x04, 0xe0, 0x2d, 0xe5, // str lr, [sp,#-4]! 162 0x04, 0xe0, 0x9f, 0xe5, // ldr lr, L2 163 0x0e, 0xe0, 0x8f, 0xe0, // L1: add lr, pc, lr 164 0x08, 0xf0, 0xbe, 0xe5, // ldr pc, [lr, #8] 165 0x00, 0x00, 0x00, 0x00, // L2: .word &(.got.plt) - L1 - 8 166 0xd4, 0xd4, 0xd4, 0xd4, // Pad to 32-byte boundary 167 0xd4, 0xd4, 0xd4, 0xd4, // Pad to 32-byte boundary 168 0xd4, 0xd4, 0xd4, 0xd4}; 169 memcpy(Buf, PltData, sizeof(PltData)); 170 uint64_t GotPlt = In.GotPlt->getVA(); 171 uint64_t L1 = In.Plt->getVA() + 8; 172 write32le(Buf + 16, GotPlt - L1 - 8); 173 } 174 175 // The default PLT header requires the .plt.got to be within 128 Mb of the 176 // .plt in the positive direction. 177 void ARM::writePltHeader(uint8_t *Buf) const { 178 // Use a similar sequence to that in writePlt(), the difference is the calling 179 // conventions mean we use lr instead of ip. The PLT entry is responsible for 180 // saving lr on the stack, the dynamic loader is responsible for reloading 181 // it. 182 const uint32_t PltData[] = { 183 0xe52de004, // L1: str lr, [sp,#-4]! 184 0xe28fe600, // add lr, pc, #0x0NN00000 &(.got.plt - L1 - 4) 185 0xe28eea00, // add lr, lr, #0x000NN000 &(.got.plt - L1 - 4) 186 0xe5bef000, // ldr pc, [lr, #0x00000NNN] &(.got.plt -L1 - 4) 187 }; 188 189 uint64_t Offset = In.GotPlt->getVA() - In.Plt->getVA() - 4; 190 if (!llvm::isUInt<27>(Offset)) { 191 // We cannot encode the Offset, use the long form. 192 writePltHeaderLong(Buf); 193 return; 194 } 195 write32le(Buf + 0, PltData[0]); 196 write32le(Buf + 4, PltData[1] | ((Offset >> 20) & 0xff)); 197 write32le(Buf + 8, PltData[2] | ((Offset >> 12) & 0xff)); 198 write32le(Buf + 12, PltData[3] | (Offset & 0xfff)); 199 memcpy(Buf + 16, TrapInstr.data(), 4); // Pad to 32-byte boundary 200 memcpy(Buf + 20, TrapInstr.data(), 4); 201 memcpy(Buf + 24, TrapInstr.data(), 4); 202 memcpy(Buf + 28, TrapInstr.data(), 4); 203 } 204 205 void ARM::addPltHeaderSymbols(InputSection &IS) const { 206 addSyntheticLocal("$a", STT_NOTYPE, 0, 0, IS); 207 addSyntheticLocal("$d", STT_NOTYPE, 16, 0, IS); 208 } 209 210 // Long form PLT entries that do not have any restrictions on the displacement 211 // of the .plt from the .plt.got. 212 static void writePltLong(uint8_t *Buf, uint64_t GotPltEntryAddr, 213 uint64_t PltEntryAddr, int32_t Index, 214 unsigned RelOff) { 215 const uint8_t PltData[] = { 216 0x04, 0xc0, 0x9f, 0xe5, // ldr ip, L2 217 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc 218 0x00, 0xf0, 0x9c, 0xe5, // ldr pc, [ip] 219 0x00, 0x00, 0x00, 0x00, // L2: .word Offset(&(.plt.got) - L1 - 8 220 }; 221 memcpy(Buf, PltData, sizeof(PltData)); 222 uint64_t L1 = PltEntryAddr + 4; 223 write32le(Buf + 12, GotPltEntryAddr - L1 - 8); 224 } 225 226 // The default PLT entries require the .plt.got to be within 128 Mb of the 227 // .plt in the positive direction. 228 void ARM::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, 229 uint64_t PltEntryAddr, int32_t Index, 230 unsigned RelOff) const { 231 // The PLT entry is similar to the example given in Appendix A of ELF for 232 // the Arm Architecture. Instead of using the Group Relocations to find the 233 // optimal rotation for the 8-bit immediate used in the add instructions we 234 // hard code the most compact rotations for simplicity. This saves a load 235 // instruction over the long plt sequences. 236 const uint32_t PltData[] = { 237 0xe28fc600, // L1: add ip, pc, #0x0NN00000 Offset(&(.plt.got) - L1 - 8 238 0xe28cca00, // add ip, ip, #0x000NN000 Offset(&(.plt.got) - L1 - 8 239 0xe5bcf000, // ldr pc, [ip, #0x00000NNN] Offset(&(.plt.got) - L1 - 8 240 }; 241 242 uint64_t Offset = GotPltEntryAddr - PltEntryAddr - 8; 243 if (!llvm::isUInt<27>(Offset)) { 244 // We cannot encode the Offset, use the long form. 245 writePltLong(Buf, GotPltEntryAddr, PltEntryAddr, Index, RelOff); 246 return; 247 } 248 write32le(Buf + 0, PltData[0] | ((Offset >> 20) & 0xff)); 249 write32le(Buf + 4, PltData[1] | ((Offset >> 12) & 0xff)); 250 write32le(Buf + 8, PltData[2] | (Offset & 0xfff)); 251 memcpy(Buf + 12, TrapInstr.data(), 4); // Pad to 16-byte boundary 252 } 253 254 void ARM::addPltSymbols(InputSection &IS, uint64_t Off) const { 255 addSyntheticLocal("$a", STT_NOTYPE, Off, 0, IS); 256 addSyntheticLocal("$d", STT_NOTYPE, Off + 12, 0, IS); 257 } 258 259 bool ARM::needsThunk(RelExpr Expr, RelType Type, const InputFile *File, 260 uint64_t BranchAddr, const Symbol &S) const { 261 // If S is an undefined weak symbol and does not have a PLT entry then it 262 // will be resolved as a branch to the next instruction. 263 if (S.isUndefWeak() && !S.isInPlt()) 264 return false; 265 // A state change from ARM to Thumb and vice versa must go through an 266 // interworking thunk if the relocation type is not R_ARM_CALL or 267 // R_ARM_THM_CALL. 268 switch (Type) { 269 case R_ARM_PC24: 270 case R_ARM_PLT32: 271 case R_ARM_JUMP24: 272 // Source is ARM, all PLT entries are ARM so no interworking required. 273 // Otherwise we need to interwork if Symbol has bit 0 set (Thumb). 274 if (Expr == R_PC && ((S.getVA() & 1) == 1)) 275 return true; 276 LLVM_FALLTHROUGH; 277 case R_ARM_CALL: { 278 uint64_t Dst = (Expr == R_PLT_PC) ? S.getPltVA() : S.getVA(); 279 return !inBranchRange(Type, BranchAddr, Dst); 280 } 281 case R_ARM_THM_JUMP19: 282 case R_ARM_THM_JUMP24: 283 // Source is Thumb, all PLT entries are ARM so interworking is required. 284 // Otherwise we need to interwork if Symbol has bit 0 clear (ARM). 285 if (Expr == R_PLT_PC || ((S.getVA() & 1) == 0)) 286 return true; 287 LLVM_FALLTHROUGH; 288 case R_ARM_THM_CALL: { 289 uint64_t Dst = (Expr == R_PLT_PC) ? S.getPltVA() : S.getVA(); 290 return !inBranchRange(Type, BranchAddr, Dst); 291 } 292 } 293 return false; 294 } 295 296 uint32_t ARM::getThunkSectionSpacing() const { 297 // The placing of pre-created ThunkSections is controlled by the value 298 // ThunkSectionSpacing returned by getThunkSectionSpacing(). The aim is to 299 // place the ThunkSection such that all branches from the InputSections 300 // prior to the ThunkSection can reach a Thunk placed at the end of the 301 // ThunkSection. Graphically: 302 // | up to ThunkSectionSpacing .text input sections | 303 // | ThunkSection | 304 // | up to ThunkSectionSpacing .text input sections | 305 // | ThunkSection | 306 307 // Pre-created ThunkSections are spaced roughly 16MiB apart on ARMv7. This 308 // is to match the most common expected case of a Thumb 2 encoded BL, BLX or 309 // B.W: 310 // ARM B, BL, BLX range +/- 32MiB 311 // Thumb B.W, BL, BLX range +/- 16MiB 312 // Thumb B<cc>.W range +/- 1MiB 313 // If a branch cannot reach a pre-created ThunkSection a new one will be 314 // created so we can handle the rare cases of a Thumb 2 conditional branch. 315 // We intentionally use a lower size for ThunkSectionSpacing than the maximum 316 // branch range so the end of the ThunkSection is more likely to be within 317 // range of the branch instruction that is furthest away. The value we shorten 318 // ThunkSectionSpacing by is set conservatively to allow us to create 16,384 319 // 12 byte Thunks at any offset in a ThunkSection without risk of a branch to 320 // one of the Thunks going out of range. 321 322 // On Arm the ThunkSectionSpacing depends on the range of the Thumb Branch 323 // range. On earlier Architectures such as ARMv4, ARMv5 and ARMv6 (except 324 // ARMv6T2) the range is +/- 4MiB. 325 326 return (Config->ARMJ1J2BranchEncoding) ? 0x1000000 - 0x30000 327 : 0x400000 - 0x7500; 328 } 329 330 bool ARM::inBranchRange(RelType Type, uint64_t Src, uint64_t Dst) const { 331 uint64_t Range; 332 uint64_t InstrSize; 333 334 switch (Type) { 335 case R_ARM_PC24: 336 case R_ARM_PLT32: 337 case R_ARM_JUMP24: 338 case R_ARM_CALL: 339 Range = 0x2000000; 340 InstrSize = 4; 341 break; 342 case R_ARM_THM_JUMP19: 343 Range = 0x100000; 344 InstrSize = 2; 345 break; 346 case R_ARM_THM_JUMP24: 347 case R_ARM_THM_CALL: 348 Range = Config->ARMJ1J2BranchEncoding ? 0x1000000 : 0x400000; 349 InstrSize = 2; 350 break; 351 default: 352 return true; 353 } 354 // PC at Src is 2 instructions ahead, immediate of branch is signed 355 if (Src > Dst) 356 Range -= 2 * InstrSize; 357 else 358 Range += InstrSize; 359 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 uint64_t Distance = (Src > Dst) ? Src - Dst : Dst - Src; 370 return Distance <= Range; 371 } 372 373 void ARM::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const { 374 switch (Type) { 375 case R_ARM_ABS32: 376 case R_ARM_BASE_PREL: 377 case R_ARM_GLOB_DAT: 378 case R_ARM_GOTOFF32: 379 case R_ARM_GOT_BREL: 380 case R_ARM_GOT_PREL: 381 case R_ARM_REL32: 382 case R_ARM_RELATIVE: 383 case R_ARM_SBREL32: 384 case R_ARM_TARGET1: 385 case R_ARM_TARGET2: 386 case R_ARM_TLS_GD32: 387 case R_ARM_TLS_IE32: 388 case R_ARM_TLS_LDM32: 389 case R_ARM_TLS_LDO32: 390 case R_ARM_TLS_LE32: 391 case R_ARM_TLS_TPOFF32: 392 case R_ARM_TLS_DTPOFF32: 393 write32le(Loc, Val); 394 break; 395 case R_ARM_TLS_DTPMOD32: 396 write32le(Loc, 1); 397 break; 398 case R_ARM_PREL31: 399 checkInt(Loc, Val, 31, Type); 400 write32le(Loc, (read32le(Loc) & 0x80000000) | (Val & ~0x80000000)); 401 break; 402 case R_ARM_CALL: 403 // R_ARM_CALL is used for BL and BLX instructions, depending on the 404 // value of bit 0 of Val, we must select a BL or BLX instruction 405 if (Val & 1) { 406 // If bit 0 of Val is 1 the target is Thumb, we must select a BLX. 407 // The BLX encoding is 0xfa:H:imm24 where Val = imm24:H:'1' 408 checkInt(Loc, Val, 26, Type); 409 write32le(Loc, 0xfa000000 | // opcode 410 ((Val & 2) << 23) | // H 411 ((Val >> 2) & 0x00ffffff)); // imm24 412 break; 413 } 414 if ((read32le(Loc) & 0xfe000000) == 0xfa000000) 415 // BLX (always unconditional) instruction to an ARM Target, select an 416 // unconditional BL. 417 write32le(Loc, 0xeb000000 | (read32le(Loc) & 0x00ffffff)); 418 // fall through as BL encoding is shared with B 419 LLVM_FALLTHROUGH; 420 case R_ARM_JUMP24: 421 case R_ARM_PC24: 422 case R_ARM_PLT32: 423 checkInt(Loc, Val, 26, Type); 424 write32le(Loc, (read32le(Loc) & ~0x00ffffff) | ((Val >> 2) & 0x00ffffff)); 425 break; 426 case R_ARM_THM_JUMP11: 427 checkInt(Loc, Val, 12, Type); 428 write16le(Loc, (read32le(Loc) & 0xf800) | ((Val >> 1) & 0x07ff)); 429 break; 430 case R_ARM_THM_JUMP19: 431 // Encoding T3: Val = S:J2:J1:imm6:imm11:0 432 checkInt(Loc, Val, 21, Type); 433 write16le(Loc, 434 (read16le(Loc) & 0xfbc0) | // opcode cond 435 ((Val >> 10) & 0x0400) | // S 436 ((Val >> 12) & 0x003f)); // imm6 437 write16le(Loc + 2, 438 0x8000 | // opcode 439 ((Val >> 8) & 0x0800) | // J2 440 ((Val >> 5) & 0x2000) | // J1 441 ((Val >> 1) & 0x07ff)); // imm11 442 break; 443 case R_ARM_THM_CALL: 444 // R_ARM_THM_CALL is used for BL and BLX instructions, depending on the 445 // value of bit 0 of Val, we must select a BL or BLX instruction 446 if ((Val & 1) == 0) { 447 // Ensure BLX destination is 4-byte aligned. As BLX instruction may 448 // only be two byte aligned. This must be done before overflow check 449 Val = alignTo(Val, 4); 450 } 451 // Bit 12 is 0 for BLX, 1 for BL 452 write16le(Loc + 2, (read16le(Loc + 2) & ~0x1000) | (Val & 1) << 12); 453 if (!Config->ARMJ1J2BranchEncoding) { 454 // Older Arm architectures do not support R_ARM_THM_JUMP24 and have 455 // different encoding rules and range due to J1 and J2 always being 1. 456 checkInt(Loc, Val, 23, Type); 457 write16le(Loc, 458 0xf000 | // opcode 459 ((Val >> 12) & 0x07ff)); // imm11 460 write16le(Loc + 2, 461 (read16le(Loc + 2) & 0xd000) | // opcode 462 0x2800 | // J1 == J2 == 1 463 ((Val >> 1) & 0x07ff)); // imm11 464 break; 465 } 466 // Fall through as rest of encoding is the same as B.W 467 LLVM_FALLTHROUGH; 468 case R_ARM_THM_JUMP24: 469 // Encoding B T4, BL T1, BLX T2: Val = S:I1:I2:imm10:imm11:0 470 checkInt(Loc, Val, 25, Type); 471 write16le(Loc, 472 0xf000 | // opcode 473 ((Val >> 14) & 0x0400) | // S 474 ((Val >> 12) & 0x03ff)); // imm10 475 write16le(Loc + 2, 476 (read16le(Loc + 2) & 0xd000) | // opcode 477 (((~(Val >> 10)) ^ (Val >> 11)) & 0x2000) | // J1 478 (((~(Val >> 11)) ^ (Val >> 13)) & 0x0800) | // J2 479 ((Val >> 1) & 0x07ff)); // imm11 480 break; 481 case R_ARM_MOVW_ABS_NC: 482 case R_ARM_MOVW_PREL_NC: 483 write32le(Loc, (read32le(Loc) & ~0x000f0fff) | ((Val & 0xf000) << 4) | 484 (Val & 0x0fff)); 485 break; 486 case R_ARM_MOVT_ABS: 487 case R_ARM_MOVT_PREL: 488 checkInt(Loc, Val, 32, Type); 489 write32le(Loc, (read32le(Loc) & ~0x000f0fff) | 490 (((Val >> 16) & 0xf000) << 4) | ((Val >> 16) & 0xfff)); 491 break; 492 case R_ARM_THM_MOVT_ABS: 493 case R_ARM_THM_MOVT_PREL: 494 // Encoding T1: A = imm4:i:imm3:imm8 495 checkInt(Loc, Val, 32, Type); 496 write16le(Loc, 497 0xf2c0 | // opcode 498 ((Val >> 17) & 0x0400) | // i 499 ((Val >> 28) & 0x000f)); // imm4 500 write16le(Loc + 2, 501 (read16le(Loc + 2) & 0x8f00) | // opcode 502 ((Val >> 12) & 0x7000) | // imm3 503 ((Val >> 16) & 0x00ff)); // imm8 504 break; 505 case R_ARM_THM_MOVW_ABS_NC: 506 case R_ARM_THM_MOVW_PREL_NC: 507 // Encoding T3: A = imm4:i:imm3:imm8 508 write16le(Loc, 509 0xf240 | // opcode 510 ((Val >> 1) & 0x0400) | // i 511 ((Val >> 12) & 0x000f)); // imm4 512 write16le(Loc + 2, 513 (read16le(Loc + 2) & 0x8f00) | // opcode 514 ((Val << 4) & 0x7000) | // imm3 515 (Val & 0x00ff)); // imm8 516 break; 517 default: 518 error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type)); 519 } 520 } 521 522 int64_t ARM::getImplicitAddend(const uint8_t *Buf, RelType Type) const { 523 switch (Type) { 524 default: 525 return 0; 526 case R_ARM_ABS32: 527 case R_ARM_BASE_PREL: 528 case R_ARM_GOTOFF32: 529 case R_ARM_GOT_BREL: 530 case R_ARM_GOT_PREL: 531 case R_ARM_REL32: 532 case R_ARM_TARGET1: 533 case R_ARM_TARGET2: 534 case R_ARM_TLS_GD32: 535 case R_ARM_TLS_LDM32: 536 case R_ARM_TLS_LDO32: 537 case R_ARM_TLS_IE32: 538 case R_ARM_TLS_LE32: 539 return SignExtend64<32>(read32le(Buf)); 540 case R_ARM_PREL31: 541 return SignExtend64<31>(read32le(Buf)); 542 case R_ARM_CALL: 543 case R_ARM_JUMP24: 544 case R_ARM_PC24: 545 case R_ARM_PLT32: 546 return SignExtend64<26>(read32le(Buf) << 2); 547 case R_ARM_THM_JUMP11: 548 return SignExtend64<12>(read16le(Buf) << 1); 549 case R_ARM_THM_JUMP19: { 550 // Encoding T3: A = S:J2:J1:imm10:imm6:0 551 uint16_t Hi = read16le(Buf); 552 uint16_t Lo = read16le(Buf + 2); 553 return SignExtend64<20>(((Hi & 0x0400) << 10) | // S 554 ((Lo & 0x0800) << 8) | // J2 555 ((Lo & 0x2000) << 5) | // J1 556 ((Hi & 0x003f) << 12) | // imm6 557 ((Lo & 0x07ff) << 1)); // imm11:0 558 } 559 case R_ARM_THM_CALL: 560 if (!Config->ARMJ1J2BranchEncoding) { 561 // Older Arm architectures do not support R_ARM_THM_JUMP24 and have 562 // different encoding rules and range due to J1 and J2 always being 1. 563 uint16_t Hi = read16le(Buf); 564 uint16_t Lo = read16le(Buf + 2); 565 return SignExtend64<22>(((Hi & 0x7ff) << 12) | // imm11 566 ((Lo & 0x7ff) << 1)); // imm11:0 567 break; 568 } 569 LLVM_FALLTHROUGH; 570 case R_ARM_THM_JUMP24: { 571 // Encoding B T4, BL T1, BLX T2: A = S:I1:I2:imm10:imm11:0 572 // I1 = NOT(J1 EOR S), I2 = NOT(J2 EOR S) 573 uint16_t Hi = read16le(Buf); 574 uint16_t Lo = read16le(Buf + 2); 575 return SignExtend64<24>(((Hi & 0x0400) << 14) | // S 576 (~((Lo ^ (Hi << 3)) << 10) & 0x00800000) | // I1 577 (~((Lo ^ (Hi << 1)) << 11) & 0x00400000) | // I2 578 ((Hi & 0x003ff) << 12) | // imm0 579 ((Lo & 0x007ff) << 1)); // imm11:0 580 } 581 // ELF for the ARM Architecture 4.6.1.1 the implicit addend for MOVW and 582 // MOVT is in the range -32768 <= A < 32768 583 case R_ARM_MOVW_ABS_NC: 584 case R_ARM_MOVT_ABS: 585 case R_ARM_MOVW_PREL_NC: 586 case R_ARM_MOVT_PREL: { 587 uint64_t Val = read32le(Buf) & 0x000f0fff; 588 return SignExtend64<16>(((Val & 0x000f0000) >> 4) | (Val & 0x00fff)); 589 } 590 case R_ARM_THM_MOVW_ABS_NC: 591 case R_ARM_THM_MOVT_ABS: 592 case R_ARM_THM_MOVW_PREL_NC: 593 case R_ARM_THM_MOVT_PREL: { 594 // Encoding T3: A = imm4:i:imm3:imm8 595 uint16_t Hi = read16le(Buf); 596 uint16_t Lo = read16le(Buf + 2); 597 return SignExtend64<16>(((Hi & 0x000f) << 12) | // imm4 598 ((Hi & 0x0400) << 1) | // i 599 ((Lo & 0x7000) >> 4) | // imm3 600 (Lo & 0x00ff)); // imm8 601 } 602 } 603 } 604 605 TargetInfo *elf::getARMTargetInfo() { 606 static ARM Target; 607 return &Target; 608 } 609