t_win32.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. This unit implements support import,export,link routines
  5. for the (i386) Win32 target
  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 t_win32;
  20. {$i defines.inc}
  21. interface
  22. uses
  23. import,export,link;
  24. const
  25. winstackpagesize = 4096;
  26. type
  27. timportlibwin32=class(timportlib)
  28. procedure preparelib(const s:string);override;
  29. procedure importprocedure(const func,module:string;index:longint;const name:string);override;
  30. procedure importvariable(const varname,module:string;const name:string);override;
  31. procedure generatelib;override;
  32. procedure generatesmartlib;override;
  33. end;
  34. texportlibwin32=class(texportlib)
  35. st : string;
  36. last_index : longint;
  37. procedure preparelib(const s:string);override;
  38. procedure exportprocedure(hp : texported_item);override;
  39. procedure exportvar(hp : texported_item);override;
  40. procedure generatelib;override;
  41. end;
  42. tlinkerwin32=class(tlinker)
  43. private
  44. Function WriteResponseFile(isdll:boolean) : Boolean;
  45. Function PostProcessExecutable(const fn:string;isdll:boolean) : Boolean;
  46. public
  47. Constructor Create;
  48. Procedure SetDefaultInfo;override;
  49. function MakeExecutable:boolean;override;
  50. function MakeSharedLibrary:boolean;override;
  51. end;
  52. implementation
  53. uses
  54. {$ifdef Delphi}
  55. dmisc,
  56. {$else Delphi}
  57. dos,
  58. {$endif Delphi}
  59. cutils,cclasses,
  60. aasm,fmodule,globtype,globals,systems,verbose,
  61. script,gendef,impdef,
  62. cpubase,cpuasm
  63. {$ifdef GDB}
  64. ,gdb
  65. {$endif}
  66. ;
  67. function DllName(Const Name : string) : string;
  68. var n : string;
  69. begin
  70. n:=Upper(SplitExtension(Name));
  71. if (n='.DLL') or (n='.DRV') or (n='.EXE') then
  72. DllName:=Name
  73. else
  74. DllName:=Name+target_os.sharedlibext;
  75. end;
  76. function FindDLL(const s:string):string;
  77. var
  78. sysdir : string;
  79. FoundDll : string;
  80. Found : boolean;
  81. begin
  82. Found:=false;
  83. { Look for DLL in:
  84. 1. Current dir
  85. 2. Library Path
  86. 3. windir,windir/system,windir/system32 }
  87. FoundDll:=FindFile(s,'.'+DirSep,found)+s;
  88. if (not found) then
  89. FoundDll:=includesearchpath.FindFile(s,found)+s;
  90. if (not found) then
  91. begin
  92. sysdir:=FixPath(GetEnv('windir'),false);
  93. FoundDll:=FindFile(s,sysdir+';'+sysdir+'system'+DirSep+';'+sysdir+'system32'+DirSep,found)+s;
  94. end;
  95. if (not found) then
  96. begin
  97. message1(exec_w_libfile_not_found,s);
  98. FoundDll:=s;
  99. end;
  100. FindDll:=FoundDll;
  101. end;
  102. {*****************************************************************************
  103. TIMPORTLIBWIN32
  104. *****************************************************************************}
  105. procedure timportlibwin32.preparelib(const s : string);
  106. begin
  107. if not(assigned(importssection)) then
  108. importssection:=TAAsmoutput.create;
  109. end;
  110. procedure timportlibwin32.importprocedure(const func,module : string;index : longint;const name : string);
  111. var
  112. hp1 : timportlist;
  113. hp2 : timported_item;
  114. hs : string;
  115. begin
  116. hs:=DllName(module);
  117. { search for the module }
  118. hp1:=timportlist(current_module.imports.first);
  119. while assigned(hp1) do
  120. begin
  121. if hs=hp1.dllname^ then
  122. break;
  123. hp1:=timportlist(hp1.next);
  124. end;
  125. { generate a new item ? }
  126. if not(assigned(hp1)) then
  127. begin
  128. hp1:=timportlist.create(hs);
  129. current_module.imports.concat(hp1);
  130. end;
  131. { search for reuse of old import item }
  132. hp2:=timported_item(hp1.imported_items.first);
  133. while assigned(hp2) do
  134. begin
  135. if hp2.func^=func then
  136. break;
  137. hp2:=timported_item(hp2.next);
  138. end;
  139. if not assigned(hp2) then
  140. begin
  141. hp2:=timported_item.create(func,name,index);
  142. hp1.imported_items.concat(hp2);
  143. end;
  144. end;
  145. procedure timportlibwin32.importvariable(const varname,module:string;const name:string);
  146. var
  147. hp1 : timportlist;
  148. hp2 : timported_item;
  149. hs : string;
  150. begin
  151. hs:=DllName(module);
  152. { search for the module }
  153. hp1:=timportlist(current_module.imports.first);
  154. while assigned(hp1) do
  155. begin
  156. if hs=hp1.dllname^ then
  157. break;
  158. hp1:=timportlist(hp1.next);
  159. end;
  160. { generate a new item ? }
  161. if not(assigned(hp1)) then
  162. begin
  163. hp1:=timportlist.create(hs);
  164. current_module.imports.concat(hp1);
  165. end;
  166. hp2:=timported_item.create_var(varname,name);
  167. hp1.imported_items.concat(hp2);
  168. end;
  169. procedure timportlibwin32.generatesmartlib;
  170. var
  171. hp1 : timportlist;
  172. hp2 : timported_item;
  173. lhead,lname,lcode,
  174. lidata4,lidata5 : pasmlabel;
  175. r : preference;
  176. begin
  177. hp1:=timportlist(current_module.imports.first);
  178. while assigned(hp1) do
  179. begin
  180. { Get labels for the sections }
  181. getdatalabel(lhead);
  182. getdatalabel(lname);
  183. getaddrlabel(lidata4);
  184. getaddrlabel(lidata5);
  185. { create header for this importmodule }
  186. importsSection.concat(Tai_cut.Create_begin);
  187. importsSection.concat(Tai_section.Create(sec_idata2));
  188. importsSection.concat(Tai_label.Create(lhead));
  189. { pointer to procedure names }
  190. importsSection.concat(Tai_const_symbol.Create_rva(lidata4));
  191. { two empty entries follow }
  192. importsSection.concat(Tai_const.Create_32bit(0));
  193. importsSection.concat(Tai_const.Create_32bit(0));
  194. { pointer to dll name }
  195. importsSection.concat(Tai_const_symbol.Create_rva(lname));
  196. { pointer to fixups }
  197. importsSection.concat(Tai_const_symbol.Create_rva(lidata5));
  198. { first write the name references }
  199. importsSection.concat(Tai_section.Create(sec_idata4));
  200. importsSection.concat(Tai_const.Create_32bit(0));
  201. importsSection.concat(Tai_label.Create(lidata4));
  202. { then the addresses and create also the indirect jump }
  203. importsSection.concat(Tai_section.Create(sec_idata5));
  204. importsSection.concat(Tai_const.Create_32bit(0));
  205. importsSection.concat(Tai_label.Create(lidata5));
  206. { create procedures }
  207. hp2:=timported_item(hp1.imported_items.first);
  208. while assigned(hp2) do
  209. begin
  210. { insert cuts }
  211. importsSection.concat(Tai_cut.Create);
  212. { create indirect jump }
  213. if not hp2.is_var then
  214. begin
  215. getlabel(lcode);
  216. new(r);
  217. reset_reference(r^);
  218. r^.symbol:=lcode;
  219. { place jump in codesegment, insert a code section in the
  220. importsection to reduce the amount of .s files (PFV) }
  221. importsSection.concat(Tai_section.Create(sec_code));
  222. {$IfDef GDB}
  223. if (cs_debuginfo in aktmoduleswitches) then
  224. importsSection.concat(Tai_stab_function_name.Create(nil));
  225. {$EndIf GDB}
  226. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,0));
  227. importsSection.concat(Taicpu.Op_ref(A_JMP,S_NO,r));
  228. importsSection.concat(Tai_align.Create_op(4,$90));
  229. end;
  230. { create head link }
  231. importsSection.concat(Tai_section.Create(sec_idata7));
  232. importsSection.concat(Tai_const_symbol.Create_rva(lhead));
  233. { fixup }
  234. getlabel(pasmlabel(hp2.lab));
  235. importsSection.concat(Tai_section.Create(sec_idata4));
  236. importsSection.concat(Tai_const_symbol.Create_rva(hp2.lab));
  237. { add jump field to importsection }
  238. importsSection.concat(Tai_section.Create(sec_idata5));
  239. if hp2.is_var then
  240. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,0))
  241. else
  242. importsSection.concat(Tai_label.Create(lcode));
  243. if hp2.name^<>'' then
  244. importsSection.concat(Tai_const_symbol.Create_rva(hp2.lab))
  245. else
  246. importsSection.concat(Tai_const.Create_32bit($80000000 or hp2.ordnr));
  247. { finally the import information }
  248. importsSection.concat(Tai_section.Create(sec_idata6));
  249. importsSection.concat(Tai_label.Create(hp2.lab));
  250. importsSection.concat(Tai_const.Create_16bit(hp2.ordnr));
  251. importsSection.concat(Tai_string.Create(hp2.name^+#0));
  252. importsSection.concat(Tai_align.Create_op(2,0));
  253. hp2:=timported_item(hp2.next);
  254. end;
  255. { write final section }
  256. importsSection.concat(Tai_cut.Create_end);
  257. { end of name references }
  258. importsSection.concat(Tai_section.Create(sec_idata4));
  259. importsSection.concat(Tai_const.Create_32bit(0));
  260. { end if addresses }
  261. importsSection.concat(Tai_section.Create(sec_idata5));
  262. importsSection.concat(Tai_const.Create_32bit(0));
  263. { dllname }
  264. importsSection.concat(Tai_section.Create(sec_idata7));
  265. importsSection.concat(Tai_label.Create(lname));
  266. importsSection.concat(Tai_string.Create(hp1.dllname^+#0));
  267. hp1:=timportlist(hp1.next);
  268. end;
  269. end;
  270. procedure timportlibwin32.generatelib;
  271. var
  272. hp1 : timportlist;
  273. hp2 : timported_item;
  274. l1,l2,l3,l4 : pasmlabel;
  275. r : preference;
  276. begin
  277. hp1:=timportlist(current_module.imports.first);
  278. while assigned(hp1) do
  279. begin
  280. { align codesegment for the jumps }
  281. importsSection.concat(Tai_section.Create(sec_code));
  282. importsSection.concat(Tai_align.Create_op(4,$90));
  283. { Get labels for the sections }
  284. getlabel(l1);
  285. getlabel(l2);
  286. getlabel(l3);
  287. importsSection.concat(Tai_section.Create(sec_idata2));
  288. { pointer to procedure names }
  289. importsSection.concat(Tai_const_symbol.Create_rva(l2));
  290. { two empty entries follow }
  291. importsSection.concat(Tai_const.Create_32bit(0));
  292. importsSection.concat(Tai_const.Create_32bit(0));
  293. { pointer to dll name }
  294. importsSection.concat(Tai_const_symbol.Create_rva(l1));
  295. { pointer to fixups }
  296. importsSection.concat(Tai_const_symbol.Create_rva(l3));
  297. { only create one section for each else it will
  298. create a lot of idata* }
  299. { first write the name references }
  300. importsSection.concat(Tai_section.Create(sec_idata4));
  301. importsSection.concat(Tai_label.Create(l2));
  302. hp2:=timported_item(hp1.imported_items.first);
  303. while assigned(hp2) do
  304. begin
  305. getlabel(pasmlabel(hp2.lab));
  306. if hp2.name^<>'' then
  307. importsSection.concat(Tai_const_symbol.Create_rva(hp2.lab))
  308. else
  309. importsSection.concat(Tai_const.Create_32bit($80000000 or hp2.ordnr));
  310. hp2:=timported_item(hp2.next);
  311. end;
  312. { finalize the names ... }
  313. importsSection.concat(Tai_const.Create_32bit(0));
  314. { then the addresses and create also the indirect jump }
  315. importsSection.concat(Tai_section.Create(sec_idata5));
  316. importsSection.concat(Tai_label.Create(l3));
  317. hp2:=timported_item(hp1.imported_items.first);
  318. while assigned(hp2) do
  319. begin
  320. if not hp2.is_var then
  321. begin
  322. getlabel(l4);
  323. { create indirect jump }
  324. new(r);
  325. reset_reference(r^);
  326. r^.symbol:=l4;
  327. { place jump in codesegment }
  328. importsSection.concat(Tai_section.Create(sec_code));
  329. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,0));
  330. importsSection.concat(Taicpu.Op_ref(A_JMP,S_NO,r));
  331. importsSection.concat(Tai_align.Create_op(4,$90));
  332. { add jump field to importsection }
  333. importsSection.concat(Tai_section.Create(sec_idata5));
  334. importsSection.concat(Tai_label.Create(l4));
  335. end
  336. else
  337. begin
  338. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,0));
  339. end;
  340. importsSection.concat(Tai_const_symbol.Create_rva(hp2.lab));
  341. hp2:=timported_item(hp2.next);
  342. end;
  343. { finalize the addresses }
  344. importsSection.concat(Tai_const.Create_32bit(0));
  345. { finally the import information }
  346. importsSection.concat(Tai_section.Create(sec_idata6));
  347. hp2:=timported_item(hp1.imported_items.first);
  348. while assigned(hp2) do
  349. begin
  350. importsSection.concat(Tai_label.Create(hp2.lab));
  351. { the ordinal number }
  352. importsSection.concat(Tai_const.Create_16bit(hp2.ordnr));
  353. importsSection.concat(Tai_string.Create(hp2.name^+#0));
  354. importsSection.concat(Tai_align.Create_op(2,0));
  355. hp2:=timported_item(hp2.next);
  356. end;
  357. { create import dll name }
  358. importsSection.concat(Tai_section.Create(sec_idata7));
  359. importsSection.concat(Tai_label.Create(l1));
  360. importsSection.concat(Tai_string.Create(hp1.dllname^+#0));
  361. hp1:=timportlist(hp1.next);
  362. end;
  363. end;
  364. {*****************************************************************************
  365. TEXPORTLIBWIN32
  366. *****************************************************************************}
  367. procedure texportlibwin32.preparelib(const s:string);
  368. begin
  369. if not(assigned(exportssection)) then
  370. exportssection:=TAAsmoutput.create;
  371. last_index:=0;
  372. end;
  373. procedure texportlibwin32.exportvar(hp : texported_item);
  374. begin
  375. { same code used !! PM }
  376. exportprocedure(hp);
  377. end;
  378. procedure texportlibwin32.exportprocedure(hp : texported_item);
  379. { must be ordered at least for win32 !! }
  380. var
  381. hp2 : texported_item;
  382. begin
  383. { first test the index value }
  384. if (hp.options and eo_index)<>0 then
  385. begin
  386. if (hp.index<=0) or (hp.index>$ffff) then
  387. begin
  388. message1(parser_e_export_invalid_index,tostr(hp.index));
  389. exit;
  390. end;
  391. if (hp.index<=last_index) then
  392. begin
  393. message1(parser_e_export_ordinal_double,tostr(hp.index));
  394. { disregard index value }
  395. inc(last_index);
  396. hp.index:=last_index;
  397. exit;
  398. end
  399. else
  400. begin
  401. last_index:=hp.index;
  402. end;
  403. end
  404. else
  405. begin
  406. inc(last_index);
  407. hp.index:=last_index;
  408. end;
  409. { now place in correct order }
  410. hp2:=texported_item(current_module._exports.first);
  411. while assigned(hp2) and
  412. (hp.name^>hp2.name^) do
  413. hp2:=texported_item(hp2.next);
  414. { insert hp there !! }
  415. if assigned(hp2) and (hp2.name^=hp.name^) then
  416. begin
  417. { this is not allowed !! }
  418. message1(parser_e_export_name_double,hp.name^);
  419. exit;
  420. end;
  421. if hp2=texported_item(current_module._exports.first) then
  422. current_module._exports.concat(hp)
  423. else if assigned(hp2) then
  424. begin
  425. hp.next:=hp2;
  426. hp.previous:=hp2.previous;
  427. if assigned(hp2.previous) then
  428. hp2.previous.next:=hp;
  429. hp2.previous:=hp;
  430. end
  431. else
  432. current_module._exports.concat(hp);
  433. end;
  434. procedure texportlibwin32.generatelib;
  435. var
  436. ordinal_base,ordinal_max,ordinal_min : longint;
  437. current_index : longint;
  438. entries,named_entries : longint;
  439. name_label,dll_name_label,export_address_table : pasmlabel;
  440. export_name_table_pointers,export_ordinal_table : pasmlabel;
  441. hp,hp2 : texported_item;
  442. temtexport : TLinkedList;
  443. address_table,name_table_pointers,
  444. name_table,ordinal_table : TAAsmoutput;
  445. begin
  446. hp:=texported_item(current_module._exports.first);
  447. if not assigned(hp) then
  448. exit;
  449. ordinal_max:=0;
  450. ordinal_min:=$7FFFFFFF;
  451. entries:=0;
  452. named_entries:=0;
  453. getlabel(dll_name_label);
  454. getlabel(export_address_table);
  455. getlabel(export_name_table_pointers);
  456. getlabel(export_ordinal_table);
  457. { count entries }
  458. while assigned(hp) do
  459. begin
  460. inc(entries);
  461. if (hp.index>ordinal_max) then
  462. ordinal_max:=hp.index;
  463. if (hp.index>0) and (hp.index<ordinal_min) then
  464. ordinal_min:=hp.index;
  465. if assigned(hp.name) then
  466. inc(named_entries);
  467. hp:=texported_item(hp.next);
  468. end;
  469. { no support for higher ordinal base yet !! }
  470. ordinal_base:=1;
  471. current_index:=ordinal_base;
  472. { we must also count the holes !! }
  473. entries:=ordinal_max-ordinal_base+1;
  474. exportsSection.concat(Tai_section.Create(sec_edata));
  475. { export flags }
  476. exportsSection.concat(Tai_const.Create_32bit(0));
  477. { date/time stamp }
  478. exportsSection.concat(Tai_const.Create_32bit(0));
  479. { major version }
  480. exportsSection.concat(Tai_const.Create_16bit(0));
  481. { minor version }
  482. exportsSection.concat(Tai_const.Create_16bit(0));
  483. { pointer to dll name }
  484. exportsSection.concat(Tai_const_symbol.Create_rva(dll_name_label));
  485. { ordinal base normally set to 1 }
  486. exportsSection.concat(Tai_const.Create_32bit(ordinal_base));
  487. { number of entries }
  488. exportsSection.concat(Tai_const.Create_32bit(entries));
  489. { number of named entries }
  490. exportsSection.concat(Tai_const.Create_32bit(named_entries));
  491. { address of export address table }
  492. exportsSection.concat(Tai_const_symbol.Create_rva(export_address_table));
  493. { address of name pointer pointers }
  494. exportsSection.concat(Tai_const_symbol.Create_rva(export_name_table_pointers));
  495. { address of ordinal number pointers }
  496. exportsSection.concat(Tai_const_symbol.Create_rva(export_ordinal_table));
  497. { the name }
  498. exportsSection.concat(Tai_label.Create(dll_name_label));
  499. if st='' then
  500. exportsSection.concat(Tai_string.Create(current_module.modulename^+target_os.sharedlibext+#0))
  501. else
  502. exportsSection.concat(Tai_string.Create(st+target_os.sharedlibext+#0));
  503. { export address table }
  504. address_table:=TAAsmoutput.create;
  505. address_table.concat(Tai_align.Create_op(4,0));
  506. address_table.concat(Tai_label.Create(export_address_table));
  507. name_table_pointers:=TAAsmoutput.create;
  508. name_table_pointers.concat(Tai_align.Create_op(4,0));
  509. name_table_pointers.concat(Tai_label.Create(export_name_table_pointers));
  510. ordinal_table:=TAAsmoutput.create;
  511. ordinal_table.concat(Tai_align.Create_op(4,0));
  512. ordinal_table.concat(Tai_label.Create(export_ordinal_table));
  513. name_table:=TAAsmoutput.Create;
  514. name_table.concat(Tai_align.Create_op(4,0));
  515. { write each address }
  516. hp:=texported_item(current_module._exports.first);
  517. while assigned(hp) do
  518. begin
  519. if (hp.options and eo_name)<>0 then
  520. begin
  521. getlabel(name_label);
  522. name_table_pointers.concat(Tai_const_symbol.Create_rva(name_label));
  523. ordinal_table.concat(Tai_const.Create_16bit(hp.index-ordinal_base));
  524. name_table.concat(Tai_align.Create_op(2,0));
  525. name_table.concat(Tai_label.Create(name_label));
  526. name_table.concat(Tai_string.Create(hp.name^+#0));
  527. end;
  528. hp:=texported_item(hp.next);
  529. end;
  530. { order in increasing ordinal values }
  531. { into temtexport list }
  532. temtexport:=TLinkedList.Create;
  533. hp:=texported_item(current_module._exports.first);
  534. while assigned(hp) do
  535. begin
  536. current_module._exports.remove(hp);
  537. hp2:=texported_item(temtexport.first);
  538. while assigned(hp2) and (hp.index>hp2.index) do
  539. begin
  540. hp2:=texported_item(hp2.next);
  541. end;
  542. if hp2=texported_item(temtexport.first) then
  543. temtexport.insert(hp)
  544. else
  545. begin
  546. if assigned(hp2) then
  547. begin
  548. hp.next:=hp2;
  549. hp.previous:=hp2.previous;
  550. hp2.previous:=hp;
  551. if assigned(hp.previous) then
  552. hp.previous.next:=hp;
  553. end
  554. else
  555. temtexport.concat(hp);
  556. end;
  557. hp:=texported_item(current_module._exports.first);;
  558. end;
  559. { write the export adress table }
  560. current_index:=ordinal_base;
  561. hp:=texported_item(temtexport.first);
  562. while assigned(hp) do
  563. begin
  564. { fill missing values }
  565. while current_index<hp.index do
  566. begin
  567. address_table.concat(Tai_const.Create_32bit(0));
  568. inc(current_index);
  569. end;
  570. address_table.concat(Tai_const_symbol.Createname_rva(hp.sym^.mangledname));
  571. inc(current_index);
  572. hp:=texported_item(hp.next);
  573. end;
  574. exportsSection.concatlist(address_table);
  575. exportsSection.concatlist(name_table_pointers);
  576. exportsSection.concatlist(ordinal_table);
  577. exportsSection.concatlist(name_table);
  578. address_table.Free;
  579. name_table_pointers.free;
  580. ordinal_table.free;
  581. name_table.free;
  582. temtexport.free;
  583. end;
  584. {****************************************************************************
  585. TLINKERWIN32
  586. ****************************************************************************}
  587. Constructor TLinkerWin32.Create;
  588. begin
  589. Inherited Create;
  590. { allow duplicated libs (PM) }
  591. SharedLibFiles.doubles:=true;
  592. StaticLibFiles.doubles:=true;
  593. If not ForceDeffileForExport then
  594. UseDeffileForExport:=false;
  595. end;
  596. Procedure TLinkerWin32.SetDefaultInfo;
  597. begin
  598. with Info do
  599. begin
  600. ExeCmd[1]:='ldw $OPT $STRIP $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  601. DllCmd[1]:='ldw $OPT $STRIP --dll $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  602. if RelocSection or UseDeffileForExport then
  603. begin
  604. { ExeCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF';
  605. use short forms to avoid 128 char limitation problem }
  606. ExeCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  607. ExeCmd[3]:='ldw $OPT $STRIP $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  608. { DllCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF'; }
  609. DllCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  610. DllCmd[3]:='ldw $OPT $STRIP --dll $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  611. end;
  612. end;
  613. end;
  614. Function TLinkerWin32.WriteResponseFile(isdll:boolean) : Boolean;
  615. function do_makedef(const DllName,LibName:string):boolean;
  616. var
  617. CmdLine : string;
  618. begin
  619. if (not do_build) and
  620. FileExists(LibName) then
  621. begin
  622. if GetNamedFileTime(LibName)>GetNamedFileTime(DllName) then
  623. begin
  624. do_makedef:=true;
  625. exit;
  626. end;
  627. end;
  628. asw_name:=FindUtil('asw');
  629. arw_name:=FindUtil('arw');
  630. if cs_link_extern in aktglobalswitches then
  631. begin
  632. CmdLine:='-l '+LibName+' -i '+DLLName;
  633. if asw_name<>'' then
  634. CmdLine:=CmdLine+' -a '+asw_name;
  635. if arw_name<>'' then
  636. CmdLine:=CmdLine+' -r '+arw_name;
  637. do_makedef:=DoExec(FindUtil('fpimpdef'),CmdLine,false,false);
  638. end
  639. else
  640. do_makedef:=makedef(DLLName,LIbName);
  641. end;
  642. Var
  643. linkres : TLinkRes;
  644. i : longint;
  645. HPath : TStringListItem;
  646. s,s2 : string;
  647. found,
  648. linklibc : boolean;
  649. begin
  650. WriteResponseFile:=False;
  651. { Create static import libraries for DLL that are
  652. included using the $linklib directive }
  653. While not SharedLibFiles.Empty do
  654. begin
  655. s:=SharedLibFiles.GetFirst;
  656. s2:=AddExtension(s,target_os.sharedlibext);
  657. s:=target_os.libprefix+SplitName(s)+target_os.staticlibext;
  658. if Do_makedef(FindDLL(s2),s) then
  659. begin
  660. if s<>''then
  661. StaticLibFiles.insert(s);
  662. end
  663. else
  664. begin
  665. Message(exec_w_error_while_linking);
  666. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  667. end;
  668. end;
  669. { Open link.res file }
  670. LinkRes.Init(outputexedir+Info.ResName);
  671. { Write path to search libraries }
  672. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  673. while assigned(HPath) do
  674. begin
  675. LinkRes.Add('SEARCH_DIR('+GetShortName(HPath.Str)+')');
  676. HPath:=TStringListItem(HPath.Next);
  677. end;
  678. HPath:=TStringListItem(LibrarySearchPath.First);
  679. while assigned(HPath) do
  680. begin
  681. LinkRes.Add('SEARCH_DIR('+GetShortName(HPath.Str)+')');
  682. HPath:=TStringListItem(HPath.Next);
  683. end;
  684. { add objectfiles, start with prt0 always }
  685. LinkRes.Add('INPUT(');
  686. if isdll then
  687. LinkRes.AddFileName(GetShortName(FindObjectFile('wdllprt0','')))
  688. else
  689. LinkRes.AddFileName(GetShortName(FindObjectFile('wprt0','')));
  690. while not ObjectFiles.Empty do
  691. begin
  692. s:=ObjectFiles.GetFirst;
  693. if s<>'' then
  694. LinkRes.AddFileName(GetShortName(s));
  695. end;
  696. LinkRes.Add(')');
  697. { Write staticlibraries }
  698. if not StaticLibFiles.Empty then
  699. begin
  700. LinkRes.Add('GROUP(');
  701. While not StaticLibFiles.Empty do
  702. begin
  703. S:=StaticLibFiles.GetFirst;
  704. LinkRes.AddFileName(GetShortName(s));
  705. end;
  706. LinkRes.Add(')');
  707. end;
  708. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  709. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  710. if not SharedLibFiles.Empty then
  711. begin
  712. linklibc:=false;
  713. LinkRes.Add('INPUT(');
  714. While not SharedLibFiles.Empty do
  715. begin
  716. S:=SharedLibFiles.GetFirst;
  717. if pos('.',s)=0 then
  718. { we never directly link a DLL
  719. its allways through an import library PM }
  720. { libraries created by C compilers have .a extensions }
  721. s2:=s+'.a'{ target_os.sharedlibext }
  722. else
  723. s2:=s;
  724. s2:=FindLibraryFile(s2,'',found);
  725. if found then
  726. begin
  727. LinkRes.Add(s2);
  728. continue;
  729. end;
  730. if pos(target_os.libprefix,s)=1 then
  731. s:=copy(s,length(target_os.libprefix)+1,255);
  732. if s<>'c' then
  733. begin
  734. i:=Pos(target_os.sharedlibext,S);
  735. if i>0 then
  736. Delete(S,i,255);
  737. LinkRes.Add('-l'+s);
  738. end
  739. else
  740. begin
  741. LinkRes.Add('-l'+s);
  742. linklibc:=true;
  743. end;
  744. end;
  745. { be sure that libc is the last lib }
  746. if linklibc then
  747. LinkRes.Add('-lc');
  748. LinkRes.Add(')');
  749. end;
  750. { Write and Close response }
  751. linkres.writetodisk;
  752. linkres.done;
  753. WriteResponseFile:=True;
  754. end;
  755. function TLinkerWin32.MakeExecutable:boolean;
  756. var
  757. binstr,
  758. cmdstr : string;
  759. found,
  760. success : boolean;
  761. i : longint;
  762. AsBinStr : string[80];
  763. StripStr,
  764. RelocStr,
  765. AppTypeStr,
  766. ImageBaseStr : string[40];
  767. begin
  768. if not(cs_link_extern in aktglobalswitches) then
  769. Message1(exec_i_linking,current_module.exefilename^);
  770. { Create some replacements }
  771. RelocStr:='';
  772. AppTypeStr:='';
  773. ImageBaseStr:='';
  774. StripStr:='';
  775. AsBinStr:=FindExe('asw',found);
  776. if RelocSection then
  777. { Using short form to avoid problems with 128 char limitation under Dos. }
  778. RelocStr:='-b base.$$$';
  779. if apptype=app_gui then
  780. AppTypeStr:='--subsystem windows';
  781. if assigned(DLLImageBase) then
  782. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  783. if (cs_link_strip in aktglobalswitches) then
  784. StripStr:='-s';
  785. { Write used files and libraries }
  786. WriteResponseFile(false);
  787. { Call linker }
  788. success:=false;
  789. for i:=1 to 3 do
  790. begin
  791. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  792. if binstr<>'' then
  793. begin
  794. Replace(cmdstr,'$EXE',current_module.exefilename^);
  795. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  796. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  797. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  798. Replace(cmdstr,'$ASBIN',AsbinStr);
  799. Replace(cmdstr,'$RELOC',RelocStr);
  800. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  801. Replace(cmdstr,'$STRIP',StripStr);
  802. if not DefFile.Empty {and UseDefFileForExport} then
  803. begin
  804. DefFile.WriteFile;
  805. Replace(cmdstr,'$DEF','-d '+deffile.fname);
  806. end
  807. else
  808. Replace(cmdstr,'$DEF','');
  809. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  810. if not success then
  811. break;
  812. end;
  813. end;
  814. { Post process }
  815. if success then
  816. success:=PostProcessExecutable(current_module.exefilename^,false);
  817. { Remove ReponseFile }
  818. if (success) and not(cs_link_extern in aktglobalswitches) then
  819. begin
  820. RemoveFile(outputexedir+Info.ResName);
  821. RemoveFile('base.$$$');
  822. RemoveFile('exp.$$$');
  823. RemoveFile('deffile.$$$');
  824. end;
  825. MakeExecutable:=success; { otherwise a recursive call to link method }
  826. end;
  827. Function TLinkerWin32.MakeSharedLibrary:boolean;
  828. var
  829. binstr,
  830. cmdstr : string;
  831. found,
  832. success : boolean;
  833. i : longint;
  834. AsBinStr : string[80];
  835. StripStr,
  836. RelocStr,
  837. AppTypeStr,
  838. ImageBaseStr : string[40];
  839. begin
  840. MakeSharedLibrary:=false;
  841. if not(cs_link_extern in aktglobalswitches) then
  842. Message1(exec_i_linking,current_module.sharedlibfilename^);
  843. { Create some replacements }
  844. RelocStr:='';
  845. AppTypeStr:='';
  846. ImageBaseStr:='';
  847. StripStr:='';
  848. AsBinStr:=FindExe('asw',found);
  849. if RelocSection then
  850. { Using short form to avoid problems with 128 char limitation under Dos. }
  851. RelocStr:='-b base.$$$';
  852. if apptype=app_gui then
  853. AppTypeStr:='--subsystem windows';
  854. if assigned(DLLImageBase) then
  855. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  856. if (cs_link_strip in aktglobalswitches) then
  857. StripStr:='-s';
  858. { Write used files and libraries }
  859. WriteResponseFile(true);
  860. { Call linker }
  861. success:=false;
  862. for i:=1 to 3 do
  863. begin
  864. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  865. if binstr<>'' then
  866. begin
  867. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  868. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  869. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  870. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  871. Replace(cmdstr,'$ASBIN',AsbinStr);
  872. Replace(cmdstr,'$RELOC',RelocStr);
  873. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  874. Replace(cmdstr,'$STRIP',StripStr);
  875. if not DefFile.Empty {and UseDefFileForExport} then
  876. begin
  877. DefFile.WriteFile;
  878. Replace(cmdstr,'$DEF','-d '+deffile.fname);
  879. end
  880. else
  881. Replace(cmdstr,'$DEF','');
  882. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  883. if not success then
  884. break;
  885. end;
  886. end;
  887. { Post process }
  888. if success then
  889. success:=PostProcessExecutable(current_module.sharedlibfilename^,true);
  890. { Remove ReponseFile }
  891. if (success) and not(cs_link_extern in aktglobalswitches) then
  892. begin
  893. RemoveFile(outputexedir+Info.ResName);
  894. RemoveFile('base.$$$');
  895. RemoveFile('exp.$$$');
  896. end;
  897. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  898. end;
  899. function tlinkerwin32.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  900. type
  901. tdosheader = packed record
  902. e_magic : word;
  903. e_cblp : word;
  904. e_cp : word;
  905. e_crlc : word;
  906. e_cparhdr : word;
  907. e_minalloc : word;
  908. e_maxalloc : word;
  909. e_ss : word;
  910. e_sp : word;
  911. e_csum : word;
  912. e_ip : word;
  913. e_cs : word;
  914. e_lfarlc : word;
  915. e_ovno : word;
  916. e_res : array[0..3] of word;
  917. e_oemid : word;
  918. e_oeminfo : word;
  919. e_res2 : array[0..9] of word;
  920. e_lfanew : longint;
  921. end;
  922. tpeheader = packed record
  923. PEMagic : array[0..3] of char;
  924. Machine : word;
  925. NumberOfSections : word;
  926. TimeDateStamp : longint;
  927. PointerToSymbolTable : longint;
  928. NumberOfSymbols : longint;
  929. SizeOfOptionalHeader : word;
  930. Characteristics : word;
  931. Magic : word;
  932. MajorLinkerVersion : byte;
  933. MinorLinkerVersion : byte;
  934. SizeOfCode : longint;
  935. SizeOfInitializedData : longint;
  936. SizeOfUninitializedData : longint;
  937. AddressOfEntryPoint : longint;
  938. BaseOfCode : longint;
  939. BaseOfData : longint;
  940. ImageBase : longint;
  941. SectionAlignment : longint;
  942. FileAlignment : longint;
  943. MajorOperatingSystemVersion : word;
  944. MinorOperatingSystemVersion : word;
  945. MajorImageVersion : word;
  946. MinorImageVersion : word;
  947. MajorSubsystemVersion : word;
  948. MinorSubsystemVersion : word;
  949. Reserved1 : longint;
  950. SizeOfImage : longint;
  951. SizeOfHeaders : longint;
  952. CheckSum : longint;
  953. Subsystem : word;
  954. DllCharacteristics : word;
  955. SizeOfStackReserve : longint;
  956. SizeOfStackCommit : longint;
  957. SizeOfHeapReserve : longint;
  958. SizeOfHeapCommit : longint;
  959. LoaderFlags : longint;
  960. NumberOfRvaAndSizes : longint;
  961. DataDirectory : array[1..$80] of byte;
  962. end;
  963. tcoffsechdr=packed record
  964. name : array[0..7] of char;
  965. vsize : longint;
  966. rvaofs : longint;
  967. datalen : longint;
  968. datapos : longint;
  969. relocpos : longint;
  970. lineno1 : longint;
  971. nrelocs : word;
  972. lineno2 : word;
  973. flags : longint;
  974. end;
  975. psecfill=^tsecfill;
  976. tsecfill=record
  977. fillpos,
  978. fillsize : longint;
  979. next : psecfill;
  980. end;
  981. var
  982. f : file;
  983. cmdstr : string;
  984. dosheader : tdosheader;
  985. peheader : tpeheader;
  986. firstsecpos,
  987. maxfillsize,
  988. l,peheaderpos : longint;
  989. coffsec : tcoffsechdr;
  990. secroot,hsecroot : psecfill;
  991. zerobuf : pointer;
  992. begin
  993. postprocessexecutable:=false;
  994. { when -s is used or it's a dll then quit }
  995. if (cs_link_extern in aktglobalswitches) then
  996. begin
  997. case apptype of
  998. app_gui :
  999. cmdstr:='--subsystem gui';
  1000. app_cui :
  1001. cmdstr:='--subsystem console';
  1002. end;
  1003. if dllversion<>'' then
  1004. cmdstr:=cmdstr+' --version '+dllversion;
  1005. cmdstr:=cmdstr+' --input '+fn;
  1006. cmdstr:=cmdstr+' --stack '+tostr(stacksize);
  1007. DoExec(FindUtil('postw32'),cmdstr,false,false);
  1008. postprocessexecutable:=true;
  1009. exit;
  1010. end;
  1011. { open file }
  1012. assign(f,fn);
  1013. {$I-}
  1014. reset(f,1);
  1015. if ioresult<>0 then
  1016. Message1(execinfo_f_cant_open_executable,fn);
  1017. { read headers }
  1018. blockread(f,dosheader,sizeof(tdosheader));
  1019. peheaderpos:=dosheader.e_lfanew;
  1020. seek(f,peheaderpos);
  1021. blockread(f,peheader,sizeof(tpeheader));
  1022. { write info }
  1023. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  1024. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  1025. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  1026. { change stack size (PM) }
  1027. { I am not sure that the default value is adequate !! }
  1028. peheader.SizeOfStackReserve:=stacksize;
  1029. { change the header }
  1030. { sub system }
  1031. { gui=2 }
  1032. { cui=3 }
  1033. case apptype of
  1034. app_gui :
  1035. peheader.Subsystem:=2;
  1036. app_cui :
  1037. peheader.Subsystem:=3;
  1038. end;
  1039. if dllversion<>'' then
  1040. begin
  1041. peheader.MajorImageVersion:=dllmajor;
  1042. peheader.MinorImageVersion:=dllminor;
  1043. end;
  1044. { reset timestamp }
  1045. peheader.TimeDateStamp:=0;
  1046. { write header back }
  1047. seek(f,peheaderpos);
  1048. blockwrite(f,peheader,sizeof(tpeheader));
  1049. if ioresult<>0 then
  1050. Message1(execinfo_f_cant_process_executable,fn);
  1051. seek(f,peheaderpos);
  1052. blockread(f,peheader,sizeof(tpeheader));
  1053. { write the value after the change }
  1054. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  1055. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  1056. { read section info }
  1057. maxfillsize:=0;
  1058. firstsecpos:=0;
  1059. secroot:=nil;
  1060. for l:=1 to peheader.NumberOfSections do
  1061. begin
  1062. blockread(f,coffsec,sizeof(tcoffsechdr));
  1063. if coffsec.datapos>0 then
  1064. begin
  1065. if secroot=nil then
  1066. firstsecpos:=coffsec.datapos;
  1067. new(hsecroot);
  1068. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  1069. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  1070. hsecroot^.next:=secroot;
  1071. secroot:=hsecroot;
  1072. if secroot^.fillsize>maxfillsize then
  1073. maxfillsize:=secroot^.fillsize;
  1074. end;
  1075. end;
  1076. if firstsecpos>0 then
  1077. begin
  1078. l:=firstsecpos-filepos(f);
  1079. if l>maxfillsize then
  1080. maxfillsize:=l;
  1081. end
  1082. else
  1083. l:=0;
  1084. { get zero buffer }
  1085. getmem(zerobuf,maxfillsize);
  1086. fillchar(zerobuf^,maxfillsize,0);
  1087. { zero from sectioninfo until first section }
  1088. blockwrite(f,zerobuf^,l);
  1089. { zero section alignments }
  1090. while assigned(secroot) do
  1091. begin
  1092. seek(f,secroot^.fillpos);
  1093. blockwrite(f,zerobuf^,secroot^.fillsize);
  1094. hsecroot:=secroot;
  1095. secroot:=secroot^.next;
  1096. dispose(hsecroot);
  1097. end;
  1098. freemem(zerobuf,maxfillsize);
  1099. close(f);
  1100. {$I+}
  1101. if ioresult<>0 then;
  1102. postprocessexecutable:=true;
  1103. end;
  1104. end.
  1105. {
  1106. $Log$
  1107. Revision 1.9 2001-01-13 00:09:22 peter
  1108. * made Pavel O. happy ;)
  1109. Revision 1.8 2000/12/30 22:53:25 peter
  1110. * export with the case provided in the exports section
  1111. Revision 1.7 2000/12/25 00:07:30 peter
  1112. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1113. tlinkedlist objects)
  1114. Revision 1.6 2000/11/12 22:20:37 peter
  1115. * create generic toutputsection for binary writers
  1116. Revision 1.5 2000/09/24 15:06:31 peter
  1117. * use defines.inc
  1118. Revision 1.4 2000/08/27 16:11:54 peter
  1119. * moved some util functions from globals,cobjects to cutils
  1120. * splitted files into finput,fmodule
  1121. Revision 1.3 2000/07/21 15:14:02 jonas
  1122. + added is_addr field for labels, if they are only used for getting the address
  1123. (e.g. for io checks) and corresponding getaddrlabel() procedure
  1124. Revision 1.2 2000/07/13 11:32:50 michael
  1125. + removed logs
  1126. }