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