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