1 //===-- RISCVMCExpr.cpp - RISCV specific MC expression classes ------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains the implementation of the assembly expression modifiers
10 // accepted by the RISCV architecture (e.g. ":lo12:", ":gottprel_g1:", ...).
12 //===----------------------------------------------------------------------===//
14 #include "RISCVMCExpr.h"
15 #include "MCTargetDesc/RISCVAsmBackend.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"
29 #define DEBUG_TYPE "riscvmcexpr"
31 const RISCVMCExpr *RISCVMCExpr::create(const MCExpr *Expr, VariantKind Kind,
33 return new (Ctx) RISCVMCExpr(Expr, Kind);
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));
42 OS << '%' << getVariantKindName(getKind()) << '(';
44 if (Kind == VK_RISCV_CALL_PLT)
50 const MCFixup *RISCVMCExpr::getPCRelHiFixup() const {
52 if (!getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr, nullptr))
55 const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA();
59 const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol();
60 const auto *DF = dyn_cast_or_null<MCDataFragment>(AUIPCSymbol->getFragment());
65 uint64_t Offset = AUIPCSymbol->getOffset();
66 if (DF->getContents().size() == Offset) {
67 DF = dyn_cast_or_null<MCDataFragment>(DF->getNextNode());
73 for (const MCFixup &F : DF->getFixups()) {
74 if (F.getOffset() != Offset)
77 switch ((unsigned)F.getKind()) {
80 case RISCV::fixup_riscv_got_hi20:
81 case RISCV::fixup_riscv_tls_got_hi20:
82 case RISCV::fixup_riscv_tls_gd_hi20:
83 case RISCV::fixup_riscv_pcrel_hi20:
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.
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.
105 static_cast<RISCVAsmBackend &>(Layout->getAssembler().getBackend());
106 if (RAB.willForceRelocations())
110 if (!getSubExpr()->evaluateAsValue(AUIPCLoc, *Layout))
113 const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA();
114 // Don't try to evaluate %pcrel_hi/%pcrel_lo pairs that cross fragment
117 findAssociatedFragment() != AUIPCSRE->findAssociatedFragment())
120 const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol();
124 const MCFixup *TargetFixup = getPCRelHiFixup();
128 if ((unsigned)TargetFixup->getKind() != RISCV::fixup_riscv_pcrel_hi20)
132 if (!TargetFixup->getValue()->evaluateAsValue(Target, *Layout))
135 if (!Target.getSymA() || !Target.getSymA()->getSymbol().isInSection())
138 if (&Target.getSymA()->getSymbol().getSection() !=
139 findAssociatedFragment()->getParent())
142 uint64_t AUIPCOffset = AUIPCSymbol->getOffset();
144 Res = MCValue::get(Target.getSymA(), nullptr,
145 Target.getConstant() + (Fixup->getOffset() - AUIPCOffset));
149 bool RISCVMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
150 const MCAsmLayout *Layout,
151 const MCFixup *Fixup) const {
152 if (Kind == VK_RISCV_PCREL_LO && evaluatePCRelLo(Res, Layout, Fixup))
155 if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
158 // Some custom fixup types are not valid with symbol difference expressions
159 if (Res.getSymA() && Res.getSymB()) {
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:
180 void RISCVMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
181 Streamer.visitUsedExpr(*getSubExpr());
184 RISCVMCExpr::VariantKind RISCVMCExpr::getVariantKindForName(StringRef name) {
185 return StringSwitch<RISCVMCExpr::VariantKind>(name)
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)
196 .Default(VK_RISCV_Invalid);
199 StringRef RISCVMCExpr::getVariantKindName(VariantKind Kind) {
202 llvm_unreachable("Invalid ELF symbol kind");
207 case VK_RISCV_PCREL_LO:
209 case VK_RISCV_PCREL_HI:
211 case VK_RISCV_GOT_HI:
212 return "got_pcrel_hi";
213 case VK_RISCV_TPREL_LO:
215 case VK_RISCV_TPREL_HI:
217 case VK_RISCV_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";
226 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
227 switch (Expr->getKind()) {
229 llvm_unreachable("Can't handle nested target expression");
231 case MCExpr::Constant:
234 case MCExpr::Binary: {
235 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
236 fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
237 fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
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);
250 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
255 void RISCVMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
259 case VK_RISCV_TPREL_HI:
260 case VK_RISCV_TLS_GOT_HI:
261 case VK_RISCV_TLS_GD_HI:
265 fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
268 bool RISCVMCExpr::evaluateAsConstant(int64_t &Res) const {
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)
278 if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr))
281 if (!Value.isAbsolute())
284 Res = evaluateAsInt64(Value.getConstant());
288 int64_t RISCVMCExpr::evaluateAsInt64(int64_t Value) const {
291 llvm_unreachable("Invalid kind");
293 return SignExtend64<12>(Value);
295 // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
296 return ((Value + 0x800) >> 12) & 0xfffff;