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