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

From a6d126af643ab89eef0298482a9bc6d832d5a77d Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <skalogryz.lists@gmail.com>
Date: Tue, 26 Nov 2019 09:16:47 -0500

git-svn-id: branches/wasm@46053 -
This commit is contained in:
nickysn 2020-08-03 12:59:46 +00:00
parent 8b1d615a77
commit 7029fd383a
2 changed files with 34 additions and 12 deletions

View File

@ -12,11 +12,12 @@ uses
wasmtoolutils;
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;
begin
@ -27,14 +28,19 @@ begin
writeln('options:');
writeln(' --exportrename @inputfile - renaming export names');
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;
type
{ 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)
action : string; // action to take place
paramsFn : string; // input file name
@ -53,7 +59,9 @@ end;
procedure ProcessParams(acts: TList; const inputFn: string; doVerbose: Boolean);
var
i : integer;
j : integer;
ta : TToolActions;
wk : TStringList;
begin
for i:=0 to acts.Count-1 do begin
ta := TToolActions(acts[i]);
@ -66,8 +74,14 @@ begin
ExportRename(inputFn, ta.paramsFn, doVerbose);
end else if ta.action = ACT_SYMBOLFLAG then begin
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;

View File

@ -9,7 +9,7 @@ uses
function ChangeSymbolFlagStream(st: TStream; syms: TStrings): Boolean;
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);
function ExportRenameSym(var x: TExportSection; syms: TStrings): Integer;
@ -68,7 +68,9 @@ procedure MatchExportNameToSymFlag(
const c: TCodeSection;
const e: TElementSection;
const x: TExportSection;
var l: TLinkingSection; doVerbose: Boolean);
var l: TLinkingSection;
weakList: TStrings; // do known set of globals weak
doVerbose: Boolean);
type
TFuncType = (ftImpl = 0, ftIntf, ftStub, ftExport);
@ -155,13 +157,19 @@ begin
writeln;
end;
//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;
function PredictSymbolsFromLink(const wasmfn: string; doVerbose: Boolean = false): Boolean;
function PredictSymbolsFromLink(const wasmfn: string; weakList: TStrings; doVerbose: Boolean = false): Boolean;
var
st : TFileStream;
dw : LongWord;
@ -246,7 +254,7 @@ begin
if Result then begin
if doVerbose then writeln('detecting symbols');
MatchExportNameToSymFlag(imp, c, e, x, l, doVerbose);
MatchExportNameToSymFlag(imp, c, e, x, l, weakList, doVerbose);
mem:=TMemoryStream.Create;
mem2:=TMemoryStream.Create;
try