Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/PowerPC/MCTargetDesc/PPCELFObjectWriter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- PPCELFObjectWriter.cpp - PPC ELF Writer ---------------------------===//
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 "MCTargetDesc/PPCFixupKinds.h"
10
#include "MCTargetDesc/PPCMCExpr.h"
11
#include "MCTargetDesc/PPCMCTargetDesc.h"
12
#include "llvm/ADT/STLExtras.h"
13
#include "llvm/MC/MCELFObjectWriter.h"
14
#include "llvm/MC/MCExpr.h"
15
#include "llvm/MC/MCObjectWriter.h"
16
#include "llvm/MC/MCSymbolELF.h"
17
#include "llvm/MC/MCValue.h"
18
#include "llvm/Support/ErrorHandling.h"
19
20
using namespace llvm;
21
22
namespace {
23
  class PPCELFObjectWriter : public MCELFObjectTargetWriter {
24
  public:
25
    PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI);
26
27
  protected:
28
    unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
29
                          const MCFixup &Fixup, bool IsPCRel) const override;
30
31
    bool needsRelocateWithSymbol(const MCSymbol &Sym,
32
                                 unsigned Type) const override;
33
  };
34
}
35
36
PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI)
37
  : MCELFObjectTargetWriter(Is64Bit, OSABI,
38
                            Is64Bit ?  ELF::EM_PPC64 : ELF::EM_PPC,
39
2.05k
                            /*HasRelocationAddend*/ true) {}
40
41
static MCSymbolRefExpr::VariantKind getAccessVariant(const MCValue &Target,
42
1.36k
                                                     const MCFixup &Fixup) {
43
1.36k
  const MCExpr *Expr = Fixup.getValue();
44
1.36k
45
1.36k
  if (Expr->getKind() != MCExpr::Target)
46
1.07k
    return Target.getAccessVariant();
47
292
48
292
  switch (cast<PPCMCExpr>(Expr)->getKind()) {
49
292
  case PPCMCExpr::VK_PPC_None:
50
0
    return MCSymbolRefExpr::VK_None;
51
292
  case PPCMCExpr::VK_PPC_LO:
52
133
    return MCSymbolRefExpr::VK_PPC_LO;
53
292
  case PPCMCExpr::VK_PPC_HI:
54
11
    return MCSymbolRefExpr::VK_PPC_HI;
55
292
  case PPCMCExpr::VK_PPC_HA:
56
128
    return MCSymbolRefExpr::VK_PPC_HA;
57
292
  case PPCMCExpr::VK_PPC_HIGH:
58
2
    return MCSymbolRefExpr::VK_PPC_HIGH;
59
292
  case PPCMCExpr::VK_PPC_HIGHA:
60
2
    return MCSymbolRefExpr::VK_PPC_HIGHA;
61
292
  case PPCMCExpr::VK_PPC_HIGHERA:
62
4
    return MCSymbolRefExpr::VK_PPC_HIGHERA;
63
292
  case PPCMCExpr::VK_PPC_HIGHER:
64
4
    return MCSymbolRefExpr::VK_PPC_HIGHER;
65
292
  case PPCMCExpr::VK_PPC_HIGHEST:
66
4
    return MCSymbolRefExpr::VK_PPC_HIGHEST;
67
292
  case PPCMCExpr::VK_PPC_HIGHESTA:
68
4
    return MCSymbolRefExpr::VK_PPC_HIGHESTA;
69
0
  }
70
0
  llvm_unreachable("unknown PPCMCExpr kind");
71
0
}
72
73
unsigned PPCELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
74
                                          const MCFixup &Fixup,
