win_targ.pas 7.2 KB

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