1 //===- lib/MC/MCWasmStreamer.cpp - Wasm Object Output ---------------------===//
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 assembles .s files and emits Wasm .o object files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/MC/MCWasmStreamer.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAsmLayout.h"
18 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCObjectStreamer.h"
24 #include "llvm/MC/MCSection.h"
25 #include "llvm/MC/MCSectionWasm.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MCSymbolWasm.h"
28 #include "llvm/MC/MCValue.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/raw_ostream.h"
34 
35 using namespace llvm;
36 
37 MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
38 
mergeFragment(MCDataFragment * DF,MCDataFragment * EF)39 void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) {
40   flushPendingLabels(DF, DF->getContents().size());
41 
42   for (unsigned I = 0, E = EF->getFixups().size(); I != E; ++I) {
43     EF->getFixups()[I].setOffset(EF->getFixups()[I].getOffset() +
44                                  DF->getContents().size());
45     DF->getFixups().push_back(EF->getFixups()[I]);
46   }
47   if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
48     DF->setHasInstructions(*EF->getSubtargetInfo());
49   DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
50 }
51 
emitAssemblerFlag(MCAssemblerFlag Flag)52 void MCWasmStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
53   // Let the target do whatever target specific stuff it needs to do.
54   getAssembler().getBackend().handleAssemblerFlag(Flag);
55 
56   // Do any generic stuff we need to do.
57   llvm_unreachable("invalid assembler flag!");
58 }
59 
changeSection(MCSection * Section,const MCExpr * Subsection)60 void MCWasmStreamer::changeSection(MCSection *Section,
61                                    const MCExpr *Subsection) {
62   MCAssembler &Asm = getAssembler();
63   auto *SectionWasm = cast<MCSectionWasm>(Section);
64   const MCSymbol *Grp = SectionWasm->getGroup();
65   if (Grp)
66     Asm.registerSymbol(*Grp);
67 
68   this->MCObjectStreamer::changeSection(Section, Subsection);
69   Asm.registerSymbol(*Section->getBeginSymbol());
70 }
71 
emitWeakReference(MCSymbol * Alias,const MCSymbol * Symbol)72 void MCWasmStreamer::emitWeakReference(MCSymbol *Alias,
73                                        const MCSymbol *Symbol) {
74   getAssembler().registerSymbol(*Symbol);
75   const MCExpr *Value = MCSymbolRefExpr::create(
76       Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
77   Alias->setVariableValue(Value);
78 }
79 
emitSymbolAttribute(MCSymbol * S,MCSymbolAttr Attribute)80 bool MCWasmStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
81   assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
82 
83   auto *Symbol = cast<MCSymbolWasm>(S);
84 
85   // Adding a symbol attribute always introduces the symbol; note that an
86   // important side effect of calling registerSymbol here is to register the
87   // symbol with the assembler.
88   getAssembler().registerSymbol(*Symbol);
89 
90   switch (Attribute) {
91   case MCSA_LazyReference:
92   case MCSA_Reference:
93   case MCSA_SymbolResolver:
94   case MCSA_PrivateExtern:
95   case MCSA_WeakDefinition:
96   case MCSA_WeakDefAutoPrivate:
97   case MCSA_Invalid:
98   case MCSA_IndirectSymbol:
99   case MCSA_Protected:
100     return false;
101 
102   case MCSA_Hidden:
103     Symbol->setHidden(true);
104     break;
105 
106   case MCSA_Weak:
107   case MCSA_WeakReference:
108     Symbol->setWeak(true);
109     Symbol->setExternal(true);
110     break;
111 
112   case MCSA_Global:
113     Symbol->setExternal(true);
114     break;
115 
116   case MCSA_ELF_TypeFunction:
117     Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
118     break;
119 
120   case MCSA_ELF_TypeObject:
121   case MCSA_Cold:
122     break;
123 
124   case MCSA_NoDeadStrip:
125     Symbol->setNoStrip();
126     break;
127 
128   default:
129     // unrecognized directive
130     llvm_unreachable("unexpected MCSymbolAttr");
131     return false;
132   }
133 
134   return true;
135 }
136 
emitCommonSymbol(MCSymbol * S,uint64_t Size,unsigned ByteAlignment)137 void MCWasmStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size,
138                                       unsigned ByteAlignment) {
139   llvm_unreachable("Common symbols are not yet implemented for Wasm");
140 }
141 
emitELFSize(MCSymbol * Symbol,const MCExpr * Value)142 void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
143   cast<MCSymbolWasm>(Symbol)->setSize(Value);
144 }
145 
emitLocalCommonSymbol(MCSymbol * S,uint64_t Size,unsigned ByteAlignment)146 void MCWasmStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
147                                            unsigned ByteAlignment) {
148   llvm_unreachable("Local common symbols are not yet implemented for Wasm");
149 }
150 
emitIdent(StringRef IdentString)151 void MCWasmStreamer::emitIdent(StringRef IdentString) {
152   // TODO(sbc): Add the ident section once we support mergable strings
153   // sections in the object format
154 }
155 
emitInstToFragment(const MCInst & Inst,const MCSubtargetInfo & STI)156 void MCWasmStreamer::emitInstToFragment(const MCInst &Inst,
157                                         const MCSubtargetInfo &STI) {
158   this->MCObjectStreamer::emitInstToFragment(Inst, STI);
159 }
160 
emitInstToData(const MCInst & Inst,const MCSubtargetInfo & STI)161 void MCWasmStreamer::emitInstToData(const MCInst &Inst,
162                                     const MCSubtargetInfo &STI) {
163   MCAssembler &Assembler = getAssembler();
164   SmallVector<MCFixup, 4> Fixups;
165   SmallString<256> Code;
166   raw_svector_ostream VecOS(Code);
167   Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
168 
169   // Append the encoded instruction to the current data fragment (or create a
170   // new such fragment if the current fragment is not a data fragment).
171   MCDataFragment *DF = getOrCreateDataFragment();
172 
173   // Add the fixups and data.
174   for (unsigned I = 0, E = Fixups.size(); I != E; ++I) {
175     Fixups[I].setOffset(Fixups[I].getOffset() + DF->getContents().size());
176     DF->getFixups().push_back(Fixups[I]);
177   }
178   DF->setHasInstructions(STI);
179   DF->getContents().append(Code.begin(), Code.end());
180 }
181 
finishImpl()182 void MCWasmStreamer::finishImpl() {
183   emitFrames(nullptr);
184 
185   this->MCObjectStreamer::finishImpl();
186 }
187 
createWasmStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> && MAB,std::unique_ptr<MCObjectWriter> && OW,std::unique_ptr<MCCodeEmitter> && CE,bool RelaxAll)188 MCStreamer *llvm::createWasmStreamer(MCContext &Context,
189                                      std::unique_ptr<MCAsmBackend> &&MAB,
190                                      std::unique_ptr<MCObjectWriter> &&OW,
191                                      std::unique_ptr<MCCodeEmitter> &&CE,
192                                      bool RelaxAll) {
193   MCWasmStreamer *S =
194       new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
195   if (RelaxAll)
196     S->getAssembler().setRelaxAll(true);
197   return S;
198 }
199 
emitThumbFunc(MCSymbol * Func)200 void MCWasmStreamer::emitThumbFunc(MCSymbol *Func) {
201   llvm_unreachable("Generic Wasm doesn't support this directive");
202 }
203 
emitSymbolDesc(MCSymbol * Symbol,unsigned DescValue)204 void MCWasmStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
205   llvm_unreachable("Wasm doesn't support this directive");
206 }
207 
emitZerofill(MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment,SMLoc Loc)208 void MCWasmStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
209                                   uint64_t Size, unsigned ByteAlignment,
210                                   SMLoc Loc) {
211   llvm_unreachable("Wasm doesn't support this directive");
212 }
213 
emitTBSSSymbol(MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)214 void MCWasmStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
215                                     uint64_t Size, unsigned ByteAlignment) {
216   llvm_unreachable("Wasm doesn't support this directive");
217 }
218