wasmlinkchange.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. { This file is part of wasmbin - a collection of WebAssembly binary utils.
  2. Copyright (C) 2019, 2020 Dmitry Boyarintsev <[email protected]>
  3. Copyright (C) 2020 by the Free Pascal development team
  4. This source is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 2 of the License, or (at your option)
  7. any later version.
  8. This code is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. details.
  12. A copy of the GNU General Public License is available on the World Wide Web
  13. at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
  14. to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1335, USA.
  16. }
  17. unit wasmlinkchange;
  18. {$mode objfpc}{$H+}
  19. interface
  20. uses Classes, SysUtils, wasmlink, wasmbin, lebutils;
  21. type
  22. TSymbolType = (
  23. st_Nochange,
  24. st_Local, // 'L'
  25. st_Weak, // 'W'
  26. st_VisHidden, // 'H' visibility-local
  27. st_VisDefault // 'D' visibility default (not-hidden)
  28. );
  29. TSymbolTypes = set of TSymbolType;
  30. TSymbolConfigure = class(TObject)
  31. symname : string;
  32. needtype : TSymbolType;
  33. end;
  34. procedure ReadSymbolsConf(const fn: string; dst: TStrings);
  35. procedure ReadSymbolsConf(src: TStream; dst: TStrings);
  36. procedure ProcessLinkingSection(st: TStream; syms: TStrings);
  37. implementation
  38. procedure ReadSymbolsConf(const fn: string; dst: TStrings);
  39. var
  40. fs: TFileStream;
  41. begin
  42. fs:=TFileStream.Create(fn, fmOpenRead or fmShareDenyNone);
  43. try
  44. ReadSymbolsConf(fs, dst);
  45. finally
  46. fs.Free;
  47. end;
  48. end;
  49. function StrToSymType(const s: string): TSymbolType;
  50. begin
  51. if length(s)=0 then
  52. Result:=st_Nochange
  53. else
  54. case upCase(s[1]) of
  55. 'L': Result:=st_Local;
  56. 'H': Result:=st_VisHidden;
  57. 'W': Result:=st_Weak;
  58. 'D': Result:=st_VisDefault;
  59. else
  60. Result:=st_Nochange;
  61. end;
  62. end;
  63. function StrToSymTypes(const s: string): TSymbolTypes;
  64. var
  65. i: integer;
  66. tt : TSymbolType;
  67. begin
  68. Result := [];
  69. for i:=1 to length(s) do begin
  70. tt := StrToSymType(s[i]);
  71. if tt <> st_Nochange then
  72. Include(Result, tt);
  73. end;
  74. end;
  75. procedure ReadSymbolsConf(src: TStream; dst: TStrings);
  76. begin
  77. dst.LoadFromStream(src);
  78. end;
  79. procedure ProcessLinkingSection(st: TStream; syms: TStrings);
  80. var
  81. mt : TLinkingMetadata;
  82. //en : Int64;
  83. sub : TLinkingSubSection;
  84. cnt : LongWord;
  85. nx : Int64;
  86. i : integer;
  87. si : TSymInfo;
  88. ofs : Int64;
  89. v : string;
  90. tt : TSymbolType;
  91. tts : TSymbolTypes;
  92. fl : LongWord;
  93. begin
  94. //en := st.Position+secsize;
  95. ReadMetadata(st, mt);
  96. writeln('version: ', mt.version);
  97. //while st.Position<en do begin
  98. ReadLinkSubSect(st, sub);
  99. nx := st.Position+sub.length;
  100. writeln('subsec=',SubSecTypeToStr(sub.sectype),' ',sub.sectype);
  101. cnt := ReadU(st);
  102. writeln('- symbol table [count=', cnt,']');
  103. for i:=0 to cnt-1 do begin
  104. write(' - ',i,' ');
  105. ofs := st.Position;
  106. ReadSymInfo(st, si);
  107. //write(SymKindToStr(si.kind),' ',IntToHex(si.flags,8));
  108. //if si.hasSymName then write(' ',si.symname);
  109. //writeln;
  110. if si.hasSymName then begin
  111. v := syms.Values[si.symname];
  112. fl := si.flags;
  113. tts := StrToSymTypes(v);
  114. for tt:=Low(TSymbolType) to High(TSymbolType) do begin
  115. if not (tt in tts) then continue;
  116. writeln(tt);
  117. case tt of
  118. st_Local:
  119. si.flags := (si.flags or WASM_SYM_BINDING_LOCAL) and (not WASM_SYM_BINDING_WEAK) and (not WASM_SYM_UNDEFINED);
  120. st_VisDefault:
  121. si.flags := (si.flags and not WASM_SYM_VISIBILITY_HIDDEN);
  122. st_VisHidden:
  123. si.flags := (si.flags or WASM_SYM_VISIBILITY_HIDDEN);
  124. st_Weak:
  125. si.flags := (si.flags or WASM_SYM_BINDING_WEAK) and (not WASM_SYM_BINDING_LOCAL)
  126. end;
  127. end;
  128. if fl <> si.flags then begin
  129. st.Position := ofs;
  130. WriteSymInfo(st, si);
  131. end;
  132. end;
  133. //writeln(si.symname);
  134. end;
  135. st.Position:=nx;
  136. //end;
  137. end;
  138. end.