t_sinclairql.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. {
  2. Copyright (c) 2020 by Free Pascal Development Team
  3. This unit implements support import, export, link routines
  4. for the m68k Sinclair QL 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_sinclairql;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. rescmn, comprsrc, link;
  23. type
  24. PLinkerSinclairQL = ^TLinkerSinclairQL;
  25. TLinkerSinclairQL = class(texternallinker)
  26. private
  27. Origin: DWord;
  28. UseVLink: boolean;
  29. function WriteResponseFile(isdll: boolean): boolean;
  30. procedure SetSinclairQLInfo;
  31. function MakeSinclairQLExe: boolean;
  32. public
  33. constructor Create; override;
  34. procedure SetDefaultInfo; override;
  35. procedure InitSysInitUnitName; override;
  36. function MakeExecutable: boolean; override;
  37. end;
  38. implementation
  39. uses
  40. sysutils,cutils,cfileutl,cclasses,aasmbase,
  41. globtype,globals,systems,verbose,cscript,fmodule,i_sinclairql;
  42. const
  43. DefaultOrigin = $20000;
  44. constructor TLinkerSinclairQL.Create;
  45. begin
  46. UseVLink:=(cs_link_vlink in current_settings.globalswitches);
  47. Inherited Create;
  48. { allow duplicated libs (PM) }
  49. SharedLibFiles.doubles:=true;
  50. StaticLibFiles.doubles:=true;
  51. end;
  52. procedure TLinkerSinclairQL.SetSinclairQLInfo;
  53. begin
  54. if ImageBaseSetExplicity then
  55. Origin:=ImageBase
  56. else
  57. Origin:=DefaultOrigin;
  58. with Info do
  59. begin
  60. if not UseVLink then
  61. begin
  62. ExeCmd[1]:='ld $DYNLINK $OPT -d -n -o $EXE $RES';
  63. end
  64. else
  65. begin
  66. ExeCmd[1]:='vlink -b rawbin1 $FLAGS $GCSECTIONS $OPT $STRIP -o $EXE -T $RES';
  67. end;
  68. end;
  69. end;
  70. procedure TLinkerSinclairQL.SetDefaultInfo;
  71. begin
  72. if target_info.system = system_m68k_sinclairql then
  73. SetSinclairQLInfo;
  74. end;
  75. procedure TLinkerSinclairQL.InitSysInitUnitName;
  76. begin
  77. sysinitunit:='si_prc';
  78. end;
  79. function TLinkerSinclairQL.WriteResponseFile(isdll: boolean): boolean;
  80. var
  81. linkres : TLinkRes;
  82. i : longint;
  83. HPath : TCmdStrListItem;
  84. s : string;
  85. linklibc : boolean;
  86. begin
  87. WriteResponseFile:=False;
  88. { Open link.res file }
  89. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  90. { Write path to search libraries }
  91. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  92. while assigned(HPath) do
  93. begin
  94. s:=HPath.Str;
  95. if (cs_link_on_target in current_settings.globalswitches) then
  96. s:=ScriptFixFileName(s);
  97. LinkRes.Add('-L'+s);
  98. HPath:=TCmdStrListItem(HPath.Next);
  99. end;
  100. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  101. while assigned(HPath) do
  102. begin
  103. s:=HPath.Str;
  104. if s<>'' then
  105. LinkRes.Add('SEARCH_DIR("'+s+'")');
  106. HPath:=TCmdStrListItem(HPath.Next);
  107. end;
  108. LinkRes.Add('INPUT (');
  109. { add objectfiles, start with prt0 always }
  110. if not (target_info.system in systems_internal_sysinit) then
  111. begin
  112. s:=FindObjectFile('prt0','',false);
  113. LinkRes.AddFileName(maybequoted(s));
  114. end;
  115. while not ObjectFiles.Empty do
  116. begin
  117. s:=ObjectFiles.GetFirst;
  118. if s<>'' then
  119. begin
  120. { vlink doesn't use SEARCH_DIR for object files }
  121. if UseVLink then
  122. s:=FindObjectFile(s,'',false);
  123. LinkRes.AddFileName(maybequoted(s));
  124. end;
  125. end;
  126. { Write staticlibraries }
  127. if not StaticLibFiles.Empty then
  128. begin
  129. { vlink doesn't need, and doesn't support GROUP }
  130. if not UseVLink then
  131. begin
  132. LinkRes.Add(')');
  133. LinkRes.Add('GROUP(');
  134. end;
  135. while not StaticLibFiles.Empty do
  136. begin
  137. S:=StaticLibFiles.GetFirst;
  138. LinkRes.AddFileName(maybequoted(s));
  139. end;
  140. end;
  141. LinkRes.Add(')');
  142. with LinkRes do
  143. begin
  144. Add('');
  145. Add('SECTIONS');
  146. Add('{');
  147. Add(' . = 0x'+hexstr(Origin,8)+';');
  148. Add(' .text : { *(.text .text.* _CODE _CODE.* ) }');
  149. Add(' .data : { *(.data .data.* .rodata .rodata.* .fpc.* ) }');
  150. Add(' .bss : { *(_BSS _BSS.*) *(.bss .bss.*) *(_BSSEND _BSSEND.*) *(_HEAP _HEAP.*) *(.stack .stack.*) *(_STACK _STACK.*) }');
  151. Add('}');
  152. end;
  153. { Write and Close response }
  154. linkres.writetodisk;
  155. linkres.free;
  156. WriteResponseFile:=True;
  157. end;
  158. function TLinkerSinclairQL.MakeSinclairQLExe: boolean;
  159. var
  160. BinStr,
  161. CmdStr : TCmdStr;
  162. StripStr: string[40];
  163. DynLinkStr : string;
  164. GCSectionsStr : string;
  165. FlagsStr : string;
  166. ExeName: string;
  167. begin
  168. StripStr:='';
  169. GCSectionsStr:='';
  170. DynLinkStr:='';
  171. FlagsStr:='';
  172. if (cs_link_strip in current_settings.globalswitches) then
  173. StripStr:='-s';
  174. if rlinkpath<>'' then
  175. DynLinkStr:='--rpath-link '+rlinkpath;
  176. if UseVLink then
  177. begin
  178. if create_smartlink_sections then
  179. GCSectionsStr:='-gc-all -sc';
  180. end;
  181. ExeName:=current_module.exefilename;
  182. if apptype = app_gui then
  183. Replace(ExeName,target_info.exeext,'.prg');
  184. { Call linker }
  185. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  186. binstr:=FindUtil(utilsprefix+BinStr);
  187. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  188. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ExeName)));
  189. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  190. Replace(cmdstr,'$FLAGS',FlagsStr);
  191. Replace(cmdstr,'$STRIP',StripStr);
  192. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  193. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  194. MakeSinclairQLExe:=DoExec(BinStr,CmdStr,true,false);
  195. end;
  196. function TLinkerSinclairQL.MakeExecutable:boolean;
  197. var
  198. success : boolean;
  199. begin
  200. if not(cs_link_nolink in current_settings.globalswitches) then
  201. Message1(exec_i_linking,current_module.exefilename);
  202. { Write used files and libraries }
  203. WriteResponseFile(false);
  204. success:=MakeSinclairQLExe;
  205. { Remove ReponseFile }
  206. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  207. DeleteFile(outputexedir+Info.ResName);
  208. MakeExecutable:=success; { otherwise a recursive call to link method }
  209. end;
  210. {*****************************************************************************
  211. Initialize
  212. *****************************************************************************}
  213. initialization
  214. RegisterLinker(ld_sinclairql,TLinkerSinclairQL);
  215. RegisterTarget(system_m68k_sinclairql_info);
  216. end.