Browse Source

[PATCH 057/188] adding an option to mark global variable as weak

From a6d126af643ab89eef0298482a9bc6d832d5a77d Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Tue, 26 Nov 2019 09:16:47 -0500

git-svn-id: branches/wasm@46053 -
nickysn 5 years ago
parent
commit
7029fd383a
2 changed files with 34 additions and 12 deletions
  1. 22 8
      utils/wasmbin/wasmtool.lpr
  2. 12 4
      utils/wasmbin/wasmtoolutils.pas

+ 22 - 8
utils/wasmbin/wasmtool.lpr

@@ -12,11 +12,12 @@ uses
   wasmtoolutils;
   wasmtoolutils;
 
 
 const
 const
-  ACT_EXPORTRENAME = 'exportrename';
-  ACT_SYMBOLFLAG   = 'symbolflag';
-  ACT_SYMBOLAUTO   = 'symbolauto';
+  ACT_EXPORTRENAME   = 'exportrename';
+  ACT_SYMBOLFLAG     = 'symbolflag';
+  ACT_SYMBOLAUTO     = 'symbolauto';
+  ACT_WEAK           = 'weak';
 
 
-  VERSION = '1.0';
+  VERSION = '1.1';
 
 
 procedure PrintHelp;
 procedure PrintHelp;
 begin
 begin
@@ -27,14 +28,19 @@ begin
   writeln('options:');
   writeln('options:');
   writeln('  --exportrename @inputfile - renaming export names');
   writeln('  --exportrename @inputfile - renaming export names');
   writeln('  --symbolflag   @inputfile - update symbol flags as specified in input');
   writeln('  --symbolflag   @inputfile - update symbol flags as specified in input');
-  writeln('  --symbolauto              - update symbol by the use');
-  writeln('  --verbose - enabling verbose mode');
+  writeln('  --symbolauto              - update symbol by the use ');
+  writeln('    --weak %name%           - specify symbol names for global variables to be marked weak reference');
 end;
 end;
 
 
 type
 type
 
 
   { TToolActions }
   { TToolActions }
 
 
+  // it's assumed that the wasmtool will be instructed
+  // to take mulitple actions on the same file.
+  // thus every action is recorded as TToolActions
+  // Those are created during the parameters parsing
+
   TToolActions = class(TObject)
   TToolActions = class(TObject)
     action   : string; // action to take place
     action   : string; // action to take place
     paramsFn : string; // input file name
     paramsFn : string; // input file name
@@ -53,7 +59,9 @@ end;
 procedure ProcessParams(acts: TList; const inputFn: string; doVerbose: Boolean);
 procedure ProcessParams(acts: TList; const inputFn: string; doVerbose: Boolean);
 var
 var
   i  : integer;
   i  : integer;
+  j  : integer;
   ta : TToolActions;
   ta : TToolActions;
+  wk : TStringList;
 begin
 begin
   for i:=0 to acts.Count-1 do begin
   for i:=0 to acts.Count-1 do begin
     ta := TToolActions(acts[i]);
     ta := TToolActions(acts[i]);
@@ -66,8 +74,14 @@ begin
       ExportRename(inputFn, ta.paramsFn, doVerbose);
       ExportRename(inputFn, ta.paramsFn, doVerbose);
     end else if ta.action = ACT_SYMBOLFLAG then begin
     end else if ta.action = ACT_SYMBOLFLAG then begin
       ChangeSymbolFlag(inputFn, ta.paramsFn);
       ChangeSymbolFlag(inputFn, ta.paramsFn);
-    end else if ta.action = ACT_SYMBOLAUTO then begin
-      PredictSymbolsFromLink(inputFn, doVerbose);
+    end else if (ta.action = ACT_SYMBOLAUTO) then begin
+      wk := TStringList.Create;
+      for j:=0 to acts.Count-1 do begin
+        if TToolActions(acts[j]).action = ACT_WEAK then
+          wk.Add( TToolActions(acts[j]).paramsFn );
+      end;
+      PredictSymbolsFromLink(inputFn, wk, doVerbose);
+      wk.free;
     end;
     end;
   end;
   end;
 end;
 end;

+ 12 - 4
utils/wasmbin/wasmtoolutils.pas

@@ -9,7 +9,7 @@ uses
 
 
 function ChangeSymbolFlagStream(st: TStream; syms: TStrings): Boolean;
 function ChangeSymbolFlagStream(st: TStream; syms: TStrings): Boolean;
 procedure ChangeSymbolFlag(const fn, symfn: string);
 procedure ChangeSymbolFlag(const fn, symfn: string);
-function PredictSymbolsFromLink(const wasmfn: string; doVerbose: Boolean = false): Boolean;
+function PredictSymbolsFromLink(const wasmfn: string; weakList: TStrings; doVerbose: Boolean = false): Boolean;
 
 
 procedure MatchExportNameToSymName(const x: TExportSection; const l: TLinkingSection; dst: TStrings);
 procedure MatchExportNameToSymName(const x: TExportSection; const l: TLinkingSection; dst: TStrings);
 function ExportRenameSym(var x: TExportSection; syms: TStrings): Integer;
 function ExportRenameSym(var x: TExportSection; syms: TStrings): Integer;
@@ -68,7 +68,9 @@ procedure MatchExportNameToSymFlag(
   const c: TCodeSection;
   const c: TCodeSection;
   const e: TElementSection;
   const e: TElementSection;
   const x: TExportSection;
   const x: TExportSection;
-  var l: TLinkingSection; doVerbose: Boolean);
+  var l: TLinkingSection;
+  weakList: TStrings; // do known set of globals weak
+  doVerbose: Boolean);
 type
 type
   TFuncType = (ftImpl = 0, ftIntf, ftStub, ftExport);
   TFuncType = (ftImpl = 0, ftIntf, ftStub, ftExport);
 
 
@@ -155,13 +157,19 @@ begin
         writeln;
         writeln;
       end;
       end;
       //if l.symbols[i].symindex>mx then mx := ;
       //if l.symbols[i].symindex>mx then mx := ;
+    end else if (l.symbols[i].kind = SYMTAB_GLOBAL) and Assigned(weakList) then begin
+      if l.symbols[i].hasSymName and (weakList.IndexOf(l.symbols[i].symname)>=0) then begin
+        if doVerbose then
+          writeln('weakining: ',l.symbols[i].symname);
+        l.symbols[i].flags := l.symbols[i].flags or WASM_SYM_BINDING_WEAK or WASM_SYM_VISIBILITY_HIDDEN;
+      end;
     end;
     end;
   end;
   end;
 
 
 
 
 end;
 end;
 
 
-function PredictSymbolsFromLink(const wasmfn: string; doVerbose: Boolean = false): Boolean;
+function PredictSymbolsFromLink(const wasmfn: string; weakList: TStrings; doVerbose: Boolean = false): Boolean;
 var
 var
   st : TFileStream;
   st : TFileStream;
   dw : LongWord;
   dw : LongWord;
@@ -246,7 +254,7 @@ begin
 
 
     if Result then begin
     if Result then begin
       if doVerbose then writeln('detecting symbols');
       if doVerbose then writeln('detecting symbols');
-      MatchExportNameToSymFlag(imp, c, e, x, l, doVerbose);
+      MatchExportNameToSymFlag(imp, c, e, x, l, weakList, doVerbose);
       mem:=TMemoryStream.Create;
       mem:=TMemoryStream.Create;
       mem2:=TMemoryStream.Create;
       mem2:=TMemoryStream.Create;
       try
       try