win_targ.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. {$ifdef m68k}
  36. ,m68k
  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. getlabel(l1);
  77. getlabel(l2);
  78. getlabel(l3);
  79. { create import directory entry }
  80. importssection^.concat(new(pai_section,init_idata(2)));
  81. { pointer to procedure names }
  82. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str
  83. (l2)))));
  84. { two empty entries follow }
  85. importssection^.concat(new(pai_const,init_32bit(0)));
  86. importssection^.concat(new(pai_const,init_32bit(0)));
  87. { pointer to dll name }
  88. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str
  89. (l1)))));
  90. { pointer to fixups }
  91. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str
  92. (l3)))));
  93. { now walk through all imported procedures }
  94. { we could that do in one while loop, but }
  95. { this would give too much idata* entries }
  96. { first write the name references }
  97. importssection^.concat(new(pai_section,init_idata(4)));
  98. importssection^.concat(new(pai_label,init(l2)));
  99. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  100. while assigned(hp2) do
  101. begin
  102. getlabel(plabel(hp2^.lab));
  103. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str
  104. (hp2^.lab)))));
  105. hp2:=pimported_procedure(hp2^.next);
  106. end;
  107. { finalize the names ... }
  108. importssection^.concat(new(pai_const,init_32bit(0)));
  109. { then the addresses and create also the indirect jump }
  110. importssection^.concat(new(pai_section,init_idata(5)));
  111. importssection^.concat(new(pai_label,init(l3)));
  112. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  113. while assigned(hp2) do
  114. begin
  115. getlabel(l4);
  116. { text segment should be aligned }
  117. codesegment^.concat(new(pai_align,init_op(4,$90)));
  118. codesegment^.concat(new(pai_symbol,init_global(hp2^.func^)));
  119. { the indirect jump }
  120. new(r);
  121. reset_reference(r^);
  122. r^.symbol:=stringdup(lab2str(l4));
  123. {$ifdef i386}
  124. codesegment^.concat(new(pai386,op_ref(A_JMP,S_NO,r)));
  125. {$endif}
  126. importssection^.concat(new(pai_label,init(l4)));
  127. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str
  128. (hp2^.lab)))));
  129. hp2:=pimported_procedure(hp2^.next);
  130. end;
  131. { finalize the addresses }
  132. importssection^.concat(new(pai_const,init_32bit(0)));
  133. { finally the import information }
  134. importssection^.concat(new(pai_section,init_idata(6)));
  135. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  136. while assigned(hp2) do
  137. begin
  138. importssection^.concat(new(pai_label,init(hp2^.lab)));
  139. { the ordinal number }
  140. importssection^.concat(new(pai_const,init_16bit(hp2^.ordnr)));
  141. importssection^.concat(new(pai_string,init(hp2^.name^+#0)));
  142. hp2:=pimported_procedure(hp2^.next);
  143. end;
  144. { create import dll name }
  145. importssection^.concat(new(pai_section,init_idata(7)));
  146. importssection^.concat(new(pai_label,init(l1)));
  147. importssection^.concat(new(pai_string,init(hp1^.dllname^+#0)));
  148. hp1:=pimportlist(hp1^.next);
  149. end;
  150. end;
  151. end.
  152. {
  153. $Log$
  154. Revision 1.2 1998-05-06 18:36:55 peter
  155. * tai_section extended with code,data,bss sections and enumerated type
  156. * ident 'compiled by FPC' moved to pmodules
  157. * small fix for smartlink
  158. }