LLVM 22.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 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_PAGE_SIZE) {
295 uint32_t PageSizeLog2 = readVaruint32(Ctx);
296 if (PageSizeLog2 >= 32)
297 report_fatal_error("log2(wasm page size) too large");
298 Result.PageSize = 1 << PageSizeLog2;
299 }
300 return Result;
301}
302
304 wasm::WasmTableType TableType;
305 auto ElemType = parseValType(Ctx, readVaruint32(Ctx));
306 TableType.ElemType = ElemType;
307 TableType.Limits = readLimits(Ctx);
308 return TableType;
309}
310
312 WasmSectionOrderChecker &Checker) {
313 Section.Type = readUint8(Ctx);
314 LLVM_DEBUG(dbgs() << "readSection type=" << Section.Type << "\n");
315 // When reading the section's size, store the size of the LEB used to encode
316 // it. This allows objcopy/strip to reproduce the binary identically.
317 const uint8_t *PreSizePtr = Ctx.Ptr;
319 Section.HeaderSecSizeEncodingLen = Ctx.Ptr - PreSizePtr;
320 Section.Offset = Ctx.Ptr - Ctx.Start;
321 if (Size == 0)
322 return make_error<StringError>("zero length section",
324 if (Ctx.Ptr + Size > Ctx.End)
325 return make_error<StringError>("section too large",
327 if (Section.Type == wasm::WASM_SEC_CUSTOM) {
329 SectionCtx.Start = Ctx.Ptr;
330 SectionCtx.Ptr = Ctx.Ptr;
331 SectionCtx.End = Ctx.Ptr + Size;
332
333 Section.Name = readString(SectionCtx);
334
335 uint32_t SectionNameSize = SectionCtx.Ptr - SectionCtx.Start;
336 Ctx.Ptr += SectionNameSize;
337 Size -= SectionNameSize;
338 }
339
340 if (!Checker.isValidSectionOrder(Section.Type, Section.Name)) {
341 return make_error<StringError>("out of order section type: " +
342 llvm::to_string(Section.Type),
344 }
345
346 Section.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
347 Ctx.Ptr += Size;
348 return Error::success();
349}
350
352 : ObjectFile(Binary::ID_Wasm, Buffer) {
353 ErrorAsOutParameter ErrAsOutParam(Err);
354 Header.Magic = getData().substr(0, 4);
355 if (Header.Magic != StringRef("\0asm", 4)) {
356 Err = make_error<StringError>("invalid magic number",
358 return;
359 }
360
361 ReadContext Ctx;
362 Ctx.Start = getData().bytes_begin();
363 Ctx.Ptr = Ctx.Start + 4;
364 Ctx.End = Ctx.Start + getData().size();
365
366 if (Ctx.Ptr + 4 > Ctx.End) {
367 Err = make_error<StringError>("missing version number",
369 return;
370 }
371
372 Header.Version = readUint32(Ctx);
373 if (Header.Version != wasm::WasmVersion) {
374 Err = make_error<StringError>("invalid version number: " +
375 Twine(Header.Version),
377 return;
378 }
379
381 while (Ctx.Ptr < Ctx.End) {
382 WasmSection Sec;
383 if ((Err = readSection(Sec, Ctx, Checker)))
384 return;
385 if ((Err = parseSection(Sec)))
386 return;
387
388 Sections.push_back(Sec);
389 }
390}
391
392Error WasmObjectFile::parseSection(WasmSection &Sec) {
393 ReadContext Ctx;
394 Ctx.Start = Sec.Content.data();
395 Ctx.End = Ctx.Start + Sec.Content.size();
396 Ctx.Ptr = Ctx.Start;
397 switch (Sec.Type) {
399 return parseCustomSection(Sec, Ctx);
401 return parseTypeSection(Ctx);
403 return parseImportSection(Ctx);
405 return parseFunctionSection(Ctx);
407 return parseTableSection(Ctx);
409 return parseMemorySection(Ctx);
411 return parseTagSection(Ctx);
413 return parseGlobalSection(Ctx);
415 return parseExportSection(Ctx);
417 return parseStartSection(Ctx);
419 return parseElemSection(Ctx);
421 return parseCodeSection(Ctx);
423 return parseDataSection(Ctx);
425 return parseDataCountSection(Ctx);
426 default:
427 return make_error<GenericBinaryError>(
428 "invalid section type: " + Twine(Sec.Type), object_error::parse_failed);
429 }
430}
431
432Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) {
433 // Legacy "dylink" section support.
434 // See parseDylink0Section for the current "dylink.0" section parsing.
435 HasDylinkSection = true;
436 DylinkInfo.MemorySize = readVaruint32(Ctx);
437 DylinkInfo.MemoryAlignment = readVaruint32(Ctx);
438 DylinkInfo.TableSize = readVaruint32(Ctx);
439 DylinkInfo.TableAlignment = readVaruint32(Ctx);
440 uint32_t Count = readVaruint32(Ctx);
441 while (Count--) {
442 DylinkInfo.Needed.push_back(readString(Ctx));
443 }
444
445 if (Ctx.Ptr != Ctx.End)
446 return make_error<GenericBinaryError>("dylink section ended prematurely",
448 return Error::success();
449}
450
451Error WasmObjectFile::parseDylink0Section(ReadContext &Ctx) {
452 // See
453 // https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
454 HasDylinkSection = true;
455
456 const uint8_t *OrigEnd = Ctx.End;
457 while (Ctx.Ptr < OrigEnd) {
458 Ctx.End = OrigEnd;
459 uint8_t Type = readUint8(Ctx);
461 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size
462 << "\n");
463 Ctx.End = Ctx.Ptr + Size;
464 uint32_t Count;
465 switch (Type) {
467 DylinkInfo.MemorySize = readVaruint32(Ctx);
468 DylinkInfo.MemoryAlignment = readVaruint32(Ctx);
469 DylinkInfo.TableSize = readVaruint32(Ctx);
470 DylinkInfo.TableAlignment = readVaruint32(Ctx);
471 break;
473 Count = readVaruint32(Ctx);
474 while (Count--) {
475 DylinkInfo.Needed.push_back(readString(Ctx));
476 }
477 break;
479 uint32_t Count = readVaruint32(Ctx);
480 while (Count--) {
481 DylinkInfo.ExportInfo.push_back({readString(Ctx), readVaruint32(Ctx)});
482 }
483 break;
484 }
486 uint32_t Count = readVaruint32(Ctx);
487 while (Count--) {
488 DylinkInfo.ImportInfo.push_back(
489 {readString(Ctx), readString(Ctx), readVaruint32(Ctx)});
490 }
491 break;
492 }
494 Count = readVaruint32(Ctx);
495 while (Count--) {
496 DylinkInfo.RuntimePath.push_back(readString(Ctx));
497 }
498 break;
499 }
500 default:
501 LLVM_DEBUG(dbgs() << "unknown dylink.0 sub-section: " << Type << "\n");
502 Ctx.Ptr += Size;
503 break;
504 }
505 if (Ctx.Ptr != Ctx.End) {
506 return make_error<GenericBinaryError>(
507 "dylink.0 sub-section ended prematurely", object_error::parse_failed);
508 }
509 }
510
511 if (Ctx.Ptr != Ctx.End)
512 return make_error<GenericBinaryError>("dylink.0 section ended prematurely",
514 return Error::success();
515}
516
517Error WasmObjectFile::parseNameSection(ReadContext &Ctx) {
518 llvm::DenseSet<uint64_t> SeenFunctions;
519 llvm::DenseSet<uint64_t> SeenGlobals;
520 llvm::DenseSet<uint64_t> SeenSegments;
521
522 // If we have linking section (symbol table) or if we are parsing a DSO
523 // then we don't use the name section for symbol information.
524 bool PopulateSymbolTable = !HasLinkingSection && !HasDylinkSection;
525
526 // If we are using the name section for symbol information then it will
527 // supersede any symbols created by the export section.
528 if (PopulateSymbolTable)
529 Symbols.clear();
530
531 while (Ctx.Ptr < Ctx.End) {
532 uint8_t Type = readUint8(Ctx);
534 const uint8_t *SubSectionEnd = Ctx.Ptr + Size;
535
536 switch (Type) {
540 uint32_t Count = readVaruint32(Ctx);
541 while (Count--) {
547 /* Flags */ 0,
548 /* ImportModule */ std::nullopt,
549 /* ImportName */ std::nullopt,
550 /* ExportName */ std::nullopt,
551 {/* ElementIndex */ Index}};
552 const wasm::WasmSignature *Signature = nullptr;
553 const wasm::WasmGlobalType *GlobalType = nullptr;
554 const wasm::WasmTableType *TableType = nullptr;
556 if (!SeenFunctions.insert(Index).second)
557 return make_error<GenericBinaryError>(
558 "function named more than once", object_error::parse_failed);
559 if (!isValidFunctionIndex(Index) || Name.empty())
560 return make_error<GenericBinaryError>("invalid function name entry",
562
563 if (isDefinedFunctionIndex(Index)) {
564 wasm::WasmFunction &F = getDefinedFunction(Index);
565 F.DebugName = Name;
566 Signature = &Signatures[F.SigIndex];
567 if (F.ExportName) {
568 Info.ExportName = F.ExportName;
570 } else {
572 }
573 } else {
575 }
576 } else if (Type == wasm::WASM_NAMES_GLOBAL) {
577 if (!SeenGlobals.insert(Index).second)
578 return make_error<GenericBinaryError>("global named more than once",
580 if (!isValidGlobalIndex(Index) || Name.empty())
581 return make_error<GenericBinaryError>("invalid global name entry",
583 nameType = wasm::NameType::GLOBAL;
585 if (isDefinedGlobalIndex(Index)) {
586 GlobalType = &getDefinedGlobal(Index).Type;
587 } else {
589 }
590 } else {
591 if (!SeenSegments.insert(Index).second)
592 return make_error<GenericBinaryError>(
593 "segment named more than once", object_error::parse_failed);
594 if (Index > DataSegments.size())
595 return make_error<GenericBinaryError>("invalid data segment name entry",
600 assert(Index < DataSegments.size());
602 Index, 0, DataSegments[Index].Data.Content.size()};
603 }
604 DebugNames.push_back(wasm::WasmDebugName{nameType, Index, Name});
605 if (PopulateSymbolTable)
606 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
607 }
608 break;
609 }
610 // Ignore local names for now
612 default:
613 Ctx.Ptr += Size;
614 break;
615 }
616 if (Ctx.Ptr != SubSectionEnd)
617 return make_error<GenericBinaryError>(
618 "name sub-section ended prematurely", object_error::parse_failed);
619 }
620
621 if (Ctx.Ptr != Ctx.End)
622 return make_error<GenericBinaryError>("name section ended prematurely",
624 return Error::success();
625}
626
627Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) {
628 HasLinkingSection = true;
629
630 LinkingData.Version = readVaruint32(Ctx);
631 if (LinkingData.Version != wasm::WasmMetadataVersion) {
632 return make_error<GenericBinaryError>(
633 "unexpected metadata version: " + Twine(LinkingData.Version) +
634 " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")",
636 }
637
638 const uint8_t *OrigEnd = Ctx.End;
639 while (Ctx.Ptr < OrigEnd) {
640 Ctx.End = OrigEnd;
641 uint8_t Type = readUint8(Ctx);
643 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size
644 << "\n");
645 Ctx.End = Ctx.Ptr + Size;
646 switch (Type) {
648 if (Error Err = parseLinkingSectionSymtab(Ctx))
649 return Err;
650 break;
652 uint32_t Count = readVaruint32(Ctx);
653 if (Count > DataSegments.size())
654 return make_error<GenericBinaryError>("too many segment names",
656 for (uint32_t I = 0; I < Count; I++) {
657 DataSegments[I].Data.Name = readString(Ctx);
658 DataSegments[I].Data.Alignment = readVaruint32(Ctx);
659 DataSegments[I].Data.LinkingFlags = readVaruint32(Ctx);
660 }
661 break;
662 }
664 uint32_t Count = readVaruint32(Ctx);
665 LinkingData.InitFunctions.reserve(Count);
666 for (uint32_t I = 0; I < Count; I++) {
668 Init.Priority = readVaruint32(Ctx);
669 Init.Symbol = readVaruint32(Ctx);
670 if (!isValidFunctionSymbol(Init.Symbol))
671 return make_error<GenericBinaryError>("invalid function symbol: " +
672 Twine(Init.Symbol),
674 LinkingData.InitFunctions.emplace_back(Init);
675 }
676 break;
677 }
679 if (Error Err = parseLinkingSectionComdat(Ctx))
680 return Err;
681 break;
682 default:
683 Ctx.Ptr += Size;
684 break;
685 }
686 if (Ctx.Ptr != Ctx.End)
687 return make_error<GenericBinaryError>(
688 "linking sub-section ended prematurely", object_error::parse_failed);
689 }
690 if (Ctx.Ptr != OrigEnd)
691 return make_error<GenericBinaryError>("linking section ended prematurely",
693 return Error::success();
694}
695
696Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
697 uint32_t Count = readVaruint32(Ctx);
698 // Clear out any symbol information that was derived from the exports
699 // section.
700 Symbols.clear();
701 Symbols.reserve(Count);
702 StringSet<> SymbolNames;
703
704 std::vector<wasm::WasmImport *> ImportedGlobals;
705 std::vector<wasm::WasmImport *> ImportedFunctions;
706 std::vector<wasm::WasmImport *> ImportedTags;
707 std::vector<wasm::WasmImport *> ImportedTables;
708 ImportedGlobals.reserve(Imports.size());
709 ImportedFunctions.reserve(Imports.size());
710 ImportedTags.reserve(Imports.size());
711 ImportedTables.reserve(Imports.size());
712 for (auto &I : Imports) {
714 ImportedFunctions.emplace_back(&I);
715 else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL)
716 ImportedGlobals.emplace_back(&I);
717 else if (I.Kind == wasm::WASM_EXTERNAL_TAG)
718 ImportedTags.emplace_back(&I);
719 else if (I.Kind == wasm::WASM_EXTERNAL_TABLE)
720 ImportedTables.emplace_back(&I);
721 }
722
723 while (Count--) {
725 const wasm::WasmSignature *Signature = nullptr;
726 const wasm::WasmGlobalType *GlobalType = nullptr;
727 const wasm::WasmTableType *TableType = nullptr;
728
729 Info.Kind = readUint8(Ctx);
730 Info.Flags = readVaruint32(Ctx);
731 bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0;
732
733 switch (Info.Kind) {
735 Info.ElementIndex = readVaruint32(Ctx);
736 if (!isValidFunctionIndex(Info.ElementIndex) ||
737 IsDefined != isDefinedFunctionIndex(Info.ElementIndex))
738 return make_error<GenericBinaryError>("invalid function symbol index",
740 if (IsDefined) {
741 Info.Name = readString(Ctx);
742 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions;
743 wasm::WasmFunction &Function = Functions[FuncIndex];
744 Signature = &Signatures[Function.SigIndex];
745 if (Function.SymbolName.empty())
746 Function.SymbolName = Info.Name;
747 } else {
748 wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex];
749 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
750 Info.Name = readString(Ctx);
751 Info.ImportName = Import.Field;
752 } else {
753 Info.Name = Import.Field;
754 }
755 Signature = &Signatures[Import.SigIndex];
756 Info.ImportModule = Import.Module;
757 }
758 break;
759
761 Info.ElementIndex = readVaruint32(Ctx);
762 if (!isValidGlobalIndex(Info.ElementIndex) ||
763 IsDefined != isDefinedGlobalIndex(Info.ElementIndex))
764 return make_error<GenericBinaryError>("invalid global symbol index",
766 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
768 return make_error<GenericBinaryError>("undefined weak global symbol",
770 if (IsDefined) {
771 Info.Name = readString(Ctx);
772 unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals;
773 wasm::WasmGlobal &Global = Globals[GlobalIndex];
774 GlobalType = &Global.Type;
775 if (Global.SymbolName.empty())
776 Global.SymbolName = Info.Name;
777 } else {
778 wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex];
779 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
780 Info.Name = readString(Ctx);
781 Info.ImportName = Import.Field;
782 } else {
783 Info.Name = Import.Field;
784 }
785 GlobalType = &Import.Global;
786 Info.ImportModule = Import.Module;
787 }
788 break;
789
791 Info.ElementIndex = readVaruint32(Ctx);
792 if (!isValidTableNumber(Info.ElementIndex) ||
793 IsDefined != isDefinedTableNumber(Info.ElementIndex))
794 return make_error<GenericBinaryError>("invalid table symbol index",
796 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
798 return make_error<GenericBinaryError>("undefined weak table symbol",
800 if (IsDefined) {
801 Info.Name = readString(Ctx);
802 unsigned TableNumber = Info.ElementIndex - NumImportedTables;
803 wasm::WasmTable &Table = Tables[TableNumber];
804 TableType = &Table.Type;
805 if (Table.SymbolName.empty())
806 Table.SymbolName = Info.Name;
807 } else {
808 wasm::WasmImport &Import = *ImportedTables[Info.ElementIndex];
809 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
810 Info.Name = readString(Ctx);
811 Info.ImportName = Import.Field;
812 } else {
813 Info.Name = Import.Field;
814 }
815 TableType = &Import.Table;
816 Info.ImportModule = Import.Module;
817 }
818 break;
819
821 Info.Name = readString(Ctx);
822 if (IsDefined) {
823 auto Index = readVaruint32(Ctx);
824 auto Offset = readVaruint64(Ctx);
825 auto Size = readVaruint64(Ctx);
826 if (!(Info.Flags & wasm::WASM_SYMBOL_ABSOLUTE)) {
827 if (static_cast<size_t>(Index) >= DataSegments.size())
828 return make_error<GenericBinaryError>(
829 "invalid data segment index: " + Twine(Index),
831 size_t SegmentSize = DataSegments[Index].Data.Content.size();
832 if (Offset > SegmentSize)
833 return make_error<GenericBinaryError>(
834 "invalid data symbol offset: `" + Info.Name +
835 "` (offset: " + Twine(Offset) +
836 " segment size: " + Twine(SegmentSize) + ")",
838 }
840 }
841 break;
842
844 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
846 return make_error<GenericBinaryError>(
847 "section symbols must have local binding",
849 Info.ElementIndex = readVaruint32(Ctx);
850 // Use somewhat unique section name as symbol name.
851 StringRef SectionName = Sections[Info.ElementIndex].Name;
852 Info.Name = SectionName;
853 break;
854 }
855
857 Info.ElementIndex = readVaruint32(Ctx);
858 if (!isValidTagIndex(Info.ElementIndex) ||
859 IsDefined != isDefinedTagIndex(Info.ElementIndex))
860 return make_error<GenericBinaryError>("invalid tag symbol index",
862 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
864 return make_error<GenericBinaryError>("undefined weak global symbol",
866 if (IsDefined) {
867 Info.Name = readString(Ctx);
868 unsigned TagIndex = Info.ElementIndex - NumImportedTags;
869 wasm::WasmTag &Tag = Tags[TagIndex];
870 Signature = &Signatures[Tag.SigIndex];
871 if (Tag.SymbolName.empty())
872 Tag.SymbolName = Info.Name;
873
874 } else {
875 wasm::WasmImport &Import = *ImportedTags[Info.ElementIndex];
876 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) {
877 Info.Name = readString(Ctx);
878 Info.ImportName = Import.Field;
879 } else {
880 Info.Name = Import.Field;
881 }
882 Signature = &Signatures[Import.SigIndex];
883 Info.ImportModule = Import.Module;
884 }
885 break;
886 }
887
888 default:
889 return make_error<GenericBinaryError>("invalid symbol type: " +
890 Twine(unsigned(Info.Kind)),
892 }
893
894 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) !=
896 !SymbolNames.insert(Info.Name).second)
897 return make_error<GenericBinaryError>("duplicate symbol name " +
898 Twine(Info.Name),
900 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
901 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
902 }
903
904 return Error::success();
905}
906
907Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) {
908 uint32_t ComdatCount = readVaruint32(Ctx);
909 StringSet<> ComdatSet;
910 for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) {
912 if (Name.empty() || !ComdatSet.insert(Name).second)
913 return make_error<GenericBinaryError>("bad/duplicate COMDAT name " +
914 Twine(Name),
916 LinkingData.Comdats.emplace_back(Name);
918 if (Flags != 0)
919 return make_error<GenericBinaryError>("unsupported COMDAT flags",
921
922 uint32_t EntryCount = readVaruint32(Ctx);
923 while (EntryCount--) {
924 unsigned Kind = readVaruint32(Ctx);
925 unsigned Index = readVaruint32(Ctx);
926 switch (Kind) {
927 default:
928 return make_error<GenericBinaryError>("invalid COMDAT entry type",
931 if (Index >= DataSegments.size())
932 return make_error<GenericBinaryError>(
933 "COMDAT data index out of range", object_error::parse_failed);
934 if (DataSegments[Index].Data.Comdat != UINT32_MAX)
935 return make_error<GenericBinaryError>("data segment in two COMDATs",
937 DataSegments[Index].Data.Comdat = ComdatIndex;
938 break;
940 if (!isDefinedFunctionIndex(Index))
941 return make_error<GenericBinaryError>(
942 "COMDAT function index out of range", object_error::parse_failed);
943 if (getDefinedFunction(Index).Comdat != UINT32_MAX)
944 return make_error<GenericBinaryError>("function in two COMDATs",
946 getDefinedFunction(Index).Comdat = ComdatIndex;
947 break;
949 if (Index >= Sections.size())
950 return make_error<GenericBinaryError>(
951 "COMDAT section index out of range", object_error::parse_failed);
952 if (Sections[Index].Type != wasm::WASM_SEC_CUSTOM)
953 return make_error<GenericBinaryError>(
954 "non-custom section in a COMDAT", object_error::parse_failed);
955 Sections[Index].Comdat = ComdatIndex;
956 break;
957 }
958 }
959 }
960 return Error::success();
961}
962
963Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) {
965 uint32_t Fields = readVaruint32(Ctx);
966 for (size_t I = 0; I < Fields; ++I) {
967 StringRef FieldName = readString(Ctx);
968 if (!FieldsSeen.insert(FieldName).second)
969 return make_error<GenericBinaryError>(
970 "producers section does not have unique fields",
972 std::vector<std::pair<std::string, std::string>> *ProducerVec = nullptr;
973 if (FieldName == "language") {
974 ProducerVec = &ProducerInfo.Languages;
975 } else if (FieldName == "processed-by") {
976 ProducerVec = &ProducerInfo.Tools;
977 } else if (FieldName == "sdk") {
978 ProducerVec = &ProducerInfo.SDKs;
979 } else {
980 return make_error<GenericBinaryError>(
981 "producers section field is not named one of language, processed-by, "
982 "or sdk",
984 }
985 uint32_t ValueCount = readVaruint32(Ctx);
986 llvm::SmallSet<StringRef, 8> ProducersSeen;
987 for (size_t J = 0; J < ValueCount; ++J) {
990 if (!ProducersSeen.insert(Name).second) {
991 return make_error<GenericBinaryError>(
992 "producers section contains repeated producer",
994 }
995 ProducerVec->emplace_back(std::string(Name), std::string(Version));
996 }
997 }
998 if (Ctx.Ptr != Ctx.End)
999 return make_error<GenericBinaryError>("producers section ended prematurely",
1001 return Error::success();
1002}
1003
1004Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) {
1005 llvm::SmallSet<std::string, 8> FeaturesSeen;
1007 for (size_t I = 0; I < FeatureCount; ++I) {
1008 wasm::WasmFeatureEntry Feature;
1009 Feature.Prefix = readUint8(Ctx);
1010 switch (Feature.Prefix) {
1013 break;
1014 default:
1015 return make_error<GenericBinaryError>("unknown feature policy prefix",
1017 }
1018 Feature.Name = std::string(readString(Ctx));
1019 if (!FeaturesSeen.insert(Feature.Name).second)
1020 return make_error<GenericBinaryError>(
1021 "target features section contains repeated feature \"" +
1022 Feature.Name + "\"",
1024 TargetFeatures.push_back(Feature);
1025 }
1026 if (Ctx.Ptr != Ctx.End)
1027 return make_error<GenericBinaryError>(
1028 "target features section ended prematurely",
1030 return Error::success();
1031}
1032
1033Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
1034 uint32_t SectionIndex = readVaruint32(Ctx);
1035 if (SectionIndex >= Sections.size())
1036 return make_error<GenericBinaryError>("invalid section index",
1038 WasmSection &Section = Sections[SectionIndex];
1039 uint32_t RelocCount = readVaruint32(Ctx);
1040 uint32_t EndOffset = Section.Content.size();
1041 uint32_t PreviousOffset = 0;
1042 while (RelocCount--) {
1043 wasm::WasmRelocation Reloc = {};
1044 uint32_t type = readVaruint32(Ctx);
1045 Reloc.Type = type;
1046 Reloc.Offset = readVaruint32(Ctx);
1047 if (Reloc.Offset < PreviousOffset)
1048 return make_error<GenericBinaryError>("relocations not in offset order",
1050
1051 auto badReloc = [&](StringRef msg) {
1052 return make_error<GenericBinaryError>(
1053 msg + ": " + Twine(Symbols[Reloc.Index].Info.Name),
1055 };
1056
1057 PreviousOffset = Reloc.Offset;
1058 Reloc.Index = readVaruint32(Ctx);
1059 switch (type) {
1060 case wasm::R_WASM_FUNCTION_INDEX_LEB:
1061 case wasm::R_WASM_FUNCTION_INDEX_I32:
1062 case wasm::R_WASM_TABLE_INDEX_SLEB:
1063 case wasm::R_WASM_TABLE_INDEX_SLEB64:
1064 case wasm::R_WASM_TABLE_INDEX_I32:
1065 case wasm::R_WASM_TABLE_INDEX_I64:
1066 case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
1067 case wasm::R_WASM_TABLE_INDEX_REL_SLEB64:
1068 if (!isValidFunctionSymbol(Reloc.Index))
1069 return badReloc("invalid function relocation");
1070 break;
1071 case wasm::R_WASM_TABLE_NUMBER_LEB:
1072 if (!isValidTableSymbol(Reloc.Index))
1073 return badReloc("invalid table relocation");
1074 break;
1075 case wasm::R_WASM_TYPE_INDEX_LEB:
1076 if (Reloc.Index >= Signatures.size())
1077 return badReloc("invalid relocation type index");
1078 break;
1079 case wasm::R_WASM_GLOBAL_INDEX_LEB:
1080 // R_WASM_GLOBAL_INDEX_LEB are can be used against function and data
1081 // symbols to refer to their GOT entries.
1082 if (!isValidGlobalSymbol(Reloc.Index) &&
1083 !isValidDataSymbol(Reloc.Index) &&
1084 !isValidFunctionSymbol(Reloc.Index))
1085 return badReloc("invalid global relocation");
1086 break;
1087 case wasm::R_WASM_GLOBAL_INDEX_I32:
1088 if (!isValidGlobalSymbol(Reloc.Index))
1089 return badReloc("invalid global relocation");
1090 break;
1091 case wasm::R_WASM_TAG_INDEX_LEB:
1092 if (!isValidTagSymbol(Reloc.Index))
1093 return badReloc("invalid tag relocation");
1094 break;
1095 case wasm::R_WASM_MEMORY_ADDR_LEB:
1096 case wasm::R_WASM_MEMORY_ADDR_SLEB:
1097 case wasm::R_WASM_MEMORY_ADDR_I32:
1098 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
1099 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB:
1100 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
1101 if (!isValidDataSymbol(Reloc.Index))
1102 return badReloc("invalid data relocation");
1103 Reloc.Addend = readVarint32(Ctx);
1104 break;
1105 case wasm::R_WASM_MEMORY_ADDR_LEB64:
1106 case wasm::R_WASM_MEMORY_ADDR_SLEB64:
1107 case wasm::R_WASM_MEMORY_ADDR_I64:
1108 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
1109 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64:
1110 if (!isValidDataSymbol(Reloc.Index))
1111 return badReloc("invalid data relocation");
1112 Reloc.Addend = readVarint64(Ctx);
1113 break;
1114 case wasm::R_WASM_FUNCTION_OFFSET_I32:
1115 if (!isValidFunctionSymbol(Reloc.Index))
1116 return badReloc("invalid function relocation");
1117 Reloc.Addend = readVarint32(Ctx);
1118 break;
1119 case wasm::R_WASM_FUNCTION_OFFSET_I64:
1120 if (!isValidFunctionSymbol(Reloc.Index))
1121 return badReloc("invalid function relocation");
1122 Reloc.Addend = readVarint64(Ctx);
1123 break;
1124 case wasm::R_WASM_SECTION_OFFSET_I32:
1125 if (!isValidSectionSymbol(Reloc.Index))
1126 return badReloc("invalid section relocation");
1127 Reloc.Addend = readVarint32(Ctx);
1128 break;
1129 default:
1130 return make_error<GenericBinaryError>("invalid relocation type: " +
1131 Twine(type),
1133 }
1134
1135 // Relocations must fit inside the section, and must appear in order. They
1136 // also shouldn't overlap a function/element boundary, but we don't bother
1137 // to check that.
1138 uint64_t Size = 5;
1139 if (Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LEB64 ||
1140 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_SLEB64 ||
1141 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_REL_SLEB64)
1142 Size = 10;
1143 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I32 ||
1144 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I32 ||
1145 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I32 ||
1146 Reloc.Type == wasm::R_WASM_SECTION_OFFSET_I32 ||
1147 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
1148 Reloc.Type == wasm::R_WASM_FUNCTION_INDEX_I32 ||
1149 Reloc.Type == wasm::R_WASM_GLOBAL_INDEX_I32)
1150 Size = 4;
1151 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I64 ||
1152 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I64 ||
1153 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I64)
1154 Size = 8;
1155 if (Reloc.Offset + Size > EndOffset)
1156 return make_error<GenericBinaryError>("invalid relocation offset",
1158
1159 Section.Relocations.push_back(Reloc);
1160 }
1161 if (Ctx.Ptr != Ctx.End)
1162 return make_error<GenericBinaryError>("reloc section ended prematurely",
1164 return Error::success();
1165}
1166
1167Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) {
1168 if (Sec.Name == "dylink") {
1169 if (Error Err = parseDylinkSection(Ctx))
1170 return Err;
1171 } else if (Sec.Name == "dylink.0") {
1172 if (Error Err = parseDylink0Section(Ctx))
1173 return Err;
1174 } else if (Sec.Name == "name") {
1175 if (Error Err = parseNameSection(Ctx))
1176 return Err;
1177 } else if (Sec.Name == "linking") {
1178 if (Error Err = parseLinkingSection(Ctx))
1179 return Err;
1180 } else if (Sec.Name == "producers") {
1181 if (Error Err = parseProducersSection(Ctx))
1182 return Err;
1183 } else if (Sec.Name == "target_features") {
1184 if (Error Err = parseTargetFeaturesSection(Ctx))
1185 return Err;
1186 } else if (Sec.Name.starts_with("reloc.")) {
1187 if (Error Err = parseRelocSection(Sec.Name, Ctx))
1188 return Err;
1189 }
1190 return Error::success();
1191}
1192
1193Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
1194 auto parseFieldDef = [&]() {
1195 uint32_t TypeCode = readVaruint32((Ctx));
1196 /* Discard StorageType */ parseValType(Ctx, TypeCode);
1197 /* Discard Mutability */ readVaruint32(Ctx);
1198 };
1199
1200 uint32_t Count = readVaruint32(Ctx);
1201 Signatures.reserve(Count);
1202 while (Count--) {
1204 uint8_t Form = readUint8(Ctx);
1205 if (Form == wasm::WASM_TYPE_REC) {
1206 // Rec groups expand the type index space (beyond what was declared at
1207 // the top of the section, and also consume one element in that space.
1208 uint32_t RecSize = readVaruint32(Ctx);
1209 if (RecSize == 0)
1210 return make_error<GenericBinaryError>("Rec group size cannot be 0",
1212 Signatures.reserve(Signatures.size() + RecSize);
1213 Count += RecSize;
1215 Signatures.push_back(std::move(Sig));
1216 HasUnmodeledTypes = true;
1217 continue;
1218 }
1219 if (Form != wasm::WASM_TYPE_FUNC) {
1220 // Currently LLVM only models function types, and not other composite
1221 // types. Here we parse the type declarations just enough to skip past
1222 // them in the binary.
1223 if (Form == wasm::WASM_TYPE_SUB || Form == wasm::WASM_TYPE_SUB_FINAL) {
1224 uint32_t Supers = readVaruint32(Ctx);
1225 if (Supers > 0) {
1226 if (Supers != 1)
1227 return make_error<GenericBinaryError>(
1228 "Invalid number of supertypes", object_error::parse_failed);
1229 /* Discard SuperIndex */ readVaruint32(Ctx);
1230 }
1231 Form = readVaruint32(Ctx);
1232 }
1233 if (Form == wasm::WASM_TYPE_STRUCT) {
1234 uint32_t FieldCount = readVaruint32(Ctx);
1235 while (FieldCount--) {
1236 parseFieldDef();
1237 }
1238 } else if (Form == wasm::WASM_TYPE_ARRAY) {
1239 parseFieldDef();
1240 } else {
1241 return make_error<GenericBinaryError>("bad form",
1243 }
1245 Signatures.push_back(std::move(Sig));
1246 HasUnmodeledTypes = true;
1247 continue;
1248 }
1249
1250 uint32_t ParamCount = readVaruint32(Ctx);
1251 Sig.Params.reserve(ParamCount);
1252 while (ParamCount--) {
1253 uint32_t ParamType = readUint8(Ctx);
1254 Sig.Params.push_back(parseValType(Ctx, ParamType));
1255 }
1256 uint32_t ReturnCount = readVaruint32(Ctx);
1257 while (ReturnCount--) {
1259 Sig.Returns.push_back(parseValType(Ctx, ReturnType));
1260 }
1261
1262 Signatures.push_back(std::move(Sig));
1263 }
1264 if (Ctx.Ptr != Ctx.End)
1265 return make_error<GenericBinaryError>("type section ended prematurely",
1267 return Error::success();
1268}
1269
1270Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
1271 uint32_t Count = readVaruint32(Ctx);
1272 uint32_t NumTypes = Signatures.size();
1273 Imports.reserve(Count);
1274 for (uint32_t I = 0; I < Count; I++) {
1276 Im.Module = readString(Ctx);
1277 Im.Field = readString(Ctx);
1278 Im.Kind = readUint8(Ctx);
1279 switch (Im.Kind) {
1281 NumImportedFunctions++;
1282 Im.SigIndex = readVaruint32(Ctx);
1283 if (Im.SigIndex >= NumTypes)
1284 return make_error<GenericBinaryError>("invalid function type",
1286 break;
1288 NumImportedGlobals++;
1289 Im.Global.Type = readUint8(Ctx);
1290 Im.Global.Mutable = readVaruint1(Ctx);
1291 break;
1293 Im.Memory = readLimits(Ctx);
1295 HasMemory64 = true;
1296 break;
1298 Im.Table = readTableType(Ctx);
1299 NumImportedTables++;
1300 auto ElemType = Im.Table.ElemType;
1301 if (ElemType != wasm::ValType::FUNCREF &&
1302 ElemType != wasm::ValType::EXTERNREF &&
1303 ElemType != wasm::ValType::EXNREF &&
1304 ElemType != wasm::ValType::OTHERREF)
1305 return make_error<GenericBinaryError>("invalid table element type",
1307 break;
1308 }
1310 NumImportedTags++;
1311 if (readUint8(Ctx) != 0) // Reserved 'attribute' field
1312 return make_error<GenericBinaryError>("invalid attribute",
1314 Im.SigIndex = readVaruint32(Ctx);
1315 if (Im.SigIndex >= NumTypes)
1316 return make_error<GenericBinaryError>("invalid tag type",
1318 break;
1319 default:
1320 return make_error<GenericBinaryError>("unexpected import kind",
1322 }
1323 Imports.push_back(Im);
1324 }
1325 if (Ctx.Ptr != Ctx.End)
1326 return make_error<GenericBinaryError>("import section ended prematurely",
1328 return Error::success();
1329}
1330
1331Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
1332 uint32_t Count = readVaruint32(Ctx);
1333 Functions.reserve(Count);
1334 uint32_t NumTypes = Signatures.size();
1335 while (Count--) {
1337 if (Type >= NumTypes)
1338 return make_error<GenericBinaryError>("invalid function type",
1341 F.SigIndex = Type;
1342 Functions.push_back(F);
1343 }
1344 if (Ctx.Ptr != Ctx.End)
1345 return make_error<GenericBinaryError>("function section ended prematurely",
1347 return Error::success();
1348}
1349
1350Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
1351 TableSection = Sections.size();
1352 uint32_t Count = readVaruint32(Ctx);
1353 Tables.reserve(Count);
1354 while (Count--) {
1356 T.Type = readTableType(Ctx);
1357 T.Index = NumImportedTables + Tables.size();
1358 Tables.push_back(T);
1359 auto ElemType = Tables.back().Type.ElemType;
1360 if (ElemType != wasm::ValType::FUNCREF &&
1361 ElemType != wasm::ValType::EXTERNREF &&
1362 ElemType != wasm::ValType::EXNREF &&
1363 ElemType != wasm::ValType::OTHERREF) {
1364 return make_error<GenericBinaryError>("invalid table element type",
1366 }
1367 }
1368 if (Ctx.Ptr != Ctx.End)
1369 return make_error<GenericBinaryError>("table section ended prematurely",
1371 return Error::success();
1372}
1373
1374Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
1375 uint32_t Count = readVaruint32(Ctx);
1376 Memories.reserve(Count);
1377 while (Count--) {
1378 auto Limits = readLimits(Ctx);
1379 if (Limits.Flags & wasm::WASM_LIMITS_FLAG_IS_64)
1380 HasMemory64 = true;
1381 Memories.push_back(Limits);
1382 }
1383 if (Ctx.Ptr != Ctx.End)
1384 return make_error<GenericBinaryError>("memory section ended prematurely",
1386 return Error::success();
1387}
1388
1389Error WasmObjectFile::parseTagSection(ReadContext &Ctx) {
1390 TagSection = Sections.size();
1391 uint32_t Count = readVaruint32(Ctx);
1392 Tags.reserve(Count);
1393 uint32_t NumTypes = Signatures.size();
1394 while (Count--) {
1395 if (readUint8(Ctx) != 0) // Reserved 'attribute' field
1396 return make_error<GenericBinaryError>("invalid attribute",
1399 if (Type >= NumTypes)
1400 return make_error<GenericBinaryError>("invalid tag type",
1403 Tag.Index = NumImportedTags + Tags.size();
1404 Tag.SigIndex = Type;
1405 Signatures[Type].Kind = wasm::WasmSignature::Tag;
1406 Tags.push_back(Tag);
1407 }
1408
1409 if (Ctx.Ptr != Ctx.End)
1410 return make_error<GenericBinaryError>("tag section ended prematurely",
1412 return Error::success();
1413}
1414
1415Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
1416 GlobalSection = Sections.size();
1417 const uint8_t *SectionStart = Ctx.Ptr;
1418 uint32_t Count = readVaruint32(Ctx);
1419 Globals.reserve(Count);
1420 while (Count--) {
1422 Global.Index = NumImportedGlobals + Globals.size();
1423 const uint8_t *GlobalStart = Ctx.Ptr;
1424 Global.Offset = static_cast<uint32_t>(GlobalStart - SectionStart);
1425 auto GlobalOpcode = readVaruint32(Ctx);
1426 Global.Type.Type = (uint8_t)parseValType(Ctx, GlobalOpcode);
1427 Global.Type.Mutable = readVaruint1(Ctx);
1428 if (Error Err = readInitExpr(Global.InitExpr, Ctx))
1429 return Err;
1430 Global.Size = static_cast<uint32_t>(Ctx.Ptr - GlobalStart);
1431 Globals.push_back(Global);
1432 }
1433 if (Ctx.Ptr != Ctx.End)
1434 return make_error<GenericBinaryError>("global section ended prematurely",
1436 return Error::success();
1437}
1438
1439Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
1440 uint32_t Count = readVaruint32(Ctx);
1441 Exports.reserve(Count);
1442 Symbols.reserve(Count);
1443 for (uint32_t I = 0; I < Count; I++) {
1445 Ex.Name = readString(Ctx);
1446 Ex.Kind = readUint8(Ctx);
1447 Ex.Index = readVaruint32(Ctx);
1448 const wasm::WasmSignature *Signature = nullptr;
1449 const wasm::WasmGlobalType *GlobalType = nullptr;
1450 const wasm::WasmTableType *TableType = nullptr;
1452 Info.Name = Ex.Name;
1453 Info.Flags = 0;
1454 switch (Ex.Kind) {
1456 if (!isValidFunctionIndex(Ex.Index))
1457 return make_error<GenericBinaryError>("invalid function export",
1460 Info.ElementIndex = Ex.Index;
1461 if (isDefinedFunctionIndex(Ex.Index)) {
1462 getDefinedFunction(Ex.Index).ExportName = Ex.Name;
1463 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions;
1464 wasm::WasmFunction &Function = Functions[FuncIndex];
1465 Signature = &Signatures[Function.SigIndex];
1466 }
1467 // Else the function is imported. LLVM object files don't use this
1468 // pattern and we still treat this as an undefined symbol, but we want to
1469 // parse it without crashing.
1470 break;
1471 }
1473 if (!isValidGlobalIndex(Ex.Index))
1474 return make_error<GenericBinaryError>("invalid global export",
1477 uint64_t Offset = 0;
1478 if (isDefinedGlobalIndex(Ex.Index)) {
1479 auto Global = getDefinedGlobal(Ex.Index);
1480 if (!Global.InitExpr.Extended) {
1481 auto Inst = Global.InitExpr.Inst;
1482 if (Inst.Opcode == wasm::WASM_OPCODE_I32_CONST) {
1483 Offset = Inst.Value.Int32;
1484 } else if (Inst.Opcode == wasm::WASM_OPCODE_I64_CONST) {
1485 Offset = Inst.Value.Int64;
1486 }
1487 }
1488 }
1489 Info.DataRef = wasm::WasmDataReference{0, Offset, 0};
1490 break;
1491 }
1493 if (!isValidTagIndex(Ex.Index))
1494 return make_error<GenericBinaryError>("invalid tag export",
1497 Info.ElementIndex = Ex.Index;
1498 break;
1500 break;
1503 Info.ElementIndex = Ex.Index;
1504 break;
1505 default:
1506 return make_error<GenericBinaryError>("unexpected export kind",
1508 }
1509 Exports.push_back(Ex);
1510 if (Ex.Kind != wasm::WASM_EXTERNAL_MEMORY) {
1511 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
1512 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
1513 }
1514 }
1515 if (Ctx.Ptr != Ctx.End)
1516 return make_error<GenericBinaryError>("export section ended prematurely",
1518 return Error::success();
1519}
1520
1521bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const {
1522 return Index < NumImportedFunctions + Functions.size();
1523}
1524
1525bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const {
1526 return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
1527}
1528
1529bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const {
1530 return Index < NumImportedGlobals + Globals.size();
1531}
1532
1533bool WasmObjectFile::isValidTableNumber(uint32_t Index) const {
1534 return Index < NumImportedTables + Tables.size();
1535}
1536
1537bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const {
1538 return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
1539}
1540
1541bool WasmObjectFile::isDefinedTableNumber(uint32_t Index) const {
1542 return Index >= NumImportedTables && isValidTableNumber(Index);
1543}
1544
1545bool WasmObjectFile::isValidTagIndex(uint32_t Index) const {
1546 return Index < NumImportedTags + Tags.size();
1547}
1548
1549bool WasmObjectFile::isDefinedTagIndex(uint32_t Index) const {
1550 return Index >= NumImportedTags && isValidTagIndex(Index);
1551}
1552
1553bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const {
1554 return Index < Symbols.size() && Symbols[Index].isTypeFunction();
1555}
1556
1557bool WasmObjectFile::isValidTableSymbol(uint32_t Index) const {
1558 return Index < Symbols.size() && Symbols[Index].isTypeTable();
1559}
1560
1561bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const {
1562 return Index < Symbols.size() && Symbols[Index].isTypeGlobal();
1563}
1564
1565bool WasmObjectFile::isValidTagSymbol(uint32_t Index) const {
1566 return Index < Symbols.size() && Symbols[Index].isTypeTag();
1567}
1568
1569bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const {
1570 return Index < Symbols.size() && Symbols[Index].isTypeData();
1571}
1572
1573bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const {
1574 return Index < Symbols.size() && Symbols[Index].isTypeSection();
1575}
1576
1577wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
1578 assert(isDefinedFunctionIndex(Index));
1579 return Functions[Index - NumImportedFunctions];
1580}
1581
1582const wasm::WasmFunction &
1583WasmObjectFile::getDefinedFunction(uint32_t Index) const {
1584 assert(isDefinedFunctionIndex(Index));
1585 return Functions[Index - NumImportedFunctions];
1586}
1587
1588const wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) const {
1589 assert(isDefinedGlobalIndex(Index));
1590 return Globals[Index - NumImportedGlobals];
1591}
1592
1593wasm::WasmTag &WasmObjectFile::getDefinedTag(uint32_t Index) {
1594 assert(isDefinedTagIndex(Index));
1595 return Tags[Index - NumImportedTags];
1596}
1597
1598Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
1599 StartFunction = readVaruint32(Ctx);
1600 if (!isValidFunctionIndex(StartFunction))
1601 return make_error<GenericBinaryError>("invalid start function",
1603 return Error::success();
1604}
1605
1606Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) {
1607 CodeSection = Sections.size();
1608 uint32_t FunctionCount = readVaruint32(Ctx);
1609 if (FunctionCount != Functions.size()) {
1610 return make_error<GenericBinaryError>("invalid function count",
1612 }
1613
1614 for (uint32_t i = 0; i < FunctionCount; i++) {
1615 wasm::WasmFunction& Function = Functions[i];
1616 const uint8_t *FunctionStart = Ctx.Ptr;
1618 const uint8_t *FunctionEnd = Ctx.Ptr + Size;
1619
1620 Function.CodeOffset = Ctx.Ptr - FunctionStart;
1621 Function.Index = NumImportedFunctions + i;
1622 Function.CodeSectionOffset = FunctionStart - Ctx.Start;
1623 Function.Size = FunctionEnd - FunctionStart;
1624
1625 uint32_t NumLocalDecls = readVaruint32(Ctx);
1626 Function.Locals.reserve(NumLocalDecls);
1627 while (NumLocalDecls--) {
1629 Decl.Count = readVaruint32(Ctx);
1630 Decl.Type = readUint8(Ctx);
1631 Function.Locals.push_back(Decl);
1632 }
1633
1634 uint32_t BodySize = FunctionEnd - Ctx.Ptr;
1635 // Ensure that Function is within Ctx's buffer.
1636 if (Ctx.Ptr + BodySize > Ctx.End) {
1637 return make_error<GenericBinaryError>("Function extends beyond buffer",
1639 }
1640 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize);
1641 // This will be set later when reading in the linking metadata section.
1642 Function.Comdat = UINT32_MAX;
1643 Ctx.Ptr += BodySize;
1644 assert(Ctx.Ptr == FunctionEnd);
1645 }
1646 if (Ctx.Ptr != Ctx.End)
1647 return make_error<GenericBinaryError>("code section ended prematurely",
1649 return Error::success();
1650}
1651
1652Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
1653 uint32_t Count = readVaruint32(Ctx);
1654 ElemSegments.reserve(Count);
1655 while (Count--) {
1656 wasm::WasmElemSegment Segment;
1657 Segment.Flags = readVaruint32(Ctx);
1658
1662 if (Segment.Flags & ~SupportedFlags)
1663 return make_error<GenericBinaryError>(
1664 "Unsupported flags for element segment", object_error::parse_failed);
1665
1667 if ((Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_PASSIVE) == 0) {
1669 } else if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_DECLARATIVE) {
1671 } else {
1673 }
1674 bool HasTableNumber =
1677 bool HasElemKind =
1680 bool HasElemType =
1683 bool HasInitExprs =
1685
1686 if (HasTableNumber)
1687 Segment.TableNumber = readVaruint32(Ctx);
1688 else
1689 Segment.TableNumber = 0;
1690
1691 if (!isValidTableNumber(Segment.TableNumber))
1692 return make_error<GenericBinaryError>("invalid TableNumber",
1694
1695 if (Mode != wasm::ElemSegmentMode::Active) {
1696 Segment.Offset.Extended = false;
1698 Segment.Offset.Inst.Value.Int32 = 0;
1699 } else {
1700 if (Error Err = readInitExpr(Segment.Offset, Ctx))
1701 return Err;
1702 }
1703
1704 if (HasElemKind) {
1705 auto ElemKind = readVaruint32(Ctx);
1707 Segment.ElemKind = parseValType(Ctx, ElemKind);
1708 if (Segment.ElemKind != wasm::ValType::FUNCREF &&
1710 Segment.ElemKind != wasm::ValType::EXNREF &&
1711 Segment.ElemKind != wasm::ValType::OTHERREF) {
1712 return make_error<GenericBinaryError>("invalid elem type",
1714 }
1715 } else {
1716 if (ElemKind != 0)
1717 return make_error<GenericBinaryError>("invalid elem type",
1720 }
1721 } else if (HasElemType) {
1722 auto ElemType = parseValType(Ctx, readVaruint32(Ctx));
1723 Segment.ElemKind = ElemType;
1724 } else {
1726 }
1727
1728 uint32_t NumElems = readVaruint32(Ctx);
1729
1730 if (HasInitExprs) {
1731 while (NumElems--) {
1732 wasm::WasmInitExpr Expr;
1733 if (Error Err = readInitExpr(Expr, Ctx))
1734 return Err;
1735 }
1736 } else {
1737 while (NumElems--) {
1738 Segment.Functions.push_back(readVaruint32(Ctx));
1739 }
1740 }
1741 ElemSegments.push_back(Segment);
1742 }
1743 if (Ctx.Ptr != Ctx.End)
1744 return make_error<GenericBinaryError>("elem section ended prematurely",
1746 return Error::success();
1747}
1748
1749Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
1750 DataSection = Sections.size();
1751 uint32_t Count = readVaruint32(Ctx);
1752 if (DataCount && Count != *DataCount)
1753 return make_error<GenericBinaryError>(
1754 "number of data segments does not match DataCount section");
1755 DataSegments.reserve(Count);
1756 while (Count--) {
1757 WasmSegment Segment;
1758 Segment.Data.InitFlags = readVaruint32(Ctx);
1759 Segment.Data.MemoryIndex =
1761 ? readVaruint32(Ctx)
1762 : 0;
1763 if ((Segment.Data.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
1764 if (Error Err = readInitExpr(Segment.Data.Offset, Ctx))
1765 return Err;
1766 } else {
1767 Segment.Data.Offset.Extended = false;
1769 Segment.Data.Offset.Inst.Value.Int32 = 0;
1770 }
1772 if (Size > (size_t)(Ctx.End - Ctx.Ptr))
1773 return make_error<GenericBinaryError>("invalid segment size",
1775 Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size);
1776 // The rest of these Data fields are set later, when reading in the linking
1777 // metadata section.
1778 Segment.Data.Alignment = 0;
1779 Segment.Data.LinkingFlags = 0;
1780 Segment.Data.Comdat = UINT32_MAX;
1781 Segment.SectionOffset = Ctx.Ptr - Ctx.Start;
1782 Ctx.Ptr += Size;
1783 DataSegments.push_back(Segment);
1784 }
1785 if (Ctx.Ptr != Ctx.End)
1786 return make_error<GenericBinaryError>("data section ended prematurely",
1788 return Error::success();
1789}
1790
1791Error WasmObjectFile::parseDataCountSection(ReadContext &Ctx) {
1792 DataCount = readVaruint32(Ctx);
1793 return Error::success();
1794}
1795
1797 return Header;
1798}
1799
1800void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.b++; }
1801
1804 const WasmSymbol &Sym = getWasmSymbol(Symb);
1805
1806 LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n");
1807 if (Sym.isBindingWeak())
1808 Result |= SymbolRef::SF_Weak;
1809 if (!Sym.isBindingLocal())
1810 Result |= SymbolRef::SF_Global;
1811 if (Sym.isHidden())
1812 Result |= SymbolRef::SF_Hidden;
1813 if (!Sym.isDefined())
1814 Result |= SymbolRef::SF_Undefined;
1815 if (Sym.isTypeFunction())
1816 Result |= SymbolRef::SF_Executable;
1817 return Result;
1818}
1819
1822 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null
1823 Ref.d.b = 0; // Symbol index
1824 return BasicSymbolRef(Ref, this);
1825}
1826
1829 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null
1830 Ref.d.b = Symbols.size(); // Symbol index
1831 return BasicSymbolRef(Ref, this);
1832}
1833
1835 return Symbols[Symb.d.b];
1836}
1837
1839 return getWasmSymbol(Symb.getRawDataRefImpl());
1840}
1841
1843 return getWasmSymbol(Symb).Info.Name;
1844}
1845
1847 auto &Sym = getWasmSymbol(Symb);
1848 if (!Sym.isDefined())
1849 return 0;
1851 if (!Sec)
1852 return Sec.takeError();
1853 uint32_t SectionAddress = getSectionAddress(Sec.get()->getRawDataRefImpl());
1854 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION &&
1855 isDefinedFunctionIndex(Sym.Info.ElementIndex)) {
1856 return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset +
1857 SectionAddress;
1858 }
1859 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL &&
1860 isDefinedGlobalIndex(Sym.Info.ElementIndex)) {
1861 return getDefinedGlobal(Sym.Info.ElementIndex).Offset + SectionAddress;
1862 }
1863
1864 return getSymbolValue(Symb);
1865}
1866
1868 switch (Sym.Info.Kind) {
1873 return Sym.Info.ElementIndex;
1875 // The value of a data symbol is the segment offset, plus the symbol
1876 // offset within the segment.
1877 uint32_t SegmentIndex = Sym.Info.DataRef.Segment;
1878 const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data;
1879 if (Segment.Offset.Extended) {
1880 llvm_unreachable("extended init exprs not supported");
1881 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_I32_CONST) {
1882 return Segment.Offset.Inst.Value.Int32 + Sym.Info.DataRef.Offset;
1883 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_I64_CONST) {
1884 return Segment.Offset.Inst.Value.Int64 + Sym.Info.DataRef.Offset;
1885 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_GLOBAL_GET) {
1886 return Sym.Info.DataRef.Offset;
1887 } else {
1888 llvm_unreachable("unknown init expr opcode");
1889 }
1890 }
1892 return 0;
1893 }
1894 llvm_unreachable("invalid symbol type");
1895}
1896
1898 return getWasmSymbolValue(getWasmSymbol(Symb));
1899}
1900
1902 llvm_unreachable("not yet implemented");
1903 return 0;
1904}
1905
1907 llvm_unreachable("not yet implemented");
1908 return 0;
1909}
1910
1913 const WasmSymbol &Sym = getWasmSymbol(Symb);
1914
1915 switch (Sym.Info.Kind) {
1919 return SymbolRef::ST_Other;
1921 return SymbolRef::ST_Data;
1923 return SymbolRef::ST_Debug;
1925 return SymbolRef::ST_Other;
1927 return SymbolRef::ST_Other;
1928 }
1929
1930 llvm_unreachable("unknown WasmSymbol::SymbolType");
1931 return SymbolRef::ST_Other;
1932}
1933
1936 const WasmSymbol &Sym = getWasmSymbol(Symb);
1937 if (Sym.isUndefined())
1938 return section_end();
1939
1941 Ref.d.a = getSymbolSectionIdImpl(Sym);
1942 return section_iterator(SectionRef(Ref, this));
1943}
1944
1946 const WasmSymbol &Sym = getWasmSymbol(Symb);
1947 return getSymbolSectionIdImpl(Sym);
1948}
1949
1950uint32_t WasmObjectFile::getSymbolSectionIdImpl(const WasmSymbol &Sym) const {
1951 switch (Sym.Info.Kind) {
1953 return CodeSection;
1955 return GlobalSection;
1957 return DataSection;
1959 return Sym.Info.ElementIndex;
1961 return TagSection;
1963 return TableSection;
1964 default:
1965 llvm_unreachable("unknown WasmSymbol::SymbolType");
1966 }
1967}
1968
1970 const WasmSymbol &Sym = getWasmSymbol(Symb);
1971 if (!Sym.isDefined())
1972 return 0;
1973 if (Sym.isTypeGlobal())
1974 return getDefinedGlobal(Sym.Info.ElementIndex).Size;
1975 if (Sym.isTypeData())
1976 return Sym.Info.DataRef.Size;
1977 if (Sym.isTypeFunction())
1978 return functions()[Sym.Info.ElementIndex - getNumImportedFunctions()].Size;
1979 // Currently symbol size is only tracked for data segments and functions. In
1980 // principle we could also track size (e.g. binary size) for tables, globals
1981 // and element segments etc too.
1982 return 0;
1983}
1984
1986
1988 const WasmSection &S = Sections[Sec.d.a];
1989 if (S.Type == wasm::WASM_SEC_CUSTOM)
1990 return S.Name;
1994}
1995
1997 // For object files, use 0 for section addresses, and section offsets for
1998 // symbol addresses. For linked files, use file offsets.
1999 // See also getSymbolAddress.
2000 return isRelocatableObject() || isSharedObject() ? 0
2001 : Sections[Sec.d.a].Offset;
2002}
2003
2005 return Sec.d.a;
2006}
2007
2009 const WasmSection &S = Sections[Sec.d.a];
2010 return S.Content.size();
2011}
2012
2015 const WasmSection &S = Sections[Sec.d.a];
2016 // This will never fail since wasm sections can never be empty (user-sections
2017 // must have a name and non-user sections each have a defined structure).
2018 return S.Content;
2019}
2020
2022 return 1;
2023}
2024
2026 return false;
2027}
2028
2031}
2032
2035}
2036
2037bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; }
2038
2039bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; }
2040
2042 DataRefImpl RelocRef;
2043 RelocRef.d.a = Ref.d.a;
2044 RelocRef.d.b = 0;
2045 return relocation_iterator(RelocationRef(RelocRef, this));
2046}
2047
2049 const WasmSection &Sec = getWasmSection(Ref);
2050 DataRefImpl RelocRef;
2051 RelocRef.d.a = Ref.d.a;
2052 RelocRef.d.b = Sec.Relocations.size();
2053 return relocation_iterator(RelocationRef(RelocRef, this));
2054}
2055
2057
2060 return Rel.Offset;
2061}
2062
2065 if (Rel.Type == wasm::R_WASM_TYPE_INDEX_LEB)
2066 return symbol_end();
2068 Sym.d.a = 1;
2069 Sym.d.b = Rel.Index;
2070 return symbol_iterator(SymbolRef(Sym, this));
2071}
2072
2075 return Rel.Type;
2076}
2077
2079 DataRefImpl Ref, SmallVectorImpl<char> &Result) const {
2081 StringRef Res = "Unknown";
2082
2083#define WASM_RELOC(name, value) \
2084 case wasm::name: \
2085 Res = #name; \
2086 break;
2087
2088 switch (Rel.Type) {
2089#include "llvm/BinaryFormat/WasmRelocs.def"
2090 }
2091
2092#undef WASM_RELOC
2093
2094 Result.append(Res.begin(), Res.end());
2095}
2096
2099 Ref.d.a = 0;
2100 return section_iterator(SectionRef(Ref, this));
2101}
2102
2105 Ref.d.a = Sections.size();
2106 return section_iterator(SectionRef(Ref, this));
2107}
2108
2110 return HasMemory64 ? 8 : 4;
2111}
2112
2114
2116 return HasMemory64 ? Triple::wasm64 : Triple::wasm32;
2117}
2118
2120 return SubtargetFeatures();
2121}
2122
2123bool WasmObjectFile::isRelocatableObject() const { return HasLinkingSection; }
2124
2125bool WasmObjectFile::isSharedObject() const { return HasDylinkSection; }
2126
2128 assert(Ref.d.a < Sections.size());
2129 return Sections[Ref.d.a];
2130}
2131
2132const WasmSection &
2134 return getWasmSection(Section.getRawDataRefImpl());
2135}
2136
2139 return getWasmRelocation(Ref.getRawDataRefImpl());
2140}
2141
2144 assert(Ref.d.a < Sections.size());
2145 const WasmSection &Sec = Sections[Ref.d.a];
2146 assert(Ref.d.b < Sec.Relocations.size());
2147 return Sec.Relocations[Ref.d.b];
2148}
2149
2150int WasmSectionOrderChecker::getSectionOrder(unsigned ID,
2151 StringRef CustomSectionName) {
2152 switch (ID) {
2154 return StringSwitch<unsigned>(CustomSectionName)
2155 .Case("dylink", WASM_SEC_ORDER_DYLINK)
2156 .Case("dylink.0", WASM_SEC_ORDER_DYLINK)
2157 .Case("linking", WASM_SEC_ORDER_LINKING)
2159 .Case("name", WASM_SEC_ORDER_NAME)
2160 .Case("producers", WASM_SEC_ORDER_PRODUCERS)
2161 .Case("target_features", WASM_SEC_ORDER_TARGET_FEATURES)
2164 return WASM_SEC_ORDER_TYPE;
2166 return WASM_SEC_ORDER_IMPORT;
2170 return WASM_SEC_ORDER_TABLE;
2172 return WASM_SEC_ORDER_MEMORY;
2174 return WASM_SEC_ORDER_GLOBAL;
2176 return WASM_SEC_ORDER_EXPORT;
2178 return WASM_SEC_ORDER_START;
2180 return WASM_SEC_ORDER_ELEM;
2182 return WASM_SEC_ORDER_CODE;
2184 return WASM_SEC_ORDER_DATA;
2187 case wasm::WASM_SEC_TAG:
2188 return WASM_SEC_ORDER_TAG;
2189 default:
2190 return WASM_SEC_ORDER_NONE;
2191 }
2192}
2193
2194// Represents the edges in a directed graph where any node B reachable from node
2195// A is not allowed to appear before A in the section ordering, but may appear
2196// afterward.
2198 [WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = {
2199 // WASM_SEC_ORDER_NONE
2200 {},
2201 // WASM_SEC_ORDER_TYPE
2202 {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT},
2203 // WASM_SEC_ORDER_IMPORT
2204 {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION},
2205 // WASM_SEC_ORDER_FUNCTION
2206 {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE},
2207 // WASM_SEC_ORDER_TABLE
2208 {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY},
2209 // WASM_SEC_ORDER_MEMORY
2210 {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_TAG},
2211 // WASM_SEC_ORDER_TAG
2212 {WASM_SEC_ORDER_TAG, WASM_SEC_ORDER_GLOBAL},
2213 // WASM_SEC_ORDER_GLOBAL
2214 {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EXPORT},
2215 // WASM_SEC_ORDER_EXPORT
2216 {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START},
2217 // WASM_SEC_ORDER_START
2218 {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM},
2219 // WASM_SEC_ORDER_ELEM
2220 {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT},
2221 // WASM_SEC_ORDER_DATACOUNT
2222 {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE},
2223 // WASM_SEC_ORDER_CODE
2224 {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA},
2225 // WASM_SEC_ORDER_DATA
2226 {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING},
2227
2228 // Custom Sections
2229 // WASM_SEC_ORDER_DYLINK
2230 {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE},
2231 // WASM_SEC_ORDER_LINKING
2232 {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME},
2233 // WASM_SEC_ORDER_RELOC (can be repeated)
2234 {},
2235 // WASM_SEC_ORDER_NAME
2236 {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS},
2237 // WASM_SEC_ORDER_PRODUCERS
2238 {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES},
2239 // WASM_SEC_ORDER_TARGET_FEATURES
2240 {WASM_SEC_ORDER_TARGET_FEATURES}};
2241
2243 StringRef CustomSectionName) {
2244 int Order = getSectionOrder(ID, CustomSectionName);
2245 if (Order == WASM_SEC_ORDER_NONE)
2246 return true;
2247
2248 // Disallowed predecessors we need to check for
2250
2251 // Keep track of completed checks to avoid repeating work
2252 bool Checked[WASM_NUM_SEC_ORDERS] = {};
2253
2254 int Curr = Order;
2255 while (true) {
2256 // Add new disallowed predecessors to work list
2257 for (size_t I = 0;; ++I) {
2258 int Next = DisallowedPredecessors[Curr][I];
2259 if (Next == WASM_SEC_ORDER_NONE)
2260 break;
2261 if (Checked[Next])
2262 continue;
2263 WorkList.push_back(Next);
2264 Checked[Next] = true;
2265 }
2266
2267 if (WorkList.empty())
2268 break;
2269
2270 // Consider next disallowed predecessor
2271 Curr = WorkList.pop_back_val();
2272 if (Seen[Curr])
2273 return false;
2274 }
2275
2276 // Have not seen any disallowed predecessors
2277 Seen[Order] = true;
2278 return true;
2279}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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:638
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
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 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:147
const T * data() const
Definition: ArrayRef.h:144
Implements a dense probed hash-table based set.
Definition: DenseSet.h:263
Helper for Errors used as out-parameters.
Definition: Error.h:1144
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
bool empty() const
Definition: Function.h:857
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:134
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:182
bool empty() const
Definition: SmallVector.h:82
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void reserve(size_type N)
Definition: SmallVector.h:664
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:581
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:269
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:151
iterator begin() const
Definition: StringRef.h:120
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:154
iterator end() const
Definition: StringRef.h:122
const unsigned char * bytes_begin() const
Definition: StringRef.h:132
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:25
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:39
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:68
R Default(T Value)
Definition: StringSwitch.h:177
StringSwitch & StartsWith(StringLiteral S, T Value)
Definition: StringSwitch.h:80
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(const uint64_t &Val)
Definition: Twine.h:418
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:194
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: SymbolicFile.h:104
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:211
MemoryBufferRef Data
Definition: Binary.h:38
StringRef getData() const
Definition: Binary.cpp:39
This class is the base class for all object file types.
Definition: ObjectFile.h:231
friend class RelocationRef
Definition: ObjectFile.h:289
friend class SymbolRef
Definition: ObjectFile.h:249
friend class SectionRef
Definition: ObjectFile.h:263
static Expected< std::unique_ptr< WasmObjectFile > > createWasmObjectFile(MemoryBufferRef Object)
Expected< uint64_t > getSymbolValue(DataRefImpl Symb) const
Definition: ObjectFile.cpp:57
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:54
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:83
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:170
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:358
unsigned getBinding() const
Definition: Wasm.h:90
LLVM_DUMP_METHOD void dump() const
bool isTypeData() const
Definition: Wasm.h:60
bool isHidden() const
Definition: Wasm.h:94
wasm::WasmSymbolInfo Info
Definition: Wasm.h:49
LLVM_ABI void print(raw_ostream &Out) const
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.
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:49
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:79
uint32_t read32le(const void *P)
Definition: Endian.h:429
@ 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_UNDEFINED
Definition: Wasm.h:247
@ WASM_DATA_SEGMENT_IS_PASSIVE
Definition: Wasm.h:166
@ WASM_DATA_SEGMENT_HAS_MEMINDEX
Definition: Wasm.h:167
@ WASM_OPCODE_I64_ADD
Definition: Wasm.h:111
@ WASM_OPCODE_I32_SUB
Definition: Wasm.h:109
@ WASM_OPCODE_F64_CONST
Definition: Wasm.h:107
@ WASM_OPCODE_END
Definition: Wasm.h:95
@ WASM_OPCODE_I64_MUL
Definition: Wasm.h:113
@ WASM_OPCODE_REF_NULL
Definition: Wasm.h:114
@ WASM_OPCODE_GC_PREFIX
Definition: Wasm.h:116
@ WASM_OPCODE_REF_FUNC
Definition: Wasm.h:115
@ WASM_OPCODE_F32_CONST
Definition: Wasm.h:106
@ WASM_OPCODE_GLOBAL_GET
Definition: Wasm.h:100
@ WASM_OPCODE_I64_SUB
Definition: Wasm.h:112
@ WASM_OPCODE_I32_MUL
Definition: Wasm.h:110
@ WASM_OPCODE_I32_ADD
Definition: Wasm.h:108
@ WASM_OPCODE_I64_CONST
Definition: Wasm.h:105
@ WASM_OPCODE_I32_CONST
Definition: Wasm.h:104
@ WASM_LIMITS_FLAG_HAS_MAX
Definition: Wasm.h:159
@ WASM_LIMITS_FLAG_IS_64
Definition: Wasm.h:161
@ WASM_LIMITS_FLAG_HAS_PAGE_SIZE
Definition: Wasm.h:162
@ 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_GLOBAL
Definition: Wasm.h:242
@ WASM_DYLINK_RUNTIME_PATH
Definition: Wasm.h:207
@ WASM_DYLINK_NEEDED
Definition: Wasm.h:204
@ WASM_DYLINK_MEM_INFO
Definition: Wasm.h:203
@ WASM_DYLINK_EXPORT_INFO
Definition: Wasm.h:205
@ WASM_DYLINK_IMPORT_INFO
Definition: Wasm.h:206
const uint32_t WasmMetadataVersion
Definition: Wasm.h:31
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:243
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:244
@ WASM_ELEM_SEGMENT_HAS_INIT_EXPRS
Definition: Wasm.h:174
@ WASM_ELEM_SEGMENT_IS_DECLARATIVE
Definition: Wasm.h:172
@ WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER
Definition: Wasm.h:173
@ WASM_ELEM_SEGMENT_IS_PASSIVE
Definition: Wasm.h:171
WasmSymbolType
Definition: Wasm.h:219
@ WASM_SYMBOL_TYPE_GLOBAL
Definition: Wasm.h:222
@ WASM_SYMBOL_TYPE_DATA
Definition: Wasm.h:221
@ WASM_SYMBOL_TYPE_TAG
Definition: Wasm.h:224
@ WASM_SYMBOL_TYPE_TABLE
Definition: Wasm.h:225
@ WASM_SYMBOL_TYPE_SECTION
Definition: Wasm.h:223
@ WASM_SYMBOL_TYPE_FUNCTION
Definition: Wasm.h:220
const uint32_t WasmVersion
Definition: Wasm.h:29
ElemSegmentMode
Definition: Wasm.h:426
@ WASM_EXTERNAL_TABLE
Definition: Wasm.h:87
@ WASM_EXTERNAL_FUNCTION
Definition: Wasm.h:86
@ WASM_EXTERNAL_TAG
Definition: Wasm.h:90
@ WASM_EXTERNAL_MEMORY
Definition: Wasm.h:88
@ WASM_EXTERNAL_GLOBAL
Definition: Wasm.h:89
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:239
NameType
Definition: Wasm.h:483
@ WASM_OPCODE_ARRAY_NEW_FIXED
Definition: Wasm.h:125
@ WASM_OPCODE_REF_I31
Definition: Wasm.h:126
@ WASM_OPCODE_ARRAY_NEW_DEFAULT
Definition: Wasm.h:124
@ WASM_OPCODE_STRUCT_NEW
Definition: Wasm.h:121
@ WASM_OPCODE_STRUCT_NEW_DEFAULT
Definition: Wasm.h:122
@ WASM_OPCODE_ARRAY_NEW
Definition: Wasm.h:123
@ WASM_FEATURE_PREFIX_USED
Definition: Wasm.h:180
@ WASM_FEATURE_PREFIX_DISALLOWED
Definition: Wasm.h:181
LLVM_ABI llvm::StringRef sectionTypeToString(uint32_t type)
Definition: Wasm.cpp:41
@ WASM_COMDAT_SECTION
Definition: Wasm.h:215
@ WASM_COMDAT_FUNCTION
Definition: Wasm.h:213
@ WASM_COMDAT_DATA
Definition: Wasm.h:212
@ WASM_INIT_FUNCS
Definition: Wasm.h:196
@ WASM_COMDAT_INFO
Definition: Wasm.h:197
@ WASM_SEGMENT_INFO
Definition: Wasm.h:195
@ WASM_SYMBOL_TABLE
Definition: Wasm.h:198
const unsigned WASM_SYMBOL_EXPLICIT_NAME
Definition: Wasm.h:249
const unsigned WASM_SYMBOL_ABSOLUTE
Definition: Wasm.h:252
@ WASM_NAMES_LOCAL
Definition: Wasm.h:188
@ WASM_NAMES_DATA_SEGMENT
Definition: Wasm.h:190
@ WASM_NAMES_GLOBAL
Definition: Wasm.h:189
@ WASM_NAMES_FUNCTION
Definition: Wasm.h:187
const unsigned WASM_ELEM_SEGMENT_MASK_HAS_ELEM_DESC
Definition: Wasm.h:176
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
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:132
@ 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:166
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1305
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition: Error.cpp:167
const char * to_string(ThinOrFullLTOPhase Phase)
Definition: Pass.cpp:301
@ 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:116
std::vector< wasm::WasmRelocation > Relocations
Definition: Wasm.h:117
wasm::WasmDataSegment Data
Definition: Wasm.h:124
uint32_t SectionOffset
Definition: Wasm.h:123
ArrayRef< uint8_t > Content
Definition: Wasm.h:417
WasmInitExpr Offset
Definition: Wasm.h:415
uint32_t MemoryAlignment
Definition: Wasm.h:295
std::vector< StringRef > Needed
Definition: Wasm.h:298
std::vector< StringRef > RuntimePath
Definition: Wasm.h:301
std::vector< WasmDylinkExportInfo > ExportInfo
Definition: Wasm.h:300
std::vector< WasmDylinkImportInfo > ImportInfo
Definition: Wasm.h:299
uint32_t TableAlignment
Definition: Wasm.h:297
WasmInitExpr Offset
Definition: Wasm.h:438
std::vector< uint32_t > Functions
Definition: Wasm.h:439
StringRef Name
Definition: Wasm.h:316
uint32_t Index
Definition: Wasm.h:318
std::optional< StringRef > ExportName
Definition: Wasm.h:404
uint32_t CodeSectionOffset
Definition: Wasm.h:401
uint32_t Offset
Definition: Wasm.h:369
WasmGlobalType Type
Definition: Wasm.h:366
WasmLimits Memory
Definition: Wasm.h:387
StringRef Field
Definition: Wasm.h:381
WasmGlobalType Global
Definition: Wasm.h:385
StringRef Module
Definition: Wasm.h:380
uint32_t SigIndex
Definition: Wasm.h:384
WasmTableType Table
Definition: Wasm.h:386
union llvm::wasm::WasmInitExprMVP::@188 Value
WasmInitExprMVP Inst
Definition: Wasm.h:355
ArrayRef< uint8_t > Body
Definition: Wasm.h:356
std::vector< WasmInitFunc > InitFunctions
Definition: Wasm.h:498
std::vector< StringRef > Comdats
Definition: Wasm.h:499
std::vector< std::pair< std::string, std::string > > SDKs
Definition: Wasm.h:307
std::vector< std::pair< std::string, std::string > > Languages
Definition: Wasm.h:305
std::vector< std::pair< std::string, std::string > > Tools
Definition: Wasm.h:306
enum llvm::wasm::WasmSignature::@193 Kind
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:507
SmallVector< ValType, 4 > Params
Definition: Wasm.h:508
WasmDataReference DataRef
Definition: Wasm.h:479
WasmLimits Limits
Definition: Wasm.h:330
WasmTableType Type
Definition: Wasm.h:335
StringRef SymbolName
Definition: Wasm.h:336
struct llvm::object::DataRefImpl::@378 d