Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- RISCVMCExpr.cpp - RISCV specific MC expression classes ------------===//
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 the implementation of the assembly expression modifiers
10
// accepted by the RISCV architecture (e.g. ":lo12:", ":gottprel_g1:", ...).
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "RISCVMCExpr.h"
15
#include "MCTargetDesc/RISCVAsmBackend.h"
16
#include "RISCV.h"
17
#include "RISCVFixupKinds.h"
18
#include "llvm/BinaryFormat/ELF.h"
19
#include "llvm/MC/MCAsmLayout.h"
20
#include "llvm/MC/MCAssembler.h"
21
#include "llvm/MC/MCContext.h"
22
#include "llvm/MC/MCStreamer.h"
23
#include "llvm/MC/MCSymbolELF.h"
24
#include "llvm/MC/MCValue.h"
25
#include "llvm/Support/ErrorHandling.h"
26
27
using namespace llvm;
28
29
#define DEBUG_TYPE "riscvmcexpr"
30
31
const RISCVMCExpr *RISCVMCExpr::create(const MCExpr *Expr, VariantKind Kind,
32
2.93k
                                       MCContext &Ctx) {
33
2.93k
  return new (Ctx) RISCVMCExpr(Expr, Kind);
34
2.93k
}
35
36
2.51k
void RISCVMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
37
2.51k
  VariantKind Kind = getKind();
38
2.51k
  bool HasVariant = ((Kind != VK_RISCV_None) && (Kind != VK_RISCV_CALL) &&
39
2.51k
                     
(Kind != VK_RISCV_CALL_PLT)1.14k
);
40
2.51k
41
2.51k
  if (HasVariant)
42
1.11k
    OS << '%' << getVariantKindName(getKind()) << '(';
43
2.51k
  Expr->print(OS, MAI);
44
2.51k
  if (Kind == VK_RISCV_CALL_PLT)
45
29
    OS << "@plt";
46
2.51k
  if (HasVariant)
47
1.11k
    OS << ')';
48
2.51k
}
49
50
140
const MCFixup *RISCVMCExpr::getPCRelHiFixup() const {
51
140
  MCValue AUIPCLoc;
52
140
  if (!getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr, nullptr))
53
0
    return nullptr;
54
140
55
140
  const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA();
56
140
  if (!AUIPCSRE)
57
0
    return nullptr;
58
140
59
140
  const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol();
60
140
  const auto *DF = dyn_cast_or_null<MCDataFragment>(AUIPCSymbol->getFragment());
61
140
62
140
  if (!DF)
63
7
    return nullptr;
64
133
65
133
  uint64_t Offset = AUIPCSymbol->getOffset();
66
133
  if (DF->getContents().size() == Offset) {
67
8
    DF = dyn_cast_or_null<MCDataFragment>(DF->getNextNode());
68
8
    if (!DF)
69
0
      return nullptr;
70
8
    Offset = 0;
71
8
  }
72
133
73
938
  
for (const MCFixup &F : DF->getFixups())133
{
74
938
    if (F.getOffset() != Offset)
75
804
      continue;
76
134
77
134
    switch ((unsigned)F.getKind()) {
78
134
    default:
79
4
      continue;
80
134
    case RISCV::fixup_riscv_got_hi20:
81
130
    case RISCV::fixup_riscv_tls_got_hi20:
82
130
    case RISCV::fixup_riscv_tls_gd_hi20:
83
130
    case RISCV::fixup_riscv_pcrel_hi20:
84
130
      return &F;
85
134
    }
86
134
  }
87
133
88
133
  
