MCWasmStreamer.cpp 8.9 KB

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