win_targ.pas 31 KB

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