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++) {
668 if (DataSegments[
I].
Data.Alignment > 32)
670 "invalid data segment alignment: `" + DataSegments[
I].
Data.Name +
671 "` (alignment: " + Twine(DataSegments[
I].
Data.Alignment) +
680 LinkingData.InitFunctions.reserve(
Count);
681 for (uint32_t
I = 0;
I <
Count;
I++) {
682 wasm::WasmInitFunc Init;
685 if (!isValidFunctionSymbol(Init.
Symbol))
689 LinkingData.InitFunctions.emplace_back(Init);
694 if (
Error Err = parseLinkingSectionComdat(Ctx))
701 if (Ctx.Ptr != Ctx.End)
705 if (Ctx.Ptr != OrigEnd)
711Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
716 Symbols.reserve(
Count);
717 StringSet<> SymbolNames;
719 std::vector<wasm::WasmImport *> ImportedGlobals;
720 std::vector<wasm::WasmImport *> ImportedFunctions;
721 std::vector<wasm::WasmImport *> ImportedTags;
722 std::vector<wasm::WasmImport *> ImportedTables;
723 ImportedGlobals.reserve(Imports.size());
724 ImportedFunctions.reserve(Imports.size());
725 ImportedTags.reserve(Imports.size());
726 ImportedTables.reserve(Imports.size());
727 for (
auto &
I : Imports) {
729 ImportedFunctions.emplace_back(&
I);
731 ImportedGlobals.emplace_back(&
I);
733 ImportedTags.emplace_back(&
I);
735 ImportedTables.emplace_back(&
I);
739 wasm::WasmSymbolInfo
Info;
740 const wasm::WasmSignature *Signature =
nullptr;
741 const wasm::WasmGlobalType *GlobalType =
nullptr;
742 const wasm::WasmTableType *TableType =
nullptr;
751 if (!isValidFunctionIndex(
Info.ElementIndex) ||
752 IsDefined != isDefinedFunctionIndex(
Info.ElementIndex))
757 unsigned FuncIndex =
Info.ElementIndex - NumImportedFunctions;
758 wasm::WasmFunction &
Function = Functions[FuncIndex];
759 Signature = &Signatures[
Function.SigIndex];
763 wasm::WasmImport &
Import = *ImportedFunctions[
Info.ElementIndex];
770 Signature = &Signatures[
Import.SigIndex];
777 if (!isValidGlobalIndex(
Info.ElementIndex) ||
778 IsDefined != isDefinedGlobalIndex(
Info.ElementIndex))
787 unsigned GlobalIndex =
Info.ElementIndex - NumImportedGlobals;
788 wasm::WasmGlobal &
Global = Globals[GlobalIndex];
790 if (
Global.SymbolName.empty())
793 wasm::WasmImport &
Import = *ImportedGlobals[
Info.ElementIndex];
800 GlobalType = &
Import.Global;
807 if (!isValidTableNumber(
Info.ElementIndex) ||
808 IsDefined != isDefinedTableNumber(
Info.ElementIndex))
817 unsigned TableNumber =
Info.ElementIndex - NumImportedTables;
818 wasm::WasmTable &
Table = Tables[TableNumber];
819 TableType = &
Table.Type;
820 if (
Table.SymbolName.empty())
823 wasm::WasmImport &
Import = *ImportedTables[
Info.ElementIndex];
830 TableType = &
Import.Table;
842 "common symbols cannot be absolute: " +
Info.Name,
848 "invalid common symbol alignment: `" +
Info.Name +
849 "` (alignment: " + Twine(
unsigned(Alignment)) +
")",
857 if (Index >= DataSegments.size())
859 "invalid data segment index: " + Twine(Index),
861 size_t SegmentSize = DataSegments[
Index].Data.Content.size();
864 "invalid data symbol offset: `" +
Info.Name +
865 "` (offset: " + Twine(
Offset) +
866 " segment size: " + Twine(SegmentSize) +
")",
878 "section symbols must have local binding",
889 if (!isValidTagIndex(
Info.ElementIndex) ||
890 IsDefined != isDefinedTagIndex(
Info.ElementIndex))
899 unsigned TagIndex =
Info.ElementIndex - NumImportedTags;
900 wasm::WasmTag &
Tag = Tags[TagIndex];
901 Signature = &Signatures[
Tag.SigIndex];
902 if (
Tag.SymbolName.empty())
906 wasm::WasmImport &
Import = *ImportedTags[
Info.ElementIndex];
913 Signature = &Signatures[
Import.SigIndex];
921 Twine(
unsigned(
Info.Kind)),
931 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
932 LLVM_DEBUG(
dbgs() <<
"Adding symbol: " << Symbols.back() <<
"\n");
938Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) {
940 StringSet<> ComdatSet;
941 for (
unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) {
943 if (
Name.empty() || !ComdatSet.
insert(Name).second)
947 LinkingData.Comdats.emplace_back(Name);
954 while (EntryCount--) {
962 if (Index >= DataSegments.size())
965 if (DataSegments[Index].
Data.Comdat != UINT32_MAX)
968 DataSegments[
Index].Data.Comdat = ComdatIndex;
971 if (!isDefinedFunctionIndex(Index))
974 if (getDefinedFunction(Index).Comdat != UINT32_MAX)
977 getDefinedFunction(Index).Comdat = ComdatIndex;
980 if (Index >= Sections.size())
986 Sections[
Index].Comdat = ComdatIndex;
994Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) {
995 llvm::SmallSet<StringRef, 3> FieldsSeen;
997 for (
size_t I = 0;
I < Fields; ++
I) {
999 if (!FieldsSeen.
insert(FieldName).second)
1001 "producers section does not have unique fields",
1003 std::vector<std::pair<std::string, std::string>> *ProducerVec =
nullptr;
1004 if (FieldName ==
"language") {
1005 ProducerVec = &ProducerInfo.Languages;
1006 }
else if (FieldName ==
"processed-by") {
1007 ProducerVec = &ProducerInfo.Tools;
1008 }
else if (FieldName ==
"sdk") {
1009 ProducerVec = &ProducerInfo.SDKs;
1012 "producers section field is not named one of language, processed-by, "
1017 llvm::SmallSet<StringRef, 8> ProducersSeen;
1018 for (
size_t J = 0; J < ValueCount; ++J) {
1021 if (!ProducersSeen.
insert(Name).second) {
1023 "producers section contains repeated producer",
1026 ProducerVec->emplace_back(std::string(Name), std::string(
Version));
1029 if (Ctx.Ptr != Ctx.End)
1035Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) {
1036 llvm::SmallSet<std::string, 8> FeaturesSeen;
1039 wasm::WasmFeatureEntry Feature;
1041 switch (Feature.
Prefix) {
1050 if (!FeaturesSeen.
insert(Feature.
Name).second)
1052 "target features section contains repeated feature \"" +
1053 Feature.
Name +
"\"",
1055 TargetFeatures.push_back(Feature);
1057 if (Ctx.Ptr != Ctx.End)
1059 "target features section ended prematurely",
1064Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
1066 if (SectionIndex >= Sections.size())
1069 WasmSection &
Section = Sections[SectionIndex];
1071 uint32_t EndOffset =
Section.Content.size();
1072 uint32_t PreviousOffset = 0;
1073 while (RelocCount--) {
1074 wasm::WasmRelocation Reloc = {};
1078 if (Reloc.
Offset < PreviousOffset)
1082 auto badReloc = [&](StringRef msg) {
1083 if (Reloc.
Index >= Symbols.size())
1085 msg +
": index " + Twine(Reloc.
Index) +
" out of range",
1088 msg +
": " + Twine(Symbols[Reloc.
Index].Info.Name),
1092 PreviousOffset = Reloc.
Offset;
1095 case wasm::R_WASM_FUNCTION_INDEX_LEB:
1096 case wasm::R_WASM_FUNCTION_INDEX_I32:
1097 case wasm::R_WASM_TABLE_INDEX_SLEB:
1098 case wasm::R_WASM_TABLE_INDEX_SLEB64:
1099 case wasm::R_WASM_TABLE_INDEX_I32:
1100 case wasm::R_WASM_TABLE_INDEX_I64:
1101 case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
1102 case wasm::R_WASM_TABLE_INDEX_REL_SLEB64:
1103 if (!isValidFunctionSymbol(Reloc.
Index))
1104 return badReloc(
"invalid function relocation");
1106 case wasm::R_WASM_TABLE_NUMBER_LEB:
1107 if (!isValidTableSymbol(Reloc.
Index))
1108 return badReloc(
"invalid table relocation");
1110 case wasm::R_WASM_TYPE_INDEX_LEB:
1111 if (Reloc.
Index >= Signatures.size())
1112 return badReloc(
"invalid relocation type index");
1114 case wasm::R_WASM_GLOBAL_INDEX_LEB:
1117 if (!isValidGlobalSymbol(Reloc.
Index) &&
1118 !isValidDataSymbol(Reloc.
Index) &&
1119 !isValidFunctionSymbol(Reloc.
Index))
1120 return badReloc(
"invalid global relocation");
1122 case wasm::R_WASM_GLOBAL_INDEX_I32:
1123 if (!isValidGlobalSymbol(Reloc.
Index))
1124 return badReloc(
"invalid global relocation");
1126 case wasm::R_WASM_TAG_INDEX_LEB:
1127 if (!isValidTagSymbol(Reloc.
Index))
1128 return badReloc(
"invalid tag relocation");
1130 case wasm::R_WASM_MEMORY_ADDR_LEB:
1131 case wasm::R_WASM_MEMORY_ADDR_SLEB:
1132 case wasm::R_WASM_MEMORY_ADDR_I32:
1133 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
1134 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB:
1135 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
1136 if (!isValidDataSymbol(Reloc.
Index))
1137 return badReloc(
"invalid data relocation");
1140 case wasm::R_WASM_MEMORY_ADDR_LEB64:
1141 case wasm::R_WASM_MEMORY_ADDR_SLEB64:
1142 case wasm::R_WASM_MEMORY_ADDR_I64:
1143 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
1144 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64:
1145 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I64:
1146 if (!isValidDataSymbol(Reloc.
Index))
1147 return badReloc(
"invalid data relocation");
1150 case wasm::R_WASM_FUNCTION_OFFSET_I32:
1151 if (!isValidFunctionSymbol(Reloc.
Index))
1152 return badReloc(
"invalid function relocation");
1155 case wasm::R_WASM_FUNCTION_OFFSET_I64:
1156 if (!isValidFunctionSymbol(Reloc.
Index))
1157 return badReloc(
"invalid function relocation");
1160 case wasm::R_WASM_SECTION_OFFSET_I32:
1161 if (!isValidSectionSymbol(Reloc.
Index))
1162 return badReloc(
"invalid section relocation");
1175 if (Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_LEB64 ||
1176 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_SLEB64 ||
1177 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_REL_SLEB64)
1179 if (Reloc.
Type == wasm::R_WASM_TABLE_INDEX_I32 ||
1180 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_I32 ||
1181 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I32 ||
1182 Reloc.
Type == wasm::R_WASM_SECTION_OFFSET_I32 ||
1183 Reloc.
Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
1184 Reloc.
Type == wasm::R_WASM_FUNCTION_INDEX_I32 ||
1185 Reloc.
Type == wasm::R_WASM_GLOBAL_INDEX_I32)
1187 if (Reloc.
Type == wasm::R_WASM_TABLE_INDEX_I64 ||
1188 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_I64 ||
1189 Reloc.
Type == wasm::R_WASM_FUNCTION_OFFSET_I64 ||
1190 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I64)
1196 Section.Relocations.push_back(Reloc);
1198 if (Ctx.Ptr != Ctx.End)
1204Error WasmObjectFile::parseCustomSection(
WasmSection &Sec, ReadContext &Ctx) {
1205 if (Sec.
Name ==
"dylink") {
1206 if (
Error Err = parseDylinkSection(Ctx))
1208 }
else if (Sec.
Name ==
"dylink.0") {
1209 if (
Error Err = parseDylink0Section(Ctx))
1211 }
else if (Sec.
Name ==
"name") {
1212 if (
Error Err = parseNameSection(Ctx))
1214 }
else if (Sec.
Name ==
"linking") {
1215 if (
Error Err = parseLinkingSection(Ctx))
1217 }
else if (Sec.
Name ==
"producers") {
1218 if (
Error Err = parseProducersSection(Ctx))
1220 }
else if (Sec.
Name ==
"target_features") {
1221 if (
Error Err = parseTargetFeaturesSection(Ctx))
1224 if (
Error Err = parseRelocSection(Sec.
Name, Ctx))
1230Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
1231 auto parseFieldDef = [&]() {
1238 Signatures.reserve(
Count);
1240 wasm::WasmSignature Sig;
1249 Signatures.reserve(Signatures.size() + RecSize);
1252 Signatures.push_back(std::move(Sig));
1253 HasUnmodeledTypes =
true;
1272 while (FieldCount--) {
1282 Signatures.push_back(std::move(Sig));
1283 HasUnmodeledTypes =
true;
1289 while (ParamCount--) {
1294 while (ReturnCount--) {
1299 Signatures.push_back(std::move(Sig));
1301 if (Ctx.Ptr != Ctx.End)
1307Error WasmObjectFile::parseImport(ReadContext &Ctx, wasm::WasmImport &Im) {
1310 NumImportedFunctions++;
1312 if (Im.
SigIndex >= Signatures.size())
1317 NumImportedGlobals++;
1328 NumImportedTables++;
1344 if (Im.
SigIndex >= Signatures.size())
1350 Twine(
unsigned(Im.
Kind)),
1353 Imports.push_back(Im);
1357Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
1359 Imports.reserve(
Count);
1362 wasm::WasmImport Im;
1367 if (Im.
Kind == 0x7E && Im.
Field ==
"") {
1369 "compact import format (0x7E) is not yet supported",
1371 }
else if (Im.
Kind == 0x7F && Im.
Field ==
"") {
1373 while (NumCompactImports--) {
1376 Error rtn = parseImport(Ctx, Im);
1382 Error rtn = parseImport(Ctx, Im);
1388 if (Ctx.Ptr != Ctx.End)
1394Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
1396 Functions.reserve(
Count);
1397 uint32_t NumTypes = Signatures.size();
1400 if (
Type >= NumTypes)
1403 wasm::WasmFunction
F;
1405 Functions.push_back(
F);
1407 if (Ctx.Ptr != Ctx.End)
1413Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
1414 TableSection = Sections.size();
1416 Tables.reserve(
Count);
1420 T.Index = NumImportedTables + Tables.size();
1421 Tables.push_back(
T);
1422 auto ElemType = Tables.back().Type.ElemType;
1431 if (Ctx.Ptr != Ctx.End)
1437Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
1439 Memories.reserve(
Count);
1444 Memories.push_back(Limits);
1446 if (Ctx.Ptr != Ctx.End)
1452Error WasmObjectFile::parseTagSection(ReadContext &Ctx) {
1453 TagSection = Sections.size();
1455 Tags.reserve(
Count);
1456 uint32_t NumTypes = Signatures.size();
1462 if (
Type >= NumTypes)
1466 Tag.Index = NumImportedTags + Tags.size();
1469 Tags.push_back(
Tag);
1472 if (Ctx.Ptr != Ctx.End)
1478Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
1479 GlobalSection = Sections.size();
1480 const uint8_t *SectionStart = Ctx.Ptr;
1482 Globals.reserve(
Count);
1485 Global.Index = NumImportedGlobals + Globals.size();
1486 const uint8_t *GlobalStart = Ctx.Ptr;
1487 Global.Offset =
static_cast<uint32_t
>(GlobalStart - SectionStart);
1493 Global.Size =
static_cast<uint32_t
>(Ctx.Ptr - GlobalStart);
1494 Globals.push_back(
Global);
1496 if (Ctx.Ptr != Ctx.End)
1502Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
1504 Exports.reserve(
Count);
1505 Symbols.reserve(
Count);
1509 if (HasDylinkSection) {
1510 for (
const auto &ExportInfo : DylinkInfo.ExportInfo) {
1515 for (uint32_t
I = 0;
I <
Count;
I++) {
1516 wasm::WasmExport Ex;
1520 const wasm::WasmSignature *Signature =
nullptr;
1521 const wasm::WasmGlobalType *GlobalType =
nullptr;
1522 const wasm::WasmTableType *TableType =
nullptr;
1523 wasm::WasmSymbolInfo
Info;
1528 if (HasDylinkSection) {
1531 Info.Flags = It->second;
1536 if (!isValidFunctionIndex(Ex.
Index))
1541 if (isDefinedFunctionIndex(Ex.
Index)) {
1542 getDefinedFunction(Ex.
Index).ExportName = Ex.
Name;
1543 unsigned FuncIndex =
Info.ElementIndex - NumImportedFunctions;
1544 wasm::WasmFunction &
Function = Functions[FuncIndex];
1545 Signature = &Signatures[
Function.SigIndex];
1553 if (!isValidGlobalIndex(Ex.
Index))
1558 if (isDefinedGlobalIndex(Ex.
Index)) {
1560 if (!
Global.InitExpr.Extended) {
1561 auto Inst =
Global.InitExpr.Inst;
1563 Offset = Inst.Value.Int32;
1565 Offset = Inst.Value.Int64;
1569 Info.DataRef = wasm::WasmDataReference{0,
Offset, 0};
1573 if (!isValidTagIndex(Ex.
Index))
1578 if (isDefinedTagIndex(Ex.
Index)) {
1579 unsigned TagIndex = Ex.
Index - NumImportedTags;
1580 Signature = &Signatures[Tags[TagIndex].SigIndex];
1593 Exports.push_back(Ex);
1595 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
1596 LLVM_DEBUG(
dbgs() <<
"Adding symbol: " << Symbols.back() <<
"\n");
1599 if (Ctx.Ptr != Ctx.End)
1605bool WasmObjectFile::isValidFunctionIndex(uint32_t Index)
const {
1606 return Index < NumImportedFunctions + Functions.size();
1609bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index)
const {
1610 return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
1613bool WasmObjectFile::isValidGlobalIndex(uint32_t Index)
const {
1614 return Index < NumImportedGlobals + Globals.size();
1617bool WasmObjectFile::isValidTableNumber(uint32_t Index)
const {
1618 return Index < NumImportedTables + Tables.size();
1621bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index)
const {
1622 return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
1625bool WasmObjectFile::isDefinedTableNumber(uint32_t Index)
const {
1626 return Index >= NumImportedTables && isValidTableNumber(Index);
1629bool WasmObjectFile::isValidTagIndex(uint32_t Index)
const {
1630 return Index < NumImportedTags + Tags.size();
1633bool WasmObjectFile::isDefinedTagIndex(uint32_t Index)
const {
1634 return Index >= NumImportedTags && isValidTagIndex(Index);
1637bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index)
const {
1638 return Index < Symbols.size() && Symbols[
Index].isTypeFunction();
1641bool WasmObjectFile::isValidTableSymbol(uint32_t Index)
const {
1642 return Index < Symbols.size() && Symbols[
Index].isTypeTable();
1645bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index)
const {
1646 return Index < Symbols.size() && Symbols[
Index].isTypeGlobal();
1649bool WasmObjectFile::isValidTagSymbol(uint32_t Index)
const {
1650 return Index < Symbols.size() && Symbols[
Index].isTypeTag();
1653bool WasmObjectFile::isValidDataSymbol(uint32_t Index)
const {
1654 return Index < Symbols.size() && Symbols[
Index].isTypeData();
1657bool WasmObjectFile::isValidSectionSymbol(uint32_t Index)
const {
1658 return Index < Symbols.size() && Symbols[
Index].isTypeSection();
1661wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
1662 assert(isDefinedFunctionIndex(Index));
1663 return Functions[
Index - NumImportedFunctions];
1666const wasm::WasmFunction &
1667WasmObjectFile::getDefinedFunction(uint32_t Index)
const {
1668 assert(isDefinedFunctionIndex(Index));
1669 return Functions[
Index - NumImportedFunctions];
1672const wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index)
const {
1673 assert(isDefinedGlobalIndex(Index));
1674 return Globals[
Index - NumImportedGlobals];
1677wasm::WasmTag &WasmObjectFile::getDefinedTag(uint32_t Index) {
1678 assert(isDefinedTagIndex(Index));
1679 return Tags[
Index - NumImportedTags];
1682Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
1684 if (!isValidFunctionIndex(StartFunction))
1690Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) {
1691 CodeSection = Sections.size();
1693 if (FunctionCount != Functions.size()) {
1698 for (uint32_t i = 0; i < FunctionCount; i++) {
1699 wasm::WasmFunction&
Function = Functions[i];
1700 const uint8_t *FunctionStart = Ctx.Ptr;
1702 const uint8_t *FunctionEnd = Ctx.Ptr +
Size;
1704 Function.CodeOffset = Ctx.Ptr - FunctionStart;
1705 Function.Index = NumImportedFunctions + i;
1706 Function.CodeSectionOffset = FunctionStart - Ctx.Start;
1707 Function.Size = FunctionEnd - FunctionStart;
1710 Function.Locals.reserve(NumLocalDecls);
1711 while (NumLocalDecls--) {
1712 wasm::WasmLocalDecl Decl;
1718 uint32_t BodySize = FunctionEnd - Ctx.Ptr;
1720 if (Ctx.Ptr + BodySize > Ctx.End) {
1724 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize);
1727 Ctx.Ptr += BodySize;
1728 assert(Ctx.Ptr == FunctionEnd);
1730 if (Ctx.Ptr != Ctx.End)
1736Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
1738 ElemSegments.reserve(
Count);
1740 wasm::WasmElemSegment Segment;
1746 if (Segment.
Flags & ~SupportedFlags)
1758 bool HasTableNumber =
1805 }
else if (HasElemType) {
1815 while (NumElems--) {
1816 wasm::WasmInitExpr Expr;
1821 while (NumElems--) {
1825 ElemSegments.push_back(Segment);
1827 if (Ctx.Ptr != Ctx.End)
1833Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
1834 DataSection = Sections.size();
1836 if (DataCount &&
Count != *DataCount)
1838 "number of data segments does not match DataCount section");
1839 DataSegments.reserve(
Count);
1841 WasmSegment Segment;
1856 if (
Size > (
size_t)(Ctx.End - Ctx.Ptr))
1867 DataSegments.push_back(Segment);
1869 if (Ctx.Ptr != Ctx.End)
1875Error WasmObjectFile::parseDataCountSection(ReadContext &Ctx) {
1890 LLVM_DEBUG(
dbgs() <<
"getSymbolFlags: ptr=" << &Sym <<
" " << Sym <<
"\n");
1914 Ref.d.b = Symbols.size();
1919 return Symbols[Symb.
d.
b];
1932 if (!Sym.isDefined())
1939 isDefinedFunctionIndex(Sym.Info.ElementIndex)) {
1940 return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset +
1944 isDefinedGlobalIndex(Sym.Info.ElementIndex)) {
1945 return getDefinedGlobal(Sym.Info.ElementIndex).Offset + SectionAddress;
2025 Ref.d.a = getSymbolSectionIdImpl(Sym);
2031 return getSymbolSectionIdImpl(Sym);
2039 return GlobalSection;
2047 return TableSection;
2085 : Sections[Sec.
d.
a].Offset;
2127 RelocRef.
d.
a =
Ref.d.a;
2135 RelocRef.
d.
a =
Ref.d.a;
2149 if (Rel.
Type == wasm::R_WASM_TYPE_INDEX_LEB)
2167#define WASM_RELOC(name, value) \
2173#include "llvm/BinaryFormat/WasmRelocs.def"
2178 Result.append(Res.
begin(), Res.
end());
2189 Ref.d.a = Sections.size();
2194 return HasMemory64 ? 8 : 4;
2213 return Sections[
Ref.d.a];
2234int WasmSectionOrderChecker::getSectionOrder(
unsigned ID,
2282 [WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = {
2286 {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT},
2288 {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION},
2290 {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE},
2292 {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY},
2294 {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_TAG},
2296 {WASM_SEC_ORDER_TAG, WASM_SEC_ORDER_GLOBAL},
2298 {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EXPORT},
2300 {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START},
2302 {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM},
2304 {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT},
2306 {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE},
2308 {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA},
2310 {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING},
2314 {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE},
2316 {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME},
2320 {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS},
2322 {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES},
2324 {WASM_SEC_ORDER_TARGET_FEATURES}};
2328 int Order = getSectionOrder(ID, CustomSectionName);
2341 for (
size_t I = 0;; ++
I) {
2348 Checked[
Next] =
true;
2351 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