コード例 #1
0
ファイル: WasmModule.cs プロジェクト: SavchukSergey/WasmNET
        public void AcceptSection(WasmSection section)
        {
            var reader = new WasmReader(section.Payload);

            switch (section.Code)
            {
            case WasmSectionCode.Type:
                TypeSections.Add(reader.ReadTypeSection());
                break;

            case WasmSectionCode.Import:
                ImportSections.Add(reader.ReadImportSection());
                break;

            case WasmSectionCode.Function:
                FunctionSections.Add(reader.ReadFunctionSection());
                break;

            case WasmSectionCode.Table:
                TableSections.Add(reader.ReadTableSection());
                break;

            case WasmSectionCode.Memory:
                MemorySections.Add(reader.ReadMemorySection());
                break;

            case WasmSectionCode.Global:
                GlobalSections.Add(reader.ReadGlobalSection());
                break;

            case WasmSectionCode.Export:
                ExportSections.Add(reader.ReadExportSection());
                break;

            case WasmSectionCode.Start:
                StartSections.Add(reader.ReadStartSection());
                break;

            case WasmSectionCode.Element:
                ElementSections.Add(reader.ReadElementSection());
                break;

            case WasmSectionCode.Code:
                CodeSections.Add(reader.ReadCodeSection());
                break;

            case WasmSectionCode.Data:
                DataSections.Add(reader.ReadDataSection());
                break;

            default:
                CustomSections.Add(section);
                break;
            }
        }
コード例 #2
0
        /// <summary>
        /// based on <a href="https://github.com/dotnet/coreclr/blob/master/src/zap/zapimport.cpp">ZapImportSectionsTable::Save</a>
        /// </summary>
        private void ParseImportSections()
        {
            if (!R2RHeader.Sections.ContainsKey(R2RSection.SectionType.READYTORUN_SECTION_IMPORT_SECTIONS))
            {
                return;
            }
            R2RSection importSectionsSection = R2RHeader.Sections[R2RSection.SectionType.READYTORUN_SECTION_IMPORT_SECTIONS];
            int        offset    = GetOffset(importSectionsSection.RelativeVirtualAddress);
            int        endOffset = offset + importSectionsSection.Size;

            while (offset < endOffset)
            {
                int rva                     = NativeReader.ReadInt32(Image, ref offset);
                int sectionOffset           = GetOffset(rva);
                int startOffset             = sectionOffset;
                int size                    = NativeReader.ReadInt32(Image, ref offset);
                CorCompileImportFlags flags = (CorCompileImportFlags)NativeReader.ReadUInt16(Image, ref offset);
                byte type                   = NativeReader.ReadByte(Image, ref offset);
                byte entrySize              = NativeReader.ReadByte(Image, ref offset);
                if (entrySize == 0)
                {
                    switch (Machine)
                    {
                    case Machine.I386:
                    case Machine.ArmThumb2:
                        entrySize = 4;
                        break;

                    case Machine.Amd64:
                    case Machine.Arm64:
                        entrySize = 8;
                        break;

                    default:
                        throw new NotImplementedException(Machine.ToString());
                    }
                }
                int entryCount = 0;
                if (entrySize != 0)
                {
                    entryCount = size / entrySize;
                }
                int signatureRVA = NativeReader.ReadInt32(Image, ref offset);

                int signatureOffset = 0;
                if (signatureRVA != 0)
                {
                    signatureOffset = GetOffset(signatureRVA);
                }
                List <R2RImportSection.ImportSectionEntry> entries = new List <R2RImportSection.ImportSectionEntry>();
                for (int i = 0; i < entryCount; i++)
                {
                    int    entryOffset = sectionOffset - startOffset;
                    long   section     = NativeReader.ReadInt64(Image, ref sectionOffset);
                    uint   sigRva      = NativeReader.ReadUInt32(Image, ref signatureOffset);
                    int    sigOffset   = GetOffset((int)sigRva);
                    string cellName    = MetadataNameFormatter.FormatSignature(_assemblyResolver, this, sigOffset);
                    entries.Add(new R2RImportSection.ImportSectionEntry(entries.Count, entryOffset, entryOffset + rva, section, sigRva, cellName));
                    ImportCellNames.Add(rva + entrySize * i, cellName);
                }

                int auxDataRVA    = NativeReader.ReadInt32(Image, ref offset);
                int auxDataOffset = 0;
                if (auxDataRVA != 0)
                {
                    auxDataOffset = GetOffset(auxDataRVA);
                }
                ImportSections.Add(new R2RImportSection(ImportSections.Count, this, rva, size, flags, type, entrySize, signatureRVA, entries, auxDataRVA, auxDataOffset, Machine, R2RHeader.MajorVersion));
            }
        }