t_win32.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. {
  2. $Id$
  3. Copyright (c) 1999 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. interface
  21. uses import,export,link;
  22. const
  23. winstackpagesize = 4096;
  24. type
  25. pimportlibwin32=^timportlibwin32;
  26. timportlibwin32=object(timportlib)
  27. procedure preparelib(const s:string);virtual;
  28. procedure importprocedure(const func,module:string;index:longint;const name:string);virtual;
  29. procedure importvariable(const varname,module:string;const name:string);virtual;
  30. procedure generatelib;virtual;
  31. procedure generatesmartlib;virtual;
  32. end;
  33. pexportlibwin32=^texportlibwin32;
  34. texportlibwin32=object(texportlib)
  35. st : string;
  36. last_index : longint;
  37. procedure preparelib(const s:string);virtual;
  38. procedure exportprocedure(hp : pexported_item);virtual;
  39. procedure exportvar(hp : pexported_item);virtual;
  40. procedure generatelib;virtual;
  41. end;
  42. plinkerwin32=^tlinkerwin32;
  43. tlinkerwin32=object(tlinker)
  44. private
  45. Function WriteResponseFile(isdll:boolean) : Boolean;
  46. Function PostProcessExecutable(const fn:string;isdll:boolean) : Boolean;
  47. public
  48. Constructor Init;
  49. Procedure SetDefaultInfo;virtual;
  50. function MakeExecutable:boolean;virtual;
  51. function MakeSharedLibrary:boolean;virtual;
  52. end;
  53. implementation
  54. uses
  55. aasm,files,globtype,globals,cobjects,systems,verbose,
  56. script,gendef,
  57. cpubase,cpuasm
  58. {$ifdef GDB}
  59. ,gdb
  60. {$endif}
  61. ;
  62. function DllName(Const Name : string) : string;
  63. var n : string;
  64. begin
  65. n:=Upper(SplitExtension(Name));
  66. if (n='.DLL') or (n='.DRV') or (n='.EXE') then
  67. DllName:=Name
  68. else
  69. DllName:=Name+target_os.sharedlibext;
  70. end;
  71. {*****************************************************************************
  72. TIMPORTLIBWIN32
  73. *****************************************************************************}
  74. procedure timportlibwin32.preparelib(const s : string);
  75. begin
  76. if not(assigned(importssection)) then
  77. importssection:=new(paasmoutput,init);
  78. end;
  79. procedure timportlibwin32.importprocedure(const func,module : string;index : longint;const name : string);
  80. var
  81. hp1 : pimportlist;
  82. hp2 : pimported_item;
  83. hs : string;
  84. begin
  85. hs:=DllName(module);
  86. { search for the module }
  87. hp1:=pimportlist(current_module^.imports^.first);
  88. while assigned(hp1) do
  89. begin
  90. if hs=hp1^.dllname^ then
  91. break;
  92. hp1:=pimportlist(hp1^.next);
  93. end;
  94. { generate a new item ? }
  95. if not(assigned(hp1)) then
  96. begin
  97. hp1:=new(pimportlist,init(hs));
  98. current_module^.imports^.concat(hp1);
  99. end;
  100. { search for reuse of old import item }
  101. hp2:=pimported_item(hp1^.imported_items^.first);
  102. while assigned(hp2) do
  103. begin
  104. if hp2^.func^=func then
  105. break;
  106. hp2:=pimported_item(hp2^.next);
  107. end;
  108. if not assigned(hp2) then
  109. begin
  110. hp2:=new(pimported_item,init(func,name,index));
  111. hp1^.imported_items^.concat(hp2);
  112. end;
  113. end;
  114. procedure timportlibwin32.importvariable(const varname,module:string;const name:string);
  115. var
  116. hp1 : pimportlist;
  117. hp2 : pimported_item;
  118. hs : string;
  119. begin
  120. hs:=DllName(module);
  121. { search for the module }
  122. hp1:=pimportlist(current_module^.imports^.first);
  123. while assigned(hp1) do
  124. begin
  125. if hs=hp1^.dllname^ then
  126. break;
  127. hp1:=pimportlist(hp1^.next);
  128. end;
  129. { generate a new item ? }
  130. if not(assigned(hp1)) then
  131. begin
  132. hp1:=new(pimportlist,init(hs));
  133. current_module^.imports^.concat(hp1);
  134. end;
  135. hp2:=new(pimported_item,init_var(varname,name));
  136. hp1^.imported_items^.concat(hp2);
  137. end;
  138. procedure timportlibwin32.generatesmartlib;
  139. var
  140. hp1 : pimportlist;
  141. hp2 : pimported_item;
  142. lhead,lname,lcode,
  143. lidata4,lidata5 : pasmlabel;
  144. r : preference;
  145. begin
  146. hp1:=pimportlist(current_module^.imports^.first);
  147. while assigned(hp1) do
  148. begin
  149. { Get labels for the sections }
  150. getdatalabel(lhead);
  151. getdatalabel(lname);
  152. getlabel(lidata4);
  153. getlabel(lidata5);
  154. { create header for this importmodule }
  155. importssection^.concat(new(pai_cut,init_begin));
  156. importssection^.concat(new(pai_section,init(sec_idata2)));
  157. importssection^.concat(new(pai_label,init(lhead)));
  158. { pointer to procedure names }
  159. importssection^.concat(new(pai_const_symbol,init_rva(lidata4)));
  160. { two empty entries follow }
  161. importssection^.concat(new(pai_const,init_32bit(0)));
  162. importssection^.concat(new(pai_const,init_32bit(0)));
  163. { pointer to dll name }
  164. importssection^.concat(new(pai_const_symbol,init_rva(lname)));
  165. { pointer to fixups }
  166. importssection^.concat(new(pai_const_symbol,init_rva(lidata5)));
  167. { first write the name references }
  168. importssection^.concat(new(pai_section,init(sec_idata4)));
  169. importssection^.concat(new(pai_const,init_32bit(0)));
  170. importssection^.concat(new(pai_label,init(lidata4)));
  171. { then the addresses and create also the indirect jump }
  172. importssection^.concat(new(pai_section,init(sec_idata5)));
  173. importssection^.concat(new(pai_const,init_32bit(0)));
  174. importssection^.concat(new(pai_label,init(lidata5)));
  175. { create procedures }
  176. hp2:=pimported_item(hp1^.imported_items^.first);
  177. while assigned(hp2) do
  178. begin
  179. { insert cuts }
  180. importssection^.concat(new(pai_cut,init));
  181. { create indirect jump }
  182. if not hp2^.is_var then
  183. begin
  184. getlabel(lcode);
  185. new(r);
  186. reset_reference(r^);
  187. r^.symbol:=lcode;
  188. { place jump in codesegment, insert a code section in the
  189. importsection to reduce the amount of .s files (PFV) }
  190. importssection^.concat(new(pai_section,init(sec_code)));
  191. {$IfDef GDB}
  192. if (cs_debuginfo in aktmoduleswitches) then
  193. importssection^.concat(new(pai_stab_function_name,init(nil)));
  194. {$EndIf GDB}
  195. importssection^.concat(new(pai_symbol,initname_global(hp2^.func^,0)));
  196. importssection^.concat(new(paicpu,op_ref(A_JMP,S_NO,r)));
  197. importssection^.concat(new(pai_align,init_op(4,$90)));
  198. end;
  199. { create head link }
  200. importssection^.concat(new(pai_section,init(sec_idata7)));
  201. importssection^.concat(new(pai_const_symbol,init_rva(lhead)));
  202. { fixup }
  203. getlabel(pasmlabel(hp2^.lab));
  204. importssection^.concat(new(pai_section,init(sec_idata4)));
  205. importssection^.concat(new(pai_const_symbol,init_rva(hp2^.lab)));
  206. { add jump field to importsection }
  207. importssection^.concat(new(pai_section,init(sec_idata5)));
  208. if hp2^.is_var then
  209. importssection^.concat(new(pai_symbol,initname_global(hp2^.func^,0)))
  210. else
  211. importssection^.concat(new(pai_label,init(lcode)));
  212. if hp2^.name^<>'' then
  213. importssection^.concat(new(pai_const_symbol,init_rva(hp2^.lab)))
  214. else
  215. importssection^.concat(new(pai_const,init_32bit($80000000 or hp2^.ordnr)));
  216. { finally the import information }
  217. importssection^.concat(new(pai_section,init(sec_idata6)));
  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. importssection^.concat(new(pai_align,init_op(2,0)));
  222. hp2:=pimported_item(hp2^.next);
  223. end;
  224. { write final section }
  225. importssection^.concat(new(pai_cut,init_end));
  226. { end of name references }
  227. importssection^.concat(new(pai_section,init(sec_idata4)));
  228. importssection^.concat(new(pai_const,init_32bit(0)));
  229. { end if addresses }
  230. importssection^.concat(new(pai_section,init(sec_idata5)));
  231. importssection^.concat(new(pai_const,init_32bit(0)));
  232. { dllname }
  233. importssection^.concat(new(pai_section,init(sec_idata7)));
  234. importssection^.concat(new(pai_label,init(lname)));
  235. importssection^.concat(new(pai_string,init(hp1^.dllname^+#0)));
  236. hp1:=pimportlist(hp1^.next);
  237. end;
  238. end;
  239. procedure timportlibwin32.generatelib;
  240. var
  241. hp1 : pimportlist;
  242. hp2 : pimported_item;
  243. l1,l2,l3,l4 : pasmlabel;
  244. r : preference;
  245. begin
  246. hp1:=pimportlist(current_module^.imports^.first);
  247. while assigned(hp1) do
  248. begin
  249. { align codesegment for the jumps }
  250. importssection^.concat(new(pai_section,init(sec_code)));
  251. importssection^.concat(new(pai_align,init_op(4,$90)));
  252. { Get labels for the sections }
  253. getlabel(l1);
  254. getlabel(l2);
  255. getlabel(l3);
  256. importssection^.concat(new(pai_section,init(sec_idata2)));
  257. { pointer to procedure names }
  258. importssection^.concat(new(pai_const_symbol,init_rva(l2)));
  259. { two empty entries follow }
  260. importssection^.concat(new(pai_const,init_32bit(0)));
  261. importssection^.concat(new(pai_const,init_32bit(0)));
  262. { pointer to dll name }
  263. importssection^.concat(new(pai_const_symbol,init_rva(l1)));
  264. { pointer to fixups }
  265. importssection^.concat(new(pai_const_symbol,init_rva(l3)));
  266. { only create one section for each else it will
  267. create a lot of idata* }
  268. { first write the name references }
  269. importssection^.concat(new(pai_section,init(sec_idata4)));
  270. importssection^.concat(new(pai_label,init(l2)));
  271. hp2:=pimported_item(hp1^.imported_items^.first);
  272. while assigned(hp2) do
  273. begin
  274. getlabel(pasmlabel(hp2^.lab));
  275. if hp2^.name^<>'' then
  276. importssection^.concat(new(pai_const_symbol,init_rva(hp2^.lab)))
  277. else
  278. importssection^.concat(new(pai_const,init_32bit($80000000 or hp2^.ordnr)));
  279. hp2:=pimported_item(hp2^.next);
  280. end;
  281. { finalize the names ... }
  282. importssection^.concat(new(pai_const,init_32bit(0)));
  283. { then the addresses and create also the indirect jump }
  284. importssection^.concat(new(pai_section,init(sec_idata5)));
  285. importssection^.concat(new(pai_label,init(l3)));
  286. hp2:=pimported_item(hp1^.imported_items^.first);
  287. while assigned(hp2) do
  288. begin
  289. if not hp2^.is_var then
  290. begin
  291. getlabel(l4);
  292. { create indirect jump }
  293. new(r);
  294. reset_reference(r^);
  295. r^.symbol:=l4;
  296. { place jump in codesegment }
  297. importssection^.concat(new(pai_section,init(sec_code)));
  298. importssection^.concat(new(pai_symbol,initname_global(hp2^.func^,0)));
  299. importssection^.concat(new(paicpu,op_ref(A_JMP,S_NO,r)));
  300. importssection^.concat(new(pai_align,init_op(4,$90)));
  301. { add jump field to importsection }
  302. importssection^.concat(new(pai_section,init(sec_idata5)));
  303. importssection^.concat(new(pai_label,init(l4)));
  304. end
  305. else
  306. begin
  307. importssection^.concat(new(pai_symbol,initname_global(hp2^.func^,0)));
  308. end;
  309. importssection^.concat(new(pai_const_symbol,init_rva(hp2^.lab)));
  310. hp2:=pimported_item(hp2^.next);
  311. end;
  312. { finalize the addresses }
  313. importssection^.concat(new(pai_const,init_32bit(0)));
  314. { finally the import information }
  315. importssection^.concat(new(pai_section,init(sec_idata6)));
  316. hp2:=pimported_item(hp1^.imported_items^.first);
  317. while assigned(hp2) do
  318. begin
  319. importssection^.concat(new(pai_label,init(hp2^.lab)));
  320. { the ordinal number }
  321. importssection^.concat(new(pai_const,init_16bit(hp2^.ordnr)));
  322. importssection^.concat(new(pai_string,init(hp2^.name^+#0)));
  323. importssection^.concat(new(pai_align,init_op(2,0)));
  324. hp2:=pimported_item(hp2^.next);
  325. end;
  326. { create import dll name }
  327. importssection^.concat(new(pai_section,init(sec_idata7)));
  328. importssection^.concat(new(pai_label,init(l1)));
  329. importssection^.concat(new(pai_string,init(hp1^.dllname^+#0)));
  330. hp1:=pimportlist(hp1^.next);
  331. end;
  332. end;
  333. {*****************************************************************************
  334. TEXPORTLIBWIN32
  335. *****************************************************************************}
  336. procedure texportlibwin32.preparelib(const s:string);
  337. begin
  338. if not(assigned(exportssection)) then
  339. exportssection:=new(paasmoutput,init);
  340. last_index:=0;
  341. end;
  342. procedure texportlibwin32.exportvar(hp : pexported_item);
  343. begin
  344. { same code used !! PM }
  345. exportprocedure(hp);
  346. end;
  347. procedure texportlibwin32.exportprocedure(hp : pexported_item);
  348. { must be ordered at least for win32 !! }
  349. var
  350. hp2 : pexported_item;
  351. begin
  352. { first test the index value }
  353. if (hp^.options and eo_index)<>0 then
  354. begin
  355. if (hp^.index<=0) or (hp^.index>$ffff) then
  356. begin
  357. message1(parser_e_export_invalid_index,tostr(hp^.index));
  358. exit;
  359. end;
  360. hp2:=pexported_item(current_module^._exports^.first);
  361. while assigned(hp2) do
  362. begin
  363. if (hp^.index=hp2^.index) then
  364. if ((hp2^.options and eo_index)<>0) then
  365. begin
  366. message1(parser_e_export_ordinal_double,tostr(hp^.index));
  367. exit;
  368. end
  369. else
  370. begin
  371. inc(last_index);
  372. hp2^.index:=last_index;
  373. end;
  374. hp2:=pexported_item(hp2^.next);
  375. end;
  376. if hp^.index=last_index+1 then
  377. inc(last_index);
  378. end
  379. else
  380. begin
  381. inc(last_index);
  382. hp^.index:=last_index;
  383. end;
  384. { use pascal name is none specified }
  385. if (hp^.options and eo_name)=0 then
  386. begin
  387. hp^.name:=stringdup(hp^.sym^.name);
  388. hp^.options:=hp^.options or eo_name;
  389. end;
  390. { now place in correct order }
  391. hp2:=pexported_item(current_module^._exports^.first);
  392. while assigned(hp2) and
  393. (hp^.name^>hp2^.name^) do
  394. hp2:=pexported_item(hp2^.next);
  395. { insert hp there !! }
  396. if assigned(hp2) and (hp2^.name^=hp^.name^) then
  397. begin
  398. { this is not allowed !! }
  399. message1(parser_e_export_name_double,hp^.name^);
  400. exit;
  401. end;
  402. if hp2=pexported_item(current_module^._exports^.first) then
  403. current_module^._exports^.insert(hp)
  404. else if assigned(hp2) then
  405. begin
  406. hp^.next:=hp2;
  407. hp^.previous:=hp2^.previous;
  408. if assigned(hp2^.previous) then
  409. hp2^.previous^.next:=hp;
  410. hp2^.previous:=hp;
  411. end
  412. else
  413. current_module^._exports^.concat(hp);
  414. end;
  415. procedure texportlibwin32.generatelib;
  416. var
  417. ordinal_base,ordinal_max,ordinal_min : longint;
  418. current_index : longint;
  419. entries,named_entries : longint;
  420. name_label,dll_name_label,export_address_table : pasmlabel;
  421. export_name_table_pointers,export_ordinal_table : pasmlabel;
  422. hp,hp2 : pexported_item;
  423. tempexport : plinkedlist;
  424. address_table,name_table_pointers,
  425. name_table,ordinal_table : paasmoutput;
  426. begin
  427. ordinal_max:=0;
  428. ordinal_min:=$7FFFFFFF;
  429. entries:=0;
  430. named_entries:=0;
  431. getlabel(dll_name_label);
  432. getlabel(export_address_table);
  433. getlabel(export_name_table_pointers);
  434. getlabel(export_ordinal_table);
  435. hp:=pexported_item(current_module^._exports^.first);
  436. { count entries }
  437. while assigned(hp) do
  438. begin
  439. inc(entries);
  440. if (hp^.index>ordinal_max) then
  441. ordinal_max:=hp^.index;
  442. if (hp^.index>0) and (hp^.index<ordinal_min) then
  443. ordinal_min:=hp^.index;
  444. if assigned(hp^.name) then
  445. inc(named_entries);
  446. hp:=pexported_item(hp^.next);
  447. end;
  448. { no support for higher ordinal base yet !! }
  449. ordinal_base:=1;
  450. current_index:=ordinal_base;
  451. { we must also count the holes !! }
  452. entries:=ordinal_max-ordinal_base+1;
  453. exportssection^.concat(new(pai_section,init(sec_edata)));
  454. { export flags }
  455. exportssection^.concat(new(pai_const,init_32bit(0)));
  456. { date/time stamp }
  457. exportssection^.concat(new(pai_const,init_32bit(0)));
  458. { major version }
  459. exportssection^.concat(new(pai_const,init_16bit(0)));
  460. { minor version }
  461. exportssection^.concat(new(pai_const,init_16bit(0)));
  462. { pointer to dll name }
  463. exportssection^.concat(new(pai_const_symbol,init_rva(dll_name_label)));
  464. { ordinal base normally set to 1 }
  465. exportssection^.concat(new(pai_const,init_32bit(ordinal_base)));
  466. { number of entries }
  467. exportssection^.concat(new(pai_const,init_32bit(entries)));
  468. { number of named entries }
  469. exportssection^.concat(new(pai_const,init_32bit(named_entries)));
  470. { address of export address table }
  471. exportssection^.concat(new(pai_const_symbol,init_rva(export_address_table)));
  472. { address of name pointer pointers }
  473. exportssection^.concat(new(pai_const_symbol,init_rva(export_name_table_pointers)));
  474. { address of ordinal number pointers }
  475. exportssection^.concat(new(pai_const_symbol,init_rva(export_ordinal_table)));
  476. { the name }
  477. exportssection^.concat(new(pai_label,init(dll_name_label)));
  478. if st='' then
  479. exportssection^.concat(new(pai_string,init(current_module^.modulename^+target_os.sharedlibext+#0)))
  480. else
  481. exportssection^.concat(new(pai_string,init(st+target_os.sharedlibext+#0)));
  482. { export address table }
  483. address_table:=new(paasmoutput,init);
  484. address_table^.concat(new(pai_align,init_op(4,0)));
  485. address_table^.concat(new(pai_label,init(export_address_table)));
  486. name_table_pointers:=new(paasmoutput,init);
  487. name_table_pointers^.concat(new(pai_align,init_op(4,0)));
  488. name_table_pointers^.concat(new(pai_label,init(export_name_table_pointers)));
  489. ordinal_table:=new(paasmoutput,init);
  490. ordinal_table^.concat(new(pai_align,init_op(4,0)));
  491. ordinal_table^.concat(new(pai_label,init(export_ordinal_table)));
  492. name_table:=new(paasmoutput,init);
  493. name_table^.concat(new(pai_align,init_op(4,0)));
  494. { write each address }
  495. hp:=pexported_item(current_module^._exports^.first);
  496. while assigned(hp) do
  497. begin
  498. if (hp^.options and eo_name)<>0 then
  499. begin
  500. getlabel(name_label);
  501. name_table_pointers^.concat(new(pai_const_symbol,init_rva(name_label)));
  502. ordinal_table^.concat(new(pai_const,init_16bit(hp^.index-ordinal_base)));
  503. name_table^.concat(new(pai_align,init_op(2,0)));
  504. name_table^.concat(new(pai_label,init(name_label)));
  505. name_table^.concat(new(pai_string,init(hp^.name^+#0)));
  506. end;
  507. hp:=pexported_item(hp^.next);
  508. end;
  509. { order in increasing ordinal values }
  510. { into tempexport list }
  511. tempexport:=new(plinkedlist,init);
  512. hp:=pexported_item(current_module^._exports^.first);
  513. while assigned(hp) do
  514. begin
  515. current_module^._exports^.remove(hp);
  516. hp2:=pexported_item(tempexport^.first);
  517. while assigned(hp2) and (hp^.index>hp2^.index) do
  518. begin
  519. hp2:=pexported_item(hp2^.next);
  520. end;
  521. if hp2=pexported_item(tempexport^.first) then
  522. tempexport^.insert(hp)
  523. else
  524. begin
  525. if assigned(hp2) then
  526. begin
  527. hp^.next:=hp2;
  528. hp^.previous:=hp2^.previous;
  529. hp2^.previous:=hp;
  530. if assigned(hp^.previous) then
  531. hp^.previous^.next:=hp;
  532. end
  533. else
  534. tempexport^.concat(hp);
  535. end;
  536. hp:=pexported_item(current_module^._exports^.first);;
  537. end;
  538. { write the export adress table }
  539. current_index:=ordinal_base;
  540. hp:=pexported_item(tempexport^.first);
  541. while assigned(hp) do
  542. begin
  543. { fill missing values }
  544. while current_index<hp^.index do
  545. begin
  546. address_table^.concat(new(pai_const,init_32bit(0)));
  547. inc(current_index);
  548. end;
  549. address_table^.concat(new(pai_const_symbol,initname_rva(hp^.sym^.mangledname)));
  550. inc(current_index);
  551. hp:=pexported_item(hp^.next);
  552. end;
  553. exportssection^.concatlist(address_table);
  554. exportssection^.concatlist(name_table_pointers);
  555. exportssection^.concatlist(ordinal_table);
  556. exportssection^.concatlist(name_table);
  557. dispose(address_table,done);
  558. dispose(name_table_pointers,done);
  559. dispose(ordinal_table,done);
  560. dispose(name_table,done);
  561. dispose(tempexport,done);
  562. end;
  563. {****************************************************************************
  564. TLINKERWIN32
  565. ****************************************************************************}
  566. Constructor TLinkerWin32.Init;
  567. begin
  568. Inherited Init;
  569. { allow duplicated libs (PM) }
  570. SharedLibFiles.doubles:=true;
  571. StaticLibFiles.doubles:=true;
  572. end;
  573. Procedure TLinkerWin32.SetDefaultInfo;
  574. begin
  575. with Info do
  576. begin
  577. ExeCmd[1]:='ldw $OPT $STRIP $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  578. DllCmd[1]:='ldw $OPT --dll $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  579. if RelocSection then
  580. begin
  581. ExeCmd[2]:='dlltool --as asw.exe --dllname $EXE --output-exp exp.$$$ $RELOC $DEF';
  582. ExeCmd[3]:='ldw $OPT $STRIP $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  583. DllCmd[2]:='dlltool --as asw.exe --dllname $EXE --output-exp exp.$$$ $RELOC $DEF';
  584. DllCmd[3]:='ldw $OPT $STRIP --dll $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  585. end;
  586. end;
  587. end;
  588. Function TLinkerWin32.WriteResponseFile(isdll:boolean) : Boolean;
  589. Var
  590. linkres : TLinkRes;
  591. i : longint;
  592. HPath : PStringQueueItem;
  593. s : string;
  594. linklibc : boolean;
  595. begin
  596. WriteResponseFile:=False;
  597. { Open link.res file }
  598. LinkRes.Init(Info.ResName);
  599. { Write path to search libraries }
  600. HPath:=current_module^.locallibrarysearchpath.First;
  601. while assigned(HPath) do
  602. begin
  603. LinkRes.Add('SEARCH_DIR('+HPath^.Data^+')');
  604. HPath:=HPath^.Next;
  605. end;
  606. HPath:=LibrarySearchPath.First;
  607. while assigned(HPath) do
  608. begin
  609. LinkRes.Add('SEARCH_DIR('+HPath^.Data^+')');
  610. HPath:=HPath^.Next;
  611. end;
  612. { add objectfiles, start with prt0 always }
  613. LinkRes.Add('INPUT(');
  614. if isdll then
  615. LinkRes.AddFileName(FindObjectFile('wdllprt0'))
  616. else
  617. LinkRes.AddFileName(FindObjectFile('wprt0'));
  618. while not ObjectFiles.Empty do
  619. begin
  620. s:=ObjectFiles.Get;
  621. if s<>'' then
  622. LinkRes.AddFileName(s);
  623. end;
  624. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  625. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  626. linklibc:=false;
  627. While not SharedLibFiles.Empty do
  628. begin
  629. S:=SharedLibFiles.Get;
  630. if s<>'c' then
  631. begin
  632. i:=Pos(target_os.sharedlibext,S);
  633. if i>0 then
  634. Delete(S,i,255);
  635. LinkRes.Add('-l'+s);
  636. end
  637. else
  638. begin
  639. LinkRes.Add('-l'+s);
  640. linklibc:=true;
  641. end;
  642. end;
  643. { be sure that libc is the last lib }
  644. if linklibc then
  645. LinkRes.Add('-lc');
  646. LinkRes.Add(')');
  647. { Write staticlibraries }
  648. if not StaticLibFiles.Empty then
  649. begin
  650. LinkRes.Add('GROUP(');
  651. While not StaticLibFiles.Empty do
  652. begin
  653. S:=StaticLibFiles.Get;
  654. LinkRes.AddFileName(s)
  655. end;
  656. LinkRes.Add(')');
  657. end;
  658. { Write and Close response }
  659. linkres.writetodisk;
  660. linkres.done;
  661. WriteResponseFile:=True;
  662. end;
  663. function TLinkerWin32.MakeExecutable:boolean;
  664. var
  665. binstr,
  666. cmdstr : string;
  667. success : boolean;
  668. i : longint;
  669. StripStr,
  670. RelocStr,
  671. AppTypeStr,
  672. ImageBaseStr : string[40];
  673. begin
  674. if not(cs_link_extern in aktglobalswitches) then
  675. Message1(exec_i_linking,current_module^.exefilename^);
  676. { Create some replacements }
  677. RelocStr:='';
  678. AppTypeStr:='';
  679. ImageBaseStr:='';
  680. StripStr:='';
  681. if RelocSection then
  682. RelocStr:='--base-file base.$$$';
  683. if apptype=at_gui then
  684. AppTypeStr:='--subsystem windows';
  685. if assigned(DLLImageBase) then
  686. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  687. if (cs_link_strip in aktglobalswitches) then
  688. StripStr:='-s';
  689. { Write used files and libraries }
  690. WriteResponseFile(false);
  691. { Call linker }
  692. success:=false;
  693. for i:=1 to 3 do
  694. begin
  695. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  696. if binstr<>'' then
  697. begin
  698. Replace(cmdstr,'$EXE',current_module^.exefilename^);
  699. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  700. Replace(cmdstr,'$RES',current_module^.outpath^+Info.ResName);
  701. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  702. Replace(cmdstr,'$RELOC',RelocStr);
  703. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  704. Replace(cmdstr,'$STRIP',StripStr);
  705. if not DefFile.Empty then
  706. Replace(cmdstr,'$DEF','-d '+deffile.fname)
  707. else
  708. Replace(cmdstr,'$DEF','');
  709. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  710. if not success then
  711. break;
  712. end;
  713. end;
  714. { Post process }
  715. if success then
  716. success:=PostProcessExecutable(current_module^.exefilename^,false);
  717. { Remove ReponseFile }
  718. if (success) and not(cs_link_extern in aktglobalswitches) then
  719. begin
  720. RemoveFile(current_module^.outpath^+Info.ResName);
  721. RemoveFile('base.$$$');
  722. RemoveFile('exp.$$$');
  723. end;
  724. MakeExecutable:=success; { otherwise a recursive call to link method }
  725. end;
  726. Function TLinkerWin32.MakeSharedLibrary:boolean;
  727. var
  728. binstr,
  729. cmdstr : string;
  730. success : boolean;
  731. i : longint;
  732. StripStr,
  733. RelocStr,
  734. AppTypeStr,
  735. ImageBaseStr : string[40];
  736. begin
  737. MakeSharedLibrary:=false;
  738. if not(cs_link_extern in aktglobalswitches) then
  739. Message1(exec_i_linking,current_module^.sharedlibfilename^);
  740. { Create some replacements }
  741. RelocStr:='';
  742. AppTypeStr:='';
  743. ImageBaseStr:='';
  744. StripStr:='';
  745. if RelocSection then
  746. RelocStr:='--base-file base.$$$';
  747. if apptype=at_gui then
  748. AppTypeStr:='--subsystem windows';
  749. if assigned(DLLImageBase) then
  750. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  751. if (cs_link_strip in aktglobalswitches) then
  752. StripStr:='-s';
  753. { Write used files and libraries }
  754. WriteResponseFile(true);
  755. { Call linker }
  756. success:=false;
  757. for i:=1to 3 do
  758. begin
  759. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  760. if binstr<>'' then
  761. begin
  762. Replace(cmdstr,'$EXE',current_module^.sharedlibfilename^);
  763. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  764. Replace(cmdstr,'$RES',current_module^.outpath^+Info.ResName);
  765. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  766. Replace(cmdstr,'$RELOC',RelocStr);
  767. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  768. Replace(cmdstr,'$STRIP',StripStr);
  769. if not DefFile.Empty then
  770. Replace(cmdstr,'$DEF','-d '+deffile.fname)
  771. else
  772. Replace(cmdstr,'$DEF','');
  773. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  774. if not success then
  775. break;
  776. end;
  777. end;
  778. { Post process }
  779. if success then
  780. success:=PostProcessExecutable(current_module^.sharedlibfilename^,true);
  781. { Remove ReponseFile }
  782. if (success) and not(cs_link_extern in aktglobalswitches) then
  783. begin
  784. RemoveFile(current_module^.outpath^+Info.ResName);
  785. RemoveFile('base.$$$');
  786. RemoveFile('exp.$$$');
  787. end;
  788. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  789. end;
  790. function tlinkerwin32.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  791. type
  792. tdosheader = packed record
  793. e_magic : word;
  794. e_cblp : word;
  795. e_cp : word;
  796. e_crlc : word;
  797. e_cparhdr : word;
  798. e_minalloc : word;
  799. e_maxalloc : word;
  800. e_ss : word;
  801. e_sp : word;
  802. e_csum : word;
  803. e_ip : word;
  804. e_cs : word;
  805. e_lfarlc : word;
  806. e_ovno : word;
  807. e_res : array[0..3] of word;
  808. e_oemid : word;
  809. e_oeminfo : word;
  810. e_res2 : array[0..9] of word;
  811. e_lfanew : longint;
  812. end;
  813. tpeheader = packed record
  814. PEMagic : array[0..3] of char;
  815. Machine : word;
  816. NumberOfSections : word;
  817. TimeDateStamp : longint;
  818. PointerToSymbolTable : longint;
  819. NumberOfSymbols : longint;
  820. SizeOfOptionalHeader : word;
  821. Characteristics : word;
  822. Magic : word;
  823. MajorLinkerVersion : byte;
  824. MinorLinkerVersion : byte;
  825. SizeOfCode : longint;
  826. SizeOfInitializedData : longint;
  827. SizeOfUninitializedData : longint;
  828. AddressOfEntryPoint : longint;
  829. BaseOfCode : longint;
  830. BaseOfData : longint;
  831. ImageBase : longint;
  832. SectionAlignment : longint;
  833. FileAlignment : longint;
  834. MajorOperatingSystemVersion : word;
  835. MinorOperatingSystemVersion : word;
  836. MajorImageVersion : word;
  837. MinorImageVersion : word;
  838. MajorSubsystemVersion : word;
  839. MinorSubsystemVersion : word;
  840. Reserved1 : longint;
  841. SizeOfImage : longint;
  842. SizeOfHeaders : longint;
  843. CheckSum : longint;
  844. Subsystem : word;
  845. DllCharacteristics : word;
  846. SizeOfStackReserve : longint;
  847. SizeOfStackCommit : longint;
  848. SizeOfHeapReserve : longint;
  849. SizeOfHeapCommit : longint;
  850. LoaderFlags : longint;
  851. NumberOfRvaAndSizes : longint;
  852. DataDirectory : array[1..$80] of byte;
  853. end;
  854. tcoffsechdr=packed record
  855. name : array[0..7] of char;
  856. vsize : longint;
  857. rvaofs : longint;
  858. datalen : longint;
  859. datapos : longint;
  860. relocpos : longint;
  861. lineno1 : longint;
  862. nrelocs : word;
  863. lineno2 : word;
  864. flags : longint;
  865. end;
  866. psecfill=^tsecfill;
  867. tsecfill=record
  868. fillpos,
  869. fillsize : longint;
  870. next : psecfill;
  871. end;
  872. var
  873. f : file;
  874. dosheader : tdosheader;
  875. peheader : tpeheader;
  876. firstsecpos,
  877. maxfillsize,
  878. i,l,peheaderpos : longint;
  879. coffsec : tcoffsechdr;
  880. secroot,hsecroot : psecfill;
  881. zerobuf : pointer;
  882. begin
  883. postprocessexecutable:=false;
  884. { when -s is used or it's a dll then quit }
  885. if (cs_link_extern in aktglobalswitches) then
  886. begin
  887. postprocessexecutable:=true;
  888. exit;
  889. end;
  890. { open file }
  891. assign(f,fn);
  892. {$I-}
  893. reset(f,1);
  894. if ioresult<>0 then
  895. Message1(execinfo_f_cant_open_executable,fn);
  896. { read headers }
  897. blockread(f,dosheader,sizeof(tdosheader));
  898. peheaderpos:=dosheader.e_lfanew;
  899. seek(f,peheaderpos);
  900. blockread(f,peheader,sizeof(tpeheader));
  901. { write info }
  902. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  903. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  904. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  905. { change stack size (PM) }
  906. { I am not sure that the default value is adequate !! }
  907. peheader.SizeOfStackReserve:=stacksize;
  908. { change the header }
  909. { sub system }
  910. { gui=2 }
  911. { cui=3 }
  912. if apptype=at_gui then
  913. peheader.Subsystem:=2
  914. else if apptype=at_cui then
  915. peheader.Subsystem:=3;
  916. seek(f,peheaderpos);
  917. blockwrite(f,peheader,sizeof(tpeheader));
  918. if ioresult<>0 then
  919. Message1(execinfo_f_cant_process_executable,fn);
  920. seek(f,peheaderpos);
  921. blockread(f,peheader,sizeof(tpeheader));
  922. { write the value after the change }
  923. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  924. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  925. { read section info }
  926. maxfillsize:=0;
  927. firstsecpos:=0;
  928. secroot:=nil;
  929. for l:=1to peheader.NumberOfSections do
  930. begin
  931. blockread(f,coffsec,sizeof(tcoffsechdr));
  932. if coffsec.datapos>0 then
  933. begin
  934. if secroot=nil then
  935. firstsecpos:=coffsec.datapos;
  936. new(hsecroot);
  937. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  938. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  939. hsecroot^.next:=secroot;
  940. secroot:=hsecroot;
  941. if secroot^.fillsize>maxfillsize then
  942. maxfillsize:=secroot^.fillsize;
  943. end;
  944. end;
  945. if firstsecpos>0 then
  946. begin
  947. l:=firstsecpos-filepos(f);
  948. if l>maxfillsize then
  949. maxfillsize:=l;
  950. end
  951. else
  952. l:=0;
  953. { get zero buffer }
  954. getmem(zerobuf,maxfillsize);
  955. fillchar(zerobuf^,maxfillsize,0);
  956. { zero from sectioninfo until first section }
  957. blockwrite(f,zerobuf^,l);
  958. { zero section alignments }
  959. while assigned(secroot) do
  960. begin
  961. seek(f,secroot^.fillpos);
  962. blockwrite(f,zerobuf^,secroot^.fillsize);
  963. hsecroot:=secroot;
  964. secroot:=secroot^.next;
  965. dispose(hsecroot);
  966. end;
  967. freemem(zerobuf,maxfillsize);
  968. close(f);
  969. {$I+}
  970. i:=ioresult;
  971. postprocessexecutable:=true;
  972. end;
  973. end.
  974. {
  975. $Log$
  976. Revision 1.7 1999-11-15 15:01:56 pierre
  977. + Pavel's changes to support reloc section in exes
  978. Revision 1.6 1999/11/12 11:03:50 peter
  979. * searchpaths changed to stringqueue object
  980. Revision 1.5 1999/11/04 10:55:31 peter
  981. * TSearchPathString for the string type of the searchpaths, which is
  982. ansistring under FPC/Delphi
  983. Revision 1.4 1999/11/02 15:06:58 peter
  984. * import library fixes for win32
  985. * alignment works again
  986. Revision 1.3 1999/10/28 10:33:06 pierre
  987. * Libs can be link serveral times
  988. Revision 1.2 1999/10/22 14:42:40 peter
  989. * reset linklibc
  990. Revision 1.1 1999/10/21 14:29:38 peter
  991. * redesigned linker object
  992. + library support for linux (only procedures can be exported)
  993. }