LLVM 20.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";
50 }
51 Out << "]";
52 if (!isTypeData()) {
53 Out << ", ElemIndex=" << Info.ElementIndex;
54 } else if (isDefined()) {
55 Out << ", Segment=" << Info.DataRef.Segment;
56 Out << ", Offset=" << Info.DataRef.Offset;
57 Out << ", Size=" << Info.DataRef.Size;
58 }
59}
60
61#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
63#endif
64
67 Error Err = Error::success();
68 auto ObjectFile = std::make_unique<WasmObjectFile>(Buffer, Err);
69 if (Err)
70 return std::move(Err);
71
72 return std::move(ObjectFile);
73}
74
75#define VARINT7_MAX ((1 << 7) - 1)
76#define VARINT7_MIN (-(1 << 7))
77#define VARUINT7_MAX (1 << 7)
78#define VARUINT1_MAX (1)
79
81 if (Ctx.Ptr == Ctx.End)
82 report_fatal_error("EOF while reading uint8");
83 return *Ctx.Ptr++;
84}
85
87 if (Ctx.Ptr + 4 > Ctx.End)
88 report_fatal_error("EOF while reading uint32");
90 Ctx.Ptr += 4;
91 return Result;
92}
93
95 if (Ctx.Ptr + 4 > Ctx.End)
96 report_fatal_error("EOF while reading float64");
97 int32_t Result = 0;
98 memcpy(&Result, Ctx.Ptr, sizeof(Result));
99 Ctx.Ptr += sizeof(Result);
100 return Result;
101}
102
104 if (Ctx.Ptr + 8 > Ctx.End)
105 report_fatal_error("EOF while reading float64");
106 int64_t Result = 0;
107 memcpy(&Result, Ctx.Ptr, sizeof(Result));
108 Ctx.Ptr += sizeof(Result);
109 return Result;
110}
111
113 unsigned Count;
114 const char *Error = nullptr;
115 uint64_t Result = decodeULEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
116 if (Error)
118 Ctx.Ptr += Count;
119 return Result;
120}
121
123 uint32_t StringLen = readULEB128(Ctx);
124 if (Ctx.Ptr + StringLen > Ctx.End)
125 report_fatal_error("EOF while reading string");
126 StringRef Return =
127 StringRef(reinterpret_cast<const char *>(Ctx.Ptr), StringLen);
128 Ctx.Ptr += StringLen;
129 return Return;
130}
131
133 unsigned Count;
134 const char *Error = nullptr;
135 uint64_t Result = decodeSLEB128(Ctx.Ptr, &Count, Ctx.End, &Error);
136 if (Error)
138 Ctx.Ptr += Count;
139 return Result;
140}
141
143 int64_t Result = readLEB128(Ctx);
144 if (Result > VARUINT1_MAX || Result < 0)
145 report_fatal_error("LEB is outside Varuint1 range");
146 return Result;
147}
148
150 int64_t Result = readLEB128(Ctx);
151 if (Result > INT32_MAX || Result < INT32_MIN)
152 report_fatal_error("LEB is outside Varint32 range");
153 return Result;
154}
155
157 uint64_t Result = readULEB128(Ctx);
158 if (Result > UINT32_MAX)
159 report_fatal_error("LEB is outside Varuint32 range");
160 return Result;
161}
162
164 return readLEB128(Ctx);
165}
166
168 return readULEB128(Ctx);
169}
170
172 return readUint8(Ctx);
173}
174
176 uint32_t Code) {
177 // only directly encoded FUNCREF/EXTERNREF/EXNREF are supported
178 // (not ref null func, ref null extern, or ref null exn)
179 switch (Code) {
188 return wasm::ValType(Code);
189 }
191 /* Discard HeapType */ readVarint64(Ctx);
192 }
194}
195
198 auto Start = Ctx.Ptr;
199
200 Expr.Extended = false;
201 Expr.Inst.Opcode = readOpcode(Ctx);
202 switch (Expr.Inst.Opcode) {
204 Expr.Inst.Value.Int32 = readVarint32(Ctx);
205 break;
207 Expr.Inst.Value.Int64 = readVarint64(Ctx);
208 break;
210 Expr.Inst.Value.Float32 = readFloat32(Ctx);
211 break;
213 Expr.Inst.Value.Float64 = readFloat64(Ctx);
214 break;
216 Expr.Inst.Value.Global = readULEB128(Ctx);
217 break;
219 /* Discard type */ parseValType(Ctx, readVaruint32(Ctx));
220 break;
221 }
222 default:
223 Expr.Extended = true;
224 }
225
226 if (!Expr.Extended) {
227 uint8_t EndOpcode = readOpcode(Ctx);
228 if (EndOpcode != wasm::WASM_OPCODE_END)
229 Expr.Extended = true;
230 }
231
232 if (Expr.Extended) {
233 Ctx.Ptr = Start;
234 while (true) {
235 uint8_t Opcode = readOpcode(Ctx);
236 switch (Opcode) {
242 readULEB128(Ctx);
243 break;
245 readFloat32(Ctx);
246 break;
248 readFloat64(Ctx);
249 break;
256 break;
258 break;
259 // The GC opcodes are in a separate (prefixed space). This flat switch
260 // structure works as long as there is no overlap between the GC and
261 // general opcodes used in init exprs.
266 readULEB128(Ctx); // heap type index
267 break;
269 readULEB128(Ctx); // heap type index
270 readULEB128(Ctx); // array size
271 break;
273 break;
275 Expr.Body = ArrayRef<uint8_t>(Start, Ctx.Ptr - Start);
276 return Error::success();
277 default:
278 return make_error<GenericBinaryError>(
279 Twine("invalid opcode in init_expr: ") + Twine(unsigned(Opcode)),
281 }
282 }
283 }
284
285 return Error::success();
286}
287
289 wasm::WasmLimits Result;
290 Result.Flags = readVaruint32(Ctx);
291 Result.Minimum = readVaruint64(Ctx);
292 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
293 Result.Maximum = readVaruint64(Ctx);
294 return Result;
295}
296
298 wasm::WasmTableType TableType;
299 auto ElemType = parseValType(Ctx, readVaruint32(Ctx));
300 TableType.ElemType = ElemType;
301 TableType.Limits = readLimits(Ctx);
302 return TableType;
303}
304
306 WasmSectionOrderChecker &Checker) {
307 Section.Type = readUint8(Ctx);
308 LLVM_DEBUG(dbgs() << "readSection type=" << Section.Type << "\n");
309 // When reading the section's size, store the size of the LEB used to encode
310 // it. This allows objcopy/strip to reproduce the binary identically.
311 const uint8_t *PreSizePtr = Ctx.Ptr;
313 Section.HeaderSecSizeEncodingLen = Ctx.Ptr - PreSizePtr;
314 Section.Offset = Ctx.Ptr - Ctx.Start;
315 if (Size == 0)
316 return make_error<StringError>("zero length section",
318 if (Ctx.Ptr + Size > Ctx.End)
319 return make_error<StringError>("section too large",
321 if (Section.Type == wasm::WASM_SEC_CUSTOM) {
323 SectionCtx.Start = Ctx.Ptr;
324 SectionCtx.Ptr = Ctx.Ptr;
325 SectionCtx.End = Ctx.Ptr + Size;
326
327 Section.Name = readString(SectionCtx);
328
329 uint32_t SectionNameSize = SectionCtx.Ptr - SectionCtx.Start;
330 Ctx.Ptr += SectionNameSize;
331 Size -= SectionNameSize;
332 }
333
334 if (!Checker.isValidSectionOrder(Section.Type, Section.Name)) {
335 return make_error<StringError>("out of order section type: " +
336 llvm::to_string(Section.Type),
338 }
339
340 Section.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
341 Ctx.Ptr += Size;
342 return Error::success();
343}
344
346 : ObjectFile(Binary::ID_Wasm, Buffer) {
347 ErrorAsOutParameter ErrAsOutParam(Err);
348 Header.Magic = getData().substr(0, 4);
349 if (Header.Magic != StringRef("\0asm", 4)) {
350 Err = make_error<StringError>("invalid magic number",
352 return;
353 }
354
355 ReadContext Ctx;
356 Ctx.Start = getData().bytes_begin();
357 Ctx.Ptr = Ctx.Start + 4;
358 Ctx.End = Ctx.Start + getData().size();
359
360 if (Ctx.Ptr + 4 > Ctx.End) {
361 Err = make_error<StringError>("missing version number",
363 return;
364 }
365
366 Header.Version = readUint32(Ctx);
367 if (Header.Version != wasm::WasmVersion) {
368 Err = make_error<StringError>("invalid version number: " +
369 Twine(Header.Version),
371 return;
372 }
373
375 while (Ctx.Ptr < Ctx.End) {
376 WasmSection Sec;
377 if ((Err = readSection(Sec, Ctx, Checker)))
378 return;
379 if ((Err = parseSection(Sec)))
380 return;
381
382 Sections.push_back(Sec);
383 }
384}
385
386Error WasmObjectFile::parseSection(WasmSection &Sec) {
387 ReadContext Ctx;
388 Ctx.Start = Sec.Content.data();
389 Ctx.End = Ctx.Start + Sec.Content.size();
390 Ctx.Ptr = Ctx.Start;
391 switch (Sec.Type) {
393 return parseCustomSection(Sec, Ctx);
395 return parseTypeSection(Ctx);
397 return parseImportSection(Ctx);
399 return parseFunctionSection(Ctx);
401 return parseTableSection(Ctx);
403 return parseMemorySection(Ctx);
405 return parseTagSection(Ctx);
407 return parseGlobalSection(Ctx);
409 return parseExportSection(Ctx);
411 return parseStartSection(Ctx);
413 return parseElemSection(Ctx);
415 return parseCodeSection(Ctx);
417 return parseDataSection(Ctx);
419 return parseDataCountSection(Ctx);
420 default:
421 return make_error<GenericBinaryError>(
422 "invalid section type: " + Twine(Sec.Type), object_error::parse_failed);
423 }
424}
425
426Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) {
427 // Legacy "dylink" section support.
428 // See parseDylink0Section for the current "dylink.0" section parsing.
429 HasDylinkSection = true;
430 DylinkInfo.MemorySize = readVaruint32(Ctx);
431 DylinkInfo.MemoryAlignment = readVaruint32(Ctx);
432 DylinkInfo.TableSize = readVaruint32(Ctx);
433 DylinkInfo.TableAlignment = readVaruint32(Ctx);
434 uint32_t Count = readVaruint32(Ctx);
435 while (Count--) {
436 DylinkInfo.Needed.push_back(readString(Ctx));
437 }
438
439 if (Ctx.Ptr != Ctx.End)
440 return make_error<GenericBinaryError>("dylink section ended prematurely",
442 return Error::success();
443}
444
445Error WasmObjectFile::parseDylink0Section(ReadContext &Ctx) {
446 // See
447 // https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
448 HasDylinkSection = true;
449
450 const uint8_t *OrigEnd = Ctx.End;
451 while (Ctx.Ptr < OrigEnd) {
452 Ctx.End = OrigEnd;
453 uint8_t Type = readUint8(Ctx);
455 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size
456 << "\n");
457 Ctx.End = Ctx.Ptr + Size;
458 uint32_t Count;
459 switch (Type) {
461 DylinkInfo.MemorySize = readVaruint32(Ctx);
462 DylinkInfo.MemoryAlignment = readVaruint32(Ctx);
463 DylinkInfo.TableSize = readVaruint32(Ctx);
464 DylinkInfo.TableAlignment = readVaruint32(Ctx);
465 break;
467 Count = readVaruint32(Ctx);
468 while (Count--) {
469 DylinkInfo.Needed.push_back(readString(Ctx));
470 }
471 break;
473 uint32_t Count = readVaruint32(Ctx);
474 while (Count--) {
475 DylinkInfo.ExportInfo.push_back({readString(Ctx), readVaruint32(Ctx)});
476 }
477 break;
478 }
480 uint32_t Count = readVaruint32(Ctx);
481 while (Count--) {
482 DylinkInfo.ImportInfo.push_back(
483 {readString(Ctx), readString(Ctx), readVaruint32(Ctx)});
484 }
485 break;
486 }
487 default:
488 LLVM_DEBUG(dbgs() << "unknown dylink.0 sub-section: " << Type << "\n");
489 Ctx.Ptr += Size;
490 break;
491 }
492 if (Ctx.Ptr != Ctx.End) {
493 return make_error<GenericBinaryError>(
494 "dylink.0 sub-section ended prematurely", object_error::parse_failed);
495 }
496 }
497
498 if (Ctx.Ptr != Ctx.End)
499 return make_error<GenericBinaryError>("dylink.0 section ended prematurely",
501 return Error::success();
502}
503
504Error WasmObjectFile::parseNameSection(ReadContext &Ctx) {
505 llvm::DenseSet<uint64_t> SeenFunctions;
506 llvm::DenseSet<uint64_t> SeenGlobals;
507 llvm::DenseSet<uint64_t> SeenSegments;
508
509 // If we have linking section (symbol table) or if we are parsing a DSO
510 // then we don't use the name section for symbol information.
511 bool PopulateSymbolTable = !HasLinkingSection && !HasDylinkSection;
512
513 // If we are using the name section for symbol information then it will
514 // supersede any symbols created by the export section.
515 if (PopulateSymbolTable)
516 Symbols.clear();
517
518 while (Ctx.Ptr < Ctx.End) {
519 uint8_t Type = readUint8(Ctx);
521 const uint8_t *SubSectionEnd = Ctx.Ptr + Size;
522
523 switch (Type) {
527 uint32_t Count = readVaruint32(Ctx);
528 while (Count--) {
534 /* Flags */ 0,
535 /* ImportModule */ std::nullopt,
536 /* ImportName */ std::nullopt,
537 /* ExportName */ std::nullopt,
538 {/* ElementIndex */ Index}};
539 const wasm::WasmSignature *Signature = nullptr;
540 const wasm::WasmGlobalType *GlobalType = nullptr;
541 const wasm::WasmTableType *TableType = nullptr;
543 if (!SeenFunctions.insert(Index).second)
544 return make_error<GenericBinaryError>(
545 "function named more than once", object_error::parse_failed);
546 if (!isValidFunctionIndex(Index) || Name.empty())
547 return make_error<GenericBinaryError>("invalid function name entry",
549
550 if (isDefinedFunctionIndex(Index)) {
551 wasm::WasmFunction &F = getDefinedFunction(Index);
552 F.DebugName = Name;
553 Signature = &Signatures[F.SigIndex];
554 if (F.ExportName) {
555 Info.ExportName = F.ExportName;
557 } else {
559 }
560 } else {
562 }
563 } else if (Type == wasm::WASM_NAMES_GLOBAL) {
564 if (!SeenGlobals.insert(Index).second)
565 return make_error<GenericBinaryError>("global named more than once",
567 if (!isValidGlobalIndex(Index) || Name.empty())
568 return make_error<GenericBinaryError>("invalid global name entry",
570 nameType = wasm::NameType::GLOBAL;
572 if (isDefinedGlobalIndex(Index)) {
573 GlobalType = &getDefinedGlobal(Index).Type;
574 } else {
576 }
577 } else {
578 if (!SeenSegments.insert(Index).second)
579 return make_error<GenericBinaryError>(
580 "segment named more than once", object_error::parse_failed);
581 if (Index > DataSegments.size())
582 return make_error<GenericBinaryError>("invalid data segment name entry",
587 assert(Index < DataSegments.size());
589 Index, 0, DataSegments[Index].Data.Content.size()};
590 }
591 DebugNames.push_back(wasm::WasmDebugName{nameType, Index, Name});
592 if (PopulateSymbolTable)
593 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
594 }
595 break;
596 }
597 // Ignore local names for now
599 default:
600 Ctx.Ptr += Size;
601 break;
602 }
603 if (Ctx.Ptr != SubSectionEnd)
604 return make_error<GenericBinaryError>(
605 "name sub-section ended prematurely", object_error::parse_failed);
606 }
607
608 if (Ctx.Ptr != Ctx.End)
609 return make_error<GenericBinaryError>("name section ended prematurely",
611 return Error::success();
612}
613
614Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) {
615 HasLinkingSection = true;
616
617 LinkingData.Version = readVaruint32(Ctx);
618 if (LinkingData.Version != wasm::WasmMetadataVersion) {
619 return make_error<GenericBinaryError>(
620 "unexpected metadata version: " + Twine(LinkingData.Version) +
621 " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")",
623 }
624
625 const uint8_t *OrigEnd = Ctx.End;
626 while (Ctx.Ptr < OrigEnd) {
627 Ctx.End = OrigEnd;
628 uint8_t Type = readUint8(Ctx);
630 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size
631 << "\n");
632 Ctx.End = Ctx.Ptr + Size;
633 switch (Type) {
635 if (Error Err = parseLinkingSectionSymtab(Ctx))
636 return Err;
637 break;
639 uint32_t Count = readVaruint32(Ctx);
640 if (Count > DataSegments.size())
641 return make_error<GenericBinaryError>("too many segment names",
643 for (uint32_t I = 0; I < Count; I++) {
644 DataSegments[I].Data.Name = readString(Ctx);
645 DataSegments[I].Data.Alignment = readVaruint32(Ctx);
646 DataSegments[I].Data.LinkingFlags = readVaruint32(Ctx);
647 }
648 break;
649 }
651 uint32_t Count = readVaruint32(Ctx);
652 LinkingData.InitFunctions.reserve(Count);
653 for (uint32_t I = 0; I < Count; I++) {
655 Init.Priority = readVaruint32(Ctx);
656 Init.Symbol = readVaruint32(Ctx);
657 if (!isValidFunctionSymbol(Init.Symbol))
658 return make_error<GenericBinaryError>("invalid function symbol: " +
659 Twine(Init.Symbol),
661 LinkingData.InitFunctions.emplace_back(Init);
662 }
663 break;
664 }
666 if (Error Err = parseLinkingSectionComdat(Ctx))
667 return Err;
668 break;
669 default:
670 Ctx.Ptr += Size;
671 break;
672 }
673 if (Ctx.Ptr != Ctx.End)
674 return make_error<GenericBinaryError>(
675 "linking sub-section ended prematurely", object_error::parse_failed);
676 }
677 if (Ctx.Ptr != OrigEnd)
678 return make_error<GenericBinaryError>("linking section ended prematurely",
680 return Error::success();
681}
682
683Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
684 uint32_t Count = readVaruint32(Ctx);
685 // Clear out any symbol information that was derived from the exports
686 // section.
687 Symbols.clear();
688 Symbols.reserve(Count);
689 StringSet<> SymbolNames;
690
691 std::vector<wasm::WasmImport *> ImportedGlobals;
692 std::vector<wasm::WasmImport *> ImportedFunctions;
693 std::vector<wasm::WasmImport *> ImportedTags;
694 std::vector<wasm::WasmImport *> ImportedTables;
695 ImportedGlobals.reserve(Imports.size());
696 ImportedFunctions.reserve(Imports.size());
697 ImportedTags.reserve(Imports.size());
698 ImportedTables.reserve(Imports.size());
699 for (auto &I : Imports) {
701 ImportedFunctions.emplace_back(&I);
702 else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL)
703 ImportedGlobals.emplace_back(&I);
704 else if (I.Kind == wasm::WASM_EXTERNAL_TAG)
705 ImportedTags.emplace_back(&I);
706 else if (I.Kind == wasm::WASM_EXTERNAL_TABLE)
707 ImportedTables.emplace_back(&I);
708 }
709
710 while (Count--) {
712 const wasm::WasmSignature *Signature = nullptr;
713 const wasm::WasmGlobalType *GlobalType = nullptr;
714 const wasm::WasmTableType *TableType = nullptr;
715
716 Info.Kind = readUint8(Ctx);
717 Info.Flags = readVaruint32(Ctx);
718 bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0;
719
720 switch (Info.Kind) {
722 Info.ElementIndex = readVaruint32(Ctx);
723 if (!isValidFunctionIndex(Info.ElementIndex) ||
724 IsDefined != isDefinedFunctionIndex(Info.ElementIndex))
725 return make_error<GenericBinaryError>("invalid function symbol index",
727 if (IsDefined) {
728 Info.Name = readString(Ctx);
729 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions;
730 wasm::WasmFunction &Function = Functions[FuncIndex];
731 Signature = &Signatures[Function.SigIndex];
732 if (Function.SymbolName.empty())
733 Function.SymbolName = Info.Name;
734 } else {
735 wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex];
736 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
737 Info.Name = readString(Ctx);
738 Info.ImportName = Import.Field;
739 } else {
740 Info.Name = Import.Field;
741 }
742 Signature = &Signatures[Import.SigIndex];
743 Info.ImportModule = Import.Module;
744 }
745 break;
746
748 Info.ElementIndex = readVaruint32(Ctx);
749 if (!isValidGlobalIndex(Info.ElementIndex) ||
750 IsDefined != isDefinedGlobalIndex(Info.ElementIndex))
751 return make_error<GenericBinaryError>("invalid global symbol index",
753 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
755 return make_error<GenericBinaryError>("undefined weak global symbol",
757 if (IsDefined) {
758 Info.Name = readString(Ctx);
759 unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals;
760 wasm::WasmGlobal &Global = Globals[GlobalIndex];
761 GlobalType = &Global.Type;
762 if (Global.SymbolName.empty())
763 Global.SymbolName = Info.Name;
764 } else {
765 wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex];
766 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
767 Info.Name = readString(Ctx);
768 Info.ImportName = Import.Field;
769 } else {
770 Info.Name = Import.Field;
771 }
772 GlobalType = &Import.Global;
773 Info.ImportModule = Import.Module;
774 }
775 break;
776
778 Info.ElementIndex = readVaruint32(Ctx);
779 if (!isValidTableNumber(Info.ElementIndex) ||
780 IsDefined != isDefinedTableNumber(Info.ElementIndex))
781 return make_error<GenericBinaryError>("invalid table symbol index",
783 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
785 return make_error<GenericBinaryError>("undefined weak table symbol",
787 if (IsDefined) {
788 Info.Name = readString(Ctx);
789 unsigned TableNumber = Info.ElementIndex - NumImportedTables;
790 wasm::WasmTable &Table = Tables[TableNumber];
791 TableType = &Table.Type;
792 if (Table.SymbolName.empty())
793 Table.SymbolName = Info.Name;
794 } else {
795 wasm::WasmImport &Import = *ImportedTables[Info.ElementIndex];
796 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
797 Info.Name = readString(Ctx);
798 Info.ImportName = Import.Field;
799 } else {
800 Info.Name = Import.Field;
801 }
802 TableType = &Import.Table;
803 Info.ImportModule = Import.Module;
804 }
805 break;
806
808 Info.Name = readString(Ctx);
809 if (IsDefined) {
810 auto Index = readVaruint32(Ctx);
811 auto Offset = readVaruint64(Ctx);
812 auto Size = readVaruint64(Ctx);
813 if (!(Info.Flags & wasm::WASM_SYMBOL_ABSOLUTE)) {
814 if (static_cast<size_t>(Index) >= DataSegments.size())
815 return make_error<GenericBinaryError>(
816 "invalid data segment index: " + Twine(Index),
818 size_t SegmentSize = DataSegments[Index].Data.Content.size();
819 if (Offset > SegmentSize)
820 return make_error<GenericBinaryError>(
821 "invalid data symbol offset: `" + Info.Name +
822 "` (offset: " + Twine(Offset) +
823 " segment size: " + Twine(SegmentSize) + ")",
825 }
827 }
828 break;
829
831 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
833 return make_error<GenericBinaryError>(
834 "section symbols must have local binding",
836 Info.ElementIndex = readVaruint32(Ctx);
837 // Use somewhat unique section name as symbol name.
838 StringRef SectionName = Sections[Info.ElementIndex].Name;
839 Info.Name = SectionName;
840 break;
841 }
842
844 Info.ElementIndex = readVaruint32(Ctx);
845 if (!isValidTagIndex(Info.ElementIndex) ||
846 IsDefined != isDefinedTagIndex(Info.ElementIndex))
847 return make_error<GenericBinaryError>("invalid tag symbol index",
849 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
851 return make_error<GenericBinaryError>("undefined weak global symbol",
853 if (IsDefined) {
854 Info.Name = readString(Ctx);
855 unsigned TagIndex = Info.ElementIndex - NumImportedTags;
856 wasm::WasmTag &Tag = Tags[TagIndex];
857 Signature = &Signatures[Tag.SigIndex];
858 if (Tag.SymbolName.empty())
859 Tag.SymbolName = Info.Name;
860
861 } else {
862 wasm::WasmImport &Import = *ImportedTags[Info.ElementIndex];
863 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
864 Info.Name = readString(Ctx);
865 Info.ImportName = Import.Field;
866 } else {
867 Info.Name = Import.Field;
868 }
869 Signature = &Signatures[Import.SigIndex];
870 Info.ImportModule = Import.Module;
871 }
872 break;
873 }
874
875 default:
876 return make_error<GenericBinaryError>("invalid symbol type: " +
877 Twine(unsigned(Info.Kind)),
879 }
880
881 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
883 !SymbolNames.insert(Info.Name).second)
884 return make_error<GenericBinaryError>("duplicate symbol name " +
885 Twine(Info.Name),
887 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
888 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
889 }
890
891 return Error::success();
892}
893
894Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) {
895 uint32_t ComdatCount = readVaruint32(Ctx);
896 StringSet<> ComdatSet;
897 for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) {
899 if (Name.empty() || !ComdatSet.insert(Name).second)
900 return make_error<GenericBinaryError>("bad/duplicate COMDAT name " +
901 Twine(Name),
903 LinkingData.Comdats.emplace_back(Name);
905 if (Flags != 0)
906 return make_error<GenericBinaryError>("unsupported COMDAT flags",
908
909 uint32_t EntryCount = readVaruint32(Ctx);
910 while (EntryCount--) {
911 unsigned Kind = readVaruint32(Ctx);
912 unsigned Index = readVaruint32(Ctx);
913 switch (Kind) {
914 default:
915 return make_error<GenericBinaryError>("invalid COMDAT entry type",
918 if (Index >= DataSegments.size())
919 return make_error<GenericBinaryError>(
920 "COMDAT data index out of range", object_error::parse_failed);
921 if (DataSegments[Index].Data.Comdat != UINT32_MAX)
922 return make_error<GenericBinaryError>("data segment in two COMDATs",
924 DataSegments[Index].Data.Comdat = ComdatIndex;
925 break;
927 if (!isDefinedFunctionIndex(Index))
928 return make_error<GenericBinaryError>(
929 "COMDAT function index out of range", object_error::parse_failed);
930 if (getDefinedFunction(Index).Comdat != UINT32_MAX)
931 return make_error<GenericBinaryError>("function in two COMDATs",
933 getDefinedFunction(Index).Comdat = ComdatIndex;
934 break;
936 if (Index >= Sections.size())
937 return make_error<GenericBinaryError>(
938 "COMDAT section index out of range", object_error::parse_failed);
939 if (Sections[Index].Type != wasm::WASM_SEC_CUSTOM)
940 return make_error<GenericBinaryError>(
941 "non-custom section in a COMDAT", object_error::parse_failed);
942 Sections[Index].Comdat = ComdatIndex;
943 break;
944 }
945 }
946 }
947 return Error::success();
948}
949
950Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) {
952 uint32_t Fields = readVaruint32(Ctx);
953 for (size_t I = 0; I < Fields; ++I) {
954 StringRef FieldName = readString(Ctx);
955 if (!FieldsSeen.insert(FieldName).second)
956 return make_error<GenericBinaryError>(
957 "producers section does not have unique fields",
959 std::vector<std::pair<std::string, std::string>> *ProducerVec = nullptr;
960 if (FieldName == "language") {
961 ProducerVec = &ProducerInfo.Languages;
962 } else if (FieldName == "processed-by") {
963 ProducerVec = &ProducerInfo.Tools;
964 } else if (FieldName == "sdk") {
965 ProducerVec = &ProducerInfo.SDKs;
966 } else {
967 return make_error<GenericBinaryError>(
968 "producers section field is not named one of language, processed-by, "
969 "or sdk",
971 }
972 uint32_t ValueCount = readVaruint32(Ctx);
973 llvm::SmallSet<StringRef, 8> ProducersSeen;
974 for (size_t J = 0; J < ValueCount; ++J) {
977 if (!ProducersSeen.insert(Name).second) {
978 return make_error<GenericBinaryError>(
979 "producers section contains repeated producer",
981 }
982 ProducerVec->emplace_back(std::string(Name), std::string(Version));
983 }
984 }
985 if (Ctx.Ptr != Ctx.End)
986 return make_error<GenericBinaryError>("producers section ended prematurely",
988 return Error::success();
989}
990
991Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) {
994 for (size_t I = 0; I < FeatureCount; ++I) {
996 Feature.Prefix = readUint8(Ctx);
997 switch (Feature.Prefix) {
1000 break;
1001 default:
1002 return make_error<GenericBinaryError>("unknown feature policy prefix",
1004 }
1005 Feature.Name = std::string(readString(Ctx));
1006 if (!FeaturesSeen.insert(Feature.Name).second)
1007 return make_error<GenericBinaryError>(
1008 "target features section contains repeated feature \"" +
1009 Feature.Name + "\"",
1011 TargetFeatures.push_back(Feature);
1012 }
1013 if (Ctx.Ptr != Ctx.End)
1014 return make_error<GenericBinaryError>(
1015 "target features section ended prematurely",
1017 return Error::success();
1018}
1019
1020Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
1021 uint32_t SectionIndex = readVaruint32(Ctx);
1022 if (SectionIndex >= Sections.size())
1023 return make_error<GenericBinaryError>("invalid section index",
1025 WasmSection &Section = Sections[SectionIndex];
1026 uint32_t RelocCount = readVaruint32(Ctx);
1027 uint32_t EndOffset = Section.Content.size();
1028 uint32_t PreviousOffset = 0;
1029 while (RelocCount--) {
1030 wasm::WasmRelocation Reloc = {};
1031 uint32_t type = readVaruint32(Ctx);
1032 Reloc.Type = type;
1033 Reloc.Offset = readVaruint32(Ctx);
1034 if (Reloc.Offset < PreviousOffset)
1035 return make_error<GenericBinaryError>("relocations not in offset order",
1037
1038 auto badReloc = [&](StringRef msg) {
1039 return make_error<GenericBinaryError>(
1040 msg + ": " + Twine(Symbols[Reloc.Index].Info.Name),
1042 };
1043
1044 PreviousOffset = Reloc.Offset;
1045 Reloc.Index = readVaruint32(Ctx);
1046 switch (type) {
1047 case wasm::R_WASM_FUNCTION_INDEX_LEB:
1048 case wasm::R_WASM_FUNCTION_INDEX_I32:
1049 case wasm::R_WASM_TABLE_INDEX_SLEB:
1050 case wasm::R_WASM_TABLE_INDEX_SLEB64:
1051 case wasm::R_WASM_TABLE_INDEX_I32:
1052 case wasm::R_WASM_TABLE_INDEX_I64:
1053 case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
1054 case wasm::R_WASM_TABLE_INDEX_REL_SLEB64:
1055 if (!isValidFunctionSymbol(Reloc.Index))
1056 return badReloc("invalid function relocation");
1057 break;
1058 case wasm::R_WASM_TABLE_NUMBER_LEB:
1059 if (!isValidTableSymbol(Reloc.Index))
1060 return badReloc("invalid table relocation");
1061 break;
1062 case wasm::R_WASM_TYPE_INDEX_LEB:
1063 if (Reloc.Index >= Signatures.size())
1064 return badReloc("invalid relocation type index");
1065 break;
1066 case wasm::R_WASM_GLOBAL_INDEX_LEB:
1067 // R_WASM_GLOBAL_INDEX_LEB are can be used against function and data
1068 // symbols to refer to their GOT entries.
1069 if (!isValidGlobalSymbol(Reloc.Index) &&
1070 !isValidDataSymbol(Reloc.Index) &&
1071 !isValidFunctionSymbol(Reloc.Index))
1072 return badReloc("invalid global relocation");
1073 break;
1074 case wasm::R_WASM_GLOBAL_INDEX_I32:
1075 if (!isValidGlobalSymbol(Reloc.Index))
1076 return badReloc("invalid global relocation");
1077 break;
1078 case wasm::R_WASM_TAG_INDEX_LEB:
1079 if (!isValidTagSymbol(Reloc.Index))
1080 return badReloc("invalid tag relocation");
1081 break;
1082 case wasm::R_WASM_MEMORY_ADDR_LEB:
1083 case wasm::R_WASM_MEMORY_ADDR_SLEB:
1084 case wasm::R_WASM_MEMORY_ADDR_I32:
1085 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
1086 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB:
1087 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
1088 if (!isValidDataSymbol(Reloc.Index))
1089 return badReloc("invalid data relocation");
1090 Reloc.Addend = readVarint32(Ctx);
1091 break;
1092 case wasm::R_WASM_MEMORY_ADDR_LEB64:
1093 case wasm::R_WASM_MEMORY_ADDR_SLEB64:
1094 case wasm::R_WASM_MEMORY_ADDR_I64:
1095 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
1096 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64:
1097 if (!isValidDataSymbol(Reloc.Index))
1098 return badReloc("invalid data relocation");
1099 Reloc.Addend = readVarint64(Ctx);
1100 break;
1101 case wasm::R_WASM_FUNCTION_OFFSET_I32:
1102 if (!isValidFunctionSymbol(Reloc.Index))
1103 return badReloc("invalid function relocation");
1104 Reloc.Addend = readVarint32(Ctx);
1105 break;
1106 case wasm::R_WASM_FUNCTION_OFFSET_I64:
1107 if (!isValidFunctionSymbol(Reloc.Index))
1108 return badReloc("invalid function relocation");
1109 Reloc.Addend = readVarint64(Ctx);
1110 break;
1111 case wasm::R_WASM_SECTION_OFFSET_I32:
1112 if (!isValidSectionSymbol(Reloc.Index))
1113 return badReloc("invalid section relocation");
1114 Reloc.Addend = readVarint32(Ctx);
1115 break;
1116 default:
1117 return make_error<GenericBinaryError>("invalid relocation type: " +
1118 Twine(type),
1120 }
1121
1122 // Relocations must fit inside the section, and must appear in order. They
1123 // also shouldn't overlap a function/element boundary, but we don't bother
1124 // to check that.
1125 uint64_t Size = 5;
1126 if (Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LEB64 ||
1127 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_SLEB64 ||
1128 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_REL_SLEB64)
1129 Size = 10;
1130 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I32 ||
1131 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I32 ||
1132 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I32 ||
1133 Reloc.Type == wasm::R_WASM_SECTION_OFFSET_I32 ||
1134 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
1135 Reloc.Type == wasm::R_WASM_FUNCTION_INDEX_I32 ||
1136 Reloc.Type == wasm::R_WASM_GLOBAL_INDEX_I32)
1137 Size = 4;
1138 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I64 ||
1139 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I64 ||
1140 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I64)
1141 Size = 8;
1142 if (Reloc.Offset + Size > EndOffset)
1143 return make_error<GenericBinaryError>("invalid relocation offset",
1145
1146 Section.Relocations.push_back(Reloc);
1147 }
1148 if (Ctx.Ptr != Ctx.End)
1149 return make_error<GenericBinaryError>("reloc section ended prematurely",
1151 return Error::success();
1152}
1153
1154Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) {
1155 if (Sec.Name == "dylink") {
1156 if (Error Err = parseDylinkSection(Ctx))
1157 return Err;
1158 } else if (Sec.Name == "dylink.0") {
1159 if (Error Err = parseDylink0Section(Ctx))
1160 return Err;
1161 } else if (Sec.Name == "name") {
1162 if (Error Err = parseNameSection(Ctx))
1163 return Err;
1164 } else if (Sec.Name == "linking") {
1165 if (Error Err = parseLinkingSection(Ctx))
1166 return Err;
1167 } else if (Sec.Name == "producers") {
1168 if (Error Err = parseProducersSection(Ctx))
1169 return Err;
1170 } else if (Sec.Name == "target_features") {
1171 if (Error Err = parseTargetFeaturesSection(Ctx))
1172 return Err;
1173 } else if (Sec.Name.starts_with("reloc.")) {
1174 if (Error Err = parseRelocSection(Sec.Name, Ctx))
1175 return Err;
1176 }
1177 return Error::success();
1178}
1179
1180Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
1181 auto parseFieldDef = [&]() {
1182 uint32_t TypeCode = readVaruint32((Ctx));
1183 /* Discard StorageType */ parseValType(Ctx, TypeCode);
1184 /* Discard Mutability */ readVaruint32(Ctx);
1185 };
1186
1187 uint32_t Count = readVaruint32(Ctx);
1188 Signatures.reserve(Count);
1189 while (Count--) {
1191 uint8_t Form = readUint8(Ctx);
1192 if (Form == wasm::WASM_TYPE_REC) {
1193 // Rec groups expand the type index space (beyond what was declared at
1194 // the top of the section, and also consume one element in that space.
1195 uint32_t RecSize = readVaruint32(Ctx);
1196 if (RecSize == 0)
1197 return make_error<GenericBinaryError>("Rec group size cannot be 0",
1199 Signatures.reserve(Signatures.size() + RecSize);
1200 Count += RecSize;
1202 Signatures.push_back(std::move(Sig));
1203 HasUnmodeledTypes = true;
1204 continue;
1205 }
1206 if (Form != wasm::WASM_TYPE_FUNC) {
1207 // Currently LLVM only models function types, and not other composite
1208 // types. Here we parse the type declarations just enough to skip past
1209 // them in the binary.
1210 if (Form == wasm::WASM_TYPE_SUB || Form == wasm::WASM_TYPE_SUB_FINAL) {
1211 uint32_t Supers = readVaruint32(Ctx);
1212 if (Supers > 0) {
1213 if (Supers != 1)
1214 return make_error<GenericBinaryError>(
1215 "Invalid number of supertypes", object_error::parse_failed);
1216 /* Discard SuperIndex */ readVaruint32(Ctx);
1217 }
1218 Form = readVaruint32(Ctx);
1219 }
1220 if (Form == wasm::WASM_TYPE_STRUCT) {
1221 uint32_t FieldCount = readVaruint32(Ctx);
1222 while (FieldCount--) {
1223 parseFieldDef();
1224 }
1225 } else if (Form == wasm::WASM_TYPE_ARRAY) {
1226 parseFieldDef();
1227 } else {
1228 return make_error<GenericBinaryError>("bad form",
1230 }
1232 Signatures.push_back(std::move(Sig));
1233 HasUnmodeledTypes = true;
1234 continue;
1235 }
1236
1237 uint32_t ParamCount = readVaruint32(Ctx);
1238 Sig.Params.reserve(ParamCount);
1239 while (ParamCount--) {
1240 uint32_t ParamType = readUint8(Ctx);
1241 Sig.Params.push_back(parseValType(Ctx, ParamType));
1242 }
1243 uint32_t ReturnCount = readVaruint32(Ctx);
1244 while (ReturnCount--) {
1246 Sig.Returns.push_back(parseValType(Ctx, ReturnType));
1247 }
1248
1249 Signatures.push_back(std::move(Sig));
1250 }
1251 if (Ctx.Ptr != Ctx.End)
1252 return make_error<GenericBinaryError>("type section ended prematurely",
1254 return Error::success();
1255}
1256
1257Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
1258 uint32_t Count = readVaruint32(Ctx);
1259 uint32_t NumTypes = Signatures.size();
1260 Imports.reserve(Count);
1261 for (uint32_t I = 0; I < Count; I++) {
1263 Im.Module = readString(Ctx);
1264 Im.Field = readString(Ctx);
1265 Im.Kind = readUint8(Ctx);
1266 switch (Im.Kind) {
1268 NumImportedFunctions++;
1269 Im.SigIndex = readVaruint32(Ctx);
1270 if (Im.SigIndex >= NumTypes)
1271 return make_error<GenericBinaryError>("invalid function type",
1273 break;
1275 NumImportedGlobals++;
1276 Im.Global.Type = readUint8(Ctx);
1277 Im.Global.Mutable = readVaruint1(Ctx);
1278 break;
1280 Im.Memory = readLimits(Ctx);
1282 HasMemory64 = true;
1283 break;
1285 Im.Table = readTableType(Ctx);
1286 NumImportedTables++;
1287 auto ElemType = Im.Table.ElemType;
1288 if (ElemType != wasm::ValType::FUNCREF &&
1289 ElemType != wasm::ValType::EXTERNREF &&
1290 ElemType != wasm::ValType::EXNREF &&
1291 ElemType != wasm::ValType::OTHERREF)
1292 return make_error<GenericBinaryError>("invalid table element type",
1294 break;
1295 }
1297 NumImportedTags++;
1298 if (readUint8(Ctx) != 0) // Reserved 'attribute' field
1299 return make_error<GenericBinaryError>("invalid attribute",
1301 Im.SigIndex = readVaruint32(Ctx);
1302 if (Im.SigIndex >= NumTypes)
1303 return make_error<GenericBinaryError>("invalid tag type",
1305 break;
1306 default:
1307 return make_error<GenericBinaryError>("unexpected import kind",
1309 }
1310 Imports.push_back(Im);
1311 }
1312 if (Ctx.Ptr != Ctx.End)
1313 return make_error<GenericBinaryError>("import section ended prematurely",
1315 return Error::success();
1316}
1317
1318Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
1319 uint32_t Count = readVaruint32(Ctx);
1320 Functions.reserve(Count);
1321 uint32_t NumTypes = Signatures.size();
1322 while (Count--) {
1324 if (Type >= NumTypes)
1325 return make_error<GenericBinaryError>("invalid function type",
1328 F.SigIndex = Type;
1329 Functions.push_back(F);
1330 }
1331 if (Ctx.Ptr != Ctx.End)
1332 return make_error<GenericBinaryError>("function section ended prematurely",
1334 return Error::success();
1335}
1336
1337Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
1338 TableSection = Sections.size();
1339 uint32_t Count = readVaruint32(Ctx);
1340 Tables.reserve(Count);
1341 while (Count--) {
1343 T.Type = readTableType(Ctx);
1344 T.Index = NumImportedTables + Tables.size();
1345 Tables.push_back(T);
1346 auto ElemType = Tables.back().Type.ElemType;
1347 if (ElemType != wasm::ValType::FUNCREF &&
1348 ElemType != wasm::ValType::EXTERNREF &&
1349 ElemType != wasm::ValType::EXNREF &&
1350 ElemType != wasm::ValType::OTHERREF) {
1351 return make_error<GenericBinaryError>("invalid table element type",
1353 }
1354 }
1355 if (Ctx.Ptr != Ctx.End)
1356 return make_error<GenericBinaryError>("table section ended prematurely",
1358 return Error::success();
1359}
1360
1361Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
1362 uint32_t Count = readVaruint32(Ctx);
1363 Memories.reserve(Count);
1364 while (Count--) {
1365 auto Limits = readLimits(Ctx);
1366 if (Limits.Flags & wasm::WASM_LIMITS_FLAG_IS_64)
1367 HasMemory64 = true;
1368 Memories.push_back(Limits);
1369 }
1370 if (Ctx.Ptr != Ctx.End)
1371 return make_error<GenericBinaryError>("memory section ended prematurely",
1373 return Error::success();
1374}
1375
1376Error WasmObjectFile::parseTagSection(ReadContext &Ctx) {
1377 TagSection = Sections.size();
1378 uint32_t Count = readVaruint32(Ctx);
1379 Tags.reserve(Count);
1380 uint32_t NumTypes = Signatures.size();
1381 while (Count--) {
1382 if (readUint8(Ctx) != 0) // Reserved 'attribute' field
1383 return make_error<GenericBinaryError>("invalid attribute",
1386 if (Type >= NumTypes)
1387 return make_error<GenericBinaryError>("invalid tag type",
1390 Tag.Index = NumImportedTags + Tags.size();
1391 Tag.SigIndex = Type;
1392 Signatures[Type].Kind = wasm::WasmSignature::Tag;
1393 Tags.push_back(Tag);
1394 }
1395
1396 if (Ctx.Ptr != Ctx.End)
1397 return make_error<GenericBinaryError>("tag section ended prematurely",
1399 return Error::success();
1400}
1401
1402Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
1403 GlobalSection = Sections.size();
1404 const uint8_t *SectionStart = Ctx.Ptr;
1405 uint32_t Count = readVaruint32(Ctx);
1406 Globals.reserve(Count);
1407 while (Count--) {
1409 Global.Index = NumImportedGlobals + Globals.size();
1410 const uint8_t *GlobalStart = Ctx.Ptr;
1411 Global.Offset = static_cast<uint32_t>(GlobalStart - SectionStart);
1412 auto GlobalOpcode = readVaruint32(Ctx);
1413 Global.Type.Type = (uint8_t)parseValType(Ctx, GlobalOpcode);
1414 Global.Type.Mutable = readVaruint1(Ctx);
1415 if (Error Err = readInitExpr(Global.InitExpr, Ctx))
1416 return Err;
1417 Global.Size = static_cast<uint32_t>(Ctx.Ptr - GlobalStart);
1418 Globals.push_back(Global);
1419 }
1420 if (Ctx.Ptr != Ctx.End)
1421 return make_error<GenericBinaryError>("global section ended prematurely",
1423 return Error::success();
1424}
1425
1426Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
1427 uint32_t Count = readVaruint32(Ctx);
1428 Exports.reserve(Count);
1429 Symbols.reserve(Count);
1430 for (uint32_t I = 0; I < Count; I++) {
1432 Ex.Name = readString(Ctx);
1433 Ex.Kind = readUint8(Ctx);
1434 Ex.Index = readVaruint32(Ctx);
1435 const wasm::WasmSignature *Signature = nullptr;
1436 const wasm::WasmGlobalType *GlobalType = nullptr;
1437 const wasm::WasmTableType *TableType = nullptr;
1439 Info.Name = Ex.Name;
1440 Info.Flags = 0;
1441 switch (Ex.Kind) {
1443 if (!isDefinedFunctionIndex(Ex.Index))
1444 return make_error<GenericBinaryError>("invalid function export",
1446 getDefinedFunction(Ex.Index).ExportName = Ex.Name;
1448 Info.ElementIndex = Ex.Index;
1449 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions;
1450 wasm::WasmFunction &Function = Functions[FuncIndex];
1451 Signature = &Signatures[Function.SigIndex];
1452 break;
1453 }
1455 if (!isValidGlobalIndex(Ex.Index))
1456 return make_error<GenericBinaryError>("invalid global export",
1459 uint64_t Offset = 0;
1460 if (isDefinedGlobalIndex(Ex.Index)) {
1461 auto Global = getDefinedGlobal(Ex.Index);
1462 if (!Global.InitExpr.Extended) {
1463 auto Inst = Global.InitExpr.Inst;
1464 if (Inst.Opcode == wasm::WASM_OPCODE_I32_CONST) {
1465 Offset = Inst.Value.Int32;
1466 } else if (Inst.Opcode == wasm::WASM_OPCODE_I64_CONST) {
1467 Offset = Inst.Value.Int64;
1468 }
1469 }
1470 }
1471 Info.DataRef = wasm::WasmDataReference{0, Offset, 0};
1472 break;
1473 }
1475 if (!isValidTagIndex(Ex.Index))
1476 return make_error<GenericBinaryError>("invalid tag export",
1479 Info.ElementIndex = Ex.Index;
1480 break;
1482 break;
1485 Info.ElementIndex = Ex.Index;
1486 break;
1487 default:
1488 return make_error<GenericBinaryError>("unexpected export kind",
1490 }
1491 Exports.push_back(Ex);
1492 if (Ex.Kind != wasm::WASM_EXTERNAL_MEMORY) {
1493 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
1494 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
1495 }
1496 }
1497 if (Ctx.Ptr != Ctx.End)
1498 return make_error<GenericBinaryError>("export section ended prematurely",
1500 return Error::success();
1501}
1502
1503bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const {
1504 return Index < NumImportedFunctions + Functions.size();
1505}
1506
1507bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const {
1508 return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
1509}
1510
1511bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const {
1512 return Index < NumImportedGlobals + Globals.size();
1513}
1514
1515bool WasmObjectFile::isValidTableNumber(uint32_t Index) const {
1516 return Index < NumImportedTables + Tables.size();
1517}
1518
1519bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const {
1520 return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
1521}
1522
1523bool WasmObjectFile::isDefinedTableNumber(uint32_t Index) const {
1524 return Index >= NumImportedTables && isValidTableNumber(Index);
1525}
1526
1527bool WasmObjectFile::isValidTagIndex(uint32_t Index) const {
1528 return Index < NumImportedTags + Tags.size();
1529}
1530
1531bool WasmObjectFile::isDefinedTagIndex(uint32_t Index) const {
1532 return Index >= NumImportedTags && isValidTagIndex(Index);
1533}
1534
1535bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const {
1536 return Index < Symbols.size() && Symbols[Index].isTypeFunction();
1537}
1538
1539bool WasmObjectFile::isValidTableSymbol(uint32_t Index) const {
1540 return Index < Symbols.size() && Symbols[Index].isTypeTable();
1541}
1542
1543bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const {
1544 return Index < Symbols.size() && Symbols[Index].isTypeGlobal();
1545}
1546
1547bool WasmObjectFile::isValidTagSymbol(uint32_t Index) const {
1548 return Index < Symbols.size() && Symbols[Index].isTypeTag();
1549}
1550
1551bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const {
1552 return Index < Symbols.size() && Symbols[Index].isTypeData();
1553}
1554
1555bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const {
1556 return Index < Symbols.size() && Symbols[Index].isTypeSection();
1557}
1558
1559wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
1560 assert(isDefinedFunctionIndex(Index));
1561 return Functions[Index - NumImportedFunctions];
1562}
1563
1564const wasm::WasmFunction &
1565WasmObjectFile::getDefinedFunction(uint32_t Index) const {
1566 assert(isDefinedFunctionIndex(Index));
1567 return Functions[Index - NumImportedFunctions];
1568}
1569
1570const wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) const {
1571 assert(isDefinedGlobalIndex(Index));
1572 return Globals[Index - NumImportedGlobals];
1573}
1574
1575wasm::WasmTag &WasmObjectFile::getDefinedTag(uint32_t Index) {
1576 assert(isDefinedTagIndex(Index));
1577 return Tags[Index - NumImportedTags];
1578}
1579
1580Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
1581 StartFunction = readVaruint32(Ctx);
1582 if (!isValidFunctionIndex(StartFunction))
1583 return make_error<GenericBinaryError>("invalid start function",
1585 return Error::success();
1586}
1587
1588Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) {
1589 CodeSection = Sections.size();
1590 uint32_t FunctionCount = readVaruint32(Ctx);
1591 if (FunctionCount != Functions.size()) {
1592 return make_error<GenericBinaryError>("invalid function count",
1594 }
1595
1596 for (uint32_t i = 0; i < FunctionCount; i++) {
1597 wasm::WasmFunction& Function = Functions[i];
1598 const uint8_t *FunctionStart = Ctx.Ptr;
1600 const uint8_t *FunctionEnd = Ctx.Ptr + Size;
1601
1602 Function.CodeOffset = Ctx.Ptr - FunctionStart;
1603 Function.Index = NumImportedFunctions + i;
1604 Function.CodeSectionOffset = FunctionStart - Ctx.Start;
1605 Function.Size = FunctionEnd - FunctionStart;
1606
1607 uint32_t NumLocalDecls = readVaruint32(Ctx);
1608 Function.Locals.reserve(NumLocalDecls);
1609 while (NumLocalDecls--) {
1611 Decl.Count = readVaruint32(Ctx);
1612 Decl.Type = readUint8(Ctx);
1613 Function.Locals.push_back(Decl);
1614 }
1615
1616 uint32_t BodySize = FunctionEnd - Ctx.Ptr;
1617 // Ensure that Function is within Ctx's buffer.
1618 if (Ctx.Ptr + BodySize > Ctx.End) {
1619 return make_error<GenericBinaryError>("Function extends beyond buffer",
1621 }
1622 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize);
1623 // This will be set later when reading in the linking metadata section.
1624 Function.Comdat = UINT32_MAX;
1625 Ctx.Ptr += BodySize;
1626 assert(Ctx.Ptr == FunctionEnd);
1627 }
1628 if (Ctx.Ptr != Ctx.End)
1629 return make_error<GenericBinaryError>("code section ended prematurely",
1631 return Error::success();
1632}
1633
1634Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
1635 uint32_t Count = readVaruint32(Ctx);
1636 ElemSegments.reserve(Count);
1637 while (Count--) {
1638 wasm::WasmElemSegment Segment;
1639 Segment.Flags = readVaruint32(Ctx);
1640
1644 if (Segment.Flags & ~SupportedFlags)
1645 return make_error<GenericBinaryError>(
1646 "Unsupported flags for element segment", object_error::parse_failed);
1647
1648 bool IsPassive = (Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_PASSIVE) != 0;
1649 bool IsDeclarative =
1650 IsPassive && (Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_DECLARATIVE);
1651 bool HasTableNumber =
1652 !IsPassive &&
1654 bool HasInitExprs =
1656 bool HasElemKind =
1658 !HasInitExprs;
1659
1660 if (HasTableNumber)
1661 Segment.TableNumber = readVaruint32(Ctx);
1662 else
1663 Segment.TableNumber = 0;
1664
1665 if (!isValidTableNumber(Segment.TableNumber))
1666 return make_error<GenericBinaryError>("invalid TableNumber",
1668
1669 if (IsPassive || IsDeclarative) {
1670 Segment.Offset.Extended = false;
1672 Segment.Offset.Inst.Value.Int32 = 0;
1673 } else {
1674 if (Error Err = readInitExpr(Segment.Offset, Ctx))
1675 return Err;
1676 }
1677
1678 if (HasElemKind) {
1679 auto ElemKind = readVaruint32(Ctx);
1681 Segment.ElemKind = parseValType(Ctx, ElemKind);
1682 if (Segment.ElemKind != wasm::ValType::FUNCREF &&
1684 Segment.ElemKind != wasm::ValType::EXNREF &&
1685 Segment.ElemKind != wasm::ValType::OTHERREF) {
1686 return make_error<GenericBinaryError>("invalid elem type",
1688 }
1689 } else {
1690 if (ElemKind != 0)
1691 return make_error<GenericBinaryError>("invalid elem type",
1694 }
1695 } else if (HasInitExprs) {
1696 auto ElemType = parseValType(Ctx, readVaruint32(Ctx));
1697 Segment.ElemKind = ElemType;
1698 } else {
1700 }
1701
1702 uint32_t NumElems = readVaruint32(Ctx);
1703
1704 if (HasInitExprs) {
1705 while (NumElems--) {
1706 wasm::WasmInitExpr Expr;
1707 if (Error Err = readInitExpr(Expr, Ctx))
1708 return Err;
1709 }
1710 } else {
1711 while (NumElems--) {
1712 Segment.Functions.push_back(readVaruint32(Ctx));
1713 }
1714 }
1715 ElemSegments.push_back(Segment);
1716 }
1717 if (Ctx.Ptr != Ctx.End)
1718 return make_error<GenericBinaryError>("elem section ended prematurely",
1720 return Error::success();
1721}
1722
1723Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
1724 DataSection = Sections.size();
1725 uint32_t Count = readVaruint32(Ctx);
1726 if (DataCount && Count != *DataCount)
1727 return make_error<GenericBinaryError>(
1728 "number of data segments does not match DataCount section");
1729 DataSegments.reserve(Count);
1730 while (Count--) {
1731 WasmSegment Segment;
1732 Segment.Data.InitFlags = readVaruint32(Ctx);
1733 Segment.Data.MemoryIndex =
1735 ? readVaruint32(Ctx)
1736 : 0;
1737 if ((Segment.Data.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
1738 if (Error Err = readInitExpr(Segment.Data.Offset, Ctx))
1739 return Err;
1740 } else {
1741 Segment.Data.Offset.Extended = false;
1743 Segment.Data.Offset.Inst.Value.Int32 = 0;
1744 }
1746 if (Size > (size_t)(Ctx.End - Ctx.Ptr))
1747 return make_error<GenericBinaryError>("invalid segment size",
1749 Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
1750 // The rest of these Data fields are set later, when reading in the linking
1751 // metadata section.
1752 Segment.Data.Alignment = 0;
1753 Segment.Data.LinkingFlags = 0;
1754 Segment.Data.Comdat = UINT32_MAX;
1755 Segment.SectionOffset = Ctx.Ptr - Ctx.Start;
1756 Ctx.Ptr += Size;
1757 DataSegments.push_back(Segment);
1758 }
1759 if (Ctx.Ptr != Ctx.End)
1760 return make_error<GenericBinaryError>("data section ended prematurely",
1762 return Error::success();
1763}
1764
1765Error WasmObjectFile::parseDataCountSection(ReadContext &Ctx) {
1766 DataCount = readVaruint32(Ctx);
1767 return Error::success();
1768}
1769
1771 return Header;
1772}
1773
1774void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.b++; }
1775
1778 const WasmSymbol &Sym = getWasmSymbol(Symb);
1779
1780 LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n");
1781 if (Sym.isBindingWeak())
1782 Result |= SymbolRef::SF_Weak;
1783 if (!Sym.isBindingLocal())
1784 Result |= SymbolRef::SF_Global;
1785 if (Sym.isHidden())
1786 Result |= SymbolRef::SF_Hidden;
1787 if (!Sym.isDefined())
1788 Result |= SymbolRef::SF_Undefined;
1789 if (Sym.isTypeFunction())
1790 Result |= SymbolRef::SF_Executable;
1791 return Result;
1792}
1793
1796 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null
1797 Ref.d.b = 0; // Symbol index
1798 return BasicSymbolRef(Ref, this);
1799}
1800
1803 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null
1804 Ref.d.b = Symbols.size(); // Symbol index
1805 return BasicSymbolRef(Ref, this);
1806}
1807
1809 return Symbols[Symb.d.b];
1810}
1811
1813 return getWasmSymbol(Symb.getRawDataRefImpl());
1814}
1815
1817 return getWasmSymbol(Symb).Info.Name;
1818}
1819
1821 auto &Sym = getWasmSymbol(Symb);
1822 if (!Sym.isDefined())
1823 return 0;
1825 if (!Sec)
1826 return Sec.takeError();
1827 uint32_t SectionAddress = getSectionAddress(Sec.get()->getRawDataRefImpl());
1828 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION &&
1829 isDefinedFunctionIndex(Sym.Info.ElementIndex)) {
1830 return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset +
1831 SectionAddress;
1832 }
1833 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL &&
1834 isDefinedGlobalIndex(Sym.Info.ElementIndex)) {
1835 return getDefinedGlobal(Sym.Info.ElementIndex).Offset + SectionAddress;
1836 }
1837
1838 return getSymbolValue(Symb);
1839}
1840
1842 switch (Sym.Info.Kind) {
1847 return Sym.Info.ElementIndex;
1849 // The value of a data symbol is the segment offset, plus the symbol
1850 // offset within the segment.
1851 uint32_t SegmentIndex = Sym.Info.DataRef.Segment;
1852 const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data;
1853 if (Segment.Offset.Extended) {
1854 llvm_unreachable("extended init exprs not supported");
1855 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_I32_CONST) {
1856 return Segment.Offset.Inst.Value.Int32 + Sym.Info.DataRef.Offset;
1857 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_I64_CONST) {
1858 return Segment.Offset.Inst.Value.Int64 + Sym.Info.DataRef.Offset;
1859 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_GLOBAL_GET) {
1860 return Sym.Info.DataRef.Offset;
1861 } else {
1862 llvm_unreachable("unknown init expr opcode");
1863 }
1864 }
1866 return 0;
1867 }
1868 llvm_unreachable("invalid symbol type");
1869}
1870
1872 return getWasmSymbolValue(getWasmSymbol(Symb));
1873}
1874
1876 llvm_unreachable("not yet implemented");
1877 return 0;
1878}
1879
1881 llvm_unreachable("not yet implemented");
1882 return 0;
1883}
1884
1887 const WasmSymbol &Sym = getWasmSymbol(Symb);
1888
1889 switch (Sym.Info.Kind) {
1893 return SymbolRef::ST_Other;
1895 return SymbolRef::ST_Data;
1897 return SymbolRef::ST_Debug;
1899 return SymbolRef::ST_Other;
1901 return SymbolRef::ST_Other;
1902 }
1903
1904 llvm_unreachable("unknown WasmSymbol::SymbolType");
1905 return SymbolRef::ST_Other;
1906}
1907
1910 const WasmSymbol &Sym = getWasmSymbol(Symb);
1911 if (Sym.isUndefined())
1912 return section_end();
1913
1915 Ref.d.a = getSymbolSectionIdImpl(Sym);
1916 return section_iterator(SectionRef(Ref, this));
1917}
1918
1920 const WasmSymbol &Sym = getWasmSymbol(Symb);
1921 return getSymbolSectionIdImpl(Sym);
1922}
1923
1924uint32_t WasmObjectFile::getSymbolSectionIdImpl(const WasmSymbol &Sym) const {
1925 switch (Sym.Info.Kind) {
1927 return CodeSection;
1929 return GlobalSection;
1931 return DataSection;
1933 return Sym.Info.ElementIndex;
1935 return TagSection;
1937 return TableSection;
1938 default:
1939 llvm_unreachable("unknown WasmSymbol::SymbolType");
1940 }
1941}
1942
1944 const WasmSymbol &Sym = getWasmSymbol(Symb);
1945 if (!Sym.isDefined())
1946 return 0;
1947 if (Sym.isTypeGlobal())
1948 return getDefinedGlobal(Sym.Info.ElementIndex).Size;
1949 if (Sym.isTypeData())
1950 return Sym.Info.DataRef.Size;
1951 if (Sym.isTypeFunction())
1952 return functions()[Sym.Info.ElementIndex - getNumImportedFunctions()].Size;
1953 // Currently symbol size is only tracked for data segments and functions. In
1954 // principle we could also track size (e.g. binary size) for tables, globals
1955 // and element segments etc too.
1956 return 0;
1957}
1958
1960
1962 const WasmSection &S = Sections[Sec.d.a];
1963 if (S.Type == wasm::WASM_SEC_CUSTOM)
1964 return S.Name;
1968}
1969
1971 // For object files, use 0 for section addresses, and section offsets for
1972 // symbol addresses. For linked files, use file offsets.
1973 // See also getSymbolAddress.
1974 return isRelocatableObject() || isSharedObject() ? 0
1975 : Sections[Sec.d.a].Offset;
1976}
1977
1979 return Sec.d.a;
1980}
1981
1983 const WasmSection &S = Sections[Sec.d.a];
1984 return S.Content.size();
1985}
1986
1989 const WasmSection &S = Sections[Sec.d.a];
1990 // This will never fail since wasm sections can never be empty (user-sections
1991 // must have a name and non-user sections each have a defined structure).
1992 return S.Content;
1993}
1994
1996 return 1;
1997}
1998
2000 return false;
2001}
2002
2005}
2006
2009}
2010
2011bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
2012
2013bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
2014
2016 DataRefImpl RelocRef;
2017 RelocRef.d.a = Ref.d.a;
2018 RelocRef.d.b = 0;
2019 return relocation_iterator(RelocationRef(RelocRef, this));
2020}
2021
2023 const WasmSection &Sec = getWasmSection(Ref);
2024 DataRefImpl RelocRef;
2025 RelocRef.d.a = Ref.d.a;
2026 RelocRef.d.b = Sec.Relocations.size();
2027 return relocation_iterator(RelocationRef(RelocRef, this));
2028}
2029
2031
2034 return Rel.Offset;
2035}
2036
2039 if (Rel.Type == wasm::R_WASM_TYPE_INDEX_LEB)
2040 return symbol_end();
2042 Sym.d.a = 1;
2043 Sym.d.b = Rel.Index;
2044 return symbol_iterator(SymbolRef(Sym, this));
2045}
2046
2049 return Rel.Type;
2050}
2051
2053 DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
2055 StringRef Res = "Unknown";
2056
2057#define WASM_RELOC(name, value) \
2058 case wasm::name: \
2059 Res = #name; \
2060 break;
2061
2062 switch (Rel.Type) {
2063#include "llvm/BinaryFormat/WasmRelocs.def"
2064 }
2065
2066#undef WASM_RELOC
2067
2068 Result.append(Res.begin(), Res.end());
2069}
2070
2073 Ref.d.a = 0;
2074 return section_iterator(SectionRef(Ref, this));
2075}
2076
2079 Ref.d.a = Sections.size();
2080 return section_iterator(SectionRef(Ref, this));
2081}
2082
2084 return HasMemory64 ? 8 : 4;
2085}
2086
2088
2090 return HasMemory64 ? Triple::wasm64 : Triple::wasm32;
2091}
2092
2094 return SubtargetFeatures();
2095}
2096
2097bool WasmObjectFile::isRelocatableObject() const { return HasLinkingSection; }
2098
2099bool WasmObjectFile::isSharedObject() const { return HasDylinkSection; }
2100
2102 assert(Ref.d.a < Sections.size());
2103 return Sections[Ref.d.a];
2104}
2105
2106const WasmSection &
2108 return getWasmSection(Section.getRawDataRefImpl());
2109}
2110
2113 return getWasmRelocation(Ref.getRawDataRefImpl());
2114}
2115
2118 assert(Ref.d.a < Sections.size());
2119 const WasmSection &Sec = Sections[Ref.d.a];
2120 assert(Ref.d.b < Sec.Relocations.size());
2121 return Sec.Relocations[Ref.d.b];
2122}
2123
2124int WasmSectionOrderChecker::getSectionOrder(unsigned ID,
2125 StringRef CustomSectionName) {
2126 switch (ID) {
2128 return StringSwitch<unsigned>(CustomSectionName)
2129 .Case("dylink", WASM_SEC_ORDER_DYLINK)
2130 .Case("dylink.0", WASM_SEC_ORDER_DYLINK)
2131 .Case("linking", WASM_SEC_ORDER_LINKING)
2133 .Case("name", WASM_SEC_ORDER_NAME)
2134 .Case("producers", WASM_SEC_ORDER_PRODUCERS)
2135 .Case("target_features", WASM_SEC_ORDER_TARGET_FEATURES)
2138 return WASM_SEC_ORDER_TYPE;
2140 return WASM_SEC_ORDER_IMPORT;
2144 return WASM_SEC_ORDER_TABLE;
2146 return WASM_SEC_ORDER_MEMORY;
2148 return WASM_SEC_ORDER_GLOBAL;
2150 return WASM_SEC_ORDER_EXPORT;
2152 return WASM_SEC_ORDER_START;
2154 return WASM_SEC_ORDER_ELEM;
2156 return WASM_SEC_ORDER_CODE;
2158 return WASM_SEC_ORDER_DATA;
2161 case wasm::WASM_SEC_TAG:
2162 return WASM_SEC_ORDER_TAG;
2163 default:
2164 return WASM_SEC_ORDER_NONE;
2165 }
2166}
2167
2168// Represents the edges in a directed graph where any node B reachable from node
2169// A is not allowed to appear before A in the section ordering, but may appear
2170// afterward.
2172 [WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = {
2173 // WASM_SEC_ORDER_NONE
2174 {},
2175 // WASM_SEC_ORDER_TYPE
2176 {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT},
2177 // WASM_SEC_ORDER_IMPORT
2178 {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION},
2179 // WASM_SEC_ORDER_FUNCTION
2180 {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE},
2181 // WASM_SEC_ORDER_TABLE
2182 {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY},
2183 // WASM_SEC_ORDER_MEMORY
2184 {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_TAG},
2185 // WASM_SEC_ORDER_TAG
2186 {WASM_SEC_ORDER_TAG, WASM_SEC_ORDER_GLOBAL},
2187 // WASM_SEC_ORDER_GLOBAL
2188 {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EXPORT},
2189 // WASM_SEC_ORDER_EXPORT
2190 {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START},
2191 // WASM_SEC_ORDER_START
2192 {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM},
2193 // WASM_SEC_ORDER_ELEM
2194 {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT},
2195 // WASM_SEC_ORDER_DATACOUNT
2196 {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE},
2197 // WASM_SEC_ORDER_CODE
2198 {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA},
2199 // WASM_SEC_ORDER_DATA
2200 {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING},
2201
2202 // Custom Sections
2203 // WASM_SEC_ORDER_DYLINK
2204 {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE},
2205 // WASM_SEC_ORDER_LINKING
2206 {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME},
2207 // WASM_SEC_ORDER_RELOC (can be repeated)
2208 {},
2209 // WASM_SEC_ORDER_NAME
2210 {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS},
2211 // WASM_SEC_ORDER_PRODUCERS
2212 {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES},
2213 // WASM_SEC_ORDER_TARGET_FEATURES
2214 {WASM_SEC_ORDER_TARGET_FEATURES}};
2215
2217 StringRef CustomSectionName) {
2218 int Order = getSectionOrder(ID, CustomSectionName);
2219 if (Order == WASM_SEC_ORDER_NONE)
2220 return true;
2221
2222 // Disallowed predecessors we need to check for
2224
2225 // Keep track of completed checks to avoid repeating work
2226 bool Checked[WASM_NUM_SEC_ORDERS] = {};
2227
2228 int Curr = Order;
2229 while (true) {
2230 // Add new disallowed predecessors to work list
2231 for (size_t I = 0;; ++I) {
2232 int Next = DisallowedPredecessors[Curr][I];
2233 if (Next == WASM_SEC_ORDER_NONE)
2234 break;
2235 if (Checked[Next])
2236 continue;
2237 WorkList.push_back(Next);
2238 Checked[Next] = true;
2239 }
2240
2241 if (WorkList.empty())
2242 break;
2243
2244 // Consider next disallowed predecessor
2245 Curr = WorkList.pop_back_val();
2246 if (Seen[Curr])
2247 return false;
2248 }
2249
2250 // Have not seen any disallowed predecessors
2251 Seen[Order] = true;
2252 return true;
2253}
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:622
#define LLVM_DEBUG(...)
Definition: Debug.h:106
This file defines the DenseSet and SmallDenseSet classes.
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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...
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 StringRef readString(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)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
const T * data() const
Definition: ArrayRef.h:165
Implements a dense probed hash-table based set.
Definition: DenseSet.h:278
Helper for Errors used as out-parameters.
Definition: Error.h:1130
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
reference get()
Returns a reference to the stored T value.
Definition: Error.h:578
bool empty() const
Definition: Function.h:859
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:132
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:181
bool empty() const
Definition: SmallVector.h:81
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void reserve(size_type N)
Definition: SmallVector.h:663
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:571
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:265
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
iterator begin() const
Definition: StringRef.h:116
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
iterator end() const
Definition: StringRef.h:118
const unsigned char * bytes_begin() const
Definition: StringRef.h:128
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:38
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
StringSwitch & StartsWith(StringLiteral S, T Value)
Definition: StringSwitch.h:83
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:81
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:416
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:213
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: SymbolicFile.h:103
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:210
MemoryBufferRef Data
Definition: Binary.h:37
StringRef getData() const
Definition: Binary.cpp:39
This class is the base class for all object file types.
Definition: ObjectFile.h:229
friend class RelocationRef
Definition: ObjectFile.h:287
friend class SymbolRef
Definition: ObjectFile.h:247
friend class SectionRef
Definition: ObjectFile.h:261
static Expected< std::unique_ptr< WasmObjectFile > > createWasmObjectFile(MemoryBufferRef Object)
Expected< uint64_t > getSymbolValue(DataRefImpl Symb) const
Definition: ObjectFile.cpp:56
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:168
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:160
const wasm::WasmRelocation & getWasmRelocation(const RelocationRef &Ref) const
uint32_t getSymbolSize(SymbolRef Sym) const
ArrayRef< wasm::WasmFunction > functions() const
Definition: Wasm.h:155
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
bool isValidSectionOrder(unsigned ID, StringRef CustomSectionName="")
static int DisallowedPredecessors[WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS]
Definition: Wasm.h:356
unsigned getBinding() const
Definition: Wasm.h:89
LLVM_DUMP_METHOD void dump() const
bool isTypeData() const
Definition: Wasm.h:59
bool isHidden() const
Definition: Wasm.h:93
wasm::WasmSymbolInfo Info
Definition: Wasm.h:48
void print(raw_ostream &Out) const
bool isDefined() const
Definition: Wasm.h:71
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:47
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:77
uint32_t read32le(const void *P)
Definition: Endian.h:425
@ WASM_OPCODE_I64_ADD
Definition: Wasm.h:109
@ WASM_OPCODE_I32_SUB
Definition: Wasm.h:107
@ WASM_OPCODE_F64_CONST
Definition: Wasm.h:105
@ WASM_OPCODE_END
Definition: Wasm.h:93
@ WASM_OPCODE_I64_MUL
Definition: Wasm.h:111
@ WASM_OPCODE_REF_NULL
Definition: Wasm.h:112
@ WASM_OPCODE_GC_PREFIX
Definition: Wasm.h:114
@ WASM_OPCODE_REF_FUNC
Definition: Wasm.h:113
@ WASM_OPCODE_F32_CONST
Definition: Wasm.h:104
@ WASM_OPCODE_GLOBAL_GET
Definition: Wasm.h:98
@ WASM_OPCODE_I64_SUB
Definition: Wasm.h:110
@ WASM_OPCODE_I32_MUL
Definition: Wasm.h:108
@ WASM_OPCODE_I32_ADD
Definition: Wasm.h:106
@ WASM_OPCODE_I64_CONST
Definition: Wasm.h:103
@ WASM_OPCODE_I32_CONST
Definition: Wasm.h:102
const unsigned WASM_SYMBOL_UNDEFINED
Definition: Wasm.h:243
@ WASM_TYPE_ARRAY
Definition: Wasm.h:74
@ WASM_TYPE_NULLABLE
Definition: Wasm.h:72
@ WASM_TYPE_I64
Definition: Wasm.h:55
@ WASM_TYPE_F64
Definition: Wasm.h:57
@ WASM_TYPE_FUNCREF
Definition: Wasm.h:63
@ WASM_TYPE_REC
Definition: Wasm.h:78
@ WASM_TYPE_EXTERNREF
Definition: Wasm.h:64
@ WASM_TYPE_SUB
Definition: Wasm.h:76
@ WASM_TYPE_FUNC
Definition: Wasm.h:73
@ WASM_TYPE_STRUCT
Definition: Wasm.h:75
@ WASM_TYPE_NONNULLABLE
Definition: Wasm.h:71
@ WASM_TYPE_I32
Definition: Wasm.h:54
@ WASM_TYPE_F32
Definition: Wasm.h:56
@ WASM_TYPE_V128
Definition: Wasm.h:58
@ WASM_TYPE_SUB_FINAL
Definition: Wasm.h:77
@ WASM_TYPE_EXNREF
Definition: Wasm.h:65
@ WASM_ELEM_SEGMENT_HAS_INIT_EXPRS
Definition: Wasm.h:171
@ WASM_ELEM_SEGMENT_IS_DECLARATIVE
Definition: Wasm.h:169
@ WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER
Definition: Wasm.h:170
@ WASM_ELEM_SEGMENT_IS_PASSIVE
Definition: Wasm.h:168
@ WASM_EXTERNAL_TABLE
Definition: Wasm.h:85
@ WASM_EXTERNAL_FUNCTION
Definition: Wasm.h:84
@ WASM_EXTERNAL_TAG
Definition: Wasm.h:88
@ WASM_EXTERNAL_MEMORY
Definition: Wasm.h:86
@ WASM_EXTERNAL_GLOBAL
Definition: Wasm.h:87
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition: Wasm.h:238
const unsigned WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND
Definition: Wasm.h:173
const uint32_t WasmMetadataVersion
Definition: Wasm.h:30
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:239
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:240
@ WASM_NAMES_LOCAL
Definition: Wasm.h:185
@ WASM_NAMES_DATA_SEGMENT
Definition: Wasm.h:187
@ WASM_NAMES_GLOBAL
Definition: Wasm.h:186
@ WASM_NAMES_FUNCTION
Definition: Wasm.h:184
WasmSymbolType
Definition: Wasm.h:215
@ WASM_SYMBOL_TYPE_GLOBAL
Definition: Wasm.h:218
@ WASM_SYMBOL_TYPE_DATA
Definition: Wasm.h:217
@ WASM_SYMBOL_TYPE_TAG
Definition: Wasm.h:220
@ WASM_SYMBOL_TYPE_TABLE
Definition: Wasm.h:221
@ WASM_SYMBOL_TYPE_SECTION
Definition: Wasm.h:219
@ WASM_SYMBOL_TYPE_FUNCTION
Definition: Wasm.h:216
const uint32_t WasmVersion
Definition: Wasm.h:28
@ WASM_SEC_CODE
Definition: Wasm.h:45
@ WASM_SEC_MEMORY
Definition: Wasm.h:40
@ WASM_SEC_IMPORT
Definition: Wasm.h:37
@ WASM_SEC_EXPORT
Definition: Wasm.h:42
@ WASM_SEC_DATACOUNT
Definition: Wasm.h:47
@ WASM_SEC_LAST_KNOWN
Definition: Wasm.h:49
@ WASM_SEC_CUSTOM
Definition: Wasm.h:35
@ WASM_SEC_FUNCTION
Definition: Wasm.h:38
@ WASM_SEC_ELEM
Definition: Wasm.h:44
@ WASM_SEC_START
Definition: Wasm.h:43
@ WASM_SEC_TABLE
Definition: Wasm.h:39
@ WASM_SEC_TYPE
Definition: Wasm.h:36
@ WASM_SEC_TAG
Definition: Wasm.h:48
@ WASM_SEC_GLOBAL
Definition: Wasm.h:41
@ WASM_SEC_DATA
Definition: Wasm.h:46
@ WASM_OPCODE_ARRAY_NEW_FIXED
Definition: Wasm.h:123
@ WASM_OPCODE_REF_I31
Definition: Wasm.h:124
@ WASM_OPCODE_ARRAY_NEW_DEFAULT
Definition: Wasm.h:122
@ WASM_OPCODE_STRUCT_NEW
Definition: Wasm.h:119
@ WASM_OPCODE_STRUCT_NEW_DEFAULT
Definition: Wasm.h:120
@ WASM_OPCODE_ARRAY_NEW
Definition: Wasm.h:121
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:235
@ WASM_LIMITS_FLAG_HAS_MAX
Definition: Wasm.h:157
@ WASM_LIMITS_FLAG_IS_64
Definition: Wasm.h:159
NameType
Definition: Wasm.h:471
@ WASM_DATA_SEGMENT_IS_PASSIVE
Definition: Wasm.h:163
@ WASM_DATA_SEGMENT_HAS_MEMINDEX
Definition: Wasm.h:164
llvm::StringRef sectionTypeToString(uint32_t type)
Definition: Wasm.cpp:41
@ WASM_COMDAT_SECTION
Definition: Wasm.h:211
@ WASM_COMDAT_FUNCTION
Definition: Wasm.h:209
@ WASM_COMDAT_DATA
Definition: Wasm.h:208
const unsigned WASM_SYMBOL_EXPLICIT_NAME
Definition: Wasm.h:245
@ WASM_FEATURE_PREFIX_USED
Definition: Wasm.h:177
@ WASM_FEATURE_PREFIX_DISALLOWED
Definition: Wasm.h:178
const unsigned WASM_SYMBOL_ABSOLUTE
Definition: Wasm.h:248
@ WASM_DYLINK_NEEDED
Definition: Wasm.h:201
@ WASM_DYLINK_MEM_INFO
Definition: Wasm.h:200
@ WASM_DYLINK_EXPORT_INFO
Definition: Wasm.h:202
@ WASM_DYLINK_IMPORT_INFO
Definition: Wasm.h:203
@ WASM_INIT_FUNCS
Definition: Wasm.h:193
@ WASM_COMDAT_INFO
Definition: Wasm.h:194
@ WASM_SEGMENT_INFO
Definition: Wasm.h:192
@ WASM_SYMBOL_TABLE
Definition: Wasm.h:195
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
std::string to_string(const T &Value)
Definition: ScopedPrinter.h:85
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:131
@ Import
Import information from summary.
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:165
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1291
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
@ Global
Append to llvm.global_dtors.
@ Ref
The access may reference the value stored in memory.
const char * toString(DWARFSectionKind Kind)
ArrayRef< uint8_t > Content
Definition: Wasm.h:115
std::vector< wasm::WasmRelocation > Relocations
Definition: Wasm.h:116
wasm::WasmDataSegment Data
Definition: Wasm.h:123
uint32_t SectionOffset
Definition: Wasm.h:122
ArrayRef< uint8_t > Content
Definition: Wasm.h:411
WasmInitExpr Offset
Definition: Wasm.h:409
uint32_t MemoryAlignment
Definition: Wasm.h:291
std::vector< StringRef > Needed
Definition: Wasm.h:294
std::vector< WasmDylinkExportInfo > ExportInfo
Definition: Wasm.h:296
std::vector< WasmDylinkImportInfo > ImportInfo
Definition: Wasm.h:295
uint32_t TableAlignment
Definition: Wasm.h:293
WasmInitExpr Offset
Definition: Wasm.h:428
std::vector< uint32_t > Functions
Definition: Wasm.h:429
StringRef Name
Definition: Wasm.h:311
uint32_t Index
Definition: Wasm.h:313
std::optional< StringRef > ExportName
Definition: Wasm.h:398
uint32_t CodeSectionOffset
Definition: Wasm.h:395
uint32_t Offset
Definition: Wasm.h:363
WasmGlobalType Type
Definition: Wasm.h:360
WasmLimits Memory
Definition: Wasm.h:381
StringRef Field
Definition: Wasm.h:375
WasmGlobalType Global
Definition: Wasm.h:379
StringRef Module
Definition: Wasm.h:374
uint32_t SigIndex
Definition: Wasm.h:378
WasmTableType Table
Definition: Wasm.h:380
union llvm::wasm::WasmInitExprMVP::@187 Value
WasmInitExprMVP Inst
Definition: Wasm.h:349
ArrayRef< uint8_t > Body
Definition: Wasm.h:350
std::vector< WasmInitFunc > InitFunctions
Definition: Wasm.h:486
std::vector< StringRef > Comdats
Definition: Wasm.h:487
std::vector< std::pair< std::string, std::string > > SDKs
Definition: Wasm.h:302
std::vector< std::pair< std::string, std::string > > Languages
Definition: Wasm.h:300
std::vector< std::pair< std::string, std::string > > Tools
Definition: Wasm.h:301
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:495
SmallVector< ValType, 4 > Params
Definition: Wasm.h:496
enum llvm::wasm::WasmSignature::@192 Kind
WasmDataReference DataRef
Definition: Wasm.h:467
WasmLimits Limits
Definition: Wasm.h:324
WasmTableType Type
Definition: Wasm.h:329
StringRef SymbolName
Definition: Wasm.h:330
struct llvm::object::DataRefImpl::@371 d