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 bool isPicRel(RelType Type) const override; 33 RelType getDynRel(RelType Type) const override; 34 int64_t getImplicitAddend(const uint8_t *Buf, RelType Type) const override; 35 void writeGotPlt(uint8_t *Buf, const Symbol &S) const override; 36 void writeIgotPlt(uint8_t *Buf, const Symbol &S) const override; 37 void writePltHeader(uint8_t *Buf) const override; 38 void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr, 39 int32_t Index, unsigned RelOff) const override; 40 void addPltSymbols(InputSectionBase *IS, uint64_t Off) const override; 41 void addPltHeaderSymbols(InputSectionBase *ISD) const override; 42 bool needsThunk(RelExpr Expr, RelType Type, const InputFile *File, 43 uint64_t BranchAddr, const Symbol &S) 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 PltRel = R_ARM_JUMP_SLOT; 55 TlsGotRel = R_ARM_TLS_TPOFF32; 56 TlsModuleIndexRel = R_ARM_TLS_DTPMOD32; 57 TlsOffsetRel = R_ARM_TLS_DTPOFF32; 58 GotEntrySize = 4; 59 GotPltEntrySize = 4; 60 PltEntrySize = 16; 61 PltHeaderSize = 20; 62 TrapInstr = 0xd4d4d4d4; 63 // ARM uses Variant 1 TLS 64 TcbSize = 8; 65 NeedsThunks = true; 66 67 // The placing of pre-created ThunkSections is controlled by the 68 // ThunkSectionSpacing parameter. The aim is to place the 69 // ThunkSection such that all branches from the InputSections prior to the 70 // ThunkSection can reach a Thunk placed at the end of the ThunkSection. 71 // Graphically: 72 // | up to ThunkSectionSpacing .text input sections | 73 // | ThunkSection | 74 // | up to ThunkSectionSpacing .text input sections | 75 // | ThunkSection | 76 77 // Pre-created ThunkSections are spaced roughly 16MiB apart on ARM. This is to 78 // match the most common expected case of a Thumb 2 encoded BL, BLX or B.W 79 // ARM B, BL, BLX range +/- 32MiB 80 // Thumb B.W, BL, BLX range +/- 16MiB 81 // Thumb B<cc>.W range +/- 1MiB 82 // If a branch cannot reach a pre-created ThunkSection a new one will be 83 // created so we can handle the rare cases of a Thumb 2 conditional branch. 84 // We intentionally use a lower size for ThunkSectionSpacing than the maximum 85 // branch range so the end of the ThunkSection is more likely to be within 86 // range of the branch instruction that is furthest away. The value we shorten 87 // ThunkSectionSpacing by is set conservatively to allow us to create 16,384 88 // 12 byte Thunks at any offset in a ThunkSection without risk of a branch to 89 // one of the Thunks going out of range. 90 91 // FIXME: lld assumes that the Thumb BL and BLX encoding permits the J1 and 92 // J2 bits to be used to extend the branch range. On earlier Architectures 93 // such as ARMv4, ARMv5 and ARMv6 (except ARMv6T2) the range is +/- 4MiB. If 94 // support for the earlier encodings is added then when they are used the 95 // ThunkSectionSpacing will need lowering. 96 ThunkSectionSpacing = 0x1000000 - 0x30000; 97 } 98 99 uint32_t ARM::calcEFlags() const { 100 // We don't currently use any features incompatible with EF_ARM_EABI_VER5, 101 // but we don't have any firm guarantees of conformance. Linux AArch64 102 // kernels (as of 2016) require an EABI version to be set. 103 return EF_ARM_EABI_VER5; 104 } 105 106 RelExpr ARM::getRelExpr(RelType Type, const Symbol &S, 107 const uint8_t *Loc) const { 108 switch (Type) { 109 case R_ARM_THM_JUMP11: 110 return R_PC; 111 case R_ARM_CALL: 112 case R_ARM_JUMP24: 113 case R_ARM_PC24: 114 case R_ARM_PLT32: 115 case R_ARM_PREL31: 116 case R_ARM_THM_JUMP19: 117 case R_ARM_THM_JUMP24: 118 case R_ARM_THM_CALL: 119 return R_PLT_PC; 120 case R_ARM_GOTOFF32: 121 // (S + A) - GOT_ORG 122 return R_GOTREL; 123 case R_ARM_GOT_BREL: 124 // GOT(S) + A - GOT_ORG 125 return R_GOT_OFF; 126 case R_ARM_GOT_PREL: 127 case R_ARM_TLS_IE32: 128 // GOT(S) + A - P 129 return R_GOT_PC; 130 case R_ARM_SBREL32: 131 return R_ARM_SBREL; 132 case R_ARM_TARGET1: 133 return Config->Target1Rel ? R_PC : R_ABS; 134 case R_ARM_TARGET2: 135 if (Config->Target2 == Target2Policy::Rel) 136 return R_PC; 137 if (Config->Target2 == Target2Policy::Abs) 138 return R_ABS; 139 return R_GOT_PC; 140 case R_ARM_TLS_GD32: 141 return R_TLSGD_PC; 142 case R_ARM_TLS_LDM32: 143 return R_TLSLD_PC; 144 case R_ARM_BASE_PREL: 145 // B(S) + A - P 146 // FIXME: currently B(S) assumed to be .got, this may not hold for all 147 // platforms. 148 return R_GOTONLY_PC; 149 case R_ARM_MOVW_PREL_NC: 150 case R_ARM_MOVT_PREL: 151 case R_ARM_REL32: 152 case R_ARM_THM_MOVW_PREL_NC: 153 case R_ARM_THM_MOVT_PREL: 154 return R_PC; 155 case R_ARM_NONE: 156 return R_NONE; 157 case R_ARM_TLS_LE32: 158 return R_TLS; 159 default: 160 return R_ABS; 161 } 162 } 163 164 bool ARM::isPicRel(RelType Type) const { 165 return (Type == R_ARM_TARGET1 && !Config->Target1Rel) || 166 (Type == R_ARM_ABS32); 167 } 168 169 RelType ARM::getDynRel(RelType Type) const { 170 if (Type == R_ARM_TARGET1 && !Config->Target1Rel) 171 return R_ARM_ABS32; 172 if (Type == R_ARM_ABS32) 173 return Type; 174 // Keep it going with a dummy value so that we can find more reloc errors. 175 return R_ARM_ABS32; 176 } 177 178 void ARM::writeGotPlt(uint8_t *Buf, const Symbol &) const { 179 write32le(Buf, InX::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 void ARM::writePltHeader(uint8_t *Buf) const { 188 const uint8_t PltData[] = { 189 0x04, 0xe0, 0x2d, 0xe5, // str lr, [sp,#-4]! 190 0x04, 0xe0, 0x9f, 0xe5, // ldr lr, L2 191 0x0e, 0xe0, 0x8f, 0xe0, // L1: add lr, pc, lr 192 0x08, 0xf0, 0xbe, 0xe5, // ldr pc, [lr, #8] 193 0x00, 0x00, 0x00, 0x00, // L2: .word &(.got.plt) - L1 - 8 194 }; 195 memcpy(Buf, PltData, sizeof(PltData)); 196 uint64_t GotPlt = InX::GotPlt->getVA(); 197 uint64_t L1 = InX::Plt->getVA() + 8; 198 write32le(Buf + 16, GotPlt - L1 - 8); 199 } 200 201 void ARM::addPltHeaderSymbols(InputSectionBase *ISD) const { 202 auto *IS = cast<InputSection>(ISD); 203 addSyntheticLocal("$a", STT_NOTYPE, 0, 0, IS); 204 addSyntheticLocal("$d", STT_NOTYPE, 16, 0, IS); 205 } 206 207 void ARM::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, 208 uint64_t PltEntryAddr, int32_t Index, 209 unsigned RelOff) const { 210 // FIXME: Using simple code sequence with simple relocations. 211 // There is a more optimal sequence but it requires support for the group 212 // relocations. See ELF for the ARM Architecture Appendix A.3 213 const uint8_t PltData[] = { 214 0x04, 0xc0, 0x9f, 0xe5, // ldr ip, L2 215 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc 216 0x00, 0xf0, 0x9c, 0xe5, // ldr pc, [ip] 217 0x00, 0x00, 0x00, 0x00, // L2: .word Offset(&(.plt.got) - L1 - 8 218 }; 219 memcpy(Buf, PltData, sizeof(PltData)); 220 uint64_t L1 = PltEntryAddr + 4; 221 write32le(Buf + 12, GotPltEntryAddr - L1 - 8); 222 } 223 224 void ARM::addPltSymbols(InputSectionBase *ISD, uint64_t Off) const { 225 auto *IS = cast<InputSection>(ISD); 226 addSyntheticLocal("$a", STT_NOTYPE, Off, 0, IS); 227 addSyntheticLocal("$d", STT_NOTYPE, Off + 12, 0, IS); 228 } 229 230 bool ARM::needsThunk(RelExpr Expr, RelType Type, const InputFile *File, 231 uint64_t BranchAddr, const Symbol &S) const { 232 // If S is an undefined weak symbol in an executable we don't need a Thunk. 233 // In a DSO calls to undefined symbols, including weak ones get PLT entries 234 // which may need a thunk. 235 if (S.isUndefWeak() && !Config->Shared) 236 return false; 237 // A state change from ARM to Thumb and vice versa must go through an 238 // interworking thunk if the relocation type is not R_ARM_CALL or 239 // R_ARM_THM_CALL. 240 switch (Type) { 241 case R_ARM_PC24: 242 case R_ARM_PLT32: 243 case R_ARM_JUMP24: 244 // Source is ARM, all PLT entries are ARM so no interworking required. 245 // Otherwise we need to interwork if Symbol has bit 0 set (Thumb). 246 if (Expr == R_PC && ((S.getVA() & 1) == 1)) 247 return true; 248 LLVM_FALLTHROUGH; 249 case R_ARM_CALL: { 250 uint64_t Dst = (Expr == R_PLT_PC) ? S.getPltVA() : S.getVA(); 251 return !inBranchRange(Type, BranchAddr, Dst); 252 } 253 case R_ARM_THM_JUMP19: 254 case R_ARM_THM_JUMP24: 255 // Source is Thumb, all PLT entries are ARM so interworking is required. 256 // Otherwise we need to interwork if Symbol has bit 0 clear (ARM). 257 if (Expr == R_PLT_PC || ((S.getVA() & 1) == 0)) 258 return true; 259 LLVM_FALLTHROUGH; 260 case R_ARM_THM_CALL: { 261 uint64_t Dst = (Expr == R_PLT_PC) ? S.getPltVA() : S.getVA(); 262 return !inBranchRange(Type, BranchAddr, Dst); 263 } 264 } 265 return false; 266 } 267 268 bool ARM::inBranchRange(RelType Type, uint64_t Src, uint64_t Dst) const { 269 uint64_t Range; 270 uint64_t InstrSize; 271 272 switch (Type) { 273 case R_ARM_PC24: 274 case R_ARM_PLT32: 275 case R_ARM_JUMP24: 276 case R_ARM_CALL: 277 Range = 0x2000000; 278 InstrSize = 4; 279 break; 280 case R_ARM_THM_JUMP19: 281 Range = 0x100000; 282 InstrSize = 2; 283 break; 284 case R_ARM_THM_JUMP24: 285 case R_ARM_THM_CALL: 286 Range = 0x1000000; 287 InstrSize = 2; 288 break; 289 default: 290 return true; 291 } 292 // PC at Src is 2 instructions ahead, immediate of branch is signed 293 if (Src > Dst) 294 Range -= 2 * InstrSize; 295 else 296 Range += InstrSize; 297 298 if ((Dst & 0x1) == 0) 299 // Destination is ARM, if ARM caller then Src is already 4-byte aligned. 300 // If Thumb Caller (BLX) the Src address has bottom 2 bits cleared to ensure 301 // destination will be 4 byte aligned. 302 Src &= ~0x3; 303 else 304 // Bit 0 == 1 denotes Thumb state, it is not part of the range 305 Dst &= ~0x1; 306 307 uint64_t Distance = (Src > Dst) ? Src - Dst : Dst - Src; 308 return Distance <= Range; 309 } 310 311 void ARM::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const { 312 switch (Type) { 313 case R_ARM_ABS32: 314 case R_ARM_BASE_PREL: 315 case R_ARM_GLOB_DAT: 316 case R_ARM_GOTOFF32: 317 case R_ARM_GOT_BREL: 318 case R_ARM_GOT_PREL: 319 case R_ARM_REL32: 320 case R_ARM_RELATIVE: 321 case R_ARM_SBREL32: 322 case R_ARM_TARGET1: 323 case R_ARM_TARGET2: 324 case R_ARM_TLS_GD32: 325 case R_ARM_TLS_IE32: 326 case R_ARM_TLS_LDM32: 327 case R_ARM_TLS_LDO32: 328 case R_ARM_TLS_LE32: 329 case R_ARM_TLS_TPOFF32: 330 case R_ARM_TLS_DTPOFF32: 331 write32le(Loc, Val); 332 break; 333 case R_ARM_TLS_DTPMOD32: 334 write32le(Loc, 1); 335 break; 336 case R_ARM_PREL31: 337 checkInt<31>(Loc, Val, Type); 338 write32le(Loc, (read32le(Loc) & 0x80000000) | (Val & ~0x80000000)); 339 break; 340 case R_ARM_CALL: 341 // R_ARM_CALL is used for BL and BLX instructions, depending on the 342 // value of bit 0 of Val, we must select a BL or BLX instruction 343 if (Val & 1) { 344 // If bit 0 of Val is 1 the target is Thumb, we must select a BLX. 345 // The BLX encoding is 0xfa:H:imm24 where Val = imm24:H:'1' 346 checkInt<26>(Loc, Val, Type); 347 write32le(Loc, 0xfa000000 | // opcode 348 ((Val & 2) << 23) | // H 349 ((Val >> 2) & 0x00ffffff)); // imm24 350 break; 351 } 352 if ((read32le(Loc) & 0xfe000000) == 0xfa000000) 353 // BLX (always unconditional) instruction to an ARM Target, select an 354 // unconditional BL. 355 write32le(Loc, 0xeb000000 | (read32le(Loc) & 0x00ffffff)); 356 // fall through as BL encoding is shared with B 357 LLVM_FALLTHROUGH; 358 case R_ARM_JUMP24: 359 case R_ARM_PC24: 360 case R_ARM_PLT32: 361 checkInt<26>(Loc, Val, Type); 362 write32le(Loc, (read32le(Loc) & ~0x00ffffff) | ((Val >> 2) & 0x00ffffff)); 363 break; 364 case R_ARM_THM_JUMP11: 365 checkInt<12>(Loc, Val, Type); 366 write16le(Loc, (read32le(Loc) & 0xf800) | ((Val >> 1) & 0x07ff)); 367 break; 368 case R_ARM_THM_JUMP19: 369 // Encoding T3: Val = S:J2:J1:imm6:imm11:0 370 checkInt<21>(Loc, Val, Type); 371 write16le(Loc, 372 (read16le(Loc) & 0xfbc0) | // opcode cond 373 ((Val >> 10) & 0x0400) | // S 374 ((Val >> 12) & 0x003f)); // imm6 375 write16le(Loc + 2, 376 0x8000 | // opcode 377 ((Val >> 8) & 0x0800) | // J2 378 ((Val >> 5) & 0x2000) | // J1 379 ((Val >> 1) & 0x07ff)); // imm11 380 break; 381 case R_ARM_THM_CALL: 382 // R_ARM_THM_CALL is used for BL and BLX instructions, depending on the 383 // value of bit 0 of Val, we must select a BL or BLX instruction 384 if ((Val & 1) == 0) { 385 // Ensure BLX destination is 4-byte aligned. As BLX instruction may 386 // only be two byte aligned. This must be done before overflow check 387 Val = alignTo(Val, 4); 388 } 389 // Bit 12 is 0 for BLX, 1 for BL 390 write16le(Loc + 2, (read16le(Loc + 2) & ~0x1000) | (Val & 1) << 12); 391 // Fall through as rest of encoding is the same as B.W 392 LLVM_FALLTHROUGH; 393 case R_ARM_THM_JUMP24: 394 // Encoding B T4, BL T1, BLX T2: Val = S:I1:I2:imm10:imm11:0 395 // FIXME: Use of I1 and I2 require v6T2ops 396 checkInt<25>(Loc, Val, Type); 397 write16le(Loc, 398 0xf000 | // opcode 399 ((Val >> 14) & 0x0400) | // S 400 ((Val >> 12) & 0x03ff)); // imm10 401 write16le(Loc + 2, 402 (read16le(Loc + 2) & 0xd000) | // opcode 403 (((~(Val >> 10)) ^ (Val >> 11)) & 0x2000) | // J1 404 (((~(Val >> 11)) ^ (Val >> 13)) & 0x0800) | // J2 405 ((Val >> 1) & 0x07ff)); // imm11 406 break; 407 case R_ARM_MOVW_ABS_NC: 408 case R_ARM_MOVW_PREL_NC: 409 write32le(Loc, (read32le(Loc) & ~0x000f0fff) | ((Val & 0xf000) << 4) | 410 (Val & 0x0fff)); 411 break; 412 case R_ARM_MOVT_ABS: 413 case R_ARM_MOVT_PREL: 414 checkInt<32>(Loc, Val, Type); 415 write32le(Loc, (read32le(Loc) & ~0x000f0fff) | 416 (((Val >> 16) & 0xf000) << 4) | ((Val >> 16) & 0xfff)); 417 break; 418 case R_ARM_THM_MOVT_ABS: 419 case R_ARM_THM_MOVT_PREL: 420 // Encoding T1: A = imm4:i:imm3:imm8 421 checkInt<32>(Loc, Val, Type); 422 write16le(Loc, 423 0xf2c0 | // opcode 424 ((Val >> 17) & 0x0400) | // i 425 ((Val >> 28) & 0x000f)); // imm4 426 write16le(Loc + 2, 427 (read16le(Loc + 2) & 0x8f00) | // opcode 428 ((Val >> 12) & 0x7000) | // imm3 429 ((Val >> 16) & 0x00ff)); // imm8 430 break; 431 case R_ARM_THM_MOVW_ABS_NC: 432 case R_ARM_THM_MOVW_PREL_NC: 433 // Encoding T3: A = imm4:i:imm3:imm8 434 write16le(Loc, 435 0xf240 | // opcode 436 ((Val >> 1) & 0x0400) | // i 437 ((Val >> 12) & 0x000f)); // imm4 438 write16le(Loc + 2, 439 (read16le(Loc + 2) & 0x8f00) | // opcode 440 ((Val << 4) & 0x7000) | // imm3 441 (Val & 0x00ff)); // imm8 442 break; 443 default: 444 error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type)); 445 } 446 } 447 448 int64_t ARM::getImplicitAddend(const uint8_t *Buf, RelType Type) const { 449 switch (Type) { 450 default: 451 return 0; 452 case R_ARM_ABS32: 453 case R_ARM_BASE_PREL: 454 case R_ARM_GOTOFF32: 455 case R_ARM_GOT_BREL: 456 case R_ARM_GOT_PREL: 457 case R_ARM_REL32: 458 case R_ARM_TARGET1: 459 case R_ARM_TARGET2: 460 case R_ARM_TLS_GD32: 461 case R_ARM_TLS_LDM32: 462 case R_ARM_TLS_LDO32: 463 case R_ARM_TLS_IE32: 464 case R_ARM_TLS_LE32: 465 return SignExtend64<32>(read32le(Buf)); 466 case R_ARM_PREL31: 467 return SignExtend64<31>(read32le(Buf)); 468 case R_ARM_CALL: 469 case R_ARM_JUMP24: 470 case R_ARM_PC24: 471 case R_ARM_PLT32: 472 return SignExtend64<26>(read32le(Buf) << 2); 473 case R_ARM_THM_JUMP11: 474 return SignExtend64<12>(read16le(Buf) << 1); 475 case R_ARM_THM_JUMP19: { 476 // Encoding T3: A = S:J2:J1:imm10:imm6:0 477 uint16_t Hi = read16le(Buf); 478 uint16_t Lo = read16le(Buf + 2); 479 return SignExtend64<20>(((Hi & 0x0400) << 10) | // S 480 ((Lo & 0x0800) << 8) | // J2 481 ((Lo & 0x2000) << 5) | // J1 482 ((Hi & 0x003f) << 12) | // imm6 483 ((Lo & 0x07ff) << 1)); // imm11:0 484 } 485 case R_ARM_THM_CALL: 486 case R_ARM_THM_JUMP24: { 487 // Encoding B T4, BL T1, BLX T2: A = S:I1:I2:imm10:imm11:0 488 // I1 = NOT(J1 EOR S), I2 = NOT(J2 EOR S) 489 // FIXME: I1 and I2 require v6T2ops 490 uint16_t Hi = read16le(Buf); 491 uint16_t Lo = read16le(Buf + 2); 492 return SignExtend64<24>(((Hi & 0x0400) << 14) | // S 493 (~((Lo ^ (Hi << 3)) << 10) & 0x00800000) | // I1 494 (~((Lo ^ (Hi << 1)) << 11) & 0x00400000) | // I2 495 ((Hi & 0x003ff) << 12) | // imm0 496 ((Lo & 0x007ff) << 1)); // imm11:0 497 } 498 // ELF for the ARM Architecture 4.6.1.1 the implicit addend for MOVW and 499 // MOVT is in the range -32768 <= A < 32768 500 case R_ARM_MOVW_ABS_NC: 501 case R_ARM_MOVT_ABS: 502 case R_ARM_MOVW_PREL_NC: 503 case R_ARM_MOVT_PREL: { 504 uint64_t Val = read32le(Buf) & 0x000f0fff; 505 return SignExtend64<16>(((Val & 0x000f0000) >> 4) | (Val & 0x00fff)); 506 } 507 case R_ARM_THM_MOVW_ABS_NC: 508 case R_ARM_THM_MOVT_ABS: 509 case R_ARM_THM_MOVW_PREL_NC: 510 case R_ARM_THM_MOVT_PREL: { 511 // Encoding T3: A = imm4:i:imm3:imm8 512 uint16_t Hi = read16le(Buf); 513 uint16_t Lo = read16le(Buf + 2); 514 return SignExtend64<16>(((Hi & 0x000f) << 12) | // imm4 515 ((Hi & 0x0400) << 1) | // i 516 ((Lo & 0x7000) >> 4) | // imm3 517 (Lo & 0x00ff)); // imm8 518 } 519 } 520 } 521 522 TargetInfo *elf::getARMTargetInfo() { 523 static ARM Target; 524 return &Target; 525 } 526