t_win32.pas 42 KB

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