t_win16.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. procedure DefaultLinkScript;override;
  59. public
  60. constructor create;override;
  61. end;
  62. {****************************************************************************
  63. TImportLibWin16
  64. ****************************************************************************}
  65. procedure TImportLibWin16.generatelib;
  66. var
  67. ObjWriter: TOmfLibObjectWriter;
  68. ObjOutput: TOmfObjOutput;
  69. i,j: Integer;
  70. ImportLibrary: TImportLibrary;
  71. ImportSymbol: TImportSymbol;
  72. AsmPrefix: String;
  73. procedure AddImport(const dllname,afuncname,mangledname:string;ordnr:longint;isvar:boolean);
  74. begin
  75. ObjOutput.startObjectfile(mangledname);
  76. ObjOutput.WriteDllImport(dllname,afuncname,mangledname,ordnr,isvar);
  77. ObjOutput.Writer.closefile;
  78. end;
  79. begin
  80. AsmPrefix:='imp'+Lower(current_module.modulename^);
  81. current_module.linkotherstaticlibs.add(current_module.importlibfilename,link_always);
  82. ObjWriter:=TOmfLibObjectWriter.CreateAr(current_module.importlibfilename,32);
  83. ObjOutput:=TOmfObjOutput.Create(ObjWriter);
  84. for i:=0 to current_module.ImportLibraryList.Count-1 do
  85. begin
  86. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  87. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  88. begin
  89. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  90. AddImport(ImportLibrary.Name,ImportSymbol.Name,ImportSymbol.MangledName,ImportSymbol.OrdNr,ImportSymbol.IsVar);
  91. end;
  92. end;
  93. ObjOutput.Free;
  94. ObjWriter.Free;
  95. end;
  96. {****************************************************************************
  97. TExportLibWin16
  98. ****************************************************************************}
  99. destructor TExportLibWin16.Destroy;
  100. begin
  101. EList.Free;
  102. inherited Destroy;
  103. end;
  104. procedure TExportLibWin16.preparelib(const s: string);
  105. begin
  106. if EList=nil then
  107. EList:=TFPList.Create;
  108. end;
  109. procedure TExportLibWin16.exportprocedure(hp: texported_item);
  110. begin
  111. if (eo_index in hp.options) and ((hp.index<=0) or (hp.index>$ffff)) then
  112. begin
  113. message1(parser_e_export_invalid_index,tostr(hp.index));
  114. exit;
  115. end;
  116. EList.Add(hp);
  117. end;
  118. procedure TExportLibWin16.generatelib;
  119. var
  120. ObjWriter: TObjectWriter;
  121. ObjOutput: TOmfObjOutput;
  122. RawRecord: TOmfRawRecord;
  123. Header: TOmfRecord_THEADR;
  124. i: Integer;
  125. hp: texported_item;
  126. ModEnd: TOmfRecord_MODEND;
  127. DllExport_COMENT: TOmfRecord_COMENT;
  128. expflag: Byte;
  129. internal_name: TSymStr;
  130. begin
  131. if EList.Count=0 then
  132. exit;
  133. current_module.linkotherofiles.add(current_module.exportfilename,link_always);
  134. ObjWriter:=TObjectWriter.Create;
  135. ObjOutput:=TOmfObjOutput.Create(ObjWriter);
  136. ObjWriter.createfile(current_module.exportfilename);
  137. { write header record }
  138. RawRecord:=TOmfRawRecord.Create;
  139. Header:=TOmfRecord_THEADR.Create;
  140. Header.ModuleName:=current_module.exportfilename;
  141. Header.EncodeTo(RawRecord);
  142. RawRecord.WriteTo(ObjWriter);
  143. Header.Free;
  144. for i:=0 to EList.Count-1 do
  145. begin
  146. hp:=texported_item(EList[i]);
  147. { write EXPDEF record }
  148. DllExport_COMENT:=TOmfRecord_COMENT.Create;
  149. DllExport_COMENT.CommentClass:=CC_OmfExtension;
  150. expflag:=0;
  151. if eo_index in hp.options then
  152. expflag:=expflag or $80;
  153. if eo_resident in hp.options then
  154. expflag:=expflag or $40;
  155. if assigned(hp.sym) then
  156. case hp.sym.typ of
  157. staticvarsym:
  158. internal_name:=tstaticvarsym(hp.sym).mangledname;
  159. procsym:
  160. internal_name:=tprocdef(tprocsym(hp.sym).ProcdefList[0]).mangledname;
  161. else
  162. internalerror(2015092701);
  163. end
  164. else
  165. internal_name:=hp.name^;
  166. DllExport_COMENT.CommentString:=#2+Chr(expflag)+Chr(Length(hp.name^))+hp.name^+Chr(Length(internal_name))+internal_name;
  167. if eo_index in hp.options then
  168. DllExport_COMENT.CommentString:=DllExport_COMENT.CommentString+Chr(Byte(hp.index))+Chr(Byte(hp.index shr 8));
  169. DllExport_COMENT.EncodeTo(RawRecord);
  170. RawRecord.WriteTo(ObjWriter);
  171. DllExport_COMENT.Free;
  172. end;
  173. { write MODEND record }
  174. ModEnd:=TOmfRecord_MODEND.Create;
  175. ModEnd.EncodeTo(RawRecord);
  176. RawRecord.WriteTo(ObjWriter);
  177. ModEnd.Free;
  178. ObjWriter.closefile;
  179. ObjOutput.Free;
  180. ObjWriter.Free;
  181. RawRecord.Free;
  182. end;
  183. {****************************************************************************
  184. TExternalLinkerWin16WLink
  185. ****************************************************************************}
  186. function TExternalLinkerWin16WLink.WriteResponseFile(isdll: boolean): Boolean;
  187. Var
  188. linkres : TLinkRes;
  189. s : string;
  190. i: Integer;
  191. begin
  192. WriteResponseFile:=False;
  193. { Open link.res file }
  194. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  195. { Add all options to link.res instead of passing them via command line:
  196. DOS command line is limited to 126 characters! }
  197. LinkRes.Add('option quiet');
  198. if target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
  199. LinkRes.Add('debug dwarf')
  200. else if target_dbg.id=dbg_codeview then
  201. LinkRes.Add('debug codeview');
  202. if cs_link_separate_dbg_file in current_settings.globalswitches then
  203. LinkRes.Add('option symfile');
  204. { add objectfiles, start with prt0 always }
  205. case current_settings.x86memorymodel of
  206. mm_tiny: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0t','',false)));
  207. mm_small: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0s','',false)));
  208. mm_medium: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0m','',false)));
  209. mm_compact: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0c','',false)));
  210. mm_large: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0l','',false)));
  211. mm_huge: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0h','',false)));
  212. end;
  213. while not ObjectFiles.Empty do
  214. begin
  215. s:=ObjectFiles.GetFirst;
  216. if s<>'' then
  217. LinkRes.Add('file ' + maybequoted(s));
  218. end;
  219. while not StaticLibFiles.Empty do
  220. begin
  221. s:=StaticLibFiles.GetFirst;
  222. if s<>'' then
  223. LinkRes.Add('library '+MaybeQuoted(s));
  224. end;
  225. LinkRes.Add('format windows');
  226. LinkRes.Add('option heapsize='+tostr(heapsize));
  227. if (cs_link_map in current_settings.globalswitches) then
  228. LinkRes.Add('option map='+maybequoted(ChangeFileExt(current_module.exefilename,'.map')));
  229. LinkRes.Add('name ' + maybequoted(current_module.exefilename));
  230. LinkRes.Add('option dosseg');
  231. { Write and Close response }
  232. linkres.writetodisk;
  233. LinkRes.Free;
  234. WriteResponseFile:=True;
  235. end;
  236. constructor TExternalLinkerWin16WLink.Create;
  237. begin
  238. Inherited Create;
  239. { allow duplicated libs (PM) }
  240. SharedLibFiles.doubles:=true;
  241. StaticLibFiles.doubles:=true;
  242. end;
  243. procedure TExternalLinkerWin16WLink.SetDefaultInfo;
  244. begin
  245. with Info do
  246. begin
  247. ExeCmd[1]:='wlink $OPT $RES';
  248. end;
  249. end;
  250. function TExternalLinkerWin16WLink.MakeExecutable: boolean;
  251. var
  252. binstr,
  253. cmdstr : TCmdStr;
  254. success : boolean;
  255. begin
  256. if not(cs_link_nolink in current_settings.globalswitches) then
  257. Message1(exec_i_linking,current_module.exefilename);
  258. { Write used files and libraries and our own tlink script }
  259. WriteResponsefile(false);
  260. { Call linker }
  261. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  262. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  263. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  264. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  265. { Remove ReponseFile }
  266. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  267. DeleteFile(outputexedir+Info.ResName);
  268. MakeExecutable:=success; { otherwise a recursive call to link method }
  269. end;
  270. {****************************************************************************
  271. TInternalLinkerWin16
  272. ****************************************************************************}
  273. procedure TInternalLinkerWin16.DefaultLinkScript;
  274. begin
  275. {todo}
  276. end;
  277. constructor TInternalLinkerWin16.create;
  278. begin
  279. inherited create;
  280. CArObjectReader:=TOmfLibObjectReader;
  281. CExeOutput:=TNewExeOutput;
  282. CObjInput:=TOmfObjInput;
  283. end;
  284. {*****************************************************************************
  285. Initialize
  286. *****************************************************************************}
  287. initialization
  288. RegisterLinker(ld_int_win16,TInternalLinkerWin16);
  289. RegisterLinker(ld_win16,TExternalLinkerWin16WLink);
  290. RegisterImport(system_i8086_win16,TImportLibWin16);
  291. RegisterExport(system_i8086_win16,TExportLibWin16);
  292. RegisterTarget(system_i8086_win16_info);
  293. end.