t_msxdos.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (z80) MSX-DOS 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_msxdos;
  19. {$i fpcdefs.inc}
  20. {$define USE_LINKER_WLINK}
  21. interface
  22. implementation
  23. uses
  24. SysUtils,
  25. cutils,cfileutl,cclasses,
  26. globtype,globals,systems,verbose,cscript,
  27. fmodule,i_msxdos,
  28. link,aasmbase,cpuinfo,ogrel,owar;
  29. const
  30. DefaultOrigin = $100;
  31. type
  32. { sdld - the sdld linker from the SDCC project ( http://sdcc.sourceforge.net/ ) }
  33. { vlink - the vlink linker by Frank Wille (http://sun.hasenbraten.de/vlink/ ) }
  34. TLinkerMSXDOS=class(texternallinker)
  35. private
  36. FOrigin: Word;
  37. Function WriteResponseFile_Sdld: Boolean;
  38. Function WriteResponseFile_Vlink: Boolean;
  39. procedure SetDefaultInfo_Sdld;
  40. procedure SetDefaultInfo_Vlink;
  41. function MakeExecutable_Sdld: boolean;
  42. function MakeExecutable_Vlink: boolean;
  43. public
  44. procedure SetDefaultInfo; override;
  45. function MakeExecutable: boolean; override;
  46. procedure InitSysInitUnitName; override;
  47. function postprocessexecutable(const fn : string;isdll:boolean): boolean;
  48. end;
  49. TInternalLinkerMSXDOS=class(tinternallinker)
  50. private
  51. FOrigin: Word;
  52. protected
  53. procedure DefaultLinkScript;override;
  54. function ExecutableFilename:String;override;
  55. public
  56. constructor create;override;
  57. procedure InitSysInitUnitName;override;
  58. function MakeExecutable: boolean; override;
  59. function postprocessexecutable(const fn : string): boolean;
  60. end;
  61. {*****************************************************************************
  62. TLinkerMSXDOS
  63. *****************************************************************************}
  64. function TLinkerMSXDOS.WriteResponseFile_Sdld: Boolean;
  65. Var
  66. linkres : TLinkRes;
  67. s : TCmdStr;
  68. prtobj: string[80];
  69. begin
  70. result:=False;
  71. prtobj:='prt0';
  72. { Open link.res file }
  73. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  74. { Write the origin (i.e. the program load address) }
  75. LinkRes.Add('-b _CODE='+tostr(FOrigin));
  76. if not (target_info.system in systems_internal_sysinit) and (prtobj <> '') then
  77. begin
  78. s:=FindObjectFile(prtobj,'',false);
  79. LinkRes.AddFileName(s);
  80. end;
  81. while not ObjectFiles.Empty do
  82. begin
  83. s:=ObjectFiles.GetFirst;
  84. if s<>'' then
  85. begin
  86. if not(cs_link_on_target in current_settings.globalswitches) then
  87. s:=FindObjectFile(s,'',false);
  88. LinkRes.AddFileName((maybequoted(s)));
  89. end;
  90. end;
  91. { Write staticlibraries }
  92. if not StaticLibFiles.Empty then
  93. begin
  94. while not StaticLibFiles.Empty do
  95. begin
  96. S:=StaticLibFiles.GetFirst;
  97. LinkRes.Add('-l'+maybequoted(s));
  98. end;
  99. end;
  100. { Write and Close response }
  101. linkres.writetodisk;
  102. linkres.free;
  103. result:=True;
  104. end;
  105. function TLinkerMSXDOS.WriteResponseFile_Vlink: Boolean;
  106. Var
  107. linkres : TLinkRes;
  108. s : TCmdStr;
  109. prtobj: string[80];
  110. begin
  111. result:=false;
  112. prtobj:='prt0';
  113. { Open link.res file }
  114. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  115. LinkRes.Add('INPUT (');
  116. if not (target_info.system in systems_internal_sysinit) and (prtobj <> '') then
  117. begin
  118. s:=FindObjectFile(prtobj,'',false);
  119. LinkRes.AddFileName(maybequoted(FixFileName(s)));
  120. end;
  121. while not ObjectFiles.Empty do
  122. begin
  123. s:=ObjectFiles.GetFirst;
  124. if s<>'' then
  125. begin
  126. s:=FindObjectFile(s,'',false);
  127. LinkRes.AddFileName(maybequoted(FixFileName(s)));
  128. end;
  129. end;
  130. while not StaticLibFiles.Empty do
  131. begin
  132. S:=StaticLibFiles.GetFirst;
  133. LinkRes.AddFileName(maybequoted(FixFileName(s)));
  134. end;
  135. LinkRes.Add(')');
  136. with LinkRes do
  137. begin
  138. Add('');
  139. Add('SECTIONS');
  140. Add('{');
  141. Add(' . = 0x'+hexstr(FOrigin,4)+';');
  142. Add(' .text : { *(.text .text.* ) }');
  143. Add(' .data : { *(.data .data.* .rodata .rodata.* .bss .bss.* .fpc.* .stack .stack.* ) }');
  144. Add('}');
  145. end;
  146. { Write and Close response }
  147. linkres.writetodisk;
  148. linkres.free;
  149. result:=true;
  150. end;
  151. procedure TLinkerMSXDOS.SetDefaultInfo_Sdld;
  152. const
  153. ExeName='sdldz80';
  154. begin
  155. FOrigin:=$100;
  156. with Info do
  157. begin
  158. ExeCmd[1]:=ExeName+' -n $OPT -i $MAP $EXE -f $RES'
  159. end;
  160. end;
  161. procedure TLinkerMSXDOS.SetDefaultInfo_Vlink;
  162. const
  163. ExeName='vlink';
  164. begin
  165. FOrigin:=$100;
  166. with Info do
  167. begin
  168. ExeCmd[1]:=ExeName+' -bihex $GCSECTIONS -e $STARTSYMBOL $STRIP $OPT -o $EXE -T $RES'
  169. end;
  170. end;
  171. procedure TLinkerMSXDOS.SetDefaultInfo;
  172. begin
  173. if not (cs_link_vlink in current_settings.globalswitches) then
  174. SetDefaultInfo_Sdld
  175. else
  176. SetDefaultInfo_Vlink;
  177. end;
  178. function TLinkerMSXDOS.MakeExecutable_Sdld: boolean;
  179. var
  180. binstr,
  181. cmdstr,
  182. mapstr: TCmdStr;
  183. success : boolean;
  184. StaticStr,
  185. //GCSectionsStr,
  186. DynLinkStr,
  187. StripStr,
  188. FixedExeFileName: string;
  189. begin
  190. { for future use }
  191. StaticStr:='';
  192. StripStr:='';
  193. mapstr:='';
  194. DynLinkStr:='';
  195. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.ihx')));
  196. if (cs_link_map in current_settings.globalswitches) then
  197. mapstr:='-mw';
  198. { Write used files and libraries }
  199. WriteResponseFile_Sdld();
  200. { Call linker }
  201. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  202. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  203. Replace(cmdstr,'$EXE',FixedExeFileName);
  204. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  205. Replace(cmdstr,'$STATIC',StaticStr);
  206. Replace(cmdstr,'$STRIP',StripStr);
  207. Replace(cmdstr,'$MAP',mapstr);
  208. //Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  209. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  210. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  211. { Remove ReponseFile }
  212. if success and not(cs_link_nolink in current_settings.globalswitches) then
  213. DeleteFile(outputexedir+Info.ResName);
  214. { Post process }
  215. if success and not(cs_link_nolink in current_settings.globalswitches) then
  216. success:=PostProcessExecutable(FixedExeFileName,false);
  217. result:=success; { otherwise a recursive call to link method }
  218. end;
  219. function TLinkerMSXDOS.MakeExecutable_Vlink: boolean;
  220. var
  221. binstr,
  222. cmdstr: TCmdStr;
  223. success: boolean;
  224. GCSectionsStr,
  225. StripStr,
  226. StartSymbolStr,
  227. FixedExeFilename: string;
  228. begin
  229. GCSectionsStr:='-gc-all -mtype';
  230. StripStr:='';
  231. StartSymbolStr:='start';
  232. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.ihx')));
  233. { Write used files and libraries }
  234. WriteResponseFile_Vlink();
  235. { Call linker }
  236. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  237. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  238. Replace(cmdstr,'$EXE',FixedExeFileName);
  239. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  240. Replace(cmdstr,'$STRIP',StripStr);
  241. Replace(cmdstr,'$STARTSYMBOL',StartSymbolStr);
  242. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  243. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  244. { Remove ReponseFile }
  245. if success and not(cs_link_nolink in current_settings.globalswitches) then
  246. DeleteFile(outputexedir+Info.ResName);
  247. { Post process }
  248. if success and not(cs_link_nolink in current_settings.globalswitches) then
  249. success:=PostProcessExecutable(FixedExeFileName,false);
  250. result:=success;
  251. end;
  252. function TLinkerMSXDOS.MakeExecutable: boolean;
  253. begin
  254. if not (cs_link_vlink in current_settings.globalswitches) then
  255. result:=MakeExecutable_Sdld
  256. else
  257. result:=MakeExecutable_Vlink;
  258. end;
  259. procedure TLinkerMSXDOS.InitSysInitUnitName;
  260. begin
  261. sysinitunit:='si_prc';
  262. end;
  263. function TLinkerMSXDOS.postprocessexecutable(const fn: string; isdll: boolean): boolean;
  264. begin
  265. result:=DoExec(FindUtil(utilsprefix+'ihxutil'),' -t bin '+fn+' '+maybequoted(ScriptFixFileName(current_module.exefilename)),true,false);
  266. end;
  267. {*****************************************************************************
  268. TInternalLinkerZXSpectrum
  269. *****************************************************************************}
  270. procedure TInternalLinkerMSXDOS.DefaultLinkScript;
  271. var
  272. s : TCmdStr;
  273. prtobj: string[80];
  274. begin
  275. prtobj:='prt0';
  276. if not (target_info.system in systems_internal_sysinit) and (prtobj <> '') then
  277. LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile(prtobj,'',false)));
  278. while not ObjectFiles.Empty do
  279. begin
  280. s:=ObjectFiles.GetFirst;
  281. if s<>'' then
  282. begin
  283. if not(cs_link_on_target in current_settings.globalswitches) then
  284. s:=FindObjectFile(s,'',false);
  285. LinkScript.Concat('READOBJECT ' + maybequoted(s));
  286. end;
  287. end;
  288. LinkScript.Concat('GROUP');
  289. { Write staticlibraries }
  290. if not StaticLibFiles.Empty then
  291. begin
  292. while not StaticLibFiles.Empty do
  293. begin
  294. S:=StaticLibFiles.GetFirst;
  295. if s<>'' then
  296. LinkScript.Concat('READSTATICLIBRARY '+MaybeQuoted(s));
  297. end;
  298. end;
  299. LinkScript.Concat('ENDGROUP');
  300. LinkScript.Concat('IMAGEBASE '+tostr(FOrigin));
  301. LinkScript.Concat('EXESECTION .text');
  302. LinkScript.Concat(' OBJSECTION _CODE');
  303. LinkScript.Concat('ENDEXESECTION');
  304. LinkScript.Concat('EXESECTION .data');
  305. LinkScript.Concat(' OBJSECTION _DATA');
  306. LinkScript.Concat('ENDEXESECTION');
  307. LinkScript.Concat('EXESECTION .bss');
  308. LinkScript.Concat(' OBJSECTION _BSS');
  309. LinkScript.Concat(' OBJSECTION _BSSEND');
  310. LinkScript.Concat(' OBJSECTION _HEAP');
  311. LinkScript.Concat(' OBJSECTION _STACK');
  312. LinkScript.Concat('ENDEXESECTION');
  313. LinkScript.Concat('ENTRYNAME start');
  314. end;
  315. constructor TInternalLinkerMSXDOS.create;
  316. begin
  317. inherited create;
  318. CArObjectReader:=TArObjectReader;
  319. CExeOutput:=TIntelHexExeOutput;
  320. CObjInput:=TRelObjInput;
  321. if ImageBaseSetExplicity then
  322. FOrigin:=ImageBase
  323. else
  324. FOrigin:=DefaultOrigin;
  325. end;
  326. function TInternalLinkerMSXDOS.ExecutableFilename:String;
  327. begin
  328. result:=ChangeFileExt(current_module.exefilename,'.ihx');
  329. end;
  330. procedure TInternalLinkerMSXDOS.InitSysInitUnitName;
  331. begin
  332. sysinitunit:='si_prc';
  333. end;
  334. function TInternalLinkerMSXDOS.MakeExecutable: boolean;
  335. begin
  336. result:=inherited;
  337. { Post process }
  338. if result and not(cs_link_nolink in current_settings.globalswitches) then
  339. result:=PostProcessExecutable(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.ihx'))));
  340. end;
  341. function TInternalLinkerMSXDOS.postprocessexecutable(const fn: string): boolean;
  342. var
  343. exitcode: longint;
  344. FoundBin: ansistring;
  345. Found: Boolean;
  346. utilexe: TCmdStr;
  347. begin
  348. result:=false;
  349. utilexe:=utilsprefix+'ihxutil'+source_info.exeext;
  350. FoundBin:='';
  351. Found:=false;
  352. if utilsdirectory<>'' then
  353. Found:=FindFile(utilexe,utilsdirectory,false,Foundbin);
  354. if (not Found) then
  355. Found:=FindExe(utilexe,false,Foundbin);
  356. if Found then
  357. begin
  358. exitcode:=RequotedExecuteProcess(foundbin,' -t bin '+fn+' '+maybequoted(ScriptFixFileName(current_module.exefilename)));
  359. result:=exitcode<>0;
  360. end;
  361. end;
  362. {*****************************************************************************
  363. Initialize
  364. *****************************************************************************}
  365. initialization
  366. {$ifdef z80}
  367. RegisterLinker(ld_msxdos,TLinkerMSXDOS);
  368. RegisterLinker(ld_int_msxdos,TInternalLinkerMSXDOS);
  369. RegisterTarget(system_z80_msxdos_info);
  370. {$endif z80}
  371. end.