t_wasi.pas 6.8 KB

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