Coverage Report

Created: 2017-09-21 03:39

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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 "Error.h"
11
#include "InputFiles.h"
12
#include "Symbols.h"
13
#include "SyntheticSections.h"
14
#include "Target.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(uint32_t Type, const SymbolBody &S, const InputFile &File,
28
                     const uint8_t *Loc) const override;
29
  int64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
30
  void writeGotPltHeader(uint8_t *Buf) const override;
31
  uint32_t getDynRel(uint32_t Type) const override;
32
  void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
33
  void writeIgotPlt(uint8_t *Buf, const SymbolBody &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, uint32_t Type, uint64_t Val) const override;
38
39
  RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
40
                          RelExpr Expr) const override;
41
  void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
42
  void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
43
  void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
44
  void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
45
};
46
} // namespace
47
48
101
X86::X86() {
49
101
  GotBaseSymOff = -1;
50
101
  CopyRel = R_386_COPY;
51
101
  GotRel = R_386_GLOB_DAT;
52
101
  PltRel = R_386_JUMP_SLOT;
53
101
  IRelativeRel = R_386_IRELATIVE;
54
101
  RelativeRel = R_386_RELATIVE;
55
101
  TlsGotRel = R_386_TLS_TPOFF;
56
101
  TlsModuleIndexRel = R_386_TLS_DTPMOD32;
57
101
  TlsOffsetRel = R_386_TLS_DTPOFF32;
58
101
  GotEntrySize = 4;
59
101
  GotPltEntrySize = 4;
60
101
  PltEntrySize = 16;
61
101
  PltHeaderSize = 16;
62
101
  TlsGdRelaxSkip = 2;
63
101
  TrapInstr = 0xcccccccc; // 0xcc = INT3
64
101
}
65
66
RelExpr X86::getRelExpr(uint32_t Type, const SymbolBody &S,
67
161
                        const InputFile &File, const uint8_t *Loc) const {
68
161
  switch (Type) {
69
37
  case R_386_8:
70
37
  case R_386_16:
71
37
  case R_386_32:
72
37
  case R_386_TLS_LDO_32:
73
37
    return R_ABS;
74
6
  case R_386_TLS_GD:
75
6
    return R_TLSGD;
76
4
  case R_386_TLS_LDM:
77
4
    return R_TLSLD;
78
17
  case R_386_PLT32:
79
17
    return R_PLT_PC;
80
28
  case R_386_PC8:
81
28
  case R_386_PC16:
82
28
  case R_386_PC32:
83
28
    return R_PC;
84
6
  case R_386_GOTPC:
85
6
    return R_GOTONLY_PC_FROM_END;
86
17
  case R_386_TLS_IE:
87
17
    return R_GOT;
88
26
  case R_386_GOT32:
89
26
  case R_386_GOT32X:
90
26
    // These relocations can be calculated in two different ways.
91
26
    // Usual calculation is G + A - GOT what means an offset in GOT table
92
26
    // (R_GOT_FROM_END). When instruction pointed by relocation has no base
93
26
    // register, then relocations can be used when PIC code is disabled. In that
94
26
    // case calculation is G + A, it resolves to an address of entry in GOT
95
26
    // (R_GOT) and not an offset.
96
26
    //
97
26
    // To check that instruction has no base register we scan ModR/M byte.
98
26
    // See "Table 2-2. 32-Bit Addressing Forms with the ModR/M Byte"
99
26
    // (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
100
26
    //  64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
101
26
    if ((Loc[-1] & 0xc7) != 0x5)
102
20
      return R_GOT_FROM_END;
103
6
    
if (6
Config->Pic6
)
104
3
      error(toString(&File) + ": relocation " + toString(Type) + " against '" +
105
3
            S.getName() +
106
3
            "' without base register can not be used when PIC enabled");
107
6
    return R_GOT;
108
6
  case R_386_TLS_GOTIE:
109
6
    return R_GOT_FROM_END;
110
4
  case R_386_GOTOFF:
111
4
    return R_GOTREL_FROM_END;
112
4
  case R_386_TLS_LE:
113
4
    return R_TLS;
114
4
  case R_386_TLS_LE_32:
115
4
    return R_NEG_TLS;
116
1
  case R_386_NONE:
117
1
    return R_NONE;
118
1
  default:
119
1
    error(toString(&File) + ": unknown relocation type: " + toString(Type));
120
1
    return R_HINT;
121
0
  }
122
0
}
123
124
RelExpr X86::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
125
4
                             RelExpr Expr) const {
126
4
  switch (Expr) {
127
0
  default:
128
0
    return Expr;
129
2
  case R_RELAX_TLS_GD_TO_IE:
130
2
    return R_RELAX_TLS_GD_TO_IE_END;
131
2
  case R_RELAX_TLS_GD_TO_LE:
132
2
    return R_RELAX_TLS_GD_TO_LE_NEG;
133
0
  }
134
0
}
135
136
8
void X86::writeGotPltHeader(uint8_t *Buf) const {
137
8
  write32le(Buf, InX::Dynamic->getVA());
138
8
}
139
140
12
void X86::writeGotPlt(uint8_t *Buf, const SymbolBody &S) const {
141
12
  // Entries in .got.plt initially points back to the corresponding
142
12
  // PLT entries with a fixed offset to skip the first instruction.
143
12
  write32le(Buf, S.getPltVA() + 6);
144
12
}
145
146
6
void X86::writeIgotPlt(uint8_t *Buf, const SymbolBody &S) const {
147
6
  // An x86 entry is the address of the ifunc resolver function.
148
6
  write32le(Buf, S.getVA());
149
6
}
150
151
20
uint32_t X86::getDynRel(uint32_t Type) const {
152
20
  if (Type == R_386_TLS_LE)
153
2
    return R_386_TLS_TPOFF;
154
18
  
if (18
Type == R_386_TLS_LE_3218
)
155
2
    return R_386_TLS_TPOFF32;
156
16
  return Type;
157
16
}
158
159
8
void X86::writePltHeader(uint8_t *Buf) const {
160
8
  if (
Config->Pic8
) {
161
4
    const uint8_t V[] = {
162
4
        0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl GOTPLT+4(%ebx)
163
4
        0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *GOTPLT+8(%ebx)
164
4
        0x90, 0x90, 0x90, 0x90              // nop
165
4
    };
166
4
    memcpy(Buf, V, sizeof(V));
167
4
168
4
    uint32_t Ebx = InX::Got->getVA() + InX::Got->getSize();
169
4
    uint32_t GotPlt = InX::GotPlt->getVA() - Ebx;
170
4
    write32le(Buf + 2, GotPlt + 4);
171
4
    write32le(Buf + 8, GotPlt + 8);
172
4
    return;
173
4
  }
174
4
175
4
  const uint8_t PltData[] = {
176
4
      0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOTPLT+4)
177
4
      0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOTPLT+8)
