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(
"invalid opcode in init_expr: ") +
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;
817 wasm::WasmImport &
Import = *ImportedTables[
Info.ElementIndex];
824 TableType = &
Import.Table;
836 if (
static_cast<size_t>(Index) >= DataSegments.size())
838 "invalid data segment index: " + Twine(Index),
840 size_t SegmentSize = DataSegments[
Index].Data.Content.size();
843 "invalid data symbol offset: `" +
Info.Name +
844 "` (offset: " + Twine(
Offset) +
845 " segment size: " + Twine(SegmentSize) +
")",
856 "section symbols must have local binding",
867 if (!isValidTagIndex(
Info.ElementIndex) ||
868 IsDefined != isDefinedTagIndex(
Info.ElementIndex))
877 unsigned TagIndex =
Info.ElementIndex - NumImportedTags;
878 wasm::WasmTag &
Tag = Tags[TagIndex];
879 Signature = &Signatures[
Tag.SigIndex];
880 if (
Tag.SymbolName.empty())
884 wasm::WasmImport &
Import = *ImportedTags[
Info.ElementIndex];
891 Signature = &Signatures[
Import.SigIndex];
899 Twine(
unsigned(
Info.Kind)),
909 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
910 LLVM_DEBUG(
dbgs() <<
"Adding symbol: " << Symbols.back() <<
"\n");
916Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) {
918 StringSet<> ComdatSet;
919 for (
unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) {
921 if (
Name.empty() || !ComdatSet.
insert(Name).second)
925 LinkingData.Comdats.emplace_back(Name);
932 while (EntryCount--) {
940 if (Index >= DataSegments.size())
943 if (DataSegments[Index].
Data.Comdat != UINT32_MAX)
946 DataSegments[
Index].Data.Comdat = ComdatIndex;
949 if (!isDefinedFunctionIndex(Index))
952 if (getDefinedFunction(Index).Comdat != UINT32_MAX)
955 getDefinedFunction(Index).Comdat = ComdatIndex;
958 if (Index >= Sections.size())
964 Sections[
Index].Comdat = ComdatIndex;
972Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) {
973 llvm::SmallSet<StringRef, 3> FieldsSeen;
975 for (
size_t I = 0;
I < Fields; ++
I) {
977 if (!FieldsSeen.
insert(FieldName).second)
979 "producers section does not have unique fields",
981 std::vector<std::pair<std::string, std::string>> *ProducerVec =
nullptr;
982 if (FieldName ==
"language") {
983 ProducerVec = &ProducerInfo.Languages;
984 }
else if (FieldName ==
"processed-by") {
985 ProducerVec = &ProducerInfo.Tools;
986 }
else if (FieldName ==
"sdk") {
987 ProducerVec = &ProducerInfo.SDKs;
990 "producers section field is not named one of language, processed-by, "
995 llvm::SmallSet<StringRef, 8> ProducersSeen;
996 for (
size_t J = 0; J < ValueCount; ++J) {
999 if (!ProducersSeen.
insert(Name).second) {
1001 "producers section contains repeated producer",
1004 ProducerVec->emplace_back(std::string(Name), std::string(
Version));
1007 if (Ctx.Ptr != Ctx.End)
1013Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) {
1014 llvm::SmallSet<std::string, 8> FeaturesSeen;
1017 wasm::WasmFeatureEntry Feature;
1019 switch (Feature.
Prefix) {
1028 if (!FeaturesSeen.
insert(Feature.
Name).second)
1030 "target features section contains repeated feature \"" +
1031 Feature.
Name +
"\"",
1033 TargetFeatures.push_back(Feature);
1035 if (Ctx.Ptr != Ctx.End)
1037 "target features section ended prematurely",
1042Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
1044 if (SectionIndex >= Sections.size())
1047 WasmSection &
Section = Sections[SectionIndex];
1049 uint32_t EndOffset =
Section.Content.size();
1050 uint32_t PreviousOffset = 0;
1051 while (RelocCount--) {
1052 wasm::WasmRelocation Reloc = {};
1056 if (Reloc.
Offset < PreviousOffset)
1060 auto badReloc = [&](StringRef msg) {
1062 msg +
": " + Twine(Symbols[Reloc.
Index].Info.Name),
1066 PreviousOffset = Reloc.
Offset;
1069 case wasm::R_WASM_FUNCTION_INDEX_LEB:
1070 case wasm::R_WASM_FUNCTION_INDEX_I32:
1071 case wasm::R_WASM_TABLE_INDEX_SLEB:
1072 case wasm::R_WASM_TABLE_INDEX_SLEB64:
1073 case wasm::R_WASM_TABLE_INDEX_I32:
1074 case wasm::R_WASM_TABLE_INDEX_I64:
1075 case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
1076 case wasm::R_WASM_TABLE_INDEX_REL_SLEB64:
1077 if (!isValidFunctionSymbol(Reloc.
Index))
1078 return badReloc(
"invalid function relocation");
1080 case wasm::R_WASM_TABLE_NUMBER_LEB:
1081 if (!isValidTableSymbol(Reloc.
Index))
1082 return badReloc(
"invalid table relocation");
1084 case wasm::R_WASM_TYPE_INDEX_LEB:
1085 if (Reloc.
Index >= Signatures.size())
1086 return badReloc(
"invalid relocation type index");
1088 case wasm::R_WASM_GLOBAL_INDEX_LEB:
1091 if (!isValidGlobalSymbol(Reloc.
Index) &&
1092 !isValidDataSymbol(Reloc.
Index) &&
1093 !isValidFunctionSymbol(Reloc.
Index))
1094 return badReloc(
"invalid global relocation");
1096 case wasm::R_WASM_GLOBAL_INDEX_I32:
1097 if (!isValidGlobalSymbol(Reloc.
Index))
1098 return badReloc(
"invalid global relocation");
1100 case wasm::R_WASM_TAG_INDEX_LEB:
1101 if (!isValidTagSymbol(Reloc.
Index))
1102 return badReloc(
"invalid tag relocation");
1104 case wasm::R_WASM_MEMORY_ADDR_LEB:
1105 case wasm::R_WASM_MEMORY_ADDR_SLEB:
1106 case wasm::R_WASM_MEMORY_ADDR_I32:
1107 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
1108 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB:
1109 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
1110 if (!isValidDataSymbol(Reloc.
Index))
1111 return badReloc(
"invalid data relocation");
1114 case wasm::R_WASM_MEMORY_ADDR_LEB64:
1115 case wasm::R_WASM_MEMORY_ADDR_SLEB64:
1116 case wasm::R_WASM_MEMORY_ADDR_I64:
1117 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
1118 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64:
1119 if (!isValidDataSymbol(Reloc.
Index))
1120 return badReloc(
"invalid data relocation");
1123 case wasm::R_WASM_FUNCTION_OFFSET_I32:
1124 if (!isValidFunctionSymbol(Reloc.
Index))
1125 return badReloc(
"invalid function relocation");
1128 case wasm::R_WASM_FUNCTION_OFFSET_I64:
1129 if (!isValidFunctionSymbol(Reloc.
Index))
1130 return badReloc(
"invalid function relocation");
1133 case wasm::R_WASM_SECTION_OFFSET_I32:
1134 if (!isValidSectionSymbol(Reloc.
Index))
1135 return badReloc(
"invalid section relocation");
1148 if (Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_LEB64 ||
1149 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_SLEB64 ||
1150 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_REL_SLEB64)
1152 if (Reloc.
Type == wasm::R_WASM_TABLE_INDEX_I32 ||
1153 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_I32 ||
1154 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I32 ||
1155 Reloc.
Type == wasm::R_WASM_SECTION_OFFSET_I32 ||
1156 Reloc.
Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
1157 Reloc.
Type == wasm::R_WASM_FUNCTION_INDEX_I32 ||
1158 Reloc.
Type == wasm::R_WASM_GLOBAL_INDEX_I32)
1160 if (Reloc.
Type == wasm::R_WASM_TABLE_INDEX_I64 ||
1161 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_I64 ||
1162 Reloc.
Type == wasm::R_WASM_FUNCTION_OFFSET_I64)
1168 Section.Relocations.push_back(Reloc);
1170 if (Ctx.Ptr != Ctx.End)
1176Error WasmObjectFile::parseCustomSection(
WasmSection &Sec, ReadContext &Ctx) {
1177 if (Sec.
Name ==
"dylink") {
1178 if (
Error Err = parseDylinkSection(Ctx))
1180 }
else if (Sec.
Name ==
"dylink.0") {
1181 if (
Error Err = parseDylink0Section(Ctx))
1183 }
else if (Sec.
Name ==
"name") {
1184 if (
Error Err = parseNameSection(Ctx))
1186 }
else if (Sec.
Name ==
"linking") {
1187 if (
Error Err = parseLinkingSection(Ctx))
1189 }
else if (Sec.
Name ==
"producers") {
1190 if (
Error Err = parseProducersSection(Ctx))
1192 }
else if (Sec.
Name ==
"target_features") {
1193 if (
Error Err = parseTargetFeaturesSection(Ctx))
1196 if (
Error Err = parseRelocSection(Sec.
Name, Ctx))
1202Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
1203 auto parseFieldDef = [&]() {
1210 Signatures.reserve(
Count);
1212 wasm::WasmSignature Sig;
1221 Signatures.reserve(Signatures.size() + RecSize);
1224 Signatures.push_back(std::move(Sig));
1225 HasUnmodeledTypes =
true;
1244 while (FieldCount--) {
1254 Signatures.push_back(std::move(Sig));
1255 HasUnmodeledTypes =
true;
1261 while (ParamCount--) {
1266 while (ReturnCount--) {
1271 Signatures.push_back(std::move(Sig));
1273 if (Ctx.Ptr != Ctx.End)
1279Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
1281 uint32_t NumTypes = Signatures.size();
1282 Imports.reserve(
Count);
1283 for (uint32_t
I = 0;
I <
Count;
I++) {
1284 wasm::WasmImport Im;
1290 NumImportedFunctions++;
1297 NumImportedGlobals++;
1308 NumImportedTables++;
1332 Imports.push_back(Im);
1334 if (Ctx.Ptr != Ctx.End)
1340Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
1342 Functions.reserve(
Count);
1343 uint32_t NumTypes = Signatures.size();
1346 if (
Type >= NumTypes)
1349 wasm::WasmFunction
F;
1351 Functions.push_back(
F);
1353 if (Ctx.Ptr != Ctx.End)
1359Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
1360 TableSection = Sections.size();
1362 Tables.reserve(
Count);
1366 T.Index = NumImportedTables + Tables.size();
1367 Tables.push_back(
T);
1368 auto ElemType = Tables.back().Type.ElemType;
1377 if (Ctx.Ptr != Ctx.End)
1383Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
1385 Memories.reserve(
Count);
1390 Memories.push_back(Limits);
1392 if (Ctx.Ptr != Ctx.End)
1398Error WasmObjectFile::parseTagSection(ReadContext &Ctx) {
1399 TagSection = Sections.size();
1401 Tags.reserve(
Count);
1402 uint32_t NumTypes = Signatures.size();
1408 if (
Type >= NumTypes)
1412 Tag.Index = NumImportedTags + Tags.size();
1415 Tags.push_back(
Tag);
1418 if (Ctx.Ptr != Ctx.End)
1424Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
1425 GlobalSection = Sections.size();
1426 const uint8_t *SectionStart = Ctx.Ptr;
1428 Globals.reserve(
Count);
1431 Global.Index = NumImportedGlobals + Globals.size();
1432 const uint8_t *GlobalStart = Ctx.Ptr;
1433 Global.Offset =
static_cast<uint32_t
>(GlobalStart - SectionStart);
1439 Global.Size =
static_cast<uint32_t
>(Ctx.Ptr - GlobalStart);
1440 Globals.push_back(
Global);
1442 if (Ctx.Ptr != Ctx.End)
1448Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
1450 Exports.reserve(
Count);
1451 Symbols.reserve(
Count);
1452 for (uint32_t
I = 0;
I <
Count;
I++) {
1453 wasm::WasmExport Ex;
1457 const wasm::WasmSignature *Signature =
nullptr;
1458 const wasm::WasmGlobalType *GlobalType =
nullptr;
1459 const wasm::WasmTableType *TableType =
nullptr;
1460 wasm::WasmSymbolInfo
Info;
1465 if (!isValidFunctionIndex(Ex.
Index))
1470 if (isDefinedFunctionIndex(Ex.
Index)) {
1471 getDefinedFunction(Ex.
Index).ExportName = Ex.
Name;
1472 unsigned FuncIndex =
Info.ElementIndex - NumImportedFunctions;
1473 wasm::WasmFunction &
Function = Functions[FuncIndex];
1474 Signature = &Signatures[
Function.SigIndex];
1482 if (!isValidGlobalIndex(Ex.
Index))
1487 if (isDefinedGlobalIndex(Ex.
Index)) {
1489 if (!
Global.InitExpr.Extended) {
1490 auto Inst =
Global.InitExpr.Inst;
1492 Offset = Inst.Value.Int32;
1494 Offset = Inst.Value.Int64;
1498 Info.DataRef = wasm::WasmDataReference{0,
Offset, 0};
1502 if (!isValidTagIndex(Ex.
Index))
1518 Exports.push_back(Ex);
1520 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
1521 LLVM_DEBUG(
dbgs() <<
"Adding symbol: " << Symbols.back() <<
"\n");
1524 if (Ctx.Ptr != Ctx.End)
1530bool WasmObjectFile::isValidFunctionIndex(uint32_t Index)
const {
1531 return Index < NumImportedFunctions + Functions.size();
1534bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index)
const {
1535 return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
1538bool WasmObjectFile::isValidGlobalIndex(uint32_t Index)
const {
1539 return Index < NumImportedGlobals + Globals.size();
1542bool WasmObjectFile::isValidTableNumber(uint32_t Index)
const {
1543 return Index < NumImportedTables + Tables.size();
1546bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index)
const {
1547 return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
1550bool WasmObjectFile::isDefinedTableNumber(uint32_t Index)
const {
1551 return Index >= NumImportedTables && isValidTableNumber(Index);
1554bool WasmObjectFile::isValidTagIndex(uint32_t Index)
const {
1555 return Index < NumImportedTags + Tags.size();
1558bool WasmObjectFile::isDefinedTagIndex(uint32_t Index)
const {
1559 return Index >= NumImportedTags && isValidTagIndex(Index);
1562bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index)
const {
1563 return Index < Symbols.size() && Symbols[
Index].isTypeFunction();
1566bool WasmObjectFile::isValidTableSymbol(uint32_t Index)
const {
1567 return Index < Symbols.size() && Symbols[
Index].isTypeTable();
1570bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index)
const {
1571 return Index < Symbols.size() && Symbols[
Index].isTypeGlobal();
1574bool WasmObjectFile::isValidTagSymbol(uint32_t Index)
const {
1575 return Index < Symbols.size() && Symbols[
Index].isTypeTag();
1578bool WasmObjectFile::isValidDataSymbol(uint32_t Index)
const {
1579 return Index < Symbols.size() && Symbols[
Index].isTypeData();
1582bool WasmObjectFile::isValidSectionSymbol(uint32_t Index)
const {
1583 return Index < Symbols.size() && Symbols[
Index].isTypeSection();
1586wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
1587 assert(isDefinedFunctionIndex(Index));
1588 return Functions[
Index - NumImportedFunctions];
1591const wasm::WasmFunction &
1592WasmObjectFile::getDefinedFunction(uint32_t Index)
const {
1593 assert(isDefinedFunctionIndex(Index));
1594 return Functions[
Index - NumImportedFunctions];
1597const wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index)
const {
1598 assert(isDefinedGlobalIndex(Index));
1599 return Globals[
Index - NumImportedGlobals];
1602wasm::WasmTag &WasmObjectFile::getDefinedTag(uint32_t Index) {
1603 assert(isDefinedTagIndex(Index));
1604 return Tags[
Index - NumImportedTags];
1607Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
1609 if (!isValidFunctionIndex(StartFunction))
1615Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) {
1616 CodeSection = Sections.size();
1618 if (FunctionCount != Functions.size()) {
1623 for (uint32_t i = 0; i < FunctionCount; i++) {
1624 wasm::WasmFunction&
Function = Functions[i];
1625 const uint8_t *FunctionStart = Ctx.Ptr;
1627 const uint8_t *FunctionEnd = Ctx.Ptr +
Size;
1629 Function.CodeOffset = Ctx.Ptr - FunctionStart;
1630 Function.Index = NumImportedFunctions + i;
1631 Function.CodeSectionOffset = FunctionStart - Ctx.Start;
1632 Function.Size = FunctionEnd - FunctionStart;
1635 Function.Locals.reserve(NumLocalDecls);
1636 while (NumLocalDecls--) {
1637 wasm::WasmLocalDecl Decl;
1643 uint32_t BodySize = FunctionEnd - Ctx.Ptr;
1645 if (Ctx.Ptr + BodySize > Ctx.End) {
1649 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize);
1652 Ctx.Ptr += BodySize;
1653 assert(Ctx.Ptr == FunctionEnd);
1655 if (Ctx.Ptr != Ctx.End)
1661Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
1663 ElemSegments.reserve(
Count);
1665 wasm::WasmElemSegment Segment;
1671 if (Segment.
Flags & ~SupportedFlags)
1683 bool HasTableNumber =
1730 }
else if (HasElemType) {
1740 while (NumElems--) {
1741 wasm::WasmInitExpr Expr;
1746 while (NumElems--) {
1750 ElemSegments.push_back(Segment);
1752 if (Ctx.Ptr != Ctx.End)
1758Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
1759 DataSection = Sections.size();
1761 if (DataCount &&
Count != *DataCount)
1763 "number of data segments does not match DataCount section");
1764 DataSegments.reserve(
Count);
1766 WasmSegment Segment;
1781 if (
Size > (
size_t)(Ctx.End - Ctx.Ptr))
1792 DataSegments.push_back(Segment);
1794 if (Ctx.Ptr != Ctx.End)
1800Error WasmObjectFile::parseDataCountSection(ReadContext &Ctx) {
1815 LLVM_DEBUG(
dbgs() <<
"getSymbolFlags: ptr=" << &Sym <<
" " << Sym <<
"\n");
1839 Ref.d.b = Symbols.size();
1844 return Symbols[Symb.
d.
b];
1857 if (!Sym.isDefined())
1864 isDefinedFunctionIndex(Sym.Info.ElementIndex)) {
1865 return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset +
1869 isDefinedGlobalIndex(Sym.Info.ElementIndex)) {
1870 return getDefinedGlobal(Sym.Info.ElementIndex).Offset + SectionAddress;
1950 Ref.d.a = getSymbolSectionIdImpl(Sym);
1956 return getSymbolSectionIdImpl(Sym);
1964 return GlobalSection;
1972 return TableSection;
2010 : Sections[Sec.
d.
a].Offset;
2052 RelocRef.
d.
a =
Ref.d.a;
2060 RelocRef.
d.
a =
Ref.d.a;
2074 if (Rel.
Type == wasm::R_WASM_TYPE_INDEX_LEB)
2092#define WASM_RELOC(name, value) \
2098#include "llvm/BinaryFormat/WasmRelocs.def"
2103 Result.append(Res.
begin(), Res.
end());
2114 Ref.d.a = Sections.size();
2119 return HasMemory64 ? 8 : 4;
2138 return Sections[
Ref.d.a];
2159int WasmSectionOrderChecker::getSectionOrder(
unsigned ID,
2207 [WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = {
2211 {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT},
2213 {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION},
2215 {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE},
2217 {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY},
2219 {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_TAG},
2221 {WASM_SEC_ORDER_TAG, WASM_SEC_ORDER_GLOBAL},
2223 {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EXPORT},
2225 {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START},
2227 {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM},
2229 {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT},
2231 {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE},
2233 {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA},
2235 {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING},
2239 {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE},
2241 {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME},
2245 {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS},
2247 {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES},
2249 {WASM_SEC_ORDER_TARGET_FEATURES}};
2253 int Order = getSectionOrder(
ID, CustomSectionName);
2266 for (
size_t I = 0;; ++
I) {
2273 Checked[
Next] =
true;
2276 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 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 StringRef readString(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)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - 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.
StringRef - 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 bool empty() const
empty - Check if the string is empty.
constexpr size_t size() const
size - 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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
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
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_DATA_SEGMENT_IS_PASSIVE
@ WASM_DATA_SEGMENT_HAS_MEMINDEX
@ 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_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_OPCODE_ARRAY_NEW_FIXED
@ WASM_OPCODE_ARRAY_NEW_DEFAULT
@ WASM_OPCODE_STRUCT_NEW_DEFAULT
@ WASM_NAMES_DATA_SEGMENT
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
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.
FunctionAddr VTableAddr uintptr_t uintptr_t Version
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)
FunctionAddr VTableAddr Count
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.
FunctionAddr VTableAddr Next
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
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::@021046217255127373215144224227277301132130072341 Value
SmallVector< ValType, 1 > Returns
SmallVector< ValType, 4 > Params
enum llvm::wasm::WasmSignature::@105325325225225371022120066125223151323055007017 Kind
WasmDataReference DataRef
struct llvm::object::DataRefImpl::@005117267142344013370254144343227032034000327225 d