return nullptr3
;
89
133
}
90
91
bool RISCVMCExpr::evaluatePCRelLo(MCValue &Res, const MCAsmLayout *Layout,
92
83
                                  const MCFixup *Fixup) const {
93
83
  // VK_RISCV_PCREL_LO has to be handled specially.  The MCExpr inside is
94
83
  // actually the location of a auipc instruction with a VK_RISCV_PCREL_HI fixup
95
83
  // pointing to the real target.  We need to generate an MCValue in the form of
96
83
  // (<real target> + <offset from this fixup to the auipc fixup>).  The Fixup
97
83
  // is pcrel relative to the VK_RISCV_PCREL_LO fixup, so we need to add the
98
83
  // offset to the VK_RISCV_PCREL_HI Fixup from VK_RISCV_PCREL_LO to correct.
99
83
100
83
  // Don't try to evaluate if the fixup will be forced as a relocation (e.g.
101
83
  // as linker relaxation is enabled). If we evaluated pcrel_lo in this case,
102
83
  // the modified fixup will be converted into a relocation that no longer
103
83
  // points to the pcrel_hi as the linker requires.
104
83
  auto &RAB =
105
83
      static_cast<RISCVAsmBackend &>(Layout->getAssembler().getBackend());
106
83
  if (RAB.willForceRelocations())
107
11
    return false;
108
72
109
72
  MCValue AUIPCLoc;
110
72
  if (!getSubExpr()->evaluateAsValue(AUIPCLoc, *Layout))
111
0
    return false;
112
72
113
72
  const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA();
114
72
  // Don't try to evaluate %pcrel_hi/%pcrel_lo pairs that cross fragment
115
72
  // boundries.
116
72
  if (!AUIPCSRE ||
117
72
      
findAssociatedFragment() != AUIPCSRE->findAssociatedFragment()68
)
118
4
    return false;
119
68
120
68
  const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol();
121
68
  if (!AUIPCSymbol)
122
0
    return false;
123
68
124
68
  const MCFixup *TargetFixup = getPCRelHiFixup();
125
68
  if (!TargetFixup)
126
8
    return false;
127
60
128
60
  if ((unsigned)TargetFixup->getKind() != RISCV::fixup_riscv_pcrel_hi20)
129
24
    return false;
130
36
131
36
  MCValue Target;
132
36
  if (!TargetFixup->getValue()->evaluateAsValue(Target, *Layout))
133
0
    return false;
134
36
135
36
  if (!Target.getSymA() || !Target.getSymA()->getSymbol().isInSection())
136
28
    return false;
137
8
138
8
  if (&Target.getSymA()->getSymbol().getSection() !=
139
8
      findAssociatedFragment()->getParent())
140
0
    return false;
141
8
142
8
  uint64_t AUIPCOffset = AUIPCSymbol->getOffset();
143
8
144
8
  Res = MCValue::get(Target.getSymA(), nullptr,
145
8
                     Target.getConstant() + (Fixup->getOffset() - AUIPCOffset));
146
8
  return true;
147
8
}
148
149
bool RISCVMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
150
                                            const MCAsmLayout *Layout,
151
372
                                            const MCFixup *Fixup) const {
152
372
  if (Kind == VK_RISCV_PCREL_LO && 
evaluatePCRelLo(Res, Layout, Fixup)83
)
153
8
    return true;
154
364
155
364
  if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
156
0
    return false;
157
364
158
364
  // Some custom fixup types are not valid with symbol difference expressions
159
364
  if (Res.getSymA() && 
Res.getSymB()344
) {
160
4
    switch (getKind()) {
161
4
    default:
162
0
      return true;
163
4
    case VK_RISCV_LO:
164
4
    case VK_RISCV_HI:
165
4
    case VK_RISCV_PCREL_LO:
166
4
    case VK_RISCV_PCREL_HI:
167
4
    case VK_RISCV_GOT_HI:
168
4
    case VK_RISCV_TPREL_LO:
169
4
    case VK_RISCV_TPREL_HI:
170
4
    case VK_RISCV_TPREL_ADD:
171
4
    case VK_RISCV_TLS_GOT_HI:
172
4
    case VK_RISCV_TLS_GD_HI:
173
4
      return false;
174
360
    }
175
360
  }
176
360
177
360
  return true;
178
360
}
179
180
336
void RISCVMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
181
336
  Streamer.visitUsedExpr(*getSubExpr());
