1//===- WasmYAML.h - Wasm YAMLIO implementation ------------------*- C++ -*-===//
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/// This file declares classes for handling the YAML representation
11/// of wasm binaries.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_OBJECTYAML_WASMYAML_H
16#define LLVM_OBJECTYAML_WASMYAML_H
17
18#include "llvm/ADT/StringRef.h"
19#include "llvm/BinaryFormat/Wasm.h"
20#include "llvm/ObjectYAML/YAML.h"
21#include "llvm/Support/Casting.h"
22#include <cstdint>
23#include <memory>
24#include <vector>
25
26namespace llvm {
27namespace WasmYAML {
28
29LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionType)
30LLVM_YAML_STRONG_TYPEDEF(uint32_t, ValueType)
31LLVM_YAML_STRONG_TYPEDEF(uint32_t, TableType)
32LLVM_YAML_STRONG_TYPEDEF(uint32_t, SignatureForm)
33LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportKind)
34LLVM_YAML_STRONG_TYPEDEF(uint32_t, Opcode)
35LLVM_YAML_STRONG_TYPEDEF(uint32_t, RelocType)
36LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolFlags)
37LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolKind)
38LLVM_YAML_STRONG_TYPEDEF(uint32_t, SegmentFlags)
39LLVM_YAML_STRONG_TYPEDEF(uint32_t, LimitFlags)
40LLVM_YAML_STRONG_TYPEDEF(uint32_t, ComdatKind)
41LLVM_YAML_STRONG_TYPEDEF(uint32_t, FeaturePolicyPrefix)
42
43struct FileHeader {
44 yaml::Hex32 Version;
45};
46
47struct Limits {
48 LimitFlags Flags;
49 yaml::Hex32 Minimum;
50 yaml::Hex32 Maximum;
51 yaml::Hex32 PageSize;
52};
53
54struct Table {
55 TableType ElemType;
56 Limits TableLimits;
57 uint32_t Index;
58};
59
60struct Export {
61 StringRef Name;
62 ExportKind Kind;
63 uint32_t Index;
64};
65
66struct InitExpr {
67 InitExpr() {}
68 bool Extended;
69 union {
70 wasm::WasmInitExprMVP Inst;
71 yaml::BinaryRef Body;
72 };
73};
74
75struct ElemSegment {
76 uint32_t Flags;
77 uint32_t TableNumber;
78 ValueType ElemKind;
79 InitExpr Offset;
80 std::vector<uint32_t> Functions;
81};
82
83struct Global {
84 uint32_t Index;
85 ValueType Type;
86 bool Mutable;
87 InitExpr Init;
88};
89
90struct Import {
91 Import() {}
92 StringRef Module;
93 StringRef Field;
94 ExportKind Kind;
95 union {
96 uint32_t SigIndex;
97 Table TableImport;
98 Limits Memory;
99 uint32_t TagIndex;
100 Global GlobalImport;
101 };
102};
103
104struct LocalDecl {
105 ValueType Type;
106 uint32_t Count;
107};
108
109struct Function {
110 uint32_t Index;
111 std::vector<LocalDecl> Locals;
112 yaml::BinaryRef Body;
113};
114
115struct Relocation {
116 RelocType Type;
117 uint32_t Index;
118 // TODO(wvo): this would strictly be better as Hex64, but that will change
119 // all existing obj2yaml output.
120 yaml::Hex32 Offset;
121 int64_t Addend;
122};
123
124struct DataSegment {
125 uint32_t SectionOffset;
126 uint32_t InitFlags;
127 uint32_t MemoryIndex;
128 InitExpr Offset;
129 yaml::BinaryRef Content;
130};
131
132struct NameEntry {
133 uint32_t Index;
134 StringRef Name;
135};
136
137struct ProducerEntry {
138 std::string Name;
139 std::string Version;
140};
141
142struct FeatureEntry {
143 FeaturePolicyPrefix Prefix;
144 std::string Name;
145};
146
147struct SegmentInfo {
148 uint32_t Index;
149 StringRef Name;
150 uint32_t Alignment;
151 SegmentFlags Flags;
152};
153
154struct Signature {
155 uint32_t Index;
156 SignatureForm Form = wasm::WASM_TYPE_FUNC;
157 std::vector<ValueType> ParamTypes;
158 std::vector<ValueType> ReturnTypes;
159};
160
161struct SymbolInfo {
162 uint32_t Index;
163 StringRef Name;
164 SymbolKind Kind;
165 SymbolFlags Flags;
166 union {
167 uint32_t ElementIndex;
168 wasm::WasmDataReference DataRef;
169 };
170};
171
172struct InitFunction {
173 uint32_t Priority;
174 uint32_t Symbol;
175};
176
177struct ComdatEntry {
178 ComdatKind Kind;
179 uint32_t Index;
180};
181
182struct Comdat {
183 StringRef Name;
184 std::vector<ComdatEntry> Entries;
185};
186
187struct Section {
188 explicit Section(SectionType SecType) : Type(SecType) {}
189 virtual ~Section();
190
191 SectionType Type;
192 std::vector<Relocation> Relocations;
193 std::optional<uint8_t> HeaderSecSizeEncodingLen;
194};
195
196struct CustomSection : Section {
197 explicit CustomSection(StringRef Name)
198 : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {}
199
200 static bool classof(const Section *S) {
201 return S->Type == wasm::WASM_SEC_CUSTOM;
202 }
203
204 StringRef Name;
205 yaml::BinaryRef Payload;
206};
207
208struct DylinkImportInfo {
209 StringRef Module;
210 StringRef Field;
211 SymbolFlags Flags;
212};
213
214struct DylinkExportInfo {
215 StringRef Name;
216 SymbolFlags Flags;
217};
218
219struct DylinkSection : CustomSection {
220 DylinkSection() : CustomSection("dylink.0") {}
221
222 static bool classof(const Section *S) {
223 auto C = dyn_cast<CustomSection>(Val: S);
224 return C && C->Name == "dylink.0";
225 }
226
227 uint32_t MemorySize;
228 uint32_t MemoryAlignment;
229 uint32_t TableSize;
230 uint32_t TableAlignment;
231 std::vector<StringRef> Needed;
232 std::vector<DylinkImportInfo> ImportInfo;
233 std::vector<DylinkExportInfo> ExportInfo;
234 std::vector<StringRef> RuntimePath;
235};
236
237struct NameSection : CustomSection {
238 NameSection() : CustomSection("name") {}
239
240 static bool classof(const Section *S) {
241 auto C = dyn_cast<CustomSection>(Val: S);
242 return C && C->Name == "name";
243 }
244
245 std::vector<NameEntry> FunctionNames;
246 std::vector<NameEntry> GlobalNames;
247 std::vector<NameEntry> DataSegmentNames;
248};
249
250struct LinkingSection : CustomSection {
251 LinkingSection() : CustomSection("linking") {}
252
253 static bool classof(const Section *S) {
254 auto C = dyn_cast<CustomSection>(Val: S);
255 return C && C->Name == "linking";
256 }
257
258 uint32_t Version;
259 std::vector<SymbolInfo> SymbolTable;
260 std::vector<SegmentInfo> SegmentInfos;
261 std::vector<InitFunction> InitFunctions;
262 std::vector<Comdat> Comdats;
263};
264
265struct ProducersSection : CustomSection {
266 ProducersSection() : CustomSection("producers") {}
267
268 static bool classof(const Section *S) {
269 auto C = dyn_cast<CustomSection>(Val: S);
270 return C && C->Name == "producers";
271 }
272
273 std::vector<ProducerEntry> Languages;
274 std::vector<ProducerEntry> Tools;
275 std::vector<ProducerEntry> SDKs;
276};
277
278struct TargetFeaturesSection : CustomSection {
279 TargetFeaturesSection() : CustomSection("target_features") {}
280
281 static bool classof(const Section *S) {
282 auto C = dyn_cast<CustomSection>(Val: S);
283 return C && C->Name == "target_features";
284 }
285
286 std::vector<FeatureEntry> Features;
287};
288
289struct TypeSection : Section {
290 TypeSection() : Section(wasm::WASM_SEC_TYPE) {}
291
292 static bool classof(const Section *S) {
293 return S->Type == wasm::WASM_SEC_TYPE;
294 }
295
296 std::vector<Signature> Signatures;
297};
298
299struct ImportSection : Section {
300 ImportSection() : Section(wasm::WASM_SEC_IMPORT) {}
301
302 static bool classof(const Section *S) {
303 return S->Type == wasm::WASM_SEC_IMPORT;
304 }
305
306 std::vector<Import> Imports;
307};
308
309struct FunctionSection : Section {
310 FunctionSection() : Section(wasm::WASM_SEC_FUNCTION) {}
311
312 static bool classof(const Section *S) {
313 return S->Type == wasm::WASM_SEC_FUNCTION;
314 }
315
316 std::vector<uint32_t> FunctionTypes;
317};
318
319struct TableSection : Section {
320 TableSection() : Section(wasm::WASM_SEC_TABLE) {}
321
322 static bool classof(const Section *S) {
323 return S->Type == wasm::WASM_SEC_TABLE;
324 }
325
326 std::vector<Table> Tables;
327};
328
329struct MemorySection : Section {
330 MemorySection() : Section(wasm::WASM_SEC_MEMORY) {}
331
332 static bool classof(const Section *S) {
333 return S->Type == wasm::WASM_SEC_MEMORY;
334 }
335
336 std::vector<Limits> Memories;
337};
338
339struct TagSection : Section {
340 TagSection() : Section(wasm::WASM_SEC_TAG) {}
341
342 static bool classof(const Section *S) {
343 return S->Type == wasm::WASM_SEC_TAG;
344 }
345
346 std::vector<uint32_t> TagTypes;
347};
348
349struct GlobalSection : Section {
350 GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {}
351
352 static bool classof(const Section *S) {
353 return S->Type == wasm::WASM_SEC_GLOBAL;
354 }
355
356 std::vector<Global> Globals;
357};
358
359struct ExportSection : Section {
360 ExportSection() : Section(wasm::WASM_SEC_EXPORT) {}
361
362 static bool classof(const Section *S) {
363 return S->Type == wasm::WASM_SEC_EXPORT;
364 }
365
366 std::vector<Export> Exports;
367};
368
369struct StartSection : Section {
370 StartSection() : Section(wasm::WASM_SEC_START) {}
371
372 static bool classof(const Section *S) {
373 return S->Type == wasm::WASM_SEC_START;
374 }
375
376 uint32_t StartFunction;
377};
378
379struct ElemSection : Section {
380 ElemSection() : Section(wasm::WASM_SEC_ELEM) {}
381
382 static bool classof(const Section *S) {
383 return S->Type == wasm::WASM_SEC_ELEM;
384 }
385
386 std::vector<ElemSegment> Segments;
387};
388
389struct CodeSection : Section {
390 CodeSection() : Section(wasm::WASM_SEC_CODE) {}
391
392 static bool classof(const Section *S) {
393 return S->Type == wasm::WASM_SEC_CODE;
394 }
395
396 std::vector<Function> Functions;
397};
398
399struct DataSection : Section {
400 DataSection() : Section(wasm::WASM_SEC_DATA) {}
401
402 static bool classof(const Section *S) {
403 return S->Type == wasm::WASM_SEC_DATA;
404 }
405
406 std::vector<DataSegment> Segments;
407};
408
409struct DataCountSection : Section {
410 DataCountSection() : Section(wasm::WASM_SEC_DATACOUNT) {}
411
412 static bool classof(const Section *S) {
413 return S->Type == wasm::WASM_SEC_DATACOUNT;
414 }
415
416 uint32_t Count;
417};
418
419struct Object {
420 FileHeader Header;
421 std::vector<std::unique_ptr<Section>> Sections;
422};
423
424} // end namespace WasmYAML
425} // end namespace llvm
426
427LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
428LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
429LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
430LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
431LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
432LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
433LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
434LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
435LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
436LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
437LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
438LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
439LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
440LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
441LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ProducerEntry)
442LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::FeatureEntry)
443LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo)
444LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
445LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction)
446LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ComdatEntry)
447LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Comdat)
448LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DylinkImportInfo)
449LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DylinkExportInfo)
450
451namespace llvm {
452namespace yaml {
453
454template <> struct MappingTraits<WasmYAML::FileHeader> {
455 static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
456};
457
458template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
459 static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
460};
461
462template <> struct MappingTraits<WasmYAML::Object> {
463 static void mapping(IO &IO, WasmYAML::Object &Object);
464};
465
466template <> struct MappingTraits<WasmYAML::Import> {
467 static void mapping(IO &IO, WasmYAML::Import &Import);
468};
469
470template <> struct MappingTraits<WasmYAML::Export> {
471 static void mapping(IO &IO, WasmYAML::Export &Export);
472};
473
474template <> struct MappingTraits<WasmYAML::Global> {
475 static void mapping(IO &IO, WasmYAML::Global &Global);
476};
477
478template <> struct ScalarBitSetTraits<WasmYAML::LimitFlags> {
479 static void bitset(IO &IO, WasmYAML::LimitFlags &Value);
480};
481
482template <> struct ScalarBitSetTraits<WasmYAML::SymbolFlags> {
483 static void bitset(IO &IO, WasmYAML::SymbolFlags &Value);
484};
485
486template <> struct ScalarEnumerationTraits<WasmYAML::SymbolKind> {
487 static void enumeration(IO &IO, WasmYAML::SymbolKind &Kind);
488};
489
490template <> struct ScalarBitSetTraits<WasmYAML::SegmentFlags> {
491 static void bitset(IO &IO, WasmYAML::SegmentFlags &Value);
492};
493
494template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
495 static void enumeration(IO &IO, WasmYAML::SectionType &Type);
496};
497
498template <> struct MappingTraits<WasmYAML::Signature> {
499 static void mapping(IO &IO, WasmYAML::Signature &Signature);
500};
501
502template <> struct MappingTraits<WasmYAML::Table> {
503 static void mapping(IO &IO, WasmYAML::Table &Table);
504};
505
506template <> struct MappingTraits<WasmYAML::Limits> {
507 static void mapping(IO &IO, WasmYAML::Limits &Limits);
508};
509
510template <> struct MappingTraits<WasmYAML::Function> {
511 static void mapping(IO &IO, WasmYAML::Function &Function);
512};
513
514template <> struct MappingTraits<WasmYAML::Relocation> {
515 static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
516};
517
518template <> struct MappingTraits<WasmYAML::NameEntry> {
519 static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
520};
521
522template <> struct MappingTraits<WasmYAML::ProducerEntry> {
523 static void mapping(IO &IO, WasmYAML::ProducerEntry &ProducerEntry);
524};
525
526template <> struct ScalarEnumerationTraits<WasmYAML::FeaturePolicyPrefix> {
527 static void enumeration(IO &IO, WasmYAML::FeaturePolicyPrefix &Prefix);
528};
529
530template <> struct MappingTraits<WasmYAML::FeatureEntry> {
531 static void mapping(IO &IO, WasmYAML::FeatureEntry &FeatureEntry);
532};
533
534template <> struct MappingTraits<WasmYAML::SegmentInfo> {
535 static void mapping(IO &IO, WasmYAML::SegmentInfo &SegmentInfo);
536};
537
538template <> struct MappingTraits<WasmYAML::LocalDecl> {
539 static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
540};
541
542template <> struct MappingTraits<WasmYAML::InitExpr> {
543 static void mapping(IO &IO, WasmYAML::InitExpr &Expr);
544};
545
546template <> struct MappingTraits<WasmYAML::DataSegment> {
547 static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
548};
549
550template <> struct MappingTraits<WasmYAML::ElemSegment> {
551 static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
552};
553
554template <> struct MappingTraits<WasmYAML::SymbolInfo> {
555 static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
556};
557
558template <> struct MappingTraits<WasmYAML::InitFunction> {
559 static void mapping(IO &IO, WasmYAML::InitFunction &Init);
560};
561
562template <> struct ScalarEnumerationTraits<WasmYAML::ComdatKind> {
563 static void enumeration(IO &IO, WasmYAML::ComdatKind &Kind);
564};
565
566template <> struct MappingTraits<WasmYAML::ComdatEntry> {
567 static void mapping(IO &IO, WasmYAML::ComdatEntry &ComdatEntry);
568};
569
570template <> struct MappingTraits<WasmYAML::Comdat> {
571 static void mapping(IO &IO, WasmYAML::Comdat &Comdat);
572};
573
574template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
575 static void enumeration(IO &IO, WasmYAML::ValueType &Type);
576};
577
578template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
579 static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
580};
581
582template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
583 static void enumeration(IO &IO, WasmYAML::TableType &Type);
584};
585
586template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
587 static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
588};
589
590template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
591 static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
592};
593
594template <> struct MappingTraits<WasmYAML::DylinkImportInfo> {
595 static void mapping(IO &IO, WasmYAML::DylinkImportInfo &Info);
596};
597
598template <> struct MappingTraits<WasmYAML::DylinkExportInfo> {
599 static void mapping(IO &IO, WasmYAML::DylinkExportInfo &Info);
600};
601
602} // end namespace yaml
603} // end namespace llvm
604
605#endif // LLVM_OBJECTYAML_WASMYAML_H
606