t_zxspectrum.pas 13 KB

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