win_targ.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Florian Klaempfl
  4. This unit implements some support routines for the win32 target like
  5. import/export handling
  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 win_targ;
  20. interface
  21. uses import,export,link;
  22. const
  23. winstackpagesize = 4096;
  24. type
  25. pimportlibwin32=^timportlibwin32;
  26. timportlibwin32=object(timportlib)
  27. procedure preparelib(const s:string);virtual;
  28. procedure importprocedure(const func,module:string;index:longint;const name:string);virtual;
  29. procedure importvariable(const varname,module:string;const name:string);virtual;
  30. procedure generatelib;virtual;
  31. procedure generatesmartlib;
  32. end;
  33. pexportlibwin32=^texportlibwin32;
  34. texportlibwin32=object(texportlib)
  35. st : string;
  36. last_index : longint;
  37. procedure preparelib(const s:string);virtual;
  38. procedure exportprocedure(hp : pexported_item);virtual;
  39. procedure exportvar(hp : pexported_item);virtual;
  40. procedure generatelib;virtual;
  41. end;
  42. plinkerwin32=^tlinkerwin32;
  43. tlinkerwin32=object(tlinker)
  44. procedure postprocessexecutable(const n : string);virtual;
  45. end;
  46. implementation
  47. uses
  48. aasm,files,strings,globtype,globals,cobjects,systems,verbose,
  49. cpubase,cpuasm
  50. {$ifdef GDB}
  51. ,gdb
  52. {$endif}
  53. ;
  54. function DllName(Const Name : string) : string;
  55. var n : string;
  56. begin
  57. n:=Upper(SplitExtension(Name));
  58. if (n='.DLL') or (n='.DRV') or (n='.EXE') then
  59. DllName:=Name
  60. else
  61. DllName:=Name+target_os.sharedlibext;
  62. end;
  63. procedure timportlibwin32.preparelib(const s : string);
  64. begin
  65. if not(assigned(importssection)) then
  66. importssection:=new(paasmoutput,init);
  67. end;
  68. procedure timportlibwin32.importprocedure(const func,module : string;index : longint;const name : string);
  69. var
  70. hp1 : pimportlist;
  71. hp2 : pimported_item;
  72. hs : string;
  73. begin
  74. { that IS wrong for DRV files
  75. hs:=SplitName(module); }
  76. hs:=DllName(module);
  77. { search for the module }
  78. hp1:=pimportlist(current_module^.imports^.first);
  79. while assigned(hp1) do
  80. begin
  81. if hs=hp1^.dllname^ then
  82. break;
  83. hp1:=pimportlist(hp1^.next);
  84. end;
  85. { generate a new item ? }
  86. if not(assigned(hp1)) then
  87. begin
  88. hp1:=new(pimportlist,init(hs));
  89. current_module^.imports^.concat(hp1);
  90. end;
  91. { search for reuse of old import item }
  92. hp2:=pimported_item(hp1^.imported_items^.first);
  93. while assigned(hp2) do
  94. begin
  95. if hp2^.func^=func then
  96. break;
  97. hp2:=pimported_item(hp2^.next);
  98. end;
  99. if not assigned(hp2) then
  100. begin
  101. hp2:=new(pimported_item,init(func,name,index));
  102. hp1^.imported_items^.concat(hp2);
  103. end;
  104. end;
  105. procedure timportlibwin32.importvariable(const varname,module:string;const name:string);
  106. var
  107. hp1 : pimportlist;
  108. hp2 : pimported_item;
  109. hs : string;
  110. begin
  111. hs:=DllName(module);
  112. { search for the module }
  113. hp1:=pimportlist(current_module^.imports^.first);
  114. while assigned(hp1) do
  115. begin
  116. if hs=hp1^.dllname^ then
  117. break;
  118. hp1:=pimportlist(hp1^.next);
  119. end;
  120. { generate a new item ? }
  121. if not(assigned(hp1)) then
  122. begin
  123. hp1:=new(pimportlist,init(hs));
  124. current_module^.imports^.concat(hp1);
  125. end;
  126. hp2:=new(pimported_item,init_var(varname,name));
  127. hp1^.imported_items^.concat(hp2);
  128. end;
  129. procedure timportlibwin32.generatesmartlib;
  130. var
  131. hp1 : pimportlist;
  132. hp2 : pimported_item;
  133. lhead,lname,lcode,
  134. lidata4,lidata5 : pasmlabel;
  135. r : preference;
  136. begin
  137. hp1:=pimportlist(current_module^.imports^.first);
  138. while assigned(hp1) do
  139. begin
  140. importssection^.concat(new(pai_cut,init));
  141. codesegment^.concat(new(pai_cut,init));
  142. { create header for this importmodule }
  143. { Get labels for the sections }
  144. getdatalabel(lhead);
  145. getdatalabel(lname);
  146. getlabel(lidata4);
  147. getlabel(lidata5);
  148. importssection^.concat(new(pai_section,init(sec_idata2)));
  149. importssection^.concat(new(pai_label,init(lhead)));
  150. { pointer to procedure names }
  151. importssection^.concat(new(pai_const_symbol,init_rva(lidata4)));
  152. { two empty entries follow }
  153. importssection^.concat(new(pai_const,init_32bit(0)));
  154. importssection^.concat(new(pai_const,init_32bit(0)));
  155. { pointer to dll name }
  156. importssection^.concat(new(pai_const_symbol,init_rva(lname)));
  157. { pointer to fixups }
  158. importssection^.concat(new(pai_const_symbol,init_rva(lidata5)));
  159. { first write the name references }
  160. importssection^.concat(new(pai_section,init(sec_idata4)));
  161. importssection^.concat(new(pai_const,init_32bit(0)));
  162. importssection^.concat(new(pai_label,init(lidata4)));
  163. { then the addresses and create also the indirect jump }
  164. importssection^.concat(new(pai_section,init(sec_idata5)));
  165. importssection^.concat(new(pai_const,init_32bit(0)));
  166. importssection^.concat(new(pai_label,init(lidata5)));
  167. { write final section }
  168. importssection^.concat(new(pai_cut,init_end));
  169. { end of name references }
  170. importssection^.concat(new(pai_section,init(sec_idata4)));
  171. importssection^.concat(new(pai_const,init_32bit(0)));
  172. { end if addresses }
  173. importssection^.concat(new(pai_section,init(sec_idata5)));
  174. importssection^.concat(new(pai_const,init_32bit(0)));
  175. { dllname }
  176. importssection^.concat(new(pai_section,init(sec_idata7)));
  177. importssection^.concat(new(pai_label,init(lname)));
  178. importssection^.concat(new(pai_string,init(hp1^.dllname^+{target_os.sharedlibext+}#0)));
  179. { create procedures }
  180. hp2:=pimported_item(hp1^.imported_items^.first);
  181. while assigned(hp2) do
  182. begin
  183. { insert cuts }
  184. importssection^.concat(new(pai_cut,init));
  185. { create indirect jump }
  186. if not hp2^.is_var then
  187. begin
  188. getlabel(lcode);
  189. new(r);
  190. reset_reference(r^);
  191. r^.symbol:=lcode;
  192. { place jump in codesegment, insert a code section in the
  193. importsection to reduce the amount of .s files (PFV) }
  194. importssection^.concat(new(pai_section,init(sec_code)));
  195. {$IfDef GDB}
  196. if (cs_debuginfo in aktmoduleswitches) then
  197. importssection^.concat(new(pai_stab_function_name,init(nil)));
  198. {$EndIf GDB}
  199. importssection^.concat(new(pai_align,init_op(4,$90)));
  200. importssection^.concat(new(pai_symbol,initname_global(hp2^.func^,0)));
  201. importssection^.concat(new(paicpu,op_ref(A_JMP,S_NO,r)));
  202. end;
  203. { create head link }
  204. importssection^.concat(new(pai_section,init(sec_idata7)));
  205. importssection^.concat(new(pai_const_symbol,init_rva(lhead)));
  206. { fixup }
  207. getlabel(pasmlabel(hp2^.lab));
  208. importssection^.concat(new(pai_section,init(sec_idata4)));
  209. importssection^.concat(new(pai_const_symbol,init_rva(hp2^.lab)));
  210. { add jump field to importsection }
  211. importssection^.concat(new(pai_section,init(sec_idata5)));
  212. if hp2^.is_var then
  213. importssection^.concat(new(pai_symbol,initname_global(hp2^.func^,0)))
  214. else
  215. importssection^.concat(new(pai_label,init(lcode)));
  216. if hp2^.name^<>'' then
  217. importssection^.concat(new(pai_const_symbol,init_rva(hp2^.lab)))
  218. else
  219. importssection^.concat(new(pai_const,init_32bit($80000000 or hp2^.ordnr)));
  220. { finally the import information }
  221. importssection^.concat(new(pai_section,init(sec_idata6)));
  222. importssection^.concat(new(pai_label,init(hp2^.lab)));
  223. importssection^.concat(new(pai_const,init_16bit(hp2^.ordnr)));
  224. importssection^.concat(new(pai_string,init(hp2^.name^+#0)));
  225. importssection^.concat(new(pai_align,init_op(2,0)));
  226. hp2:=pimported_item(hp2^.next);
  227. end;
  228. hp1:=pimportlist(hp1^.next);
  229. end;
  230. end;
  231. procedure timportlibwin32.generatelib;
  232. var
  233. hp1 : pimportlist;
  234. hp2 : pimported_item;
  235. l1,l2,l3,l4 : pasmlabel;
  236. r : preference;
  237. begin
  238. if (cs_smartlink in aktmoduleswitches) then
  239. begin
  240. generatesmartlib;
  241. exit;
  242. end;
  243. hp1:=pimportlist(current_module^.imports^.first);
  244. while assigned(hp1) do
  245. begin
  246. { Insert cuts for smartlinking }
  247. if (cs_smartlink in aktmoduleswitches) then
  248. begin
  249. importssection^.concat(new(pai_cut,init));
  250. codesegment^.concat(new(pai_cut,init));
  251. end;
  252. {$IfDef GDB}
  253. if (cs_debuginfo in aktmoduleswitches) then
  254. codesegment^.concat(new(pai_stab_function_name,init(nil)));
  255. {$EndIf GDB}
  256. { Get labels for the sections }
  257. getlabel(l1);
  258. getlabel(l2);
  259. getlabel(l3);
  260. importssection^.concat(new(pai_section,init(sec_idata2)));
  261. { pointer to procedure names }
  262. importssection^.concat(new(pai_const_symbol,init_rva(l2)));
  263. { two empty entries follow }
  264. importssection^.concat(new(pai_const,init_32bit(0)));
  265. importssection^.concat(new(pai_const,init_32bit(0)));
  266. { pointer to dll name }
  267. importssection^.concat(new(pai_const_symbol,init_rva(l1)));
  268. { pointer to fixups }
  269. importssection^.concat(new(pai_const_symbol,init_rva(l3)));
  270. { only create one section for each else it will
  271. create a lot of idata* }
  272. { first write the name references }
  273. importssection^.concat(new(pai_section,init(sec_idata4)));
  274. importssection^.concat(new(pai_label,init(l2)));
  275. hp2:=pimported_item(hp1^.imported_items^.first);
  276. while assigned(hp2) do
  277. begin
  278. getlabel(pasmlabel(hp2^.lab));
  279. if hp2^.name^<>'' then
  280. importssection^.concat(new(pai_const_symbol,init_rva(hp2^.lab)))
  281. else
  282. importssection^.concat(new(pai_const,init_32bit($80000000 or hp2^.ordnr)));
  283. hp2:=pimported_item(hp2^.next);
  284. end;
  285. { finalize the names ... }
  286. importssection^.concat(new(pai_const,init_32bit(0)));
  287. { then the addresses and create also the indirect jump }
  288. importssection^.concat(new(pai_section,init(sec_idata5)));
  289. importssection^.concat(new(pai_label,init(l3)));
  290. hp2:=pimported_item(hp1^.imported_items^.first);
  291. while assigned(hp2) do
  292. begin
  293. if not hp2^.is_var then
  294. begin
  295. getdatalabel(l4);
  296. { create indirect jump }
  297. new(r);
  298. reset_reference(r^);
  299. r^.symbol:=l4;
  300. { place jump in codesegment }
  301. codesegment^.concat(new(pai_align,init_op(4,$90)));
  302. codesegment^.concat(new(pai_symbol,initname_global(hp2^.func^,0)));
  303. codesegment^.concat(new(paicpu,op_ref(A_JMP,S_NO,r)));
  304. { add jump field to importsection }
  305. importssection^.concat(new(pai_label,init(l4)));
  306. end
  307. else
  308. begin
  309. importssection^.concat(new(pai_symbol,initname_global(hp2^.func^,0)));
  310. end;
  311. importssection^.concat(new(pai_const_symbol,init_rva(hp2^.lab)));
  312. hp2:=pimported_item(hp2^.next);
  313. end;
  314. { finalize the addresses }
  315. importssection^.concat(new(pai_const,init_32bit(0)));
  316. { finally the import information }
  317. importssection^.concat(new(pai_section,init(sec_idata6)));
  318. hp2:=pimported_item(hp1^.imported_items^.first);
  319. while assigned(hp2) do
  320. begin
  321. importssection^.concat(new(pai_label,init(hp2^.lab)));
  322. { the ordinal number }
  323. importssection^.concat(new(pai_const,init_16bit(hp2^.ordnr)));
  324. importssection^.concat(new(pai_string,init(hp2^.name^+#0)));
  325. importssection^.concat(new(pai_align,init_op(2,0)));
  326. hp2:=pimported_item(hp2^.next);
  327. end;
  328. { create import dll name }
  329. importssection^.concat(new(pai_section,init(sec_idata7)));
  330. importssection^.concat(new(pai_label,init(l1)));
  331. importssection^.concat(new(pai_string,init(hp1^.dllname^+{target_os.sharedlibext+}#0)));
  332. hp1:=pimportlist(hp1^.next);
  333. end;
  334. end;
  335. procedure texportlibwin32.preparelib(const s:string);
  336. begin
  337. if not(assigned(exportssection)) then
  338. exportssection:=new(paasmoutput,init);
  339. last_index:=0;
  340. end;
  341. procedure texportlibwin32.exportvar(hp : pexported_item);
  342. begin
  343. { same code used !! PM }
  344. exportprocedure(hp);
  345. end;
  346. procedure texportlibwin32.exportprocedure(hp : pexported_item);
  347. { must be ordered at least for win32 !! }
  348. var hp2 : pexported_item;
  349. begin
  350. hp2:=pexported_item(current_module^._exports^.first);
  351. { first test the index value }
  352. if (hp^.options and eo_index)<>0 then
  353. begin
  354. if (hp^.index<=0) or (hp^.index>$ffff) then
  355. message1(parser_e_export_invalid_index,tostr(hp^.index))
  356. else while assigned(hp2) do
  357. begin
  358. if (hp^.index=hp2^.index) then
  359. if ((hp2^.options and eo_index)<>0) then
  360. message1(parser_e_export_ordinal_double,tostr(hp^.index))
  361. else
  362. begin
  363. inc(last_index);
  364. hp2^.index:=last_index;
  365. end;
  366. hp2:=pexported_item(hp2^.next);
  367. end;
  368. if hp^.index=last_index+1 then
  369. inc(last_index);
  370. end
  371. else
  372. begin
  373. inc(last_index);
  374. hp^.index:=last_index;
  375. end;
  376. { use pascal name is none specified }
  377. if (hp^.options and eo_name)=0 then
  378. begin
  379. hp^.name:=stringdup(hp^.sym^.name);
  380. hp^.options:=hp^.options or eo_name;
  381. end;
  382. { now place in correct order }
  383. hp2:=pexported_item(current_module^._exports^.first);
  384. while assigned(hp2) and
  385. (hp^.name^>hp2^.name^) do
  386. hp2:=pexported_item(hp2^.next);
  387. { insert hp there !! }
  388. if assigned(hp2) and (hp2^.name^=hp^.name^) then
  389. begin
  390. { this is not allowed !! }
  391. message1(parser_e_export_name_double,hp^.name^);
  392. end;
  393. if hp2=pexported_item(current_module^._exports^.first) then
  394. current_module^._exports^.insert(hp)
  395. else if assigned(hp2) then
  396. begin
  397. hp^.next:=hp2;
  398. hp^.previous:=hp2^.previous;
  399. if assigned(hp2^.previous) then
  400. hp2^.previous^.next:=hp;
  401. hp2^.previous:=hp;
  402. end
  403. else
  404. current_module^._exports^.concat(hp);
  405. end;
  406. procedure texportlibwin32.generatelib;
  407. var
  408. ordinal_base,ordinal_max,ordinal_min : longint;
  409. current_index : longint;
  410. entries,named_entries : longint;
  411. name_label,dll_name_label,export_address_table : pasmlabel;
  412. export_name_table_pointers,export_ordinal_table : pasmlabel;
  413. hp,hp2 : pexported_item;
  414. tempexport : plinkedlist;
  415. address_table,name_table_pointers,
  416. name_table,ordinal_table : paasmoutput;
  417. begin
  418. ordinal_max:=0;
  419. ordinal_min:=$7FFFFFFF;
  420. entries:=0;
  421. named_entries:=0;
  422. getlabel(dll_name_label);
  423. getlabel(export_address_table);
  424. getlabel(export_name_table_pointers);
  425. getlabel(export_ordinal_table);
  426. hp:=pexported_item(current_module^._exports^.first);
  427. { count entries }
  428. while assigned(hp) do
  429. begin
  430. inc(entries);
  431. if (hp^.index>ordinal_max) then
  432. ordinal_max:=hp^.index;
  433. if (hp^.index>0) and (hp^.index<ordinal_min) then
  434. ordinal_min:=hp^.index;
  435. if assigned(hp^.name) then
  436. inc(named_entries);
  437. hp:=pexported_item(hp^.next);
  438. end;
  439. { no support for higher ordinal base yet !! }
  440. ordinal_base:=1;
  441. current_index:=ordinal_base;
  442. { we must also count the holes !! }
  443. entries:=ordinal_max-ordinal_base+1;
  444. exportssection^.concat(new(pai_section,init(sec_edata)));
  445. { export flags }
  446. exportssection^.concat(new(pai_const,init_32bit(0)));
  447. { date/time stamp }
  448. exportssection^.concat(new(pai_const,init_32bit(0)));
  449. { major version }
  450. exportssection^.concat(new(pai_const,init_16bit(0)));
  451. { minor version }
  452. exportssection^.concat(new(pai_const,init_16bit(0)));
  453. { pointer to dll name }
  454. exportssection^.concat(new(pai_const_symbol,init_rva(dll_name_label)));
  455. { ordinal base normally set to 1 }
  456. exportssection^.concat(new(pai_const,init_32bit(ordinal_base)));
  457. { number of entries }
  458. exportssection^.concat(new(pai_const,init_32bit(entries)));
  459. { number of named entries }
  460. exportssection^.concat(new(pai_const,init_32bit(named_entries)));
  461. { address of export address table }
  462. exportssection^.concat(new(pai_const_symbol,init_rva(export_address_table)));
  463. { address of name pointer pointers }
  464. exportssection^.concat(new(pai_const_symbol,init_rva(export_name_table_pointers)));
  465. { address of ordinal number pointers }
  466. exportssection^.concat(new(pai_const_symbol,init_rva(export_ordinal_table)));
  467. { the name }
  468. exportssection^.concat(new(pai_label,init(dll_name_label)));
  469. if st='' then
  470. exportssection^.concat(new(pai_string,init(current_module^.modulename^+target_os.sharedlibext+#0)))
  471. else
  472. exportssection^.concat(new(pai_string,init(st+target_os.sharedlibext+#0)));
  473. { export address table }
  474. address_table:=new(paasmoutput,init);
  475. address_table^.concat(new(pai_align,init_op(4,0)));
  476. address_table^.concat(new(pai_label,init(export_address_table)));
  477. name_table_pointers:=new(paasmoutput,init);
  478. name_table_pointers^.concat(new(pai_align,init_op(4,0)));
  479. name_table_pointers^.concat(new(pai_label,init(export_name_table_pointers)));
  480. ordinal_table:=new(paasmoutput,init);
  481. ordinal_table^.concat(new(pai_align,init_op(4,0)));
  482. ordinal_table^.concat(new(pai_label,init(export_ordinal_table)));
  483. name_table:=new(paasmoutput,init);
  484. name_table^.concat(new(pai_align,init_op(4,0)));
  485. { write each address }
  486. hp:=pexported_item(current_module^._exports^.first);
  487. while assigned(hp) do
  488. begin
  489. if (hp^.options and eo_name)<>0 then
  490. begin
  491. getlabel(name_label);
  492. name_table_pointers^.concat(new(pai_const_symbol,init_rva(name_label)));
  493. ordinal_table^.concat(new(pai_const,init_16bit(hp^.index-ordinal_base)));
  494. name_table^.concat(new(pai_align,init_op(2,0)));
  495. name_table^.concat(new(pai_label,init(name_label)));
  496. name_table^.concat(new(pai_string,init(hp^.name^+#0)));
  497. end;
  498. hp:=pexported_item(hp^.next);
  499. end;
  500. { order in increasing ordinal values }
  501. { into tempexport list }
  502. tempexport:=new(plinkedlist,init);
  503. hp:=pexported_item(current_module^._exports^.first);
  504. while assigned(hp) do
  505. begin
  506. current_module^._exports^.remove(hp);
  507. hp2:=pexported_item(tempexport^.first);
  508. while assigned(hp2) and (hp^.index>hp2^.index) do
  509. begin
  510. hp2:=pexported_item(hp2^.next);
  511. end;
  512. if hp2=pexported_item(tempexport^.first) then
  513. tempexport^.insert(hp)
  514. else
  515. begin
  516. if assigned(hp2) then
  517. begin
  518. hp^.next:=hp2;
  519. hp^.previous:=hp2^.previous;
  520. hp2^.previous:=hp;
  521. if assigned(hp^.previous) then
  522. hp^.previous^.next:=hp;
  523. end
  524. else
  525. tempexport^.concat(hp);
  526. end;
  527. hp:=pexported_item(current_module^._exports^.first);;
  528. end;
  529. { write the export adress table }
  530. current_index:=ordinal_base;
  531. hp:=pexported_item(tempexport^.first);
  532. while assigned(hp) do
  533. begin
  534. { fill missing values }
  535. while current_index<hp^.index do
  536. begin
  537. address_table^.concat(new(pai_const,init_32bit(0)));
  538. inc(current_index);
  539. end;
  540. address_table^.concat(new(pai_const_symbol,initname_rva(hp^.sym^.mangledname)));
  541. inc(current_index);
  542. hp:=pexported_item(hp^.next);
  543. end;
  544. exportssection^.concatlist(address_table);
  545. exportssection^.concatlist(name_table_pointers);
  546. exportssection^.concatlist(ordinal_table);
  547. exportssection^.concatlist(name_table);
  548. dispose(address_table,done);
  549. dispose(name_table_pointers,done);
  550. dispose(ordinal_table,done);
  551. dispose(name_table,done);
  552. dispose(tempexport,done);
  553. end;
  554. {****************************************************************************
  555. Postprocess Executable
  556. ****************************************************************************}
  557. procedure tlinkerwin32.postprocessexecutable(const n : string);
  558. type
  559. tdosheader = packed record
  560. e_magic : word;
  561. e_cblp : word;
  562. e_cp : word;
  563. e_crlc : word;
  564. e_cparhdr : word;
  565. e_minalloc : word;
  566. e_maxalloc : word;
  567. e_ss : word;
  568. e_sp : word;
  569. e_csum : word;
  570. e_ip : word;
  571. e_cs : word;
  572. e_lfarlc : word;
  573. e_ovno : word;
  574. e_res : array[0..3] of word;
  575. e_oemid : word;
  576. e_oeminfo : word;
  577. e_res2 : array[0..9] of word;
  578. e_lfanew : longint;
  579. end;
  580. tpeheader = packed record
  581. PEMagic : array[0..3] of char;
  582. Machine : word;
  583. NumberOfSections : word;
  584. TimeDateStamp : longint;
  585. PointerToSymbolTable : longint;
  586. NumberOfSymbols : longint;
  587. SizeOfOptionalHeader : word;
  588. Characteristics : word;
  589. Magic : word;
  590. MajorLinkerVersion : byte;
  591. MinorLinkerVersion : byte;
  592. SizeOfCode : longint;
  593. SizeOfInitializedData : longint;
  594. SizeOfUninitializedData : longint;
  595. AddressOfEntryPoint : longint;
  596. BaseOfCode : longint;
  597. BaseOfData : longint;
  598. ImageBase : longint;
  599. SectionAlignment : longint;
  600. FileAlignment : longint;
  601. MajorOperatingSystemVersion : word;
  602. MinorOperatingSystemVersion : word;
  603. MajorImageVersion : word;
  604. MinorImageVersion : word;
  605. MajorSubsystemVersion : word;
  606. MinorSubsystemVersion : word;
  607. Reserved1 : longint;
  608. SizeOfImage : longint;
  609. SizeOfHeaders : longint;
  610. CheckSum : longint;
  611. Subsystem : word;
  612. DllCharacteristics : word;
  613. SizeOfStackReserve : longint;
  614. SizeOfStackCommit : longint;
  615. SizeOfHeapReserve : longint;
  616. SizeOfHeapCommit : longint;
  617. LoaderFlags : longint;
  618. NumberOfRvaAndSizes : longint;
  619. DataDirectory : array[1..$80] of byte;
  620. end;
  621. tcoffsechdr=packed record
  622. name : array[0..7] of char;
  623. vsize : longint;
  624. rvaofs : longint;
  625. datalen : longint;
  626. datapos : longint;
  627. relocpos : longint;
  628. lineno1 : longint;
  629. nrelocs : word;
  630. lineno2 : word;
  631. flags : longint;
  632. end;
  633. psecfill=^tsecfill;
  634. tsecfill=record
  635. fillpos,
  636. fillsize : longint;
  637. next : psecfill;
  638. end;
  639. var
  640. f : file;
  641. dosheader : tdosheader;
  642. peheader : tpeheader;
  643. firstsecpos,
  644. maxfillsize,
  645. l,peheaderpos : longint;
  646. coffsec : tcoffsechdr;
  647. secroot,hsecroot : psecfill;
  648. zerobuf : pointer;
  649. begin
  650. { when -s is used quit, because there is no .exe }
  651. if cs_link_extern in aktglobalswitches then
  652. exit;
  653. { open file }
  654. assign(f,n);
  655. {$I-}
  656. reset(f,1);
  657. if ioresult<>0 then
  658. Message1(execinfo_f_cant_open_executable,n);
  659. { read headers }
  660. blockread(f,dosheader,sizeof(tdosheader));
  661. peheaderpos:=dosheader.e_lfanew;
  662. seek(f,peheaderpos);
  663. blockread(f,peheader,sizeof(tpeheader));
  664. { write info }
  665. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  666. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  667. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  668. { change stack size (PM) }
  669. { I am not sure that the default value is adequate !! }
  670. peheader.SizeOfStackReserve:=stacksize;
  671. { change the header }
  672. { sub system }
  673. { gui=2 }
  674. { cui=3 }
  675. if apptype=at_gui then
  676. peheader.Subsystem:=2
  677. else if apptype=at_cui then
  678. peheader.Subsystem:=3;
  679. seek(f,peheaderpos);
  680. blockwrite(f,peheader,sizeof(tpeheader));
  681. if ioresult<>0 then
  682. Message1(execinfo_f_cant_process_executable,n);
  683. seek(f,peheaderpos);
  684. blockread(f,peheader,sizeof(tpeheader));
  685. { write the value after the change }
  686. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  687. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  688. { read section info }
  689. maxfillsize:=0;
  690. firstsecpos:=0;
  691. secroot:=nil;
  692. for l:=1to peheader.NumberOfSections do
  693. begin
  694. blockread(f,coffsec,sizeof(tcoffsechdr));
  695. if coffsec.datapos>0 then
  696. begin
  697. if secroot=nil then
  698. firstsecpos:=coffsec.datapos;
  699. new(hsecroot);
  700. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  701. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  702. hsecroot^.next:=secroot;
  703. secroot:=hsecroot;
  704. if secroot^.fillsize>maxfillsize then
  705. maxfillsize:=secroot^.fillsize;
  706. end;
  707. end;
  708. if firstsecpos>0 then
  709. begin
  710. l:=firstsecpos-filepos(f);
  711. if l>maxfillsize then
  712. maxfillsize:=l;
  713. end
  714. else
  715. l:=0;
  716. { get zero buffer }
  717. getmem(zerobuf,maxfillsize);
  718. fillchar(zerobuf^,maxfillsize,0);
  719. { zero from sectioninfo until first section }
  720. blockwrite(f,zerobuf^,l);
  721. { zero section alignments }
  722. while assigned(secroot) do
  723. begin
  724. seek(f,secroot^.fillpos);
  725. blockwrite(f,zerobuf^,secroot^.fillsize);
  726. hsecroot:=secroot;
  727. secroot:=secroot^.next;
  728. dispose(hsecroot);
  729. end;
  730. freemem(zerobuf,maxfillsize);
  731. close(f);
  732. {$I+}
  733. end;
  734. end.
  735. {
  736. $Log$
  737. Revision 1.33 1999-08-25 12:00:07 jonas
  738. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  739. Revision 1.32 1999/08/11 17:26:38 peter
  740. * tlinker object is now inherited for win32 and dos
  741. * postprocessexecutable is now a method of tlinker
  742. Revision 1.31 1999/08/04 00:23:50 florian
  743. * renamed i386asm and i386base to cpuasm and cpubase
  744. Revision 1.30 1999/07/29 20:54:11 peter
  745. * write .size also
  746. Revision 1.29 1999/07/22 16:12:28 peter
  747. * merged
  748. Revision 1.28 1999/07/18 10:20:03 florian
  749. * made it compilable with Dlephi 4 again
  750. + fixed problem with large stack allocations on win32
  751. Revision 1.27.2.1 1999/07/22 16:09:30 peter
  752. * reuse old import entries
  753. Revision 1.27 1999/05/27 19:45:30 peter
  754. * removed oldasm
  755. * plabel -> pasmlabel
  756. * -a switches to source writing automaticly
  757. * assembler readers OOPed
  758. * asmsymbol automaticly external
  759. * jumptables and other label fixes for asm readers
  760. Revision 1.26 1999/05/21 13:55:24 peter
  761. * NEWLAB for label as symbol
  762. Revision 1.25 1999/05/17 13:02:13 pierre
  763. * -Csmmm works for win32 but default is set to 32Mb
  764. Revision 1.24 1999/05/01 13:25:04 peter
  765. * merged nasm compiler
  766. * old asm moved to oldasm/
  767. Revision 1.23 1999/04/07 14:18:32 pierre
  768. * typo correction
  769. Revision 1.22 1999/04/07 14:04:40 pierre
  770. * adds .dll as library suffix only if
  771. the name does not end with .dll .drv or .exe !
  772. Revision 1.21 1999/02/25 21:02:59 peter
  773. * ag386bin updates
  774. + coff writer
  775. Revision 1.20 1999/02/22 02:44:14 peter
  776. * ag386bin doesn't use i386.pas anymore
  777. Revision 1.19 1998/12/11 00:04:06 peter
  778. + globtype,tokens,version unit splitted from globals
  779. Revision 1.18 1998/12/02 10:26:13 pierre
  780. * writing of .edata was wrong for indexes above number of exported items
  781. * importing by index only did not work !
  782. Revision 1.17 1998/12/01 23:35:43 pierre
  783. * alignment fixes
  784. Revision 1.16 1998/11/30 13:26:26 pierre
  785. * the code for ordering the exported procs/vars was buggy
  786. + added -WB to force binding (Ozerski way of creating DLL)
  787. this is off by default as direct writing of .edata section seems
  788. OK
  789. Revision 1.15 1998/11/30 09:43:25 pierre
  790. * some range check bugs fixed (still not working !)
  791. + added DLL writing support for win32 (also accepts variables)
  792. + TempAnsi for code that could be used for Temporary ansi strings
  793. handling
  794. Revision 1.14 1998/11/28 16:21:00 peter
  795. + support for dll variables
  796. Revision 1.13 1998/10/29 11:35:54 florian
  797. * some dll support for win32
  798. * fixed assembler writing for PalmOS
  799. Revision 1.12 1998/10/27 10:22:35 florian
  800. + First things for win32 export sections
  801. Revision 1.11 1998/10/22 17:54:09 florian
  802. + switch $APPTYPE for win32 added
  803. Revision 1.10 1998/10/22 15:18:51 florian
  804. + switch -vx for win32 added
  805. Revision 1.9 1998/10/19 15:41:03 peter
  806. * better splitname to support glib-1.1.dll alike names
  807. Revision 1.8 1998/09/07 18:33:35 peter
  808. + smartlinking for win95 imports
  809. Revision 1.7 1998/09/03 17:39:06 florian
  810. + better code for type conversation longint/dword to real type
  811. Revision 1.6 1998/08/10 14:50:38 peter
  812. + localswitches, moduleswitches, globalswitches splitting
  813. Revision 1.5 1998/06/10 10:43:18 peter
  814. * write also the .dll extension (needed for NT)
  815. Revision 1.4 1998/06/08 22:59:56 peter
  816. * smartlinking works for win32
  817. * some defines to exclude some compiler parts
  818. Revision 1.3 1998/06/04 23:52:06 peter
  819. * m68k compiles
  820. + .def file creation moved to gendef.pas so it could also be used
  821. for win32
  822. Revision 1.2 1998/05/06 18:36:55 peter
  823. * tai_section extended with code,data,bss sections and enumerated type
  824. * ident 'compiled by FPC' moved to pmodules
  825. * small fix for smartlink
  826. }