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