wasmld.lpr 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. program wasmld;
  18. {$mode objfpc}{$H+}
  19. uses
  20. {$IFDEF UNIX}{$IFDEF UseCThreads}
  21. cthreads,
  22. {$ENDIF}{$ENDIF}
  23. { you can add units after this }
  24. Classes, SysUtils, wasmbin, lebutils, wasmbindebug, wasmlink, wasmlinkchange;
  25. function ReadStream(st: TStream): Boolean;
  26. var
  27. dw : LongWord;
  28. ofs : int64;
  29. sc : TSection;
  30. ps : int64;
  31. nm : string;
  32. begin
  33. dw := st.ReadDWord;
  34. Result := dw = WasmId_Int;
  35. if not Result then begin
  36. writeln('not a wasm file');
  37. Exit;
  38. end;
  39. dw := st.ReadDWord;
  40. writeln('version: ', dw);
  41. while st.Position<st.Size do begin
  42. ofs := st.Position;
  43. sc.id := st.ReadByte;
  44. sc.Size := ReadU(st);
  45. writeln(ofs,': id=', sc.id,'(', SectionIdToStr(sc.id),') sz=', sc.size);
  46. ps := st.Position+sc.size;
  47. if sc.id=0 then begin
  48. nm := GetName(st);
  49. writeln(nm);
  50. if nm = SectionName_Linking then
  51. DumpLinking(st, sc.size - (st.Position - ofs));
  52. end;
  53. //if sc.id= 1 then DumpTypes(st);
  54. if st.Position <> ps then
  55. begin
  56. //writeln('adjust stream targ=',ps,' actual: ', st.position);
  57. st.Position := ps;
  58. end;
  59. end;
  60. end;
  61. procedure ReadWasmFile(const fn: string);
  62. var
  63. fs :TFileStream;
  64. begin
  65. fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyNone);
  66. try
  67. ReadStream(fs);
  68. finally
  69. fs.Free;
  70. end;
  71. end;
  72. function WriteStream(st: TStream; syms: TStrings): Boolean;
  73. var
  74. dw : LongWord;
  75. ofs : int64;
  76. sc : TSection;
  77. ps : int64;
  78. nm : string;
  79. begin
  80. writeln('read: ');
  81. dw := st.ReadDWord;
  82. writeln('dw: ' ,dw);
  83. Result := dw = WasmId_Int;
  84. if not Result then begin
  85. writeln('not a wasm file');
  86. Exit;
  87. end;
  88. dw := st.ReadDWord;
  89. writeln('version: ', dw);
  90. while st.Position<st.Size do begin
  91. ofs := st.Position;
  92. sc.id := st.ReadByte;
  93. sc.Size := ReadU(st);
  94. writeln(ofs,': id=', sc.id,'(', SectionIdToStr(sc.id),') sz=', sc.size);
  95. ps := st.Position+sc.size;
  96. if sc.id=0 then begin
  97. nm := GetName(st);
  98. writeln(nm);
  99. if nm = SectionName_Linking then begin
  100. writeln('rewriting linking...');
  101. ProcessLinkingSection(st, syms);
  102. break;
  103. end;
  104. //DumpLinking(st, sc.size - (st.Position - ofs));
  105. end;
  106. //if sc.id= 1 then DumpTypes(st);
  107. if st.Position <> ps then
  108. begin
  109. //writeln('adjust stream targ=',ps,' actual: ', st.position);
  110. st.Position := ps;
  111. end;
  112. end;
  113. end;
  114. procedure ProcessWasmFile(const fn, symfn: string);
  115. var
  116. fs :TFileStream;
  117. syms: TStringList;
  118. begin
  119. writeln('proc: ', fn);
  120. syms:=TStringList.Create;
  121. fs := TFileStream.Create(fn, fmOpenReadWrite or fmShareDenyNone);
  122. try
  123. if (symfn<>'') then
  124. ReadSymbolsConf(symfn, syms);
  125. writeln('size: ', fs.size);
  126. WriteStream(fs, syms);
  127. finally
  128. fs.Free;
  129. syms.Free;
  130. end;
  131. end;
  132. procedure RenameExport(var x: TExportSection; syms: TStrings);
  133. var
  134. i : integer;
  135. v : string;
  136. begin
  137. for i:=0 to length(x.entries)-1 do begin
  138. v := syms.Values[x.entries[i].name];
  139. if v <> '' then
  140. x.entries[i].name := v;
  141. end;
  142. end;
  143. procedure ParseCode(st: TStream);
  144. var
  145. cnt : integer;
  146. cs : TCodeSection;
  147. i : integer;
  148. begin
  149. ReadCodeSection(st, cs);
  150. writeln('code=',length(cs.entries));
  151. for i:=0 to length(cs.entries)-1 do begin
  152. writelN('code[', i,'] ', isUnreachable(cs.entries[i]));
  153. writeln(' locals= ', length(cs.entries[i].locals));
  154. writeln(' code = ', length(cs.entries[i].instBuf));
  155. writeln(' inst = ', IntToHex(cs.entries[i].instBuf[0],2),' ',IntToHex(cs.entries[i].instBuf[1],2));
  156. end;
  157. end;
  158. procedure ParseTable(st: TStream);
  159. begin
  160. end;
  161. procedure ParseElems(st: TStream);
  162. var
  163. s : TElementSection;
  164. i : integer;
  165. j : integer;
  166. begin
  167. ReadElementSection(st, s);
  168. writelN('entries = ', length(s.entries));
  169. for i:=0 to length(s.entries)-1 do begin
  170. writeln(' ',i,' functions = ', length(s.entries[i].funcs));
  171. for j:=0 to length(s.entries[i].funcs) - 1 do
  172. writeln(' ',s.entries[i].funcs[j]);
  173. end;
  174. end;
  175. function ProcessSections(st, dst: TStream; syms: TStrings): Boolean;
  176. var
  177. dw : LongWord;
  178. ofs : int64;
  179. sc : TSection;
  180. ps : int64;
  181. x : TExportSection;
  182. i : integer;
  183. mem : TMemoryStream;
  184. begin
  185. dw := st.ReadDWord;
  186. Result := dw = WasmId_Int;
  187. if not Result then begin
  188. Exit;
  189. end;
  190. dw := st.ReadDWord;
  191. while st.Position<st.Size do begin
  192. ofs := st.Position;
  193. sc.id := st.ReadByte;
  194. sc.Size := ReadU(st);
  195. writeln(ofs,': id=', sc.id,'(', SectionIdToStr(sc.id),') sz=', sc.size);
  196. ps := st.Position+sc.size;
  197. if sc.id=SECT_ELEMENT then begin
  198. ParseElems(st);
  199. end else if sc.id=SECT_TABLE then begin
  200. ParseTable(st);
  201. end else if sc.id=SECT_CODE then begin
  202. ParseCode(st);
  203. (*end else if sc.id = SECT_EXPORT then begin
  204. ReadExport(st, x);
  205. RenameExport(x, syms);
  206. st.Position:=0;
  207. dst.CopyFrom(st, ofs);
  208. st.Position:=ps;
  209. mem := TMemoryStream.Create;
  210. WriteExport(x, mem);
  211. mem.Position:=0;
  212. dst.WriteByte(SECT_EXPORT);
  213. WriteU32(dst, mem.Size);
  214. dst.CopyFrom(mem, mem.Size);
  215. writeln('entries = ', length(x.entries));
  216. for i:=0 to length(x.entries)-1 do begin
  217. writeln(x.entries[i].desc,' ', x.entries[i].name)
  218. end;
  219. dst.CopyFrom(st, st.Size-st.Position);
  220. break; // done
  221. *)
  222. end;
  223. {if sc.id=0 then begin
  224. nm := GetName(st);
  225. writeln(nm);
  226. if nm = SectionName_Linking then begin
  227. writeln('rewriting linking...');
  228. ProcessLinkingSection(st, syms);
  229. break;
  230. end;
  231. //DumpLinking(st, sc.size - (st.Position - ofs));
  232. end;}
  233. //if sc.id= 1 then DumpTypes(st);
  234. if st.Position <> ps then
  235. begin
  236. //writeln('adjust stream targ=',ps,' actual: ', st.position);
  237. st.Position := ps;
  238. end;
  239. end;
  240. end;
  241. procedure ProcessWasmSection(const fn, {%H-}symfn: string);
  242. var
  243. fs : TFileStream;
  244. syms : TStringList;
  245. dst : TMemoryStream;
  246. begin
  247. writeln('proc: ', fn);
  248. syms:=TStringList.Create;
  249. fs := TFileStream.Create(fn, fmOpenReadWrite or fmShareDenyNone);
  250. dst := TMemoryStream.Create;
  251. try
  252. if (symfn <> '') and fileExists(symfn) then
  253. syms.LoadFromFile(symfn);
  254. ProcessSections(fs, dst, syms);
  255. if dst.Size>0 then begin
  256. fs.Position:=0;
  257. dst.Position:=0;
  258. fs.CopyFrom(dst, dst.Size);
  259. fs.Size:=dst.Size;
  260. end;
  261. finally
  262. dst.Free;
  263. fs.Free;
  264. syms.Free;
  265. end;
  266. end;
  267. var
  268. symfn : string;
  269. begin
  270. if ParamCount=0 then begin
  271. writeln('please specify .wasm file');
  272. exit;
  273. end;
  274. symfn:='';
  275. if ParamCount>1 then symfn:=ParamStr(2);
  276. //ReadWasmFile(ParamStr(1));
  277. //ProcessWasmFile(ParamStr(1), symfn);
  278. ProcessWasmSection(ParamStr(1), symfn);
  279. end.