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