impdef.pas 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Pavel
  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 imtdef;
  21. {$ifndef STANDALONE}
  22. {$i fpcdefs.inc}
  23. {$endif}
  24. interface
  25. uses
  26. {$ifdef Delphi}
  27. SysUtils,
  28. Dmisc;
  29. {$else}
  30. Dos;
  31. {$endif}
  32. var
  33. asw_name,
  34. arw_name : string;
  35. function makedef(const binname,
  36. {$IFDEF STANDALONE}
  37. textname,
  38. {$ENDIF}
  39. libname:string):longbool;
  40. implementation
  41. {$IFDEF STANDALONE}
  42. var
  43. __textname : string;
  44. const
  45. kind : array[longbool] of pchar=('',' DATA');
  46. {$ENDIF}
  47. var
  48. f:file;
  49. {$IFDEF STANDALONE}
  50. t:text;
  51. FileCreated:longbool;
  52. {$ENDIF}
  53. lname:string;
  54. impname:string;
  55. TheWord:array[0..1]of char;
  56. PEoffset:cardinal;
  57. loaded:longint;
  58. function DOSstubOK(var x:longint):longbool;
  59. begin
  60. blockread(f,TheWord,2,loaded);
  61. if loaded<>2 then
  62. DOSstubOK:=false
  63. else
  64. begin
  65. DOSstubOK:=TheWord='MZ';
  66. seek(f,$3C);
  67. blockread(f,x,4,loaded);
  68. if(loaded<>4)or(x>filesize(f))then
  69. DOSstubOK:=false;
  70. end;
  71. end;
  72. function isPE(x:longint):longbool;
  73. begin
  74. seek(f,x);
  75. blockread(f,TheWord,2,loaded);
  76. isPE:=(loaded=2)and(TheWord='PE');
  77. end;
  78. var
  79. cstring : array[0..127]of char;
  80. function GetEdata(PE:cardinal):longbool;
  81. type
  82. TObjInfo=packed record
  83. ObjName:array[0..7]of char;
  84. VirtSize,
  85. VirtAddr,
  86. RawSize,
  87. RawOffset,
  88. Reloc,
  89. LineNum:cardinal;
  90. RelCount,
  91. LineCount:word;
  92. flags:cardinal;
  93. end;
  94. var
  95. i:cardinal;
  96. ObjOfs:cardinal;
  97. Obj:TObjInfo;
  98. APE_obj,APE_Optsize:word;
  99. ExportRVA:cardinal;
  100. delta:cardinal;
  101. const
  102. IMAGE_SCN_CNT_CODE=$00000020;
  103. const
  104. {$ifdef unix}
  105. DirSep = '/';
  106. {$else}
  107. {$ifdef amiga}
  108. DirSep = '/';
  109. {$else}
  110. DirSep = '\';
  111. {$endif}
  112. {$endif}
  113. var
  114. path:string;
  115. _d:dirstr;
  116. _n:namestr;
  117. _e:extstr;
  118. common_created:longbool;
  119. procedure cleardir(const s,ext:string);
  120. var
  121. ff:file;
  122. dir:searchrec;
  123. attr:word;
  124. begin
  125. findfirst(s+dirsep+ext,anyfile,dir);
  126. while (doserror=0) do
  127. begin
  128. assign(ff,s+dirsep+dir.name);
  129. GetFattr(ff,attr);
  130. if not((DOSError<>0)or(Attr and Directory<>0))then
  131. Erase(ff);
  132. findnext(dir);
  133. end;
  134. findclose(dir);
  135. end;
  136. procedure CreateTempDir(const s:string);
  137. var
  138. attr:word;
  139. ff:file;
  140. begin
  141. assign(ff,s);
  142. GetFattr(ff,attr);
  143. if DosError=0 then
  144. begin
  145. cleardir(s,'*.sw');
  146. cleardir(s,'*.swo');
  147. end
  148. else
  149. begin
  150. {$I-}
  151. mkdir(s);
  152. {$I+}
  153. if ioresult<>0 then;
  154. end;
  155. end;
  156. procedure call_asw(const name:string);
  157. begin
  158. writeln(name);
  159. exec(asw_name,'-o '+name+'o '+name);
  160. end;
  161. procedure call_arw;
  162. var
  163. f:file;
  164. attr:word;
  165. begin
  166. {$IFDEF STANDALONE}
  167. if impname='' then
  168. exit;
  169. {$ENDIF}
  170. assign(f,impname);
  171. GetFAttr(f,attr);
  172. If DOSError=0 then
  173. erase(f);
  174. exec(arw_name,'rs '+impname+' '+path+dirsep+'*.swo');
  175. cleardir(path,'*.sw');
  176. cleardir(path,'*.swo');
  177. {$i-}
  178. RmDir(path);
  179. {$i+}
  180. if ioresult<>0 then;
  181. end;
  182. procedure makeasm(index:cardinal;name:pchar;isData:longbool);
  183. type
  184. tt=array[1..1]of pchar;
  185. pt=^tt;
  186. const
  187. fn_template:array[1..24]of pchar=(
  188. '.section .idata$2',
  189. '.rva .L4',
  190. '.long 0,0',
  191. '.rva ',
  192. '.rva .L5',
  193. '.section .idata$4',
  194. '.L4:',
  195. '.rva .L6',
  196. '.long 0',
  197. '.section .idata$5',
  198. '.L5:',
  199. '.text',
  200. '.globl ',
  201. ':',
  202. 'jmp *.L7',
  203. '.balign 4,144',
  204. '.section .idata$5',
  205. '.L7:',
  206. '.rva .L6',
  207. '.long 0',
  208. '.section .idata$6',
  209. '.L6:',
  210. '.short 0',
  211. '.ascii "\000"'
  212. );
  213. var_template:array[1..19]of pchar=(
  214. '.section .idata$2',
  215. '.rva .L7',
  216. '.long 0,0',
  217. '.rva ',
  218. '.rva .L8',
  219. '.section .idata$4',
  220. '.L7:',
  221. '.rva .L9',
  222. '.long 0',
  223. '.section .idata$5',
  224. '.L8:',
  225. '.globl ',
  226. ':',
  227. '.rva .L9',
  228. '.long 0',
  229. '.section .idata$6',
  230. '.L9:',
  231. '.short 0',
  232. '.ascii "\000"'
  233. );
  234. __template:array[longbool]of pointer=(@fn_template,@var_template);
  235. common_part:array[1..5]of pchar=(
  236. '.balign 2,0',
  237. '.section .idata$7',
  238. '.globl ',
  239. ':',
  240. '.ascii "\000"'
  241. );
  242. posit:array[longbool,1..4]of longint=((4,13,14,24),(4,12,13,19));
  243. var
  244. template:array[longbool]of pt absolute __template;
  245. f:text;
  246. s:string;
  247. i:longint;
  248. n:string;
  249. common_name,asmout:string;
  250. __d:dirstr;
  251. __n:namestr;
  252. __x:extstr;
  253. begin
  254. if not common_created then
  255. begin
  256. common_name:='_$'+_n+'@common';
  257. asmout:=path+dirsep+'0.sw';
  258. assign(f,asmout);
  259. rewrite(f);
  260. for i:=1 to 5 do
  261. begin
  262. s:=StrPas(Common_part[i]);
  263. case i of
  264. 3:
  265. s:=s+common_name;
  266. 4:
  267. s:=common_name+s;
  268. 5:
  269. begin
  270. fsplit(lname,__d,__n,__x);
  271. insert(__n+__x,s,9);
  272. end;
  273. end;
  274. writeln(f,s);
  275. end;
  276. close(f);
  277. call_asw(asmout);
  278. common_created:=true;
  279. end;
  280. n:=strpas(name);
  281. str(succ(index):0,s);
  282. asmout:=path+dirsep+s+'.sw';
  283. assign(f,asmout);
  284. rewrite(f);
  285. for i:=1 to posit[isData,4]do
  286. begin
  287. s:=StrPas(template[isData]^[i]);
  288. if i=posit[isData,1]then
  289. s:=s+common_name
  290. else if i=posit[isData,2]then
  291. s:=s+n
  292. else if i=posit[isData,3]then
  293. s:=n+s
  294. else if i=posit[isData,4]then
  295. insert(n,s,9);
  296. writeln(f,s);
  297. end;
  298. close(f);
  299. call_asw(asmout);
  300. end;
  301. procedure ProcessEdata;
  302. type
  303. a8=array[0..7]of char;
  304. function GetSectionName(rva:cardinal;var Flags:cardinal):a8;
  305. var
  306. i:cardinal;
  307. LocObjOfs:cardinal;
  308. LocObj:TObjInfo;
  309. begin
  310. GetSectionName:='';
  311. Flags:=0;
  312. LocObjOfs:=APE_OptSize+PEoffset+24;
  313. for i:=1 to APE_obj do
  314. begin
  315. seek(f,LocObjOfs);
  316. blockread(f,LocObj,sizeof(LocObj));
  317. if(rva>=LocObj.VirtAddr)and(rva<=LocObj.VirtAddr+LocObj.RawSize)then
  318. begin
  319. GetSectionName:=a8(LocObj.ObjName);
  320. Flags:=LocObj.flags;
  321. end;
  322. end;
  323. end;
  324. var
  325. j,Fl:cardinal;
  326. ulongval,procEntry:cardinal;
  327. Ordinal:word;
  328. isData:longbool;
  329. ExpDir:packed record
  330. flag,
  331. stamp:cardinal;
  332. Major,
  333. Minor:word;
  334. Name,
  335. Base,
  336. NumFuncs,
  337. NumNames,
  338. AddrFuncs,
  339. AddrNames,
  340. AddrOrds:cardinal;
  341. end;
  342. begin
  343. with Obj do
  344. begin
  345. seek(f,RawOffset+delta);
  346. blockread(f,ExpDir,sizeof(ExpDir));
  347. fsplit(impname,_d,_n,_e);
  348. path:=_d+_n+'.ils';
  349. {$IFDEF STANDALONE}
  350. if impname<>'' then
  351. {$ENDIF}
  352. CreateTempDir(path);
  353. Common_created:=false;
  354. for j:=0 to pred(ExpDir.NumNames)do
  355. begin
  356. seek(f,RawOffset-VirtAddr+ExpDir.AddrOrds+j*2);
  357. blockread(f,Ordinal,2);
  358. seek(f,RawOffset-VirtAddr+ExpDir.AddrFuncs+Cardinal(Ordinal*4));
  359. blockread(f,ProcEntry,4);
  360. seek(f,RawOffset-VirtAddr+ExpDir.AddrNames+j*4);
  361. blockread(f,ulongval,4);
  362. seek(f,RawOffset-VirtAddr+ulongval);
  363. blockread(f,cstring,sizeof(cstring));
  364. {$IFDEF STANDALONE}
  365. if not FileCreated then
  366. begin
  367. FileCreated:=true;
  368. if(__textname<>'')or(impname='')then
  369. begin
  370. rewrite(t);
  371. writeln(t,'EXPORTS');
  372. end;
  373. end;
  374. {$ENDIF}
  375. isData:=GetSectionName(procentry,Fl)='';
  376. if not isData then
  377. isData:=Fl and IMAGE_SCN_CNT_CODE<>IMAGE_SCN_CNT_CODE;
  378. {$IFDEF STANDALONE}
  379. if(__textname<>'')or(impname='')then
  380. writeln(t,cstring,' @',succ(ordinal):0,' ',kind[isData]);
  381. if impname<>''then
  382. {$ENDIF}
  383. makeasm(j,cstring,isData);
  384. end;
  385. call_arw;
  386. end;
  387. end;
  388. begin
  389. GetEdata:=false;
  390. {$IFDEF STANDALONE}
  391. FileCreated:=false;
  392. {$ENDIF}
  393. seek(f,PE+120);
  394. blockread(f,ExportRVA,4);
  395. seek(f,PE+6);
  396. blockread(f,APE_Obj,2);
  397. seek(f,PE+20);
  398. blockread(f,APE_OptSize,2);
  399. ObjOfs:=APE_OptSize+PEoffset+24;
  400. for i:=1 to APE_obj do
  401. begin
  402. seek(f,ObjOfs);
  403. blockread(f,Obj,sizeof(Obj));
  404. inc(ObjOfs,sizeof(Obj));
  405. with Obj do
  406. if(VirtAddr<=ExportRva)and(ExportRva<VirtAddr+VirtSize)then
  407. begin
  408. delta:=ExportRva-VirtAddr;
  409. ProcessEdata;
  410. GetEdata:=true;
  411. end;
  412. end;
  413. end;
  414. function makedef(const binname,
  415. {$IFDEF STANDALONE}
  416. textname,
  417. {$ENDIF}
  418. libname:string):longbool;
  419. var
  420. OldFileMode:longint;
  421. begin
  422. assign(f,binname);
  423. {$IFDEF STANDALONE}
  424. FileCreated:=false;
  425. assign(t,textname);
  426. __textname:=textname;
  427. {$ENDIF}
  428. impname:=libname;
  429. lname:=binname;
  430. OldFileMode:=filemode;
  431. {$I-}
  432. filemode:=0;
  433. reset(f,1);
  434. filemode:=OldFileMode;
  435. {$I+}
  436. if IOResult<>0 then
  437. begin
  438. makedef:=false;
  439. exit;
  440. end;
  441. if not DOSstubOK(PEoffset)then
  442. makedef:=false
  443. else if not IsPE(PEoffset)then
  444. makedef:=false
  445. else
  446. makedef:=GetEdata(PEoffset);
  447. close(f);
  448. {$IFDEF STANDALONE}
  449. if FileCreated then
  450. if(textname<>'')or(impname='')then
  451. close(t);
  452. {$ENDIF}
  453. end;
  454. end.
  455. {
  456. $Log$
  457. Revision 1.10 2002-10-05 12:43:24 carl
  458. * fixes for Delphi 6 compilation
  459. (warning : Some features do not work under Delphi)
  460. Revision 1.9 2002/05/18 13:34:08 peter
  461. * readded missing revisions
  462. Revision 1.8 2002/05/16 19:46:37 carl
  463. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  464. + try to fix temp allocation (still in ifdef)
  465. + generic constructor calls
  466. + start of tassembler / tmodulebase class cleanup
  467. }