win_targ.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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;
  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 generatelib;virtual;
  28. procedure generatesmartlib;
  29. end;
  30. { sets some flags of the executable }
  31. procedure postprocessexecutable(n : string);
  32. implementation
  33. uses
  34. aasm,files,strings,globals,cobjects,systems,verbose
  35. {$ifdef GDB}
  36. ,gdb
  37. {$endif}
  38. {$ifdef i386}
  39. ,i386
  40. {$endif}
  41. ;
  42. type
  43. tdosheader = packed record
  44. e_magic : word;
  45. e_cblp : word;
  46. e_cp : word;
  47. e_crlc : word;
  48. e_cparhdr : word;
  49. e_minalloc : word;
  50. e_maxalloc : word;
  51. e_ss : word;
  52. e_sp : word;
  53. e_csum : word;
  54. e_ip : word;
  55. e_cs : word;
  56. e_lfarlc : word;
  57. e_ovno : word;
  58. e_res : array[0..3] of word;
  59. e_oemid : word;
  60. e_oeminfo : word;
  61. e_res2 : array[0..9] of word;
  62. e_lfanew : longint;
  63. end;
  64. tpeheader = packed record
  65. PEMagic : array[0..3] of char;
  66. Machine : word;
  67. NumberOfSections : word;
  68. TimeDateStamp : longint;
  69. PointerToSymbolTable : longint;
  70. NumberOfSymbols : longint;
  71. SizeOfOptionalHeader : word;
  72. Characteristics : word;
  73. Magic : word;
  74. MajorLinkerVersion : byte;
  75. MinorLinkerVersion : byte;
  76. SizeOfCode : longint;
  77. SizeOfInitializedData : longint;
  78. SizeOfUninitializedData : longint;
  79. AddressOfEntryPoint : longint;
  80. BaseOfCode : longint;
  81. BaseOfData : longint;
  82. ImageBase : longint;
  83. SectionAlignment : longint;
  84. FileAlignment : longint;
  85. MajorOperatingSystemVersion : word;
  86. MinorOperatingSystemVersion : word;
  87. MajorImageVersion : word;
  88. MinorImageVersion : word;
  89. MajorSubsystemVersion : word;
  90. MinorSubsystemVersion : word;
  91. Reserved1 : longint;
  92. SizeOfImage : longint;
  93. SizeOfHeaders : longint;
  94. CheckSum : longint;
  95. Subsystem : word;
  96. DllCharacteristics : word;
  97. SizeOfStackReserve : longint;
  98. SizeOfStackCommit : longint;
  99. SizeOfHeapReserve : longint;
  100. SizeOfHeapCommit : longint;
  101. LoaderFlags : longint;
  102. NumberOfRvaAndSizes : longint;
  103. { DataDirectory : array[0..(IMAGE_NUMBEROF_DIRECTORY_ENTRIES)-1] of IMAGE_DATA_DIRECTORY; }
  104. end;
  105. procedure timportlibwin32.preparelib(const s : string);
  106. begin
  107. if not(assigned(importssection)) then
  108. importssection:=new(paasmoutput,init);
  109. end;
  110. procedure timportlibwin32.importprocedure(const func,module : string;index : longint;const name : string);
  111. var
  112. hp1 : pimportlist;
  113. hp2 : pimported_procedure;
  114. hs : string;
  115. begin
  116. hs:=SplitName(module);
  117. { search for the module }
  118. hp1:=pimportlist(current_module^.imports^.first);
  119. while assigned(hp1) do
  120. begin
  121. if hs=hp1^.dllname^ then
  122. break;
  123. hp1:=pimportlist(hp1^.next);
  124. end;
  125. { generate a new item ? }
  126. if not(assigned(hp1)) then
  127. begin
  128. hp1:=new(pimportlist,init(hs));
  129. current_module^.imports^.concat(hp1);
  130. end;
  131. hp2:=new(pimported_procedure,init(func,name,index));
  132. hp1^.imported_procedures^.concat(hp2);
  133. end;
  134. procedure timportlibwin32.generatesmartlib;
  135. var
  136. hp1 : pimportlist;
  137. hp2 : pimported_procedure;
  138. lhead,lname,lcode,
  139. lidata4,lidata5 : plabel;
  140. r : preference;
  141. begin
  142. hp1:=pimportlist(current_module^.imports^.first);
  143. while assigned(hp1) do
  144. begin
  145. importssection^.concat(new(pai_cut,init));
  146. codesegment^.concat(new(pai_cut,init));
  147. { create header for this importmodule }
  148. { Get labels for the sections }
  149. getdatalabel(lhead);
  150. getdatalabel(lname);
  151. getlabel(lidata4);
  152. getlabel(lidata5);
  153. importssection^.concat(new(pai_section,init_idata(2)));
  154. importssection^.concat(new(pai_label,init(lhead)));
  155. { pointer to procedure names }
  156. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(lidata4)))));
  157. { two empty entries follow }
  158. importssection^.concat(new(pai_const,init_32bit(0)));
  159. importssection^.concat(new(pai_const,init_32bit(0)));
  160. { pointer to dll name }
  161. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(lname)))));
  162. { pointer to fixups }
  163. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(lidata5)))));
  164. { first write the name references }
  165. importssection^.concat(new(pai_section,init_idata(4)));
  166. importssection^.concat(new(pai_const,init_32bit(0)));
  167. importssection^.concat(new(pai_label,init(lidata4)));
  168. { then the addresses and create also the indirect jump }
  169. importssection^.concat(new(pai_section,init_idata(5)));
  170. importssection^.concat(new(pai_const,init_32bit(0)));
  171. importssection^.concat(new(pai_label,init(lidata5)));
  172. { write final section }
  173. importssection^.concat(new(pai_cut,init_end));
  174. { end of name references }
  175. importssection^.concat(new(pai_section,init_idata(4)));
  176. importssection^.concat(new(pai_const,init_32bit(0)));
  177. { end if addresses }
  178. importssection^.concat(new(pai_section,init_idata(5)));
  179. importssection^.concat(new(pai_const,init_32bit(0)));
  180. { dllname }
  181. importssection^.concat(new(pai_section,init_idata(7)));
  182. importssection^.concat(new(pai_label,init(lname)));
  183. importssection^.concat(new(pai_string,init(hp1^.dllname^+target_os.sharedlibext+#0)));
  184. { create procedures }
  185. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  186. while assigned(hp2) do
  187. begin
  188. { insert cuts }
  189. importssection^.concat(new(pai_cut,init));
  190. { create indirect jump }
  191. getlabel(lcode);
  192. new(r);
  193. reset_reference(r^);
  194. r^.symbol:=stringdup(lab2str(lcode));
  195. { place jump in codesegment, insert a code section in the
  196. importsection to reduce the amount of .s files (PFV) }
  197. importssection^.concat(new(pai_section,init(sec_code)));
  198. {$IfDef GDB}
  199. if (cs_debuginfo in aktmoduleswitches) then
  200. importssection^.concat(new(pai_stab_function_name,init(nil)));
  201. {$EndIf GDB}
  202. importssection^.concat(new(pai_align,init_op(4,$90)));
  203. importssection^.concat(new(pai_symbol,init_global(hp2^.func^)));
  204. importssection^.concat(new(pai386,op_ref(A_JMP,S_NO,r)));
  205. { create head link }
  206. importssection^.concat(new(pai_section,init_idata(7)));
  207. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(lhead)))));
  208. { fixup }
  209. getlabel(plabel(hp2^.lab));
  210. importssection^.concat(new(pai_section,init_idata(4)));
  211. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(hp2^.lab)))));
  212. { add jump field to importsection }
  213. importssection^.concat(new(pai_section,init_idata(5)));
  214. importssection^.concat(new(pai_label,init(lcode)));
  215. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(hp2^.lab)))));
  216. { finally the import information }
  217. importssection^.concat(new(pai_section,init_idata(6)));
  218. importssection^.concat(new(pai_label,init(hp2^.lab)));
  219. importssection^.concat(new(pai_const,init_16bit(hp2^.ordnr)));
  220. importssection^.concat(new(pai_string,init(hp2^.name^+#0)));
  221. hp2:=pimported_procedure(hp2^.next);
  222. end;
  223. hp1:=pimportlist(hp1^.next);
  224. end;
  225. end;
  226. procedure timportlibwin32.generatelib;
  227. var
  228. hp1 : pimportlist;
  229. hp2 : pimported_procedure;
  230. l1,l2,l3,l4 : plabel;
  231. r : preference;
  232. begin
  233. if (cs_smartlink in aktmoduleswitches) then
  234. begin
  235. generatesmartlib;
  236. exit;
  237. end;
  238. hp1:=pimportlist(current_module^.imports^.first);
  239. while assigned(hp1) do
  240. begin
  241. { Insert cuts for smartlinking }
  242. if (cs_smartlink in aktmoduleswitches) then
  243. begin
  244. importssection^.concat(new(pai_cut,init));
  245. codesegment^.concat(new(pai_cut,init));
  246. end;
  247. {$IfDef GDB}
  248. if (cs_debuginfo in aktmoduleswitches) then
  249. codesegment^.concat(new(pai_stab_function_name,init(nil)));
  250. {$EndIf GDB}
  251. { Get labels for the sections }
  252. getlabel(l1);
  253. getlabel(l2);
  254. getlabel(l3);
  255. importssection^.concat(new(pai_section,init_idata(2)));
  256. { pointer to procedure names }
  257. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(l2)))));
  258. { two empty entries follow }
  259. importssection^.concat(new(pai_const,init_32bit(0)));
  260. importssection^.concat(new(pai_const,init_32bit(0)));
  261. { pointer to dll name }
  262. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(l1)))));
  263. { pointer to fixups }
  264. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(l3)))));
  265. { only create one section for each else it will
  266. create a lot of idata* }
  267. { first write the name references }
  268. importssection^.concat(new(pai_section,init_idata(4)));
  269. importssection^.concat(new(pai_label,init(l2)));
  270. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  271. while assigned(hp2) do
  272. begin
  273. getlabel(plabel(hp2^.lab));
  274. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(hp2^.lab)))));
  275. hp2:=pimported_procedure(hp2^.next);
  276. end;
  277. { finalize the names ... }
  278. importssection^.concat(new(pai_const,init_32bit(0)));
  279. { then the addresses and create also the indirect jump }
  280. importssection^.concat(new(pai_section,init_idata(5)));
  281. importssection^.concat(new(pai_label,init(l3)));
  282. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  283. while assigned(hp2) do
  284. begin
  285. getdatalabel(l4);
  286. { create indirect jump }
  287. new(r);
  288. reset_reference(r^);
  289. r^.symbol:=stringdup(lab2str(l4));
  290. { place jump in codesegment }
  291. codesegment^.concat(new(pai_align,init_op(4,$90)));
  292. codesegment^.concat(new(pai_symbol,init_global(hp2^.func^)));
  293. codesegment^.concat(new(pai386,op_ref(A_JMP,S_NO,r)));
  294. { add jump field to importsection }
  295. importssection^.concat(new(pai_label,init(l4)));
  296. importssection^.concat(new(pai_const,init_rva(strpnew(lab2str(hp2^.lab)))));
  297. hp2:=pimported_procedure(hp2^.next);
  298. end;
  299. { finalize the addresses }
  300. importssection^.concat(new(pai_const,init_32bit(0)));
  301. { finally the import information }
  302. importssection^.concat(new(pai_section,init_idata(6)));
  303. hp2:=pimported_procedure(hp1^.imported_procedures^.first);
  304. while assigned(hp2) do
  305. begin
  306. importssection^.concat(new(pai_label,init(hp2^.lab)));
  307. { the ordinal number }
  308. importssection^.concat(new(pai_const,init_16bit(hp2^.ordnr)));
  309. importssection^.concat(new(pai_string,init(hp2^.name^+#0)));
  310. hp2:=pimported_procedure(hp2^.next);
  311. end;
  312. { create import dll name }
  313. importssection^.concat(new(pai_section,init_idata(7)));
  314. importssection^.concat(new(pai_label,init(l1)));
  315. importssection^.concat(new(pai_string,init(hp1^.dllname^+target_os.sharedlibext+#0)));
  316. hp1:=pimportlist(hp1^.next);
  317. end;
  318. end;
  319. procedure postprocessexecutable(n : string);
  320. var
  321. f : file;
  322. dosheader : tdosheader;
  323. peheader : tpeheader;
  324. peheaderpos : longint;
  325. begin
  326. assign(f,n);
  327. reset(f,1);
  328. blockread(f,dosheader,sizeof(tdosheader));
  329. peheaderpos:=dosheader.e_lfanew;
  330. seek(f,peheaderpos);
  331. blockread(f,peheader,sizeof(tpeheader));
  332. { write info }
  333. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  334. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  335. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  336. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  337. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  338. { change the header }
  339. { sub system }
  340. { gui=2 }
  341. { cui=3 }
  342. if apptype=at_gui then
  343. peheader.Subsystem:=2
  344. else if apptype=at_cui then
  345. peheader.Subsystem:=3;
  346. seek(f,peheaderpos);
  347. blockwrite(f,peheader,sizeof(tpeheader));
  348. close(f);
  349. end;
  350. end.
  351. {
  352. $Log$
  353. Revision 1.11 1998-10-22 17:54:09 florian
  354. + switch $APPTYPE for win32 added
  355. Revision 1.10 1998/10/22 15:18:51 florian
  356. + switch -vx for win32 added
  357. Revision 1.9 1998/10/19 15:41:03 peter
  358. * better splitname to support glib-1.1.dll alike names
  359. Revision 1.8 1998/09/07 18:33:35 peter
  360. + smartlinking for win95 imports
  361. Revision 1.7 1998/09/03 17:39:06 florian
  362. + better code for type conversation longint/dword to real type
  363. Revision 1.6 1998/08/10 14:50:38 peter
  364. + localswitches, moduleswitches, globalswitches splitting
  365. Revision 1.5 1998/06/10 10:43:18 peter
  366. * write also the .dll extension (needed for NT)
  367. Revision 1.4 1998/06/08 22:59:56 peter
  368. * smartlinking works for win32
  369. * some defines to exclude some compiler parts
  370. Revision 1.3 1998/06/04 23:52:06 peter
  371. * m68k compiles
  372. + .def file creation moved to gendef.pas so it could also be used
  373. for win32
  374. Revision 1.2 1998/05/06 18:36:55 peter
  375. * tai_section extended with code,data,bss sections and enumerated type
  376. * ident 'compiled by FPC' moved to pmodules
  377. * small fix for smartlink
  378. }