WasmEmitter.cpp 23 KB

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