Coverage Report

Created: 2018-08-19 21:11

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/Arch/X86_64.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- X86_64.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/Object/ELF.h"
16
#include "llvm/Support/Endian.h"
17
18
using namespace llvm;
19
using namespace llvm::object;
20
using namespace llvm::support::endian;
21
using namespace llvm::ELF;
22
using namespace lld;
23
using namespace lld::elf;
24
25
namespace {
26
template <class ELFT> class X86_64 : public TargetInfo {
27
public:
28
  X86_64();
29
  RelExpr getRelExpr(RelType Type, const Symbol &S,
30
                     const uint8_t *Loc) const override;
31
  RelType getDynRel(RelType Type) const override;
32
  void writeGotPltHeader(uint8_t *Buf) const override;
33
  void writeGotPlt(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 relaxGot(uint8_t *Loc, uint64_t Val) const override;
42
  void relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
43
  void relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
44
  void relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
45
  void relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
46
  bool adjustPrologueForCrossSplitStack(uint8_t *Loc,
47
                                        uint8_t *End) const override;
48
49
private:
50
  void relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
51
                     uint8_t ModRm) const;
52
};
53
} // namespace
54
55
1.68k
template <class ELFT> X86_64<ELFT>::X86_64() {
56
1.68k
  CopyRel = R_X86_64_COPY;
57
1.68k
  GotRel = R_X86_64_GLOB_DAT;
58
1.68k
  PltRel = R_X86_64_JUMP_SLOT;
59
1.68k
  RelativeRel = R_X86_64_RELATIVE;
60
1.68k
  IRelativeRel = R_X86_64_IRELATIVE;
61
1.68k
  TlsGotRel = R_X86_64_TPOFF64;
62
1.68k
  TlsModuleIndexRel = R_X86_64_DTPMOD64;
63
1.68k
  TlsOffsetRel = R_X86_64_DTPOFF64;
64
1.68k
  GotEntrySize = 8;
65
1.68k
  GotPltEntrySize = 8;
66
1.68k
  PltEntrySize = 16;
67
1.68k
  PltHeaderSize = 16;
68
1.68k
  TlsGdRelaxSkip = 2;
69
1.68k
  TrapInstr = 0xcccccccc; // 0xcc = INT3
70
1.68k
71
1.68k
  // Align to the large page size (known as a superpage or huge page).
72
1.68k
  // FreeBSD automatically promotes large, superpage-aligned allocations.
73
1.68k
  DefaultImageBase = 0x200000;
74
1.68k
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::X86_64()
Line
Count
Source
55
3
template <class ELFT> X86_64<ELFT>::X86_64() {
56
3
  CopyRel = R_X86_64_COPY;
57
3
  GotRel = R_X86_64_GLOB_DAT;
58
3
  PltRel = R_X86_64_JUMP_SLOT;
59
3
  RelativeRel = R_X86_64_RELATIVE;
60
3
  IRelativeRel = R_X86_64_IRELATIVE;
61
3
  TlsGotRel = R_X86_64_TPOFF64;
62
3
  TlsModuleIndexRel = R_X86_64_DTPMOD64;
63
3
  TlsOffsetRel = R_X86_64_DTPOFF64;
64
3
  GotEntrySize = 8;
65
3
  GotPltEntrySize = 8;
66
3
  PltEntrySize = 16;
67
3
  PltHeaderSize = 16;
68
3
  TlsGdRelaxSkip = 2;
69
3
  TrapInstr = 0xcccccccc; // 0xcc = INT3
70
3
71
3
  // Align to the large page size (known as a superpage or huge page).
72
3
  // FreeBSD automatically promotes large, superpage-aligned allocations.
73
3
  DefaultImageBase = 0x200000;
74
3
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::X86_64()
Line
Count
Source
55
1.68k
template <class ELFT> X86_64<ELFT>::X86_64() {
56
1.68k
  CopyRel = R_X86_64_COPY;
57
1.68k
  GotRel = R_X86_64_GLOB_DAT;
58
1.68k
  PltRel = R_X86_64_JUMP_SLOT;
59
1.68k
  RelativeRel = R_X86_64_RELATIVE;
60
1.68k
  IRelativeRel = R_X86_64_IRELATIVE;
61
1.68k
  TlsGotRel = R_X86_64_TPOFF64;
62
1.68k
  TlsModuleIndexRel = R_X86_64_DTPMOD64;
63
1.68k
  TlsOffsetRel = R_X86_64_DTPOFF64;
64
1.68k
  GotEntrySize = 8;
65
1.68k
  GotPltEntrySize = 8;
66
1.68k
  PltEntrySize = 16;
67
1.68k
  PltHeaderSize = 16;
68
1.68k
  TlsGdRelaxSkip = 2;
69
1.68k
  TrapInstr = 0xcccccccc; // 0xcc = INT3
70
1.68k
71
1.68k
  // Align to the large page size (known as a superpage or huge page).
72
1.68k
  // FreeBSD automatically promotes large, superpage-aligned allocations.
73
1.68k
  DefaultImageBase = 0x200000;
74
1.68k
}
75
76
template <class ELFT>
77
RelExpr X86_64<ELFT>::getRelExpr(RelType Type, const Symbol &S,
78
1.29k
                                 const uint8_t *Loc) const {
79
1.29k
  switch (Type) {
80
1.29k
  case R_X86_64_8:
81
502
  case R_X86_64_16:
82
502
  case R_X86_64_32:
83
502
  case R_X86_64_32S:
84
502
  case R_X86_64_64:
85
502
  case R_X86_64_DTPOFF32:
86
502
  case R_X86_64_DTPOFF64:
87
502
    return R_ABS;
88
502
  case R_X86_64_TPOFF32:
89
7
    return R_TLS;
90
502
  case R_X86_64_TLSLD:
91
6
    return R_TLSLD_PC;
92
502
  case R_X86_64_TLSGD:
93
17
    return R_TLSGD_PC;
94
502
  case R_X86_64_SIZE32:
95
37
  case R_X86_64_SIZE64:
96
37
    return R_SIZE;
97
387
  case R_X86_64_PLT32:
98
387
    return R_PLT_PC;
99
191
  case R_X86_64_PC32:
100
191
  case R_X86_64_PC64:
101
191
    return R_PC;
102
191
  case R_X86_64_GOT32:
103
4
  case R_X86_64_GOT64:
104
4
    return R_GOT_FROM_END;
105
135
  case R_X86_64_GOTPCREL:
106
135
  case R_X86_64_GOTPCRELX:
107
135
  case R_X86_64_REX_GOTPCRELX:
108
135
  case R_X86_64_GOTTPOFF:
109
135
    return R_GOT_PC;
110
135
  case R_X86_64_GOTOFF64:
111
1
    return R_GOTREL_FROM_END;
112
135
  case R_X86_64_GOTPC32:
113
2
  case R_X86_64_GOTPC64:
114
2
    return R_GOTONLY_PC_FROM_END;
115
5
  case R_X86_64_NONE:
116
5
    return R_NONE;
117
4
  default:
118
4
    return R_INVALID;
119
1.29k
  }
120
1.29k
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getRelExpr(unsigned int, lld::elf::Symbol const&, unsigned char const*) const
Line
Count
Source
78
1
                                 const uint8_t *Loc) const {
79
1
  switch (Type) {
80
1
  case R_X86_64_8:
81
0
  case R_X86_64_16:
82
0
  case R_X86_64_32:
83
0
  case R_X86_64_32S:
84
0
  case R_X86_64_64:
85
0
  case R_X86_64_DTPOFF32:
86
0
  case R_X86_64_DTPOFF64:
87
0
    return R_ABS;
88
0
  case R_X86_64_TPOFF32:
89
0
    return R_TLS;
90
0
  case R_X86_64_TLSLD:
91
0
    return R_TLSLD_PC;
92
0
  case R_X86_64_TLSGD:
93
0
    return R_TLSGD_PC;
94
0
  case R_X86_64_SIZE32:
95
0
  case R_X86_64_SIZE64:
96
0
    return R_SIZE;
97
1
  case R_X86_64_PLT32:
98
1
    return R_PLT_PC;
99
0
  case R_X86_64_PC32:
100
0
  case R_X86_64_PC64:
101
0
    return R_PC;
102
0
  case R_X86_64_GOT32:
103
0
  case R_X86_64_GOT64:
104
0
    return R_GOT_FROM_END;
105
0
  case R_X86_64_GOTPCREL:
106
0
  case R_X86_64_GOTPCRELX:
107
0
  case R_X86_64_REX_GOTPCRELX:
108
0
  case R_X86_64_GOTTPOFF:
109
0
    return R_GOT_PC;
110
0
  case R_X86_64_GOTOFF64:
111
0
    return R_GOTREL_FROM_END;
112
0
  case R_X86_64_GOTPC32:
113
0
  case R_X86_64_GOTPC64:
114
0
    return R_GOTONLY_PC_FROM_END;
115
0
  case R_X86_64_NONE:
116
0
    return R_NONE;
117
0
  default:
118
0
    return R_INVALID;
119
1
  }
120
1
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getRelExpr(unsigned int, lld::elf::Symbol const&, unsigned char const*) const
Line
Count
Source
78
1.29k
                                 const uint8_t *Loc) const {
79
1.29k
  switch (Type) {
80
1.29k
  case R_X86_64_8:
81
502
  case R_X86_64_16:
82
502
  case R_X86_64_32:
83
502
  case R_X86_64_32S:
84
502
  case R_X86_64_64:
85
502
  case R_X86_64_DTPOFF32:
86
502
  case R_X86_64_DTPOFF64:
87
502
    return R_ABS;
88
502
  case R_X86_64_TPOFF32:
89
7
    return R_TLS;
90
502
  case R_X86_64_TLSLD:
91
6
    return R_TLSLD_PC;
92
502
  case R_X86_64_TLSGD:
93
17
    return R_TLSGD_PC;
94
502
  case R_X86_64_SIZE32:
95
37
  case R_X86_64_SIZE64:
96
37
    return R_SIZE;
97
386
  case R_X86_64_PLT32:
98
386
    return R_PLT_PC;
99
191
  case R_X86_64_PC32:
100
191
  case R_X86_64_PC64:
101
191
    return R_PC;
102
191
  case R_X86_64_GOT32:
103
4
  case R_X86_64_GOT64:
104
4
    return R_GOT_FROM_END;
105
135
  case R_X86_64_GOTPCREL:
106
135
  case R_X86_64_GOTPCRELX:
107
135
  case R_X86_64_REX_GOTPCRELX:
108
135
  case R_X86_64_GOTTPOFF:
109
135
    return R_GOT_PC;
110
135
  case R_X86_64_GOTOFF64:
111
1
    return R_GOTREL_FROM_END;
112
135
  case R_X86_64_GOTPC32:
113
2
  case R_X86_64_GOTPC64:
114
2
    return R_GOTONLY_PC_FROM_END;
115
5
  case R_X86_64_NONE:
116
5
    return R_NONE;
117
4
  default:
118
4
    return R_INVALID;
119
1.29k
  }
120
1.29k
}
121
122
149
template <class ELFT> void X86_64<ELFT>::writeGotPltHeader(uint8_t *Buf) const {
123
149
  // The first entry holds the value of _DYNAMIC. It is not clear why that is
124
149
  // required, but it is documented in the psabi and the glibc dynamic linker
125
149
  // seems to use it (note that this is relevant for linking ld.so, not any
126
149
  // other program).
127
149
  write64le(Buf, InX::Dynamic->getVA());
128
149
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeGotPltHeader(unsigned char*) const
Line
Count
Source
122
1
template <class ELFT> void X86_64<ELFT>::writeGotPltHeader(uint8_t *Buf) const {
123
1
  // The first entry holds the value of _DYNAMIC. It is not clear why that is
124
1
  // required, but it is documented in the psabi and the glibc dynamic linker
125
1
  // seems to use it (note that this is relevant for linking ld.so, not any
126
1
  // other program).
127
1
  write64le(Buf, InX::Dynamic->getVA());
128
1
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeGotPltHeader(unsigned char*) const
Line
Count
Source
122
148
template <class ELFT> void X86_64<ELFT>::writeGotPltHeader(uint8_t *Buf) const {
123
148
  // The first entry holds the value of _DYNAMIC. It is not clear why that is
124
148
  // required, but it is documented in the psabi and the glibc dynamic linker
125
148
  // seems to use it (note that this is relevant for linking ld.so, not any
126
148
  // other program).
127
148
  write64le(Buf, InX::Dynamic->getVA());
128
148
}
129
130
template <class ELFT>
131
168
void X86_64<ELFT>::writeGotPlt(uint8_t *Buf, const Symbol &S) const {
132
168
  // See comments in X86::writeGotPlt.
133
168
  write64le(Buf, S.getPltVA() + 6);
134
168
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeGotPlt(unsigned char*, lld::elf::Symbol const&) const
Line
Count
Source
131
1
void X86_64<ELFT>::writeGotPlt(uint8_t *Buf, const Symbol &S) const {
132
1
  // See comments in X86::writeGotPlt.
133
1
  write64le(Buf, S.getPltVA() + 6);
134
1
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeGotPlt(unsigned char*, lld::elf::Symbol const&) const
Line
Count
Source
131
167
void X86_64<ELFT>::writeGotPlt(uint8_t *Buf, const Symbol &S) const {
132
167
  // See comments in X86::writeGotPlt.
133
167
  write64le(Buf, S.getPltVA() + 6);
134
167
}
135
136
125
template <class ELFT> void X86_64<ELFT>::writePltHeader(uint8_t *Buf) const {
137
125
  const uint8_t PltData[] = {
138
125
      0xff, 0x35, 0, 0, 0, 0, // pushq GOTPLT+8(%rip)
139
125
      0xff, 0x25, 0, 0, 0, 0, // jmp *GOTPLT+16(%rip)
140
125
      0x0f, 0x1f, 0x40, 0x00, // nop
141
125
  };
142
125
  memcpy(Buf, PltData, sizeof(PltData));
143
125
  uint64_t GotPlt = InX::GotPlt->getVA();
144
125
  uint64_t Plt = InX::Plt->getVA();
145
125
  write32le(Buf + 2, GotPlt - Plt + 2); // GOTPLT+8
146
125
  write32le(Buf + 8, GotPlt - Plt + 4); // GOTPLT+16
147
125
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writePltHeader(unsigned char*) const
Line
Count
Source
136
1
template <class ELFT> void X86_64<ELFT>::writePltHeader(uint8_t *Buf) const {
137
1
  const uint8_t PltData[] = {
138
1
      0xff, 0x35, 0, 0, 0, 0, // pushq GOTPLT+8(%rip)
139
1
      0xff, 0x25, 0, 0, 0, 0, // jmp *GOTPLT+16(%rip)
140
1
      0x0f, 0x1f, 0x40, 0x00, // nop
141
1
  };
142
1
  memcpy(Buf, PltData, sizeof(PltData));
143
1
  uint64_t GotPlt = InX::GotPlt->getVA();
144
1
  uint64_t Plt = InX::Plt->getVA();
145
1
  write32le(Buf + 2, GotPlt - Plt + 2); // GOTPLT+8
146
1
  write32le(Buf + 8, GotPlt - Plt + 4); // GOTPLT+16
147
1
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writePltHeader(unsigned char*) const
Line
Count
Source
136
124
template <class ELFT> void X86_64<ELFT>::writePltHeader(uint8_t *Buf) const {
137
124
  const uint8_t PltData[] = {
138
124
      0xff, 0x35, 0, 0, 0, 0, // pushq GOTPLT+8(%rip)
139
124
      0xff, 0x25, 0, 0, 0, 0, // jmp *GOTPLT+16(%rip)
140
124
      0x0f, 0x1f, 0x40, 0x00, // nop
141
124
  };
142
124
  memcpy(Buf, PltData, sizeof(PltData));
143
124
  uint64_t GotPlt = InX::GotPlt->getVA();
144
124
  uint64_t Plt = InX::Plt->getVA();
145
124
  write32le(Buf + 2, GotPlt - Plt + 2); // GOTPLT+8
146
124
  write32le(Buf + 8, GotPlt - Plt + 4); // GOTPLT+16
147
124
}
148
149
template <class ELFT>
150
void X86_64<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
151
                            uint64_t PltEntryAddr, int32_t Index,
152
168
                            unsigned RelOff) const {
153
168
  const uint8_t Inst[] = {
154
168
      0xff, 0x25, 0, 0, 0, 0, // jmpq *got(%rip)
155
168
      0x68, 0, 0, 0, 0,       // pushq <relocation index>
156
168
      0xe9, 0, 0, 0, 0,       // jmpq plt[0]
157
168
  };
158
168
  memcpy(Buf, Inst, sizeof(Inst));
159
168
160
168
  write32le(Buf + 2, GotPltEntryAddr - PltEntryAddr - 6);
161
168
  write32le(Buf + 7, Index);
162
168
  write32le(Buf + 12, -getPltEntryOffset(Index) - 16);
163
168
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writePlt(unsigned char*, unsigned long long, unsigned long long, int, unsigned int) const
Line
Count
Source
152
1
                            unsigned RelOff) const {
153
1
  const uint8_t Inst[] = {
154
1
      0xff, 0x25, 0, 0, 0, 0, // jmpq *got(%rip)
155
1
      0x68, 0, 0, 0, 0,       // pushq <relocation index>
156
1
      0xe9, 0, 0, 0, 0,       // jmpq plt[0]
157
1
  };
158
1
  memcpy(Buf, Inst, sizeof(Inst));
159
1
160
1
  write32le(Buf + 2, GotPltEntryAddr - PltEntryAddr - 6);
161
1
  write32le(Buf + 7, Index);
162
1
  write32le(Buf + 12, -getPltEntryOffset(Index) - 16);
163
1
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writePlt(unsigned char*, unsigned long long, unsigned long long, int, unsigned int) const
Line
Count
Source
152
167
                            unsigned RelOff) const {
153
167
  const uint8_t Inst[] = {
154
167
      0xff, 0x25, 0, 0, 0, 0, // jmpq *got(%rip)
155
167
      0x68, 0, 0, 0, 0,       // pushq <relocation index>
156
167
      0xe9, 0, 0, 0, 0,       // jmpq plt[0]
157
167
  };
158
167
  memcpy(Buf, Inst, sizeof(Inst));
159
167
160
167
  write32le(Buf + 2, GotPltEntryAddr - PltEntryAddr - 6);
161
167
  write32le(Buf + 7, Index);
162
167
  write32le(Buf + 12, -getPltEntryOffset(Index) - 16);
163
167
}
164
165
104
template <class ELFT> RelType X86_64<ELFT>::getDynRel(RelType Type) const {
166
104
  if (Type == R_X86_64_64 || 
Type == R_X86_64_PC6429
||
Type == R_X86_64_SIZE3226
||
167
104
      
Type == R_X86_64_SIZE6420
)
168
90
    return Type;
169
14
  return R_X86_64_NONE;
170
14
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getDynRel(unsigned int) const
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getDynRel(unsigned int) const
Line
Count
Source
165
104
template <class ELFT> RelType X86_64<ELFT>::getDynRel(RelType Type) const {
166
104
  if (Type == R_X86_64_64 || 
Type == R_X86_64_PC6429
||
Type == R_X86_64_SIZE3226
||
167
104
      
Type == R_X86_64_SIZE6420
)
168
90
    return Type;
169
14
  return R_X86_64_NONE;
170
14
}
171
172
template <class ELFT>
173
void X86_64<ELFT>::relaxTlsGdToLe(uint8_t *Loc, RelType Type,
174
7
                                  uint64_t Val) const {
175
7
  // Convert
176
7
  //   .byte 0x66
177
7
  //   leaq x@tlsgd(%rip), %rdi
178
7
  //   .word 0x6666
179
7
  //   rex64
180
7
  //   call __tls_get_addr@plt
181
7
  // to
182
7
  //   mov %fs:0x0,%rax
183
7
  //   lea x@tpoff,%rax
184
7
  const uint8_t Inst[] = {
185
7
      0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
186
7
      0x48, 0x8d, 0x80, 0, 0, 0, 0,                         // lea x@tpoff,%rax
187
7
  };
188
7
  memcpy(Loc - 4, Inst, sizeof(Inst));
189
7
190
7
  // The original code used a pc relative relocation and so we have to
191
7
  // compensate for the -4 in had in the addend.
192
7
  write32le(Loc + 8, Val + 4);
193
7
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::relaxTlsGdToLe(unsigned char*, unsigned int, unsigned long long) const
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::relaxTlsGdToLe(unsigned char*, unsigned int, unsigned long long) const
Line
Count
Source
174
7
                                  uint64_t Val) const {
175
7
  // Convert
176
7
  //   .byte 0x66
177
7
  //   leaq x@tlsgd(%rip), %rdi
178
7
  //   .word 0x6666
179
7
  //   rex64
180
7
  //   call __tls_get_addr@plt
181
7
  // to
182
7
  //   mov %fs:0x0,%rax
183
7
  //   lea x@tpoff,%rax
184
7
  const uint8_t Inst[] = {
185
7
      0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
186
7
      0x48, 0x8d, 0x80, 0, 0, 0, 0,                         // lea x@tpoff,%rax
187
7
  };
188
7
  memcpy(Loc - 4, Inst, sizeof(Inst));
189
7
190
7
  // The original code used a pc relative relocation and so we have to
191
7
  // compensate for the -4 in had in the addend.
192
7
  write32le(Loc + 8, Val + 4);
193
7
}
194
195
template <class ELFT>
196
void X86_64<ELFT>::relaxTlsGdToIe(uint8_t *Loc, RelType Type,
197
4
                                  uint64_t Val) const {
198
4
  // Convert
199
4
  //   .byte 0x66
200
4
  //   leaq x@tlsgd(%rip), %rdi
201
4
  //   .word 0x6666
202
4
  //   rex64
203
4
  //   call __tls_get_addr@plt
204
4
  // to
205
4
  //   mov %fs:0x0,%rax
206
4
  //   addq x@tpoff,%rax
207
4
  const uint8_t Inst[] = {
208
4
      0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
209
4
      0x48, 0x03, 0x05, 0, 0, 0, 0,                         // addq x@tpoff,%rax
210
4
  };
211
4
  memcpy(Loc - 4, Inst, sizeof(Inst));
212
4
213
4
  // Both code sequences are PC relatives, but since we are moving the constant
214
4
  // forward by 8 bytes we have to subtract the value by 8.
215
4
  write32le(Loc + 8, Val - 8);
216
4
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::relaxTlsGdToIe(unsigned char*, unsigned int, unsigned long long) const
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::relaxTlsGdToIe(unsigned char*, unsigned int, unsigned long long) const
Line
Count
Source
197
4
                                  uint64_t Val) const {
198
4
  // Convert
199
4
  //   .byte 0x66
200
4
  //   leaq x@tlsgd(%rip), %rdi
201
4
  //   .word 0x6666
202
4
  //   rex64
203
4
  //   call __tls_get_addr@plt
204
4
  // to
205
4
  //   mov %fs:0x0,%rax
206
4
  //   addq x@tpoff,%rax
207
4
  const uint8_t Inst[] = {
208
4
      0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
209
4
      0x48, 0x03, 0x05, 0, 0, 0, 0,                         // addq x@tpoff,%rax
210
4
  };
211
4
  memcpy(Loc - 4, Inst, sizeof(Inst));
212
4
213
4
  // Both code sequences are PC relatives, but since we are moving the constant
214
4
  // forward by 8 bytes we have to subtract the value by 8.
215
4
  write32le(Loc + 8, Val - 8);
216
4
}
217
218
// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
219
// R_X86_64_TPOFF32 so that it does not use GOT.
220
template <class ELFT>
221
void X86_64<ELFT>::relaxTlsIeToLe(uint8_t *Loc, RelType Type,
222
31
                                  uint64_t Val) const {
223
31
  uint8_t *Inst = Loc - 3;
224
31
  uint8_t Reg = Loc[-1] >> 3;
225
31
  uint8_t *RegSlot = Loc - 1;
226
31
227
31
  // Note that ADD with RSP or R12 is converted to ADD instead of LEA
228
31
  // because LEA with these registers needs 4 bytes to encode and thus
229
31
  // wouldn't fit the space.
230
31
231
31
  if (memcmp(Inst, "\x48\x03\x25", 3) == 0) {
232
4
    // "addq foo@gottpoff(%rip),%rsp" -> "addq $foo,%rsp"
233
4
    memcpy(Inst, "\x48\x81\xc4", 3);
234
27
  } else if (memcmp(Inst, "\x4c\x03\x25", 3) == 0) {
235
4
    // "addq foo@gottpoff(%rip),%r12" -> "addq $foo,%r12"
236
4
    memcpy(Inst, "\x49\x81\xc4", 3);
237
23
  } else if (memcmp(Inst, "\x4c\x03", 2) == 0) {
238
4
    // "addq foo@gottpoff(%rip),%r[8-15]" -> "leaq foo(%r[8-15]),%r[8-15]"
239
4
    memcpy(Inst, "\x4d\x8d", 2);
240
4
    *RegSlot = 0x80 | (Reg << 3) | Reg;
241
19
  } else if (memcmp(Inst, "\x48\x03", 2) == 0) {
242
4
    // "addq foo@gottpoff(%rip),%reg -> "leaq foo(%reg),%reg"
243
4
    memcpy(Inst, "\x48\x8d", 2);
244
4
    *RegSlot = 0x80 | (Reg << 3) | Reg;
245
15
  } else if (memcmp(Inst, "\x4c\x8b", 2) == 0) {
246
4
    // "movq foo@gottpoff(%rip),%r[8-15]" -> "movq $foo,%r[8-15]"
247
4
    memcpy(Inst, "\x49\xc7", 2);
248
4
    *RegSlot = 0xc0 | Reg;
249
11
  } else if (memcmp(Inst, "\x48\x8b", 2) == 0) {
250
9
    // "movq foo@gottpoff(%rip),%reg" -> "movq $foo,%reg"
251
9
    memcpy(Inst, "\x48\xc7", 2);
252
9
    *RegSlot = 0xc0 | Reg;
253
9
  } else {
254
2
    error(getErrorLocation(Loc - 3) +
255
2
          "R_X86_64_GOTTPOFF must be used in MOVQ or ADDQ instructions only");
256
2
  }
257
31
258
31
  // The original code used a PC relative relocation.
259
31
  // Need to compensate for the -4 it had in the addend.
260
31
  write32le(Loc, Val + 4);
261
31
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::relaxTlsIeToLe(unsigned char*, unsigned int, unsigned long long) const
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::relaxTlsIeToLe(unsigned char*, unsigned int, unsigned long long) const
Line
Count
Source
222
31
                                  uint64_t Val) const {
223
31
  uint8_t *Inst = Loc - 3;
224
31
  uint8_t Reg = Loc[-1] >> 3;
225
31
  uint8_t *RegSlot = Loc - 1;
226
31
227
31
  // Note that ADD with RSP or R12 is converted to ADD instead of LEA
228
31
  // because LEA with these registers needs 4 bytes to encode and thus
229
31
  // wouldn't fit the space.
230
31
231
31
  if (memcmp(Inst, "\x48\x03\x25", 3) == 0) {
232
4
    // "addq foo@gottpoff(%rip),%rsp" -> "addq $foo,%rsp"
233
4
    memcpy(Inst, "\x48\x81\xc4", 3);
234
27
  } else if (memcmp(Inst, "\x4c\x03\x25", 3) == 0) {
235
4
    // "addq foo@gottpoff(%rip),%r12" -> "addq $foo,%r12"
236
4
    memcpy(Inst, "\x49\x81\xc4", 3);
237
23
  } else if (memcmp(Inst, "\x4c\x03", 2) == 0) {
238
4
    // "addq foo@gottpoff(%rip),%r[8-15]" -> "leaq foo(%r[8-15]),%r[8-15]"
239
4
    memcpy(Inst, "\x4d\x8d", 2);
240
4
    *RegSlot = 0x80 | (Reg << 3) | Reg;
241
19
  } else if (memcmp(Inst, "\x48\x03", 2) == 0) {
242
4
    // "addq foo@gottpoff(%rip),%reg -> "leaq foo(%reg),%reg"
243
4
    memcpy(Inst, "\x48\x8d", 2);
244
4
    *RegSlot = 0x80 | (Reg << 3) | Reg;
245
15
  } else if (memcmp(Inst, "\x4c\x8b", 2) == 0) {
246
4
    // "movq foo@gottpoff(%rip),%r[8-15]" -> "movq $foo,%r[8-15]"
247
4
    memcpy(Inst, "\x49\xc7", 2);
248
4
    *RegSlot = 0xc0 | Reg;
249
11
  } else if (memcmp(Inst, "\x48\x8b", 2) == 0) {
250
9
    // "movq foo@gottpoff(%rip),%reg" -> "movq $foo,%reg"
251
9
    memcpy(Inst, "\x48\xc7", 2);
252
9
    *RegSlot = 0xc0 | Reg;
253
9
  } else {
254
2
    error(getErrorLocation(Loc - 3) +
255
2
          "R_X86_64_GOTTPOFF must be used in MOVQ or ADDQ instructions only");
256
2
  }
257
31
258
31
  // The original code used a PC relative relocation.
259
31
  // Need to compensate for the -4 it had in the addend.
260
31
  write32le(Loc, Val + 4);
261
31
}
262
263
template <class ELFT>
264
void X86_64<ELFT>::relaxTlsLdToLe(uint8_t *Loc, RelType Type,
265
8
                                  uint64_t Val) const {
266
8
  // Convert
267
8
  //   leaq bar@tlsld(%rip), %rdi
268
8
  //   callq __tls_get_addr@PLT
269
8
  //   leaq bar@dtpoff(%rax), %rcx
270
8
  // to
271
8
  //   .word 0x6666
272
8
  //   .byte 0x66
273
8
  //   mov %fs:0,%rax
274
8
  //   leaq bar@tpoff(%rax), %rcx
275
8
  if (Type == R_X86_64_DTPOFF64) {
276
2
    write64le(Loc, Val);
277
2
    return;
278
2
  }
279
6
  if (Type == R_X86_64_DTPOFF32) {
280
3
    write32le(Loc, Val);
281
3
    return;
282
3
  }
283
3
284
3
  const uint8_t Inst[] = {
285
3
      0x66, 0x66,                                           // .word 0x6666
286
3
      0x66,                                                 // .byte 0x66
287
3
      0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0,%rax
288
3
  };
289
3
  memcpy(Loc - 3, Inst, sizeof(Inst));
290
3
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::relaxTlsLdToLe(unsigned char*, unsigned int, unsigned long long) const
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::relaxTlsLdToLe(unsigned char*, unsigned int, unsigned long long) const
Line
Count
Source
265
8
                                  uint64_t Val) const {
266
8
  // Convert
267
8
  //   leaq bar@tlsld(%rip), %rdi
268
8
  //   callq __tls_get_addr@PLT
269
8
  //   leaq bar@dtpoff(%rax), %rcx
270
8
  // to
271
8
  //   .word 0x6666
272
8
  //   .byte 0x66
273
8
  //   mov %fs:0,%rax
274
8
  //   leaq bar@tpoff(%rax), %rcx
275
8
  if (Type == R_X86_64_DTPOFF64) {
276
2
    write64le(Loc, Val);
277
2
    return;
278
2
  }
279
6
  if (Type == R_X86_64_DTPOFF32) {
280
3
    write32le(Loc, Val);
281
3
    return;
282
3
  }
283
3
284
3
  const uint8_t Inst[] = {
285
3
      0x66, 0x66,                                           // .word 0x6666
286
3
      0x66,                                                 // .byte 0x66
287
3
      0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0,%rax
288
3
  };
289
3
  memcpy(Loc - 3, Inst, sizeof(Inst));
290
3
}
291
292
template <class ELFT>
293
1.05k
void X86_64<ELFT>::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
294
1.05k
  switch (Type) {
295
1.05k
  case R_X86_64_8:
296
5
    checkUInt(Loc, Val, 8, Type);
297
5
    *Loc = Val;
298
5
    break;
299
1.05k
  case R_X86_64_16:
300
3
    checkUInt(Loc, Val, 16, Type);
301
3
    write16le(Loc, Val);
302
3
    break;
303
1.05k
  case R_X86_64_32:
304
119
    checkUInt(Loc, Val, 32, Type);
305
119
    write32le(Loc, Val);
306
119
    break;
307
1.05k
  case R_X86_64_32S:
308
712
  case R_X86_64_TPOFF32:
309
712
  case R_X86_64_GOT32:
310
712
  case R_X86_64_GOTPC32:
311
712
  case R_X86_64_GOTPCREL:
312
712
  case R_X86_64_GOTPCRELX:
313
712
  case R_X86_64_REX_GOTPCRELX:
314
712
  case R_X86_64_PC32:
315
712
  case R_X86_64_GOTTPOFF:
316
712
  case R_X86_64_PLT32:
317
712
  case R_X86_64_TLSGD:
318
712
  case R_X86_64_TLSLD:
319
712
  case R_X86_64_DTPOFF32:
320
712
  case R_X86_64_SIZE32:
321
712
    checkInt(Loc, Val, 32, Type);
322
712
    write32le(Loc, Val);
323
712
    break;
324
712
  case R_X86_64_64:
325
213
  case R_X86_64_DTPOFF64:
326
213
  case R_X86_64_GLOB_DAT:
327
213
  case R_X86_64_PC64:
328
213
  case R_X86_64_SIZE64:
329
213
  case R_X86_64_GOT64:
330
213
  case R_X86_64_GOTOFF64:
331
213
  case R_X86_64_GOTPC64:
332
213
    write64le(Loc, Val);
333
213
    break;
334
213
  default:
335
2
    error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
336
1.05k
  }
337
1.05k
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::relocateOne(unsigned char*, unsigned int, unsigned long long) const
Line
Count
Source
293
1
void X86_64<ELFT>::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
294
1
  switch (Type) {
295
1
  case R_X86_64_8:
296
0
    checkUInt(Loc, Val, 8, Type);
297
0
    *Loc = Val;
298
0
    break;
299
1
  case R_X86_64_16:
300
0
    checkUInt(Loc, Val, 16, Type);
301
0
    write16le(Loc, Val);
302
0
    break;
303
1
  case R_X86_64_32:
304
0
    checkUInt(Loc, Val, 32, Type);
305
0
    write32le(Loc, Val);
306
0
    break;
307
1
  case R_X86_64_32S:
308
1
  case R_X86_64_TPOFF32:
309
1
  case R_X86_64_GOT32:
310
1
  case R_X86_64_GOTPC32:
311
1
  case R_X86_64_GOTPCREL:
312
1
  case R_X86_64_GOTPCRELX:
313
1
  case R_X86_64_REX_GOTPCRELX:
314
1
  case R_X86_64_PC32:
315
1
  case R_X86_64_GOTTPOFF:
316
1
  case R_X86_64_PLT32:
317
1
  case R_X86_64_TLSGD:
318
1
  case R_X86_64_TLSLD:
319
1
  case R_X86_64_DTPOFF32:
320
1
  case R_X86_64_SIZE32:
321
1
    checkInt(Loc, Val, 32, Type);
322
1
    write32le(Loc, Val);
323
1
    break;
324
1
  case R_X86_64_64:
325
0
  case R_X86_64_DTPOFF64:
326
0
  case R_X86_64_GLOB_DAT:
327
0
  case R_X86_64_PC64:
328
0
  case R_X86_64_SIZE64:
329
0
  case R_X86_64_GOT64:
330
0
  case R_X86_64_GOTOFF64:
331
0
  case R_X86_64_GOTPC64:
332
0
    write64le(Loc, Val);
333
0
    break;
334
0
  default:
335
0
    error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
336
1
  }
337
1
}
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::relocateOne(unsigned char*, unsigned int, unsigned long long) const
Line
Count
Source
293
1.05k
void X86_64<ELFT>::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
294
1.05k
  switch (Type) {
295
1.05k
  case R_X86_64_8:
296
5
    checkUInt(Loc, Val, 8, Type);
297
5
    *Loc = Val;
298
5
    break;
299
1.05k
  case R_X86_64_16:
300
3
    checkUInt(Loc, Val, 16, Type);
301
3
    write16le(Loc, Val);
302
3
    break;
303
1.05k
  case R_X86_64_32:
304
119
    checkUInt(Loc, Val, 32, Type);
305
119
    write32le(Loc, Val);
306
119
    break;
307
1.05k
  case R_X86_64_32S:
308
711
  case R_X86_64_TPOFF32:
309
711
  case R_X86_64_GOT32:
310
711
  case R_X86_64_GOTPC32:
311
711
  case R_X86_64_GOTPCREL:
312
711
  case R_X86_64_GOTPCRELX:
313
711
  case R_X86_64_REX_GOTPCRELX:
314
711
  case R_X86_64_PC32:
315
711
  case R_X86_64_GOTTPOFF:
316
711
  case R_X86_64_PLT32:
317
711
  case R_X86_64_TLSGD:
318
711
  case R_X86_64_TLSLD:
319
711
  case R_X86_64_DTPOFF32:
320
711
  case R_X86_64_SIZE32:
321
711
    checkInt(Loc, Val, 32, Type);
322
711
    write32le(Loc, Val);
323
711
    break;
324
711
  case R_X86_64_64:
325
213
  case R_X86_64_DTPOFF64:
326
213
  case R_X86_64_GLOB_DAT:
327
213
  case R_X86_64_PC64:
328
213
  case R_X86_64_SIZE64:
329
213
  case R_X86_64_GOT64:
330
213
  case R_X86_64_GOTOFF64:
331
213
  case R_X86_64_GOTPC64:
332
213
    write64le(Loc, Val);
333
213
    break;
334
213
  default:
335
2
    error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
336
1.05k
  }
337
1.05k
}
338
339
template <class ELFT>
340
RelExpr X86_64<ELFT>::adjustRelaxExpr(RelType Type, const uint8_t *Data,
341
66
                                      RelExpr RelExpr) const {
342
66
  if (Type != R_X86_64_GOTPCRELX && 
Type != R_X86_64_REX_GOTPCRELX58
)
343
25
    return RelExpr;
344
41
  const uint8_t Op = Data[-2];
345
41
  const uint8_t ModRm = Data[-1];
346
41
347
41
  // FIXME: When PIC is disabled and foo is defined locally in the
348
41
  // lower 32 bit address space, memory operand in mov can be converted into
349
41
  // immediate operand. Otherwise, mov must be changed to lea. We support only
350
41
  // latter relaxation at this moment.
351
41
  if (Op == 0x8b)
352
15
    return R_RELAX_GOT_PC;
353
26
354
26
  // Relax call and jmp.
355
26
  if (Op == 0xff && 
(8
ModRm == 0x158
||
ModRm == 0x254
))
356
8
    return R_RELAX_GOT_PC;
357
18
358
18
  // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor.
359
18
  // If PIC then no relaxation is available.
360
18
  // We also don't relax test/binop instructions without REX byte,
361
18
  // they are 32bit operations and not common to have.
362
18
  assert(Type == R_X86_64_REX_GOTPCRELX);
363
18
  return Config->Pic ? 
RelExpr9
:
R_RELAX_GOT_PC_NOPIC9
;
364
18
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::adjustRelaxExpr(unsigned int, unsigned char const*, lld::elf::RelExpr) const
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::adjustRelaxExpr(unsigned int, unsigned char const*, lld::elf::RelExpr) const
Line
Count
Source
341
66
                                      RelExpr RelExpr) const {
342
66
  if (Type != R_X86_64_GOTPCRELX && 
Type != R_X86_64_REX_GOTPCRELX58
)
343
25
    return RelExpr;
344
41
  const uint8_t Op = Data[-2];
345
41
  const uint8_t ModRm = Data[-1];
346
41
347
41
  // FIXME: When PIC is disabled and foo is defined locally in the
348
41
  // lower 32 bit address space, memory operand in mov can be converted into
349
41
  // immediate operand. Otherwise, mov must be changed to lea. We support only
350
41
  // latter relaxation at this moment.
351
41
  if (Op == 0x8b)
352
15
    return R_RELAX_GOT_PC;
353
26
354
26
  // Relax call and jmp.
355
26
  if (Op == 0xff && 
(8
ModRm == 0x158
||
ModRm == 0x254
))
356
8
    return R_RELAX_GOT_PC;
357
18
358
18
  // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor.
359
18
  // If PIC then no relaxation is available.
360
18
  // We also don't relax test/binop instructions without REX byte,
361
18
  // they are 32bit operations and not common to have.
362
18
  assert(Type == R_X86_64_REX_GOTPCRELX);
363
18
  return Config->Pic ? 
RelExpr9
:
R_RELAX_GOT_PC_NOPIC9
;
364
18
}
365
366
// A subset of relaxations can only be applied for no-PIC. This method
367
// handles such relaxations. Instructions encoding information was taken from:
368
// "Intel 64 and IA-32 Architectures Software Developer's Manual V2"
369
// (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
370
//    64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
371
template <class ELFT>
372
void X86_64<ELFT>::relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
373
9
                                 uint8_t ModRm) const {
374
9
  const uint8_t Rex = Loc[-3];
375
9
  // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg".
376
9
  if (Op == 0x85) {
377
1
    // See "TEST-Logical Compare" (4-428 Vol. 2B),
378
1
    // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension).
379
1
380
1
    // ModR/M byte has form XX YYY ZZZ, where
381
1
    // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1).
382
1
    // XX has different meanings:
383
1
    // 00: The operand's memory address is in reg1.
384
1
    // 01: The operand's memory address is reg1 + a byte-sized displacement.
385
1
    // 10: The operand's memory address is reg1 + a word-sized displacement.
386
1
    // 11: The operand is reg1 itself.
387
1
    // If an instruction requires only one operand, the unused reg2 field
388
1
    // holds extra opcode bits rather than a register code
389
1
    // 0xC0 == 11 000 000 binary.
390
1
    // 0x38 == 00 111 000 binary.
391
1
    // We transfer reg2 to reg1 here as operand.
392
1
    // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3).
393
1
    Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3; // ModR/M byte.
394
1
395
1
    // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32
396
1
    // See "TEST-Logical Compare" (4-428 Vol. 2B).
397
1
    Loc[-2] = 0xf7;
398
1
399
1
    // Move R bit to the B bit in REX byte.
400
1
    // REX byte is encoded as 0100WRXB, where
401
1
    // 0100 is 4bit fixed pattern.
402
1
    // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the
403
1
    //   default operand size is used (which is 32-bit for most but not all
404
1
    //   instructions).
405
1
    // REX.R This 1-bit value is an extension to the MODRM.reg field.
406
1
    // REX.X This 1-bit value is an extension to the SIB.index field.
407
1
    // REX.B This 1-bit value is an extension to the MODRM.rm field or the
408
1
    // SIB.base field.
409
1
    // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A).
410
1
    Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2;
411
1
    write32le(Loc, Val);
412
1
    return;
413
1
  }
414
8
415
8
  // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub
416
8
  // or xor operations.
417
8
418
8
  // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg".
419
8
  // Logic is close to one for test instruction above, but we also
420
8
  // write opcode extension here, see below for details.
421
8
  Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3 | (Op & 0x3c); // ModR/M byte.
422
8
423
8
  // Primary opcode is 0x81, opcode extension is one of:
424
8
  // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB,
425
8
  // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP.
426
8
  // This value was wrote to MODRM.reg in a line above.
427
8
  // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15),
428
8
  // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for
429
8
  // descriptions about each operation.
430
8
  Loc[-2] = 0x81;
431
8
  Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2;
432
8
  write32le(Loc, Val);
433
8
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::relaxGotNoPic(unsigned char*, unsigned long long, unsigned char, unsigned char) const
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::relaxGotNoPic(unsigned char*, unsigned long long, unsigned char, unsigned char) const
Line
Count
Source
373
9
                                 uint8_t ModRm) const {
374
9
  const uint8_t Rex = Loc[-3];
375
9
  // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg".
376
9
  if (Op == 0x85) {
377
1
    // See "TEST-Logical Compare" (4-428 Vol. 2B),
378
1
    // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension).
379
1
380
1
    // ModR/M byte has form XX YYY ZZZ, where
381
1
    // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1).
382
1
    // XX has different meanings:
383
1
    // 00: The operand's memory address is in reg1.
384
1
    // 01: The operand's memory address is reg1 + a byte-sized displacement.
385
1
    // 10: The operand's memory address is reg1 + a word-sized displacement.
386
1
    // 11: The operand is reg1 itself.
387
1
    // If an instruction requires only one operand, the unused reg2 field
388
1
    // holds extra opcode bits rather than a register code
389
1
    // 0xC0 == 11 000 000 binary.
390
1
    // 0x38 == 00 111 000 binary.
391
1
    // We transfer reg2 to reg1 here as operand.
392
1
    // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3).
393
1
    Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3; // ModR/M byte.
394
1
395
1
    // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32
396
1
    // See "TEST-Logical Compare" (4-428 Vol. 2B).
397
1
    Loc[-2] = 0xf7;
398
1
399
1
    // Move R bit to the B bit in REX byte.
400
1
    // REX byte is encoded as 0100WRXB, where
401
1
    // 0100 is 4bit fixed pattern.
402
1
    // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the
403
1
    //   default operand size is used (which is 32-bit for most but not all
404
1
    //   instructions).
405
1
    // REX.R This 1-bit value is an extension to the MODRM.reg field.
406
1
    // REX.X This 1-bit value is an extension to the SIB.index field.
407
1
    // REX.B This 1-bit value is an extension to the MODRM.rm field or the
408
1
    // SIB.base field.
409
1
    // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A).
410
1
    Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2;
411
1
    write32le(Loc, Val);
412
1
    return;
413
1
  }
414
8
415
8
  // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub
416
8
  // or xor operations.
417
8
418
8
  // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg".
419
8
  // Logic is close to one for test instruction above, but we also
420
8
  // write opcode extension here, see below for details.
421
8
  Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3 | (Op & 0x3c); // ModR/M byte.
422
8
423
8
  // Primary opcode is 0x81, opcode extension is one of:
424
8
  // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB,
425
8
  // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP.
426
8
  // This value was wrote to MODRM.reg in a line above.
427
8
  // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15),
428
8
  // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for
429
8
  // descriptions about each operation.
430
8
  Loc[-2] = 0x81;
431
8
  Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2;
432
8
  write32le(Loc, Val);
433
8
}
434
435
template <class ELFT>
436
32
void X86_64<ELFT>::relaxGot(uint8_t *Loc, uint64_t Val) const {
437
32
  const uint8_t Op = Loc[-2];
438
32
  const uint8_t ModRm = Loc[-1];
439
32
440
32
  // Convert "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
441
32
  if (Op == 0x8b) {
442
15
    Loc[-2] = 0x8d;
443
15
    write32le(Loc, Val);
444
15
    return;
445
15
  }
446
17
447
17
  if (Op != 0xff) {
448
9
    // We are relaxing a rip relative to an absolute, so compensate
449
9
    // for the old -4 addend.
450
9
    assert(!Config->Pic);
451
9
    relaxGotNoPic(Loc, Val + 4, Op, ModRm);
452
9
    return;
453
9
  }
454
8
455
8
  // Convert call/jmp instructions.
456
8
  if (ModRm == 0x15) {
457
4
    // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call foo".
458
4
    // Instead we convert to "addr32 call foo" where addr32 is an instruction
459
4
    // prefix. That makes result expression to be a single instruction.
460
4
    Loc[-2] = 0x67; // addr32 prefix
461
4
    Loc[-1] = 0xe8; // call
462
4
    write32le(Loc, Val);
463
4
    return;
464
4
  }
465
4
466
4
  // Convert "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop".
467
4
  // jmp doesn't return, so it is fine to use nop here, it is just a stub.
468
4
  assert(ModRm == 0x25);
469
4
  Loc[-2] = 0xe9; // jmp
470
4
  Loc[3] = 0x90;  // nop
471
4
  write32le(Loc - 1, Val + 1);
472
4
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, false> >::relaxGot(unsigned char*, unsigned long long) const
X86_64.cpp:(anonymous namespace)::X86_64<llvm::object::ELFType<(llvm::support::endianness)1, true> >::relaxGot(unsigned char*, unsigned long long) const
Line
Count
Source
436
32
void X86_64<ELFT>::relaxGot(uint8_t *Loc, uint64_t Val) const {
437
32
  const uint8_t Op = Loc[-2];
438
32
  const uint8_t ModRm = Loc[-1];
439
32
440
32
  // Convert "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
441
32
  if (Op == 0x8b) {
442
15
    Loc[-2] = 0x8d;
443
15
    write32le(Loc, Val);
444
15
    return;
445
15
  }
446
17
447
17
  if (Op != 0xff) {
448
9
    // We are relaxing a rip relative to an absolute, so compensate
449
9
    // for the old -4 addend.
450
9
    assert(!Config->Pic);
451
9
    relaxGotNoPic(Loc, Val + 4, Op, ModRm);
452
9
    return;
453
9
  }
454
8
455
8
  // Convert call/jmp instructions.
456
8
  if (ModRm == 0x15) {
457
4
    // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call foo".
458
4
    // Instead we convert to "addr32 call foo" where addr32 is an instruction
459
4
    // prefix. That makes result expression to be a single instruction.
460
4
    Loc[-2] = 0x67; // addr32 prefix
461
4
    Loc[-1] = 0xe8; // call
462
4
    write32le(Loc, Val);
463
4
    return;
464
4
  }
465
4
466
4
  // Convert "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop".
467
4
  // jmp doesn't return, so it is fine to use nop here, it is just a stub.
468
4
  assert(ModRm == 0x25);
469
4
  Loc[-2] = 0xe9; // jmp
470
4
  Loc[3] = 0x90;  // nop
471
4
  write32le(Loc - 1, Val + 1);
472
4
}
473
474
// This anonymous namespace works around a warning bug in
475
// old versions of gcc. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
476
namespace {
477
478
// A split-stack prologue starts by checking the amount of stack remaining
479
// in one of two ways:
480
// A) Comparing of the stack pointer to a field in the tcb.
481
// B) Or a load of a stack pointer offset with an lea to r10 or r11.
482
template <>
483
bool X86_64<ELF64LE>::adjustPrologueForCrossSplitStack(uint8_t *Loc,
484
7
                                                       uint8_t *End) const {
485
7
  if (Loc + 8 >= End)
486
0
    return false;
487
7
488
7
  // Replace "cmp %fs:0x70,%rsp" and subsequent branch
489
7
  // with "stc, nopl 0x0(%rax,%rax,1)"
490
7
  if (memcmp(Loc, "\x64\x48\x3b\x24\x25", 5) == 0) {
491
2
    memcpy(Loc, "\xf9\x0f\x1f\x84\x00\x00\x00\x00", 8);
492
2
    return true;
493
2
  }
494
5
495
5
  // Adjust "lea X(%rsp),%rYY" to lea "(X - 0x4000)(%rsp),%rYY" where rYY could
496
5
  // be r10 or r11. The lea instruction feeds a subsequent compare which checks
497
5
  // if there is X available stack space. Making X larger effectively reserves
498
5
  // that much additional space. The stack grows downward so subtract the value.
499
5
  if (memcmp(Loc, "\x4c\x8d\x94\x24", 4) == 0 ||
500
5
      
memcmp(Loc, "\x4c\x8d\x9c\x24", 4) == 04
) {
501
2
    // The offset bytes are encoded four bytes after the start of the
502
2
    // instruction.
503
2
    write32le(Loc + 4, read32le(Loc + 4) - 0x4000);
504
2
    return true;
505
2
  }
506
3
  return false;
507
3
}
508
509
template <>
510
bool X86_64<ELF32LE>::adjustPrologueForCrossSplitStack(uint8_t *Loc,
511
0
                                                       uint8_t *End) const {
512
0
  llvm_unreachable("Target doesn't support split stacks.");
513
0
}
514
515
} // namespace
516
517
// These nonstandard PLT entries are to migtigate Spectre v2 security
518
// vulnerability. In order to mitigate Spectre v2, we want to avoid indirect
519
// branch instructions such as `jmp *GOTPLT(%rip)`. So, in the following PLT
520
// entries, we use a CALL followed by MOV and RET to do the same thing as an
521
// indirect jump. That instruction sequence is so-called "retpoline".
522
//
523
// We have two types of retpoline PLTs as a size optimization. If `-z now`
524
// is specified, all dynamic symbols are resolved at load-time. Thus, when
525
// that option is given, we can omit code for symbol lazy resolution.
526
namespace {
527
template <class ELFT> class Retpoline : public X86_64<ELFT> {
528
public:
529
  Retpoline();
530
  void writeGotPlt(uint8_t *Buf, const Symbol &S) const override;
531
  void writePltHeader(uint8_t *Buf) const override;
532
  void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
533
                int32_t Index, unsigned RelOff) const override;
534
};
535
536
template <class ELFT> class RetpolineZNow : public X86_64<ELFT> {
537
public:
538
  RetpolineZNow();
539
4
  void writeGotPlt(uint8_t *Buf, const Symbol &S) const override {}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::RetpolineZNow<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeGotPlt(unsigned char*, lld::elf::Symbol const&) const
X86_64.cpp:(anonymous namespace)::RetpolineZNow<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeGotPlt(unsigned char*, lld::elf::Symbol const&) const
Line
Count
Source
539
4
  void writeGotPlt(uint8_t *Buf, const Symbol &S) const override {}
540
  void writePltHeader(uint8_t *Buf) const override;
541
  void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
542
                int32_t Index, unsigned RelOff) const override;
543
};
544
} // namespace
545
546
3
template <class ELFT> Retpoline<ELFT>::Retpoline() {
547
3
  TargetInfo::PltHeaderSize = 48;
548
3
  TargetInfo::PltEntrySize = 32;
549
3
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::Retpoline<llvm::object::ELFType<(llvm::support::endianness)1, false> >::Retpoline()
X86_64.cpp:(anonymous namespace)::Retpoline<llvm::object::ELFType<(llvm::support::endianness)1, true> >::Retpoline()
Line
Count
Source
546
3
template <class ELFT> Retpoline<ELFT>::Retpoline() {
547
3
  TargetInfo::PltHeaderSize = 48;
548
3
  TargetInfo::PltEntrySize = 32;
549
3
}
550
551
template <class ELFT>
552
5
void Retpoline<ELFT>::writeGotPlt(uint8_t *Buf, const Symbol &S) const {
553
5
  write64le(Buf, S.getPltVA() + 17);
554
5
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::Retpoline<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeGotPlt(unsigned char*, lld::elf::Symbol const&) const
X86_64.cpp:(anonymous namespace)::Retpoline<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeGotPlt(unsigned char*, lld::elf::Symbol const&) const
Line
Count
Source
552
5
void Retpoline<ELFT>::writeGotPlt(uint8_t *Buf, const Symbol &S) const {
553
5
  write64le(Buf, S.getPltVA() + 17);
554
5
}
555
556
3
template <class ELFT> void Retpoline<ELFT>::writePltHeader(uint8_t *Buf) const {
557
3
  const uint8_t Insn[] = {
558
3
      0xff, 0x35, 0,    0,    0,    0,          // 0:    pushq GOTPLT+8(%rip)
559
3
      0x4c, 0x8b, 0x1d, 0,    0,    0,    0,    // 6:    mov GOTPLT+16(%rip), %r11
560
3
      0xe8, 0x0e, 0x00, 0x00, 0x00,             // d:    callq next
561
3
      0xf3, 0x90,                               // 12: loop: pause
562
3
      0x0f, 0xae, 0xe8,                         // 14:   lfence
563
3
      0xeb, 0xf9,                               // 17:   jmp loop
564
3
      0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 19:   int3; .align 16
565
3
      0x4c, 0x89, 0x1c, 0x24,                   // 20: next: mov %r11, (%rsp)
566
3
      0xc3,                                     // 24:   ret
567
3
      0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 25:   int3; padding
568
3
      0xcc, 0xcc, 0xcc, 0xcc,                   // 2c:   int3; padding
569
3
  };
570
3
  memcpy(Buf, Insn, sizeof(Insn));
571
3
572
3
  uint64_t GotPlt = InX::GotPlt->getVA();
573
3
  uint64_t Plt = InX::Plt->getVA();
574
3
  write32le(Buf + 2, GotPlt - Plt - 6 + 8);
575
3
  write32le(Buf + 9, GotPlt - Plt - 13 + 16);
576
3
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::Retpoline<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writePltHeader(unsigned char*) const
X86_64.cpp:(anonymous namespace)::Retpoline<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writePltHeader(unsigned char*) const
Line
Count
Source
556
3
template <class ELFT> void Retpoline<ELFT>::writePltHeader(uint8_t *Buf) const {
557
3
  const uint8_t Insn[] = {
558
3
      0xff, 0x35, 0,    0,    0,    0,          // 0:    pushq GOTPLT+8(%rip)
559
3
      0x4c, 0x8b, 0x1d, 0,    0,    0,    0,    // 6:    mov GOTPLT+16(%rip), %r11
560
3
      0xe8, 0x0e, 0x00, 0x00, 0x00,             // d:    callq next
561
3
      0xf3, 0x90,                               // 12: loop: pause
562
3
      0x0f, 0xae, 0xe8,                         // 14:   lfence
563
3
      0xeb, 0xf9,                               // 17:   jmp loop
564
3
      0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 19:   int3; .align 16
565
3
      0x4c, 0x89, 0x1c, 0x24,                   // 20: next: mov %r11, (%rsp)
566
3
      0xc3,                                     // 24:   ret
567
3
      0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 25:   int3; padding
568
3
      0xcc, 0xcc, 0xcc, 0xcc,                   // 2c:   int3; padding
569
3
  };
570
3
  memcpy(Buf, Insn, sizeof(Insn));
571
3
572
3
  uint64_t GotPlt = InX::GotPlt->getVA();
573
3
  uint64_t Plt = InX::Plt->getVA();
574
3
  write32le(Buf + 2, GotPlt - Plt - 6 + 8);
575
3
  write32le(Buf + 9, GotPlt - Plt - 13 + 16);
576
3
}
577
578
template <class ELFT>
579
void Retpoline<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
580
                               uint64_t PltEntryAddr, int32_t Index,
581
5
                               unsigned RelOff) const {
582
5
  const uint8_t Insn[] = {
583
5
      0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 0:  mov foo@GOTPLT(%rip), %r11
584
5
      0xe8, 0,    0,    0,    0,    // 7:  callq plt+0x20
585
5
      0xe9, 0,    0,    0,    0,    // c:  jmp plt+0x12
586
5
      0x68, 0,    0,    0,    0,    // 11: pushq <relocation index>
587
5
      0xe9, 0,    0,    0,    0,    // 16: jmp plt+0
588
5
      0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1b: int3; padding
589
5
  };
590
5
  memcpy(Buf, Insn, sizeof(Insn));
591
5
592
5
  uint64_t Off = TargetInfo::getPltEntryOffset(Index);
593
5
594
5
  write32le(Buf + 3, GotPltEntryAddr - PltEntryAddr - 7);
595
5
  write32le(Buf + 8, -Off - 12 + 32);
596
5
  write32le(Buf + 13, -Off - 17 + 18);
597
5
  write32le(Buf + 18, Index);
598
5
  write32le(Buf + 23, -Off - 27);
599
5
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::Retpoline<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writePlt(unsigned char*, unsigned long long, unsigned long long, int, unsigned int) const
X86_64.cpp:(anonymous namespace)::Retpoline<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writePlt(unsigned char*, unsigned long long, unsigned long long, int, unsigned int) const
Line
Count
Source
581
5
                               unsigned RelOff) const {
582
5
  const uint8_t Insn[] = {
583
5
      0x4c, 0x8b, 0x1d, 0, 0, 0, 0, // 0:  mov foo@GOTPLT(%rip), %r11
584
5
      0xe8, 0,    0,    0,    0,    // 7:  callq plt+0x20
585
5
      0xe9, 0,    0,    0,    0,    // c:  jmp plt+0x12
586
5
      0x68, 0,    0,    0,    0,    // 11: pushq <relocation index>
587
5
      0xe9, 0,    0,    0,    0,    // 16: jmp plt+0
588
5
      0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1b: int3; padding
589
5
  };
590
5
  memcpy(Buf, Insn, sizeof(Insn));
591
5
592
5
  uint64_t Off = TargetInfo::getPltEntryOffset(Index);
593
5
594
5
  write32le(Buf + 3, GotPltEntryAddr - PltEntryAddr - 7);
595
5
  write32le(Buf + 8, -Off - 12 + 32);
596
5
  write32le(Buf + 13, -Off - 17 + 18);
597
5
  write32le(Buf + 18, Index);
598
5
  write32le(Buf + 23, -Off - 27);
599
5
}
600
601
2
template <class ELFT> RetpolineZNow<ELFT>::RetpolineZNow() {
602
2
  TargetInfo::PltHeaderSize = 32;
603
2
  TargetInfo::PltEntrySize = 16;
604
2
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::RetpolineZNow<llvm::object::ELFType<(llvm::support::endianness)1, false> >::RetpolineZNow()
X86_64.cpp:(anonymous namespace)::RetpolineZNow<llvm::object::ELFType<(llvm::support::endianness)1, true> >::RetpolineZNow()
Line
Count
Source
601
2
template <class ELFT> RetpolineZNow<ELFT>::RetpolineZNow() {
602
2
  TargetInfo::PltHeaderSize = 32;
603
2
  TargetInfo::PltEntrySize = 16;
604
2
}
605
606
template <class ELFT>
607
2
void RetpolineZNow<ELFT>::writePltHeader(uint8_t *Buf) const {
608
2
  const uint8_t Insn[] = {
609
2
      0xe8, 0x0b, 0x00, 0x00, 0x00, // 0:    call next
610
2
      0xf3, 0x90,                   // 5:  loop: pause
611
2
      0x0f, 0xae, 0xe8,             // 7:    lfence
612
2
      0xeb, 0xf9,                   // a:    jmp loop
613
2
      0xcc, 0xcc, 0xcc, 0xcc,       // c:    int3; .align 16
614
2
      0x4c, 0x89, 0x1c, 0x24,       // 10: next: mov %r11, (%rsp)
615
2
      0xc3,                         // 14:   ret
616
2
      0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 15:   int3; padding
617
2
      0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1a:   int3; padding
618
2
      0xcc,                         // 1f:   int3; padding
619
2
  };
620
2
  memcpy(Buf, Insn, sizeof(Insn));
621
2
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::RetpolineZNow<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writePltHeader(unsigned char*) const
X86_64.cpp:(anonymous namespace)::RetpolineZNow<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writePltHeader(unsigned char*) const
Line
Count
Source
607
2
void RetpolineZNow<ELFT>::writePltHeader(uint8_t *Buf) const {
608
2
  const uint8_t Insn[] = {
609
2
      0xe8, 0x0b, 0x00, 0x00, 0x00, // 0:    call next
610
2
      0xf3, 0x90,                   // 5:  loop: pause
611
2
      0x0f, 0xae, 0xe8,             // 7:    lfence
612
2
      0xeb, 0xf9,                   // a:    jmp loop
613
2
      0xcc, 0xcc, 0xcc, 0xcc,       // c:    int3; .align 16
614
2
      0x4c, 0x89, 0x1c, 0x24,       // 10: next: mov %r11, (%rsp)
615
2
      0xc3,                         // 14:   ret
616
2
      0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 15:   int3; padding
617
2
      0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1a:   int3; padding
618
2
      0xcc,                         // 1f:   int3; padding
619
2
  };
620
2
  memcpy(Buf, Insn, sizeof(Insn));
621
2
}
622
623
template <class ELFT>
624
void RetpolineZNow<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
625
                                   uint64_t PltEntryAddr, int32_t Index,
626
4
                                   unsigned RelOff) const {
627
4
  const uint8_t Insn[] = {
628
4
      0x4c, 0x8b, 0x1d, 0,    0, 0, 0, // mov foo@GOTPLT(%rip), %r11
629
4
      0xe9, 0,    0,    0,    0,       // jmp plt+0
630
4
      0xcc, 0xcc, 0xcc, 0xcc,          // int3; padding
631
4
  };
632
4
  memcpy(Buf, Insn, sizeof(Insn));
633
4
634
4
  write32le(Buf + 3, GotPltEntryAddr - PltEntryAddr - 7);
635
4
  write32le(Buf + 8, -TargetInfo::getPltEntryOffset(Index) - 12);
636
4
}
Unexecuted instantiation: X86_64.cpp:(anonymous namespace)::RetpolineZNow<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writePlt(unsigned char*, unsigned long long, unsigned long long, int, unsigned int) const
X86_64.cpp:(anonymous namespace)::RetpolineZNow<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writePlt(unsigned char*, unsigned long long, unsigned long long, int, unsigned int) const
Line
Count
Source
626
4
                                   unsigned RelOff) const {
627
4
  const uint8_t Insn[] = {
628
4
      0x4c, 0x8b, 0x1d, 0,    0, 0, 0, // mov foo@GOTPLT(%rip), %r11
629
4
      0xe9, 0,    0,    0,    0,       // jmp plt+0
630
4
      0xcc, 0xcc, 0xcc, 0xcc,          // int3; padding
631
4
  };
632
4
  memcpy(Buf, Insn, sizeof(Insn));
633
4
634
4
  write32le(Buf + 3, GotPltEntryAddr - PltEntryAddr - 7);
635
4
  write32le(Buf + 8, -TargetInfo::getPltEntryOffset(Index) - 12);
636
4
}
637
638
1.68k
template <class ELFT> static TargetInfo *getTargetInfo() {
639
1.68k
  if (Config->ZRetpolineplt) {
640
5
    if (Config->ZNow) {
641
2
      static RetpolineZNow<ELFT> T;
642
2
      return &T;
643
2
    }
644
3
    static Retpoline<ELFT> T;
645
3
    return &T;
646
3
  }
647
1.68k
648
1.68k
  static X86_64<ELFT> T;
649
1.68k
  return &T;
650
1.68k
}
X86_64.cpp:lld::elf::TargetInfo* getTargetInfo<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
638
3
template <class ELFT> static TargetInfo *getTargetInfo() {
639
3
  if (Config->ZRetpolineplt) {
640
0
    if (Config->ZNow) {
641
0
      static RetpolineZNow<ELFT> T;
642
0
      return &T;
643
0
    }
644
0
    static Retpoline<ELFT> T;
645
0
    return &T;
646
0
  }
647
3
648
3
  static X86_64<ELFT> T;
649
3
  return &T;
650
3
}
X86_64.cpp:lld::elf::TargetInfo* getTargetInfo<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
638
1.68k
template <class ELFT> static TargetInfo *getTargetInfo() {
639
1.68k
  if (Config->ZRetpolineplt) {
640
5
    if (Config->ZNow) {
641
2
      static RetpolineZNow<ELFT> T;
642
2
      return &T;
643
2
    }
644
3
    static Retpoline<ELFT> T;
645
3
    return &T;
646
3
  }
647
1.68k
648
1.68k
  static X86_64<ELFT> T;
649
1.68k
  return &T;
650
1.68k
}
651
652
3
TargetInfo *elf::getX32TargetInfo() { return getTargetInfo<ELF32LE>(); }
653
1.68k
TargetInfo *elf::getX86_64TargetInfo() { return getTargetInfo<ELF64LE>(); }