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