win_targ.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Florian Klaempfl
  4. This unit implements some support routines for the win32 target like
  5. import/export handling
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit win_targ;
  20. interface
  21. uses import;
  22. type
  23. pimportlibwin32=^timportlibwin32;
  24. timportlibwin32=object(timportlib)
  25. procedure preparelib(const s:string);virtual;
  26. procedure importprocedure(const func,module:string;index:longint;const name:string);virtual;
  27. procedure generatelib;virtual;
  28. procedure generatesmartlib;
  29. end;
  30. { sets some flags of the executable }
  31. procedure postprocessexecutable;
  32. implementation
  33. uses
  34. aasm,files,strings,globals,cobjects,systems
  35. {$ifdef GDB}
  36. ,gdb
  37. {$endif}
  38. {$ifdef i386}
  39. ,i386
  40. {$endif}
  41. ;
  42. procedure timportlibwin32.preparelib(const s : string);
  43. begin
  44. if not(assigned(importssection)) then
  45. importssection:=new(paasmoutput,init);
  46. end;
  47. procedure timportlibwin32.importprocedure(const func,module : string;index : longint;const name : string);
  48. var
  49. hp1 : pimportlist;
  50. hp2 : pimported_procedure;
  51. begin
  52. { search for the module }
  53. hp1:=pimportlist(current_module^.imports^.first);
  54. while assigned(hp1) do
  55. begin
  56. if module=hp1^.dllname^ then
  57. break;
  58. hp1:=pimportlist(hp1^.next);
  59. end;
  60. { generate a new item ? }
  61. if not(assigned(hp1)) then
  62. begin
  63. hp1:=new(pimportlist,init(module));
  64. current_module^.imports^.concat(hp1);
  65. end;
  66. hp2:=new(pimported_procedure,init(func,name,index));
  67. hp1^.imported_procedures^.concat(hp2);
  68. end;
  69. procedure timportlibwin32.generatesmartlib;
  70. var
  71. hp1 : pimportlist;
  72. hp2 : pimported_procedure;
  73. lhead,lname,lcode,
  74. lidata4,lidata5 : plabel;
  75. r : preference;
  76. begin
  77. hp1:=pimportlist(current_module^.imports^.first);
  78. while assigned(hp1) do
  79. begin
  80. importssection^.concat(new(pai_cut,init));
  81. codesegment^.concat(new(pai_cut,init));
  82. { create header for this importmodule }
  83. { Get labels for the sections }
  84. getdatalabel(lhead);
  85. getdatalabel(lname);
  86. getlabel(lidata4);
  87. getlabel(lidata5);
  88. importssection^.concat(new(pai_section,init_idata(2)));
  89. importssection^.concat(new(pai_label,init(lhead)));
  90. { pointer to procedure names }
  91. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(lidata4)))));
  92. { two empty entries follow }
  93. importssection^.concat(new(pai_const,init_32bit(0)));
  94. importssection^.concat(new(pai_const,init_32bit(0)));
  95. { pointer to dll name }
  96. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(lname)))));
  97. { pointer to fixups }
  98. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(lidata5)))));
  99. { first write the name references }
  100. importssection^.concat(new(pai_section,init_idata(4)));
  101. importssection^.concat(new(pai_const,init_32bit(0)));
  102. importssection^.concat(new(pai_label,init(lidata4)));
  103. { then the addresses and create also the indirect jump }
  104. importssection^.concat(new(pai_section,init_idata(5)));
  105. importssection^.concat(new(pai_const,init_32bit(0)));
  106. importssection^.concat(new(pai_label,init(lidata5)));
  107. { write final section }
  108. importssection^.concat(new(pai_cut,init_end));
  109. { end of name references }
  110. importssection^.concat(new(pai_section,init_idata(4)));
  111. importssection^.concat(new(pai_const,init_32bit(0)));
  112. { end if addresses }
  113. importssection^.concat(new(pai_section,init_idata(5)));
  114. importssection^.concat(new(pai_const,init_32bit(0)));
  115. { dllname }
  116. importssection^.concat(new(pai_section,init_idata(7)));
  117. importssection^.concat(new(pai_label,init(lname)));
  118. importssection^.concat(new(pai_string,init(hp1^.dllname^+target_os.sharedlibext+#0)));
  119. { create procedures }
  120. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  121. while assigned(hp2) do
  122. begin
  123. { insert cuts }
  124. importssection^.concat(new(pai_cut,init));
  125. { create indirect jump }
  126. getlabel(lcode);
  127. new(r);
  128. reset_reference(r^);
  129. r^.symbol:=stringdup(lab2str(lcode));
  130. { place jump in codesegment, insert a code section in the
  131. importsection to reduce the amount of .s files (PFV) }
  132. importssection^.concat(new(pai_section,init(sec_code)));
  133. {$IfDef GDB}
  134. if (cs_debuginfo in aktmoduleswitches) then
  135. importssection^.concat(new(pai_stab_function_name,init(nil)));
  136. {$EndIf GDB}
  137. importssection^.concat(new(pai_align,init_op(4,$90)));
  138. importssection^.concat(new(pai_symbol,init_global(hp2^.func^)));
  139. importssection^.concat(new(pai386,op_ref(A_JMP,S_NO,r)));
  140. { create head link }
  141. importssection^.concat(new(pai_section,init_idata(7)));
  142. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(lhead)))));
  143. { fixup }
  144. getlabel(plabel(hp2^.lab));
  145. importssection^.concat(new(pai_section,init_idata(4)));
  146. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(hp2^.lab)))));
  147. { add jump field to importsection }
  148. importssection^.concat(new(pai_section,init_idata(5)));
  149. importssection^.concat(new(pai_label,init(lcode)));
  150. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(hp2^.lab)))));
  151. { finally the import information }
  152. importssection^.concat(new(pai_section,init_idata(6)));
  153. importssection^.concat(new(pai_label,init(hp2^.lab)));
  154. importssection^.concat(new(pai_const,init_16bit(hp2^.ordnr)));
  155. importssection^.concat(new(pai_string,init(hp2^.name^+#0)));
  156. hp2:=pimported_procedure(hp2^.next);
  157. end;
  158. hp1:=pimportlist(hp1^.next);
  159. end;
  160. end;
  161. procedure timportlibwin32.generatelib;
  162. var
  163. hp1 : pimportlist;
  164. hp2 : pimported_procedure;
  165. l1,l2,l3,l4 : plabel;
  166. r : preference;
  167. begin
  168. if (cs_smartlink in aktmoduleswitches) then
  169. begin
  170. generatesmartlib;
  171. exit;
  172. end;
  173. hp1:=pimportlist(current_module^.imports^.first);
  174. while assigned(hp1) do
  175. begin
  176. { Insert cuts for smartlinking }
  177. if (cs_smartlink in aktmoduleswitches) then
  178. begin
  179. importssection^.concat(new(pai_cut,init));
  180. codesegment^.concat(new(pai_cut,init));
  181. end;
  182. {$IfDef GDB}
  183. if (cs_debuginfo in aktmoduleswitches) then
  184. codesegment^.concat(new(pai_stab_function_name,init(nil)));
  185. {$EndIf GDB}
  186. { Get labels for the sections }
  187. getlabel(l1);
  188. getlabel(l2);
  189. getlabel(l3);
  190. importssection^.concat(new(pai_section,init_idata(2)));
  191. { pointer to procedure names }
  192. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(l2)))));
  193. { two empty entries follow }
  194. importssection^.concat(new(pai_const,init_32bit(0)));
  195. importssection^.concat(new(pai_const,init_32bit(0)));
  196. { pointer to dll name }
  197. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(l1)))));
  198. { pointer to fixups }
  199. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(l3)))));
  200. { only create one section for each else it will
  201. create a lot of idata* }
  202. { first write the name references }
  203. importssection^.concat(new(pai_section,init_idata(4)));
  204. importssection^.concat(new(pai_label,init(l2)));
  205. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  206. while assigned(hp2) do
  207. begin
  208. getlabel(plabel(hp2^.lab));
  209. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(hp2^.lab)))));
  210. hp2:=pimported_procedure(hp2^.next);
  211. end;
  212. { finalize the names ... }
  213. importssection^.concat(new(pai_const,init_32bit(0)));
  214. { then the addresses and create also the indirect jump }
  215. importssection^.concat(new(pai_section,init_idata(5)));
  216. importssection^.concat(new(pai_label,init(l3)));
  217. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  218. while assigned(hp2) do
  219. begin
  220. getdatalabel(l4);
  221. { create indirect jump }
  222. new(r);
  223. reset_reference(r^);
  224. r^.symbol:=stringdup(lab2str(l4));
  225. { place jump in codesegment }
  226. codesegment^.concat(new(pai_align,init_op(4,$90)));
  227. codesegment^.concat(new(pai_symbol,init_global(hp2^.func^)));
  228. codesegment^.concat(new(pai386,op_ref(A_JMP,S_NO,r)));
  229. { add jump field to importsection }
  230. importssection^.concat(new(pai_label,init(l4)));
  231. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(hp2^.lab)))));
  232. hp2:=pimported_procedure(hp2^.next);
  233. end;
  234. { finalize the addresses }
  235. importssection^.concat(new(pai_const,init_32bit(0)));
  236. { finally the import information }
  237. importssection^.concat(new(pai_section,init_idata(6)));
  238. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  239. while assigned(hp2) do
  240. begin
  241. importssection^.concat(new(pai_label,init(hp2^.lab)));
  242. { the ordinal number }
  243. importssection^.concat(new(pai_const,init_16bit(hp2^.ordnr)));
  244. importssection^.concat(new(pai_string,init(hp2^.name^+#0)));
  245. hp2:=pimported_procedure(hp2^.next);
  246. end;
  247. { create import dll name }
  248. importssection^.concat(new(pai_section,init_idata(7)));
  249. importssection^.concat(new(pai_label,init(l1)));
  250. importssection^.concat(new(pai_string,init(hp1^.dllname^+target_os.sharedlibext+#0)));
  251. hp1:=pimportlist(hp1^.next);
  252. end;
  253. end;
  254. procedure postprocessexecutable;
  255. begin
  256. end;
  257. end.
  258. {
  259. $Log$
  260. Revision 1.8 1998-09-07 18:33:35 peter
  261. + smartlinking for win95 imports
  262. Revision 1.7 1998/09/03 17:39:06 florian
  263. + better code for type conversation longint/dword to real type
  264. Revision 1.6 1998/08/10 14:50:38 peter
  265. + localswitches, moduleswitches, globalswitches splitting
  266. Revision 1.5 1998/06/10 10:43:18 peter
  267. * write also the .dll extension (needed for NT)
  268. Revision 1.4 1998/06/08 22:59:56 peter
  269. * smartlinking works for win32
  270. * some defines to exclude some compiler parts
  271. Revision 1.3 1998/06/04 23:52:06 peter
  272. * m68k compiles
  273. + .def file creation moved to gendef.pas so it could also be used
  274. for win32
  275. Revision 1.2 1998/05/06 18:36:55 peter
  276. * tai_section extended with code,data,bss sections and enumerated type
  277. * ident 'compiled by FPC' moved to pmodules
  278. * small fix for smartlink
  279. }