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