t_win32.pas 48 KB

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