Repository: dotnet/runtime
File: src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWriter.cs (C#)

Code before:
            SectionByName(WasmObjectNodeSection.TypeSection.Name).Emit(outputFileStream);
            // Function section (3)
            SectionByName(WasmObjectNodeSection.FunctionSection.Name).Emit(outputFileStream);
            // Table section (4)
            SectionByName(WasmObjectNodeSection.TableSection.Name).Emit(outputFileStream);
            // Memory section (5)
            SectionByName(WasmObjectNodeSection.MemorySection.Name).Emit(outputFileStream);
            // Export section (7)
            SectionByName(WasmObjectNodeSection.ExportSection.Name).Emit(outputFileStream);
            // Code section (10)
            WasmSection codeSection = SectionByName(ObjectNodeSection.WasmCodeSection.Name);
            PrependCount(codeSection, _methodCount);
            codeSection.Emit(outputFileStream);
            // Data section (11) (all data segments combined)
            SectionByName(WasmObjectNodeSection.CombinedDataSection.Name).Emit(outputFileStream);
        }

        private protected override void EmitRelocations(int sectionIndex, List<SymbolicRelocation> relocationList)
        {
            // This is a no-op for now under Wasm
        }

        // For now, this function just prepares the function, exports, and type sections for emission by prepending the counts.
        private protected override void EmitSymbolTable(IDictionary<Utf8String, SymbolDefinition> definedSymbols, SortedSet<Utf8String> undefinedSymbols)
        {
            WriteMemoryExport("memory", 0);
            WriteTableExport("table", 0);

            string[] functionExports = _uniqueSymbols.Keys.ToArray();
            // TODO-WASM: Handle exports better (e.g., only export public methods, etc.)
            // Also, see if we could leverage definedSymbols for this instead of doing our own bookkeeping in _uniqueSymbols.
            foreach (string name in functionExports.OrderBy(name => name))
            {
                WriteFunctionExport(name, _uniqueSymbols[name]);
            }

            int funcIdx = _sectionNameToIndex[WasmObjectNodeSection.FunctionSection.Name];
            PrependCount(_sections[funcIdx], _methodCount);

            int typeIdx = _sectionNameToIndex[WasmObjectNodeSection.TypeSection.Name];
            PrependCount(_sections[typeIdx], _uniqueSignatures.Count);

            int exportIdx = _sectionNameToIndex[WasmObjectNodeSection.ExportSection.Name];
            PrependCount(_sections[exportIdx], NumExports);
        }
    }

    // TODO-WASM: The logic here isn't comprehensive yet. It should cover primitive types and references,
    // but by-value structs + nullable types aren't handled yet.
    public static class WasmAbiContext
    {

Review comment (suggestion):
I think we need memory import not export, so that main module WASM linear memory could be passed to R2R module

Code after:
            SectionByName(WasmObjectNodeSection.TypeSection.Name).Emit(outputFileStream);
            // Function section (3)
            SectionByName(WasmObjectNodeSection.FunctionSection.Name).Emit(outputFileStream);
            // Table section (4)
            SectionByName(WasmObjectNodeSection.TableSection.Name).Emit(outputFileStream);
            // Memory section (5)
            SectionByName(WasmObjectNodeSection.MemorySection.Name).Emit(outputFileStream);
            // Export section (7)
            SectionByName(WasmObjectNodeSection.ExportSection.Name).Emit(outputFileStream);
            // Code section (10)
            WasmSection codeSection = SectionByName(ObjectNodeSection.WasmCodeSection.Name);
            PrependCount(codeSection, _methodCount);
            codeSection.Emit(outputFileStream);
            // Data section (11) (all data segments combined)
            SectionByName(WasmObjectNodeSection.CombinedDataSection.Name).Emit(outputFileStream);
        }

        private protected override void EmitRelocations(int sectionIndex, List<SymbolicRelocation> relocationList)
        {
            // This is a no-op for now under Wasm
        }

        // For now, this function just prepares the function, exports, and type sections for emission by prepending the counts.
        private protected override void EmitSymbolTable(IDictionary<Utf8String, SymbolDefinition> definedSymbols, SortedSet<Utf8String> undefinedSymbols)
        {
            WriteMemoryExport("memory", 0);
            WriteTableExport("table", 0);

            string[] functionExports = _uniqueSymbols.Keys.ToArray();
            // TODO-WASM: Handle exports better (e.g., only export public methods, etc.)
            // Also, see if we could leverage definedSymbols for this instead of doing our own bookkeeping in _uniqueSymbols.
            foreach (string name in functionExports.OrderBy(name => name))
            {
                WriteFunctionExport(name, _uniqueSymbols[name]);
            }

            int funcIdx = _sectionNameToIndex[WasmObjectNodeSection.FunctionSection.Name];
            PrependCount(_sections[funcIdx], _methodCount);

            int typeIdx = _sectionNameToIndex[WasmObjectNodeSection.TypeSection.Name];
            PrependCount(_sections[typeIdx], _uniqueSignatures.Count);

            int exportIdx = _sectionNameToIndex[WasmObjectNodeSection.ExportSection.Name];
            PrependCount(_sections[exportIdx], _numExports);
        }
    }

    // TODO-WASM: The logic here isn't comprehensive yet. It should cover primitive types and references,
    // but by-value structs + nullable types aren't handled yet.
    public static class WasmAbiContext
    {