33 cl::desc(
"Emit R_RISCV_SET_ULEB128/E_RISCV_SUB_ULEB128 if appropriate"));
37 cl::desc(
"When generating R_RISCV_ALIGN, insert $alignment-2 "
38 "bytes of NOPs even in norvc code"));
41 bool Is64Bit,
bool IsLittleEndian,
53#define ELF_RELOC(NAME, ID) .Case(#NAME, ID)
54#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
56#define ELF_RISCV_NONSTANDARD_RELOC(_VENDOR, NAME, ID) .Case(#NAME, ID)
57#include "llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def"
58#undef ELF_RISCV_NONSTANDARD_RELOC
59 .
Case(
"BFD_RELOC_NONE", ELF::R_RISCV_NONE)
60 .
Case(
"BFD_RELOC_32", ELF::R_RISCV_32)
61 .
Case(
"BFD_RELOC_64", ELF::R_RISCV_64)
75 {
"fixup_riscv_hi20", 12, 20, 0},
76 {
"fixup_riscv_lo12_i", 20, 12, 0},
77 {
"fixup_riscv_12_i", 20, 12, 0},
78 {
"fixup_riscv_lo12_s", 0, 32, 0},
79 {
"fixup_riscv_pcrel_hi20", 12, 20, 0},
80 {
"fixup_riscv_pcrel_lo12_i", 20, 12, 0},
81 {
"fixup_riscv_pcrel_lo12_s", 0, 32, 0},
82 {
"fixup_riscv_jal", 12, 20, 0},
83 {
"fixup_riscv_branch", 0, 32, 0},
84 {
"fixup_riscv_rvc_jump", 2, 11, 0},
85 {
"fixup_riscv_rvc_branch", 0, 16, 0},
86 {
"fixup_riscv_rvc_imm", 0, 16, 0},
87 {
"fixup_riscv_call", 0, 64, 0},
88 {
"fixup_riscv_call_plt", 0, 64, 0},
90 {
"fixup_riscv_qc_e_branch", 0, 48, 0},
91 {
"fixup_riscv_qc_e_32", 16, 32, 0},
92 {
"fixup_riscv_qc_abs20_u", 0, 32, 0},
93 {
"fixup_riscv_qc_e_call_plt", 0, 48, 0},
96 {
"fixup_riscv_nds_branch_10", 0, 32, 0},
99 "Not all fixup kinds added to Infos array");
118 bool Resolved)
const {
120 auto Kind =
Fixup.getKind();
169 if (!STI.
hasFeature(RISCV::FeatureVendorXqcili))
179 return RISCV::QC_E_LI;
182 if (!STI.
hasFeature(RISCV::FeatureVendorXqcilb))
187 if (Reg == RISCV::X0)
188 return RISCV::QC_E_J;
189 if (Reg == RISCV::X1)
190 return RISCV::QC_E_JAL;
195 return RISCV::PseudoLongBEQ;
197 return RISCV::PseudoLongBNE;
199 return RISCV::PseudoLongBLT;
201 return RISCV::PseudoLongBGE;
203 return RISCV::PseudoLongBLTU;
205 return RISCV::PseudoLongBGEU;
207 return RISCV::PseudoLongQC_BEQI;
209 return RISCV::PseudoLongQC_BNEI;
211 return RISCV::PseudoLongQC_BLTI;
213 return RISCV::PseudoLongQC_BGEI;
214 case RISCV::QC_BLTUI:
215 return RISCV::PseudoLongQC_BLTUI;
216 case RISCV::QC_BGEUI:
217 return RISCV::PseudoLongQC_BGEUI;
218 case RISCV::QC_E_BEQI:
219 return RISCV::PseudoLongQC_E_BEQI;
220 case RISCV::QC_E_BNEI:
221 return RISCV::PseudoLongQC_E_BNEI;
222 case RISCV::QC_E_BLTI:
223 return RISCV::PseudoLongQC_E_BLTI;
224 case RISCV::QC_E_BGEI:
225 return RISCV::PseudoLongQC_E_BGEI;
226 case RISCV::QC_E_BLTUI:
227 return RISCV::PseudoLongQC_E_BLTUI;
228 case RISCV::QC_E_BGEUI:
229 return RISCV::PseudoLongQC_E_BGEUI;
238 if (STI.
hasFeature(RISCV::FeatureExactAssembly))
253 "Branch Relaxation Error");
260 "JAL is only relaxable with Xqcilb");
263 "JAL only relaxable with rd=x0 or rd=x1");
279 "C.LI is only relaxable with Xqcili");
295 case RISCV::QC_BLTUI:
296 case RISCV::QC_BGEUI:
297 case RISCV::QC_E_BEQI:
298 case RISCV::QC_E_BNEI:
299 case RISCV::QC_E_BLTI:
300 case RISCV::QC_E_BGEI:
301 case RISCV::QC_E_BLTUI:
302 case RISCV::QC_E_BGEUI:
309 Inst = std::move(Res);
323 auto *Sec =
F.getParent();
324 if (
F.getLayoutOrder() <= Sec->firstLinkerRelaxable())
331 if (
F.getAlignment() <= MinNopLen)
334 Size =
F.getAlignment().value() - MinNopLen;
339 F.setLinkerRelaxable();
344 int64_t LineDelta =
F.getDwarfLineDelta();
345 const MCExpr &AddrDelta =
F.getDwarfAddrDelta();
349 if (AddrDelta.evaluateAsAbsolute(
Value, *
Asm))
351 [[maybe_unused]]
bool IsAbsolute =
353 assert(IsAbsolute &&
"CFA with invalid expression");
360 OS <<
uint8_t(dwarf::DW_LNS_advance_line);
371 <<
uint8_t(dwarf::DW_LNE_set_address);
375 OS <<
uint8_t(dwarf::DW_LNS_fixed_advance_pc);
376 support::endian::write<uint16_t>(
OS, 0,
Endian);
381 OS <<
uint8_t(dwarf::DW_LNS_extended_op);
383 OS <<
uint8_t(dwarf::DW_LNE_end_sequence);
388 F.setVarContents(
Data);
395 const MCExpr &AddrDelta =
F.getDwarfAddrDelta();
398 if (AddrDelta.evaluateAsAbsolute(
Value, *
Asm))
400 [[maybe_unused]]
bool IsAbsolute =
402 assert(IsAbsolute &&
"CFA with invalid expression");
405 "expected 1-byte alignment");
407 F.clearVarContents();
412 auto AddFixups = [&Fixups, &AddrDelta](
unsigned Offset,
413 std::pair<unsigned, unsigned>
Fixup) {
414 const MCBinaryExpr &MBE = cast<MCBinaryExpr>(AddrDelta);
422 OS <<
uint8_t(dwarf::DW_CFA_advance_loc);
423 AddFixups(0, {ELF::R_RISCV_SET6, ELF::R_RISCV_SUB6});
424 }
else if (isUInt<8>(
Value)) {
425 OS <<
uint8_t(dwarf::DW_CFA_advance_loc1);
426 support::endian::write<uint8_t>(
OS, 0,
Endian);
427 AddFixups(1, {ELF::R_RISCV_SET8, ELF::R_RISCV_SUB8});
428 }
else if (isUInt<16>(
Value)) {
429 OS <<
uint8_t(dwarf::DW_CFA_advance_loc2);
430 support::endian::write<uint16_t>(
OS, 0,
Endian);
431 AddFixups(1, {ELF::R_RISCV_SET16, ELF::R_RISCV_SUB16});
432 }
else if (isUInt<32>(
Value)) {
433 OS <<
uint8_t(dwarf::DW_CFA_advance_loc4);
434 support::endian::write<uint32_t>(
OS, 0,
Endian);
435 AddFixups(1, {ELF::R_RISCV_SET32, ELF::R_RISCV_SUB32});
439 F.setVarContents(
Data);
440 F.setVarFixups(Fixups);
445 int64_t &
Value)
const {
447 return std::make_pair(
false,
false);
461 if (STI.
hasFeature(RISCV::FeatureExactAssembly))
482 if (Count % 4 == 2) {
490 for (; Count >= 4; Count -= 4)
498 switch (
Fixup.getKind()) {
509 return Value & 0xfff;
511 if (!isInt<12>(
Value)) {
513 "operand must be a constant 12-bit integer");
515 return Value & 0xfff;
518 return (((
Value >> 5) & 0x7f) << 25) | ((
Value & 0x1f) << 7);
522 return ((
Value + 0x800) >> 12) & 0xfffff;
524 if (!isInt<21>(
Value))
529 unsigned Sbit = (
Value >> 20) & 0x1;
530 unsigned Hi8 = (
Value >> 12) & 0xff;
531 unsigned Mid1 = (
Value >> 11) & 0x1;
532 unsigned Lo10 = (
Value >> 1) & 0x3ff;
537 Value = (Sbit << 19) | (Lo10 << 9) | (Mid1 << 8) | Hi8;
542 if (!isInt<13>(
Value))
548 unsigned Sbit = (
Value >> 12) & 0x1;
549 unsigned Hi1 = (
Value >> 11) & 0x1;
550 unsigned Mid6 = (
Value >> 5) & 0x3f;
551 unsigned Lo4 = (
Value >> 1) & 0xf;
556 Value = (Sbit << 31) | (Mid6 << 25) | (Lo4 << 8) | (Hi1 << 7);
566 return UpperImm | ((LowerImm << 20) << 32);
569 if (!isInt<12>(
Value))
572 unsigned Bit11 = (
Value >> 11) & 0x1;
573 unsigned Bit4 = (
Value >> 4) & 0x1;
574 unsigned Bit9_8 = (
Value >> 8) & 0x3;
575 unsigned Bit10 = (
Value >> 10) & 0x1;
576 unsigned Bit6 = (
Value >> 6) & 0x1;
577 unsigned Bit7 = (
Value >> 7) & 0x1;
578 unsigned Bit3_1 = (
Value >> 1) & 0x7;
579 unsigned Bit5 = (
Value >> 5) & 0x1;
580 Value = (Bit11 << 10) | (Bit4 << 9) | (Bit9_8 << 7) | (Bit10 << 6) |
581 (Bit6 << 5) | (Bit7 << 4) | (Bit3_1 << 1) | Bit5;
585 if (!isInt<9>(
Value))
588 unsigned Bit8 = (
Value >> 8) & 0x1;
589 unsigned Bit7_6 = (
Value >> 6) & 0x3;
590 unsigned Bit5 = (
Value >> 5) & 0x1;
591 unsigned Bit4_3 = (
Value >> 3) & 0x3;
592 unsigned Bit2_1 = (
Value >> 1) & 0x3;
593 Value = (Bit8 << 12) | (Bit4_3 << 10) | (Bit7_6 << 5) | (Bit2_1 << 3) |
598 if (!isInt<6>(
Value))
600 unsigned Bit5 = (
Value >> 5) & 0x1;
601 unsigned Bit4_0 =
Value & 0x1f;
602 Value = (Bit5 << 12) | (Bit4_0 << 2);
606 if (!isInt<32>(
Value))
608 return Value & 0xffffffffu;
611 if (!isInt<20>(
Value))
613 unsigned Bit19 = (
Value >> 19) & 0x1;
614 unsigned Bit14_0 =
Value & 0x7fff;
615 unsigned Bit18_15 = (
Value >> 15) & 0xf;
616 Value = (Bit19 << 31) | (Bit14_0 << 16) | (Bit18_15 << 12);
620 if (!isInt<32>(
Value))
630 Value = (Bit31_16 << 32ull) | (Bit12 << 31) | (Bit10_5 << 25) |
631 (Bit15_13 << 17) | (Bit4_1 << 8) | (Bit11 << 7);
635 if (!isInt<11>(
Value))
640 unsigned Sbit = (
Value >> 10) & 0x1;
641 unsigned Hi5 = (
Value >> 5) & 0x1f;
642 unsigned Lo4 = (
Value >> 1) & 0xf;
646 Value = (Sbit << 31) | (Hi5 << 25) | (Lo4 << 8);
652bool RISCVAsmBackend::isPCRelFixupResolved(
const MCSymbol *SymA,
656 if (!
F.getParent()->isLinkerRelaxable())
691 if (
DF->getContents().size() ==
Offset) {
701 auto Kind =
F.getKind();
710 case ELF::R_RISCV_GOT_HI20:
711 case ELF::R_RISCV_TLS_GOT_HI20:
712 case ELF::R_RISCV_TLS_GD_HI20:
713 case ELF::R_RISCV_TLSDESC_HI20:
729 switch (
Fixup.getKind()) {
739 "could not find corresponding %pcrel_hi");
756 if (SA.isUndefined())
769 isPCRelFixupResolved(AUIPCTarget.
getAddSym(), *AUIPCDF);
775 switch (
Fixup.getKind()) {
783 VendorIdentifier =
"QUALCOMM";
786 VendorIdentifier =
"ANDES";
794 auto [It, Inserted] =
795 VendorSymbols.try_emplace(VendorIdentifier, VendorSymbol);
803 VendorSymbol = It->getValue();
838 "relocatable SymA-SymB cannot have relocation specifier");
839 unsigned TA = 0, TB = 0;
840 switch (
Fixup.getKind()) {
842 TA = ELF::R_RISCV_ADD8;
843 TB = ELF::R_RISCV_SUB8;
846 TA = ELF::R_RISCV_ADD16;
847 TB = ELF::R_RISCV_SUB16;
850 TA = ELF::R_RISCV_ADD32;
851 TB = ELF::R_RISCV_SUB32;
854 TA = ELF::R_RISCV_ADD64;
855 TB = ELF::R_RISCV_SUB64;
858 TA = ELF::R_RISCV_SET_ULEB128;
859 TB = ELF::R_RISCV_SUB_ULEB128;
870 FixedValue = FixedValueA - FixedValueB;
876 bool NeedsRelax =
Fixup.isLinkerRelaxable() &&
881 if (IsResolved &&
Fixup.isPCRel())
882 IsResolved = isPCRelFixupResolved(
Target.getAddSym(),
F);
938 unsigned NumBytes =
alignTo(
Info.TargetSize +
Info.TargetOffset, 8) / 8;
940 "Invalid fixup offset!");
946 for (
unsigned i = 0; i != NumBytes; ++i) {
947 unsigned Idx = SwapValue ? (NumBytes - 1 - i) : i;
952std::unique_ptr<MCObjectTargetWriter>
963 return new RISCVAsmBackend(STI, OSABI, TT.isArch64Bit(), TT.isLittleEndian(),
unsigned const MachineRegisterInfo * MRI
static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, uint64_t Value, MCContext &Ctx, const Triple &TheTriple, bool IsResolved)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Analysis containing CSE Info
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
static unsigned getRelaxedOpcode(unsigned Opcode)
mir Rename Register Operands
PowerPC TLS Dynamic Call Fixup
static cl::opt< bool > AlignRvc("riscv-align-rvc", cl::init(true), cl::Hidden, cl::desc("When generating R_RISCV_ALIGN, insert $alignment-2 " "bytes of NOPs even in norvc code"))
static bool relaxableFixupNeedsRelocation(const MCFixupKind Kind)
static bool isDataFixup(unsigned Kind)
static const MCFixup * getPCRelHiFixup(const MCSpecifierExpr &Expr, const MCFragment **DFOut)
static cl::opt< bool > ULEB128Reloc("riscv-uleb128-reloc", cl::init(true), cl::Hidden, cl::desc("Emit R_RISCV_SET_ULEB128/E_RISCV_SUB_ULEB128 if appropriate"))
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Generic interface to target specific assembler backends.
const llvm::endianness Endian
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
MCContext & getContext() const
unsigned getCodePointerSize() const
Get the code pointer size in bytes.
MCContext & getContext() const
LLVM_ABI bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
MCObjectWriter & getWriter() const
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
uint64_t getFragmentOffset(const MCFragment &F) const
Binary assembler expressions.
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Context object for machine code objects.
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI MCSymbol * createLocalSymbol(StringRef Name)
Create a local, non-temporary symbol like an ELF mapping symbol.
const MCAsmInfo * getAsmInfo() const
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
Base class for the full range of assembler expressions which are needed for parsing.
LLVM_ABI bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const
Try to evaluate the expression to a relocatable value, i.e.
static LLVM_ABI bool evaluateSymbolicAdd(const MCAssembler *, bool, const MCValue &, const MCValue &, MCValue &)
LLVM_ABI bool evaluateKnownAbsolute(int64_t &Res, const MCAssembler &Asm) const
Aggressive variant of evaluateAsRelocatable when relocations are unavailable (e.g.
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
static MCFixupKind getDataKindForSize(unsigned Size)
Return the generic fixup kind for a value with the given size.
const MCExpr * getValue() const
uint32_t getOffset() const
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
MCFixupKind getKind() const
const MCExpr & getLEBValue() const
MCSection * getParent() const
LLVM_ABI void setVarFixups(ArrayRef< MCFixup > Fixups)
Instances of this class represent a single low-level machine instruction.
unsigned getOpcode() const
ArrayRef< MCOperand > getOperands() const
void addOperand(const MCOperand Op)
void setOpcode(unsigned Op)
const MCOperand & getOperand(unsigned i) const
virtual void recordRelocation(const MCFragment &F, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)
Record a relocation entry.
MCRegister getReg() const
Returns the register number.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Wrapper class representing physical registers. Should be passed by value.
Extension point for target-specific MCExpr subclasses with a relocation specifier,...
const MCExpr * getSubExpr() const
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const Triple & getTargetTriple() const
const FeatureBitset & getFeatureBits() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
void setFragment(MCFragment *F) const
Mark the symbol as defined in the fragment F.
LLVM_ABI void setVariableValue(const MCExpr *Value)
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
MCFragment * getFragment() const
uint64_t getOffset() const
static MCValue get(const MCSymbol *SymA, const MCSymbol *SymB=nullptr, int64_t Val=0, uint32_t Specifier=0)
const MCSymbol * getAddSym() const
int64_t getConstant() const
const MCSymbol * getSubSym() const
std::optional< bool > evaluateFixup(const MCFragment &, MCFixup &, MCValue &, uint64_t &) override
std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const override
void relaxInstruction(MCInst &Inst, const MCSubtargetInfo &STI) const override
Relax the instruction in the given fragment to the next wider instruction.
bool relaxAlign(MCFragment &F, unsigned &Size) override
bool addReloc(const MCFragment &, const MCFixup &, const MCValue &, uint64_t &FixedValue, bool IsResolved)
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override
Get information on a fixup kind.
RISCVAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit, bool IsLittleEndian, const MCTargetOptions &Options)
std::pair< bool, bool > relaxLEB128(MCFragment &LF, int64_t &Value) const override
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target, uint8_t *Data, uint64_t Value, bool IsResolved) override
bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const override
Write an (optimal) nop sequence of Count bytes to the given output.
void maybeAddVendorReloc(const MCFragment &, const MCFixup &)
bool mayNeedRelaxation(unsigned Opcode, ArrayRef< MCOperand > Operands, const MCSubtargetInfo &STI) const override
Check whether the given instruction (encoded as Opcode+Operands) may need relaxation.
bool fixupNeedsRelaxationAdvanced(const MCFragment &, const MCFixup &, const MCValue &, uint64_t, bool) const override
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
std::optional< MCFixupKind > getFixupKind(StringRef Name) const override
Map a relocation name used in .reloc to a fixup kind.
bool relaxDwarfCFA(MCFragment &) const override
bool relaxDwarfLineAddr(MCFragment &) const override
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & write(unsigned char C)
A raw_ostream that writes to an SmallVector or SmallString.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
bool uncompress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI)
@ fixup_riscv_pcrel_lo12_i
@ fixup_riscv_pcrel_lo12_s
@ fixup_riscv_nds_branch_10
@ fixup_riscv_qc_e_call_plt
@ fixup_riscv_qc_e_branch
initializer< Ty > init(const Ty &Val)
bool isRelocation(MCFixupKind FixupKind)
This is an optimization pass for GlobalISel generic memory operations.
std::unique_ptr< MCObjectTargetWriter > createRISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit)
@ FirstLiteralRelocationKind
@ FK_Data_8
A eight-byte fixup.
@ FK_Data_1
A one-byte fixup.
@ FK_Data_4
A four-byte fixup.
@ FK_Data_leb128
A leb128 fixup.
@ FK_Data_2
A two-byte fixup.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
@ Success
The lock was released successfully.
MCAsmBackend * createRISCVAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Target independent information on a fixup kind.