t_msdos.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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,cpuinfo,
  29. omfbase,ogomf;
  30. type
  31. { Borland TLINK support }
  32. TExternalLinkerMsDosTLink=class(texternallinker)
  33. private
  34. Function WriteResponseFile(isdll:boolean) : Boolean;
  35. public
  36. constructor Create;override;
  37. procedure SetDefaultInfo;override;
  38. function MakeExecutable:boolean;override;
  39. end;
  40. { the ALINK linker from http://alink.sourceforge.net/ }
  41. TExternalLinkerMsDosALink=class(texternallinker)
  42. private
  43. Function WriteResponseFile(isdll:boolean) : Boolean;
  44. public
  45. constructor Create;override;
  46. procedure SetDefaultInfo;override;
  47. function MakeExecutable:boolean;override;
  48. end;
  49. { the (Open) Watcom linker }
  50. TExternalLinkerMsDosWLink=class(texternallinker)
  51. private
  52. Function WriteResponseFile(isdll:boolean) : Boolean;
  53. Function PostProcessExecutable(const fn:string) : Boolean;
  54. public
  55. constructor Create;override;
  56. procedure SetDefaultInfo;override;
  57. function MakeExecutable:boolean;override;
  58. end;
  59. { TInternalLinkerMsDos }
  60. TInternalLinkerMsDos=class(tinternallinker)
  61. constructor create;override;
  62. end;
  63. {****************************************************************************
  64. TExternalLinkerMsDosTLink
  65. ****************************************************************************}
  66. Constructor TExternalLinkerMsDosTLink.Create;
  67. begin
  68. Inherited Create;
  69. { allow duplicated libs (PM) }
  70. SharedLibFiles.doubles:=true;
  71. StaticLibFiles.doubles:=true;
  72. end;
  73. procedure TExternalLinkerMsDosTLink.SetDefaultInfo;
  74. begin
  75. with Info do
  76. begin
  77. ExeCmd[1]:='tlink $OPT $RES';
  78. end;
  79. end;
  80. Function TExternalLinkerMsDosTLink.WriteResponseFile(isdll:boolean) : Boolean;
  81. Var
  82. linkres : TLinkRes;
  83. s : string;
  84. begin
  85. WriteResponseFile:=False;
  86. { Open link.res file }
  87. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  88. { Add all options to link.res instead of passing them via command line:
  89. DOS command line is limited to 126 characters! }
  90. { add objectfiles, start with prt0 always }
  91. LinkRes.Add(GetShortName(FindObjectFile('prt0','',false)) + ' +');
  92. while not ObjectFiles.Empty do
  93. begin
  94. s:=ObjectFiles.GetFirst;
  95. if s<>'' then
  96. LinkRes.Add(GetShortName(s) + ' +');
  97. end;
  98. LinkRes.Add(', ' + maybequoted(current_module.exefilename));
  99. { Write and Close response }
  100. linkres.writetodisk;
  101. LinkRes.Free;
  102. WriteResponseFile:=True;
  103. end;
  104. function TExternalLinkerMsDosTLink.MakeExecutable:boolean;
  105. var
  106. binstr,
  107. cmdstr : TCmdStr;
  108. success : boolean;
  109. begin
  110. if not(cs_link_nolink in current_settings.globalswitches) then
  111. Message1(exec_i_linking,current_module.exefilename);
  112. { Write used files and libraries and our own tlink script }
  113. WriteResponsefile(false);
  114. { Call linker }
  115. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  116. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  117. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  118. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  119. { Remove ReponseFile }
  120. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  121. DeleteFile(outputexedir+Info.ResName);
  122. MakeExecutable:=success; { otherwise a recursive call to link method }
  123. end;
  124. {****************************************************************************
  125. TExternalLinkerMsDosALink
  126. ****************************************************************************}
  127. { TExternalLinkerMsDosALink }
  128. function TExternalLinkerMsDosALink.WriteResponseFile(isdll: boolean): Boolean;
  129. Var
  130. linkres : TLinkRes;
  131. s : string;
  132. begin
  133. WriteResponseFile:=False;
  134. { Open link.res file }
  135. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  136. { Add all options to link.res instead of passing them via command line:
  137. DOS command line is limited to 126 characters! }
  138. { add objectfiles, start with prt0 always }
  139. LinkRes.Add(maybequoted(FindObjectFile('prt0','',false)));
  140. while not ObjectFiles.Empty do
  141. begin
  142. s:=ObjectFiles.GetFirst;
  143. if s<>'' then
  144. LinkRes.Add(maybequoted(s));
  145. end;
  146. LinkRes.Add('-oEXE');
  147. LinkRes.Add('-o ' + maybequoted(current_module.exefilename));
  148. { Write and Close response }
  149. linkres.writetodisk;
  150. LinkRes.Free;
  151. WriteResponseFile:=True;
  152. end;
  153. constructor TExternalLinkerMsDosALink.Create;
  154. begin
  155. Inherited Create;
  156. { allow duplicated libs (PM) }
  157. SharedLibFiles.doubles:=true;
  158. StaticLibFiles.doubles:=true;
  159. end;
  160. procedure TExternalLinkerMsDosALink.SetDefaultInfo;
  161. begin
  162. with Info do
  163. begin
  164. ExeCmd[1]:='alink $OPT $RES';
  165. end;
  166. end;
  167. function TExternalLinkerMsDosALink.MakeExecutable: boolean;
  168. var
  169. binstr,
  170. cmdstr : TCmdStr;
  171. success : boolean;
  172. begin
  173. if not(cs_link_nolink in current_settings.globalswitches) then
  174. Message1(exec_i_linking,current_module.exefilename);
  175. { Write used files and libraries and our own tlink script }
  176. WriteResponsefile(false);
  177. { Call linker }
  178. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  179. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  180. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  181. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  182. { Remove ReponseFile }
  183. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  184. DeleteFile(outputexedir+Info.ResName);
  185. MakeExecutable:=success; { otherwise a recursive call to link method }
  186. end;
  187. {****************************************************************************
  188. TExternalLinkerMsDosWLink
  189. ****************************************************************************}
  190. { TExternalLinkerMsDosWLink }
  191. function TExternalLinkerMsDosWLink.WriteResponseFile(isdll: boolean): Boolean;
  192. Var
  193. linkres : TLinkRes;
  194. s : string;
  195. begin
  196. WriteResponseFile:=False;
  197. { Open link.res file }
  198. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  199. { Add all options to link.res instead of passing them via command line:
  200. DOS command line is limited to 126 characters! }
  201. LinkRes.Add('option quiet');
  202. if paratargetdbg in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
  203. LinkRes.Add('debug dwarf');
  204. { add objectfiles, start with prt0 always }
  205. case current_settings.x86memorymodel of
  206. mm_tiny: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0t','',false)));
  207. mm_small: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0s','',false)));
  208. mm_medium: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0m','',false)));
  209. mm_compact: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0c','',false)));
  210. mm_large: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0l','',false)));
  211. mm_huge: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0h','',false)));
  212. end;
  213. while not ObjectFiles.Empty do
  214. begin
  215. s:=ObjectFiles.GetFirst;
  216. if s<>'' then
  217. LinkRes.Add('file ' + maybequoted(s));
  218. end;
  219. while not StaticLibFiles.Empty do
  220. begin
  221. s:=StaticLibFiles.GetFirst;
  222. if s<>'' then
  223. LinkRes.Add('library '+MaybeQuoted(s));
  224. end;
  225. if apptype=app_com then
  226. LinkRes.Add('format dos com')
  227. else
  228. LinkRes.Add('format dos');
  229. if current_settings.x86memorymodel=mm_tiny then
  230. LinkRes.Add('order clname CODE clname DATA clname BSS')
  231. else
  232. LinkRes.Add('order clname CODE clname BEGDATA segment _NULL segment _AFTERNULL clname DATA clname BSS clname STACK clname HEAP');
  233. if (cs_link_map in current_settings.globalswitches) then
  234. LinkRes.Add('option map='+maybequoted(ChangeFileExt(current_module.exefilename,'.map')));
  235. LinkRes.Add('name ' + maybequoted(current_module.exefilename));
  236. { Write and Close response }
  237. linkres.writetodisk;
  238. LinkRes.Free;
  239. WriteResponseFile:=True;
  240. end;
  241. constructor TExternalLinkerMsDosWLink.Create;
  242. begin
  243. Inherited Create;
  244. { allow duplicated libs (PM) }
  245. SharedLibFiles.doubles:=true;
  246. StaticLibFiles.doubles:=true;
  247. end;
  248. procedure TExternalLinkerMsDosWLink.SetDefaultInfo;
  249. begin
  250. with Info do
  251. begin
  252. ExeCmd[1]:='wlink $OPT $RES';
  253. end;
  254. end;
  255. function TExternalLinkerMsDosWLink.MakeExecutable: boolean;
  256. var
  257. binstr,
  258. cmdstr : TCmdStr;
  259. success : boolean;
  260. begin
  261. if not(cs_link_nolink in current_settings.globalswitches) then
  262. Message1(exec_i_linking,current_module.exefilename);
  263. { Write used files and libraries and our own tlink script }
  264. WriteResponsefile(false);
  265. { Call linker }
  266. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  267. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  268. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  269. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  270. { Post process }
  271. if success then
  272. success:=PostProcessExecutable(current_module.exefilename);
  273. { Remove ReponseFile }
  274. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  275. DeleteFile(outputexedir+Info.ResName);
  276. MakeExecutable:=success; { otherwise a recursive call to link method }
  277. end;
  278. { In far data memory models, this function sets the MaxAlloc value in the DOS MZ
  279. header according to the difference between HeapMin and HeapMax. We have to do
  280. this manually, because WLink sets MaxAlloc to $FFFF and there seems to be no
  281. way to specify a different value with a linker option. }
  282. function TExternalLinkerMsDosWLink.PostProcessExecutable(const fn: string): Boolean;
  283. var
  284. f: file;
  285. minalloc,maxalloc: Word;
  286. heapmin_paragraphs, heapmax_paragraphs: Integer;
  287. begin
  288. { nothing to do in the near data memory models }
  289. if current_settings.x86memorymodel in x86_near_data_models then
  290. exit(true);
  291. { .COM files are not supported in the far data memory models }
  292. if apptype=app_com then
  293. internalerror(2014062501);
  294. { open file }
  295. assign(f,fn);
  296. {$push}{$I-}
  297. reset(f,1);
  298. if ioresult<>0 then
  299. Message1(execinfo_f_cant_open_executable,fn);
  300. { read minalloc }
  301. seek(f,$A);
  302. BlockRead(f,minalloc,2);
  303. if source_info.endian<>target_info.endian then
  304. minalloc:=SwapEndian(minalloc);
  305. { calculate the additional number of paragraphs needed }
  306. heapmin_paragraphs:=(heapsize + 15) div 16;
  307. heapmax_paragraphs:=(maxheapsize + 15) div 16;
  308. maxalloc:=min(minalloc-heapmin_paragraphs+heapmax_paragraphs,$FFFF);
  309. { write maxalloc }
  310. seek(f,$C);
  311. if source_info.endian<>target_info.endian then
  312. maxalloc:=SwapEndian(maxalloc);
  313. BlockWrite(f,maxalloc,2);
  314. close(f);
  315. {$pop}
  316. if ioresult<>0 then;
  317. Result:=true;
  318. end;
  319. {****************************************************************************
  320. TInternalLinkerMsDos
  321. ****************************************************************************}
  322. constructor TInternalLinkerMsDos.create;
  323. begin
  324. inherited create;
  325. CExeOutput:=TMZExeOutput;
  326. CObjInput:=TOmfObjInput;
  327. end;
  328. {*****************************************************************************
  329. Initialize
  330. *****************************************************************************}
  331. initialization
  332. RegisterLinker(ld_int_msdos,TInternalLinkerMsDos);
  333. {$if defined(USE_LINKER_TLINK)}
  334. RegisterLinker(ld_msdos,TExternalLinkerMsDosTLink);
  335. {$elseif defined(USE_LINKER_ALINK)}
  336. RegisterLinker(ld_msdos,TExternalLinkerMsDosALink);
  337. {$elseif defined(USE_LINKER_WLINK)}
  338. RegisterLinker(ld_msdos,TExternalLinkerMsDosWLink);
  339. {$else}
  340. {$fatal no linker defined}
  341. {$endif}
  342. RegisterTarget(system_i8086_msdos_info);
  343. end.