t_wasi.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. {
  2. Copyright (c) 2019 by Dmitry Boyarintsev
  3. This unit implements support import,export,link routines
  4. for the WASI target
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit t_wasi;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. systems,
  23. globtype, globals,
  24. aasmbase,
  25. cfileutl, cutils, cclasses,
  26. import, export, aasmdata, aasmcpu,
  27. fmodule, ogbase,
  28. symsym, symdef,
  29. link,
  30. i_wasi, tgcpu;
  31. type
  32. { texportlibwasi }
  33. texportlibwasi=class(texportlib)
  34. procedure preparelib(const s : string);override;
  35. procedure exportprocedure(hp : texported_item);override;
  36. procedure exportvar(hp : texported_item);override;
  37. procedure generatelib;override;
  38. end;
  39. { timportlibwasi }
  40. timportlibwasi = class(timportlib)
  41. procedure generatelib;override;
  42. end;
  43. { tlinkerwasi }
  44. tlinkerwasi=class(texternallinker)
  45. public
  46. constructor Create;override;
  47. procedure SetDefaultInfo;override;
  48. function MakeExecutable:boolean;override;
  49. function MakeSharedLibrary:boolean;override;
  50. end;
  51. implementation
  52. uses
  53. verbose;
  54. { timportlibwasi }
  55. procedure timportlibwasi.generatelib;
  56. begin
  57. end;
  58. { tlinkerwasi }
  59. constructor tlinkerwasi.Create;
  60. begin
  61. inherited Create;
  62. end;
  63. procedure tlinkerwasi.SetDefaultInfo;
  64. begin
  65. Info.DllCmd[1] := 'wasm-ld $SONAME $GCSECTIONS -o $EXE';
  66. //Info.DllCmd[2] := 'wasmtool --exportrename $INPUT $EXE';
  67. end;
  68. function tlinkerwasi.MakeExecutable:boolean;
  69. var
  70. GCSectionsStr : ansistring;
  71. binstr, cmdstr : Tcmdstr;
  72. InitStr,
  73. FiniStr,
  74. SoNameStr : string[80];
  75. mapstr,ltostr : TCmdStr;
  76. success : Boolean;
  77. tmp : TCmdStrListItem;
  78. tempFileName : ansistring;
  79. begin
  80. if not(cs_link_nolink in current_settings.globalswitches) then
  81. Message1(exec_i_linking,current_module.exefilename);
  82. if (cs_link_smart in current_settings.globalswitches) and
  83. create_smartlink_sections then
  84. GCSectionsStr:='--gc-sections'
  85. else
  86. GCSectionsStr:='';
  87. SoNameStr:='';
  88. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  89. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  90. tmp := TCmdStrListItem(ObjectFiles.First);
  91. while Assigned(tmp) do begin
  92. cmdstr := tmp.Str+ ' ' + cmdstr;
  93. tmp := TCmdStrListItem(tmp.Next);
  94. end;
  95. // if HasExports then
  96. // cmdstr := cmdstr + ' --export-dynamic'; //' --export-dynamic';
  97. cmdstr := cmdstr + ' --no-entry --allow-undefined';
  98. if (cs_link_strip in current_settings.globalswitches) then
  99. begin
  100. { only remove non global symbols and debugging info for a library }
  101. cmdstr := cmdstr + ' --strip-all';
  102. end;
  103. //Replace(cmdstr,'$OPT',Info.ExtraOptions);
  104. //Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  105. //Replace(cmdstr,'$INIT',InitStr);
  106. //Replace(cmdstr,'$FINI',FiniStr);
  107. Replace(cmdstr,'$SONAME',SoNameStr);
  108. //Replace(cmdstr,'$MAP',mapstr);
  109. //Replace(cmdstr,'$LTO',ltostr);
  110. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  111. writeln(utilsprefix+binstr,' ',cmdstr);
  112. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  113. //SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  114. //Replace(cmdstr,'$INPUT',current_module.objfilename );
  115. //Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  116. //DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  117. MakeExecutable:=success;
  118. end;
  119. function tlinkerwasi.MakeSharedLibrary: boolean;
  120. var
  121. GCSectionsStr : ansistring;
  122. binstr, cmdstr : Tcmdstr;
  123. InitStr,
  124. FiniStr,
  125. SoNameStr : string[80];
  126. mapstr,ltostr : TCmdStr;
  127. success : Boolean;
  128. tmp : TCmdStrListItem;
  129. tempFileName : ansistring;
  130. begin
  131. //Result := true;
  132. //Result:=inherited MakeSharedLibrary;
  133. if (cs_link_smart in current_settings.globalswitches) and
  134. create_smartlink_sections then
  135. GCSectionsStr:='--gc-sections'
  136. else
  137. GCSectionsStr:='';
  138. SoNameStr:='';
  139. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  140. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  141. tmp := TCmdStrListItem(ObjectFiles.First);
  142. while Assigned(tmp) do begin
  143. cmdstr := tmp.Str+ ' ' + cmdstr;
  144. tmp := TCmdStrListItem(tmp.Next);
  145. end;
  146. if HasExports then
  147. cmdstr := cmdstr + ' --export-dynamic'; //' --export-dynamic';
  148. cmdstr := cmdstr + ' --no-entry --allow-undefined';
  149. if (cs_link_strip in current_settings.globalswitches) then
  150. begin
  151. { only remove non global symbols and debugging info for a library }
  152. cmdstr := cmdstr + ' --strip-all';
  153. end;
  154. //Replace(cmdstr,'$OPT',Info.ExtraOptions);
  155. //Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  156. //Replace(cmdstr,'$INIT',InitStr);
  157. //Replace(cmdstr,'$FINI',FiniStr);
  158. Replace(cmdstr,'$SONAME',SoNameStr);
  159. //Replace(cmdstr,'$MAP',mapstr);
  160. //Replace(cmdstr,'$LTO',ltostr);
  161. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  162. writeln(utilsprefix+binstr,' ',cmdstr);
  163. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  164. //SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  165. //Replace(cmdstr,'$INPUT',current_module.objfilename );
  166. //Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  167. //DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  168. MakeSharedLibrary:=success;
  169. end;
  170. { texportlibwasi }
  171. procedure texportlibwasi.preparelib(const s: string);
  172. begin
  173. //nothing to happen. wasm files are modules
  174. end;
  175. procedure texportlibwasi.exportprocedure(hp: texported_item);
  176. var
  177. nm : TSymStr;
  178. begin
  179. nm := tprocdef(tprocsym(hp.sym).ProcdefList[0]).mangledname;
  180. current_asmdata.asmlists[al_exports].Concat(tai_impexp.create(hp.name^, nm, ie_Func));
  181. end;
  182. procedure texportlibwasi.exportvar(hp: texported_item);
  183. begin
  184. //inherited exportvar(hp);
  185. end;
  186. procedure texportlibwasi.generatelib;
  187. begin
  188. //inherited generatelib;
  189. end;
  190. initialization
  191. RegisterTarget(system_wasi_info);
  192. RegisterImport(system_wasm32_wasi, timportlibwasi);
  193. RegisterExport(system_wasm32_wasi, texportlibwasi);
  194. RegisterLinker(ld_wasi, tlinkerwasi);
  195. end.