t_msdos.pas 12 KB

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