Parcourir la source

[PATCH 008/188] adding processing of link sections

From 8f3c9e9f94e2d6ffa3efd08b6d8015a3608e17d4 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Wed, 25 Sep 2019 17:03:45 -0400

git-svn-id: branches/wasm@46004 -
nickysn il y a 5 ans
Parent
commit
50df4ac869
3 fichiers modifiés avec 127 ajouts et 6 suppressions
  1. 1 0
      .gitattributes
  2. 73 6
      utils/wasmbin/wasmld.lpr
  3. 53 0
      utils/wasmbin/wasmlinkchange.pas

+ 1 - 0
.gitattributes

@@ -18982,3 +18982,4 @@ utils/wasmbin/wasmbindebug.pas svneol=native#text/plain
 utils/wasmbin/wasmld.lpi svneol=native#text/plain
 utils/wasmbin/wasmld.lpr svneol=native#text/plain
 utils/wasmbin/wasmlink.pas svneol=native#text/plain
+utils/wasmbin/wasmlinkchange.pas svneol=native#text/plain

+ 73 - 6
utils/wasmbin/wasmld.lpr

@@ -7,7 +7,7 @@ uses
   cthreads,
   {$ENDIF}{$ENDIF}
   { you can add units after this }
-  Classes, SysUtils, wasmbin, lebutils, wasmbindebug, wasmlink;
+  Classes, SysUtils, wasmbin, lebutils, wasmbindebug, wasmlink, wasmlinkchange;
 
 function ReadStream(st: TStream): Boolean;
 var
@@ -49,18 +49,85 @@ begin
 
 end;
 
+procedure ReadWasmFile(const fn: string);
 var
   fs :TFileStream;
 begin
-  if ParamCount=0 then begin
-    writeln('please sepcify .wasm file');
-    exit;
-  end;
-  fs := TFileStream.Create(ParamStr(1), fmOpenRead or fmShareDenyNone);
+  fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyNone);
   try
     ReadStream(fs);
   finally
     fs.Free;
   end;
+end;
+
+
+function WriteStream(st: TStream): Boolean;
+var
+  dw  : LongWord;
+  ofs : int64;
+  sc  : TSection;
+  ps  : int64;
+  nm  : string;
+begin
+  writeln('read: ');
+  dw := st.ReadDWord;
+  writeln('dw: ' ,dw);
+  Result := dw = WasmId_Int;
+  if not Result then begin
+    writeln('not a wasm file');
+    Exit;
+  end;
+  dw := st.ReadDWord;
+  writeln('version: ', dw);
+  while st.Position<st.Size do begin
+    ofs := st.Position;
+    sc.id := st.ReadByte;
+    sc.Size := ReadU(st);
+    writeln(ofs,': id=', sc.id,'(', SectionIdToStr(sc.id),') sz=', sc.size);
+
+    ps := st.Position+sc.size;
+    if sc.id=0 then begin
+      nm := GetName(st);
+      writeln(nm);
+      if nm = SectionName_Linking then begin
+        writeln('rewriting linking...');
+        ProcessLinkingSection(st);
+        break;
+      end;
+        //DumpLinking(st, sc.size - (st.Position - ofs));
+    end;
+    //if sc.id= 1 then DumpTypes(st);
+
+    if st.Position <> ps then
+    begin
+      //writeln('adjust stream targ=',ps,' actual: ', st.position);
+      st.Position := ps;
+    end;
+  end;
+end;
+
+procedure ProcessWasmFile(const fn: string);
+var
+  fs :TFileStream;
+begin
+  writeln('proc: ', fn);
+  fs := TFileStream.Create(fn, fmOpenReadWrite or fmShareDenyNone);
+  try
+    writeln('size: ', fs.size);
+    WriteStream(fs);
+  finally
+    fs.Free;
+  end;
+end;
+
+begin
+  if ParamCount=0 then begin
+    writeln('please specify .wasm file');
+    exit;
+  end;
+
+  //ReadWasmFile(ParamStr(1));
+  ProcessWasmFile(ParamStr(1));
 end.
 

+ 53 - 0
utils/wasmbin/wasmlinkchange.pas

@@ -0,0 +1,53 @@
+unit wasmlinkchange;
+
+interface
+
+uses Classes, SysUtils, wasmlink, wasmbin, lebutils;
+
+procedure ProcessLinkingSection(st: TStream);
+
+implementation
+
+procedure ProcessLinkingSection(st: TStream);
+var
+  mt  : TLinkingMetadata;
+  //en  : Int64;
+  sub : TLinkinSubSection;
+  cnt : LongWord;
+  nx  : Int64;
+  i   : integer;
+  si  : TSymInfo;
+  ofs : Int64;
+begin
+  //en := st.Position+secsize;
+  ReadMetadata(st, mt);
+  writeln('version: ', mt.version);
+  //while st.Position<en do begin
+    ReadLinkSubSect(st, sub);
+    nx := st.Position+sub.length;
+
+    writeln('subsec=',SubSecTypeToStr(sub.sectype),' ',sub.sectype);
+    cnt := ReadU(st);
+    writeln('- symbol table [count=', cnt,']');
+    for i:=0 to cnt-1 do begin
+      write('  - ',i,' ');
+
+      ofs := st.Position;
+      ReadSymInfo(st, si);
+      //write(SymKindToStr(si.kind),' ',IntToHex(si.flags,8));
+      //if si.hasSymName then write(' ',si.symname);
+      //writeln;
+      if si.flags and WASM_SYM_UNDEFINED = 0 then
+        si.flags := si.flags or WASM_SYM_BINDING_LOCAL;
+
+      st.Position := ofs;
+      WriteSymInfo(st, si);
+
+      //writeln(si.symname);
+    end;
+
+    st.Position:=nx;
+  //end;
+end;
+
+end.