Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/ELF/Thunks.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Thunks.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
// This file contains Thunk subclasses.
11
//
12
// A thunk is a small piece of code written after an input section
13
// which is used to jump between "incompatible" functions
14
// such as MIPS PIC and non-PIC or ARM non-Thumb and Thumb functions.
15
//
16
// If a jump target is too far and its address doesn't fit to a
17
// short jump instruction, we need to create a thunk too, but we
18
// haven't supported it yet.
19
//
20
// i386 and x86-64 don't need thunks.
21
//
22
//===---------------------------------------------------------------------===//
23
24
#include "Thunks.h"
25
#include "Config.h"
26
#include "Error.h"
27
#include "InputSection.h"
28
#include "Memory.h"
29
#include "OutputSections.h"
30
#include "Symbols.h"
31
#include "SyntheticSections.h"
32
#include "Target.h"
33
#include "llvm/BinaryFormat/ELF.h"
34
#include "llvm/Support/Casting.h"
35
#include "llvm/Support/Endian.h"
36
#include "llvm/Support/ErrorHandling.h"
37
#include "llvm/Support/MathExtras.h"
38
#include <cstdint>
39
#include <cstring>
40
41
using namespace llvm;
42
using namespace llvm::object;
43
using namespace llvm::support::endian;
44
using namespace llvm::ELF;
45
46
namespace lld {
47
namespace elf {
48
49
namespace {
50
51
// Specific ARM Thunk implementations. The naming convention is:
52
// Source State, TargetState, Target Requirement, ABS or PI, Range
53
class ARMV7ABSLongThunk final : public Thunk {
54
public:
55
4
  ARMV7ABSLongThunk(const SymbolBody &Dest) : Thunk(Dest) {}
56
57
8
  uint32_t size() const override { return 12; }
58
  void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
59
  void addSymbols(ThunkSection &IS) override;
60
  bool isCompatibleWith(uint32_t RelocType) const override;
61
};
62
63
class ARMV7PILongThunk final : public Thunk {
64
public:
65
5
  ARMV7PILongThunk(const SymbolBody &Dest) : Thunk(Dest) {}
66
67
10
  uint32_t size() const override { return 16; }
68
  void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
69
  void addSymbols(ThunkSection &IS) override;
70
  bool isCompatibleWith(uint32_t RelocType) const override;
71
};
72
73
class ThumbV7ABSLongThunk final : public Thunk {
74
public:
75
5
  ThumbV7ABSLongThunk(const SymbolBody &Dest) : Thunk(Dest) { Alignment = 2; }
76
77
10
  uint32_t size() const override { return 10; }
78
  void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
79
  void addSymbols(ThunkSection &IS) override;
80
  bool isCompatibleWith(uint32_t RelocType) const override;
81
};
82
83
class ThumbV7PILongThunk final : public Thunk {
84
public:
85
11
  ThumbV7PILongThunk(const SymbolBody &Dest) : Thunk(Dest) { Alignment = 2; }
86
87
22
  uint32_t size() const override { return 12; }
88
  void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
89
  void addSymbols(ThunkSection &IS) override;
90
  bool isCompatibleWith(uint32_t RelocType) const override;
91
};
92
93
// MIPS LA25 thunk
94
class MipsThunk final : public Thunk {
95
public:
96
28
  MipsThunk(const SymbolBody &Dest) : Thunk(Dest) {}
97
98
56
  uint32_t size() const override { return 16; }
99
  void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
100
  void addSymbols(ThunkSection &IS) override;
101
  InputSection *getTargetInputSection() const override;
102
};
103
104
} // end anonymous namespace
105
106
// ARM Target Thunks
107
25
static uint64_t getARMThunkDestVA(const SymbolBody &S) {
108
25
  uint64_t V = S.isInPlt() ? 
S.getPltVA()6
:
S.getVA()19
;
109
25
  return SignExtend64<32>(V);
110
25
}
111
112
4
void ARMV7ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
113
4
  const uint8_t Data[] = {
114
4
      0x00, 0xc0, 0x00, 0xe3, // movw         ip,:lower16:S
115
4
      0x00, 0xc0, 0x40, 0xe3, // movt         ip,:upper16:S
116
4
      0x1c, 0xff, 0x2f, 0xe1, // bx   ip
117
4
  };
118
4
  uint64_t S = getARMThunkDestVA(Destination);
119
4
  memcpy(Buf, Data, sizeof(Data));
120
4
  Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
121
4
  Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
122
4
}
123
124
4
void ARMV7ABSLongThunk::addSymbols(ThunkSection &IS) {
125
4
  ThunkSym = addSyntheticLocal(
126
4
      Saver.save("__ARMv7ABSLongThunk_" + Destination.getName()), STT_FUNC,
127
4
      Offset, size(), &IS);
128
4
  addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, &IS);
129
4
}
130
131
1
bool ARMV7ABSLongThunk::isCompatibleWith(uint32_t RelocType) const {
132
1
  // Thumb branch relocations can't use BLX
133
1
  return RelocType != R_ARM_THM_JUMP19 && RelocType != R_ARM_THM_JUMP24;
134
1
}
135
136
5
void ThumbV7ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
137
5
  const uint8_t Data[] = {
138
5
      0x40, 0xf2, 0x00, 0x0c, // movw         ip, :lower16:S
139
5
      0xc0, 0xf2, 0x00, 0x0c, // movt         ip, :upper16:S
140
5
      0x60, 0x47,             // bx   ip
141
5
  };
