impdef.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit finds the export defs from PE files
  5. C source code of DEWIN Windows disassembler (written by A. Milukov) was
  6. partially used
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit impdef;
  21. {$i defines.inc}
  22. interface
  23. function makedef(const binname,textname:string):longbool;
  24. implementation
  25. var
  26. f:file;
  27. t:text;
  28. TheWord:array[0..1]of char;
  29. PEoffset:cardinal;
  30. loaded:longint;
  31. FileCreated:longbool;
  32. function DOSstubOK(var x:cardinal):longbool;
  33. begin
  34. blockread(f,TheWord,2,loaded);
  35. if loaded<>2 then
  36. DOSstubOK:=false
  37. else
  38. begin
  39. DOSstubOK:=TheWord='MZ';
  40. seek(f,$3C);
  41. blockread(f,x,4,loaded);
  42. if(loaded<>4)or(x>filesize(f))then
  43. DOSstubOK:=false;
  44. end;
  45. end;
  46. function isPE(x:cardinal):longbool;
  47. begin
  48. seek(f,x);
  49. blockread(f,TheWord,2,loaded);
  50. isPE:=(loaded=2)and(TheWord='PE');
  51. end;
  52. var
  53. cstring:array[0..127]of char;
  54. function GetEdata(PE:cardinal):longbool;
  55. type
  56. TObjInfo=packed record
  57. ObjName:array[0..7]of char;
  58. VirtSize,
  59. VirtAddr,
  60. RawSize,
  61. RawOffset,
  62. Reloc,
  63. LineNum:cardinal;
  64. RelCount,
  65. LineCount:word;
  66. flags:cardinal;
  67. end;
  68. var
  69. i:cardinal;
  70. ObjOfs:cardinal;
  71. Obj:TObjInfo;
  72. APE_obj,APE_Optsize:word;
  73. ExportRVA:cardinal;
  74. delta:cardinal;
  75. procedure ProcessEdata;
  76. var
  77. j:cardinal;
  78. ulongval:cardinal;
  79. ExpDir:packed record
  80. flag,
  81. stamp:cardinal;
  82. Major,
  83. Minor:word;
  84. Name,
  85. Base,
  86. NumFuncs,
  87. NumNames,
  88. AddrFuncs,
  89. AddrNames,
  90. AddrOrds:cardinal;
  91. end;
  92. begin
  93. with Obj do
  94. begin
  95. seek(f,RawOffset+delta);
  96. blockread(f,ExpDir,sizeof(ExpDir));
  97. seek(f,RawOffset-VirtAddr+ExpDir.Name);
  98. blockread(f,cstring,sizeof(cstring));
  99. for j:=0 to pred(ExpDir.NumNames)do
  100. begin
  101. seek(f,RawOffset-VirtAddr+ExpDir.AddrNames+j*4);
  102. blockread(f,ulongval,4);
  103. seek(f,RawOffset-VirtAddr+ulongval);
  104. blockread(f,cstring,sizeof(cstring));
  105. if not FileCreated then
  106. begin
  107. FileCreated:=true;
  108. rewrite(t);
  109. writeln(t,'EXPORTS');
  110. end;
  111. { do not use the implicit '_' }
  112. writeln(t,cstring,'=',cstring);
  113. end;
  114. end;
  115. end;
  116. begin
  117. GetEdata:=false;
  118. FileCreated:=false;
  119. seek(f,PE+120);
  120. blockread(f,ExportRVA,4);
  121. seek(f,PE+6);
  122. blockread(f,APE_Obj,2);
  123. seek(f,PE+20);
  124. blockread(f,APE_OptSize,2);
  125. ObjOfs:=APE_OptSize+PEoffset+24;
  126. for i:=1 to APE_obj do
  127. begin
  128. seek(f,ObjOfs);
  129. blockread(f,Obj,sizeof(Obj));
  130. inc(ObjOfs,sizeof(Obj));
  131. with Obj do
  132. if(VirtAddr<=ExportRva)and(ExportRva<VirtAddr+VirtSize)then
  133. begin
  134. delta:=ExportRva-VirtAddr;
  135. ProcessEdata;
  136. GetEdata:=true;
  137. end;
  138. end;
  139. end;
  140. function makedef(const binname,textname:string):longbool;
  141. var
  142. OldFileMode:longint;
  143. begin
  144. FileCreated:=false;
  145. assign(f,binname);
  146. assign(t,textname);
  147. OldFileMode:=filemode;
  148. filemode:=0;
  149. reset(f,1);
  150. filemode:=OldFileMode;
  151. if not DOSstubOK(PEoffset)then
  152. makedef:=false
  153. else if not IsPE(PEoffset)then
  154. makedef:=false
  155. else
  156. makedef:=GetEdata(PEoffset);
  157. close(f);
  158. if FileCreated then
  159. close(t);
  160. end;
  161. end.
  162. {
  163. $Log$
  164. Revision 1.4 2000-11-20 13:58:19 pierre
  165. * missing end. added
  166. Revision 1.3 2000/09/24 15:06:17 peter
  167. * use defines.inc
  168. Revision 1.2 2000/07/13 11:32:43 michael
  169. + removed logs
  170. }