Coverage Report

Created: 2019-01-21 03:01

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