t_sinclairql.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. if UseVLink and (source_info.dirsep <> '/') then
  118. LinkRes.fForceUseForwardSlash:=true;
  119. { Write path to search libraries }
  120. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  121. while assigned(HPath) do
  122. begin
  123. s:=HPath.Str;
  124. if (cs_link_on_target in current_settings.globalswitches) then
  125. s:=ScriptFixFileName(s);
  126. LinkRes.Add('-L'+s);
  127. HPath:=TCmdStrListItem(HPath.Next);
  128. end;
  129. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  130. while assigned(HPath) do
  131. begin
  132. s:=HPath.Str;
  133. if s<>'' then
  134. LinkRes.Add('SEARCH_DIR("'+s+'")');
  135. HPath:=TCmdStrListItem(HPath.Next);
  136. end;
  137. LinkRes.Add('INPUT (');
  138. { add objectfiles, start with prt0 always }
  139. if not (target_info.system in systems_internal_sysinit) then
  140. begin
  141. s:=FindObjectFile('prt0','',false);
  142. LinkRes.AddFileName(maybequoted(s));
  143. end;
  144. while not ObjectFiles.Empty do
  145. begin
  146. s:=ObjectFiles.GetFirst;
  147. if s<>'' then
  148. begin
  149. { vlink doesn't use SEARCH_DIR for object files }
  150. if UseVLink then
  151. s:=FindObjectFile(s,'',false);
  152. LinkRes.AddFileName(maybequoted(s));
  153. end;
  154. end;
  155. { Write staticlibraries }
  156. if not StaticLibFiles.Empty then
  157. begin
  158. { vlink doesn't need, and doesn't support GROUP }
  159. if not UseVLink then
  160. begin
  161. LinkRes.Add(')');
  162. LinkRes.Add('GROUP(');
  163. end;
  164. while not StaticLibFiles.Empty do
  165. begin
  166. S:=StaticLibFiles.GetFirst;
  167. LinkRes.AddFileName(maybequoted(s));
  168. end;
  169. end;
  170. LinkRes.Add(')');
  171. with LinkRes do
  172. begin
  173. Add('');
  174. Add('PHDRS {');
  175. Add(' '+ProgramHeaderName+' PT_LOAD;');
  176. Add('}');
  177. Add('SECTIONS');
  178. Add('{');
  179. Add(' . = 0x'+hexstr(Origin,8)+';');
  180. Add(' .text : {');
  181. Add(' _stext = .;');
  182. Add(' *(.text .text.* )');
  183. Add(' *(.data .data.* .rodata .rodata.* .fpc.* )');
  184. Add(' *(.stack .stack.*)');
  185. { force the end of section to be word aligned }
  186. Add(' . = ALIGN(2); SHORT(0x514C);');
  187. Add(' _etext = .;');
  188. Add(' } :'+ProgramHeaderName);
  189. Add(' .bss (NOLOAD): {');
  190. Add(' _sbss = .;');
  191. Add(' *(.bss .bss.*)');
  192. Add(' _ebss = .;');
  193. Add(' } :'+ProgramHeaderName);
  194. Add('}');
  195. end;
  196. { Write and Close response }
  197. linkres.writetodisk;
  198. linkres.free;
  199. WriteResponseFile:=True;
  200. end;
  201. function TLinkerSinclairQL.MakeSinclairQLExe: boolean;
  202. var
  203. BinStr,
  204. CmdStr : TCmdStr;
  205. StripStr: string[40];
  206. DynLinkStr : string;
  207. GCSectionsStr : string;
  208. FlagsStr : string;
  209. MapStr : string;
  210. ExeName: string;
  211. fd,fs: file;
  212. fhdr: text;
  213. buf: pointer;
  214. bufread,bufsize: longint;
  215. HdrName: string;
  216. HeaderLine: string;
  217. HeaderSize: longint;
  218. code: word;
  219. QLHeader: TQLHeader;
  220. XTccData: TXTccData;
  221. BinSize: longint;
  222. DataSpace: DWord;
  223. begin
  224. StripStr:='';
  225. GCSectionsStr:='';
  226. DynLinkStr:='';
  227. FlagsStr:='';
  228. MapStr:='';
  229. if (cs_link_map in current_settings.globalswitches) then
  230. MapStr:='-M'+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';
  239. end;
  240. ExeName:=current_module.exefilename;
  241. HdrName:=ExeName+'.hdr';
  242. { Call linker }
  243. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  244. binstr:=FindUtil(utilsprefix+BinStr);
  245. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  246. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ExeName)));
  247. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  248. Replace(cmdstr,'$MAP',MapStr);
  249. Replace(cmdstr,'$FLAGS',FlagsStr);
  250. Replace(cmdstr,'$STRIP',StripStr);
  251. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  252. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  253. MakeSinclairQLExe:=DoExec(BinStr,CmdStr,true,false);
  254. { Kludge:
  255. With the above linker script, vlink will produce two files. The main binary
  256. and the relocation info. Here we copy the two together. (KB) }
  257. if MakeSinclairQLExe then
  258. begin
  259. QLHeader:=DefaultQLHeader;
  260. XTccData:=DefaultXTccData;
  261. BinSize:=0;
  262. bufsize:=16384;
  263. {$push}
  264. {$i-}
  265. { Rename vlink's output file into the header file it is, then parse the
  266. expected length from it. Later we use either this size or the final binary
  267. size in the BASIC loader, depending on which one is bigger. (KB) }
  268. RenameFile(ExeName,HdrName);
  269. assign(fhdr,HdrName);
  270. reset(fhdr);
  271. readln(fhdr,HeaderLine);
  272. Val(Copy(HeaderLine,RPos('0x',HeaderLine),Length(HeaderLine)),HeaderSize,code);
  273. close(fhdr);
  274. buf:=GetMem(bufsize);
  275. assign(fd,ExeName);
  276. rewrite(fd,1);
  277. assign(fs,ExeName+'.'+ProgramHeaderName);
  278. reset(fs,1);
  279. BinSize := FileSize(fs);
  280. { We assume .bss size is total size indicated by linker minus emmited binary.
  281. DataSpace size is .bss + stack space }
  282. DataSpace := NToBE(DWord(HeaderSize - BinSize + StackSize));
  283. { Option: prepend QEmuLator and QPC2 v5 compatible header to EXE }
  284. if sinclairql_metadata_format='QHDR' then
  285. begin
  286. QLHeader.hdr_data:=DataSpace;
  287. blockwrite(fd, QLHeader, sizeof(QLHeader));
  288. end;
  289. repeat
  290. blockread(fs,buf^,bufsize,bufread);
  291. blockwrite(fd,buf^,bufread);
  292. until eof(fs);
  293. close(fs);
  294. // erase(fs);
  295. assign(fs,ExeName+'.'+ProgramHeaderName+'.rel'+ProgramHeaderName);
  296. reset(fs,1);
  297. repeat
  298. blockread(fs,buf^,bufsize,bufread);
  299. blockwrite(fd,buf^,bufread);
  300. until eof(fs);
  301. close(fs);
  302. // erase(fs);
  303. { Option: append cross compilation data space marker, this can be picked up by
  304. a special version of InfoZIP (compiled with -DQLZIP and option -Q) or by any
  305. of the XTcc unpack utilities }
  306. if sinclairql_metadata_format='XTCC' then
  307. begin
  308. XTccData.xtcc_data:=DataSpace;
  309. blockwrite(fd, XTccData, sizeof(XTccData));
  310. end;
  311. close(fd);
  312. {$pop}
  313. FreeMem(buf);
  314. MakeSinclairQLExe:=(code = 0) and not (BinSize = 0) and (IOResult = 0);
  315. end;
  316. end;
  317. function TLinkerSinclairQL.MakeExecutable:boolean;
  318. var
  319. success : boolean;
  320. bootfile : TScript;
  321. ExeName: String;
  322. begin
  323. if not(cs_link_nolink in current_settings.globalswitches) then
  324. Message1(exec_i_linking,current_module.exefilename);
  325. { Write used files and libraries }
  326. WriteResponseFile(false);
  327. success:=MakeSinclairQLExe;
  328. { Remove ReponseFile }
  329. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  330. DeleteFile(outputexedir+Info.ResName);
  331. MakeExecutable:=success; { otherwise a recursive call to link method }
  332. end;
  333. {*****************************************************************************
  334. Initialize
  335. *****************************************************************************}
  336. initialization
  337. RegisterLinker(ld_sinclairql,TLinkerSinclairQL);
  338. RegisterTarget(system_m68k_sinclairql_info);
  339. end.