t_zxspectrum.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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,ogbase,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. function GetCodeSize(aExeOutput: TExeOutput): QWord;override;
  50. function GetDataSize(aExeOutput: TExeOutput): QWord;override;
  51. function GetBssSize(aExeOutput: TExeOutput): QWord;override;
  52. public
  53. constructor create;override;
  54. procedure InitSysInitUnitName;override;
  55. end;
  56. {*****************************************************************************
  57. TLinkerZXSpectrum
  58. *****************************************************************************}
  59. function TLinkerZXSpectrum.WriteResponseFile_Sdld: Boolean;
  60. Var
  61. linkres : TLinkRes;
  62. s : TCmdStr;
  63. prtobj: string[80];
  64. begin
  65. result:=False;
  66. prtobj:='prt0';
  67. { Open link.res file }
  68. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  69. { Write the origin (i.e. the program load address) }
  70. LinkRes.Add('-b _CODE='+tostr(FOrigin));
  71. if not (target_info.system in systems_internal_sysinit) and (prtobj <> '') then
  72. begin
  73. s:=FindObjectFile(prtobj,'',false);
  74. LinkRes.AddFileName(s);
  75. end;
  76. while not ObjectFiles.Empty do
  77. begin
  78. s:=ObjectFiles.GetFirst;
  79. if s<>'' then
  80. begin
  81. if not(cs_link_on_target in current_settings.globalswitches) then
  82. s:=FindObjectFile(s,'',false);
  83. LinkRes.AddFileName((maybequoted(s)));
  84. end;
  85. end;
  86. { Write staticlibraries }
  87. if not StaticLibFiles.Empty then
  88. begin
  89. while not StaticLibFiles.Empty do
  90. begin
  91. S:=StaticLibFiles.GetFirst;
  92. LinkRes.Add('-l'+maybequoted(s));
  93. end;
  94. end;
  95. { Write and Close response }
  96. linkres.writetodisk;
  97. linkres.free;
  98. result:=True;
  99. end;
  100. function TLinkerZXSpectrum.WriteResponseFile_Vlink: Boolean;
  101. Var
  102. linkres : TLinkRes;
  103. s : TCmdStr;
  104. prtobj: string[80];
  105. begin
  106. result:=false;
  107. prtobj:='prt0';
  108. { Open link.res file }
  109. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  110. LinkRes.Add('INPUT (');
  111. if not (target_info.system in systems_internal_sysinit) and (prtobj <> '') then
  112. begin
  113. s:=FindObjectFile(prtobj,'',false);
  114. LinkRes.AddFileName(maybequoted(s));
  115. end;
  116. while not ObjectFiles.Empty do
  117. begin
  118. s:=ObjectFiles.GetFirst;
  119. if s<>'' then
  120. begin
  121. s:=FindObjectFile(s,'',false);
  122. LinkRes.AddFileName(maybequoted(s));
  123. end;
  124. end;
  125. while not StaticLibFiles.Empty do
  126. begin
  127. S:=StaticLibFiles.GetFirst;
  128. LinkRes.AddFileName(maybequoted(s));
  129. end;
  130. LinkRes.Add(')');
  131. with LinkRes do
  132. begin
  133. Add('');
  134. Add('SECTIONS');
  135. Add('{');
  136. Add(' . = 0x'+hexstr(FOrigin,4)+';');
  137. Add(' .text : { *(.text .text.* ) }');
  138. Add(' .data : { *(.data .data.* .rodata .rodata.* .bss .bss.* .fpc.* .stack .stack.* ) }');
  139. Add('}');
  140. end;
  141. { Write and Close response }
  142. linkres.writetodisk;
  143. linkres.free;
  144. result:=true;
  145. end;
  146. procedure TLinkerZXSpectrum.SetDefaultInfo_Sdld;
  147. const
  148. ExeName='sdldz80';
  149. begin
  150. FOrigin:={32768}23800;
  151. with Info do
  152. begin
  153. ExeCmd[1]:=ExeName+' -n $OPT -i $MAP $EXE -f $RES'
  154. end;
  155. end;
  156. procedure TLinkerZXSpectrum.SetDefaultInfo_Vlink;
  157. const
  158. ExeName='vlink';
  159. begin
  160. FOrigin:={32768}23800;
  161. with Info do
  162. begin
  163. ExeCmd[1]:=ExeName+' -bihex $GCSECTIONS -e $STARTSYMBOL $STRIP $OPT -o $EXE -T $RES'
  164. end;
  165. end;
  166. procedure TLinkerZXSpectrum.SetDefaultInfo;
  167. begin
  168. if not (cs_link_vlink in current_settings.globalswitches) then
  169. SetDefaultInfo_Sdld
  170. else
  171. SetDefaultInfo_Vlink;
  172. end;
  173. function TLinkerZXSpectrum.MakeExecutable_Sdld: boolean;
  174. var
  175. binstr,
  176. cmdstr,
  177. mapstr: TCmdStr;
  178. success : boolean;
  179. StaticStr,
  180. //GCSectionsStr,
  181. DynLinkStr,
  182. StripStr,
  183. FixedExeFileName: string;
  184. begin
  185. { for future use }
  186. StaticStr:='';
  187. StripStr:='';
  188. mapstr:='';
  189. DynLinkStr:='';
  190. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.ihx')));
  191. if (cs_link_map in current_settings.globalswitches) then
  192. mapstr:='-mw';
  193. { Write used files and libraries }
  194. WriteResponseFile_Sdld();
  195. { Call linker }
  196. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  197. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  198. Replace(cmdstr,'$EXE',FixedExeFileName);
  199. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  200. Replace(cmdstr,'$STATIC',StaticStr);
  201. Replace(cmdstr,'$STRIP',StripStr);
  202. Replace(cmdstr,'$MAP',mapstr);
  203. //Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  204. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  205. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  206. { Remove ReponseFile }
  207. if success and not(cs_link_nolink in current_settings.globalswitches) then
  208. DeleteFile(outputexedir+Info.ResName);
  209. { Post process }
  210. if success and not(cs_link_nolink in current_settings.globalswitches) then
  211. success:=PostProcessExecutable(FixedExeFileName,false);
  212. result:=success; { otherwise a recursive call to link method }
  213. end;
  214. function TLinkerZXSpectrum.MakeExecutable_Vlink: boolean;
  215. var
  216. binstr,
  217. cmdstr: TCmdStr;
  218. success: boolean;
  219. GCSectionsStr,
  220. StripStr,
  221. StartSymbolStr,
  222. FixedExeFilename: string;
  223. begin
  224. GCSectionsStr:='-gc-all -mtype';
  225. StripStr:='';
  226. StartSymbolStr:='start';
  227. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.ihx')));
  228. { Write used files and libraries }
  229. WriteResponseFile_Vlink();
  230. { Call linker }
  231. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  232. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  233. Replace(cmdstr,'$EXE',FixedExeFileName);
  234. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  235. Replace(cmdstr,'$STRIP',StripStr);
  236. Replace(cmdstr,'$STARTSYMBOL',StartSymbolStr);
  237. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  238. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  239. { Remove ReponseFile }
  240. if success and not(cs_link_nolink in current_settings.globalswitches) then
  241. DeleteFile(outputexedir+Info.ResName);
  242. { Post process }
  243. if success and not(cs_link_nolink in current_settings.globalswitches) then
  244. success:=PostProcessExecutable(FixedExeFileName,false);
  245. result:=success;
  246. end;
  247. function TLinkerZXSpectrum.MakeExecutable: boolean;
  248. begin
  249. if not (cs_link_vlink in current_settings.globalswitches) then
  250. result:=MakeExecutable_Sdld
  251. else
  252. result:=MakeExecutable_Vlink;
  253. end;
  254. procedure TLinkerZXSpectrum.InitSysInitUnitName;
  255. begin
  256. sysinitunit:='si_prc';
  257. end;
  258. function TLinkerZXSpectrum.postprocessexecutable(const fn: string; isdll: boolean): boolean;
  259. begin
  260. result:=DoExec(FindUtil(utilsprefix+'ihx2tzx'),' '+fn,true,false);
  261. end;
  262. {*****************************************************************************
  263. TInternalLinkerZXSpectrum
  264. *****************************************************************************}
  265. procedure TInternalLinkerZXSpectrum.DefaultLinkScript;
  266. var
  267. s : TCmdStr;
  268. prtobj: string[80];
  269. begin
  270. prtobj:='prt0';
  271. if not (target_info.system in systems_internal_sysinit) and (prtobj <> '') then
  272. LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile(prtobj,'',false)));
  273. while not ObjectFiles.Empty do
  274. begin
  275. s:=ObjectFiles.GetFirst;
  276. if s<>'' then
  277. begin
  278. if not(cs_link_on_target in current_settings.globalswitches) then
  279. s:=FindObjectFile(s,'',false);
  280. LinkScript.Concat('READOBJECT ' + maybequoted(s));
  281. end;
  282. end;
  283. LinkScript.Concat('GROUP');
  284. { Write staticlibraries }
  285. if not StaticLibFiles.Empty then
  286. begin
  287. while not StaticLibFiles.Empty do
  288. begin
  289. S:=StaticLibFiles.GetFirst;
  290. if s<>'' then
  291. LinkScript.Concat('READSTATICLIBRARY '+MaybeQuoted(s));
  292. end;
  293. end;
  294. LinkScript.Concat('ENDGROUP');
  295. LinkScript.Concat('EXESECTION .text');
  296. LinkScript.Concat(' OBJSECTION _CODE');
  297. LinkScript.Concat('ENDEXESECTION');
  298. LinkScript.Concat('EXESECTION .data');
  299. LinkScript.Concat(' OBJSECTION _DATA');
  300. LinkScript.Concat('ENDEXESECTION');
  301. LinkScript.Concat('EXESECTION .bss');
  302. LinkScript.Concat(' OBJSECTION _BSS');
  303. LinkScript.Concat(' OBJSECTION _HEAP');
  304. LinkScript.Concat(' OBJSECTION _STACK');
  305. LinkScript.Concat('ENDEXESECTION');
  306. LinkScript.Concat('ENTRYNAME start');
  307. end;
  308. function TInternalLinkerZXSpectrum.GetCodeSize(aExeOutput: TExeOutput): QWord;
  309. begin
  310. Result:=0;
  311. end;
  312. function TInternalLinkerZXSpectrum.GetDataSize(aExeOutput: TExeOutput): QWord;
  313. begin
  314. Result:=0;
  315. end;
  316. function TInternalLinkerZXSpectrum.GetBssSize(aExeOutput: TExeOutput): QWord;
  317. begin
  318. Result:=0;
  319. end;
  320. constructor TInternalLinkerZXSpectrum.create;
  321. begin
  322. inherited create;
  323. CArObjectReader:=TArObjectReader;
  324. CExeOutput:=TIntelHexExeOutput;
  325. CObjInput:=TRelObjInput;
  326. end;
  327. procedure TInternalLinkerZXSpectrum.InitSysInitUnitName;
  328. begin
  329. sysinitunit:='si_prc';
  330. end;
  331. {*****************************************************************************
  332. Initialize
  333. *****************************************************************************}
  334. initialization
  335. {$ifdef z80}
  336. RegisterLinker(ld_int_zxspectrum,TInternalLinkerZXSpectrum);
  337. RegisterLinker(ld_zxspectrum,TLinkerZXSpectrum);
  338. RegisterTarget(system_z80_zxspectrum_info);
  339. {$endif z80}
  340. end.