142
5
  uint64_t S = getARMThunkDestVA(Destination);
143
5
  memcpy(Buf, Data, sizeof(Data));
144
5
  Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
145
5
  Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
146
5
}
147
148
5
void ThumbV7ABSLongThunk::addSymbols(ThunkSection &IS) {
149
5
  ThunkSym = addSyntheticLocal(
150
5
      Saver.save("__Thumbv7ABSLongThunk_" + Destination.getName()), STT_FUNC,
151
5
      Offset | 0x1, size(), &IS);
152
5
  addSyntheticLocal("$t", STT_NOTYPE, Offset, 0, &IS);
153
5
}
154
155
3
bool ThumbV7ABSLongThunk::isCompatibleWith(uint32_t RelocType) const {
156
3
  // ARM branch relocations can't use BLX
157
3
  return RelocType != R_ARM_JUMP24 && RelocType != R_ARM_PC24 &&
158
3
         RelocType != R_ARM_PLT32;
159
3
}
160
161
5
void ARMV7PILongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
162
5
  const uint8_t Data[] = {
163
5
      0xf0, 0xcf, 0x0f, 0xe3, // P:  movw ip,:lower16:S - (P + (L1-P) + 8)
164
5
      0x00, 0xc0, 0x40, 0xe3, //     movt ip,:upper16:S - (P + (L1-P) + 8)
165
5
      0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
166
5
      0x1c, 0xff, 0x2f, 0xe1, //     bx r12
167
5
  };
168
5
  uint64_t S = getARMThunkDestVA(Destination);
169
5
  uint64_t P = ThunkSym->getVA();
170
5
  uint64_t Offset = S - P - 16;
171
5
  memcpy(Buf, Data, sizeof(Data));
172
5
  Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, Offset);
173
5
  Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, Offset);
174
5
}
175
176
5
void ARMV7PILongThunk::addSymbols(ThunkSection &IS) {
177
5
  ThunkSym = addSyntheticLocal(
178
5
      Saver.save("__ARMV7PILongThunk_" + Destination.getName()), STT_FUNC,
179
5
      Offset, size(), &IS);
180
5
  addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, &IS);
181
5
}
182
183
1
bool ARMV7PILongThunk::isCompatibleWith(uint32_t RelocType) const {
184
1
  // Thumb branch relocations can't use BLX
185
1
  return RelocType != R_ARM_THM_JUMP19 && RelocType != R_ARM_THM_JUMP24;
186
1
}
187
188
11
void ThumbV7PILongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
189
11
  const uint8_t Data[] = {
190
11
      0x4f, 0xf6, 0xf4, 0x7c, // P:  movw ip,:lower16:S - (P + (L1-P) + 4)
191
11
      0xc0, 0xf2, 0x00, 0x0c, //     movt ip,:upper16:S - (P + (L1-P) + 4)
192
11
      0xfc, 0x44,             // L1: add  r12, pc
193
11
      0x60, 0x47,             //     bx   r12
194
11
  };
