t_zxspectrum.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. {
  2. Copyright (c) 2005-2020 by Free Pascal Compiler team
  3. This unit implements support import, export, link routines
  4. for the ZX Spectrum 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_zxspectrum;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. SysUtils,
  24. cutils,cfileutl,cclasses,
  25. globtype,globals,systems,verbose,comphook,cscript,fmodule,i_zxspectrum,link,
  26. cpuinfo,ogrel,owar;
  27. type
  28. { sdld - the sdld linker from the SDCC project ( http://sdcc.sourceforge.net/ ) }
  29. { vlink - the vlink linker by Frank Wille (http://sun.hasenbraten.de/vlink/ ) }
  30. TLinkerZXSpectrum=class(texternallinker)
  31. private
  32. FOrigin: Word;
  33. Function WriteResponseFile_Sdld: Boolean;
  34. Function WriteResponseFile_Vlink: Boolean;
  35. procedure SetDefaultInfo_Sdld;
  36. procedure SetDefaultInfo_Vlink;
  37. function MakeExecutable_Sdld: boolean;
  38. function MakeExecutable_Vlink: boolean;
  39. public
  40. procedure SetDefaultInfo; override;
  41. function MakeExecutable: boolean; override;
  42. procedure InitSysInitUnitName; override;
  43. function postprocessexecutable(const fn : string;isdll:boolean): boolean;
  44. end;
  45. { TInternalLinkerZXSpectrum }
  46. TInternalLinkerZXSpectrum=class(tinternallinker)
  47. protected
  48. procedure DefaultLinkScript;override;
  49. public
  50. constructor create;override;
  51. end;
  52. {*****************************************************************************
  53. TLinkerZXSpectrum
  54. *****************************************************************************}
  55. function TLinkerZXSpectrum.WriteResponseFile_Sdld: Boolean;
  56. Var
  57. linkres : TLinkRes;
  58. s : TCmdStr;
  59. prtobj: string[80];
  60. begin
  61. result:=False;
  62. prtobj:='prt0';
  63. { Open link.res file }
  64. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  65. { Write the origin (i.e. the program load address) }
  66. LinkRes.Add('-b _CODE='+tostr(FOrigin));
  67. if not (target_info.system in systems_internal_sysinit) and (prtobj <> '') then
  68. begin
  69. s:=FindObjectFile(prtobj,'',false);
  70. LinkRes.AddFileName(s);
  71. end;
  72. while not ObjectFiles.Empty do
  73. begin
  74. s:=ObjectFiles.GetFirst;
  75. if s<>'' then
  76. begin
  77. if not(cs_link_on_target in current_settings.globalswitches) then
  78. s:=FindObjectFile(s,'',false);
  79. LinkRes.AddFileName((maybequoted(s)));
  80. end;
  81. end;
  82. { Write staticlibraries }
  83. if not StaticLibFiles.Empty then
  84. begin
  85. while not StaticLibFiles.Empty do
  86. begin
  87. S:=StaticLibFiles.GetFirst;
  88. LinkRes.Add('-l'+maybequoted(s));
  89. end;
  90. end;
  91. { Write and Close response }
  92. linkres.writetodisk;
  93. linkres.free;
  94. result:=True;
  95. end;
  96. function TLinkerZXSpectrum.WriteResponseFile_Vlink: Boolean;
  97. Var
  98. linkres : TLinkRes;
  99. s : TCmdStr;
  100. prtobj: string[80];
  101. begin
  102. result:=false;
  103. prtobj:='prt0';
  104. { Open link.res file }
  105. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  106. LinkRes.Add('INPUT (');
  107. if not (target_info.system in systems_internal_sysinit) and (prtobj <> '') then
  108. begin
  109. s:=FindObjectFile(prtobj,'',false);
  110. LinkRes.AddFileName(maybequoted(s));
  111. end;
  112. while not ObjectFiles.Empty do
  113. begin
  114. s:=ObjectFiles.GetFirst;
  115. if s<>'' then
  116. begin
  117. s:=FindObjectFile(s,'',false);
  118. LinkRes.AddFileName(maybequoted(s));
  119. end;
  120. end;
  121. while not StaticLibFiles.Empty do
  122. begin
  123. S:=StaticLibFiles.GetFirst;
  124. LinkRes.AddFileName(maybequoted(s));
  125. end;
  126. LinkRes.Add(')');
  127. with LinkRes do
  128. begin
  129. Add('');
  130. Add('SECTIONS');
  131. Add('{');
  132. Add(' . = 0x'+hexstr(FOrigin,4)+';');
  133. Add(' .text : { *(.text .text.* ) }');
  134. Add(' .data : { *(.data .data.* .rodata .rodata.* .bss .bss.* .fpc.* .stack .stack.* ) }');
  135. Add('}');
  136. end;
  137. { Write and Close response }
  138. linkres.writetodisk;
  139. linkres.free;
  140. result:=true;
  141. end;
  142. procedure TLinkerZXSpectrum.SetDefaultInfo_Sdld;
  143. const
  144. ExeName='sdldz80';
  145. begin
  146. FOrigin:={32768}23800;
  147. with Info do
  148. begin
  149. ExeCmd[1]:=ExeName+' -n $OPT -i $MAP $EXE -f $RES'
  150. end;
  151. end;
  152. procedure TLinkerZXSpectrum.SetDefaultInfo_Vlink;
  153. const
  154. ExeName='vlink';
  155. begin
  156. FOrigin:={32768}23800;
  157. with Info do
  158. begin
  159. ExeCmd[1]:=ExeName+' -bihex $GCSECTIONS -e $STARTSYMBOL $STRIP $OPT -o $EXE -T $RES'
  160. end;
  161. end;
  162. procedure TLinkerZXSpectrum.SetDefaultInfo;
  163. begin
  164. if not (cs_link_vlink in current_settings.globalswitches) then
  165. SetDefaultInfo_Sdld
  166. else
  167. SetDefaultInfo_Vlink;
  168. end;
  169. function TLinkerZXSpectrum.MakeExecutable_Sdld: boolean;
  170. var
  171. binstr,
  172. cmdstr,
  173. mapstr: TCmdStr;
  174. success : boolean;
  175. StaticStr,
  176. //GCSectionsStr,
  177. DynLinkStr,
  178. StripStr,
  179. FixedExeFileName: string;
  180. begin
  181. { for future use }
  182. StaticStr:='';
  183. StripStr:='';
  184. mapstr:='';
  185. DynLinkStr:='';
  186. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.ihx')));
  187. if (cs_link_map in current_settings.globalswitches) then
  188. mapstr:='-mw';
  189. { Write used files and libraries }
  190. WriteResponseFile_Sdld();
  191. { Call linker }
  192. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  193. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  194. Replace(cmdstr,'$EXE',FixedExeFileName);
  195. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  196. Replace(cmdstr,'$STATIC',StaticStr);
  197. Replace(cmdstr,'$STRIP',StripStr);
  198. Replace(cmdstr,'$MAP',mapstr);
  199. //Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  200. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  201. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  202. { Remove ReponseFile }
  203. if success and not(cs_link_nolink in current_settings.globalswitches) then
  204. DeleteFile(outputexedir+Info.ResName);
  205. { Post process }
  206. if success and not(cs_link_nolink in current_settings.globalswitches) then
  207. success:=PostProcessExecutable(FixedExeFileName,false);
  208. result:=success; { otherwise a recursive call to link method }
  209. end;
  210. function TLinkerZXSpectrum.MakeExecutable_Vlink: boolean;
  211. var
  212. binstr,
  213. cmdstr: TCmdStr;
  214. success: boolean;
  215. GCSectionsStr,
  216. StripStr,
  217. StartSymbolStr,
  218. FixedExeFilename: string;
  219. begin
  220. GCSectionsStr:='-gc-all -mtype';
  221. StripStr:='';
  222. StartSymbolStr:='start';
  223. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.ihx')));
  224. { Write used files and libraries }
  225. WriteResponseFile_Vlink();
  226. { Call linker }
  227. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  228. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  229. Replace(cmdstr,'$EXE',FixedExeFileName);
  230. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  231. Replace(cmdstr,'$STRIP',StripStr);
  232. Replace(cmdstr,'$STARTSYMBOL',StartSymbolStr);
  233. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  234. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  235. { Remove ReponseFile }
  236. if success and not(cs_link_nolink in current_settings.globalswitches) then
  237. DeleteFile(outputexedir+Info.ResName);
  238. { Post process }
  239. if success and not(cs_link_nolink in current_settings.globalswitches) then
  240. success:=PostProcessExecutable(FixedExeFileName,false);
  241. result:=success;
  242. end;
  243. function TLinkerZXSpectrum.MakeExecutable: boolean;
  244. begin
  245. if not (cs_link_vlink in current_settings.globalswitches) then
  246. result:=MakeExecutable_Sdld
  247. else
  248. result:=MakeExecutable_Vlink;
  249. end;
  250. procedure TLinkerZXSpectrum.InitSysInitUnitName;
  251. begin
  252. sysinitunit:='si_prc';
  253. end;
  254. function TLinkerZXSpectrum.postprocessexecutable(const fn: string; isdll: boolean): boolean;
  255. begin
  256. result:=DoExec(FindUtil(utilsprefix+'ihx2tzx'),' '+fn,true,false);
  257. end;
  258. {*****************************************************************************
  259. TInternalLinkerZXSpectrum
  260. *****************************************************************************}
  261. procedure TInternalLinkerZXSpectrum.DefaultLinkScript;
  262. begin
  263. end;
  264. constructor TInternalLinkerZXSpectrum.create;
  265. begin
  266. inherited create;
  267. CArObjectReader:=TArObjectReader;
  268. CExeOutput:=TIntelHexExeOutput;
  269. CObjInput:=TRelObjInput;
  270. end;
  271. {*****************************************************************************
  272. Initialize
  273. *****************************************************************************}
  274. initialization
  275. {$ifdef z80}
  276. RegisterLinker(ld_int_zxspectrum,TInternalLinkerZXSpectrum);
  277. RegisterLinker(ld_zxspectrum,TLinkerZXSpectrum);
  278. RegisterTarget(system_z80_zxspectrum_info);
  279. {$endif z80}
  280. end.