Browse Source

[PATCH 005/188] update binary writing utilities

From 3751fab664131c25a47b65db3bfbc7189f8a9676 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Wed, 25 Sep 2019 10:33:10 -0400

git-svn-id: branches/wasm@46001 -
nickysn 5 years ago
parent
commit
8f456bbeb5
2 changed files with 37 additions and 7 deletions
  1. 8 0
      utils/wasmbin/wasmbin.pas
  2. 29 7
      utils/wasmbin/wasmlink.pas

+ 8 - 0
utils/wasmbin/wasmbin.pas

@@ -95,6 +95,7 @@ function ValTypeToStr(id: integer): string;
 //    size - in butes Leb128
 //    bytes - in utf8 format
 function ReadName(st: TStream): string;
+procedure WriteName(st: TStream; const str: string);
 function GetName(sr: TStream): string;
 
 // reads
@@ -151,6 +152,13 @@ begin
   if ln>0 then st.Read(result[1], ln);
 end;
 
+procedure WriteName(st: TStream; const str: string);
+begin
+  WriteU32(st, length(str));
+  if length(str)>0 then
+    st.Write(str[1], length(str));
+end;
+
 function GetName(sr: TStream): string;
 begin
   Result := ReadName(sr);

+ 29 - 7
utils/wasmbin/wasmlink.pas

@@ -67,16 +67,16 @@ const
 
 type
   TSymInfo = record
-    kind    : UInt8;
-    flags   : UInt32;
+    kind        : UInt8;
+    flags       : UInt32;
 
     hasSymIndex : Boolean; // always true for Kind non Data
                            // for Data it's true for defined symbols (see flags);
-    symindex   : UInt32; // either symbol or data symbol offset
-    hasSymName : Boolean;
-    symname    : string;
-    dataofs    : integer; // only if Kind is Data and hasSymIndex = true;
-    datasize   : integer;
+    symindex    : UInt32;  // either symbol or data symbol offset
+    hasSymName  : Boolean;
+    symname     : string;
+    dataofs     : integer; // only if Kind is Data and hasSymIndex = true;
+    datasize    : integer;
 
   end;
 
@@ -134,6 +134,8 @@ function ReadMetaData(st: TStream; out m:TLinkingMetadata): Boolean;
 function ReadLinkSubSect(st: TStream; out m: TLinkinSubSection): Boolean;
 function ReadSymInfo(st: TStream; out m: TSymInfo): Boolean;
 
+procedure WriteSymInfo(st: TStream; const m: TSymInfo);
+
 // dumps linking information. Note: that the name of the "Linking" section
 // must have already been read
 procedure DumpLinking(st: TStream; secsize: integer);
@@ -187,6 +189,26 @@ begin
   Result := true;
 end;
 
+procedure WriteSymInfo(st: TStream; const m: TSymInfo);
+begin
+  st.WriteByte(m.kind);
+  WriteU32(st, m.flags);
+
+  if m.kind = SYMTAB_DATA then begin
+    WriteName(st, m.symname);
+    if (m.flags and WASM_SYM_UNDEFINED)=0 then begin
+      WriteU32(st, m.symindex);
+      WriteU32(st, m.dataofs);
+      WriteU32(st, m.datasize);
+    end;
+
+  end else begin
+    WriteU32(st, m.symindex);
+    if ((m.flags and WASM_SYM_IMPORTED) = 0) or ((m.flags and WASM_SYM_EXPLICIT_NAME) > 0) then
+      WriteName(st, m.symname);
+  end;
+end;
+
 procedure DumpLinking(st: TStream; secsize: integer);
 var
   mt  : TLinkingMetadata;