win_targ.pas 30 KB

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