1 //===- lib/MC/MCWasmStreamer.cpp - Wasm Object Output ---------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file assembles .s files and emits Wasm .o object files.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/MC/MCWasmStreamer.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAssembler.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCFixup.h"
21 #include "llvm/MC/MCFragment.h"
22 #include "llvm/MC/MCObjectStreamer.h"
23 #include "llvm/MC/MCSection.h"
24 #include "llvm/MC/MCSectionWasm.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCSymbolWasm.h"
27 #include "llvm/MC/TargetRegistry.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/ErrorHandling.h"
34 class MCSubtargetInfo
;
39 MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
41 void MCWasmStreamer::emitLabel(MCSymbol
*S
, SMLoc Loc
) {
42 auto *Symbol
= cast
<MCSymbolWasm
>(S
);
43 MCObjectStreamer::emitLabel(Symbol
, Loc
);
45 const MCSectionWasm
&Section
=
46 static_cast<const MCSectionWasm
&>(*getCurrentSectionOnly());
47 if (Section
.getSegmentFlags() & wasm::WASM_SEG_FLAG_TLS
)
51 void MCWasmStreamer::emitLabelAtPos(MCSymbol
*S
, SMLoc Loc
, MCDataFragment
&F
,
53 auto *Symbol
= cast
<MCSymbolWasm
>(S
);
54 MCObjectStreamer::emitLabelAtPos(Symbol
, Loc
, F
, Offset
);
56 const MCSectionWasm
&Section
=
57 static_cast<const MCSectionWasm
&>(*getCurrentSectionOnly());
58 if (Section
.getSegmentFlags() & wasm::WASM_SEG_FLAG_TLS
)
62 void MCWasmStreamer::emitAssemblerFlag(MCAssemblerFlag Flag
) {
63 // Let the target do whatever target specific stuff it needs to do.
64 getAssembler().getBackend().handleAssemblerFlag(Flag
);
66 // Do any generic stuff we need to do.
67 llvm_unreachable("invalid assembler flag!");
70 void MCWasmStreamer::changeSection(MCSection
*Section
, uint32_t Subsection
) {
71 MCAssembler
&Asm
= getAssembler();
72 auto *SectionWasm
= cast
<MCSectionWasm
>(Section
);
73 const MCSymbol
*Grp
= SectionWasm
->getGroup();
75 Asm
.registerSymbol(*Grp
);
77 this->MCObjectStreamer::changeSection(Section
, Subsection
);
78 Asm
.registerSymbol(*Section
->getBeginSymbol());
81 void MCWasmStreamer::emitWeakReference(MCSymbol
*Alias
,
82 const MCSymbol
*Symbol
) {
83 getAssembler().registerSymbol(*Symbol
);
84 const MCExpr
*Value
= MCSymbolRefExpr::create(
85 Symbol
, MCSymbolRefExpr::VK_WEAKREF
, getContext());
86 Alias
->setVariableValue(Value
);
89 bool MCWasmStreamer::emitSymbolAttribute(MCSymbol
*S
, MCSymbolAttr Attribute
) {
90 assert(Attribute
!= MCSA_IndirectSymbol
&& "indirect symbols not supported");
92 auto *Symbol
= cast
<MCSymbolWasm
>(S
);
94 // Adding a symbol attribute always introduces the symbol; note that an
95 // important side effect of calling registerSymbol here is to register the
96 // symbol with the assembler.
97 getAssembler().registerSymbol(*Symbol
);
100 case MCSA_LazyReference
:
102 case MCSA_SymbolResolver
:
103 case MCSA_PrivateExtern
:
104 case MCSA_WeakDefinition
:
105 case MCSA_WeakDefAutoPrivate
:
107 case MCSA_IndirectSymbol
:
113 Symbol
->setHidden(true);
117 case MCSA_WeakReference
:
118 Symbol
->setWeak(true);
119 Symbol
->setExternal(true);
123 Symbol
->setExternal(true);
126 case MCSA_ELF_TypeFunction
:
127 Symbol
->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION
);
130 case MCSA_ELF_TypeTLS
:
134 case MCSA_ELF_TypeObject
:
138 case MCSA_NoDeadStrip
:
139 Symbol
->setNoStrip();
143 // unrecognized directive
144 llvm_unreachable("unexpected MCSymbolAttr");
151 void MCWasmStreamer::emitCommonSymbol(MCSymbol
*S
, uint64_t Size
,
152 Align ByteAlignment
) {
153 llvm_unreachable("Common symbols are not yet implemented for Wasm");
156 void MCWasmStreamer::emitELFSize(MCSymbol
*Symbol
, const MCExpr
*Value
) {
157 cast
<MCSymbolWasm
>(Symbol
)->setSize(Value
);
160 void MCWasmStreamer::emitLocalCommonSymbol(MCSymbol
*S
, uint64_t Size
,
161 Align ByteAlignment
) {
162 llvm_unreachable("Local common symbols are not yet implemented for Wasm");
165 void MCWasmStreamer::emitIdent(StringRef IdentString
) {
166 // TODO(sbc): Add the ident section once we support mergable strings
167 // sections in the object format
170 void MCWasmStreamer::emitInstToFragment(const MCInst
&Inst
,
171 const MCSubtargetInfo
&STI
) {
172 this->MCObjectStreamer::emitInstToFragment(Inst
, STI
);
173 MCRelaxableFragment
&F
= *cast
<MCRelaxableFragment
>(getCurrentFragment());
175 for (auto &Fixup
: F
.getFixups())
176 fixSymbolsInTLSFixups(Fixup
.getValue());
179 void MCWasmStreamer::emitInstToData(const MCInst
&Inst
,
180 const MCSubtargetInfo
&STI
) {
181 MCAssembler
&Assembler
= getAssembler();
182 SmallVector
<MCFixup
, 4> Fixups
;
183 SmallString
<256> Code
;
184 Assembler
.getEmitter().encodeInstruction(Inst
, Code
, Fixups
, STI
);
186 for (auto &Fixup
: Fixups
)
187 fixSymbolsInTLSFixups(Fixup
.getValue());
189 // Append the encoded instruction to the current data fragment (or create a
190 // new such fragment if the current fragment is not a data fragment).
191 MCDataFragment
*DF
= getOrCreateDataFragment();
193 // Add the fixups and data.
194 for (unsigned I
= 0, E
= Fixups
.size(); I
!= E
; ++I
) {
195 Fixups
[I
].setOffset(Fixups
[I
].getOffset() + DF
->getContents().size());
196 DF
->getFixups().push_back(Fixups
[I
]);
198 DF
->setHasInstructions(STI
);
199 DF
->getContents().append(Code
.begin(), Code
.end());
202 void MCWasmStreamer::finishImpl() {
205 this->MCObjectStreamer::finishImpl();
208 void MCWasmStreamer::fixSymbolsInTLSFixups(const MCExpr
*expr
) {
209 switch (expr
->getKind()) {
211 case MCExpr::Constant
:
214 case MCExpr::Binary
: {
215 const MCBinaryExpr
*be
= cast
<MCBinaryExpr
>(expr
);
216 fixSymbolsInTLSFixups(be
->getLHS());
217 fixSymbolsInTLSFixups(be
->getRHS());
221 case MCExpr::SymbolRef
: {
222 const MCSymbolRefExpr
&symRef
= *cast
<MCSymbolRefExpr
>(expr
);
223 switch (symRef
.getKind()) {
224 case MCSymbolRefExpr::VK_WASM_TLSREL
:
225 case MCSymbolRefExpr::VK_WASM_GOT_TLS
:
226 getAssembler().registerSymbol(symRef
.getSymbol());
227 cast
<MCSymbolWasm
>(symRef
.getSymbol()).setTLS();
236 fixSymbolsInTLSFixups(cast
<MCUnaryExpr
>(expr
)->getSubExpr());
241 void MCWasmStreamer::emitThumbFunc(MCSymbol
*Func
) {
242 llvm_unreachable("Generic Wasm doesn't support this directive");
245 void MCWasmStreamer::emitSymbolDesc(MCSymbol
*Symbol
, unsigned DescValue
) {
246 llvm_unreachable("Wasm doesn't support this directive");
249 void MCWasmStreamer::emitZerofill(MCSection
*Section
, MCSymbol
*Symbol
,
250 uint64_t Size
, Align ByteAlignment
,
252 llvm_unreachable("Wasm doesn't support this directive");
255 void MCWasmStreamer::emitTBSSSymbol(MCSection
*Section
, MCSymbol
*Symbol
,
256 uint64_t Size
, Align ByteAlignment
) {
257 llvm_unreachable("Wasm doesn't support this directive");
260 MCStreamer
*llvm::createWasmStreamer(MCContext
&Context
,
261 std::unique_ptr
<MCAsmBackend
> &&MAB
,
262 std::unique_ptr
<MCObjectWriter
> &&OW
,
263 std::unique_ptr
<MCCodeEmitter
> &&CE
) {
265 new MCWasmStreamer(Context
, std::move(MAB
), std::move(OW
), std::move(CE
));