t_win32.pas 36 KB

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