123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- {
- Copyright (c) 2019 by Dmitry Boyarintsev
- This unit implements support import,export,link routines
- for the WASI target
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit t_wasi;
- {$i fpcdefs.inc}
- interface
- uses
- systems,
- globtype, globals,
- aasmbase,
- cfileutl, cutils, cclasses,
- import, export, aasmdata, aasmcpu,
- fmodule, ogbase,
- symsym, symdef,
- link,
- i_wasi, tgcpu;
- type
- { texportlibwasi }
- texportlibwasi=class(texportlib)
- procedure preparelib(const s : string);override;
- procedure exportprocedure(hp : texported_item);override;
- procedure exportvar(hp : texported_item);override;
- procedure generatelib;override;
- end;
- { timportlibwasi }
- timportlibwasi = class(timportlib)
- procedure generatelib;override;
- end;
- { tlinkerwasi }
- tlinkerwasi=class(texternallinker)
- public
- constructor Create;override;
- procedure SetDefaultInfo;override;
- procedure InitSysInitUnitName;override;
- function MakeExecutable:boolean;override;
- function MakeSharedLibrary:boolean;override;
- end;
- implementation
- uses
- SysUtils,
- verbose;
- { timportlibwasi }
- procedure timportlibwasi.generatelib;
- begin
- end;
- { tlinkerwasi }
- constructor tlinkerwasi.Create;
- begin
- inherited Create;
- end;
- procedure tlinkerwasi.SetDefaultInfo;
- begin
- with Info do
- begin
- ExeCmd[1] := 'wasm-ld $SONAME $GCSECTIONS $MAP -o $EXE';
- DllCmd[1] := 'wasm-ld $SONAME $GCSECTIONS $MAP -o $EXE';
- end;
- end;
- procedure tlinkerwasi.InitSysInitUnitName;
- begin
- if current_module.islibrary then
- sysinitunit:='si_dll'
- else
- sysinitunit:='si_prc';
- end;
- function tlinkerwasi.MakeExecutable:boolean;
- var
- GCSectionsStr : ansistring;
- binstr, cmdstr : Tcmdstr;
- InitStr,
- FiniStr,
- SoNameStr : string[80];
- mapstr,ltostr : TCmdStr;
- success : Boolean;
- tmp : TCmdStrListItem;
- tempFileName : ansistring;
- begin
- if not(cs_link_nolink in current_settings.globalswitches) then
- Message1(exec_i_linking,current_module.exefilename);
- { Create some replacements }
- mapstr:='';
- if (cs_link_map in current_settings.globalswitches) then
- mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
- if (cs_link_smart in current_settings.globalswitches) and
- create_smartlink_sections then
- GCSectionsStr:='--gc-sections'
- else
- GCSectionsStr:='';
- SoNameStr:='';
- SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
- Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
- tmp := TCmdStrListItem(ObjectFiles.First);
- while Assigned(tmp) do begin
- cmdstr := tmp.Str+ ' ' + cmdstr;
- tmp := TCmdStrListItem(tmp.Next);
- end;
- // if HasExports then
- // cmdstr := cmdstr + ' --export-dynamic'; //' --export-dynamic';
- cmdstr := cmdstr + ' --no-entry';
- if (cs_link_strip in current_settings.globalswitches) then
- begin
- { only remove non global symbols and debugging info for a library }
- cmdstr := cmdstr + ' --strip-all';
- end;
- //Replace(cmdstr,'$OPT',Info.ExtraOptions);
- //Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
- //Replace(cmdstr,'$INIT',InitStr);
- //Replace(cmdstr,'$FINI',FiniStr);
- Replace(cmdstr,'$SONAME',SoNameStr);
- Replace(cmdstr,'$MAP',mapstr);
- //Replace(cmdstr,'$LTO',ltostr);
- Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
- success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
- MakeExecutable:=success;
- end;
- function tlinkerwasi.MakeSharedLibrary: boolean;
- var
- GCSectionsStr : ansistring;
- binstr, cmdstr : Tcmdstr;
- InitStr,
- FiniStr,
- SoNameStr : string[80];
- mapstr,ltostr : TCmdStr;
- success : Boolean;
- tmp : TCmdStrListItem;
- tempFileName : ansistring;
- begin
- Result:=false;
- if not(cs_link_nolink in current_settings.globalswitches) then
- Message1(exec_i_linking,current_module.sharedlibfilename);
- { Create some replacements }
- mapstr:='';
- if (cs_link_map in current_settings.globalswitches) then
- mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.sharedlibfilename,'.map'));
- if (cs_link_smart in current_settings.globalswitches) and
- create_smartlink_sections then
- GCSectionsStr:='--gc-sections'
- else
- GCSectionsStr:='';
- SoNameStr:='';
- SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
- Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
- tmp := TCmdStrListItem(ObjectFiles.First);
- while Assigned(tmp) do begin
- cmdstr := tmp.Str+ ' ' + cmdstr;
- tmp := TCmdStrListItem(tmp.Next);
- end;
- // if HasExports then
- // cmdstr := cmdstr + ' --export-dynamic'; //' --export-dynamic';
- cmdstr := cmdstr + ' --no-entry';
- if (cs_link_strip in current_settings.globalswitches) then
- begin
- { only remove non global symbols and debugging info for a library }
- cmdstr := cmdstr + ' --strip-all';
- end;
- //Replace(cmdstr,'$OPT',Info.ExtraOptions);
- //Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
- //Replace(cmdstr,'$INIT',InitStr);
- //Replace(cmdstr,'$FINI',FiniStr);
- Replace(cmdstr,'$SONAME',SoNameStr);
- Replace(cmdstr,'$MAP',mapstr);
- //Replace(cmdstr,'$LTO',ltostr);
- Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
- writeln(utilsprefix+binstr,' ',cmdstr);
- success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
- MakeSharedLibrary:=success;
- end;
- { texportlibwasi }
- procedure texportlibwasi.preparelib(const s: string);
- begin
- //nothing to happen. wasm files are modules
- end;
- procedure texportlibwasi.exportprocedure(hp: texported_item);
- var
- nm : TSymStr;
- begin
- nm := tprocdef(tprocsym(hp.sym).ProcdefList[0]).mangledname;
- current_asmdata.asmlists[al_exports].Concat(tai_export_name.create(hp.name^, nm, ie_Func));
- end;
- procedure texportlibwasi.exportvar(hp: texported_item);
- begin
- //inherited exportvar(hp);
- end;
- procedure texportlibwasi.generatelib;
- begin
- //inherited generatelib;
- end;
- initialization
- RegisterTarget(system_wasm32_wasi_info);
- RegisterImport(system_wasm32_wasi, timportlibwasi);
- RegisterExport(system_wasm32_wasi, texportlibwasi);
- RegisterLinker(ld_wasi, tlinkerwasi);
- end.
|