t_zxspectrum.pas 8.9 KB

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