WasmEmitter.cpp 23 KB

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