WasmEmitter.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. //===- yaml2wasm - Convert YAML to a Wasm object file --------------------===//
  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. /// \file
  10. /// The Wasm component of yaml2obj.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. //
  14. #include "llvm/Object/Wasm.h"
  15. #include "llvm/ObjectYAML/ObjectYAML.h"
  16. #include "llvm/ObjectYAML/yaml2obj.h"
  17. #include "llvm/Support/Endian.h"
  18. #include "llvm/Support/LEB128.h"
  19. using namespace llvm;
  20. namespace {
  21. /// This parses a yaml stream that represents a Wasm object file.
  22. /// See docs/yaml2obj for the yaml scheema.
  23. class WasmWriter {
  24. public:
  25. WasmWriter(WasmYAML::Object &Obj, yaml::ErrorHandler EH)
  26. : Obj(Obj), ErrHandler(EH) {}
  27. bool writeWasm(raw_ostream &OS);
  28. private:
  29. void writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
  30. uint32_t SectionIndex);
  31. void writeInitExpr(raw_ostream &OS, const WasmYAML::InitExpr &InitExpr);
  32. void writeSectionContent(raw_ostream &OS, WasmYAML::CustomSection &Section);
  33. void writeSectionContent(raw_ostream &OS, WasmYAML::TypeSection &Section);
  34. void writeSectionContent(raw_ostream &OS, WasmYAML::ImportSection &Section);
  35. void writeSectionContent(raw_ostream &OS, WasmYAML::FunctionSection &Section);
  36. void writeSectionContent(raw_ostream &OS, WasmYAML::TableSection &Section);
  37. void writeSectionContent(raw_ostream &OS, WasmYAML::MemorySection &Section);
  38. void writeSectionContent(raw_ostream &OS, WasmYAML::TagSection &Section);
  39. void writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section);
  40. void writeSectionContent(raw_ostream &OS, WasmYAML::ExportSection &Section);
  41. void writeSectionContent(raw_ostream &OS, WasmYAML::StartSection &Section);
  42. void writeSectionContent(raw_ostream &OS, WasmYAML::ElemSection &Section);
  43. void writeSectionContent(raw_ostream &OS, WasmYAML::CodeSection &Section);
  44. void writeSectionContent(raw_ostream &OS, WasmYAML::DataSection &Section);
  45. void writeSectionContent(raw_ostream &OS, WasmYAML::DataCountSection &Section);
  46. // Custom section types
  47. void writeSectionContent(raw_ostream &OS, WasmYAML::DylinkSection &Section);
  48. void writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section);
  49. void writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section);
  50. void writeSectionContent(raw_ostream &OS, WasmYAML::ProducersSection &Section);
  51. void writeSectionContent(raw_ostream &OS,
  52. WasmYAML::TargetFeaturesSection &Section);
  53. WasmYAML::Object &Obj;
  54. uint32_t NumImportedFunctions = 0;
  55. uint32_t NumImportedGlobals = 0;
  56. uint32_t NumImportedTables = 0;
  57. uint32_t NumImportedTags = 0;
  58. bool HasError = false;
  59. yaml::ErrorHandler ErrHandler;
  60. void reportError(const Twine &Msg);
  61. };
  62. class SubSectionWriter {
  63. raw_ostream &OS;
  64. std::string OutString;
  65. raw_string_ostream StringStream;
  66. public:
  67. SubSectionWriter(raw_ostream &OS) : OS(OS), StringStream(OutString) {}
  68. void done() {
  69. StringStream.flush();
  70. encodeULEB128(OutString.size(), OS);
  71. OS << OutString;
  72. OutString.clear();
  73. }
  74. raw_ostream &getStream() { return StringStream; }
  75. };
  76. } // end anonymous namespace
  77. static int writeUint64(raw_ostream &OS, uint64_t Value) {
  78. char Data[sizeof(Value)];
  79. support::endian::write64le(Data, Value);
  80. OS.write(Data, sizeof(Data));
  81. return 0;
  82. }
  83. static int writeUint32(raw_ostream &OS, uint32_t Value) {
  84. char Data[sizeof(Value)];
  85. support::endian::write32le(Data, Value);
  86. OS.write(Data, sizeof(Data));
  87. return 0;
  88. }
  89. static int writeUint8(raw_ostream &OS, uint8_t Value) {
  90. char Data[sizeof(Value)];
  91. memcpy(Data, &Value, sizeof(Data));
  92. OS.write(Data, sizeof(Data));
  93. return 0;
  94. }
  95. static int writeStringRef(const StringRef &Str, raw_ostream &OS) {
  96. encodeULEB128(Str.size(), OS);
  97. OS << Str;
  98. return 0;
  99. }
  100. static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS) {
  101. writeUint8(OS, Lim.Flags);
  102. encodeULEB128(Lim.Minimum, OS);
  103. if (Lim.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
  104. encodeULEB128(Lim.Maximum, OS);
  105. return 0;
  106. }
  107. void WasmWriter::reportError(const Twine &Msg) {
  108. ErrHandler(Msg);
  109. HasError = true;
  110. }
  111. void WasmWriter::writeInitExpr(raw_ostream &OS,
  112. const WasmYAML::InitExpr &InitExpr) {
  113. if (InitExpr.Extended) {
  114. InitExpr.Body.writeAsBinary(OS);
  115. } else {
  116. writeUint8(OS, InitExpr.Inst.Opcode);
  117. switch (InitExpr.Inst.Opcode) {
  118. case wasm::WASM_OPCODE_I32_CONST:
  119. encodeSLEB128(InitExpr.Inst.Value.Int32, OS);
  120. break;
  121. case wasm::WASM_OPCODE_I64_CONST:
  122. encodeSLEB128(InitExpr.Inst.Value.Int64, OS);
  123. break;
  124. case wasm::WASM_OPCODE_F32_CONST:
  125. writeUint32(OS, InitExpr.Inst.Value.Float32);
  126. break;
  127. case wasm::WASM_OPCODE_F64_CONST:
  128. writeUint64(OS, InitExpr.Inst.Value.Float64);
  129. break;
  130. case wasm::WASM_OPCODE_GLOBAL_GET:
  131. encodeULEB128(InitExpr.Inst.Value.Global, OS);
  132. break;
  133. default:
  134. reportError("unknown opcode in init_expr: " +
  135. Twine(InitExpr.Inst.Opcode));
  136. return;
  137. }
  138. writeUint8(OS, wasm::WASM_OPCODE_END);
  139. }
  140. }
  141. void WasmWriter::writeSectionContent(raw_ostream &OS,
  142. WasmYAML::DylinkSection &Section) {
  143. writeStringRef(Section.Name, OS);
  144. writeUint8(OS, wasm::WASM_DYLINK_MEM_INFO);
  145. SubSectionWriter SubSection(OS);
  146. raw_ostream &SubOS = SubSection.getStream();
  147. encodeULEB128(Section.MemorySize, SubOS);
  148. encodeULEB128(Section.MemoryAlignment, SubOS);
  149. encodeULEB128(Section.TableSize, SubOS);
  150. encodeULEB128(Section.TableAlignment, SubOS);
  151. SubSection.done();
  152. if (Section.Needed.size()) {
  153. writeUint8(OS, wasm::WASM_DYLINK_NEEDED);
  154. raw_ostream &SubOS = SubSection.getStream();
  155. encodeULEB128(Section.Needed.size(), SubOS);
  156. for (StringRef Needed : Section.Needed)
  157. writeStringRef(Needed, SubOS);
  158. SubSection.done();
  159. }
  160. }
  161. void WasmWriter::writeSectionContent(raw_ostream &OS,
  162. WasmYAML::LinkingSection &Section) {
  163. writeStringRef(Section.Name, OS);
  164. encodeULEB128(Section.Version, OS);
  165. SubSectionWriter SubSection(OS);
  166. // SYMBOL_TABLE subsection
  167. if (Section.SymbolTable.size()) {
  168. writeUint8(OS, wasm::WASM_SYMBOL_TABLE);
  169. encodeULEB128(Section.SymbolTable.size(), SubSection.getStream());
  170. for (auto Sym : llvm::enumerate(Section.SymbolTable)) {
  171. const WasmYAML::SymbolInfo &Info = Sym.value();
  172. assert(Info.Index == Sym.index());
  173. writeUint8(SubSection.getStream(), Info.Kind);
  174. encodeULEB128(Info.Flags, SubSection.getStream());
  175. switch (Info.Kind) {
  176. case wasm::WASM_SYMBOL_TYPE_FUNCTION:
  177. case wasm::WASM_SYMBOL_TYPE_GLOBAL:
  178. case wasm::WASM_SYMBOL_TYPE_TABLE:
  179. case wasm::WASM_SYMBOL_TYPE_TAG:
  180. encodeULEB128(Info.ElementIndex, SubSection.getStream());
  181. if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0 ||
  182. (Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0)
  183. writeStringRef(Info.Name, SubSection.getStream());
  184. break;
  185. case wasm::WASM_SYMBOL_TYPE_DATA:
  186. writeStringRef(Info.Name, SubSection.getStream());
  187. if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
  188. encodeULEB128(Info.DataRef.Segment, SubSection.getStream());
  189. encodeULEB128(Info.DataRef.Offset, SubSection.getStream());
  190. encodeULEB128(Info.DataRef.Size, SubSection.getStream());
  191. }
  192. break;
  193. case wasm::WASM_SYMBOL_TYPE_SECTION:
  194. encodeULEB128(Info.ElementIndex, SubSection.getStream());
  195. break;
  196. default:
  197. llvm_unreachable("unexpected kind");
  198. }
  199. }
  200. SubSection.done();
  201. }
  202. // SEGMENT_NAMES subsection
  203. if (Section.SegmentInfos.size()) {
  204. writeUint8(OS, wasm::WASM_SEGMENT_INFO);
  205. encodeULEB128(Section.SegmentInfos.size(), SubSection.getStream());
  206. for (const WasmYAML::SegmentInfo &SegmentInfo : Section.SegmentInfos) {
  207. writeStringRef(SegmentInfo.Name, SubSection.getStream());
  208. encodeULEB128(SegmentInfo.Alignment, SubSection.getStream());
  209. encodeULEB128(SegmentInfo.Flags, SubSection.getStream());
  210. }
  211. SubSection.done();
  212. }
  213. // INIT_FUNCS subsection
  214. if (Section.InitFunctions.size()) {
  215. writeUint8(OS, wasm::WASM_INIT_FUNCS);
  216. encodeULEB128(Section.InitFunctions.size(), SubSection.getStream());
  217. for (const WasmYAML::InitFunction &Func : Section.InitFunctions) {
  218. encodeULEB128(Func.Priority, SubSection.getStream());
  219. encodeULEB128(Func.Symbol, SubSection.getStream());
  220. }
  221. SubSection.done();
  222. }
  223. // COMDAT_INFO subsection
  224. if (Section.Comdats.size()) {
  225. writeUint8(OS, wasm::WASM_COMDAT_INFO);
  226. encodeULEB128(Section.Comdats.size(), SubSection.getStream());
  227. for (const auto &C : Section.Comdats) {
  228. writeStringRef(C.Name, SubSection.getStream());
  229. encodeULEB128(0, SubSection.getStream()); // flags for future use
  230. encodeULEB128(C.Entries.size(), SubSection.getStream());
  231. for (const WasmYAML::ComdatEntry &Entry : C.Entries) {
  232. writeUint8(SubSection.getStream(), Entry.Kind);
  233. encodeULEB128(Entry.Index, SubSection.getStream());
  234. }
  235. }
  236. SubSection.done();
  237. }
  238. }
  239. void WasmWriter::writeSectionContent(raw_ostream &OS,
  240. WasmYAML::NameSection &Section) {
  241. writeStringRef(Section.Name, OS);
  242. if (Section.FunctionNames.size()) {
  243. writeUint8(OS, wasm::WASM_NAMES_FUNCTION);
  244. SubSectionWriter SubSection(OS);
  245. encodeULEB128(Section.FunctionNames.size(), SubSection.getStream());
  246. for (const WasmYAML::NameEntry &NameEntry : Section.FunctionNames) {
  247. encodeULEB128(NameEntry.Index, SubSection.getStream());
  248. writeStringRef(NameEntry.Name, SubSection.getStream());
  249. }
  250. SubSection.done();
  251. }
  252. if (Section.GlobalNames.size()) {
  253. writeUint8(OS, wasm::WASM_NAMES_GLOBAL);
  254. SubSectionWriter SubSection(OS);
  255. encodeULEB128(Section.GlobalNames.size(), SubSection.getStream());
  256. for (const WasmYAML::NameEntry &NameEntry : Section.GlobalNames) {
  257. encodeULEB128(NameEntry.Index, SubSection.getStream());
  258. writeStringRef(NameEntry.Name, SubSection.getStream());
  259. }
  260. SubSection.done();
  261. }
  262. if (Section.DataSegmentNames.size()) {
  263. writeUint8(OS, wasm::WASM_NAMES_DATA_SEGMENT);
  264. SubSectionWriter SubSection(OS);
  265. encodeULEB128(Section.DataSegmentNames.size(), SubSection.getStream());
  266. for (const WasmYAML::NameEntry &NameEntry : Section.DataSegmentNames) {
  267. encodeULEB128(NameEntry.Index, SubSection.getStream());
  268. writeStringRef(NameEntry.Name, SubSection.getStream());
  269. }
  270. SubSection.done();
  271. }
  272. }
  273. void WasmWriter::writeSectionContent(raw_ostream &OS,
  274. WasmYAML::ProducersSection &Section) {
  275. writeStringRef(Section.Name, OS);
  276. int Fields = int(!Section.Languages.empty()) + int(!Section.Tools.empty()) +
  277. int(!Section.SDKs.empty());
  278. if (Fields == 0)
  279. return;
  280. encodeULEB128(Fields, OS);
  281. for (auto &Field : {std::make_pair(StringRef("language"), &Section.Languages),
  282. std::make_pair(StringRef("processed-by"), &Section.Tools),
  283. std::make_pair(StringRef("sdk"), &Section.SDKs)}) {
  284. if (Field.second->empty())
  285. continue;
  286. writeStringRef(Field.first, OS);
  287. encodeULEB128(Field.second->size(), OS);
  288. for (auto &Entry : *Field.second) {
  289. writeStringRef(Entry.Name, OS);
  290. writeStringRef(Entry.Version, OS);
  291. }
  292. }
  293. }
  294. void WasmWriter::writeSectionContent(raw_ostream &OS,
  295. WasmYAML::TargetFeaturesSection &Section) {
  296. writeStringRef(Section.Name, OS);
  297. encodeULEB128(Section.Features.size(), OS);
  298. for (auto &E : Section.Features) {
  299. writeUint8(OS, E.Prefix);
  300. writeStringRef(E.Name, OS);
  301. }
  302. }
  303. void WasmWriter::writeSectionContent(raw_ostream &OS,
  304. WasmYAML::CustomSection &Section) {
  305. if (auto S = dyn_cast<WasmYAML::DylinkSection>(&Section)) {
  306. writeSectionContent(OS, *S);
  307. } else if (auto S = dyn_cast<WasmYAML::NameSection>(&Section)) {
  308. writeSectionContent(OS, *S);
  309. } else if (auto S = dyn_cast<WasmYAML::LinkingSection>(&Section)) {
  310. writeSectionContent(OS, *S);
  311. } else if (auto S = dyn_cast<WasmYAML::ProducersSection>(&Section)) {
  312. writeSectionContent(OS, *S);
  313. } else if (auto S = dyn_cast<WasmYAML::TargetFeaturesSection>(&Section)) {
  314. writeSectionContent(OS, *S);
  315. } else {
  316. writeStringRef(Section.Name, OS);
  317. Section.Payload.writeAsBinary(OS);
  318. }
  319. }
  320. void WasmWriter::writeSectionContent(raw_ostream &OS,
  321. WasmYAML::TypeSection &Section) {
  322. encodeULEB128(Section.Signatures.size(), OS);
  323. uint32_t ExpectedIndex = 0;
  324. for (const WasmYAML::Signature &Sig : Section.Signatures) {
  325. if (Sig.Index != ExpectedIndex) {
  326. reportError("unexpected type index: " + Twine(Sig.Index));
  327. return;
  328. }
  329. ++ExpectedIndex;
  330. writeUint8(OS, Sig.Form);
  331. encodeULEB128(Sig.ParamTypes.size(), OS);
  332. for (auto ParamType : Sig.ParamTypes)
  333. writeUint8(OS, ParamType);
  334. encodeULEB128(Sig.ReturnTypes.size(), OS);
  335. for (auto ReturnType : Sig.ReturnTypes)
  336. writeUint8(OS, ReturnType);
  337. }
  338. }
  339. void WasmWriter::writeSectionContent(raw_ostream &OS,
  340. WasmYAML::ImportSection &Section) {
  341. encodeULEB128(Section.Imports.size(), OS);
  342. for (const WasmYAML::Import &Import : Section.Imports) {
  343. writeStringRef(Import.Module, OS);
  344. writeStringRef(Import.Field, OS);
  345. writeUint8(OS, Import.Kind);
  346. switch (Import.Kind) {
  347. case wasm::WASM_EXTERNAL_FUNCTION:
  348. encodeULEB128(Import.SigIndex, OS);
  349. NumImportedFunctions++;
  350. break;
  351. case wasm::WASM_EXTERNAL_GLOBAL:
  352. writeUint8(OS, Import.GlobalImport.Type);
  353. writeUint8(OS, Import.GlobalImport.Mutable);
  354. NumImportedGlobals++;
  355. break;
  356. case wasm::WASM_EXTERNAL_TAG:
  357. writeUint8(OS, 0); // Reserved 'attribute' field
  358. encodeULEB128(Import.SigIndex, OS);
  359. NumImportedTags++;
  360. break;
  361. case wasm::WASM_EXTERNAL_MEMORY:
  362. writeLimits(Import.Memory, OS);
  363. break;
  364. case wasm::WASM_EXTERNAL_TABLE:
  365. writeUint8(OS, Import.TableImport.ElemType);
  366. writeLimits(Import.TableImport.TableLimits, OS);
  367. NumImportedTables++;
  368. break;
  369. default:
  370. reportError("unknown import type: " +Twine(Import.Kind));
  371. return;
  372. }
  373. }
  374. }
  375. void WasmWriter::writeSectionContent(raw_ostream &OS,
  376. WasmYAML::FunctionSection &Section) {
  377. encodeULEB128(Section.FunctionTypes.size(), OS);
  378. for (uint32_t FuncType : Section.FunctionTypes)
  379. encodeULEB128(FuncType, OS);
  380. }
  381. void WasmWriter::writeSectionContent(raw_ostream &OS,
  382. WasmYAML::ExportSection &Section) {
  383. encodeULEB128(Section.Exports.size(), OS);
  384. for (const WasmYAML::Export &Export : Section.Exports) {
  385. writeStringRef(Export.Name, OS);
  386. writeUint8(OS, Export.Kind);
  387. encodeULEB128(Export.Index, OS);
  388. }
  389. }
  390. void WasmWriter::writeSectionContent(raw_ostream &OS,
  391. WasmYAML::StartSection &Section) {
  392. encodeULEB128(Section.StartFunction, OS);
  393. }
  394. void WasmWriter::writeSectionContent(raw_ostream &OS,
  395. WasmYAML::TableSection &Section) {
  396. encodeULEB128(Section.Tables.size(), OS);
  397. uint32_t ExpectedIndex = NumImportedTables;
  398. for (auto &Table : Section.Tables) {
  399. if (Table.Index != ExpectedIndex) {
  400. reportError("unexpected table index: " + Twine(Table.Index));
  401. return;
  402. }
  403. ++ExpectedIndex;
  404. writeUint8(OS, Table.ElemType);
  405. writeLimits(Table.TableLimits, OS);
  406. }
  407. }
  408. void WasmWriter::writeSectionContent(raw_ostream &OS,
  409. WasmYAML::MemorySection &Section) {
  410. encodeULEB128(Section.Memories.size(), OS);
  411. for (const WasmYAML::Limits &Mem : Section.Memories)
  412. writeLimits(Mem, OS);
  413. }
  414. void WasmWriter::writeSectionContent(raw_ostream &OS,
  415. WasmYAML::TagSection &Section) {
  416. encodeULEB128(Section.TagTypes.size(), OS);
  417. for (uint32_t TagType : Section.TagTypes) {
  418. writeUint8(OS, 0); // Reserved 'attribute' field
  419. encodeULEB128(TagType, OS);
  420. }
  421. }
  422. void WasmWriter::writeSectionContent(raw_ostream &OS,
  423. WasmYAML::GlobalSection &Section) {
  424. encodeULEB128(Section.Globals.size(), OS);
  425. uint32_t ExpectedIndex = NumImportedGlobals;
  426. for (auto &Global : Section.Globals) {
  427. if (Global.Index != ExpectedIndex) {
  428. reportError("unexpected global index: " + Twine(Global.Index));
  429. return;
  430. }
  431. ++ExpectedIndex;
  432. writeUint8(OS, Global.Type);
  433. writeUint8(OS, Global.Mutable);
  434. writeInitExpr(OS, Global.Init);
  435. }
  436. }
  437. void WasmWriter::writeSectionContent(raw_ostream &OS,
  438. WasmYAML::ElemSection &Section) {
  439. encodeULEB128(Section.Segments.size(), OS);
  440. for (auto &Segment : Section.Segments) {
  441. encodeULEB128(Segment.Flags, OS);
  442. if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER)
  443. encodeULEB128(Segment.TableNumber, OS);
  444. writeInitExpr(OS, Segment.Offset);
  445. if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND) {
  446. // We only support active function table initializers, for which the elem
  447. // kind is specified to be written as 0x00 and interpreted to mean
  448. // "funcref".
  449. if (Segment.ElemKind != uint32_t(wasm::ValType::FUNCREF)) {
  450. reportError("unexpected elemkind: " + Twine(Segment.ElemKind));
  451. return;
  452. }
  453. const uint8_t ElemKind = 0;
  454. writeUint8(OS, ElemKind);
  455. }
  456. encodeULEB128(Segment.Functions.size(), OS);
  457. for (auto &Function : Segment.Functions)
  458. encodeULEB128(Function, OS);
  459. }
  460. }
  461. void WasmWriter::writeSectionContent(raw_ostream &OS,
  462. WasmYAML::CodeSection &Section) {
  463. encodeULEB128(Section.Functions.size(), OS);
  464. uint32_t ExpectedIndex = NumImportedFunctions;
  465. for (auto &Func : Section.Functions) {
  466. std::string OutString;
  467. raw_string_ostream StringStream(OutString);
  468. if (Func.Index != ExpectedIndex) {
  469. reportError("unexpected function index: " + Twine(Func.Index));
  470. return;
  471. }
  472. ++ExpectedIndex;
  473. encodeULEB128(Func.Locals.size(), StringStream);
  474. for (auto &LocalDecl : Func.Locals) {
  475. encodeULEB128(LocalDecl.Count, StringStream);
  476. writeUint8(StringStream, LocalDecl.Type);
  477. }
  478. Func.Body.writeAsBinary(StringStream);
  479. // Write the section size followed by the content
  480. StringStream.flush();
  481. encodeULEB128(OutString.size(), OS);
  482. OS << OutString;
  483. }
  484. }
  485. void WasmWriter::writeSectionContent(raw_ostream &OS,
  486. WasmYAML::DataSection &Section) {
  487. encodeULEB128(Section.Segments.size(), OS);
  488. for (auto &Segment : Section.Segments) {
  489. encodeULEB128(Segment.InitFlags, OS);
  490. if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX)
  491. encodeULEB128(Segment.MemoryIndex, OS);
  492. if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0)
  493. writeInitExpr(OS, Segment.Offset);
  494. encodeULEB128(Segment.Content.binary_size(), OS);
  495. Segment.Content.writeAsBinary(OS);
  496. }
  497. }
  498. void WasmWriter::writeSectionContent(raw_ostream &OS,
  499. WasmYAML::DataCountSection &Section) {
  500. encodeULEB128(Section.Count, OS);
  501. }
  502. void WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
  503. uint32_t SectionIndex) {
  504. switch (Sec.Type) {
  505. case wasm::WASM_SEC_CODE:
  506. writeStringRef("reloc.CODE", OS);
  507. break;
  508. case wasm::WASM_SEC_DATA:
  509. writeStringRef("reloc.DATA", OS);
  510. break;
  511. case wasm::WASM_SEC_CUSTOM: {
  512. auto *CustomSection = cast<WasmYAML::CustomSection>(&Sec);
  513. writeStringRef(("reloc." + CustomSection->Name).str(), OS);
  514. break;
  515. }
  516. default:
  517. llvm_unreachable("not yet implemented");
  518. }
  519. encodeULEB128(SectionIndex, OS);
  520. encodeULEB128(Sec.Relocations.size(), OS);
  521. for (auto Reloc : Sec.Relocations) {
  522. writeUint8(OS, Reloc.Type);
  523. encodeULEB128(Reloc.Offset, OS);
  524. encodeULEB128(Reloc.Index, OS);
  525. if (wasm::relocTypeHasAddend(Reloc.Type))
  526. encodeSLEB128(Reloc.Addend, OS);
  527. }
  528. }
  529. bool WasmWriter::writeWasm(raw_ostream &OS) {
  530. // Write headers
  531. OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic));
  532. writeUint32(OS, Obj.Header.Version);
  533. // Write each section
  534. llvm::object::WasmSectionOrderChecker Checker;
  535. for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
  536. StringRef SecName = "";
  537. if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
  538. SecName = S->Name;
  539. if (!Checker.isValidSectionOrder(Sec->Type, SecName)) {
  540. reportError("out of order section type: " + Twine(Sec->Type));
  541. return false;
  542. }
  543. encodeULEB128(Sec->Type, OS);
  544. std::string OutString;
  545. raw_string_ostream StringStream(OutString);
  546. if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
  547. writeSectionContent(StringStream, *S);
  548. else if (auto S = dyn_cast<WasmYAML::TypeSection>(Sec.get()))
  549. writeSectionContent(StringStream, *S);
  550. else if (auto S = dyn_cast<WasmYAML::ImportSection>(Sec.get()))
  551. writeSectionContent(StringStream, *S);
  552. else if (auto S = dyn_cast<WasmYAML::FunctionSection>(Sec.get()))
  553. writeSectionContent(StringStream, *S);
  554. else if (auto S = dyn_cast<WasmYAML::TableSection>(Sec.get()))
  555. writeSectionContent(StringStream, *S);
  556. else if (auto S = dyn_cast<WasmYAML::MemorySection>(Sec.get()))
  557. writeSectionContent(StringStream, *S);
  558. else if (auto S = dyn_cast<WasmYAML::TagSection>(Sec.get()))
  559. writeSectionContent(StringStream, *S);
  560. else if (auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get()))
  561. writeSectionContent(StringStream, *S);
  562. else if (auto S = dyn_cast<WasmYAML::ExportSection>(Sec.get()))
  563. writeSectionContent(StringStream, *S);
  564. else if (auto S = dyn_cast<WasmYAML::StartSection>(Sec.get()))
  565. writeSectionContent(StringStream, *S);
  566. else if (auto S = dyn_cast<WasmYAML::ElemSection>(Sec.get()))
  567. writeSectionContent(StringStream, *S);
  568. else if (auto S = dyn_cast<WasmYAML::CodeSection>(Sec.get()))
  569. writeSectionContent(StringStream, *S);
  570. else if (auto S = dyn_cast<WasmYAML::DataSection>(Sec.get()))
  571. writeSectionContent(StringStream, *S);
  572. else if (auto S = dyn_cast<WasmYAML::DataCountSection>(Sec.get()))
  573. writeSectionContent(StringStream, *S);
  574. else
  575. reportError("unknown section type: " + Twine(Sec->Type));
  576. if (HasError)
  577. return false;
  578. StringStream.flush();
  579. // Write the section size followed by the content
  580. encodeULEB128(OutString.size(), OS);
  581. OS << OutString;
  582. }
  583. // write reloc sections for any section that have relocations
  584. uint32_t SectionIndex = 0;
  585. for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
  586. if (Sec->Relocations.empty()) {
  587. SectionIndex++;
  588. continue;
  589. }
  590. writeUint8(OS, wasm::WASM_SEC_CUSTOM);
  591. std::string OutString;
  592. raw_string_ostream StringStream(OutString);
  593. writeRelocSection(StringStream, *Sec, SectionIndex++);
  594. StringStream.flush();
  595. encodeULEB128(OutString.size(), OS);
  596. OS << OutString;
  597. }
  598. return true;
  599. }
  600. namespace llvm {
  601. namespace yaml {
  602. bool yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH) {
  603. WasmWriter Writer(Doc, EH);
  604. return Writer.writeWasm(Out);
  605. }
  606. } // namespace yaml
  607. } // namespace llvm