Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/Thunks.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Thunks.cpp --------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===---------------------------------------------------------------------===//
8
//
9
// This file contains Thunk subclasses.
10
//
11
// A thunk is a small piece of code written after an input section
12
// which is used to jump between "incompatible" functions
13
// such as MIPS PIC and non-PIC or ARM non-Thumb and Thumb functions.
14
//
15
// If a jump target is too far and its address doesn't fit to a
16
// short jump instruction, we need to create a thunk too, but we
17
// haven't supported it yet.
18
//
19
// i386 and x86-64 don't need thunks.
20
//
21
//===---------------------------------------------------------------------===//
22
23
#include "Thunks.h"
24
#include "Config.h"
25
#include "InputSection.h"
26
#include "OutputSections.h"
27
#include "Symbols.h"
28
#include "SyntheticSections.h"
29
#include "Target.h"
30
#include "lld/Common/ErrorHandler.h"
31
#include "lld/Common/Memory.h"
32
#include "llvm/BinaryFormat/ELF.h"
33
#include "llvm/Support/Casting.h"
34
#include "llvm/Support/Endian.h"
35
#include "llvm/Support/ErrorHandling.h"
36
#include "llvm/Support/MathExtras.h"
37
#include <cstdint>
38
#include <cstring>
39
40
using namespace llvm;
41
using namespace llvm::object;
42
using namespace llvm::ELF;
43
44
namespace lld {
45
namespace elf {
46
47
namespace {
48
49
// AArch64 long range Thunks
50
class AArch64ABSLongThunk final : public Thunk {
51
public:
52
6
  AArch64ABSLongThunk(Symbol &dest) : Thunk(dest) {}
53
12
  uint32_t size() override { return 16; }
54
  void writeTo(uint8_t *buf) override;
55
  void addSymbols(ThunkSection &isec) override;
56
};
57
58
class AArch64ADRPThunk final : public Thunk {
59
public:
60
3
  AArch64ADRPThunk(Symbol &dest) : Thunk(dest) {}
61
6
  uint32_t size() override { return 12; }
62
  void writeTo(uint8_t *buf) override;
63
  void addSymbols(ThunkSection &isec) override;
64
};
65
66
// Base class for ARM thunks.
67
//
68
// An ARM thunk may be either short or long. A short thunk is simply a branch
69
// (B) instruction, and it may be used to call ARM functions when the distance
70
// from the thunk to the target is less than 32MB. Long thunks can branch to any
71
// virtual address and can switch between ARM and Thumb, and they are
72
// implemented in the derived classes. This class tries to create a short thunk
73
// if the target is in range, otherwise it creates a long thunk.
74
class ARMThunk : public Thunk {
75
public:
76
30
  ARMThunk(Symbol &dest) : Thunk(dest) {}
77
78
  bool getMayUseShortThunk();
79
63
  uint32_t size() override { return getMayUseShortThunk() ? 
416
:
sizeLong()47
; }
80
  void writeTo(uint8_t *buf) override;
81
  bool isCompatibleWith(const InputSection &isec,
82
                        const Relocation &rel) const override;
83
84
  // Returns the size of a long thunk.
85
  virtual uint32_t sizeLong() = 0;
86
87
  // Writes a long thunk to Buf.
88
  virtual void writeLong(uint8_t *buf) = 0;
89
90
private:
91
  // This field tracks whether all previously considered layouts would allow
92
  // this thunk to be short. If we have ever needed a long thunk, we always
93
  // create a long thunk, even if the thunk may be short given the current
94
  // distance to the target. We do this because transitioning from long to short
95
  // can create layout oscillations in certain corner cases which would prevent
96
  // the layout from converging.
97
  bool mayUseShortThunk = true;
98
};
99
100
// Base class for Thumb-2 thunks.
101
//
102
// This class is similar to ARMThunk, but it uses the Thumb-2 B.W instruction
103
// which has a range of 16MB.
104
class ThumbThunk : public Thunk {
105
public:
106
79
  ThumbThunk(Symbol &dest) : Thunk(dest) { alignment = 2; }
107
108
  bool getMayUseShortThunk();
109
164
  uint32_t size() override { return getMayUseShortThunk() ? 
444
:
sizeLong()120
; }
110
  void writeTo(uint8_t *buf) override;
111
  bool isCompatibleWith(const InputSection &isec,
112
                        const Relocation &rel) const override;
113
114
  // Returns the size of a long thunk.
115
  virtual uint32_t sizeLong() = 0;
116
117
  // Writes a long thunk to Buf.
118
  virtual void writeLong(uint8_t *buf) = 0;
119
120
private:
121
  // See comment in ARMThunk above.
122
  bool mayUseShortThunk = true;
123
};
124
125
// Specific ARM Thunk implementations. The naming convention is:
126
// Source State, TargetState, Target Requirement, ABS or PI, Range
127
class ARMV7ABSLongThunk final : public ARMThunk {
128
public:
129
16
  ARMV7ABSLongThunk(Symbol &dest) : ARMThunk(dest) {}
130
131
27
  uint32_t sizeLong() override { return 12; }
132
  void writeLong(uint8_t *buf) override;
133
  void addSymbols(ThunkSection &isec) override;
134
};
135
136
class ARMV7PILongThunk final : public ARMThunk {
137
public:
138
7
  ARMV7PILongThunk(Symbol &dest) : ARMThunk(dest) {}
139
140
14
  uint32_t sizeLong() override { return 16; }
141
  void writeLong(uint8_t *buf) override;
142
  void addSymbols(ThunkSection &isec) override;
143
};
144
145
class ThumbV7ABSLongThunk final : public ThumbThunk {
146
public:
147
53
  ThumbV7ABSLongThunk(Symbol &dest) : ThumbThunk(dest) {}
148
149
66
  uint32_t sizeLong() override { return 10; }
150
  void writeLong(uint8_t *buf) override;
151
  void addSymbols(ThunkSection &isec) override;
152
};
153
154
class ThumbV7PILongThunk final : public ThumbThunk {
155
public:
156
24
  ThumbV7PILongThunk(Symbol &dest) : ThumbThunk(dest) {}
157
158
50
  uint32_t sizeLong() override { return 12; }
159
  void writeLong(uint8_t *buf) override;
160
  void addSymbols(ThunkSection &isec) override;
161
};
162
163
// Implementations of Thunks for older Arm architectures that do not support
164
// the movt/movw instructions. These thunks require at least Architecture v5
165
// as used on processors such as the Arm926ej-s. There are no Thumb entry
166
// points as there is no Thumb branch instruction on these architecture that
167
// can result in a thunk
168
class ARMV5ABSLongThunk final : public ARMThunk {
169
public:
170
3
  ARMV5ABSLongThunk(Symbol &dest) : ARMThunk(dest) {}
171
172
4
  uint32_t sizeLong() override { return 8; }
173
  void writeLong(uint8_t *buf) override;
174
  void addSymbols(ThunkSection &isec) override;
175
  bool isCompatibleWith(const InputSection &isec,
176
                        const Relocation &rel) const override;
177
};
178
179
class ARMV5PILongThunk final : public ARMThunk {
180
public:
181
4
  ARMV5PILongThunk(Symbol &dest) : ARMThunk(dest) {}
182
183
2
  uint32_t sizeLong() override { return 16; }
184
  void writeLong(uint8_t *buf) override;
185
  void addSymbols(ThunkSection &isec) override;
186
  bool isCompatibleWith(const InputSection &isec,
187
                        const Relocation &rel) const override;
188
};
189
190
// Implementations of Thunks for Arm v6-M. Only Thumb instructions are permitted
191
class ThumbV6MABSLongThunk final : public ThumbThunk {
192
public:
193
1
  ThumbV6MABSLongThunk(Symbol &dest) : ThumbThunk(dest) {}
194
195
2
  uint32_t sizeLong() override { return 12; }
196
  void writeLong(uint8_t *buf) override;
197
  void addSymbols(ThunkSection &isec) override;
198
};
199
200
class ThumbV6MPILongThunk final : public ThumbThunk {
201
public:
202
1
  ThumbV6MPILongThunk(Symbol &dest) : ThumbThunk(dest) {}
203
204
2
  uint32_t sizeLong() override { return 16; }
205
  void writeLong(uint8_t *buf) override;
206
  void addSymbols(ThunkSection &isec) override;
207
};
208
209
// MIPS LA25 thunk
210
class MipsThunk final : public Thunk {
211
public:
212
38
  MipsThunk(Symbol &dest) : Thunk(dest) {}
213
214
141
  uint32_t size() override { return 16; }
215
  void writeTo(uint8_t *buf) override;
216
  void addSymbols(ThunkSection &isec) override;
217
  InputSection *getTargetInputSection() const override;
218
};
219
220
// microMIPS R2-R5 LA25 thunk
221
class MicroMipsThunk final : public Thunk {
222
public:
223
3
  MicroMipsThunk(Symbol &dest) : Thunk(dest) {}
224
225
12
  uint32_t size() override { return 14; }
226
  void writeTo(uint8_t *buf) override;
227
  void addSymbols(ThunkSection &isec) override;
228
  InputSection *getTargetInputSection() const override;
229
};
230
231
// microMIPS R6 LA25 thunk
232
class MicroMipsR6Thunk final : public Thunk {
233
public:
234
2
  MicroMipsR6Thunk(Symbol &dest) : Thunk(dest) {}
235
236
8
  uint32_t size() override { return 12; }
237
  void writeTo(uint8_t *buf) override;
238
  void addSymbols(ThunkSection &isec) override;
239
  InputSection *getTargetInputSection() const override;
240
};
241
242
class PPC32PltCallStub final : public Thunk {
243
public:
244
  PPC32PltCallStub(const InputSection &isec, const Relocation &rel, Symbol &dest)
245
      : Thunk(dest), addend(rel.type == R_PPC_PLTREL24 ? rel.addend : 0),
246
17
        file(isec.file) {}
247
34
  uint32_t size() override { return 16; }
248
  void writeTo(uint8_t *buf) override;
249
  void addSymbols(ThunkSection &isec) override;
250
  bool isCompatibleWith(const InputSection &isec, const Relocation &rel) const override;
251
252
private:
253
  // For R_PPC_PLTREL24, this records the addend, which will be used to decide
254
  // the offsets in the call stub.
255
  uint32_t addend;
256
257
  // Records the call site of the call stub.
258
  const InputFile *file;
259
};
260
261
// PPC64 Plt call stubs.
262
// Any call site that needs to call through a plt entry needs a call stub in
263
// the .text section. The call stub is responsible for:
264
// 1) Saving the toc-pointer to the stack.
265
// 2) Loading the target functions address from the procedure linkage table into
266
//    r12 for use by the target functions global entry point, and into the count
267
//    register.
268
// 3) Transfering control to the target function through an indirect branch.
269
class PPC64PltCallStub final : public Thunk {
270
public:
271
27
  PPC64PltCallStub(Symbol &dest) : Thunk(dest) {}
272
54
  uint32_t size() override { return 20; }
273
  void writeTo(uint8_t *buf) override;
274
  void addSymbols(ThunkSection &isec) override;
275
};
276
277
// A bl instruction uses a signed 24 bit offset, with an implicit 4 byte
278
// alignment. This gives a possible 26 bits of 'reach'. If the call offset is
279
// larger then that we need to emit a long-branch thunk. The target address
280
// of the callee is stored in a table to be accessed TOC-relative. Since the
281
// call must be local (a non-local call will have a PltCallStub instead) the
282
// table stores the address of the callee's local entry point. For
283
// position-independent code a corresponding relative dynamic relocation is
284
// used.
285
class PPC64LongBranchThunk : public Thunk {
286
public:
287
18
  uint32_t size() override { return 16; }
288
  void writeTo(uint8_t *buf) override;
289
  void addSymbols(ThunkSection &isec) override;
290
291
protected:
292
9
  PPC64LongBranchThunk(Symbol &dest) : Thunk(dest) {}
293
};
294
295
class PPC64PILongBranchThunk final : public PPC64LongBranchThunk {
296
public:
297
1
  PPC64PILongBranchThunk(Symbol &dest) : PPC64LongBranchThunk(dest) {
298
1
    assert(!dest.isPreemptible);
299
1
    if (dest.isInPPC64Branchlt())
300
0
      return;
301
1
302
1
    in.ppc64LongBranchTarget->addEntry(dest);
303
1
    mainPart->relaDyn->addReloc(
304
1
        {target->relativeRel, in.ppc64LongBranchTarget,
305
1
         dest.getPPC64LongBranchOffset(), true, &dest,
306
1
         getPPC64GlobalEntryToLocalEntryOffset(dest.stOther)});
307
1
  }
308
};
309
310
class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk {
311
public:
312
8
  PPC64PDLongBranchThunk(Symbol &dest) : PPC64LongBranchThunk(dest) {
313
8
    if (!dest.isInPPC64Branchlt())
314
8
      in.ppc64LongBranchTarget->addEntry(dest);
315
8
  }
316
};
317
318
} // end anonymous namespace
319
320
Defined *Thunk::addSymbol(StringRef name, uint8_t type, uint64_t value,
321
347
                          InputSectionBase &section) {
322
347
  Defined *d = addSyntheticLocal(name, type, value, /*size=*/0, section);
323
347
  syms.push_back(d);
324
347
  return d;
325
347
}
326
327
512
void Thunk::setOffset(uint64_t newOffset) {
328
512
  for (Defined *d : syms)
329
789
    d->value = d->value - offset + newOffset;
330
512
  offset = newOffset;
331
512
}
332
333
// AArch64 long range Thunks
334
335
9
static uint64_t getAArch64ThunkDestVA(const Symbol &s) {
336
9
  uint64_t v = s.isInPlt() ? 
s.getPltVA()1
:
s.getVA()8
;
337
9
  return v;
338
9
}
339
340
6
void AArch64ABSLongThunk::writeTo(uint8_t *buf) {
341
6
  const uint8_t data[] = {
342
6
    0x50, 0x00, 0x00, 0x58, //     ldr x16, L0
343
6
    0x00, 0x02, 0x1f, 0xd6, //     br  x16
344
6
    0x00, 0x00, 0x00, 0x00, // L0: .xword S
345
6
    0x00, 0x00, 0x00, 0x00,
346
6
  };
347
6
  uint64_t s = getAArch64ThunkDestVA(destination);
348
6
  memcpy(buf, data, sizeof(data));
349
6
  target->relocateOne(buf + 8, R_AARCH64_ABS64, s);
350
6
}
351
352
6
void AArch64ABSLongThunk::addSymbols(ThunkSection &isec) {
353
6
  addSymbol(saver.save("__AArch64AbsLongThunk_" + destination.getName()),
354
6
            STT_FUNC, 0, isec);
355
6
  addSymbol("$x", STT_NOTYPE, 0, isec);
356
6
  addSymbol("$d", STT_NOTYPE, 8, isec);
357
6
}
358
359
// This Thunk has a maximum range of 4Gb, this is sufficient for all programs
360
// using the small code model, including pc-relative ones. At time of writing
361
// clang and gcc do not support the large code model for position independent
362
// code so it is safe to use this for position independent thunks without
363
// worrying about the destination being more than 4Gb away.
364
3
void AArch64ADRPThunk::writeTo(uint8_t *buf) {
365
3
  const uint8_t data[] = {
366
3
      0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)
367
3
      0x10, 0x02, 0x00, 0x91, // add  x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)
368
3
      0x00, 0x02, 0x1f, 0xd6, // br   x16
369
3
  };
370
3
  uint64_t s = getAArch64ThunkDestVA(destination);
371
3
  uint64_t p = getThunkTargetSym()->getVA();
372
3
  memcpy(buf, data, sizeof(data));
373
3
  target->relocateOne(buf, R_AARCH64_ADR_PREL_PG_HI21,
374
3
                      getAArch64Page(s) - getAArch64Page(p));
375
3
  target->relocateOne(buf + 4, R_AARCH64_ADD_ABS_LO12_NC, s);
376
3
}
377
378
3
void AArch64ADRPThunk::addSymbols(ThunkSection &isec) {
379
3
  addSymbol(saver.save("__AArch64ADRPThunk_" + destination.getName()), STT_FUNC,
380
3
            0, isec);
381
3
  addSymbol("$x", STT_NOTYPE, 0, isec);
382
3
}
383
384
// ARM Target Thunks
385
278
static uint64_t getARMThunkDestVA(const Symbol &s) {
386
278
  uint64_t v = s.isInPlt() ? 
s.getPltVA()42
:
s.getVA()236
;
387
278
  return SignExtend64<32>(v);
388
278
}
389
390
// This function returns true if the target is not Thumb and is within 2^26, and
391
// it has not previously returned false (see comment for mayUseShortThunk).
392
93
bool ARMThunk::getMayUseShortThunk() {
393
93
  if (!mayUseShortThunk)
394
47
    return false;
395
46
  uint64_t s = getARMThunkDestVA(destination);
396
46
  if (s & 1) {
397
14
    mayUseShortThunk = false;
398
14
    return false;
399
14
  }
400
32
  uint64_t p = getThunkTargetSym()->getVA();
401
32
  int64_t offset = s - p - 8;
402
32
  mayUseShortThunk = llvm::isInt<26>(offset);
403
32
  return mayUseShortThunk;
404
32
}
405
406
30
void ARMThunk::writeTo(uint8_t *buf) {
407
30
  if (!getMayUseShortThunk()) {
408
23
    writeLong(buf);
409
23
    return;
410
23
  }
411
7
412
7
  uint64_t s = getARMThunkDestVA(destination);
413
7
  uint64_t p = getThunkTargetSym()->getVA();
414
7
  int64_t offset = s - p - 8;
415
7
  const uint8_t data[] = {
416
7
    0x00, 0x00, 0x00, 0xea, // b S
417
7
  };
418
7
  memcpy(buf, data, sizeof(data));
419
7
  target->relocateOne(buf, R_ARM_JUMP24, offset);
420
7
}
421
422
bool ARMThunk::isCompatibleWith(const InputSection &isec,
423
4
                                const Relocation &rel) const {
424
4
  // Thumb branch relocations can't use BLX
425
4
  return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
426
4
}
427
428
// This function returns true if the target is Thumb and is within 2^25, and
429
// it has not previously returned false (see comment for mayUseShortThunk).
430
243
bool ThumbThunk::getMayUseShortThunk() {
431
243
  if (!mayUseShortThunk)
432
120
    return false;
433
123
  uint64_t s = getARMThunkDestVA(destination);
434
123
  if ((s & 1) == 0) {
435
30
    mayUseShortThunk = false;
436
30
    return false;
437
30
  }
438
93
  uint64_t p = getThunkTargetSym()->getVA() & ~1;
439
93
  int64_t offset = s - p - 4;
440
93
  mayUseShortThunk = llvm::isInt<25>(offset);
441
93
  return mayUseShortThunk;
442
93
}
443
444
79
void ThumbThunk::writeTo(uint8_t *buf) {
445
79
  if (!getMayUseShortThunk()) {
446
58
    writeLong(buf);
447
58
    return;
448
58
  }
449
21
450
21
  uint64_t s = getARMThunkDestVA(destination);
451
21
  uint64_t p = getThunkTargetSym()->getVA();
452
21
  int64_t offset = s - p - 4;
453
21
  const uint8_t data[] = {
454
21
      0x00, 0xf0, 0x00, 0xb0, // b.w S
455
21
  };
456
21
  memcpy(buf, data, sizeof(data));
457
21
  target->relocateOne(buf, R_ARM_THM_JUMP24, offset);
458
21
}
459
460
bool ThumbThunk::isCompatibleWith(const InputSection &isec,
461
39
                                  const Relocation &rel) const {
462
39
  // ARM branch relocations can't use BLX
463
39
  return rel.type != R_ARM_JUMP24 && rel.type != R_ARM_PC24 && rel.type != R_ARM_PLT32;
464
39
}
465
466
13
void ARMV7ABSLongThunk::writeLong(uint8_t *buf) {
467
13
  const uint8_t data[] = {
468
13
      0x00, 0xc0, 0x00, 0xe3, // movw         ip,:lower16:S
469
13
      0x00, 0xc0, 0x40, 0xe3, // movt         ip,:upper16:S
470
13
      0x1c, 0xff, 0x2f, 0xe1, // bx   ip
471
13
  };
472
13
  uint64_t s = getARMThunkDestVA(destination);
473
13
  memcpy(buf, data, sizeof(data));
474
13
  target->relocateOne(buf, R_ARM_MOVW_ABS_NC, s);
475
13
  target->relocateOne(buf + 4, R_ARM_MOVT_ABS, s);
476
13
}
477
478
16
void ARMV7ABSLongThunk::addSymbols(ThunkSection &isec) {
479
16
  addSymbol(saver.save("__ARMv7ABSLongThunk_" + destination.getName()),
480
16
            STT_FUNC, 0, isec);
481
16
  addSymbol("$a", STT_NOTYPE, 0, isec);
482
16
}
483
484
32
void ThumbV7ABSLongThunk::writeLong(uint8_t *buf) {
485
32
  const uint8_t data[] = {
486
32
      0x40, 0xf2, 0x00, 0x0c, // movw         ip, :lower16:S
487
32
      0xc0, 0xf2, 0x00, 0x0c, // movt         ip, :upper16:S
488
32
      0x60, 0x47,             // bx   ip
489
32
  };
490
32
  uint64_t s = getARMThunkDestVA(destination);
491
32
  memcpy(buf, data, sizeof(data));
492
32
  target->relocateOne(buf, R_ARM_THM_MOVW_ABS_NC, s);
493
32
  target->relocateOne(buf + 4, R_ARM_THM_MOVT_ABS, s);
494
32
}
495
496
53
void ThumbV7ABSLongThunk::addSymbols(ThunkSection &isec) {
497
53
  addSymbol(saver.save("__Thumbv7ABSLongThunk_" + destination.getName()),
498
53
            STT_FUNC, 1, isec);
499
53
  addSymbol("$t", STT_NOTYPE, 0, isec);
500
53
}
501
502
7
void ARMV7PILongThunk::writeLong(uint8_t *buf) {
503
7
  const uint8_t data[] = {
504
7
      0xf0, 0xcf, 0x0f, 0xe3, // P:  movw ip,:lower16:S - (P + (L1-P) + 8)
505
7
      0x00, 0xc0, 0x40, 0xe3, //     movt ip,:upper16:S - (P + (L1-P) + 8)
506
7
      0x0f, 0xc0, 0x8c, 0xe0, // L1: add  ip, ip, pc
507
7
      0x1c, 0xff, 0x2f, 0xe1, //     bx   ip
508
7
  };
509
7
  uint64_t s = getARMThunkDestVA(destination);
510
7
  uint64_t p = getThunkTargetSym()->getVA();
511
7
  int64_t offset = s - p - 16;
512
7
  memcpy(buf, data, sizeof(data));
513
7
  target->relocateOne(buf, R_ARM_MOVW_PREL_NC, offset);
514
7
  target->relocateOne(buf + 4, R_ARM_MOVT_PREL, offset);
515
7
}
516
517
7
void ARMV7PILongThunk::addSymbols(ThunkSection &isec) {
518
7
  addSymbol(saver.save("__ARMV7PILongThunk_" + destination.getName()), STT_FUNC,
519
7
            0, isec);
520
7
  addSymbol("$a", STT_NOTYPE, 0, isec);
521
7
}
522
523
24
void ThumbV7PILongThunk::writeLong(uint8_t *buf) {
524
24
  const uint8_t data[] = {
525
24
      0x4f, 0xf6, 0xf4, 0x7c, // P:  movw ip,:lower16:S - (P + (L1-P) + 4)
526
24
      0xc0, 0xf2, 0x00, 0x0c, //     movt ip,:upper16:S - (P + (L1-P) + 4)
527
24
      0xfc, 0x44,             // L1: add  ip, pc
528
24
      0x60, 0x47,             //     bx   ip
529
24
  };
530
24
  uint64_t s = getARMThunkDestVA(destination);
531
24
  uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
532
24
  int64_t offset = s - p - 12;
533
24
  memcpy(buf, data, sizeof(data));
534
24
  target->relocateOne(buf, R_ARM_THM_MOVW_PREL_NC, offset);
535
24
  target->relocateOne(buf + 4, R_ARM_THM_MOVT_PREL, offset);
536
24
}
537
538
24
void ThumbV7PILongThunk::addSymbols(ThunkSection &isec) {
539
24
  addSymbol(saver.save("__ThumbV7PILongThunk_" + destination.getName()),
540
24
            STT_FUNC, 1, isec);
541
24
  addSymbol("$t", STT_NOTYPE, 0, isec);
542
24
}
543
544
2
void ARMV5ABSLongThunk::writeLong(uint8_t *buf) {
545
2
  const uint8_t data[] = {
546
2
      0x04, 0xf0, 0x1f, 0xe5, //     ldr pc, [pc,#-4] ; L1
547
2
      0x00, 0x00, 0x00, 0x00, // L1: .word S
548
2
  };
549
2
  memcpy(buf, data, sizeof(data));
550
2
  target->relocateOne(buf + 4, R_ARM_ABS32, getARMThunkDestVA(destination));
551
2
}
552
553
3
void ARMV5ABSLongThunk::addSymbols(ThunkSection &isec) {
554
3
  addSymbol(saver.save("__ARMv5ABSLongThunk_" + destination.getName()),
555
3
            STT_FUNC, 0, isec);
556
3
  addSymbol("$a", STT_NOTYPE, 0, isec);
557
3
  addSymbol("$d", STT_NOTYPE, 4, isec);
558
3
}
559
560
bool ARMV5ABSLongThunk::isCompatibleWith(const InputSection &isec,
561
1
                                         const Relocation &rel) const {
562
1
  // Thumb branch relocations can't use BLX
563
1
  return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
564
1
}
565
566
1
void ARMV5PILongThunk::writeLong(uint8_t *buf) {
567
1
  const uint8_t data[] = {
568
1
      0x04, 0xc0, 0x9f, 0xe5, // P:  ldr ip, [pc,#4] ; L2
569
1
      0x0c, 0xc0, 0x8f, 0xe0, // L1: add ip, pc, ip
570
1
      0x1c, 0xff, 0x2f, 0xe1, //     bx ip
571
1
      0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8)
572
1
  };
573
1
  uint64_t s = getARMThunkDestVA(destination);
574
1
  uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
575
1
  memcpy(buf, data, sizeof(data));
576
1
  target->relocateOne(buf + 12, R_ARM_REL32, s - p - 12);
577
1
}
578
579
4
void ARMV5PILongThunk::addSymbols(ThunkSection &isec) {
580
4
  addSymbol(saver.save("__ARMV5PILongThunk_" + destination.getName()), STT_FUNC,
581
4
            0, isec);
582
4
  addSymbol("$a", STT_NOTYPE, 0, isec);
583
4
  addSymbol("$d", STT_NOTYPE, 12, isec);
584
4
}
585
586
bool ARMV5PILongThunk::isCompatibleWith(const InputSection &isec,
587
2
                                        const Relocation &rel) const {
588
2
  // Thumb branch relocations can't use BLX
589
2
  return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
590
2
}
591
592
1
void ThumbV6MABSLongThunk::writeLong(uint8_t *buf) {
593
1
  // Most Thumb instructions cannot access the high registers r8 - r15. As the
594
1
  // only register we can corrupt is r12 we must instead spill a low register
595
1
  // to the stack to use as a scratch register. We push r1 even though we
596
1
  // don't need to get some space to use for the return address.
597
1
  const uint8_t data[] = {
598
1
      0x03, 0xb4,            // push {r0, r1} ; Obtain scratch registers
599
1
      0x01, 0x48,            // ldr r0, [pc, #4] ; L1
600
1
      0x01, 0x90,            // str r0, [sp, #4] ; SP + 4 = S
601
1
      0x01, 0xbd,            // pop {r0, pc} ; restore r0 and branch to dest
602
1
      0x00, 0x00, 0x00, 0x00 // L1: .word S
603
1
  };
604
1
  uint64_t s = getARMThunkDestVA(destination);
605
1
  memcpy(buf, data, sizeof(data));
606
1
  target->relocateOne(buf + 8, R_ARM_ABS32, s);
607
1
}
608
609
1
void ThumbV6MABSLongThunk::addSymbols(ThunkSection &isec) {
610
1
  addSymbol(saver.save("__Thumbv6MABSLongThunk_" + destination.getName()),
611
1
            STT_FUNC, 1, isec);
612
1
  addSymbol("$t", STT_NOTYPE, 0, isec);
613
1
  addSymbol("$d", STT_NOTYPE, 8, isec);
614
1
}
615
616
1
void ThumbV6MPILongThunk::writeLong(uint8_t *buf) {
617
1
  // Most Thumb instructions cannot access the high registers r8 - r15. As the
618
1
  // only register we can corrupt is ip (r12) we must instead spill a low
619
1
  // register to the stack to use as a scratch register.
620
1
  const uint8_t data[] = {
621
1
      0x01, 0xb4,             // P:  push {r0}        ; Obtain scratch register
622
1
      0x02, 0x48,             //     ldr r0, [pc, #8] ; L2
623
1
      0x84, 0x46,             //     mov ip, r0       ; high to low register
624
1
      0x01, 0xbc,             //     pop {r0}         ; restore scratch register
625
1
      0xe7, 0x44,             // L1: add pc, ip       ; transfer control
626
1
      0xc0, 0x46,             //     nop              ; pad to 4-byte boundary
627
1
      0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 4)
628
1
  };
