32#define DEBUG_TYPE "wasm-object"
38 Out <<
"Name=" <<
Info.Name
62 Out <<
", ElemIndex=" <<
Info.ElementIndex;
64 Out <<
", Segment=" <<
Info.DataRef.Segment;
65 Out <<
", Offset=" <<
Info.DataRef.Offset;
66 Out <<
", Size=" <<
Info.DataRef.Size;
70#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
77 auto ObjectFile = std::make_unique<WasmObjectFile>(Buffer, Err);
79 return std::move(Err);
84#define VARINT7_MAX ((1 << 7) - 1)
85#define VARINT7_MIN (-(1 << 7))
86#define VARUINT7_MAX (1 << 7)
87#define VARUINT1_MAX (1)
90 if (Ctx.Ptr == Ctx.End)
96 if (Ctx.Ptr + 4 > Ctx.End)
104 if (Ctx.Ptr + 4 > Ctx.End)
107 memcpy(&Result, Ctx.Ptr,
sizeof(Result));
108 Ctx.Ptr +=
sizeof(Result);
113 if (Ctx.Ptr + 8 > Ctx.End)
116 memcpy(&Result, Ctx.Ptr,
sizeof(Result));
117 Ctx.Ptr +=
sizeof(Result);
123 const char *
Error =
nullptr;
133 if (Ctx.Ptr + StringLen > Ctx.End)
136 StringRef(
reinterpret_cast<const char *
>(Ctx.Ptr), StringLen);
137 Ctx.Ptr += StringLen;
143 const char *
Error =
nullptr;
160 if (Result > INT32_MAX || Result < INT32_MIN)
167 if (Result > UINT32_MAX)
207 auto Start = Ctx.Ptr;
288 Twine(
unsigned(Opcode)),
305 if (PageSizeLog2 >= 32)
307 Result.PageSize = 1 << PageSizeLog2;
315 TableType.ElemType = ElemType;
323 LLVM_DEBUG(
dbgs() <<
"readSection type=" << Section.Type <<
"\n");
326 const uint8_t *PreSizePtr = Ctx.Ptr;
328 Section.HeaderSecSizeEncodingLen = Ctx.Ptr - PreSizePtr;
329 Section.Offset = Ctx.Ptr - Ctx.Start;
333 if (Ctx.Ptr +
Size > Ctx.End)
338 SectionCtx.
Start = Ctx.Ptr;
339 SectionCtx.
Ptr = Ctx.Ptr;
340 SectionCtx.
End = Ctx.Ptr +
Size;
345 Ctx.Ptr += SectionNameSize;
346 Size -= SectionNameSize;
364 if (Header.Magic !=
StringRef(
"\0asm", 4)) {
372 Ctx.Ptr = Ctx.Start + 4;
375 if (Ctx.Ptr + 4 > Ctx.End) {
384 Twine(Header.Version),
390 while (Ctx.Ptr < Ctx.End) {
394 if ((Err = parseSection(Sec)))
397 Sections.push_back(Sec);
408 return parseCustomSection(Sec, Ctx);
410 return parseTypeSection(Ctx);
412 return parseImportSection(Ctx);
414 return parseFunctionSection(Ctx);
416 return parseTableSection(Ctx);
418 return parseMemorySection(Ctx);
420 return parseTagSection(Ctx);
422 return parseGlobalSection(Ctx);
424 return parseExportSection(Ctx);
426 return parseStartSection(Ctx);
428 return parseElemSection(Ctx);
430 return parseCodeSection(Ctx);
432 return parseDataSection(Ctx);
434 return parseDataCountSection(Ctx);
441Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) {
444 HasDylinkSection =
true;
454 if (Ctx.Ptr != Ctx.End)
460Error WasmObjectFile::parseDylink0Section(ReadContext &Ctx) {
463 HasDylinkSection =
true;
465 const uint8_t *OrigEnd = Ctx.End;
466 while (Ctx.Ptr < OrigEnd) {
472 Ctx.End = Ctx.Ptr +
Size;
497 DylinkInfo.ImportInfo.push_back(
505 DylinkInfo.RuntimePath.push_back(
readString(Ctx));
514 if (Ctx.Ptr != Ctx.End) {
520 if (Ctx.Ptr != Ctx.End)
526Error WasmObjectFile::parseNameSection(ReadContext &Ctx) {
527 llvm::DenseSet<uint64_t> SeenFunctions;
528 llvm::DenseSet<uint64_t> SeenGlobals;
529 llvm::DenseSet<uint64_t> SeenSegments;
533 bool PopulateSymbolTable = !HasLinkingSection && !HasDylinkSection;
537 if (PopulateSymbolTable)
540 while (Ctx.Ptr < Ctx.End) {
543 const uint8_t *SubSectionEnd = Ctx.Ptr +
Size;
561 const wasm::WasmSignature *Signature =
nullptr;
562 const wasm::WasmGlobalType *GlobalType =
nullptr;
563 const wasm::WasmTableType *TableType =
nullptr;
565 if (!SeenFunctions.
insert(Index).second)
568 if (!isValidFunctionIndex(Index) ||
Name.empty())
572 if (isDefinedFunctionIndex(Index)) {
573 wasm::WasmFunction &
F = getDefinedFunction(Index);
575 Signature = &Signatures[
F.SigIndex];
577 Info.ExportName =
F.ExportName;
586 if (!SeenGlobals.
insert(Index).second)
589 if (!isValidGlobalIndex(Index) ||
Name.empty())
594 if (isDefinedGlobalIndex(Index)) {
595 GlobalType = &getDefinedGlobal(Index).
Type;
600 if (!SeenSegments.
insert(Index).second)
603 if (Index >= DataSegments.size())
609 assert(Index < DataSegments.size());
610 Info.DataRef = wasm::WasmDataReference{
611 Index, 0, DataSegments[
Index].Data.Content.size()};
613 DebugNames.push_back(wasm::WasmDebugName{nameType,
Index,
Name});
614 if (PopulateSymbolTable)
615 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
625 if (Ctx.Ptr != SubSectionEnd)
630 if (Ctx.Ptr != Ctx.End)
636Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) {
637 HasLinkingSection =
true;
642 "unexpected metadata version: " + Twine(LinkingData.Version) +
647 const uint8_t *OrigEnd = Ctx.End;
648 while (Ctx.Ptr < OrigEnd) {
654 Ctx.End = Ctx.Ptr +
Size;
657 if (
Error Err = parseLinkingSectionSymtab(Ctx))
662 if (
Count > DataSegments.size())
665 for (uint32_t
I = 0;
I <
Count;
I++) {
674 LinkingData.InitFunctions.reserve(
Count);
675 for (uint32_t
I = 0;
I <
Count;
I++) {
676 wasm::WasmInitFunc Init;
679 if (!isValidFunctionSymbol(Init.
Symbol))
683 LinkingData.InitFunctions.emplace_back(Init);
688 if (
Error Err = parseLinkingSectionComdat(Ctx))
695 if (Ctx.Ptr != Ctx.End)
699 if (Ctx.Ptr != OrigEnd)
705Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
710 Symbols.reserve(
Count);
711 StringSet<> SymbolNames;
713 std::vector<wasm::WasmImport *> ImportedGlobals;
714 std::vector<wasm::WasmImport *> ImportedFunctions;
715 std::vector<wasm::WasmImport *> ImportedTags;
716 std::vector<wasm::WasmImport *> ImportedTables;
717 ImportedGlobals.reserve(Imports.size());
718 ImportedFunctions.reserve(Imports.size());
719 ImportedTags.reserve(Imports.size());
720 ImportedTables.reserve(Imports.size());
721 for (
auto &
I : Imports) {
723 ImportedFunctions.emplace_back(&
I);
725 ImportedGlobals.emplace_back(&
I);
727 ImportedTags.emplace_back(&
I);
729 ImportedTables.emplace_back(&
I);
733 wasm::WasmSymbolInfo
Info;
734 const wasm::WasmSignature *Signature =
nullptr;
735 const wasm::WasmGlobalType *GlobalType =
nullptr;
736 const wasm::WasmTableType *TableType =
nullptr;
745 if (!isValidFunctionIndex(
Info.ElementIndex) ||
746 IsDefined != isDefinedFunctionIndex(
Info.ElementIndex))
751 unsigned FuncIndex =
Info.ElementIndex - NumImportedFunctions;
752 wasm::WasmFunction &
Function = Functions[FuncIndex];
753 Signature = &Signatures[
Function.SigIndex];
757 wasm::WasmImport &
Import = *ImportedFunctions[
Info.ElementIndex];
764 Signature = &Signatures[
Import.SigIndex];
771 if (!isValidGlobalIndex(
Info.ElementIndex) ||
772 IsDefined != isDefinedGlobalIndex(
Info.ElementIndex))
781 unsigned GlobalIndex =
Info.ElementIndex - NumImportedGlobals;
782 wasm::WasmGlobal &
Global = Globals[GlobalIndex];
784 if (
Global.SymbolName.empty())
787 wasm::WasmImport &
Import = *ImportedGlobals[
Info.ElementIndex];
794 GlobalType = &
Import.Global;
801 if (!isValidTableNumber(
Info.ElementIndex) ||
802 IsDefined != isDefinedTableNumber(
Info.ElementIndex))
811 unsigned TableNumber =
Info.ElementIndex - NumImportedTables;
812 wasm::WasmTable &
Table = Tables[TableNumber];
813 TableType = &
Table.Type;
814 if (
Table.SymbolName.empty())
817 wasm::WasmImport &
Import = *ImportedTables[
Info.ElementIndex];
824 TableType = &
Import.Table;
836 "common symbols cannot be absolute: " +
Info.Name,
840 Info.CommonRef = wasm::WasmCommonReference{
Size, Alignment};
846 if (Index >= DataSegments.size())
848 "invalid data segment index: " + Twine(Index),
850 size_t SegmentSize = DataSegments[
Index].Data.Content.size();
853 "invalid data symbol offset: `" +
Info.Name +
854 "` (offset: " + Twine(
Offset) +
855 " segment size: " + Twine(SegmentSize) +
")",
867 "section symbols must have local binding",
878 if (!isValidTagIndex(
Info.ElementIndex) ||
879 IsDefined != isDefinedTagIndex(
Info.ElementIndex))
888 unsigned TagIndex =
Info.ElementIndex - NumImportedTags;
889 wasm::WasmTag &
Tag = Tags[TagIndex];
890 Signature = &Signatures[
Tag.SigIndex];
891 if (
Tag.SymbolName.empty())
895 wasm::WasmImport &
Import = *ImportedTags[
Info.ElementIndex];
902 Signature = &Signatures[
Import.SigIndex];
910 Twine(
unsigned(
Info.Kind)),
920 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
921 LLVM_DEBUG(
dbgs() <<
"Adding symbol: " << Symbols.back() <<
"\n");
927Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) {
929 StringSet<> ComdatSet;
930 for (
unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) {
932 if (
Name.empty() || !ComdatSet.
insert(Name).second)
936 LinkingData.Comdats.emplace_back(Name);
943 while (EntryCount--) {
951 if (Index >= DataSegments.size())
954 if (DataSegments[Index].
Data.Comdat != UINT32_MAX)
957 DataSegments[
Index].Data.Comdat = ComdatIndex;
960 if (!isDefinedFunctionIndex(Index))
963 if (getDefinedFunction(Index).Comdat != UINT32_MAX)
966 getDefinedFunction(Index).Comdat = ComdatIndex;
969 if (Index >= Sections.size())
975 Sections[
Index].Comdat = ComdatIndex;
983Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) {
984 llvm::SmallSet<StringRef, 3> FieldsSeen;
986 for (
size_t I = 0;
I < Fields; ++
I) {
988 if (!FieldsSeen.
insert(FieldName).second)
990 "producers section does not have unique fields",
992 std::vector<std::pair<std::string, std::string>> *ProducerVec =
nullptr;
993 if (FieldName ==
"language") {
994 ProducerVec = &ProducerInfo.Languages;
995 }
else if (FieldName ==
"processed-by") {
996 ProducerVec = &ProducerInfo.Tools;
997 }
else if (FieldName ==
"sdk") {
998 ProducerVec = &ProducerInfo.SDKs;
1001 "producers section field is not named one of language, processed-by, "
1006 llvm::SmallSet<StringRef, 8> ProducersSeen;
1007 for (
size_t J = 0; J < ValueCount; ++J) {
1010 if (!ProducersSeen.
insert(Name).second) {
1012 "producers section contains repeated producer",
1015 ProducerVec->emplace_back(std::string(Name), std::string(
Version));
1018 if (Ctx.Ptr != Ctx.End)
1024Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) {
1025 llvm::SmallSet<std::string, 8> FeaturesSeen;
1028 wasm::WasmFeatureEntry Feature;
1030 switch (Feature.
Prefix) {
1039 if (!FeaturesSeen.
insert(Feature.
Name).second)
1041 "target features section contains repeated feature \"" +
1042 Feature.
Name +
"\"",
1044 TargetFeatures.push_back(Feature);
1046 if (Ctx.Ptr != Ctx.End)
1048 "target features section ended prematurely",
1053Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
1055 if (SectionIndex >= Sections.size())
1058 WasmSection &
Section = Sections[SectionIndex];
1060 uint32_t EndOffset =
Section.Content.size();
1061 uint32_t PreviousOffset = 0;
1062 while (RelocCount--) {
1063 wasm::WasmRelocation Reloc = {};
1067 if (Reloc.
Offset < PreviousOffset)
1071 auto badReloc = [&](StringRef msg) {
1072 if (Reloc.
Index >= Symbols.size())
1074 msg +
": index " + Twine(Reloc.
Index) +
" out of range",
1077 msg +
": " + Twine(Symbols[Reloc.
Index].Info.Name),
1081 PreviousOffset = Reloc.
Offset;
1084 case wasm::R_WASM_FUNCTION_INDEX_LEB:
1085 case wasm::R_WASM_FUNCTION_INDEX_I32:
1086 case wasm::R_WASM_TABLE_INDEX_SLEB:
1087 case wasm::R_WASM_TABLE_INDEX_SLEB64:
1088 case wasm::R_WASM_TABLE_INDEX_I32:
1089 case wasm::R_WASM_TABLE_INDEX_I64:
1090 case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
1091 case wasm::R_WASM_TABLE_INDEX_REL_SLEB64:
1092 if (!isValidFunctionSymbol(Reloc.
Index))
1093 return badReloc(
"invalid function relocation");
1095 case wasm::R_WASM_TABLE_NUMBER_LEB:
1096 if (!isValidTableSymbol(Reloc.
Index))
1097 return badReloc(
"invalid table relocation");
1099 case wasm::R_WASM_TYPE_INDEX_LEB:
1100 if (Reloc.
Index >= Signatures.size())
1101 return badReloc(
"invalid relocation type index");
1103 case wasm::R_WASM_GLOBAL_INDEX_LEB:
1106 if (!isValidGlobalSymbol(Reloc.
Index) &&
1107 !isValidDataSymbol(Reloc.
Index) &&
1108 !isValidFunctionSymbol(Reloc.
Index))
1109 return badReloc(
"invalid global relocation");
1111 case wasm::R_WASM_GLOBAL_INDEX_I32:
1112 if (!isValidGlobalSymbol(Reloc.
Index))
1113 return badReloc(
"invalid global relocation");
1115 case wasm::R_WASM_TAG_INDEX_LEB:
1116 if (!isValidTagSymbol(Reloc.
Index))
1117 return badReloc(
"invalid tag relocation");
1119 case wasm::R_WASM_MEMORY_ADDR_LEB:
1120 case wasm::R_WASM_MEMORY_ADDR_SLEB:
1121 case wasm::R_WASM_MEMORY_ADDR_I32:
1122 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
1123 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB:
1124 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
1125 if (!isValidDataSymbol(Reloc.
Index))
1126 return badReloc(
"invalid data relocation");
1129 case wasm::R_WASM_MEMORY_ADDR_LEB64:
1130 case wasm::R_WASM_MEMORY_ADDR_SLEB64:
1131 case wasm::R_WASM_MEMORY_ADDR_I64:
1132 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
1133 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64:
1134 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I64:
1135 if (!isValidDataSymbol(Reloc.
Index))
1136 return badReloc(
"invalid data relocation");
1139 case wasm::R_WASM_FUNCTION_OFFSET_I32:
1140 if (!isValidFunctionSymbol(Reloc.
Index))
1141 return badReloc(
"invalid function relocation");
1144 case wasm::R_WASM_FUNCTION_OFFSET_I64:
1145 if (!isValidFunctionSymbol(Reloc.
Index))
1146 return badReloc(
"invalid function relocation");
1149 case wasm::R_WASM_SECTION_OFFSET_I32:
1150 if (!isValidSectionSymbol(Reloc.
Index))
1151 return badReloc(
"invalid section relocation");
1164 if (Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_LEB64 ||
1165 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_SLEB64 ||
1166 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_REL_SLEB64)
1168 if (Reloc.
Type == wasm::R_WASM_TABLE_INDEX_I32 ||
1169 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_I32 ||
1170 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I32 ||
1171 Reloc.
Type == wasm::R_WASM_SECTION_OFFSET_I32 ||
1172 Reloc.
Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
1173 Reloc.
Type == wasm::R_WASM_FUNCTION_INDEX_I32 ||
1174 Reloc.
Type == wasm::R_WASM_GLOBAL_INDEX_I32)
1176 if (Reloc.
Type == wasm::R_WASM_TABLE_INDEX_I64 ||
1177 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_I64 ||
1178 Reloc.
Type == wasm::R_WASM_FUNCTION_OFFSET_I64 ||
1179 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I64)
1185 Section.Relocations.push_back(Reloc);
1187 if (Ctx.Ptr != Ctx.End)
1193Error WasmObjectFile::parseCustomSection(
WasmSection &Sec, ReadContext &Ctx) {
1194 if (Sec.
Name ==
"dylink") {
1195 if (
Error Err = parseDylinkSection(Ctx))
1197 }
else if (Sec.
Name ==
"dylink.0") {
1198 if (
Error Err = parseDylink0Section(Ctx))
1200 }
else if (Sec.
Name ==
"name") {
1201 if (
Error Err = parseNameSection(Ctx))
1203 }
else if (Sec.
Name ==
"linking") {
1204 if (
Error Err = parseLinkingSection(Ctx))
1206 }
else if (Sec.
Name ==
"producers") {
1207 if (
Error Err = parseProducersSection(Ctx))
1209 }
else if (Sec.
Name ==
"target_features") {
1210 if (
Error Err = parseTargetFeaturesSection(Ctx))
1213 if (
Error Err = parseRelocSection(Sec.
Name, Ctx))
1219Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
1220 auto parseFieldDef = [&]() {
1227 Signatures.reserve(
Count);
1229 wasm::WasmSignature Sig;
1238 Signatures.reserve(Signatures.size() + RecSize);
1241 Signatures.push_back(std::move(Sig));
1242 HasUnmodeledTypes =
true;
1261 while (FieldCount--) {
1271 Signatures.push_back(std::move(Sig));
1272 HasUnmodeledTypes =
true;
1278 while (ParamCount--) {
1283 while (ReturnCount--) {
1288 Signatures.push_back(std::move(Sig));
1290 if (Ctx.Ptr != Ctx.End)
1296Error WasmObjectFile::parseImport(ReadContext &Ctx, wasm::WasmImport &Im) {
1299 NumImportedFunctions++;
1301 if (Im.
SigIndex >= Signatures.size())
1306 NumImportedGlobals++;
1317 NumImportedTables++;
1333 if (Im.
SigIndex >= Signatures.size())
1339 Twine(
unsigned(Im.
Kind)),
1342 Imports.push_back(Im);
1346Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
1348 Imports.reserve(
Count);
1351 wasm::WasmImport Im;
1356 if (Im.
Kind == 0x7E && Im.
Field ==
"") {
1358 "compact import format (0x7E) is not yet supported",
1360 }
else if (Im.
Kind == 0x7F && Im.
Field ==
"") {
1362 while (NumCompactImports--) {
1365 Error rtn = parseImport(Ctx, Im);
1371 Error rtn = parseImport(Ctx, Im);
1377 if (Ctx.Ptr != Ctx.End)
1383Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
1385 Functions.reserve(
Count);
1386 uint32_t NumTypes = Signatures.size();
1389 if (
Type >= NumTypes)
1392 wasm::WasmFunction
F;
1394 Functions.push_back(
F);
1396 if (Ctx.Ptr != Ctx.End)
1402Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
1403 TableSection = Sections.size();
1405 Tables.reserve(
Count);
1409 T.Index = NumImportedTables + Tables.size();
1410 Tables.push_back(
T);
1411 auto ElemType = Tables.back().Type.ElemType;
1420 if (Ctx.Ptr != Ctx.End)
1426Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
1428 Memories.reserve(
Count);
1433 Memories.push_back(Limits);
1435 if (Ctx.Ptr != Ctx.End)
1441Error WasmObjectFile::parseTagSection(ReadContext &Ctx) {
1442 TagSection = Sections.size();
1444 Tags.reserve(
Count);
1445 uint32_t NumTypes = Signatures.size();
1451 if (
Type >= NumTypes)
1455 Tag.Index = NumImportedTags + Tags.size();
1458 Tags.push_back(
Tag);
1461 if (Ctx.Ptr != Ctx.End)
1467Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
1468 GlobalSection = Sections.size();
1469 const uint8_t *SectionStart = Ctx.Ptr;
1471 Globals.reserve(
Count);
1474 Global.Index = NumImportedGlobals + Globals.size();
1475 const uint8_t *GlobalStart = Ctx.Ptr;
1476 Global.Offset =
static_cast<uint32_t
>(GlobalStart - SectionStart);
1482 Global.Size =
static_cast<uint32_t
>(Ctx.Ptr - GlobalStart);
1483 Globals.push_back(
Global);
1485 if (Ctx.Ptr != Ctx.End)
1491Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
1493 Exports.reserve(
Count);
1494 Symbols.reserve(
Count);
1498 if (HasDylinkSection) {
1499 for (
const auto &ExportInfo : DylinkInfo.ExportInfo) {
1504 for (uint32_t
I = 0;
I <
Count;
I++) {
1505 wasm::WasmExport Ex;
1509 const wasm::WasmSignature *Signature =
nullptr;
1510 const wasm::WasmGlobalType *GlobalType =
nullptr;
1511 const wasm::WasmTableType *TableType =
nullptr;
1512 wasm::WasmSymbolInfo
Info;
1517 if (HasDylinkSection) {
1520 Info.Flags = It->second;
1525 if (!isValidFunctionIndex(Ex.
Index))
1530 if (isDefinedFunctionIndex(Ex.
Index)) {
1531 getDefinedFunction(Ex.
Index).ExportName = Ex.
Name;
1532 unsigned FuncIndex =
Info.ElementIndex - NumImportedFunctions;
1533 wasm::WasmFunction &
Function = Functions[FuncIndex];
1534 Signature = &Signatures[
Function.SigIndex];
1542 if (!isValidGlobalIndex(Ex.
Index))
1547 if (isDefinedGlobalIndex(Ex.
Index)) {
1549 if (!
Global.InitExpr.Extended) {
1550 auto Inst =
Global.InitExpr.Inst;
1552 Offset = Inst.Value.Int32;
1554 Offset = Inst.Value.Int64;
1558 Info.DataRef = wasm::WasmDataReference{0,
Offset, 0};
1562 if (!isValidTagIndex(Ex.
Index))
1567 if (isDefinedTagIndex(Ex.
Index)) {
1568 unsigned TagIndex = Ex.
Index - NumImportedTags;
1569 Signature = &Signatures[Tags[TagIndex].SigIndex];
1582 Exports.push_back(Ex);
1584 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
1585 LLVM_DEBUG(
dbgs() <<
"Adding symbol: " << Symbols.back() <<
"\n");
1588 if (Ctx.Ptr != Ctx.End)
1594bool WasmObjectFile::isValidFunctionIndex(uint32_t Index)
const {
1595 return Index < NumImportedFunctions + Functions.size();
1598bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index)
const {
1599 return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
1602bool WasmObjectFile::isValidGlobalIndex(uint32_t Index)
const {
1603 return Index < NumImportedGlobals + Globals.size();
1606bool WasmObjectFile::isValidTableNumber(uint32_t Index)
const {
1607 return Index < NumImportedTables + Tables.size();
1610bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index)
const {
1611 return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
1614bool WasmObjectFile::isDefinedTableNumber(uint32_t Index)
const {
1615 return Index >= NumImportedTables && isValidTableNumber(Index);
1618bool WasmObjectFile::isValidTagIndex(uint32_t Index)
const {
1619 return Index < NumImportedTags + Tags.size();
1622bool WasmObjectFile::isDefinedTagIndex(uint32_t Index)
const {
1623 return Index >= NumImportedTags && isValidTagIndex(Index);
1626bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index)
const {
1627 return Index < Symbols.size() && Symbols[
Index].isTypeFunction();
1630bool WasmObjectFile::isValidTableSymbol(uint32_t Index)
const {
1631 return Index < Symbols.size() && Symbols[
Index].isTypeTable();
1634bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index)
const {
1635 return Index < Symbols.size() && Symbols[
Index].isTypeGlobal();
1638bool WasmObjectFile::isValidTagSymbol(uint32_t Index)
const {
1639 return Index < Symbols.size() && Symbols[
Index].isTypeTag();
1642bool WasmObjectFile::isValidDataSymbol(uint32_t Index)
const {
1643 return Index < Symbols.size() && Symbols[
Index].isTypeData();
1646bool WasmObjectFile::isValidSectionSymbol(uint32_t Index)
const {
1647 return Index < Symbols.size() && Symbols[
Index].isTypeSection();
1650wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
1651 assert(isDefinedFunctionIndex(Index));
1652 return Functions[
Index - NumImportedFunctions];
1655const wasm::WasmFunction &
1656WasmObjectFile::getDefinedFunction(uint32_t Index)
const {
1657 assert(isDefinedFunctionIndex(Index));
1658 return Functions[
Index - NumImportedFunctions];
1661const wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index)
const {
1662 assert(isDefinedGlobalIndex(Index));
1663 return Globals[
Index - NumImportedGlobals];
1666wasm::WasmTag &WasmObjectFile::getDefinedTag(uint32_t Index) {
1667 assert(isDefinedTagIndex(Index));
1668 return Tags[
Index - NumImportedTags];
1671Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
1673 if (!isValidFunctionIndex(StartFunction))
1679Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) {
1680 CodeSection = Sections.size();
1682 if (FunctionCount != Functions.size()) {
1687 for (uint32_t i = 0; i < FunctionCount; i++) {
1688 wasm::WasmFunction&
Function = Functions[i];
1689 const uint8_t *FunctionStart = Ctx.Ptr;
1691 const uint8_t *FunctionEnd = Ctx.Ptr +
Size;
1693 Function.CodeOffset = Ctx.Ptr - FunctionStart;
1694 Function.Index = NumImportedFunctions + i;
1695 Function.CodeSectionOffset = FunctionStart - Ctx.Start;
1696 Function.Size = FunctionEnd - FunctionStart;
1699 Function.Locals.reserve(NumLocalDecls);
1700 while (NumLocalDecls--) {
1701 wasm::WasmLocalDecl Decl;
1707 uint32_t BodySize = FunctionEnd - Ctx.Ptr;
1709 if (Ctx.Ptr + BodySize > Ctx.End) {
1713 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize);
1716 Ctx.Ptr += BodySize;
1717 assert(Ctx.Ptr == FunctionEnd);
1719 if (Ctx.Ptr != Ctx.End)
1725Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
1727 ElemSegments.reserve(
Count);
1729 wasm::WasmElemSegment Segment;
1735 if (Segment.
Flags & ~SupportedFlags)
1747 bool HasTableNumber =
1794 }
else if (HasElemType) {
1804 while (NumElems--) {
1805 wasm::WasmInitExpr Expr;
1810 while (NumElems--) {
1814 ElemSegments.push_back(Segment);
1816 if (Ctx.Ptr != Ctx.End)
1822Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
1823 DataSection = Sections.size();
1825 if (DataCount &&
Count != *DataCount)
1827 "number of data segments does not match DataCount section");
1828 DataSegments.reserve(
Count);
1830 WasmSegment Segment;
1845 if (
Size > (
size_t)(Ctx.End - Ctx.Ptr))
1856 DataSegments.push_back(Segment);
1858 if (Ctx.Ptr != Ctx.End)
1864Error WasmObjectFile::parseDataCountSection(ReadContext &Ctx) {
1879 LLVM_DEBUG(
dbgs() <<
"getSymbolFlags: ptr=" << &Sym <<
" " << Sym <<
"\n");
1903 Ref.d.b = Symbols.size();
1908 return Symbols[Symb.
d.
b];
1921 if (!Sym.isDefined())
1928 isDefinedFunctionIndex(Sym.Info.ElementIndex)) {
1929 return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset +
1933 isDefinedGlobalIndex(Sym.Info.ElementIndex)) {
1934 return getDefinedGlobal(Sym.Info.ElementIndex).Offset + SectionAddress;
2014 Ref.d.a = getSymbolSectionIdImpl(Sym);
2020 return getSymbolSectionIdImpl(Sym);
2028 return GlobalSection;
2036 return TableSection;
2074 : Sections[Sec.
d.
a].Offset;
2116 RelocRef.
d.
a =
Ref.d.a;
2124 RelocRef.
d.
a =
Ref.d.a;
2138 if (Rel.
Type == wasm::R_WASM_TYPE_INDEX_LEB)
2156#define WASM_RELOC(name, value) \
2162#include "llvm/BinaryFormat/WasmRelocs.def"
2167 Result.append(Res.
begin(), Res.
end());
2178 Ref.d.a = Sections.size();
2183 return HasMemory64 ? 8 : 4;
2202 return Sections[
Ref.d.a];
2223int WasmSectionOrderChecker::getSectionOrder(
unsigned ID,
2271 [WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = {
2275 {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT},
2277 {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION},
2279 {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE},
2281 {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY},
2283 {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_TAG},
2285 {WASM_SEC_ORDER_TAG, WASM_SEC_ORDER_GLOBAL},
2287 {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EXPORT},
2289 {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START},
2291 {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM},
2293 {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT},
2295 {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE},
2297 {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA},
2299 {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING},
2303 {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE},
2305 {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME},
2309 {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS},
2311 {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES},
2313 {WASM_SEC_ORDER_TARGET_FEATURES}};
2317 int Order = getSectionOrder(ID, CustomSectionName);
2330 for (
size_t I = 0;; ++
I) {
2337 Checked[
Next] =
true;
2340 if (WorkList.
empty())
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
This file defines the DenseSet and SmallDenseSet classes.
static Error readString(StringRef Buffer, const char *&Src, size_t MaxSize, StringRef &Val, Twine Desc)
Read a null-terminated string at the position Src from Buffer, with maximum byte size of MaxSize (inc...
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
This file defines the SmallSet class.
StringSet - A set-like wrapper for the StringMap.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
static uint8_t readVaruint1(WasmObjectFile::ReadContext &Ctx)
static Error readInitExpr(wasm::WasmInitExpr &Expr, WasmObjectFile::ReadContext &Ctx)
static int32_t readVarint32(WasmObjectFile::ReadContext &Ctx)
static wasm::WasmTableType readTableType(WasmObjectFile::ReadContext &Ctx)
static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx)
static uint64_t readVaruint64(WasmObjectFile::ReadContext &Ctx)
static Error readSection(WasmSection &Section, WasmObjectFile::ReadContext &Ctx, WasmSectionOrderChecker &Checker)
static int64_t readLEB128(WasmObjectFile::ReadContext &Ctx)
static uint32_t readVaruint32(WasmObjectFile::ReadContext &Ctx)
static uint32_t readUint32(WasmObjectFile::ReadContext &Ctx)
static uint8_t readOpcode(WasmObjectFile::ReadContext &Ctx)
static uint8_t readUint8(WasmObjectFile::ReadContext &Ctx)
static int32_t readFloat32(WasmObjectFile::ReadContext &Ctx)
static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx)
static int64_t readFloat64(WasmObjectFile::ReadContext &Ctx)
static wasm::ValType parseValType(WasmObjectFile::ReadContext &Ctx, uint32_t Code)
static int64_t readVarint64(WasmObjectFile::ReadContext &Ctx)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
Helper for Errors used as out-parameters.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
constexpr size_t size() const
Get the string size.
const unsigned char * bytes_begin() const
std::pair< typename Base::iterator, bool > insert(StringRef key)
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
StringSwitch & StartsWith(StringLiteral S, T Value)
Manages the enabling and disabling of subtarget specific features.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static Twine utohexstr(uint64_t Val)
std::pair< iterator, bool > insert(const ValueT &V)
This is a value type class that represents a single symbol in the list of symbols in the object file.
DataRefImpl getRawDataRefImpl() const
StringRef getData() const
friend class RelocationRef
static Expected< std::unique_ptr< WasmObjectFile > > createWasmObjectFile(MemoryBufferRef Object)
Expected< uint64_t > getSymbolValue(DataRefImpl Symb) const
ObjectFile(unsigned int Type, MemoryBufferRef Source)
basic_symbol_iterator symbol_begin() const override
relocation_iterator section_rel_end(DataRefImpl Sec) const override
void moveSymbolNext(DataRefImpl &Symb) const override
uint64_t getSectionAlignment(DataRefImpl Sec) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
uint64_t getWasmSymbolValue(const WasmSymbol &Sym) const
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
bool isSectionText(DataRefImpl Sec) const override
bool isSectionBSS(DataRefImpl Sec) const override
basic_symbol_iterator symbol_end() const override
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
section_iterator section_begin() const override
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
void moveRelocationNext(DataRefImpl &Rel) const override
uint32_t getSymbolSectionId(SymbolRef Sym) const
bool isSectionCompressed(DataRefImpl Sec) const override
bool isSectionVirtual(DataRefImpl Sec) const override
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
StringRef getFileFormatName() const override
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
WasmObjectFile(MemoryBufferRef Object, Error &Err)
section_iterator section_end() const override
Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
uint64_t getSectionSize(DataRefImpl Sec) const override
Triple::ArchType getArch() const override
uint64_t getRelocationType(DataRefImpl Rel) const override
const WasmSection & getWasmSection(const SectionRef &Section) const
Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const override
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
Expected< SubtargetFeatures > getFeatures() const override
const wasm::WasmObjectHeader & getHeader() const
void moveSectionNext(DataRefImpl &Sec) const override
uint32_t getNumImportedFunctions() const
bool isSharedObject() const
const wasm::WasmRelocation & getWasmRelocation(const RelocationRef &Ref) const
uint32_t getSymbolSize(SymbolRef Sym) const
ArrayRef< wasm::WasmFunction > functions() const
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="")
@ WASM_SEC_ORDER_TARGET_FEATURES
@ WASM_SEC_ORDER_PRODUCERS
@ WASM_SEC_ORDER_DATACOUNT
@ WASM_SEC_ORDER_FUNCTION
static LLVM_ABI int DisallowedPredecessors[WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS]
bool isTypeFunction() const
unsigned getBinding() const
LLVM_DUMP_METHOD void dump() const
bool isBindingWeak() const
wasm::WasmSymbolInfo Info
LLVM_ABI void print(raw_ostream &Out) const
bool isBindingLocal() const
bool isTypeGlobal() const
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
content_iterator< SectionRef > section_iterator
content_iterator< BasicSymbolRef > basic_symbol_iterator
content_iterator< RelocationRef > relocation_iterator
uint32_t read32le(const void *P)
const unsigned WASM_SYMBOL_UNDEFINED
@ WASM_NAMES_DATA_SEGMENT
const unsigned WASM_SYMBOL_NO_STRIP
const unsigned WASM_SYMBOL_BINDING_GLOBAL
const unsigned WASM_SYMBOL_TLS
const uint32_t WasmMetadataVersion
const unsigned WASM_SYMBOL_BINDING_WEAK
const unsigned WASM_SYMBOL_BINDING_LOCAL
@ WASM_LIMITS_FLAG_HAS_MAX
@ WASM_LIMITS_FLAG_HAS_PAGE_SIZE
@ WASM_FEATURE_PREFIX_USED
@ WASM_FEATURE_PREFIX_DISALLOWED
@ WASM_SYMBOL_TYPE_GLOBAL
@ WASM_SYMBOL_TYPE_SECTION
@ WASM_SYMBOL_TYPE_FUNCTION
const uint32_t WasmVersion
const unsigned WASM_SYMBOL_BINDING_COMMON
const unsigned WASM_SYMBOL_EXPORTED
const unsigned WASM_SYMBOL_BINDING_MASK
@ WASM_ELEM_SEGMENT_HAS_INIT_EXPRS
@ WASM_ELEM_SEGMENT_IS_DECLARATIVE
@ WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER
@ WASM_ELEM_SEGMENT_IS_PASSIVE
@ WASM_DYLINK_RUNTIME_PATH
@ WASM_DYLINK_EXPORT_INFO
@ WASM_DYLINK_IMPORT_INFO
@ WASM_DATA_SEGMENT_IS_PASSIVE
@ WASM_DATA_SEGMENT_HAS_MEMINDEX
LLVM_ABI llvm::StringRef sectionTypeToString(uint32_t type)
const unsigned WASM_SYMBOL_EXPLICIT_NAME
const unsigned WASM_SYMBOL_ABSOLUTE
const unsigned WASM_ELEM_SEGMENT_MASK_HAS_ELEM_DESC
@ WASM_OPCODE_ARRAY_NEW_FIXED
@ WASM_OPCODE_ARRAY_NEW_DEFAULT
@ WASM_OPCODE_STRUCT_NEW_DEFAULT
This is an optimization pass for GlobalISel generic memory operations.
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.
@ 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.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
const char * to_string(ThinOrFullLTOPhase Phase)
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
@ Global
Append to llvm.global_dtors.
@ Ref
The access may reference the value stored in memory.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
ArrayRef< uint8_t > Content
std::vector< wasm::WasmRelocation > Relocations
wasm::WasmDataSegment Data
ArrayRef< uint8_t > Content
std::vector< StringRef > Needed
std::vector< uint32_t > Functions
union llvm::wasm::WasmInitExprMVP::@234311111124136373374304035273310237314141125040 Value
SmallVector< ValType, 1 > Returns
SmallVector< ValType, 4 > Params
enum llvm::wasm::WasmSignature::@330257225222011177372050205212257221301063235146 Kind
WasmDataReference DataRef
struct llvm::object::DataRefImpl::@005117267142344013370254144343227032034000327225 d