t_win16.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i8086) Win16 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_win16;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. SysUtils,
  24. cutils,cfileutl,cclasses,
  25. globtype,globals,systems,verbose,cscript,
  26. import,export,fmodule,i_win16,
  27. link,aasmbase,cpuinfo,
  28. omfbase,ogbase,ogomf,owbase,owomflib,
  29. symconst,symdef,symsym;
  30. type
  31. { TImportLibWin16 }
  32. TImportLibWin16=class(timportlib)
  33. public
  34. procedure generatelib;override;
  35. end;
  36. { TExportLibWin16 }
  37. TExportLibWin16=class(texportlib)
  38. private
  39. EList: TFPList;
  40. public
  41. destructor Destroy;override;
  42. procedure preparelib(const s : string);override;
  43. procedure exportprocedure(hp : texported_item);override;
  44. procedure generatelib;override;
  45. end;
  46. { the (Open) Watcom linker }
  47. TExternalLinkerWin16WLink=class(texternallinker)
  48. private
  49. Function WriteResponseFile(isdll:boolean) : Boolean;
  50. public
  51. constructor Create;override;
  52. procedure SetDefaultInfo;override;
  53. function MakeExecutable:boolean;override;
  54. end;
  55. { TInternalLinkerWin16 }
  56. TInternalLinkerWin16=class(tinternallinker)
  57. protected
  58. function GetCodeSize(aExeOutput: TExeOutput): QWord;override;
  59. function GetDataSize(aExeOutput: TExeOutput): QWord;override;
  60. function GetBssSize(aExeOutput: TExeOutput): QWord;override;
  61. procedure DefaultLinkScript;override;
  62. public
  63. constructor create;override;
  64. end;
  65. { TDLLScannerWin16 }
  66. TDLLScannerWin16=class(tDLLScanner)
  67. private
  68. importfound : boolean;
  69. { procedure CheckDLLFunc(const dllname,funcname:string);}
  70. public
  71. function Scan(const binname:string):boolean;override;
  72. end;
  73. {****************************************************************************
  74. TImportLibWin16
  75. ****************************************************************************}
  76. procedure TImportLibWin16.generatelib;
  77. var
  78. ObjWriter: TOmfLibObjectWriter;
  79. ObjOutput: TOmfObjOutput;
  80. i,j: Integer;
  81. ImportLibrary: TImportLibrary;
  82. ImportSymbol: TImportSymbol;
  83. AsmPrefix: String;
  84. procedure AddImport(const dllname,afuncname,mangledname:string;ordnr:longint;isvar:boolean);
  85. begin
  86. ObjOutput.startObjectfile(mangledname);
  87. ObjOutput.WriteDllImport(dllname,afuncname,mangledname,ordnr,isvar);
  88. ObjOutput.Writer.closefile;
  89. end;
  90. begin
  91. AsmPrefix:='imp'+Lower(current_module.modulename^);
  92. current_module.linkotherstaticlibs.add(current_module.importlibfilename,link_always);
  93. ObjWriter:=TOmfLibObjectWriter.CreateAr(current_module.importlibfilename,32);
  94. ObjOutput:=TOmfObjOutput.Create(ObjWriter);
  95. for i:=0 to current_module.ImportLibraryList.Count-1 do
  96. begin
  97. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  98. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  99. begin
  100. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  101. AddImport(StripDllExt(ImportLibrary.Name),ImportSymbol.Name,ImportSymbol.MangledName,ImportSymbol.OrdNr,ImportSymbol.IsVar);
  102. end;
  103. end;
  104. ObjOutput.Free;
  105. ObjWriter.Free;
  106. end;
  107. {****************************************************************************
  108. TExportLibWin16
  109. ****************************************************************************}
  110. destructor TExportLibWin16.Destroy;
  111. begin
  112. EList.Free;
  113. inherited Destroy;
  114. end;
  115. procedure TExportLibWin16.preparelib(const s: string);
  116. begin
  117. if EList=nil then
  118. EList:=TFPList.Create;
  119. end;
  120. procedure TExportLibWin16.exportprocedure(hp: texported_item);
  121. begin
  122. if (eo_index in hp.options) and ((hp.index<=0) or (hp.index>$ffff)) then
  123. begin
  124. message1(parser_e_export_invalid_index,tostr(hp.index));
  125. exit;
  126. end;
  127. EList.Add(hp);
  128. end;
  129. procedure TExportLibWin16.generatelib;
  130. var
  131. ObjWriter: TObjectWriter;
  132. ObjOutput: TOmfObjOutput;
  133. RawRecord: TOmfRawRecord;
  134. Header: TOmfRecord_THEADR;
  135. i: Integer;
  136. hp: texported_item;
  137. ModEnd: TOmfRecord_MODEND;
  138. DllExport_COMENT: TOmfRecord_COMENT=nil;
  139. DllExport_COMENT_EXPDEF: TOmfRecord_COMENT_EXPDEF=nil;
  140. begin
  141. if EList.Count=0 then
  142. exit;
  143. current_module.linkotherofiles.add(current_module.exportfilename,link_always);
  144. ObjWriter:=TObjectWriter.Create;
  145. ObjOutput:=TOmfObjOutput.Create(ObjWriter);
  146. ObjWriter.createfile(current_module.exportfilename);
  147. { write header record }
  148. RawRecord:=TOmfRawRecord.Create;
  149. Header:=TOmfRecord_THEADR.Create;
  150. Header.ModuleName:=current_module.exportfilename;
  151. Header.EncodeTo(RawRecord);
  152. RawRecord.WriteTo(ObjWriter);
  153. Header.Free;
  154. for i:=0 to EList.Count-1 do
  155. begin
  156. hp:=texported_item(EList[i]);
  157. { write EXPDEF record }
  158. DllExport_COMENT_EXPDEF:=TOmfRecord_COMENT_EXPDEF.Create;
  159. DllExport_COMENT_EXPDEF.ExportByOrdinal:=eo_index in hp.options;
  160. DllExport_COMENT_EXPDEF.ResidentName:=eo_resident in hp.options;
  161. DllExport_COMENT_EXPDEF.ExportedName:=hp.name^;
  162. if assigned(hp.sym) then
  163. case hp.sym.typ of
  164. staticvarsym:
  165. DllExport_COMENT_EXPDEF.InternalName:=tstaticvarsym(hp.sym).mangledname;
  166. procsym:
  167. DllExport_COMENT_EXPDEF.InternalName:=tprocdef(tprocsym(hp.sym).ProcdefList[0]).mangledname;
  168. else
  169. internalerror(2015092701);
  170. end
  171. else
  172. DllExport_COMENT_EXPDEF.InternalName:=hp.name^;
  173. if eo_index in hp.options then
  174. DllExport_COMENT_EXPDEF.ExportOrdinal:=hp.index;
  175. DllExport_COMENT:=TOmfRecord_COMENT.Create;
  176. DllExport_COMENT_EXPDEF.EncodeTo(DllExport_COMENT);
  177. FreeAndNil(DllExport_COMENT_EXPDEF);
  178. DllExport_COMENT.EncodeTo(RawRecord);
  179. FreeAndNil(DllExport_COMENT);
  180. RawRecord.WriteTo(ObjWriter);
  181. end;
  182. { write MODEND record }
  183. ModEnd:=TOmfRecord_MODEND.Create;
  184. ModEnd.EncodeTo(RawRecord);
  185. RawRecord.WriteTo(ObjWriter);
  186. ModEnd.Free;
  187. ObjWriter.closefile;
  188. ObjOutput.Free;
  189. ObjWriter.Free;
  190. RawRecord.Free;
  191. end;
  192. {****************************************************************************
  193. TExternalLinkerWin16WLink
  194. ****************************************************************************}
  195. function TExternalLinkerWin16WLink.WriteResponseFile(isdll: boolean): Boolean;
  196. Var
  197. linkres : TLinkRes;
  198. s : string;
  199. i: Integer;
  200. begin
  201. WriteResponseFile:=False;
  202. { Open link.res file }
  203. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  204. { Add all options to link.res instead of passing them via command line:
  205. DOS command line is limited to 126 characters! }
  206. LinkRes.Add('option quiet');
  207. LinkRes.Add('option description '+maybequoted(description));
  208. if target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
  209. LinkRes.Add('debug dwarf')
  210. else if target_dbg.id=dbg_codeview then
  211. LinkRes.Add('debug codeview');
  212. if cs_link_separate_dbg_file in current_settings.globalswitches then
  213. LinkRes.Add('option symfile');
  214. { add objectfiles, start with prt0 always }
  215. case current_settings.x86memorymodel of
  216. mm_tiny: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0t','',false)));
  217. mm_small: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0s','',false)));
  218. mm_medium: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0m','',false)));
  219. mm_compact: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0c','',false)));
  220. mm_large: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0l','',false)));
  221. mm_huge: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0h','',false)));
  222. end;
  223. while not ObjectFiles.Empty do
  224. begin
  225. s:=ObjectFiles.GetFirst;
  226. if s<>'' then
  227. LinkRes.Add('file ' + maybequoted(s));
  228. end;
  229. while not StaticLibFiles.Empty do
  230. begin
  231. s:=StaticLibFiles.GetFirst;
  232. if s<>'' then
  233. LinkRes.Add('library '+MaybeQuoted(s));
  234. end;
  235. LinkRes.Add('format windows');
  236. LinkRes.Add('option heapsize='+tostr(heapsize));
  237. if (cs_link_map in current_settings.globalswitches) then
  238. LinkRes.Add('option map='+maybequoted(ChangeFileExt(current_module.exefilename,'.map')));
  239. LinkRes.Add('name ' + maybequoted(current_module.exefilename));
  240. LinkRes.Add('option dosseg');
  241. { Write and Close response }
  242. linkres.writetodisk;
  243. LinkRes.Free;
  244. WriteResponseFile:=True;
  245. end;
  246. constructor TExternalLinkerWin16WLink.Create;
  247. begin
  248. Inherited Create;
  249. { allow duplicated libs (PM) }
  250. SharedLibFiles.doubles:=true;
  251. StaticLibFiles.doubles:=true;
  252. end;
  253. procedure TExternalLinkerWin16WLink.SetDefaultInfo;
  254. begin
  255. with Info do
  256. begin
  257. ExeCmd[1]:='wlink $OPT $RES';
  258. end;
  259. end;
  260. function TExternalLinkerWin16WLink.MakeExecutable: boolean;
  261. var
  262. binstr,
  263. cmdstr : TCmdStr;
  264. success : boolean;
  265. begin
  266. if not(cs_link_nolink in current_settings.globalswitches) then
  267. Message1(exec_i_linking,current_module.exefilename);
  268. { Write used files and libraries and our own tlink script }
  269. WriteResponsefile(false);
  270. { Call linker }
  271. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  272. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  273. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  274. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  275. { Remove ReponseFile }
  276. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  277. DeleteFile(outputexedir+Info.ResName);
  278. MakeExecutable:=success; { otherwise a recursive call to link method }
  279. end;
  280. {****************************************************************************
  281. TInternalLinkerWin16
  282. ****************************************************************************}
  283. function TInternalLinkerWin16.GetCodeSize(aExeOutput: TExeOutput): QWord;
  284. begin
  285. { todo }
  286. Result:=0;
  287. end;
  288. function TInternalLinkerWin16.GetDataSize(aExeOutput: TExeOutput): QWord;
  289. begin
  290. { todo }
  291. Result:=0;
  292. end;
  293. function TInternalLinkerWin16.GetBssSize(aExeOutput: TExeOutput): QWord;
  294. begin
  295. { todo }
  296. Result:=0;
  297. end;
  298. procedure TInternalLinkerWin16.DefaultLinkScript;
  299. var
  300. s: TCmdStr;
  301. begin
  302. { add objectfiles, start with prt0 always }
  303. case current_settings.x86memorymodel of
  304. mm_small: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0s','',false)));
  305. mm_medium: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0m','',false)));
  306. mm_compact: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0c','',false)));
  307. mm_large: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0l','',false)));
  308. mm_huge: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0h','',false)));
  309. else
  310. internalerror(2019061501);
  311. end;
  312. while not ObjectFiles.Empty do
  313. begin
  314. s:=ObjectFiles.GetFirst;
  315. if s<>'' then
  316. LinkScript.Concat('READOBJECT ' + maybequoted(s));
  317. end;
  318. LinkScript.Concat('GROUP');
  319. while not StaticLibFiles.Empty do
  320. begin
  321. s:=StaticLibFiles.GetFirst;
  322. if s<>'' then
  323. LinkScript.Concat('READSTATICLIBRARY '+MaybeQuoted(s));
  324. end;
  325. LinkScript.Concat('ENDGROUP');
  326. LinkScript.Concat('EXESECTION .NE_code');
  327. LinkScript.Concat(' OBJSECTION _TEXT||CODE');
  328. LinkScript.Concat(' OBJSECTION *||CODE');
  329. LinkScript.Concat('ENDEXESECTION');
  330. LinkScript.Concat('EXESECTION .NE_data');
  331. LinkScript.Concat(' OBJSECTION *||FAR_DATA');
  332. LinkScript.Concat(' OBJSECTION _NULL||BEGDATA');
  333. LinkScript.Concat(' OBJSECTION _AFTERNULL||BEGDATA');
  334. LinkScript.Concat(' OBJSECTION *||BEGDATA');
  335. LinkScript.Concat(' OBJSECTION *||DATA');
  336. LinkScript.Concat(' SYMBOL _edata');
  337. LinkScript.Concat(' OBJSECTION *||BSS');
  338. LinkScript.Concat(' SYMBOL _end');
  339. LinkScript.Concat(' OBJSECTION *||STACK');
  340. LinkScript.Concat(' OBJSECTION *||HEAP');
  341. LinkScript.Concat('ENDEXESECTION');
  342. if (cs_debuginfo in current_settings.moduleswitches) and
  343. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4]) then
  344. begin
  345. LinkScript.Concat('EXESECTION .debug_info');
  346. LinkScript.Concat(' OBJSECTION .DEBUG_INFO||DWARF');
  347. LinkScript.Concat('ENDEXESECTION');
  348. LinkScript.Concat('EXESECTION .debug_abbrev');
  349. LinkScript.Concat(' OBJSECTION .DEBUG_ABBREV||DWARF');
  350. LinkScript.Concat('ENDEXESECTION');
  351. LinkScript.Concat('EXESECTION .debug_line');
  352. LinkScript.Concat(' OBJSECTION .DEBUG_LINE||DWARF');
  353. LinkScript.Concat('ENDEXESECTION');
  354. LinkScript.Concat('EXESECTION .debug_aranges');
  355. LinkScript.Concat(' OBJSECTION .DEBUG_ARANGES||DWARF');
  356. LinkScript.Concat('ENDEXESECTION');
  357. end;
  358. LinkScript.Concat('ENTRYNAME ..start');
  359. end;
  360. constructor TInternalLinkerWin16.create;
  361. begin
  362. inherited create;
  363. CArObjectReader:=TOmfLibObjectReader;
  364. CExeOutput:=TNewExeOutput;
  365. CObjInput:=TOmfObjInput;
  366. end;
  367. {****************************************************************************
  368. TDLLScannerWin16
  369. ****************************************************************************}
  370. function TDLLScannerWin16.Scan(const binname: string): boolean;
  371. var
  372. hs,
  373. dllname : TCmdStr;
  374. begin
  375. result:=false;
  376. { is there already an import library the we will use that one }
  377. if FindLibraryFile(binname,target_info.staticClibprefix,target_info.staticClibext,hs) then
  378. exit;
  379. { check if we can find the dll }
  380. hs:=binname;
  381. if ExtractFileExt(hs)='' then
  382. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  383. if not FindDll(hs,dllname) then
  384. exit;
  385. importfound:=false;
  386. {todo: ReadDLLImports(dllname,@CheckDLLFunc);}
  387. if importfound then
  388. current_module.dllscannerinputlist.Pack;
  389. result:=importfound;
  390. end;
  391. {*****************************************************************************
  392. Initialize
  393. *****************************************************************************}
  394. initialization
  395. RegisterLinker(ld_int_win16,TInternalLinkerWin16);
  396. RegisterLinker(ld_win16,TExternalLinkerWin16WLink);
  397. RegisterImport(system_i8086_win16,TImportLibWin16);
  398. RegisterExport(system_i8086_win16,TExportLibWin16);
  399. RegisterDLLScanner(system_i8086_win16,TDLLScannerWin16);
  400. RegisterTarget(system_i8086_win16_info);
  401. end.