Coverage Report

Created: 2018-10-23 15:26

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