t_msdos.pas 14 KB

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