t_atari.pas 6.3 KB

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