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