195
11
  uint64_t S = getARMThunkDestVA(Destination);
196
11
  uint64_t P = ThunkSym->getVA() & ~0x1;
197
11
  uint64_t Offset = S - P - 12;
198
11
  memcpy(Buf, Data, sizeof(Data));
199
11
  Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, Offset);
200
11
  Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, Offset);
201
11
}
202
203
11
void ThumbV7PILongThunk::addSymbols(ThunkSection &IS) {
204
11
  ThunkSym = addSyntheticLocal(
205
11
      Saver.save("__ThumbV7PILongThunk_" + Destination.getName()), STT_FUNC,
206
11
      Offset | 0x1, size(), &IS);
207
11
  addSyntheticLocal("$t", STT_NOTYPE, Offset, 0, &IS);
208
11
}
209
210
6
bool ThumbV7PILongThunk::isCompatibleWith(uint32_t RelocType) const {
211
6
  // ARM branch relocations can't use BLX
212
6
  return RelocType != R_ARM_JUMP24 && RelocType != R_ARM_PC24 &&
213
6
         RelocType != R_ARM_PLT32;
214
6
}
215
216
// Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
217
28
void MipsThunk::writeTo(uint8_t *Buf, ThunkSection &) const {
218
28
  uint64_t S = Destination.getVA();
219
28
  write32(Buf, 0x3c190000, Config->Endianness); // lui   $25, %hi(func)
220
28
  write32(Buf + 4, 0x08000000 | (S >> 2), Config->Endianness); // j     func
221
28
  write32(Buf + 8, 0x27390000, Config->Endianness); // addiu $25, $25, %lo(func)
222
28
  write32(Buf + 12, 0x00000000, Config->Endianness); // nop
223
28
  Target->relocateOne(Buf, R_MIPS_HI16, S);
224
28
  Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
225
28
}
226
227
28
void MipsThunk::addSymbols(ThunkSection &IS) {
228
28
  ThunkSym =
229
28
      addSyntheticLocal(Saver.save("__LA25Thunk_" + Destination.getName()),
230
28
                        STT_FUNC, Offset, size(), &IS);
231
28
}
232
233
58
InputSection *MipsThunk::getTargetInputSection() const {
234
58
  auto *DR = dyn_cast<DefinedRegular>(&Destination);
235
58
  return dyn_cast<InputSection>(DR->Section);
236
58
}
237
238
53
Thunk::Thunk(const SymbolBody &D) : Destination(D), Offset(0) {}
239
240
53
Thunk::~Thunk() = default;
241
242
// Creates a thunk for Thumb-ARM interworking.
243
25
static Thunk *addThunkArm(uint32_t Reloc, SymbolBody &S) {
244
25
  // ARM relocations need ARM to Thumb interworking Thunks.
245
25
  // Thumb relocations need Thumb to ARM relocations.
246
25
  // Use position independent Thunks if we require position independent code.
247
25
  switch (Reloc) {
248
9
  case R_ARM_PC24:
249
9
  case R_ARM_PLT32:
250
9
  case R_ARM_JUMP24:
251
9
    if (Config->Pic)
252
5
      return make<ARMV7PILongThunk>(S);
253
4
    return make<ARMV7ABSLongThunk>(S);
254
16
  case R_ARM_THM_JUMP19:
255
16
  case R_ARM_THM_JUMP24:
256
16
    if (Config->Pic)
257
11
      return make<ThumbV7PILongThunk>(S);
258
5
    return make<ThumbV7ABSLongThunk>(S);
259
0
  }
260
0
  fatal("unrecognized relocation type");
261
0
}
262
263
28
static Thunk *addThunkMips(SymbolBody &S) { return make<MipsThunk>(S); }
264
265
53
Thunk *addThunk(uint32_t RelocType, SymbolBody &S) {
266
53
  if (Config->EMachine == EM_ARM)
267
25
    return addThunkArm(RelocType, S);
268
28
  else 
if (28
Config->EMachine == EM_MIPS28
)
269
28
    return addThunkMips(S);
270
0
  
llvm_unreachable0
("add Thunk only supported for ARM and Mips");
271
0
  return nullptr;
272
53
}
273
274
} // end namespace elf
275
} // end namespace lld