629
1
  uint64_t s = getARMThunkDestVA(destination);
630
1
  uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
631
1
  memcpy(buf, data, sizeof(data));
632
1
  target->relocateOne(buf + 12, R_ARM_REL32, s - p - 12);
633
1
}
634
635
1
void ThumbV6MPILongThunk::addSymbols(ThunkSection &isec) {
636
1
  addSymbol(saver.save("__Thumbv6MPILongThunk_" + destination.getName()),
637
1
            STT_FUNC, 1, isec);
638
1
  addSymbol("$t", STT_NOTYPE, 0, isec);
639
1
  addSymbol("$d", STT_NOTYPE, 12, isec);
640
1
}
641
642
// Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
643
38
void MipsThunk::writeTo(uint8_t *buf) {
644
38
  uint64_t s = destination.getVA();
645
38
  write32(buf, 0x3c190000); // lui   $25, %hi(func)
646
38
  write32(buf + 4, 0x08000000 | (s >> 2)); // j     func
647
38
  write32(buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
648
38
  write32(buf + 12, 0x00000000); // nop
649
38
  target->relocateOne(buf, R_MIPS_HI16, s);
650
38
  target->relocateOne(buf + 8, R_MIPS_LO16, s);
651
38
}
652
653
38
void MipsThunk::addSymbols(ThunkSection &isec) {
654
38
  addSymbol(saver.save("__LA25Thunk_" + destination.getName()), STT_FUNC, 0,
655
38
            isec);
656
38
}
657
658
88
InputSection *MipsThunk::getTargetInputSection() const {
659
88
  auto &dr = cast<Defined>(destination);
660
88
  return dyn_cast<InputSection>(dr.section);
661
88
}
662
663
// Write microMIPS R2-R5 LA25 thunk code
664
// to call PIC function from the non-PIC one.
665
3
void MicroMipsThunk::writeTo(uint8_t *buf) {
666
3
  uint64_t s = destination.getVA();
667
3
  write16(buf, 0x41b9);       // lui   $25, %hi(func)
668
3
  write16(buf + 4, 0xd400);   // j     func
669
3
  write16(buf + 8, 0x3339);   // addiu $25, $25, %lo(func)
670
3
  write16(buf + 12, 0x0c00);  // nop
671
3
  target->relocateOne(buf, R_MICROMIPS_HI16, s);
672
3
  target->relocateOne(buf + 4, R_MICROMIPS_26_S1, s);
673
3
  target->relocateOne(buf + 8, R_MICROMIPS_LO16, s);
674
3
}
675
676
3
void MicroMipsThunk::addSymbols(ThunkSection &isec) {
677
3
  Defined *d = addSymbol(
678
3
      saver.save("__microLA25Thunk_" + destination.getName()), STT_FUNC, 0, isec);
679
3
  d->stOther |= STO_MIPS_MICROMIPS;
680
3
}
681
682
6
InputSection *MicroMipsThunk::getTargetInputSection() const {
683
6
  auto &dr = cast<Defined>(destination);
684
6
  return dyn_cast<InputSection>(dr.section);
685
6
}
686
687
// Write microMIPS R6 LA25 thunk code
688
// to call PIC function from the non-PIC one.
689
2
void MicroMipsR6Thunk::writeTo(uint8_t *buf) {
690
2
  uint64_t s = destination.getVA();
691
2
  uint64_t p = getThunkTargetSym()->getVA();
692
2
  write16(buf, 0x1320);       // lui   $25, %hi(func)
693
2
  write16(buf + 4, 0x3339);   // addiu $25, $25, %lo(func)
694
2
  write16(buf + 8, 0x9400);   // bc    func
695
2
  target->relocateOne(buf, R_MICROMIPS_HI16, s);
696
2
  target->relocateOne(buf + 4, R_MICROMIPS_LO16, s);
697
2
  target->relocateOne(buf + 8, R_MICROMIPS_PC26_S1, s - p - 12);
698
2
}
699
700
2
void MicroMipsR6Thunk::addSymbols(ThunkSection &isec) {
701
2
  Defined *d = addSymbol(
702
2
      saver.save("__microLA25Thunk_" + destination.getName()), STT_FUNC, 0, isec);
703
2
  d->stOther |= STO_MIPS_MICROMIPS;
704
2
}
705
706
4
InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
707
4
  auto &dr = cast<Defined>(destination);
708
4
  return dyn_cast<InputSection>(dr.section);
709
4
}
710
711
17
void PPC32PltCallStub::writeTo(uint8_t *buf) {
712
17
  if (!config->isPic) {
713
4
    uint64_t va = destination.getGotPltVA();
714
4
    write32(buf + 0, 0x3d600000 | (va + 0x8000) >> 16); // lis r11,ha
715
4
    write32(buf + 4, 0x816b0000 | (uint16_t)va);        // lwz r11,l(r11)
716
4
    write32(buf + 8, 0x7d6903a6);                       // mtctr r11
717
4
    write32(buf + 12, 0x4e800420);                      // bctr
718
4
    return;
719
4
  }
720
13
  uint32_t offset;
721
13
  if (addend >= 0x8000) {
722
8
    // The stub loads an address relative to r30 (.got2+Addend). Addend is
723
8
    // almost always 0x8000. The address of .got2 is different in another object
724
8
    // file, so a stub cannot be shared.
725
8
    offset = destination.getGotPltVA() - (in.ppc32Got2->getParent()->getVA() +
726
8
                                          file->ppc32Got2OutSecOff + addend);
727
8
  } else {
728
5
    // The stub loads an address relative to _GLOBAL_OFFSET_TABLE_ (which is
729
5
    // currently the address of .got).
730
5
    offset = destination.getGotPltVA() - in.got->getVA();
731
5
  }
732
13
  uint16_t ha = (offset + 0x8000) >> 16, l = (uint16_t)offset;
733
13
  if (ha == 0) {
734
6
    write32(buf + 0, 0x817e0000 | l); // lwz r11,l(r30)
735
6
    write32(buf + 4, 0x7d6903a6);     // mtctr r11
736
6
    write32(buf + 8, 0x4e800420);     // bctr
737
6
    write32(buf + 12, 0x60000000);    // nop
738
7
  } else {
739
7
    write32(buf + 0, 0x3d7e0000 | ha); // addis r11,r30,ha
740
7
    write32(buf + 4, 0x816b0000 | l);  // lwz r11,l(r11)
741
7
    write32(buf + 8, 0x7d6903a6);      // mtctr r11
742
7
    write32(buf + 12, 0x4e800420);     // bctr
743
7
  }
744
13
}
745
746
17
void PPC32PltCallStub::addSymbols(ThunkSection &isec) {
747
17
  std::string buf;
748
17
  raw_string_ostream os(buf);
749
17
  os << format_hex_no_prefix(addend, 8);
750
17
  if (!config->isPic)
751
4
    os << ".plt_call32.";
752
13
  else if (addend >= 0x8000)
753
8
    os << ".got2.plt_pic32.";
754
5
  else
755
5
    os << ".plt_pic32.";
756
17
  os << destination.getName();
757
17
  addSymbol(saver.save(os.str()), STT_FUNC, 0, isec);
758
17
}
759
760
bool PPC32PltCallStub::isCompatibleWith(const InputSection &isec,
761
17
                                        const Relocation &rel) const {
762
17
  return !config->isPic || 
(15
isec.file == file15
&&
rel.addend == addend11
);
763
17
}
764
765
36
static void writePPCLoadAndBranch(uint8_t *buf, int64_t offset) {
766
36
  uint16_t offHa = (offset + 0x8000) >> 16;
767
36
  uint16_t offLo = offset & 0xffff;
768
36
769
36
  write32(buf + 0, 0x3d820000 | offHa); // addis r12, r2, OffHa
770
36
  write32(buf + 4, 0xe98c0000 | offLo); // ld    r12, OffLo(r12)
771
36
  write32(buf + 8, 0x7d8903a6);         // mtctr r12
772
36
  write32(buf + 12, 0x4e800420);        // bctr
773
36
}
774
775
27
void PPC64PltCallStub::writeTo(uint8_t *buf) {
776
27
  int64_t offset = destination.getGotPltVA() - getPPC64TocBase();
777
27
  // Save the TOC pointer to the save-slot reserved in the call frame.
778
27
  write32(buf + 0, 0xf8410018); // std     r2,24(r1)
779
27
  writePPCLoadAndBranch(buf + 4, offset);
780
27
}
781
782
27
void PPC64PltCallStub::addSymbols(ThunkSection &isec) {
783
27
  Defined *s = addSymbol(saver.save("__plt_" + destination.getName()), STT_FUNC,
784
27
                         0, isec);
785
27
  s->needsTocRestore = true;
786
27
}
787
788
9
void PPC64LongBranchThunk::writeTo(uint8_t *buf) {
789
9
  int64_t offset = destination.getPPC64LongBranchTableVA() - getPPC64TocBase();
790
9
  writePPCLoadAndBranch(buf, offset);
791
9
}
792
793
9
void PPC64LongBranchThunk::addSymbols(ThunkSection &isec) {
794
9
  addSymbol(saver.save("__long_branch_" + destination.getName()), STT_FUNC, 0,
795
9
            isec);
796
9
}
797
798
214
Thunk::Thunk(Symbol &d) : destination(d), offset(0) {}
799
800
214
Thunk::~Thunk() = default;
801
802
9
static Thunk *addThunkAArch64(RelType type, Symbol &s) {
803
9
  if (type != R_AARCH64_CALL26 && 
type != R_AARCH64_JUMP261
)
804
0
    fatal("unrecognized relocation type");
805
9
  if (config->picThunk)
806
3
    return make<AArch64ADRPThunk>(s);
807
6
  return make<AArch64ABSLongThunk>(s);
808
6
}
809
810
// Creates a thunk for Thumb-ARM interworking.
811
// Arm Architectures v5 and v6 do not support Thumb2 technology. This means
812
// - MOVT and MOVW instructions cannot be used
813
// - Only Thumb relocation that can generate a Thunk is a BL, this can always
814
//   be transformed into a BLX
815
7
static Thunk *addThunkPreArmv7(RelType reloc, Symbol &s) {
816
7
  switch (reloc) {
817
7
  case R_ARM_PC24:
818
7
  case R_ARM_PLT32:
819
7
  case R_ARM_JUMP24:
820
7
  case R_ARM_CALL:
821
7
  case R_ARM_THM_CALL:
822
7
    if (config->picThunk)
823
4
      return make<ARMV5PILongThunk>(s);
824
3
    return make<ARMV5ABSLongThunk>(s);
825
0
  }
826
0
  fatal("relocation " + toString(reloc) + " to " + toString(s) +
827
0
        " not supported for Armv5 or Armv6 targets");
828
0
}
829
830
// Create a thunk for Thumb long branch on V6-M.
831
// Arm Architecture v6-M only supports Thumb instructions. This means
832
// - MOVT and MOVW instructions cannot be used.
833
// - Only a limited number of instructions can access registers r8 and above
834
// - No interworking support is needed (all Thumb).
835
2
static Thunk *addThunkV6M(RelType reloc, Symbol &s) {
836
2
  switch (reloc) {
837
2
  case R_ARM_THM_JUMP19:
838
2
  case R_ARM_THM_JUMP24:
839
2
  case R_ARM_THM_CALL:
840
2
    if (config->isPic)
841
1
      return make<ThumbV6MPILongThunk>(s);
842
1
    return make<ThumbV6MABSLongThunk>(s);
843
0
  }
844
0
  fatal("relocation " + toString(reloc) + " to " + toString(s) +
845
0
        " not supported for Armv6-M targets");
846
0
}
847
848
// Creates a thunk for Thumb-ARM interworking or branch range extension.
849
109
static Thunk *addThunkArm(RelType reloc, Symbol &s) {
850
109
  // Decide which Thunk is needed based on:
851
109
  // Available instruction set
852
109
  // - An Arm Thunk can only be used if Arm state is available.
853
109
  // - A Thumb Thunk can only be used if Thumb state is available.
854
109
  // - Can only use a Thunk if it uses instructions that the Target supports.
855
109
  // Relocation is branch or branch and link
856
109
  // - Branch instructions cannot change state, can only select Thunk that
857
109
  //   starts in the same state as the caller.
858
109
  // - Branch and link relocations can change state, can select Thunks from
859
109
  //   either Arm or Thumb.
860
109
  // Position independent Thunks if we require position independent code.
861
109
862
109
  // Handle architectures that have restrictions on the instructions that they
863
109
  // can use in Thunks. The flags below are set by reading the BuildAttributes
864
109
  // of the input objects. InputFiles.cpp contains the mapping from ARM
865
109
  // architecture to flag.
866
109
  if (!config->armHasMovtMovw) {
867
9
    if (!config->armJ1J2BranchEncoding)
868
7
      return addThunkPreArmv7(reloc, s);
869
2
    return addThunkV6M(reloc, s);
870
2
  }
871
100
872
100
  switch (reloc) {
873
100
  case R_ARM_PC24:
874
23
  case R_ARM_PLT32:
875
23
  case R_ARM_JUMP24:
876
23
  case R_ARM_CALL:
877
23
    if (config->picThunk)
878
7
      return make<ARMV7PILongThunk>(s);
879
16
    return make<ARMV7ABSLongThunk>(s);
880
77
  case R_ARM_THM_JUMP19:
881
77
  case R_ARM_THM_JUMP24:
882
77
  case R_ARM_THM_CALL:
883
77
    if (config->picThunk)
884
24
      return make<ThumbV7PILongThunk>(s);
885
53
    return make<ThumbV7ABSLongThunk>(s);
886
0
  }
887
0
  fatal("unrecognized relocation type");
888
0
}
889
890
43
static Thunk *addThunkMips(RelType type, Symbol &s) {
891
43
  if ((s.stOther & STO_MIPS_MICROMIPS) && 
isMipsR6()5
)
892
2
    return make<MicroMipsR6Thunk>(s);
893
41
  if (s.stOther & STO_MIPS_MICROMIPS)
894
3
    return make<MicroMipsThunk>(s);
895
38
  return make<MipsThunk>(s);
896
38
}
897
898
17
static Thunk *addThunkPPC32(const InputSection &isec, const Relocation &rel, Symbol &s) {
899
17
  assert((rel.type == R_PPC_REL24 || rel.type == R_PPC_PLTREL24) &&
900
17
         "unexpected relocation type for thunk");
901
17
  return make<PPC32PltCallStub>(isec, rel, s);
902
17
}
903
904
36
static Thunk *addThunkPPC64(RelType type, Symbol &s) {
905
36
  assert(type == R_PPC64_REL24 && "unexpected relocation type for thunk");
906
36
  if (s.isInPlt())
907
27
    return make<PPC64PltCallStub>(s);
908
9
909
9
  if (config->picThunk)
910
1
    return make<PPC64PILongBranchThunk>(s);
911
8
912
8
  return make<PPC64PDLongBranchThunk>(s);
913
8
}
914
915
214
Thunk *addThunk(const InputSection &isec, Relocation &rel) {
916
214
  Symbol &s = *rel.sym;
917
214
918
214
  if (config->emachine == EM_AARCH64)
919
9
    return addThunkAArch64(rel.type, s);
920
205
921
205
  if (config->emachine == EM_ARM)
922
109
    return addThunkArm(rel.type, s);
923
96
924
96
  if (config->emachine == EM_MIPS)
925
43
    return addThunkMips(rel.type, s);
926
53
927
53
  if (config->emachine == EM_PPC)
928
17
    return addThunkPPC32(isec, rel, s);
929
36
930
36
  if (config->emachine == EM_PPC64)
931
36
    return addThunkPPC64(rel.type, s);
932
0
933
0
  llvm_unreachable("add Thunk only supported for ARM, Mips and PowerPC");
934
0
}
935
936
} // end namespace elf
937
} // end namespace lld