impdef.pas 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. unit impdef;
  2. {
  3. C source code of DEWIN Windows disassembler (written by A. Milukov) was
  4. partially used
  5. }
  6. interface
  7. function makedef(const binname,textname:string):longbool;
  8. implementation
  9. var
  10. f:file;
  11. t:text;
  12. TheWord:array[0..1]of char;
  13. PEoffset:cardinal;
  14. loaded:{$ifdef fpc}longint{$else}integer{$endif};
  15. FileCreated:longbool;
  16. function DOSstubOK(var x:cardinal):longbool;
  17. begin
  18. blockread(f,TheWord,2,loaded);
  19. if loaded<>2 then
  20. DOSstubOK:=false
  21. else
  22. begin
  23. DOSstubOK:=TheWord='MZ';
  24. seek(f,$3C);
  25. blockread(f,x,4,loaded);
  26. if(loaded<>4)or(x>filesize(f))then
  27. DOSstubOK:=false;
  28. end;
  29. end;
  30. function isPE(x:cardinal):longbool;
  31. begin
  32. seek(f,x);
  33. blockread(f,TheWord,2,loaded);
  34. isPE:=(loaded=2)and(TheWord='PE');
  35. end;
  36. var
  37. cstring:array[0..127]of char;
  38. function GetEdata(PE:cardinal):longbool;
  39. type
  40. TObjInfo=packed record
  41. ObjName:array[0..7]of char;
  42. VirtSize,
  43. VirtAddr,
  44. RawSize,
  45. RawOffset,
  46. Reloc,
  47. LineNum:cardinal;
  48. RelCount,
  49. LineCount:word;
  50. flags:cardinal;
  51. end;
  52. var
  53. i:cardinal;
  54. ObjOfs:cardinal;
  55. Obj:TObjInfo;
  56. APE_obj,APE_Optsize:word;
  57. ExportRVA:cardinal;
  58. delta:cardinal;
  59. procedure ProcessEdata;
  60. var
  61. j:cardinal;
  62. ulongval:cardinal;
  63. ExpDir:packed record
  64. flag,
  65. stamp:cardinal;
  66. Major,
  67. Minor:word;
  68. Name,
  69. Base,
  70. NumFuncs,
  71. NumNames,
  72. AddrFuncs,
  73. AddrNames,
  74. AddrOrds:cardinal;
  75. end;
  76. begin
  77. with Obj do
  78. begin
  79. seek(f,RawOffset+delta);
  80. blockread(f,ExpDir,sizeof(ExpDir));
  81. seek(f,RawOffset-VirtAddr+ExpDir.Name);
  82. blockread(f,cstring,sizeof(cstring));
  83. for j:=0 to pred(ExpDir.NumNames)do
  84. begin
  85. seek(f,RawOffset-VirtAddr+ExpDir.AddrNames+j*4);
  86. blockread(f,ulongval,4);
  87. seek(f,RawOffset-VirtAddr+ulongval);
  88. blockread(f,cstring,sizeof(cstring));
  89. if not FileCreated then
  90. begin
  91. FileCreated:=true;
  92. rewrite(t);
  93. writeln(t,'EXPORTS');
  94. end;
  95. { do not use the implicit '_' }
  96. writeln(t,cstring,'=',cstring);
  97. end;
  98. end;
  99. end;
  100. begin
  101. GetEdata:=false;
  102. FileCreated:=false;
  103. seek(f,PE+120);
  104. blockread(f,ExportRVA,4);
  105. seek(f,PE+6);
  106. blockread(f,APE_Obj,2);
  107. seek(f,PE+20);
  108. blockread(f,APE_OptSize,2);
  109. ObjOfs:=APE_OptSize+PEoffset+24;
  110. for i:=1 to APE_obj do
  111. begin
  112. seek(f,ObjOfs);
  113. blockread(f,Obj,sizeof(Obj));
  114. inc(ObjOfs,sizeof(Obj));
  115. with Obj do
  116. if(VirtAddr<=ExportRva)and(ExportRva<VirtAddr+VirtSize)then
  117. begin
  118. delta:=ExportRva-VirtAddr;
  119. ProcessEdata;
  120. GetEdata:=true;
  121. end;
  122. end;
  123. end;
  124. function makedef(const binname,textname:string):longbool;
  125. var
  126. OldFileMode:longint;
  127. begin
  128. FileCreated:=false;
  129. assign(f,binname);
  130. assign(t,textname);
  131. OldFileMode:=filemode;
  132. filemode:=0;
  133. reset(f,1);
  134. filemode:=OldFileMode;
  135. if not DOSstubOK(PEoffset)then
  136. makedef:=false
  137. else if not IsPE(PEoffset)then
  138. makedef:=false
  139. else
  140. makedef:=GetEdata(PEoffset);
  141. close(f);
  142. if FileCreated then
  143. close(t);
  144. end;
  145. end. $Log$
  146. end. Revision 1.2 2000-07-13 11:32:43 michael
  147. end. + removed logs
  148. end.
  149. }