2
0

t_win16.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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,fmodule,i_win16,
  27. link,aasmbase,cpuinfo,
  28. omfbase,ogbase,ogomf,owomflib;
  29. type
  30. { TImportLibWin16 }
  31. TImportLibWin16=class(timportlib)
  32. public
  33. procedure generatelib;override;
  34. end;
  35. { the (Open) Watcom linker }
  36. TExternalLinkerWin16WLink=class(texternallinker)
  37. private
  38. Function WriteResponseFile(isdll:boolean) : Boolean;
  39. public
  40. constructor Create;override;
  41. procedure SetDefaultInfo;override;
  42. function MakeExecutable:boolean;override;
  43. end;
  44. {****************************************************************************
  45. TImportLibWin16
  46. ****************************************************************************}
  47. procedure TImportLibWin16.generatelib;
  48. var
  49. ObjWriter: TOmfLibObjectWriter;
  50. ObjOutput: TOmfObjOutput;
  51. i,j: Integer;
  52. ImportLibrary: TImportLibrary;
  53. ImportSymbol: TImportSymbol;
  54. AsmPrefix: String;
  55. procedure AddImport(const dllname,afuncname,mangledname:string;ordnr:longint;isvar:boolean);
  56. begin
  57. ObjOutput.startObjectfile(mangledname);
  58. ObjOutput.WriteDllImport(dllname,afuncname,mangledname,ordnr,isvar);
  59. ObjOutput.Writer.closefile;
  60. end;
  61. begin
  62. AsmPrefix:='imp'+Lower(current_module.modulename^);
  63. current_module.linkotherstaticlibs.add(current_module.importlibfilename,link_always);
  64. ObjWriter:=TOmfLibObjectWriter.CreateAr(current_module.importlibfilename,32);
  65. ObjOutput:=TOmfObjOutput.Create(ObjWriter);
  66. for i:=0 to current_module.ImportLibraryList.Count-1 do
  67. begin
  68. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  69. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  70. begin
  71. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  72. AddImport(ImportLibrary.Name,ImportSymbol.Name,ImportSymbol.MangledName,ImportSymbol.OrdNr,ImportSymbol.IsVar);
  73. end;
  74. end;
  75. ObjOutput.Free;
  76. ObjWriter.Free;
  77. end;
  78. {****************************************************************************
  79. TExternalLinkerWin16WLink
  80. ****************************************************************************}
  81. function TExternalLinkerWin16WLink.WriteResponseFile(isdll: boolean): Boolean;
  82. Var
  83. linkres : TLinkRes;
  84. s : string;
  85. i: Integer;
  86. begin
  87. WriteResponseFile:=False;
  88. { Open link.res file }
  89. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  90. { Add all options to link.res instead of passing them via command line:
  91. DOS command line is limited to 126 characters! }
  92. LinkRes.Add('option quiet');
  93. if target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
  94. LinkRes.Add('debug dwarf');
  95. { add objectfiles, start with prt0 always }
  96. case current_settings.x86memorymodel of
  97. mm_tiny: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0t','',false)));
  98. mm_small: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0s','',false)));
  99. mm_medium: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0m','',false)));
  100. mm_compact: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0c','',false)));
  101. mm_large: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0l','',false)));
  102. mm_huge: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0h','',false)));
  103. end;
  104. while not ObjectFiles.Empty do
  105. begin
  106. s:=ObjectFiles.GetFirst;
  107. if s<>'' then
  108. LinkRes.Add('file ' + maybequoted(s));
  109. end;
  110. while not StaticLibFiles.Empty do
  111. begin
  112. s:=StaticLibFiles.GetFirst;
  113. if s<>'' then
  114. LinkRes.Add('library '+MaybeQuoted(s));
  115. end;
  116. LinkRes.Add('format windows');
  117. LinkRes.Add('option heapsize='+tostr(heapsize));
  118. if (cs_link_map in current_settings.globalswitches) then
  119. LinkRes.Add('option map='+maybequoted(ChangeFileExt(current_module.exefilename,'.map')));
  120. LinkRes.Add('name ' + maybequoted(current_module.exefilename));
  121. LinkRes.Add('option dosseg');
  122. { Write and Close response }
  123. linkres.writetodisk;
  124. LinkRes.Free;
  125. WriteResponseFile:=True;
  126. end;
  127. constructor TExternalLinkerWin16WLink.Create;
  128. begin
  129. Inherited Create;
  130. { allow duplicated libs (PM) }
  131. SharedLibFiles.doubles:=true;
  132. StaticLibFiles.doubles:=true;
  133. end;
  134. procedure TExternalLinkerWin16WLink.SetDefaultInfo;
  135. begin
  136. with Info do
  137. begin
  138. ExeCmd[1]:='wlink $OPT $RES';
  139. end;
  140. end;
  141. function TExternalLinkerWin16WLink.MakeExecutable: boolean;
  142. var
  143. binstr,
  144. cmdstr : TCmdStr;
  145. success : boolean;
  146. begin
  147. if not(cs_link_nolink in current_settings.globalswitches) then
  148. Message1(exec_i_linking,current_module.exefilename);
  149. { Write used files and libraries and our own tlink script }
  150. WriteResponsefile(false);
  151. { Call linker }
  152. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  153. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  154. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  155. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  156. { Remove ReponseFile }
  157. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  158. DeleteFile(outputexedir+Info.ResName);
  159. MakeExecutable:=success; { otherwise a recursive call to link method }
  160. end;
  161. {*****************************************************************************
  162. Initialize
  163. *****************************************************************************}
  164. initialization
  165. RegisterLinker(ld_win16,TExternalLinkerWin16WLink);
  166. RegisterImport(system_i8086_win16,TImportLibWin16);
  167. RegisterTarget(system_i8086_win16_info);
  168. end.