impdef.pas 9.7 KB

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