Lines Matching full:wasm

1 //===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===//
9 // This file implements Wasm object file writer information.
15 #include "llvm/BinaryFormat/Wasm.h"
60 // A wasm data segment. A wasm binary contains only a single data section
63 // wasm data segment.
74 // A wasm function to be written into the function section.
80 // A wasm global to be written into the global section.
82 wasm::WasmGlobalType Type;
108 bool hasAddend() const { return wasm::relocTypeHasAddend(Type); } in hasAddend()
111 Out << wasm::relocTypetoString(Type) << " Off=" << Offset in print()
183 /// The target specific Wasm writer instance.
201 // Maps data symbols to the Wasm segment and offset/size with the segment.
202 DenseMap<const MCSymbolWasm *, wasm::WasmDataReference> DataLocations;
216 DenseMap<wasm::WasmSignature, uint32_t> SignatureIndices;
217 SmallVector<wasm::WasmSignature, 4> Signatures;
283 void prepareImports(SmallVectorImpl<wasm::WasmImport> &Imports,
307 void writeValueType(wasm::ValType Ty) { W->OS << static_cast<char>(Ty); } in writeValueType()
309 void writeTypeSection(ArrayRef<wasm::WasmSignature> Signatures);
310 void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint64_t DataSize,
313 void writeExportSection(ArrayRef<wasm::WasmExport> Exports);
319 void writeEventSection(ArrayRef<wasm::WasmEventType> Events);
320 void writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals);
321 void writeTableSection(ArrayRef<wasm::WasmTable> Tables);
325 ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
367 startSection(Section, wasm::WASM_SEC_CUSTOM); in startCustomSection()
372 // Custom sections in wasm also have a string identifier. in startCustomSection()
400 // Emit the Wasm header.
402 W->OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic)); in writeHeader()
403 W->write<uint32_t>(wasm::WasmVersion); in writeHeader()
466 // LLVM expects wrapping, in contrast to wasm's immediates which can't in recordRelocation()
475 if (Type == wasm::R_WASM_FUNCTION_OFFSET_I32 || in recordRelocation()
476 Type == wasm::R_WASM_FUNCTION_OFFSET_I64 || in recordRelocation()
477 Type == wasm::R_WASM_SECTION_OFFSET_I32) { in recordRelocation()
495 if (Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB || in recordRelocation()
496 Type == wasm::R_WASM_TABLE_INDEX_SLEB || in recordRelocation()
497 Type == wasm::R_WASM_TABLE_INDEX_SLEB64 || in recordRelocation()
498 Type == wasm::R_WASM_TABLE_INDEX_I32 || in recordRelocation()
499 Type == wasm::R_WASM_TABLE_INDEX_I64) { in recordRelocation()
520 if (Type != wasm::R_WASM_TYPE_INDEX_LEB) { in recordRelocation()
523 "supported by wasm"); in recordRelocation()
552 if ((RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_LEB || in getProvisionalValue()
553 RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_I32) && in getProvisionalValue()
560 case wasm::R_WASM_TABLE_INDEX_REL_SLEB: in getProvisionalValue()
561 case wasm::R_WASM_TABLE_INDEX_SLEB: in getProvisionalValue()
562 case wasm::R_WASM_TABLE_INDEX_SLEB64: in getProvisionalValue()
563 case wasm::R_WASM_TABLE_INDEX_I32: in getProvisionalValue()
564 case wasm::R_WASM_TABLE_INDEX_I64: { in getProvisionalValue()
569 if (RelEntry.Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB) in getProvisionalValue()
574 case wasm::R_WASM_TYPE_INDEX_LEB: in getProvisionalValue()
577 case wasm::R_WASM_FUNCTION_INDEX_LEB: in getProvisionalValue()
578 case wasm::R_WASM_GLOBAL_INDEX_LEB: in getProvisionalValue()
579 case wasm::R_WASM_GLOBAL_INDEX_I32: in getProvisionalValue()
580 case wasm::R_WASM_EVENT_INDEX_LEB: in getProvisionalValue()
581 case wasm::R_WASM_TABLE_NUMBER_LEB: in getProvisionalValue()
582 // Provisional value is function/global/event Wasm index in getProvisionalValue()
583 assert(WasmIndices.count(RelEntry.Symbol) > 0 && "symbol not found in wasm index space"); in getProvisionalValue()
585 case wasm::R_WASM_FUNCTION_OFFSET_I32: in getProvisionalValue()
586 case wasm::R_WASM_FUNCTION_OFFSET_I64: in getProvisionalValue()
587 case wasm::R_WASM_SECTION_OFFSET_I32: { in getProvisionalValue()
592 case wasm::R_WASM_MEMORY_ADDR_LEB: in getProvisionalValue()
593 case wasm::R_WASM_MEMORY_ADDR_LEB64: in getProvisionalValue()
594 case wasm::R_WASM_MEMORY_ADDR_SLEB: in getProvisionalValue()
595 case wasm::R_WASM_MEMORY_ADDR_SLEB64: in getProvisionalValue()
596 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB: in getProvisionalValue()
597 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64: in getProvisionalValue()
598 case wasm::R_WASM_MEMORY_ADDR_I32: in getProvisionalValue()
599 case wasm::R_WASM_MEMORY_ADDR_I64: in getProvisionalValue()
600 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB: { in getProvisionalValue()
607 const wasm::WasmDataReference &BaseRef = DataLocations[Base], in getProvisionalValue()
658 if (RelEntry.Type == wasm::R_WASM_TYPE_INDEX_LEB) { in getRelocationIndexValue()
683 case wasm::R_WASM_FUNCTION_INDEX_LEB: in applyRelocations()
684 case wasm::R_WASM_TYPE_INDEX_LEB: in applyRelocations()
685 case wasm::R_WASM_GLOBAL_INDEX_LEB: in applyRelocations()
686 case wasm::R_WASM_MEMORY_ADDR_LEB: in applyRelocations()
687 case wasm::R_WASM_EVENT_INDEX_LEB: in applyRelocations()
688 case wasm::R_WASM_TABLE_NUMBER_LEB: in applyRelocations()
691 case wasm::R_WASM_MEMORY_ADDR_LEB64: in applyRelocations()
694 case wasm::R_WASM_TABLE_INDEX_I32: in applyRelocations()
695 case wasm::R_WASM_MEMORY_ADDR_I32: in applyRelocations()
696 case wasm::R_WASM_FUNCTION_OFFSET_I32: in applyRelocations()
697 case wasm::R_WASM_SECTION_OFFSET_I32: in applyRelocations()
698 case wasm::R_WASM_GLOBAL_INDEX_I32: in applyRelocations()
701 case wasm::R_WASM_TABLE_INDEX_I64: in applyRelocations()
702 case wasm::R_WASM_MEMORY_ADDR_I64: in applyRelocations()
703 case wasm::R_WASM_FUNCTION_OFFSET_I64: in applyRelocations()
706 case wasm::R_WASM_TABLE_INDEX_SLEB: in applyRelocations()
707 case wasm::R_WASM_TABLE_INDEX_REL_SLEB: in applyRelocations()
708 case wasm::R_WASM_MEMORY_ADDR_SLEB: in applyRelocations()
709 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB: in applyRelocations()
710 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB: in applyRelocations()
713 case wasm::R_WASM_TABLE_INDEX_SLEB64: in applyRelocations()
714 case wasm::R_WASM_MEMORY_ADDR_SLEB64: in applyRelocations()
715 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64: in applyRelocations()
725 ArrayRef<wasm::WasmSignature> Signatures) { in writeTypeSection()
730 startSection(Section, wasm::WASM_SEC_TYPE); in writeTypeSection()
734 for (const wasm::WasmSignature &Sig : Signatures) { in writeTypeSection()
735 W->OS << char(wasm::WASM_TYPE_FUNC); in writeTypeSection()
737 for (wasm::ValType Ty : Sig.Params) in writeTypeSection()
740 for (wasm::ValType Ty : Sig.Returns) in writeTypeSection()
747 void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports, in writeImportSection()
753 uint64_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize; in writeImportSection()
756 startSection(Section, wasm::WASM_SEC_IMPORT); in writeImportSection()
759 for (const wasm::WasmImport &Import : Imports) { in writeImportSection()
765 case wasm::WASM_EXTERNAL_FUNCTION: in writeImportSection()
768 case wasm::WASM_EXTERNAL_GLOBAL: in writeImportSection()
772 case wasm::WASM_EXTERNAL_MEMORY: in writeImportSection()
776 case wasm::WASM_EXTERNAL_TABLE: in writeImportSection()
781 case wasm::WASM_EXTERNAL_EVENT: in writeImportSection()
798 startSection(Section, wasm::WASM_SEC_FUNCTION); in writeFunctionSection()
807 void WasmObjectWriter::writeEventSection(ArrayRef<wasm::WasmEventType> Events) { in writeEventSection()
812 startSection(Section, wasm::WASM_SEC_EVENT); in writeEventSection()
815 for (const wasm::WasmEventType &Event : Events) { in writeEventSection()
823 void WasmObjectWriter::writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals) { in writeGlobalSection()
828 startSection(Section, wasm::WASM_SEC_GLOBAL); in writeGlobalSection()
831 for (const wasm::WasmGlobal &Global : Globals) { in writeGlobalSection()
836 case wasm::WASM_TYPE_I32: in writeGlobalSection()
839 case wasm::WASM_TYPE_I64: in writeGlobalSection()
842 case wasm::WASM_TYPE_F32: in writeGlobalSection()
845 case wasm::WASM_TYPE_F64: in writeGlobalSection()
848 case wasm::WASM_TYPE_EXTERNREF: in writeGlobalSection()
849 writeValueType(wasm::ValType::EXTERNREF); in writeGlobalSection()
854 W->OS << char(wasm::WASM_OPCODE_END); in writeGlobalSection()
860 void WasmObjectWriter::writeTableSection(ArrayRef<wasm::WasmTable> Tables) { in writeTableSection()
865 startSection(Section, wasm::WASM_SEC_TABLE); in writeTableSection()
868 for (const wasm::WasmTable &Table : Tables) { in writeTableSection()
872 if (Table.Type.Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) in writeTableSection()
878 void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) { in writeExportSection()
883 startSection(Section, wasm::WASM_SEC_EXPORT); in writeExportSection()
886 for (const wasm::WasmExport &Export : Exports) { in writeExportSection()
900 startSection(Section, wasm::WASM_SEC_ELEM); in writeElemSection()
906 W->OS << char(wasm::WASM_OPCODE_I32_CONST); in writeElemSection()
908 W->OS << char(wasm::WASM_OPCODE_END); in writeElemSection()
922 startSection(Section, wasm::WASM_SEC_DATACOUNT); in writeDataCountSection()
934 startSection(Section, wasm::WASM_SEC_CODE); in writeCodeSection()
962 startSection(Section, wasm::WASM_SEC_DATA); in writeDataSection()
968 if (Segment.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX) in writeDataSection()
970 if ((Segment.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) { in writeDataSection()
971 W->OS << char(Segment.Offset > INT32_MAX ? wasm::WASM_OPCODE_I64_CONST in writeDataSection()
972 : wasm::WASM_OPCODE_I32_CONST); in writeDataSection()
974 W->OS << char(wasm::WASM_OPCODE_END); in writeDataSection()
1000 // wasm section, and this order is determined by the order of Asm.Symbols() in writeRelocSection()
1036 ArrayRef<wasm::WasmSymbolInfo> SymbolInfos, in writeLinkingMetaDataSection()
1041 encodeULEB128(wasm::WasmMetadataVersion, W->OS); in writeLinkingMetaDataSection()
1045 startSection(SubSection, wasm::WASM_SYMBOL_TABLE); in writeLinkingMetaDataSection()
1047 for (const wasm::WasmSymbolInfo &Sym : SymbolInfos) { in writeLinkingMetaDataSection()
1051 case wasm::WASM_SYMBOL_TYPE_FUNCTION: in writeLinkingMetaDataSection()
1052 case wasm::WASM_SYMBOL_TYPE_GLOBAL: in writeLinkingMetaDataSection()
1053 case wasm::WASM_SYMBOL_TYPE_EVENT: in writeLinkingMetaDataSection()
1054 case wasm::WASM_SYMBOL_TYPE_TABLE: in writeLinkingMetaDataSection()
1056 if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0 || in writeLinkingMetaDataSection()
1057 (Sym.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) in writeLinkingMetaDataSection()
1060 case wasm::WASM_SYMBOL_TYPE_DATA: in writeLinkingMetaDataSection()
1062 if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) { in writeLinkingMetaDataSection()
1068 case wasm::WASM_SYMBOL_TYPE_SECTION: { in writeLinkingMetaDataSection()
1082 startSection(SubSection, wasm::WASM_SEGMENT_INFO); in writeLinkingMetaDataSection()
1093 startSection(SubSection, wasm::WASM_INIT_FUNCS); in writeLinkingMetaDataSection()
1103 startSection(SubSection, wasm::WASM_COMDAT_INFO); in writeLinkingMetaDataSection()
1155 wasm::WasmSignature S; in registerFunctionType()
1177 wasm::WasmSignature S; in registerEventType()
1209 SmallVectorImpl<wasm::WasmImport> &Imports, MCAssembler &Asm, in prepareImports()
1214 wasm::WasmImport MemImport; in prepareImports()
1217 MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY; in prepareImports()
1218 MemImport.Memory.Flags = is64Bit() ? wasm::WASM_LIMITS_FLAG_IS_64 in prepareImports()
1219 : wasm::WASM_LIMITS_FLAG_NONE; in prepareImports()
1229 // (because wasm always needs a type signature). in prepareImports()
1247 wasm::WasmImport Import; in prepareImports()
1250 Import.Kind = wasm::WASM_EXTERNAL_FUNCTION; in prepareImports()
1259 wasm::WasmImport Import; in prepareImports()
1261 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL; in prepareImports()
1271 wasm::WasmImport Import; in prepareImports()
1274 Import.Kind = wasm::WASM_EXTERNAL_EVENT; in prepareImports()
1275 Import.Event.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION; in prepareImports()
1284 wasm::WasmImport Import; in prepareImports()
1287 Import.Kind = wasm::WASM_EXTERNAL_TABLE; in prepareImports()
1288 wasm::ValType ElemType = WS.getTableType(); in prepareImports()
1293 Import.Table.Limits = {wasm::WASM_LIMITS_FLAG_NONE, 0, 0}; in prepareImports()
1305 wasm::WasmImport Import; in prepareImports()
1311 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL; in prepareImports()
1312 Import.Global = {wasm::WASM_TYPE_I32, true}; in prepareImports()
1347 SmallVector<wasm::WasmImport, 4> Imports; in writeOneObject()
1348 SmallVector<wasm::WasmExport, 4> Exports; in writeOneObject()
1349 SmallVector<wasm::WasmEventType, 1> Events; in writeOneObject()
1350 SmallVector<wasm::WasmGlobal, 1> Globals; in writeOneObject()
1351 SmallVector<wasm::WasmTable, 1> Tables; in writeOneObject()
1352 SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos; in writeOneObject()
1385 Section.getPassive() ? (uint32_t)wasm::WASM_SEGMENT_IS_PASSIVE : 0; in writeOneObject()
1396 WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex}); in writeOneObject()
1430 WasmComdatEntry{wasm::WASM_COMDAT_SECTION, in writeOneObject()
1480 WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index}); in writeOneObject()
1484 wasm::WasmExport Export; in writeOneObject()
1486 Export.Kind = wasm::WASM_EXTERNAL_FUNCTION; in writeOneObject()
1521 // corresponding Wasm data segment. in writeOneObject()
1522 wasm::WasmDataReference Ref = wasm::WasmDataReference{ in writeOneObject()
1529 // A "true" Wasm global (currently just __stack_pointer) in writeOneObject()
1532 wasm::WasmGlobal Global; in writeOneObject()
1536 case wasm::WASM_TYPE_I32: in writeOneObject()
1537 Global.InitExpr.Opcode = wasm::WASM_OPCODE_I32_CONST; in writeOneObject()
1539 case wasm::WASM_TYPE_I64: in writeOneObject()
1540 Global.InitExpr.Opcode = wasm::WASM_OPCODE_I64_CONST; in writeOneObject()
1542 case wasm::WASM_TYPE_F32: in writeOneObject()
1543 Global.InitExpr.Opcode = wasm::WASM_OPCODE_F32_CONST; in writeOneObject()
1545 case wasm::WASM_TYPE_F64: in writeOneObject()
1546 Global.InitExpr.Opcode = wasm::WASM_OPCODE_F64_CONST; in writeOneObject()
1548 case wasm::WASM_TYPE_EXTERNREF: in writeOneObject()
1549 Global.InitExpr.Opcode = wasm::WASM_OPCODE_REF_NULL; in writeOneObject()
1564 wasm::WasmTable Table; in writeOneObject()
1568 Table.Type.Limits = {wasm::WASM_LIMITS_FLAG_NONE, 0, 0}; in writeOneObject()
1581 wasm::WasmEventType Event; in writeOneObject()
1583 Event.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION; in writeOneObject()
1638 wasm::WasmDataReference Ref = wasm::WasmDataReference{ in writeOneObject()
1657 // For the moment, don't emit table symbols -- wasm-ld can't handle them. in writeOneObject()
1664 Flags |= wasm::WASM_SYMBOL_BINDING_WEAK; in writeOneObject()
1666 Flags |= wasm::WASM_SYMBOL_VISIBILITY_HIDDEN; in writeOneObject()
1668 Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL; in writeOneObject()
1670 Flags |= wasm::WASM_SYMBOL_UNDEFINED; in writeOneObject()
1672 Flags |= wasm::WASM_SYMBOL_NO_STRIP; in writeOneObject()
1674 Flags |= wasm::WASM_SYMBOL_EXPORTED; in writeOneObject()
1678 Flags |= wasm::WASM_SYMBOL_EXPLICIT_NAME; in writeOneObject()
1680 Flags |= wasm::WASM_SYMBOL_EXPORTED; in writeOneObject()
1682 wasm::WasmSymbolInfo Info; in writeOneObject()
1702 if (Rel.Type != wasm::R_WASM_TABLE_INDEX_I32 && in writeOneObject()
1703 Rel.Type != wasm::R_WASM_TABLE_INDEX_I64 && in writeOneObject()
1704 Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB && in writeOneObject()
1705 Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB64 && in writeOneObject()
1706 Rel.Type != wasm::R_WASM_TABLE_INDEX_REL_SLEB) in writeOneObject()
1792 // Write out the Wasm header. in writeOneObject()
1816 if (Entry.Kind == wasm::WASM_COMDAT_SECTION) { in writeOneObject()