1 //===- Target.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 // Machine-specific things, such as applying relocations, creation of 11 // GOT or PLT entries, etc., are handled in this file. 12 // 13 // Refer the ELF spec for the single letter varaibles, S, A or P, used 14 // in this file. SA is S+A. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "Target.h" 19 #include "Error.h" 20 #include "OutputSections.h" 21 #include "Symbols.h" 22 23 #include "llvm/ADT/ArrayRef.h" 24 #include "llvm/Object/ELF.h" 25 #include "llvm/Support/Endian.h" 26 #include "llvm/Support/ELF.h" 27 28 using namespace llvm; 29 using namespace llvm::object; 30 using namespace llvm::support::endian; 31 using namespace llvm::ELF; 32 33 namespace lld { 34 namespace elf2 { 35 36 TargetInfo *Target; 37 38 template <endianness E> static void add32(void *P, int32_t V) { 39 write32<E>(P, read32<E>(P) + V); 40 } 41 42 static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); } 43 static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); } 44 45 template <unsigned N> static void checkInt(int64_t V, uint32_t Type) { 46 if (isInt<N>(V)) 47 return; 48 StringRef S = getELFRelocationTypeName(Config->EMachine, Type); 49 error("Relocation " + S + " out of range"); 50 } 51 52 template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) { 53 if (isUInt<N>(V)) 54 return; 55 StringRef S = getELFRelocationTypeName(Config->EMachine, Type); 56 error("Relocation " + S + " out of range"); 57 } 58 59 template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) { 60 if (isInt<N>(V) || isUInt<N>(V)) 61 return; 62 StringRef S = getELFRelocationTypeName(Config->EMachine, Type); 63 error("Relocation " + S + " out of range"); 64 } 65 66 template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) { 67 if ((V & (N - 1)) == 0) 68 return; 69 StringRef S = getELFRelocationTypeName(Config->EMachine, Type); 70 error("Improper alignment for relocation " + S); 71 } 72 73 template <class ELFT> bool isGnuIFunc(const SymbolBody &S) { 74 if (auto *SS = dyn_cast<DefinedElf<ELFT>>(&S)) 75 return SS->Sym.getType() == STT_GNU_IFUNC; 76 return false; 77 } 78 79 namespace { 80 class X86TargetInfo final : public TargetInfo { 81 public: 82 X86TargetInfo(); 83 void writeGotPltHeader(uint8_t *Buf) const override; 84 unsigned getDynRel(unsigned Type) const override; 85 unsigned getTlsGotRel(unsigned Type) const override; 86 bool isTlsLocalDynamicRel(unsigned Type) const override; 87 bool isTlsGlobalDynamicRel(unsigned Type) const override; 88 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override; 89 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override; 90 void writePltZero(uint8_t *Buf) const override; 91 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, 92 int32_t Index, unsigned RelOff) const override; 93 bool needsCopyRelImpl(uint32_t Type) const override; 94 bool needsDynRelative(unsigned Type) const override; 95 bool needsGot(uint32_t Type, SymbolBody &S) const override; 96 PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override; 97 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 98 uint64_t SA, uint64_t ZA = 0, 99 uint8_t *PairedLoc = nullptr) const override; 100 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override; 101 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 102 uint64_t SA, const SymbolBody *S) const override; 103 bool isGotRelative(uint32_t Type) const override; 104 105 private: 106 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, 107 uint64_t SA) const; 108 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, 109 uint64_t SA) const; 110 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, 111 uint64_t SA) const; 112 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd, 113 uint64_t P, uint64_t SA) const; 114 }; 115 116 class X86_64TargetInfo final : public TargetInfo { 117 public: 118 X86_64TargetInfo(); 119 unsigned getTlsGotRel(unsigned Type) const override; 120 bool isTlsLocalDynamicRel(unsigned Type) const override; 121 bool isTlsGlobalDynamicRel(unsigned Type) const override; 122 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override; 123 void writeGotPltHeader(uint8_t *Buf) const override; 124 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override; 125 void writePltZero(uint8_t *Buf) const override; 126 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, 127 int32_t Index, unsigned RelOff) const override; 128 bool needsCopyRelImpl(uint32_t Type) const override; 129 bool needsGot(uint32_t Type, SymbolBody &S) const override; 130 PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override; 131 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 132 uint64_t SA, uint64_t ZA = 0, 133 uint8_t *PairedLoc = nullptr) const override; 134 bool isRelRelative(uint32_t Type) const override; 135 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override; 136 bool isSizeRel(uint32_t Type) const override; 137 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 138 uint64_t SA, const SymbolBody *S) const override; 139 140 private: 141 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, 142 uint64_t SA) const; 143 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, 144 uint64_t SA) const; 145 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, 146 uint64_t SA) const; 147 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, 148 uint64_t SA) const; 149 }; 150 151 class PPCTargetInfo final : public TargetInfo { 152 public: 153 PPCTargetInfo(); 154 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 155 uint64_t SA, uint64_t ZA = 0, 156 uint8_t *PairedLoc = nullptr) const override; 157 bool isRelRelative(uint32_t Type) const override; 158 }; 159 160 class PPC64TargetInfo final : public TargetInfo { 161 public: 162 PPC64TargetInfo(); 163 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, 164 int32_t Index, unsigned RelOff) const override; 165 bool needsGot(uint32_t Type, SymbolBody &S) const override; 166 PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override; 167 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 168 uint64_t SA, uint64_t ZA = 0, 169 uint8_t *PairedLoc = nullptr) const override; 170 bool isRelRelative(uint32_t Type) const override; 171 }; 172 173 class AArch64TargetInfo final : public TargetInfo { 174 public: 175 AArch64TargetInfo(); 176 unsigned getDynRel(unsigned Type) const override; 177 bool isTlsGlobalDynamicRel(unsigned Type) const override; 178 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override; 179 void writePltZero(uint8_t *Buf) const override; 180 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, 181 int32_t Index, unsigned RelOff) const override; 182 unsigned getTlsGotRel(unsigned Type) const override; 183 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override; 184 bool needsCopyRelImpl(uint32_t Type) const override; 185 bool needsGot(uint32_t Type, SymbolBody &S) const override; 186 PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override; 187 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 188 uint64_t SA, uint64_t ZA = 0, 189 uint8_t *PairedLoc = nullptr) const override; 190 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 191 uint64_t SA, const SymbolBody *S) const override; 192 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override; 193 194 private: 195 void relocateTlsGdToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd, 196 uint64_t P, uint64_t SA) const; 197 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd, 198 uint64_t P, uint64_t SA) const; 199 200 static const uint64_t TcbSize = 16; 201 }; 202 203 class AMDGPUTargetInfo final : public TargetInfo { 204 public: 205 AMDGPUTargetInfo() {} 206 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 207 uint64_t SA, uint64_t ZA = 0, 208 uint8_t *PairedLoc = nullptr) const override; 209 }; 210 211 template <class ELFT> class MipsTargetInfo final : public TargetInfo { 212 public: 213 MipsTargetInfo(); 214 unsigned getDynRel(unsigned Type) const override; 215 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override; 216 void writePltZero(uint8_t *Buf) const override; 217 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, 218 int32_t Index, unsigned RelOff) const override; 219 void writeGotHeader(uint8_t *Buf) const override; 220 bool needsCopyRelImpl(uint32_t Type) const override; 221 bool needsGot(uint32_t Type, SymbolBody &S) const override; 222 PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override; 223 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 224 uint64_t S, uint64_t ZA = 0, 225 uint8_t *PairedLoc = nullptr) const override; 226 bool isHintRel(uint32_t Type) const override; 227 bool isRelRelative(uint32_t Type) const override; 228 }; 229 } // anonymous namespace 230 231 TargetInfo *createTarget() { 232 switch (Config->EMachine) { 233 case EM_386: 234 return new X86TargetInfo(); 235 case EM_AARCH64: 236 return new AArch64TargetInfo(); 237 case EM_AMDGPU: 238 return new AMDGPUTargetInfo(); 239 case EM_MIPS: 240 switch (Config->EKind) { 241 case ELF32LEKind: 242 return new MipsTargetInfo<ELF32LE>(); 243 case ELF32BEKind: 244 return new MipsTargetInfo<ELF32BE>(); 245 default: 246 fatal("Unsupported MIPS target"); 247 } 248 case EM_PPC: 249 return new PPCTargetInfo(); 250 case EM_PPC64: 251 return new PPC64TargetInfo(); 252 case EM_X86_64: 253 return new X86_64TargetInfo(); 254 } 255 fatal("Unknown target machine"); 256 } 257 258 TargetInfo::~TargetInfo() {} 259 260 bool TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const { 261 return false; 262 } 263 264 uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; } 265 266 bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; } 267 268 template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) { 269 if (Config->Shared) 270 return false; 271 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S); 272 if (!SS) 273 return false; 274 return SS->Sym.getType() == STT_OBJECT; 275 } 276 277 bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const { 278 bool MayNeed; 279 switch (Config->EKind) { 280 case ELF32LEKind: 281 MayNeed = mayNeedCopy<ELF32LE>(S); 282 break; 283 case ELF64LEKind: 284 MayNeed = mayNeedCopy<ELF64LE>(S); 285 break; 286 case ELF32BEKind: 287 MayNeed = mayNeedCopy<ELF32BE>(S); 288 break; 289 case ELF64BEKind: 290 MayNeed = mayNeedCopy<ELF64BE>(S); 291 break; 292 default: 293 llvm_unreachable("Invalid ELF kind"); 294 } 295 return MayNeed && needsCopyRelImpl(Type); 296 } 297 298 bool TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const { 299 return false; 300 } 301 302 bool TargetInfo::isGotRelative(uint32_t Type) const { return false; } 303 bool TargetInfo::isHintRel(uint32_t Type) const { return false; } 304 bool TargetInfo::isRelRelative(uint32_t Type) const { return true; } 305 bool TargetInfo::isSizeRel(uint32_t Type) const { return false; } 306 307 bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; } 308 309 TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type, 310 const SymbolBody &S) const { 311 return Plt_No; 312 } 313 314 bool TargetInfo::isTlsLocalDynamicRel(unsigned Type) const { 315 return false; 316 } 317 318 bool TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const { 319 return false; 320 } 321 322 unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, 323 uint64_t P, uint64_t SA, 324 const SymbolBody *S) const { 325 return 0; 326 } 327 328 X86TargetInfo::X86TargetInfo() { 329 CopyRel = R_386_COPY; 330 GotRel = R_386_GLOB_DAT; 331 PltRel = R_386_JUMP_SLOT; 332 IRelativeRel = R_386_IRELATIVE; 333 RelativeRel = R_386_RELATIVE; 334 TlsGotRel = R_386_TLS_TPOFF; 335 TlsModuleIndexRel = R_386_TLS_DTPMOD32; 336 TlsOffsetRel = R_386_TLS_DTPOFF32; 337 UseLazyBinding = true; 338 PltEntrySize = 16; 339 PltZeroSize = 16; 340 } 341 342 void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const { 343 write32le(Buf, Out<ELF32LE>::Dynamic->getVA()); 344 } 345 346 void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const { 347 // Entries in .got.plt initially points back to the corresponding 348 // PLT entries with a fixed offset to skip the first instruction. 349 write32le(Buf, Plt + 6); 350 } 351 352 unsigned X86TargetInfo::getDynRel(unsigned Type) const { 353 if (Type == R_386_TLS_LE) 354 return R_386_TLS_TPOFF; 355 if (Type == R_386_TLS_LE_32) 356 return R_386_TLS_TPOFF32; 357 return Type; 358 } 359 360 unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const { 361 if (Type == R_386_TLS_IE) 362 return Type; 363 return TlsGotRel; 364 } 365 366 bool X86TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const { 367 return Type == R_386_TLS_GD; 368 } 369 370 bool X86TargetInfo::isTlsLocalDynamicRel(unsigned Type) const { 371 return Type == R_386_TLS_LDM; 372 } 373 374 bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const { 375 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 || 376 Type == R_386_TLS_GOTIE) 377 return Config->Shared; 378 if (Type == R_386_TLS_IE) 379 return canBePreempted(&S, true); 380 return Type == R_386_TLS_GD; 381 } 382 383 void X86TargetInfo::writePltZero(uint8_t *Buf) const { 384 // Executable files and shared object files have 385 // separate procedure linkage tables. 386 if (Config->Shared) { 387 const uint8_t V[] = { 388 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx) 389 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx) 390 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop 391 }; 392 memcpy(Buf, V, sizeof(V)); 393 return; 394 } 395 396 const uint8_t PltData[] = { 397 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4) 398 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8) 399 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop 400 }; 401 memcpy(Buf, PltData, sizeof(PltData)); 402 uint32_t Got = Out<ELF32LE>::GotPlt->getVA(); 403 write32le(Buf + 2, Got + 4); 404 write32le(Buf + 8, Got + 8); 405 } 406 407 void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr, 408 uint64_t PltEntryAddr, int32_t Index, 409 unsigned RelOff) const { 410 const uint8_t Inst[] = { 411 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx) 412 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset 413 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC 414 }; 415 memcpy(Buf, Inst, sizeof(Inst)); 416 417 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT 418 Buf[1] = Config->Shared ? 0xa3 : 0x25; 419 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA() 420 : Out<ELF32LE>::Got->getVA(); 421 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr); 422 write32le(Buf + 7, RelOff); 423 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16); 424 } 425 426 bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const { 427 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8; 428 } 429 430 bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { 431 if (S.IsTls && Type == R_386_TLS_GD) 432 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true); 433 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE) 434 return !canRelaxTls(Type, &S); 435 return Type == R_386_GOT32 || needsPlt(Type, S); 436 } 437 438 TargetInfo::PltNeed X86TargetInfo::needsPlt(uint32_t Type, 439 const SymbolBody &S) const { 440 if (isGnuIFunc<ELF32LE>(S) || 441 (Type == R_386_PLT32 && canBePreempted(&S, true)) || 442 (Type == R_386_PC32 && S.isShared())) 443 return Plt_Explicit; 444 return Plt_No; 445 } 446 447 bool X86TargetInfo::isGotRelative(uint32_t Type) const { 448 // This relocation does not require got entry, 449 // but it is relative to got and needs it to be created. 450 // Here we request for that. 451 return Type == R_386_GOTOFF; 452 } 453 454 void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, 455 uint64_t P, uint64_t SA, uint64_t ZA, 456 uint8_t *PairedLoc) const { 457 switch (Type) { 458 case R_386_32: 459 add32le(Loc, SA); 460 break; 461 case R_386_GOT32: { 462 uint64_t V = SA - Out<ELF32LE>::Got->getVA() - 463 Out<ELF32LE>::Got->getNumEntries() * 4; 464 checkInt<32>(V, Type); 465 add32le(Loc, V); 466 break; 467 } 468 case R_386_GOTOFF: 469 add32le(Loc, SA - Out<ELF32LE>::Got->getVA()); 470 break; 471 case R_386_GOTPC: 472 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P); 473 break; 474 case R_386_PC32: 475 case R_386_PLT32: 476 add32le(Loc, SA - P); 477 break; 478 case R_386_TLS_GD: 479 case R_386_TLS_LDM: 480 case R_386_TLS_TPOFF: { 481 uint64_t V = SA - Out<ELF32LE>::Got->getVA() - 482 Out<ELF32LE>::Got->getNumEntries() * 4; 483 checkInt<32>(V, Type); 484 write32le(Loc, V); 485 break; 486 } 487 case R_386_TLS_IE: 488 case R_386_TLS_LDO_32: 489 write32le(Loc, SA); 490 break; 491 case R_386_TLS_LE: 492 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz); 493 break; 494 case R_386_TLS_LE_32: 495 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA); 496 break; 497 default: 498 fatal("unrecognized reloc " + Twine(Type)); 499 } 500 } 501 502 bool X86TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const { 503 if (Config->Shared || (S && !S->IsTls)) 504 return false; 505 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM || 506 Type == R_386_TLS_GD || 507 (Type == R_386_TLS_IE && !canBePreempted(S, true)) || 508 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true)); 509 } 510 511 bool X86TargetInfo::needsDynRelative(unsigned Type) const { 512 return Config->Shared && Type == R_386_TLS_IE; 513 } 514 515 unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, 516 uint64_t P, uint64_t SA, 517 const SymbolBody *S) const { 518 switch (Type) { 519 case R_386_TLS_GD: 520 if (canBePreempted(S, true)) 521 relocateTlsGdToIe(Loc, BufEnd, P, SA); 522 else 523 relocateTlsGdToLe(Loc, BufEnd, P, SA); 524 // The next relocation should be against __tls_get_addr, so skip it 525 return 1; 526 case R_386_TLS_GOTIE: 527 case R_386_TLS_IE: 528 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA); 529 return 0; 530 case R_386_TLS_LDM: 531 relocateTlsLdToLe(Loc, BufEnd, P, SA); 532 // The next relocation should be against __tls_get_addr, so skip it 533 return 1; 534 case R_386_TLS_LDO_32: 535 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA); 536 return 0; 537 } 538 llvm_unreachable("Unknown TLS optimization"); 539 } 540 541 // "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1 542 // IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows 543 // how GD can be optimized to IE: 544 // leal x@tlsgd(, %ebx, 1), 545 // call __tls_get_addr@plt 546 // Is converted to: 547 // movl %gs:0, %eax 548 // addl x@gotntpoff(%ebx), %eax 549 void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, 550 uint64_t SA) const { 551 const uint8_t Inst[] = { 552 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax 553 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax 554 }; 555 memcpy(Loc - 3, Inst, sizeof(Inst)); 556 relocateOne(Loc + 5, BufEnd, R_386_32, P, 557 SA - Out<ELF32LE>::Got->getVA() - 558 Out<ELF32LE>::Got->getNumEntries() * 4); 559 } 560 561 // GD can be optimized to LE: 562 // leal x@tlsgd(, %ebx, 1), 563 // call __tls_get_addr@plt 564 // Can be converted to: 565 // movl %gs:0,%eax 566 // addl $x@ntpoff,%eax 567 // But gold emits subl $foo@tpoff,%eax instead of addl. 568 // These instructions are completely equal in behavior. 569 // This method generates subl to be consistent with gold. 570 void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, 571 uint64_t SA) const { 572 const uint8_t Inst[] = { 573 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax 574 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax 575 }; 576 memcpy(Loc - 3, Inst, sizeof(Inst)); 577 relocateOne(Loc + 5, BufEnd, R_386_32, P, 578 Out<ELF32LE>::TlsPhdr->p_memsz - SA); 579 } 580 581 // LD can be optimized to LE: 582 // leal foo(%reg),%eax 583 // call ___tls_get_addr 584 // Is converted to: 585 // movl %gs:0,%eax 586 // nop 587 // leal 0(%esi,1),%esi 588 void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, 589 uint64_t SA) const { 590 const uint8_t Inst[] = { 591 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax 592 0x90, // nop 593 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi 594 }; 595 memcpy(Loc - 2, Inst, sizeof(Inst)); 596 } 597 598 // In some conditions, relocations can be optimized to avoid using GOT. 599 // This function does that for Initial Exec to Local Exec case. 600 // Read "ELF Handling For Thread-Local Storage, 5.1 601 // IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf) 602 // by Ulrich Drepper for details. 603 void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc, 604 uint8_t *BufEnd, uint64_t P, 605 uint64_t SA) const { 606 // Ulrich's document section 6.2 says that @gotntpoff can 607 // be used with MOVL or ADDL instructions. 608 // @indntpoff is similar to @gotntpoff, but for use in 609 // position dependent code. 610 uint8_t *Inst = Loc - 2; 611 uint8_t *Op = Loc - 1; 612 uint8_t Reg = (Loc[-1] >> 3) & 7; 613 bool IsMov = *Inst == 0x8b; 614 if (Type == R_386_TLS_IE) { 615 // For R_386_TLS_IE relocation we perform the next transformations: 616 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX 617 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG 618 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG 619 // First one is special because when EAX is used the sequence is 5 bytes 620 // long, otherwise it is 6 bytes. 621 if (*Op == 0xa1) { 622 *Op = 0xb8; 623 } else { 624 *Inst = IsMov ? 0xc7 : 0x81; 625 *Op = 0xc0 | ((*Op >> 3) & 7); 626 } 627 } else { 628 // R_386_TLS_GOTIE relocation can be optimized to 629 // R_386_TLS_LE so that it does not use GOT. 630 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG". 631 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG" 632 // Note: gold converts to ADDL instead of LEAL. 633 *Inst = IsMov ? 0xc7 : 0x8d; 634 if (IsMov) 635 *Op = 0xc0 | ((*Op >> 3) & 7); 636 else 637 *Op = 0x80 | Reg | (Reg << 3); 638 } 639 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA); 640 } 641 642 X86_64TargetInfo::X86_64TargetInfo() { 643 CopyRel = R_X86_64_COPY; 644 GotRel = R_X86_64_GLOB_DAT; 645 PltRel = R_X86_64_JUMP_SLOT; 646 RelativeRel = R_X86_64_RELATIVE; 647 IRelativeRel = R_X86_64_IRELATIVE; 648 TlsGotRel = R_X86_64_TPOFF64; 649 TlsModuleIndexRel = R_X86_64_DTPMOD64; 650 TlsOffsetRel = R_X86_64_DTPOFF64; 651 UseLazyBinding = true; 652 PltEntrySize = 16; 653 PltZeroSize = 16; 654 } 655 656 void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const { 657 write64le(Buf, Out<ELF64LE>::Dynamic->getVA()); 658 } 659 660 void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const { 661 // See comments in X86TargetInfo::writeGotPlt. 662 write32le(Buf, Plt + 6); 663 } 664 665 void X86_64TargetInfo::writePltZero(uint8_t *Buf) const { 666 const uint8_t PltData[] = { 667 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip) 668 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip) 669 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax) 670 }; 671 memcpy(Buf, PltData, sizeof(PltData)); 672 uint64_t Got = Out<ELF64LE>::GotPlt->getVA(); 673 uint64_t Plt = Out<ELF64LE>::Plt->getVA(); 674 write32le(Buf + 2, Got - Plt + 2); // GOT+8 675 write32le(Buf + 8, Got - Plt + 4); // GOT+16 676 } 677 678 void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr, 679 uint64_t PltEntryAddr, int32_t Index, 680 unsigned RelOff) const { 681 const uint8_t Inst[] = { 682 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip) 683 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index> 684 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0] 685 }; 686 memcpy(Buf, Inst, sizeof(Inst)); 687 688 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6); 689 write32le(Buf + 7, Index); 690 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16); 691 } 692 693 bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const { 694 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 || 695 Type == R_X86_64_64; 696 } 697 698 bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { 699 if (Type == R_X86_64_TLSGD) 700 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true); 701 if (Type == R_X86_64_GOTTPOFF) 702 return !canRelaxTls(Type, &S); 703 return Type == R_X86_64_GOTPCREL || needsPlt(Type, S); 704 } 705 706 unsigned X86_64TargetInfo::getTlsGotRel(unsigned Type) const { 707 // No other types of TLS relocations requiring GOT should 708 // reach here. 709 assert(Type == R_X86_64_GOTTPOFF); 710 return R_X86_64_PC32; 711 } 712 713 bool X86_64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const { 714 return Type == R_X86_64_TLSGD; 715 } 716 717 bool X86_64TargetInfo::isTlsLocalDynamicRel(unsigned Type) const { 718 return Type == R_X86_64_TLSLD; 719 } 720 721 bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const { 722 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD; 723 } 724 725 TargetInfo::PltNeed X86_64TargetInfo::needsPlt(uint32_t Type, 726 const SymbolBody &S) const { 727 if (needsCopyRel(Type, S)) 728 return Plt_No; 729 if (isGnuIFunc<ELF64LE>(S)) 730 return Plt_Explicit; 731 732 switch (Type) { 733 default: 734 return Plt_No; 735 case R_X86_64_32: 736 case R_X86_64_64: 737 case R_X86_64_PC32: 738 // This relocation is defined to have a value of (S + A - P). 739 // The problems start when a non PIC program calls a function in a shared 740 // library. 741 // In an ideal world, we could just report an error saying the relocation 742 // can overflow at runtime. 743 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to 744 // libc.so. 745 // 746 // The general idea on how to handle such cases is to create a PLT entry 747 // and use that as the function value. 748 // 749 // For the static linking part, we just return true and everything else 750 // will use the the PLT entry as the address. 751 // 752 // The remaining problem is making sure pointer equality still works. We 753 // need the help of the dynamic linker for that. We let it know that we have 754 // a direct reference to a so symbol by creating an undefined symbol with a 755 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to 756 // the value of the symbol we created. This is true even for got entries, so 757 // pointer equality is maintained. To avoid an infinite loop, the only entry 758 // that points to the real function is a dedicated got entry used by the 759 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT, 760 // R_386_JMP_SLOT, etc). 761 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S)) { 762 if (SS->Sym.getType() == STT_FUNC) 763 return Plt_Implicit; 764 } 765 return Plt_No; 766 case R_X86_64_PLT32: 767 if (canBePreempted(&S, true)) 768 return Plt_Explicit; 769 return Plt_No; 770 } 771 } 772 773 bool X86_64TargetInfo::isRelRelative(uint32_t Type) const { 774 switch (Type) { 775 default: 776 return false; 777 case R_X86_64_DTPOFF32: 778 case R_X86_64_DTPOFF64: 779 case R_X86_64_PC8: 780 case R_X86_64_PC16: 781 case R_X86_64_PC32: 782 case R_X86_64_PC64: 783 case R_X86_64_PLT32: 784 return true; 785 } 786 } 787 788 bool X86_64TargetInfo::isSizeRel(uint32_t Type) const { 789 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64; 790 } 791 792 bool X86_64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const { 793 if (Config->Shared || (S && !S->IsTls)) 794 return false; 795 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD || 796 Type == R_X86_64_DTPOFF32 || 797 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true)); 798 } 799 800 // "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5 801 // x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows 802 // how LD can be optimized to LE: 803 // leaq bar@tlsld(%rip), %rdi 804 // callq __tls_get_addr@PLT 805 // leaq bar@dtpoff(%rax), %rcx 806 // Is converted to: 807 // .word 0x6666 808 // .byte 0x66 809 // mov %fs:0,%rax 810 // leaq bar@tpoff(%rax), %rcx 811 void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, 812 uint64_t P, uint64_t SA) const { 813 const uint8_t Inst[] = { 814 0x66, 0x66, //.word 0x6666 815 0x66, //.byte 0x66 816 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax 817 }; 818 memcpy(Loc - 3, Inst, sizeof(Inst)); 819 } 820 821 // "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5 822 // x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows 823 // how GD can be optimized to LE: 824 // .byte 0x66 825 // leaq x@tlsgd(%rip), %rdi 826 // .word 0x6666 827 // rex64 828 // call __tls_get_addr@plt 829 // Is converted to: 830 // mov %fs:0x0,%rax 831 // lea x@tpoff,%rax 832 void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, 833 uint64_t P, uint64_t SA) const { 834 const uint8_t Inst[] = { 835 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax 836 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax 837 }; 838 memcpy(Loc - 4, Inst, sizeof(Inst)); 839 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA); 840 } 841 842 // "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5 843 // x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows 844 // how GD can be optimized to IE: 845 // .byte 0x66 846 // leaq x@tlsgd(%rip), %rdi 847 // .word 0x6666 848 // rex64 849 // call __tls_get_addr@plt 850 // Is converted to: 851 // mov %fs:0x0,%rax 852 // addq x@tpoff,%rax 853 void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, 854 uint64_t P, uint64_t SA) const { 855 const uint8_t Inst[] = { 856 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax 857 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax 858 }; 859 memcpy(Loc - 4, Inst, sizeof(Inst)); 860 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA); 861 } 862 863 // In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to 864 // R_X86_64_TPOFF32 so that it does not use GOT. 865 // This function does that. Read "ELF Handling For Thread-Local Storage, 866 // 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf) 867 // by Ulrich Drepper for details. 868 void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, 869 uint64_t P, uint64_t SA) const { 870 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be 871 // used in MOVQ or ADDQ instructions only. 872 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG". 873 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG" 874 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP". 875 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48. 876 uint8_t *Prefix = Loc - 3; 877 uint8_t *Inst = Loc - 2; 878 uint8_t *RegSlot = Loc - 1; 879 uint8_t Reg = Loc[-1] >> 3; 880 bool IsMov = *Inst == 0x8b; 881 bool RspAdd = !IsMov && Reg == 4; 882 // r12 and rsp registers requires special handling. 883 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11 884 // result out is 7 bytes: 4d 8d 9b XX XX XX XX, 885 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX. 886 // The same true for rsp. So we convert to addq for them, saving 1 byte that 887 // we dont have. 888 if (RspAdd) 889 *Inst = 0x81; 890 else 891 *Inst = IsMov ? 0xc7 : 0x8d; 892 if (*Prefix == 0x4c) 893 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d; 894 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3)); 895 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA); 896 } 897 898 // This function applies a TLS relocation with an optimization as described 899 // in the Ulrich's document. As a result of rewriting instructions at the 900 // relocation target, relocations immediately follow the TLS relocation (which 901 // would be applied to rewritten instructions) may have to be skipped. 902 // This function returns a number of relocations that need to be skipped. 903 unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, 904 uint32_t Type, uint64_t P, uint64_t SA, 905 const SymbolBody *S) const { 906 switch (Type) { 907 case R_X86_64_DTPOFF32: 908 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA); 909 return 0; 910 case R_X86_64_GOTTPOFF: 911 relocateTlsIeToLe(Loc, BufEnd, P, SA); 912 return 0; 913 case R_X86_64_TLSGD: { 914 if (canBePreempted(S, true)) 915 relocateTlsGdToIe(Loc, BufEnd, P, SA); 916 else 917 relocateTlsGdToLe(Loc, BufEnd, P, SA); 918 // The next relocation should be against __tls_get_addr, so skip it 919 return 1; 920 } 921 case R_X86_64_TLSLD: 922 relocateTlsLdToLe(Loc, BufEnd, P, SA); 923 // The next relocation should be against __tls_get_addr, so skip it 924 return 1; 925 } 926 llvm_unreachable("Unknown TLS optimization"); 927 } 928 929 void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, 930 uint64_t P, uint64_t SA, uint64_t ZA, 931 uint8_t *PairedLoc) const { 932 switch (Type) { 933 case R_X86_64_32: 934 checkUInt<32>(SA, Type); 935 write32le(Loc, SA); 936 break; 937 case R_X86_64_32S: 938 checkInt<32>(SA, Type); 939 write32le(Loc, SA); 940 break; 941 case R_X86_64_64: 942 case R_X86_64_DTPOFF64: 943 write64le(Loc, SA); 944 break; 945 case R_X86_64_DTPOFF32: 946 write32le(Loc, SA); 947 break; 948 case R_X86_64_GOTPCREL: 949 case R_X86_64_PC32: 950 case R_X86_64_PLT32: 951 case R_X86_64_TLSGD: 952 case R_X86_64_TLSLD: 953 write32le(Loc, SA - P); 954 break; 955 case R_X86_64_SIZE32: 956 write32le(Loc, ZA); 957 break; 958 case R_X86_64_SIZE64: 959 write64le(Loc, ZA); 960 break; 961 case R_X86_64_TPOFF32: { 962 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz; 963 checkInt<32>(Val, Type); 964 write32le(Loc, Val); 965 break; 966 } 967 default: 968 fatal("unrecognized reloc " + Twine(Type)); 969 } 970 } 971 972 // Relocation masks following the #lo(value), #hi(value), #ha(value), 973 // #higher(value), #highera(value), #highest(value), and #highesta(value) 974 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi 975 // document. 976 static uint16_t applyPPCLo(uint64_t V) { return V; } 977 static uint16_t applyPPCHi(uint64_t V) { return V >> 16; } 978 static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; } 979 static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; } 980 static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; } 981 static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; } 982 static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; } 983 984 PPCTargetInfo::PPCTargetInfo() {} 985 bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; } 986 987 void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, 988 uint64_t P, uint64_t SA, uint64_t ZA, 989 uint8_t *PairedLoc) const { 990 switch (Type) { 991 case R_PPC_ADDR16_HA: 992 write16be(Loc, applyPPCHa(SA)); 993 break; 994 case R_PPC_ADDR16_LO: 995 write16be(Loc, applyPPCLo(SA)); 996 break; 997 default: 998 fatal("unrecognized reloc " + Twine(Type)); 999 } 1000 } 1001 1002 PPC64TargetInfo::PPC64TargetInfo() { 1003 GotRel = R_PPC64_GLOB_DAT; 1004 RelativeRel = R_PPC64_RELATIVE; 1005 PltEntrySize = 32; 1006 1007 // We need 64K pages (at least under glibc/Linux, the loader won't 1008 // set different permissions on a finer granularity than that). 1009 PageSize = 65536; 1010 1011 // The PPC64 ELF ABI v1 spec, says: 1012 // 1013 // It is normally desirable to put segments with different characteristics 1014 // in separate 256 Mbyte portions of the address space, to give the 1015 // operating system full paging flexibility in the 64-bit address space. 1016 // 1017 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers 1018 // use 0x10000000 as the starting address. 1019 VAStart = 0x10000000; 1020 } 1021 1022 uint64_t getPPC64TocBase() { 1023 // The TOC consists of sections .got, .toc, .tocbss, .plt in that 1024 // order. The TOC starts where the first of these sections starts. 1025 1026 // FIXME: This obviously does not do the right thing when there is no .got 1027 // section, but there is a .toc or .tocbss section. 1028 uint64_t TocVA = Out<ELF64BE>::Got->getVA(); 1029 if (!TocVA) 1030 TocVA = Out<ELF64BE>::Plt->getVA(); 1031 1032 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 1033 // thus permitting a full 64 Kbytes segment. Note that the glibc startup 1034 // code (crt1.o) assumes that you can get from the TOC base to the 1035 // start of the .toc section with only a single (signed) 16-bit relocation. 1036 return TocVA + 0x8000; 1037 } 1038 1039 void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr, 1040 uint64_t PltEntryAddr, int32_t Index, 1041 unsigned RelOff) const { 1042 uint64_t Off = GotEntryAddr - getPPC64TocBase(); 1043 1044 // FIXME: What we should do, in theory, is get the offset of the function 1045 // descriptor in the .opd section, and use that as the offset from %r2 (the 1046 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will 1047 // be a pointer to the function descriptor in the .opd section. Using 1048 // this scheme is simpler, but requires an extra indirection per PLT dispatch. 1049 1050 write32be(Buf, 0xf8410028); // std %r2, 40(%r1) 1051 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha 1052 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11) 1053 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12) 1054 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11 1055 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12) 1056 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12) 1057 write32be(Buf + 28, 0x4e800420); // bctr 1058 } 1059 1060 bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { 1061 if (needsPlt(Type, S)) 1062 return true; 1063 1064 switch (Type) { 1065 default: return false; 1066 case R_PPC64_GOT16: 1067 case R_PPC64_GOT16_DS: 1068 case R_PPC64_GOT16_HA: 1069 case R_PPC64_GOT16_HI: 1070 case R_PPC64_GOT16_LO: 1071 case R_PPC64_GOT16_LO_DS: 1072 return true; 1073 } 1074 } 1075 1076 TargetInfo::PltNeed PPC64TargetInfo::needsPlt(uint32_t Type, 1077 const SymbolBody &S) const { 1078 // These are function calls that need to be redirected through a PLT stub. 1079 if (Type == R_PPC64_REL24 && canBePreempted(&S, false)) 1080 return Plt_Explicit; 1081 return Plt_No; 1082 } 1083 1084 bool PPC64TargetInfo::isRelRelative(uint32_t Type) const { 1085 switch (Type) { 1086 default: 1087 return true; 1088 case R_PPC64_ADDR64: 1089 case R_PPC64_TOC: 1090 return false; 1091 } 1092 } 1093 1094 void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, 1095 uint64_t P, uint64_t SA, uint64_t ZA, 1096 uint8_t *PairedLoc) const { 1097 uint64_t TB = getPPC64TocBase(); 1098 1099 // For a TOC-relative relocation, adjust the addend and proceed in terms of 1100 // the corresponding ADDR16 relocation type. 1101 switch (Type) { 1102 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break; 1103 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break; 1104 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break; 1105 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break; 1106 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break; 1107 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break; 1108 default: break; 1109 } 1110 1111 switch (Type) { 1112 case R_PPC64_ADDR14: { 1113 checkAlignment<4>(SA, Type); 1114 // Preserve the AA/LK bits in the branch instruction 1115 uint8_t AALK = Loc[3]; 1116 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc)); 1117 break; 1118 } 1119 case R_PPC64_ADDR16: 1120 checkInt<16>(SA, Type); 1121 write16be(Loc, SA); 1122 break; 1123 case R_PPC64_ADDR16_DS: 1124 checkInt<16>(SA, Type); 1125 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3)); 1126 break; 1127 case R_PPC64_ADDR16_HA: 1128 write16be(Loc, applyPPCHa(SA)); 1129 break; 1130 case R_PPC64_ADDR16_HI: 1131 write16be(Loc, applyPPCHi(SA)); 1132 break; 1133 case R_PPC64_ADDR16_HIGHER: 1134 write16be(Loc, applyPPCHigher(SA)); 1135 break; 1136 case R_PPC64_ADDR16_HIGHERA: 1137 write16be(Loc, applyPPCHighera(SA)); 1138 break; 1139 case R_PPC64_ADDR16_HIGHEST: 1140 write16be(Loc, applyPPCHighest(SA)); 1141 break; 1142 case R_PPC64_ADDR16_HIGHESTA: 1143 write16be(Loc, applyPPCHighesta(SA)); 1144 break; 1145 case R_PPC64_ADDR16_LO: 1146 write16be(Loc, applyPPCLo(SA)); 1147 break; 1148 case R_PPC64_ADDR16_LO_DS: 1149 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3)); 1150 break; 1151 case R_PPC64_ADDR32: 1152 checkInt<32>(SA, Type); 1153 write32be(Loc, SA); 1154 break; 1155 case R_PPC64_ADDR64: 1156 write64be(Loc, SA); 1157 break; 1158 case R_PPC64_REL16_HA: 1159 write16be(Loc, applyPPCHa(SA - P)); 1160 break; 1161 case R_PPC64_REL16_HI: 1162 write16be(Loc, applyPPCHi(SA - P)); 1163 break; 1164 case R_PPC64_REL16_LO: 1165 write16be(Loc, applyPPCLo(SA - P)); 1166 break; 1167 case R_PPC64_REL24: { 1168 // If we have an undefined weak symbol, we might get here with a symbol 1169 // address of zero. That could overflow, but the code must be unreachable, 1170 // so don't bother doing anything at all. 1171 if (!SA) 1172 break; 1173 1174 uint64_t PltStart = Out<ELF64BE>::Plt->getVA(); 1175 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize(); 1176 bool InPlt = PltStart <= SA && SA < PltEnd; 1177 1178 if (!InPlt && Out<ELF64BE>::Opd) { 1179 // If this is a local call, and we currently have the address of a 1180 // function-descriptor, get the underlying code address instead. 1181 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA(); 1182 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize(); 1183 bool InOpd = OpdStart <= SA && SA < OpdEnd; 1184 1185 if (InOpd) 1186 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]); 1187 } 1188 1189 uint32_t Mask = 0x03FFFFFC; 1190 checkInt<24>(SA - P, Type); 1191 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask)); 1192 1193 uint32_t Nop = 0x60000000; 1194 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop) 1195 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1) 1196 break; 1197 } 1198 case R_PPC64_REL32: 1199 checkInt<32>(SA - P, Type); 1200 write32be(Loc, SA - P); 1201 break; 1202 case R_PPC64_REL64: 1203 write64be(Loc, SA - P); 1204 break; 1205 case R_PPC64_TOC: 1206 write64be(Loc, SA); 1207 break; 1208 default: 1209 fatal("unrecognized reloc " + Twine(Type)); 1210 } 1211 } 1212 1213 AArch64TargetInfo::AArch64TargetInfo() { 1214 CopyRel = R_AARCH64_COPY; 1215 IRelativeRel = R_AARCH64_IRELATIVE; 1216 GotRel = R_AARCH64_GLOB_DAT; 1217 PltRel = R_AARCH64_JUMP_SLOT; 1218 TlsGotRel = R_AARCH64_TLS_TPREL64; 1219 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64; 1220 TlsOffsetRel = R_AARCH64_TLS_DTPREL64; 1221 UseLazyBinding = true; 1222 PltEntrySize = 16; 1223 PltZeroSize = 32; 1224 } 1225 1226 bool AArch64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const { 1227 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 || 1228 Type == R_AARCH64_TLSDESC_LD64_LO12_NC || 1229 Type == R_AARCH64_TLSDESC_ADD_LO12_NC || 1230 Type == R_AARCH64_TLSDESC_CALL; 1231 } 1232 1233 unsigned AArch64TargetInfo::getDynRel(unsigned Type) const { 1234 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64) 1235 return Type; 1236 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type); 1237 error("Relocation " + S + " cannot be used when making a shared object; " 1238 "recompile with -fPIC."); 1239 // Keep it going with a dummy value so that we can find more reloc errors. 1240 return R_AARCH64_ABS32; 1241 } 1242 1243 void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const { 1244 write64le(Buf, Out<ELF64LE>::Plt->getVA()); 1245 } 1246 1247 void AArch64TargetInfo::writePltZero(uint8_t *Buf) const { 1248 const uint8_t PltData[] = { 1249 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! 1250 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) 1251 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] 1252 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) 1253 0x20, 0x02, 0x1f, 0xd6, // br x17 1254 0x1f, 0x20, 0x03, 0xd5, // nop 1255 0x1f, 0x20, 0x03, 0xd5, // nop 1256 0x1f, 0x20, 0x03, 0xd5 // nop 1257 }; 1258 memcpy(Buf, PltData, sizeof(PltData)); 1259 1260 uint64_t Got = Out<ELF64LE>::GotPlt->getVA(); 1261 uint64_t Plt = Out<ELF64LE>::Plt->getVA(); 1262 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16); 1263 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8, 1264 Got + 16); 1265 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12, 1266 Got + 16); 1267 } 1268 1269 void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr, 1270 uint64_t PltEntryAddr, int32_t Index, 1271 unsigned RelOff) const { 1272 const uint8_t Inst[] = { 1273 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) 1274 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] 1275 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n])) 1276 0x20, 0x02, 0x1f, 0xd6 // br x17 1277 }; 1278 memcpy(Buf, Inst, sizeof(Inst)); 1279 1280 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr, 1281 GotEntryAddr); 1282 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4, 1283 GotEntryAddr); 1284 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8, 1285 GotEntryAddr); 1286 } 1287 1288 unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const { 1289 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 || 1290 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC); 1291 return Type; 1292 } 1293 1294 bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const { 1295 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 || 1296 Type == R_AARCH64_TLSDESC_LD64_LO12_NC || 1297 Type == R_AARCH64_TLSDESC_ADD_LO12_NC || 1298 Type == R_AARCH64_TLSDESC_CALL || 1299 Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 || 1300 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC; 1301 } 1302 1303 bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const { 1304 switch (Type) { 1305 default: 1306 return false; 1307 case R_AARCH64_ABS16: 1308 case R_AARCH64_ABS32: 1309 case R_AARCH64_ABS64: 1310 case R_AARCH64_ADD_ABS_LO12_NC: 1311 case R_AARCH64_ADR_PREL_LO21: 1312 case R_AARCH64_ADR_PREL_PG_HI21: 1313 case R_AARCH64_LDST8_ABS_LO12_NC: 1314 case R_AARCH64_LDST16_ABS_LO12_NC: 1315 case R_AARCH64_LDST32_ABS_LO12_NC: 1316 case R_AARCH64_LDST64_ABS_LO12_NC: 1317 case R_AARCH64_LDST128_ABS_LO12_NC: 1318 return true; 1319 } 1320 } 1321 1322 bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { 1323 switch (Type) { 1324 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 1325 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: 1326 case R_AARCH64_ADR_GOT_PAGE: 1327 case R_AARCH64_LD64_GOT_LO12_NC: 1328 return true; 1329 default: 1330 return needsPlt(Type, S); 1331 } 1332 } 1333 1334 TargetInfo::PltNeed AArch64TargetInfo::needsPlt(uint32_t Type, 1335 const SymbolBody &S) const { 1336 if (isGnuIFunc<ELF64LE>(S)) 1337 return Plt_Explicit; 1338 switch (Type) { 1339 default: 1340 return Plt_No; 1341 case R_AARCH64_CALL26: 1342 case R_AARCH64_CONDBR19: 1343 case R_AARCH64_JUMP26: 1344 case R_AARCH64_TSTBR14: 1345 if (canBePreempted(&S, true)) 1346 return Plt_Explicit; 1347 return Plt_No; 1348 } 1349 } 1350 1351 static void updateAArch64Addr(uint8_t *L, uint64_t Imm) { 1352 uint32_t ImmLo = (Imm & 0x3) << 29; 1353 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5; 1354 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5); 1355 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi); 1356 } 1357 1358 static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) { 1359 or32le(L, (Imm & 0xFFF) << 10); 1360 } 1361 1362 // Page(Expr) is the page address of the expression Expr, defined 1363 // as (Expr & ~0xFFF). (This applies even if the machine page size 1364 // supported by the platform has a different value.) 1365 static uint64_t getAArch64Page(uint64_t Expr) { 1366 return Expr & (~static_cast<uint64_t>(0xFFF)); 1367 } 1368 1369 void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, 1370 uint32_t Type, uint64_t P, uint64_t SA, 1371 uint64_t ZA, uint8_t *PairedLoc) const { 1372 switch (Type) { 1373 case R_AARCH64_ABS16: 1374 checkIntUInt<16>(SA, Type); 1375 write16le(Loc, SA); 1376 break; 1377 case R_AARCH64_ABS32: 1378 checkIntUInt<32>(SA, Type); 1379 write32le(Loc, SA); 1380 break; 1381 case R_AARCH64_ABS64: 1382 write64le(Loc, SA); 1383 break; 1384 case R_AARCH64_ADD_ABS_LO12_NC: 1385 // This relocation stores 12 bits and there's no instruction 1386 // to do it. Instead, we do a 32 bits store of the value 1387 // of r_addend bitwise-or'ed Loc. This assumes that the addend 1388 // bits in Loc are zero. 1389 or32le(Loc, (SA & 0xFFF) << 10); 1390 break; 1391 case R_AARCH64_ADR_GOT_PAGE: { 1392 uint64_t X = getAArch64Page(SA) - getAArch64Page(P); 1393 checkInt<33>(X, Type); 1394 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12] 1395 break; 1396 } 1397 case R_AARCH64_ADR_PREL_LO21: { 1398 uint64_t X = SA - P; 1399 checkInt<21>(X, Type); 1400 updateAArch64Addr(Loc, X & 0x1FFFFF); 1401 break; 1402 } 1403 case R_AARCH64_ADR_PREL_PG_HI21: 1404 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: { 1405 uint64_t X = getAArch64Page(SA) - getAArch64Page(P); 1406 checkInt<33>(X, Type); 1407 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12] 1408 break; 1409 } 1410 case R_AARCH64_CALL26: 1411 case R_AARCH64_JUMP26: { 1412 uint64_t X = SA - P; 1413 checkInt<28>(X, Type); 1414 or32le(Loc, (X & 0x0FFFFFFC) >> 2); 1415 break; 1416 } 1417 case R_AARCH64_CONDBR19: { 1418 uint64_t X = SA - P; 1419 checkInt<21>(X, Type); 1420 or32le(Loc, (X & 0x1FFFFC) << 3); 1421 break; 1422 } 1423 case R_AARCH64_LD64_GOT_LO12_NC: 1424 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 1425 checkAlignment<8>(SA, Type); 1426 or32le(Loc, (SA & 0xFF8) << 7); 1427 break; 1428 case R_AARCH64_LDST128_ABS_LO12_NC: 1429 or32le(Loc, (SA & 0x0FF8) << 6); 1430 break; 1431 case R_AARCH64_LDST16_ABS_LO12_NC: 1432 or32le(Loc, (SA & 0x0FFC) << 9); 1433 break; 1434 case R_AARCH64_LDST8_ABS_LO12_NC: 1435 or32le(Loc, (SA & 0xFFF) << 10); 1436 break; 1437 case R_AARCH64_LDST32_ABS_LO12_NC: 1438 or32le(Loc, (SA & 0xFFC) << 8); 1439 break; 1440 case R_AARCH64_LDST64_ABS_LO12_NC: 1441 or32le(Loc, (SA & 0xFF8) << 7); 1442 break; 1443 case R_AARCH64_PREL16: 1444 checkIntUInt<16>(SA - P, Type); 1445 write16le(Loc, SA - P); 1446 break; 1447 case R_AARCH64_PREL32: 1448 checkIntUInt<32>(SA - P, Type); 1449 write32le(Loc, SA - P); 1450 break; 1451 case R_AARCH64_PREL64: 1452 write64le(Loc, SA - P); 1453 break; 1454 case R_AARCH64_TSTBR14: { 1455 uint64_t X = SA - P; 1456 checkInt<16>(X, Type); 1457 or32le(Loc, (X & 0xFFFC) << 3); 1458 break; 1459 } 1460 case R_AARCH64_TLSLE_ADD_TPREL_HI12: { 1461 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA; 1462 checkInt<24>(V, Type); 1463 updateAArch64Add(Loc, (V & 0xFFF000) >> 12); 1464 break; 1465 } 1466 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: { 1467 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA; 1468 updateAArch64Add(Loc, V & 0xFFF); 1469 break; 1470 } 1471 default: 1472 fatal("unrecognized reloc " + Twine(Type)); 1473 } 1474 } 1475 1476 bool AArch64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const { 1477 if (Config->Shared || (S && !S->IsTls)) 1478 return false; 1479 1480 // Global-Dynamic relocs can be relaxed to Initial-Exec if the target is 1481 // an executable. And if the target is local it can also be fully relaxed to 1482 // Local-Exec. 1483 if (isTlsGlobalDynamicRel(Type)) 1484 return !canBePreempted(S, true); 1485 1486 // Initial-Exec relocs can be relaxed to Local-Exec if the target is a local 1487 // symbol. 1488 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 || 1489 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) 1490 return !canBePreempted(S, true); 1491 1492 return false; 1493 } 1494 1495 unsigned AArch64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, 1496 uint32_t Type, uint64_t P, uint64_t SA, 1497 const SymbolBody *S) const { 1498 switch (Type) { 1499 case R_AARCH64_TLSDESC_ADR_PAGE21: 1500 case R_AARCH64_TLSDESC_LD64_LO12_NC: 1501 case R_AARCH64_TLSDESC_ADD_LO12_NC: 1502 case R_AARCH64_TLSDESC_CALL: { 1503 if (canBePreempted(S, true)) 1504 fatal("Unsupported TLS optimization"); 1505 uint64_t X = S ? S->getVA<ELF64LE>() : SA; 1506 relocateTlsGdToLe(Type, Loc, BufEnd, P, X); 1507 return 0; 1508 } 1509 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: 1510 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: 1511 relocateTlsIeToLe(Type, Loc, BufEnd, P, S->getVA<ELF64LE>()); 1512 return 0; 1513 } 1514 llvm_unreachable("Unknown TLS optimization"); 1515 } 1516 1517 // Global-Dynamic relocations can be relaxed to Local-Exec if both binary is 1518 // an executable and target is final (can notbe preempted). 1519 void AArch64TargetInfo::relocateTlsGdToLe(unsigned Type, uint8_t *Loc, 1520 uint8_t *BufEnd, uint64_t P, 1521 uint64_t SA) const { 1522 // TLSDESC Global-Dynamic relocation are in the form: 1523 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21] 1524 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC] 1525 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC] 1526 // .tlsdesccall [R_AARCH64_TLSDESC_CALL] 1527 // And it can optimized to: 1528 // movz x0, #0x0, lsl #16 1529 // movk x0, #0x10 1530 // nop 1531 // nop 1532 1533 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align); 1534 uint64_t X = SA + TPOff; 1535 checkUInt<32>(X, Type); 1536 1537 uint32_t NewInst; 1538 switch (Type) { 1539 case R_AARCH64_TLSDESC_ADD_LO12_NC: 1540 case R_AARCH64_TLSDESC_CALL: 1541 // nop 1542 NewInst = 0xd503201f; 1543 break; 1544 case R_AARCH64_TLSDESC_ADR_PAGE21: 1545 // movz 1546 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5); 1547 break; 1548 case R_AARCH64_TLSDESC_LD64_LO12_NC: 1549 // movk 1550 NewInst = 0xf2800000 | ((X & 0xffff) << 5); 1551 break; 1552 default: 1553 llvm_unreachable("Unsupported Relocation for TLS GD to LE relax"); 1554 } 1555 write32le(Loc, NewInst); 1556 } 1557 1558 // Initial-Exec relocations can be relaxed to Local-Exec if symbol is final 1559 // (can not be preempted). 1560 void AArch64TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc, 1561 uint8_t *BufEnd, uint64_t P, 1562 uint64_t SA) const { 1563 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align); 1564 uint64_t X = SA + TPOff; 1565 checkUInt<32>(X, Type); 1566 1567 uint32_t Inst = read32le (Loc); 1568 uint32_t NewInst; 1569 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) { 1570 // Generate movz. 1571 unsigned RegNo = (Inst & 0x1f); 1572 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5); 1573 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) { 1574 // Generate movk 1575 unsigned RegNo = (Inst & 0x1f); 1576 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5); 1577 } else { 1578 llvm_unreachable("Invalid Relocation for TLS IE to LE Relax"); 1579 } 1580 write32le(Loc, NewInst); 1581 } 1582 1583 1584 // Implementing relocations for AMDGPU is low priority since most 1585 // programs don't use relocations now. Thus, this function is not 1586 // actually called (relocateOne is called for each relocation). 1587 // That's why the AMDGPU port works without implementing this function. 1588 void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, 1589 uint64_t P, uint64_t SA, uint64_t ZA, 1590 uint8_t *PairedLoc) const { 1591 llvm_unreachable("not implemented"); 1592 } 1593 1594 template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() { 1595 GotHeaderEntriesNum = 2; 1596 GotPltHeaderEntriesNum = 2; 1597 PageSize = 65536; 1598 PltEntrySize = 16; 1599 PltZeroSize = 32; 1600 UseLazyBinding = true; 1601 CopyRel = R_MIPS_COPY; 1602 PltRel = R_MIPS_JUMP_SLOT; 1603 RelativeRel = R_MIPS_REL32; 1604 } 1605 1606 template <class ELFT> 1607 unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const { 1608 if (Type == R_MIPS_32 || Type == R_MIPS_64) 1609 return R_MIPS_REL32; 1610 StringRef S = getELFRelocationTypeName(EM_MIPS, Type); 1611 error("Relocation " + S + " cannot be used when making a shared object; " 1612 "recompile with -fPIC."); 1613 // Keep it going with a dummy value so that we can find more reloc errors. 1614 return R_MIPS_32; 1615 } 1616 1617 template <class ELFT> 1618 void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const { 1619 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off; 1620 typedef typename ELFFile<ELFT>::uintX_t uintX_t; 1621 1622 // Set the MSB of the second GOT slot. This is not required by any 1623 // MIPS ABI documentation, though. 1624 // 1625 // There is a comment in glibc saying that "The MSB of got[1] of a 1626 // gnu object is set to identify gnu objects," and in GNU gold it 1627 // says "the second entry will be used by some runtime loaders". 1628 // But how this field is being used is unclear. 1629 // 1630 // We are not really willing to mimic other linkers behaviors 1631 // without understanding why they do that, but because all files 1632 // generated by GNU tools have this special GOT value, and because 1633 // we've been doing this for years, it is probably a safe bet to 1634 // keep doing this for now. We really need to revisit this to see 1635 // if we had to do this. 1636 auto *P = reinterpret_cast<Elf_Off *>(Buf); 1637 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31); 1638 } 1639 1640 template <class ELFT> 1641 void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const { 1642 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA()); 1643 } 1644 1645 static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; } 1646 1647 template <endianness E, uint8_t BSIZE, uint8_t SHIFT> 1648 static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P, 1649 uint64_t S) { 1650 uint32_t Mask = 0xffffffff >> (32 - BSIZE); 1651 uint32_t Instr = read32<E>(Loc); 1652 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT); 1653 if (SHIFT > 0) 1654 checkAlignment<(1 << SHIFT)>(S + A, Type); 1655 int64_t V = S + A - P; 1656 checkInt<BSIZE + SHIFT>(V, Type); 1657 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask)); 1658 } 1659 1660 template <endianness E> 1661 static void applyMipsHi16Reloc(uint8_t *Loc, uint64_t S, int64_t A) { 1662 uint32_t Instr = read32<E>(Loc); 1663 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + A)); 1664 } 1665 1666 template <class ELFT> 1667 void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const { 1668 const endianness E = ELFT::TargetEndianness; 1669 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0]) 1670 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28) 1671 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0]) 1672 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28 1673 write32<E>(Buf + 16, 0x03e07825); // move $15, $31 1674 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2 1675 write32<E>(Buf + 24, 0x0320f809); // jalr $25 1676 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2 1677 uint64_t Got = Out<ELFT>::GotPlt->getVA(); 1678 uint64_t Plt = Out<ELFT>::Plt->getVA(); 1679 applyMipsHi16Reloc<E>(Buf, Got, 0); 1680 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, Plt + 4, Got); 1681 relocateOne(Buf + 8, Buf + 12, R_MIPS_LO16, Plt + 8, Got); 1682 } 1683 1684 template <class ELFT> 1685 void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr, 1686 uint64_t PltEntryAddr, int32_t Index, 1687 unsigned RelOff) const { 1688 const endianness E = ELFT::TargetEndianness; 1689 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry) 1690 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15) 1691 write32<E>(Buf + 8, 0x03200008); // jr $25 1692 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry) 1693 applyMipsHi16Reloc<E>(Buf, GotEntryAddr, 0); 1694 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, PltEntryAddr + 4, GotEntryAddr); 1695 relocateOne(Buf + 12, Buf + 16, R_MIPS_LO16, PltEntryAddr + 8, GotEntryAddr); 1696 } 1697 1698 template <class ELFT> 1699 bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const { 1700 return Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type); 1701 } 1702 1703 template <class ELFT> 1704 bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const { 1705 return needsPlt(Type, S) || Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16; 1706 } 1707 1708 template <class ELFT> 1709 TargetInfo::PltNeed MipsTargetInfo<ELFT>::needsPlt(uint32_t Type, 1710 const SymbolBody &S) const { 1711 if (needsCopyRel(Type, S)) 1712 return Plt_No; 1713 if (Type == R_MIPS_26 && canBePreempted(&S, false)) 1714 return Plt_Explicit; 1715 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type)) 1716 if (S.isShared()) 1717 return Plt_Explicit; 1718 return Plt_No; 1719 } 1720 1721 template <class ELFT> 1722 void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd, 1723 uint32_t Type, uint64_t P, uint64_t S, 1724 uint64_t ZA, uint8_t *PairedLoc) const { 1725 const endianness E = ELFT::TargetEndianness; 1726 switch (Type) { 1727 case R_MIPS_32: 1728 add32<E>(Loc, S); 1729 break; 1730 case R_MIPS_26: { 1731 uint32_t Instr = read32<E>(Loc); 1732 // FIXME (simon): If the relocation target symbol is not a PLT entry 1733 // we should use another expression for calculation: 1734 // ((A << 2) | (P & 0xf0000000)) >> 2 1735 S += SignExtend64<28>((Instr & 0x3ffffff) << 2); 1736 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2)); 1737 break; 1738 } 1739 case R_MIPS_CALL16: 1740 case R_MIPS_GOT16: { 1741 int64_t V = S - getMipsGpAddr<ELFT>(); 1742 if (Type == R_MIPS_GOT16) 1743 checkInt<16>(V, Type); 1744 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff)); 1745 break; 1746 } 1747 case R_MIPS_GPREL16: { 1748 uint32_t Instr = read32<E>(Loc); 1749 int64_t V = S + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>(); 1750 checkInt<16>(V, Type); 1751 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff)); 1752 break; 1753 } 1754 case R_MIPS_GPREL32: 1755 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>()); 1756 break; 1757 case R_MIPS_HI16: { 1758 uint32_t Instr = read32<E>(Loc); 1759 if (PairedLoc) { 1760 uint64_t AHL = ((Instr & 0xffff) << 16) + 1761 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff); 1762 applyMipsHi16Reloc<E>(Loc, S, AHL); 1763 } else { 1764 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16"); 1765 applyMipsHi16Reloc<E>(Loc, S, 0); 1766 } 1767 break; 1768 } 1769 case R_MIPS_JALR: 1770 // Ignore this optimization relocation for now 1771 break; 1772 case R_MIPS_LO16: { 1773 uint32_t Instr = read32<E>(Loc); 1774 int64_t AHL = SignExtend64<16>(Instr & 0xffff); 1775 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL) & 0xffff)); 1776 break; 1777 } 1778 case R_MIPS_PC16: 1779 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S); 1780 break; 1781 case R_MIPS_PC19_S2: 1782 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S); 1783 break; 1784 case R_MIPS_PC21_S2: 1785 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S); 1786 break; 1787 case R_MIPS_PC26_S2: 1788 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S); 1789 break; 1790 case R_MIPS_PC32: 1791 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S); 1792 break; 1793 case R_MIPS_PCHI16: { 1794 uint32_t Instr = read32<E>(Loc); 1795 if (PairedLoc) { 1796 uint64_t AHL = ((Instr & 0xffff) << 16) + 1797 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff); 1798 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + AHL - P)); 1799 } else { 1800 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16"); 1801 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S - P)); 1802 } 1803 break; 1804 } 1805 case R_MIPS_PCLO16: { 1806 uint32_t Instr = read32<E>(Loc); 1807 int64_t AHL = SignExtend64<16>(Instr & 0xffff); 1808 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL - P) & 0xffff)); 1809 break; 1810 } 1811 default: 1812 fatal("unrecognized reloc " + Twine(Type)); 1813 } 1814 } 1815 1816 template <class ELFT> 1817 bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const { 1818 return Type == R_MIPS_JALR; 1819 } 1820 1821 template <class ELFT> 1822 bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const { 1823 switch (Type) { 1824 default: 1825 return false; 1826 case R_MIPS_PC16: 1827 case R_MIPS_PC19_S2: 1828 case R_MIPS_PC21_S2: 1829 case R_MIPS_PC26_S2: 1830 case R_MIPS_PC32: 1831 case R_MIPS_PCHI16: 1832 case R_MIPS_PCLO16: 1833 return true; 1834 } 1835 } 1836 1837 // _gp is a MIPS-specific ABI-defined symbol which points to 1838 // a location that is relative to GOT. This function returns 1839 // the value for the symbol. 1840 template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() { 1841 unsigned GPOffset = 0x7ff0; 1842 if (uint64_t V = Out<ELFT>::Got->getVA()) 1843 return V + GPOffset; 1844 return 0; 1845 } 1846 1847 template bool isGnuIFunc<ELF32LE>(const SymbolBody &S); 1848 template bool isGnuIFunc<ELF32BE>(const SymbolBody &S); 1849 template bool isGnuIFunc<ELF64LE>(const SymbolBody &S); 1850 template bool isGnuIFunc<ELF64BE>(const SymbolBody &S); 1851 1852 template uint32_t getMipsGpAddr<ELF32LE>(); 1853 template uint32_t getMipsGpAddr<ELF32BE>(); 1854 template uint64_t getMipsGpAddr<ELF64LE>(); 1855 template uint64_t getMipsGpAddr<ELF64BE>(); 1856 } 1857 } 1858