t_sinclairql.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 $QLFLAGS $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. QLFlagsStr: string;
  210. MapStr : string;
  211. ExeName: string;
  212. fd,fs: file;
  213. fhdr: text;
  214. buf: pointer;
  215. bufread,bufsize: longint;
  216. HdrName: string;
  217. HeaderLine: string;
  218. HeaderSize: longint;
  219. code: word;
  220. QLHeader: TQLHeader;
  221. XTccData: TXTccData;
  222. BinSize: longint;
  223. RelocSize: longint;
  224. DataSpace: DWord;
  225. begin
  226. StripStr:='';
  227. GCSectionsStr:='';
  228. DynLinkStr:='';
  229. FlagsStr:='';
  230. QLFlagsStr:='';
  231. MapStr:='';
  232. if (cs_link_map in current_settings.globalswitches) then
  233. MapStr:='-M'+maybequoted(ScriptFixFilename(current_module.mapfilename));
  234. if (cs_link_strip in current_settings.globalswitches) then
  235. StripStr:='-s';
  236. if rlinkpath<>'' then
  237. DynLinkStr:='--rpath-link '+rlinkpath;
  238. if UseVLink then
  239. begin
  240. if create_smartlink_sections then
  241. GCSectionsStr:='-gc-all';
  242. if sinclairql_vlink_experimental then
  243. QLFlagsStr:='-b sinclairql -q -'+lower(sinclairql_metadata_format)+' -stack='+tostr(StackSize)
  244. else
  245. QLFlagsStr:='-b rawseg -q';
  246. end;
  247. ExeName:=current_module.exefilename;
  248. HdrName:=ExeName+'.hdr';
  249. { Call linker }
  250. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  251. binstr:=FindUtil(utilsprefix+BinStr);
  252. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  253. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ExeName)));
  254. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  255. Replace(cmdstr,'$MAP',MapStr);
  256. Replace(cmdstr,'$FLAGS',FlagsStr);
  257. Replace(cmdstr,'$STRIP',StripStr);
  258. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  259. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  260. Replace(cmdstr,'$QLFLAGS',QLFlagsStr);
  261. MakeSinclairQLExe:=DoExec(BinStr,CmdStr,true,false);
  262. { Kludge:
  263. With the above linker script, vlink will produce two files. The main binary
  264. and the relocation info. Here we copy the two together. (KB) }
  265. if MakeSinclairQLExe and not sinclairql_vlink_experimental then
  266. begin
  267. QLHeader:=DefaultQLHeader;
  268. XTccData:=DefaultXTccData;
  269. BinSize:=0;
  270. RelocSize:=0;
  271. bufsize:=16384;
  272. {$push}
  273. {$i-}
  274. { Rename vlink's output file into the header file it is, then parse the
  275. expected length from it. Later we use either this size or the final binary
  276. size in the BASIC loader, depending on which one is bigger. (KB) }
  277. RenameFile(ExeName,HdrName);
  278. assign(fhdr,HdrName);
  279. reset(fhdr);
  280. readln(fhdr,HeaderLine);
  281. Val(Copy(HeaderLine,RPos('0x',HeaderLine),Length(HeaderLine)),HeaderSize,code);
  282. close(fhdr);
  283. buf:=GetMem(bufsize);
  284. assign(fd,ExeName);
  285. rewrite(fd,1);
  286. assign(fs,ExeName+'.'+ProgramHeaderName+'.rel'+ProgramHeaderName);
  287. reset(fs,1);
  288. RelocSize := FileSize(fs);
  289. close(fs);
  290. assign(fs,ExeName+'.'+ProgramHeaderName);
  291. reset(fs,1);
  292. BinSize := FileSize(fs);
  293. { We assume .bss size is total size indicated by linker minus emmited binary.
  294. DataSpace size is .bss + stack space }
  295. DataSpace := NToBE(DWord(max((HeaderSize - BinSize) - RelocSize + StackSize,0)));
  296. { Option: prepend QEmuLator and QPC2 v5 compatible header to EXE }
  297. if sinclairql_metadata_format='QHDR' then
  298. begin
  299. QLHeader.hdr_data:=DataSpace;
  300. blockwrite(fd, QLHeader, sizeof(QLHeader));
  301. end;
  302. repeat
  303. blockread(fs,buf^,bufsize,bufread);
  304. blockwrite(fd,buf^,bufread);
  305. until eof(fs);
  306. close(fs);
  307. // erase(fs);
  308. assign(fs,ExeName+'.'+ProgramHeaderName+'.rel'+ProgramHeaderName);
  309. reset(fs,1);
  310. repeat
  311. blockread(fs,buf^,bufsize,bufread);
  312. blockwrite(fd,buf^,bufread);
  313. until eof(fs);
  314. close(fs);
  315. // erase(fs);
  316. { Option: append cross compilation data space marker, this can be picked up by
  317. a special version of InfoZIP (compiled with -DQLZIP and option -Q) or by any
  318. of the XTcc unpack utilities }
  319. if sinclairql_metadata_format='XTCC' then
  320. begin
  321. XTccData.xtcc_data:=DataSpace;
  322. blockwrite(fd, XTccData, sizeof(XTccData));
  323. end;
  324. close(fd);
  325. {$pop}
  326. FreeMem(buf);
  327. MakeSinclairQLExe:=(code = 0) and not (BinSize = 0) and (IOResult = 0);
  328. end;
  329. end;
  330. function TLinkerSinclairQL.MakeExecutable:boolean;
  331. var
  332. success : boolean;
  333. bootfile : TScript;
  334. ExeName: String;
  335. begin
  336. if not(cs_link_nolink in current_settings.globalswitches) then
  337. Message1(exec_i_linking,current_module.exefilename);
  338. { Write used files and libraries }
  339. WriteResponseFile(false);
  340. success:=MakeSinclairQLExe;
  341. { Remove ReponseFile }
  342. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  343. DeleteFile(outputexedir+Info.ResName);
  344. MakeExecutable:=success; { otherwise a recursive call to link method }
  345. end;
  346. {*****************************************************************************
  347. Initialize
  348. *****************************************************************************}
  349. initialization
  350. RegisterLinker(ld_sinclairql,TLinkerSinclairQL);
  351. RegisterTarget(system_m68k_sinclairql_info);
  352. end.