LLVM 20.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"
24
25using namespace llvm;
26
27// This part is for ELF object output.
29 const MCSubtargetInfo &STI)
30 : RISCVTargetStreamer(S), CurrentVendor("riscv") {
32 const FeatureBitset &Features = STI.getFeatureBits();
33 auto &MAB = static_cast<RISCVAsmBackend &>(MCA.getBackend());
35 MAB.getTargetOptions().getABIName()));
37 // `j label` in `.option norelax; j label; .option relax; ...; label:` needs a
38 // relocation to ensure the jump target is correct after linking. This is due
39 // to a limitation that shouldForceRelocation has to make the decision upfront
40 // without knowing a possibly future .option relax. When RISCVAsmParser is used,
41 // its ParseInstruction may call setForceRelocs as well.
42 if (STI.hasFeature(RISCV::FeatureRelax))
43 static_cast<RISCVAsmBackend &>(MAB).setForceRelocs();
44}
45
47 return static_cast<RISCVELFStreamer &>(Streamer);
48}
49
58
59void RISCVTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
60 getStreamer().setAttributeItem(Attribute, Value, /*OverwriteExisting=*/true);
61}
62
63void RISCVTargetELFStreamer::emitTextAttribute(unsigned Attribute,
65 getStreamer().setAttributeItem(Attribute, String, /*OverwriteExisting=*/true);
66}
67
68void RISCVTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
69 unsigned IntValue,
70 StringRef StringValue) {
71 getStreamer().setAttributeItems(Attribute, IntValue, StringValue,
72 /*OverwriteExisting=*/true);
73}
74
75void RISCVTargetELFStreamer::finishAttributeSection() {
77 if (S.Contents.empty())
78 return;
79
80 S.emitAttributesSection(CurrentVendor, ".riscv.attributes",
81 ELF::SHT_RISCV_ATTRIBUTES, AttributeSection);
82}
83
88
89 unsigned EFlags = W.getELFHeaderEFlags();
90
91 if (hasRVC())
92 EFlags |= ELF::EF_RISCV_RVC;
93 if (hasTSO())
94 EFlags |= ELF::EF_RISCV_TSO;
95
96 switch (ABI) {
99 break;
103 break;
107 break;
110 EFlags |= ELF::EF_RISCV_RVE;
111 break;
113 llvm_unreachable("Improperly initialised target ABI");
114 }
115
116 W.setELFHeaderEFlags(EFlags);
117}
118
119void RISCVTargetELFStreamer::reset() {
120 AttributeSection = nullptr;
121}
122
125 cast<MCSymbolELF>(Symbol).setOther(ELF::STO_RISCV_VARIANT_CC);
126}
127
128void RISCVELFStreamer::reset() {
129 static_cast<RISCVTargetStreamer *>(getTargetStreamer())->reset();
131 LastMappingSymbols.clear();
132 LastEMS = EMS_None;
133}
134
135void RISCVELFStreamer::emitDataMappingSymbol() {
136 if (LastEMS == EMS_Data)
137 return;
138 emitMappingSymbol("$d");
139 LastEMS = EMS_Data;
140}
141
142void RISCVELFStreamer::emitInstructionsMappingSymbol() {
143 if (LastEMS == EMS_Instructions)
144 return;
145 emitMappingSymbol("$x");
146 LastEMS = EMS_Instructions;
147}
148
149void RISCVELFStreamer::emitMappingSymbol(StringRef Name) {
150 auto *Symbol = cast<MCSymbolELF>(getContext().createLocalSymbol(Name));
151 emitLabel(Symbol);
152 Symbol->setType(ELF::STT_NOTYPE);
153 Symbol->setBinding(ELF::STB_LOCAL);
154}
155
157 // We have to keep track of the mapping symbol state of any sections we
158 // use. Each one should start off as EMS_None, which is provided as the
159 // default constructor by DenseMap::lookup.
160 LastMappingSymbols[getPreviousSection().first] = LastEMS;
161 LastEMS = LastMappingSymbols.lookup(Section);
162
163 MCELFStreamer::changeSection(Section, Subsection);
164}
165
167 const MCSubtargetInfo &STI) {
168 emitInstructionsMappingSymbol();
170}
171
173 emitDataMappingSymbol();
175}
176
177void RISCVELFStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
178 SMLoc Loc) {
179 emitDataMappingSymbol();
180 MCELFStreamer::emitFill(NumBytes, FillValue, Loc);
181}
182
184 SMLoc Loc) {
185 emitDataMappingSymbol();
187}
188
189namespace llvm {
191 std::unique_ptr<MCAsmBackend> MAB,
192 std::unique_ptr<MCObjectWriter> MOW,
193 std::unique_ptr<MCCodeEmitter> MCE) {
195 new RISCVELFStreamer(C, std::move(MAB), std::move(MOW), std::move(MCE));
196 return S;
197}
198} // namespace llvm
std::string Name
uint64_t Size
Container class for subtarget features.
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:188
bool registerSymbol(const MCSymbol &Symbol)
Context object for machine code objects.
Definition: MCContext.h:83
SmallVector< AttributeItem, 64 > Contents
void emitAttributesSection(StringRef Vendor, const Twine &Section, unsigned Type, MCSection *&AttributeSection)
void changeSection(MCSection *Section, uint32_t Subsection=0) override
This is called by popSection and switchSection, if the current section changes.
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.
ELFObjectWriter & getWriter()
void setAttributeItem(unsigned Attribute, unsigned Value, bool OverwriteExisting)
void reset() override
state management
Definition: MCELFStreamer.h:41
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
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:36
Streaming machine code generation interface.
Definition: MCStreamer.h:213
MCSectionSubPair getPreviousSection() const
Return the previous section that the streamer is emitting code to.
Definition: MCStreamer.h:405
MCContext & getContext() const
Definition: MCStreamer.h:300
MCTargetStreamer * getTargetStreamer()
Definition: MCStreamer.h:309
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:41
MCStreamer & Streamer
Definition: MCStreamer.h:96
void changeSection(MCSection *Section, uint32_t Subsection) override
This is called by popSection and switchSection, if the current section changes.
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.
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.
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:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
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
@ SHT_RISCV_ATTRIBUTES
Definition: ELF.h:1171
@ EF_RISCV_RVE
Definition: ELF.h:680
@ EF_RISCV_RVC
Definition: ELF.h:674
@ EF_RISCV_TSO
Definition: ELF.h:681
@ EF_RISCV_FLOAT_ABI_SINGLE
Definition: ELF.h:677
@ EF_RISCV_FLOAT_ABI_DOUBLE
Definition: ELF.h:678
@ STO_RISCV_VARIANT_CC
Definition: ELF.h:692
@ STB_LOCAL
Definition: ELF.h:1341
@ STT_NOTYPE
Definition: ELF.h:1353
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)