blob: 9b2a98d804845a9282786873b13cd11ca435f95f [file] [log] [blame]
Derek Schuffd3d84fd2017-03-30 19:44:09 +00001//===- yaml2wasm - Convert YAML to a Wasm object file --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief The Wasm component of yaml2obj.
12///
13//===----------------------------------------------------------------------===//
14//
Sam Clegg6c899ba2018-02-23 05:08:34 +000015
Derek Schuffd3d84fd2017-03-30 19:44:09 +000016#include "llvm/ObjectYAML/ObjectYAML.h"
17#include "llvm/Support/Endian.h"
18#include "llvm/Support/LEB128.h"
19
20using namespace llvm;
21
22/// This parses a yaml stream that represents a Wasm object file.
23/// See docs/yaml2obj for the yaml scheema.
24class WasmWriter {
25public:
26 WasmWriter(WasmYAML::Object &Obj) : Obj(Obj) {}
27 int writeWasm(raw_ostream &OS);
Sam Cleggb7787fd2017-06-20 04:04:59 +000028
29private:
Sam Clegg6f08c842018-04-24 18:11:36 +000030 int writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
31 uint32_t SectionIndex);
Sam Clegg03cdd122017-05-05 18:12:34 +000032
Derek Schuffd3d84fd2017-03-30 19:44:09 +000033 int writeSectionContent(raw_ostream &OS, WasmYAML::CustomSection &Section);
34 int writeSectionContent(raw_ostream &OS, WasmYAML::TypeSection &Section);
35 int writeSectionContent(raw_ostream &OS, WasmYAML::ImportSection &Section);
36 int writeSectionContent(raw_ostream &OS, WasmYAML::FunctionSection &Section);
37 int writeSectionContent(raw_ostream &OS, WasmYAML::TableSection &Section);
38 int writeSectionContent(raw_ostream &OS, WasmYAML::MemorySection &Section);
39 int writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section);
40 int writeSectionContent(raw_ostream &OS, WasmYAML::ExportSection &Section);
41 int writeSectionContent(raw_ostream &OS, WasmYAML::StartSection &Section);
42 int writeSectionContent(raw_ostream &OS, WasmYAML::ElemSection &Section);
43 int writeSectionContent(raw_ostream &OS, WasmYAML::CodeSection &Section);
44 int writeSectionContent(raw_ostream &OS, WasmYAML::DataSection &Section);
45
Sam Cleggb7787fd2017-06-20 04:04:59 +000046 // Custom section types
47 int writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section);
48 int writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section);
Derek Schuffd3d84fd2017-03-30 19:44:09 +000049 WasmYAML::Object &Obj;
Sam Clegge53af7f2018-01-09 21:38:53 +000050 uint32_t NumImportedFunctions = 0;
51 uint32_t NumImportedGlobals = 0;
Derek Schuffd3d84fd2017-03-30 19:44:09 +000052};
53
54static int writeUint64(raw_ostream &OS, uint64_t Value) {
55 char Data[sizeof(Value)];
56 support::endian::write64le(Data, Value);
57 OS.write(Data, sizeof(Data));
58 return 0;
59}
60
61static int writeUint32(raw_ostream &OS, uint32_t Value) {
62 char Data[sizeof(Value)];
63 support::endian::write32le(Data, Value);
64 OS.write(Data, sizeof(Data));
65 return 0;
66}
67
68static int writeUint8(raw_ostream &OS, uint8_t Value) {
69 char Data[sizeof(Value)];
70 memcpy(Data, &Value, sizeof(Data));
71 OS.write(Data, sizeof(Data));
72 return 0;
73}
74
Sam Clegg03cdd122017-05-05 18:12:34 +000075static int writeStringRef(const StringRef &Str, raw_ostream &OS) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +000076 encodeULEB128(Str.size(), OS);
77 OS << Str;
78 return 0;
79}
80
Sam Clegg03cdd122017-05-05 18:12:34 +000081static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS) {
Sam Clegg03e101f2018-03-01 18:06:21 +000082 writeUint8(OS, Lim.Flags);
Derek Schuffd3d84fd2017-03-30 19:44:09 +000083 encodeULEB128(Lim.Initial, OS);
84 if (Lim.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
85 encodeULEB128(Lim.Maximum, OS);
86 return 0;
87}
88
Sam Clegg03cdd122017-05-05 18:12:34 +000089static int writeInitExpr(const wasm::WasmInitExpr &InitExpr, raw_ostream &OS) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +000090 writeUint8(OS, InitExpr.Opcode);
91 switch (InitExpr.Opcode) {
92 case wasm::WASM_OPCODE_I32_CONST:
93 encodeSLEB128(InitExpr.Value.Int32, OS);
94 break;
95 case wasm::WASM_OPCODE_I64_CONST:
96 encodeSLEB128(InitExpr.Value.Int64, OS);
97 break;
98 case wasm::WASM_OPCODE_F32_CONST:
99 writeUint32(OS, InitExpr.Value.Float32);
100 break;
101 case wasm::WASM_OPCODE_F64_CONST:
102 writeUint64(OS, InitExpr.Value.Float64);
103 break;
104 case wasm::WASM_OPCODE_GET_GLOBAL:
105 encodeULEB128(InitExpr.Value.Global, OS);
106 break;
107 default:
Sam Clegge53af7f2018-01-09 21:38:53 +0000108 errs() << "Unknown opcode in init_expr: " << InitExpr.Opcode << "\n";
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000109 return 1;
110 }
111 writeUint8(OS, wasm::WASM_OPCODE_END);
112 return 0;
113}
114
Sam Clegg9e1ade92017-06-27 20:27:59 +0000115class SubSectionWriter {
116 raw_ostream &OS;
117 std::string OutString;
118 raw_string_ostream StringStream;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000119
Sam Clegg9e1ade92017-06-27 20:27:59 +0000120public:
121 SubSectionWriter(raw_ostream &OS) : OS(OS), StringStream(OutString) {}
Sam Cleggb7787fd2017-06-20 04:04:59 +0000122
Sam Clegg9e1ade92017-06-27 20:27:59 +0000123 void Done() {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000124 StringStream.flush();
125 encodeULEB128(OutString.size(), OS);
126 OS << OutString;
Sam Clegg9e1ade92017-06-27 20:27:59 +0000127 OutString.clear();
128 }
129
130 raw_ostream& GetStream() {
131 return StringStream;
132 }
133};
134
135int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section) {
136 writeStringRef(Section.Name, OS);
137
138 SubSectionWriter SubSection(OS);
139
Sam Clegg6c899ba2018-02-23 05:08:34 +0000140 // SYMBOL_TABLE subsection
141 if (Section.SymbolTable.size()) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000142 writeUint8(OS, wasm::WASM_SYMBOL_TABLE);
Sam Clegg9e1ade92017-06-27 20:27:59 +0000143
Sam Clegg6c899ba2018-02-23 05:08:34 +0000144 encodeULEB128(Section.SymbolTable.size(), SubSection.GetStream());
Benjamin Kramer8d71fdc2018-02-23 12:20:18 +0000145#ifndef NDEBUG
Sam Clegg6c899ba2018-02-23 05:08:34 +0000146 uint32_t SymbolIndex = 0;
147#endif
148 for (const WasmYAML::SymbolInfo &Info : Section.SymbolTable) {
149 assert(Info.Index == SymbolIndex++);
Sam Clegg03e101f2018-03-01 18:06:21 +0000150 writeUint8(SubSection.GetStream(), Info.Kind);
Sam Clegg9e1ade92017-06-27 20:27:59 +0000151 encodeULEB128(Info.Flags, SubSection.GetStream());
Sam Clegg6c899ba2018-02-23 05:08:34 +0000152 switch (Info.Kind) {
153 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
154 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
155 encodeULEB128(Info.ElementIndex, SubSection.GetStream());
156 if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0)
157 writeStringRef(Info.Name, SubSection.GetStream());
158 break;
159 case wasm::WASM_SYMBOL_TYPE_DATA:
160 writeStringRef(Info.Name, SubSection.GetStream());
161 if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
162 encodeULEB128(Info.DataRef.Segment, SubSection.GetStream());
163 encodeULEB128(Info.DataRef.Offset, SubSection.GetStream());
164 encodeULEB128(Info.DataRef.Size, SubSection.GetStream());
165 }
166 break;
167 default:
168 llvm_unreachable("unexpected kind");
169 }
Sam Clegg9e1ade92017-06-27 20:27:59 +0000170 }
171
172 SubSection.Done();
Sam Cleggb7787fd2017-06-20 04:04:59 +0000173 }
Sam Cleggd95ed952017-09-20 19:03:35 +0000174
175 // SEGMENT_NAMES subsection
Sam Clegg63ebb812017-09-29 16:50:08 +0000176 if (Section.SegmentInfos.size()) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000177 writeUint8(OS, wasm::WASM_SEGMENT_INFO);
Sam Clegg63ebb812017-09-29 16:50:08 +0000178 encodeULEB128(Section.SegmentInfos.size(), SubSection.GetStream());
179 for (const WasmYAML::SegmentInfo &SegmentInfo : Section.SegmentInfos) {
Sam Clegg63ebb812017-09-29 16:50:08 +0000180 writeStringRef(SegmentInfo.Name, SubSection.GetStream());
181 encodeULEB128(SegmentInfo.Alignment, SubSection.GetStream());
182 encodeULEB128(SegmentInfo.Flags, SubSection.GetStream());
Sam Cleggd95ed952017-09-20 19:03:35 +0000183 }
184 SubSection.Done();
185 }
Sam Clegg42739982017-12-14 21:10:03 +0000186
187 // INIT_FUNCS subsection
188 if (Section.InitFunctions.size()) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000189 writeUint8(OS, wasm::WASM_INIT_FUNCS);
Sam Clegg42739982017-12-14 21:10:03 +0000190 encodeULEB128(Section.InitFunctions.size(), SubSection.GetStream());
191 for (const WasmYAML::InitFunction &Func : Section.InitFunctions) {
192 encodeULEB128(Func.Priority, SubSection.GetStream());
Sam Clegg6c899ba2018-02-23 05:08:34 +0000193 encodeULEB128(Func.Symbol, SubSection.GetStream());
Sam Clegg42739982017-12-14 21:10:03 +0000194 }
195 SubSection.Done();
196 }
Sam Cleggea7cace2018-01-09 23:43:14 +0000197
198 // COMDAT_INFO subsection
199 if (Section.Comdats.size()) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000200 writeUint8(OS, wasm::WASM_COMDAT_INFO);
Sam Cleggea7cace2018-01-09 23:43:14 +0000201 encodeULEB128(Section.Comdats.size(), SubSection.GetStream());
202 for (const auto &C : Section.Comdats) {
203 writeStringRef(C.Name, SubSection.GetStream());
204 encodeULEB128(0, SubSection.GetStream()); // flags for future use
205 encodeULEB128(C.Entries.size(), SubSection.GetStream());
206 for (const WasmYAML::ComdatEntry &Entry : C.Entries) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000207 writeUint8(SubSection.GetStream(), Entry.Kind);
Sam Cleggea7cace2018-01-09 23:43:14 +0000208 encodeULEB128(Entry.Index, SubSection.GetStream());
209 }
210 }
211 SubSection.Done();
212 }
213
Sam Cleggb7787fd2017-06-20 04:04:59 +0000214 return 0;
215}
216
217int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section) {
Sam Clegg03cdd122017-05-05 18:12:34 +0000218 writeStringRef(Section.Name, OS);
219 if (Section.FunctionNames.size()) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000220 writeUint8(OS, wasm::WASM_NAMES_FUNCTION);
Sam Clegg03cdd122017-05-05 18:12:34 +0000221
Sam Clegg9e1ade92017-06-27 20:27:59 +0000222 SubSectionWriter SubSection(OS);
223
224 encodeULEB128(Section.FunctionNames.size(), SubSection.GetStream());
Sam Clegg03cdd122017-05-05 18:12:34 +0000225 for (const WasmYAML::NameEntry &NameEntry : Section.FunctionNames) {
Sam Clegg9e1ade92017-06-27 20:27:59 +0000226 encodeULEB128(NameEntry.Index, SubSection.GetStream());
227 writeStringRef(NameEntry.Name, SubSection.GetStream());
Sam Clegg03cdd122017-05-05 18:12:34 +0000228 }
229
Sam Clegg9e1ade92017-06-27 20:27:59 +0000230 SubSection.Done();
Sam Clegg03cdd122017-05-05 18:12:34 +0000231 }
232 return 0;
233}
234
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000235int WasmWriter::writeSectionContent(raw_ostream &OS,
236 WasmYAML::CustomSection &Section) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000237 if (auto S = dyn_cast<WasmYAML::NameSection>(&Section)) {
238 if (auto Err = writeSectionContent(OS, *S))
239 return Err;
240 } else if (auto S = dyn_cast<WasmYAML::LinkingSection>(&Section)) {
241 if (auto Err = writeSectionContent(OS, *S))
242 return Err;
Sam Clegg03cdd122017-05-05 18:12:34 +0000243 } else {
Sam Clegg9745afa2018-04-12 20:31:12 +0000244 writeStringRef(Section.Name, OS);
Sam Clegg03cdd122017-05-05 18:12:34 +0000245 Section.Payload.writeAsBinary(OS);
246 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000247 return 0;
248}
249
250int WasmWriter::writeSectionContent(raw_ostream &OS,
251 WasmYAML::TypeSection &Section) {
252 encodeULEB128(Section.Signatures.size(), OS);
Sam Clegge53af7f2018-01-09 21:38:53 +0000253 uint32_t ExpectedIndex = 0;
Sam Clegg03cdd122017-05-05 18:12:34 +0000254 for (const WasmYAML::Signature &Sig : Section.Signatures) {
Sam Clegge53af7f2018-01-09 21:38:53 +0000255 if (Sig.Index != ExpectedIndex) {
256 errs() << "Unexpected type index: " << Sig.Index << "\n";
257 return 1;
258 }
259 ++ExpectedIndex;
Sam Clegg03e101f2018-03-01 18:06:21 +0000260 writeUint8(OS, Sig.Form);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000261 encodeULEB128(Sig.ParamTypes.size(), OS);
262 for (auto ParamType : Sig.ParamTypes)
Sam Clegg03e101f2018-03-01 18:06:21 +0000263 writeUint8(OS, ParamType);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000264 if (Sig.ReturnType == wasm::WASM_TYPE_NORESULT) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000265 encodeULEB128(0, OS);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000266 } else {
267 encodeULEB128(1, OS);
Sam Clegg03e101f2018-03-01 18:06:21 +0000268 writeUint8(OS, Sig.ReturnType);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000269 }
270 }
271 return 0;
272}
273
274int WasmWriter::writeSectionContent(raw_ostream &OS,
275 WasmYAML::ImportSection &Section) {
276 encodeULEB128(Section.Imports.size(), OS);
Sam Clegg03cdd122017-05-05 18:12:34 +0000277 for (const WasmYAML::Import &Import : Section.Imports) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000278 writeStringRef(Import.Module, OS);
279 writeStringRef(Import.Field, OS);
Sam Clegg03e101f2018-03-01 18:06:21 +0000280 writeUint8(OS, Import.Kind);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000281 switch (Import.Kind) {
282 case wasm::WASM_EXTERNAL_FUNCTION:
283 encodeULEB128(Import.SigIndex, OS);
Sam Clegge53af7f2018-01-09 21:38:53 +0000284 NumImportedFunctions++;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000285 break;
286 case wasm::WASM_EXTERNAL_GLOBAL:
Sam Clegg03e101f2018-03-01 18:06:21 +0000287 writeUint8(OS, Import.GlobalImport.Type);
Sam Clegg41db5192017-05-10 00:14:04 +0000288 writeUint8(OS, Import.GlobalImport.Mutable);
Sam Clegge53af7f2018-01-09 21:38:53 +0000289 NumImportedGlobals++;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000290 break;
291 case wasm::WASM_EXTERNAL_MEMORY:
292 writeLimits(Import.Memory, OS);
293 break;
294 case wasm::WASM_EXTERNAL_TABLE:
Sam Clegg03e101f2018-03-01 18:06:21 +0000295 writeUint8(OS,Import.TableImport.ElemType);
Sam Clegg41db5192017-05-10 00:14:04 +0000296 writeLimits(Import.TableImport.TableLimits, OS);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000297 break;
298 default:
Sam Clegge53af7f2018-01-09 21:38:53 +0000299 errs() << "Unknown import type: " << Import.Kind << "\n";
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000300 return 1;
301 }
302 }
303 return 0;
304}
305
306int WasmWriter::writeSectionContent(raw_ostream &OS,
307 WasmYAML::FunctionSection &Section) {
308 encodeULEB128(Section.FunctionTypes.size(), OS);
309 for (uint32_t FuncType : Section.FunctionTypes) {
310 encodeULEB128(FuncType, OS);
311 }
312 return 0;
313}
314
315int WasmWriter::writeSectionContent(raw_ostream &OS,
316 WasmYAML::ExportSection &Section) {
317 encodeULEB128(Section.Exports.size(), OS);
Sam Clegg03cdd122017-05-05 18:12:34 +0000318 for (const WasmYAML::Export &Export : Section.Exports) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000319 writeStringRef(Export.Name, OS);
Sam Clegg03e101f2018-03-01 18:06:21 +0000320 writeUint8(OS, Export.Kind);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000321 encodeULEB128(Export.Index, OS);
322 }
323 return 0;
324}
325
326int WasmWriter::writeSectionContent(raw_ostream &OS,
327 WasmYAML::StartSection &Section) {
328 encodeULEB128(Section.StartFunction, OS);
329 return 0;
330}
331
332int WasmWriter::writeSectionContent(raw_ostream &OS,
333 WasmYAML::TableSection &Section) {
334 encodeULEB128(Section.Tables.size(), OS);
335 for (auto &Table : Section.Tables) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000336 writeUint8(OS, Table.ElemType);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000337 writeLimits(Table.TableLimits, OS);
338 }
339 return 0;
340}
341
342int WasmWriter::writeSectionContent(raw_ostream &OS,
343 WasmYAML::MemorySection &Section) {
344 encodeULEB128(Section.Memories.size(), OS);
Sam Clegg03cdd122017-05-05 18:12:34 +0000345 for (const WasmYAML::Limits &Mem : Section.Memories) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000346 writeLimits(Mem, OS);
347 }
348 return 0;
349}
350
351int WasmWriter::writeSectionContent(raw_ostream &OS,
352 WasmYAML::GlobalSection &Section) {
353 encodeULEB128(Section.Globals.size(), OS);
Sam Clegge53af7f2018-01-09 21:38:53 +0000354 uint32_t ExpectedIndex = NumImportedGlobals;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000355 for (auto &Global : Section.Globals) {
Sam Clegge53af7f2018-01-09 21:38:53 +0000356 if (Global.Index != ExpectedIndex) {
357 errs() << "Unexpected global index: " << Global.Index << "\n";
358 return 1;
359 }
360 ++ExpectedIndex;
Sam Clegg03e101f2018-03-01 18:06:21 +0000361 writeUint8(OS, Global.Type);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000362 writeUint8(OS, Global.Mutable);
363 writeInitExpr(Global.InitExpr, OS);
364 }
365 return 0;
366}
367
368int WasmWriter::writeSectionContent(raw_ostream &OS,
369 WasmYAML::ElemSection &Section) {
370 encodeULEB128(Section.Segments.size(), OS);
371 for (auto &Segment : Section.Segments) {
372 encodeULEB128(Segment.TableIndex, OS);
373 writeInitExpr(Segment.Offset, OS);
374
375 encodeULEB128(Segment.Functions.size(), OS);
376 for (auto &Function : Segment.Functions) {
377 encodeULEB128(Function, OS);
378 }
379 }
380 return 0;
381}
382
383int WasmWriter::writeSectionContent(raw_ostream &OS,
384 WasmYAML::CodeSection &Section) {
385 encodeULEB128(Section.Functions.size(), OS);
Sam Clegge53af7f2018-01-09 21:38:53 +0000386 uint32_t ExpectedIndex = NumImportedFunctions;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000387 for (auto &Func : Section.Functions) {
388 std::string OutString;
389 raw_string_ostream StringStream(OutString);
Sam Clegge53af7f2018-01-09 21:38:53 +0000390 if (Func.Index != ExpectedIndex) {
391 errs() << "Unexpected function index: " << Func.Index << "\n";
392 return 1;
393 }
394 ++ExpectedIndex;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000395
396 encodeULEB128(Func.Locals.size(), StringStream);
397 for (auto &LocalDecl : Func.Locals) {
398 encodeULEB128(LocalDecl.Count, StringStream);
Sam Clegg03e101f2018-03-01 18:06:21 +0000399 writeUint8(StringStream, LocalDecl.Type);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000400 }
401
402 Func.Body.writeAsBinary(StringStream);
403
404 // Write the section size followed by the content
405 StringStream.flush();
406 encodeULEB128(OutString.size(), OS);
407 OS << OutString;
408 }
409 return 0;
410}
411
412int WasmWriter::writeSectionContent(raw_ostream &OS,
413 WasmYAML::DataSection &Section) {
414 encodeULEB128(Section.Segments.size(), OS);
415 for (auto &Segment : Section.Segments) {
Sam Clegg9c07f942017-07-12 00:24:54 +0000416 encodeULEB128(Segment.MemoryIndex, OS);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000417 writeInitExpr(Segment.Offset, OS);
418 encodeULEB128(Segment.Content.binary_size(), OS);
419 Segment.Content.writeAsBinary(OS);
420 }
421 return 0;
422}
423
Sam Clegg6f08c842018-04-24 18:11:36 +0000424int WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
425 uint32_t SectionIndex) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000426 StringRef Name;
427 switch (Sec.Type) {
428 case wasm::WASM_SEC_CODE:
429 Name = "reloc.CODE";
430 break;
431 case wasm::WASM_SEC_DATA:
432 Name = "reloc.DATA";
433 break;
434 default:
435 llvm_unreachable("not yet implemented");
436 return 1;
437 }
438
439 writeStringRef(Name, OS);
Sam Clegg6f08c842018-04-24 18:11:36 +0000440 encodeULEB128(SectionIndex, OS);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000441 encodeULEB128(Sec.Relocations.size(), OS);
442
443 for (auto Reloc: Sec.Relocations) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000444 writeUint8(OS, Reloc.Type);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000445 encodeULEB128(Reloc.Offset, OS);
446 encodeULEB128(Reloc.Index, OS);
447 switch (Reloc.Type) {
Sam Clegg13a2e892017-09-01 17:32:01 +0000448 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
449 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
450 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000451 encodeULEB128(Reloc.Addend, OS);
452 }
453 }
454 return 0;
455}
456
457
458int WasmWriter::writeWasm(raw_ostream &OS) {
459 // Write headers
460 OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic));
461 writeUint32(OS, Obj.Header.Version);
462
463 // Write each section
Sam Clegge53af7f2018-01-09 21:38:53 +0000464 uint32_t LastType = 0;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000465 for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
Sam Clegge53af7f2018-01-09 21:38:53 +0000466 uint32_t Type = Sec->Type;
467 if (Type != wasm::WASM_SEC_CUSTOM) {
468 if (Type < LastType) {
469 errs() << "Out of order section type: " << Type << "\n";
470 return 1;
471 }
472 LastType = Type;
473 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000474
Sam Clegge53af7f2018-01-09 21:38:53 +0000475 encodeULEB128(Sec->Type, OS);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000476 std::string OutString;
477 raw_string_ostream StringStream(OutString);
478 if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get())) {
479 if (auto Err = writeSectionContent(StringStream, *S))
480 return Err;
481 } else if (auto S = dyn_cast<WasmYAML::TypeSection>(Sec.get())) {
482 if (auto Err = writeSectionContent(StringStream, *S))
483 return Err;
484 } else if (auto S = dyn_cast<WasmYAML::ImportSection>(Sec.get())) {
485 if (auto Err = writeSectionContent(StringStream, *S))
486 return Err;
487 } else if (auto S = dyn_cast<WasmYAML::FunctionSection>(Sec.get())) {
488 if (auto Err = writeSectionContent(StringStream, *S))
489 return Err;
490 } else if (auto S = dyn_cast<WasmYAML::TableSection>(Sec.get())) {
491 if (auto Err = writeSectionContent(StringStream, *S))
492 return Err;
493 } else if (auto S = dyn_cast<WasmYAML::MemorySection>(Sec.get())) {
494 if (auto Err = writeSectionContent(StringStream, *S))
495 return Err;
496 } else if (auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get())) {
497 if (auto Err = writeSectionContent(StringStream, *S))
498 return Err;
499 } else if (auto S = dyn_cast<WasmYAML::ExportSection>(Sec.get())) {
500 if (auto Err = writeSectionContent(StringStream, *S))
501 return Err;
502 } else if (auto S = dyn_cast<WasmYAML::StartSection>(Sec.get())) {
503 if (auto Err = writeSectionContent(StringStream, *S))
504 return Err;
505 } else if (auto S = dyn_cast<WasmYAML::ElemSection>(Sec.get())) {
506 if (auto Err = writeSectionContent(StringStream, *S))
507 return Err;
508 } else if (auto S = dyn_cast<WasmYAML::CodeSection>(Sec.get())) {
509 if (auto Err = writeSectionContent(StringStream, *S))
510 return Err;
511 } else if (auto S = dyn_cast<WasmYAML::DataSection>(Sec.get())) {
512 if (auto Err = writeSectionContent(StringStream, *S))
513 return Err;
514 } else {
515 errs() << "Unknown section type: " << Sec->Type << "\n";
516 return 1;
517 }
518 StringStream.flush();
519
520 // Write the section size followed by the content
521 encodeULEB128(OutString.size(), OS);
522 OS << OutString;
523 }
524
525 // write reloc sections for any section that have relocations
Sam Clegg6f08c842018-04-24 18:11:36 +0000526 uint32_t SectionIndex = 0;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000527 for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
Sam Clegg6f08c842018-04-24 18:11:36 +0000528 if (Sec->Relocations.empty()) {
529 SectionIndex++;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000530 continue;
Sam Clegg6f08c842018-04-24 18:11:36 +0000531 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000532
Sam Clegg03e101f2018-03-01 18:06:21 +0000533 writeUint8(OS, wasm::WASM_SEC_CUSTOM);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000534 std::string OutString;
535 raw_string_ostream StringStream(OutString);
Sam Clegg6f08c842018-04-24 18:11:36 +0000536 writeRelocSection(StringStream, *Sec, SectionIndex++);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000537 StringStream.flush();
538
539 encodeULEB128(OutString.size(), OS);
540 OS << OutString;
541 }
542
543 return 0;
544}
545
546int yaml2wasm(llvm::WasmYAML::Object &Doc, raw_ostream &Out) {
547 WasmWriter Writer(Doc);
548
549 return Writer.writeWasm(Out);
550}