MCWasmStreamer.cpp 8.9 KB

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