LLVM 19.0.0git
RISCVELFStreamer.cpp
Go to the documentation of this file.
1//===-- RISCVELFStreamer.cpp - RISC-V ELF Target Streamer Methods ---------===//
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 provides RISC-V specific target streamer methods.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RISCVELFStreamer.h"
14#include "RISCVAsmBackend.h"
15#include "RISCVBaseInfo.h"
16#include "RISCVMCTargetDesc.h"
19#include "llvm/MC/MCAssembler.h"
21#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCValue.h"
26#include "llvm/Support/LEB128.h"
28
29using namespace llvm;
30
31// This part is for ELF object output.
33 const MCSubtargetInfo &STI)
34 : RISCVTargetStreamer(S), CurrentVendor("riscv") {
36 const FeatureBitset &Features = STI.getFeatureBits();
37 auto &MAB = static_cast<RISCVAsmBackend &>(MCA.getBackend());
39 MAB.getTargetOptions().getABIName()));
41 // `j label` in `.option norelax; j label; .option relax; ...; label:` needs a
42 // relocation to ensure the jump target is correct after linking. This is due
43 // to a limitation that shouldForceRelocation has to make the decision upfront
44 // without knowing a possibly future .option relax. When RISCVAsmParser is used,
45 // its ParseInstruction may call setForceRelocs as well.
46 if (STI.hasFeature(RISCV::FeatureRelax))
47 static_cast<RISCVAsmBackend &>(MAB).setForceRelocs();
48}
49
51 return static_cast<RISCVELFStreamer &>(Streamer);
52}
53
62
63void RISCVTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
64 getStreamer().setAttributeItem(Attribute, Value, /*OverwriteExisting=*/true);
65}
66
67void RISCVTargetELFStreamer::emitTextAttribute(unsigned Attribute,
69 getStreamer().setAttributeItem(Attribute, String, /*OverwriteExisting=*/true);
70}
71
72void RISCVTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
73 unsigned IntValue,
74 StringRef StringValue) {
75 getStreamer().setAttributeItems(Attribute, IntValue, StringValue,
76 /*OverwriteExisting=*/true);
77}
78
79void RISCVTargetELFStreamer::finishAttributeSection() {
81 if (S.Contents.empty())
82 return;
83
84 S.emitAttributesSection(CurrentVendor, ".riscv.attributes",
85 ELF::SHT_RISCV_ATTRIBUTES, AttributeSection);
86}
87
92
93 unsigned EFlags = MCA.getELFHeaderEFlags();
94
95 if (hasRVC())
96 EFlags |= ELF::EF_RISCV_RVC;
97 if (hasTSO())
98 EFlags |= ELF::EF_RISCV_TSO;
99
100 switch (ABI) {
103 break;
107 break;
111 break;
114 EFlags |= ELF::EF_RISCV_RVE;
115 break;
117 llvm_unreachable("Improperly initialised target ABI");
118 }
119
120 MCA.setELFHeaderEFlags(EFlags);
121}
122
123void RISCVTargetELFStreamer::reset() {
124 AttributeSection = nullptr;
125}
126
129 cast<MCSymbolELF>(Symbol).setOther(ELF::STO_RISCV_VARIANT_CC);
130}
131
132void RISCVELFStreamer::reset() {
133 static_cast<RISCVTargetStreamer *>(getTargetStreamer())->reset();
135 MappingSymbolCounter = 0;
136 LastMappingSymbols.clear();
137 LastEMS = EMS_None;
138}
139
140void RISCVELFStreamer::emitDataMappingSymbol() {
141 if (LastEMS == EMS_Data)
142 return;
143 emitMappingSymbol("$d");
144 LastEMS = EMS_Data;
145}
146
147void RISCVELFStreamer::emitInstructionsMappingSymbol() {
148 if (LastEMS == EMS_Instructions)
149 return;
150 emitMappingSymbol("$x");
151 LastEMS = EMS_Instructions;
152}
153
154void RISCVELFStreamer::emitMappingSymbol(StringRef Name) {
155 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
156 Name + "." + Twine(MappingSymbolCounter++)));
157 emitLabel(Symbol);
158 Symbol->setType(ELF::STT_NOTYPE);
159 Symbol->setBinding(ELF::STB_LOCAL);
160}
161
163 const MCExpr *Subsection) {
164 // We have to keep track of the mapping symbol state of any sections we
165 // use. Each one should start off as EMS_None, which is provided as the
166 // default constructor by DenseMap::lookup.
167 LastMappingSymbols[getPreviousSection().first] = LastEMS;
168 LastEMS = LastMappingSymbols.lookup(Section);
169
170 MCELFStreamer::changeSection(Section, Subsection);
171}
172
174 const MCSubtargetInfo &STI) {
175 emitInstructionsMappingSymbol();
177}
178
180 emitDataMappingSymbol();
182}
183
184void RISCVELFStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
185 SMLoc Loc) {
186 emitDataMappingSymbol();
187 MCELFStreamer::emitFill(NumBytes, FillValue, Loc);
188}
189
191 SMLoc Loc) {
192 emitDataMappingSymbol();
194}
195
196namespace llvm {
198 std::unique_ptr<MCAsmBackend> MAB,
199 std::unique_ptr<MCObjectWriter> MOW,
200 std::unique_ptr<MCCodeEmitter> MCE,
201 bool RelaxAll) {
203 new RISCVELFStreamer(C, std::move(MAB), std::move(MOW), std::move(MCE));
204 S->getAssembler().setRelaxAll(RelaxAll);
205 return S;
206}
207} // namespace llvm
std::string Name
uint64_t Size
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
void changeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc) override
Emit Size bytes worth of the value specified by FillValue.
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override
Emit the expression Value into the output as a native integer of the given Size bytes.
Container class for subtarget features.
unsigned getELFHeaderEFlags() const
ELF e_header flags.
Definition: MCAssembler.h:281
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:334
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:282
bool registerSymbol(const MCSymbol &Symbol)
void setRelaxAll(bool Value)
Definition: MCAssembler.h:363
Context object for machine code objects.
Definition: MCContext.h:76
SmallVector< AttributeItem, 64 > Contents
void emitAttributesSection(StringRef Vendor, const Twine &Section, unsigned Type, MCSection *&AttributeSection)
void setAttributeItems(unsigned Attribute, unsigned IntValue, StringRef StringValue, bool OverwriteExisting)
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
void setAttributeItem(unsigned Attribute, unsigned Value, bool OverwriteExisting)
void reset() override
state management
Definition: MCELFStreamer.h:40
void changeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc=SMLoc()) override
Emit Size bytes worth of the value specified by FillValue.
MCAssembler & getAssembler()
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
Streaming machine code generation interface.
Definition: MCStreamer.h:212
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 ...
Definition: MCSymbol.h:40
MCStreamer & Streamer
Definition: MCStreamer.h:95
void emitDirectiveOptionPop() override
void emitDirectiveOptionPush() override
void emitDirectiveOptionNoPIC() override
void emitDirectiveOptionNoRVC() override
RISCVTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
void emitDirectiveVariantCC(MCSymbol &Symbol) override
void emitDirectiveOptionRelax() override
RISCVELFStreamer & getStreamer()
void emitDirectiveOptionNoRelax() override
void emitDirectiveOptionPIC() override
void emitDirectiveOptionRVC() override
RISCVABI::ABI getTargetABI() const
void setFlagsFromFeatures(const MCSubtargetInfo &STI)
void setTargetABI(RISCVABI::ABI ABI)
Represents a location in source code.
Definition: SMLoc.h:23
bool empty() const
Definition: SmallVector.h:94
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LLVM Value Representation.
Definition: Value.h:74
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ EF_RISCV_RVE
Definition: ELF.h:679
@ EF_RISCV_RVC
Definition: ELF.h:673
@ EF_RISCV_TSO
Definition: ELF.h:680
@ EF_RISCV_FLOAT_ABI_SINGLE
Definition: ELF.h:676
@ EF_RISCV_FLOAT_ABI_DOUBLE
Definition: ELF.h:677
@ SHT_RISCV_ATTRIBUTES
Definition: ELF.h:1140
@ STB_LOCAL
Definition: ELF.h:1310
@ STO_RISCV_VARIANT_CC
Definition: ELF.h:691
@ STT_NOTYPE
Definition: ELF.h:1322
ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits, StringRef ABIName)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCELFStreamer * createRISCVELFStreamer(MCContext &C, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > MOW, std::unique_ptr< MCCodeEmitter > MCE, bool RelaxAll)