Browse Source

[PATCH 053/188] writing linking information

From e8c74a902c4aef497203f7905e2cb8530bd49bf7 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Sat, 23 Nov 2019 19:01:47 -0500

git-svn-id: branches/wasm@46049 -
nickysn 5 years ago
parent
commit
96daa5050a
1 changed files with 42 additions and 3 deletions
  1. 42 3
      utils/wasmbin/wasmbinwriter.pas

+ 42 - 3
utils/wasmbin/wasmbinwriter.pas

@@ -31,6 +31,7 @@ type
     procedure AddReloc(relocType: byte; ofs: int64; index: UInt32);
     procedure AddReloc(relocType: byte; ofs: int64; index: UInt32);
 
 
     procedure WriteRelocU32(u: longword);
     procedure WriteRelocU32(u: longword);
+    procedure WriteString(const s: string);
     procedure SectionBegin(secId: byte; out secRec: TSectionRec; secsize: longWord=0);
     procedure SectionBegin(secId: byte; out secRec: TSectionRec; secsize: longWord=0);
     function SectionEnd(var secRec: TSectionRec): Boolean;
     function SectionEnd(var secRec: TSectionRec): Boolean;
 
 
@@ -41,6 +42,9 @@ type
     procedure WriteExportSect(m: TWasmModule);
     procedure WriteExportSect(m: TWasmModule);
     procedure WriteCodeSect(m: TWasmModule);
     procedure WriteCodeSect(m: TWasmModule);
 
 
+    procedure WriteLinkingSect(m: TWasmModule);
+    procedure WriteRelocSect(m: TWasmModule);
+
     procedure pushStream(st: TStream);
     procedure pushStream(st: TStream);
     function popStream: TStream;
     function popStream: TStream;
   public
   public
@@ -137,6 +141,13 @@ begin
   WriteU(dst, u, sizeof(u)*8, keepLeb128);
   WriteU(dst, u, sizeof(u)*8, keepLeb128);
 end;
 end;
 
 
+procedure TBinWriter.WriteString(const s: string);
+begin
+  WriteU32(dst, length(s));
+  if length(s)>0 then
+    dst.Write(s[1], length(s));
+end;
+
 function TBinWriter.Write(m: TWasmModule; adst: TStream): Boolean;
 function TBinWriter.Write(m: TWasmModule; adst: TStream): Boolean;
 var
 var
   l : Longword;
   l : Longword;
@@ -166,6 +177,11 @@ begin
   // 10 code section
   // 10 code section
   WriteCodeSect(m);
   WriteCodeSect(m);
 
 
+  if writeReloc then begin
+    WriteLinkingSect(m);
+    WriteRelocSect(m)
+  end;
+
   Result:=true;
   Result:=true;
 end;
 end;
 
 
@@ -225,7 +241,9 @@ begin
 
 
   WriteU32(dst, m.FuncCount);
   WriteU32(dst, m.FuncCount);
   for i:=0 to m.FuncCount-1 do
   for i:=0 to m.FuncCount-1 do
-    WriteRelocU32(m.GetFunc(i).functype.typeNum);
+    // wat2test doesn't write the function section as relocatable
+    // WriteRelocU32(m.GetFunc(i).functype.typeNum);
+    WriteU32(dst, m.GetFunc(i).functype.typeNum);
 
 
   SectionEnd(sc);
   SectionEnd(sc);
 end;
 end;
@@ -245,7 +263,10 @@ begin
     if length(x.name)>0 then
     if length(x.name)>0 then
       dst.Write(x.name[1], length(x.name));
       dst.Write(x.name[1], length(x.name));
     dst.WriteByte(x.exportType);
     dst.WriteByte(x.exportType);
-    WriteRelocU32(x.exportNum);
+
+    //wat2wasm doesn't write relocate the information
+    //WriteRelocU32(x.exportNum);
+    WriteU32(dst, x.exportNum);
   end;
   end;
 
 
   SectionEnd(sc);
   SectionEnd(sc);
@@ -296,7 +317,25 @@ begin
   SectionEnd(sc);
   SectionEnd(sc);
 end;
 end;
 
 
-procedure TBinWriter.WriteInstList(list: TWasmInstrList; ofsAddition: Longword);
+procedure TBinWriter.WriteLinkingSect(m: TWasmModule);
+var
+  sc : TSectionRec;
+begin
+  SectionBegin(SECT_CUSTOM, sc);
+  WriteString(SectionName_Linking);
+
+  WriteU32(dst, LINKING_VERSION);
+  // todo: fill out subsections
+
+  SectionEnd(sc);
+end;
+
+procedure TBinWriter.WriteRelocSect(m: TWasmModule);
+begin
+
+end;
+
+procedure TBinWriter.WriteInstList(list: TWasmInstrList; ofsAddition: LongWord);
 var
 var
   i  : integer;
   i  : integer;
   ci : TWasmInstr;
   ci : TWasmInstr;