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