t_zxspectrum.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 $MAP -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. MapStr,
  233. FixedExeFilename: string;
  234. begin
  235. GCSectionsStr:='-gc-all -mtype';
  236. StripStr:='';
  237. MapStr:='';
  238. StartSymbolStr:='start';
  239. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.ihx')));
  240. if (cs_link_map in current_settings.globalswitches) then
  241. MapStr:='-M'+maybequoted(ScriptFixFileName(current_module.mapfilename));
  242. { Write used files and libraries }
  243. WriteResponseFile_Vlink();
  244. { Call linker }
  245. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  246. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  247. Replace(cmdstr,'$EXE',FixedExeFileName);
  248. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  249. Replace(cmdstr,'$MAP',MapStr);
  250. Replace(cmdstr,'$STRIP',StripStr);
  251. Replace(cmdstr,'$STARTSYMBOL',StartSymbolStr);
  252. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  253. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  254. { Remove ReponseFile }
  255. if success and not(cs_link_nolink in current_settings.globalswitches) then
  256. DeleteFile(outputexedir+Info.ResName);
  257. { Post process }
  258. if success and not(cs_link_nolink in current_settings.globalswitches) then
  259. success:=PostProcessExecutable(FixedExeFileName,false);
  260. result:=success;
  261. end;
  262. function TLinkerZXSpectrum.MakeExecutable: boolean;
  263. begin
  264. if not (cs_link_vlink in current_settings.globalswitches) then
  265. result:=MakeExecutable_Sdld
  266. else
  267. result:=MakeExecutable_Vlink;
  268. end;
  269. procedure TLinkerZXSpectrum.InitSysInitUnitName;
  270. begin
  271. sysinitunit:='si_prc';
  272. end;
  273. function TLinkerZXSpectrum.postprocessexecutable(const fn: string; isdll: boolean): boolean;
  274. begin
  275. result:=DoExec(FindUtil(utilsprefix+'ihxutil'),' '+fn,true,false);
  276. end;
  277. {*****************************************************************************
  278. TInternalLinkerZXSpectrum
  279. *****************************************************************************}
  280. procedure TInternalLinkerZXSpectrum.DefaultLinkScript;
  281. var
  282. s : TCmdStr;
  283. prtobj: string[80];
  284. begin
  285. prtobj:='prt0';
  286. if not (target_info.system in systems_internal_sysinit) and (prtobj <> '') then
  287. LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile(prtobj,'',false)));
  288. while not ObjectFiles.Empty do
  289. begin
  290. s:=ObjectFiles.GetFirst;
  291. if s<>'' then
  292. begin
  293. if not(cs_link_on_target in current_settings.globalswitches) then
  294. s:=FindObjectFile(s,'',false);
  295. LinkScript.Concat('READOBJECT ' + maybequoted(s));
  296. end;
  297. end;
  298. LinkScript.Concat('GROUP');
  299. { Write staticlibraries }
  300. if not StaticLibFiles.Empty then
  301. begin
  302. while not StaticLibFiles.Empty do
  303. begin
  304. S:=StaticLibFiles.GetFirst;
  305. if s<>'' then
  306. LinkScript.Concat('READSTATICLIBRARY '+MaybeQuoted(s));
  307. end;
  308. end;
  309. LinkScript.Concat('ENDGROUP');
  310. LinkScript.Concat('IMAGEBASE '+tostr(FOrigin));
  311. LinkScript.Concat('EXESECTION .text');
  312. LinkScript.Concat(' OBJSECTION _CODE');
  313. LinkScript.Concat('ENDEXESECTION');
  314. LinkScript.Concat('EXESECTION .data');
  315. LinkScript.Concat(' OBJSECTION _DATA');
  316. LinkScript.Concat('ENDEXESECTION');
  317. LinkScript.Concat('EXESECTION .bss');
  318. LinkScript.Concat(' OBJSECTION _BSS');
  319. LinkScript.Concat(' OBJSECTION _BSSEND');
  320. LinkScript.Concat(' OBJSECTION _HEAP');
  321. LinkScript.Concat(' OBJSECTION _STACK');
  322. LinkScript.Concat('ENDEXESECTION');
  323. LinkScript.Concat('ENTRYNAME start');
  324. end;
  325. constructor TInternalLinkerZXSpectrum.create;
  326. begin
  327. inherited create;
  328. CArObjectReader:=TArObjectReader;
  329. CExeOutput:=TZXSpectrumIntelHexExeOutput;
  330. CObjInput:=TRelObjInput;
  331. if ImageBaseSetExplicity then
  332. FOrigin:=ImageBase
  333. else
  334. FOrigin:=DefaultOrigin;
  335. end;
  336. procedure TInternalLinkerZXSpectrum.InitSysInitUnitName;
  337. begin
  338. sysinitunit:='si_prc';
  339. end;
  340. function TInternalLinkerZXSpectrum.MakeExecutable: boolean;
  341. begin
  342. result:=inherited;
  343. { Post process }
  344. if result and not(cs_link_nolink in current_settings.globalswitches) then
  345. result:=PostProcessExecutable(current_module.exefilename);
  346. end;
  347. function TInternalLinkerZXSpectrum.postprocessexecutable(const fn: string): boolean;
  348. var
  349. exitcode: longint;
  350. FoundBin: ansistring;
  351. Found: Boolean;
  352. utilexe: TCmdStr;
  353. begin
  354. result:=false;
  355. utilexe:=utilsprefix+'ihxutil';
  356. FoundBin:='';
  357. Found:=false;
  358. if utilsdirectory<>'' then
  359. Found:=FindFile(utilexe,utilsdirectory,false,Foundbin);
  360. if (not Found) then
  361. Found:=FindExe(utilexe,false,Foundbin);
  362. if Found then
  363. begin
  364. exitcode:=RequotedExecuteProcess(foundbin,' '+fn);
  365. result:=exitcode<>0;
  366. end;
  367. end;
  368. {*****************************************************************************
  369. Initialize
  370. *****************************************************************************}
  371. initialization
  372. {$ifdef z80}
  373. RegisterLinker(ld_int_zxspectrum,TInternalLinkerZXSpectrum);
  374. RegisterLinker(ld_zxspectrum,TLinkerZXSpectrum);
  375. RegisterTarget(system_z80_zxspectrum_info);
  376. {$endif z80}
  377. end.