LLVM 17.0.0git
RISCVELFStreamer.cpp
Go to the documentation of this file.
1//===-- RISCVELFStreamer.cpp - RISCV 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 RISCV 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"), STI(STI) {
36 const FeatureBitset &Features = STI.getFeatureBits();
37 auto &MAB = static_cast<RISCVAsmBackend &>(MCA.getBackend());
39 MAB.getTargetOptions().getABIName()));
40}
41
43 return static_cast<RISCVELFStreamer &>(Streamer);
44}
45
54
55void RISCVTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
56 getStreamer().setAttributeItem(Attribute, Value, /*OverwriteExisting=*/true);
57}
58
59void RISCVTargetELFStreamer::emitTextAttribute(unsigned Attribute,
61 getStreamer().setAttributeItem(Attribute, String, /*OverwriteExisting=*/true);
62}
63
64void RISCVTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
65 unsigned IntValue,
66 StringRef StringValue) {
67 getStreamer().setAttributeItems(Attribute, IntValue, StringValue,
68 /*OverwriteExisting=*/true);
69}
70
71void RISCVTargetELFStreamer::finishAttributeSection() {
73 if (S.Contents.empty())
74 return;
75
76 S.emitAttributesSection(CurrentVendor, ".riscv.attributes",
77 ELF::SHT_RISCV_ATTRIBUTES, AttributeSection);
78}
79
83 const FeatureBitset &Features = STI.getFeatureBits();
85
86 unsigned EFlags = MCA.getELFHeaderEFlags();
87
88 if (Features[RISCV::FeatureStdExtC])
89 EFlags |= ELF::EF_RISCV_RVC;
90 if (Features[RISCV::FeatureStdExtZtso])
91 EFlags |= ELF::EF_RISCV_TSO;
92
93 switch (ABI) {
96 break;
100 break;
104 break;
107 EFlags |= ELF::EF_RISCV_RVE;
108 break;
110 llvm_unreachable("Improperly initialised target ABI");
111 }
112
113 MCA.setELFHeaderEFlags(EFlags);
114}
115
116void RISCVTargetELFStreamer::reset() {
117 AttributeSection = nullptr;
118}
119
122 cast<MCSymbolELF>(Symbol).setOther(ELF::STO_RISCV_VARIANT_CC);
123}
124
125bool RISCVELFStreamer::requiresFixups(MCContext &C, const MCExpr *Value,
126 const MCExpr *&LHS, const MCExpr *&RHS) {
127 const auto *MBE = dyn_cast<MCBinaryExpr>(Value);
128 if (MBE == nullptr)
129 return false;
130
131 MCValue E;
132 if (!Value->evaluateAsRelocatable(E, nullptr, nullptr))
133 return false;
134 if (E.getSymA() == nullptr || E.getSymB() == nullptr)
135 return false;
136
137 const auto &A = E.getSymA()->getSymbol();
138 const auto &B = E.getSymB()->getSymbol();
139
141 MCConstantExpr::create(E.getConstant(), C), C);
142 RHS = E.getSymB();
143
144 // Avoid ADD/SUB if Kind is not VK_None, e.g. A@plt - B + C.
145 if (E.getSymA()->getKind() != MCSymbolRefExpr::VK_None)
146 return false;
147
148 // If either symbol is in a text section, we need to delay the relocation
149 // evaluation as relaxation may alter the size of the symbol.
150 //
151 // Unfortunately, we cannot identify if the symbol was built with relaxation
152 // as we do not track the state per symbol or section. However, BFD will
153 // always emit the relocation and so we follow suit which avoids the need to
154 // track that information.
155 if (A.isInSection() && A.getSection().getKind().isText())
156 return true;
157 if (B.isInSection() && B.getSection().getKind().isText())
158 return true;
159
160 // If A is undefined and B is defined, we should emit ADD/SUB for A-B.
161 // Unfortunately, A may be defined later, but this requiresFixups call has to
162 // eagerly make a decision. For now, emit ADD/SUB unless A is .L*. This
163 // heuristic handles many temporary label differences for .debug_* and
164 // .apple_types sections.
165 //
166 // TODO Implement delayed relocation decision.
167 if (!A.isInSection() && !A.isTemporary() && B.isInSection())
168 return true;
169
170 // Support cross-section symbolic differences ...
171 return A.isInSection() && B.isInSection() &&
172 A.getSection().getName() != B.getSection().getName();
173}
174
175void RISCVELFStreamer::reset() {
176 static_cast<RISCVTargetStreamer *>(getTargetStreamer())->reset();
178}
179
181 SMLoc Loc) {
182 const MCExpr *A, *B;
183 if (!requiresFixups(getContext(), Value, A, B))
185
187
188 MCDataFragment *DF = getOrCreateDataFragment();
189 flushPendingLabels(DF, DF->getContents().size());
190 MCDwarfLineEntry::make(this, getCurrentSectionOnly());
191
192 MCFixupKind Add, Sub;
193 std::tie(Add, Sub) = RISCV::getRelocPairForSize(Size);
194
195 DF->getFixups().push_back(
196 MCFixup::create(DF->getContents().size(), A, Add, Loc));
197 DF->getFixups().push_back(
198 MCFixup::create(DF->getContents().size(), B, Sub, Loc));
199
200 DF->getContents().resize(DF->getContents().size() + Size, 0);
201}
202
203namespace llvm {
205 std::unique_ptr<MCAsmBackend> MAB,
206 std::unique_ptr<MCObjectWriter> MOW,
207 std::unique_ptr<MCCodeEmitter> MCE,
208 bool RelaxAll) {
210 new RISCVELFStreamer(C, std::move(MAB), std::move(MOW), std::move(MCE));
211 S->getAssembler().setRelaxAll(RelaxAll);
212 return S;
213}
214} // namespace llvm
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
uint64_t Size
Value * RHS
Value * LHS
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.
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
unsigned getELFHeaderEFlags() const
ELF e_header flags.
Definition: MCAssembler.h:276
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:329
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:277
void setRelaxAll(bool Value)
Definition: MCAssembler.h:358
static const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.cpp:183
@ Add
Addition.
Definition: MCExpr.h:484
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
Context object for machine code objects.
Definition: MCContext.h:76
Fragment for data and encoded instructions.
Definition: MCFragment.h:241
static void make(MCStreamer *MCOS, MCSection *Section)
Definition: MCDwarf.cpp:91
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
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:87
MCAssembler & getAssembler()
Streaming machine code generation interface.
Definition: MCStreamer.h:212
virtual void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Emit the expression Value into the output as a native integer of the given Size bytes.
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
const FeatureBitset & getFeatureBits() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:386
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:95
This represents an "assembler immediate".
Definition: MCValue.h:36
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 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
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:676
@ EF_RISCV_RVC
Definition: ELF.h:670
@ EF_RISCV_TSO
Definition: ELF.h:677
@ EF_RISCV_FLOAT_ABI_SINGLE
Definition: ELF.h:673
@ EF_RISCV_FLOAT_ABI_DOUBLE
Definition: ELF.h:674
@ SHT_RISCV_ATTRIBUTES
Definition: ELF.h:1068
@ STO_RISCV_VARIANT_CC
Definition: ELF.h:688
ABI computeTargetABI(const Triple &TT, FeatureBitset FeatureBits, StringRef ABIName)
static std::pair< MCFixupKind, MCFixupKind > getRelocPairForSize(unsigned Size)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
@ Add
Sum of integers.
MCELFStreamer * createRISCVELFStreamer(MCContext &C, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > MOW, std::unique_ptr< MCCodeEmitter > MCE, bool RelaxAll)