t_zxspectrum.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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;
  27. type
  28. { TLinkerZXSpectrum_SdccSdld - the sdld linker from the SDCC project ( http://sdcc.sourceforge.net/ ) }
  29. TLinkerZXSpectrum_SdccSdld=class(texternallinker)
  30. private
  31. FOrigin: Word;
  32. Function WriteResponseFile: Boolean;
  33. public
  34. { constructor Create; override;}
  35. procedure SetDefaultInfo; override;
  36. function MakeExecutable:boolean; override;
  37. function postprocessexecutable(const fn : string;isdll:boolean):boolean;
  38. end;
  39. {*****************************************************************************
  40. TLinkerZXSpectrum_SdccSdld
  41. *****************************************************************************}
  42. function TLinkerZXSpectrum_SdccSdld.WriteResponseFile: Boolean;
  43. Var
  44. linkres : TLinkRes;
  45. i : longint;
  46. HPath : TCmdStrListItem;
  47. s,s1,s2 : TCmdStr;
  48. prtobj,
  49. cprtobj : string[80];
  50. linklibc : boolean;
  51. found1,
  52. found2 : boolean;
  53. {$if defined(ARM)}
  54. LinkStr : string;
  55. {$endif}
  56. begin
  57. WriteResponseFile:=False;
  58. linklibc:=(SharedLibFiles.Find('c')<>nil);
  59. prtobj:='prt0';
  60. cprtobj:='cprt0';
  61. if linklibc then
  62. prtobj:=cprtobj;
  63. { Open link.res file }
  64. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  65. { Write path to search libraries }
  66. (* HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  67. while assigned(HPath) do
  68. begin
  69. s:=HPath.Str;
  70. if (cs_link_on_target in current_settings.globalswitches) then
  71. s:=ScriptFixFileName(s);
  72. LinkRes.Add('-L'+s);
  73. HPath:=TCmdStrListItem(HPath.Next);
  74. end;
  75. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  76. while assigned(HPath) do
  77. begin
  78. s:=HPath.Str;
  79. if s<>'' then
  80. LinkRes.Add('SEARCH_DIR("'+s+'")');
  81. HPath:=TCmdStrListItem(HPath.Next);
  82. end;
  83. LinkRes.Add('INPUT (');
  84. { add objectfiles, start with prt0 always }*)
  85. //s:=FindObjectFile('prt0','',false);
  86. if prtobj<>'' then
  87. begin
  88. s:=FindObjectFile(prtobj,'',false);
  89. LinkRes.AddFileName(s);
  90. end;
  91. { try to add crti and crtbegin if linking to C }
  92. if linklibc then
  93. begin
  94. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  95. LinkRes.AddFileName(s);
  96. if librarysearchpath.FindFile('crti.o',false,s) then
  97. LinkRes.AddFileName(s);
  98. end;
  99. while not ObjectFiles.Empty do
  100. begin
  101. s:=ObjectFiles.GetFirst;
  102. if s<>'' then
  103. begin
  104. { vlink doesn't use SEARCH_DIR for object files }
  105. if not(cs_link_on_target in current_settings.globalswitches) then
  106. s:=FindObjectFile(s,'',false);
  107. LinkRes.AddFileName((maybequoted(s)));
  108. end;
  109. end;
  110. { Write staticlibraries }
  111. if not StaticLibFiles.Empty then
  112. begin
  113. { vlink doesn't need, and doesn't support GROUP }
  114. { if (cs_link_on_target in current_settings.globalswitches) then
  115. begin
  116. LinkRes.Add(')');
  117. LinkRes.Add('GROUP(');
  118. end;}
  119. while not StaticLibFiles.Empty do
  120. begin
  121. S:=StaticLibFiles.GetFirst;
  122. LinkRes.AddFileName((maybequoted(s)));
  123. end;
  124. end;
  125. (* if (cs_link_on_target in current_settings.globalswitches) then
  126. begin
  127. LinkRes.Add(')');
  128. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  129. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  130. linklibc:=false;
  131. while not SharedLibFiles.Empty do
  132. begin
  133. S:=SharedLibFiles.GetFirst;
  134. if s<>'c' then
  135. begin
  136. i:=Pos(target_info.sharedlibext,S);
  137. if i>0 then
  138. Delete(S,i,255);
  139. LinkRes.Add('-l'+s);
  140. end
  141. else
  142. begin
  143. LinkRes.Add('-l'+s);
  144. linklibc:=true;
  145. end;
  146. end;
  147. { be sure that libc&libgcc is the last lib }
  148. if linklibc then
  149. begin
  150. LinkRes.Add('-lc');
  151. LinkRes.Add('-lgcc');
  152. end;
  153. end
  154. else
  155. begin
  156. while not SharedLibFiles.Empty do
  157. begin
  158. S:=SharedLibFiles.GetFirst;
  159. LinkRes.Add('lib'+s+target_info.staticlibext);
  160. end;
  161. LinkRes.Add(')');
  162. end;*)
  163. { objects which must be at the end }
  164. (*if linklibc then
  165. begin
  166. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  167. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  168. if found1 or found2 then
  169. begin
  170. LinkRes.Add('INPUT(');
  171. if found1 then
  172. LinkRes.AddFileName(s1);
  173. if found2 then
  174. LinkRes.AddFileName(s2);
  175. LinkRes.Add(')');
  176. end;
  177. end;*)
  178. { Write and Close response }
  179. linkres.writetodisk;
  180. linkres.free;
  181. WriteResponseFile:=True;
  182. end;
  183. procedure TLinkerZXSpectrum_SdccSdld.SetDefaultInfo;
  184. const
  185. {$if defined(Z80)}
  186. ExeName='sdcc-sdldz80';
  187. {$else}
  188. ExeName='sdcc-sdld';
  189. {$endif}
  190. begin
  191. FOrigin:=32768;
  192. with Info do
  193. begin
  194. ExeCmd[1]:=ExeName+' -n -b _CODE=$ORIGIN $OPT -i $MAP $EXE -f $RES'
  195. //-g '+platform_select+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP -L. -o $EXE -T $RES';
  196. end;
  197. end;
  198. function TLinkerZXSpectrum_SdccSdld.MakeExecutable: boolean;
  199. var
  200. binstr,
  201. cmdstr,
  202. mapstr: TCmdStr;
  203. success : boolean;
  204. StaticStr,
  205. //GCSectionsStr,
  206. DynLinkStr,
  207. StripStr,
  208. FixedExeFileName: string;
  209. begin
  210. { for future use }
  211. StaticStr:='';
  212. StripStr:='';
  213. mapstr:='';
  214. DynLinkStr:='';
  215. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.ihx')));
  216. (* GCSectionsStr:='--gc-sections';
  217. //if not(cs_link_extern in current_settings.globalswitches) then
  218. if not(cs_link_nolink in current_settings.globalswitches) then
  219. Message1(exec_i_linking,current_module.exefilename);*)
  220. if (cs_link_map in current_settings.globalswitches) then
  221. mapstr:='-mw';
  222. { Write used files and libraries }
  223. WriteResponseFile();
  224. { Call linker }
  225. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  226. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  227. Replace(cmdstr,'$ORIGIN',tostr(FOrigin));
  228. if not(cs_link_on_target in current_settings.globalswitches) then
  229. begin
  230. Replace(cmdstr,'$EXE',FixedExeFileName);
  231. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  232. Replace(cmdstr,'$STATIC',StaticStr);
  233. Replace(cmdstr,'$STRIP',StripStr);
  234. Replace(cmdstr,'$MAP',mapstr);
  235. //Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  236. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  237. end
  238. else
  239. begin
  240. Replace(cmdstr,'$EXE',FixedExeFileName);
  241. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  242. Replace(cmdstr,'$STATIC',StaticStr);
  243. Replace(cmdstr,'$STRIP',StripStr);
  244. Replace(cmdstr,'$MAP',mapstr);
  245. //Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  246. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  247. end;
  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. (* if success and (target_info.system in [system_arm_embedded,system_avr_embedded,system_mipsel_embedded,system_xtensa_embedded]) then
  256. begin
  257. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  258. FixedExeFileName+' '+
  259. maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.hex'))),true,false);
  260. if success then
  261. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  262. FixedExeFileName+' '+
  263. maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin'))),true,false);
  264. end;*)
  265. MakeExecutable:=success; { otherwise a recursive call to link method }
  266. end;
  267. function TLinkerZXSpectrum_SdccSdld.postprocessexecutable(const fn: string; isdll: boolean): boolean;
  268. begin
  269. result:=DoExec(FindUtil(utilsprefix+'ihx2tzx'),' '+fn,true,false);
  270. end;
  271. {*****************************************************************************
  272. Initialize
  273. *****************************************************************************}
  274. initialization
  275. {$ifdef z80}
  276. RegisterLinker(ld_zxspectrum,TLinkerZXSpectrum_SdccSdld);
  277. RegisterTarget(system_z80_zxspectrum_info);
  278. {$endif z80}
  279. end.