178
4
      0x90, 0x90, 0x90, 0x90              // nop
179
4
  };
180
4
  memcpy(Buf, PltData, sizeof(PltData));
181
4
  uint32_t GotPlt = InX::GotPlt->getVA();
182
4
  write32le(Buf + 2, GotPlt + 4);
183
4
  write32le(Buf + 8, GotPlt + 8);
184
4
}
185
186
void X86::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
187
                   uint64_t PltEntryAddr, int32_t Index,
188
18
                   unsigned RelOff) const {
189
18
  const uint8_t Inst[] = {
190
18
      0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
191
18
      0x68, 0x00, 0x00, 0x00, 0x00,       // pushl $reloc_offset
192
18
      0xe9, 0x00, 0x00, 0x00, 0x00        // jmp .PLT0@PC
193
18
  };
194
18
  memcpy(Buf, Inst, sizeof(Inst));
195
18
196
18
  if (
Config->Pic18
) {
197
6
    // jmp *foo@GOT(%ebx)
198
6
    uint32_t Ebx = InX::Got->getVA() + InX::Got->getSize();
199
6
    Buf[1] = 0xa3;
200
6
    write32le(Buf + 2, GotPltEntryAddr - Ebx);
201
18
  } else {
202
12
    // jmp *foo_in_GOT
203
12
    Buf[1] = 0x25;
204
12
    write32le(Buf + 2, GotPltEntryAddr);
205
12
  }
206
18
207
18
  write32le(Buf + 7, RelOff);
208
18
  write32le(Buf + 12, -Index * PltEntrySize - PltHeaderSize - 16);
209
18
}
210
211
163
int64_t X86::getImplicitAddend(const uint8_t *Buf, uint32_t Type) const {
212
163
  switch (Type) {
213
38
  default:
214
38
    return 0;
215
8
  case R_386_8:
216
8
  case R_386_PC8:
217
8
    return SignExtend64<8>(*Buf);
218
9
  case R_386_16:
219
9
  case R_386_PC16:
220
9
    return SignExtend64<16>(read16le(Buf));
221
108
  case R_386_32:
222
108
  case R_386_GOT32:
223
108
  case R_386_GOT32X:
224
108
  case R_386_GOTOFF:
225
108
  case R_386_GOTPC:
226
108
  case R_386_PC32:
227
108
  case R_386_PLT32:
228
108
  case R_386_TLS_LDO_32:
229
108
  case R_386_TLS_LE:
230
108
    return SignExtend64<32>(read32le(Buf));
231
0
  }
232
0
}
233
234
124
void X86::relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
235
124
  // R_386_{PC,}{8,16} are not part of the i386 psABI, but they are
236
124
  // being used for some 16-bit programs such as boot loaders, so
237
124
  // we want to support them.
