t_win32.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. { now place in correct order }
  387. hp2:=texported_item(current_module._exports.first);
  388. while assigned(hp2) and
  389. (hp.name^>hp2.name^) do
  390. hp2:=texported_item(hp2.next);
  391. { insert hp there !! }
  392. if assigned(hp2) and (hp2.name^=hp.name^) then
  393. begin
  394. { this is not allowed !! }
  395. message1(parser_e_export_name_double,hp.name^);
  396. exit;
  397. end;
  398. if hp2=texported_item(current_module._exports.first) then
  399. current_module._exports.concat(hp)
  400. else if assigned(hp2) then
  401. begin
  402. hp.next:=hp2;
  403. hp.previous:=hp2.previous;
  404. if assigned(hp2.previous) then
  405. hp2.previous.next:=hp;
  406. hp2.previous:=hp;
  407. end
  408. else
  409. current_module._exports.concat(hp);
  410. end;
  411. procedure texportlibwin32.generatelib;
  412. var
  413. ordinal_base,ordinal_max,ordinal_min : longint;
  414. current_index : longint;
  415. entries,named_entries : longint;
  416. name_label,dll_name_label,export_address_table : pasmlabel;
  417. export_name_table_pointers,export_ordinal_table : pasmlabel;
  418. hp,hp2 : texported_item;
  419. temtexport : TLinkedList;
  420. address_table,name_table_pointers,
  421. name_table,ordinal_table : TAAsmoutput;
  422. begin
  423. hp:=texported_item(current_module._exports.first);
  424. if not assigned(hp) then
  425. exit;
  426. ordinal_max:=0;
  427. ordinal_min:=$7FFFFFFF;
  428. entries:=0;
  429. named_entries:=0;
  430. getlabel(dll_name_label);
  431. getlabel(export_address_table);
  432. getlabel(export_name_table_pointers);
  433. getlabel(export_ordinal_table);
  434. { count entries }
  435. while assigned(hp) do
  436. begin
  437. inc(entries);
  438. if (hp.index>ordinal_max) then
  439. ordinal_max:=hp.index;
  440. if (hp.index>0) and (hp.index<ordinal_min) then
  441. ordinal_min:=hp.index;
  442. if assigned(hp.name) then
  443. inc(named_entries);
  444. hp:=texported_item(hp.next);
  445. end;
  446. { no support for higher ordinal base yet !! }
  447. ordinal_base:=1;
  448. current_index:=ordinal_base;
  449. { we must also count the holes !! }
  450. entries:=ordinal_max-ordinal_base+1;
  451. exportsSection.concat(Tai_section.Create(sec_edata));
  452. { export flags }
  453. exportsSection.concat(Tai_const.Create_32bit(0));
  454. { date/time stamp }
  455. exportsSection.concat(Tai_const.Create_32bit(0));
  456. { major version }
  457. exportsSection.concat(Tai_const.Create_16bit(0));
  458. { minor version }
  459. exportsSection.concat(Tai_const.Create_16bit(0));
  460. { pointer to dll name }
  461. exportsSection.concat(Tai_const_symbol.Create_rva(dll_name_label));
  462. { ordinal base normally set to 1 }
  463. exportsSection.concat(Tai_const.Create_32bit(ordinal_base));
  464. { number of entries }
  465. exportsSection.concat(Tai_const.Create_32bit(entries));
  466. { number of named entries }
  467. exportsSection.concat(Tai_const.Create_32bit(named_entries));
  468. { address of export address table }
  469. exportsSection.concat(Tai_const_symbol.Create_rva(export_address_table));
  470. { address of name pointer pointers }
  471. exportsSection.concat(Tai_const_symbol.Create_rva(export_name_table_pointers));
  472. { address of ordinal number pointers }
  473. exportsSection.concat(Tai_const_symbol.Create_rva(export_ordinal_table));
  474. { the name }
  475. exportsSection.concat(Tai_label.Create(dll_name_label));
  476. if st='' then
  477. exportsSection.concat(Tai_string.Create(current_module.modulename^+target_os.sharedlibext+#0))
  478. else
  479. exportsSection.concat(Tai_string.Create(st+target_os.sharedlibext+#0));
  480. { export address table }
  481. address_table:=TAAsmoutput.create;
  482. address_table.concat(Tai_align.Create_op(4,0));
  483. address_table.concat(Tai_label.Create(export_address_table));
  484. name_table_pointers:=TAAsmoutput.create;
  485. name_table_pointers.concat(Tai_align.Create_op(4,0));
  486. name_table_pointers.concat(Tai_label.Create(export_name_table_pointers));
  487. ordinal_table:=TAAsmoutput.create;
  488. ordinal_table.concat(Tai_align.Create_op(4,0));
  489. ordinal_table.concat(Tai_label.Create(export_ordinal_table));
  490. name_table:=TAAsmoutput.Create;
  491. name_table.concat(Tai_align.Create_op(4,0));
  492. { write each address }
  493. hp:=texported_item(current_module._exports.first);
  494. while assigned(hp) do
  495. begin
  496. if (hp.options and eo_name)<>0 then
  497. begin
  498. getlabel(name_label);
  499. name_table_pointers.concat(Tai_const_symbol.Create_rva(name_label));
  500. ordinal_table.concat(Tai_const.Create_16bit(hp.index-ordinal_base));
  501. name_table.concat(Tai_align.Create_op(2,0));
  502. name_table.concat(Tai_label.Create(name_label));
  503. name_table.concat(Tai_string.Create(hp.name^+#0));
  504. end;
  505. hp:=texported_item(hp.next);
  506. end;
  507. { order in increasing ordinal values }
  508. { into temtexport list }
  509. temtexport:=TLinkedList.Create;
  510. hp:=texported_item(current_module._exports.first);
  511. while assigned(hp) do
  512. begin
  513. current_module._exports.remove(hp);
  514. hp2:=texported_item(temtexport.first);
  515. while assigned(hp2) and (hp.index>hp2.index) do
  516. begin
  517. hp2:=texported_item(hp2.next);
  518. end;
  519. if hp2=texported_item(temtexport.first) then
  520. temtexport.insert(hp)
  521. else
  522. begin
  523. if assigned(hp2) then
  524. begin
  525. hp.next:=hp2;
  526. hp.previous:=hp2.previous;
  527. hp2.previous:=hp;
  528. if assigned(hp.previous) then
  529. hp.previous.next:=hp;
  530. end
  531. else
  532. temtexport.concat(hp);
  533. end;
  534. hp:=texported_item(current_module._exports.first);;
  535. end;
  536. { write the export adress table }
  537. current_index:=ordinal_base;
  538. hp:=texported_item(temtexport.first);
  539. while assigned(hp) do
  540. begin
  541. { fill missing values }
  542. while current_index<hp.index do
  543. begin
  544. address_table.concat(Tai_const.Create_32bit(0));
  545. inc(current_index);
  546. end;
  547. address_table.concat(Tai_const_symbol.Createname_rva(hp.sym^.mangledname));
  548. inc(current_index);
  549. hp:=texported_item(hp.next);
  550. end;
  551. exportsSection.concatlist(address_table);
  552. exportsSection.concatlist(name_table_pointers);
  553. exportsSection.concatlist(ordinal_table);
  554. exportsSection.concatlist(name_table);
  555. address_table.Free;
  556. name_table_pointers.free;
  557. ordinal_table.free;
  558. name_table.free;
  559. temtexport.free;
  560. end;
  561. {****************************************************************************
  562. TLINKERWIN32
  563. ****************************************************************************}
  564. Constructor TLinkerWin32.Create;
  565. begin
  566. Inherited Create;
  567. { allow duplicated libs (PM) }
  568. SharedLibFiles.doubles:=true;
  569. StaticLibFiles.doubles:=true;
  570. If not ForceDeffileForExport then
  571. UseDeffileForExport:=false;
  572. end;
  573. Procedure TLinkerWin32.SetDefaultInfo;
  574. begin
  575. with Info do
  576. begin
  577. ExeCmd[1]:='ldw $OPT $STRIP $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  578. DllCmd[1]:='ldw $OPT $STRIP --dll $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  579. if RelocSection or UseDeffileForExport then
  580. begin
  581. { ExeCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF';
  582. use short forms to avoid 128 char limitation problem }
  583. ExeCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  584. ExeCmd[3]:='ldw $OPT $STRIP $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  585. { DllCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF'; }
  586. DllCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  587. DllCmd[3]:='ldw $OPT $STRIP --dll $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  588. end;
  589. end;
  590. end;
  591. {$ifndef PAVEL_LINKLIB}
  592. Function TLinkerWin32.WriteResponseFile(isdll:boolean) : Boolean;
  593. Var
  594. linkres : TLinkRes;
  595. i : longint;
  596. HPath : TStringListItem;
  597. s,s2 : string;
  598. found,linklibc : boolean;
  599. begin
  600. WriteResponseFile:=False;
  601. { Open link.res file }
  602. LinkRes.Init(outputexedir+Info.ResName);
  603. { Write path to search libraries }
  604. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  605. while assigned(HPath) do
  606. begin
  607. LinkRes.Add('SEARCH_DIR('+GetShortName(HPath.Str)+')');
  608. HPath:=TStringListItem(HPath.Next);
  609. end;
  610. HPath:=TStringListItem(LibrarySearchPath.First);
  611. while assigned(HPath) do
  612. begin
  613. LinkRes.Add('SEARCH_DIR('+GetShortName(HPath.Str)+')');
  614. HPath:=TStringListItem(HPath.Next);
  615. end;
  616. { add objectfiles, start with prt0 always }
  617. LinkRes.Add('INPUT(');
  618. if isdll then
  619. LinkRes.AddFileName(GetShortName(FindObjectFile('wdllprt0','')))
  620. else
  621. LinkRes.AddFileName(GetShortName(FindObjectFile('wprt0','')));
  622. while not ObjectFiles.Empty do
  623. begin
  624. s:=ObjectFiles.GetFirst;
  625. if s<>'' then
  626. LinkRes.AddFileName(GetShortName(s));
  627. end;
  628. LinkRes.Add(')');
  629. { Write staticlibraries }
  630. if not StaticLibFiles.Empty then
  631. begin
  632. LinkRes.Add('GROUP(');
  633. While not StaticLibFiles.Empty do
  634. begin
  635. S:=StaticLibFiles.GetFirst;
  636. LinkRes.AddFileName(GetShortName(s));
  637. end;
  638. LinkRes.Add(')');
  639. end;
  640. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  641. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  642. if not SharedLibFiles.Empty then
  643. begin
  644. linklibc:=false;
  645. LinkRes.Add('INPUT(');
  646. While not SharedLibFiles.Empty do
  647. begin
  648. S:=SharedLibFiles.GetFirst;
  649. if pos('.',s)=0 then
  650. { we never directly link a DLL
  651. its allways through an import library PM }
  652. { libraries created by C compilers have .a extensions }
  653. s2:=s+'.a'{ target_os.sharedlibext }
  654. else
  655. s2:=s;
  656. s2:=FindLibraryFile(s2,'',found);
  657. if found then
  658. begin
  659. LinkRes.Add(s2);
  660. continue;
  661. end;
  662. if pos(target_os.libprefix,s)=1 then
  663. s:=copy(s,length(target_os.libprefix)+1,255);
  664. if s<>'c' then
  665. begin
  666. i:=Pos(target_os.sharedlibext,S);
  667. if i>0 then
  668. Delete(S,i,255);
  669. LinkRes.Add('-l'+s);
  670. end
  671. else
  672. begin
  673. LinkRes.Add('-l'+s);
  674. linklibc:=true;
  675. end;
  676. end;
  677. { be sure that libc is the last lib }
  678. if linklibc then
  679. LinkRes.Add('-lc');
  680. LinkRes.Add(')');
  681. end;
  682. { Write and Close response }
  683. linkres.writetodisk;
  684. linkres.done;
  685. WriteResponseFile:=True;
  686. end;
  687. {$else PAVEL_LINKLIB}
  688. Function TLinkerWin32.WriteResponseFile(isdll:boolean) : Boolean;
  689. Var
  690. linkres : TLinkRes;
  691. HPath : PStringQueueItem;
  692. s,s2 : string;
  693. success : boolean;
  694. function ExpandName(const s:string):string;
  695. var
  696. sysdir:string;
  697. procedure GetSysDir;
  698. begin
  699. sysdir:=GetEnv('windir');
  700. if sysdir<>''then
  701. begin
  702. if not(sysdir[length(sysdir)]in['\','/'])then
  703. sysdir:=sysdir+dirsep;
  704. end;
  705. end;
  706. function IsFile(d:string;var PathToDll:string):longbool;
  707. var
  708. f:file;
  709. attr:word;
  710. begin
  711. PathToDll:='';
  712. if d<>''then
  713. if d[length(d)]<>dirsep then
  714. d:=d+dirsep;
  715. d:=d+s;
  716. assign(f,d);
  717. GetFattr(f,Attr);
  718. if DOSerror<>0 then
  719. IsFile:=false
  720. else
  721. begin
  722. if(attr and directory)=0 then
  723. begin
  724. IsFile:=true;
  725. PathToDll:=GetShortName(d);
  726. end
  727. else
  728. IsFile:=false;
  729. end;
  730. end;
  731. var
  732. PathToDll:string;
  733. begin
  734. if not isFile('',PathToDll)then
  735. begin
  736. HPath:=LibrarySearchPath.First;
  737. while assigned(HPath) do
  738. begin
  739. if isFile(GetShortName(HPath^.Data^),PathToDll)then
  740. break;
  741. HPath:=HPath^.Next;
  742. end;
  743. if PathToDll='' then
  744. begin
  745. GetSysDir;
  746. if not isFile(sysdir,PathToDll)then
  747. if not isFile(sysdir+'system32',PathToDll)then
  748. if not isFile(sysdir+'system',PathToDll)then
  749. begin
  750. message1(exec_w_libfile_not_found,S2);
  751. PathToDll:=S2;
  752. end;
  753. end;
  754. end;
  755. ExpandName:=PathToDll;
  756. end;
  757. function DotPos(const s:string):longint;
  758. var
  759. i:longint;
  760. begin
  761. DotPos:=0;
  762. for i:=length(s)downto 1 do
  763. begin
  764. if s[i]in['/','\',':']then
  765. exit
  766. else if s[i]='.'then
  767. begin
  768. DotPos:=i;
  769. exit;
  770. end;
  771. end;
  772. end;
  773. procedure strip(var s:string);
  774. var
  775. d:dirstr;
  776. n:namestr;
  777. e:extstr;
  778. begin
  779. fsplit(s,d,n,e);
  780. s:=n;
  781. end;
  782. function do_makedef(const s:string):longbool;
  783. begin
  784. if cs_link_extern in aktglobalswitches then
  785. do_makedef:=DoExec(FindUtil('fpimpdef'),'-o deffile.$$$ -i '+s,false,false)
  786. else
  787. do_makedef:=makedef(s,'deffile.$$$');
  788. end;
  789. begin
  790. WriteResponseFile:=False;
  791. While not SharedLibFiles.Empty do
  792. begin
  793. S:=SharedLibFiles.Get;
  794. if DotPos(s)=0 then
  795. s2:=s+target_os.sharedlibext
  796. else
  797. s2:=s;
  798. strip(s);
  799. if not do_makedef(ExpandName(s2))then
  800. begin
  801. Message(exec_w_error_while_linking);
  802. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  803. end
  804. else
  805. begin
  806. s:=target_os.libprefix+s+target_os.staticlibext;
  807. success:=DoExec(FindUtil('dlltool'),'-l '+s+' -D '+s2+' -d deffile.$$$',false,false);
  808. ObjectFiles.insert(s);
  809. if not success then
  810. break;
  811. end;
  812. end;
  813. { Open link.res file }
  814. LinkRes.Init(outputexedir+Info.ResName);
  815. { Write path to search libraries }
  816. HPath:=current_module.locallibrarysearchpath.First;
  817. while assigned(HPath) do
  818. begin
  819. LinkRes.Add('SEARCH_DIR('+GetShortName(HPath^.Data^)+')');
  820. HPath:=HPath^.Next;
  821. end;
  822. HPath:=LibrarySearchPath.First;
  823. while assigned(HPath) do
  824. begin
  825. LinkRes.Add('SEARCH_DIR('+GetShortName(HPath^.Data^)+')');
  826. HPath:=HPath^.Next;
  827. end;
  828. { add objectfiles, start with prt0 always }
  829. LinkRes.Add('INPUT(');
  830. if isdll then
  831. LinkRes.AddFileName(GetShortName(FindObjectFile('wdllprt0')))
  832. else
  833. LinkRes.AddFileName(GetShortName(FindObjectFile('wprt0')));
  834. while not ObjectFiles.Empty do
  835. begin
  836. s:=ObjectFiles.Get;
  837. if s<>'' then
  838. LinkRes.AddFileName(GetShortName(s));
  839. end;
  840. LinkRes.Add(')');
  841. { Write staticlibraries }
  842. if not StaticLibFiles.Empty then
  843. begin
  844. LinkRes.Add('GROUP(');
  845. While not StaticLibFiles.Empty do
  846. begin
  847. S:=StaticLibFiles.Get;
  848. LinkRes.AddFileName(GetShortName(s));
  849. end;
  850. LinkRes.Add(')');
  851. end;
  852. { Write and Close response }
  853. linkres.writetodisk;
  854. linkres.done;
  855. WriteResponseFile:=True;
  856. end;
  857. {$endif PAVEL_LINKLIB}
  858. function TLinkerWin32.MakeExecutable:boolean;
  859. var
  860. binstr,
  861. cmdstr : string;
  862. found,
  863. success : boolean;
  864. i : longint;
  865. AsBinStr : string[80];
  866. StripStr,
  867. RelocStr,
  868. AppTypeStr,
  869. ImageBaseStr : string[40];
  870. begin
  871. if not(cs_link_extern in aktglobalswitches) then
  872. Message1(exec_i_linking,current_module.exefilename^);
  873. { Create some replacements }
  874. RelocStr:='';
  875. AppTypeStr:='';
  876. ImageBaseStr:='';
  877. StripStr:='';
  878. AsBinStr:=FindExe('asw',found);
  879. if RelocSection then
  880. { Using short form to avoid problems with 128 char limitation under Dos. }
  881. RelocStr:='-b base.$$$';
  882. if apptype=app_gui then
  883. AppTypeStr:='--subsystem windows';
  884. if assigned(DLLImageBase) then
  885. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  886. if (cs_link_strip in aktglobalswitches) then
  887. StripStr:='-s';
  888. { Write used files and libraries }
  889. WriteResponseFile(false);
  890. { Call linker }
  891. success:=false;
  892. for i:=1 to 3 do
  893. begin
  894. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  895. if binstr<>'' then
  896. begin
  897. Replace(cmdstr,'$EXE',current_module.exefilename^);
  898. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  899. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  900. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  901. Replace(cmdstr,'$ASBIN',AsbinStr);
  902. Replace(cmdstr,'$RELOC',RelocStr);
  903. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  904. Replace(cmdstr,'$STRIP',StripStr);
  905. if not DefFile.Empty {and UseDefFileForExport} then
  906. begin
  907. DefFile.WriteFile;
  908. Replace(cmdstr,'$DEF','-d '+deffile.fname);
  909. end
  910. else
  911. Replace(cmdstr,'$DEF','');
  912. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  913. if not success then
  914. break;
  915. end;
  916. end;
  917. { Post process }
  918. if success then
  919. success:=PostProcessExecutable(current_module.exefilename^,false);
  920. { Remove ReponseFile }
  921. if (success) and not(cs_link_extern in aktglobalswitches) then
  922. begin
  923. RemoveFile(outputexedir+Info.ResName);
  924. RemoveFile('base.$$$');
  925. RemoveFile('exp.$$$');
  926. RemoveFile('deffile.$$$');
  927. end;
  928. MakeExecutable:=success; { otherwise a recursive call to link method }
  929. end;
  930. Function TLinkerWin32.MakeSharedLibrary:boolean;
  931. var
  932. binstr,
  933. cmdstr : string;
  934. found,
  935. success : boolean;
  936. i : longint;
  937. AsBinStr : string[80];
  938. StripStr,
  939. RelocStr,
  940. AppTypeStr,
  941. ImageBaseStr : string[40];
  942. begin
  943. MakeSharedLibrary:=false;
  944. if not(cs_link_extern in aktglobalswitches) then
  945. Message1(exec_i_linking,current_module.sharedlibfilename^);
  946. { Create some replacements }
  947. RelocStr:='';
  948. AppTypeStr:='';
  949. ImageBaseStr:='';
  950. StripStr:='';
  951. AsBinStr:=FindExe('asw',found);
  952. if RelocSection then
  953. { Using short form to avoid problems with 128 char limitation under Dos. }
  954. RelocStr:='-b base.$$$';
  955. if apptype=app_gui then
  956. AppTypeStr:='--subsystem windows';
  957. if assigned(DLLImageBase) then
  958. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  959. if (cs_link_strip in aktglobalswitches) then
  960. StripStr:='-s';
  961. { Write used files and libraries }
  962. WriteResponseFile(true);
  963. { Call linker }
  964. success:=false;
  965. for i:=1 to 3 do
  966. begin
  967. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  968. if binstr<>'' then
  969. begin
  970. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  971. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  972. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  973. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  974. Replace(cmdstr,'$ASBIN',AsbinStr);
  975. Replace(cmdstr,'$RELOC',RelocStr);
  976. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  977. Replace(cmdstr,'$STRIP',StripStr);
  978. if not DefFile.Empty {and UseDefFileForExport} then
  979. begin
  980. DefFile.WriteFile;
  981. Replace(cmdstr,'$DEF','-d '+deffile.fname);
  982. end
  983. else
  984. Replace(cmdstr,'$DEF','');
  985. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  986. if not success then
  987. break;
  988. end;
  989. end;
  990. { Post process }
  991. if success then
  992. success:=PostProcessExecutable(current_module.sharedlibfilename^,true);
  993. { Remove ReponseFile }
  994. if (success) and not(cs_link_extern in aktglobalswitches) then
  995. begin
  996. RemoveFile(outputexedir+Info.ResName);
  997. RemoveFile('base.$$$');
  998. RemoveFile('exp.$$$');
  999. end;
  1000. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1001. end;
  1002. function tlinkerwin32.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1003. type
  1004. tdosheader = packed record
  1005. e_magic : word;
  1006. e_cblp : word;
  1007. e_cp : word;
  1008. e_crlc : word;
  1009. e_cparhdr : word;
  1010. e_minalloc : word;
  1011. e_maxalloc : word;
  1012. e_ss : word;
  1013. e_sp : word;
  1014. e_csum : word;
  1015. e_ip : word;
  1016. e_cs : word;
  1017. e_lfarlc : word;
  1018. e_ovno : word;
  1019. e_res : array[0..3] of word;
  1020. e_oemid : word;
  1021. e_oeminfo : word;
  1022. e_res2 : array[0..9] of word;
  1023. e_lfanew : longint;
  1024. end;
  1025. tpeheader = packed record
  1026. PEMagic : array[0..3] of char;
  1027. Machine : word;
  1028. NumberOfSections : word;
  1029. TimeDateStamp : longint;
  1030. PointerToSymbolTable : longint;
  1031. NumberOfSymbols : longint;
  1032. SizeOfOptionalHeader : word;
  1033. Characteristics : word;
  1034. Magic : word;
  1035. MajorLinkerVersion : byte;
  1036. MinorLinkerVersion : byte;
  1037. SizeOfCode : longint;
  1038. SizeOfInitializedData : longint;
  1039. SizeOfUninitializedData : longint;
  1040. AddressOfEntryPoint : longint;
  1041. BaseOfCode : longint;
  1042. BaseOfData : longint;
  1043. ImageBase : longint;
  1044. SectionAlignment : longint;
  1045. FileAlignment : longint;
  1046. MajorOperatingSystemVersion : word;
  1047. MinorOperatingSystemVersion : word;
  1048. MajorImageVersion : word;
  1049. MinorImageVersion : word;
  1050. MajorSubsystemVersion : word;
  1051. MinorSubsystemVersion : word;
  1052. Reserved1 : longint;
  1053. SizeOfImage : longint;
  1054. SizeOfHeaders : longint;
  1055. CheckSum : longint;
  1056. Subsystem : word;
  1057. DllCharacteristics : word;
  1058. SizeOfStackReserve : longint;
  1059. SizeOfStackCommit : longint;
  1060. SizeOfHeapReserve : longint;
  1061. SizeOfHeapCommit : longint;
  1062. LoaderFlags : longint;
  1063. NumberOfRvaAndSizes : longint;
  1064. DataDirectory : array[1..$80] of byte;
  1065. end;
  1066. tcoffsechdr=packed record
  1067. name : array[0..7] of char;
  1068. vsize : longint;
  1069. rvaofs : longint;
  1070. datalen : longint;
  1071. datapos : longint;
  1072. relocpos : longint;
  1073. lineno1 : longint;
  1074. nrelocs : word;
  1075. lineno2 : word;
  1076. flags : longint;
  1077. end;
  1078. psecfill=^tsecfill;
  1079. tsecfill=record
  1080. fillpos,
  1081. fillsize : longint;
  1082. next : psecfill;
  1083. end;
  1084. var
  1085. f : file;
  1086. cmdstr : string;
  1087. dosheader : tdosheader;
  1088. peheader : tpeheader;
  1089. firstsecpos,
  1090. maxfillsize,
  1091. l,peheaderpos : longint;
  1092. coffsec : tcoffsechdr;
  1093. secroot,hsecroot : psecfill;
  1094. zerobuf : pointer;
  1095. begin
  1096. postprocessexecutable:=false;
  1097. { when -s is used or it's a dll then quit }
  1098. if (cs_link_extern in aktglobalswitches) then
  1099. begin
  1100. case apptype of
  1101. app_gui :
  1102. cmdstr:='--subsystem gui';
  1103. app_cui :
  1104. cmdstr:='--subsystem console';
  1105. end;
  1106. if dllversion<>'' then
  1107. cmdstr:=cmdstr+' --version '+dllversion;
  1108. cmdstr:=cmdstr+' --input '+fn;
  1109. cmdstr:=cmdstr+' --stack '+tostr(stacksize);
  1110. DoExec(FindUtil('postw32'),cmdstr,false,false);
  1111. postprocessexecutable:=true;
  1112. exit;
  1113. end;
  1114. { open file }
  1115. assign(f,fn);
  1116. {$I-}
  1117. reset(f,1);
  1118. if ioresult<>0 then
  1119. Message1(execinfo_f_cant_open_executable,fn);
  1120. { read headers }
  1121. blockread(f,dosheader,sizeof(tdosheader));
  1122. peheaderpos:=dosheader.e_lfanew;
  1123. seek(f,peheaderpos);
  1124. blockread(f,peheader,sizeof(tpeheader));
  1125. { write info }
  1126. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  1127. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  1128. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  1129. { change stack size (PM) }
  1130. { I am not sure that the default value is adequate !! }
  1131. peheader.SizeOfStackReserve:=stacksize;
  1132. { change the header }
  1133. { sub system }
  1134. { gui=2 }
  1135. { cui=3 }
  1136. case apptype of
  1137. app_gui :
  1138. peheader.Subsystem:=2;
  1139. app_cui :
  1140. peheader.Subsystem:=3;
  1141. end;
  1142. if dllversion<>'' then
  1143. begin
  1144. peheader.MajorImageVersion:=dllmajor;
  1145. peheader.MinorImageVersion:=dllminor;
  1146. end;
  1147. { reset timestamp }
  1148. peheader.TimeDateStamp:=0;
  1149. { write header back }
  1150. seek(f,peheaderpos);
  1151. blockwrite(f,peheader,sizeof(tpeheader));
  1152. if ioresult<>0 then
  1153. Message1(execinfo_f_cant_process_executable,fn);
  1154. seek(f,peheaderpos);
  1155. blockread(f,peheader,sizeof(tpeheader));
  1156. { write the value after the change }
  1157. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  1158. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  1159. { read section info }
  1160. maxfillsize:=0;
  1161. firstsecpos:=0;
  1162. secroot:=nil;
  1163. for l:=1 to peheader.NumberOfSections do
  1164. begin
  1165. blockread(f,coffsec,sizeof(tcoffsechdr));
  1166. if coffsec.datapos>0 then
  1167. begin
  1168. if secroot=nil then
  1169. firstsecpos:=coffsec.datapos;
  1170. new(hsecroot);
  1171. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  1172. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  1173. hsecroot^.next:=secroot;
  1174. secroot:=hsecroot;
  1175. if secroot^.fillsize>maxfillsize then
  1176. maxfillsize:=secroot^.fillsize;
  1177. end;
  1178. end;
  1179. if firstsecpos>0 then
  1180. begin
  1181. l:=firstsecpos-filepos(f);
  1182. if l>maxfillsize then
  1183. maxfillsize:=l;
  1184. end
  1185. else
  1186. l:=0;
  1187. { get zero buffer }
  1188. getmem(zerobuf,maxfillsize);
  1189. fillchar(zerobuf^,maxfillsize,0);
  1190. { zero from sectioninfo until first section }
  1191. blockwrite(f,zerobuf^,l);
  1192. { zero section alignments }
  1193. while assigned(secroot) do
  1194. begin
  1195. seek(f,secroot^.fillpos);
  1196. blockwrite(f,zerobuf^,secroot^.fillsize);
  1197. hsecroot:=secroot;
  1198. secroot:=secroot^.next;
  1199. dispose(hsecroot);
  1200. end;
  1201. freemem(zerobuf,maxfillsize);
  1202. close(f);
  1203. {$I+}
  1204. if ioresult<>0 then;
  1205. postprocessexecutable:=true;
  1206. end;
  1207. end.
  1208. {
  1209. $Log$
  1210. Revision 1.8 2000-12-30 22:53:25 peter
  1211. * export with the case provided in the exports section
  1212. Revision 1.7 2000/12/25 00:07:30 peter
  1213. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1214. tlinkedlist objects)
  1215. Revision 1.6 2000/11/12 22:20:37 peter
  1216. * create generic toutputsection for binary writers
  1217. Revision 1.5 2000/09/24 15:06:31 peter
  1218. * use defines.inc
  1219. Revision 1.4 2000/08/27 16:11:54 peter
  1220. * moved some util functions from globals,cobjects to cutils
  1221. * splitted files into finput,fmodule
  1222. Revision 1.3 2000/07/21 15:14:02 jonas
  1223. + added is_addr field for labels, if they are only used for getting the address
  1224. (e.g. for io checks) and corresponding getaddrlabel() procedure
  1225. Revision 1.2 2000/07/13 11:32:50 michael
  1226. + removed logs
  1227. }