t_win16.pas 16 KB

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