LLVM 23.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 auto Index = readVaruint32(Ctx);
833 auto Offset = readVaruint64(Ctx);
834 auto Size = readVaruint64(Ctx);
835 if (!(Info.Flags & wasm::WASM_SYMBOL_ABSOLUTE)) {
836 if (Index >= DataSegments.size())
838 "invalid data segment index: " + Twine(Index),
840 size_t SegmentSize = DataSegments[Index].Data.Content.size();
841 if (Offset > SegmentSize)
843 "invalid data symbol offset: `" + Info.Name +
844 "` (offset: " + Twine(Offset) +
845 " segment size: " + Twine(SegmentSize) + ")",
847 }
848 Info.DataRef = wasm::WasmDataReference{Index, Offset, Size};
849 }
850 break;
851
853 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
856 "section symbols must have local binding",
858 Info.ElementIndex = readVaruint32(Ctx);
859 // Use somewhat unique section name as symbol name.
860 StringRef SectionName = Sections[Info.ElementIndex].Name;
861 Info.Name = SectionName;
862 break;
863 }
864
866 Info.ElementIndex = readVaruint32(Ctx);
867 if (!isValidTagIndex(Info.ElementIndex) ||
868 IsDefined != isDefinedTagIndex(Info.ElementIndex))
869 return make_error<GenericBinaryError>("invalid tag symbol index",
871 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
873 return make_error<GenericBinaryError>("undefined weak global symbol",
875 if (IsDefined) {
876 Info.Name = readString(Ctx);
877 unsigned TagIndex = Info.ElementIndex - NumImportedTags;
878 wasm::WasmTag &Tag = Tags[TagIndex];
879 Signature = &Signatures[Tag.SigIndex];
880 if (Tag.SymbolName.empty())
881 Tag.SymbolName = Info.Name;
882
883 } else {
884 wasm::WasmImport &Import = *ImportedTags[Info.ElementIndex];
885 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
886 Info.Name = readString(Ctx);
887 Info.ImportName = Import.Field;
888 } else {
889 Info.Name = Import.Field;
890 }
891 Signature = &Signatures[Import.SigIndex];
892 Info.ImportModule = Import.Module;
893 }
894 break;
895 }
896
897 default:
898 return make_error<GenericBinaryError>("invalid symbol type: " +
899 Twine(unsigned(Info.Kind)),
901 }
902
903 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
905 !SymbolNames.insert(Info.Name).second)
906 return make_error<GenericBinaryError>("duplicate symbol name " +
907 Twine(Info.Name),
909 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
910 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
911 }
912
913 return Error::success();
914}
915
916Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) {
917 uint32_t ComdatCount = readVaruint32(Ctx);
918 StringSet<> ComdatSet;
919 for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) {
920 StringRef Name = readString(Ctx);
921 if (Name.empty() || !ComdatSet.insert(Name).second)
922 return make_error<GenericBinaryError>("bad/duplicate COMDAT name " +
923 Twine(Name),
925 LinkingData.Comdats.emplace_back(Name);
926 uint32_t Flags = readVaruint32(Ctx);
927 if (Flags != 0)
928 return make_error<GenericBinaryError>("unsupported COMDAT flags",
930
931 uint32_t EntryCount = readVaruint32(Ctx);
932 while (EntryCount--) {
933 unsigned Kind = readVaruint32(Ctx);
934 unsigned Index = readVaruint32(Ctx);
935 switch (Kind) {
936 default:
937 return make_error<GenericBinaryError>("invalid COMDAT entry type",
940 if (Index >= DataSegments.size())
942 "COMDAT data index out of range", object_error::parse_failed);
943 if (DataSegments[Index].Data.Comdat != UINT32_MAX)
944 return make_error<GenericBinaryError>("data segment in two COMDATs",
946 DataSegments[Index].Data.Comdat = ComdatIndex;
947 break;
949 if (!isDefinedFunctionIndex(Index))
951 "COMDAT function index out of range", object_error::parse_failed);
952 if (getDefinedFunction(Index).Comdat != UINT32_MAX)
953 return make_error<GenericBinaryError>("function in two COMDATs",
955 getDefinedFunction(Index).Comdat = ComdatIndex;
956 break;
958 if (Index >= Sections.size())
960 "COMDAT section index out of range", object_error::parse_failed);
961 if (Sections[Index].Type != wasm::WASM_SEC_CUSTOM)
963 "non-custom section in a COMDAT", object_error::parse_failed);
964 Sections[Index].Comdat = ComdatIndex;
965 break;
966 }
967 }
968 }
969 return Error::success();
970}
971
972Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) {
973 llvm::SmallSet<StringRef, 3> FieldsSeen;
974 uint32_t Fields = readVaruint32(Ctx);
975 for (size_t I = 0; I < Fields; ++I) {
976 StringRef FieldName = readString(Ctx);
977 if (!FieldsSeen.insert(FieldName).second)
979 "producers section does not have unique fields",
981 std::vector<std::pair<std::string, std::string>> *ProducerVec = nullptr;
982 if (FieldName == "language") {
983 ProducerVec = &ProducerInfo.Languages;
984 } else if (FieldName == "processed-by") {
985 ProducerVec = &ProducerInfo.Tools;
986 } else if (FieldName == "sdk") {
987 ProducerVec = &ProducerInfo.SDKs;
988 } else {
990 "producers section field is not named one of language, processed-by, "
991 "or sdk",
993 }
994 uint32_t ValueCount = readVaruint32(Ctx);
995 llvm::SmallSet<StringRef, 8> ProducersSeen;
996 for (size_t J = 0; J < ValueCount; ++J) {
997 StringRef Name = readString(Ctx);
998 StringRef Version = readString(Ctx);
999 if (!ProducersSeen.insert(Name).second) {
1001 "producers section contains repeated producer",
1003 }
1004 ProducerVec->emplace_back(std::string(Name), std::string(Version));
1005 }
1006 }
1007 if (Ctx.Ptr != Ctx.End)
1008 return make_error<GenericBinaryError>("producers section ended prematurely",
1010 return Error::success();
1011}
1012
1013Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) {
1014 llvm::SmallSet<std::string, 8> FeaturesSeen;
1015 uint32_t FeatureCount = readVaruint32(Ctx);
1016 for (size_t I = 0; I < FeatureCount; ++I) {
1017 wasm::WasmFeatureEntry Feature;
1018 Feature.Prefix = readUint8(Ctx);
1019 switch (Feature.Prefix) {
1022 break;
1023 default:
1024 return make_error<GenericBinaryError>("unknown feature policy prefix",
1026 }
1027 Feature.Name = std::string(readString(Ctx));
1028 if (!FeaturesSeen.insert(Feature.Name).second)
1030 "target features section contains repeated feature \"" +
1031 Feature.Name + "\"",
1033 TargetFeatures.push_back(Feature);
1034 }
1035 if (Ctx.Ptr != Ctx.End)
1037 "target features section ended prematurely",
1039 return Error::success();
1040}
1041
1042Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
1043 uint32_t SectionIndex = readVaruint32(Ctx);
1044 if (SectionIndex >= Sections.size())
1045 return make_error<GenericBinaryError>("invalid section index",
1047 WasmSection &Section = Sections[SectionIndex];
1048 uint32_t RelocCount = readVaruint32(Ctx);
1049 uint32_t EndOffset = Section.Content.size();
1050 uint32_t PreviousOffset = 0;
1051 while (RelocCount--) {
1052 wasm::WasmRelocation Reloc = {};
1053 uint32_t type = readVaruint32(Ctx);
1054 Reloc.Type = type;
1055 Reloc.Offset = readVaruint32(Ctx);
1056 if (Reloc.Offset < PreviousOffset)
1057 return make_error<GenericBinaryError>("relocations not in offset order",
1059
1060 auto badReloc = [&](StringRef msg) {
1062 msg + ": " + Twine(Symbols[Reloc.Index].Info.Name),
1064 };
1065
1066 PreviousOffset = Reloc.Offset;
1067 Reloc.Index = readVaruint32(Ctx);
1068 switch (type) {
1069 case wasm::R_WASM_FUNCTION_INDEX_LEB:
1070 case wasm::R_WASM_FUNCTION_INDEX_I32:
1071 case wasm::R_WASM_TABLE_INDEX_SLEB:
1072 case wasm::R_WASM_TABLE_INDEX_SLEB64:
1073 case wasm::R_WASM_TABLE_INDEX_I32:
1074 case wasm::R_WASM_TABLE_INDEX_I64:
1075 case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
1076 case wasm::R_WASM_TABLE_INDEX_REL_SLEB64:
1077 if (!isValidFunctionSymbol(Reloc.Index))
1078 return badReloc("invalid function relocation");
1079 break;
1080 case wasm::R_WASM_TABLE_NUMBER_LEB:
1081 if (!isValidTableSymbol(Reloc.Index))
1082 return badReloc("invalid table relocation");
1083 break;
1084 case wasm::R_WASM_TYPE_INDEX_LEB:
1085 if (Reloc.Index >= Signatures.size())
1086 return badReloc("invalid relocation type index");
1087 break;
1088 case wasm::R_WASM_GLOBAL_INDEX_LEB:
1089 // R_WASM_GLOBAL_INDEX_LEB are can be used against function and data
1090 // symbols to refer to their GOT entries.
1091 if (!isValidGlobalSymbol(Reloc.Index) &&
1092 !isValidDataSymbol(Reloc.Index) &&
1093 !isValidFunctionSymbol(Reloc.Index))
1094 return badReloc("invalid global relocation");
1095 break;
1096 case wasm::R_WASM_GLOBAL_INDEX_I32:
1097 if (!isValidGlobalSymbol(Reloc.Index))
1098 return badReloc("invalid global relocation");
1099 break;
1100 case wasm::R_WASM_TAG_INDEX_LEB:
1101 if (!isValidTagSymbol(Reloc.Index))
1102 return badReloc("invalid tag relocation");
1103 break;
1104 case wasm::R_WASM_MEMORY_ADDR_LEB:
1105 case wasm::R_WASM_MEMORY_ADDR_SLEB:
1106 case wasm::R_WASM_MEMORY_ADDR_I32:
1107 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
1108 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB:
1109 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
1110 if (!isValidDataSymbol(Reloc.Index))
1111 return badReloc("invalid data relocation");
1112 Reloc.Addend = readVarint32(Ctx);
1113 break;
1114 case wasm::R_WASM_MEMORY_ADDR_LEB64:
1115 case wasm::R_WASM_MEMORY_ADDR_SLEB64:
1116 case wasm::R_WASM_MEMORY_ADDR_I64:
1117 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
1118 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64:
1119 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I64:
1120 if (!isValidDataSymbol(Reloc.Index))
1121 return badReloc("invalid data relocation");
1122 Reloc.Addend = readVarint64(Ctx);
1123 break;
1124 case wasm::R_WASM_FUNCTION_OFFSET_I32:
1125 if (!isValidFunctionSymbol(Reloc.Index))
1126 return badReloc("invalid function relocation");
1127 Reloc.Addend = readVarint32(Ctx);
1128 break;
1129 case wasm::R_WASM_FUNCTION_OFFSET_I64:
1130 if (!isValidFunctionSymbol(Reloc.Index))
1131 return badReloc("invalid function relocation");
1132 Reloc.Addend = readVarint64(Ctx);
1133 break;
1134 case wasm::R_WASM_SECTION_OFFSET_I32:
1135 if (!isValidSectionSymbol(Reloc.Index))
1136 return badReloc("invalid section relocation");
1137 Reloc.Addend = readVarint32(Ctx);
1138 break;
1139 default:
1140 return make_error<GenericBinaryError>("invalid relocation type: " +
1141 Twine(type),
1143 }
1144
1145 // Relocations must fit inside the section, and must appear in order. They
1146 // also shouldn't overlap a function/element boundary, but we don't bother
1147 // to check that.
1148 uint64_t Size = 5;
1149 if (Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LEB64 ||
1150 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_SLEB64 ||
1151 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_REL_SLEB64)
1152 Size = 10;
1153 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I32 ||
1154 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I32 ||
1155 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I32 ||
1156 Reloc.Type == wasm::R_WASM_SECTION_OFFSET_I32 ||
1157 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
1158 Reloc.Type == wasm::R_WASM_FUNCTION_INDEX_I32 ||
1159 Reloc.Type == wasm::R_WASM_GLOBAL_INDEX_I32)
1160 Size = 4;
1161 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I64 ||
1162 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I64 ||
1163 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I64 ||
1164 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I64)
1165 Size = 8;
1166 if (Reloc.Offset + Size > EndOffset)
1167 return make_error<GenericBinaryError>("invalid relocation offset",
1169
1170 Section.Relocations.push_back(Reloc);
1171 }
1172 if (Ctx.Ptr != Ctx.End)
1173 return make_error<GenericBinaryError>("reloc section ended prematurely",
1175 return Error::success();
1176}
1177
1178Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) {
1179 if (Sec.Name == "dylink") {
1180 if (Error Err = parseDylinkSection(Ctx))
1181 return Err;
1182 } else if (Sec.Name == "dylink.0") {
1183 if (Error Err = parseDylink0Section(Ctx))
1184 return Err;
1185 } else if (Sec.Name == "name") {
1186 if (Error Err = parseNameSection(Ctx))
1187 return Err;
1188 } else if (Sec.Name == "linking") {
1189 if (Error Err = parseLinkingSection(Ctx))
1190 return Err;
1191 } else if (Sec.Name == "producers") {
1192 if (Error Err = parseProducersSection(Ctx))
1193 return Err;
1194 } else if (Sec.Name == "target_features") {
1195 if (Error Err = parseTargetFeaturesSection(Ctx))
1196 return Err;
1197 } else if (Sec.Name.starts_with("reloc.")) {
1198 if (Error Err = parseRelocSection(Sec.Name, Ctx))
1199 return Err;
1200 }
1201 return Error::success();
1202}
1203
1204Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
1205 auto parseFieldDef = [&]() {
1206 uint32_t TypeCode = readVaruint32((Ctx));
1207 /* Discard StorageType */ parseValType(Ctx, TypeCode);
1208 /* Discard Mutability */ readVaruint32(Ctx);
1209 };
1210
1211 uint32_t Count = readVaruint32(Ctx);
1212 Signatures.reserve(Count);
1213 while (Count--) {
1214 wasm::WasmSignature Sig;
1215 uint8_t Form = readUint8(Ctx);
1216 if (Form == wasm::WASM_TYPE_REC) {
1217 // Rec groups expand the type index space (beyond what was declared at
1218 // the top of the section, and also consume one element in that space.
1219 uint32_t RecSize = readVaruint32(Ctx);
1220 if (RecSize == 0)
1221 return make_error<GenericBinaryError>("Rec group size cannot be 0",
1223 Signatures.reserve(Signatures.size() + RecSize);
1224 Count += RecSize;
1226 Signatures.push_back(std::move(Sig));
1227 HasUnmodeledTypes = true;
1228 continue;
1229 }
1230 if (Form != wasm::WASM_TYPE_FUNC) {
1231 // Currently LLVM only models function types, and not other composite
1232 // types. Here we parse the type declarations just enough to skip past
1233 // them in the binary.
1234 if (Form == wasm::WASM_TYPE_SUB || Form == wasm::WASM_TYPE_SUB_FINAL) {
1235 uint32_t Supers = readVaruint32(Ctx);
1236 if (Supers > 0) {
1237 if (Supers != 1)
1239 "Invalid number of supertypes", object_error::parse_failed);
1240 /* Discard SuperIndex */ readVaruint32(Ctx);
1241 }
1242 Form = readVaruint32(Ctx);
1243 }
1244 if (Form == wasm::WASM_TYPE_STRUCT) {
1245 uint32_t FieldCount = readVaruint32(Ctx);
1246 while (FieldCount--) {
1247 parseFieldDef();
1248 }
1249 } else if (Form == wasm::WASM_TYPE_ARRAY) {
1250 parseFieldDef();
1251 } else {
1252 return make_error<GenericBinaryError>("bad form",
1254 }
1256 Signatures.push_back(std::move(Sig));
1257 HasUnmodeledTypes = true;
1258 continue;
1259 }
1260
1261 uint32_t ParamCount = readVaruint32(Ctx);
1262 Sig.Params.reserve(ParamCount);
1263 while (ParamCount--) {
1264 uint32_t ParamType = readUint8(Ctx);
1265 Sig.Params.push_back(parseValType(Ctx, ParamType));
1266 }
1267 uint32_t ReturnCount = readVaruint32(Ctx);
1268 while (ReturnCount--) {
1269 uint32_t ReturnType = readUint8(Ctx);
1270 Sig.Returns.push_back(parseValType(Ctx, ReturnType));
1271 }
1272
1273 Signatures.push_back(std::move(Sig));
1274 }
1275 if (Ctx.Ptr != Ctx.End)
1276 return make_error<GenericBinaryError>("type section ended prematurely",
1278 return Error::success();
1279}
1280
1281Error WasmObjectFile::parseImport(ReadContext &Ctx, wasm::WasmImport &Im) {
1282 switch (Im.Kind) {
1284 NumImportedFunctions++;
1285 Im.SigIndex = readVaruint32(Ctx);
1286 if (Im.SigIndex >= Signatures.size())
1287 return make_error<GenericBinaryError>("invalid function type",
1289 break;
1291 NumImportedGlobals++;
1292 Im.Global.Type = readUint8(Ctx);
1293 Im.Global.Mutable = readVaruint1(Ctx);
1294 break;
1296 Im.Memory = readLimits(Ctx);
1298 HasMemory64 = true;
1299 break;
1301 Im.Table = readTableType(Ctx);
1302 NumImportedTables++;
1303 auto ElemType = Im.Table.ElemType;
1304 if (ElemType != wasm::ValType::FUNCREF &&
1305 ElemType != wasm::ValType::EXTERNREF &&
1306 ElemType != wasm::ValType::EXNREF &&
1307 ElemType != wasm::ValType::OTHERREF)
1308 return make_error<GenericBinaryError>("invalid table element type",
1310 break;
1311 }
1313 NumImportedTags++;
1314 if (readUint8(Ctx) != 0) // Reserved 'attribute' field
1315 return make_error<GenericBinaryError>("invalid attribute",
1317 Im.SigIndex = readVaruint32(Ctx);
1318 if (Im.SigIndex >= Signatures.size())
1319 return make_error<GenericBinaryError>("invalid tag type",
1321 break;
1322 default:
1323 return make_error<GenericBinaryError>("unexpected import kind: " +
1324 Twine(unsigned(Im.Kind)),
1326 }
1327 Imports.push_back(Im);
1328 return Error::success();
1329}
1330
1331Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
1332 uint32_t Count = readVaruint32(Ctx);
1333 Imports.reserve(Count);
1334 uint32_t I = 0;
1335 while (I < Count) {
1336 wasm::WasmImport Im;
1337 Im.Module = readString(Ctx);
1338 Im.Field = readString(Ctx);
1339 Im.Kind = readUint8(Ctx);
1340 // 0x7E/0x7F along with an empty Field signals a block of compact imports.
1341 if (Im.Kind == 0x7E && Im.Field == "") {
1343 "compact import format (0x7E) is not yet supported",
1345 } else if (Im.Kind == 0x7F && Im.Field == "") {
1346 uint32_t NumCompactImports = readVaruint32(Ctx);
1347 while (NumCompactImports--) {
1348 Im.Field = readString(Ctx);
1349 Im.Kind = readUint8(Ctx);
1350 Error rtn = parseImport(Ctx, Im);
1351 if (rtn)
1352 return rtn;
1353 I++;
1354 }
1355 } else {
1356 Error rtn = parseImport(Ctx, Im);
1357 if (rtn)
1358 return rtn;
1359 I++;
1360 }
1361 }
1362 if (Ctx.Ptr != Ctx.End)
1363 return make_error<GenericBinaryError>("import section ended prematurely",
1365 return Error::success();
1366}
1367
1368Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
1369 uint32_t Count = readVaruint32(Ctx);
1370 Functions.reserve(Count);
1371 uint32_t NumTypes = Signatures.size();
1372 while (Count--) {
1373 uint32_t Type = readVaruint32(Ctx);
1374 if (Type >= NumTypes)
1375 return make_error<GenericBinaryError>("invalid function type",
1377 wasm::WasmFunction F;
1378 F.SigIndex = Type;
1379 Functions.push_back(F);
1380 }
1381 if (Ctx.Ptr != Ctx.End)
1382 return make_error<GenericBinaryError>("function section ended prematurely",
1384 return Error::success();
1385}
1386
1387Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
1388 TableSection = Sections.size();
1389 uint32_t Count = readVaruint32(Ctx);
1390 Tables.reserve(Count);
1391 while (Count--) {
1392 wasm::WasmTable T;
1393 T.Type = readTableType(Ctx);
1394 T.Index = NumImportedTables + Tables.size();
1395 Tables.push_back(T);
1396 auto ElemType = Tables.back().Type.ElemType;
1397 if (ElemType != wasm::ValType::FUNCREF &&
1398 ElemType != wasm::ValType::EXTERNREF &&
1399 ElemType != wasm::ValType::EXNREF &&
1400 ElemType != wasm::ValType::OTHERREF) {
1401 return make_error<GenericBinaryError>("invalid table element type",
1403 }
1404 }
1405 if (Ctx.Ptr != Ctx.End)
1406 return make_error<GenericBinaryError>("table section ended prematurely",
1408 return Error::success();
1409}
1410
1411Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
1412 uint32_t Count = readVaruint32(Ctx);
1413 Memories.reserve(Count);
1414 while (Count--) {
1415 auto Limits = readLimits(Ctx);
1416 if (Limits.Flags & wasm::WASM_LIMITS_FLAG_IS_64)
1417 HasMemory64 = true;
1418 Memories.push_back(Limits);
1419 }
1420 if (Ctx.Ptr != Ctx.End)
1421 return make_error<GenericBinaryError>("memory section ended prematurely",
1423 return Error::success();
1424}
1425
1426Error WasmObjectFile::parseTagSection(ReadContext &Ctx) {
1427 TagSection = Sections.size();
1428 uint32_t Count = readVaruint32(Ctx);
1429 Tags.reserve(Count);
1430 uint32_t NumTypes = Signatures.size();
1431 while (Count--) {
1432 if (readUint8(Ctx) != 0) // Reserved 'attribute' field
1433 return make_error<GenericBinaryError>("invalid attribute",
1435 uint32_t Type = readVaruint32(Ctx);
1436 if (Type >= NumTypes)
1437 return make_error<GenericBinaryError>("invalid tag type",
1439 wasm::WasmTag Tag;
1440 Tag.Index = NumImportedTags + Tags.size();
1441 Tag.SigIndex = Type;
1442 Signatures[Type].Kind = wasm::WasmSignature::Tag;
1443 Tags.push_back(Tag);
1444 }
1445
1446 if (Ctx.Ptr != Ctx.End)
1447 return make_error<GenericBinaryError>("tag section ended prematurely",
1449 return Error::success();
1450}
1451
1452Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
1453 GlobalSection = Sections.size();
1454 const uint8_t *SectionStart = Ctx.Ptr;
1455 uint32_t Count = readVaruint32(Ctx);
1456 Globals.reserve(Count);
1457 while (Count--) {
1458 wasm::WasmGlobal Global;
1459 Global.Index = NumImportedGlobals + Globals.size();
1460 const uint8_t *GlobalStart = Ctx.Ptr;
1461 Global.Offset = static_cast<uint32_t>(GlobalStart - SectionStart);
1462 auto GlobalOpcode = readVaruint32(Ctx);
1463 Global.Type.Type = (uint8_t)parseValType(Ctx, GlobalOpcode);
1464 Global.Type.Mutable = readVaruint1(Ctx);
1465 if (Error Err = readInitExpr(Global.InitExpr, Ctx))
1466 return Err;
1467 Global.Size = static_cast<uint32_t>(Ctx.Ptr - GlobalStart);
1468 Globals.push_back(Global);
1469 }
1470 if (Ctx.Ptr != Ctx.End)
1471 return make_error<GenericBinaryError>("global section ended prematurely",
1473 return Error::success();
1474}
1475
1476Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
1477 uint32_t Count = readVaruint32(Ctx);
1478 Exports.reserve(Count);
1479 Symbols.reserve(Count);
1480
1481 // Build hash map of export flags for faster cross-referencing
1482 llvm::DenseMap<StringRef, uint32_t> ExportFlags;
1483 if (HasDylinkSection) {
1484 for (const auto &ExportInfo : DylinkInfo.ExportInfo) {
1485 ExportFlags[ExportInfo.Name] = ExportInfo.Flags;
1486 }
1487 }
1488
1489 for (uint32_t I = 0; I < Count; I++) {
1490 wasm::WasmExport Ex;
1491 Ex.Name = readString(Ctx);
1492 Ex.Kind = readUint8(Ctx);
1493 Ex.Index = readVaruint32(Ctx);
1494 const wasm::WasmSignature *Signature = nullptr;
1495 const wasm::WasmGlobalType *GlobalType = nullptr;
1496 const wasm::WasmTableType *TableType = nullptr;
1497 wasm::WasmSymbolInfo Info;
1498 Info.Name = Ex.Name;
1499 Info.Flags = 0;
1500 // For shared objects, symbol flags may be specified in the dylink section
1501 // instead of the export section
1502 if (HasDylinkSection) {
1503 auto It = ExportFlags.find(Ex.Name);
1504 if (It != ExportFlags.end()) {
1505 Info.Flags = It->second;
1506 }
1507 }
1508 switch (Ex.Kind) {
1510 if (!isValidFunctionIndex(Ex.Index))
1511 return make_error<GenericBinaryError>("invalid function export",
1514 Info.ElementIndex = Ex.Index;
1515 if (isDefinedFunctionIndex(Ex.Index)) {
1516 getDefinedFunction(Ex.Index).ExportName = Ex.Name;
1517 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions;
1518 wasm::WasmFunction &Function = Functions[FuncIndex];
1519 Signature = &Signatures[Function.SigIndex];
1520 }
1521 // Else the function is imported. LLVM object files don't use this
1522 // pattern and we still treat this as an undefined symbol, but we want to
1523 // parse it without crashing.
1524 break;
1525 }
1527 if (!isValidGlobalIndex(Ex.Index))
1528 return make_error<GenericBinaryError>("invalid global export",
1531 uint64_t Offset = 0;
1532 if (isDefinedGlobalIndex(Ex.Index)) {
1533 auto Global = getDefinedGlobal(Ex.Index);
1534 if (!Global.InitExpr.Extended) {
1535 auto Inst = Global.InitExpr.Inst;
1536 if (Inst.Opcode == wasm::WASM_OPCODE_I32_CONST) {
1537 Offset = Inst.Value.Int32;
1538 } else if (Inst.Opcode == wasm::WASM_OPCODE_I64_CONST) {
1539 Offset = Inst.Value.Int64;
1540 }
1541 }
1542 }
1543 Info.DataRef = wasm::WasmDataReference{0, Offset, 0};
1544 break;
1545 }
1547 if (!isValidTagIndex(Ex.Index))
1548 return make_error<GenericBinaryError>("invalid tag export",
1551 Info.ElementIndex = Ex.Index;
1552 if (isDefinedTagIndex(Ex.Index)) {
1553 unsigned TagIndex = Ex.Index - NumImportedTags;
1554 Signature = &Signatures[Tags[TagIndex].SigIndex];
1555 }
1556 break;
1558 break;
1561 Info.ElementIndex = Ex.Index;
1562 break;
1563 default:
1564 return make_error<GenericBinaryError>("unexpected export kind",
1566 }
1567 Exports.push_back(Ex);
1568 if (Ex.Kind != wasm::WASM_EXTERNAL_MEMORY) {
1569 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
1570 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
1571 }
1572 }
1573 if (Ctx.Ptr != Ctx.End)
1574 return make_error<GenericBinaryError>("export section ended prematurely",
1576 return Error::success();
1577}
1578
1579bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const {
1580 return Index < NumImportedFunctions + Functions.size();
1581}
1582
1583bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const {
1584 return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
1585}
1586
1587bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const {
1588 return Index < NumImportedGlobals + Globals.size();
1589}
1590
1591bool WasmObjectFile::isValidTableNumber(uint32_t Index) const {
1592 return Index < NumImportedTables + Tables.size();
1593}
1594
1595bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const {
1596 return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
1597}
1598
1599bool WasmObjectFile::isDefinedTableNumber(uint32_t Index) const {
1600 return Index >= NumImportedTables && isValidTableNumber(Index);
1601}
1602
1603bool WasmObjectFile::isValidTagIndex(uint32_t Index) const {
1604 return Index < NumImportedTags + Tags.size();
1605}
1606
1607bool WasmObjectFile::isDefinedTagIndex(uint32_t Index) const {
1608 return Index >= NumImportedTags && isValidTagIndex(Index);
1609}
1610
1611bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const {
1612 return Index < Symbols.size() && Symbols[Index].isTypeFunction();
1613}
1614
1615bool WasmObjectFile::isValidTableSymbol(uint32_t Index) const {
1616 return Index < Symbols.size() && Symbols[Index].isTypeTable();
1617}
1618
1619bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const {
1620 return Index < Symbols.size() && Symbols[Index].isTypeGlobal();
1621}
1622
1623bool WasmObjectFile::isValidTagSymbol(uint32_t Index) const {
1624 return Index < Symbols.size() && Symbols[Index].isTypeTag();
1625}
1626
1627bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const {
1628 return Index < Symbols.size() && Symbols[Index].isTypeData();
1629}
1630
1631bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const {
1632 return Index < Symbols.size() && Symbols[Index].isTypeSection();
1633}
1634
1635wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
1636 assert(isDefinedFunctionIndex(Index));
1637 return Functions[Index - NumImportedFunctions];
1638}
1639
1640const wasm::WasmFunction &
1641WasmObjectFile::getDefinedFunction(uint32_t Index) const {
1642 assert(isDefinedFunctionIndex(Index));
1643 return Functions[Index - NumImportedFunctions];
1644}
1645
1646const wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) const {
1647 assert(isDefinedGlobalIndex(Index));
1648 return Globals[Index - NumImportedGlobals];
1649}
1650
1651wasm::WasmTag &WasmObjectFile::getDefinedTag(uint32_t Index) {
1652 assert(isDefinedTagIndex(Index));
1653 return Tags[Index - NumImportedTags];
1654}
1655
1656Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
1657 StartFunction = readVaruint32(Ctx);
1658 if (!isValidFunctionIndex(StartFunction))
1659 return make_error<GenericBinaryError>("invalid start function",
1661 return Error::success();
1662}
1663
1664Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) {
1665 CodeSection = Sections.size();
1666 uint32_t FunctionCount = readVaruint32(Ctx);
1667 if (FunctionCount != Functions.size()) {
1668 return make_error<GenericBinaryError>("invalid function count",
1670 }
1671
1672 for (uint32_t i = 0; i < FunctionCount; i++) {
1673 wasm::WasmFunction& Function = Functions[i];
1674 const uint8_t *FunctionStart = Ctx.Ptr;
1675 uint32_t Size = readVaruint32(Ctx);
1676 const uint8_t *FunctionEnd = Ctx.Ptr + Size;
1677
1678 Function.CodeOffset = Ctx.Ptr - FunctionStart;
1679 Function.Index = NumImportedFunctions + i;
1680 Function.CodeSectionOffset = FunctionStart - Ctx.Start;
1681 Function.Size = FunctionEnd - FunctionStart;
1682
1683 uint32_t NumLocalDecls = readVaruint32(Ctx);
1684 Function.Locals.reserve(NumLocalDecls);
1685 while (NumLocalDecls--) {
1686 wasm::WasmLocalDecl Decl;
1687 Decl.Count = readVaruint32(Ctx);
1688 Decl.Type = readUint8(Ctx);
1689 Function.Locals.push_back(Decl);
1690 }
1691
1692 uint32_t BodySize = FunctionEnd - Ctx.Ptr;
1693 // Ensure that Function is within Ctx's buffer.
1694 if (Ctx.Ptr + BodySize > Ctx.End) {
1695 return make_error<GenericBinaryError>("Function extends beyond buffer",
1697 }
1698 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize);
1699 // This will be set later when reading in the linking metadata section.
1700 Function.Comdat = UINT32_MAX;
1701 Ctx.Ptr += BodySize;
1702 assert(Ctx.Ptr == FunctionEnd);
1703 }
1704 if (Ctx.Ptr != Ctx.End)
1705 return make_error<GenericBinaryError>("code section ended prematurely",
1707 return Error::success();
1708}
1709
1710Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
1711 uint32_t Count = readVaruint32(Ctx);
1712 ElemSegments.reserve(Count);
1713 while (Count--) {
1714 wasm::WasmElemSegment Segment;
1715 Segment.Flags = readVaruint32(Ctx);
1716
1717 uint32_t SupportedFlags = wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER |
1720 if (Segment.Flags & ~SupportedFlags)
1722 "Unsupported flags for element segment", object_error::parse_failed);
1723
1725 if ((Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_PASSIVE) == 0) {
1727 } else if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_DECLARATIVE) {
1729 } else {
1731 }
1732 bool HasTableNumber =
1735 bool HasElemKind =
1738 bool HasElemType =
1741 bool HasInitExprs =
1743
1744 if (HasTableNumber)
1745 Segment.TableNumber = readVaruint32(Ctx);
1746 else
1747 Segment.TableNumber = 0;
1748
1749 if (!isValidTableNumber(Segment.TableNumber))
1750 return make_error<GenericBinaryError>("invalid TableNumber",
1752
1754 Segment.Offset.Extended = false;
1756 Segment.Offset.Inst.Value.Int32 = 0;
1757 } else {
1758 if (Error Err = readInitExpr(Segment.Offset, Ctx))
1759 return Err;
1760 }
1761
1762 if (HasElemKind) {
1763 auto ElemKind = readVaruint32(Ctx);
1765 Segment.ElemKind = parseValType(Ctx, ElemKind);
1766 if (Segment.ElemKind != wasm::ValType::FUNCREF &&
1768 Segment.ElemKind != wasm::ValType::EXNREF &&
1769 Segment.ElemKind != wasm::ValType::OTHERREF) {
1770 return make_error<GenericBinaryError>("invalid elem type",
1772 }
1773 } else {
1774 if (ElemKind != 0)
1775 return make_error<GenericBinaryError>("invalid elem type",
1778 }
1779 } else if (HasElemType) {
1780 auto ElemType = parseValType(Ctx, readVaruint32(Ctx));
1781 Segment.ElemKind = ElemType;
1782 } else {
1784 }
1785
1786 uint32_t NumElems = readVaruint32(Ctx);
1787
1788 if (HasInitExprs) {
1789 while (NumElems--) {
1790 wasm::WasmInitExpr Expr;
1791 if (Error Err = readInitExpr(Expr, Ctx))
1792 return Err;
1793 }
1794 } else {
1795 while (NumElems--) {
1796 Segment.Functions.push_back(readVaruint32(Ctx));
1797 }
1798 }
1799 ElemSegments.push_back(Segment);
1800 }
1801 if (Ctx.Ptr != Ctx.End)
1802 return make_error<GenericBinaryError>("elem section ended prematurely",
1804 return Error::success();
1805}
1806
1807Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
1808 DataSection = Sections.size();
1809 uint32_t Count = readVaruint32(Ctx);
1810 if (DataCount && Count != *DataCount)
1812 "number of data segments does not match DataCount section");
1813 DataSegments.reserve(Count);
1814 while (Count--) {
1815 WasmSegment Segment;
1816 Segment.Data.InitFlags = readVaruint32(Ctx);
1817 Segment.Data.MemoryIndex =
1819 ? readVaruint32(Ctx)
1820 : 0;
1821 if ((Segment.Data.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
1822 if (Error Err = readInitExpr(Segment.Data.Offset, Ctx))
1823 return Err;
1824 } else {
1825 Segment.Data.Offset.Extended = false;
1827 Segment.Data.Offset.Inst.Value.Int32 = 0;
1828 }
1829 uint32_t Size = readVaruint32(Ctx);
1830 if (Size > (size_t)(Ctx.End - Ctx.Ptr))
1831 return make_error<GenericBinaryError>("invalid segment size",
1833 Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
1834 // The rest of these Data fields are set later, when reading in the linking
1835 // metadata section.
1836 Segment.Data.Alignment = 0;
1837 Segment.Data.LinkingFlags = 0;
1838 Segment.Data.Comdat = UINT32_MAX;
1839 Segment.SectionOffset = Ctx.Ptr - Ctx.Start;
1840 Ctx.Ptr += Size;
1841 DataSegments.push_back(Segment);
1842 }
1843 if (Ctx.Ptr != Ctx.End)
1844 return make_error<GenericBinaryError>("data section ended prematurely",
1846 return Error::success();
1847}
1848
1849Error WasmObjectFile::parseDataCountSection(ReadContext &Ctx) {
1850 DataCount = readVaruint32(Ctx);
1851 return Error::success();
1852}
1853
1855 return Header;
1856}
1857
1858void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.b++; }
1859
1862 const WasmSymbol &Sym = getWasmSymbol(Symb);
1863
1864 LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n");
1865 if (Sym.isBindingWeak())
1866 Result |= SymbolRef::SF_Weak;
1867 if (!Sym.isBindingLocal())
1868 Result |= SymbolRef::SF_Global;
1869 if (Sym.isHidden())
1870 Result |= SymbolRef::SF_Hidden;
1871 if (!Sym.isDefined())
1872 Result |= SymbolRef::SF_Undefined;
1873 if (Sym.isTypeFunction())
1874 Result |= SymbolRef::SF_Executable;
1875 return Result;
1876}
1877
1880 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null
1881 Ref.d.b = 0; // Symbol index
1882 return BasicSymbolRef(Ref, this);
1883}
1884
1887 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null
1888 Ref.d.b = Symbols.size(); // Symbol index
1889 return BasicSymbolRef(Ref, this);
1890}
1891
1893 return Symbols[Symb.d.b];
1894}
1895
1897 return getWasmSymbol(Symb.getRawDataRefImpl());
1898}
1899
1903
1905 auto &Sym = getWasmSymbol(Symb);
1906 if (!Sym.isDefined())
1907 return 0;
1909 if (!Sec)
1910 return Sec.takeError();
1911 uint32_t SectionAddress = getSectionAddress(Sec.get()->getRawDataRefImpl());
1912 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION &&
1913 isDefinedFunctionIndex(Sym.Info.ElementIndex)) {
1914 return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset +
1915 SectionAddress;
1916 }
1917 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL &&
1918 isDefinedGlobalIndex(Sym.Info.ElementIndex)) {
1919 return getDefinedGlobal(Sym.Info.ElementIndex).Offset + SectionAddress;
1920 }
1921
1922 return getSymbolValue(Symb);
1923}
1924
1926 switch (Sym.Info.Kind) {
1931 return Sym.Info.ElementIndex;
1933 // The value of a data symbol is the segment offset, plus the symbol
1934 // offset within the segment.
1935 uint32_t SegmentIndex = Sym.Info.DataRef.Segment;
1936 const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data;
1937 if (Segment.Offset.Extended) {
1938 llvm_unreachable("extended init exprs not supported");
1939 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_I32_CONST) {
1940 return Segment.Offset.Inst.Value.Int32 + Sym.Info.DataRef.Offset;
1941 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_I64_CONST) {
1942 return Segment.Offset.Inst.Value.Int64 + Sym.Info.DataRef.Offset;
1943 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_GLOBAL_GET) {
1944 return Sym.Info.DataRef.Offset;
1945 } else {
1946 llvm_unreachable("unknown init expr opcode");
1947 }
1948 }
1950 return 0;
1951 }
1952 llvm_unreachable("invalid symbol type");
1953}
1954
1958
1960 llvm_unreachable("not yet implemented");
1961 return 0;
1962}
1963
1965 llvm_unreachable("not yet implemented");
1966 return 0;
1967}
1968
1971 const WasmSymbol &Sym = getWasmSymbol(Symb);
1972
1973 switch (Sym.Info.Kind) {
1977 return SymbolRef::ST_Other;
1979 return SymbolRef::ST_Data;
1981 return SymbolRef::ST_Debug;
1983 return SymbolRef::ST_Other;
1985 return SymbolRef::ST_Other;
1986 }
1987
1988 llvm_unreachable("unknown WasmSymbol::SymbolType");
1989 return SymbolRef::ST_Other;
1990}
1991
1994 const WasmSymbol &Sym = getWasmSymbol(Symb);
1995 if (Sym.isUndefined())
1996 return section_end();
1997
1999 Ref.d.a = getSymbolSectionIdImpl(Sym);
2000 return section_iterator(SectionRef(Ref, this));
2001}
2002
2004 const WasmSymbol &Sym = getWasmSymbol(Symb);
2005 return getSymbolSectionIdImpl(Sym);
2006}
2007
2008uint32_t WasmObjectFile::getSymbolSectionIdImpl(const WasmSymbol &Sym) const {
2009 switch (Sym.Info.Kind) {
2011 return CodeSection;
2013 return GlobalSection;
2015 return DataSection;
2017 return Sym.Info.ElementIndex;
2019 return TagSection;
2021 return TableSection;
2022 default:
2023 llvm_unreachable("unknown WasmSymbol::SymbolType");
2024 }
2025}
2026
2028 const WasmSymbol &Sym = getWasmSymbol(Symb);
2029 if (!Sym.isDefined())
2030 return 0;
2031 if (Sym.isTypeGlobal())
2032 return getDefinedGlobal(Sym.Info.ElementIndex).Size;
2033 if (Sym.isTypeData())
2034 return Sym.Info.DataRef.Size;
2035 if (Sym.isTypeFunction())
2036 return functions()[Sym.Info.ElementIndex - getNumImportedFunctions()].Size;
2037 // Currently symbol size is only tracked for data segments and functions. In
2038 // principle we could also track size (e.g. binary size) for tables, globals
2039 // and element segments etc too.
2040 return 0;
2041}
2042
2044
2053
2055 // For object files, use 0 for section addresses, and section offsets for
2056 // symbol addresses. For linked files, use file offsets.
2057 // See also getSymbolAddress.
2058 return isRelocatableObject() || isSharedObject() ? 0
2059 : Sections[Sec.d.a].Offset;
2060}
2061
2063 return Sec.d.a;
2064}
2065
2067 const WasmSection &S = Sections[Sec.d.a];
2068 return S.Content.size();
2069}
2070
2073 const WasmSection &S = Sections[Sec.d.a];
2074 // This will never fail since wasm sections can never be empty (user-sections
2075 // must have a name and non-user sections each have a defined structure).
2076 return S.Content;
2077}
2078
2082
2084 return false;
2085}
2086
2090
2094
2095bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
2096
2097bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
2098
2100 DataRefImpl RelocRef;
2101 RelocRef.d.a = Ref.d.a;
2102 RelocRef.d.b = 0;
2103 return relocation_iterator(RelocationRef(RelocRef, this));
2104}
2105
2107 const WasmSection &Sec = getWasmSection(Ref);
2108 DataRefImpl RelocRef;
2109 RelocRef.d.a = Ref.d.a;
2110 RelocRef.d.b = Sec.Relocations.size();
2111 return relocation_iterator(RelocationRef(RelocRef, this));
2112}
2113
2115
2120
2123 if (Rel.Type == wasm::R_WASM_TYPE_INDEX_LEB)
2124 return symbol_end();
2125 DataRefImpl Sym;
2126 Sym.d.a = 1;
2127 Sym.d.b = Rel.Index;
2128 return symbol_iterator(SymbolRef(Sym, this));
2129}
2130
2135
2137 DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
2139 StringRef Res = "Unknown";
2140
2141#define WASM_RELOC(name, value) \
2142 case wasm::name: \
2143 Res = #name; \
2144 break;
2145
2146 switch (Rel.Type) {
2147#include "llvm/BinaryFormat/WasmRelocs.def"
2148 }
2149
2150#undef WASM_RELOC
2151
2152 Result.append(Res.begin(), Res.end());
2153}
2154
2157 Ref.d.a = 0;
2158 return section_iterator(SectionRef(Ref, this));
2159}
2160
2163 Ref.d.a = Sections.size();
2164 return section_iterator(SectionRef(Ref, this));
2165}
2166
2168 return HasMemory64 ? 8 : 4;
2169}
2170
2172
2174 return HasMemory64 ? Triple::wasm64 : Triple::wasm32;
2175}
2176
2180
2181bool WasmObjectFile::isRelocatableObject() const { return HasLinkingSection; }
2182
2183bool WasmObjectFile::isSharedObject() const { return HasDylinkSection; }
2184
2186 assert(Ref.d.a < Sections.size());
2187 return Sections[Ref.d.a];
2188}
2189
2190const WasmSection &
2192 return getWasmSection(Section.getRawDataRefImpl());
2193}
2194
2197 return getWasmRelocation(Ref.getRawDataRefImpl());
2198}
2199
2202 assert(Ref.d.a < Sections.size());
2203 const WasmSection &Sec = Sections[Ref.d.a];
2204 assert(Ref.d.b < Sec.Relocations.size());
2205 return Sec.Relocations[Ref.d.b];
2206}
2207
2208int WasmSectionOrderChecker::getSectionOrder(unsigned ID,
2209 StringRef CustomSectionName) {
2210 switch (ID) {
2212 return StringSwitch<unsigned>(CustomSectionName)
2213 .Case("dylink", WASM_SEC_ORDER_DYLINK)
2214 .Case("dylink.0", WASM_SEC_ORDER_DYLINK)
2215 .Case("linking", WASM_SEC_ORDER_LINKING)
2217 .Case("name", WASM_SEC_ORDER_NAME)
2218 .Case("producers", WASM_SEC_ORDER_PRODUCERS)
2219 .Case("target_features", WASM_SEC_ORDER_TARGET_FEATURES)
2222 return WASM_SEC_ORDER_TYPE;
2224 return WASM_SEC_ORDER_IMPORT;
2228 return WASM_SEC_ORDER_TABLE;
2230 return WASM_SEC_ORDER_MEMORY;
2232 return WASM_SEC_ORDER_GLOBAL;
2234 return WASM_SEC_ORDER_EXPORT;
2236 return WASM_SEC_ORDER_START;
2238 return WASM_SEC_ORDER_ELEM;
2240 return WASM_SEC_ORDER_CODE;
2242 return WASM_SEC_ORDER_DATA;
2245 case wasm::WASM_SEC_TAG:
2246 return WASM_SEC_ORDER_TAG;
2247 default:
2248 return WASM_SEC_ORDER_NONE;
2249 }
2250}
2251
2252// Represents the edges in a directed graph where any node B reachable from node
2253// A is not allowed to appear before A in the section ordering, but may appear
2254// afterward.
2256 [WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = {
2257 // WASM_SEC_ORDER_NONE
2258 {},
2259 // WASM_SEC_ORDER_TYPE
2260 {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT},
2261 // WASM_SEC_ORDER_IMPORT
2262 {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION},
2263 // WASM_SEC_ORDER_FUNCTION
2264 {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE},
2265 // WASM_SEC_ORDER_TABLE
2266 {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY},
2267 // WASM_SEC_ORDER_MEMORY
2268 {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_TAG},
2269 // WASM_SEC_ORDER_TAG
2270 {WASM_SEC_ORDER_TAG, WASM_SEC_ORDER_GLOBAL},
2271 // WASM_SEC_ORDER_GLOBAL
2272 {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EXPORT},
2273 // WASM_SEC_ORDER_EXPORT
2274 {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START},
2275 // WASM_SEC_ORDER_START
2276 {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM},
2277 // WASM_SEC_ORDER_ELEM
2278 {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT},
2279 // WASM_SEC_ORDER_DATACOUNT
2280 {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE},
2281 // WASM_SEC_ORDER_CODE
2282 {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA},
2283 // WASM_SEC_ORDER_DATA
2284 {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING},
2285
2286 // Custom Sections
2287 // WASM_SEC_ORDER_DYLINK
2288 {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE},
2289 // WASM_SEC_ORDER_LINKING
2290 {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME},
2291 // WASM_SEC_ORDER_RELOC (can be repeated)
2292 {},
2293 // WASM_SEC_ORDER_NAME
2294 {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS},
2295 // WASM_SEC_ORDER_PRODUCERS
2296 {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES},
2297 // WASM_SEC_ORDER_TARGET_FEATURES
2298 {WASM_SEC_ORDER_TARGET_FEATURES}};
2299
2301 StringRef CustomSectionName) {
2302 int Order = getSectionOrder(ID, CustomSectionName);
2303 if (Order == WASM_SEC_ORDER_NONE)
2304 return true;
2305
2306 // Disallowed predecessors we need to check for
2308
2309 // Keep track of completed checks to avoid repeating work
2310 bool Checked[WASM_NUM_SEC_ORDERS] = {};
2311
2312 int Curr = Order;
2313 while (true) {
2314 // Add new disallowed predecessors to work list
2315 for (size_t I = 0;; ++I) {
2316 int Next = DisallowedPredecessors[Curr][I];
2318 break;
2319 if (Checked[Next])
2320 continue;
2321 WorkList.push_back(Next);
2322 Checked[Next] = true;
2323 }
2324
2325 if (WorkList.empty())
2326 break;
2327
2328 // Consider next disallowed predecessor
2329 Curr = WorkList.pop_back_val();
2330 if (Seen[Curr])
2331 return false;
2332 }
2333
2334 // Have not seen any disallowed predecessors
2335 Seen[Order] = true;
2336 return true;
2337}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:663
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:591
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
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:212
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[]
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
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:432
const unsigned WASM_SYMBOL_UNDEFINED
Definition Wasm.h:256
@ 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:259
@ 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:260
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:435
const unsigned WASM_SYMBOL_EXPORTED
Definition Wasm.h:257
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:258
const unsigned WASM_SYMBOL_ABSOLUTE
Definition Wasm.h:261
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:558
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:56
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:164
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:334
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
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
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
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
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:426
WasmInitExpr Offset
Definition Wasm.h:424
std::vector< StringRef > Needed
Definition Wasm.h:307
WasmInitExpr Offset
Definition Wasm.h:447
std::vector< uint32_t > Functions
Definition Wasm.h:448
WasmLimits Memory
Definition Wasm.h:396
StringRef Field
Definition Wasm.h:390
WasmGlobalType Global
Definition Wasm.h:394
StringRef Module
Definition Wasm.h:389
uint32_t SigIndex
Definition Wasm.h:393
WasmTableType Table
Definition Wasm.h:395
union llvm::wasm::WasmInitExprMVP::@234311111124136373374304035273310237314141125040 Value
WasmInitExprMVP Inst
Definition Wasm.h:364
ArrayRef< uint8_t > Body
Definition Wasm.h:365
SmallVector< ValType, 1 > Returns
Definition Wasm.h:516
SmallVector< ValType, 4 > Params
Definition Wasm.h:517
enum llvm::wasm::WasmSignature::@330257225222011177372050205212257221301063235146 Kind
WasmDataReference DataRef
Definition Wasm.h:488
WasmTableType Type
Definition Wasm.h:344
StringRef SymbolName
Definition Wasm.h:345
struct llvm::object::DataRefImpl::@005117267142344013370254144343227032034000327225 d