t_zxspectrum.pas 9.3 KB

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