Browse Source

+ wasm object reader: allow other sections to be created before the first code section

Nikolay Nikolov 1 year ago
parent
commit
3724e00169
1 changed files with 5 additions and 4 deletions
  1. 5 4
      compiler/ogwasm.pas

+ 5 - 4
compiler/ogwasm.pas

@@ -3882,7 +3882,7 @@ implementation
       var
       var
         ModuleMagic: array [0..3] of Byte;
         ModuleMagic: array [0..3] of Byte;
         ModuleVersion: array [0..3] of Byte;
         ModuleVersion: array [0..3] of Byte;
-        i, j, FirstDataSegmentIdx, SegI: Integer;
+        i, j, FirstCodeSegmentIdx, FirstDataSegmentIdx, SegI: Integer;
         CurrSec, ObjSec: TObjSection;
         CurrSec, ObjSec: TObjSection;
         BaseSectionOffset: UInt32;
         BaseSectionOffset: UInt32;
         ObjReloc: TWasmObjRelocation;
         ObjReloc: TWasmObjRelocation;
@@ -3940,6 +3940,7 @@ implementation
               end;
               end;
 
 
         { create segments }
         { create segments }
+        FirstCodeSegmentIdx:=ObjData.ObjSectionList.Count;
         for i:=low(CodeSegments) to high(CodeSegments) do
         for i:=low(CodeSegments) to high(CodeSegments) do
           with CodeSegments[i] do
           with CodeSegments[i] do
             begin
             begin
@@ -4030,9 +4031,9 @@ implementation
                       objsym:=TWasmObjSymbol(ObjData.CreateSymbol(SymName));
                       objsym:=TWasmObjSymbol(ObjData.CreateSymbol(SymName));
                       objsym.bind:=AB_GLOBAL;
                       objsym.bind:=AB_GLOBAL;
                       objsym.typ:=AT_FUNCTION;
                       objsym.typ:=AT_FUNCTION;
-                      objsym.objsection:=TObjSection(ObjData.ObjSectionList[SymIndex-FuncTypeImportsCount]);
+                      objsym.objsection:=TObjSection(ObjData.ObjSectionList[FirstCodeSegmentIdx+SymIndex-FuncTypeImportsCount]);
                       if (SymFlags and WASM_SYM_EXPLICIT_NAME)=0 then
                       if (SymFlags and WASM_SYM_EXPLICIT_NAME)=0 then
-                        TWasmObjSection(ObjData.ObjSectionList[SymIndex-FuncTypeImportsCount]).MainFuncSymbol:=objsym;
+                        TWasmObjSection(ObjData.ObjSectionList[FirstCodeSegmentIdx+SymIndex-FuncTypeImportsCount]).MainFuncSymbol:=objsym;
                       objsym.offset:=0;
                       objsym.offset:=0;
                       objsym.size:=objsym.objsection.Size;
                       objsym.size:=objsym.objsection.Size;
                     end;
                     end;
@@ -4119,7 +4120,7 @@ implementation
                           Exit;
                           Exit;
                         end;
                         end;
                       BaseSectionOffset:=CodeSegments[SegI].CodeSectionOffset;
                       BaseSectionOffset:=CodeSegments[SegI].CodeSectionOffset;
-                      ObjSec:=TObjSection(ObjData.ObjSectionList[SegI]);
+                      ObjSec:=TObjSection(ObjData.ObjSectionList[FirstCodeSegmentIdx+SegI]);
                     end;
                     end;
                   1:
                   1:
                     begin
                     begin