win_targ.pas 29 KB

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