Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/Arch/PPC.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- PPC.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
#include "OutputSections.h"
10
#include "Symbols.h"
11
#include "SyntheticSections.h"
12
#include "Target.h"
13
#include "lld/Common/ErrorHandler.h"
14
#include "llvm/Support/Endian.h"
15
16
using namespace llvm;
17
using namespace llvm::support::endian;
18
using namespace llvm::ELF;
19
using namespace lld;
20
using namespace lld::elf;
21
22
namespace {
23
class PPC final : public TargetInfo {
24
public:
25
  PPC();
26
  RelExpr getRelExpr(RelType type, const Symbol &s,
27
                     const uint8_t *loc) const override;
28
  RelType getDynRel(RelType type) const override;
29
  void writeGotHeader(uint8_t *buf) const override;
30
0
  void writePltHeader(uint8_t *buf) const override {
31
0
    llvm_unreachable("should call writePPC32GlinkSection() instead");
32
0
  }
33
  void writePlt(uint8_t *buf, uint64_t gotPltEntryAddr, uint64_t pltEntryAddr,
34
0
    int32_t index, unsigned relOff) const override {
35
0
    llvm_unreachable("should call writePPC32GlinkSection() instead");
36
0
  }
37
  void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
38
  bool needsThunk(RelExpr expr, RelType relocType, const InputFile *file,
39
                  uint64_t branchAddr, const Symbol &s) const override;
40
  uint32_t getThunkSectionSpacing() const override;
41
  bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
42
  void relocateOne(uint8_t *loc, RelType type, uint64_t val) const override;
43
  RelExpr adjustRelaxExpr(RelType type, const uint8_t *data,
44
                          RelExpr expr) const override;
45
  int getTlsGdRelaxSkip(RelType type) const override;
46
  void relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const override;
47
  void relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const override;
48
  void relaxTlsLdToLe(uint8_t *loc, RelType type, uint64_t val) const override;
49
  void relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const override;
50
};
51
} // namespace
52
53
35
static uint16_t lo(uint32_t v) { return v; }
54
61
static uint16_t ha(uint32_t v) { return (v + 0x8000) >> 16; }
55
56
4
static uint32_t readFromHalf16(const uint8_t *loc) {
57
4
  return read32(config->isLE ? 
loc0
: loc - 2);
58
4
}
59
60
10
static void writeFromHalf16(uint8_t *loc, uint32_t insn) {
61
10
  write32(config->isLE ? 
loc0
: loc - 2, insn);
62
10
}
63
64
8
void elf::writePPC32GlinkSection(uint8_t *buf, size_t numEntries) {
65
8
  // On PPC Secure PLT ABI, bl foo@plt jumps to a call stub, which loads an
66
8
  // absolute address from a specific .plt slot (usually called .got.plt on
67
8
  // other targets) and jumps there.
68
8
  //
69
8
  // a) With immediate binding (BIND_NOW), the .plt entry is resolved at load
70
8
  // time. The .glink section is not used.
71
8
  // b) With lazy binding, the .plt entry points to a `b PLTresolve`
72
8
  // instruction in .glink, filled in by PPC::writeGotPlt().
73
8
74
8
  // Write N `b PLTresolve` first.
75
21
  for (size_t i = 0; i != numEntries; 
++i13
)
76
13
    write32(buf + 4 * i, 0x48000000 | 4 * (numEntries - i));
77
8
  buf += 4 * numEntries;
78
8
79
8
  // Then write PLTresolve(), which has two forms: PIC and non-PIC. PLTresolve()
80
8
  // computes the PLT index (by computing the distance from the landing b to
81
8
  // itself) and calls _dl_runtime_resolve() (in glibc).
82
8
  uint32_t got = in.got->getVA();
83
8
  uint32_t glink = in.plt->getVA(); // VA of .glink
84
8
  const uint8_t *end = buf + 64;
85
8
  if (config->isPic) {
86
5
    uint32_t afterBcl = in.plt->getSize() - target->pltHeaderSize + 12;
87
5
    uint32_t gotBcl = got + 4 - (glink + afterBcl);
88
5
    write32(buf + 0, 0x3d6b0000 | ha(afterBcl));  // addis r11,r11,1f-glink@ha
89
5
    write32(buf + 4, 0x7c0802a6);                 // mflr r0
90
5
    write32(buf + 8, 0x429f0005);                 // bcl 20,30,.+4
91
5
    write32(buf + 12, 0x396b0000 | lo(afterBcl)); // 1: addi r11,r11,1b-.glink@l
92
5
    write32(buf + 16, 0x7d8802a6);                // mflr r12
93
5
    write32(buf + 20, 0x7c0803a6);                // mtlr r0
94
5
    write32(buf + 24, 0x7d6c5850);                // sub r11,r11,r12
95
5
    write32(buf + 28, 0x3d8c0000 | ha(gotBcl));   // addis 12,12,GOT+4-1b@ha
96
5
    if (ha(gotBcl) == ha(gotBcl + 4)) {
97
5
      write32(buf + 32, 0x800c0000 | lo(gotBcl)); // lwz r0,r12,GOT+4-1b@l(r12)
98
5
      write32(buf + 36,
99
5
              0x818c0000 | lo(gotBcl + 4));       // lwz r12,r12,GOT+8-1b@l(r12)
100
5
    } else {
101
0
      write32(buf + 32, 0x840c0000 | lo(gotBcl)); // lwzu r0,r12,GOT+4-1b@l(r12)
102
0
      write32(buf + 36, 0x818c0000 | 4);          // lwz r12,r12,4(r12)
103
0
    }
104
5
    write32(buf + 40, 0x7c0903a6);                // mtctr 0
105
5
    write32(buf + 44, 0x7c0b5a14);                // add r0,11,11
106
5
    write32(buf + 48, 0x7d605a14);                // add r11,0,11
107
5
    write32(buf + 52, 0x4e800420);                // bctr
108
5
    buf += 56;
109
5
  } else {
110
3
    write32(buf + 0, 0x3d800000 | ha(got + 4));   // lis     r12,GOT+4@ha
111
3
    write32(buf + 4, 0x3d6b0000 | ha(-glink));    // addis   r11,r11,-Glink@ha
112
3
    if (ha(got + 4) == ha(got + 8))
113
3
      write32(buf + 8, 0x800c0000 | lo(got + 4)); // lwz r0,GOT+4@l(r12)
114
0
    else
115
0
      write32(buf + 8, 0x840c0000 | lo(got + 4)); // lwzu r0,GOT+4@l(r12)
116
3
    write32(buf + 12, 0x396b0000 | lo(-glink));   // addi    r11,r11,-Glink@l
117
3
    write32(buf + 16, 0x7c0903a6);                // mtctr   r0
118
3
    write32(buf + 20, 0x7c0b5a14);                // add     r0,r11,r11
119
3
    if (ha(got + 4) == ha(got + 8))
120
3
      write32(buf + 24, 0x818c0000 | lo(got + 8)); // lwz r12,GOT+8@ha(r12)
121
0
    else
122
0
      write32(buf + 24, 0x818c0000 | 4);          // lwz r12,4(r12)
123
3
    write32(buf + 28, 0x7d605a14);                // add     r11,r0,r11
124
3
    write32(buf + 32, 0x4e800420);                // bctr
125
3
    buf += 36;
126
3
  }
127
8
128
8
  // Pad with nop. They should not be executed.
129
39
  for (; buf < end; 
buf += 431
)
130
31
    write32(buf, 0x60000000);
131
8
}
132
133
31
PPC::PPC() {
134
31
  gotRel = R_PPC_GLOB_DAT;
135
31
  noneRel = R_PPC_NONE;
136
31
  pltRel = R_PPC_JMP_SLOT;
137
31
  relativeRel = R_PPC_RELATIVE;
138
31
  iRelativeRel = R_PPC_IRELATIVE;
139
31
  symbolicRel = R_PPC_ADDR32;
140
31
  gotBaseSymInGotPlt = false;
141
31
  gotHeaderEntriesNum = 3;
142
31
  gotPltHeaderEntriesNum = 0;
143
31
  pltHeaderSize = 64; // size of PLTresolve in .glink
144
31
  pltEntrySize = 4;
145
31
146
31
  needsThunks = true;
147
31
148
31
  tlsModuleIndexRel = R_PPC_DTPMOD32;
149
31
  tlsOffsetRel = R_PPC_DTPREL32;
150
31
  tlsGotRel = R_PPC_TPREL32;
151
31
152
31
  defaultMaxPageSize = 65536;
153
31
  defaultImageBase = 0x10000000;
154
31
155
31
  write32(trapInstr.data(), 0x7fe00008);
156
31
}
157
158
8
void PPC::writeGotHeader(uint8_t *buf) const {
159
8
  // _GLOBAL_OFFSET_TABLE_[0] = _DYNAMIC
160
8
  // glibc stores _dl_runtime_resolve in _GLOBAL_OFFSET_TABLE_[1],
161
8
  // link_map in _GLOBAL_OFFSET_TABLE_[2].
162
8
  write32(buf, mainPart->dynamic->getVA());
163
8
}
164
165
13
void PPC::writeGotPlt(uint8_t *buf, const Symbol &s) const {
166
13
  // Address of the symbol resolver stub in .glink .
167
13
  write32(buf, in.plt->getVA() + 4 * s.pltIndex);
168
13
}
169
170
bool PPC::needsThunk(RelExpr expr, RelType type, const InputFile *file,
171
134
                     uint64_t branchAddr, const Symbol &s) const {
172
134
  if (type != R_PPC_REL24 && 
type != R_PPC_PLTREL24119
)
173
100
    return false;
174
34
  if (s.isInPlt())
175
28
    return true;
176
6
  if (s.isUndefWeak())
177
2
    return false;
178
4
  return !(expr == R_PC && PPC::inBranchRange(type, branchAddr, s.getVA()));
179
4
}
180
181
62
uint32_t PPC::getThunkSectionSpacing() const { return 0x2000000; }
182
183
60
bool PPC::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
184
60
  uint64_t offset = dst - src;
