t_win16.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. function MakeSharedLibrary:boolean;override;
  55. end;
  56. { TInternalLinkerWin16 }
  57. TInternalLinkerWin16=class(tinternallinker)
  58. protected
  59. function GetCodeSize(aExeOutput: TExeOutput): QWord;override;
  60. function GetDataSize(aExeOutput: TExeOutput): QWord;override;
  61. function GetBssSize(aExeOutput: TExeOutput): QWord;override;
  62. procedure DefaultLinkScript;override;
  63. public
  64. constructor create;override;
  65. end;
  66. { TDLLScannerWin16 }
  67. TDLLScannerWin16=class(tDLLScanner)
  68. private
  69. importfound : boolean;
  70. { procedure CheckDLLFunc(const dllname,funcname:string);}
  71. public
  72. function Scan(const binname:string):boolean;override;
  73. end;
  74. {****************************************************************************
  75. TImportLibWin16
  76. ****************************************************************************}
  77. procedure TImportLibWin16.generatelib;
  78. var
  79. ObjWriter: TOmfLibObjectWriter;
  80. ObjOutput: TOmfObjOutput;
  81. i,j: Integer;
  82. ImportLibrary: TImportLibrary;
  83. ImportSymbol: TImportSymbol;
  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. current_module.linkotherstaticlibs.add(current_module.importlibfilename,link_always);
  92. ObjWriter:=TOmfLibObjectWriter.CreateAr(current_module.importlibfilename,32);
  93. ObjOutput:=TOmfObjOutput.Create(ObjWriter);
  94. for i:=0 to current_module.ImportLibraryList.Count-1 do
  95. begin
  96. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  97. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  98. begin
  99. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  100. AddImport(StripDllExt(ImportLibrary.Name),ImportSymbol.Name,ImportSymbol.MangledName,ImportSymbol.OrdNr,ImportSymbol.IsVar);
  101. end;
  102. end;
  103. ObjOutput.Free;
  104. ObjWriter.Free;
  105. end;
  106. {****************************************************************************
  107. TExportLibWin16
  108. ****************************************************************************}
  109. destructor TExportLibWin16.Destroy;
  110. begin
  111. EList.Free;
  112. inherited Destroy;
  113. end;
  114. procedure TExportLibWin16.preparelib(const s: string);
  115. begin
  116. if EList=nil then
  117. EList:=TFPList.Create;
  118. end;
  119. procedure TExportLibWin16.exportprocedure(hp: texported_item);
  120. begin
  121. if (eo_index in hp.options) and ((hp.index<=0) or (hp.index>$ffff)) then
  122. begin
  123. message1(parser_e_export_invalid_index,tostr(hp.index));
  124. exit;
  125. end;
  126. EList.Add(hp);
  127. end;
  128. procedure TExportLibWin16.generatelib;
  129. var
  130. ObjWriter: TObjectWriter;
  131. ObjOutput: TOmfObjOutput;
  132. RawRecord: TOmfRawRecord;
  133. Header: TOmfRecord_THEADR;
  134. i: Integer;
  135. hp: texported_item;
  136. ModEnd: TOmfRecord_MODEND;
  137. DllExport_COMENT: TOmfRecord_COMENT=nil;
  138. DllExport_COMENT_EXPDEF: TOmfRecord_COMENT_EXPDEF=nil;
  139. begin
  140. if EList.Count=0 then
  141. exit;
  142. current_module.linkotherofiles.add(current_module.exportfilename,link_always);
  143. ObjWriter:=TObjectWriter.Create;
  144. ObjOutput:=TOmfObjOutput.Create(ObjWriter);
  145. ObjWriter.createfile(current_module.exportfilename);
  146. { write header record }
  147. RawRecord:=TOmfRawRecord.Create;
  148. Header:=TOmfRecord_THEADR.Create;
  149. Header.ModuleName:=current_module.exportfilename;
  150. Header.EncodeTo(RawRecord);
  151. RawRecord.WriteTo(ObjWriter);
  152. Header.Free;
  153. for i:=0 to EList.Count-1 do
  154. begin
  155. hp:=texported_item(EList[i]);
  156. { write EXPDEF record }
  157. DllExport_COMENT_EXPDEF:=TOmfRecord_COMENT_EXPDEF.Create;
  158. DllExport_COMENT_EXPDEF.ExportByOrdinal:=eo_index in hp.options;
  159. DllExport_COMENT_EXPDEF.ResidentName:=eo_resident in hp.options;
  160. DllExport_COMENT_EXPDEF.ExportedName:=hp.name^;
  161. if assigned(hp.sym) then
  162. case hp.sym.typ of
  163. staticvarsym:
  164. DllExport_COMENT_EXPDEF.InternalName:=tstaticvarsym(hp.sym).mangledname;
  165. procsym:
  166. DllExport_COMENT_EXPDEF.InternalName:=tprocdef(tprocsym(hp.sym).ProcdefList[0]).mangledname;
  167. else
  168. internalerror(2015092705);
  169. end
  170. else
  171. DllExport_COMENT_EXPDEF.InternalName:=hp.name^;
  172. if eo_index in hp.options then
  173. DllExport_COMENT_EXPDEF.ExportOrdinal:=hp.index;
  174. DllExport_COMENT:=TOmfRecord_COMENT.Create;
  175. DllExport_COMENT_EXPDEF.EncodeTo(DllExport_COMENT);
  176. FreeAndNil(DllExport_COMENT_EXPDEF);
  177. DllExport_COMENT.EncodeTo(RawRecord);
  178. FreeAndNil(DllExport_COMENT);
  179. RawRecord.WriteTo(ObjWriter);
  180. end;
  181. { write MODEND record }
  182. ModEnd:=TOmfRecord_MODEND.Create;
  183. ModEnd.EncodeTo(RawRecord);
  184. RawRecord.WriteTo(ObjWriter);
  185. ModEnd.Free;
  186. ObjWriter.closefile;
  187. ObjOutput.Free;
  188. ObjWriter.Free;
  189. RawRecord.Free;
  190. end;
  191. {****************************************************************************
  192. TExternalLinkerWin16WLink
  193. ****************************************************************************}
  194. function TExternalLinkerWin16WLink.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. LinkRes.Add('option description '+maybequoted_for_script(description,script_unix));
  206. if target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
  207. LinkRes.Add('debug dwarf')
  208. else if target_dbg.id=dbg_codeview then
  209. LinkRes.Add('debug codeview');
  210. if cs_link_separate_dbg_file in current_settings.globalswitches then
  211. LinkRes.Add('option symfile');
  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 isdll then
  234. LinkRes.Add('format windows dll')
  235. else
  236. LinkRes.Add('format windows');
  237. LinkRes.Add('option heapsize='+tostr(heapsize));
  238. if (cs_link_map in current_settings.globalswitches) then
  239. LinkRes.Add('option map='+maybequoted(ChangeFileExt(current_module.exefilename,'.map')));
  240. if isdll then
  241. LinkRes.Add('name ' + maybequoted(current_module.sharedlibfilename))
  242. else
  243. LinkRes.Add('name ' + maybequoted(current_module.exefilename));
  244. LinkRes.Add('option dosseg');
  245. { Write and Close response }
  246. linkres.writetodisk;
  247. LinkRes.Free;
  248. WriteResponseFile:=True;
  249. end;
  250. constructor TExternalLinkerWin16WLink.Create;
  251. begin
  252. Inherited Create;
  253. { allow duplicated libs (PM) }
  254. SharedLibFiles.doubles:=true;
  255. StaticLibFiles.doubles:=true;
  256. end;
  257. procedure TExternalLinkerWin16WLink.SetDefaultInfo;
  258. begin
  259. with Info do
  260. begin
  261. ExeCmd[1]:='wlink $OPT $RES';
  262. DllCmd[1]:='wlink $OPT $RES';
  263. end;
  264. end;
  265. function TExternalLinkerWin16WLink.MakeExecutable: boolean;
  266. var
  267. binstr,
  268. cmdstr : TCmdStr;
  269. success : boolean;
  270. begin
  271. if not(cs_link_nolink in current_settings.globalswitches) then
  272. Message1(exec_i_linking,current_module.exefilename);
  273. { Write used files and libraries and our own tlink script }
  274. WriteResponsefile(false);
  275. { Call linker }
  276. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  277. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  278. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  279. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  280. { Remove ReponseFile }
  281. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  282. DeleteFile(outputexedir+Info.ResName);
  283. MakeExecutable:=success; { otherwise a recursive call to link method }
  284. end;
  285. function TExternalLinkerWin16WLink.MakeSharedLibrary:boolean;
  286. var
  287. binstr,
  288. cmdstr : TCmdStr;
  289. success : boolean;
  290. begin
  291. if not(cs_link_nolink in current_settings.globalswitches) then
  292. Message1(exec_i_linking,current_module.sharedlibfilename);
  293. { Write used files and libraries and our own tlink script }
  294. WriteResponsefile(true);
  295. { Call linker }
  296. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  297. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  298. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  299. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  300. { Remove ReponseFile }
  301. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  302. DeleteFile(outputexedir+Info.ResName);
  303. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  304. end;
  305. {****************************************************************************
  306. TInternalLinkerWin16
  307. ****************************************************************************}
  308. function TInternalLinkerWin16.GetCodeSize(aExeOutput: TExeOutput): QWord;
  309. begin
  310. { todo }
  311. Result:=0;
  312. end;
  313. function TInternalLinkerWin16.GetDataSize(aExeOutput: TExeOutput): QWord;
  314. begin
  315. { todo }
  316. Result:=0;
  317. end;
  318. function TInternalLinkerWin16.GetBssSize(aExeOutput: TExeOutput): QWord;
  319. begin
  320. { todo }
  321. Result:=0;
  322. end;
  323. procedure TInternalLinkerWin16.DefaultLinkScript;
  324. var
  325. s: TCmdStr;
  326. begin
  327. if IsSharedLibrary then
  328. LinkScript.Concat('ISSHAREDLIBRARY');
  329. { add objectfiles, start with prt0 always }
  330. case current_settings.x86memorymodel of
  331. mm_small: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0s','',false)));
  332. mm_medium: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0m','',false)));
  333. mm_compact: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0c','',false)));
  334. mm_large: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0l','',false)));
  335. mm_huge: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0h','',false)));
  336. else
  337. internalerror(2019061501);
  338. end;
  339. while not ObjectFiles.Empty do
  340. begin
  341. s:=ObjectFiles.GetFirst;
  342. if s<>'' then
  343. LinkScript.Concat('READOBJECT ' + maybequoted(s));
  344. end;
  345. LinkScript.Concat('GROUP');
  346. while not StaticLibFiles.Empty do
  347. begin
  348. s:=StaticLibFiles.GetFirst;
  349. if s<>'' then
  350. LinkScript.Concat('READSTATICLIBRARY '+MaybeQuoted(s));
  351. end;
  352. LinkScript.Concat('ENDGROUP');
  353. LinkScript.Concat('EXESECTION .NE_code');
  354. LinkScript.Concat(' OBJSECTION _TEXT||CODE');
  355. LinkScript.Concat(' OBJSECTION *||CODE');
  356. LinkScript.Concat('ENDEXESECTION');
  357. LinkScript.Concat('EXESECTION .NE_data');
  358. LinkScript.Concat(' OBJSECTION *||FAR_DATA');
  359. LinkScript.Concat(' OBJSECTION _NULL||BEGDATA');
  360. LinkScript.Concat(' OBJSECTION _AFTERNULL||BEGDATA');
  361. LinkScript.Concat(' OBJSECTION *||BEGDATA');
  362. LinkScript.Concat(' OBJSECTION *||DATA');
  363. LinkScript.Concat(' SYMBOL _edata');
  364. LinkScript.Concat(' OBJSECTION *||BSS');
  365. LinkScript.Concat(' SYMBOL _end');
  366. LinkScript.Concat(' OBJSECTION *||STACK');
  367. LinkScript.Concat(' OBJSECTION *||HEAP');
  368. LinkScript.Concat('ENDEXESECTION');
  369. if (cs_debuginfo in current_settings.moduleswitches) and
  370. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4]) then
  371. begin
  372. LinkScript.Concat('EXESECTION .debug_info');
  373. LinkScript.Concat(' OBJSECTION .DEBUG_INFO||DWARF');
  374. LinkScript.Concat('ENDEXESECTION');
  375. LinkScript.Concat('EXESECTION .debug_abbrev');
  376. LinkScript.Concat(' OBJSECTION .DEBUG_ABBREV||DWARF');
  377. LinkScript.Concat('ENDEXESECTION');
  378. LinkScript.Concat('EXESECTION .debug_line');
  379. LinkScript.Concat(' OBJSECTION .DEBUG_LINE||DWARF');
  380. LinkScript.Concat('ENDEXESECTION');
  381. LinkScript.Concat('EXESECTION .debug_aranges');
  382. LinkScript.Concat(' OBJSECTION .DEBUG_ARANGES||DWARF');
  383. LinkScript.Concat('ENDEXESECTION');
  384. end;
  385. LinkScript.Concat('ENTRYNAME ..start');
  386. end;
  387. constructor TInternalLinkerWin16.create;
  388. begin
  389. inherited create;
  390. CArObjectReader:=TOmfLibObjectReader;
  391. CExeOutput:=TNewExeOutput;
  392. CObjInput:=TOmfObjInput;
  393. end;
  394. {****************************************************************************
  395. TDLLScannerWin16
  396. ****************************************************************************}
  397. function TDLLScannerWin16.Scan(const binname: string): boolean;
  398. var
  399. hs,
  400. dllname : TCmdStr;
  401. begin
  402. result:=false;
  403. { is there already an import library the we will use that one }
  404. if FindLibraryFile(binname,target_info.staticClibprefix,target_info.staticClibext,hs) then
  405. exit;
  406. { check if we can find the dll }
  407. hs:=binname;
  408. if ExtractFileExt(hs)='' then
  409. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  410. if not FindDll(hs,dllname) then
  411. exit;
  412. importfound:=false;
  413. {todo: ReadDLLImports(dllname,@CheckDLLFunc);}
  414. if importfound then
  415. current_module.dllscannerinputlist.Pack;
  416. result:=importfound;
  417. end;
  418. {*****************************************************************************
  419. Initialize
  420. *****************************************************************************}
  421. initialization
  422. RegisterLinker(ld_int_win16,TInternalLinkerWin16);
  423. RegisterLinker(ld_win16,TExternalLinkerWin16WLink);
  424. RegisterImport(system_i8086_win16,TImportLibWin16);
  425. RegisterExport(system_i8086_win16,TExportLibWin16);
  426. RegisterDLLScanner(system_i8086_win16,TDLLScannerWin16);
  427. RegisterTarget(system_i8086_win16_info);
  428. end.