LLVM 24.0.0git
WasmObjectFile.cpp
Go to the documentation of this file.
1//===- WasmObjectFile.cpp - Wasm object file implementation ---------------===//
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#include "llvm/ADT/ArrayRef.h"
10#include "llvm/ADT/DenseSet.h"
11#include "llvm/ADT/SmallSet.h"
12#include "llvm/ADT/StringRef.h"
13#include "llvm/ADT/StringSet.h"
16#include "llvm/Object/Binary.h"
17#include "llvm/Object/Error.h"
20#include "llvm/Object/Wasm.h"
21#include "llvm/Support/Endian.h"
22#include "llvm/Support/Error.h"
24#include "llvm/Support/LEB128.h"
28#include <cassert>
29#include <cstdint>
30#include <cstring>
31
32#define DEBUG_TYPE "wasm-object"
33
34using namespace llvm;
35using namespace object;
36
38 Out << "Name=" << Info.Name
39 << ", Kind=" << toString(wasm::WasmSymbolType(Info.Kind)) << ", Flags=0x"
40 << Twine::utohexstr(Info.Flags) << " [";
41 switch (getBinding()) {
42 case wasm::WASM_SYMBOL_BINDING_GLOBAL: Out << "global"; break;
43 case wasm::WASM_SYMBOL_BINDING_LOCAL: Out << "local"; break;
44 case wasm::WASM_SYMBOL_BINDING_WEAK: Out << "weak"; break;
45 }
46 if (isHidden())
47 Out << ", hidden";
48 else
49 Out << ", default";
51 Out << ", no_strip";
52 if (Info.Flags & wasm::WASM_SYMBOL_TLS)
53 Out << ", tls";
55 Out << ", absolute";
57 Out << ", exported";
58 if (isUndefined())
59 Out << ", undefined";
60 Out << "]";
61 if (!isTypeData()) {
62 Out << ", ElemIndex=" << Info.ElementIndex;
63 } else if (isDefined()) {
64 Out << ", Segment=" << Info.DataRef.Segment;
65 Out << ", Offset=" << Info.DataRef.Offset;
66 Out << ", Size=" << Info.DataRef.Size;
67 }
68}
69
70#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
72#endif
73
76 Error Err = Error::success();
77 auto ObjectFile = std::make_unique<WasmObjectFile>(Buffer, Err);
78 if (Err)
79 return std::move(Err);
80
81 return std::move(ObjectFile);
82}
83
84#define VARINT7_MAX ((1 << 7) - 1)
85#define VARINT7_MIN (-(1 << 7))
86#define VARUINT7_MAX (1 << 7)
87#define VARUINT1_MAX (1)
88
90 if (Ctx.Ptr == Ctx.End)
91 report_fatal_error("EOF while reading uint8");
92 return *Ctx.Ptr++;
93}
94
96 if (Ctx.Ptr + 4 > Ctx.End)
97 report_fatal_error("EOF while reading uint32");
98 uint32_t Result = support::endian::read32le(Ctx.Ptr);
99 Ctx.Ptr += 4;
100 return Result;
101}
102
104 if (Ctx.Ptr + 4 > Ctx.End)
105 report_fatal_error("EOF while reading float64");
106 int32_t Result = 0;
107 memcpy(&Result, Ctx.Ptr, sizeof(Result));
108 Ctx.Ptr += sizeof(Result);
109 return Result;
110}
111
113 if (Ctx.Ptr + 8 > Ctx.End)
114 report_fatal_error("EOF while reading float64");
115 int64_t Result = 0;
116 memcpy(&Result, Ctx.Ptr, sizeof(Result));
117 Ctx.Ptr += sizeof(Result);
118 return Result;
119}
120
122 unsigned Count;
123 const char *Error = nullptr;
124 uint64_t Result = decodeULEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
125 if (Error)
127 Ctx.Ptr += Count;
128 return Result;
129}
130
132 uint32_t StringLen = readULEB128(Ctx);
133 if (Ctx.Ptr + StringLen > Ctx.End)
134 report_fatal_error("EOF while reading string");
135 StringRef Return =
136 StringRef(reinterpret_cast<const char *>(Ctx.Ptr), StringLen);
137 Ctx.Ptr += StringLen;
138 return Return;
139}
140
142 unsigned Count;
143 const char *Error = nullptr;
144 uint64_t Result = decodeSLEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
145 if (Error)
147 Ctx.Ptr += Count;
148 return Result;
149}
150
152 int64_t Result = readLEB128(Ctx);
153 if (Result > VARUINT1_MAX || Result < 0)
154 report_fatal_error("LEB is outside Varuint1 range");
155 return Result;
156}
157
159 int64_t Result = readLEB128(Ctx);
160 if (Result > INT32_MAX || Result < INT32_MIN)
161 report_fatal_error("LEB is outside Varint32 range");
162 return Result;
163}
164
166 uint64_t Result = readULEB128(Ctx);
167 if (Result > UINT32_MAX)
168 report_fatal_error("LEB is outside Varuint32 range");
169 return Result;
170}
171
173 return readLEB128(Ctx);
174}
175
179
181 return readUint8(Ctx);
182}
183
185 uint32_t Code) {
186 // only directly encoded FUNCREF/EXTERNREF/EXNREF are supported
187 // (not ref null func, ref null extern, or ref null exn)
188 switch (Code) {
197 return wasm::ValType(Code);
198 }
200 /* Discard HeapType */ readVarint64(Ctx);
201 }
203}
204
207 auto Start = Ctx.Ptr;
208
209 Expr.Extended = false;
210 Expr.Inst.Opcode = readOpcode(Ctx);
211 switch (Expr.Inst.Opcode) {
213 Expr.Inst.Value.Int32 = readVarint32(Ctx);
214 break;
216 Expr.Inst.Value.Int64 = readVarint64(Ctx);
217 break;
219 Expr.Inst.Value.Float32 = readFloat32(Ctx);
220 break;
222 Expr.Inst.Value.Float64 = readFloat64(Ctx);
223 break;
225 Expr.Inst.Value.Global = readULEB128(Ctx);
226 break;
228 /* Discard type */ parseValType(Ctx, readVaruint32(Ctx));
229 break;
230 }
231 default:
232 Expr.Extended = true;
233 }
234
235 if (!Expr.Extended) {
236 uint8_t EndOpcode = readOpcode(Ctx);
237 if (EndOpcode != wasm::WASM_OPCODE_END)
238 Expr.Extended = true;
239 }
240
241 if (Expr.Extended) {
242 Ctx.Ptr = Start;
243 while (true) {
244 uint8_t Opcode = readOpcode(Ctx);
245 switch (Opcode) {
251 readULEB128(Ctx);
252 break;
254 readFloat32(Ctx);
255 break;
257 readFloat64(Ctx);
258 break;
265 break;
267 break;
268 // The GC opcodes are in a separate (prefixed space). This flat switch
269 // structure works as long as there is no overlap between the GC and
270 // general opcodes used in init exprs.
275 readULEB128(Ctx); // heap type index
276 break;
278 readULEB128(Ctx); // heap type index
279 readULEB128(Ctx); // array size
280 break;
282 break;
284 Expr.Body = ArrayRef<uint8_t>(Start, Ctx.Ptr - Start);
285 return Error::success();
286 default:
287 return make_error<GenericBinaryError>("invalid opcode in init_expr: " +
288 Twine(unsigned(Opcode)),
290 }
291 }
292 }
293
294 return Error::success();
295}
296
298 wasm::WasmLimits Result;
299 Result.Flags = readVaruint32(Ctx);
300 Result.Minimum = readVaruint64(Ctx);
301 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
302 Result.Maximum = readVaruint64(Ctx);
303 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_PAGE_SIZE) {
304 uint32_t PageSizeLog2 = readVaruint32(Ctx);
305 if (PageSizeLog2 >= 32)
306 report_fatal_error("log2(wasm page size) too large");
307 Result.PageSize = 1 << PageSizeLog2;
308 }
309 return Result;
310}
311
313 wasm::WasmTableType TableType;
314 auto ElemType = parseValType(Ctx, readVaruint32(Ctx));
315 TableType.ElemType = ElemType;
316 TableType.Limits = readLimits(Ctx);
317 return TableType;
318}
319
321 WasmSectionOrderChecker &Checker) {
322 Section.Type = readUint8(Ctx);
323 LLVM_DEBUG(dbgs() << "readSection type=" << Section.Type << "\n");
324 // When reading the section's size, store the size of the LEB used to encode
325 // it. This allows objcopy/strip to reproduce the binary identically.
326 const uint8_t *PreSizePtr = Ctx.Ptr;
328 Section.HeaderSecSizeEncodingLen = Ctx.Ptr - PreSizePtr;
329 Section.Offset = Ctx.Ptr - Ctx.Start;
330 if (Size == 0)
331 return make_error<StringError>("zero length section",
333 if (Ctx.Ptr + Size > Ctx.End)
334 return make_error<StringError>("section too large",
336 if (Section.Type == wasm::WASM_SEC_CUSTOM) {
338 SectionCtx.Start = Ctx.Ptr;
339 SectionCtx.Ptr = Ctx.Ptr;
340 SectionCtx.End = Ctx.Ptr + Size;
341
342 Section.Name = readString(SectionCtx);
343
344 uint32_t SectionNameSize = SectionCtx.Ptr - SectionCtx.Start;
345 Ctx.Ptr += SectionNameSize;
346 Size -= SectionNameSize;
347 }
348
349 if (!Checker.isValidSectionOrder(Section.Type, Section.Name)) {
350 return make_error<StringError>("out of order section type: " +
351 llvm::to_string(Section.Type),
353 }
354
355 Section.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
356 Ctx.Ptr += Size;
357 return Error::success();
358}
359
361 : ObjectFile(Binary::ID_Wasm, Buffer) {
362 ErrorAsOutParameter ErrAsOutParam(Err);
363 Header.Magic = getData().substr(0, 4);
364 if (Header.Magic != StringRef("\0asm", 4)) {
365 Err = make_error<StringError>("invalid magic number",
367 return;
368 }
369
370 ReadContext Ctx;
371 Ctx.Start = getData().bytes_begin();
372 Ctx.Ptr = Ctx.Start + 4;
373 Ctx.End = Ctx.Start + getData().size();
374
375 if (Ctx.Ptr + 4 > Ctx.End) {
376 Err = make_error<StringError>("missing version number",
378 return;
379 }
380
381 Header.Version = readUint32(Ctx);
382 if (Header.Version != wasm::WasmVersion) {
383 Err = make_error<StringError>("invalid version number: " +
384 Twine(Header.Version),
386 return;
387 }
388
390 while (Ctx.Ptr < Ctx.End) {
391 WasmSection Sec;
392 if ((Err = readSection(Sec, Ctx, Checker)))
393 return;
394 if ((Err = parseSection(Sec)))
395 return;
396
397 Sections.push_back(Sec);
398 }
399}
400
401Error WasmObjectFile::parseSection(WasmSection &Sec) {
402 ReadContext Ctx;
403 Ctx.Start = Sec.Content.data();
404 Ctx.End = Ctx.Start + Sec.Content.size();
405 Ctx.Ptr = Ctx.Start;
406 switch (Sec.Type) {
408 return parseCustomSection(Sec, Ctx);
410 return parseTypeSection(Ctx);
412 return parseImportSection(Ctx);
414 return parseFunctionSection(Ctx);
416 return parseTableSection(Ctx);
418 return parseMemorySection(Ctx);
420 return parseTagSection(Ctx);
422 return parseGlobalSection(Ctx);
424 return parseExportSection(Ctx);
426 return parseStartSection(Ctx);
428 return parseElemSection(Ctx);
430 return parseCodeSection(Ctx);
432 return parseDataSection(Ctx);
434 return parseDataCountSection(Ctx);
435 default:
437 "invalid section type: " + Twine(Sec.Type), object_error::parse_failed);
438 }
439}
440
441Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) {
442 // Legacy "dylink" section support.
443 // See parseDylink0Section for the current "dylink.0" section parsing.
444 HasDylinkSection = true;
445 DylinkInfo.MemorySize = readVaruint32(Ctx);
446 DylinkInfo.MemoryAlignment = readVaruint32(Ctx);
447 DylinkInfo.TableSize = readVaruint32(Ctx);
448 DylinkInfo.TableAlignment = readVaruint32(Ctx);
450 while (Count--) {
451 DylinkInfo.Needed.push_back(readString(Ctx));
452 }
453
454 if (Ctx.Ptr != Ctx.End)
455 return make_error<GenericBinaryError>("dylink section ended prematurely",
457 return Error::success();
458}
459
460Error WasmObjectFile::parseDylink0Section(ReadContext &Ctx) {
461 // See
462 // https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
463 HasDylinkSection = true;
464
465 const uint8_t *OrigEnd = Ctx.End;
466 while (Ctx.Ptr < OrigEnd) {
467 Ctx.End = OrigEnd;
468 uint8_t Type = readUint8(Ctx);
469 uint32_t Size = readVaruint32(Ctx);
470 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size
471 << "\n");
472 Ctx.End = Ctx.Ptr + Size;
473 uint32_t Count;
474 switch (Type) {
476 DylinkInfo.MemorySize = readVaruint32(Ctx);
477 DylinkInfo.MemoryAlignment = readVaruint32(Ctx);
478 DylinkInfo.TableSize = readVaruint32(Ctx);
479 DylinkInfo.TableAlignment = readVaruint32(Ctx);
480 break;
482 Count = readVaruint32(Ctx);
483 while (Count--) {
484 DylinkInfo.Needed.push_back(readString(Ctx));
485 }
486 break;
488 uint32_t Count = readVaruint32(Ctx);
489 while (Count--) {
490 DylinkInfo.ExportInfo.push_back({readString(Ctx), readVaruint32(Ctx)});
491 }
492 break;
493 }
495 uint32_t Count = readVaruint32(Ctx);
496 while (Count--) {
497 DylinkInfo.ImportInfo.push_back(
498 {readString(Ctx), readString(Ctx), readVaruint32(Ctx)});
499 }
500 break;
501 }
503 Count = readVaruint32(Ctx);
504 while (Count--) {
505 DylinkInfo.RuntimePath.push_back(readString(Ctx));
506 }
507 break;
508 }
509 default:
510 LLVM_DEBUG(dbgs() << "unknown dylink.0 sub-section: " << Type << "\n");
511 Ctx.Ptr += Size;
512 break;
513 }
514 if (Ctx.Ptr != Ctx.End) {
516 "dylink.0 sub-section ended prematurely", object_error::parse_failed);
517 }
518 }
519
520 if (Ctx.Ptr != Ctx.End)
521 return make_error<GenericBinaryError>("dylink.0 section ended prematurely",
523 return Error::success();
524}
525
526Error WasmObjectFile::parseNameSection(ReadContext &Ctx) {
527 llvm::DenseSet<uint64_t> SeenFunctions;
528 llvm::DenseSet<uint64_t> SeenGlobals;
529 llvm::DenseSet<uint64_t> SeenSegments;
530
531 // If we have linking section (symbol table) or if we are parsing a DSO
532 // then we don't use the name section for symbol information.
533 bool PopulateSymbolTable = !HasLinkingSection && !HasDylinkSection;
534
535 // If we are using the name section for symbol information then it will
536 // supersede any symbols created by the export section.
537 if (PopulateSymbolTable)
538 Symbols.clear();
539
540 while (Ctx.Ptr < Ctx.End) {
541 uint8_t Type = readUint8(Ctx);
542 uint32_t Size = readVaruint32(Ctx);
543 const uint8_t *SubSectionEnd = Ctx.Ptr + Size;
544
545 switch (Type) {
549 uint32_t Count = readVaruint32(Ctx);
550 while (Count--) {
551 uint32_t Index = readVaruint32(Ctx);
552 StringRef Name = readString(Ctx);
554 wasm::WasmSymbolInfo Info{Name,
556 /* Flags */ 0,
557 /* ImportModule */ std::nullopt,
558 /* ImportName */ std::nullopt,
559 /* ExportName */ std::nullopt,
560 {/* ElementIndex */ Index}};
561 const wasm::WasmSignature *Signature = nullptr;
562 const wasm::WasmGlobalType *GlobalType = nullptr;
563 const wasm::WasmTableType *TableType = nullptr;
565 if (!SeenFunctions.insert(Index).second)
567 "function named more than once", object_error::parse_failed);
568 if (!isValidFunctionIndex(Index) || Name.empty())
569 return make_error<GenericBinaryError>("invalid function name entry",
571
572 if (isDefinedFunctionIndex(Index)) {
573 wasm::WasmFunction &F = getDefinedFunction(Index);
574 F.DebugName = Name;
575 Signature = &Signatures[F.SigIndex];
576 if (F.ExportName) {
577 Info.ExportName = F.ExportName;
579 } else {
581 }
582 } else {
584 }
585 } else if (Type == wasm::WASM_NAMES_GLOBAL) {
586 if (!SeenGlobals.insert(Index).second)
587 return make_error<GenericBinaryError>("global named more than once",
589 if (!isValidGlobalIndex(Index) || Name.empty())
590 return make_error<GenericBinaryError>("invalid global name entry",
592 nameType = wasm::NameType::GLOBAL;
594 if (isDefinedGlobalIndex(Index)) {
595 GlobalType = &getDefinedGlobal(Index).Type;
596 } else {
598 }
599 } else {
600 if (!SeenSegments.insert(Index).second)
602 "segment named more than once", object_error::parse_failed);
603 if (Index >= DataSegments.size())
604 return make_error<GenericBinaryError>("invalid data segment name entry",
609 assert(Index < DataSegments.size());
610 Info.DataRef = wasm::WasmDataReference{
611 Index, 0, DataSegments[Index].Data.Content.size()};
612 }
613 DebugNames.push_back(wasm::WasmDebugName{nameType, Index, Name});
614 if (PopulateSymbolTable)
615 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
616 }
617 break;
618 }
619 // Ignore local names for now
621 default:
622 Ctx.Ptr += Size;
623 break;
624 }
625 if (Ctx.Ptr != SubSectionEnd)
627 "name sub-section ended prematurely", object_error::parse_failed);
628 }
629
630 if (Ctx.Ptr != Ctx.End)
631 return make_error<GenericBinaryError>("name section ended prematurely",
633 return Error::success();
634}
635
636Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) {
637 HasLinkingSection = true;
638
639 LinkingData.Version = readVaruint32(Ctx);
640 if (LinkingData.Version != wasm::WasmMetadataVersion) {
642 "unexpected metadata version: " + Twine(LinkingData.Version) +
643 " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")",
645 }
646
647 const uint8_t *OrigEnd = Ctx.End;
648 while (Ctx.Ptr < OrigEnd) {
649 Ctx.End = OrigEnd;
650 uint8_t Type = readUint8(Ctx);
651 uint32_t Size = readVaruint32(Ctx);
652 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size
653 << "\n");
654 Ctx.End = Ctx.Ptr + Size;
655 switch (Type) {
657 if (Error Err = parseLinkingSectionSymtab(Ctx))
658 return Err;
659 break;
661 uint32_t Count = readVaruint32(Ctx);
662 if (Count > DataSegments.size())
663 return make_error<GenericBinaryError>("too many segment names",
665 for (uint32_t I = 0; I < Count; I++) {
666 DataSegments[I].Data.Name = readString(Ctx);
667 DataSegments[I].Data.Alignment = readVaruint32(Ctx);
668 DataSegments[I].Data.LinkingFlags = readVaruint32(Ctx);
669 }
670 break;
671 }
673 uint32_t Count = readVaruint32(Ctx);
674 LinkingData.InitFunctions.reserve(Count);
675 for (uint32_t I = 0; I < Count; I++) {
676 wasm::WasmInitFunc Init;
677 Init.Priority = readVaruint32(Ctx);
678 Init.Symbol = readVaruint32(Ctx);
679 if (!isValidFunctionSymbol(Init.Symbol))
680 return make_error<GenericBinaryError>("invalid function symbol: " +
681 Twine(Init.Symbol),
683 LinkingData.InitFunctions.emplace_back(Init);
684 }
685 break;
686 }
688 if (Error Err = parseLinkingSectionComdat(Ctx))
689 return Err;
690 break;
691 default:
692 Ctx.Ptr += Size;
693 break;
694 }
695 if (Ctx.Ptr != Ctx.End)
697 "linking sub-section ended prematurely", object_error::parse_failed);
698 }
699 if (Ctx.Ptr != OrigEnd)
700 return make_error<GenericBinaryError>("linking section ended prematurely",
702 return Error::success();
703}
704
705Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
706 uint32_t Count = readVaruint32(Ctx);
707 // Clear out any symbol information that was derived from the exports
708 // section.
709 Symbols.clear();
710 Symbols.reserve(Count);
711 StringSet<> SymbolNames;
712
713 std::vector<wasm::WasmImport *> ImportedGlobals;
714 std::vector<wasm::WasmImport *> ImportedFunctions;
715 std::vector<wasm::WasmImport *> ImportedTags;
716 std::vector<wasm::WasmImport *> ImportedTables;
717 ImportedGlobals.reserve(Imports.size());
718 ImportedFunctions.reserve(Imports.size());
719 ImportedTags.reserve(Imports.size());
720 ImportedTables.reserve(Imports.size());
721 for (auto &I : Imports) {
723 ImportedFunctions.emplace_back(&I);
724 else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL)
725 ImportedGlobals.emplace_back(&I);
726 else if (I.Kind == wasm::WASM_EXTERNAL_TAG)
727 ImportedTags.emplace_back(&I);
728 else if (I.Kind == wasm::WASM_EXTERNAL_TABLE)
729 ImportedTables.emplace_back(&I);
730 }
731
732 while (Count--) {
733 wasm::WasmSymbolInfo Info;
734 const wasm::WasmSignature *Signature = nullptr;
735 const wasm::WasmGlobalType *GlobalType = nullptr;
736 const wasm::WasmTableType *TableType = nullptr;
737
738 Info.Kind = readUint8(Ctx);
739 Info.Flags = readVaruint32(Ctx);
740 bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0;
741
742 switch (Info.Kind) {
744 Info.ElementIndex = readVaruint32(Ctx);
745 if (!isValidFunctionIndex(Info.ElementIndex) ||
746 IsDefined != isDefinedFunctionIndex(Info.ElementIndex))
747 return make_error<GenericBinaryError>("invalid function symbol index",
749 if (IsDefined) {
750 Info.Name = readString(Ctx);
751 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions;
752 wasm::WasmFunction &Function = Functions[FuncIndex];
753 Signature = &Signatures[Function.SigIndex];
754 if (Function.SymbolName.empty())
755 Function.SymbolName = Info.Name;
756 } else {
757 wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex];
758 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
759 Info.Name = readString(Ctx);
760 Info.ImportName = Import.Field;
761 } else {
762 Info.Name = Import.Field;
763 }
764 Signature = &Signatures[Import.SigIndex];
765 Info.ImportModule = Import.Module;
766 }
767 break;
768
770 Info.ElementIndex = readVaruint32(Ctx);
771 if (!isValidGlobalIndex(Info.ElementIndex) ||
772 IsDefined != isDefinedGlobalIndex(Info.ElementIndex))
773 return make_error<GenericBinaryError>("invalid global symbol index",
775 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
777 return make_error<GenericBinaryError>("undefined weak global symbol",
779 if (IsDefined) {
780 Info.Name = readString(Ctx);
781 unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals;
782 wasm::WasmGlobal &Global = Globals[GlobalIndex];
783 GlobalType = &Global.Type;
784 if (Global.SymbolName.empty())
785 Global.SymbolName = Info.Name;
786 } else {
787 wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex];
788 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
789 Info.Name = readString(Ctx);
790 Info.ImportName = Import.Field;
791 } else {
792 Info.Name = Import.Field;
793 }
794 GlobalType = &Import.Global;
795 Info.ImportModule = Import.Module;
796 }
797 break;
798
800 Info.ElementIndex = readVaruint32(Ctx);
801 if (!isValidTableNumber(Info.ElementIndex) ||
802 IsDefined != isDefinedTableNumber(Info.ElementIndex))
803 return make_error<GenericBinaryError>("invalid table symbol index",
805 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
807 return make_error<GenericBinaryError>("undefined weak table symbol",
809 if (IsDefined) {
810 Info.Name = readString(Ctx);
811 unsigned TableNumber = Info.ElementIndex - NumImportedTables;
812 wasm::WasmTable &Table = Tables[TableNumber];
813 TableType = &Table.Type;
814 if (Table.SymbolName.empty())
815 Table.SymbolName = Info.Name;
816 } else {
817 wasm::WasmImport &Import = *ImportedTables[Info.ElementIndex];
818 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
819 Info.Name = readString(Ctx);
820 Info.ImportName = Import.Field;
821 } else {
822 Info.Name = Import.Field;
823 }
824 TableType = &Import.Table;
825 Info.ImportModule = Import.Module;
826 }
827 break;
828
830 Info.Name = readString(Ctx);
831 if (IsDefined) {
832 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
836 "common symbols cannot be absolute: " + Info.Name,
838 auto Size = readVaruint64(Ctx);
839 auto Alignment = readUint8(Ctx);
840 Info.CommonRef = wasm::WasmCommonReference{Size, Alignment};
841 } else {
842 auto Index = readVaruint32(Ctx);
843 auto Offset = readVaruint64(Ctx);
844 auto Size = readVaruint64(Ctx);
845 if (!(Info.Flags & wasm::WASM_SYMBOL_ABSOLUTE)) {
846 if (Index >= DataSegments.size())
848 "invalid data segment index: " + Twine(Index),
850 size_t SegmentSize = DataSegments[Index].Data.Content.size();
851 if (Offset > SegmentSize)
853 "invalid data symbol offset: `" + Info.Name +
854 "` (offset: " + Twine(Offset) +
855 " segment size: " + Twine(SegmentSize) + ")",
857 }
858 Info.DataRef = wasm::WasmDataReference{Index, Offset, Size};
859 }
860 }
861 break;
862
864 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
867 "section symbols must have local binding",
869 Info.ElementIndex = readVaruint32(Ctx);
870 // Use somewhat unique section name as symbol name.
871 StringRef SectionName = Sections[Info.ElementIndex].Name;
872 Info.Name = SectionName;
873 break;
874 }
875
877 Info.ElementIndex = readVaruint32(Ctx);
878 if (!isValidTagIndex(Info.ElementIndex) ||
879 IsDefined != isDefinedTagIndex(Info.ElementIndex))
880 return make_error<GenericBinaryError>("invalid tag symbol index",
882 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
884 return make_error<GenericBinaryError>("undefined weak global symbol",
886 if (IsDefined) {
887 Info.Name = readString(Ctx);
888 unsigned TagIndex = Info.ElementIndex - NumImportedTags;
889 wasm::WasmTag &Tag = Tags[TagIndex];
890 Signature = &Signatures[Tag.SigIndex];
891 if (Tag.SymbolName.empty())
892 Tag.SymbolName = Info.Name;
893
894 } else {
895 wasm::WasmImport &Import = *ImportedTags[Info.ElementIndex];
896 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
897 Info.Name = readString(Ctx);
898 Info.ImportName = Import.Field;
899 } else {
900 Info.Name = Import.Field;
901 }
902 Signature = &Signatures[Import.SigIndex];
903 Info.ImportModule = Import.Module;
904 }
905 break;
906 }
907
908 default:
909 return make_error<GenericBinaryError>("invalid symbol type: " +
910 Twine(unsigned(Info.Kind)),
912 }
913
914 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
916 !SymbolNames.insert(Info.Name).second)
917 return make_error<GenericBinaryError>("duplicate symbol name " +
918 Twine(Info.Name),
920 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
921 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
922 }
923
924 return Error::success();
925}
926
927Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) {
928 uint32_t ComdatCount = readVaruint32(Ctx);
929 StringSet<> ComdatSet;
930 for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) {
931 StringRef Name = readString(Ctx);
932 if (Name.empty() || !ComdatSet.insert(Name).second)
933 return make_error<GenericBinaryError>("bad/duplicate COMDAT name " +
934 Twine(Name),
936 LinkingData.Comdats.emplace_back(Name);
937 uint32_t Flags = readVaruint32(Ctx);
938 if (Flags != 0)
939 return make_error<GenericBinaryError>("unsupported COMDAT flags",
941
942 uint32_t EntryCount = readVaruint32(Ctx);
943 while (EntryCount--) {
944 unsigned Kind = readVaruint32(Ctx);
945 unsigned Index = readVaruint32(Ctx);
946 switch (Kind) {
947 default:
948 return make_error<GenericBinaryError>("invalid COMDAT entry type",
951 if (Index >= DataSegments.size())
953 "COMDAT data index out of range", object_error::parse_failed);
954 if (DataSegments[Index].Data.Comdat != UINT32_MAX)
955 return make_error<GenericBinaryError>("data segment in two COMDATs",
957 DataSegments[Index].Data.Comdat = ComdatIndex;
958 break;
960 if (!isDefinedFunctionIndex(Index))
962 "COMDAT function index out of range", object_error::parse_failed);
963 if (getDefinedFunction(Index).Comdat != UINT32_MAX)
964 return make_error<GenericBinaryError>("function in two COMDATs",
966 getDefinedFunction(Index).Comdat = ComdatIndex;
967 break;
969 if (Index >= Sections.size())
971 "COMDAT section index out of range", object_error::parse_failed);
972 if (Sections[Index].Type != wasm::WASM_SEC_CUSTOM)
974 "non-custom section in a COMDAT", object_error::parse_failed);
975 Sections[Index].Comdat = ComdatIndex;
976 break;
977 }
978 }
979 }
980 return Error::success();
981}
982
983Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) {
984 llvm::SmallSet<StringRef, 3> FieldsSeen;
985 uint32_t Fields = readVaruint32(Ctx);
986 for (size_t I = 0; I < Fields; ++I) {
987 StringRef FieldName = readString(Ctx);
988 if (!FieldsSeen.insert(FieldName).second)
990 "producers section does not have unique fields",
992 std::vector<std::pair<std::string, std::string>> *ProducerVec = nullptr;
993 if (FieldName == "language") {
994 ProducerVec = &ProducerInfo.Languages;
995 } else if (FieldName == "processed-by") {
996 ProducerVec = &ProducerInfo.Tools;
997 } else if (FieldName == "sdk") {
998 ProducerVec = &ProducerInfo.SDKs;
999 } else {
1001 "producers section field is not named one of language, processed-by, "
1002 "or sdk",
1004 }
1005 uint32_t ValueCount = readVaruint32(Ctx);
1006 llvm::SmallSet<StringRef, 8> ProducersSeen;
1007 for (size_t J = 0; J < ValueCount; ++J) {
1008 StringRef Name = readString(Ctx);
1009 StringRef Version = readString(Ctx);
1010 if (!ProducersSeen.insert(Name).second) {
1012 "producers section contains repeated producer",
1014 }
1015 ProducerVec->emplace_back(std::string(Name), std::string(Version));
1016 }
1017 }
1018 if (Ctx.Ptr != Ctx.End)
1019 return make_error<GenericBinaryError>("producers section ended prematurely",
1021 return Error::success();
1022}
1023
1024Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) {
1025 llvm::SmallSet<std::string, 8> FeaturesSeen;
1026 uint32_t FeatureCount = readVaruint32(Ctx);
1027 for (size_t I = 0; I < FeatureCount; ++I) {
1028 wasm::WasmFeatureEntry Feature;
1029 Feature.Prefix = readUint8(Ctx);
1030 switch (Feature.Prefix) {
1033 break;
1034 default:
1035 return make_error<GenericBinaryError>("unknown feature policy prefix",
1037 }
1038 Feature.Name = std::string(readString(Ctx));
1039 if (!FeaturesSeen.insert(Feature.Name).second)
1041 "target features section contains repeated feature \"" +
1042 Feature.Name + "\"",
1044 TargetFeatures.push_back(Feature);
1045 }
1046 if (Ctx.Ptr != Ctx.End)
1048 "target features section ended prematurely",
1050 return Error::success();
1051}
1052
1053Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
1054 uint32_t SectionIndex = readVaruint32(Ctx);
1055 if (SectionIndex >= Sections.size())
1056 return make_error<GenericBinaryError>("invalid section index",
1058 WasmSection &Section = Sections[SectionIndex];
1059 uint32_t RelocCount = readVaruint32(Ctx);
1060 uint32_t EndOffset = Section.Content.size();
1061 uint32_t PreviousOffset = 0;
1062 while (RelocCount--) {
1063 wasm::WasmRelocation Reloc = {};
1064 uint32_t type = readVaruint32(Ctx);
1065 Reloc.Type = type;
1066 Reloc.Offset = readVaruint32(Ctx);
1067 if (Reloc.Offset < PreviousOffset)
1068 return make_error<GenericBinaryError>("relocations not in offset order",
1070
1071 auto badReloc = [&](StringRef msg) {
1072 if (Reloc.Index >= Symbols.size())
1074 msg + ": index " + Twine(Reloc.Index) + " out of range",
1077 msg + ": " + Twine(Symbols[Reloc.Index].Info.Name),
1079 };
1080
1081 PreviousOffset = Reloc.Offset;
1082 Reloc.Index = readVaruint32(Ctx);
1083 switch (type) {
1084 case wasm::R_WASM_FUNCTION_INDEX_LEB:
1085 case wasm::R_WASM_FUNCTION_INDEX_I32:
1086 case wasm::R_WASM_TABLE_INDEX_SLEB:
1087 case wasm::R_WASM_TABLE_INDEX_SLEB64:
1088 case wasm::R_WASM_TABLE_INDEX_I32:
1089 case wasm::R_WASM_TABLE_INDEX_I64:
1090 case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
1091 case wasm::R_WASM_TABLE_INDEX_REL_SLEB64:
1092 if (!isValidFunctionSymbol(Reloc.Index))
1093 return badReloc("invalid function relocation");
1094 break;
1095 case wasm::R_WASM_TABLE_NUMBER_LEB:
1096 if (!isValidTableSymbol(Reloc.Index))
1097 return badReloc("invalid table relocation");
1098 break;
1099 case wasm::R_WASM_TYPE_INDEX_LEB:
1100 if (Reloc.Index >= Signatures.size())
1101 return badReloc("invalid relocation type index");
1102 break;
1103 case wasm::R_WASM_GLOBAL_INDEX_LEB:
1104 // R_WASM_GLOBAL_INDEX_LEB are can be used against function and data
1105 // symbols to refer to their GOT entries.
1106 if (!isValidGlobalSymbol(Reloc.Index) &&
1107 !isValidDataSymbol(Reloc.Index) &&
1108 !isValidFunctionSymbol(Reloc.Index))
1109 return badReloc("invalid global relocation");
1110 break;
1111 case wasm::R_WASM_GLOBAL_INDEX_I32:
1112 if (!isValidGlobalSymbol(Reloc.Index))
1113 return badReloc("invalid global relocation");
1114 break;
1115 case wasm::R_WASM_TAG_INDEX_LEB:
1116 if (!isValidTagSymbol(Reloc.Index))
1117 return badReloc("invalid tag relocation");
1118 break;
1119 case wasm::R_WASM_MEMORY_ADDR_LEB:
1120 case wasm::R_WASM_MEMORY_ADDR_SLEB:
1121 case wasm::R_WASM_MEMORY_ADDR_I32:
1122 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
1123 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB:
1124 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
1125 if (!isValidDataSymbol(Reloc.Index))
1126 return badReloc("invalid data relocation");
1127 Reloc.Addend = readVarint32(Ctx);
1128 break;
1129 case wasm::R_WASM_MEMORY_ADDR_LEB64:
1130 case wasm::R_WASM_MEMORY_ADDR_SLEB64:
1131 case wasm::R_WASM_MEMORY_ADDR_I64:
1132 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
1133 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64:
1134 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I64:
1135 if (!isValidDataSymbol(Reloc.Index))
1136 return badReloc("invalid data relocation");
1137 Reloc.Addend = readVarint64(Ctx);
1138 break;
1139 case wasm::R_WASM_FUNCTION_OFFSET_I32:
1140 if (!isValidFunctionSymbol(Reloc.Index))
1141 return badReloc("invalid function relocation");
1142 Reloc.Addend = readVarint32(Ctx);
1143 break;
1144 case wasm::R_WASM_FUNCTION_OFFSET_I64:
1145 if (!isValidFunctionSymbol(Reloc.Index))
1146 return badReloc("invalid function relocation");
1147 Reloc.Addend = readVarint64(Ctx);
1148 break;
1149 case wasm::R_WASM_SECTION_OFFSET_I32:
1150 if (!isValidSectionSymbol(Reloc.Index))
1151 return badReloc("invalid section relocation");
1152 Reloc.Addend = readVarint32(Ctx);
1153 break;
1154 default:
1155 return make_error<GenericBinaryError>("invalid relocation type: " +
1156 Twine(type),
1158 }
1159
1160 // Relocations must fit inside the section, and must appear in order. They
1161 // also shouldn't overlap a function/element boundary, but we don't bother
1162 // to check that.
1163 uint64_t Size = 5;
1164 if (Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LEB64 ||
1165 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_SLEB64 ||
1166 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_REL_SLEB64)
1167 Size = 10;
1168 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I32 ||
1169 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I32 ||
1170 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I32 ||
1171 Reloc.Type == wasm::R_WASM_SECTION_OFFSET_I32 ||
1172 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
1173 Reloc.Type == wasm::R_WASM_FUNCTION_INDEX_I32 ||
1174 Reloc.Type == wasm::R_WASM_GLOBAL_INDEX_I32)
1175 Size = 4;
1176 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I64 ||
1177 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I64 ||
1178 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I64 ||
1179 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I64)
1180 Size = 8;
1181 if (Reloc.Offset + Size > EndOffset)
1182 return make_error<GenericBinaryError>("invalid relocation offset",
1184
1185 Section.Relocations.push_back(Reloc);
1186 }
1187 if (Ctx.Ptr != Ctx.End)
1188 return make_error<GenericBinaryError>("reloc section ended prematurely",
1190 return Error::success();
1191}
1192
1193Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) {
1194 if (Sec.Name == "dylink") {
1195 if (Error Err = parseDylinkSection(Ctx))
1196 return Err;
1197 } else if (Sec.Name == "dylink.0") {
1198 if (Error Err = parseDylink0Section(Ctx))
1199 return Err;
1200 } else if (Sec.Name == "name") {
1201 if (Error Err = parseNameSection(Ctx))
1202 return Err;
1203 } else if (Sec.Name == "linking") {
1204 if (Error Err = parseLinkingSection(Ctx))
1205 return Err;
1206 } else if (Sec.Name == "producers") {
1207 if (Error Err = parseProducersSection(Ctx))
1208 return Err;
1209 } else if (Sec.Name == "target_features") {
1210 if (Error Err = parseTargetFeaturesSection(Ctx))
1211 return Err;
1212 } else if (Sec.Name.starts_with("reloc.")) {
1213 if (Error Err = parseRelocSection(Sec.Name, Ctx))
1214 return Err;
1215 }
1216 return Error::success();
1217}
1218
1219Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
1220 auto parseFieldDef = [&]() {
1221 uint32_t TypeCode = readVaruint32((Ctx));
1222 /* Discard StorageType */ parseValType(Ctx, TypeCode);
1223 /* Discard Mutability */ readVaruint32(Ctx);
1224 };
1225
1226 uint32_t Count = readVaruint32(Ctx);
1227 Signatures.reserve(Count);
1228 while (Count--) {
1229 wasm::WasmSignature Sig;
1230 uint8_t Form = readUint8(Ctx);
1231 if (Form == wasm::WASM_TYPE_REC) {
1232 // Rec groups expand the type index space (beyond what was declared at
1233 // the top of the section, and also consume one element in that space.
1234 uint32_t RecSize = readVaruint32(Ctx);
1235 if (RecSize == 0)
1236 return make_error<GenericBinaryError>("Rec group size cannot be 0",
1238 Signatures.reserve(Signatures.size() + RecSize);
1239 Count += RecSize;
1241 Signatures.push_back(std::move(Sig));
1242 HasUnmodeledTypes = true;
1243 continue;
1244 }
1245 if (Form != wasm::WASM_TYPE_FUNC) {
1246 // Currently LLVM only models function types, and not other composite
1247 // types. Here we parse the type declarations just enough to skip past
1248 // them in the binary.
1249 if (Form == wasm::WASM_TYPE_SUB || Form == wasm::WASM_TYPE_SUB_FINAL) {
1250 uint32_t Supers = readVaruint32(Ctx);
1251 if (Supers > 0) {
1252 if (Supers != 1)
1254 "Invalid number of supertypes", object_error::parse_failed);
1255 /* Discard SuperIndex */ readVaruint32(Ctx);
1256 }
1257 Form = readVaruint32(Ctx);
1258 }
1259 if (Form == wasm::WASM_TYPE_STRUCT) {
1260 uint32_t FieldCount = readVaruint32(Ctx);
1261 while (FieldCount--) {
1262 parseFieldDef();
1263 }
1264 } else if (Form == wasm::WASM_TYPE_ARRAY) {
1265 parseFieldDef();
1266 } else {
1267 return make_error<GenericBinaryError>("bad form",
1269 }
1271 Signatures.push_back(std::move(Sig));
1272 HasUnmodeledTypes = true;
1273 continue;
1274 }
1275
1276 uint32_t ParamCount = readVaruint32(Ctx);
1277 Sig.Params.reserve(ParamCount);
1278 while (ParamCount--) {
1279 uint32_t ParamType = readUint8(Ctx);
1280 Sig.Params.push_back(parseValType(Ctx, ParamType));
1281 }
1282 uint32_t ReturnCount = readVaruint32(Ctx);
1283 while (ReturnCount--) {
1284 uint32_t ReturnType = readUint8(Ctx);
1285 Sig.Returns.push_back(parseValType(Ctx, ReturnType));
1286 }
1287
1288 Signatures.push_back(std::move(Sig));
1289 }
1290 if (Ctx.Ptr != Ctx.End)
1291 return make_error<GenericBinaryError>("type section ended prematurely",
1293 return Error::success();
1294}
1295
1296Error WasmObjectFile::parseImport(ReadContext &Ctx, wasm::WasmImport &Im) {
1297 switch (Im.Kind) {
1299 NumImportedFunctions++;
1300 Im.SigIndex = readVaruint32(Ctx);
1301 if (Im.SigIndex >= Signatures.size())
1302 return make_error<GenericBinaryError>("invalid function type",
1304 break;
1306 NumImportedGlobals++;
1307 Im.Global.Type = readUint8(Ctx);
1308 Im.Global.Mutable = readVaruint1(Ctx);
1309 break;
1311 Im.Memory = readLimits(Ctx);
1313 HasMemory64 = true;
1314 break;
1316 Im.Table = readTableType(Ctx);
1317 NumImportedTables++;
1318 auto ElemType = Im.Table.ElemType;
1319 if (ElemType != wasm::ValType::FUNCREF &&
1320 ElemType != wasm::ValType::EXTERNREF &&
1321 ElemType != wasm::ValType::EXNREF &&
1322 ElemType != wasm::ValType::OTHERREF)
1323 return make_error<GenericBinaryError>("invalid table element type",
1325 break;
1326 }
1328 NumImportedTags++;
1329 if (readUint8(Ctx) != 0) // Reserved 'attribute' field
1330 return make_error<GenericBinaryError>("invalid attribute",
1332 Im.SigIndex = readVaruint32(Ctx);
1333 if (Im.SigIndex >= Signatures.size())
1334 return make_error<GenericBinaryError>("invalid tag type",
1336 break;
1337 default:
1338 return make_error<GenericBinaryError>("unexpected import kind: " +
1339 Twine(unsigned(Im.Kind)),
1341 }
1342 Imports.push_back(Im);
1343 return Error::success();
1344}
1345
1346Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
1347 uint32_t Count = readVaruint32(Ctx);
1348 Imports.reserve(Count);
1349 uint32_t I = 0;
1350 while (I < Count) {
1351 wasm::WasmImport Im;
1352 Im.Module = readString(Ctx);
1353 Im.Field = readString(Ctx);
1354 Im.Kind = readUint8(Ctx);
1355 // 0x7E/0x7F along with an empty Field signals a block of compact imports.
1356 if (Im.Kind == 0x7E && Im.Field == "") {
1358 "compact import format (0x7E) is not yet supported",
1360 } else if (Im.Kind == 0x7F && Im.Field == "") {
1361 uint32_t NumCompactImports = readVaruint32(Ctx);
1362 while (NumCompactImports--) {
1363 Im.Field = readString(Ctx);
1364 Im.Kind = readUint8(Ctx);
1365 Error rtn = parseImport(Ctx, Im);
1366 if (rtn)
1367 return rtn;
1368 I++;
1369 }
1370 } else {
1371 Error rtn = parseImport(Ctx, Im);
1372 if (rtn)
1373 return rtn;
1374 I++;
1375 }
1376 }
1377 if (Ctx.Ptr != Ctx.End)
1378 return make_error<GenericBinaryError>("import section ended prematurely",
1380 return Error::success();
1381}
1382
1383Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
1384 uint32_t Count = readVaruint32(Ctx);
1385 Functions.reserve(Count);
1386 uint32_t NumTypes = Signatures.size();
1387 while (Count--) {
1388 uint32_t Type = readVaruint32(Ctx);
1389 if (Type >= NumTypes)
1390 return make_error<GenericBinaryError>("invalid function type",
1392 wasm::WasmFunction F;
1393 F.SigIndex = Type;
1394 Functions.push_back(F);
1395 }
1396 if (Ctx.Ptr != Ctx.End)
1397 return make_error<GenericBinaryError>("function section ended prematurely",
1399 return Error::success();
1400}
1401
1402Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
1403 TableSection = Sections.size();
1404 uint32_t Count = readVaruint32(Ctx);
1405 Tables.reserve(Count);
1406 while (Count--) {
1407 wasm::WasmTable T;
1408 T.Type = readTableType(Ctx);
1409 T.Index = NumImportedTables + Tables.size();
1410 Tables.push_back(T);
1411 auto ElemType = Tables.back().Type.ElemType;
1412 if (ElemType != wasm::ValType::FUNCREF &&
1413 ElemType != wasm::ValType::EXTERNREF &&
1414 ElemType != wasm::ValType::EXNREF &&
1415 ElemType != wasm::ValType::OTHERREF) {
1416 return make_error<GenericBinaryError>("invalid table element type",
1418 }
1419 }
1420 if (Ctx.Ptr != Ctx.End)
1421 return make_error<GenericBinaryError>("table section ended prematurely",
1423 return Error::success();
1424}
1425
1426Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
1427 uint32_t Count = readVaruint32(Ctx);
1428 Memories.reserve(Count);
1429 while (Count--) {
1430 auto Limits = readLimits(Ctx);
1431 if (Limits.Flags & wasm::WASM_LIMITS_FLAG_IS_64)
1432 HasMemory64 = true;
1433 Memories.push_back(Limits);
1434 }
1435 if (Ctx.Ptr != Ctx.End)
1436 return make_error<GenericBinaryError>("memory section ended prematurely",
1438 return Error::success();
1439}
1440
1441Error WasmObjectFile::parseTagSection(ReadContext &Ctx) {
1442 TagSection = Sections.size();
1443 uint32_t Count = readVaruint32(Ctx);
1444 Tags.reserve(Count);
1445 uint32_t NumTypes = Signatures.size();
1446 while (Count--) {
1447 if (readUint8(Ctx) != 0) // Reserved 'attribute' field
1448 return make_error<GenericBinaryError>("invalid attribute",
1450 uint32_t Type = readVaruint32(Ctx);
1451 if (Type >= NumTypes)
1452 return make_error<GenericBinaryError>("invalid tag type",
1454 wasm::WasmTag Tag;
1455 Tag.Index = NumImportedTags + Tags.size();
1456 Tag.SigIndex = Type;
1457 Signatures[Type].Kind = wasm::WasmSignature::Tag;
1458 Tags.push_back(Tag);
1459 }
1460
1461 if (Ctx.Ptr != Ctx.End)
1462 return make_error<GenericBinaryError>("tag section ended prematurely",
1464 return Error::success();
1465}
1466
1467Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
1468 GlobalSection = Sections.size();
1469 const uint8_t *SectionStart = Ctx.Ptr;
1470 uint32_t Count = readVaruint32(Ctx);
1471 Globals.reserve(Count);
1472 while (Count--) {
1473 wasm::WasmGlobal Global;
1474 Global.Index = NumImportedGlobals + Globals.size();
1475 const uint8_t *GlobalStart = Ctx.Ptr;
1476 Global.Offset = static_cast<uint32_t>(GlobalStart - SectionStart);
1477 auto GlobalOpcode = readVaruint32(Ctx);
1478 Global.Type.Type = (uint8_t)parseValType(Ctx, GlobalOpcode);
1479 Global.Type.Mutable = readVaruint1(Ctx);
1480 if (Error Err = readInitExpr(Global.InitExpr, Ctx))
1481 return Err;
1482 Global.Size = static_cast<uint32_t>(Ctx.Ptr - GlobalStart);
1483 Globals.push_back(Global);
1484 }
1485 if (Ctx.Ptr != Ctx.End)
1486 return make_error<GenericBinaryError>("global section ended prematurely",
1488 return Error::success();
1489}
1490
1491Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
1492 uint32_t Count = readVaruint32(Ctx);
1493 Exports.reserve(Count);
1494 Symbols.reserve(Count);
1495
1496 // Build hash map of export flags for faster cross-referencing
1497 llvm::DenseMap<StringRef, uint32_t> ExportFlags;
1498 if (HasDylinkSection) {
1499 for (const auto &ExportInfo : DylinkInfo.ExportInfo) {
1500 ExportFlags[ExportInfo.Name] = ExportInfo.Flags;
1501 }
1502 }
1503
1504 for (uint32_t I = 0; I < Count; I++) {
1505 wasm::WasmExport Ex;
1506 Ex.Name = readString(Ctx);
1507 Ex.Kind = readUint8(Ctx);
1508 Ex.Index = readVaruint32(Ctx);
1509 const wasm::WasmSignature *Signature = nullptr;
1510 const wasm::WasmGlobalType *GlobalType = nullptr;
1511 const wasm::WasmTableType *TableType = nullptr;
1512 wasm::WasmSymbolInfo Info;
1513 Info.Name = Ex.Name;
1514 Info.Flags = 0;
1515 // For shared objects, symbol flags may be specified in the dylink section
1516 // instead of the export section
1517 if (HasDylinkSection) {
1518 auto It = ExportFlags.find(Ex.Name);
1519 if (It != ExportFlags.end()) {
1520 Info.Flags = It->second;
1521 }
1522 }
1523 switch (Ex.Kind) {
1525 if (!isValidFunctionIndex(Ex.Index))
1526 return make_error<GenericBinaryError>("invalid function export",
1529 Info.ElementIndex = Ex.Index;
1530 if (isDefinedFunctionIndex(Ex.Index)) {
1531 getDefinedFunction(Ex.Index).ExportName = Ex.Name;
1532 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions;
1533 wasm::WasmFunction &Function = Functions[FuncIndex];
1534 Signature = &Signatures[Function.SigIndex];
1535 }
1536 // Else the function is imported. LLVM object files don't use this
1537 // pattern and we still treat this as an undefined symbol, but we want to
1538 // parse it without crashing.
1539 break;
1540 }
1542 if (!isValidGlobalIndex(Ex.Index))
1543 return make_error<GenericBinaryError>("invalid global export",
1546 uint64_t Offset = 0;
1547 if (isDefinedGlobalIndex(Ex.Index)) {
1548 auto Global = getDefinedGlobal(Ex.Index);
1549 if (!Global.InitExpr.Extended) {
1550 auto Inst = Global.InitExpr.Inst;
1551 if (Inst.Opcode == wasm::WASM_OPCODE_I32_CONST) {
1552 Offset = Inst.Value.Int32;
1553 } else if (Inst.Opcode == wasm::WASM_OPCODE_I64_CONST) {
1554 Offset = Inst.Value.Int64;
1555 }
1556 }
1557 }
1558 Info.DataRef = wasm::WasmDataReference{0, Offset, 0};
1559 break;
1560 }
1562 if (!isValidTagIndex(Ex.Index))
1563 return make_error<GenericBinaryError>("invalid tag export",
1566 Info.ElementIndex = Ex.Index;
1567 if (isDefinedTagIndex(Ex.Index)) {
1568 unsigned TagIndex = Ex.Index - NumImportedTags;
1569 Signature = &Signatures[Tags[TagIndex].SigIndex];
1570 }
1571 break;
1573 break;
1576 Info.ElementIndex = Ex.Index;
1577 break;
1578 default:
1579 return make_error<GenericBinaryError>("unexpected export kind",
1581 }
1582 Exports.push_back(Ex);
1583 if (Ex.Kind != wasm::WASM_EXTERNAL_MEMORY) {
1584 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
1585 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
1586 }
1587 }
1588 if (Ctx.Ptr != Ctx.End)
1589 return make_error<GenericBinaryError>("export section ended prematurely",
1591 return Error::success();
1592}
1593
1594bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const {
1595 return Index < NumImportedFunctions + Functions.size();
1596}
1597
1598bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const {
1599 return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
1600}
1601
1602bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const {
1603 return Index < NumImportedGlobals + Globals.size();
1604}
1605
1606bool WasmObjectFile::isValidTableNumber(uint32_t Index) const {
1607 return Index < NumImportedTables + Tables.size();
1608}
1609
1610bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const {
1611 return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
1612}
1613
1614bool WasmObjectFile::isDefinedTableNumber(uint32_t Index) const {
1615 return Index >= NumImportedTables && isValidTableNumber(Index);
1616}
1617
1618bool WasmObjectFile::isValidTagIndex(uint32_t Index) const {
1619 return Index < NumImportedTags + Tags.size();
1620}
1621
1622bool WasmObjectFile::isDefinedTagIndex(uint32_t Index) const {
1623 return Index >= NumImportedTags && isValidTagIndex(Index);
1624}
1625
1626bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const {
1627 return Index < Symbols.size() && Symbols[Index].isTypeFunction();
1628}
1629
1630bool WasmObjectFile::isValidTableSymbol(uint32_t Index) const {
1631 return Index < Symbols.size() && Symbols[Index].isTypeTable();
1632}
1633
1634bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const {
1635 return Index < Symbols.size() && Symbols[Index].isTypeGlobal();
1636}
1637
1638bool WasmObjectFile::isValidTagSymbol(uint32_t Index) const {
1639 return Index < Symbols.size() && Symbols[Index].isTypeTag();
1640}
1641
1642bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const {
1643 return Index < Symbols.size() && Symbols[Index].isTypeData();
1644}
1645
1646bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const {
1647 return Index < Symbols.size() && Symbols[Index].isTypeSection();
1648}
1649
1650wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
1651 assert(isDefinedFunctionIndex(Index));
1652 return Functions[Index - NumImportedFunctions];
1653}
1654
1655const wasm::WasmFunction &
1656WasmObjectFile::getDefinedFunction(uint32_t Index) const {
1657 assert(isDefinedFunctionIndex(Index));
1658 return Functions[Index - NumImportedFunctions];
1659}
1660
1661const wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) const {
1662 assert(isDefinedGlobalIndex(Index));
1663 return Globals[Index - NumImportedGlobals];
1664}
1665
1666wasm::WasmTag &WasmObjectFile::getDefinedTag(uint32_t Index) {
1667 assert(isDefinedTagIndex(Index));
1668 return Tags[Index - NumImportedTags];
1669}
1670
1671Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
1672 StartFunction = readVaruint32(Ctx);
1673 if (!isValidFunctionIndex(StartFunction))
1674 return make_error<GenericBinaryError>("invalid start function",
1676 return Error::success();
1677}
1678
1679Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) {
1680 CodeSection = Sections.size();
1681 uint32_t FunctionCount = readVaruint32(Ctx);
1682 if (FunctionCount != Functions.size()) {
1683 return make_error<GenericBinaryError>("invalid function count",
1685 }
1686
1687 for (uint32_t i = 0; i < FunctionCount; i++) {
1688 wasm::WasmFunction& Function = Functions[i];
1689 const uint8_t *FunctionStart = Ctx.Ptr;
1690 uint32_t Size = readVaruint32(Ctx);
1691 const uint8_t *FunctionEnd = Ctx.Ptr + Size;
1692
1693 Function.CodeOffset = Ctx.Ptr - FunctionStart;
1694 Function.Index = NumImportedFunctions + i;
1695 Function.CodeSectionOffset = FunctionStart - Ctx.Start;
1696 Function.Size = FunctionEnd - FunctionStart;
1697
1698 uint32_t NumLocalDecls = readVaruint32(Ctx);
1699 Function.Locals.reserve(NumLocalDecls);
1700 while (NumLocalDecls--) {
1701 wasm::WasmLocalDecl Decl;
1702 Decl.Count = readVaruint32(Ctx);
1703 Decl.Type = readUint8(Ctx);
1704 Function.Locals.push_back(Decl);
1705 }
1706
1707 uint32_t BodySize = FunctionEnd - Ctx.Ptr;
1708 // Ensure that Function is within Ctx's buffer.
1709 if (Ctx.Ptr + BodySize > Ctx.End) {
1710 return make_error<GenericBinaryError>("Function extends beyond buffer",
1712 }
1713 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize);
1714 // This will be set later when reading in the linking metadata section.
1715 Function.Comdat = UINT32_MAX;
1716 Ctx.Ptr += BodySize;
1717 assert(Ctx.Ptr == FunctionEnd);
1718 }
1719 if (Ctx.Ptr != Ctx.End)
1720 return make_error<GenericBinaryError>("code section ended prematurely",
1722 return Error::success();
1723}
1724
1725Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
1726 uint32_t Count = readVaruint32(Ctx);
1727 ElemSegments.reserve(Count);
1728 while (Count--) {
1729 wasm::WasmElemSegment Segment;
1730 Segment.Flags = readVaruint32(Ctx);
1731
1732 uint32_t SupportedFlags = wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER |
1735 if (Segment.Flags & ~SupportedFlags)
1737 "Unsupported flags for element segment", object_error::parse_failed);
1738
1740 if ((Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_PASSIVE) == 0) {
1742 } else if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_DECLARATIVE) {
1744 } else {
1746 }
1747 bool HasTableNumber =
1750 bool HasElemKind =
1753 bool HasElemType =
1756 bool HasInitExprs =
1758
1759 if (HasTableNumber)
1760 Segment.TableNumber = readVaruint32(Ctx);
1761 else
1762 Segment.TableNumber = 0;
1763
1764 if (!isValidTableNumber(Segment.TableNumber))
1765 return make_error<GenericBinaryError>("invalid TableNumber",
1767
1769 Segment.Offset.Extended = false;
1771 Segment.Offset.Inst.Value.Int32 = 0;
1772 } else {
1773 if (Error Err = readInitExpr(Segment.Offset, Ctx))
1774 return Err;
1775 }
1776
1777 if (HasElemKind) {
1778 auto ElemKind = readVaruint32(Ctx);
1780 Segment.ElemKind = parseValType(Ctx, ElemKind);
1781 if (Segment.ElemKind != wasm::ValType::FUNCREF &&
1783 Segment.ElemKind != wasm::ValType::EXNREF &&
1784 Segment.ElemKind != wasm::ValType::OTHERREF) {
1785 return make_error<GenericBinaryError>("invalid elem type",
1787 }
1788 } else {
1789 if (ElemKind != 0)
1790 return make_error<GenericBinaryError>("invalid elem type",
1793 }
1794 } else if (HasElemType) {
1795 auto ElemType = parseValType(Ctx, readVaruint32(Ctx));
1796 Segment.ElemKind = ElemType;
1797 } else {
1799 }
1800
1801 uint32_t NumElems = readVaruint32(Ctx);
1802
1803 if (HasInitExprs) {
1804 while (NumElems--) {
1805 wasm::WasmInitExpr Expr;
1806 if (Error Err = readInitExpr(Expr, Ctx))
1807 return Err;
1808 }
1809 } else {
1810 while (NumElems--) {
1811 Segment.Functions.push_back(readVaruint32(Ctx));
1812 }
1813 }
1814 ElemSegments.push_back(Segment);
1815 }
1816 if (Ctx.Ptr != Ctx.End)
1817 return make_error<GenericBinaryError>("elem section ended prematurely",
1819 return Error::success();
1820}
1821
1822Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
1823 DataSection = Sections.size();
1824 uint32_t Count = readVaruint32(Ctx);
1825 if (DataCount && Count != *DataCount)
1827 "number of data segments does not match DataCount section");
1828 DataSegments.reserve(Count);
1829 while (Count--) {
1830 WasmSegment Segment;
1831 Segment.Data.InitFlags = readVaruint32(Ctx);
1832 Segment.Data.MemoryIndex =
1834 ? readVaruint32(Ctx)
1835 : 0;
1836 if ((Segment.Data.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
1837 if (Error Err = readInitExpr(Segment.Data.Offset, Ctx))
1838 return Err;
1839 } else {
1840 Segment.Data.Offset.Extended = false;
1842 Segment.Data.Offset.Inst.Value.Int32 = 0;
1843 }
1844 uint32_t Size = readVaruint32(Ctx);
1845 if (Size > (size_t)(Ctx.End - Ctx.Ptr))
1846 return make_error<GenericBinaryError>("invalid segment size",
1848 Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
1849 // The rest of these Data fields are set later, when reading in the linking
1850 // metadata section.
1851 Segment.Data.Alignment = 0;
1852 Segment.Data.LinkingFlags = 0;
1853 Segment.Data.Comdat = UINT32_MAX;
1854 Segment.SectionOffset = Ctx.Ptr - Ctx.Start;
1855 Ctx.Ptr += Size;
1856 DataSegments.push_back(Segment);
1857 }
1858 if (Ctx.Ptr != Ctx.End)
1859 return make_error<GenericBinaryError>("data section ended prematurely",
1861 return Error::success();
1862}
1863
1864Error WasmObjectFile::parseDataCountSection(ReadContext &Ctx) {
1865 DataCount = readVaruint32(Ctx);
1866 return Error::success();
1867}
1868
1870 return Header;
1871}
1872
1873void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.b++; }
1874
1877 const WasmSymbol &Sym = getWasmSymbol(Symb);
1878
1879 LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n");
1880 if (Sym.isBindingWeak())
1881 Result |= SymbolRef::SF_Weak;
1882 if (!Sym.isBindingLocal())
1883 Result |= SymbolRef::SF_Global;
1884 if (Sym.isHidden())
1885 Result |= SymbolRef::SF_Hidden;
1886 if (!Sym.isDefined())
1887 Result |= SymbolRef::SF_Undefined;
1888 if (Sym.isTypeFunction())
1889 Result |= SymbolRef::SF_Executable;
1890 return Result;
1891}
1892
1895 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null
1896 Ref.d.b = 0; // Symbol index
1897 return BasicSymbolRef(Ref, this);
1898}
1899
1902 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null
1903 Ref.d.b = Symbols.size(); // Symbol index
1904 return BasicSymbolRef(Ref, this);
1905}
1906
1908 return Symbols[Symb.d.b];
1909}
1910
1912 return getWasmSymbol(Symb.getRawDataRefImpl());
1913}
1914
1918
1920 auto &Sym = getWasmSymbol(Symb);
1921 if (!Sym.isDefined())
1922 return 0;
1924 if (!Sec)
1925 return Sec.takeError();
1926 uint32_t SectionAddress = getSectionAddress(Sec.get()->getRawDataRefImpl());
1927 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION &&
1928 isDefinedFunctionIndex(Sym.Info.ElementIndex)) {
1929 return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset +
1930 SectionAddress;
1931 }
1932 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL &&
1933 isDefinedGlobalIndex(Sym.Info.ElementIndex)) {
1934 return getDefinedGlobal(Sym.Info.ElementIndex).Offset + SectionAddress;
1935 }
1936
1937 return getSymbolValue(Symb);
1938}
1939
1941 switch (Sym.Info.Kind) {
1946 return Sym.Info.ElementIndex;
1948 // The value of a data symbol is the segment offset, plus the symbol
1949 // offset within the segment.
1950 uint32_t SegmentIndex = Sym.Info.DataRef.Segment;
1951 const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data;
1952 if (Segment.Offset.Extended) {
1953 llvm_unreachable("extended init exprs not supported");
1954 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_I32_CONST) {
1955 return Segment.Offset.Inst.Value.Int32 + Sym.Info.DataRef.Offset;
1956 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_I64_CONST) {
1957 return Segment.Offset.Inst.Value.Int64 + Sym.Info.DataRef.Offset;
1958 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_GLOBAL_GET) {
1959 return Sym.Info.DataRef.Offset;
1960 } else {
1961 llvm_unreachable("unknown init expr opcode");
1962 }
1963 }
1965 return 0;
1966 }
1967 llvm_unreachable("invalid symbol type");
1968}
1969
1973
1975 llvm_unreachable("not yet implemented");
1976 return 0;
1977}
1978
1980 llvm_unreachable("not yet implemented");
1981 return 0;
1982}
1983
1986 const WasmSymbol &Sym = getWasmSymbol(Symb);
1987
1988 switch (Sym.Info.Kind) {
1992 return SymbolRef::ST_Other;
1994 return SymbolRef::ST_Data;
1996 return SymbolRef::ST_Debug;
1998 return SymbolRef::ST_Other;
2000 return SymbolRef::ST_Other;
2001 }
2002
2003 llvm_unreachable("unknown WasmSymbol::SymbolType");
2004 return SymbolRef::ST_Other;
2005}
2006
2009 const WasmSymbol &Sym = getWasmSymbol(Symb);
2010 if (Sym.isUndefined())
2011 return section_end();
2012
2014 Ref.d.a = getSymbolSectionIdImpl(Sym);
2015 return section_iterator(SectionRef(Ref, this));
2016}
2017
2019 const WasmSymbol &Sym = getWasmSymbol(Symb);
2020 return getSymbolSectionIdImpl(Sym);
2021}
2022
2023uint32_t WasmObjectFile::getSymbolSectionIdImpl(const WasmSymbol &Sym) const {
2024 switch (Sym.Info.Kind) {
2026 return CodeSection;
2028 return GlobalSection;
2030 return DataSection;
2032 return Sym.Info.ElementIndex;
2034 return TagSection;
2036 return TableSection;
2037 default:
2038 llvm_unreachable("unknown WasmSymbol::SymbolType");
2039 }
2040}
2041
2043 const WasmSymbol &Sym = getWasmSymbol(Symb);
2044 if (!Sym.isDefined())
2045 return 0;
2046 if (Sym.isTypeGlobal())
2047 return getDefinedGlobal(Sym.Info.ElementIndex).Size;
2048 if (Sym.isTypeData())
2049 return Sym.Info.DataRef.Size;
2050 if (Sym.isTypeFunction())
2051 return functions()[Sym.Info.ElementIndex - getNumImportedFunctions()].Size;
2052 // Currently symbol size is only tracked for data segments and functions. In
2053 // principle we could also track size (e.g. binary size) for tables, globals
2054 // and element segments etc too.
2055 return 0;
2056}
2057
2059
2068
2070 // For object files, use 0 for section addresses, and section offsets for
2071 // symbol addresses. For linked files, use file offsets.
2072 // See also getSymbolAddress.
2073 return isRelocatableObject() || isSharedObject() ? 0
2074 : Sections[Sec.d.a].Offset;
2075}
2076
2078 return Sec.d.a;
2079}
2080
2082 const WasmSection &S = Sections[Sec.d.a];
2083 return S.Content.size();
2084}
2085
2088 const WasmSection &S = Sections[Sec.d.a];
2089 // This will never fail since wasm sections can never be empty (user-sections
2090 // must have a name and non-user sections each have a defined structure).
2091 return S.Content;
2092}
2093
2095 return 1;
2096}
2097
2099 return false;
2100}
2101
2105
2109
2110bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
2111
2112bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
2113
2115 DataRefImpl RelocRef;
2116 RelocRef.d.a = Ref.d.a;
2117 RelocRef.d.b = 0;
2118 return relocation_iterator(RelocationRef(RelocRef, this));
2119}
2120
2122 const WasmSection &Sec = getWasmSection(Ref);
2123 DataRefImpl RelocRef;
2124 RelocRef.d.a = Ref.d.a;
2125 RelocRef.d.b = Sec.Relocations.size();
2126 return relocation_iterator(RelocationRef(RelocRef, this));
2127}
2128
2130
2133 return Rel.Offset;
2134}
2135
2138 if (Rel.Type == wasm::R_WASM_TYPE_INDEX_LEB)
2139 return symbol_end();
2140 DataRefImpl Sym;
2141 Sym.d.a = 1;
2142 Sym.d.b = Rel.Index;
2143 return symbol_iterator(SymbolRef(Sym, this));
2144}
2145
2148 return Rel.Type;
2149}
2150
2152 DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
2154 StringRef Res = "Unknown";
2155
2156#define WASM_RELOC(name, value) \
2157 case wasm::name: \
2158 Res = #name; \
2159 break;
2160
2161 switch (Rel.Type) {
2162#include "llvm/BinaryFormat/WasmRelocs.def"
2163 }
2164
2165#undef WASM_RELOC
2166
2167 Result.append(Res.begin(), Res.end());
2168}
2169
2172 Ref.d.a = 0;
2173 return section_iterator(SectionRef(Ref, this));
2174}
2175
2178 Ref.d.a = Sections.size();
2179 return section_iterator(SectionRef(Ref, this));
2180}
2181
2183 return HasMemory64 ? 8 : 4;
2184}
2185
2187
2189 return HasMemory64 ? Triple::wasm64 : Triple::wasm32;
2190}
2191
2195
2196bool WasmObjectFile::isRelocatableObject() const { return HasLinkingSection; }
2197
2198bool WasmObjectFile::isSharedObject() const { return HasDylinkSection; }
2199
2201 assert(Ref.d.a < Sections.size());
2202 return Sections[Ref.d.a];
2203}
2204
2205const WasmSection &
2207 return getWasmSection(Section.getRawDataRefImpl());
2208}
2209
2212 return getWasmRelocation(Ref.getRawDataRefImpl());
2213}
2214
2217 assert(Ref.d.a < Sections.size());
2218 const WasmSection &Sec = Sections[Ref.d.a];
2219 assert(Ref.d.b < Sec.Relocations.size());
2220 return Sec.Relocations[Ref.d.b];
2221}
2222
2223int WasmSectionOrderChecker::getSectionOrder(unsigned ID,
2224 StringRef CustomSectionName) {
2225 switch (ID) {
2227 return StringSwitch<unsigned>(CustomSectionName)
2228 .Case("dylink", WASM_SEC_ORDER_DYLINK)
2229 .Case("dylink.0", WASM_SEC_ORDER_DYLINK)
2230 .Case("linking", WASM_SEC_ORDER_LINKING)
2232 .Case("name", WASM_SEC_ORDER_NAME)
2233 .Case("producers", WASM_SEC_ORDER_PRODUCERS)
2234 .Case("target_features", WASM_SEC_ORDER_TARGET_FEATURES)
2237 return WASM_SEC_ORDER_TYPE;
2239 return WASM_SEC_ORDER_IMPORT;
2243 return WASM_SEC_ORDER_TABLE;
2245 return WASM_SEC_ORDER_MEMORY;
2247 return WASM_SEC_ORDER_GLOBAL;
2249 return WASM_SEC_ORDER_EXPORT;
2251 return WASM_SEC_ORDER_START;
2253 return WASM_SEC_ORDER_ELEM;
2255 return WASM_SEC_ORDER_CODE;
2257 return WASM_SEC_ORDER_DATA;
2260 case wasm::WASM_SEC_TAG:
2261 return WASM_SEC_ORDER_TAG;
2262 default:
2263 return WASM_SEC_ORDER_NONE;
2264 }
2265}
2266
2267// Represents the edges in a directed graph where any node B reachable from node
2268// A is not allowed to appear before A in the section ordering, but may appear
2269// afterward.
2271 [WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = {
2272 // WASM_SEC_ORDER_NONE
2273 {},
2274 // WASM_SEC_ORDER_TYPE
2275 {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT},
2276 // WASM_SEC_ORDER_IMPORT
2277 {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION},
2278 // WASM_SEC_ORDER_FUNCTION
2279 {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE},
2280 // WASM_SEC_ORDER_TABLE
2281 {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY},
2282 // WASM_SEC_ORDER_MEMORY
2283 {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_TAG},
2284 // WASM_SEC_ORDER_TAG
2285 {WASM_SEC_ORDER_TAG, WASM_SEC_ORDER_GLOBAL},
2286 // WASM_SEC_ORDER_GLOBAL
2287 {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EXPORT},
2288 // WASM_SEC_ORDER_EXPORT
2289 {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START},
2290 // WASM_SEC_ORDER_START
2291 {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM},
2292 // WASM_SEC_ORDER_ELEM
2293 {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT},
2294 // WASM_SEC_ORDER_DATACOUNT
2295 {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE},
2296 // WASM_SEC_ORDER_CODE
2297 {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA},
2298 // WASM_SEC_ORDER_DATA
2299 {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING},
2300
2301 // Custom Sections
2302 // WASM_SEC_ORDER_DYLINK
2303 {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE},
2304 // WASM_SEC_ORDER_LINKING
2305 {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME},
2306 // WASM_SEC_ORDER_RELOC (can be repeated)
2307 {},
2308 // WASM_SEC_ORDER_NAME
2309 {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS},
2310 // WASM_SEC_ORDER_PRODUCERS
2311 {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES},
2312 // WASM_SEC_ORDER_TARGET_FEATURES
2313 {WASM_SEC_ORDER_TARGET_FEATURES}};
2314
2316 StringRef CustomSectionName) {
2317 int Order = getSectionOrder(ID, CustomSectionName);
2318 if (Order == WASM_SEC_ORDER_NONE)
2319 return true;
2320
2321 // Disallowed predecessors we need to check for
2323
2324 // Keep track of completed checks to avoid repeating work
2325 bool Checked[WASM_NUM_SEC_ORDERS] = {};
2326
2327 int Curr = Order;
2328 while (true) {
2329 // Add new disallowed predecessors to work list
2330 for (size_t I = 0;; ++I) {
2331 int Next = DisallowedPredecessors[Curr][I];
2333 break;
2334 if (Checked[Next])
2335 continue;
2336 WorkList.push_back(Next);
2337 Checked[Next] = true;
2338 }
2339
2340 if (WorkList.empty())
2341 break;
2342
2343 // Consider next disallowed predecessor
2344 Curr = WorkList.pop_back_val();
2345 if (Seen[Curr])
2346 return false;
2347 }
2348
2349 // Have not seen any disallowed predecessors
2350 Seen[Order] = true;
2351 return true;
2352}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:678
This file defines the DenseSet and SmallDenseSet classes.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
static Error readString(StringRef Buffer, const char *&Src, size_t MaxSize, StringRef &Val, Twine Desc)
Read a null-terminated string at the position Src from Buffer, with maximum byte size of MaxSize (inc...
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
This file defines the SmallSet class.
StringSet - A set-like wrapper for the StringMap.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
#define LLVM_DEBUG(...)
Definition Debug.h:119
static uint8_t readVaruint1(WasmObjectFile::ReadContext &Ctx)
static Error readInitExpr(wasm::WasmInitExpr &Expr, WasmObjectFile::ReadContext &Ctx)
static int32_t readVarint32(WasmObjectFile::ReadContext &Ctx)
static wasm::WasmTableType readTableType(WasmObjectFile::ReadContext &Ctx)
static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx)
static uint64_t readVaruint64(WasmObjectFile::ReadContext &Ctx)
static Error readSection(WasmSection &Section, WasmObjectFile::ReadContext &Ctx, WasmSectionOrderChecker &Checker)
static int64_t readLEB128(WasmObjectFile::ReadContext &Ctx)
static uint32_t readVaruint32(WasmObjectFile::ReadContext &Ctx)
static uint32_t readUint32(WasmObjectFile::ReadContext &Ctx)
static uint8_t readOpcode(WasmObjectFile::ReadContext &Ctx)
static uint8_t readUint8(WasmObjectFile::ReadContext &Ctx)
#define VARUINT1_MAX
static int32_t readFloat32(WasmObjectFile::ReadContext &Ctx)
static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx)
static int64_t readFloat64(WasmObjectFile::ReadContext &Ctx)
static wasm::ValType parseValType(WasmObjectFile::ReadContext &Ctx, uint32_t Code)
static int64_t readVarint64(WasmObjectFile::ReadContext &Ctx)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
const T * data() const
Definition ArrayRef.h:138
Helper for Errors used as out-parameters.
Definition Error.h:1160
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
iterator begin() const
Definition StringRef.h:114
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
iterator end() const
Definition StringRef.h:116
const unsigned char * bytes_begin() const
Definition StringRef.h:122
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition StringSet.h:39
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
StringSwitch & StartsWith(StringLiteral S, T Value)
Manages the enabling and disabling of subtarget specific features.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static Twine utohexstr(uint64_t Val)
Definition Twine.h:385
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:209
This is a value type class that represents a single symbol in the list of symbols in the object file.
DataRefImpl getRawDataRefImpl() const
MemoryBufferRef Data
Definition Binary.h:38
StringRef getData() const
Definition Binary.cpp:39
friend class RelocationRef
Definition ObjectFile.h:289
static Expected< std::unique_ptr< WasmObjectFile > > createWasmObjectFile(MemoryBufferRef Object)
Expected< uint64_t > getSymbolValue(DataRefImpl Symb) const
ObjectFile(unsigned int Type, MemoryBufferRef Source)
basic_symbol_iterator symbol_begin() const override
relocation_iterator section_rel_end(DataRefImpl Sec) const override
void moveSymbolNext(DataRefImpl &Symb) const override
uint64_t getSectionAlignment(DataRefImpl Sec) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
uint64_t getWasmSymbolValue(const WasmSymbol &Sym) const
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
bool isSectionText(DataRefImpl Sec) const override
bool isSectionBSS(DataRefImpl Sec) const override
basic_symbol_iterator symbol_end() const override
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
section_iterator section_begin() const override
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
void moveRelocationNext(DataRefImpl &Rel) const override
uint32_t getSymbolSectionId(SymbolRef Sym) const
bool isSectionCompressed(DataRefImpl Sec) const override
bool isSectionVirtual(DataRefImpl Sec) const override
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
StringRef getFileFormatName() const override
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
WasmObjectFile(MemoryBufferRef Object, Error &Err)
section_iterator section_end() const override
Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
uint64_t getSectionSize(DataRefImpl Sec) const override
Triple::ArchType getArch() const override
uint64_t getRelocationType(DataRefImpl Rel) const override
const WasmSection & getWasmSection(const SectionRef &Section) const
Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const override
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
Expected< SubtargetFeatures > getFeatures() const override
const wasm::WasmObjectHeader & getHeader() const
void moveSectionNext(DataRefImpl &Sec) const override
uint32_t getNumImportedFunctions() const
Definition Wasm.h:161
const wasm::WasmRelocation & getWasmRelocation(const RelocationRef &Ref) const
uint32_t getSymbolSize(SymbolRef Sym) const
ArrayRef< wasm::WasmFunction > functions() const
Definition Wasm.h:156
const WasmSymbol & getWasmSymbol(const DataRefImpl &Symb) const
uint64_t getSectionAddress(DataRefImpl Sec) const override
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
bool isSectionData(DataRefImpl Sec) const override
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
LLVM_ABI bool isValidSectionOrder(unsigned ID, StringRef CustomSectionName="")
static LLVM_ABI int DisallowedPredecessors[WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS]
Definition Wasm.h:360
bool isTypeFunction() const
Definition Wasm.h:54
unsigned getBinding() const
Definition Wasm.h:90
LLVM_DUMP_METHOD void dump() const
bool isTypeData() const
Definition Wasm.h:60
bool isBindingWeak() const
Definition Wasm.h:78
bool isHidden() const
Definition Wasm.h:94
wasm::WasmSymbolInfo Info
Definition Wasm.h:49
bool isUndefined() const
Definition Wasm.h:74
LLVM_ABI void print(raw_ostream &Out) const
bool isBindingLocal() const
Definition Wasm.h:86
bool isTypeGlobal() const
Definition Wasm.h:62
bool isDefined() const
Definition Wasm.h:72
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const char SectionName[]
content_iterator< SectionRef > section_iterator
Definition ObjectFile.h:49
content_iterator< BasicSymbolRef > basic_symbol_iterator
content_iterator< RelocationRef > relocation_iterator
Definition ObjectFile.h:79
uint32_t read32le(const void *P)
Definition Endian.h:412
const unsigned WASM_SYMBOL_UNDEFINED
Definition Wasm.h:257
@ WASM_NAMES_LOCAL
Definition Wasm.h:197
@ WASM_NAMES_DATA_SEGMENT
Definition Wasm.h:199
@ WASM_NAMES_GLOBAL
Definition Wasm.h:198
@ WASM_NAMES_FUNCTION
Definition Wasm.h:196
const unsigned WASM_SYMBOL_NO_STRIP
Definition Wasm.h:260
@ WASM_TYPE_ARRAY
Definition Wasm.h:76
@ WASM_TYPE_NULLABLE
Definition Wasm.h:74
@ WASM_TYPE_I64
Definition Wasm.h:57
@ WASM_TYPE_F64
Definition Wasm.h:59
@ WASM_TYPE_FUNCREF
Definition Wasm.h:65
@ WASM_TYPE_REC
Definition Wasm.h:80
@ WASM_TYPE_EXTERNREF
Definition Wasm.h:66
@ WASM_TYPE_SUB
Definition Wasm.h:78
@ WASM_TYPE_FUNC
Definition Wasm.h:75
@ WASM_TYPE_STRUCT
Definition Wasm.h:77
@ WASM_TYPE_NONNULLABLE
Definition Wasm.h:73
@ WASM_TYPE_I32
Definition Wasm.h:56
@ WASM_TYPE_F32
Definition Wasm.h:58
@ WASM_TYPE_V128
Definition Wasm.h:60
@ WASM_TYPE_SUB_FINAL
Definition Wasm.h:79
@ WASM_TYPE_EXNREF
Definition Wasm.h:67
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition Wasm.h:251
const unsigned WASM_SYMBOL_TLS
Definition Wasm.h:261
const uint32_t WasmMetadataVersion
Definition Wasm.h:31
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition Wasm.h:252
@ WASM_SEC_CODE
Definition Wasm.h:47
@ WASM_SEC_MEMORY
Definition Wasm.h:42
@ WASM_SEC_IMPORT
Definition Wasm.h:39
@ WASM_SEC_EXPORT
Definition Wasm.h:44
@ WASM_SEC_DATACOUNT
Definition Wasm.h:49
@ WASM_SEC_LAST_KNOWN
Definition Wasm.h:51
@ WASM_SEC_CUSTOM
Definition Wasm.h:37
@ WASM_SEC_FUNCTION
Definition Wasm.h:40
@ WASM_SEC_ELEM
Definition Wasm.h:46
@ WASM_SEC_START
Definition Wasm.h:45
@ WASM_SEC_TABLE
Definition Wasm.h:41
@ WASM_SEC_TYPE
Definition Wasm.h:38
@ WASM_SEC_TAG
Definition Wasm.h:50
@ WASM_SEC_GLOBAL
Definition Wasm.h:43
@ WASM_SEC_DATA
Definition Wasm.h:48
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition Wasm.h:253
@ WASM_LIMITS_FLAG_HAS_MAX
Definition Wasm.h:168
@ WASM_LIMITS_FLAG_IS_64
Definition Wasm.h:170
@ WASM_LIMITS_FLAG_HAS_PAGE_SIZE
Definition Wasm.h:171
@ WASM_FEATURE_PREFIX_USED
Definition Wasm.h:189
@ WASM_FEATURE_PREFIX_DISALLOWED
Definition Wasm.h:190
WasmSymbolType
Definition Wasm.h:228
@ WASM_SYMBOL_TYPE_GLOBAL
Definition Wasm.h:231
@ WASM_SYMBOL_TYPE_DATA
Definition Wasm.h:230
@ WASM_SYMBOL_TYPE_TAG
Definition Wasm.h:233
@ WASM_SYMBOL_TYPE_TABLE
Definition Wasm.h:234
@ WASM_SYMBOL_TYPE_SECTION
Definition Wasm.h:232
@ WASM_SYMBOL_TYPE_FUNCTION
Definition Wasm.h:229
const uint32_t WasmVersion
Definition Wasm.h:29
ElemSegmentMode
Definition Wasm.h:436
const unsigned WASM_SYMBOL_BINDING_COMMON
Definition Wasm.h:254
const unsigned WASM_SYMBOL_EXPORTED
Definition Wasm.h:258
const unsigned WASM_SYMBOL_BINDING_MASK
Definition Wasm.h:248
@ WASM_ELEM_SEGMENT_HAS_INIT_EXPRS
Definition Wasm.h:183
@ WASM_ELEM_SEGMENT_IS_DECLARATIVE
Definition Wasm.h:181
@ WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER
Definition Wasm.h:182
@ WASM_ELEM_SEGMENT_IS_PASSIVE
Definition Wasm.h:180
@ WASM_DYLINK_RUNTIME_PATH
Definition Wasm.h:216
@ WASM_DYLINK_NEEDED
Definition Wasm.h:213
@ WASM_DYLINK_MEM_INFO
Definition Wasm.h:212
@ WASM_DYLINK_EXPORT_INFO
Definition Wasm.h:214
@ WASM_DYLINK_IMPORT_INFO
Definition Wasm.h:215
@ WASM_DATA_SEGMENT_IS_PASSIVE
Definition Wasm.h:175
@ WASM_DATA_SEGMENT_HAS_MEMINDEX
Definition Wasm.h:176
@ WASM_EXTERNAL_TABLE
Definition Wasm.h:96
@ WASM_EXTERNAL_FUNCTION
Definition Wasm.h:95
@ WASM_EXTERNAL_TAG
Definition Wasm.h:99
@ WASM_EXTERNAL_MEMORY
Definition Wasm.h:97
@ WASM_EXTERNAL_GLOBAL
Definition Wasm.h:98
@ WASM_INIT_FUNCS
Definition Wasm.h:205
@ WASM_COMDAT_INFO
Definition Wasm.h:206
@ WASM_SEGMENT_INFO
Definition Wasm.h:204
@ WASM_SYMBOL_TABLE
Definition Wasm.h:207
@ WASM_COMDAT_SECTION
Definition Wasm.h:224
@ WASM_COMDAT_FUNCTION
Definition Wasm.h:222
@ WASM_COMDAT_DATA
Definition Wasm.h:221
@ WASM_OPCODE_I64_ADD
Definition Wasm.h:120
@ WASM_OPCODE_I32_SUB
Definition Wasm.h:118
@ WASM_OPCODE_F64_CONST
Definition Wasm.h:116
@ WASM_OPCODE_END
Definition Wasm.h:104
@ WASM_OPCODE_I64_MUL
Definition Wasm.h:122
@ WASM_OPCODE_REF_NULL
Definition Wasm.h:123
@ WASM_OPCODE_GC_PREFIX
Definition Wasm.h:125
@ WASM_OPCODE_REF_FUNC
Definition Wasm.h:124
@ WASM_OPCODE_F32_CONST
Definition Wasm.h:115
@ WASM_OPCODE_GLOBAL_GET
Definition Wasm.h:109
@ WASM_OPCODE_I64_SUB
Definition Wasm.h:121
@ WASM_OPCODE_I32_MUL
Definition Wasm.h:119
@ WASM_OPCODE_I32_ADD
Definition Wasm.h:117
@ WASM_OPCODE_I64_CONST
Definition Wasm.h:114
@ WASM_OPCODE_I32_CONST
Definition Wasm.h:113
LLVM_ABI llvm::StringRef sectionTypeToString(uint32_t type)
Definition Wasm.cpp:41
const unsigned WASM_SYMBOL_EXPLICIT_NAME
Definition Wasm.h:259
const unsigned WASM_SYMBOL_ABSOLUTE
Definition Wasm.h:262
const unsigned WASM_ELEM_SEGMENT_MASK_HAS_ELEM_DESC
Definition Wasm.h:185
@ WASM_OPCODE_ARRAY_NEW_FIXED
Definition Wasm.h:134
@ WASM_OPCODE_REF_I31
Definition Wasm.h:135
@ WASM_OPCODE_ARRAY_NEW_DEFAULT
Definition Wasm.h:133
@ WASM_OPCODE_STRUCT_NEW
Definition Wasm.h:130
@ WASM_OPCODE_STRUCT_NEW_DEFAULT
Definition Wasm.h:131
@ WASM_OPCODE_ARRAY_NEW
Definition Wasm.h:132
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
Definition LEB128.h:130
@ Import
Import information from summary.
Definition IPO.h:39
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
Definition LEB128.h:169
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
const char * to_string(ThinOrFullLTOPhase Phase)
Definition Pass.cpp:306
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
@ Global
Append to llvm.global_dtors.
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
ArrayRef< uint8_t > Content
Definition Wasm.h:116
std::vector< wasm::WasmRelocation > Relocations
Definition Wasm.h:117
wasm::WasmDataSegment Data
Definition Wasm.h:124
ArrayRef< uint8_t > Content
Definition Wasm.h:427
WasmInitExpr Offset
Definition Wasm.h:425
std::vector< StringRef > Needed
Definition Wasm.h:308
WasmInitExpr Offset
Definition Wasm.h:448
std::vector< uint32_t > Functions
Definition Wasm.h:449
WasmLimits Memory
Definition Wasm.h:397
StringRef Field
Definition Wasm.h:391
WasmGlobalType Global
Definition Wasm.h:395
StringRef Module
Definition Wasm.h:390
uint32_t SigIndex
Definition Wasm.h:394
WasmTableType Table
Definition Wasm.h:396
union llvm::wasm::WasmInitExprMVP::@234311111124136373374304035273310237314141125040 Value
WasmInitExprMVP Inst
Definition Wasm.h:365
ArrayRef< uint8_t > Body
Definition Wasm.h:366
SmallVector< ValType, 1 > Returns
Definition Wasm.h:524
SmallVector< ValType, 4 > Params
Definition Wasm.h:525
enum llvm::wasm::WasmSignature::@330257225222011177372050205212257221301063235146 Kind
WasmDataReference DataRef
Definition Wasm.h:494
struct llvm::object::DataRefImpl::@005117267142344013370254144343227032034000327225 d