t_sinclairql.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. type
  43. TQLHeader = packed record
  44. hdr_id: array[0..17] of char;
  45. hdr_reserved: byte;
  46. hdr_length: byte;
  47. hdr_access: byte;
  48. hdr_type: byte;
  49. hdr_data: dword;
  50. hdr_extra: dword;
  51. end;
  52. TXTccData = packed record
  53. xtcc_id: array[0..3] of char;
  54. xtcc_data: dword;
  55. end;
  56. const
  57. DefaultQLHeader: TQLHeader = (
  58. hdr_id: ']!QDOS File Header';
  59. hdr_reserved: 0;
  60. hdr_length: $f;
  61. hdr_access: 0;
  62. hdr_type: 1;
  63. hdr_data: 0;
  64. hdr_extra: 0;
  65. );
  66. DefaultXTccData: TXTCCData = (
  67. xtcc_id: 'XTcc';
  68. xtcc_data: 0;
  69. );
  70. const
  71. DefaultOrigin = $0;
  72. ProgramHeaderName = 'main';
  73. constructor TLinkerSinclairQL.Create;
  74. begin
  75. UseVLink:=(cs_link_vlink in current_settings.globalswitches);
  76. Inherited Create;
  77. { allow duplicated libs (PM) }
  78. SharedLibFiles.doubles:=true;
  79. StaticLibFiles.doubles:=true;
  80. end;
  81. procedure TLinkerSinclairQL.SetSinclairQLInfo;
  82. begin
  83. if ImageBaseSetExplicity then
  84. Origin:=ImageBase
  85. else
  86. Origin:=DefaultOrigin;
  87. with Info do
  88. begin
  89. if not UseVLink then
  90. begin
  91. ExeCmd[1]:='ld $DYNLINK $OPT -d -n -o $EXE $RES';
  92. end
  93. else
  94. begin
  95. ExeCmd[1]:='vlink -b rawseg -q $FLAGS $GCSECTIONS $OPT $STRIP $MAP -o $EXE -T $RES';
  96. end;
  97. end;
  98. end;
  99. procedure TLinkerSinclairQL.SetDefaultInfo;
  100. begin
  101. if target_info.system = system_m68k_sinclairql then
  102. SetSinclairQLInfo;
  103. end;
  104. procedure TLinkerSinclairQL.InitSysInitUnitName;
  105. begin
  106. sysinitunit:='si_prc';
  107. end;
  108. function TLinkerSinclairQL.WriteResponseFile(isdll: boolean): boolean;
  109. var
  110. linkres : TLinkRes;
  111. HPath : TCmdStrListItem;
  112. s : string;
  113. begin
  114. WriteResponseFile:=False;
  115. { Open link.res file }
  116. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  117. { Write path to search libraries }
  118. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  119. while assigned(HPath) do
  120. begin
  121. s:=HPath.Str;
  122. if (cs_link_on_target in current_settings.globalswitches) then
  123. s:=ScriptFixFileName(s);
  124. LinkRes.Add('-L'+s);
  125. HPath:=TCmdStrListItem(HPath.Next);
  126. end;
  127. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  128. while assigned(HPath) do
  129. begin
  130. s:=HPath.Str;
  131. if s<>'' then
  132. LinkRes.Add('SEARCH_DIR("'+s+'")');
  133. HPath:=TCmdStrListItem(HPath.Next);
  134. end;
  135. LinkRes.Add('INPUT (');
  136. { add objectfiles, start with prt0 always }
  137. if not (target_info.system in systems_internal_sysinit) then
  138. begin
  139. s:=FindObjectFile('prt0','',false);
  140. LinkRes.AddFileName(maybequoted(s));
  141. end;
  142. while not ObjectFiles.Empty do
  143. begin
  144. s:=ObjectFiles.GetFirst;
  145. if s<>'' then
  146. begin
  147. { vlink doesn't use SEARCH_DIR for object files }
  148. if UseVLink then
  149. s:=FindObjectFile(s,'',false);
  150. LinkRes.AddFileName(maybequoted(s));
  151. end;
  152. end;
  153. { Write staticlibraries }
  154. if not StaticLibFiles.Empty then
  155. begin
  156. { vlink doesn't need, and doesn't support GROUP }
  157. if not UseVLink then
  158. begin
  159. LinkRes.Add(')');
  160. LinkRes.Add('GROUP(');
  161. end;
  162. while not StaticLibFiles.Empty do
  163. begin
  164. S:=StaticLibFiles.GetFirst;
  165. LinkRes.AddFileName(maybequoted(s));
  166. end;
  167. end;
  168. LinkRes.Add(')');
  169. with LinkRes do
  170. begin
  171. Add('');
  172. Add('PHDRS {');
  173. Add(' '+ProgramHeaderName+' PT_LOAD;');
  174. Add('}');
  175. Add('SECTIONS');
  176. Add('{');
  177. Add(' . = 0x'+hexstr(Origin,8)+';');
  178. Add(' .text : {');
  179. Add(' _stext = .;');
  180. Add(' *(.text .text.* )');
  181. Add(' *(.data .data.* .rodata .rodata.* .fpc.* )');
  182. Add(' *(.stack .stack.*)');
  183. { force the end of section to be word aligned }
  184. Add(' . = ALIGN(2); SHORT(0x514C);');
  185. Add(' _etext = .;');
  186. Add(' } :'+ProgramHeaderName);
  187. Add(' .bss (NOLOAD): {');
  188. Add(' _sbss = .;');
  189. Add(' *(.bss .bss.*)');
  190. Add(' _ebss = .;');
  191. Add(' } :'+ProgramHeaderName);
  192. Add('}');
  193. end;
  194. { Write and Close response }
  195. linkres.writetodisk;
  196. linkres.free;
  197. WriteResponseFile:=True;
  198. end;
  199. function TLinkerSinclairQL.MakeSinclairQLExe: boolean;
  200. var
  201. BinStr,
  202. CmdStr : TCmdStr;
  203. StripStr: string[40];
  204. DynLinkStr : string;
  205. GCSectionsStr : string;
  206. FlagsStr : string;
  207. MapStr : string;
  208. ExeName: string;
  209. fd,fs: file;
  210. fhdr: text;
  211. buf: pointer;
  212. bufread,bufsize: longint;
  213. HdrName: string;
  214. HeaderLine: string;
  215. HeaderSize: longint;
  216. code: word;
  217. QLHeader: TQLHeader;
  218. XTccData: TXTccData;
  219. BinSize: longint;
  220. DataSpace: DWord;
  221. begin
  222. StripStr:='';
  223. GCSectionsStr:='';
  224. DynLinkStr:='';
  225. FlagsStr:='';
  226. MapStr:='';
  227. if (cs_link_map in current_settings.globalswitches) then
  228. MapStr:='-M'+maybequoted(ScriptFixFilename(current_module.mapfilename));
  229. if (cs_link_strip in current_settings.globalswitches) then
  230. StripStr:='-s';
  231. if rlinkpath<>'' then
  232. DynLinkStr:='--rpath-link '+rlinkpath;
  233. if UseVLink then
  234. begin
  235. if create_smartlink_sections then
  236. GCSectionsStr:='-gc-all';
  237. end;
  238. ExeName:=current_module.exefilename;
  239. HdrName:=ExeName+'.hdr';
  240. { Call linker }
  241. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  242. binstr:=FindUtil(utilsprefix+BinStr);
  243. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  244. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ExeName)));
  245. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  246. Replace(cmdstr,'$MAP',MapStr);
  247. Replace(cmdstr,'$FLAGS',FlagsStr);
  248. Replace(cmdstr,'$STRIP',StripStr);
  249. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  250. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  251. MakeSinclairQLExe:=DoExec(BinStr,CmdStr,true,false);
  252. { Kludge:
  253. With the above linker script, vlink will produce two files. The main binary
  254. and the relocation info. Here we copy the two together. (KB) }
  255. if MakeSinclairQLExe then
  256. begin
  257. QLHeader:=DefaultQLHeader;
  258. XTccData:=DefaultXTccData;
  259. BinSize:=0;
  260. bufsize:=16384;
  261. {$push}
  262. {$i-}
  263. { Rename vlink's output file into the header file it is, then parse the
  264. expected length from it. Later we use either this size or the final binary
  265. size in the BASIC loader, depending on which one is bigger. (KB) }
  266. RenameFile(ExeName,HdrName);
  267. assign(fhdr,HdrName);
  268. reset(fhdr);
  269. readln(fhdr,HeaderLine);
  270. Val(Copy(HeaderLine,RPos('0x',HeaderLine),Length(HeaderLine)),HeaderSize,code);
  271. close(fhdr);
  272. buf:=GetMem(bufsize);
  273. assign(fd,ExeName);
  274. rewrite(fd,1);
  275. assign(fs,ExeName+'.'+ProgramHeaderName);
  276. reset(fs,1);
  277. BinSize := FileSize(fs);
  278. { We assume .bss size is total size indicated by linker minus emmited binary.
  279. DataSpace size is .bss + stack space }
  280. DataSpace := NToBE(DWord(HeaderSize - BinSize + StackSize));
  281. { Option: prepend QEmuLator and QPC2 v5 compatible header to EXE }
  282. if sinclairql_metadata_format='QHDR' then
  283. begin
  284. QLHeader.hdr_data:=DataSpace;
  285. blockwrite(fd, QLHeader, sizeof(QLHeader));
  286. end;
  287. repeat
  288. blockread(fs,buf^,bufsize,bufread);
  289. blockwrite(fd,buf^,bufread);
  290. until eof(fs);
  291. close(fs);
  292. // erase(fs);
  293. assign(fs,ExeName+'.'+ProgramHeaderName+'.rel'+ProgramHeaderName);
  294. reset(fs,1);
  295. repeat
  296. blockread(fs,buf^,bufsize,bufread);
  297. blockwrite(fd,buf^,bufread);
  298. until eof(fs);
  299. close(fs);
  300. // erase(fs);
  301. { Option: append cross compilation data space marker, this can be picked up by
  302. a special version of InfoZIP (compiled with -DQLZIP and option -Q) or by any
  303. of the XTcc unpack utilities }
  304. if sinclairql_metadata_format='XTCC' then
  305. begin
  306. XTccData.xtcc_data:=DataSpace;
  307. blockwrite(fd, XTccData, sizeof(XTccData));
  308. end;
  309. close(fd);
  310. {$pop}
  311. FreeMem(buf);
  312. MakeSinclairQLExe:=(code = 0) and not (BinSize = 0) and (IOResult = 0);
  313. end;
  314. end;
  315. function TLinkerSinclairQL.MakeExecutable:boolean;
  316. var
  317. success : boolean;
  318. bootfile : TScript;
  319. ExeName: String;
  320. begin
  321. if not(cs_link_nolink in current_settings.globalswitches) then
  322. Message1(exec_i_linking,current_module.exefilename);
  323. { Write used files and libraries }
  324. WriteResponseFile(false);
  325. success:=MakeSinclairQLExe;
  326. { Remove ReponseFile }
  327. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  328. DeleteFile(outputexedir+Info.ResName);
  329. MakeExecutable:=success; { otherwise a recursive call to link method }
  330. end;
  331. {*****************************************************************************
  332. Initialize
  333. *****************************************************************************}
  334. initialization
  335. RegisterLinker(ld_sinclairql,TLinkerSinclairQL);
  336. RegisterTarget(system_m68k_sinclairql_info);
  337. end.