t_atari.pas 7.3 KB

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