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_TLSDESC: 728 return read64le(buf + 8); 729 case R_X86_64_JUMP_SLOT: 730 case R_X86_64_NONE: 731 // These relocations are defined as not having an implicit addend. 732 return 0; 733 default: 734 internalLinkerError(getErrorLocation(buf), 735 "cannot read addend for relocation " + toString(type)); 736 return 0; 737 } 738 } 739 740 void X86_64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 741 switch (rel.type) { 742 case R_X86_64_8: 743 checkIntUInt(loc, val, 8, rel); 744 *loc = val; 745 break; 746 case R_X86_64_PC8: 747 checkInt(loc, val, 8, rel); 748 *loc = val; 749 break; 750 case R_X86_64_16: 751 checkIntUInt(loc, val, 16, rel); 752 write16le(loc, val); 753 break; 754 case R_X86_64_PC16: 755 checkInt(loc, val, 16, rel); 756 write16le(loc, val); 757 break; 758 case R_X86_64_32: 759 checkUInt(loc, val, 32, rel); 760 write32le(loc, val); 761 break; 762 case R_X86_64_32S: 763 case R_X86_64_TPOFF32: 764 case R_X86_64_GOT32: 765 case R_X86_64_GOTPC32: 766 case R_X86_64_GOTPC32_TLSDESC: 767 case R_X86_64_GOTPCREL: 768 case R_X86_64_GOTPCRELX: 769 case R_X86_64_REX_GOTPCRELX: 770 case R_X86_64_PC32: 771 case R_X86_64_GOTTPOFF: 772 case R_X86_64_PLT32: 773 case R_X86_64_TLSGD: 774 case R_X86_64_TLSLD: 775 case R_X86_64_DTPOFF32: 776 case R_X86_64_SIZE32: 777 checkInt(loc, val, 32, rel); 778 write32le(loc, val); 779 break; 780 case R_X86_64_64: 781 case R_X86_64_DTPOFF64: 782 case R_X86_64_PC64: 783 case R_X86_64_SIZE64: 784 case R_X86_64_GOT64: 785 case R_X86_64_GOTOFF64: 786 case R_X86_64_GOTPC64: 787 case R_X86_64_PLTOFF64: 788 write64le(loc, val); 789 break; 790 case R_X86_64_TLSDESC: 791 // The addend is stored in the second 64-bit word. 792 write64le(loc + 8, val); 793 break; 794 default: 795 llvm_unreachable("unknown relocation"); 796 } 797 } 798 799 RelExpr X86_64::adjustGotPcExpr(RelType type, int64_t addend, 800 const uint8_t *loc) const { 801 // Only R_X86_64_[REX_]GOTPCRELX can be relaxed. GNU as may emit GOTPCRELX 802 // with addend != -4. Such an instruction does not load the full GOT entry, so 803 // we cannot relax the relocation. E.g. movl x@GOTPCREL+4(%rip), %rax 804 // (addend=0) loads the high 32 bits of the GOT entry. 805 if (!config->relax || addend != -4 || 806 (type != R_X86_64_GOTPCRELX && type != R_X86_64_REX_GOTPCRELX)) 807 return R_GOT_PC; 808 const uint8_t op = loc[-2]; 809 const uint8_t modRm = loc[-1]; 810 811 // FIXME: When PIC is disabled and foo is defined locally in the 812 // lower 32 bit address space, memory operand in mov can be converted into 813 // immediate operand. Otherwise, mov must be changed to lea. We support only 814 // latter relaxation at this moment. 815 if (op == 0x8b) 816 return R_RELAX_GOT_PC; 817 818 // Relax call and jmp. 819 if (op == 0xff && (modRm == 0x15 || modRm == 0x25)) 820 return R_RELAX_GOT_PC; 821 822 // We don't support test/binop instructions without a REX prefix. 823 if (type == R_X86_64_GOTPCRELX) 824 return R_GOT_PC; 825 826 // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor. 827 // If PIC then no relaxation is available. 828 return config->isPic ? R_GOT_PC : R_RELAX_GOT_PC_NOPIC; 829 } 830 831 // A subset of relaxations can only be applied for no-PIC. This method 832 // handles such relaxations. Instructions encoding information was taken from: 833 // "Intel 64 and IA-32 Architectures Software Developer's Manual V2" 834 // (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/ 835 // 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf) 836 static void relaxGotNoPic(uint8_t *loc, uint64_t val, uint8_t op, 837 uint8_t modRm) { 838 const uint8_t rex = loc[-3]; 839 // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg". 840 if (op == 0x85) { 841 // See "TEST-Logical Compare" (4-428 Vol. 2B), 842 // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension). 843 844 // ModR/M byte has form XX YYY ZZZ, where 845 // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1). 846 // XX has different meanings: 847 // 00: The operand's memory address is in reg1. 848 // 01: The operand's memory address is reg1 + a byte-sized displacement. 849 // 10: The operand's memory address is reg1 + a word-sized displacement. 850 // 11: The operand is reg1 itself. 851 // If an instruction requires only one operand, the unused reg2 field 852 // holds extra opcode bits rather than a register code 853 // 0xC0 == 11 000 000 binary. 854 // 0x38 == 00 111 000 binary. 855 // We transfer reg2 to reg1 here as operand. 856 // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3). 857 loc[-1] = 0xc0 | (modRm & 0x38) >> 3; // ModR/M byte. 858 859 // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32 860 // See "TEST-Logical Compare" (4-428 Vol. 2B). 861 loc[-2] = 0xf7; 862 863 // Move R bit to the B bit in REX byte. 864 // REX byte is encoded as 0100WRXB, where 865 // 0100 is 4bit fixed pattern. 866 // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the 867 // default operand size is used (which is 32-bit for most but not all 868 // instructions). 869 // REX.R This 1-bit value is an extension to the MODRM.reg field. 870 // REX.X This 1-bit value is an extension to the SIB.index field. 871 // REX.B This 1-bit value is an extension to the MODRM.rm field or the 872 // SIB.base field. 873 // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A). 874 loc[-3] = (rex & ~0x4) | (rex & 0x4) >> 2; 875 write32le(loc, val); 876 return; 877 } 878 879 // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub 880 // or xor operations. 881 882 // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg". 883 // Logic is close to one for test instruction above, but we also 884 // write opcode extension here, see below for details. 885 loc[-1] = 0xc0 | (modRm & 0x38) >> 3 | (op & 0x3c); // ModR/M byte. 886 887 // Primary opcode is 0x81, opcode extension is one of: 888 // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB, 889 // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP. 890 // This value was wrote to MODRM.reg in a line above. 891 // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15), 892 // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for 893 // descriptions about each operation. 894 loc[-2] = 0x81; 895 loc[-3] = (rex & ~0x4) | (rex & 0x4) >> 2; 896 write32le(loc, val); 897 } 898 899 void X86_64::relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) const { 900 checkInt(loc, val, 32, rel); 901 const uint8_t op = loc[-2]; 902 const uint8_t modRm = loc[-1]; 903 904 // Convert "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg". 905 if (op == 0x8b) { 906 loc[-2] = 0x8d; 907 write32le(loc, val); 908 return; 909 } 910 911 if (op != 0xff) { 912 // We are relaxing a rip relative to an absolute, so compensate 913 // for the old -4 addend. 914 assert(!config->isPic); 915 relaxGotNoPic(loc, val + 4, op, modRm); 916 return; 917 } 918 919 // Convert call/jmp instructions. 920 if (modRm == 0x15) { 921 // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call foo". 922 // Instead we convert to "addr32 call foo" where addr32 is an instruction 923 // prefix. That makes result expression to be a single instruction. 924 loc[-2] = 0x67; // addr32 prefix 925 loc[-1] = 0xe8; // call 926 write32le(loc, val); 927 return; 928 } 929 930 // Convert "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop". 931 // jmp doesn't return, so it is fine to use nop here, it is just a stub. 932 assert(modRm == 0x25); 933 loc[-2] = 0xe9; // jmp 934 loc[3] = 0x90; // nop 935 write32le(loc - 1, val + 1); 936 } 937 938 // A split-stack prologue starts by checking the amount of stack remaining 939 // in one of two ways: 940 // A) Comparing of the stack pointer to a field in the tcb. 941 // B) Or a load of a stack pointer offset with an lea to r10 or r11. 942 bool X86_64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end, 943 uint8_t stOther) const { 944 if (!config->is64) { 945 error("Target doesn't support split stacks."); 946 return false; 947 } 948 949 if (loc + 8 >= end) 950 return false; 951 952 // Replace "cmp %fs:0x70,%rsp" and subsequent branch 953 // with "stc, nopl 0x0(%rax,%rax,1)" 954 if (memcmp(loc, "\x64\x48\x3b\x24\x25", 5) == 0) { 955 memcpy(loc, "\xf9\x0f\x1f\x84\x00\x00\x00\x00", 8); 956 return true; 957 } 958 959 // Adjust "lea X(%rsp),%rYY" to lea "(X - 0x4000)(%rsp),%rYY" where rYY could 960 // be r10 or r11. The lea instruction feeds a subsequent compare which checks 961 // if there is X available stack space. Making X larger effectively reserves 962 // that much additional space. The stack grows downward so subtract the value. 963 if (memcmp(loc, "\x4c\x8d\x94\x24", 4) == 0 || 964 memcmp(loc, "\x4c\x8d\x9c\x24", 4) == 0) { 965 // The offset bytes are encoded four bytes after the start of the 966 // instruction. 967 write32le(loc + 4, read32le(loc + 4) - 0x4000); 968 return true; 969 } 970 return false; 971 } 972 973 // If Intel Indirect Branch Tracking is enabled, we have to emit special PLT 974 // entries containing endbr64 instructions. A PLT entry will be split into two 975 // parts, one in .plt.sec (writePlt), and the other in .plt (writeIBTPlt). 976 namespace { 977 class IntelIBT : public X86_64 { 978 public: 979 IntelIBT(); 980 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 981 void writePlt(uint8_t *buf, const Symbol &sym, 982 uint64_t pltEntryAddr) const override; 983 void writeIBTPlt(uint8_t *buf, size_t numEntries) const override; 984 985 static const unsigned IBTPltHeaderSize = 16; 986 }; 987 } // namespace 988 989 IntelIBT::IntelIBT() { pltHeaderSize = 0; } 990 991 void IntelIBT::writeGotPlt(uint8_t *buf, const Symbol &s) const { 992 uint64_t va = 993 in.ibtPlt->getVA() + IBTPltHeaderSize + s.pltIndex * pltEntrySize; 994 write64le(buf, va); 995 } 996 997 void IntelIBT::writePlt(uint8_t *buf, const Symbol &sym, 998 uint64_t pltEntryAddr) const { 999 const uint8_t Inst[] = { 1000 0xf3, 0x0f, 0x1e, 0xfa, // endbr64 1001 0xff, 0x25, 0, 0, 0, 0, // jmpq *got(%rip) 1002 0x66, 0x0f, 0x1f, 0x44, 0, 0, // nop 1003 }; 1004 memcpy(buf, Inst, sizeof(Inst)); 1005 write32le(buf + 6, sym.getGotPltVA() - pltEntryAddr - 10); 1006 } 1007 1008 void IntelIBT::writeIBTPlt(uint8_t *buf, size_t numEntries) const { 1009 writePltHeader(buf); 1010 buf += IBTPltHeaderSize; 1011 1012 const uint8_t inst[] = { 1013 0xf3, 0x0f, 0x1e, 0xfa, // endbr64 1014 0x68, 0, 0, 0, 0, // pushq <relocation index> 1015 0xe9, 0, 0, 0, 0, // jmpq plt[0] 1016 0x66, 0x90, // nop 1017 }; 1018 1019 for (size_t i = 0; i < numEntries; ++i) { 1020 memcpy(buf, inst, sizeof(inst)); 1021 write32le(buf + 5, i); 1022 write32le(buf + 10, -pltHeaderSize - sizeof(inst) * i - 30); 1023 buf += sizeof(inst); 1024 } 1025 } 1026 1027 // These nonstandard PLT entries are to migtigate Spectre v2 security 1028 // vulnerability. In order to mitigate Spectre v2, we want to avoid indirect 1029 // branch instructions such as `jmp *GOTPLT(%rip)`. So, in the following PLT 1030 // entries, we use a CALL followed by MOV and RET to do the same thing as an 1031 // indirect jump. That instruction sequence is so-called "retpoline". 1032 // 1033 // We have two types of retpoline PLTs as a size optimization. If `-z now` 1034 // is specified, all dynamic symbols are resolved at load-time. Thus, when 1035 // that option is given, we can omit code for symbol lazy resolution. 1036 namespace { 1037 class Retpoline : public X86_64 { 1038 public: 1039 Retpoline(); 1040 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 1041 void writePltHeader(uint8_t *buf) const override; 1042 void writePlt(uint8_t *buf, const Symbol &sym, 1043 uint64_t pltEntryAddr) const override; 1044 }; 1045 1046 class RetpolineZNow : public X86_64 { 1047 public: 1048 RetpolineZNow(); 1049 void writeGotPlt(uint8_t *buf, const Symbol &s) const override {} 1050 void writePltHeader(uint8_t *buf) const override; 1051 void writePlt(uint8_t *buf, const Symbol &sym, 1052 uint64_t pltEntryAddr) const override; 1053 }; 1054 } // namespace 1055 1056 Retpoline::Retpoline() { 1057 pltHeaderSize = 48; 1058 pltEntrySize = 32; 1059 ipltEntrySize = 32; 1060 } 1061 1062 void Retpoline::writeGotPlt(uint8_t *buf, const Symbol &s) const { 1063 write64le(buf, s.getPltVA() + 17); 1064 } 1065 1066 void Retpoline::writePltHeader(uint8_t *buf) const { 1067 const uint8_t insn[] = { 1068 0xff, 0x35, 0, 0, 0, 0, // 0: pushq GOTPLT+8(%rip) 1069 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 6: mov GOTPLT+16(%rip), %r11 1070 0xe8, 0x0e, 0x00, 0x00, 0x00, // d: callq next 1071 0xf3, 0x90, // 12: loop: pause 1072 0x0f, 0xae, 0xe8, // 14: lfence 1073 0xeb, 0xf9, // 17: jmp loop 1074 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 19: int3; .align 16 1075 0x4c, 0x89, 0x1c, 0x24, // 20: next: mov %r11, (%rsp) 1076 0xc3, // 24: ret 1077 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 25: int3; padding 1078 0xcc, 0xcc, 0xcc, 0xcc, // 2c: int3; padding 1079 }; 1080 memcpy(buf, insn, sizeof(insn)); 1081 1082 uint64_t gotPlt = in.gotPlt->getVA(); 1083 uint64_t plt = in.plt->getVA(); 1084 write32le(buf + 2, gotPlt - plt - 6 + 8); 1085 write32le(buf + 9, gotPlt - plt - 13 + 16); 1086 } 1087 1088 void Retpoline::writePlt(uint8_t *buf, const Symbol &sym, 1089 uint64_t pltEntryAddr) const { 1090 const uint8_t insn[] = { 1091 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 0: mov foo@GOTPLT(%rip), %r11 1092 0xe8, 0, 0, 0, 0, // 7: callq plt+0x20 1093 0xe9, 0, 0, 0, 0, // c: jmp plt+0x12 1094 0x68, 0, 0, 0, 0, // 11: pushq <relocation index> 1095 0xe9, 0, 0, 0, 0, // 16: jmp plt+0 1096 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1b: int3; padding 1097 }; 1098 memcpy(buf, insn, sizeof(insn)); 1099 1100 uint64_t off = pltEntryAddr - in.plt->getVA(); 1101 1102 write32le(buf + 3, sym.getGotPltVA() - pltEntryAddr - 7); 1103 write32le(buf + 8, -off - 12 + 32); 1104 write32le(buf + 13, -off - 17 + 18); 1105 write32le(buf + 18, sym.pltIndex); 1106 write32le(buf + 23, -off - 27); 1107 } 1108 1109 RetpolineZNow::RetpolineZNow() { 1110 pltHeaderSize = 32; 1111 pltEntrySize = 16; 1112 ipltEntrySize = 16; 1113 } 1114 1115 void RetpolineZNow::writePltHeader(uint8_t *buf) const { 1116 const uint8_t insn[] = { 1117 0xe8, 0x0b, 0x00, 0x00, 0x00, // 0: call next 1118 0xf3, 0x90, // 5: loop: pause 1119 0x0f, 0xae, 0xe8, // 7: lfence 1120 0xeb, 0xf9, // a: jmp loop 1121 0xcc, 0xcc, 0xcc, 0xcc, // c: int3; .align 16 1122 0x4c, 0x89, 0x1c, 0x24, // 10: next: mov %r11, (%rsp) 1123 0xc3, // 14: ret 1124 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 15: int3; padding 1125 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1a: int3; padding 1126 0xcc, // 1f: int3; padding 1127 }; 1128 memcpy(buf, insn, sizeof(insn)); 1129 } 1130 1131 void RetpolineZNow::writePlt(uint8_t *buf, const Symbol &sym, 1132 uint64_t pltEntryAddr) const { 1133 const uint8_t insn[] = { 1134 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // mov foo@GOTPLT(%rip), %r11 1135 0xe9, 0, 0, 0, 0, // jmp plt+0 1136 0xcc, 0xcc, 0xcc, 0xcc, // int3; padding 1137 }; 1138 memcpy(buf, insn, sizeof(insn)); 1139 1140 write32le(buf + 3, sym.getGotPltVA() - pltEntryAddr - 7); 1141 write32le(buf + 8, in.plt->getVA() - pltEntryAddr - 12); 1142 } 1143 1144 static TargetInfo *getTargetInfo() { 1145 if (config->zRetpolineplt) { 1146 if (config->zNow) { 1147 static RetpolineZNow t; 1148 return &t; 1149 } 1150 static Retpoline t; 1151 return &t; 1152 } 1153 1154 if (config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) { 1155 static IntelIBT t; 1156 return &t; 1157 } 1158 1159 static X86_64 t; 1160 return &t; 1161 } 1162 1163 TargetInfo *elf::getX86_64TargetInfo() { return getTargetInfo(); } 1164