win_targ.pas 29 KB

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