Browse Source

+ wasm resource writer: create a list of relocation entries (not written to
file, yet). Add relocations to the list in TWasmResourceWriter.WriteResHeader

Nikolay Nikolov 1 year ago
parent
commit
8b356c7f60
2 changed files with 26 additions and 0 deletions
  1. 8 0
      packages/fcl-res/src/wasmtypes.pp
  2. 18 0
      packages/fcl-res/src/wasmwriter.pp

+ 8 - 0
packages/fcl-res/src/wasmtypes.pp

@@ -97,6 +97,14 @@ type
     SYMTAB_EVENT    = 4,
     SYMTAB_TABLE    = 5);
 
+  PWasmRelocationEntry = ^TWasmRelocationEntry;
+  TWasmRelocationEntry = record
+    Typ: TWasmRelocationType;
+    Offset: UInt32;
+    Index: UInt32;
+    Addend: Int32;
+  end;
+
   TWasmResourceDataSegment = (wrdsResources, wrdsResHandles);
 
 implementation

+ 18 - 0
packages/fcl-res/src/wasmwriter.pp

@@ -45,6 +45,7 @@ type
     fDataCurOfs : longword;
     FWasmSections: array [TWasmSectionID] of TMemoryStream;
     FDataSegments: array [TWasmResourceDataSegment] of TMemoryStream;
+    FDataRelocations: array of TWasmRelocationEntry;
     FWasmCustomSections: array [TWasmCustomSectionType] of TMemoryStream;
     FWasmLinkingSubsections: array [low(TWasmLinkingSubsectionType)..high(TWasmLinkingSubsectionType)] of TMemoryStream;
     FWasmSymbolTable: TMemoryStream;
@@ -52,6 +53,7 @@ type
     function NextAligned(aBound, aValue : longword) : longword;
     procedure PrescanResourceTree;
     function PrescanNode(aNode : TResourceTreeNode; aNodeSize : longword) : longword;
+    procedure AddDataRelocation(aTyp: TWasmRelocationType; aOffset: UInt32; aIndex: UInt32; aAddend: Int32 = 0);
     procedure WriteResHeader(aResources : TResources);
     procedure WriteWasmSection(aStream: TStream; wsid: TWasmSectionID);
     procedure WriteWasmSectionIfNotEmpty(aStream: TStream; wsid: TWasmSectionID);
@@ -161,6 +163,19 @@ begin
   Result:=curofs;
 end;
 
+procedure TWasmResourceWriter.AddDataRelocation(aTyp: TWasmRelocationType;
+  aOffset: UInt32; aIndex: UInt32; aAddend: Int32);
+begin
+  SetLength(FDataRelocations,Length(FDataRelocations)+1);
+  with FDataRelocations[High(FDataRelocations)] do
+  begin
+    Typ:=aTyp;
+    Offset:=aOffset;
+    Index:=aIndex;
+    Addend:=aAddend;
+  end;
+end;
+
 procedure TWasmResourceWriter.WriteResHeader(aResources: TResources);
 var hdr : TResHdr32;
 begin
@@ -174,9 +189,12 @@ begin
   //  SHT_REL  : hdr.rootptr:=sizeof(hdr);
   //  SHT_RELA : hdr.rootptr:=0;
   //end;
+  hdr.rootptr:=sizeof(hdr);
 
   //fRelocTable.Add(0,sizeof(hdr),RSRCSECT_IDX);
+  AddDataRelocation(R_WASM_SECTION_OFFSET_I32,0,Ord(wrdsResources),sizeof(hdr));
   //fRelocTable.Add(sizeof(hdr.rootptr)+sizeof(hdr.count)+sizeof(hdr.usedhandles),0,HANDLESECT_IDX);
+  AddDataRelocation(R_WASM_SECTION_OFFSET_I32,sizeof(hdr.rootptr)+sizeof(hdr.count)+sizeof(hdr.usedhandles),Ord(wrdsResHandles),0);
   if fOppositeEndianess then
   begin
     hdr.rootptr:=SwapEndian(hdr.rootptr);