t_wasi.pas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. SysUtils,
  54. verbose;
  55. { timportlibwasi }
  56. procedure timportlibwasi.generatelib;
  57. begin
  58. end;
  59. { tlinkerwasi }
  60. constructor tlinkerwasi.Create;
  61. begin
  62. inherited Create;
  63. end;
  64. procedure tlinkerwasi.SetDefaultInfo;
  65. begin
  66. Info.DllCmd[1] := 'wasm-ld $SONAME $GCSECTIONS $MAP -o $EXE';
  67. //Info.DllCmd[2] := 'wasmtool --exportrename $INPUT $EXE';
  68. end;
  69. function tlinkerwasi.MakeExecutable:boolean;
  70. var
  71. GCSectionsStr : ansistring;
  72. binstr, cmdstr : Tcmdstr;
  73. InitStr,
  74. FiniStr,
  75. SoNameStr : string[80];
  76. mapstr,ltostr : TCmdStr;
  77. success : Boolean;
  78. tmp : TCmdStrListItem;
  79. tempFileName : ansistring;
  80. begin
  81. if not(cs_link_nolink in current_settings.globalswitches) then
  82. Message1(exec_i_linking,current_module.exefilename);
  83. { Create some replacements }
  84. mapstr:='';
  85. if (cs_link_map in current_settings.globalswitches) then
  86. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  87. if (cs_link_smart in current_settings.globalswitches) and
  88. create_smartlink_sections then
  89. GCSectionsStr:='--gc-sections'
  90. else
  91. GCSectionsStr:='';
  92. SoNameStr:='';
  93. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  94. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  95. tmp := TCmdStrListItem(ObjectFiles.First);
  96. while Assigned(tmp) do begin
  97. cmdstr := tmp.Str+ ' ' + cmdstr;
  98. tmp := TCmdStrListItem(tmp.Next);
  99. end;
  100. // if HasExports then
  101. // cmdstr := cmdstr + ' --export-dynamic'; //' --export-dynamic';
  102. cmdstr := cmdstr + ' --no-entry';
  103. if (cs_link_strip in current_settings.globalswitches) then
  104. begin
  105. { only remove non global symbols and debugging info for a library }
  106. cmdstr := cmdstr + ' --strip-all';
  107. end;
  108. //Replace(cmdstr,'$OPT',Info.ExtraOptions);
  109. //Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  110. //Replace(cmdstr,'$INIT',InitStr);
  111. //Replace(cmdstr,'$FINI',FiniStr);
  112. Replace(cmdstr,'$SONAME',SoNameStr);
  113. Replace(cmdstr,'$MAP',mapstr);
  114. //Replace(cmdstr,'$LTO',ltostr);
  115. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  116. writeln(utilsprefix+binstr,' ',cmdstr);
  117. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  118. //SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  119. //Replace(cmdstr,'$INPUT',current_module.objfilename );
  120. //Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  121. //DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  122. MakeExecutable:=success;
  123. end;
  124. function tlinkerwasi.MakeSharedLibrary: boolean;
  125. var
  126. GCSectionsStr : ansistring;
  127. binstr, cmdstr : Tcmdstr;
  128. InitStr,
  129. FiniStr,
  130. SoNameStr : string[80];
  131. mapstr,ltostr : TCmdStr;
  132. success : Boolean;
  133. tmp : TCmdStrListItem;
  134. tempFileName : ansistring;
  135. begin
  136. //Result := true;
  137. //Result:=inherited MakeSharedLibrary;
  138. if (cs_link_smart in current_settings.globalswitches) and
  139. create_smartlink_sections then
  140. GCSectionsStr:='--gc-sections'
  141. else
  142. GCSectionsStr:='';
  143. SoNameStr:='';
  144. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  145. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  146. tmp := TCmdStrListItem(ObjectFiles.First);
  147. while Assigned(tmp) do begin
  148. cmdstr := tmp.Str+ ' ' + cmdstr;
  149. tmp := TCmdStrListItem(tmp.Next);
  150. end;
  151. if HasExports then
  152. cmdstr := cmdstr + ' --export-dynamic'; //' --export-dynamic';
  153. cmdstr := cmdstr + ' --no-entry --allow-undefined';
  154. if (cs_link_strip in current_settings.globalswitches) then
  155. begin
  156. { only remove non global symbols and debugging info for a library }
  157. cmdstr := cmdstr + ' --strip-all';
  158. end;
  159. //Replace(cmdstr,'$OPT',Info.ExtraOptions);
  160. //Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  161. //Replace(cmdstr,'$INIT',InitStr);
  162. //Replace(cmdstr,'$FINI',FiniStr);
  163. Replace(cmdstr,'$SONAME',SoNameStr);
  164. //Replace(cmdstr,'$MAP',mapstr);
  165. //Replace(cmdstr,'$LTO',ltostr);
  166. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  167. writeln(utilsprefix+binstr,' ',cmdstr);
  168. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  169. //SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  170. //Replace(cmdstr,'$INPUT',current_module.objfilename );
  171. //Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  172. //DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  173. MakeSharedLibrary:=success;
  174. end;
  175. { texportlibwasi }
  176. procedure texportlibwasi.preparelib(const s: string);
  177. begin
  178. //nothing to happen. wasm files are modules
  179. end;
  180. procedure texportlibwasi.exportprocedure(hp: texported_item);
  181. var
  182. nm : TSymStr;
  183. begin
  184. nm := tprocdef(tprocsym(hp.sym).ProcdefList[0]).mangledname;
  185. current_asmdata.asmlists[al_exports].Concat(tai_impexp.create(hp.name^, nm, ie_Func));
  186. end;
  187. procedure texportlibwasi.exportvar(hp: texported_item);
  188. begin
  189. //inherited exportvar(hp);
  190. end;
  191. procedure texportlibwasi.generatelib;
  192. begin
  193. //inherited generatelib;
  194. end;
  195. initialization
  196. RegisterTarget(system_wasi_info);
  197. RegisterImport(system_wasm32_wasi, timportlibwasi);
  198. RegisterExport(system_wasm32_wasi, texportlibwasi);
  199. RegisterLinker(ld_wasi, tlinkerwasi);
  200. end.