t_atari.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. case (target_info.system) of
  66. system_m68k_Atari: SetAtariInfo;
  67. end;
  68. end;
  69. procedure TLinkerAtari.InitSysInitUnitName;
  70. begin
  71. sysinitunit:='si_prc';
  72. end;
  73. function TLinkerAtari.WriteResponseFile(isdll: boolean): boolean;
  74. var
  75. linkres : TLinkRes;
  76. i : longint;
  77. HPath : TCmdStrListItem;
  78. s : string;
  79. linklibc : boolean;
  80. begin
  81. WriteResponseFile:=False;
  82. { Open link.res file }
  83. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  84. { Write path to search libraries }
  85. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  86. while assigned(HPath) do
  87. begin
  88. s:=HPath.Str;
  89. if (cs_link_on_target in current_settings.globalswitches) then
  90. s:=ScriptFixFileName(s);
  91. LinkRes.Add('-L'+s);
  92. HPath:=TCmdStrListItem(HPath.Next);
  93. end;
  94. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  95. while assigned(HPath) do
  96. begin
  97. s:=HPath.Str;
  98. if s<>'' then
  99. LinkRes.Add('SEARCH_DIR("'+s+'")');
  100. HPath:=TCmdStrListItem(HPath.Next);
  101. end;
  102. LinkRes.Add('INPUT (');
  103. { add objectfiles, start with prt0 always }
  104. if not (target_info.system in systems_internal_sysinit) then
  105. begin
  106. s:=FindObjectFile('prt0','',false);
  107. LinkRes.AddFileName(maybequoted(s));
  108. end;
  109. while not ObjectFiles.Empty do
  110. begin
  111. s:=ObjectFiles.GetFirst;
  112. if s<>'' then
  113. begin
  114. { vlink doesn't use SEARCH_DIR for object files }
  115. if UseVLink then
  116. s:=FindObjectFile(s,'',false);
  117. LinkRes.AddFileName(maybequoted(s));
  118. end;
  119. end;
  120. { Write staticlibraries }
  121. if not StaticLibFiles.Empty then
  122. begin
  123. { vlink doesn't need, and doesn't support GROUP }
  124. if not UseVLink then
  125. begin
  126. LinkRes.Add(')');
  127. LinkRes.Add('GROUP(');
  128. end;
  129. while not StaticLibFiles.Empty do
  130. begin
  131. S:=StaticLibFiles.GetFirst;
  132. LinkRes.AddFileName(maybequoted(s));
  133. end;
  134. end;
  135. if not UseVLink then
  136. begin
  137. LinkRes.Add(')');
  138. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  139. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  140. linklibc:=false;
  141. while not SharedLibFiles.Empty do
  142. begin
  143. S:=SharedLibFiles.GetFirst;
  144. if s<>'c' then
  145. begin
  146. i:=Pos(target_info.sharedlibext,S);
  147. if i>0 then
  148. Delete(S,i,255);
  149. LinkRes.Add('-l'+s);
  150. end
  151. else
  152. begin
  153. LinkRes.Add('-l'+s);
  154. linklibc:=true;
  155. end;
  156. end;
  157. { be sure that libc&libgcc is the last lib }
  158. if linklibc then
  159. begin
  160. LinkRes.Add('-lc');
  161. LinkRes.Add('-lgcc');
  162. end;
  163. end
  164. else
  165. begin
  166. while not SharedLibFiles.Empty do
  167. begin
  168. S:=SharedLibFiles.GetFirst;
  169. LinkRes.Add('lib'+s+target_info.staticlibext);
  170. end;
  171. LinkRes.Add(')');
  172. end;
  173. { Write and Close response }
  174. linkres.writetodisk;
  175. linkres.free;
  176. WriteResponseFile:=True;
  177. end;
  178. function TLinkerAtari.MakeAtariExe: boolean;
  179. var
  180. BinStr,
  181. CmdStr : TCmdStr;
  182. StripStr: string[40];
  183. DynLinkStr : string;
  184. GCSectionsStr : string;
  185. FlagsStr : string;
  186. ExeName: string;
  187. begin
  188. StripStr:='';
  189. GCSectionsStr:='';
  190. DynLinkStr:='';
  191. FlagsStr:='-tos-flags fastload,fastram';
  192. if (cs_link_strip in current_settings.globalswitches) then
  193. StripStr:='-s';
  194. if rlinkpath<>'' then
  195. DynLinkStr:='--rpath-link '+rlinkpath;
  196. if UseVLink then
  197. begin
  198. if create_smartlink_sections then
  199. GCSectionsStr:='-gc-all -sc';
  200. end;
  201. ExeName:=current_module.exefilename;
  202. if apptype = app_gui then
  203. Replace(ExeName,target_info.exeext,'.prg');
  204. { Call linker }
  205. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  206. binstr:=FindUtil(utilsprefix+BinStr);
  207. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  208. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ExeName)));
  209. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  210. Replace(cmdstr,'$FLAGS',FlagsStr);
  211. Replace(cmdstr,'$STRIP',StripStr);
  212. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  213. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  214. MakeAtariExe:=DoExec(BinStr,CmdStr,true,false);
  215. end;
  216. function TLinkerAtari.MakeExecutable:boolean;
  217. var
  218. success : boolean;
  219. begin
  220. if not(cs_link_nolink in current_settings.globalswitches) then
  221. Message1(exec_i_linking,current_module.exefilename);
  222. { Write used files and libraries }
  223. WriteResponseFile(false);
  224. success:=MakeAtariExe;
  225. { Remove ReponseFile }
  226. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  227. DeleteFile(outputexedir+Info.ResName);
  228. MakeExecutable:=success; { otherwise a recursive call to link method }
  229. end;
  230. {*****************************************************************************
  231. Initialize
  232. *****************************************************************************}
  233. initialization
  234. RegisterLinker(ld_atari,TLinkerAtari);
  235. RegisterTarget(system_m68k_atari_info);
  236. end.