t_win32.pas 36 KB

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