Lines Matching full:wasm
1 //===- WasmObjectFile.cpp - Wasm object file implementation ---------------===//
17 #include "llvm/BinaryFormat/Wasm.h"
23 #include "llvm/Object/Wasm.h"
35 #define DEBUG_TYPE "wasm-object"
42 << ", Kind=" << toString(wasm::WasmSymbolType(Info.Kind)) in print()
167 static Error readInitExpr(wasm::WasmInitExpr &Expr, in readInitExpr()
172 case wasm::WASM_OPCODE_I32_CONST: in readInitExpr()
175 case wasm::WASM_OPCODE_I64_CONST: in readInitExpr()
178 case wasm::WASM_OPCODE_F32_CONST: in readInitExpr()
181 case wasm::WASM_OPCODE_F64_CONST: in readInitExpr()
184 case wasm::WASM_OPCODE_GLOBAL_GET: in readInitExpr()
187 case wasm::WASM_OPCODE_REF_NULL: { in readInitExpr()
188 wasm::ValType Ty = static_cast<wasm::ValType>(readULEB128(Ctx)); in readInitExpr()
189 if (Ty != wasm::ValType::EXTERNREF) { in readInitExpr()
201 if (EndOpcode != wasm::WASM_OPCODE_END) { in readInitExpr()
208 static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) { in readLimits()
209 wasm::WasmLimits Result; in readLimits()
212 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) in readLimits()
217 static wasm::WasmTableType readTableType(WasmObjectFile::ReadContext &Ctx) { in readTableType()
218 wasm::WasmTableType TableType; in readTableType()
236 if (Section.Type == wasm::WASM_SEC_CUSTOM) { in readSection()
282 if (Header.Version != wasm::WasmVersion) { in WasmObjectFile()
306 case wasm::WASM_SEC_CUSTOM: in parseSection()
308 case wasm::WASM_SEC_TYPE: in parseSection()
310 case wasm::WASM_SEC_IMPORT: in parseSection()
312 case wasm::WASM_SEC_FUNCTION: in parseSection()
314 case wasm::WASM_SEC_TABLE: in parseSection()
316 case wasm::WASM_SEC_MEMORY: in parseSection()
318 case wasm::WASM_SEC_EVENT: in parseSection()
320 case wasm::WASM_SEC_GLOBAL: in parseSection()
322 case wasm::WASM_SEC_EXPORT: in parseSection()
324 case wasm::WASM_SEC_START: in parseSection()
326 case wasm::WASM_SEC_ELEM: in parseSection()
328 case wasm::WASM_SEC_CODE: in parseSection()
330 case wasm::WASM_SEC_DATA: in parseSection()
332 case wasm::WASM_SEC_DATACOUNT: in parseSection()
370 case wasm::WASM_NAMES_FUNCTION: in parseNameSection()
371 case wasm::WASM_NAMES_GLOBAL: { in parseNameSection()
376 if (Type == wasm::WASM_NAMES_FUNCTION) { in parseNameSection()
394 wasm::NameType T = Type == wasm::WASM_NAMES_FUNCTION in parseNameSection()
395 ? wasm::NameType::FUNCTION in parseNameSection()
396 : wasm::NameType::GLOBAL; in parseNameSection()
397 DebugNames.push_back(wasm::WasmDebugName{T, Index, Name}); in parseNameSection()
402 case wasm::WASM_NAMES_LOCAL: in parseNameSection()
427 if (LinkingData.Version != wasm::WasmMetadataVersion) { in parseLinkingSection()
430 " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")", in parseLinkingSection()
443 case wasm::WASM_SYMBOL_TABLE: in parseLinkingSection()
447 case wasm::WASM_SEGMENT_INFO: { in parseLinkingSection()
459 case wasm::WASM_INIT_FUNCS: { in parseLinkingSection()
463 wasm::WasmInitFunc Init; in parseLinkingSection()
474 case wasm::WASM_COMDAT_INFO: in parseLinkingSection()
498 std::vector<wasm::WasmImport *> ImportedGlobals; in parseLinkingSectionSymtab()
499 std::vector<wasm::WasmImport *> ImportedFunctions; in parseLinkingSectionSymtab()
500 std::vector<wasm::WasmImport *> ImportedEvents; in parseLinkingSectionSymtab()
501 std::vector<wasm::WasmImport *> ImportedTables; in parseLinkingSectionSymtab()
507 if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION) in parseLinkingSectionSymtab()
509 else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL) in parseLinkingSectionSymtab()
511 else if (I.Kind == wasm::WASM_EXTERNAL_EVENT) in parseLinkingSectionSymtab()
513 else if (I.Kind == wasm::WASM_EXTERNAL_TABLE) in parseLinkingSectionSymtab()
518 wasm::WasmSymbolInfo Info; in parseLinkingSectionSymtab()
519 const wasm::WasmSignature *Signature = nullptr; in parseLinkingSectionSymtab()
520 const wasm::WasmGlobalType *GlobalType = nullptr; in parseLinkingSectionSymtab()
522 const wasm::WasmEventType *EventType = nullptr; in parseLinkingSectionSymtab()
526 bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0; in parseLinkingSectionSymtab()
529 case wasm::WASM_SYMBOL_TYPE_FUNCTION: in parseLinkingSectionSymtab()
539 wasm::WasmFunction &Function = Functions[FuncIndex]; in parseLinkingSectionSymtab()
543 wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex]; in parseLinkingSectionSymtab()
544 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) { in parseLinkingSectionSymtab()
557 case wasm::WASM_SYMBOL_TYPE_GLOBAL: in parseLinkingSectionSymtab()
563 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == in parseLinkingSectionSymtab()
564 wasm::WASM_SYMBOL_BINDING_WEAK) in parseLinkingSectionSymtab()
570 wasm::WasmGlobal &Global = Globals[GlobalIndex]; in parseLinkingSectionSymtab()
575 wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex]; in parseLinkingSectionSymtab()
576 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) { in parseLinkingSectionSymtab()
589 case wasm::WASM_SYMBOL_TYPE_TABLE: in parseLinkingSectionSymtab()
595 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == in parseLinkingSectionSymtab()
596 wasm::WASM_SYMBOL_BINDING_WEAK) in parseLinkingSectionSymtab()
602 wasm::WasmTable &Table = Tables[TableIndex]; in parseLinkingSectionSymtab()
607 wasm::WasmImport &Import = *ImportedTables[Info.ElementIndex]; in parseLinkingSectionSymtab()
608 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) { in parseLinkingSectionSymtab()
622 case wasm::WASM_SYMBOL_TYPE_DATA: in parseLinkingSectionSymtab()
634 Info.DataRef = wasm::WasmDataReference{Index, Offset, Size}; in parseLinkingSectionSymtab()
638 case wasm::WASM_SYMBOL_TYPE_SECTION: { in parseLinkingSectionSymtab()
639 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != in parseLinkingSectionSymtab()
640 wasm::WASM_SYMBOL_BINDING_LOCAL) in parseLinkingSectionSymtab()
651 case wasm::WASM_SYMBOL_TYPE_EVENT: { in parseLinkingSectionSymtab()
657 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == in parseLinkingSectionSymtab()
658 wasm::WASM_SYMBOL_BINDING_WEAK) in parseLinkingSectionSymtab()
664 wasm::WasmEvent &Event = Events[EventIndex]; in parseLinkingSectionSymtab()
671 wasm::WasmImport &Import = *ImportedEvents[Info.ElementIndex]; in parseLinkingSectionSymtab()
672 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) { in parseLinkingSectionSymtab()
692 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != in parseLinkingSectionSymtab()
693 wasm::WASM_SYMBOL_BINDING_LOCAL && in parseLinkingSectionSymtab()
730 case wasm::WASM_COMDAT_DATA: in parseLinkingSectionComdat()
739 case wasm::WASM_COMDAT_FUNCTION: in parseLinkingSectionComdat()
748 case wasm::WASM_COMDAT_SECTION: in parseLinkingSectionComdat()
752 if (Sections[Index].Type != wasm::WASM_SEC_CUSTOM) in parseLinkingSectionComdat()
808 wasm::WasmFeatureEntry Feature; in parseTargetFeaturesSection()
811 case wasm::WASM_FEATURE_PREFIX_USED: in parseTargetFeaturesSection()
812 case wasm::WASM_FEATURE_PREFIX_REQUIRED: in parseTargetFeaturesSection()
813 case wasm::WASM_FEATURE_PREFIX_DISALLOWED: in parseTargetFeaturesSection()
844 wasm::WasmRelocation Reloc = {}; in parseRelocSection()
853 case wasm::R_WASM_FUNCTION_INDEX_LEB: in parseRelocSection()
854 case wasm::R_WASM_TABLE_INDEX_SLEB: in parseRelocSection()
855 case wasm::R_WASM_TABLE_INDEX_SLEB64: in parseRelocSection()
856 case wasm::R_WASM_TABLE_INDEX_I32: in parseRelocSection()
857 case wasm::R_WASM_TABLE_INDEX_I64: in parseRelocSection()
858 case wasm::R_WASM_TABLE_INDEX_REL_SLEB: in parseRelocSection()
863 case wasm::R_WASM_TABLE_NUMBER_LEB: in parseRelocSection()
868 case wasm::R_WASM_TYPE_INDEX_LEB: in parseRelocSection()
873 case wasm::R_WASM_GLOBAL_INDEX_LEB: in parseRelocSection()
882 case wasm::R_WASM_GLOBAL_INDEX_I32: in parseRelocSection()
887 case wasm::R_WASM_EVENT_INDEX_LEB: in parseRelocSection()
892 case wasm::R_WASM_MEMORY_ADDR_LEB: in parseRelocSection()
893 case wasm::R_WASM_MEMORY_ADDR_SLEB: in parseRelocSection()
894 case wasm::R_WASM_MEMORY_ADDR_I32: in parseRelocSection()
895 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB: in parseRelocSection()
896 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB: in parseRelocSection()
902 case wasm::R_WASM_MEMORY_ADDR_LEB64: in parseRelocSection()
903 case wasm::R_WASM_MEMORY_ADDR_SLEB64: in parseRelocSection()
904 case wasm::R_WASM_MEMORY_ADDR_I64: in parseRelocSection()
905 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64: in parseRelocSection()
911 case wasm::R_WASM_FUNCTION_OFFSET_I32: in parseRelocSection()
917 case wasm::R_WASM_FUNCTION_OFFSET_I64: in parseRelocSection()
923 case wasm::R_WASM_SECTION_OFFSET_I32: in parseRelocSection()
939 if (Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LEB64 || in parseRelocSection()
940 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_SLEB64 || in parseRelocSection()
941 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_REL_SLEB64) in parseRelocSection()
943 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I32 || in parseRelocSection()
944 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I32 || in parseRelocSection()
945 Reloc.Type == wasm::R_WASM_SECTION_OFFSET_I32 || in parseRelocSection()
946 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I32 || in parseRelocSection()
947 Reloc.Type == wasm::R_WASM_GLOBAL_INDEX_I32) in parseRelocSection()
949 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I64 || in parseRelocSection()
950 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I64 || in parseRelocSection()
951 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I64) in parseRelocSection()
992 wasm::WasmSignature Sig; in parseTypeSection()
994 if (Form != wasm::WASM_TYPE_FUNC) { in parseTypeSection()
1002 Sig.Params.push_back(wasm::ValType(ParamType)); in parseTypeSection()
1007 Sig.Returns.push_back(wasm::ValType(ReturnType)); in parseTypeSection()
1021 wasm::WasmImport Im; in parseImportSection()
1026 case wasm::WASM_EXTERNAL_FUNCTION: in parseImportSection()
1030 case wasm::WASM_EXTERNAL_GLOBAL: in parseImportSection()
1035 case wasm::WASM_EXTERNAL_MEMORY: in parseImportSection()
1037 if (Im.Memory.Flags & wasm::WASM_LIMITS_FLAG_IS_64) in parseImportSection()
1040 case wasm::WASM_EXTERNAL_TABLE: { in parseImportSection()
1044 if (ElemType != wasm::WASM_TYPE_FUNCREF && in parseImportSection()
1045 ElemType != wasm::WASM_TYPE_EXTERNREF) in parseImportSection()
1050 case wasm::WASM_EXTERNAL_EVENT: in parseImportSection()
1090 wasm::WasmTable T; in parseTableSection()
1095 if (ElemType != wasm::WASM_TYPE_FUNCREF && in parseTableSection()
1096 ElemType != wasm::WASM_TYPE_EXTERNREF) { in parseTableSection()
1112 if (Limits.Flags & wasm::WASM_LIMITS_FLAG_IS_64) in parseMemorySection()
1127 wasm::WasmEvent Event; in parseEventSection()
1145 wasm::WasmGlobal Global; in parseGlobalSection()
1163 wasm::WasmExport Ex; in parseExportSection()
1168 case wasm::WASM_EXTERNAL_FUNCTION: in parseExportSection()
1175 case wasm::WASM_EXTERNAL_GLOBAL: in parseExportSection()
1180 case wasm::WASM_EXTERNAL_EVENT: in parseExportSection()
1185 case wasm::WASM_EXTERNAL_MEMORY: in parseExportSection()
1186 case wasm::WASM_EXTERNAL_TABLE: in parseExportSection()
1256 wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) { in getDefinedFunction()
1261 const wasm::WasmFunction &
1267 wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) { in getDefinedGlobal()
1272 wasm::WasmEvent &WasmObjectFile::getDefinedEvent(uint32_t Index) { in getDefinedEvent()
1295 wasm::WasmFunction& Function = Functions[i]; in parseCodeSection()
1308 wasm::WasmLocalDecl Decl; in parseCodeSection()
1331 wasm::WasmElemSegment Segment; in parseElemSection()
1361 Segment.Data.MemoryIndex = (Segment.Data.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX) in parseDataSection()
1363 if ((Segment.Data.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) { in parseDataSection()
1367 Segment.Data.Offset.Opcode = wasm::WASM_OPCODE_I32_CONST; in parseDataSection()
1395 const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const { in getHeader()
1447 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION && in getSymbolAddress()
1456 case wasm::WASM_SYMBOL_TYPE_FUNCTION: in getWasmSymbolValue()
1457 case wasm::WASM_SYMBOL_TYPE_GLOBAL: in getWasmSymbolValue()
1458 case wasm::WASM_SYMBOL_TYPE_EVENT: in getWasmSymbolValue()
1459 case wasm::WASM_SYMBOL_TYPE_TABLE: in getWasmSymbolValue()
1461 case wasm::WASM_SYMBOL_TYPE_DATA: { in getWasmSymbolValue()
1465 const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data; in getWasmSymbolValue()
1466 if (Segment.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST) { in getWasmSymbolValue()
1468 } else if (Segment.Offset.Opcode == wasm::WASM_OPCODE_I64_CONST) { in getWasmSymbolValue()
1474 case wasm::WASM_SYMBOL_TYPE_SECTION: in getWasmSymbolValue()
1499 case wasm::WASM_SYMBOL_TYPE_FUNCTION: in getSymbolType()
1501 case wasm::WASM_SYMBOL_TYPE_GLOBAL: in getSymbolType()
1503 case wasm::WASM_SYMBOL_TYPE_DATA: in getSymbolType()
1505 case wasm::WASM_SYMBOL_TYPE_SECTION: in getSymbolType()
1507 case wasm::WASM_SYMBOL_TYPE_EVENT: in getSymbolType()
1509 case wasm::WASM_SYMBOL_TYPE_TABLE: in getSymbolType()
1535 case wasm::WASM_SYMBOL_TYPE_FUNCTION: in getSymbolSectionIdImpl()
1537 case wasm::WASM_SYMBOL_TYPE_GLOBAL: in getSymbolSectionIdImpl()
1539 case wasm::WASM_SYMBOL_TYPE_DATA: in getSymbolSectionIdImpl()
1541 case wasm::WASM_SYMBOL_TYPE_SECTION: in getSymbolSectionIdImpl()
1543 case wasm::WASM_SYMBOL_TYPE_EVENT: in getSymbolSectionIdImpl()
1545 case wasm::WASM_SYMBOL_TYPE_TABLE: in getSymbolSectionIdImpl()
1557 case wasm::WASM_SEC_##X: \ in getSectionName()
1573 case wasm::WASM_SEC_CUSTOM: in getSectionName()
1595 // This will never fail since wasm sections can never be empty (user-sections in getSectionContents()
1609 return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE; in isSectionText()
1613 return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA; in isSectionData()
1638 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); in getRelocationOffset()
1643 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); in getRelocationSymbol()
1644 if (Rel.Type == wasm::R_WASM_TYPE_INDEX_LEB) in getRelocationSymbol()
1653 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); in getRelocationType()
1659 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); in getRelocationTypeName()
1663 case wasm::name: \ in getRelocationTypeName()
1692 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; } in getFileFormatName()
1716 const wasm::WasmRelocation &
1721 const wasm::WasmRelocation &
1732 case wasm::WASM_SEC_CUSTOM: in getSectionOrder()
1741 case wasm::WASM_SEC_TYPE: in getSectionOrder()
1743 case wasm::WASM_SEC_IMPORT: in getSectionOrder()
1745 case wasm::WASM_SEC_FUNCTION: in getSectionOrder()
1747 case wasm::WASM_SEC_TABLE: in getSectionOrder()
1749 case wasm::WASM_SEC_MEMORY: in getSectionOrder()
1751 case wasm::WASM_SEC_GLOBAL: in getSectionOrder()
1753 case wasm::WASM_SEC_EXPORT: in getSectionOrder()
1755 case wasm::WASM_SEC_START: in getSectionOrder()
1757 case wasm::WASM_SEC_ELEM: in getSectionOrder()
1759 case wasm::WASM_SEC_CODE: in getSectionOrder()
1761 case wasm::WASM_SEC_DATA: in getSectionOrder()
1763 case wasm::WASM_SEC_DATACOUNT: in getSectionOrder()
1765 case wasm::WASM_SEC_EVENT: in getSectionOrder()