win_targ.pas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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
  32. {$ifdef i386}
  33. ,i386
  34. {$endif}
  35. ;
  36. procedure timportlibwin32.preparelib(const s : string);
  37. begin
  38. if not(assigned(importssection)) then
  39. importssection:=new(paasmoutput,init);
  40. end;
  41. procedure timportlibwin32.importprocedure(const func,module : string;index : longint;const name : string);
  42. var
  43. hp1 : pimportlist;
  44. hp2 : pimported_procedure;
  45. begin
  46. { search for the module }
  47. hp1:=pimportlist(current_module^.imports^.first);
  48. while assigned(hp1) do
  49. begin
  50. if module=hp1^.dllname^ then
  51. break;
  52. hp1:=pimportlist(hp1^.next);
  53. end;
  54. { generate a new item ? }
  55. if not(assigned(hp1)) then
  56. begin
  57. hp1:=new(pimportlist,init(module));
  58. current_module^.imports^.concat(hp1);
  59. end;
  60. hp2:=new(pimported_procedure,init(func,name,index));
  61. hp1^.imported_procedures^.concat(hp2);
  62. end;
  63. procedure timportlibwin32.generatelib;
  64. var
  65. hp1 : pimportlist;
  66. hp2 : pimported_procedure;
  67. l1,l2,l3,l4 : plabel;
  68. r : preference;
  69. begin
  70. hp1:=pimportlist(current_module^.imports^.first);
  71. while assigned(hp1) do
  72. begin
  73. getlabel(l1);
  74. getlabel(l2);
  75. getlabel(l3);
  76. { create import directory entry }
  77. importssection^.concat(new(pai_section,init_idata(2)));
  78. { pointer to procedure names }
  79. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str
  80. (l2)))));
  81. { two empty entries follow }
  82. importssection^.concat(new(pai_const,init_32bit(0)));
  83. importssection^.concat(new(pai_const,init_32bit(0)));
  84. { pointer to dll name }
  85. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str
  86. (l1)))));
  87. { pointer to fixups }
  88. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str
  89. (l3)))));
  90. { now walk through all imported procedures }
  91. { we could that do in one while loop, but }
  92. { this would give too much idata* entries }
  93. { first write the name references }
  94. importssection^.concat(new(pai_section,init_idata(4)));
  95. importssection^.concat(new(pai_label,init(l2)));
  96. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  97. while assigned(hp2) do
  98. begin
  99. getlabel(plabel(hp2^.lab));
  100. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str
  101. (hp2^.lab)))));
  102. hp2:=pimported_procedure(hp2^.next);
  103. end;
  104. { finalize the names ... }
  105. importssection^.concat(new(pai_const,init_32bit(0)));
  106. { then the addresses and create also the indirect jump }
  107. importssection^.concat(new(pai_section,init_idata(5)));
  108. importssection^.concat(new(pai_label,init(l3)));
  109. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  110. while assigned(hp2) do
  111. begin
  112. getlabel(l4);
  113. { text segment should be aligned }
  114. codesegment^.concat(new(pai_align,init_op(4,$90)));
  115. codesegment^.concat(new(pai_symbol,init_global(hp2^.func^)));
  116. { the indirect jump }
  117. new(r);
  118. reset_reference(r^);
  119. r^.symbol:=stringdup(lab2str(l4));
  120. {$ifdef i386}
  121. codesegment^.concat(new(pai386,op_ref(A_JMP,S_NO,r)));
  122. {$endif}
  123. importssection^.concat(new(pai_label,init(l4)));
  124. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str
  125. (hp2^.lab)))));
  126. hp2:=pimported_procedure(hp2^.next);
  127. end;
  128. { finalize the addresses }
  129. importssection^.concat(new(pai_const,init_32bit(0)));
  130. { finally the import information }
  131. importssection^.concat(new(pai_section,init_idata(6)));
  132. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  133. while assigned(hp2) do
  134. begin
  135. importssection^.concat(new(pai_label,init(hp2^.lab)));
  136. { the ordinal number }
  137. importssection^.concat(new(pai_const,init_16bit(hp2^.ordnr)));
  138. importssection^.concat(new(pai_string,init(hp2^.name^+#0)));
  139. hp2:=pimported_procedure(hp2^.next);
  140. end;
  141. { create import dll name }
  142. importssection^.concat(new(pai_section,init_idata(7)));
  143. importssection^.concat(new(pai_label,init(l1)));
  144. importssection^.concat(new(pai_string,init(hp1^.dllname^+#0)));
  145. hp1:=pimportlist(hp1^.next);
  146. end;
  147. end;
  148. end.
  149. {
  150. $Log$
  151. Revision 1.3 1998-06-04 23:52:06 peter
  152. * m68k compiles
  153. + .def file creation moved to gendef.pas so it could also be used
  154. for win32
  155. Revision 1.2 1998/05/06 18:36:55 peter
  156. * tai_section extended with code,data,bss sections and enumerated type
  157. * ident 'compiled by FPC' moved to pmodules
  158. * small fix for smartlink
  159. }