t_msdos.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i8086) MS-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_msdos;
  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,script,
  27. fmodule,i_msdos,
  28. link,aasmbase;
  29. type
  30. { Borland TLINK support }
  31. TExternalLinkerMsDosTLink=class(texternallinker)
  32. private
  33. Function WriteResponseFile(isdll:boolean) : Boolean;
  34. public
  35. constructor Create;override;
  36. procedure SetDefaultInfo;override;
  37. function MakeExecutable:boolean;override;
  38. end;
  39. { the ALINK linker from http://alink.sourceforge.net/ }
  40. TExternalLinkerMsDosALink=class(texternallinker)
  41. private
  42. Function WriteResponseFile(isdll:boolean) : Boolean;
  43. public
  44. constructor Create;override;
  45. procedure SetDefaultInfo;override;
  46. function MakeExecutable:boolean;override;
  47. end;
  48. { the (Open) Watcom linker }
  49. TExternalLinkerMsDosWLink=class(texternallinker)
  50. private
  51. Function WriteResponseFile(isdll:boolean) : Boolean;
  52. public
  53. constructor Create;override;
  54. procedure SetDefaultInfo;override;
  55. function MakeExecutable:boolean;override;
  56. end;
  57. {****************************************************************************
  58. TExternalLinkerMsDosTLink
  59. ****************************************************************************}
  60. Constructor TExternalLinkerMsDosTLink.Create;
  61. begin
  62. Inherited Create;
  63. { allow duplicated libs (PM) }
  64. SharedLibFiles.doubles:=true;
  65. StaticLibFiles.doubles:=true;
  66. end;
  67. procedure TExternalLinkerMsDosTLink.SetDefaultInfo;
  68. begin
  69. with Info do
  70. begin
  71. ExeCmd[1]:='tlink $OPT $RES';
  72. end;
  73. end;
  74. Function TExternalLinkerMsDosTLink.WriteResponseFile(isdll:boolean) : Boolean;
  75. Var
  76. linkres : TLinkRes;
  77. s : string;
  78. begin
  79. WriteResponseFile:=False;
  80. { Open link.res file }
  81. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  82. { Add all options to link.res instead of passing them via command line:
  83. DOS command line is limited to 126 characters! }
  84. { add objectfiles, start with prt0 always }
  85. LinkRes.Add(GetShortName(FindObjectFile('prt0','',false)) + ' +');
  86. while not ObjectFiles.Empty do
  87. begin
  88. s:=ObjectFiles.GetFirst;
  89. if s<>'' then
  90. LinkRes.Add(GetShortName(s) + ' +');
  91. end;
  92. LinkRes.Add(', ' + maybequoted(current_module.exefilename));
  93. { Write and Close response }
  94. linkres.writetodisk;
  95. LinkRes.Free;
  96. WriteResponseFile:=True;
  97. end;
  98. function TExternalLinkerMsDosTLink.MakeExecutable:boolean;
  99. var
  100. binstr,
  101. cmdstr : TCmdStr;
  102. success : boolean;
  103. begin
  104. if not(cs_link_nolink in current_settings.globalswitches) then
  105. Message1(exec_i_linking,current_module.exefilename);
  106. { Write used files and libraries and our own tlink script }
  107. WriteResponsefile(false);
  108. { Call linker }
  109. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  110. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  111. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  112. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  113. { Remove ReponseFile }
  114. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  115. DeleteFile(outputexedir+Info.ResName);
  116. MakeExecutable:=success; { otherwise a recursive call to link method }
  117. end;
  118. {****************************************************************************
  119. TExternalLinkerMsDosALink
  120. ****************************************************************************}
  121. { TExternalLinkerMsDosALink }
  122. function TExternalLinkerMsDosALink.WriteResponseFile(isdll: boolean): Boolean;
  123. Var
  124. linkres : TLinkRes;
  125. s : string;
  126. begin
  127. WriteResponseFile:=False;
  128. { Open link.res file }
  129. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  130. { Add all options to link.res instead of passing them via command line:
  131. DOS command line is limited to 126 characters! }
  132. { add objectfiles, start with prt0 always }
  133. LinkRes.Add(maybequoted(FindObjectFile('prt0','',false)));
  134. while not ObjectFiles.Empty do
  135. begin
  136. s:=ObjectFiles.GetFirst;
  137. if s<>'' then
  138. LinkRes.Add(maybequoted(s));
  139. end;
  140. LinkRes.Add('-oEXE');
  141. LinkRes.Add('-o ' + maybequoted(current_module.exefilename));
  142. { Write and Close response }
  143. linkres.writetodisk;
  144. LinkRes.Free;
  145. WriteResponseFile:=True;
  146. end;
  147. constructor TExternalLinkerMsDosALink.Create;
  148. begin
  149. Inherited Create;
  150. { allow duplicated libs (PM) }
  151. SharedLibFiles.doubles:=true;
  152. StaticLibFiles.doubles:=true;
  153. end;
  154. procedure TExternalLinkerMsDosALink.SetDefaultInfo;
  155. begin
  156. with Info do
  157. begin
  158. ExeCmd[1]:='alink $OPT $RES';
  159. end;
  160. end;
  161. function TExternalLinkerMsDosALink.MakeExecutable: boolean;
  162. var
  163. binstr,
  164. cmdstr : TCmdStr;
  165. success : boolean;
  166. begin
  167. if not(cs_link_nolink in current_settings.globalswitches) then
  168. Message1(exec_i_linking,current_module.exefilename);
  169. { Write used files and libraries and our own tlink script }
  170. WriteResponsefile(false);
  171. { Call linker }
  172. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  173. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  174. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  175. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  176. { Remove ReponseFile }
  177. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  178. DeleteFile(outputexedir+Info.ResName);
  179. MakeExecutable:=success; { otherwise a recursive call to link method }
  180. end;
  181. {****************************************************************************
  182. TExternalLinkerMsDosWLink
  183. ****************************************************************************}
  184. { TExternalLinkerMsDosWLink }
  185. function TExternalLinkerMsDosWLink.WriteResponseFile(isdll: boolean): Boolean;
  186. Var
  187. linkres : TLinkRes;
  188. s : string;
  189. begin
  190. WriteResponseFile:=False;
  191. { Open link.res file }
  192. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  193. { Add all options to link.res instead of passing them via command line:
  194. DOS command line is limited to 126 characters! }
  195. LinkRes.Add('option quiet');
  196. if paratargetdbg in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
  197. LinkRes.Add('debug dwarf');
  198. { add objectfiles, start with prt0 always }
  199. case current_settings.x86memorymodel of
  200. mm_tiny: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0t','',false)));
  201. mm_small: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0s','',false)));
  202. mm_medium: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0m','',false)));
  203. mm_compact: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0c','',false)));
  204. mm_large: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0l','',false)));
  205. mm_huge: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0h','',false)));
  206. end;
  207. while not ObjectFiles.Empty do
  208. begin
  209. s:=ObjectFiles.GetFirst;
  210. if s<>'' then
  211. LinkRes.Add('file ' + maybequoted(s));
  212. end;
  213. while not StaticLibFiles.Empty do
  214. begin
  215. s:=StaticLibFiles.GetFirst;
  216. if s<>'' then
  217. LinkRes.Add('library '+MaybeQuoted(s));
  218. end;
  219. if apptype=app_com then
  220. LinkRes.Add('format dos com')
  221. else
  222. LinkRes.Add('format dos');
  223. if current_settings.x86memorymodel=mm_tiny then
  224. LinkRes.Add('order clname CODE clname DATA clname BSS')
  225. else
  226. LinkRes.Add('order clname CODE clname BEGDATA segment _NULL segment _AFTERNULL clname DATA clname BSS clname STACK');
  227. if (cs_link_map in current_settings.globalswitches) then
  228. LinkRes.Add('option map='+maybequoted(ChangeFileExt(current_module.exefilename,'.map')));
  229. LinkRes.Add('name ' + maybequoted(current_module.exefilename));
  230. { Write and Close response }
  231. linkres.writetodisk;
  232. LinkRes.Free;
  233. WriteResponseFile:=True;
  234. end;
  235. constructor TExternalLinkerMsDosWLink.Create;
  236. begin
  237. Inherited Create;
  238. { allow duplicated libs (PM) }
  239. SharedLibFiles.doubles:=true;
  240. StaticLibFiles.doubles:=true;
  241. end;
  242. procedure TExternalLinkerMsDosWLink.SetDefaultInfo;
  243. begin
  244. with Info do
  245. begin
  246. ExeCmd[1]:='wlink $OPT $RES';
  247. end;
  248. end;
  249. function TExternalLinkerMsDosWLink.MakeExecutable: boolean;
  250. var
  251. binstr,
  252. cmdstr : TCmdStr;
  253. success : boolean;
  254. begin
  255. if not(cs_link_nolink in current_settings.globalswitches) then
  256. Message1(exec_i_linking,current_module.exefilename);
  257. { Write used files and libraries and our own tlink script }
  258. WriteResponsefile(false);
  259. { Call linker }
  260. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  261. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  262. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  263. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  264. { Remove ReponseFile }
  265. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  266. DeleteFile(outputexedir+Info.ResName);
  267. MakeExecutable:=success; { otherwise a recursive call to link method }
  268. end;
  269. {*****************************************************************************
  270. Initialize
  271. *****************************************************************************}
  272. initialization
  273. {$if defined(USE_LINKER_TLINK)}
  274. RegisterLinker(ld_msdos,TExternalLinkerMsDosTLink);
  275. {$elseif defined(USE_LINKER_ALINK)}
  276. RegisterLinker(ld_msdos,TExternalLinkerMsDosALink);
  277. {$elseif defined(USE_LINKER_WLINK)}
  278. RegisterLinker(ld_msdos,TExternalLinkerMsDosWLink);
  279. {$else}
  280. {$fatal no linker defined}
  281. {$endif}
  282. RegisterTarget(system_i8086_msdos_info);
  283. end.