t_atari.pas 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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_exe_format+' $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. if (UseVLink) and (ataritos_exe_format = 'aoutmint') then
  132. begin
  133. LinkRes.Add('SECTIONS {');
  134. LinkRes.Add(' . = 0xe4;');
  135. LinkRes.Add(' .text: {');
  136. LinkRes.Add(' *(.i* i* I*)');
  137. LinkRes.Add(' *(.t* t* T* .c* c* CODE*)');
  138. LinkRes.Add(' *(.f* f* F*)');
  139. LinkRes.Add(' _etext = .;');
  140. LinkRes.Add(' __etext = .;');
  141. LinkRes.Add(' . = ALIGN(4);');
  142. LinkRes.Add(' }');
  143. LinkRes.Add(' .data: {');
  144. LinkRes.Add(' PROVIDE(_LinkerDB = . + 0x8000);');
  145. LinkRes.Add(' PROVIDE(_SDA_BASE_ = . + 0x8000);');
  146. LinkRes.Add(' VBCC_CONSTRUCTORS');
  147. LinkRes.Add(' *(.rodata*)');
  148. LinkRes.Add(' *(.d* d* D*)');
  149. LinkRes.Add(' *(.sdata*)');
  150. LinkRes.Add(' *(__MERGED)');
  151. LinkRes.Add(' _edata = .;');
  152. LinkRes.Add(' __edata = .;');
  153. LinkRes.Add(' . = ALIGN(4);');
  154. LinkRes.Add(' }');
  155. LinkRes.Add(' .bss: {');
  156. LinkRes.Add(' *(.sbss*)');
  157. LinkRes.Add(' *(.scommon)');
  158. LinkRes.Add(' *(.b* b* B* .u* u* U*)');
  159. LinkRes.Add(' *(COMMON)');
  160. LinkRes.Add(' _end = ALIGN(4);');
  161. LinkRes.Add(' __end = ALIGN(4);');
  162. LinkRes.Add(' }');
  163. LinkRes.Add('}');;
  164. end;
  165. LinkRes.Add('INPUT (');
  166. { add objectfiles, start with prt0 always }
  167. if not (target_info.system in systems_internal_sysinit) then
  168. begin
  169. s:=FindObjectFile('prt0','',false);
  170. LinkRes.AddFileName(maybequoted(s));
  171. end;
  172. while not ObjectFiles.Empty do
  173. begin
  174. s:=ObjectFiles.GetFirst;
  175. if s<>'' then
  176. begin
  177. { vlink doesn't use SEARCH_DIR for object files }
  178. if UseVLink then
  179. s:=FindObjectFile(s,'',false);
  180. LinkRes.AddFileName(maybequoted(s));
  181. end;
  182. end;
  183. { Write staticlibraries }
  184. if not StaticLibFiles.Empty then
  185. begin
  186. { vlink doesn't need, and doesn't support GROUP }
  187. if not UseVLink then
  188. begin
  189. LinkRes.Add(')');
  190. LinkRes.Add('GROUP(');
  191. end;
  192. while not StaticLibFiles.Empty do
  193. begin
  194. S:=StaticLibFiles.GetFirst;
  195. LinkRes.AddFileName(maybequoted(s));
  196. end;
  197. end;
  198. if not UseVLink then
  199. begin
  200. LinkRes.Add(')');
  201. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  202. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  203. linklibc:=false;
  204. while not SharedLibFiles.Empty do
  205. begin
  206. S:=SharedLibFiles.GetFirst;
  207. if s<>'c' then
  208. begin
  209. i:=Pos(target_info.sharedlibext,S);
  210. if i>0 then
  211. Delete(S,i,255);
  212. LinkRes.Add('-l'+s);
  213. end
  214. else
  215. begin
  216. LinkRes.Add('-l'+s);
  217. linklibc:=true;
  218. end;
  219. end;
  220. { be sure that libc&libgcc is the last lib }
  221. if linklibc then
  222. begin
  223. LinkRes.Add('-lc');
  224. LinkRes.Add('-lgcc');
  225. end;
  226. end
  227. else
  228. begin
  229. while not SharedLibFiles.Empty do
  230. begin
  231. S:=SharedLibFiles.GetFirst;
  232. LinkRes.Add('lib'+s+target_info.staticlibext);
  233. end;
  234. LinkRes.Add(')');
  235. end;
  236. { Write and Close response }
  237. linkres.writetodisk;
  238. linkres.free;
  239. WriteResponseFile:=True;
  240. end;
  241. function TLinkerAtari.MakeAtariExe: boolean;
  242. var
  243. BinStr,
  244. CmdStr : TCmdStr;
  245. StripStr: string[40];
  246. DynLinkStr : ansistring;
  247. GCSectionsStr : string;
  248. FlagsStr : string;
  249. MapStr: string;
  250. ExeName: string;
  251. begin
  252. StripStr:='';
  253. GCSectionsStr:='';
  254. DynLinkStr:='';
  255. MapStr:='';
  256. if UseVLink then
  257. FlagsStr:='-tos-flags '+tostr(ataritos_exe_flags)
  258. else
  259. FlagsStr:='--mprg-flags '+tostr(ataritos_exe_flags);
  260. if (cs_link_map in current_settings.globalswitches) then
  261. if UseVLink then
  262. MapStr:='-M'+maybequoted(ScriptFixFileName(current_module.mapfilename))
  263. else
  264. MapStr:='-Map '+maybequoted(ScriptFixFileName(current_module.mapfilename));
  265. if (cs_link_strip in current_settings.globalswitches) then
  266. StripStr:='-s';
  267. if rlinkpath<>'' then
  268. DynLinkStr:='--rpath-link '+rlinkpath;
  269. if UseVLink then
  270. begin
  271. if create_smartlink_sections then
  272. GCSectionsStr:='-gc-all -sc';
  273. end;
  274. ExeName:=current_module.exefilename;
  275. { Call linker }
  276. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  277. binstr:=FindUtil(utilsprefix+BinStr);
  278. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  279. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ExeName)));
  280. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  281. Replace(cmdstr,'$MAP',MapStr);
  282. Replace(cmdstr,'$FLAGS',FlagsStr);
  283. Replace(cmdstr,'$STRIP',StripStr);
  284. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  285. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  286. MakeAtariExe:=DoExec(BinStr,CmdStr,true,false);
  287. end;
  288. function TLinkerAtari.MakeExecutable:boolean;
  289. var
  290. success : boolean;
  291. begin
  292. if not(cs_link_nolink in current_settings.globalswitches) then
  293. Message1(exec_i_linking,current_module.exefilename);
  294. { Write used files and libraries }
  295. WriteResponseFile(false);
  296. success:=MakeAtariExe;
  297. { Remove ReponseFile }
  298. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  299. DeleteFile(outputexedir+Info.ResName);
  300. MakeExecutable:=success; { otherwise a recursive call to link method }
  301. end;
  302. {*****************************************************************************
  303. Initialize
  304. *****************************************************************************}
  305. initialization
  306. RegisterLinker(ld_atari,TLinkerAtari);
  307. RegisterTarget(system_m68k_atari_info);
  308. end.