t_win32.pas 42 KB

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