t_msdos.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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,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. { add objectfiles, start with prt0 always }
  213. case current_settings.x86memorymodel of
  214. mm_tiny: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0t','',false)));
  215. mm_small: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0s','',false)));
  216. mm_medium: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0m','',false)));
  217. mm_compact: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0c','',false)));
  218. mm_large: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0l','',false)));
  219. mm_huge: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0h','',false)));
  220. end;
  221. while not ObjectFiles.Empty do
  222. begin
  223. s:=ObjectFiles.GetFirst;
  224. if s<>'' then
  225. LinkRes.Add('file ' + maybequoted(s));
  226. end;
  227. while not StaticLibFiles.Empty do
  228. begin
  229. s:=StaticLibFiles.GetFirst;
  230. if s<>'' then
  231. LinkRes.Add('library '+MaybeQuoted(s));
  232. end;
  233. if apptype=app_com then
  234. LinkRes.Add('format dos com')
  235. else
  236. LinkRes.Add('format dos');
  237. if current_settings.x86memorymodel=mm_tiny then
  238. LinkRes.Add('order clname CODE clname DATA clname BSS')
  239. else
  240. LinkRes.Add('order clname CODE clname FAR_DATA clname BEGDATA segment _NULL segment _AFTERNULL clname DATA clname BSS clname STACK clname HEAP');
  241. if (cs_link_map in current_settings.globalswitches) then
  242. LinkRes.Add('option map='+maybequoted(ChangeFileExt(current_module.exefilename,'.map')));
  243. LinkRes.Add('name ' + maybequoted(current_module.exefilename));
  244. { Write and Close response }
  245. linkres.writetodisk;
  246. LinkRes.Free;
  247. WriteResponseFile:=True;
  248. end;
  249. constructor TExternalLinkerMsDosWLink.Create;
  250. begin
  251. Inherited Create;
  252. { allow duplicated libs (PM) }
  253. SharedLibFiles.doubles:=true;
  254. StaticLibFiles.doubles:=true;
  255. end;
  256. procedure TExternalLinkerMsDosWLink.SetDefaultInfo;
  257. begin
  258. with Info do
  259. begin
  260. ExeCmd[1]:='wlink $OPT $RES';
  261. end;
  262. end;
  263. function TExternalLinkerMsDosWLink.MakeExecutable: boolean;
  264. var
  265. binstr,
  266. cmdstr : TCmdStr;
  267. success : boolean;
  268. begin
  269. if not(cs_link_nolink in current_settings.globalswitches) then
  270. Message1(exec_i_linking,current_module.exefilename);
  271. { Write used files and libraries and our own tlink script }
  272. WriteResponsefile(false);
  273. { Call linker }
  274. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  275. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  276. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  277. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  278. { Post process }
  279. if success then
  280. success:=PostProcessExecutable(current_module.exefilename);
  281. { Remove ReponseFile }
  282. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  283. DeleteFile(outputexedir+Info.ResName);
  284. MakeExecutable:=success; { otherwise a recursive call to link method }
  285. end;
  286. { In far data memory models, this function sets the MaxAlloc value in the DOS MZ
  287. header according to the difference between HeapMin and HeapMax. We have to do
  288. this manually, because WLink sets MaxAlloc to $FFFF and there seems to be no
  289. way to specify a different value with a linker option. }
  290. function TExternalLinkerMsDosWLink.PostProcessExecutable(const fn: string): Boolean;
  291. var
  292. f: file;
  293. minalloc,maxalloc: Word;
  294. heapmin_paragraphs, heapmax_paragraphs: Integer;
  295. begin
  296. { nothing to do in the near data memory models }
  297. if current_settings.x86memorymodel in x86_near_data_models then
  298. exit(true);
  299. { .COM files are not supported in the far data memory models }
  300. if apptype=app_com then
  301. internalerror(2014062501);
  302. { open file }
  303. assign(f,fn);
  304. {$push}{$I-}
  305. reset(f,1);
  306. if ioresult<>0 then
  307. Message1(execinfo_f_cant_open_executable,fn);
  308. { read minalloc }
  309. seek(f,$A);
  310. BlockRead(f,minalloc,2);
  311. if source_info.endian<>target_info.endian then
  312. minalloc:=SwapEndian(minalloc);
  313. { calculate the additional number of paragraphs needed }
  314. heapmin_paragraphs:=(heapsize + 15) div 16;
  315. heapmax_paragraphs:=(maxheapsize + 15) div 16;
  316. maxalloc:=min(minalloc-heapmin_paragraphs+heapmax_paragraphs,$FFFF);
  317. { write maxalloc }
  318. seek(f,$C);
  319. if source_info.endian<>target_info.endian then
  320. maxalloc:=SwapEndian(maxalloc);
  321. BlockWrite(f,maxalloc,2);
  322. close(f);
  323. {$pop}
  324. if ioresult<>0 then;
  325. Result:=true;
  326. end;
  327. {****************************************************************************
  328. TInternalLinkerMsDos
  329. ****************************************************************************}
  330. function TInternalLinkerMsDos.GetTotalSizeForSegmentClass(
  331. aExeOutput: TExeOutput; const SegClass: string): QWord;
  332. var
  333. objseclist: TFPObjectList;
  334. objsec: TOmfObjSection;
  335. i: Integer;
  336. begin
  337. Result:=0;
  338. objseclist:=TMZExeOutput(aExeOutput).MZFlatContentSection.ObjSectionList;
  339. for i:=0 to objseclist.Count-1 do
  340. begin
  341. objsec:=TOmfObjSection(objseclist[i]);
  342. if objsec.ClassName=SegClass then
  343. Inc(Result,objsec.Size);
  344. end;
  345. end;
  346. function TInternalLinkerMsDos.GetCodeSize(aExeOutput: TExeOutput): QWord;
  347. begin
  348. Result:=GetTotalSizeForSegmentClass(aExeOutput,'CODE');
  349. end;
  350. function TInternalLinkerMsDos.GetDataSize(aExeOutput: TExeOutput): QWord;
  351. begin
  352. Result:=GetTotalSizeForSegmentClass(aExeOutput,'DATA');
  353. end;
  354. function TInternalLinkerMsDos.GetBssSize(aExeOutput: TExeOutput): QWord;
  355. begin
  356. Result:=GetTotalSizeForSegmentClass(aExeOutput,'BSS');
  357. end;
  358. procedure TInternalLinkerMsDos.DefaultLinkScript;
  359. var
  360. s: TCmdStr;
  361. begin
  362. { add objectfiles, start with prt0 always }
  363. case current_settings.x86memorymodel of
  364. mm_tiny: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0t','',false)));
  365. mm_small: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0s','',false)));
  366. mm_medium: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0m','',false)));
  367. mm_compact: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0c','',false)));
  368. mm_large: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0l','',false)));
  369. mm_huge: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0h','',false)));
  370. end;
  371. while not ObjectFiles.Empty do
  372. begin
  373. s:=ObjectFiles.GetFirst;
  374. if s<>'' then
  375. LinkScript.Concat('READOBJECT ' + maybequoted(s));
  376. end;
  377. LinkScript.Concat('GROUP');
  378. while not StaticLibFiles.Empty do
  379. begin
  380. s:=StaticLibFiles.GetFirst;
  381. if s<>'' then
  382. LinkScript.Concat('READSTATICLIBRARY '+MaybeQuoted(s));
  383. end;
  384. LinkScript.Concat('ENDGROUP');
  385. LinkScript.Concat('EXESECTION .MZ_flat_content');
  386. if current_settings.x86memorymodel=mm_tiny then
  387. begin
  388. LinkScript.Concat(' OBJSECTION *||CODE');
  389. LinkScript.Concat(' OBJSECTION *||DATA');
  390. LinkScript.Concat(' SYMBOL _edata');
  391. LinkScript.Concat(' OBJSECTION *||BSS');
  392. LinkScript.Concat(' SYMBOL _end');
  393. end
  394. else
  395. begin
  396. LinkScript.Concat(' OBJSECTION _TEXT||CODE');
  397. LinkScript.Concat(' OBJSECTION *||CODE');
  398. LinkScript.Concat(' OBJSECTION *||FAR_DATA');
  399. LinkScript.Concat(' OBJSECTION _NULL||BEGDATA');
  400. LinkScript.Concat(' OBJSECTION _AFTERNULL||BEGDATA');
  401. LinkScript.Concat(' OBJSECTION *||BEGDATA');
  402. LinkScript.Concat(' OBJSECTION *||DATA');
  403. LinkScript.Concat(' SYMBOL _edata');
  404. LinkScript.Concat(' OBJSECTION *||BSS');
  405. LinkScript.Concat(' SYMBOL _end');
  406. LinkScript.Concat(' OBJSECTION *||STACK');
  407. LinkScript.Concat(' OBJSECTION *||HEAP');
  408. end;
  409. LinkScript.Concat('ENDEXESECTION');
  410. LinkScript.Concat('ENTRYNAME ..start');
  411. end;
  412. constructor TInternalLinkerMsDos.create;
  413. begin
  414. inherited create;
  415. CArObjectReader:=TOmfLibObjectReader;
  416. CExeOutput:=TMZExeOutput;
  417. CObjInput:=TOmfObjInput;
  418. end;
  419. {*****************************************************************************
  420. Initialize
  421. *****************************************************************************}
  422. initialization
  423. RegisterLinker(ld_int_msdos,TInternalLinkerMsDos);
  424. {$if defined(USE_LINKER_TLINK)}
  425. RegisterLinker(ld_msdos,TExternalLinkerMsDosTLink);
  426. {$elseif defined(USE_LINKER_ALINK)}
  427. RegisterLinker(ld_msdos,TExternalLinkerMsDosALink);
  428. {$elseif defined(USE_LINKER_WLINK)}
  429. RegisterLinker(ld_msdos,TExternalLinkerMsDosWLink);
  430. {$else}
  431. {$fatal no linker defined}
  432. {$endif}
  433. RegisterTarget(system_i8086_msdos_info);
  434. end.