t_win16.pas 9.7 KB

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