2
0

t_atari.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 $FLAGS $OPT $STRIP $MAP -d -n -o $EXE -T $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. if not UseVLink then
  104. begin
  105. LinkRes.Add('SECTIONS');
  106. LinkRes.Add('{');
  107. LinkRes.Add(' .text 0xe4:');
  108. LinkRes.Add(' {');
  109. LinkRes.Add(' CREATE_OBJECT_SYMBOLS');
  110. LinkRes.Add(' *(.text)');
  111. LinkRes.Add(' CONSTRUCTORS');
  112. LinkRes.Add(' _etext = .;');
  113. LinkRes.Add(' __etext = .;');
  114. LinkRes.Add(' }');
  115. LinkRes.Add(' .data :');
  116. LinkRes.Add(' {');
  117. LinkRes.Add(' *(.data)');
  118. LinkRes.Add(' _edata = .;');
  119. LinkRes.Add(' __edata = .;');
  120. LinkRes.Add(' }');
  121. LinkRes.Add(' .bss :');
  122. LinkRes.Add(' {');
  123. LinkRes.Add(' __bss_start = .;');
  124. LinkRes.Add(' *(.bss)');
  125. LinkRes.Add(' *(COMMON)');
  126. LinkRes.Add(' _end = .;');
  127. LinkRes.Add(' __end = .;');
  128. LinkRes.Add(' }');
  129. LinkRes.Add('}');
  130. end;
  131. LinkRes.Add('INPUT (');
  132. { add objectfiles, start with prt0 always }
  133. if not (target_info.system in systems_internal_sysinit) then
  134. begin
  135. s:=FindObjectFile('prt0','',false);
  136. LinkRes.AddFileName(maybequoted(s));
  137. end;
  138. while not ObjectFiles.Empty do
  139. begin
  140. s:=ObjectFiles.GetFirst;
  141. if s<>'' then
  142. begin
  143. { vlink doesn't use SEARCH_DIR for object files }
  144. if UseVLink then
  145. s:=FindObjectFile(s,'',false);
  146. LinkRes.AddFileName(maybequoted(s));
  147. end;
  148. end;
  149. { Write staticlibraries }
  150. if not StaticLibFiles.Empty then
  151. begin
  152. { vlink doesn't need, and doesn't support GROUP }
  153. if not UseVLink then
  154. begin
  155. LinkRes.Add(')');
  156. LinkRes.Add('GROUP(');
  157. end;
  158. while not StaticLibFiles.Empty do
  159. begin
  160. S:=StaticLibFiles.GetFirst;
  161. LinkRes.AddFileName(maybequoted(s));
  162. end;
  163. end;
  164. if not UseVLink then
  165. begin
  166. LinkRes.Add(')');
  167. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  168. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  169. linklibc:=false;
  170. while not SharedLibFiles.Empty do
  171. begin
  172. S:=SharedLibFiles.GetFirst;
  173. if s<>'c' then
  174. begin
  175. i:=Pos(target_info.sharedlibext,S);
  176. if i>0 then
  177. Delete(S,i,255);
  178. LinkRes.Add('-l'+s);
  179. end
  180. else
  181. begin
  182. LinkRes.Add('-l'+s);
  183. linklibc:=true;
  184. end;
  185. end;
  186. { be sure that libc&libgcc is the last lib }
  187. if linklibc then
  188. begin
  189. LinkRes.Add('-lc');
  190. LinkRes.Add('-lgcc');
  191. end;
  192. end
  193. else
  194. begin
  195. while not SharedLibFiles.Empty do
  196. begin
  197. S:=SharedLibFiles.GetFirst;
  198. LinkRes.Add('lib'+s+target_info.staticlibext);
  199. end;
  200. LinkRes.Add(')');
  201. end;
  202. { Write and Close response }
  203. linkres.writetodisk;
  204. linkres.free;
  205. WriteResponseFile:=True;
  206. end;
  207. function TLinkerAtari.MakeAtariExe: boolean;
  208. var
  209. BinStr,
  210. CmdStr : TCmdStr;
  211. StripStr: string[40];
  212. DynLinkStr : ansistring;
  213. GCSectionsStr : string;
  214. FlagsStr : string;
  215. MapStr: string;
  216. ExeName: string;
  217. begin
  218. StripStr:='';
  219. GCSectionsStr:='';
  220. DynLinkStr:='';
  221. MapStr:='';
  222. if UseVLink then
  223. FlagsStr:='-tos-flags '+tostr(ataritos_exe_flags)
  224. else
  225. FlagsStr:='--mprg-flags '+tostr(ataritos_exe_flags);
  226. if (cs_link_map in current_settings.globalswitches) then
  227. if UseVLink then
  228. MapStr:='-M'+maybequoted(ScriptFixFileName(current_module.mapfilename))
  229. else
  230. MapStr:='-Map '+maybequoted(ScriptFixFileName(current_module.mapfilename));
  231. if (cs_link_strip in current_settings.globalswitches) then
  232. StripStr:='-s';
  233. if rlinkpath<>'' then
  234. DynLinkStr:='--rpath-link '+rlinkpath;
  235. if UseVLink then
  236. begin
  237. if create_smartlink_sections then
  238. GCSectionsStr:='-gc-all -sc';
  239. end;
  240. ExeName:=current_module.exefilename;
  241. if apptype = app_gui then
  242. Replace(ExeName,target_info.exeext,'.prg');
  243. { Call linker }
  244. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  245. binstr:=FindUtil(utilsprefix+BinStr);
  246. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  247. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ExeName)));
  248. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  249. Replace(cmdstr,'$MAP',MapStr);
  250. Replace(cmdstr,'$FLAGS',FlagsStr);
  251. Replace(cmdstr,'$STRIP',StripStr);
  252. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  253. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  254. MakeAtariExe:=DoExec(BinStr,CmdStr,true,false);
  255. end;
  256. function TLinkerAtari.MakeExecutable:boolean;
  257. var
  258. success : boolean;
  259. begin
  260. if not(cs_link_nolink in current_settings.globalswitches) then
  261. Message1(exec_i_linking,current_module.exefilename);
  262. { Write used files and libraries }
  263. WriteResponseFile(false);
  264. success:=MakeAtariExe;
  265. { Remove ReponseFile }
  266. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  267. DeleteFile(outputexedir+Info.ResName);
  268. MakeExecutable:=success; { otherwise a recursive call to link method }
  269. end;
  270. {*****************************************************************************
  271. Initialize
  272. *****************************************************************************}
  273. initialization
  274. RegisterLinker(ld_atari,TLinkerAtari);
  275. RegisterTarget(system_m68k_atari_info);
  276. end.