1 //===- X86.cpp ------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "InputFiles.h" 10 #include "Symbols.h" 11 #include "SyntheticSections.h" 12 #include "Target.h" 13 #include "lld/Common/ErrorHandler.h" 14 #include "llvm/Support/Endian.h" 15 16 using namespace llvm; 17 using namespace llvm::support::endian; 18 using namespace llvm::ELF; 19 20 namespace lld { 21 namespace elf { 22 23 namespace { 24 class X86 : public TargetInfo { 25 public: 26 X86(); 27 int getTlsGdRelaxSkip(RelType type) const override; 28 RelExpr getRelExpr(RelType type, const Symbol &s, 29 const uint8_t *loc) const override; 30 int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override; 31 void writeGotPltHeader(uint8_t *buf) const override; 32 RelType getDynRel(RelType type) const override; 33 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 34 void writeIgotPlt(uint8_t *buf, const Symbol &s) const override; 35 void writePltHeader(uint8_t *buf) const override; 36 void writePlt(uint8_t *buf, const Symbol &sym, 37 uint64_t pltEntryAddr) const override; 38 void relocate(uint8_t *loc, const Relocation &rel, 39 uint64_t val) const override; 40 41 RelExpr adjustRelaxExpr(RelType type, const uint8_t *data, 42 RelExpr expr) const override; 43 void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 44 uint64_t val) const override; 45 void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 46 uint64_t val) const override; 47 void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 48 uint64_t val) const override; 49 void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, 50 uint64_t val) const override; 51 }; 52 } // namespace 53 54 X86::X86() { 55 copyRel = R_386_COPY; 56 gotRel = R_386_GLOB_DAT; 57 noneRel = R_386_NONE; 58 pltRel = R_386_JUMP_SLOT; 59 iRelativeRel = R_386_IRELATIVE; 60 relativeRel = R_386_RELATIVE; 61 symbolicRel = R_386_32; 62 tlsGotRel = R_386_TLS_TPOFF; 63 tlsModuleIndexRel = R_386_TLS_DTPMOD32; 64 tlsOffsetRel = R_386_TLS_DTPOFF32; 65 pltHeaderSize = 16; 66 pltEntrySize = 16; 67 ipltEntrySize = 16; 68 trapInstr = {0xcc, 0xcc, 0xcc, 0xcc}; // 0xcc = INT3 69 70 // Align to the non-PAE large page size (known as a superpage or huge page). 71 // FreeBSD automatically promotes large, superpage-aligned allocations. 72 defaultImageBase = 0x400000; 73 } 74 75 int X86::getTlsGdRelaxSkip(RelType type) const { 76 return 2; 77 } 78 79 RelExpr X86::getRelExpr(RelType type, const Symbol &s, 80 const uint8_t *loc) const { 81 // There are 4 different TLS variable models with varying degrees of 82 // flexibility and performance. LocalExec and InitialExec models are fast but 83 // less-flexible models. If they are in use, we set DF_STATIC_TLS flag in the 84 // dynamic section to let runtime know about that. 85 if (type == R_386_TLS_LE || type == R_386_TLS_LE_32 || type == R_386_TLS_IE || 86 type == R_386_TLS_GOTIE) 87 config->hasStaticTlsModel = true; 88 89 switch (type) { 90 case R_386_8: 91 case R_386_16: 92 case R_386_32: 93 return R_ABS; 94 case R_386_TLS_LDO_32: 95 return R_DTPREL; 96 case R_386_TLS_GD: 97 return R_TLSGD_GOTPLT; 98 case R_386_TLS_LDM: 99 return R_TLSLD_GOTPLT; 100 case R_386_PLT32: 101 return R_PLT_PC; 102 case R_386_PC8: 103 case R_386_PC16: 104 case R_386_PC32: 105 return R_PC; 106 case R_386_GOTPC: 107 return R_GOTPLTONLY_PC; 108 case R_386_TLS_IE: 109 return R_GOT; 110 case R_386_GOT32: 111 case R_386_GOT32X: 112 // These relocations are arguably mis-designed because their calculations 113 // depend on the instructions they are applied to. This is bad because we 114 // usually don't care about whether the target section contains valid 115 // machine instructions or not. But this is part of the documented ABI, so 116 // we had to implement as the standard requires. 117 // 118 // x86 does not support PC-relative data access. Therefore, in order to 119 // access GOT contents, a GOT address needs to be known at link-time 120 // (which means non-PIC) or compilers have to emit code to get a GOT 121 // address at runtime (which means code is position-independent but 122 // compilers need to emit extra code for each GOT access.) This decision 123 // is made at compile-time. In the latter case, compilers emit code to 124 // load a GOT address to a register, which is usually %ebx. 125 // 126 // So, there are two ways to refer to symbol foo's GOT entry: foo@GOT or 127 // foo@GOT(%ebx). 128 // 129 // foo@GOT is not usable in PIC. If we are creating a PIC output and if we 130 // find such relocation, we should report an error. foo@GOT is resolved to 131 // an *absolute* address of foo's GOT entry, because both GOT address and 132 // foo's offset are known. In other words, it's G + A. 133 // 134 // foo@GOT(%ebx) needs to be resolved to a *relative* offset from a GOT to 135 // foo's GOT entry in the table, because GOT address is not known but foo's 136 // offset in the table is known. It's G + A - GOT. 137 // 138 // It's unfortunate that compilers emit the same relocation for these 139 // different use cases. In order to distinguish them, we have to read a 140 // machine instruction. 141 // 142 // The following code implements it. We assume that Loc[0] is the first byte 143 // of a displacement or an immediate field of a valid machine 144 // instruction. That means a ModRM byte is at Loc[-1]. By taking a look at 145 // the byte, we can determine whether the instruction uses the operand as an 146 // absolute address (R_GOT) or a register-relative address (R_GOTPLT). 147 return (loc[-1] & 0xc7) == 0x5 ? R_GOT : R_GOTPLT; 148 case R_386_TLS_GOTIE: 149 return R_GOTPLT; 150 case R_386_GOTOFF: 151 return R_GOTPLTREL; 152 case R_386_TLS_LE: 153 return R_TLS; 154 case R_386_TLS_LE_32: 155 return R_NEG_TLS; 156 case R_386_NONE: 157 return R_NONE; 158 default: 159 error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 160 ") against symbol " + toString(s)); 161 return R_NONE; 162 } 163 } 164 165 RelExpr X86::adjustRelaxExpr(RelType type, const uint8_t *data, 166 RelExpr expr) const { 167 switch (expr) { 168 default: 169 return expr; 170 case R_RELAX_TLS_GD_TO_IE: 171 return R_RELAX_TLS_GD_TO_IE_GOTPLT; 172 case R_RELAX_TLS_GD_TO_LE: 173 return R_RELAX_TLS_GD_TO_LE_NEG; 174 } 175 } 176 177 void X86::writeGotPltHeader(uint8_t *buf) const { 178 write32le(buf, mainPart->dynamic->getVA()); 179 } 180 181 void X86::writeGotPlt(uint8_t *buf, const Symbol &s) const { 182 // Entries in .got.plt initially points back to the corresponding 183 // PLT entries with a fixed offset to skip the first instruction. 184 write32le(buf, s.getPltVA() + 6); 185 } 186 187 void X86::writeIgotPlt(uint8_t *buf, const Symbol &s) const { 188 // An x86 entry is the address of the ifunc resolver function. 189 write32le(buf, s.getVA()); 190 } 191 192 RelType X86::getDynRel(RelType type) const { 193 if (type == R_386_TLS_LE) 194 return R_386_TLS_TPOFF; 195 if (type == R_386_TLS_LE_32) 196 return R_386_TLS_TPOFF32; 197 return type; 198 } 199 200 void X86::writePltHeader(uint8_t *buf) const { 201 if (config->isPic) { 202 const uint8_t v[] = { 203 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx) 204 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx) 205 0x90, 0x90, 0x90, 0x90 // nop 206 }; 207 memcpy(buf, v, sizeof(v)); 208 return; 209 } 210 211 const uint8_t pltData[] = { 212 0xff, 0x35, 0, 0, 0, 0, // pushl (GOTPLT+4) 213 0xff, 0x25, 0, 0, 0, 0, // jmp *(GOTPLT+8) 214 0x90, 0x90, 0x90, 0x90, // nop 215 }; 216 memcpy(buf, pltData, sizeof(pltData)); 217 uint32_t gotPlt = in.gotPlt->getVA(); 218 write32le(buf + 2, gotPlt + 4); 219 write32le(buf + 8, gotPlt + 8); 220 } 221 222 void X86::writePlt(uint8_t *buf, const Symbol &sym, 223 uint64_t pltEntryAddr) const { 224 unsigned relOff = in.relaPlt->entsize * sym.pltIndex; 225 if (config->isPic) { 226 const uint8_t inst[] = { 227 0xff, 0xa3, 0, 0, 0, 0, // jmp *foo@GOT(%ebx) 228 0x68, 0, 0, 0, 0, // pushl $reloc_offset 229 0xe9, 0, 0, 0, 0, // jmp .PLT0@PC 230 }; 231 memcpy(buf, inst, sizeof(inst)); 232 write32le(buf + 2, sym.getGotPltVA() - in.gotPlt->getVA()); 233 } else { 234 const uint8_t inst[] = { 235 0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT 236 0x68, 0, 0, 0, 0, // pushl $reloc_offset 237 0xe9, 0, 0, 0, 0, // jmp .PLT0@PC 238 }; 239 memcpy(buf, inst, sizeof(inst)); 240 write32le(buf + 2, sym.getGotPltVA()); 241 } 242 243 write32le(buf + 7, relOff); 244 write32le(buf + 12, in.plt->getVA() - pltEntryAddr - 16); 245 } 246 247 int64_t X86::getImplicitAddend(const uint8_t *buf, RelType type) const { 248 switch (type) { 249 case R_386_8: 250 case R_386_PC8: 251 return SignExtend64<8>(*buf); 252 case R_386_16: 253 case R_386_PC16: 254 return SignExtend64<16>(read16le(buf)); 255 case R_386_32: 256 case R_386_GOT32: 257 case R_386_GOT32X: 258 case R_386_GOTOFF: 259 case R_386_GOTPC: 260 case R_386_PC32: 261 case R_386_PLT32: 262 case R_386_TLS_LDO_32: 263 case R_386_TLS_LE: 264 return SignExtend64<32>(read32le(buf)); 265 default: 266 return 0; 267 } 268 } 269 270 void X86::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 271 switch (rel.type) { 272 case R_386_8: 273 // R_386_{PC,}{8,16} are not part of the i386 psABI, but they are 274 // being used for some 16-bit programs such as boot loaders, so 275 // we want to support them. 276 checkIntUInt(loc, val, 8, rel); 277 *loc = val; 278 break; 279 case R_386_PC8: 280 checkInt(loc, val, 8, rel); 281 *loc = val; 282 break; 283 case R_386_16: 284 checkIntUInt(loc, val, 16, rel); 285 write16le(loc, val); 286 break; 287 case R_386_PC16: 288 // R_386_PC16 is normally used with 16 bit code. In that situation 289 // the PC is 16 bits, just like the addend. This means that it can 290 // point from any 16 bit address to any other if the possibility 291 // of wrapping is included. 292 // The only restriction we have to check then is that the destination 293 // address fits in 16 bits. That is impossible to do here. The problem is 294 // that we are passed the final value, which already had the 295 // current location subtracted from it. 296 // We just check that Val fits in 17 bits. This misses some cases, but 297 // should have no false positives. 298 checkInt(loc, val, 17, rel); 299 write16le(loc, val); 300 break; 301 case R_386_32: 302 case R_386_GOT32: 303 case R_386_GOT32X: 304 case R_386_GOTOFF: 305 case R_386_GOTPC: 306 case R_386_PC32: 307 case R_386_PLT32: 308 case R_386_RELATIVE: 309 case R_386_TLS_DTPMOD32: 310 case R_386_TLS_DTPOFF32: 311 case R_386_TLS_GD: 312 case R_386_TLS_GOTIE: 313 case R_386_TLS_IE: 314 case R_386_TLS_LDM: 315 case R_386_TLS_LDO_32: 316 case R_386_TLS_LE: 317 case R_386_TLS_LE_32: 318 case R_386_TLS_TPOFF: 319 case R_386_TLS_TPOFF32: 320 checkInt(loc, val, 32, rel); 321 write32le(loc, val); 322 break; 323 default: 324 llvm_unreachable("unknown relocation"); 325 } 326 } 327 328 void X86::relaxTlsGdToLe(uint8_t *loc, const Relocation &, uint64_t val) const { 329 // Convert 330 // leal x@tlsgd(, %ebx, 1), 331 // call __tls_get_addr@plt 332 // to 333 // movl %gs:0,%eax 334 // subl $x@ntpoff,%eax 335 const uint8_t inst[] = { 336 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax 337 0x81, 0xe8, 0, 0, 0, 0, // subl Val(%ebx), %eax 338 }; 339 memcpy(loc - 3, inst, sizeof(inst)); 340 write32le(loc + 5, val); 341 } 342 343 void X86::relaxTlsGdToIe(uint8_t *loc, const Relocation &, uint64_t val) const { 344 // Convert 345 // leal x@tlsgd(, %ebx, 1), 346 // call __tls_get_addr@plt 347 // to 348 // movl %gs:0, %eax 349 // addl x@gotntpoff(%ebx), %eax 350 const uint8_t inst[] = { 351 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax 352 0x03, 0x83, 0, 0, 0, 0, // addl Val(%ebx), %eax 353 }; 354 memcpy(loc - 3, inst, sizeof(inst)); 355 write32le(loc + 5, val); 356 } 357 358 // In some conditions, relocations can be optimized to avoid using GOT. 359 // This function does that for Initial Exec to Local Exec case. 360 void X86::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 361 uint64_t val) const { 362 // Ulrich's document section 6.2 says that @gotntpoff can 363 // be used with MOVL or ADDL instructions. 364 // @indntpoff is similar to @gotntpoff, but for use in 365 // position dependent code. 366 uint8_t reg = (loc[-1] >> 3) & 7; 367 368 if (rel.type == R_386_TLS_IE) { 369 if (loc[-1] == 0xa1) { 370 // "movl foo@indntpoff,%eax" -> "movl $foo,%eax" 371 // This case is different from the generic case below because 372 // this is a 5 byte instruction while below is 6 bytes. 373 loc[-1] = 0xb8; 374 } else if (loc[-2] == 0x8b) { 375 // "movl foo@indntpoff,%reg" -> "movl $foo,%reg" 376 loc[-2] = 0xc7; 377 loc[-1] = 0xc0 | reg; 378 } else { 379 // "addl foo@indntpoff,%reg" -> "addl $foo,%reg" 380 loc[-2] = 0x81; 381 loc[-1] = 0xc0 | reg; 382 } 383 } else { 384 assert(rel.type == R_386_TLS_GOTIE); 385 if (loc[-2] == 0x8b) { 386 // "movl foo@gottpoff(%rip),%reg" -> "movl $foo,%reg" 387 loc[-2] = 0xc7; 388 loc[-1] = 0xc0 | reg; 389 } else { 390 // "addl foo@gotntpoff(%rip),%reg" -> "leal foo(%reg),%reg" 391 loc[-2] = 0x8d; 392 loc[-1] = 0x80 | (reg << 3) | reg; 393 } 394 } 395 write32le(loc, val); 396 } 397 398 void X86::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, 399 uint64_t val) const { 400 if (rel.type == R_386_TLS_LDO_32) { 401 write32le(loc, val); 402 return; 403 } 404 405 // Convert 406 // leal foo(%reg),%eax 407 // call ___tls_get_addr 408 // to 409 // movl %gs:0,%eax 410 // nop 411 // leal 0(%esi,1),%esi 412 const uint8_t inst[] = { 413 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax 414 0x90, // nop 415 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi 416 }; 417 memcpy(loc - 2, inst, sizeof(inst)); 418 } 419 420 // If Intel Indirect Branch Tracking is enabled, we have to emit special PLT 421 // entries containing endbr32 instructions. A PLT entry will be split into two 422 // parts, one in .plt.sec (writePlt), and the other in .plt (writeIBTPlt). 423 namespace { 424 class IntelIBT : public X86 { 425 public: 426 IntelIBT(); 427 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 428 void writePlt(uint8_t *buf, const Symbol &sym, 429 uint64_t pltEntryAddr) const override; 430 void writeIBTPlt(uint8_t *buf, size_t numEntries) const override; 431 432 static const unsigned IBTPltHeaderSize = 16; 433 }; 434 } // namespace 435 436 IntelIBT::IntelIBT() { pltHeaderSize = 0; } 437 438 void IntelIBT::writeGotPlt(uint8_t *buf, const Symbol &s) const { 439 uint64_t va = 440 in.ibtPlt->getVA() + IBTPltHeaderSize + s.pltIndex * pltEntrySize; 441 write32le(buf, va); 442 } 443 444 void IntelIBT::writePlt(uint8_t *buf, const Symbol &sym, 445 uint64_t /*pltEntryAddr*/) const { 446 if (config->isPic) { 447 const uint8_t inst[] = { 448 0xf3, 0x0f, 0x1e, 0xfb, // endbr32 449 0xff, 0xa3, 0, 0, 0, 0, // jmp *name@GOT(%ebx) 450 0x66, 0x0f, 0x1f, 0x44, 0, 0, // nop 451 }; 452 memcpy(buf, inst, sizeof(inst)); 453 write32le(buf + 6, sym.getGotPltVA() - in.gotPlt->getVA()); 454 return; 455 } 456 457 const uint8_t inst[] = { 458 0xf3, 0x0f, 0x1e, 0xfb, // endbr32 459 0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT 460 0x66, 0x0f, 0x1f, 0x44, 0, 0, // nop 461 }; 462 memcpy(buf, inst, sizeof(inst)); 463 write32le(buf + 6, sym.getGotPltVA()); 464 } 465 466 void IntelIBT::writeIBTPlt(uint8_t *buf, size_t numEntries) const { 467 writePltHeader(buf); 468 buf += IBTPltHeaderSize; 469 470 const uint8_t inst[] = { 471 0xf3, 0x0f, 0x1e, 0xfb, // endbr32 472 0x68, 0, 0, 0, 0, // pushl $reloc_offset 473 0xe9, 0, 0, 0, 0, // jmpq .PLT0@PC 474 0x66, 0x90, // nop 475 }; 476 477 for (size_t i = 0; i < numEntries; ++i) { 478 memcpy(buf, inst, sizeof(inst)); 479 write32le(buf + 5, i * sizeof(object::ELF32LE::Rel)); 480 write32le(buf + 10, -pltHeaderSize - sizeof(inst) * i - 30); 481 buf += sizeof(inst); 482 } 483 } 484 485 namespace { 486 class RetpolinePic : public X86 { 487 public: 488 RetpolinePic(); 489 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 490 void writePltHeader(uint8_t *buf) const override; 491 void writePlt(uint8_t *buf, const Symbol &sym, 492 uint64_t pltEntryAddr) const override; 493 }; 494 495 class RetpolineNoPic : public X86 { 496 public: 497 RetpolineNoPic(); 498 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 499 void writePltHeader(uint8_t *buf) const override; 500 void writePlt(uint8_t *buf, const Symbol &sym, 501 uint64_t pltEntryAddr) const override; 502 }; 503 } // namespace 504 505 RetpolinePic::RetpolinePic() { 506 pltHeaderSize = 48; 507 pltEntrySize = 32; 508 ipltEntrySize = 32; 509 } 510 511 void RetpolinePic::writeGotPlt(uint8_t *buf, const Symbol &s) const { 512 write32le(buf, s.getPltVA() + 17); 513 } 514 515 void RetpolinePic::writePltHeader(uint8_t *buf) const { 516 const uint8_t insn[] = { 517 0xff, 0xb3, 4, 0, 0, 0, // 0: pushl 4(%ebx) 518 0x50, // 6: pushl %eax 519 0x8b, 0x83, 8, 0, 0, 0, // 7: mov 8(%ebx), %eax 520 0xe8, 0x0e, 0x00, 0x00, 0x00, // d: call next 521 0xf3, 0x90, // 12: loop: pause 522 0x0f, 0xae, 0xe8, // 14: lfence 523 0xeb, 0xf9, // 17: jmp loop 524 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 19: int3; .align 16 525 0x89, 0x0c, 0x24, // 20: next: mov %ecx, (%esp) 526 0x8b, 0x4c, 0x24, 0x04, // 23: mov 0x4(%esp), %ecx 527 0x89, 0x44, 0x24, 0x04, // 27: mov %eax ,0x4(%esp) 528 0x89, 0xc8, // 2b: mov %ecx, %eax 529 0x59, // 2d: pop %ecx 530 0xc3, // 2e: ret 531 0xcc, // 2f: int3; padding 532 }; 533 memcpy(buf, insn, sizeof(insn)); 534 } 535 536 void RetpolinePic::writePlt(uint8_t *buf, const Symbol &sym, 537 uint64_t pltEntryAddr) const { 538 unsigned relOff = in.relaPlt->entsize * sym.pltIndex; 539 const uint8_t insn[] = { 540 0x50, // pushl %eax 541 0x8b, 0x83, 0, 0, 0, 0, // mov foo@GOT(%ebx), %eax 542 0xe8, 0, 0, 0, 0, // call plt+0x20 543 0xe9, 0, 0, 0, 0, // jmp plt+0x12 544 0x68, 0, 0, 0, 0, // pushl $reloc_offset 545 0xe9, 0, 0, 0, 0, // jmp plt+0 546 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // int3; padding 547 }; 548 memcpy(buf, insn, sizeof(insn)); 549 550 uint32_t ebx = in.gotPlt->getVA(); 551 unsigned off = pltEntryAddr - in.plt->getVA(); 552 write32le(buf + 3, sym.getGotPltVA() - ebx); 553 write32le(buf + 8, -off - 12 + 32); 554 write32le(buf + 13, -off - 17 + 18); 555 write32le(buf + 18, relOff); 556 write32le(buf + 23, -off - 27); 557 } 558 559 RetpolineNoPic::RetpolineNoPic() { 560 pltHeaderSize = 48; 561 pltEntrySize = 32; 562 ipltEntrySize = 32; 563 } 564 565 void RetpolineNoPic::writeGotPlt(uint8_t *buf, const Symbol &s) const { 566 write32le(buf, s.getPltVA() + 16); 567 } 568 569 void RetpolineNoPic::writePltHeader(uint8_t *buf) const { 570 const uint8_t insn[] = { 571 0xff, 0x35, 0, 0, 0, 0, // 0: pushl GOTPLT+4 572 0x50, // 6: pushl %eax 573 0xa1, 0, 0, 0, 0, // 7: mov GOTPLT+8, %eax 574 0xe8, 0x0f, 0x00, 0x00, 0x00, // c: call next 575 0xf3, 0x90, // 11: loop: pause 576 0x0f, 0xae, 0xe8, // 13: lfence 577 0xeb, 0xf9, // 16: jmp loop 578 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 18: int3 579 0xcc, 0xcc, 0xcc, // 1f: int3; .align 16 580 0x89, 0x0c, 0x24, // 20: next: mov %ecx, (%esp) 581 0x8b, 0x4c, 0x24, 0x04, // 23: mov 0x4(%esp), %ecx 582 0x89, 0x44, 0x24, 0x04, // 27: mov %eax ,0x4(%esp) 583 0x89, 0xc8, // 2b: mov %ecx, %eax 584 0x59, // 2d: pop %ecx 585 0xc3, // 2e: ret 586 0xcc, // 2f: int3; padding 587 }; 588 memcpy(buf, insn, sizeof(insn)); 589 590 uint32_t gotPlt = in.gotPlt->getVA(); 591 write32le(buf + 2, gotPlt + 4); 592 write32le(buf + 8, gotPlt + 8); 593 } 594 595 void RetpolineNoPic::writePlt(uint8_t *buf, const Symbol &sym, 596 uint64_t pltEntryAddr) const { 597 unsigned relOff = in.relaPlt->entsize * sym.pltIndex; 598 const uint8_t insn[] = { 599 0x50, // 0: pushl %eax 600 0xa1, 0, 0, 0, 0, // 1: mov foo_in_GOT, %eax 601 0xe8, 0, 0, 0, 0, // 6: call plt+0x20 602 0xe9, 0, 0, 0, 0, // b: jmp plt+0x11 603 0x68, 0, 0, 0, 0, // 10: pushl $reloc_offset 604 0xe9, 0, 0, 0, 0, // 15: jmp plt+0 605 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1a: int3; padding 606 0xcc, // 1f: int3; padding 607 }; 608 memcpy(buf, insn, sizeof(insn)); 609 610 unsigned off = pltEntryAddr - in.plt->getVA(); 611 write32le(buf + 2, sym.getGotPltVA()); 612 write32le(buf + 7, -off - 11 + 32); 613 write32le(buf + 12, -off - 16 + 17); 614 write32le(buf + 17, relOff); 615 write32le(buf + 22, -off - 26); 616 } 617 618 TargetInfo *getX86TargetInfo() { 619 if (config->zRetpolineplt) { 620 if (config->isPic) { 621 static RetpolinePic t; 622 return &t; 623 } 624 static RetpolineNoPic t; 625 return &t; 626 } 627 628 if (config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) { 629 static IntelIBT t; 630 return &t; 631 } 632 633 static X86 t; 634 return &t; 635 } 636 637 } // namespace elf 638 } // namespace lld 639