LLVM  10.0.0svn
RISCVMCExpr.cpp
Go to the documentation of this file.
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"
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"
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "riscvmcexpr"
30 
32  MCContext &Ctx) {
33  return new (Ctx) RISCVMCExpr(Expr, Kind);
34 }
35 
36 void RISCVMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
37  VariantKind Kind = getKind();
38  bool HasVariant = ((Kind != VK_RISCV_None) && (Kind != VK_RISCV_CALL) &&
39  (Kind != VK_RISCV_CALL_PLT));
40 
41  if (HasVariant)
42  OS << '%' << getVariantKindName(getKind()) << '(';
43  Expr->print(OS, MAI);
44  if (Kind == VK_RISCV_CALL_PLT)
45  OS << "@plt";
46  if (HasVariant)
47  OS << ')';
48 }
49 
51  MCValue AUIPCLoc;
52  if (!getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr, nullptr))
53  return nullptr;
54 
55  const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA();
56  if (!AUIPCSRE)
57  return nullptr;
58 
59  const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol();
60  const auto *DF = dyn_cast_or_null<MCDataFragment>(AUIPCSymbol->getFragment());
61 
62  if (!DF)
63  return nullptr;
64 
65  uint64_t Offset = AUIPCSymbol->getOffset();
66  if (DF->getContents().size() == Offset) {
67  DF = dyn_cast_or_null<MCDataFragment>(DF->getNextNode());
68  if (!DF)
69  return nullptr;
70  Offset = 0;
71  }
72 
73  for (const MCFixup &F : DF->getFixups()) {
74  if (F.getOffset() != Offset)
75  continue;
76 
77  switch ((unsigned)F.getKind()) {
78  default:
79  continue;
84  return &F;
85  }
86  }
87 
88  return nullptr;
89 }
90 
91 bool RISCVMCExpr::evaluatePCRelLo(MCValue &Res, const MCAsmLayout *Layout,
92  const MCFixup *Fixup) const {
93  // VK_RISCV_PCREL_LO has to be handled specially. The MCExpr inside is
94  // actually the location of a auipc instruction with a VK_RISCV_PCREL_HI fixup
95  // pointing to the real target. We need to generate an MCValue in the form of
96  // (<real target> + <offset from this fixup to the auipc fixup>). The Fixup
97  // is pcrel relative to the VK_RISCV_PCREL_LO fixup, so we need to add the
98  // offset to the VK_RISCV_PCREL_HI Fixup from VK_RISCV_PCREL_LO to correct.
99 
100  // Don't try to evaluate if the fixup will be forced as a relocation (e.g.
101  // as linker relaxation is enabled). If we evaluated pcrel_lo in this case,
102  // the modified fixup will be converted into a relocation that no longer
103  // points to the pcrel_hi as the linker requires.
104  auto &RAB =
105  static_cast<RISCVAsmBackend &>(Layout->getAssembler().getBackend());
106  if (RAB.willForceRelocations())
107  return false;
108 
109  MCValue AUIPCLoc;
110  if (!getSubExpr()->evaluateAsValue(AUIPCLoc, *Layout))
111  return false;
112 
113  const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA();
114  // Don't try to evaluate %pcrel_hi/%pcrel_lo pairs that cross fragment
115  // boundries.
116  if (!AUIPCSRE ||
118  return false;
119 
120  const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol();
121  if (!AUIPCSymbol)
122  return false;
123 
124  const MCFixup *TargetFixup = getPCRelHiFixup();
125  if (!TargetFixup)
126  return false;
127 
128  if ((unsigned)TargetFixup->getKind() != RISCV::fixup_riscv_pcrel_hi20)
129  return false;
130 
131  MCValue Target;
132  if (!TargetFixup->getValue()->evaluateAsValue(Target, *Layout))
133  return false;
134 
135  if (!Target.getSymA() || !Target.getSymA()->getSymbol().isInSection())
136  return false;
137 
138  if (&Target.getSymA()->getSymbol().getSection() !=
140  return false;
141 
142  uint64_t AUIPCOffset = AUIPCSymbol->getOffset();
143 
144  Res = MCValue::get(Target.getSymA(), nullptr,
145  Target.getConstant() + (Fixup->getOffset() - AUIPCOffset));
146  return true;
147 }
148 
150  const MCAsmLayout *Layout,
151  const MCFixup *Fixup) const {
152  if (Kind == VK_RISCV_PCREL_LO && evaluatePCRelLo(Res, Layout, Fixup))
153  return true;
154 
155  if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
156  return false;
157 
158  // Some custom fixup types are not valid with symbol difference expressions
159  if (Res.getSymA() && Res.getSymB()) {
160  switch (getKind()) {
161  default:
162  return true;
163  case VK_RISCV_LO:
164  case VK_RISCV_HI:
165  case VK_RISCV_PCREL_LO:
166  case VK_RISCV_PCREL_HI:
167  case VK_RISCV_GOT_HI:
168  case VK_RISCV_TPREL_LO:
169  case VK_RISCV_TPREL_HI:
170  case VK_RISCV_TPREL_ADD:
171  case VK_RISCV_TLS_GOT_HI:
172  case VK_RISCV_TLS_GD_HI:
173  return false;
174  }
175  }
176 
177  return true;
178 }
179 
180 void RISCVMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
181  Streamer.visitUsedExpr(*getSubExpr());
182 }
183 
186  .Case("lo", VK_RISCV_LO)
187  .Case("hi", VK_RISCV_HI)
188  .Case("pcrel_lo", VK_RISCV_PCREL_LO)
189  .Case("pcrel_hi", VK_RISCV_PCREL_HI)
190  .Case("got_pcrel_hi", VK_RISCV_GOT_HI)
191  .Case("tprel_lo", VK_RISCV_TPREL_LO)
192  .Case("tprel_hi", VK_RISCV_TPREL_HI)
193  .Case("tprel_add", VK_RISCV_TPREL_ADD)
194  .Case("tls_ie_pcrel_hi", VK_RISCV_TLS_GOT_HI)
195  .Case("tls_gd_pcrel_hi", VK_RISCV_TLS_GD_HI)
197 }
198 
200  switch (Kind) {
201  default:
202  llvm_unreachable("Invalid ELF symbol kind");
203  case VK_RISCV_LO:
204  return "lo";
205  case VK_RISCV_HI:
206  return "hi";
207  case VK_RISCV_PCREL_LO:
208  return "pcrel_lo";
209  case VK_RISCV_PCREL_HI:
210  return "pcrel_hi";
211  case VK_RISCV_GOT_HI:
212  return "got_pcrel_hi";
213  case VK_RISCV_TPREL_LO:
214  return "tprel_lo";
215  case VK_RISCV_TPREL_HI:
216  return "tprel_hi";
217  case VK_RISCV_TPREL_ADD:
218  return "tprel_add";
219  case VK_RISCV_TLS_GOT_HI:
220  return "tls_ie_pcrel_hi";
221  case VK_RISCV_TLS_GD_HI:
222  return "tls_gd_pcrel_hi";
223  }
224 }
225 
227  switch (Expr->getKind()) {
228  case MCExpr::Target:
229  llvm_unreachable("Can't handle nested target expression");
230  break;
231  case MCExpr::Constant:
232  break;
233 
234  case MCExpr::Binary: {
235  const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
238  break;
239  }
240 
241  case MCExpr::SymbolRef: {
242  // We're known to be under a TLS fixup, so any symbol should be
243  // modified. There should be only one.
244  const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
245  cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
246  break;
247  }
248 
249  case MCExpr::Unary:
250  fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
251  break;
252  }
253 }
254 
256  switch (getKind()) {
257  default:
258  return;
259  case VK_RISCV_TPREL_HI:
260  case VK_RISCV_TLS_GOT_HI:
261  case VK_RISCV_TLS_GD_HI:
262  break;
263  }
264 
266 }
267 
268 bool RISCVMCExpr::evaluateAsConstant(int64_t &Res) const {
269  MCValue Value;
270 
271  if (Kind == VK_RISCV_PCREL_HI || Kind == VK_RISCV_PCREL_LO ||
272  Kind == VK_RISCV_GOT_HI || Kind == VK_RISCV_TPREL_HI ||
273  Kind == VK_RISCV_TPREL_LO || Kind == VK_RISCV_TPREL_ADD ||
274  Kind == VK_RISCV_TLS_GOT_HI || Kind == VK_RISCV_TLS_GD_HI ||
275  Kind == VK_RISCV_CALL || Kind == VK_RISCV_CALL_PLT)
276  return false;
277 
278  if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr))
279  return false;
280 
281  if (!Value.isAbsolute())
282  return false;
283 
284  Res = evaluateAsInt64(Value.getConstant());
285  return true;
286 }
287 
288 int64_t RISCVMCExpr::evaluateAsInt64(int64_t Value) const {
289  switch (Kind) {
290  default:
291  llvm_unreachable("Invalid kind");
292  case VK_RISCV_LO:
293  return SignExtend64<12>(Value);
294  case VK_RISCV_HI:
295  // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
296  return ((Value + 0x800) >> 12) & 0xfffff;
297  }
298 }
bool evaluateAsConstant(int64_t &Res) const
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This represents an "assembler immediate".
Definition: MCValue.h:39
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm)
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: RISCVMCExpr.cpp:36
VariantKind getKind() const
Definition: RISCVMCExpr.h:59
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:570
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:52
F(f)
static StringRef getVariantKindName(VariantKind Kind)
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:77
int64_t getConstant() const
Definition: MCValue.h:46
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:48
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
Definition: MCSymbol.h:253
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:169
Context object for machine code objects.
Definition: MCContext.h:65
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:669
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:573
const MCFixup * getPCRelHiFixup() const
Get the corresponding PC-relative HI fixup that a VK_RISCV_PCREL_LO points to.
Definition: RISCVMCExpr.cpp:50
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:50
const MCExpr * getSubExpr() const
Definition: RISCVMCExpr.h:61
Unary expressions.
Definition: MCExpr.h:41
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
Streaming machine code generation interface.
Definition: MCStreamer.h:196
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
uint64_t getOffset() const
Definition: MCSymbol.h:324
uint32_t getOffset() const
Definition: MCFixup.h:130
Binary assembler expressions.
Definition: MCExpr.h:423
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
PowerPC TLS Dynamic Call Fixup
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:292
const MCSymbol & getSymbol() const
Definition: MCExpr.h:342
ExprKind getKind() const
Definition: MCExpr.h:68
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:396
MCFragment * findAssociatedFragment() const
Find the "associated section" for this expression, which is currently defined as the absolute section...
Definition: MCExpr.cpp:889
MCFragment * findAssociatedFragment() const override
Definition: RISCVMCExpr.h:74
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:988
MCSection * getParent() const
Definition: MCFragment.h:98
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:268
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override
static VariantKind getVariantKindForName(StringRef name)
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:62
References to labels and assigned expressions.
Definition: MCExpr.h:40
bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const
Try to evaluate the expression to the form (a - b + constant) where neither a nor b are variables...
Definition: MCExpr.cpp:677
LLVM Value Representation.
Definition: Value.h:74
static const char * name
Constant expressions.
Definition: MCExpr.h:39
Binary expressions.
Definition: MCExpr.h:38
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
const MCExpr * getValue() const
Definition: MCFixup.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Target specific expression.
Definition: MCExpr.h:42
void visitUsedExpr(MCStreamer &Streamer) const override
static const RISCVMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: RISCVMCExpr.cpp:31
MCFixupKind getKind() const
Definition: MCFixup.h:126