t_atari.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. {
  2. Copyright (c) 2016 by Free Pascal Development Team
  3. This unit implements support import, export, link routines
  4. for the m68k Atari 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_atari;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. rescmn, comprsrc, link;
  23. type
  24. PLinkerAtari = ^TLinkerAtari;
  25. TLinkerAtari = class(texternallinker)
  26. private
  27. UseVLink: boolean;
  28. function WriteResponseFile(isdll: boolean): boolean;
  29. procedure SetAtariInfo;
  30. function MakeAtariExe: boolean;
  31. public
  32. constructor Create; override;
  33. procedure SetDefaultInfo; override;
  34. procedure InitSysInitUnitName; override;
  35. function MakeExecutable: boolean; override;
  36. end;
  37. implementation
  38. uses
  39. sysutils,cutils,cfileutl,cclasses,aasmbase,
  40. globtype,globals,systems,verbose,cscript,fmodule,i_atari;
  41. constructor TLinkerAtari.Create;
  42. begin
  43. UseVLink:=(cs_link_vlink in current_settings.globalswitches);
  44. Inherited Create;
  45. { allow duplicated libs (PM) }
  46. SharedLibFiles.doubles:=true;
  47. StaticLibFiles.doubles:=true;
  48. end;
  49. procedure TLinkerAtari.SetAtariInfo;
  50. begin
  51. with Info do
  52. begin
  53. if not UseVLink then
  54. begin
  55. ExeCmd[1]:='ld $DYNLINK $OPT -d -n -o $EXE $RES';
  56. end
  57. else
  58. begin
  59. ExeCmd[1]:='vlink -b ataritos $FLAGS $GCSECTIONS $OPT $STRIP -o $EXE -T $RES';
  60. end;
  61. end;
  62. end;
  63. procedure TLinkerAtari.SetDefaultInfo;
  64. begin
  65. if target_info.system = system_m68k_Atari then
  66. SetAtariInfo;
  67. end;
  68. procedure TLinkerAtari.InitSysInitUnitName;
  69. begin
  70. sysinitunit:='si_prc';
  71. end;
  72. function TLinkerAtari.WriteResponseFile(isdll: boolean): boolean;
  73. var
  74. linkres : TLinkRes;
  75. i : longint;
  76. HPath : TCmdStrListItem;
  77. s : string;
  78. linklibc : boolean;
  79. begin
  80. WriteResponseFile:=False;
  81. { Open link.res file }
  82. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  83. { Write path to search libraries }
  84. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  85. while assigned(HPath) do
  86. begin
  87. s:=HPath.Str;
  88. if (cs_link_on_target in current_settings.globalswitches) then
  89. s:=ScriptFixFileName(s);
  90. LinkRes.Add('-L'+s);
  91. HPath:=TCmdStrListItem(HPath.Next);
  92. end;
  93. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  94. while assigned(HPath) do
  95. begin
  96. s:=HPath.Str;
  97. if s<>'' then
  98. LinkRes.Add('SEARCH_DIR("'+s+'")');
  99. HPath:=TCmdStrListItem(HPath.Next);
  100. end;
  101. LinkRes.Add('INPUT (');
  102. { add objectfiles, start with prt0 always }
  103. if not (target_info.system in systems_internal_sysinit) then
  104. begin
  105. s:=FindObjectFile('prt0','',false);
  106. LinkRes.AddFileName(maybequoted(s));
  107. end;
  108. while not ObjectFiles.Empty do
  109. begin
  110. s:=ObjectFiles.GetFirst;
  111. if s<>'' then
  112. begin
  113. { vlink doesn't use SEARCH_DIR for object files }
  114. if UseVLink then
  115. s:=FindObjectFile(s,'',false);
  116. LinkRes.AddFileName(maybequoted(s));
  117. end;
  118. end;
  119. { Write staticlibraries }
  120. if not StaticLibFiles.Empty then
  121. begin
  122. { vlink doesn't need, and doesn't support GROUP }
  123. if not UseVLink then
  124. begin
  125. LinkRes.Add(')');
  126. LinkRes.Add('GROUP(');
  127. end;
  128. while not StaticLibFiles.Empty do
  129. begin
  130. S:=StaticLibFiles.GetFirst;
  131. LinkRes.AddFileName(maybequoted(s));
  132. end;
  133. end;
  134. if not UseVLink then
  135. begin
  136. LinkRes.Add(')');
  137. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  138. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  139. linklibc:=false;
  140. while not SharedLibFiles.Empty do
  141. begin
  142. S:=SharedLibFiles.GetFirst;
  143. if s<>'c' then
  144. begin
  145. i:=Pos(target_info.sharedlibext,S);
  146. if i>0 then
  147. Delete(S,i,255);
  148. LinkRes.Add('-l'+s);
  149. end
  150. else
  151. begin
  152. LinkRes.Add('-l'+s);
  153. linklibc:=true;
  154. end;
  155. end;
  156. { be sure that libc&libgcc is the last lib }
  157. if linklibc then
  158. begin
  159. LinkRes.Add('-lc');
  160. LinkRes.Add('-lgcc');
  161. end;
  162. end
  163. else
  164. begin
  165. while not SharedLibFiles.Empty do
  166. begin
  167. S:=SharedLibFiles.GetFirst;
  168. LinkRes.Add('lib'+s+target_info.staticlibext);
  169. end;
  170. LinkRes.Add(')');
  171. end;
  172. { Write and Close response }
  173. linkres.writetodisk;
  174. linkres.free;
  175. WriteResponseFile:=True;
  176. end;
  177. function TLinkerAtari.MakeAtariExe: boolean;
  178. var
  179. BinStr,
  180. CmdStr : TCmdStr;
  181. StripStr: string[40];
  182. DynLinkStr : string;
  183. GCSectionsStr : string;
  184. FlagsStr : string;
  185. ExeName: string;
  186. begin
  187. StripStr:='';
  188. GCSectionsStr:='';
  189. DynLinkStr:='';
  190. FlagsStr:='-tos-flags fastload,fastram';
  191. if (cs_link_strip in current_settings.globalswitches) then
  192. StripStr:='-s';
  193. if rlinkpath<>'' then
  194. DynLinkStr:='--rpath-link '+rlinkpath;
  195. if UseVLink then
  196. begin
  197. if create_smartlink_sections then
  198. GCSectionsStr:='-gc-all -sc';
  199. end;
  200. ExeName:=current_module.exefilename;
  201. if apptype = app_gui then
  202. Replace(ExeName,target_info.exeext,'.prg');
  203. { Call linker }
  204. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  205. binstr:=FindUtil(utilsprefix+BinStr);
  206. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  207. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ExeName)));
  208. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  209. Replace(cmdstr,'$FLAGS',FlagsStr);
  210. Replace(cmdstr,'$STRIP',StripStr);
  211. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  212. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  213. MakeAtariExe:=DoExec(BinStr,CmdStr,true,false);
  214. end;
  215. function TLinkerAtari.MakeExecutable:boolean;
  216. var
  217. success : boolean;
  218. begin
  219. if not(cs_link_nolink in current_settings.globalswitches) then
  220. Message1(exec_i_linking,current_module.exefilename);
  221. { Write used files and libraries }
  222. WriteResponseFile(false);
  223. success:=MakeAtariExe;
  224. { Remove ReponseFile }
  225. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  226. DeleteFile(outputexedir+Info.ResName);
  227. MakeExecutable:=success; { otherwise a recursive call to link method }
  228. end;
  229. {*****************************************************************************
  230. Initialize
  231. *****************************************************************************}
  232. initialization
  233. RegisterLinker(ld_atari,TLinkerAtari);
  234. RegisterTarget(system_m68k_atari_info);
  235. end.