t_win32.pas 50 KB

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