t_win32.pas 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. This unit implements support import,export,link routines
  5. for the (i386) Win32 target
  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 t_win32;
  20. {$i defines.inc}
  21. interface
  22. uses
  23. import,export,link;
  24. const
  25. winstackpagesize = 4096;
  26. type
  27. timportlibwin32=class(timportlib)
  28. procedure preparelib(const s:string);override;
  29. procedure importprocedure(const func,module:string;index:longint;const name:string);override;
  30. procedure importvariable(const varname,module:string;const name:string);override;
  31. procedure generatelib;override;
  32. procedure generatenasmlib;virtual;
  33. procedure generatesmartlib;override;
  34. end;
  35. texportlibwin32=class(texportlib)
  36. st : string;
  37. last_index : longint;
  38. procedure preparelib(const s:string);override;
  39. procedure exportprocedure(hp : texported_item);override;
  40. procedure exportvar(hp : texported_item);override;
  41. procedure generatelib;override;
  42. procedure generatenasmlib;virtual;
  43. end;
  44. tlinkerwin32=class(tlinker)
  45. private
  46. Function WriteResponseFile(isdll:boolean) : Boolean;
  47. Function PostProcessExecutable(const fn:string;isdll:boolean) : Boolean;
  48. public
  49. Constructor Create;
  50. Procedure SetDefaultInfo;override;
  51. function MakeExecutable:boolean;override;
  52. function MakeSharedLibrary:boolean;override;
  53. end;
  54. tDLLScannerWin32=class(tDLLScanner)
  55. private
  56. cstring : array[0..127]of char;
  57. function DOSstubOK(var x:cardinal):longbool;
  58. function FindDLL(const s:string;var founddll:string):boolean;
  59. function DllName(Const Name : string) : string;
  60. public
  61. function isSuitableFileType(x:cardinal):longbool;override;
  62. function GetEdata(HeaderEntry:cardinal):longbool;override;
  63. function Scan(const binname:string):longbool;override;
  64. end;
  65. implementation
  66. uses
  67. {$ifdef Delphi}
  68. dmisc,
  69. {$else Delphi}
  70. dos,
  71. {$endif Delphi}
  72. cutils,cclasses,
  73. aasm,fmodule,globtype,globals,systems,verbose,
  74. script,gendef,
  75. cpubase,cpuasm
  76. {$ifdef GDB}
  77. ,gdb
  78. {$endif}
  79. ;
  80. function DllName(Const Name : string) : string;
  81. var n : string;
  82. begin
  83. n:=Upper(SplitExtension(Name));
  84. if (n='.DLL') or (n='.DRV') or (n='.EXE') then
  85. DllName:=Name
  86. else
  87. DllName:=Name+target_os.sharedlibext;
  88. end;
  89. {*****************************************************************************
  90. TIMPORTLIBWIN32
  91. *****************************************************************************}
  92. procedure timportlibwin32.preparelib(const s : string);
  93. begin
  94. if not(assigned(importssection)) then
  95. importssection:=TAAsmoutput.create;
  96. end;
  97. procedure timportlibwin32.importprocedure(const func,module : string;index : longint;const name : string);
  98. var
  99. hp1 : timportlist;
  100. hp2 : timported_item;
  101. hs : string;
  102. begin
  103. hs:=DllName(module);
  104. { search for the module }
  105. hp1:=timportlist(current_module.imports.first);
  106. while assigned(hp1) do
  107. begin
  108. if hs=hp1.dllname^ then
  109. break;
  110. hp1:=timportlist(hp1.next);
  111. end;
  112. { generate a new item ? }
  113. if not(assigned(hp1)) then
  114. begin
  115. hp1:=timportlist.create(hs);
  116. current_module.imports.concat(hp1);
  117. end;
  118. { search for reuse of old import item }
  119. hp2:=timported_item(hp1.imported_items.first);
  120. while assigned(hp2) do
  121. begin
  122. if hp2.func^=func then
  123. break;
  124. hp2:=timported_item(hp2.next);
  125. end;
  126. if not assigned(hp2) then
  127. begin
  128. hp2:=timported_item.create(func,name,index);
  129. hp1.imported_items.concat(hp2);
  130. end;
  131. end;
  132. procedure timportlibwin32.importvariable(const varname,module:string;const name:string);
  133. var
  134. hp1 : timportlist;
  135. hp2 : timported_item;
  136. hs : string;
  137. begin
  138. hs:=DllName(module);
  139. { search for the module }
  140. hp1:=timportlist(current_module.imports.first);
  141. while assigned(hp1) do
  142. begin
  143. if hs=hp1.dllname^ then
  144. break;
  145. hp1:=timportlist(hp1.next);
  146. end;
  147. { generate a new item ? }
  148. if not(assigned(hp1)) then
  149. begin
  150. hp1:=timportlist.create(hs);
  151. current_module.imports.concat(hp1);
  152. end;
  153. hp2:=timported_item.create_var(varname,name);
  154. hp1.imported_items.concat(hp2);
  155. end;
  156. procedure timportlibwin32.generatenasmlib;
  157. var
  158. hp1 : timportlist;
  159. hp2 : timported_item;
  160. p : pchar;
  161. begin
  162. importssection.concat(tai_section.create(sec_code));
  163. hp1:=timportlist(current_module.imports.first);
  164. while assigned(hp1) do
  165. begin
  166. hp2:=timported_item(hp1.imported_items.first);
  167. while assigned(hp2) do
  168. begin
  169. if (aktoutputformat=as_i386_tasm) or
  170. (aktoutputformat=as_i386_masm) then
  171. p:=strpnew(#9+'EXTRN '+hp2.func^)
  172. else
  173. p:=strpnew(#9+'EXTERN '+hp2.func^);
  174. importssection.concat(tai_direct.create(p));
  175. p:=strpnew(#9+'import '+hp2.func^+' '+hp1.dllname^+' '+hp2.name^);
  176. importssection.concat(tai_direct.create(p));
  177. hp2:=timported_item(hp2.next);
  178. end;
  179. hp1:=timportlist(hp1.next);
  180. end;
  181. end;
  182. procedure timportlibwin32.generatesmartlib;
  183. var
  184. hp1 : timportlist;
  185. hp2 : timported_item;
  186. lhead,lname,lcode,
  187. lidata4,lidata5 : pasmlabel;
  188. r : preference;
  189. begin
  190. if (aktoutputformat<>as_i386_asw) and
  191. (aktoutputformat<>as_i386_pecoff) then
  192. begin
  193. generatenasmlib;
  194. exit;
  195. end;
  196. hp1:=timportlist(current_module.imports.first);
  197. while assigned(hp1) do
  198. begin
  199. { Get labels for the sections }
  200. getdatalabel(lhead);
  201. getdatalabel(lname);
  202. getaddrlabel(lidata4);
  203. getaddrlabel(lidata5);
  204. { create header for this importmodule }
  205. importsSection.concat(Tai_cut.Create_begin);
  206. importsSection.concat(Tai_section.Create(sec_idata2));
  207. importsSection.concat(Tai_label.Create(lhead));
  208. { pointer to procedure names }
  209. importsSection.concat(Tai_const_symbol.Create_rva(lidata4));
  210. { two empty entries follow }
  211. importsSection.concat(Tai_const.Create_32bit(0));
  212. importsSection.concat(Tai_const.Create_32bit(0));
  213. { pointer to dll name }
  214. importsSection.concat(Tai_const_symbol.Create_rva(lname));
  215. { pointer to fixups }
  216. importsSection.concat(Tai_const_symbol.Create_rva(lidata5));
  217. { first write the name references }
  218. importsSection.concat(Tai_section.Create(sec_idata4));
  219. importsSection.concat(Tai_const.Create_32bit(0));
  220. importsSection.concat(Tai_label.Create(lidata4));
  221. { then the addresses and create also the indirect jump }
  222. importsSection.concat(Tai_section.Create(sec_idata5));
  223. importsSection.concat(Tai_const.Create_32bit(0));
  224. importsSection.concat(Tai_label.Create(lidata5));
  225. { create procedures }
  226. hp2:=timported_item(hp1.imported_items.first);
  227. while assigned(hp2) do
  228. begin
  229. { insert cuts }
  230. importsSection.concat(Tai_cut.Create);
  231. { create indirect jump }
  232. if not hp2.is_var then
  233. begin
  234. getlabel(lcode);
  235. new(r);
  236. reset_reference(r^);
  237. r^.symbol:=lcode;
  238. { place jump in codesegment, insert a code section in the
  239. importsection to reduce the amount of .s files (PFV) }
  240. importsSection.concat(Tai_section.Create(sec_code));
  241. {$IfDef GDB}
  242. if (cs_debuginfo in aktmoduleswitches) then
  243. importsSection.concat(Tai_stab_function_name.Create(nil));
  244. {$EndIf GDB}
  245. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,0));
  246. importsSection.concat(Taicpu.Op_ref(A_JMP,S_NO,r));
  247. importsSection.concat(Tai_align.Create_op(4,$90));
  248. end;
  249. { create head link }
  250. importsSection.concat(Tai_section.Create(sec_idata7));
  251. importsSection.concat(Tai_const_symbol.Create_rva(lhead));
  252. { fixup }
  253. getlabel(pasmlabel(hp2.lab));
  254. importsSection.concat(Tai_section.Create(sec_idata4));
  255. importsSection.concat(Tai_const_symbol.Create_rva(hp2.lab));
  256. { add jump field to importsection }
  257. importsSection.concat(Tai_section.Create(sec_idata5));
  258. if hp2.is_var then
  259. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,0))
  260. else
  261. importsSection.concat(Tai_label.Create(lcode));
  262. if hp2.name^<>'' then
  263. importsSection.concat(Tai_const_symbol.Create_rva(hp2.lab))
  264. else
  265. importsSection.concat(Tai_const.Create_32bit($80000000 or hp2.ordnr));
  266. { finally the import information }
  267. importsSection.concat(Tai_section.Create(sec_idata6));
  268. importsSection.concat(Tai_label.Create(hp2.lab));
  269. importsSection.concat(Tai_const.Create_16bit(hp2.ordnr));
  270. importsSection.concat(Tai_string.Create(hp2.name^+#0));
  271. importsSection.concat(Tai_align.Create_op(2,0));
  272. hp2:=timported_item(hp2.next);
  273. end;
  274. { write final section }
  275. importsSection.concat(Tai_cut.Create_end);
  276. { end of name references }
  277. importsSection.concat(Tai_section.Create(sec_idata4));
  278. importsSection.concat(Tai_const.Create_32bit(0));
  279. { end if addresses }
  280. importsSection.concat(Tai_section.Create(sec_idata5));
  281. importsSection.concat(Tai_const.Create_32bit(0));
  282. { dllname }
  283. importsSection.concat(Tai_section.Create(sec_idata7));
  284. importsSection.concat(Tai_label.Create(lname));
  285. importsSection.concat(Tai_string.Create(hp1.dllname^+#0));
  286. hp1:=timportlist(hp1.next);
  287. end;
  288. end;
  289. procedure timportlibwin32.generatelib;
  290. var
  291. hp1 : timportlist;
  292. hp2 : timported_item;
  293. l1,l2,l3,l4 : pasmlabel;
  294. r : preference;
  295. begin
  296. if (aktoutputformat<>as_i386_asw) and
  297. (aktoutputformat<>as_i386_pecoff) then
  298. begin
  299. generatenasmlib;
  300. exit;
  301. end;
  302. hp1:=timportlist(current_module.imports.first);
  303. while assigned(hp1) do
  304. begin
  305. { align codesegment for the jumps }
  306. importsSection.concat(Tai_section.Create(sec_code));
  307. importsSection.concat(Tai_align.Create_op(4,$90));
  308. { Get labels for the sections }
  309. getlabel(l1);
  310. getlabel(l2);
  311. getlabel(l3);
  312. importsSection.concat(Tai_section.Create(sec_idata2));
  313. { pointer to procedure names }
  314. importsSection.concat(Tai_const_symbol.Create_rva(l2));
  315. { two empty entries follow }
  316. importsSection.concat(Tai_const.Create_32bit(0));
  317. importsSection.concat(Tai_const.Create_32bit(0));
  318. { pointer to dll name }
  319. importsSection.concat(Tai_const_symbol.Create_rva(l1));
  320. { pointer to fixups }
  321. importsSection.concat(Tai_const_symbol.Create_rva(l3));
  322. { only create one section for each else it will
  323. create a lot of idata* }
  324. { first write the name references }
  325. importsSection.concat(Tai_section.Create(sec_idata4));
  326. importsSection.concat(Tai_label.Create(l2));
  327. hp2:=timported_item(hp1.imported_items.first);
  328. while assigned(hp2) do
  329. begin
  330. getlabel(pasmlabel(hp2.lab));
  331. if hp2.name^<>'' then
  332. importsSection.concat(Tai_const_symbol.Create_rva(hp2.lab))
  333. else
  334. importsSection.concat(Tai_const.Create_32bit($80000000 or hp2.ordnr));
  335. hp2:=timported_item(hp2.next);
  336. end;
  337. { finalize the names ... }
  338. importsSection.concat(Tai_const.Create_32bit(0));
  339. { then the addresses and create also the indirect jump }
  340. importsSection.concat(Tai_section.Create(sec_idata5));
  341. importsSection.concat(Tai_label.Create(l3));
  342. hp2:=timported_item(hp1.imported_items.first);
  343. while assigned(hp2) do
  344. begin
  345. if not hp2.is_var then
  346. begin
  347. getlabel(l4);
  348. { create indirect jump }
  349. new(r);
  350. reset_reference(r^);
  351. r^.symbol:=l4;
  352. { place jump in codesegment }
  353. importsSection.concat(Tai_section.Create(sec_code));
  354. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,0));
  355. importsSection.concat(Taicpu.Op_ref(A_JMP,S_NO,r));
  356. importsSection.concat(Tai_align.Create_op(4,$90));
  357. { add jump field to importsection }
  358. importsSection.concat(Tai_section.Create(sec_idata5));
  359. importsSection.concat(Tai_label.Create(l4));
  360. end
  361. else
  362. begin
  363. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,0));
  364. end;
  365. importsSection.concat(Tai_const_symbol.Create_rva(hp2.lab));
  366. hp2:=timported_item(hp2.next);
  367. end;
  368. { finalize the addresses }
  369. importsSection.concat(Tai_const.Create_32bit(0));
  370. { finally the import information }
  371. importsSection.concat(Tai_section.Create(sec_idata6));
  372. hp2:=timported_item(hp1.imported_items.first);
  373. while assigned(hp2) do
  374. begin
  375. importsSection.concat(Tai_label.Create(hp2.lab));
  376. { the ordinal number }
  377. importsSection.concat(Tai_const.Create_16bit(hp2.ordnr));
  378. importsSection.concat(Tai_string.Create(hp2.name^+#0));
  379. importsSection.concat(Tai_align.Create_op(2,0));
  380. hp2:=timported_item(hp2.next);
  381. end;
  382. { create import dll name }
  383. importsSection.concat(Tai_section.Create(sec_idata7));
  384. importsSection.concat(Tai_label.Create(l1));
  385. importsSection.concat(Tai_string.Create(hp1.dllname^+#0));
  386. hp1:=timportlist(hp1.next);
  387. end;
  388. end;
  389. {*****************************************************************************
  390. TEXPORTLIBWIN32
  391. *****************************************************************************}
  392. procedure texportlibwin32.preparelib(const s:string);
  393. begin
  394. if not(assigned(exportssection)) then
  395. exportssection:=TAAsmoutput.create;
  396. last_index:=0;
  397. end;
  398. procedure texportlibwin32.exportvar(hp : texported_item);
  399. begin
  400. { same code used !! PM }
  401. exportprocedure(hp);
  402. end;
  403. procedure texportlibwin32.exportprocedure(hp : texported_item);
  404. { must be ordered at least for win32 !! }
  405. var
  406. hp2 : texported_item;
  407. begin
  408. { first test the index value }
  409. if (hp.options and eo_index)<>0 then
  410. begin
  411. if (hp.index<=0) or (hp.index>$ffff) then
  412. begin
  413. message1(parser_e_export_invalid_index,tostr(hp.index));
  414. exit;
  415. end;
  416. if (hp.index<=last_index) then
  417. begin
  418. message1(parser_e_export_ordinal_double,tostr(hp.index));
  419. { disregard index value }
  420. inc(last_index);
  421. hp.index:=last_index;
  422. exit;
  423. end
  424. else
  425. begin
  426. last_index:=hp.index;
  427. end;
  428. end
  429. else
  430. begin
  431. inc(last_index);
  432. hp.index:=last_index;
  433. end;
  434. { now place in correct order }
  435. hp2:=texported_item(current_module._exports.first);
  436. while assigned(hp2) and
  437. (hp.name^>hp2.name^) do
  438. hp2:=texported_item(hp2.next);
  439. { insert hp there !! }
  440. if assigned(hp2) and (hp2.name^=hp.name^) then
  441. begin
  442. { this is not allowed !! }
  443. message1(parser_e_export_name_double,hp.name^);
  444. exit;
  445. end;
  446. if hp2=texported_item(current_module._exports.first) then
  447. current_module._exports.concat(hp)
  448. else if assigned(hp2) then
  449. begin
  450. hp.next:=hp2;
  451. hp.previous:=hp2.previous;
  452. if assigned(hp2.previous) then
  453. hp2.previous.next:=hp;
  454. hp2.previous:=hp;
  455. end
  456. else
  457. current_module._exports.concat(hp);
  458. end;
  459. procedure texportlibwin32.generatelib;
  460. var
  461. ordinal_base,ordinal_max,ordinal_min : longint;
  462. current_index : longint;
  463. entries,named_entries : longint;
  464. name_label,dll_name_label,export_address_table : pasmlabel;
  465. export_name_table_pointers,export_ordinal_table : pasmlabel;
  466. hp,hp2 : texported_item;
  467. temtexport : TLinkedList;
  468. address_table,name_table_pointers,
  469. name_table,ordinal_table : TAAsmoutput;
  470. begin
  471. if (aktoutputformat<>as_i386_asw) and
  472. (aktoutputformat<>as_i386_pecoff) then
  473. begin
  474. generatenasmlib;
  475. exit;
  476. end;
  477. hp:=texported_item(current_module._exports.first);
  478. if not assigned(hp) then
  479. exit;
  480. ordinal_max:=0;
  481. ordinal_min:=$7FFFFFFF;
  482. entries:=0;
  483. named_entries:=0;
  484. getlabel(dll_name_label);
  485. getlabel(export_address_table);
  486. getlabel(export_name_table_pointers);
  487. getlabel(export_ordinal_table);
  488. { count entries }
  489. while assigned(hp) do
  490. begin
  491. inc(entries);
  492. if (hp.index>ordinal_max) then
  493. ordinal_max:=hp.index;
  494. if (hp.index>0) and (hp.index<ordinal_min) then
  495. ordinal_min:=hp.index;
  496. if assigned(hp.name) then
  497. inc(named_entries);
  498. hp:=texported_item(hp.next);
  499. end;
  500. { no support for higher ordinal base yet !! }
  501. ordinal_base:=1;
  502. current_index:=ordinal_base;
  503. { we must also count the holes !! }
  504. entries:=ordinal_max-ordinal_base+1;
  505. exportsSection.concat(Tai_section.Create(sec_edata));
  506. { export flags }
  507. exportsSection.concat(Tai_const.Create_32bit(0));
  508. { date/time stamp }
  509. exportsSection.concat(Tai_const.Create_32bit(0));
  510. { major version }
  511. exportsSection.concat(Tai_const.Create_16bit(0));
  512. { minor version }
  513. exportsSection.concat(Tai_const.Create_16bit(0));
  514. { pointer to dll name }
  515. exportsSection.concat(Tai_const_symbol.Create_rva(dll_name_label));
  516. { ordinal base normally set to 1 }
  517. exportsSection.concat(Tai_const.Create_32bit(ordinal_base));
  518. { number of entries }
  519. exportsSection.concat(Tai_const.Create_32bit(entries));
  520. { number of named entries }
  521. exportsSection.concat(Tai_const.Create_32bit(named_entries));
  522. { address of export address table }
  523. exportsSection.concat(Tai_const_symbol.Create_rva(export_address_table));
  524. { address of name pointer pointers }
  525. exportsSection.concat(Tai_const_symbol.Create_rva(export_name_table_pointers));
  526. { address of ordinal number pointers }
  527. exportsSection.concat(Tai_const_symbol.Create_rva(export_ordinal_table));
  528. { the name }
  529. exportsSection.concat(Tai_label.Create(dll_name_label));
  530. if st='' then
  531. exportsSection.concat(Tai_string.Create(current_module.modulename^+target_os.sharedlibext+#0))
  532. else
  533. exportsSection.concat(Tai_string.Create(st+target_os.sharedlibext+#0));
  534. { export address table }
  535. address_table:=TAAsmoutput.create;
  536. address_table.concat(Tai_align.Create_op(4,0));
  537. address_table.concat(Tai_label.Create(export_address_table));
  538. name_table_pointers:=TAAsmoutput.create;
  539. name_table_pointers.concat(Tai_align.Create_op(4,0));
  540. name_table_pointers.concat(Tai_label.Create(export_name_table_pointers));
  541. ordinal_table:=TAAsmoutput.create;
  542. ordinal_table.concat(Tai_align.Create_op(4,0));
  543. ordinal_table.concat(Tai_label.Create(export_ordinal_table));
  544. name_table:=TAAsmoutput.Create;
  545. name_table.concat(Tai_align.Create_op(4,0));
  546. { write each address }
  547. hp:=texported_item(current_module._exports.first);
  548. while assigned(hp) do
  549. begin
  550. if (hp.options and eo_name)<>0 then
  551. begin
  552. getlabel(name_label);
  553. name_table_pointers.concat(Tai_const_symbol.Create_rva(name_label));
  554. ordinal_table.concat(Tai_const.Create_16bit(hp.index-ordinal_base));
  555. name_table.concat(Tai_align.Create_op(2,0));
  556. name_table.concat(Tai_label.Create(name_label));
  557. name_table.concat(Tai_string.Create(hp.name^+#0));
  558. end;
  559. hp:=texported_item(hp.next);
  560. end;
  561. { order in increasing ordinal values }
  562. { into temtexport list }
  563. temtexport:=TLinkedList.Create;
  564. hp:=texported_item(current_module._exports.first);
  565. while assigned(hp) do
  566. begin
  567. current_module._exports.remove(hp);
  568. hp2:=texported_item(temtexport.first);
  569. while assigned(hp2) and (hp.index>hp2.index) do
  570. begin
  571. hp2:=texported_item(hp2.next);
  572. end;
  573. if hp2=texported_item(temtexport.first) then
  574. temtexport.insert(hp)
  575. else
  576. begin
  577. if assigned(hp2) then
  578. begin
  579. hp.next:=hp2;
  580. hp.previous:=hp2.previous;
  581. hp2.previous:=hp;
  582. if assigned(hp.previous) then
  583. hp.previous.next:=hp;
  584. end
  585. else
  586. temtexport.concat(hp);
  587. end;
  588. hp:=texported_item(current_module._exports.first);;
  589. end;
  590. { write the export adress table }
  591. current_index:=ordinal_base;
  592. hp:=texported_item(temtexport.first);
  593. while assigned(hp) do
  594. begin
  595. { fill missing values }
  596. while current_index<hp.index do
  597. begin
  598. address_table.concat(Tai_const.Create_32bit(0));
  599. inc(current_index);
  600. end;
  601. address_table.concat(Tai_const_symbol.Createname_rva(hp.sym^.mangledname));
  602. inc(current_index);
  603. hp:=texported_item(hp.next);
  604. end;
  605. exportsSection.concatlist(address_table);
  606. exportsSection.concatlist(name_table_pointers);
  607. exportsSection.concatlist(ordinal_table);
  608. exportsSection.concatlist(name_table);
  609. address_table.Free;
  610. name_table_pointers.free;
  611. ordinal_table.free;
  612. name_table.free;
  613. temtexport.free;
  614. end;
  615. procedure texportlibwin32.generatenasmlib;
  616. var
  617. hp : texported_item;
  618. p : pchar;
  619. begin
  620. exportssection.concat(tai_section.create(sec_code));
  621. hp:=texported_item(current_module._exports.first);
  622. while assigned(hp) do
  623. begin
  624. p:=strpnew(#9+'export '+hp.sym^.mangledname+' '+hp.name^+' '+tostr(hp.index));
  625. exportssection.concat(tai_direct.create(p));
  626. hp:=texported_item(hp.next);
  627. end;
  628. end;
  629. {****************************************************************************
  630. TLINKERWIN32
  631. ****************************************************************************}
  632. Constructor TLinkerWin32.Create;
  633. begin
  634. Inherited Create;
  635. { allow duplicated libs (PM) }
  636. SharedLibFiles.doubles:=true;
  637. StaticLibFiles.doubles:=true;
  638. If not ForceDeffileForExport then
  639. UseDeffileForExport:=false;
  640. end;
  641. Procedure TLinkerWin32.SetDefaultInfo;
  642. begin
  643. with Info do
  644. begin
  645. ExeCmd[1]:='ldw $OPT $STRIP $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  646. DllCmd[1]:='ldw $OPT $STRIP --dll $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  647. if RelocSection or UseDeffileForExport then
  648. begin
  649. { ExeCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF';
  650. use short forms to avoid 128 char limitation problem }
  651. ExeCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  652. ExeCmd[3]:='ldw $OPT $STRIP $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  653. { DllCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF'; }
  654. DllCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  655. DllCmd[3]:='ldw $OPT $STRIP --dll $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  656. end;
  657. end;
  658. end;
  659. Function TLinkerWin32.WriteResponseFile(isdll:boolean) : Boolean;
  660. Var
  661. linkres : TLinkRes;
  662. i : longint;
  663. HPath : TStringListItem;
  664. s,s2 : string;
  665. found:boolean;
  666. begin
  667. WriteResponseFile:=False;
  668. { Open link.res file }
  669. LinkRes.Init(outputexedir+Info.ResName);
  670. { Write path to search libraries }
  671. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  672. while assigned(HPath) do
  673. begin
  674. LinkRes.Add('SEARCH_DIR('+GetShortName(HPath.Str)+')');
  675. HPath:=TStringListItem(HPath.Next);
  676. end;
  677. HPath:=TStringListItem(LibrarySearchPath.First);
  678. while assigned(HPath) do
  679. begin
  680. LinkRes.Add('SEARCH_DIR('+GetShortName(HPath.Str)+')');
  681. HPath:=TStringListItem(HPath.Next);
  682. end;
  683. { add objectfiles, start with prt0 always }
  684. LinkRes.Add('INPUT(');
  685. if isdll then
  686. LinkRes.AddFileName(GetShortName(FindObjectFile('wdllprt0','')))
  687. else
  688. LinkRes.AddFileName(GetShortName(FindObjectFile('wprt0','')));
  689. while not ObjectFiles.Empty do
  690. begin
  691. s:=ObjectFiles.GetFirst;
  692. if s<>'' then
  693. LinkRes.AddFileName(GetShortName(s));
  694. end;
  695. LinkRes.Add(')');
  696. { Write staticlibraries }
  697. if not StaticLibFiles.Empty then
  698. begin
  699. LinkRes.Add('GROUP(');
  700. While not StaticLibFiles.Empty do
  701. begin
  702. S:=StaticLibFiles.GetFirst;
  703. LinkRes.AddFileName(GetShortName(s));
  704. end;
  705. LinkRes.Add(')');
  706. end;
  707. { Write and Close response }
  708. linkres.writetodisk;
  709. linkres.done;
  710. WriteResponseFile:=True;
  711. end;
  712. function TLinkerWin32.MakeExecutable:boolean;
  713. var
  714. binstr,
  715. cmdstr : string;
  716. success : boolean;
  717. i : longint;
  718. AsBinStr : string[80];
  719. StripStr,
  720. RelocStr,
  721. AppTypeStr,
  722. ImageBaseStr : string[40];
  723. begin
  724. if not(cs_link_extern in aktglobalswitches) then
  725. Message1(exec_i_linking,current_module.exefilename^);
  726. { Create some replacements }
  727. RelocStr:='';
  728. AppTypeStr:='';
  729. ImageBaseStr:='';
  730. StripStr:='';
  731. FindExe('asw',AsBinStr);
  732. if RelocSection then
  733. { Using short form to avoid problems with 128 char limitation under Dos. }
  734. RelocStr:='-b base.$$$';
  735. if apptype=app_gui then
  736. AppTypeStr:='--subsystem windows';
  737. if assigned(DLLImageBase) then
  738. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  739. if (cs_link_strip in aktglobalswitches) then
  740. StripStr:='-s';
  741. { Write used files and libraries }
  742. WriteResponseFile(false);
  743. { Call linker }
  744. success:=false;
  745. for i:=1 to 3 do
  746. begin
  747. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  748. if binstr<>'' then
  749. begin
  750. Replace(cmdstr,'$EXE',current_module.exefilename^);
  751. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  752. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  753. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  754. Replace(cmdstr,'$ASBIN',AsbinStr);
  755. Replace(cmdstr,'$RELOC',RelocStr);
  756. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  757. Replace(cmdstr,'$STRIP',StripStr);
  758. if not DefFile.Empty {and UseDefFileForExport} then
  759. begin
  760. DefFile.WriteFile;
  761. Replace(cmdstr,'$DEF','-d '+deffile.fname);
  762. end
  763. else
  764. Replace(cmdstr,'$DEF','');
  765. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  766. if not success then
  767. break;
  768. end;
  769. end;
  770. { Post process }
  771. if success then
  772. success:=PostProcessExecutable(current_module.exefilename^,false);
  773. { Remove ReponseFile }
  774. if (success) and not(cs_link_extern in aktglobalswitches) then
  775. begin
  776. RemoveFile(outputexedir+Info.ResName);
  777. RemoveFile('base.$$$');
  778. RemoveFile('exp.$$$');
  779. RemoveFile('deffile.$$$');
  780. end;
  781. MakeExecutable:=success; { otherwise a recursive call to link method }
  782. end;
  783. Function TLinkerWin32.MakeSharedLibrary:boolean;
  784. var
  785. binstr,
  786. cmdstr : string;
  787. success : boolean;
  788. i : longint;
  789. AsBinStr : string[80];
  790. StripStr,
  791. RelocStr,
  792. AppTypeStr,
  793. ImageBaseStr : string[40];
  794. begin
  795. MakeSharedLibrary:=false;
  796. if not(cs_link_extern in aktglobalswitches) then
  797. Message1(exec_i_linking,current_module.sharedlibfilename^);
  798. { Create some replacements }
  799. RelocStr:='';
  800. AppTypeStr:='';
  801. ImageBaseStr:='';
  802. StripStr:='';
  803. FindExe('asw',AsBinStr);
  804. if RelocSection then
  805. { Using short form to avoid problems with 128 char limitation under Dos. }
  806. RelocStr:='-b base.$$$';
  807. if apptype=app_gui then
  808. AppTypeStr:='--subsystem windows';
  809. if assigned(DLLImageBase) then
  810. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  811. if (cs_link_strip in aktglobalswitches) then
  812. StripStr:='-s';
  813. { Write used files and libraries }
  814. WriteResponseFile(true);
  815. { Call linker }
  816. success:=false;
  817. for i:=1 to 3 do
  818. begin
  819. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  820. if binstr<>'' then
  821. begin
  822. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  823. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  824. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  825. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  826. Replace(cmdstr,'$ASBIN',AsbinStr);
  827. Replace(cmdstr,'$RELOC',RelocStr);
  828. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  829. Replace(cmdstr,'$STRIP',StripStr);
  830. if not DefFile.Empty {and UseDefFileForExport} then
  831. begin
  832. DefFile.WriteFile;
  833. Replace(cmdstr,'$DEF','-d '+deffile.fname);
  834. end
  835. else
  836. Replace(cmdstr,'$DEF','');
  837. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  838. if not success then
  839. break;
  840. end;
  841. end;
  842. { Post process }
  843. if success then
  844. success:=PostProcessExecutable(current_module.sharedlibfilename^,true);
  845. { Remove ReponseFile }
  846. if (success) and not(cs_link_extern in aktglobalswitches) then
  847. begin
  848. RemoveFile(outputexedir+Info.ResName);
  849. RemoveFile('base.$$$');
  850. RemoveFile('exp.$$$');
  851. end;
  852. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  853. end;
  854. function tlinkerwin32.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  855. type
  856. tdosheader = packed record
  857. e_magic : word;
  858. e_cblp : word;
  859. e_cp : word;
  860. e_crlc : word;
  861. e_cparhdr : word;
  862. e_minalloc : word;
  863. e_maxalloc : word;
  864. e_ss : word;
  865. e_sp : word;
  866. e_csum : word;
  867. e_ip : word;
  868. e_cs : word;
  869. e_lfarlc : word;
  870. e_ovno : word;
  871. e_res : array[0..3] of word;
  872. e_oemid : word;
  873. e_oeminfo : word;
  874. e_res2 : array[0..9] of word;
  875. e_lfanew : longint;
  876. end;
  877. tpeheader = packed record
  878. PEMagic : array[0..3] of char;
  879. Machine : word;
  880. NumberOfSections : word;
  881. TimeDateStamp : longint;
  882. PointerToSymbolTable : longint;
  883. NumberOfSymbols : longint;
  884. SizeOfOptionalHeader : word;
  885. Characteristics : word;
  886. Magic : word;
  887. MajorLinkerVersion : byte;
  888. MinorLinkerVersion : byte;
  889. SizeOfCode : longint;
  890. SizeOfInitializedData : longint;
  891. SizeOfUninitializedData : longint;
  892. AddressOfEntryPoint : longint;
  893. BaseOfCode : longint;
  894. BaseOfData : longint;
  895. ImageBase : longint;
  896. SectionAlignment : longint;
  897. FileAlignment : longint;
  898. MajorOperatingSystemVersion : word;
  899. MinorOperatingSystemVersion : word;
  900. MajorImageVersion : word;
  901. MinorImageVersion : word;
  902. MajorSubsystemVersion : word;
  903. MinorSubsystemVersion : word;
  904. Reserved1 : longint;
  905. SizeOfImage : longint;
  906. SizeOfHeaders : longint;
  907. CheckSum : longint;
  908. Subsystem : word;
  909. DllCharacteristics : word;
  910. SizeOfStackReserve : longint;
  911. SizeOfStackCommit : longint;
  912. SizeOfHeapReserve : longint;
  913. SizeOfHeapCommit : longint;
  914. LoaderFlags : longint;
  915. NumberOfRvaAndSizes : longint;
  916. DataDirectory : array[1..$80] of byte;
  917. end;
  918. tcoffsechdr=packed record
  919. name : array[0..7] of char;
  920. vsize : longint;
  921. rvaofs : longint;
  922. datalen : longint;
  923. datapos : longint;
  924. relocpos : longint;
  925. lineno1 : longint;
  926. nrelocs : word;
  927. lineno2 : word;
  928. flags : longint;
  929. end;
  930. psecfill=^tsecfill;
  931. tsecfill=record
  932. fillpos,
  933. fillsize : longint;
  934. next : psecfill;
  935. end;
  936. var
  937. f : file;
  938. cmdstr : string;
  939. dosheader : tdosheader;
  940. peheader : tpeheader;
  941. firstsecpos,
  942. maxfillsize,
  943. l,peheaderpos : longint;
  944. coffsec : tcoffsechdr;
  945. secroot,hsecroot : psecfill;
  946. zerobuf : pointer;
  947. begin
  948. postprocessexecutable:=false;
  949. { when -s is used or it's a dll then quit }
  950. if (cs_link_extern in aktglobalswitches) then
  951. begin
  952. case apptype of
  953. app_gui :
  954. cmdstr:='--subsystem gui';
  955. app_cui :
  956. cmdstr:='--subsystem console';
  957. end;
  958. if dllversion<>'' then
  959. cmdstr:=cmdstr+' --version '+dllversion;
  960. cmdstr:=cmdstr+' --input '+fn;
  961. cmdstr:=cmdstr+' --stack '+tostr(stacksize);
  962. DoExec(FindUtil('postw32'),cmdstr,false,false);
  963. postprocessexecutable:=true;
  964. exit;
  965. end;
  966. { open file }
  967. assign(f,fn);
  968. {$I-}
  969. reset(f,1);
  970. if ioresult<>0 then
  971. Message1(execinfo_f_cant_open_executable,fn);
  972. { read headers }
  973. blockread(f,dosheader,sizeof(tdosheader));
  974. peheaderpos:=dosheader.e_lfanew;
  975. seek(f,peheaderpos);
  976. blockread(f,peheader,sizeof(tpeheader));
  977. { write info }
  978. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  979. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  980. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  981. { change stack size (PM) }
  982. { I am not sure that the default value is adequate !! }
  983. peheader.SizeOfStackReserve:=stacksize;
  984. { change the header }
  985. { sub system }
  986. { gui=2 }
  987. { cui=3 }
  988. case apptype of
  989. app_gui :
  990. peheader.Subsystem:=2;
  991. app_cui :
  992. peheader.Subsystem:=3;
  993. end;
  994. if dllversion<>'' then
  995. begin
  996. peheader.MajorImageVersion:=dllmajor;
  997. peheader.MinorImageVersion:=dllminor;
  998. end;
  999. { reset timestamp }
  1000. peheader.TimeDateStamp:=0;
  1001. { write header back }
  1002. seek(f,peheaderpos);
  1003. blockwrite(f,peheader,sizeof(tpeheader));
  1004. if ioresult<>0 then
  1005. Message1(execinfo_f_cant_process_executable,fn);
  1006. seek(f,peheaderpos);
  1007. blockread(f,peheader,sizeof(tpeheader));
  1008. { write the value after the change }
  1009. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  1010. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  1011. { read section info }
  1012. maxfillsize:=0;
  1013. firstsecpos:=0;
  1014. secroot:=nil;
  1015. for l:=1 to peheader.NumberOfSections do
  1016. begin
  1017. blockread(f,coffsec,sizeof(tcoffsechdr));
  1018. if coffsec.datapos>0 then
  1019. begin
  1020. if secroot=nil then
  1021. firstsecpos:=coffsec.datapos;
  1022. new(hsecroot);
  1023. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  1024. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  1025. hsecroot^.next:=secroot;
  1026. secroot:=hsecroot;
  1027. if secroot^.fillsize>maxfillsize then
  1028. maxfillsize:=secroot^.fillsize;
  1029. end;
  1030. end;
  1031. if firstsecpos>0 then
  1032. begin
  1033. l:=firstsecpos-filepos(f);
  1034. if l>maxfillsize then
  1035. maxfillsize:=l;
  1036. end
  1037. else
  1038. l:=0;
  1039. { get zero buffer }
  1040. getmem(zerobuf,maxfillsize);
  1041. fillchar(zerobuf^,maxfillsize,0);
  1042. { zero from sectioninfo until first section }
  1043. blockwrite(f,zerobuf^,l);
  1044. { zero section alignments }
  1045. while assigned(secroot) do
  1046. begin
  1047. seek(f,secroot^.fillpos);
  1048. blockwrite(f,zerobuf^,secroot^.fillsize);
  1049. hsecroot:=secroot;
  1050. secroot:=secroot^.next;
  1051. dispose(hsecroot);
  1052. end;
  1053. freemem(zerobuf,maxfillsize);
  1054. close(f);
  1055. {$I+}
  1056. if ioresult<>0 then;
  1057. postprocessexecutable:=true;
  1058. end;
  1059. {****************************************************************************
  1060. TDLLScannerWin32
  1061. ****************************************************************************}
  1062. function tDLLScannerWin32.DOSstubOK(var x:cardinal):longbool;
  1063. begin
  1064. blockread(f,TheWord,2,loaded);
  1065. if loaded<>2 then
  1066. DOSstubOK:=false
  1067. else
  1068. begin
  1069. DOSstubOK:=TheWord='MZ';
  1070. seek(f,$3C);
  1071. blockread(f,x,4,loaded);
  1072. if(loaded<>4)or(x>filesize(f))then
  1073. DOSstubOK:=false;
  1074. end;
  1075. end;
  1076. function TDLLScannerWin32.FindDLL(const s:string;var founddll:string):boolean;
  1077. var
  1078. sysdir : string;
  1079. Found : boolean;
  1080. begin
  1081. Found:=false;
  1082. { Look for DLL in:
  1083. 1. Current dir
  1084. 2. Library Path
  1085. 3. windir,windir/system,windir/system32 }
  1086. Found:=FindFile(s,'.'+DirSep,founddll);
  1087. if (not found) then
  1088. Found:=librarysearchpath.FindFile(s,founddll);
  1089. if (not found) then
  1090. begin
  1091. sysdir:=FixPath(GetEnv('windir'),false);
  1092. Found:=FindFile(s,sysdir+';'+sysdir+'system'+DirSep+';'+sysdir+'system32'+DirSep,founddll);
  1093. end;
  1094. if (not found) then
  1095. begin
  1096. message1(exec_w_libfile_not_found,s);
  1097. FoundDll:=s;
  1098. end;
  1099. FindDll:=Found;
  1100. end;
  1101. function tDLLScannerWin32.DllName(Const Name : string) : string;
  1102. var n : string;
  1103. begin
  1104. n:=Upper(SplitExtension(Name));
  1105. if (n='.DLL') or (n='.DRV') or (n='.EXE') then
  1106. DllName:=Name
  1107. else
  1108. DllName:=Name+target_os.sharedlibext;
  1109. end;
  1110. function tDLLScannerWin32.isSuitableFileType(x:cardinal):longbool;
  1111. begin
  1112. seek(f,x);
  1113. blockread(f,TheWord,2,loaded);
  1114. isSuitableFileType:=(loaded=2)and(TheWord='PE');
  1115. end;
  1116. function tDLLScannerWin32.GetEdata(HeaderEntry:cardinal):longbool;
  1117. type
  1118. TObjInfo=packed record
  1119. ObjName:array[0..7]of char;
  1120. VirtSize,
  1121. VirtAddr,
  1122. RawSize,
  1123. RawOffset,
  1124. Reloc,
  1125. LineNum:cardinal;
  1126. RelCount,
  1127. LineCount:word;
  1128. flags:cardinal;
  1129. end;
  1130. var
  1131. i:cardinal;
  1132. ObjOfs:cardinal;
  1133. Obj:TObjInfo;
  1134. APE_obj,APE_Optsize:word;
  1135. ExportRVA:cardinal;
  1136. delta:cardinal;
  1137. const
  1138. IMAGE_SCN_CNT_CODE=$00000020;
  1139. var
  1140. _d:dirstr;
  1141. _n:namestr;
  1142. _e:extstr;
  1143. function isUsedFunction(name:pchar):longbool;
  1144. var
  1145. hp:tExternalsItem;
  1146. begin
  1147. isUsedFunction:=false;
  1148. hp:=tExternalsItem(current_module.Externals.first);
  1149. while assigned(hp)do
  1150. begin
  1151. if(assigned(hp.data))and(not hp.found)then
  1152. if hp.data^=StrPas(name)then
  1153. begin
  1154. isUsedFunction:=true;
  1155. hp.found:=true;
  1156. exit;
  1157. end;
  1158. hp:=tExternalsItem(hp.next);
  1159. end;
  1160. end;
  1161. procedure Store(index:cardinal;name:pchar;isData:longbool);
  1162. begin
  1163. if not isUsedFunction(name)then
  1164. exit;
  1165. if not(current_module.uses_imports) then
  1166. begin
  1167. current_module.uses_imports:=true;
  1168. importlib.preparelib(current_module.modulename^);
  1169. end;
  1170. if IsData then
  1171. importlib.importvariable(name,_n,name)
  1172. else
  1173. importlib.importprocedure(name,_n,index,name);
  1174. end;
  1175. procedure ProcessEdata;
  1176. type
  1177. a8=array[0..7]of char;
  1178. function GetSectionName(rva:cardinal;var Flags:cardinal):a8;
  1179. var
  1180. i:cardinal;
  1181. LocObjOfs:cardinal;
  1182. LocObj:TObjInfo;
  1183. begin
  1184. GetSectionName:='';
  1185. Flags:=0;
  1186. LocObjOfs:=APE_OptSize+HeaderOffset+24;
  1187. for i:=1 to APE_obj do
  1188. begin
  1189. seek(f,LocObjOfs);
  1190. blockread(f,LocObj,sizeof(LocObj));
  1191. if(rva>=LocObj.VirtAddr)and(rva<=LocObj.VirtAddr+LocObj.RawSize)then
  1192. begin
  1193. GetSectionName:=a8(LocObj.ObjName);
  1194. Flags:=LocObj.flags;
  1195. end;
  1196. end;
  1197. end;
  1198. var
  1199. j,Fl:cardinal;
  1200. ulongval,procEntry:cardinal;
  1201. Ordinal:word;
  1202. isData:longbool;
  1203. ExpDir:packed record
  1204. flag,
  1205. stamp:cardinal;
  1206. Major,
  1207. Minor:word;
  1208. Name,
  1209. Base,
  1210. NumFuncs,
  1211. NumNames,
  1212. AddrFuncs,
  1213. AddrNames,
  1214. AddrOrds:cardinal;
  1215. end;
  1216. begin
  1217. with Obj do
  1218. begin
  1219. seek(f,RawOffset+delta);
  1220. blockread(f,ExpDir,sizeof(ExpDir));
  1221. fsplit(impname,_d,_n,_e);
  1222. for j:=0 to pred(ExpDir.NumNames)do
  1223. begin
  1224. seek(f,RawOffset-VirtAddr+ExpDir.AddrOrds+j*2);
  1225. blockread(f,Ordinal,2);
  1226. seek(f,RawOffset-VirtAddr+ExpDir.AddrFuncs+Ordinal*4);
  1227. blockread(f,ProcEntry,4);
  1228. seek(f,RawOffset-VirtAddr+ExpDir.AddrNames+j*4);
  1229. blockread(f,ulongval,4);
  1230. seek(f,RawOffset-VirtAddr+ulongval);
  1231. blockread(f,cstring,sizeof(cstring));
  1232. isData:=GetSectionName(procentry,Fl)='';
  1233. if not isData then
  1234. isData:=Fl and IMAGE_SCN_CNT_CODE<>IMAGE_SCN_CNT_CODE;
  1235. Store(succ(Ordinal),cstring,isData);
  1236. end;
  1237. end;
  1238. end;
  1239. begin
  1240. GetEdata:=false;
  1241. seek(f,HeaderEntry+120);
  1242. blockread(f,ExportRVA,4);
  1243. seek(f,HeaderEntry+6);
  1244. blockread(f,APE_Obj,2);
  1245. seek(f,HeaderEntry+20);
  1246. blockread(f,APE_OptSize,2);
  1247. ObjOfs:=APE_OptSize+HeaderOffset+24;
  1248. for i:=1 to APE_obj do
  1249. begin
  1250. seek(f,ObjOfs);
  1251. blockread(f,Obj,sizeof(Obj));
  1252. inc(ObjOfs,sizeof(Obj));
  1253. with Obj do
  1254. if(VirtAddr<=ExportRva)and(ExportRva<VirtAddr+VirtSize)then
  1255. begin
  1256. delta:=ExportRva-VirtAddr;
  1257. ProcessEdata;
  1258. GetEdata:=true;
  1259. end;
  1260. end;
  1261. end;
  1262. function tDLLScannerWin32.scan(const binname:string):longbool;
  1263. var
  1264. OldFileMode:longint;
  1265. begin
  1266. if not FindDll(DLLName(binname),impname) then
  1267. exit;
  1268. assign(f,impname);
  1269. OldFileMode:=filemode;
  1270. filemode:=0;
  1271. reset(f,1);
  1272. filemode:=OldFileMode;
  1273. if not DOSstubOK(HeaderOffset)then
  1274. scan:=false
  1275. else if not isSuitableFileType(HeaderOffset)then
  1276. scan:=false
  1277. else
  1278. scan:=GetEdata(HeaderOffset);
  1279. close(f);
  1280. end;
  1281. end.
  1282. {
  1283. $Log$
  1284. Revision 1.2 2001-03-06 18:28:02 peter
  1285. * patch from Pavel with a new and much faster DLL Scanner for
  1286. automatic importing so $linklib works for DLLs. Thanks Pavel!
  1287. Revision 1.1 2001/02/26 19:43:11 peter
  1288. * moved target units to subdir
  1289. Revision 1.10 2001/02/20 21:41:16 peter
  1290. * new fixfilename, findfile for unix. Look first for lowercase, then
  1291. NormalCase and last for UPPERCASE names.
  1292. Revision 1.9 2001/01/13 00:09:22 peter
  1293. * made Pavel O. happy ;)
  1294. Revision 1.8 2000/12/30 22:53:25 peter
  1295. * export with the case provided in the exports section
  1296. Revision 1.7 2000/12/25 00:07:30 peter
  1297. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1298. tlinkedlist objects)
  1299. Revision 1.6 2000/11/12 22:20:37 peter
  1300. * create generic toutputsection for binary writers
  1301. Revision 1.5 2000/09/24 15:06:31 peter
  1302. * use defines.inc
  1303. Revision 1.4 2000/08/27 16:11:54 peter
  1304. * moved some util functions from globals,cobjects to cutils
  1305. * splitted files into finput,fmodule
  1306. Revision 1.3 2000/07/21 15:14:02 jonas
  1307. + added is_addr field for labels, if they are only used for getting the address
  1308. (e.g. for io checks) and corresponding getaddrlabel() procedure
  1309. Revision 1.2 2000/07/13 11:32:50 michael
  1310. + removed logs
  1311. }