1 //===- X86_64.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 "OutputSections.h" 10 #include "Relocations.h" 11 #include "Symbols.h" 12 #include "SyntheticSections.h" 13 #include "Target.h" 14 #include "lld/Common/ErrorHandler.h" 15 #include "llvm/BinaryFormat/ELF.h" 16 #include "llvm/Support/Endian.h" 17 #include "llvm/Support/MathExtras.h" 18 19 using namespace llvm; 20 using namespace llvm::object; 21 using namespace llvm::support::endian; 22 using namespace llvm::ELF; 23 using namespace lld; 24 using namespace lld::elf; 25 26 namespace { 27 class X86_64 : public TargetInfo { 28 public: 29 X86_64(); 30 int getTlsGdRelaxSkip(RelType type) const override; 31 RelExpr getRelExpr(RelType type, const Symbol &s, 32 const uint8_t *loc) const override; 33 RelType getDynRel(RelType type) const override; 34 void writeGotPltHeader(uint8_t *buf) const override; 35 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 36 void writeIgotPlt(uint8_t *buf, const Symbol &s) const override; 37 void writePltHeader(uint8_t *buf) const override; 38 void writePlt(uint8_t *buf, const Symbol &sym, 39 uint64_t pltEntryAddr) const override; 40 void relocate(uint8_t *loc, const Relocation &rel, 41 uint64_t val) const override; 42 int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override; 43 void applyJumpInstrMod(uint8_t *loc, JumpModType type, 44 unsigned size) const override; 45 RelExpr adjustGotPcExpr(RelType type, int64_t addend, 46 const uint8_t *loc) const override; 47 void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override; 48 bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end, 49 uint8_t stOther) const override; 50 bool deleteFallThruJmpInsn(InputSection &is, InputFile *file, 51 InputSection *nextIS) const override; 52 bool relaxOnce(int pass) const override; 53 }; 54 } // namespace 55 56 // This is vector of NOP instructions of sizes from 1 to 8 bytes. The 57 // appropriately sized instructions are used to fill the gaps between sections 58 // which are executed during fall through. 59 static const std::vector<std::vector<uint8_t>> nopInstructions = { 60 {0x90}, 61 {0x66, 0x90}, 62 {0x0f, 0x1f, 0x00}, 63 {0x0f, 0x1f, 0x40, 0x00}, 64 {0x0f, 0x1f, 0x44, 0x00, 0x00}, 65 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00}, 66 {0x0F, 0x1F, 0x80, 0x00, 0x00, 0x00, 0x00}, 67 {0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}, 68 {0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}}; 69 70 X86_64::X86_64() { 71 copyRel = R_X86_64_COPY; 72 gotRel = R_X86_64_GLOB_DAT; 73 pltRel = R_X86_64_JUMP_SLOT; 74 relativeRel = R_X86_64_RELATIVE; 75 iRelativeRel = R_X86_64_IRELATIVE; 76 symbolicRel = R_X86_64_64; 77 tlsDescRel = R_X86_64_TLSDESC; 78 tlsGotRel = R_X86_64_TPOFF64; 79 tlsModuleIndexRel = R_X86_64_DTPMOD64; 80 tlsOffsetRel = R_X86_64_DTPOFF64; 81 gotBaseSymInGotPlt = true; 82 gotEntrySize = 8; 83 pltHeaderSize = 16; 84 pltEntrySize = 16; 85 ipltEntrySize = 16; 86 trapInstr = {0xcc, 0xcc, 0xcc, 0xcc}; // 0xcc = INT3 87 nopInstrs = nopInstructions; 88 89 // Align to the large page size (known as a superpage or huge page). 90 // FreeBSD automatically promotes large, superpage-aligned allocations. 91 defaultImageBase = 0x200000; 92 } 93 94 int X86_64::getTlsGdRelaxSkip(RelType type) const { 95 // TLSDESC relocations are processed separately. See relaxTlsGdToLe below. 96 return type == R_X86_64_GOTPC32_TLSDESC || type == R_X86_64_TLSDESC_CALL ? 1 97 : 2; 98 } 99 100 // Opcodes for the different X86_64 jmp instructions. 101 enum JmpInsnOpcode : uint32_t { 102 J_JMP_32, 103 J_JNE_32, 104 J_JE_32, 105 J_JG_32, 106 J_JGE_32, 107 J_JB_32, 108 J_JBE_32, 109 J_JL_32, 110 J_JLE_32, 111 J_JA_32, 112 J_JAE_32, 113 J_UNKNOWN, 114 }; 115 116 // Given the first (optional) and second byte of the insn's opcode, this 117 // returns the corresponding enum value. 118 static JmpInsnOpcode getJmpInsnType(const uint8_t *first, 119 const uint8_t *second) { 120 if (*second == 0xe9) 121 return J_JMP_32; 122 123 if (first == nullptr) 124 return J_UNKNOWN; 125 126 if (*first == 0x0f) { 127 switch (*second) { 128 case 0x84: 129 return J_JE_32; 130 case 0x85: 131 return J_JNE_32; 132 case 0x8f: 133 return J_JG_32; 134 case 0x8d: 135 return J_JGE_32; 136 case 0x82: 137 return J_JB_32; 138 case 0x86: 139 return J_JBE_32; 140 case 0x8c: 141 return J_JL_32; 142 case 0x8e: 143 return J_JLE_32; 144 case 0x87: 145 return J_JA_32; 146 case 0x83: 147 return J_JAE_32; 148 } 149 } 150 return J_UNKNOWN; 151 } 152 153 // Return the relocation index for input section IS with a specific Offset. 154 // Returns the maximum size of the vector if no such relocation is found. 155 static unsigned getRelocationWithOffset(const InputSection &is, 156 uint64_t offset) { 157 unsigned size = is.relocs().size(); 158 for (unsigned i = size - 1; i + 1 > 0; --i) { 159 if (is.relocs()[i].offset == offset && is.relocs()[i].expr != R_NONE) 160 return i; 161 } 162 return size; 163 } 164 165 // Returns true if R corresponds to a relocation used for a jump instruction. 166 // TODO: Once special relocations for relaxable jump instructions are available, 167 // this should be modified to use those relocations. 168 static bool isRelocationForJmpInsn(Relocation &R) { 169 return R.type == R_X86_64_PLT32 || R.type == R_X86_64_PC32 || 170 R.type == R_X86_64_PC8; 171 } 172 173 // Return true if Relocation R points to the first instruction in the 174 // next section. 175 // TODO: Delete this once psABI reserves a new relocation type for fall thru 176 // jumps. 177 static bool isFallThruRelocation(InputSection &is, InputFile *file, 178 InputSection *nextIS, Relocation &r) { 179 if (!isRelocationForJmpInsn(r)) 180 return false; 181 182 uint64_t addrLoc = is.getOutputSection()->addr + is.outSecOff + r.offset; 183 uint64_t targetOffset = InputSectionBase::getRelocTargetVA( 184 file, r.type, r.addend, addrLoc, *r.sym, r.expr); 185 186 // If this jmp is a fall thru, the target offset is the beginning of the 187 // next section. 188 uint64_t nextSectionOffset = 189 nextIS->getOutputSection()->addr + nextIS->outSecOff; 190 return (addrLoc + 4 + targetOffset) == nextSectionOffset; 191 } 192 193 // Return the jmp instruction opcode that is the inverse of the given 194 // opcode. For example, JE inverted is JNE. 195 static JmpInsnOpcode invertJmpOpcode(const JmpInsnOpcode opcode) { 196 switch (opcode) { 197 case J_JE_32: 198 return J_JNE_32; 199 case J_JNE_32: 200 return J_JE_32; 201 case J_JG_32: 202 return J_JLE_32; 203 case J_JGE_32: 204 return J_JL_32; 205 case J_JB_32: 206 return J_JAE_32; 207 case J_JBE_32: 208 return J_JA_32; 209 case J_JL_32: 210 return J_JGE_32; 211 case J_JLE_32: 212 return J_JG_32; 213 case J_JA_32: 214 return J_JBE_32; 215 case J_JAE_32: 216 return J_JB_32; 217 default: 218 return J_UNKNOWN; 219 } 220 } 221 222 // Deletes direct jump instruction in input sections that jumps to the 223 // following section as it is not required. If there are two consecutive jump 224 // instructions, it checks if they can be flipped and one can be deleted. 225 // For example: 226 // .section .text 227 // a.BB.foo: 228 // ... 229 // 10: jne aa.BB.foo 230 // 16: jmp bar 231 // aa.BB.foo: 232 // ... 233 // 234 // can be converted to: 235 // a.BB.foo: 236 // ... 237 // 10: je bar #jne flipped to je and the jmp is deleted. 238 // aa.BB.foo: 239 // ... 240 bool X86_64::deleteFallThruJmpInsn(InputSection &is, InputFile *file, 241 InputSection *nextIS) const { 242 const unsigned sizeOfDirectJmpInsn = 5; 243 244 if (nextIS == nullptr) 245 return false; 246 247 if (is.getSize() < sizeOfDirectJmpInsn) 248 return false; 249 250 // If this jmp insn can be removed, it is the last insn and the 251 // relocation is 4 bytes before the end. 252 unsigned rIndex = getRelocationWithOffset(is, is.getSize() - 4); 253 if (rIndex == is.relocs().size()) 254 return false; 255 256 Relocation &r = is.relocs()[rIndex]; 257 258 // Check if the relocation corresponds to a direct jmp. 259 const uint8_t *secContents = is.content().data(); 260 // If it is not a direct jmp instruction, there is nothing to do here. 261 if (*(secContents + r.offset - 1) != 0xe9) 262 return false; 263 264 if (isFallThruRelocation(is, file, nextIS, r)) { 265 // This is a fall thru and can be deleted. 266 r.expr = R_NONE; 267 r.offset = 0; 268 is.drop_back(sizeOfDirectJmpInsn); 269 is.nopFiller = true; 270 return true; 271 } 272 273 // Now, check if flip and delete is possible. 274 const unsigned sizeOfJmpCCInsn = 6; 275 // To flip, there must be at least one JmpCC and one direct jmp. 276 if (is.getSize() < sizeOfDirectJmpInsn + sizeOfJmpCCInsn) 277 return false; 278 279 unsigned rbIndex = 280 getRelocationWithOffset(is, (is.getSize() - sizeOfDirectJmpInsn - 4)); 281 if (rbIndex == is.relocs().size()) 282 return false; 283 284 Relocation &rB = is.relocs()[rbIndex]; 285 286 const uint8_t *jmpInsnB = secContents + rB.offset - 1; 287 JmpInsnOpcode jmpOpcodeB = getJmpInsnType(jmpInsnB - 1, jmpInsnB); 288 if (jmpOpcodeB == J_UNKNOWN) 289 return false; 290 291 if (!isFallThruRelocation(is, file, nextIS, rB)) 292 return false; 293 294 // jmpCC jumps to the fall thru block, the branch can be flipped and the 295 // jmp can be deleted. 296 JmpInsnOpcode jInvert = invertJmpOpcode(jmpOpcodeB); 297 if (jInvert == J_UNKNOWN) 298 return false; 299 is.jumpInstrMod = make<JumpInstrMod>(); 300 *is.jumpInstrMod = {rB.offset - 1, jInvert, 4}; 301 // Move R's values to rB except the offset. 302 rB = {r.expr, r.type, rB.offset, r.addend, r.sym}; 303 // Cancel R 304 r.expr = R_NONE; 305 r.offset = 0; 306 is.drop_back(sizeOfDirectJmpInsn); 307 is.nopFiller = true; 308 return true; 309 } 310 311 bool X86_64::relaxOnce(int pass) const { 312 uint64_t minVA = UINT64_MAX, maxVA = 0; 313 for (OutputSection *osec : outputSections) { 314 minVA = std::min(minVA, osec->addr); 315 maxVA = std::max(maxVA, osec->addr + osec->size); 316 } 317 // If the max VA difference is under 2^31, GOT-generating relocations with a 32-bit range cannot overflow. 318 if (isUInt<31>(maxVA - minVA)) 319 return false; 320 321 SmallVector<InputSection *, 0> storage; 322 bool changed = false; 323 for (OutputSection *osec : outputSections) { 324 if (!(osec->flags & SHF_EXECINSTR)) 325 continue; 326 for (InputSection *sec : getInputSections(*osec, storage)) { 327 for (Relocation &rel : sec->relocs()) { 328 if (rel.expr != R_RELAX_GOT_PC) 329 continue; 330 331 uint64_t v = sec->getRelocTargetVA( 332 sec->file, rel.type, rel.addend, 333 sec->getOutputSection()->addr + rel.offset, *rel.sym, rel.expr); 334 if (isInt<32>(v)) 335 continue; 336 if (rel.sym->auxIdx == 0) { 337 rel.sym->allocateAux(); 338 addGotEntry(*rel.sym); 339 changed = true; 340 } 341 rel.expr = R_GOT_PC; 342 } 343 } 344 } 345 return changed; 346 } 347 348 RelExpr X86_64::getRelExpr(RelType type, const Symbol &s, 349 const uint8_t *loc) const { 350 switch (type) { 351 case R_X86_64_8: 352 case R_X86_64_16: 353 case R_X86_64_32: 354 case R_X86_64_32S: 355 case R_X86_64_64: 356 return R_ABS; 357 case R_X86_64_DTPOFF32: 358 case R_X86_64_DTPOFF64: 359 return R_DTPREL; 360 case R_X86_64_TPOFF32: 361 case R_X86_64_TPOFF64: 362 return R_TPREL; 363 case R_X86_64_TLSDESC_CALL: 364 return R_TLSDESC_CALL; 365 case R_X86_64_TLSLD: 366 return R_TLSLD_PC; 367 case R_X86_64_TLSGD: 368 return R_TLSGD_PC; 369 case R_X86_64_SIZE32: 370 case R_X86_64_SIZE64: 371 return R_SIZE; 372 case R_X86_64_PLT32: 373 return R_PLT_PC; 374 case R_X86_64_PC8: 375 case R_X86_64_PC16: 376 case R_X86_64_PC32: 377 case R_X86_64_PC64: 378 return R_PC; 379 case R_X86_64_GOT32: 380 case R_X86_64_GOT64: 381 return R_GOTPLT; 382 case R_X86_64_GOTPC32_TLSDESC: 383 return R_TLSDESC_PC; 384 case R_X86_64_GOTPCREL: 385 case R_X86_64_GOTPCRELX: 386 case R_X86_64_REX_GOTPCRELX: 387 case R_X86_64_GOTTPOFF: 388 return R_GOT_PC; 389 case R_X86_64_GOTOFF64: 390 return R_GOTPLTREL; 391 case R_X86_64_PLTOFF64: 392 return R_PLT_GOTPLT; 393 case R_X86_64_GOTPC32: 394 case R_X86_64_GOTPC64: 395 return R_GOTPLTONLY_PC; 396 case R_X86_64_NONE: 397 return R_NONE; 398 default: 399 error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 400 ") against symbol " + toString(s)); 401 return R_NONE; 402 } 403 } 404 405 void X86_64::writeGotPltHeader(uint8_t *buf) const { 406 // The first entry holds the link-time address of _DYNAMIC. It is documented 407 // in the psABI and glibc before Aug 2021 used the entry to compute run-time 408 // load address of the shared object (note that this is relevant for linking 409 // ld.so, not any other program). 410 write64le(buf, mainPart->dynamic->getVA()); 411 } 412 413 void X86_64::writeGotPlt(uint8_t *buf, const Symbol &s) const { 414 // See comments in X86::writeGotPlt. 415 write64le(buf, s.getPltVA() + 6); 416 } 417 418 void X86_64::writeIgotPlt(uint8_t *buf, const Symbol &s) const { 419 // An x86 entry is the address of the ifunc resolver function (for -z rel). 420 if (config->writeAddends) 421 write64le(buf, s.getVA()); 422 } 423 424 void X86_64::writePltHeader(uint8_t *buf) const { 425 const uint8_t pltData[] = { 426 0xff, 0x35, 0, 0, 0, 0, // pushq GOTPLT+8(%rip) 427 0xff, 0x25, 0, 0, 0, 0, // jmp *GOTPLT+16(%rip) 428 0x0f, 0x1f, 0x40, 0x00, // nop 429 }; 430 memcpy(buf, pltData, sizeof(pltData)); 431 uint64_t gotPlt = in.gotPlt->getVA(); 432 uint64_t plt = in.ibtPlt ? in.ibtPlt->getVA() : in.plt->getVA(); 433 write32le(buf + 2, gotPlt - plt + 2); // GOTPLT+8 434 write32le(buf + 8, gotPlt - plt + 4); // GOTPLT+16 435 } 436 437 void X86_64::writePlt(uint8_t *buf, const Symbol &sym, 438 uint64_t pltEntryAddr) const { 439 const uint8_t inst[] = { 440 0xff, 0x25, 0, 0, 0, 0, // jmpq *got(%rip) 441 0x68, 0, 0, 0, 0, // pushq <relocation index> 442 0xe9, 0, 0, 0, 0, // jmpq plt[0] 443 }; 444 memcpy(buf, inst, sizeof(inst)); 445 446 write32le(buf + 2, sym.getGotPltVA() - pltEntryAddr - 6); 447 write32le(buf + 7, sym.getPltIdx()); 448 write32le(buf + 12, in.plt->getVA() - pltEntryAddr - 16); 449 } 450 451 RelType X86_64::getDynRel(RelType type) const { 452 if (type == R_X86_64_64 || type == R_X86_64_PC64 || type == R_X86_64_SIZE32 || 453 type == R_X86_64_SIZE64) 454 return type; 455 return R_X86_64_NONE; 456 } 457 458 static void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) { 459 if (rel.type == R_X86_64_TLSGD) { 460 // Convert 461 // .byte 0x66 462 // leaq x@tlsgd(%rip), %rdi 463 // .word 0x6666 464 // rex64 465 // call __tls_get_addr@plt 466 // to the following two instructions. 467 const uint8_t inst[] = { 468 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 469 0x00, 0x00, // mov %fs:0x0,%rax 470 0x48, 0x8d, 0x80, 0, 0, 0, 0, // lea x@tpoff,%rax 471 }; 472 memcpy(loc - 4, inst, sizeof(inst)); 473 474 // The original code used a pc relative relocation and so we have to 475 // compensate for the -4 in had in the addend. 476 write32le(loc + 8, val + 4); 477 } else if (rel.type == R_X86_64_GOTPC32_TLSDESC) { 478 // Convert leaq x@tlsdesc(%rip), %REG to movq $x@tpoff, %REG. 479 if ((loc[-3] & 0xfb) != 0x48 || loc[-2] != 0x8d || 480 (loc[-1] & 0xc7) != 0x05) { 481 errorOrWarn(getErrorLocation(loc - 3) + 482 "R_X86_64_GOTPC32_TLSDESC must be used " 483 "in leaq x@tlsdesc(%rip), %REG"); 484 return; 485 } 486 loc[-3] = 0x48 | ((loc[-3] >> 2) & 1); 487 loc[-2] = 0xc7; 488 loc[-1] = 0xc0 | ((loc[-1] >> 3) & 7); 489 write32le(loc, val + 4); 490 } else { 491 // Convert call *x@tlsdesc(%REG) to xchg ax, ax. 492 assert(rel.type == R_X86_64_TLSDESC_CALL); 493 loc[0] = 0x66; 494 loc[1] = 0x90; 495 } 496 } 497 498 static void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) { 499 if (rel.type == R_X86_64_TLSGD) { 500 // Convert 501 // .byte 0x66 502 // leaq x@tlsgd(%rip), %rdi 503 // .word 0x6666 504 // rex64 505 // call __tls_get_addr@plt 506 // to the following two instructions. 507 const uint8_t inst[] = { 508 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 509 0x00, 0x00, // mov %fs:0x0,%rax 510 0x48, 0x03, 0x05, 0, 0, 0, 0, // addq x@gottpoff(%rip),%rax 511 }; 512 memcpy(loc - 4, inst, sizeof(inst)); 513 514 // Both code sequences are PC relatives, but since we are moving the 515 // constant forward by 8 bytes we have to subtract the value by 8. 516 write32le(loc + 8, val - 8); 517 } else if (rel.type == R_X86_64_GOTPC32_TLSDESC) { 518 // Convert leaq x@tlsdesc(%rip), %REG to movq x@gottpoff(%rip), %REG. 519 assert(rel.type == R_X86_64_GOTPC32_TLSDESC); 520 if ((loc[-3] & 0xfb) != 0x48 || loc[-2] != 0x8d || 521 (loc[-1] & 0xc7) != 0x05) { 522 errorOrWarn(getErrorLocation(loc - 3) + 523 "R_X86_64_GOTPC32_TLSDESC must be used " 524 "in leaq x@tlsdesc(%rip), %REG"); 525 return; 526 } 527 loc[-2] = 0x8b; 528 write32le(loc, val); 529 } else { 530 // Convert call *x@tlsdesc(%rax) to xchg ax, ax. 531 assert(rel.type == R_X86_64_TLSDESC_CALL); 532 loc[0] = 0x66; 533 loc[1] = 0x90; 534 } 535 } 536 537 // In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to 538 // R_X86_64_TPOFF32 so that it does not use GOT. 539 static void relaxTlsIeToLe(uint8_t *loc, const Relocation &, uint64_t val) { 540 uint8_t *inst = loc - 3; 541 uint8_t reg = loc[-1] >> 3; 542 uint8_t *regSlot = loc - 1; 543 544 // Note that ADD with RSP or R12 is converted to ADD instead of LEA 545 // because LEA with these registers needs 4 bytes to encode and thus 546 // wouldn't fit the space. 547 548 if (memcmp(inst, "\x48\x03\x25", 3) == 0) { 549 // "addq foo@gottpoff(%rip),%rsp" -> "addq $foo,%rsp" 550 memcpy(inst, "\x48\x81\xc4", 3); 551 } else if (memcmp(inst, "\x4c\x03\x25", 3) == 0) { 552 // "addq foo@gottpoff(%rip),%r12" -> "addq $foo,%r12" 553 memcpy(inst, "\x49\x81\xc4", 3); 554 } else if (memcmp(inst, "\x4c\x03", 2) == 0) { 555 // "addq foo@gottpoff(%rip),%r[8-15]" -> "leaq foo(%r[8-15]),%r[8-15]" 556 memcpy(inst, "\x4d\x8d", 2); 557 *regSlot = 0x80 | (reg << 3) | reg; 558 } else if (memcmp(inst, "\x48\x03", 2) == 0) { 559 // "addq foo@gottpoff(%rip),%reg -> "leaq foo(%reg),%reg" 560 memcpy(inst, "\x48\x8d", 2); 561 *regSlot = 0x80 | (reg << 3) | reg; 562 } else if (memcmp(inst, "\x4c\x8b", 2) == 0) { 563 // "movq foo@gottpoff(%rip),%r[8-15]" -> "movq $foo,%r[8-15]" 564 memcpy(inst, "\x49\xc7", 2); 565 *regSlot = 0xc0 | reg; 566 } else if (memcmp(inst, "\x48\x8b", 2) == 0) { 567 // "movq foo@gottpoff(%rip),%reg" -> "movq $foo,%reg" 568 memcpy(inst, "\x48\xc7", 2); 569 *regSlot = 0xc0 | reg; 570 } else { 571 error(getErrorLocation(loc - 3) + 572 "R_X86_64_GOTTPOFF must be used in MOVQ or ADDQ instructions only"); 573 } 574 575 // The original code used a PC relative relocation. 576 // Need to compensate for the -4 it had in the addend. 577 write32le(loc, val + 4); 578 } 579 580 static void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) { 581 const uint8_t inst[] = { 582 0x66, 0x66, // .word 0x6666 583 0x66, // .byte 0x66 584 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0,%rax 585 }; 586 587 if (loc[4] == 0xe8) { 588 // Convert 589 // leaq bar@tlsld(%rip), %rdi # 48 8d 3d <Loc> 590 // callq __tls_get_addr@PLT # e8 <disp32> 591 // leaq bar@dtpoff(%rax), %rcx 592 // to 593 // .word 0x6666 594 // .byte 0x66 595 // mov %fs:0,%rax 596 // leaq bar@tpoff(%rax), %rcx 597 memcpy(loc - 3, inst, sizeof(inst)); 598 return; 599 } 600 601 if (loc[4] == 0xff && loc[5] == 0x15) { 602 // Convert 603 // leaq x@tlsld(%rip),%rdi # 48 8d 3d <Loc> 604 // call *__tls_get_addr@GOTPCREL(%rip) # ff 15 <disp32> 605 // to 606 // .long 0x66666666 607 // movq %fs:0,%rax 608 // See "Table 11.9: LD -> LE Code Transition (LP64)" in 609 // https://raw.githubusercontent.com/wiki/hjl-tools/x86-psABI/x86-64-psABI-1.0.pdf 610 loc[-3] = 0x66; 611 memcpy(loc - 2, inst, sizeof(inst)); 612 return; 613 } 614 615 error(getErrorLocation(loc - 3) + 616 "expected R_X86_64_PLT32 or R_X86_64_GOTPCRELX after R_X86_64_TLSLD"); 617 } 618 619 // A JumpInstrMod at a specific offset indicates that the jump instruction 620 // opcode at that offset must be modified. This is specifically used to relax 621 // jump instructions with basic block sections. This function looks at the 622 // JumpMod and effects the change. 623 void X86_64::applyJumpInstrMod(uint8_t *loc, JumpModType type, 624 unsigned size) const { 625 switch (type) { 626 case J_JMP_32: 627 if (size == 4) 628 *loc = 0xe9; 629 else 630 *loc = 0xeb; 631 break; 632 case J_JE_32: 633 if (size == 4) { 634 loc[-1] = 0x0f; 635 *loc = 0x84; 636 } else 637 *loc = 0x74; 638 break; 639 case J_JNE_32: 640 if (size == 4) { 641 loc[-1] = 0x0f; 642 *loc = 0x85; 643 } else 644 *loc = 0x75; 645 break; 646 case J_JG_32: 647 if (size == 4) { 648 loc[-1] = 0x0f; 649 *loc = 0x8f; 650 } else 651 *loc = 0x7f; 652 break; 653 case J_JGE_32: 654 if (size == 4) { 655 loc[-1] = 0x0f; 656 *loc = 0x8d; 657 } else 658 *loc = 0x7d; 659 break; 660 case J_JB_32: 661 if (size == 4) { 662 loc[-1] = 0x0f; 663 *loc = 0x82; 664 } else 665 *loc = 0x72; 666 break; 667 case J_JBE_32: 668 if (size == 4) { 669 loc[-1] = 0x0f; 670 *loc = 0x86; 671 } else 672 *loc = 0x76; 673 break; 674 case J_JL_32: 675 if (size == 4) { 676 loc[-1] = 0x0f; 677 *loc = 0x8c; 678 } else 679 *loc = 0x7c; 680 break; 681 case J_JLE_32: 682 if (size == 4) { 683 loc[-1] = 0x0f; 684 *loc = 0x8e; 685 } else 686 *loc = 0x7e; 687 break; 688 case J_JA_32: 689 if (size == 4) { 690 loc[-1] = 0x0f; 691 *loc = 0x87; 692 } else 693 *loc = 0x77; 694 break; 695 case J_JAE_32: 696 if (size == 4) { 697 loc[-1] = 0x0f; 698 *loc = 0x83; 699 } else 700 *loc = 0x73; 701 break; 702 case J_UNKNOWN: 703 llvm_unreachable("Unknown Jump Relocation"); 704 } 705 } 706 707 int64_t X86_64::getImplicitAddend(const uint8_t *buf, RelType type) const { 708 switch (type) { 709 case R_X86_64_8: 710 case R_X86_64_PC8: 711 return SignExtend64<8>(*buf); 712 case R_X86_64_16: 713 case R_X86_64_PC16: 714 return SignExtend64<16>(read16le(buf)); 715 case R_X86_64_32: 716 case R_X86_64_32S: 717 case R_X86_64_TPOFF32: 718 case R_X86_64_GOT32: 719 case R_X86_64_GOTPC32: 720 case R_X86_64_GOTPC32_TLSDESC: 721 case R_X86_64_GOTPCREL: 722 case R_X86_64_GOTPCRELX: 723 case R_X86_64_REX_GOTPCRELX: 724 case R_X86_64_PC32: 725 case R_X86_64_GOTTPOFF: 726 case R_X86_64_PLT32: 727 case R_X86_64_TLSGD: 728 case R_X86_64_TLSLD: 729 case R_X86_64_DTPOFF32: 730 case R_X86_64_SIZE32: 731 return SignExtend64<32>(read32le(buf)); 732 case R_X86_64_64: 733 case R_X86_64_TPOFF64: 734 case R_X86_64_DTPOFF64: 735 case R_X86_64_DTPMOD64: 736 case R_X86_64_PC64: 737 case R_X86_64_SIZE64: 738 case R_X86_64_GLOB_DAT: 739 case R_X86_64_GOT64: 740 case R_X86_64_GOTOFF64: 741 case R_X86_64_GOTPC64: 742 case R_X86_64_PLTOFF64: 743 case R_X86_64_IRELATIVE: 744 case R_X86_64_RELATIVE: 745 return read64le(buf); 746 case R_X86_64_TLSDESC: 747 return read64le(buf + 8); 748 case R_X86_64_JUMP_SLOT: 749 case R_X86_64_NONE: 750 // These relocations are defined as not having an implicit addend. 751 return 0; 752 default: 753 internalLinkerError(getErrorLocation(buf), 754 "cannot read addend for relocation " + toString(type)); 755 return 0; 756 } 757 } 758 759 static void relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val); 760 761 void X86_64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 762 switch (rel.type) { 763 case R_X86_64_8: 764 checkIntUInt(loc, val, 8, rel); 765 *loc = val; 766 break; 767 case R_X86_64_PC8: 768 checkInt(loc, val, 8, rel); 769 *loc = val; 770 break; 771 case R_X86_64_16: 772 checkIntUInt(loc, val, 16, rel); 773 write16le(loc, val); 774 break; 775 case R_X86_64_PC16: 776 checkInt(loc, val, 16, rel); 777 write16le(loc, val); 778 break; 779 case R_X86_64_32: 780 checkUInt(loc, val, 32, rel); 781 write32le(loc, val); 782 break; 783 case R_X86_64_32S: 784 case R_X86_64_GOT32: 785 case R_X86_64_GOTPC32: 786 case R_X86_64_GOTPCREL: 787 case R_X86_64_PC32: 788 case R_X86_64_PLT32: 789 case R_X86_64_DTPOFF32: 790 case R_X86_64_SIZE32: 791 checkInt(loc, val, 32, rel); 792 write32le(loc, val); 793 break; 794 case R_X86_64_64: 795 case R_X86_64_TPOFF64: 796 case R_X86_64_DTPOFF64: 797 case R_X86_64_PC64: 798 case R_X86_64_SIZE64: 799 case R_X86_64_GOT64: 800 case R_X86_64_GOTOFF64: 801 case R_X86_64_GOTPC64: 802 case R_X86_64_PLTOFF64: 803 write64le(loc, val); 804 break; 805 case R_X86_64_GOTPCRELX: 806 case R_X86_64_REX_GOTPCRELX: 807 if (rel.expr != R_GOT_PC) { 808 relaxGot(loc, rel, val); 809 } else { 810 checkInt(loc, val, 32, rel); 811 write32le(loc, val); 812 } 813 break; 814 case R_X86_64_GOTPC32_TLSDESC: 815 case R_X86_64_TLSDESC_CALL: 816 case R_X86_64_TLSGD: 817 if (rel.expr == R_RELAX_TLS_GD_TO_LE) { 818 relaxTlsGdToLe(loc, rel, val); 819 } else if (rel.expr == R_RELAX_TLS_GD_TO_IE) { 820 relaxTlsGdToIe(loc, rel, val); 821 } else { 822 checkInt(loc, val, 32, rel); 823 write32le(loc, val); 824 } 825 break; 826 case R_X86_64_TLSLD: 827 if (rel.expr == R_RELAX_TLS_LD_TO_LE) { 828 relaxTlsLdToLe(loc, rel, val); 829 } else { 830 checkInt(loc, val, 32, rel); 831 write32le(loc, val); 832 } 833 break; 834 case R_X86_64_GOTTPOFF: 835 if (rel.expr == R_RELAX_TLS_IE_TO_LE) { 836 relaxTlsIeToLe(loc, rel, val); 837 } else { 838 checkInt(loc, val, 32, rel); 839 write32le(loc, val); 840 } 841 break; 842 case R_X86_64_TPOFF32: 843 checkInt(loc, val, 32, rel); 844 write32le(loc, val); 845 break; 846 847 case R_X86_64_TLSDESC: 848 // The addend is stored in the second 64-bit word. 849 write64le(loc + 8, val); 850 break; 851 default: 852 llvm_unreachable("unknown relocation"); 853 } 854 } 855 856 RelExpr X86_64::adjustGotPcExpr(RelType type, int64_t addend, 857 const uint8_t *loc) const { 858 // Only R_X86_64_[REX_]GOTPCRELX can be relaxed. GNU as may emit GOTPCRELX 859 // with addend != -4. Such an instruction does not load the full GOT entry, so 860 // we cannot relax the relocation. E.g. movl x@GOTPCREL+4(%rip), %rax 861 // (addend=0) loads the high 32 bits of the GOT entry. 862 if (!config->relax || addend != -4 || 863 (type != R_X86_64_GOTPCRELX && type != R_X86_64_REX_GOTPCRELX)) 864 return R_GOT_PC; 865 const uint8_t op = loc[-2]; 866 const uint8_t modRm = loc[-1]; 867 868 // FIXME: When PIC is disabled and foo is defined locally in the 869 // lower 32 bit address space, memory operand in mov can be converted into 870 // immediate operand. Otherwise, mov must be changed to lea. We support only 871 // latter relaxation at this moment. 872 if (op == 0x8b) 873 return R_RELAX_GOT_PC; 874 875 // Relax call and jmp. 876 if (op == 0xff && (modRm == 0x15 || modRm == 0x25)) 877 return R_RELAX_GOT_PC; 878 879 // We don't support test/binop instructions without a REX prefix. 880 if (type == R_X86_64_GOTPCRELX) 881 return R_GOT_PC; 882 883 // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor. 884 // If PIC then no relaxation is available. 885 return config->isPic ? R_GOT_PC : R_RELAX_GOT_PC_NOPIC; 886 } 887 888 // A subset of relaxations can only be applied for no-PIC. This method 889 // handles such relaxations. Instructions encoding information was taken from: 890 // "Intel 64 and IA-32 Architectures Software Developer's Manual V2" 891 // (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/ 892 // 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf) 893 static void relaxGotNoPic(uint8_t *loc, uint64_t val, uint8_t op, 894 uint8_t modRm) { 895 const uint8_t rex = loc[-3]; 896 // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg". 897 if (op == 0x85) { 898 // See "TEST-Logical Compare" (4-428 Vol. 2B), 899 // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension). 900 901 // ModR/M byte has form XX YYY ZZZ, where 902 // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1). 903 // XX has different meanings: 904 // 00: The operand's memory address is in reg1. 905 // 01: The operand's memory address is reg1 + a byte-sized displacement. 906 // 10: The operand's memory address is reg1 + a word-sized displacement. 907 // 11: The operand is reg1 itself. 908 // If an instruction requires only one operand, the unused reg2 field 909 // holds extra opcode bits rather than a register code 910 // 0xC0 == 11 000 000 binary. 911 // 0x38 == 00 111 000 binary. 912 // We transfer reg2 to reg1 here as operand. 913 // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3). 914 loc[-1] = 0xc0 | (modRm & 0x38) >> 3; // ModR/M byte. 915 916 // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32 917 // See "TEST-Logical Compare" (4-428 Vol. 2B). 918 loc[-2] = 0xf7; 919 920 // Move R bit to the B bit in REX byte. 921 // REX byte is encoded as 0100WRXB, where 922 // 0100 is 4bit fixed pattern. 923 // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the 924 // default operand size is used (which is 32-bit for most but not all 925 // instructions). 926 // REX.R This 1-bit value is an extension to the MODRM.reg field. 927 // REX.X This 1-bit value is an extension to the SIB.index field. 928 // REX.B This 1-bit value is an extension to the MODRM.rm field or the 929 // SIB.base field. 930 // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A). 931 loc[-3] = (rex & ~0x4) | (rex & 0x4) >> 2; 932 write32le(loc, val); 933 return; 934 } 935 936 // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub 937 // or xor operations. 938 939 // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg". 940 // Logic is close to one for test instruction above, but we also 941 // write opcode extension here, see below for details. 942 loc[-1] = 0xc0 | (modRm & 0x38) >> 3 | (op & 0x3c); // ModR/M byte. 943 944 // Primary opcode is 0x81, opcode extension is one of: 945 // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB, 946 // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP. 947 // This value was wrote to MODRM.reg in a line above. 948 // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15), 949 // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for 950 // descriptions about each operation. 951 loc[-2] = 0x81; 952 loc[-3] = (rex & ~0x4) | (rex & 0x4) >> 2; 953 write32le(loc, val); 954 } 955 956 static void relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) { 957 assert(isInt<32>(val) && 958 "GOTPCRELX should not have been relaxed if it overflows"); 959 const uint8_t op = loc[-2]; 960 const uint8_t modRm = loc[-1]; 961 962 // Convert "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg". 963 if (op == 0x8b) { 964 loc[-2] = 0x8d; 965 write32le(loc, val); 966 return; 967 } 968 969 if (op != 0xff) { 970 // We are relaxing a rip relative to an absolute, so compensate 971 // for the old -4 addend. 972 assert(!config->isPic); 973 relaxGotNoPic(loc, val + 4, op, modRm); 974 return; 975 } 976 977 // Convert call/jmp instructions. 978 if (modRm == 0x15) { 979 // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call foo". 980 // Instead we convert to "addr32 call foo" where addr32 is an instruction 981 // prefix. That makes result expression to be a single instruction. 982 loc[-2] = 0x67; // addr32 prefix 983 loc[-1] = 0xe8; // call 984 write32le(loc, val); 985 return; 986 } 987 988 // Convert "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop". 989 // jmp doesn't return, so it is fine to use nop here, it is just a stub. 990 assert(modRm == 0x25); 991 loc[-2] = 0xe9; // jmp 992 loc[3] = 0x90; // nop 993 write32le(loc - 1, val + 1); 994 } 995 996 // A split-stack prologue starts by checking the amount of stack remaining 997 // in one of two ways: 998 // A) Comparing of the stack pointer to a field in the tcb. 999 // B) Or a load of a stack pointer offset with an lea to r10 or r11. 1000 bool X86_64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end, 1001 uint8_t stOther) const { 1002 if (!config->is64) { 1003 error("target doesn't support split stacks"); 1004 return false; 1005 } 1006 1007 if (loc + 8 >= end) 1008 return false; 1009 1010 // Replace "cmp %fs:0x70,%rsp" and subsequent branch 1011 // with "stc, nopl 0x0(%rax,%rax,1)" 1012 if (memcmp(loc, "\x64\x48\x3b\x24\x25", 5) == 0) { 1013 memcpy(loc, "\xf9\x0f\x1f\x84\x00\x00\x00\x00", 8); 1014 return true; 1015 } 1016 1017 // Adjust "lea X(%rsp),%rYY" to lea "(X - 0x4000)(%rsp),%rYY" where rYY could 1018 // be r10 or r11. The lea instruction feeds a subsequent compare which checks 1019 // if there is X available stack space. Making X larger effectively reserves 1020 // that much additional space. The stack grows downward so subtract the value. 1021 if (memcmp(loc, "\x4c\x8d\x94\x24", 4) == 0 || 1022 memcmp(loc, "\x4c\x8d\x9c\x24", 4) == 0) { 1023 // The offset bytes are encoded four bytes after the start of the 1024 // instruction. 1025 write32le(loc + 4, read32le(loc + 4) - 0x4000); 1026 return true; 1027 } 1028 return false; 1029 } 1030 1031 void X86_64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const { 1032 uint64_t secAddr = sec.getOutputSection()->addr; 1033 if (auto *s = dyn_cast<InputSection>(&sec)) 1034 secAddr += s->outSecOff; 1035 else if (auto *ehIn = dyn_cast<EhInputSection>(&sec)) 1036 secAddr += ehIn->getParent()->outSecOff; 1037 for (const Relocation &rel : sec.relocs()) { 1038 if (rel.expr == R_NONE) // See deleteFallThruJmpInsn 1039 continue; 1040 uint8_t *loc = buf + rel.offset; 1041 const uint64_t val = 1042 sec.getRelocTargetVA(sec.file, rel.type, rel.addend, 1043 secAddr + rel.offset, *rel.sym, rel.expr); 1044 relocate(loc, rel, val); 1045 } 1046 if (sec.jumpInstrMod) { 1047 applyJumpInstrMod(buf + sec.jumpInstrMod->offset, 1048 sec.jumpInstrMod->original, sec.jumpInstrMod->size); 1049 } 1050 } 1051 1052 // If Intel Indirect Branch Tracking is enabled, we have to emit special PLT 1053 // entries containing endbr64 instructions. A PLT entry will be split into two 1054 // parts, one in .plt.sec (writePlt), and the other in .plt (writeIBTPlt). 1055 namespace { 1056 class IntelIBT : public X86_64 { 1057 public: 1058 IntelIBT(); 1059 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 1060 void writePlt(uint8_t *buf, const Symbol &sym, 1061 uint64_t pltEntryAddr) const override; 1062 void writeIBTPlt(uint8_t *buf, size_t numEntries) const override; 1063 1064 static const unsigned IBTPltHeaderSize = 16; 1065 }; 1066 } // namespace 1067 1068 IntelIBT::IntelIBT() { pltHeaderSize = 0; } 1069 1070 void IntelIBT::writeGotPlt(uint8_t *buf, const Symbol &s) const { 1071 uint64_t va = 1072 in.ibtPlt->getVA() + IBTPltHeaderSize + s.getPltIdx() * pltEntrySize; 1073 write64le(buf, va); 1074 } 1075 1076 void IntelIBT::writePlt(uint8_t *buf, const Symbol &sym, 1077 uint64_t pltEntryAddr) const { 1078 const uint8_t Inst[] = { 1079 0xf3, 0x0f, 0x1e, 0xfa, // endbr64 1080 0xff, 0x25, 0, 0, 0, 0, // jmpq *got(%rip) 1081 0x66, 0x0f, 0x1f, 0x44, 0, 0, // nop 1082 }; 1083 memcpy(buf, Inst, sizeof(Inst)); 1084 write32le(buf + 6, sym.getGotPltVA() - pltEntryAddr - 10); 1085 } 1086 1087 void IntelIBT::writeIBTPlt(uint8_t *buf, size_t numEntries) const { 1088 writePltHeader(buf); 1089 buf += IBTPltHeaderSize; 1090 1091 const uint8_t inst[] = { 1092 0xf3, 0x0f, 0x1e, 0xfa, // endbr64 1093 0x68, 0, 0, 0, 0, // pushq <relocation index> 1094 0xe9, 0, 0, 0, 0, // jmpq plt[0] 1095 0x66, 0x90, // nop 1096 }; 1097 1098 for (size_t i = 0; i < numEntries; ++i) { 1099 memcpy(buf, inst, sizeof(inst)); 1100 write32le(buf + 5, i); 1101 write32le(buf + 10, -pltHeaderSize - sizeof(inst) * i - 30); 1102 buf += sizeof(inst); 1103 } 1104 } 1105 1106 // These nonstandard PLT entries are to migtigate Spectre v2 security 1107 // vulnerability. In order to mitigate Spectre v2, we want to avoid indirect 1108 // branch instructions such as `jmp *GOTPLT(%rip)`. So, in the following PLT 1109 // entries, we use a CALL followed by MOV and RET to do the same thing as an 1110 // indirect jump. That instruction sequence is so-called "retpoline". 1111 // 1112 // We have two types of retpoline PLTs as a size optimization. If `-z now` 1113 // is specified, all dynamic symbols are resolved at load-time. Thus, when 1114 // that option is given, we can omit code for symbol lazy resolution. 1115 namespace { 1116 class Retpoline : public X86_64 { 1117 public: 1118 Retpoline(); 1119 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 1120 void writePltHeader(uint8_t *buf) const override; 1121 void writePlt(uint8_t *buf, const Symbol &sym, 1122 uint64_t pltEntryAddr) const override; 1123 }; 1124 1125 class RetpolineZNow : public X86_64 { 1126 public: 1127 RetpolineZNow(); 1128 void writeGotPlt(uint8_t *buf, const Symbol &s) const override {} 1129 void writePltHeader(uint8_t *buf) const override; 1130 void writePlt(uint8_t *buf, const Symbol &sym, 1131 uint64_t pltEntryAddr) const override; 1132 }; 1133 } // namespace 1134 1135 Retpoline::Retpoline() { 1136 pltHeaderSize = 48; 1137 pltEntrySize = 32; 1138 ipltEntrySize = 32; 1139 } 1140 1141 void Retpoline::writeGotPlt(uint8_t *buf, const Symbol &s) const { 1142 write64le(buf, s.getPltVA() + 17); 1143 } 1144 1145 void Retpoline::writePltHeader(uint8_t *buf) const { 1146 const uint8_t insn[] = { 1147 0xff, 0x35, 0, 0, 0, 0, // 0: pushq GOTPLT+8(%rip) 1148 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 6: mov GOTPLT+16(%rip), %r11 1149 0xe8, 0x0e, 0x00, 0x00, 0x00, // d: callq next 1150 0xf3, 0x90, // 12: loop: pause 1151 0x0f, 0xae, 0xe8, // 14: lfence 1152 0xeb, 0xf9, // 17: jmp loop 1153 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 19: int3; .align 16 1154 0x4c, 0x89, 0x1c, 0x24, // 20: next: mov %r11, (%rsp) 1155 0xc3, // 24: ret 1156 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 25: int3; padding 1157 0xcc, 0xcc, 0xcc, 0xcc, // 2c: int3; padding 1158 }; 1159 memcpy(buf, insn, sizeof(insn)); 1160 1161 uint64_t gotPlt = in.gotPlt->getVA(); 1162 uint64_t plt = in.plt->getVA(); 1163 write32le(buf + 2, gotPlt - plt - 6 + 8); 1164 write32le(buf + 9, gotPlt - plt - 13 + 16); 1165 } 1166 1167 void Retpoline::writePlt(uint8_t *buf, const Symbol &sym, 1168 uint64_t pltEntryAddr) const { 1169 const uint8_t insn[] = { 1170 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 0: mov foo@GOTPLT(%rip), %r11 1171 0xe8, 0, 0, 0, 0, // 7: callq plt+0x20 1172 0xe9, 0, 0, 0, 0, // c: jmp plt+0x12 1173 0x68, 0, 0, 0, 0, // 11: pushq <relocation index> 1174 0xe9, 0, 0, 0, 0, // 16: jmp plt+0 1175 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1b: int3; padding 1176 }; 1177 memcpy(buf, insn, sizeof(insn)); 1178 1179 uint64_t off = pltEntryAddr - in.plt->getVA(); 1180 1181 write32le(buf + 3, sym.getGotPltVA() - pltEntryAddr - 7); 1182 write32le(buf + 8, -off - 12 + 32); 1183 write32le(buf + 13, -off - 17 + 18); 1184 write32le(buf + 18, sym.getPltIdx()); 1185 write32le(buf + 23, -off - 27); 1186 } 1187 1188 RetpolineZNow::RetpolineZNow() { 1189 pltHeaderSize = 32; 1190 pltEntrySize = 16; 1191 ipltEntrySize = 16; 1192 } 1193 1194 void RetpolineZNow::writePltHeader(uint8_t *buf) const { 1195 const uint8_t insn[] = { 1196 0xe8, 0x0b, 0x00, 0x00, 0x00, // 0: call next 1197 0xf3, 0x90, // 5: loop: pause 1198 0x0f, 0xae, 0xe8, // 7: lfence 1199 0xeb, 0xf9, // a: jmp loop 1200 0xcc, 0xcc, 0xcc, 0xcc, // c: int3; .align 16 1201 0x4c, 0x89, 0x1c, 0x24, // 10: next: mov %r11, (%rsp) 1202 0xc3, // 14: ret 1203 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 15: int3; padding 1204 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1a: int3; padding 1205 0xcc, // 1f: int3; padding 1206 }; 1207 memcpy(buf, insn, sizeof(insn)); 1208 } 1209 1210 void RetpolineZNow::writePlt(uint8_t *buf, const Symbol &sym, 1211 uint64_t pltEntryAddr) const { 1212 const uint8_t insn[] = { 1213 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // mov foo@GOTPLT(%rip), %r11 1214 0xe9, 0, 0, 0, 0, // jmp plt+0 1215 0xcc, 0xcc, 0xcc, 0xcc, // int3; padding 1216 }; 1217 memcpy(buf, insn, sizeof(insn)); 1218 1219 write32le(buf + 3, sym.getGotPltVA() - pltEntryAddr - 7); 1220 write32le(buf + 8, in.plt->getVA() - pltEntryAddr - 12); 1221 } 1222 1223 static TargetInfo *getTargetInfo() { 1224 if (config->zRetpolineplt) { 1225 if (config->zNow) { 1226 static RetpolineZNow t; 1227 return &t; 1228 } 1229 static Retpoline t; 1230 return &t; 1231 } 1232 1233 if (config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) { 1234 static IntelIBT t; 1235 return &t; 1236 } 1237 1238 static X86_64 t; 1239 return &t; 1240 } 1241 1242 TargetInfo *elf::getX86_64TargetInfo() { return getTargetInfo(); } 1243