182
336
}
183
184
449
RISCVMCExpr::VariantKind RISCVMCExpr::getVariantKindForName(StringRef name) {
185
449
  return StringSwitch<RISCVMCExpr::VariantKind>(name)
186
449
      .Case("lo", VK_RISCV_LO)
187
449
      .Case("hi", VK_RISCV_HI)
188
449
      .Case("pcrel_lo", VK_RISCV_PCREL_LO)
189
449
      .Case("pcrel_hi", VK_RISCV_PCREL_HI)
190
449
      .Case("got_pcrel_hi", VK_RISCV_GOT_HI)
191
449
      .Case("tprel_lo", VK_RISCV_TPREL_LO)
192
449
      .Case("tprel_hi", VK_RISCV_TPREL_HI)
193
449
      .Case("tprel_add", VK_RISCV_TPREL_ADD)
194
449
      .Case("tls_ie_pcrel_hi", VK_RISCV_TLS_GOT_HI)
195
449
      .Case("tls_gd_pcrel_hi", VK_RISCV_TLS_GD_HI)
196
449
      .Default(VK_RISCV_Invalid);
197
449
}
198
199
1.11k
StringRef RISCVMCExpr::getVariantKindName(VariantKind Kind) {
200
1.11k
  switch (Kind) {
201
1.11k
  default:
202
0
    llvm_unreachable("Invalid ELF symbol kind");
203
1.11k
  case VK_RISCV_LO:
204
414
    return "lo";
205
1.11k
  case VK_RISCV_HI:
206
278
    return "hi";
207
1.11k
  case VK_RISCV_PCREL_LO:
208
203
    return "pcrel_lo";
209
1.11k
  case VK_RISCV_PCREL_HI:
210
111
    return "pcrel_hi";
211
1.11k
  case VK_RISCV_GOT_HI:
212
16
    return "got_pcrel_hi";
213
1.11k
  case VK_RISCV_TPREL_LO:
214
18
    return "tprel_lo";
215
1.11k
  case VK_RISCV_TPREL_HI:
216
14
    return "tprel_hi";
217
1.11k
  case VK_RISCV_TPREL_ADD:
218
12
    return "tprel_add";
219
1.11k
  case VK_RISCV_TLS_GOT_HI:
220
24
    return "tls_ie_pcrel_hi";
221
1.11k
  case VK_RISCV_TLS_GD_HI:
222
26
    return "tls_gd_pcrel_hi";
223
1.11k
  }
224
1.11k
}
225
226
20
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
227
20
  switch (Expr->getKind()) {
228
20
  case MCExpr::Target:
229
0
    llvm_unreachable("Can't handle nested target expression");
230
20
    
break0
;
231
20
  case MCExpr::Constant:
232
1
    break;
233
20
234
20
  case MCExpr::Binary: {
235
1
    const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
236
1
    fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
237
1
    fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
238
1
    break;
239
20
  }
240
20
241
20
  case MCExpr::SymbolRef: {
242
18
    // We're known to be under a TLS fixup, so any symbol should be
243
18
    // modified. There should be only one.
244
18
    const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
245
18
    cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
246
18
    break;
247
20
  }
248
20
249
20
  case MCExpr::Unary:
250
0
    fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
251
0
    break;
252
20
  }
253
20
}
254
255
336
void RISCVMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
256
336
  switch (getKind()) {
257
336
  default:
258
318
    return;
259
336
  case VK_RISCV_TPREL_HI:
260
18
  case VK_RISCV_TLS_GOT_HI:
261
18
  case VK_RISCV_TLS_GD_HI:
262
18
    break;
263
18
  }
264
18
265
18
  fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
266
18
}
267
268
1.16k
bool RISCVMCExpr::evaluateAsConstant(int64_t &Res) const {
269
1.16k
  MCValue Value;
270
1.16k
271
1.16k
  if (Kind == VK_RISCV_PCREL_HI || 
Kind == VK_RISCV_PCREL_LO1.06k
||
272
1.16k
      
Kind == VK_RISCV_GOT_HI820
||
Kind == VK_RISCV_TPREL_HI812
||
273
1.16k
      
Kind == VK_RISCV_TPREL_LO796
||
Kind == VK_RISCV_TPREL_ADD766
||
274
1.16k
      
Kind == VK_RISCV_TLS_GOT_HI752
||
Kind == VK_RISCV_TLS_GD_HI748
||
275
1.16k
      
Kind == VK_RISCV_CALL744
||
Kind == VK_RISCV_CALL_PLT508
)
276
693
    return false;
277
468
278
468
  if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr))
279
0
    return false;
280
468
281
468
  if (!Value.isAbsolute())
282
292
    return false;
283
176
284
176
  Res = evaluateAsInt64(Value.getConstant());
285
176
  return true;
286
176
}
287
288
176
int64_t RISCVMCExpr::evaluateAsInt64(int64_t Value) const {
289
176
  switch (Kind) {
290
176
  default:
291
0
    llvm_unreachable("Invalid kind");
292
176
  case VK_RISCV_LO:
293
144
    return SignExtend64<12>(Value);
294
176
  case VK_RISCV_HI:
295
32
    // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
296
32
    return ((Value + 0x800) >> 12) & 0xfffff;
297
176
  }
298
176
}