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));
517 if (Ctx.Ptr != Ctx.End) {
523 if (Ctx.Ptr != Ctx.End)
529Error WasmObjectFile::parseNameSection(ReadContext &Ctx) {
530 llvm::DenseSet<uint64_t> SeenFunctions;
531 llvm::DenseSet<uint64_t> SeenGlobals;
532 llvm::DenseSet<uint64_t> SeenSegments;
536 bool PopulateSymbolTable = !HasLinkingSection && !HasDylinkSection;
540 if (PopulateSymbolTable)
543 while (Ctx.Ptr < Ctx.End) {
546 const uint8_t *SubSectionEnd = Ctx.Ptr +
Size;
564 const wasm::WasmSignature *Signature =
nullptr;
565 const wasm::WasmGlobalType *GlobalType =
nullptr;
566 const wasm::WasmTableType *TableType =
nullptr;
568 if (!SeenFunctions.
insert(Index).second)
571 if (!isValidFunctionIndex(Index) ||
Name.empty())
575 if (isDefinedFunctionIndex(Index)) {
576 wasm::WasmFunction &
F = getDefinedFunction(Index);
578 Signature = &Signatures[
F.SigIndex];
580 Info.ExportName =
F.ExportName;
589 if (!SeenGlobals.
insert(Index).second)
592 if (!isValidGlobalIndex(Index) ||
Name.empty())
597 if (isDefinedGlobalIndex(Index)) {
598 GlobalType = &getDefinedGlobal(Index).
Type;
603 if (!SeenSegments.
insert(Index).second)
606 if (Index >= DataSegments.size())
612 assert(Index < DataSegments.size());
613 Info.DataRef = wasm::WasmDataReference{
614 Index, 0, DataSegments[
Index].Data.Content.size()};
616 DebugNames.push_back(wasm::WasmDebugName{nameType,
Index,
Name});
617 if (PopulateSymbolTable)
618 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
628 if (Ctx.Ptr != SubSectionEnd)
633 if (Ctx.Ptr != Ctx.End)
639Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) {
640 HasLinkingSection =
true;
645 "unexpected metadata version: " + Twine(LinkingData.Version) +
650 const uint8_t *OrigEnd = Ctx.End;
651 while (Ctx.Ptr < OrigEnd) {
657 Ctx.End = Ctx.Ptr +
Size;
660 if (
Error Err = parseLinkingSectionSymtab(Ctx))
665 if (
Count > DataSegments.size())
668 for (uint32_t
I = 0;
I <
Count;
I++) {
671 if (DataSegments[
I].
Data.Alignment > 32)
673 "invalid data segment alignment: `" + DataSegments[
I].
Data.Name +
674 "` (alignment: " + Twine(DataSegments[
I].
Data.Alignment) +
683 LinkingData.InitFunctions.reserve(
Count);
684 for (uint32_t
I = 0;
I <
Count;
I++) {
685 wasm::WasmInitFunc Init;
688 if (!isValidFunctionSymbol(Init.
Symbol))
692 LinkingData.InitFunctions.emplace_back(Init);
697 if (
Error Err = parseLinkingSectionComdat(Ctx))
707 if (Ctx.Ptr != Ctx.End)
711 if (Ctx.Ptr != OrigEnd)
717Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
722 Symbols.reserve(
Count);
723 StringSet<> SymbolNames;
725 std::vector<wasm::WasmImport *> ImportedGlobals;
726 std::vector<wasm::WasmImport *> ImportedFunctions;
727 std::vector<wasm::WasmImport *> ImportedTags;
728 std::vector<wasm::WasmImport *> ImportedTables;
729 ImportedGlobals.reserve(Imports.size());
730 ImportedFunctions.reserve(Imports.size());
731 ImportedTags.reserve(Imports.size());
732 ImportedTables.reserve(Imports.size());
733 for (
auto &
I : Imports) {
735 ImportedFunctions.emplace_back(&
I);
737 ImportedGlobals.emplace_back(&
I);
739 ImportedTags.emplace_back(&
I);
741 ImportedTables.emplace_back(&
I);
745 wasm::WasmSymbolInfo
Info;
746 const wasm::WasmSignature *Signature =
nullptr;
747 const wasm::WasmGlobalType *GlobalType =
nullptr;
748 const wasm::WasmTableType *TableType =
nullptr;
757 if (!isValidFunctionIndex(
Info.ElementIndex) ||
758 IsDefined != isDefinedFunctionIndex(
Info.ElementIndex))
763 unsigned FuncIndex =
Info.ElementIndex - NumImportedFunctions;
764 wasm::WasmFunction &
Function = Functions[FuncIndex];
765 Signature = &Signatures[
Function.SigIndex];
769 wasm::WasmImport &
Import = *ImportedFunctions[
Info.ElementIndex];
776 Signature = &Signatures[
Import.SigIndex];
783 if (!isValidGlobalIndex(
Info.ElementIndex) ||
784 IsDefined != isDefinedGlobalIndex(
Info.ElementIndex))
793 unsigned GlobalIndex =
Info.ElementIndex - NumImportedGlobals;
794 wasm::WasmGlobal &
Global = Globals[GlobalIndex];
796 if (
Global.SymbolName.empty())
799 wasm::WasmImport &
Import = *ImportedGlobals[
Info.ElementIndex];
806 GlobalType = &
Import.Global;
813 if (!isValidTableNumber(
Info.ElementIndex) ||
814 IsDefined != isDefinedTableNumber(
Info.ElementIndex))
823 unsigned TableNumber =
Info.ElementIndex - NumImportedTables;
824 wasm::WasmTable &
Table = Tables[TableNumber];
825 TableType = &
Table.Type;
826 if (
Table.SymbolName.empty())
829 wasm::WasmImport &
Import = *ImportedTables[
Info.ElementIndex];
836 TableType = &
Import.Table;
848 "common symbols cannot be absolute: " +
Info.Name,
854 "invalid common symbol alignment: `" +
Info.Name +
855 "` (alignment: " + Twine(
unsigned(Alignment)) +
")",
863 if (Index >= DataSegments.size())
865 "invalid data segment index: " + Twine(Index),
867 size_t SegmentSize = DataSegments[
Index].Data.Content.size();
870 "invalid data symbol offset: `" +
Info.Name +
871 "` (offset: " + Twine(
Offset) +
872 " segment size: " + Twine(SegmentSize) +
")",
884 "section symbols must have local binding",
895 if (!isValidTagIndex(
Info.ElementIndex) ||
896 IsDefined != isDefinedTagIndex(
Info.ElementIndex))
905 unsigned TagIndex =
Info.ElementIndex - NumImportedTags;
906 wasm::WasmTag &
Tag = Tags[TagIndex];
907 Signature = &Signatures[
Tag.SigIndex];
908 if (
Tag.SymbolName.empty())
912 wasm::WasmImport &
Import = *ImportedTags[
Info.ElementIndex];
919 Signature = &Signatures[
Import.SigIndex];
927 Twine(
unsigned(
Info.Kind)),
937 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
938 LLVM_DEBUG(
dbgs() <<
"Adding symbol: " << Symbols.back() <<
"\n");
944Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) {
946 StringSet<> ComdatSet;
947 for (
unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) {
949 if (
Name.empty() || !ComdatSet.
insert(Name).second)
953 LinkingData.Comdats.emplace_back(Name);
960 while (EntryCount--) {
968 if (Index >= DataSegments.size())
971 if (DataSegments[Index].
Data.Comdat != UINT32_MAX)
974 DataSegments[
Index].Data.Comdat = ComdatIndex;
977 if (!isDefinedFunctionIndex(Index))
980 if (getDefinedFunction(Index).Comdat != UINT32_MAX)
983 getDefinedFunction(Index).Comdat = ComdatIndex;
986 if (Index >= Sections.size())
992 Sections[
Index].Comdat = ComdatIndex;
1000Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) {
1001 llvm::SmallSet<StringRef, 3> FieldsSeen;
1003 for (
size_t I = 0;
I < Fields; ++
I) {
1005 if (!FieldsSeen.
insert(FieldName).second)
1007 "producers section does not have unique fields",
1009 std::vector<std::pair<std::string, std::string>> *ProducerVec =
nullptr;
1010 if (FieldName ==
"language") {
1011 ProducerVec = &ProducerInfo.Languages;
1012 }
else if (FieldName ==
"processed-by") {
1013 ProducerVec = &ProducerInfo.Tools;
1014 }
else if (FieldName ==
"sdk") {
1015 ProducerVec = &ProducerInfo.SDKs;
1018 "producers section field is not named one of language, processed-by, "
1023 llvm::SmallSet<StringRef, 8> ProducersSeen;
1024 for (
size_t J = 0; J < ValueCount; ++J) {
1027 if (!ProducersSeen.
insert(Name).second) {
1029 "producers section contains repeated producer",
1032 ProducerVec->emplace_back(std::string(Name), std::string(
Version));
1035 if (Ctx.Ptr != Ctx.End)
1041Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) {
1042 llvm::SmallSet<std::string, 8> FeaturesSeen;
1045 wasm::WasmFeatureEntry Feature;
1047 switch (Feature.
Prefix) {
1056 if (!FeaturesSeen.
insert(Feature.
Name).second)
1058 "target features section contains repeated feature \"" +
1059 Feature.
Name +
"\"",
1061 TargetFeatures.push_back(Feature);
1063 if (Ctx.Ptr != Ctx.End)
1065 "target features section ended prematurely",
1070Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
1072 if (SectionIndex >= Sections.size())
1075 WasmSection &
Section = Sections[SectionIndex];
1077 uint32_t EndOffset =
Section.Content.size();
1078 uint32_t PreviousOffset = 0;
1079 while (RelocCount--) {
1080 wasm::WasmRelocation Reloc = {};
1084 if (Reloc.
Offset < PreviousOffset)
1088 auto badReloc = [&](StringRef msg) {
1089 if (Reloc.
Index >= Symbols.size())
1091 msg +
": index " + Twine(Reloc.
Index) +
" out of range",
1094 msg +
": " + Twine(Symbols[Reloc.
Index].Info.Name),
1098 PreviousOffset = Reloc.
Offset;
1101 case wasm::R_WASM_FUNCTION_INDEX_LEB:
1102 case wasm::R_WASM_FUNCTION_INDEX_I32:
1103 case wasm::R_WASM_TABLE_INDEX_SLEB:
1104 case wasm::R_WASM_TABLE_INDEX_SLEB64:
1105 case wasm::R_WASM_TABLE_INDEX_I32:
1106 case wasm::R_WASM_TABLE_INDEX_I64:
1107 case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
1108 case wasm::R_WASM_TABLE_INDEX_REL_SLEB64:
1109 if (!isValidFunctionSymbol(Reloc.
Index))
1110 return badReloc(
"invalid function relocation");
1112 case wasm::R_WASM_TABLE_NUMBER_LEB:
1113 if (!isValidTableSymbol(Reloc.
Index))
1114 return badReloc(
"invalid table relocation");
1116 case wasm::R_WASM_TYPE_INDEX_LEB:
1117 if (Reloc.
Index >= Signatures.size())
1118 return badReloc(
"invalid relocation type index");
1120 case wasm::R_WASM_GLOBAL_INDEX_LEB:
1123 if (!isValidGlobalSymbol(Reloc.
Index) &&
1124 !isValidDataSymbol(Reloc.
Index) &&
1125 !isValidFunctionSymbol(Reloc.
Index))
1126 return badReloc(
"invalid global relocation");
1128 case wasm::R_WASM_GLOBAL_INDEX_I32:
1129 if (!isValidGlobalSymbol(Reloc.
Index))
1130 return badReloc(
"invalid global relocation");
1132 case wasm::R_WASM_TAG_INDEX_LEB:
1133 if (!isValidTagSymbol(Reloc.
Index))
1134 return badReloc(
"invalid tag relocation");
1136 case wasm::R_WASM_MEMORY_ADDR_LEB:
1137 case wasm::R_WASM_MEMORY_ADDR_SLEB:
1138 case wasm::R_WASM_MEMORY_ADDR_I32:
1139 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
1140 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB:
1141 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
1142 if (!isValidDataSymbol(Reloc.
Index))
1143 return badReloc(
"invalid data relocation");
1146 case wasm::R_WASM_MEMORY_ADDR_LEB64:
1147 case wasm::R_WASM_MEMORY_ADDR_SLEB64:
1148 case wasm::R_WASM_MEMORY_ADDR_I64:
1149 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
1150 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64:
1151 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I64:
1152 if (!isValidDataSymbol(Reloc.
Index))
1153 return badReloc(
"invalid data relocation");
1156 case wasm::R_WASM_FUNCTION_OFFSET_I32:
1157 if (!isValidFunctionSymbol(Reloc.
Index))
1158 return badReloc(
"invalid function relocation");
1161 case wasm::R_WASM_FUNCTION_OFFSET_I64:
1162 if (!isValidFunctionSymbol(Reloc.
Index))
1163 return badReloc(
"invalid function relocation");
1166 case wasm::R_WASM_SECTION_OFFSET_I32:
1167 if (!isValidSectionSymbol(Reloc.
Index))
1168 return badReloc(
"invalid section relocation");
1181 if (Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_LEB64 ||
1182 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_SLEB64 ||
1183 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_REL_SLEB64)
1185 if (Reloc.
Type == wasm::R_WASM_TABLE_INDEX_I32 ||
1186 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_I32 ||
1187 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I32 ||
1188 Reloc.
Type == wasm::R_WASM_SECTION_OFFSET_I32 ||
1189 Reloc.
Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
1190 Reloc.
Type == wasm::R_WASM_FUNCTION_INDEX_I32 ||
1191 Reloc.
Type == wasm::R_WASM_GLOBAL_INDEX_I32)
1193 if (Reloc.
Type == wasm::R_WASM_TABLE_INDEX_I64 ||
1194 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_I64 ||
1195 Reloc.
Type == wasm::R_WASM_FUNCTION_OFFSET_I64 ||
1196 Reloc.
Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I64)
1202 Section.Relocations.push_back(Reloc);
1204 if (Ctx.Ptr != Ctx.End)
1210Error WasmObjectFile::parseCustomSection(
WasmSection &Sec, ReadContext &Ctx) {
1211 if (Sec.
Name ==
"dylink") {
1212 if (
Error Err = parseDylinkSection(Ctx))
1214 }
else if (Sec.
Name ==
"dylink.0") {
1215 if (
Error Err = parseDylink0Section(Ctx))
1217 }
else if (Sec.
Name ==
"name") {
1218 if (
Error Err = parseNameSection(Ctx))
1220 }
else if (Sec.
Name ==
"linking") {
1221 if (
Error Err = parseLinkingSection(Ctx))
1223 }
else if (Sec.
Name ==
"producers") {
1224 if (
Error Err = parseProducersSection(Ctx))
1226 }
else if (Sec.
Name ==
"target_features") {
1227 if (
Error Err = parseTargetFeaturesSection(Ctx))
1230 if (
Error Err = parseRelocSection(Sec.
Name, Ctx))
1236Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) {
1237 auto parseFieldDef = [&]() {
1244 Signatures.reserve(
Count);
1246 wasm::WasmSignature Sig;
1255 Signatures.reserve(Signatures.size() + RecSize);
1258 Signatures.push_back(std::move(Sig));
1259 HasUnmodeledTypes =
true;
1278 while (FieldCount--) {
1288 Signatures.push_back(std::move(Sig));
1289 HasUnmodeledTypes =
true;
1295 while (ParamCount--) {
1300 while (ReturnCount--) {
1305 Signatures.push_back(std::move(Sig));
1307 if (Ctx.Ptr != Ctx.End)
1313Error WasmObjectFile::parseImport(ReadContext &Ctx, wasm::WasmImport &Im) {
1316 NumImportedFunctions++;
1318 if (Im.
SigIndex >= Signatures.size())
1323 NumImportedGlobals++;
1334 NumImportedTables++;
1350 if (Im.
SigIndex >= Signatures.size())
1356 Twine(
unsigned(Im.
Kind)),
1359 Imports.push_back(Im);
1363Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
1365 Imports.reserve(
Count);
1368 wasm::WasmImport Im;
1373 if (Im.
Kind == 0x7E && Im.
Field ==
"") {
1375 "compact import format (0x7E) is not yet supported",
1377 }
else if (Im.
Kind == 0x7F && Im.
Field ==
"") {
1379 while (NumCompactImports--) {
1382 Error rtn = parseImport(Ctx, Im);
1388 Error rtn = parseImport(Ctx, Im);
1394 if (Ctx.Ptr != Ctx.End)
1400Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) {
1402 Functions.reserve(
Count);
1403 uint32_t NumTypes = Signatures.size();
1406 if (
Type >= NumTypes)
1409 wasm::WasmFunction
F;
1411 Functions.push_back(
F);
1413 if (Ctx.Ptr != Ctx.End)
1419Error WasmObjectFile::parseTableSection(ReadContext &Ctx) {
1420 TableSection = Sections.size();
1422 Tables.reserve(
Count);
1426 T.Index = NumImportedTables + Tables.size();
1427 Tables.push_back(
T);
1428 auto ElemType = Tables.back().Type.ElemType;
1437 if (Ctx.Ptr != Ctx.End)
1443Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) {
1445 Memories.reserve(
Count);
1450 Memories.push_back(Limits);
1452 if (Ctx.Ptr != Ctx.End)
1458Error WasmObjectFile::parseTagSection(ReadContext &Ctx) {
1459 TagSection = Sections.size();
1461 Tags.reserve(
Count);
1462 uint32_t NumTypes = Signatures.size();
1468 if (
Type >= NumTypes)
1472 Tag.Index = NumImportedTags + Tags.size();
1475 Tags.push_back(
Tag);
1478 if (Ctx.Ptr != Ctx.End)
1484Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
1485 GlobalSection = Sections.size();
1486 const uint8_t *SectionStart = Ctx.Ptr;
1488 Globals.reserve(
Count);
1491 Global.Index = NumImportedGlobals + Globals.size();
1492 const uint8_t *GlobalStart = Ctx.Ptr;
1493 Global.Offset =
static_cast<uint32_t
>(GlobalStart - SectionStart);
1499 Global.Size =
static_cast<uint32_t
>(Ctx.Ptr - GlobalStart);
1500 Globals.push_back(
Global);
1502 if (Ctx.Ptr != Ctx.End)
1508Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
1510 Exports.reserve(
Count);
1511 Symbols.reserve(
Count);
1515 if (HasDylinkSection) {
1516 for (
const auto &ExportInfo : DylinkInfo.ExportInfo) {
1521 for (uint32_t
I = 0;
I <
Count;
I++) {
1522 wasm::WasmExport Ex;
1526 const wasm::WasmSignature *Signature =
nullptr;
1527 const wasm::WasmGlobalType *GlobalType =
nullptr;
1528 const wasm::WasmTableType *TableType =
nullptr;
1529 wasm::WasmSymbolInfo
Info;
1534 if (HasDylinkSection) {
1537 Info.Flags = It->second;
1542 if (!isValidFunctionIndex(Ex.
Index))
1547 if (isDefinedFunctionIndex(Ex.
Index)) {
1548 getDefinedFunction(Ex.
Index).ExportName = Ex.
Name;
1549 unsigned FuncIndex =
Info.ElementIndex - NumImportedFunctions;
1550 wasm::WasmFunction &
Function = Functions[FuncIndex];
1551 Signature = &Signatures[
Function.SigIndex];
1559 if (!isValidGlobalIndex(Ex.
Index))
1564 if (isDefinedGlobalIndex(Ex.
Index)) {
1566 if (!
Global.InitExpr.Extended) {
1567 auto Inst =
Global.InitExpr.Inst;
1569 Offset = Inst.Value.Int32;
1571 Offset = Inst.Value.Int64;
1575 Info.DataRef = wasm::WasmDataReference{0,
Offset, 0};
1579 if (!isValidTagIndex(Ex.
Index))
1584 if (isDefinedTagIndex(Ex.
Index)) {
1585 unsigned TagIndex = Ex.
Index - NumImportedTags;
1586 Signature = &Signatures[Tags[TagIndex].SigIndex];
1599 Exports.push_back(Ex);
1601 Symbols.emplace_back(Info, GlobalType, TableType, Signature);
1602 LLVM_DEBUG(
dbgs() <<
"Adding symbol: " << Symbols.back() <<
"\n");
1605 if (Ctx.Ptr != Ctx.End)
1611bool WasmObjectFile::isValidFunctionIndex(uint32_t Index)
const {
1612 return Index < NumImportedFunctions + Functions.size();
1615bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index)
const {
1616 return Index >= NumImportedFunctions && isValidFunctionIndex(Index);
1619bool WasmObjectFile::isValidGlobalIndex(uint32_t Index)
const {
1620 return Index < NumImportedGlobals + Globals.size();
1623bool WasmObjectFile::isValidTableNumber(uint32_t Index)
const {
1624 return Index < NumImportedTables + Tables.size();
1627bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index)
const {
1628 return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
1631bool WasmObjectFile::isDefinedTableNumber(uint32_t Index)
const {
1632 return Index >= NumImportedTables && isValidTableNumber(Index);
1635bool WasmObjectFile::isValidTagIndex(uint32_t Index)
const {
1636 return Index < NumImportedTags + Tags.size();
1639bool WasmObjectFile::isDefinedTagIndex(uint32_t Index)
const {
1640 return Index >= NumImportedTags && isValidTagIndex(Index);
1643bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index)
const {
1644 return Index < Symbols.size() && Symbols[
Index].isTypeFunction();
1647bool WasmObjectFile::isValidTableSymbol(uint32_t Index)
const {
1648 return Index < Symbols.size() && Symbols[
Index].isTypeTable();
1651bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index)
const {
1652 return Index < Symbols.size() && Symbols[
Index].isTypeGlobal();
1655bool WasmObjectFile::isValidTagSymbol(uint32_t Index)
const {
1656 return Index < Symbols.size() && Symbols[
Index].isTypeTag();
1659bool WasmObjectFile::isValidDataSymbol(uint32_t Index)
const {
1660 return Index < Symbols.size() && Symbols[
Index].isTypeData();
1663bool WasmObjectFile::isValidSectionSymbol(uint32_t Index)
const {
1664 return Index < Symbols.size() && Symbols[
Index].isTypeSection();
1667wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) {
1668 assert(isDefinedFunctionIndex(Index));
1669 return Functions[
Index - NumImportedFunctions];
1672const wasm::WasmFunction &
1673WasmObjectFile::getDefinedFunction(uint32_t Index)
const {
1674 assert(isDefinedFunctionIndex(Index));
1675 return Functions[
Index - NumImportedFunctions];
1678const wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index)
const {
1679 assert(isDefinedGlobalIndex(Index));
1680 return Globals[
Index - NumImportedGlobals];
1683wasm::WasmTag &WasmObjectFile::getDefinedTag(uint32_t Index) {
1684 assert(isDefinedTagIndex(Index));
1685 return Tags[
Index - NumImportedTags];
1688Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
1690 if (!isValidFunctionIndex(StartFunction))
1696Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) {
1697 CodeSection = Sections.size();
1699 if (FunctionCount != Functions.size()) {
1704 for (uint32_t i = 0; i < FunctionCount; i++) {
1705 wasm::WasmFunction&
Function = Functions[i];
1706 const uint8_t *FunctionStart = Ctx.Ptr;
1708 const uint8_t *FunctionEnd = Ctx.Ptr +
Size;
1710 Function.CodeOffset = Ctx.Ptr - FunctionStart;
1711 Function.Index = NumImportedFunctions + i;
1712 Function.CodeSectionOffset = FunctionStart - Ctx.Start;
1713 Function.Size = FunctionEnd - FunctionStart;
1716 Function.Locals.reserve(NumLocalDecls);
1717 while (NumLocalDecls--) {
1718 wasm::WasmLocalDecl Decl;
1724 uint32_t BodySize = FunctionEnd - Ctx.Ptr;
1726 if (Ctx.Ptr + BodySize > Ctx.End) {
1730 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize);
1733 Ctx.Ptr += BodySize;
1734 assert(Ctx.Ptr == FunctionEnd);
1736 if (Ctx.Ptr != Ctx.End)
1742Error WasmObjectFile::parseElemSection(ReadContext &Ctx) {
1744 ElemSegments.reserve(
Count);
1746 wasm::WasmElemSegment Segment;
1752 if (Segment.
Flags & ~SupportedFlags)
1764 bool HasTableNumber =
1811 }
else if (HasElemType) {
1821 while (NumElems--) {
1822 wasm::WasmInitExpr Expr;
1827 while (NumElems--) {
1831 ElemSegments.push_back(Segment);
1833 if (Ctx.Ptr != Ctx.End)
1839Error WasmObjectFile::parseDataSection(ReadContext &Ctx) {
1840 DataSection = Sections.size();
1842 if (DataCount &&
Count != *DataCount)
1844 "number of data segments does not match DataCount section");
1845 DataSegments.reserve(
Count);
1847 WasmSegment Segment;
1862 if (
Size > (
size_t)(Ctx.End - Ctx.Ptr))
1873 DataSegments.push_back(Segment);
1875 if (Ctx.Ptr != Ctx.End)
1881Error WasmObjectFile::parseDataCountSection(ReadContext &Ctx) {
1896 LLVM_DEBUG(
dbgs() <<
"getSymbolFlags: ptr=" << &Sym <<
" " << Sym <<
"\n");
1920 Ref.d.b = Symbols.size();
1925 return Symbols[Symb.
d.
b];
1938 if (!Sym.isDefined())
1945 isDefinedFunctionIndex(Sym.Info.ElementIndex)) {
1946 return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset +
1950 isDefinedGlobalIndex(Sym.Info.ElementIndex)) {
1951 return getDefinedGlobal(Sym.Info.ElementIndex).Offset + SectionAddress;
2031 Ref.d.a = getSymbolSectionIdImpl(Sym);
2037 return getSymbolSectionIdImpl(Sym);
2045 return GlobalSection;
2053 return TableSection;
2091 : Sections[Sec.
d.
a].Offset;
2133 RelocRef.
d.
a =
Ref.d.a;
2141 RelocRef.
d.
a =
Ref.d.a;
2155 if (Rel.
Type == wasm::R_WASM_TYPE_INDEX_LEB)
2173#define WASM_RELOC(name, value) \
2179#include "llvm/BinaryFormat/WasmRelocs.def"
2184 Result.append(Res.
begin(), Res.
end());
2195 Ref.d.a = Sections.size();
2206 if (!LinkingData.TargetArch.empty())
2208 if (!DylinkInfo.TargetArch.empty())
2226 return Sections[
Ref.d.a];
2247int WasmSectionOrderChecker::getSectionOrder(
unsigned ID,
2295 [WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = {
2299 {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT},
2301 {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION},
2303 {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE},
2305 {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY},
2307 {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_TAG},
2309 {WASM_SEC_ORDER_TAG, WASM_SEC_ORDER_GLOBAL},
2311 {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EXPORT},
2313 {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START},
2315 {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM},
2317 {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT},
2319 {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE},
2321 {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA},
2323 {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING},
2327 {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE},
2329 {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME},
2333 {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS},
2335 {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES},
2337 {WASM_SEC_ORDER_TARGET_FEATURES}};
2341 int Order = getSectionOrder(ID, CustomSectionName);
2354 for (
size_t I = 0;; ++
I) {
2361 Checked[
Next] =
true;
2364 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.
Triple - Helper class for working with autoconf configuration names.
ArchType getArch() const
Get the parsed architecture type of this triple.
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_DYLINK_TARGET_ARCH
@ 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.
@ 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
uint64_t decodeULEB128(const uint8_t *p, unsigned *n, const uint8_t *end, const char **error, ULEB128DecodeError *errorCode)
Utility function to decode a ULEB128 value and report a typed error.
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