238
124
  switch (Type) {
239
5
  case R_386_8:
240
5
    checkUInt<8>(Loc, Val, Type);
241
5
    *Loc = Val;
242
5
    break;
243
3
  case R_386_PC8:
244
3
    checkInt<8>(Loc, Val, Type);
245
3
    *Loc = Val;
246
3
    break;
247
4
  case R_386_16:
248
4
    checkUInt<16>(Loc, Val, Type);
249
4
    write16le(Loc, Val);
250
4
    break;
251
5
  case R_386_PC16:
252
5
    // R_386_PC16 is normally used with 16 bit code. In that situation
253
5
    // the PC is 16 bits, just like the addend. This means that it can
254
5
    // point from any 16 bit address to any other if the possibility
255
5
    // of wrapping is included.
256
5
    // The only restriction we have to check then is that the destination
257
5
    // address fits in 16 bits. That is impossible to do here. The problem is
258
5
    // that we are passed the final value, which already had the
259
5
    // current location subtracted from it.
260
5
    // We just check that Val fits in 17 bits. This misses some cases, but
261
5
    // should have no false positives.
262
5
    checkInt<17>(Loc, Val, Type);
263
5
    write16le(Loc, Val);
264
5
    break;
265
107
  default:
266
107
    checkInt<32>(Loc, Val, Type);
267
107
    write32le(Loc, Val);
268
124
  }
269
124
}
270
271
2
void X86::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
272
2
  // Convert
273
2
  //   leal x@tlsgd(, %ebx, 1),
274
2
  //   call __tls_get_addr@plt
275
2
  // to
276
2
  //   movl %gs:0,%eax
277
2
  //   subl $x@ntpoff,%eax
278
2
  const uint8_t Inst[] = {
279
2
      0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
280
2
      0x81, 0xe8, 0x00, 0x00, 0x00, 0x00  // subl 0(%ebx), %eax
281
2
  };
282
2
  memcpy(Loc - 3, Inst, sizeof(Inst));
283
2
  write32le(Loc + 5, Val);
284
2
}
285
286
2
void X86::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
287
2
  // Convert
288
2
  //   leal x@tlsgd(, %ebx, 1),
289
2
  //   call __tls_get_addr@plt
290
2
  // to
291
2
  //   movl %gs:0, %eax
292
2
  //   addl x@gotntpoff(%ebx), %eax
293
2
  const uint8_t Inst[] = {
294
2
      0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
295
2
      0x03, 0x83, 0x00, 0x00, 0x00, 0x00  // addl 0(%ebx), %eax
296
2
  };
297
2
  memcpy(Loc - 3, Inst, sizeof(Inst));
298
2
  write32le(Loc + 5, Val);
299
2
}
300
301
// In some conditions, relocations can be optimized to avoid using GOT.
302
// This function does that for Initial Exec to Local Exec case.
303
10
void X86::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
304
10
  // Ulrich's document section 6.2 says that @gotntpoff can
305
10
  // be used with MOVL or ADDL instructions.
306
10
  // @indntpoff is similar to @gotntpoff, but for use in
307
10
  // position dependent code.
308
10
  uint8_t Reg = (Loc[-1] >> 3) & 7;
309
10
310
10
  if (
Type == R_386_TLS_IE10
) {
311
6
    if (
Loc[-1] == 0xa16
) {
312
2
      // "movl foo@indntpoff,%eax" -> "movl $foo,%eax"
313
2
      // This case is different from the generic case below because
314
2
      // this is a 5 byte instruction while below is 6 bytes.
315
2
      Loc[-1] = 0xb8;
316
6
    } else 
if (4
Loc[-2] == 0x8b4
) {
317
2
      // "movl foo@indntpoff,%reg" -> "movl $foo,%reg"
318
2
      Loc[-2] = 0xc7;
319
2
      Loc[-1] = 0xc0 | Reg;
320
4
    } else {
321
2
      // "addl foo@indntpoff,%reg" -> "addl $foo,%reg"
322
2
      Loc[-2] = 0x81;
323
2
      Loc[-1] = 0xc0 | Reg;
324
2
    }
325
10
  } else {
326
4
    assert(Type == R_386_TLS_GOTIE);
327
4
    if (
Loc[-2] == 0x8b4
) {
328
2
      // "movl foo@gottpoff(%rip),%reg" -> "movl $foo,%reg"
329
2
      Loc[-2] = 0xc7;
330
2
      Loc[-1] = 0xc0 | Reg;
331
4
    } else {
332
2
      // "addl foo@gotntpoff(%rip),%reg" -> "leal foo(%reg),%reg"
333
2
      Loc[-2] = 0x8d;
334
2
      Loc[-1] = 0x80 | (Reg << 3) | Reg;
335
2
    }
336
4
  }
337
10
  write32le(Loc, Val);
338
10
}
339
340
4
void X86::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
341
4
  if (
Type == R_386_TLS_LDO_324
) {
342
2
    write32le(Loc, Val);
343
2
    return;
344
2
  }
345
2
346
2
  // Convert
347
2
  //   leal foo(%reg),%eax
348
2
  //   call ___tls_get_addr
349
2
  // to
350
2
  //   movl %gs:0,%eax
351
2
  //   nop
352
2
  //   leal 0(%esi,1),%esi
353
2
  const uint8_t Inst[] = {
354
2
      0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
355
2
      0x90,                               // nop
356
2
      0x8d, 0x74, 0x26, 0x00              // leal 0(%esi,1),%esi
357
2
  };
358
2
  memcpy(Loc - 2, Inst, sizeof(Inst));
359
2
}
360
361
101
TargetInfo *elf::getX86TargetInfo() {
362
101
  static X86 Target;
363
101
  return &Target;
364
101
}