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 wasm::WasmCommonReference CommonRef;
170 };
171};
172
173struct InitFunction {
174 uint32_t Priority;
175 uint32_t Symbol;
176};
177
178struct ComdatEntry {
179 ComdatKind Kind;
180 uint32_t Index;
181};
182
183struct Comdat {
184 StringRef Name;
185 std::vector<ComdatEntry> Entries;
186};
187
188struct LLVM_ABI Section {
189 explicit Section(SectionType SecType) : Type(SecType) {}
190 virtual ~Section();
191
192 SectionType Type;
193 std::vector<Relocation> Relocations;
194 std::optional<uint8_t> HeaderSecSizeEncodingLen;
195};
196
197struct CustomSection : Section {
198 explicit CustomSection(StringRef Name)
199 : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {}
200
201 static bool classof(const Section *S) {
202 return S->Type == wasm::WASM_SEC_CUSTOM;
203 }
204
205 StringRef Name;
206 yaml::BinaryRef Payload;
207};
208
209struct DylinkImportInfo {
210 StringRef Module;
211 StringRef Field;
212 SymbolFlags Flags;
213};
214
215struct DylinkExportInfo {
216 StringRef Name;
217 SymbolFlags Flags;
218};
219
220struct DylinkSection : CustomSection {
221 DylinkSection() : CustomSection("dylink.0") {}
222
223 static bool classof(const Section *S) {
224 auto C = dyn_cast<CustomSection>(Val: S);
225 return C && C->Name == "dylink.0";
226 }
227
228 uint32_t MemorySize;
229 uint32_t MemoryAlignment;
230 uint32_t TableSize;
231 uint32_t TableAlignment;
232 std::vector<StringRef> Needed;
233 std::vector<DylinkImportInfo> ImportInfo;
234 std::vector<DylinkExportInfo> ExportInfo;
235 std::vector<StringRef> RuntimePath;
236};
237
238struct NameSection : CustomSection {
239 NameSection() : CustomSection("name") {}
240
241 static bool classof(const Section *S) {
242 auto C = dyn_cast<CustomSection>(Val: S);
243 return C && C->Name == "name";
244 }
245
246 std::vector<NameEntry> FunctionNames;
247 std::vector<NameEntry> GlobalNames;
248 std::vector<NameEntry> DataSegmentNames;
249};
250
251struct LinkingSection : CustomSection {
252 LinkingSection() : CustomSection("linking") {}
253
254 static bool classof(const Section *S) {
255 auto C = dyn_cast<CustomSection>(Val: S);
256 return C && C->Name == "linking";
257 }
258
259 uint32_t Version;
260 std::vector<SymbolInfo> SymbolTable;
261 std::vector<SegmentInfo> SegmentInfos;
262 std::vector<InitFunction> InitFunctions;
263 std::vector<Comdat> Comdats;
264};
265
266struct ProducersSection : CustomSection {
267 ProducersSection() : CustomSection("producers") {}
268
269 static bool classof(const Section *S) {
270 auto C = dyn_cast<CustomSection>(Val: S);
271 return C && C->Name == "producers";
272 }
273
274 std::vector<ProducerEntry> Languages;
275 std::vector<ProducerEntry> Tools;
276 std::vector<ProducerEntry> SDKs;
277};
278
279struct TargetFeaturesSection : CustomSection {
280 TargetFeaturesSection() : CustomSection("target_features") {}
281
282 static bool classof(const Section *S) {
283 auto C = dyn_cast<CustomSection>(Val: S);
284 return C && C->Name == "target_features";
285 }
286
287 std::vector<FeatureEntry> Features;
288};
289
290struct TypeSection : Section {
291 TypeSection() : Section(wasm::WASM_SEC_TYPE) {}
292
293 static bool classof(const Section *S) {
294 return S->Type == wasm::WASM_SEC_TYPE;
295 }
296
297 std::vector<Signature> Signatures;
298};
299
300struct ImportSection : Section {
301 ImportSection() : Section(wasm::WASM_SEC_IMPORT) {}
302
303 static bool classof(const Section *S) {
304 return S->Type == wasm::WASM_SEC_IMPORT;
305 }
306
307 std::vector<Import> Imports;
308};
309
310struct FunctionSection : Section {
311 FunctionSection() : Section(wasm::WASM_SEC_FUNCTION) {}
312
313 static bool classof(const Section *S) {
314 return S->Type == wasm::WASM_SEC_FUNCTION;
315 }
316
317 std::vector<uint32_t> FunctionTypes;
318};
319
320struct TableSection : Section {
321 TableSection() : Section(wasm::WASM_SEC_TABLE) {}
322
323 static bool classof(const Section *S) {
324 return S->Type == wasm::WASM_SEC_TABLE;
325 }
326
327 std::vector<Table> Tables;
328};
329
330struct MemorySection : Section {
331 MemorySection() : Section(wasm::WASM_SEC_MEMORY) {}
332
333 static bool classof(const Section *S) {
334 return S->Type == wasm::WASM_SEC_MEMORY;
335 }
336
337 std::vector<Limits> Memories;
338};
339
340struct TagSection : Section {
341 TagSection() : Section(wasm::WASM_SEC_TAG) {}
342
343 static bool classof(const Section *S) {
344 return S->Type == wasm::WASM_SEC_TAG;
345 }
346
347 std::vector<uint32_t> TagTypes;
348};
349
350struct GlobalSection : Section {
351 GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {}
352
353 static bool classof(const Section *S) {
354 return S->Type == wasm::WASM_SEC_GLOBAL;
355 }
356
357 std::vector<Global> Globals;
358};
359
360struct ExportSection : Section {
361 ExportSection() : Section(wasm::WASM_SEC_EXPORT) {}
362
363 static bool classof(const Section *S) {
364 return S->Type == wasm::WASM_SEC_EXPORT;
365 }
366
367 std::vector<Export> Exports;
368};
369
370struct StartSection : Section {
371 StartSection() : Section(wasm::WASM_SEC_START) {}
372
373 static bool classof(const Section *S) {
374 return S->Type == wasm::WASM_SEC_START;
375 }
376
377 uint32_t StartFunction;
378};
379
380struct ElemSection : Section {
381 ElemSection() : Section(wasm::WASM_SEC_ELEM) {}
382
383 static bool classof(const Section *S) {
384 return S->Type == wasm::WASM_SEC_ELEM;
385 }
386
387 std::vector<ElemSegment> Segments;
388};
389
390struct CodeSection : Section {
391 CodeSection() : Section(wasm::WASM_SEC_CODE) {}
392
393 static bool classof(const Section *S) {
394 return S->Type == wasm::WASM_SEC_CODE;
395 }
396
397 std::vector<Function> Functions;
398};
399
400struct DataSection : Section {
401 DataSection() : Section(wasm::WASM_SEC_DATA) {}
402
403 static bool classof(const Section *S) {
404 return S->Type == wasm::WASM_SEC_DATA;
405 }
406
407 std::vector<DataSegment> Segments;
408};
409
410struct DataCountSection : Section {
411 DataCountSection() : Section(wasm::WASM_SEC_DATACOUNT) {}
412
413 static bool classof(const Section *S) {
414 return S->Type == wasm::WASM_SEC_DATACOUNT;
415 }
416
417 uint32_t Count;
418};
419
420struct Object {
421 FileHeader Header;
422 std::vector<std::unique_ptr<Section>> Sections;
423};
424
425} // end namespace WasmYAML
426} // end namespace llvm
427
428LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
429LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
430LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
431LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
432LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
433LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
434LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
435LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
436LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
437LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
438LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
439LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
440LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
441LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
442LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ProducerEntry)
443LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::FeatureEntry)
444LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo)
445LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
446LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction)
447LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ComdatEntry)
448LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Comdat)
449LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DylinkImportInfo)
450LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DylinkExportInfo)
451
452namespace llvm {
453namespace yaml {
454
455template <> struct MappingTraits<WasmYAML::FileHeader> {
456 LLVM_ABI static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
457};
458
459template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
460 LLVM_ABI static void mapping(IO &IO,
461 std::unique_ptr<WasmYAML::Section> &Section);
462};
463
464template <> struct MappingTraits<WasmYAML::Object> {
465 LLVM_ABI static void mapping(IO &IO, WasmYAML::Object &Object);
466};
467
468template <> struct MappingTraits<WasmYAML::Import> {
469 LLVM_ABI static void mapping(IO &IO, WasmYAML::Import &Import);
470};
471
472template <> struct MappingTraits<WasmYAML::Export> {
473 LLVM_ABI static void mapping(IO &IO, WasmYAML::Export &Export);
474};
475
476template <> struct MappingTraits<WasmYAML::Global> {
477 LLVM_ABI static void mapping(IO &IO, WasmYAML::Global &Global);
478};
479
480template <> struct ScalarBitSetTraits<WasmYAML::LimitFlags> {
481 LLVM_ABI static void bitset(IO &IO, WasmYAML::LimitFlags &Value);
482};
483
484template <> struct ScalarBitSetTraits<WasmYAML::SymbolFlags> {
485 LLVM_ABI static void bitset(IO &IO, WasmYAML::SymbolFlags &Value);
486};
487
488template <> struct ScalarEnumerationTraits<WasmYAML::SymbolKind> {
489 LLVM_ABI static void enumeration(IO &IO, WasmYAML::SymbolKind &Kind);
490};
491
492template <> struct ScalarBitSetTraits<WasmYAML::SegmentFlags> {
493 LLVM_ABI static void bitset(IO &IO, WasmYAML::SegmentFlags &Value);
494};
495
496template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
497 LLVM_ABI static void enumeration(IO &IO, WasmYAML::SectionType &Type);
498};
499
500template <> struct MappingTraits<WasmYAML::Signature> {
501 LLVM_ABI static void mapping(IO &IO, WasmYAML::Signature &Signature);
502};
503
504template <> struct MappingTraits<WasmYAML::Table> {
505 LLVM_ABI static void mapping(IO &IO, WasmYAML::Table &Table);
506};
507
508template <> struct MappingTraits<WasmYAML::Limits> {
509 LLVM_ABI static void mapping(IO &IO, WasmYAML::Limits &Limits);
510};
511
512template <> struct MappingTraits<WasmYAML::Function> {
513 LLVM_ABI static void mapping(IO &IO, WasmYAML::Function &Function);
514};
515
516template <> struct MappingTraits<WasmYAML::Relocation> {
517 LLVM_ABI static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
518};
519
520template <> struct MappingTraits<WasmYAML::NameEntry> {
521 LLVM_ABI static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
522};
523
524template <> struct MappingTraits<WasmYAML::ProducerEntry> {
525 LLVM_ABI static void mapping(IO &IO, WasmYAML::ProducerEntry &ProducerEntry);
526};
527
528template <> struct ScalarEnumerationTraits<WasmYAML::FeaturePolicyPrefix> {
529 LLVM_ABI static void enumeration(IO &IO,
530 WasmYAML::FeaturePolicyPrefix &Prefix);
531};
532
533template <> struct MappingTraits<WasmYAML::FeatureEntry> {
534 LLVM_ABI static void mapping(IO &IO, WasmYAML::FeatureEntry &FeatureEntry);
535};
536
537template <> struct MappingTraits<WasmYAML::SegmentInfo> {
538 LLVM_ABI static void mapping(IO &IO, WasmYAML::SegmentInfo &SegmentInfo);
539};
540
541template <> struct MappingTraits<WasmYAML::LocalDecl> {
542 LLVM_ABI static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
543};
544
545template <> struct MappingTraits<WasmYAML::InitExpr> {
546 LLVM_ABI static void mapping(IO &IO, WasmYAML::InitExpr &Expr);
547};
548
549template <> struct MappingTraits<WasmYAML::DataSegment> {
550 LLVM_ABI static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
551};
552
553template <> struct MappingTraits<WasmYAML::ElemSegment> {
554 LLVM_ABI static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
555};
556
557template <> struct MappingTraits<WasmYAML::SymbolInfo> {
558 LLVM_ABI static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
559};
560
561template <> struct MappingTraits<WasmYAML::InitFunction> {
562 LLVM_ABI static void mapping(IO &IO, WasmYAML::InitFunction &Init);
563};
564
565template <> struct ScalarEnumerationTraits<WasmYAML::ComdatKind> {
566 LLVM_ABI static void enumeration(IO &IO, WasmYAML::ComdatKind &Kind);
567};
568
569template <> struct MappingTraits<WasmYAML::ComdatEntry> {
570 LLVM_ABI static void mapping(IO &IO, WasmYAML::ComdatEntry &ComdatEntry);
571};
572
573template <> struct MappingTraits<WasmYAML::Comdat> {
574 LLVM_ABI static void mapping(IO &IO, WasmYAML::Comdat &Comdat);
575};
576
577template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
578 LLVM_ABI static void enumeration(IO &IO, WasmYAML::ValueType &Type);
579};
580
581template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
582 LLVM_ABI static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
583};
584
585template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
586 LLVM_ABI static void enumeration(IO &IO, WasmYAML::TableType &Type);
587};
588
589template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
590 LLVM_ABI static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
591};
592
593template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
594 LLVM_ABI static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
595};
596
597template <> struct MappingTraits<WasmYAML::DylinkImportInfo> {
598 LLVM_ABI static void mapping(IO &IO, WasmYAML::DylinkImportInfo &Info);
599};
600
601template <> struct MappingTraits<WasmYAML::DylinkExportInfo> {
602 LLVM_ABI static void mapping(IO &IO, WasmYAML::DylinkExportInfo &Info);
603};
604
605} // end namespace yaml
606} // end namespace llvm
607
608#endif // LLVM_OBJECTYAML_WASMYAML_H
609