1 //===-- RISCVMCExpr.cpp - RISC-V 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 RISC-V architecture (e.g. ":lo12:", ":gottprel_g1:", ...). 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "RISCVMCExpr.h" 15 #include "MCTargetDesc/RISCVAsmBackend.h" 16 #include "RISCVFixupKinds.h" 17 #include "llvm/BinaryFormat/ELF.h" 18 #include "llvm/MC/MCAsmLayout.h" 19 #include "llvm/MC/MCAssembler.h" 20 #include "llvm/MC/MCContext.h" 21 #include "llvm/MC/MCStreamer.h" 22 #include "llvm/MC/MCSymbolELF.h" 23 #include "llvm/MC/MCValue.h" 24 #include "llvm/Support/Casting.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 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 (HasVariant) 45 OS << ')'; 46 } 47 48 const MCFixup *RISCVMCExpr::getPCRelHiFixup(const MCFragment **DFOut) const { 49 MCValue AUIPCLoc; 50 if (!getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr, nullptr)) 51 return nullptr; 52 53 const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA(); 54 if (!AUIPCSRE) 55 return nullptr; 56 57 const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol(); 58 const auto *DF = dyn_cast_or_null<MCDataFragment>(AUIPCSymbol->getFragment()); 59 60 if (!DF) 61 return nullptr; 62 63 uint64_t Offset = AUIPCSymbol->getOffset(); 64 if (DF->getContents().size() == Offset) { 65 DF = dyn_cast_or_null<MCDataFragment>(DF->getNextNode()); 66 if (!DF) 67 return nullptr; 68 Offset = 0; 69 } 70 71 for (const MCFixup &F : DF->getFixups()) { 72 if (F.getOffset() != Offset) 73 continue; 74 75 switch ((unsigned)F.getKind()) { 76 default: 77 continue; 78 case RISCV::fixup_riscv_got_hi20: 79 case RISCV::fixup_riscv_tls_got_hi20: 80 case RISCV::fixup_riscv_tls_gd_hi20: 81 case RISCV::fixup_riscv_pcrel_hi20: 82 if (DFOut) 83 *DFOut = DF; 84 return &F; 85 } 86 } 87 88 return nullptr; 89 } 90 91 bool RISCVMCExpr::evaluateAsRelocatableImpl(MCValue &Res, 92 const MCAsmLayout *Layout, 93 const MCFixup *Fixup) const { 94 // Explicitly drop the layout and assembler to prevent any symbolic folding in 95 // the expression handling. This is required to preserve symbolic difference 96 // expressions to emit the paired relocations. 97 if (!getSubExpr()->evaluateAsRelocatable(Res, nullptr, nullptr)) 98 return false; 99 100 Res = 101 MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind()); 102 // Custom fixup types are not valid with symbol difference expressions. 103 return Res.getSymB() ? getKind() == VK_RISCV_None : true; 104 } 105 106 void RISCVMCExpr::visitUsedExpr(MCStreamer &Streamer) const { 107 Streamer.visitUsedExpr(*getSubExpr()); 108 } 109 110 RISCVMCExpr::VariantKind RISCVMCExpr::getVariantKindForName(StringRef name) { 111 return StringSwitch<RISCVMCExpr::VariantKind>(name) 112 .Case("lo", VK_RISCV_LO) 113 .Case("hi", VK_RISCV_HI) 114 .Case("pcrel_lo", VK_RISCV_PCREL_LO) 115 .Case("pcrel_hi", VK_RISCV_PCREL_HI) 116 .Case("got_pcrel_hi", VK_RISCV_GOT_HI) 117 .Case("tprel_lo", VK_RISCV_TPREL_LO) 118 .Case("tprel_hi", VK_RISCV_TPREL_HI) 119 .Case("tprel_add", VK_RISCV_TPREL_ADD) 120 .Case("tls_ie_pcrel_hi", VK_RISCV_TLS_GOT_HI) 121 .Case("tls_gd_pcrel_hi", VK_RISCV_TLS_GD_HI) 122 .Default(VK_RISCV_Invalid); 123 } 124 125 StringRef RISCVMCExpr::getVariantKindName(VariantKind Kind) { 126 switch (Kind) { 127 case VK_RISCV_Invalid: 128 case VK_RISCV_None: 129 llvm_unreachable("Invalid ELF symbol kind"); 130 case VK_RISCV_LO: 131 return "lo"; 132 case VK_RISCV_HI: 133 return "hi"; 134 case VK_RISCV_PCREL_LO: 135 return "pcrel_lo"; 136 case VK_RISCV_PCREL_HI: 137 return "pcrel_hi"; 138 case VK_RISCV_GOT_HI: 139 return "got_pcrel_hi"; 140 case VK_RISCV_TPREL_LO: 141 return "tprel_lo"; 142 case VK_RISCV_TPREL_HI: 143 return "tprel_hi"; 144 case VK_RISCV_TPREL_ADD: 145 return "tprel_add"; 146 case VK_RISCV_TLS_GOT_HI: 147 return "tls_ie_pcrel_hi"; 148 case VK_RISCV_TLS_GD_HI: 149 return "tls_gd_pcrel_hi"; 150 case VK_RISCV_CALL: 151 return "call"; 152 case VK_RISCV_CALL_PLT: 153 return "call_plt"; 154 case VK_RISCV_32_PCREL: 155 return "32_pcrel"; 156 } 157 llvm_unreachable("Invalid ELF symbol kind"); 158 } 159 160 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) { 161 switch (Expr->getKind()) { 162 case MCExpr::Target: 163 llvm_unreachable("Can't handle nested target expression"); 164 break; 165 case MCExpr::Constant: 166 break; 167 168 case MCExpr::Binary: { 169 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr); 170 fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm); 171 fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm); 172 break; 173 } 174 175 case MCExpr::SymbolRef: { 176 // We're known to be under a TLS fixup, so any symbol should be 177 // modified. There should be only one. 178 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr); 179 cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS); 180 break; 181 } 182 183 case MCExpr::Unary: 184 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm); 185 break; 186 } 187 } 188 189 void RISCVMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const { 190 switch (getKind()) { 191 default: 192 return; 193 case VK_RISCV_TPREL_HI: 194 case VK_RISCV_TLS_GOT_HI: 195 case VK_RISCV_TLS_GD_HI: 196 break; 197 } 198 199 fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm); 200 } 201 202 bool RISCVMCExpr::evaluateAsConstant(int64_t &Res) const { 203 MCValue Value; 204 205 if (Kind == VK_RISCV_PCREL_HI || Kind == VK_RISCV_PCREL_LO || 206 Kind == VK_RISCV_GOT_HI || Kind == VK_RISCV_TPREL_HI || 207 Kind == VK_RISCV_TPREL_LO || Kind == VK_RISCV_TPREL_ADD || 208 Kind == VK_RISCV_TLS_GOT_HI || Kind == VK_RISCV_TLS_GD_HI || 209 Kind == VK_RISCV_CALL || Kind == VK_RISCV_CALL_PLT) 210 return false; 211 212 if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr)) 213 return false; 214 215 if (!Value.isAbsolute()) 216 return false; 217 218 Res = evaluateAsInt64(Value.getConstant()); 219 return true; 220 } 221 222 int64_t RISCVMCExpr::evaluateAsInt64(int64_t Value) const { 223 switch (Kind) { 224 default: 225 llvm_unreachable("Invalid kind"); 226 case VK_RISCV_LO: 227 return SignExtend64<12>(Value); 228 case VK_RISCV_HI: 229 // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative. 230 return ((Value + 0x800) >> 12) & 0xfffff; 231 } 232 } 233