185
60
  if (type == R_PPC_REL24 || 
type == R_PPC_PLTREL2435
)
186
60
    return isInt<26>(offset);
187
0
  llvm_unreachable("unsupported relocation type used in branch");
188
0
}
189
190
RelExpr PPC::getRelExpr(RelType type, const Symbol &s,
191
131
                        const uint8_t *loc) const {
192
131
  switch (type) {
193
131
  case R_PPC_DTPREL16:
194
14
  case R_PPC_DTPREL16_HA:
195
14
  case R_PPC_DTPREL16_HI:
196
14
  case R_PPC_DTPREL16_LO:
197
14
  case R_PPC_DTPREL32:
198
14
    return R_DTPREL;
199
14
  case R_PPC_REL14:
200
11
  case R_PPC_REL32:
201
11
  case R_PPC_LOCAL24PC:
202
11
  case R_PPC_REL16_LO:
203
11
  case R_PPC_REL16_HI:
204
11
  case R_PPC_REL16_HA:
205
11
    return R_PC;
206
11
  case R_PPC_GOT16:
207
2
    return R_GOT_OFF;
208
15
  case R_PPC_REL24:
209
15
    return R_PLT_PC;
210
19
  case R_PPC_PLTREL24:
211
19
    return R_PPC32_PLTREL;
212
11
  case R_PPC_GOT_TLSGD16:
213
9
    return R_TLSGD_GOT;
214
11
  case R_PPC_GOT_TLSLD16:
215
4
    return R_TLSLD_GOT;
216
11
  case R_PPC_GOT_TPREL16:
217
4
    return R_GOT_OFF;
218
14
  case R_PPC_TLS:
219
14
    return R_TLSIE_HINT;
220
11
  case R_PPC_TLSGD:
221
9
    return R_TLSDESC_CALL;
222
11
  case R_PPC_TLSLD:
223
4
    return R_TLSLD_HINT;
224
11
  case R_PPC_TPREL16:
225
4
  case R_PPC_TPREL16_HA:
226
4
  case R_PPC_TPREL16_LO:
227
4
  case R_PPC_TPREL16_HI:
228
4
    return R_TLS;
229
22
  default:
230
22
    return R_ABS;
231
131
  }
232
131
}
233
234
9
RelType PPC::getDynRel(RelType type) const {
235
9
  if (type == R_PPC_ADDR32)
236
9
    return type;
237
0
  return R_PPC_NONE;
238
0
}
239
240
88
static std::pair<RelType, uint64_t> fromDTPREL(RelType type, uint64_t val) {
241
88
  uint64_t dtpBiasedVal = val - 0x8000;
242
88
  switch (type) {
243
88
  case R_PPC_DTPREL16:
244
2
    return {R_PPC64_ADDR16, dtpBiasedVal};
245
88
  case R_PPC_DTPREL16_HA:
246
6
    return {R_PPC_ADDR16_HA, dtpBiasedVal};
247
88
  case R_PPC_DTPREL16_HI:
248
0
    return {R_PPC_ADDR16_HI, dtpBiasedVal};
249
88
  case R_PPC_DTPREL16_LO:
250
6
    return {R_PPC_ADDR16_LO, dtpBiasedVal};
251
88
  case R_PPC_DTPREL32:
252
0
    return {R_PPC_ADDR32, dtpBiasedVal};
253
88
  default:
254
74
    return {type, val};
255
88
  }
256
88
}
257
258
88
void PPC::relocateOne(uint8_t *loc, RelType type, uint64_t val) const {
259
88
  RelType newType;
260
88
  std::tie(newType, val) = fromDTPREL(type, val);
261
88
  switch (newType) {
262
88
  case R_PPC_ADDR16:
263
2
    checkIntUInt(loc, val, 16, type);
264
2
    write16(loc, val);
265
2
    break;
266
88
  case R_PPC_GOT16:
267
11
  case R_PPC_GOT_TLSGD16:
268
11
  case R_PPC_GOT_TLSLD16:
269
11
  case R_PPC_GOT_TPREL16:
270
11
  case R_PPC_TPREL16:
271
11
    checkInt(loc, val, 16, type);
272
11
    write16(loc, val);
273
11
    break;
274
17
  case R_PPC_ADDR16_HA:
275
17
  case R_PPC_DTPREL16_HA:
276
17
  case R_PPC_GOT_TLSGD16_HA:
277
17
  case R_PPC_GOT_TLSLD16_HA:
278
17
  case R_PPC_GOT_TPREL16_HA:
279
17
  case R_PPC_REL16_HA:
280
17
  case R_PPC_TPREL16_HA:
281
17
    write16(loc, ha(val));
282
17
    break;
283
17
  case R_PPC_ADDR16_HI:
284
1
  case R_PPC_DTPREL16_HI:
285
1
  case R_PPC_GOT_TLSGD16_HI:
286
1
  case R_PPC_GOT_TLSLD16_HI:
287
1
  case R_PPC_GOT_TPREL16_HI:
288
1
  case R_PPC_REL16_HI:
289
1
  case R_PPC_TPREL16_HI:
290
1
    write16(loc, val >> 16);
291
1
    break;
292
17
  case R_PPC_ADDR16_LO:
293
17
  case R_PPC_DTPREL16_LO:
294
17
  case R_PPC_GOT_TLSGD16_LO:
295
17
  case R_PPC_GOT_TLSLD16_LO:
296
17
  case R_PPC_GOT_TPREL16_LO:
297
17
  case R_PPC_REL16_LO:
298
17
  case R_PPC_TPREL16_LO:
299
17
    write16(loc, val);
300
17
    break;
301
17
  case R_PPC_ADDR32:
302
5
  case R_PPC_REL32:
303
5
    write32(loc, val);
304
5
    break;
305
5
  case R_PPC_REL14: {
306
0
    uint32_t mask = 0x0000FFFC;
307
0
    checkInt(loc, val, 16, type);
308
0
    checkAlignment(loc, val, 4, type);
309
0
    write32(loc, (read32(loc) & ~mask) | (val & mask));
310
0
    break;
311
5
  }
312
35
  case R_PPC_REL24:
313
35
  case R_PPC_LOCAL24PC:
314
35
  case R_PPC_PLTREL24: {
315
35
    uint32_t mask = 0x03FFFFFC;
316
35
    checkInt(loc, val, 26, type);
317
35
    checkAlignment(loc, val, 4, type);
318
35
    write32(loc, (read32(loc) & ~mask) | (val & mask));
319
35
    break;
320
35
  }
321
35
  default:
322
0
    error(getErrorLocation(loc) + "unrecognized relocation " + toString(type));
323
88
  }
324
88
}
325
326
RelExpr PPC::adjustRelaxExpr(RelType type, const uint8_t *data,
327
23
                             RelExpr expr) const {
328
23
  if (expr == R_RELAX_TLS_GD_TO_IE)
329
4
    return R_RELAX_TLS_GD_TO_IE_GOT_OFF;
330
19
  if (expr == R_RELAX_TLS_LD_TO_LE)
331
11
    return R_RELAX_TLS_LD_TO_LE_ABS;
332
8
  return expr;
333
8
}
334
335
16
int PPC::getTlsGdRelaxSkip(RelType type) const {
336
16
  // A __tls_get_addr call instruction is marked with 2 relocations:
337
16
  //
338
16
  //   R_PPC_TLSGD / R_PPC_TLSLD: marker relocation
339
16
  //   R_PPC_REL24: __tls_get_addr
340
16
  //
341
16
  // After the relaxation we no longer call __tls_get_addr and should skip both
342
16
  // relocations to not create a false dependence on __tls_get_addr being
343
16
  // defined.
344
16
  if (type == R_PPC_TLSGD || 
type == R_PPC_TLSLD10
)
345
8
    return 2;
346
8
  return 1;
347
8
}
348
349
4
void PPC::relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const {
350
4
  switch (type) {
351
4
  case R_PPC_GOT_TLSGD16: {
352
2
    // addi rT, rA, x@got@tlsgd --> lwz rT, x@got@tprel(rA)
353
2
    uint32_t insn = readFromHalf16(loc);
354
2
    writeFromHalf16(loc, 0x80000000 | (insn & 0x03ff0000));
355
2
    relocateOne(loc, R_PPC_GOT_TPREL16, val);
356
2
    break;
357
4
  }
358
4
  case R_PPC_TLSGD:
359
2
    // bl __tls_get_addr(x@tldgd) --> add r3, r3, r2
360
2
    write32(loc, 0x7c631214);
361
2
    break;
362
4
  default:
363
0
    llvm_unreachable("unsupported relocation for TLS GD to IE relaxation");
364
4
  }
365
4
}
366
367
8
void PPC::relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const {
368
8
  switch (type) {
369
8
  case R_PPC_GOT_TLSGD16:
370
4
    // addi r3, r31, x@got@tlsgd --> addis r3, r2, x@tprel@ha
371
4
    writeFromHalf16(loc, 0x3c620000 | ha(val));
372
4
    break;
373
8
  case R_PPC_TLSGD:
374
4
    // bl __tls_get_addr(x@tldgd) --> add r3, r3, x@tprel@l
375
4
    write32(loc, 0x38630000 | lo(val));
376
4
    break;
377
8
  default:
378
0
    llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
379
8
  }
380
8
}
381
382
11
void PPC::relaxTlsLdToLe(uint8_t *loc, RelType type, uint64_t val) const {
383
11
  switch (type) {
384
11
  case R_PPC_GOT_TLSLD16:
385
2
    // addi r3, rA, x@got@tlsgd --> addis r3, r2, 0
386
2
    writeFromHalf16(loc, 0x3c620000);
387
2
    break;
388
11
  case R_PPC_TLSLD:
389
2
    // r3+x@dtprel computes r3+x-0x8000, while we want it to compute r3+x@tprel
390
2
    // = r3+x-0x7000, so add 4096 to r3.
391
2
    // bl __tls_get_addr(x@tlsld) --> addi r3, r3, 4096
392
2
    write32(loc, 0x38631000);
393
2
    break;
394
11
  case R_PPC_DTPREL16:
395
7
  case R_PPC_DTPREL16_HA:
396
7
  case R_PPC_DTPREL16_HI:
397
7
  case R_PPC_DTPREL16_LO:
398
7
    relocateOne(loc, type, val);
399
7
    break;
400
7
  default:
401
0
    llvm_unreachable("unsupported relocation for TLS LD to LE relaxation");
402
11
  }
403
11
}
404
405
9
void PPC::relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const {
406
9
  switch (type) {
407
9
  case R_PPC_GOT_TPREL16: {
408
2
    // lwz rT, x@got@tprel(rA) --> addis rT, r2, x@tprel@ha
409
2
    uint32_t rt = readFromHalf16(loc) & 0x03e00000;
410
2
    writeFromHalf16(loc, 0x3c020000 | rt | ha(val));
411
2
    break;
412
9
  }
413
9
  case R_PPC_TLS: {
414
7
    uint32_t insn = read32(loc);
415
7
    if (insn >> 26 != 31)
416
0
      error("unrecognized instruction for IE to LE R_PPC_TLS");
417
7
    // addi rT, rT, x@tls --> addi rT, rT, x@tprel@l
418
7
    uint32_t dFormOp = getPPCDFormOp((read32(loc) & 0x000007fe) >> 1);
419
7
    if (dFormOp == 0)
420
0
      error("unrecognized instruction for IE to LE R_PPC_TLS");
421
7
    write32(loc, (dFormOp << 26) | (insn & 0x03ff0000) | lo(val));
422
7
    break;
423
9
  }
424
9
  default:
425
0
    llvm_unreachable("unsupported relocation for TLS IE to LE relaxation");
426
9
  }
427
9
}
428
429
62
TargetInfo *elf::getPPCTargetInfo() {
430
62
  static PPC target;
431
62
  return &target;
432
62
}