Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 1 | //===- X86_64.cpp ---------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 10 | #include "InputFiles.h" |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 11 | #include "Symbols.h" |
| 12 | #include "SyntheticSections.h" |
| 13 | #include "Target.h" |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 14 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 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 { |
Chandler Carruth | c58f216 | 2018-01-22 22:05:25 +0000 | [diff] [blame] | 26 | template <class ELFT> class X86_64 : public TargetInfo { |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 27 | public: |
| 28 | X86_64(); |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 29 | RelExpr getRelExpr(RelType Type, const Symbol &S, |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 30 | const uint8_t *Loc) const override; |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 31 | bool isPicRel(RelType Type) const override; |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 32 | void writeGotPltHeader(uint8_t *Buf) const override; |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 33 | void writeGotPlt(uint8_t *Buf, const Symbol &S) const override; |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 34 | void writePltHeader(uint8_t *Buf) const override; |
| 35 | void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr, |
| 36 | int32_t Index, unsigned RelOff) const override; |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 37 | void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override; |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 38 | |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 39 | RelExpr adjustRelaxExpr(RelType Type, const uint8_t *Data, |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 40 | RelExpr Expr) const override; |
| 41 | void relaxGot(uint8_t *Loc, uint64_t Val) const override; |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 42 | void relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const override; |
| 43 | void relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override; |
| 44 | void relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override; |
| 45 | void relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override; |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | void relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op, |
| 49 | uint8_t ModRm) const; |
| 50 | }; |
| 51 | } // namespace |
| 52 | |
| 53 | template <class ELFT> X86_64<ELFT>::X86_64() { |
| 54 | CopyRel = R_X86_64_COPY; |
| 55 | GotRel = R_X86_64_GLOB_DAT; |
| 56 | PltRel = R_X86_64_JUMP_SLOT; |
| 57 | RelativeRel = R_X86_64_RELATIVE; |
| 58 | IRelativeRel = R_X86_64_IRELATIVE; |
| 59 | TlsGotRel = R_X86_64_TPOFF64; |
| 60 | TlsModuleIndexRel = R_X86_64_DTPMOD64; |
| 61 | TlsOffsetRel = R_X86_64_DTPOFF64; |
| 62 | GotEntrySize = 8; |
| 63 | GotPltEntrySize = 8; |
| 64 | PltEntrySize = 16; |
| 65 | PltHeaderSize = 16; |
| 66 | TlsGdRelaxSkip = 2; |
Rui Ueyama | 921d43f | 2017-06-26 19:45:53 +0000 | [diff] [blame] | 67 | TrapInstr = 0xcccccccc; // 0xcc = INT3 |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 68 | |
| 69 | // Align to the large page size (known as a superpage or huge page). |
| 70 | // FreeBSD automatically promotes large, superpage-aligned allocations. |
| 71 | DefaultImageBase = 0x200000; |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | template <class ELFT> |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 75 | RelExpr X86_64<ELFT>::getRelExpr(RelType Type, const Symbol &S, |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 76 | const uint8_t *Loc) const { |
| 77 | switch (Type) { |
| 78 | case R_X86_64_8: |
| 79 | case R_X86_64_16: |
| 80 | case R_X86_64_32: |
| 81 | case R_X86_64_32S: |
| 82 | case R_X86_64_64: |
| 83 | case R_X86_64_DTPOFF32: |
| 84 | case R_X86_64_DTPOFF64: |
| 85 | return R_ABS; |
| 86 | case R_X86_64_TPOFF32: |
| 87 | return R_TLS; |
| 88 | case R_X86_64_TLSLD: |
| 89 | return R_TLSLD_PC; |
| 90 | case R_X86_64_TLSGD: |
| 91 | return R_TLSGD_PC; |
| 92 | case R_X86_64_SIZE32: |
| 93 | case R_X86_64_SIZE64: |
| 94 | return R_SIZE; |
| 95 | case R_X86_64_PLT32: |
| 96 | return R_PLT_PC; |
| 97 | case R_X86_64_PC32: |
| 98 | case R_X86_64_PC64: |
| 99 | return R_PC; |
| 100 | case R_X86_64_GOT32: |
| 101 | case R_X86_64_GOT64: |
| 102 | return R_GOT_FROM_END; |
| 103 | case R_X86_64_GOTPCREL: |
| 104 | case R_X86_64_GOTPCRELX: |
| 105 | case R_X86_64_REX_GOTPCRELX: |
| 106 | case R_X86_64_GOTTPOFF: |
| 107 | return R_GOT_PC; |
| 108 | case R_X86_64_NONE: |
| 109 | return R_NONE; |
| 110 | default: |
Rui Ueyama | be85529 | 2017-10-12 03:14:06 +0000 | [diff] [blame] | 111 | return R_INVALID; |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | template <class ELFT> void X86_64<ELFT>::writeGotPltHeader(uint8_t *Buf) const { |
| 116 | // The first entry holds the value of _DYNAMIC. It is not clear why that is |
| 117 | // required, but it is documented in the psabi and the glibc dynamic linker |
| 118 | // seems to use it (note that this is relevant for linking ld.so, not any |
| 119 | // other program). |
| 120 | write64le(Buf, InX::Dynamic->getVA()); |
| 121 | } |
| 122 | |
| 123 | template <class ELFT> |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 124 | void X86_64<ELFT>::writeGotPlt(uint8_t *Buf, const Symbol &S) const { |
George Rimar | b2051f1 | 2017-08-31 10:14:10 +0000 | [diff] [blame] | 125 | // See comments in X86::writeGotPlt. |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 126 | write32le(Buf, S.getPltVA() + 6); |
| 127 | } |
| 128 | |
| 129 | template <class ELFT> void X86_64<ELFT>::writePltHeader(uint8_t *Buf) const { |
| 130 | const uint8_t PltData[] = { |
Rui Ueyama | 17a3077 | 2017-12-27 06:54:18 +0000 | [diff] [blame] | 131 | 0xff, 0x35, 0, 0, 0, 0, // pushq GOTPLT+8(%rip) |
| 132 | 0xff, 0x25, 0, 0, 0, 0, // jmp *GOTPLT+16(%rip) |
| 133 | 0x0f, 0x1f, 0x40, 0x00, // nop |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 134 | }; |
| 135 | memcpy(Buf, PltData, sizeof(PltData)); |
| 136 | uint64_t GotPlt = InX::GotPlt->getVA(); |
| 137 | uint64_t Plt = InX::Plt->getVA(); |
| 138 | write32le(Buf + 2, GotPlt - Plt + 2); // GOTPLT+8 |
| 139 | write32le(Buf + 8, GotPlt - Plt + 4); // GOTPLT+16 |
| 140 | } |
| 141 | |
| 142 | template <class ELFT> |
| 143 | void X86_64<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, |
| 144 | uint64_t PltEntryAddr, int32_t Index, |
| 145 | unsigned RelOff) const { |
| 146 | const uint8_t Inst[] = { |
Rui Ueyama | 17a3077 | 2017-12-27 06:54:18 +0000 | [diff] [blame] | 147 | 0xff, 0x25, 0, 0, 0, 0, // jmpq *got(%rip) |
| 148 | 0x68, 0, 0, 0, 0, // pushq <relocation index> |
| 149 | 0xe9, 0, 0, 0, 0, // jmpq plt[0] |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 150 | }; |
| 151 | memcpy(Buf, Inst, sizeof(Inst)); |
| 152 | |
| 153 | write32le(Buf + 2, GotPltEntryAddr - PltEntryAddr - 6); |
| 154 | write32le(Buf + 7, Index); |
Rafael Espindola | 74acdfa | 2018-03-14 17:41:34 +0000 | [diff] [blame] | 155 | write32le(Buf + 12, -getPltEntryOffset(Index) - 16); |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 158 | template <class ELFT> bool X86_64<ELFT>::isPicRel(RelType Type) const { |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 159 | return Type != R_X86_64_PC32 && Type != R_X86_64_32 && |
| 160 | Type != R_X86_64_TPOFF32; |
| 161 | } |
| 162 | |
| 163 | template <class ELFT> |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 164 | void X86_64<ELFT>::relaxTlsGdToLe(uint8_t *Loc, RelType Type, |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 165 | uint64_t Val) const { |
| 166 | // Convert |
| 167 | // .byte 0x66 |
| 168 | // leaq x@tlsgd(%rip), %rdi |
| 169 | // .word 0x6666 |
| 170 | // rex64 |
| 171 | // call __tls_get_addr@plt |
| 172 | // to |
| 173 | // mov %fs:0x0,%rax |
| 174 | // lea x@tpoff,%rax |
| 175 | const uint8_t Inst[] = { |
| 176 | 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax |
Rui Ueyama | 17a3077 | 2017-12-27 06:54:18 +0000 | [diff] [blame] | 177 | 0x48, 0x8d, 0x80, 0, 0, 0, 0, // lea x@tpoff,%rax |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 178 | }; |
| 179 | memcpy(Loc - 4, Inst, sizeof(Inst)); |
| 180 | |
| 181 | // The original code used a pc relative relocation and so we have to |
| 182 | // compensate for the -4 in had in the addend. |
| 183 | write32le(Loc + 8, Val + 4); |
| 184 | } |
| 185 | |
| 186 | template <class ELFT> |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 187 | void X86_64<ELFT>::relaxTlsGdToIe(uint8_t *Loc, RelType Type, |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 188 | uint64_t Val) const { |
| 189 | // Convert |
| 190 | // .byte 0x66 |
| 191 | // leaq x@tlsgd(%rip), %rdi |
| 192 | // .word 0x6666 |
| 193 | // rex64 |
| 194 | // call __tls_get_addr@plt |
| 195 | // to |
| 196 | // mov %fs:0x0,%rax |
| 197 | // addq x@tpoff,%rax |
| 198 | const uint8_t Inst[] = { |
| 199 | 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax |
Rui Ueyama | 17a3077 | 2017-12-27 06:54:18 +0000 | [diff] [blame] | 200 | 0x48, 0x03, 0x05, 0, 0, 0, 0, // addq x@tpoff,%rax |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 201 | }; |
| 202 | memcpy(Loc - 4, Inst, sizeof(Inst)); |
| 203 | |
| 204 | // Both code sequences are PC relatives, but since we are moving the constant |
| 205 | // forward by 8 bytes we have to subtract the value by 8. |
| 206 | write32le(Loc + 8, Val - 8); |
| 207 | } |
| 208 | |
| 209 | // In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to |
| 210 | // R_X86_64_TPOFF32 so that it does not use GOT. |
| 211 | template <class ELFT> |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 212 | void X86_64<ELFT>::relaxTlsIeToLe(uint8_t *Loc, RelType Type, |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 213 | uint64_t Val) const { |
| 214 | uint8_t *Inst = Loc - 3; |
| 215 | uint8_t Reg = Loc[-1] >> 3; |
| 216 | uint8_t *RegSlot = Loc - 1; |
| 217 | |
| 218 | // Note that ADD with RSP or R12 is converted to ADD instead of LEA |
| 219 | // because LEA with these registers needs 4 bytes to encode and thus |
| 220 | // wouldn't fit the space. |
| 221 | |
| 222 | if (memcmp(Inst, "\x48\x03\x25", 3) == 0) { |
| 223 | // "addq foo@gottpoff(%rip),%rsp" -> "addq $foo,%rsp" |
| 224 | memcpy(Inst, "\x48\x81\xc4", 3); |
| 225 | } else if (memcmp(Inst, "\x4c\x03\x25", 3) == 0) { |
| 226 | // "addq foo@gottpoff(%rip),%r12" -> "addq $foo,%r12" |
| 227 | memcpy(Inst, "\x49\x81\xc4", 3); |
| 228 | } else if (memcmp(Inst, "\x4c\x03", 2) == 0) { |
| 229 | // "addq foo@gottpoff(%rip),%r[8-15]" -> "leaq foo(%r[8-15]),%r[8-15]" |
| 230 | memcpy(Inst, "\x4d\x8d", 2); |
| 231 | *RegSlot = 0x80 | (Reg << 3) | Reg; |
| 232 | } else if (memcmp(Inst, "\x48\x03", 2) == 0) { |
| 233 | // "addq foo@gottpoff(%rip),%reg -> "leaq foo(%reg),%reg" |
| 234 | memcpy(Inst, "\x48\x8d", 2); |
| 235 | *RegSlot = 0x80 | (Reg << 3) | Reg; |
| 236 | } else if (memcmp(Inst, "\x4c\x8b", 2) == 0) { |
| 237 | // "movq foo@gottpoff(%rip),%r[8-15]" -> "movq $foo,%r[8-15]" |
| 238 | memcpy(Inst, "\x49\xc7", 2); |
| 239 | *RegSlot = 0xc0 | Reg; |
| 240 | } else if (memcmp(Inst, "\x48\x8b", 2) == 0) { |
| 241 | // "movq foo@gottpoff(%rip),%reg" -> "movq $foo,%reg" |
| 242 | memcpy(Inst, "\x48\xc7", 2); |
| 243 | *RegSlot = 0xc0 | Reg; |
| 244 | } else { |
| 245 | error(getErrorLocation(Loc - 3) + |
| 246 | "R_X86_64_GOTTPOFF must be used in MOVQ or ADDQ instructions only"); |
| 247 | } |
| 248 | |
| 249 | // The original code used a PC relative relocation. |
| 250 | // Need to compensate for the -4 it had in the addend. |
| 251 | write32le(Loc, Val + 4); |
| 252 | } |
| 253 | |
| 254 | template <class ELFT> |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 255 | void X86_64<ELFT>::relaxTlsLdToLe(uint8_t *Loc, RelType Type, |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 256 | uint64_t Val) const { |
| 257 | // Convert |
| 258 | // leaq bar@tlsld(%rip), %rdi |
| 259 | // callq __tls_get_addr@PLT |
| 260 | // leaq bar@dtpoff(%rax), %rcx |
| 261 | // to |
| 262 | // .word 0x6666 |
| 263 | // .byte 0x66 |
| 264 | // mov %fs:0,%rax |
| 265 | // leaq bar@tpoff(%rax), %rcx |
| 266 | if (Type == R_X86_64_DTPOFF64) { |
| 267 | write64le(Loc, Val); |
| 268 | return; |
| 269 | } |
| 270 | if (Type == R_X86_64_DTPOFF32) { |
| 271 | write32le(Loc, Val); |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | const uint8_t Inst[] = { |
Rui Ueyama | 17a3077 | 2017-12-27 06:54:18 +0000 | [diff] [blame] | 276 | 0x66, 0x66, // .word 0x6666 |
| 277 | 0x66, // .byte 0x66 |
| 278 | 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0,%rax |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 279 | }; |
| 280 | memcpy(Loc - 3, Inst, sizeof(Inst)); |
| 281 | } |
| 282 | |
| 283 | template <class ELFT> |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 284 | void X86_64<ELFT>::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const { |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 285 | switch (Type) { |
| 286 | case R_X86_64_8: |
| 287 | checkUInt<8>(Loc, Val, Type); |
| 288 | *Loc = Val; |
| 289 | break; |
| 290 | case R_X86_64_16: |
| 291 | checkUInt<16>(Loc, Val, Type); |
| 292 | write16le(Loc, Val); |
| 293 | break; |
| 294 | case R_X86_64_32: |
| 295 | checkUInt<32>(Loc, Val, Type); |
| 296 | write32le(Loc, Val); |
| 297 | break; |
| 298 | case R_X86_64_32S: |
| 299 | case R_X86_64_TPOFF32: |
| 300 | case R_X86_64_GOT32: |
| 301 | case R_X86_64_GOTPCREL: |
| 302 | case R_X86_64_GOTPCRELX: |
| 303 | case R_X86_64_REX_GOTPCRELX: |
| 304 | case R_X86_64_PC32: |
| 305 | case R_X86_64_GOTTPOFF: |
| 306 | case R_X86_64_PLT32: |
| 307 | case R_X86_64_TLSGD: |
| 308 | case R_X86_64_TLSLD: |
| 309 | case R_X86_64_DTPOFF32: |
| 310 | case R_X86_64_SIZE32: |
| 311 | checkInt<32>(Loc, Val, Type); |
| 312 | write32le(Loc, Val); |
| 313 | break; |
| 314 | case R_X86_64_64: |
| 315 | case R_X86_64_DTPOFF64: |
| 316 | case R_X86_64_GLOB_DAT: |
| 317 | case R_X86_64_PC64: |
| 318 | case R_X86_64_SIZE64: |
| 319 | case R_X86_64_GOT64: |
| 320 | write64le(Loc, Val); |
| 321 | break; |
| 322 | default: |
Rui Ueyama | be85529 | 2017-10-12 03:14:06 +0000 | [diff] [blame] | 323 | error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type)); |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | template <class ELFT> |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 328 | RelExpr X86_64<ELFT>::adjustRelaxExpr(RelType Type, const uint8_t *Data, |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 329 | RelExpr RelExpr) const { |
| 330 | if (Type != R_X86_64_GOTPCRELX && Type != R_X86_64_REX_GOTPCRELX) |
| 331 | return RelExpr; |
| 332 | const uint8_t Op = Data[-2]; |
| 333 | const uint8_t ModRm = Data[-1]; |
| 334 | |
| 335 | // FIXME: When PIC is disabled and foo is defined locally in the |
| 336 | // lower 32 bit address space, memory operand in mov can be converted into |
| 337 | // immediate operand. Otherwise, mov must be changed to lea. We support only |
| 338 | // latter relaxation at this moment. |
| 339 | if (Op == 0x8b) |
| 340 | return R_RELAX_GOT_PC; |
| 341 | |
| 342 | // Relax call and jmp. |
| 343 | if (Op == 0xff && (ModRm == 0x15 || ModRm == 0x25)) |
| 344 | return R_RELAX_GOT_PC; |
| 345 | |
| 346 | // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor. |
| 347 | // If PIC then no relaxation is available. |
| 348 | // We also don't relax test/binop instructions without REX byte, |
| 349 | // they are 32bit operations and not common to have. |
| 350 | assert(Type == R_X86_64_REX_GOTPCRELX); |
| 351 | return Config->Pic ? RelExpr : R_RELAX_GOT_PC_NOPIC; |
| 352 | } |
| 353 | |
| 354 | // A subset of relaxations can only be applied for no-PIC. This method |
| 355 | // handles such relaxations. Instructions encoding information was taken from: |
| 356 | // "Intel 64 and IA-32 Architectures Software Developer's Manual V2" |
| 357 | // (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/ |
| 358 | // 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf) |
| 359 | template <class ELFT> |
| 360 | void X86_64<ELFT>::relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op, |
| 361 | uint8_t ModRm) const { |
| 362 | const uint8_t Rex = Loc[-3]; |
| 363 | // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg". |
| 364 | if (Op == 0x85) { |
| 365 | // See "TEST-Logical Compare" (4-428 Vol. 2B), |
| 366 | // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension). |
| 367 | |
| 368 | // ModR/M byte has form XX YYY ZZZ, where |
| 369 | // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1). |
| 370 | // XX has different meanings: |
| 371 | // 00: The operand's memory address is in reg1. |
| 372 | // 01: The operand's memory address is reg1 + a byte-sized displacement. |
| 373 | // 10: The operand's memory address is reg1 + a word-sized displacement. |
| 374 | // 11: The operand is reg1 itself. |
| 375 | // If an instruction requires only one operand, the unused reg2 field |
| 376 | // holds extra opcode bits rather than a register code |
| 377 | // 0xC0 == 11 000 000 binary. |
| 378 | // 0x38 == 00 111 000 binary. |
| 379 | // We transfer reg2 to reg1 here as operand. |
| 380 | // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3). |
| 381 | Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3; // ModR/M byte. |
| 382 | |
| 383 | // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32 |
| 384 | // See "TEST-Logical Compare" (4-428 Vol. 2B). |
| 385 | Loc[-2] = 0xf7; |
| 386 | |
| 387 | // Move R bit to the B bit in REX byte. |
| 388 | // REX byte is encoded as 0100WRXB, where |
| 389 | // 0100 is 4bit fixed pattern. |
| 390 | // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the |
| 391 | // default operand size is used (which is 32-bit for most but not all |
| 392 | // instructions). |
| 393 | // REX.R This 1-bit value is an extension to the MODRM.reg field. |
| 394 | // REX.X This 1-bit value is an extension to the SIB.index field. |
| 395 | // REX.B This 1-bit value is an extension to the MODRM.rm field or the |
| 396 | // SIB.base field. |
| 397 | // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A). |
| 398 | Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2; |
| 399 | write32le(Loc, Val); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub |
| 404 | // or xor operations. |
| 405 | |
| 406 | // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg". |
| 407 | // Logic is close to one for test instruction above, but we also |
| 408 | // write opcode extension here, see below for details. |
| 409 | Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3 | (Op & 0x3c); // ModR/M byte. |
| 410 | |
| 411 | // Primary opcode is 0x81, opcode extension is one of: |
| 412 | // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB, |
| 413 | // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP. |
| 414 | // This value was wrote to MODRM.reg in a line above. |
| 415 | // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15), |
| 416 | // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for |
| 417 | // descriptions about each operation. |
| 418 | Loc[-2] = 0x81; |
| 419 | Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2; |
| 420 | write32le(Loc, Val); |
| 421 | } |
| 422 | |
| 423 | template <class ELFT> |
| 424 | void X86_64<ELFT>::relaxGot(uint8_t *Loc, uint64_t Val) const { |
| 425 | const uint8_t Op = Loc[-2]; |
| 426 | const uint8_t ModRm = Loc[-1]; |
| 427 | |
| 428 | // Convert "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg". |
| 429 | if (Op == 0x8b) { |
| 430 | Loc[-2] = 0x8d; |
| 431 | write32le(Loc, Val); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | if (Op != 0xff) { |
| 436 | // We are relaxing a rip relative to an absolute, so compensate |
| 437 | // for the old -4 addend. |
| 438 | assert(!Config->Pic); |
| 439 | relaxGotNoPic(Loc, Val + 4, Op, ModRm); |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | // Convert call/jmp instructions. |
| 444 | if (ModRm == 0x15) { |
| 445 | // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call foo". |
| 446 | // Instead we convert to "addr32 call foo" where addr32 is an instruction |
| 447 | // prefix. That makes result expression to be a single instruction. |
| 448 | Loc[-2] = 0x67; // addr32 prefix |
| 449 | Loc[-1] = 0xe8; // call |
| 450 | write32le(Loc, Val); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | // Convert "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop". |
| 455 | // jmp doesn't return, so it is fine to use nop here, it is just a stub. |
| 456 | assert(ModRm == 0x25); |
| 457 | Loc[-2] = 0xe9; // jmp |
| 458 | Loc[3] = 0x90; // nop |
| 459 | write32le(Loc - 1, Val + 1); |
| 460 | } |
| 461 | |
Chandler Carruth | c58f216 | 2018-01-22 22:05:25 +0000 | [diff] [blame] | 462 | namespace { |
| 463 | template <class ELFT> class Retpoline : public X86_64<ELFT> { |
| 464 | public: |
| 465 | Retpoline(); |
| 466 | void writeGotPlt(uint8_t *Buf, const Symbol &S) const override; |
| 467 | void writePltHeader(uint8_t *Buf) const override; |
| 468 | void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr, |
| 469 | int32_t Index, unsigned RelOff) const override; |
| 470 | }; |
| 471 | |
| 472 | template <class ELFT> class RetpolineZNow : public X86_64<ELFT> { |
| 473 | public: |
| 474 | RetpolineZNow(); |
| 475 | void writeGotPlt(uint8_t *Buf, const Symbol &S) const override {} |
| 476 | void writePltHeader(uint8_t *Buf) const override; |
| 477 | void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr, |
| 478 | int32_t Index, unsigned RelOff) const override; |
| 479 | }; |
| 480 | } // namespace |
| 481 | |
| 482 | template <class ELFT> Retpoline<ELFT>::Retpoline() { |
| 483 | TargetInfo::PltHeaderSize = 48; |
| 484 | TargetInfo::PltEntrySize = 32; |
Rui Ueyama | e145bc2 | 2017-06-16 20:15:03 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Chandler Carruth | c58f216 | 2018-01-22 22:05:25 +0000 | [diff] [blame] | 487 | template <class ELFT> |
| 488 | void Retpoline<ELFT>::writeGotPlt(uint8_t *Buf, const Symbol &S) const { |
| 489 | write32le(Buf, S.getPltVA() + 17); |
Rui Ueyama | e145bc2 | 2017-06-16 20:15:03 +0000 | [diff] [blame] | 490 | } |
Chandler Carruth | c58f216 | 2018-01-22 22:05:25 +0000 | [diff] [blame] | 491 | |
| 492 | template <class ELFT> void Retpoline<ELFT>::writePltHeader(uint8_t *Buf) const { |
| 493 | const uint8_t Insn[] = { |
| 494 | 0xff, 0x35, 0, 0, 0, 0, // 0: pushq GOTPLT+8(%rip) |
| 495 | 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 6: mov GOTPLT+16(%rip), %r11 |
| 496 | 0xe8, 0x0e, 0x00, 0x00, 0x00, // d: callq next |
| 497 | 0xf3, 0x90, // 12: loop: pause |
| 498 | 0x0f, 0xae, 0xe8, // 14: lfence |
| 499 | 0xeb, 0xf9, // 17: jmp loop |
| 500 | 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 19: int3; .align 16 |
| 501 | 0x4c, 0x89, 0x1c, 0x24, // 20: next: mov %r11, (%rsp) |
| 502 | 0xc3, // 24: ret |
| 503 | }; |
| 504 | memcpy(Buf, Insn, sizeof(Insn)); |
| 505 | |
| 506 | uint64_t GotPlt = InX::GotPlt->getVA(); |
| 507 | uint64_t Plt = InX::Plt->getVA(); |
| 508 | write32le(Buf + 2, GotPlt - Plt - 6 + 8); |
| 509 | write32le(Buf + 9, GotPlt - Plt - 13 + 16); |
| 510 | } |
| 511 | |
| 512 | template <class ELFT> |
| 513 | void Retpoline<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, |
| 514 | uint64_t PltEntryAddr, int32_t Index, |
| 515 | unsigned RelOff) const { |
| 516 | const uint8_t Insn[] = { |
| 517 | 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 0: mov foo@GOTPLT(%rip), %r11 |
| 518 | 0xe8, 0, 0, 0, 0, // 7: callq plt+0x20 |
| 519 | 0xe9, 0, 0, 0, 0, // c: jmp plt+0x12 |
| 520 | 0x68, 0, 0, 0, 0, // 11: pushq <relocation index> |
| 521 | 0xe9, 0, 0, 0, 0, // 16: jmp plt+0 |
| 522 | }; |
| 523 | memcpy(Buf, Insn, sizeof(Insn)); |
| 524 | |
Rafael Espindola | 74acdfa | 2018-03-14 17:41:34 +0000 | [diff] [blame] | 525 | uint64_t Off = TargetInfo::getPltEntryOffset(Index); |
Chandler Carruth | c58f216 | 2018-01-22 22:05:25 +0000 | [diff] [blame] | 526 | |
| 527 | write32le(Buf + 3, GotPltEntryAddr - PltEntryAddr - 7); |
| 528 | write32le(Buf + 8, -Off - 12 + 32); |
| 529 | write32le(Buf + 13, -Off - 17 + 18); |
| 530 | write32le(Buf + 18, Index); |
| 531 | write32le(Buf + 23, -Off - 27); |
| 532 | } |
| 533 | |
| 534 | template <class ELFT> RetpolineZNow<ELFT>::RetpolineZNow() { |
| 535 | TargetInfo::PltHeaderSize = 32; |
| 536 | TargetInfo::PltEntrySize = 16; |
| 537 | } |
| 538 | |
| 539 | template <class ELFT> |
| 540 | void RetpolineZNow<ELFT>::writePltHeader(uint8_t *Buf) const { |
| 541 | const uint8_t Insn[] = { |
| 542 | 0xe8, 0x0b, 0x00, 0x00, 0x00, // 0: call next |
| 543 | 0xf3, 0x90, // 5: loop: pause |
| 544 | 0x0f, 0xae, 0xe8, // 7: lfence |
| 545 | 0xeb, 0xf9, // a: jmp loop |
| 546 | 0xcc, 0xcc, 0xcc, 0xcc, // c: int3; .align 16 |
| 547 | 0x4c, 0x89, 0x1c, 0x24, // 10: next: mov %r11, (%rsp) |
| 548 | 0xc3, // 14: ret |
| 549 | }; |
| 550 | memcpy(Buf, Insn, sizeof(Insn)); |
| 551 | } |
| 552 | |
| 553 | template <class ELFT> |
| 554 | void RetpolineZNow<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, |
| 555 | uint64_t PltEntryAddr, int32_t Index, |
| 556 | unsigned RelOff) const { |
| 557 | const uint8_t Insn[] = { |
| 558 | 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // mov foo@GOTPLT(%rip), %r11 |
| 559 | 0xe9, 0, 0, 0, 0, // jmp plt+0 |
| 560 | }; |
| 561 | memcpy(Buf, Insn, sizeof(Insn)); |
| 562 | |
| 563 | write32le(Buf + 3, GotPltEntryAddr - PltEntryAddr - 7); |
Rafael Espindola | 74acdfa | 2018-03-14 17:41:34 +0000 | [diff] [blame] | 564 | write32le(Buf + 8, -TargetInfo::getPltEntryOffset(Index) - 12); |
Chandler Carruth | c58f216 | 2018-01-22 22:05:25 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | template <class ELFT> TargetInfo *getTargetInfo() { |
| 568 | if (Config->ZRetpolineplt) { |
| 569 | if (Config->ZNow) { |
| 570 | static RetpolineZNow<ELFT> T; |
| 571 | return &T; |
| 572 | } |
| 573 | static Retpoline<ELFT> T; |
| 574 | return &T; |
| 575 | } |
| 576 | |
| 577 | static X86_64<ELFT> T; |
| 578 | return &T; |
| 579 | } |
| 580 | |
| 581 | TargetInfo *elf::getX32TargetInfo() { return getTargetInfo<ELF32LE>(); } |
| 582 | TargetInfo *elf::getX86_64TargetInfo() { return getTargetInfo<ELF64LE>(); } |