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