75
1.36k
                                          bool IsPCRel) const {
76
1.36k
  MCSymbolRefExpr::VariantKind Modifier = getAccessVariant(Target, Fixup);
77
1.36k
78
1.36k
  // determine the type of the relocation
79
1.36k
  unsigned Type;
80
1.36k
  if (IsPCRel) {
81
447
    switch ((unsigned)Fixup.getKind()) {
82
447
    default:
83
0
      llvm_unreachable("Unimplemented");
84
447
    case PPC::fixup_ppc_br24:
85
161
    case PPC::fixup_ppc_br24abs:
86
161
      switch (Modifier) {
87
161
      
default: 0
llvm_unreachable0
("Unsupported Modifier");
88
161
      case MCSymbolRefExpr::VK_None:
89
147
        Type = ELF::R_PPC_REL24;
90
147
        break;
91
161
      case MCSymbolRefExpr::VK_PLT:
92
12
        Type = ELF::R_PPC_PLTREL24;
93
12
        break;
94
161
      case MCSymbolRefExpr::VK_PPC_LOCAL:
95
2
        Type = ELF::R_PPC_LOCAL24PC;
96
2
        break;
97
161
      }
98
161
      break;
99
161
    case PPC::fixup_ppc_brcond14:
100
2
    case PPC::fixup_ppc_brcond14abs:
101
2
      Type = ELF::R_PPC_REL14;
102
2
      break;
103
229
    case PPC::fixup_ppc_half16:
104
229
      switch (Modifier) {
105
229
      
default: 0
llvm_unreachable0
("Unsupported Modifier");
106
229
      case MCSymbolRefExpr::VK_None:
107
2
        Type = ELF::R_PPC_REL16;
108
2
        break;
109
229
      case MCSymbolRefExpr::VK_PPC_LO:
110
112
        Type = ELF::R_PPC_REL16_LO;
111
112
        break;
112
229
      case MCSymbolRefExpr::VK_PPC_HI:
113
2
        Type = ELF::R_PPC_REL16_HI;
114
2
        break;
115
229
      case MCSymbolRefExpr::VK_PPC_HA:
116
113
        Type = ELF::R_PPC_REL16_HA;
117
113
        break;
118
229
      }
119
229
      break;
120
229
    case PPC::fixup_ppc_half16ds:
121
1
      Target.print(errs());
122
1
      errs() << '\n';
123
1
      report_fatal_error("Invalid PC-relative half16ds relocation");
124
229
    case FK_Data_4:
125
47
    case FK_PCRel_4:
126
47
      Type = ELF::R_PPC_REL32;
127
47
      break;
128
47
    case FK_Data_8:
129
7
    case FK_PCRel_8:
130
7
      Type = ELF::R_PPC64_REL64;
131
7
      break;
132
916
    }
133
916
  } else {
134
916
    switch ((unsigned)Fixup.getKind()) {
135
916
      
default: 0
llvm_unreachable0
("invalid fixup kind!");
136
916
    case FK_NONE:
137
9
      Type = ELF::R_PPC_NONE;
138
9
      break;
139
916
    case PPC::fixup_ppc_br24abs:
140
2
      Type = ELF::R_PPC_ADDR24;
141
2
      break;
142
916
    case PPC::fixup_ppc_brcond14abs:
143
2
      Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_
144
2
      break;
145
916
    case PPC::fixup_ppc_half16:
146
497
      switch (Modifier) {
147
497
      
default: 0
llvm_unreachable0
("Unsupported Modifier");
148
497
      case MCSymbolRefExpr::VK_None:
149
2
        Type = ELF::R_PPC_ADDR16;
150
2
        break;
151
497
      case MCSymbolRefExpr::VK_PPC_LO:
152
19
        Type = ELF::R_PPC_ADDR16_LO;
153
19
        break;
154
497
      case MCSymbolRefExpr::VK_PPC_HI:
155
9
        Type = ELF::R_PPC_ADDR16_HI;
156
9
        break;
157
497
      case MCSymbolRefExpr::VK_PPC_HA:
158
15
        Type = ELF::R_PPC_ADDR16_HA;
159
15
        break;
160
497
      case MCSymbolRefExpr::VK_PPC_HIGH:
161
2
        Type = ELF::R_PPC64_ADDR16_HIGH;
162
2
        break;
163
497
      case MCSymbolRefExpr::VK_PPC_HIGHA:
164
2
        Type = ELF::R_PPC64_ADDR16_HIGHA;
165
2
        break;
166
497
      case MCSymbolRefExpr::VK_PPC_HIGHER:
167
4
        Type = ELF::R_PPC64_ADDR16_HIGHER;
168
4
        break;
169
497
      case MCSymbolRefExpr::VK_PPC_HIGHERA:
170
4
        Type = ELF::R_PPC64_ADDR16_HIGHERA;
171
4
        break;
172
497
      case MCSymbolRefExpr::VK_PPC_HIGHEST:
173
4
        Type = ELF::R_PPC64_ADDR16_HIGHEST;
174
4
        break;
175
497
      case MCSymbolRefExpr::VK_PPC_HIGHESTA:
176
4
        Type = ELF::R_PPC64_ADDR16_HIGHESTA;
177
4
        break;
178
497
      case MCSymbolRefExpr::VK_GOT:
179
10
        Type = ELF::R_PPC_GOT16;
180
10
        break;
181
497
      case MCSymbolRefExpr::VK_PPC_GOT_LO:
182
12
        Type = ELF::R_PPC_GOT16_LO;
183
12
        break;
184
497
      case MCSymbolRefExpr::VK_PPC_GOT_HI:
185
8
        Type = ELF::R_PPC_GOT16_HI;
186
8
        break;
187
497
      case MCSymbolRefExpr::VK_PPC_GOT_HA:
188
8
        Type = ELF::R_PPC_GOT16_HA;
189
8
        break;
190
497
      case MCSymbolRefExpr::VK_PPC_TOC:
191
0
        Type = ELF::R_PPC64_TOC16;
192
0
        break;
193
497
      case MCSymbolRefExpr::VK_PPC_TOC_LO:
194
30
        Type = ELF::R_PPC64_TOC16_LO;
195
30
        break;
196
497
      case MCSymbolRefExpr::VK_PPC_TOC_HI:
197
4
        Type = ELF::R_PPC64_TOC16_HI;
198
4
        break;
199
497
      case MCSymbolRefExpr::VK_PPC_TOC_HA:
200
97
        Type = ELF::R_PPC64_TOC16_HA;
201
97
        break;
202
497
      case MCSymbolRefExpr::VK_TPREL:
203
5
        Type = ELF::R_PPC_TPREL16;
204
5
        break;
205
497
      case MCSymbolRefExpr::VK_PPC_TPREL_LO:
206
14
        Type = ELF::R_PPC_TPREL16_LO;
207
14
        break;
208
497
      case MCSymbolRefExpr::VK_PPC_TPREL_HI:
209
6
        Type = ELF::R_PPC_TPREL16_HI;
210
6
        break;
211
497
      case MCSymbolRefExpr::VK_PPC_TPREL_HA:
212
9
        Type = ELF::R_PPC_TPREL16_HA;
213
9
        break;
214
497
      case MCSymbolRefExpr::VK_PPC_TPREL_HIGH:
215
4
        Type = ELF::R_PPC64_TPREL16_HIGH;
216
4
        break;
217
497
      case MCSymbolRefExpr::VK_PPC_TPREL_HIGHA:
218
4
        Type = ELF::R_PPC64_TPREL16_HIGHA;
219
4
        break;
220
497
      case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
221
5
        Type = ELF::R_PPC64_TPREL16_HIGHER;
222
5
        break;
223
497
      case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
224
5
        Type = ELF::R_PPC64_TPREL16_HIGHERA;
225
5
        break;
226
497
      case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
227
5
        Type = ELF::R_PPC64_TPREL16_HIGHEST;
228
5
        break;
229
497
      case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
230
5
        Type = ELF::R_PPC64_TPREL16_HIGHESTA;
231
5
        break;
232
497
      case MCSymbolRefExpr::VK_DTPREL:
233
7
        Type = ELF::R_PPC64_DTPREL16;
234
7
        break;
235
497
      case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
236
19
        Type = ELF::R_PPC64_DTPREL16_LO;
237
19
        break;
238
497
      case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
239
6
        Type = ELF::R_PPC64_DTPREL16_HI;
240
6
        break;
241
497
      case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
242
15
        Type = ELF::R_PPC64_DTPREL16_HA;
243
15
        break;
244
497
      case MCSymbolRefExpr::VK_PPC_DTPREL_HIGH:
245
4
        Type = ELF::R_PPC64_DTPREL16_HIGH;
246
4
        break;
247
497
      case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHA:
248
4
        Type = ELF::R_PPC64_DTPREL16_HIGHA;
249
4
        break;
250
497
      case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
251
6
        Type = ELF::R_PPC64_DTPREL16_HIGHER;
252
6
        break;
253
497
      case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
254
7
        Type = ELF::R_PPC64_DTPREL16_HIGHERA;
255
7
        break;
256
497
      case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
257
6
        Type = ELF::R_PPC64_DTPREL16_HIGHEST;
258
6
        break;
259
497
      case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
260
7
        Type = ELF::R_PPC64_DTPREL16_HIGHESTA;
261
7
        break;
262
497
      case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
263
6
        if (is64Bit())
264
3
          Type = ELF::R_PPC64_GOT_TLSGD16;
265
3
        else
266
3
          Type = ELF::R_PPC_GOT_TLSGD16;
267
6
        break;
268
497
      case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
269
6
        Type = ELF::R_PPC64_GOT_TLSGD16_LO;
270
6
        break;
271
497
      case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
272
2
        Type = ELF::R_PPC64_GOT_TLSGD16_HI;
273
2
        break;
274
497
      case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
275
6
        Type = ELF::R_PPC64_GOT_TLSGD16_HA;
276
6
        break;
277
497
      case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
278
6
        if (is64Bit())
279
4
          Type = ELF::R_PPC64_GOT_TLSLD16;
280
2
        else
281
2
          Type = ELF::R_PPC_GOT_TLSLD16;
282
6
        break;
283
497
      case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
284
23
        Type = ELF::R_PPC64_GOT_TLSLD16_LO;
285
23
        break;
286
497
      case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
287
4
        Type = ELF::R_PPC64_GOT_TLSLD16_HI;
288
4
        break;
289
497
      case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
290
23
        Type = ELF::R_PPC64_GOT_TLSLD16_HA;
291
23
        break;
292
497
      case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
293
4
        /* We don't have R_PPC64_GOT_TPREL16, but since GOT offsets
294
4
           are always 4-aligned, we can use R_PPC64_GOT_TPREL16_DS.  */
295
4
        Type = ELF::R_PPC64_GOT_TPREL16_DS;
296
4
        break;
297
497
      case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
298
2
        /* We don't have R_PPC64_GOT_TPREL16_LO, but since GOT offsets
299
2
           are always 4-aligned, we can use R_PPC64_GOT_TPREL16_LO_DS.  */
300
2
        Type = ELF::R_PPC64_GOT_TPREL16_LO_DS;
301
2
        break;
302
497
      case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
303
2
        Type = ELF::R_PPC64_GOT_TPREL16_HI;
304
2
        break;
305
497
      case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
306
4
        /* We don't have R_PPC64_GOT_DTPREL16, but since GOT offsets
307
4
           are always 4-aligned, we can use R_PPC64_GOT_DTPREL16_DS.  */
308
4
        Type = ELF::R_PPC64_GOT_DTPREL16_DS;
309
4
        break;
310
497
      case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
311
2
        /* We don't have R_PPC64_GOT_DTPREL16_LO, but since GOT offsets
312
2
           are always 4-aligned, we can use R_PPC64_GOT_DTPREL16_LO_DS.  */
313
2
        Type = ELF::R_PPC64_GOT_DTPREL16_LO_DS;
314
2
        break;
315
497
      case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
316
22
        Type = ELF::R_PPC64_GOT_TPREL16_HA;
317
22
        break;
318
497
      case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
319
4
        Type = ELF::R_PPC64_GOT_DTPREL16_HI;
320
4
        break;
321
497
      case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
322
4
        Type = ELF::R_PPC64_GOT_DTPREL16_HA;
323
4
        break;
324
497
      }
325
497
      break;
326
497
    case PPC::fixup_ppc_half16ds:
327
158
      switch (Modifier) {
328
158
      
default: 0
llvm_unreachable0
("Unsupported Modifier");
329
158
      case MCSymbolRefExpr::VK_None:
330
2
        Type = ELF::R_PPC64_ADDR16_DS;
331
2
        break;
332
158
      case MCSymbolRefExpr::VK_PPC_LO:
333
2
        Type = ELF::R_PPC64_ADDR16_LO_DS;
334
2
        break;
335
158
      case MCSymbolRefExpr::VK_GOT:
336
8
        Type = ELF::R_PPC64_GOT16_DS;
337
8
        break;
338
158
      case MCSymbolRefExpr::VK_PPC_GOT_LO:
339
8
        Type = ELF::R_PPC64_GOT16_LO_DS;
340
8
        break;
341
158
      case MCSymbolRefExpr::VK_PPC_TOC:
342
6
        Type = ELF::R_PPC64_TOC16_DS;
343
6
        break;
344
158
      case MCSymbolRefExpr::VK_PPC_TOC_LO:
345
78
        Type = ELF::R_PPC64_TOC16_LO_DS;
346
78
        break;
347
158
      case MCSymbolRefExpr::VK_TPREL:
348
5
        Type = ELF::R_PPC64_TPREL16_DS;
349
5
        break;
350
158
      case MCSymbolRefExpr::VK_PPC_TPREL_LO:
351
5
        Type = ELF::R_PPC64_TPREL16_LO_DS;
352
5
        break;
353
158
      case MCSymbolRefExpr::VK_DTPREL:
354
6
        Type = ELF::R_PPC64_DTPREL16_DS;
355
6
        break;
356
158
      case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
357
6
        Type = ELF::R_PPC64_DTPREL16_LO_DS;
358
6
        break;
359
158
      case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
360
4
        Type = ELF::R_PPC64_GOT_TPREL16_DS;
361
4
        break;
362
158
      case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
363
22
        Type = ELF::R_PPC64_GOT_TPREL16_LO_DS;
364
22
        break;
365
158
      case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
366
2
        Type = ELF::R_PPC64_GOT_DTPREL16_DS;
367
2
        break;
368
158
      case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
369
4
        Type = ELF::R_PPC64_GOT_DTPREL16_LO_DS;
370
4
        break;
371
158
      }
372
158
      break;
373
158
    case PPC::fixup_ppc_nofixup:
374
73
      switch (Modifier) {
375
73
      
default: 0
llvm_unreachable0
("Unsupported Modifier");
376
73
      case MCSymbolRefExpr::VK_PPC_TLSGD:
377
10
        if (is64Bit())
378
7
          Type = ELF::R_PPC64_TLSGD;
379
3
        else
380
3
          Type = ELF::R_PPC_TLSGD;
381
10
        break;
382
73
      case MCSymbolRefExpr::VK_PPC_TLSLD:
383
25
        if (is64Bit())
384
23
          Type = ELF::R_PPC64_TLSLD;
385
2
        else
386
2
          Type = ELF::R_PPC_TLSLD;
387
25
        break;
388
73
      case MCSymbolRefExpr::VK_PPC_TLS:
389
38
        if (is64Bit())
390
31
          Type = ELF::R_PPC64_TLS;
391
7
        else
392
7
          Type = ELF::R_PPC_TLS;
393
38
        break;
394
73
      }
395
73
      break;
396
160
    case FK_Data_8:
397
160
      switch (Modifier) {
398
160
      
default: 0
llvm_unreachable0
("Unsupported Modifier");
399
160
      case MCSymbolRefExpr::VK_PPC_TOCBASE:
400
43
        Type = ELF::R_PPC64_TOC;
401
43
        break;
402
160
      case MCSymbolRefExpr::VK_None:
403
104
        Type = ELF::R_PPC64_ADDR64;
404
104
        break;
405
160
      case MCSymbolRefExpr::VK_PPC_DTPMOD:
406
2
        Type = ELF::R_PPC64_DTPMOD64;
407
2
        break;
408
160
      case MCSymbolRefExpr::VK_TPREL:
409
4
        Type = ELF::R_PPC64_TPREL64;
410
4
        break;
411
160
      case MCSymbolRefExpr::VK_DTPREL:
412
7
        Type = ELF::R_PPC64_DTPREL64;
413
7
        break;
414
160
      }
415
160
      break;
416
160
    case FK_Data_4:
417
13
      Type = ELF::R_PPC_ADDR32;
418
13
      break;
419
160
    case FK_Data_2:
420
2
      Type = ELF::R_PPC_ADDR16;
421
2
      break;
422
1.36k
    }
423
1.36k
  }
424
1.36k
  return Type;
425
1.36k
}
426
427
bool PPCELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
428
297
                                                 unsigned Type) const {
429
297
  switch (Type) {
430
297
    default:
431
289
      return false;
432
297
433
297
    case ELF::R_PPC_REL24:
434
8
      // If the target symbol has a local entry point, we must keep the
435
8
      // target symbol to preserve that information for the linker.
436
8
      // The "other" values are stored in the last 6 bits of the second byte.
437
8
      // The traditional defines for STO values assume the full byte and thus
438
8
      // the shift to pack it.
439
8
      unsigned Other = cast<MCSymbolELF>(Sym).getOther() << 2;
440
8
      return (Other & ELF::STO_PPC64_LOCAL_MASK) != 0;
441
297
  }
442
297
}
443
444
std::unique_ptr<MCObjectTargetWriter>
445
2.05k
llvm::createPPCELFObjectWriter(bool Is64Bit, uint8_t OSABI) {
446
2.05k
  return llvm::make_unique<PPCELFObjectWriter>(Is64Bit, OSABI);
447
2.05k
}