t_msdos.pas 16 KB

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