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