win_targ.pas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. end;
  29. { sets some flags of the executable }
  30. procedure postprocessexecutable;
  31. implementation
  32. uses
  33. aasm,files,strings,globals,cobjects,systems
  34. {$ifdef GDB}
  35. ,gdb
  36. {$endif}
  37. {$ifdef i386}
  38. ,i386
  39. {$endif}
  40. ;
  41. procedure timportlibwin32.preparelib(const s : string);
  42. begin
  43. if not(assigned(importssection)) then
  44. importssection:=new(paasmoutput,init);
  45. end;
  46. procedure timportlibwin32.importprocedure(const func,module : string;index : longint;const name : string);
  47. var
  48. hp1 : pimportlist;
  49. hp2 : pimported_procedure;
  50. begin
  51. { search for the module }
  52. hp1:=pimportlist(current_module^.imports^.first);
  53. while assigned(hp1) do
  54. begin
  55. if module=hp1^.dllname^ then
  56. break;
  57. hp1:=pimportlist(hp1^.next);
  58. end;
  59. { generate a new item ? }
  60. if not(assigned(hp1)) then
  61. begin
  62. hp1:=new(pimportlist,init(module));
  63. current_module^.imports^.concat(hp1);
  64. end;
  65. hp2:=new(pimported_procedure,init(func,name,index));
  66. hp1^.imported_procedures^.concat(hp2);
  67. end;
  68. procedure timportlibwin32.generatelib;
  69. var
  70. hp1 : pimportlist;
  71. hp2 : pimported_procedure;
  72. l1,l2,l3,l4 : plabel;
  73. r : preference;
  74. begin
  75. hp1:=pimportlist(current_module^.imports^.first);
  76. while assigned(hp1) do
  77. begin
  78. { Insert cuts for smartlinking }
  79. if (cs_smartlink in aktmoduleswitches) then
  80. begin
  81. importssection^.concat(new(pai_cut,init));
  82. codesegment^.concat(new(pai_cut,init));
  83. end;
  84. {$IfDef GDB}
  85. if (cs_debuginfo in aktmoduleswitches) then
  86. codesegment^.concat(new(pai_stab_function_name,init(nil)));
  87. {$EndIf GDB}
  88. { Get labels for the sections }
  89. getlabel(l1);
  90. getlabel(l2);
  91. getlabel(l3);
  92. importssection^.concat(new(pai_section,init_idata(2)));
  93. { pointer to procedure names }
  94. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(l2)))));
  95. { two empty entries follow }
  96. importssection^.concat(new(pai_const,init_32bit(0)));
  97. importssection^.concat(new(pai_const,init_32bit(0)));
  98. { pointer to dll name }
  99. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(l1)))));
  100. { pointer to fixups }
  101. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(l3)))));
  102. { only create one section for each else it will
  103. create a lot of idata* }
  104. { first write the name references }
  105. importssection^.concat(new(pai_section,init_idata(4)));
  106. importssection^.concat(new(pai_label,init(l2)));
  107. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  108. while assigned(hp2) do
  109. begin
  110. getlabel(plabel(hp2^.lab));
  111. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(hp2^.lab)))));
  112. hp2:=pimported_procedure(hp2^.next);
  113. end;
  114. { finalize the names ... }
  115. importssection^.concat(new(pai_const,init_32bit(0)));
  116. { then the addresses and create also the indirect jump }
  117. importssection^.concat(new(pai_section,init_idata(5)));
  118. importssection^.concat(new(pai_label,init(l3)));
  119. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  120. while assigned(hp2) do
  121. begin
  122. getdatalabel(l4);
  123. { create indirect jump }
  124. new(r);
  125. reset_reference(r^);
  126. r^.symbol:=stringdup(lab2str(l4));
  127. { place jump in codesegment }
  128. codesegment^.concat(new(pai_align,init_op(4,$90)));
  129. codesegment^.concat(new(pai_symbol,init_global(hp2^.func^)));
  130. codesegment^.concat(new(pai386,op_ref(A_JMP,S_NO,r)));
  131. { add jump field to importsection }
  132. importssection^.concat(new(pai_label,init(l4)));
  133. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(hp2^.lab)))));
  134. hp2:=pimported_procedure(hp2^.next);
  135. end;
  136. { finalize the addresses }
  137. importssection^.concat(new(pai_const,init_32bit(0)));
  138. { finally the import information }
  139. importssection^.concat(new(pai_section,init_idata(6)));
  140. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  141. while assigned(hp2) do
  142. begin
  143. importssection^.concat(new(pai_label,init(hp2^.lab)));
  144. { the ordinal number }
  145. importssection^.concat(new(pai_const,init_16bit(hp2^.ordnr)));
  146. importssection^.concat(new(pai_string,init(hp2^.name^+#0)));
  147. hp2:=pimported_procedure(hp2^.next);
  148. end;
  149. { create import dll name }
  150. importssection^.concat(new(pai_section,init_idata(7)));
  151. importssection^.concat(new(pai_label,init(l1)));
  152. importssection^.concat(new(pai_string,init(hp1^.dllname^+target_os.sharedlibext+#0)));
  153. hp1:=pimportlist(hp1^.next);
  154. end;
  155. end;
  156. procedure postprocessexecutable;
  157. begin
  158. end;
  159. end.
  160. {
  161. $Log$
  162. Revision 1.7 1998-09-03 17:39:06 florian
  163. + better code for type conversation longint/dword to real type
  164. Revision 1.6 1998/08/10 14:50:38 peter
  165. + localswitches, moduleswitches, globalswitches splitting
  166. Revision 1.5 1998/06/10 10:43:18 peter
  167. * write also the .dll extension (needed for NT)
  168. Revision 1.4 1998/06/08 22:59:56 peter
  169. * smartlinking works for win32
  170. * some defines to exclude some compiler parts
  171. Revision 1.3 1998/06/04 23:52:06 peter
  172. * m68k compiles
  173. + .def file creation moved to gendef.pas so it could also be used
  174. for win32
  175. Revision 1.2 1998/05/06 18:36:55 peter
  176. * tai_section extended with code,data,bss sections and enumerated type
  177. * ident 'compiled by FPC' moved to pmodules
  178. * small fix for smartlink
  179. }