Browse Source

+ write the relocation tables for the code and data sections (empty for now)

Nikolay Nikolov 3 years ago
parent
commit
4e4910cb84
1 changed files with 40 additions and 7 deletions
  1. 40 7
      compiler/ogwasm.pas

+ 40 - 7
compiler/ogwasm.pas

@@ -102,6 +102,10 @@ interface
       TWasmObjOutput = class(tObjOutput)
       TWasmObjOutput = class(tObjOutput)
       private
       private
         FData: TWasmObjData;
         FData: TWasmObjData;
+        FWasmRelocationCodeTable: tdynamicarray;
+        FWasmRelocationCodeTableEntriesCount: Integer;
+        FWasmRelocationDataTable: tdynamicarray;
+        FWasmRelocationDataTableEntriesCount: Integer;
         FWasmSymbolTable: tdynamicarray;
         FWasmSymbolTable: tdynamicarray;
         FWasmSymbolTableEntriesCount: Integer;
         FWasmSymbolTableEntriesCount: Integer;
         FWasmSections: array [TWasmSectionID] of tdynamicarray;
         FWasmSections: array [TWasmSectionID] of tdynamicarray;
@@ -122,6 +126,8 @@ interface
         procedure WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
         procedure WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
         procedure WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
         procedure WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
         procedure WriteSymbolTable;
         procedure WriteSymbolTable;
+        procedure WriteRelocationCodeTable(CodeSectionIndex: Integer);
+        procedure WriteRelocationDataTable(DataSectionIndex: Integer);
         procedure WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
         procedure WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
       protected
       protected
         function writeData(Data:TObjData):boolean;override;
         function writeData(Data:TObjData):boolean;override;
@@ -652,6 +658,22 @@ implementation
         CopyDynamicArray(FWasmSymbolTable,FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTable.size);
         CopyDynamicArray(FWasmSymbolTable,FWasmLinkingSubsections[WASM_SYMBOL_TABLE],FWasmSymbolTable.size);
       end;
       end;
 
 
+    procedure TWasmObjOutput.WriteRelocationCodeTable(CodeSectionIndex: Integer);
+      begin
+        WriteUleb(FWasmCustomSections[wcstRelocCode],CodeSectionIndex);
+        WriteUleb(FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTableEntriesCount);
+        FWasmRelocationCodeTable.seek(0);
+        CopyDynamicArray(FWasmRelocationCodeTable,FWasmCustomSections[wcstRelocCode],FWasmRelocationCodeTable.size);
+      end;
+
+    procedure TWasmObjOutput.WriteRelocationDataTable(DataSectionIndex: Integer);
+      begin
+        WriteUleb(FWasmCustomSections[wcstRelocData],DataSectionIndex);
+        WriteUleb(FWasmCustomSections[wcstRelocData],FWasmRelocationDataTableEntriesCount);
+        FWasmRelocationDataTable.seek(0);
+        CopyDynamicArray(FWasmRelocationDataTable,FWasmCustomSections[wcstRelocData],FWasmRelocationDataTable.size);
+      end;
+
     procedure TWasmObjOutput.WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
     procedure TWasmObjOutput.WriteLinkingSubsection(wlst: TWasmLinkingSubsectionType);
       begin
       begin
         if FWasmLinkingSubsections[wlst].size>0 then
         if FWasmLinkingSubsections[wlst].size>0 then
@@ -842,16 +864,21 @@ implementation
         WriteLinkingSubsection(WASM_SYMBOL_TABLE);
         WriteLinkingSubsection(WASM_SYMBOL_TABLE);
         WriteLinkingSubsection(WASM_SEGMENT_INFO);
         WriteLinkingSubsection(WASM_SEGMENT_INFO);
 
 
+        WriteRelocationCodeTable(4);  { code section is section #4 }
+        WriteRelocationDataTable(5);  { code section is section #5 }
+
         Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
         Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
         Writer.write(WasmVersion,SizeOf(WasmVersion));
         Writer.write(WasmVersion,SizeOf(WasmVersion));
 
 
-        WriteWasmSection(wsiType);
-        WriteWasmSection(wsiImport);
-        WriteWasmSection(wsiFunction);
-        WriteWasmSection(wsiDataCount);
-        WriteWasmSection(wsiCode);
-        WriteWasmSection(wsiData);
-        WriteWasmCustomSection(wcstLinking);
+        WriteWasmSection(wsiType);              { section #0 }
+        WriteWasmSection(wsiImport);            { section #1 }
+        WriteWasmSection(wsiFunction);          { section #2 }
+        WriteWasmSection(wsiDataCount);         { section #3 }
+        WriteWasmSection(wsiCode);              { section #4 }
+        WriteWasmSection(wsiData);              { section #5 }
+        WriteWasmCustomSection(wcstLinking);    { section #6 }
+        WriteWasmCustomSection(wcstRelocCode);  { section #7 }
+        WriteWasmCustomSection(wcstRelocData);  { section #8 }
 
 
         Writeln('ObjSymbolList:');
         Writeln('ObjSymbolList:');
         for i:=0 to Data.ObjSymbolList.Count-1 do
         for i:=0 to Data.ObjSymbolList.Count-1 do
@@ -891,6 +918,10 @@ implementation
           FWasmLinkingSubsections[k] := tdynamicarray.create(SectionDataMaxGrow);
           FWasmLinkingSubsections[k] := tdynamicarray.create(SectionDataMaxGrow);
         FWasmSymbolTable:=tdynamicarray.create(SectionDataMaxGrow);
         FWasmSymbolTable:=tdynamicarray.create(SectionDataMaxGrow);
         FWasmSymbolTableEntriesCount:=0;
         FWasmSymbolTableEntriesCount:=0;
+        FWasmRelocationCodeTable:=tdynamicarray.create(SectionDataMaxGrow);
+        FWasmRelocationCodeTableEntriesCount:=0;
+        FWasmRelocationDataTable:=tdynamicarray.create(SectionDataMaxGrow);
+        FWasmRelocationDataTableEntriesCount:=0;
       end;
       end;
 
 
     destructor TWasmObjOutput.destroy;
     destructor TWasmObjOutput.destroy;
@@ -906,6 +937,8 @@ implementation
         for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
         for k:=low(TWasmLinkingSubsectionType) to high(TWasmLinkingSubsectionType) do
           FWasmLinkingSubsections[k].Free;
           FWasmLinkingSubsections[k].Free;
         FWasmSymbolTable.Free;
         FWasmSymbolTable.Free;
+        FWasmRelocationCodeTable.Free;
+        FWasmRelocationDataTable.Free;
         inherited destroy;
         inherited destroy;
       end;
       end;