Coverage Report

Created: 2019-07-24 05:18

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