t_zxspectrum.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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.* ) }');
  141. Add(' .data : { *(.data .data.* .rodata .rodata.* .bss .bss.* .fpc.* .stack .stack.* ) }');
  142. Add('}');
  143. end;
  144. { Write and Close response }
  145. linkres.writetodisk;
  146. linkres.free;
  147. result:=true;
  148. end;
  149. procedure TLinkerZXSpectrum.SetDefaultInfo_Sdld;
  150. const
  151. ExeName='sdldz80';
  152. begin
  153. if ImageBaseSetExplicity then
  154. FOrigin:=ImageBase
  155. else
  156. FOrigin:=DefaultOrigin;
  157. with Info do
  158. begin
  159. ExeCmd[1]:=ExeName+' -n $OPT -i $MAP $EXE -f $RES'
  160. end;
  161. end;
  162. procedure TLinkerZXSpectrum.SetDefaultInfo_Vlink;
  163. const
  164. ExeName='vlink';
  165. begin
  166. if ImageBaseSetExplicity then
  167. FOrigin:=ImageBase
  168. else
  169. FOrigin:=DefaultOrigin;
  170. with Info do
  171. begin
  172. ExeCmd[1]:=ExeName+' -bihex $GCSECTIONS -e $STARTSYMBOL $STRIP $OPT -o $EXE -T $RES'
  173. end;
  174. end;
  175. procedure TLinkerZXSpectrum.SetDefaultInfo;
  176. begin
  177. if not (cs_link_vlink in current_settings.globalswitches) then
  178. SetDefaultInfo_Sdld
  179. else
  180. SetDefaultInfo_Vlink;
  181. end;
  182. function TLinkerZXSpectrum.MakeExecutable_Sdld: boolean;
  183. var
  184. binstr,
  185. cmdstr,
  186. mapstr: TCmdStr;
  187. success : boolean;
  188. StaticStr,
  189. //GCSectionsStr,
  190. DynLinkStr,
  191. StripStr,
  192. FixedExeFileName: string;
  193. begin
  194. { for future use }
  195. StaticStr:='';
  196. StripStr:='';
  197. mapstr:='';
  198. DynLinkStr:='';
  199. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.ihx')));
  200. if (cs_link_map in current_settings.globalswitches) then
  201. mapstr:='-mw';
  202. { Write used files and libraries }
  203. WriteResponseFile_Sdld();
  204. { Call linker }
  205. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  206. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  207. Replace(cmdstr,'$EXE',FixedExeFileName);
  208. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  209. Replace(cmdstr,'$STATIC',StaticStr);
  210. Replace(cmdstr,'$STRIP',StripStr);
  211. Replace(cmdstr,'$MAP',mapstr);
  212. //Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  213. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  214. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  215. { Remove ReponseFile }
  216. if success and not(cs_link_nolink in current_settings.globalswitches) then
  217. DeleteFile(outputexedir+Info.ResName);
  218. { Post process }
  219. if success and not(cs_link_nolink in current_settings.globalswitches) then
  220. success:=PostProcessExecutable(FixedExeFileName,false);
  221. result:=success; { otherwise a recursive call to link method }
  222. end;
  223. function TLinkerZXSpectrum.MakeExecutable_Vlink: boolean;
  224. var
  225. binstr,
  226. cmdstr: TCmdStr;
  227. success: boolean;
  228. GCSectionsStr,
  229. StripStr,
  230. StartSymbolStr,
  231. FixedExeFilename: string;
  232. begin
  233. GCSectionsStr:='-gc-all -mtype';
  234. StripStr:='';
  235. StartSymbolStr:='start';
  236. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.ihx')));
  237. { Write used files and libraries }
  238. WriteResponseFile_Vlink();
  239. { Call linker }
  240. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  241. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  242. Replace(cmdstr,'$EXE',FixedExeFileName);
  243. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  244. Replace(cmdstr,'$STRIP',StripStr);
  245. Replace(cmdstr,'$STARTSYMBOL',StartSymbolStr);
  246. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  247. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  248. { Remove ReponseFile }
  249. if success and not(cs_link_nolink in current_settings.globalswitches) then
  250. DeleteFile(outputexedir+Info.ResName);
  251. { Post process }
  252. if success and not(cs_link_nolink in current_settings.globalswitches) then
  253. success:=PostProcessExecutable(FixedExeFileName,false);
  254. result:=success;
  255. end;
  256. function TLinkerZXSpectrum.MakeExecutable: boolean;
  257. begin
  258. if not (cs_link_vlink in current_settings.globalswitches) then
  259. result:=MakeExecutable_Sdld
  260. else
  261. result:=MakeExecutable_Vlink;
  262. end;
  263. procedure TLinkerZXSpectrum.InitSysInitUnitName;
  264. begin
  265. sysinitunit:='si_prc';
  266. end;
  267. function TLinkerZXSpectrum.postprocessexecutable(const fn: string; isdll: boolean): boolean;
  268. begin
  269. result:=DoExec(FindUtil(utilsprefix+'ihxutil'),' '+fn,true,false);
  270. end;
  271. {*****************************************************************************
  272. TInternalLinkerZXSpectrum
  273. *****************************************************************************}
  274. procedure TInternalLinkerZXSpectrum.DefaultLinkScript;
  275. var
  276. s : TCmdStr;
  277. prtobj: string[80];
  278. begin
  279. prtobj:='prt0';
  280. if not (target_info.system in systems_internal_sysinit) and (prtobj <> '') then
  281. LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile(prtobj,'',false)));
  282. while not ObjectFiles.Empty do
  283. begin
  284. s:=ObjectFiles.GetFirst;
  285. if s<>'' then
  286. begin
  287. if not(cs_link_on_target in current_settings.globalswitches) then
  288. s:=FindObjectFile(s,'',false);
  289. LinkScript.Concat('READOBJECT ' + maybequoted(s));
  290. end;
  291. end;
  292. LinkScript.Concat('GROUP');
  293. { Write staticlibraries }
  294. if not StaticLibFiles.Empty then
  295. begin
  296. while not StaticLibFiles.Empty do
  297. begin
  298. S:=StaticLibFiles.GetFirst;
  299. if s<>'' then
  300. LinkScript.Concat('READSTATICLIBRARY '+MaybeQuoted(s));
  301. end;
  302. end;
  303. LinkScript.Concat('ENDGROUP');
  304. LinkScript.Concat('IMAGEBASE '+tostr(FOrigin));
  305. LinkScript.Concat('EXESECTION .text');
  306. LinkScript.Concat(' OBJSECTION _CODE');
  307. LinkScript.Concat('ENDEXESECTION');
  308. LinkScript.Concat('EXESECTION .data');
  309. LinkScript.Concat(' OBJSECTION _DATA');
  310. LinkScript.Concat('ENDEXESECTION');
  311. LinkScript.Concat('EXESECTION .bss');
  312. LinkScript.Concat(' OBJSECTION _BSS');
  313. LinkScript.Concat(' OBJSECTION _BSSEND');
  314. LinkScript.Concat(' OBJSECTION _HEAP');
  315. LinkScript.Concat(' OBJSECTION _STACK');
  316. LinkScript.Concat('ENDEXESECTION');
  317. LinkScript.Concat('ENTRYNAME start');
  318. end;
  319. constructor TInternalLinkerZXSpectrum.create;
  320. begin
  321. inherited create;
  322. CArObjectReader:=TArObjectReader;
  323. CExeOutput:=TIntelHexExeOutput;
  324. CObjInput:=TRelObjInput;
  325. if ImageBaseSetExplicity then
  326. FOrigin:=ImageBase
  327. else
  328. FOrigin:=DefaultOrigin;
  329. end;
  330. procedure TInternalLinkerZXSpectrum.InitSysInitUnitName;
  331. begin
  332. sysinitunit:='si_prc';
  333. end;
  334. function TInternalLinkerZXSpectrum.MakeExecutable: boolean;
  335. begin
  336. result:=inherited;
  337. { Post process }
  338. if result and not(cs_link_nolink in current_settings.globalswitches) then
  339. result:=PostProcessExecutable(current_module.exefilename);
  340. end;
  341. function TInternalLinkerZXSpectrum.postprocessexecutable(const fn: string): boolean;
  342. var
  343. exitcode: longint;
  344. FoundBin: ansistring;
  345. Found: Boolean;
  346. utilexe: TCmdStr;
  347. begin
  348. result:=false;
  349. utilexe:=utilsprefix+'ihxutil';
  350. FoundBin:='';
  351. Found:=false;
  352. if utilsdirectory<>'' then
  353. Found:=FindFile(utilexe,utilsdirectory,false,Foundbin);
  354. if (not Found) then
  355. Found:=FindExe(utilexe,false,Foundbin);
  356. if Found then
  357. begin
  358. exitcode:=RequotedExecuteProcess(foundbin,' '+fn);
  359. result:=exitcode<>0;
  360. end;
  361. end;
  362. {*****************************************************************************
  363. Initialize
  364. *****************************************************************************}
  365. initialization
  366. {$ifdef z80}
  367. RegisterLinker(ld_int_zxspectrum,TInternalLinkerZXSpectrum);
  368. RegisterLinker(ld_zxspectrum,TLinkerZXSpectrum);
  369. RegisterTarget(system_z80_zxspectrum_info);
  370. {$endif z80}
  371. end.