wasmtoolutils.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 wasmtoolutils;
  18. {$mode objfpc}{$H+}
  19. interface
  20. uses
  21. Classes,SysUtils, wasmbin, lebutils,
  22. //wasmbindebug,
  23. wasmlink, wasmlinkchange;
  24. function ChangeSymbolFlagStream(st: TStream; syms: TStrings): Boolean;
  25. procedure ChangeSymbolFlag(const fn, symfn: string);
  26. function PredictSymbolsFromLink(const wasmfn: string; weakList: TStrings; doVerbose: Boolean = false): Boolean;
  27. procedure MatchExportNameToSymName(const x: TExportSection; const l: TLinkingSection; dst: TStrings);
  28. function ExportRenameSym(var x: TExportSection; syms: TStrings): Integer;
  29. function ExportRenameProcess(st, dst: TStream; syms: TStrings; doVerbose: Boolean): Boolean;
  30. function ExportNameGather(const wasmfile: string; syms: TStrings; doVerbose: Boolean = false): Boolean;
  31. procedure ExportRename(const fn, symfn: string; doVerbose: Boolean);
  32. implementation
  33. function ChangeSymbolFlagStream(st: TStream; syms: TStrings): Boolean;
  34. var
  35. dw : LongWord;
  36. ofs : int64;
  37. sc : TSection;
  38. ps : int64;
  39. nm : string;
  40. begin
  41. dw := st.ReadDWord;
  42. Result := dw = WasmId_Int;
  43. if not Result then Exit;
  44. dw := st.ReadDWord;
  45. while st.Position<st.Size do begin
  46. ofs := st.Position;
  47. sc.id := st.ReadByte;
  48. sc.Size := ReadU(st);
  49. ps := st.Position+sc.size;
  50. if sc.id=0 then begin
  51. nm := GetName(st);
  52. if nm = SectionName_Linking then begin
  53. ProcessLinkingSection(st, syms);
  54. break;
  55. end;
  56. //DumpLinking(st, sc.size - (st.Position - ofs));
  57. end;
  58. //if sc.id= 1 then DumpTypes(st);
  59. if st.Position <> ps then
  60. begin
  61. //writeln('adjust stream targ=',ps,' actual: ', st.position);
  62. st.Position := ps;
  63. end;
  64. end;
  65. end;
  66. // Assumption is made, there's only 1 table in the file!
  67. // if a function is a stub function (the only code is "unreachable"), the status given
  68. // "weak" (it's a reference function elsewhere)
  69. // if a function is located in the function table, then the status given is
  70. // "hidden" (do not add to the final linked executable)
  71. // if a function is not located in the function table, the status given is:
  72. // "hidden"+"local" (local means the function can be used only in this object file)
  73. procedure MatchExportNameToSymFlag(
  74. const imp: TImportSection;
  75. const c: TCodeSection;
  76. const e: TElementSection;
  77. const x: TExportSection;
  78. var l: TLinkingSection;
  79. weakList: TStrings; // do known set of globals weak
  80. doVerbose: Boolean);
  81. type
  82. TFuncType = (ftImpl = 0, ftIntf, ftStub, ftExport);
  83. TFuncInfo = record
  84. hasSymbol : Boolean;
  85. fnType : TFuncType;
  86. end;
  87. var
  88. i : integer;
  89. j : integer;
  90. idx : integer;
  91. fn : array of TFuncInfo;
  92. codeofs: integer;
  93. begin
  94. idx := -1;
  95. for i:=0 to length(l.symbols)-1 do
  96. if l.symbols[i].kind = SYMTAB_FUNCTION then begin
  97. if l.symbols[i].symindex>idx then begin
  98. idx:= l.symbols[i].symindex;
  99. end;
  100. end;
  101. SetLength(fn, idx+1);
  102. for i:=0 to length(l.symbols)-1 do
  103. if l.symbols[i].kind = SYMTAB_FUNCTION then begin
  104. idx := l.symbols[i].symindex;
  105. fn[idx].hasSymbol:=true;
  106. end;
  107. for i:=0 to length(e.entries)-1 do
  108. for j:=0 to length(e.entries[i].funcs)-1 do begin
  109. idx := e.entries[i].funcs[j];
  110. fn[idx].fnType:=ftIntf;
  111. end;
  112. codeofs:=0;
  113. for i:=0 to length(imp.entries)-1 do
  114. if imp.entries[i].desc = IMPDESC_FUNC then
  115. inc(codeofs);
  116. for i:=codeofs to length(fn)-1 do begin
  117. if not fn[i].hasSymbol then begin
  118. Continue;
  119. end;
  120. if (fn[i].fnType=ftImpl) and (isUnreachable(c.entries[i-codeofs])) then begin
  121. fn[i].fnType:=ftStub;
  122. end;
  123. end;
  124. for i:=0 to length(x.entries)-1 do begin
  125. if x.entries[i].desc = EXPDESC_FUNC then begin
  126. idx := x.entries[i].index;
  127. if fn[idx].fnType<>ftStub then
  128. fn[idx].fnType:=ftExport;
  129. end;
  130. end;
  131. for i:=0 to length(l.symbols)-1 do begin
  132. if l.symbols[i].kind = SYMTAB_FUNCTION then begin
  133. j := l.symbols[i].symindex;
  134. if j>=codeofs then // not imported
  135. case fn[j].fnType of
  136. ftImpl:
  137. l.symbols[i].flags := l.symbols[i].flags or WASM_SYM_VISIBILITY_HIDDEN or WASM_SYM_BINDING_LOCAL;
  138. ftIntf:
  139. l.symbols[i].flags := l.symbols[i].flags or WASM_SYM_VISIBILITY_HIDDEN;
  140. ftStub:
  141. l.symbols[i].flags := l.symbols[i].flags or WASM_SYM_BINDING_WEAK or WASM_SYM_VISIBILITY_HIDDEN;
  142. ftExport:
  143. //l.symbols[i].flags := l.symbols[i].flags or WASM_SYM_VISIBILITY_HIDDEN or WASM_SYM_BINDING_WEAK;
  144. ;
  145. end;
  146. if DoVerbose then begin
  147. write('func ');
  148. if l.symbols[i].hasSymName then
  149. write(l.symbols[i].symname)
  150. else
  151. write('#',j);
  152. write(' ', fn[j].fnType);
  153. writeln;
  154. end;
  155. //if l.symbols[i].symindex>mx then mx := ;
  156. end else if (l.symbols[i].kind = SYMTAB_GLOBAL) and Assigned(weakList) then begin
  157. if l.symbols[i].hasSymName and (weakList.IndexOf(l.symbols[i].symname)>=0) then begin
  158. if doVerbose then
  159. writeln('weakining: ',l.symbols[i].symname);
  160. l.symbols[i].flags := l.symbols[i].flags or WASM_SYM_BINDING_WEAK or WASM_SYM_VISIBILITY_HIDDEN;
  161. end;
  162. end;
  163. end;
  164. end;
  165. function PredictSymbolsFromLink(const wasmfn: string; weakList: TStrings; doVerbose: Boolean = false): Boolean;
  166. var
  167. st : TFileStream;
  168. dw : LongWord;
  169. foundCode : Boolean;
  170. foundElement : Boolean;
  171. foundLink : Boolean;
  172. foundExport : Boolean;
  173. foundImport : Boolean;
  174. ofs : Int64;
  175. ps : Int64;
  176. sc : TSection;
  177. c : TCodeSection;
  178. imp : TImportSection;
  179. l : TLinkingSection;
  180. e : TElementSection;
  181. x : TExportSection;
  182. nm : string;
  183. lofs : Int64;
  184. lsize : Int64;
  185. mem : TMemoryStream;
  186. mem2 : TMemoryStream;
  187. begin
  188. st := TFileStream.Create(wasmfn, fmOpenReadWrite or fmShareDenyNone);
  189. try
  190. dw := st.ReadDWord;
  191. Result := dw = WasmId_Int;
  192. if not Result then Exit;
  193. dw := st.ReadDWord;
  194. foundElement := false;
  195. foundCode := false;
  196. foundLink := false;
  197. foundExport := false;
  198. foundImport := false;
  199. Result := false;
  200. while st.Position<st.Size do begin
  201. ofs := st.Position;
  202. sc.id := st.ReadByte;
  203. sc.Size := ReadU(st);
  204. ps := st.Position+sc.size;
  205. case sc.id of
  206. SECT_IMPORT: begin
  207. ReadImportSection(st, imp);
  208. foundImport := true;
  209. end;
  210. SECT_EXPORT: begin
  211. ReadExport(st, x);
  212. foundExport := true;
  213. end;
  214. SECT_ELEMENT: begin
  215. ReadElementSection(st, e);
  216. foundElement := true;
  217. end;
  218. SECT_CODE: begin
  219. ReadCodeSection(st, c);
  220. foundCode := true;
  221. end;
  222. SECT_CUSTOM: begin
  223. nm := ReadName(st);
  224. if nm = SectionName_Linking then begin
  225. lofs:=ofs;
  226. ReadLinkingSection(st, sc.size, l);
  227. foundLink := true;
  228. lsize := ps-lofs;
  229. end;
  230. end;
  231. end;
  232. if st.Position <> ps then begin
  233. st.Position := ps;
  234. end;
  235. Result := foundLink and foundCode and foundElement;
  236. if Result then break;
  237. end;
  238. if not foundExport then SetLength(x.entries,0);
  239. if not foundImport then SetLength(imp.entries, 0);
  240. if Result then begin
  241. if doVerbose then writeln('detecting symbols');
  242. MatchExportNameToSymFlag(imp, c, e, x, l, weakList, doVerbose);
  243. mem:=TMemoryStream.Create;
  244. mem2:=TMemoryStream.Create;
  245. try
  246. st.Position:=lofs+lsize;
  247. mem2.CopyFrom(st, st.Size - st.Position);
  248. st.Position:=lofs;
  249. WriteName(mem, SectionName_Linking);
  250. WriteLinkingSection(mem, l);
  251. st.WriteByte(SECT_CUSTOM);
  252. if doVerbose then writeln('section size: ', mem.Size);
  253. WriteU32(st, mem.Size);
  254. mem.Position:=0;
  255. if doVerbose then writeln('copying from mem');
  256. st.CopyFrom(mem, mem.Size);
  257. mem2.Position:=0;
  258. if doVerbose then writeln('copying from mem2');
  259. st.CopyFrom(mem2, mem2.Size);
  260. st.Size:=st.Position;
  261. finally
  262. mem.Free;
  263. mem2.Free;
  264. end;
  265. if doVerbose then writeln('written: ', st.Position-lofs,' bytes');
  266. end else
  267. writeln('failed. section find status. Likning: ', foundLink,'; Code: ', foundCode,'; Element: ', foundElement);
  268. finally
  269. st.Free;
  270. end;
  271. end;
  272. procedure ChangeSymbolFlag(const fn, symfn: string);
  273. var
  274. fs :TFileStream;
  275. syms: TStringList;
  276. begin
  277. syms:=TStringList.Create;
  278. fs := TFileStream.Create(fn, fmOpenReadWrite or fmShareDenyNone);
  279. try
  280. if (symfn<>'') then begin
  281. ReadSymbolsConf(symfn, syms);
  282. ChangeSymbolFlagStream(fs, syms);
  283. end;
  284. finally
  285. fs.Free;
  286. syms.Free;
  287. end;
  288. end;
  289. function ExportRenameSym(var x: TExportSection; syms: TStrings): integer;
  290. var
  291. i : integer;
  292. v : string;
  293. begin
  294. Result := 0;
  295. for i:=0 to length(x.entries)-1 do begin
  296. v := syms.Values[x.entries[i].name];
  297. if v <> '' then begin
  298. x.entries[i].name := v;
  299. inc(Result);
  300. end;
  301. end;
  302. end;
  303. function ExportRenameProcess(st, dst: TStream; syms: TStrings; doVerbose: Boolean): Boolean;
  304. var
  305. dw : LongWord;
  306. ofs : int64;
  307. sc : TSection;
  308. ps : int64;
  309. x : TExportSection;
  310. mem : TMemoryStream;
  311. cnt : integer;
  312. begin
  313. dw := st.ReadDWord;
  314. Result := dw = WasmId_Int;
  315. if not Result then begin
  316. Exit;
  317. end;
  318. dw := st.ReadDWord;
  319. while st.Position<st.Size do begin
  320. ofs := st.Position;
  321. sc.id := st.ReadByte;
  322. sc.Size := ReadU(st);
  323. ps := st.Position+sc.size;
  324. if sc.id = SECT_EXPORT then begin
  325. if doVerbose then writeln(' export section found');
  326. ReadExport(st, x);
  327. cnt := ExportRenameSym(x, syms);
  328. st.Position:=0;
  329. dst.CopyFrom(st, ofs);
  330. st.Position:=ps;
  331. mem := TMemoryStream.Create;
  332. WriteExport(x, mem);
  333. mem.Position:=0;
  334. dst.WriteByte(SECT_EXPORT);
  335. WriteU32(dst, mem.Size);
  336. dst.CopyFrom(mem, mem.Size);
  337. dst.CopyFrom(st, st.Size-st.Position);
  338. break;
  339. end;
  340. if st.Position <> ps then
  341. st.Position := ps;
  342. end;
  343. end;
  344. // match between exported function names and symbol names
  345. procedure MatchExportNameToSymName(const x: TExportSection; const l: TLinkingSection; dst: TStrings);
  346. var
  347. expname : string;
  348. i,j : integer;
  349. begin
  350. for i:=0 to length(x.entries)-1 do begin
  351. // gathering only function names for now
  352. if x.entries[i].desc <> EXPDESC_FUNC then continue;
  353. expname := x.entries[i].name;
  354. for j:=0 to length(l.symbols)-1 do begin
  355. if (l.symbols[j].kind = SYMTAB_FUNCTION)
  356. and (l.symbols[j].symindex = x.entries[i].index)
  357. and (l.symbols[j].hasSymName)
  358. then
  359. dst.Values[ l.symbols[j].symname ] := expname;
  360. end;
  361. end;
  362. end;
  363. function ExportNameGather(const wasmfile: string; syms: TStrings; doVerbose: Boolean = false): Boolean;
  364. var
  365. dw : LongWord;
  366. ofs : int64;
  367. sc : TSection;
  368. ps : int64;
  369. mem : TMemoryStream;
  370. cnt : integer;
  371. st : TFileStream;
  372. nm : string;
  373. x : TExportSection;
  374. foundExport: Boolean;
  375. l : TLinkingSection;
  376. foundLink: Boolean;
  377. begin
  378. st := TFileStream.Create(wasmfile, fmOpenRead or fmShareDenyNone);
  379. try
  380. dw := st.ReadDWord;
  381. Result := dw = WasmId_Int;
  382. if not Result then begin
  383. Exit;
  384. end;
  385. dw := st.ReadDWord;
  386. foundExport:=false;
  387. foundLink:=false;
  388. while st.Position<st.Size do begin
  389. ofs := st.Position;
  390. sc.id := st.ReadByte;
  391. sc.Size := ReadU(st);
  392. ps := st.Position+sc.size;
  393. if sc.id = SECT_EXPORT then begin
  394. if doVerbose then writeln(' export section found');
  395. ReadExport(st, x);
  396. cnt := ExportRenameSym(x, syms);
  397. foundExport:=true;
  398. end else if sc.id = SECT_CUSTOM then begin
  399. nm := ReadName(st);
  400. if nm = SectionName_Linking then begin
  401. foundLink := true;
  402. ReadLinkingSection(st, sc.size, l);
  403. end;
  404. end;
  405. if st.Position <> ps then
  406. st.Position := ps;
  407. end;
  408. Result := foundLink and foundExport;
  409. if Result then
  410. MatchExportNameToSymName(x, l, syms);
  411. finally
  412. st.Free;
  413. end;
  414. end;
  415. procedure ExportRename(const fn, symfn: string; doVerbose: Boolean);
  416. var
  417. fs : TFileStream;
  418. syms : TStringList;
  419. dst : TMemoryStream;
  420. begin
  421. if doVerbose then writeln('Export symbols renaming');
  422. syms:=TStringList.Create;
  423. fs := TFileStream.Create(fn, fmOpenReadWrite or fmShareDenyNone);
  424. dst := TMemoryStream.Create;
  425. try
  426. if (symfn <> '') and fileExists(symfn) then
  427. begin
  428. if doVerbose then writeln('reading symbols: ', symfn);
  429. if isWasmFile(symfn) then
  430. ExportNameGather(symfn, syms, doVerbose)
  431. else
  432. syms.LoadFromFile(symfn);
  433. if doVerbose then write(syms.Text);
  434. end;
  435. ExportRenameProcess(fs, dst, syms, doVerbose);
  436. fs.Position:=0;
  437. dst.Position:=0;
  438. fs.CopyFrom(dst, dst.Size);
  439. fs.Size:=dst.Size;
  440. finally
  441. dst.Free;
  442. fs.Free;
  443. syms.Free;
  444. end;
  445. end;
  446. end.