Coverage Report

Created: 2018-01-17 17:22

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