t_win32.pas 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. While not SharedLibFiles.Empty do
  627. begin
  628. S:=SharedLibFiles.Get;
  629. if s<>'c' then
  630. begin
  631. i:=Pos(target_os.sharedlibext,S);
  632. if i>0 then
  633. Delete(S,i,255);
  634. LinkRes.Add('-l'+s);
  635. end
  636. else
  637. begin
  638. LinkRes.Add('-l'+s);
  639. linklibc:=true;
  640. end;
  641. end;
  642. { be sure that libc is the last lib }
  643. if linklibc then
  644. LinkRes.Add('-lc');
  645. LinkRes.Add(')');
  646. { Write staticlibraries }
  647. if not StaticLibFiles.Empty then
  648. begin
  649. LinkRes.Add('GROUP(');
  650. While not StaticLibFiles.Empty do
  651. begin
  652. S:=StaticLibFiles.Get;
  653. LinkRes.AddFileName(s)
  654. end;
  655. LinkRes.Add(')');
  656. end;
  657. { Write and Close response }
  658. linkres.writetodisk;
  659. linkres.done;
  660. WriteResponseFile:=True;
  661. end;
  662. function TLinkerWin32.MakeExecutable:boolean;
  663. var
  664. binstr,
  665. cmdstr : string;
  666. success : boolean;
  667. i : longint;
  668. StripStr,
  669. RelocStr,
  670. AppTypeStr,
  671. ImageBaseStr : string[40];
  672. begin
  673. if not(cs_link_extern in aktglobalswitches) then
  674. Message1(exec_i_linking,current_module^.exefilename^);
  675. { Create some replacements }
  676. RelocStr:='';
  677. AppTypeStr:='';
  678. ImageBaseStr:='';
  679. StripStr:='';
  680. if RelocSection then
  681. RelocStr:='--base-file base.$$$';
  682. if apptype=at_gui then
  683. AppTypeStr:='--subsystem windows';
  684. if assigned(DLLImageBase) then
  685. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  686. if (cs_link_strip in aktglobalswitches) then
  687. StripStr:='-s';
  688. { Write used files and libraries }
  689. WriteResponseFile(false);
  690. { Call linker }
  691. success:=false;
  692. for i:=1to 1 do
  693. begin
  694. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  695. if binstr<>'' then
  696. begin
  697. Replace(cmdstr,'$EXE',current_module^.exefilename^);
  698. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  699. Replace(cmdstr,'$RES',current_module^.outpath^+Info.ResName);
  700. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  701. Replace(cmdstr,'$RELOC',RelocStr);
  702. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  703. Replace(cmdstr,'$STRIP',StripStr);
  704. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  705. if not success then
  706. break;
  707. end;
  708. end;
  709. { Post process }
  710. if success then
  711. success:=PostProcessExecutable(current_module^.exefilename^,false);
  712. { Remove ReponseFile }
  713. if (success) and not(cs_link_extern in aktglobalswitches) then
  714. begin
  715. RemoveFile(current_module^.outpath^+Info.ResName);
  716. RemoveFile('base.$$$');
  717. RemoveFile('exp.$$$');
  718. end;
  719. MakeExecutable:=success; { otherwise a recursive call to link method }
  720. end;
  721. Function TLinkerWin32.MakeSharedLibrary:boolean;
  722. var
  723. binstr,
  724. cmdstr : string;
  725. success : boolean;
  726. i : longint;
  727. StripStr,
  728. RelocStr,
  729. AppTypeStr,
  730. ImageBaseStr : string[40];
  731. begin
  732. MakeSharedLibrary:=false;
  733. if not(cs_link_extern in aktglobalswitches) then
  734. Message1(exec_i_linking,current_module^.sharedlibfilename^);
  735. { Create some replacements }
  736. RelocStr:='';
  737. AppTypeStr:='';
  738. ImageBaseStr:='';
  739. StripStr:='';
  740. if RelocSection then
  741. RelocStr:='--base-file base.$$$';
  742. if apptype=at_gui then
  743. AppTypeStr:='--subsystem windows';
  744. if assigned(DLLImageBase) then
  745. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  746. if (cs_link_strip in aktglobalswitches) then
  747. StripStr:='-s';
  748. { Write used files and libraries }
  749. WriteResponseFile(true);
  750. { Call linker }
  751. success:=false;
  752. for i:=1to 3 do
  753. begin
  754. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  755. if binstr<>'' then
  756. begin
  757. Replace(cmdstr,'$EXE',current_module^.sharedlibfilename^);
  758. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  759. Replace(cmdstr,'$RES',current_module^.outpath^+Info.ResName);
  760. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  761. Replace(cmdstr,'$RELOC',RelocStr);
  762. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  763. Replace(cmdstr,'$STRIP',StripStr);
  764. Replace(cmdstr,'$DEF',deffile.fname);
  765. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  766. if not success then
  767. break;
  768. end;
  769. end;
  770. { Post process }
  771. if success then
  772. success:=PostProcessExecutable(current_module^.sharedlibfilename^,true);
  773. { Remove ReponseFile }
  774. if (success) and not(cs_link_extern in aktglobalswitches) then
  775. begin
  776. RemoveFile(current_module^.outpath^+Info.ResName);
  777. RemoveFile('base.$$$');
  778. RemoveFile('exp.$$$');
  779. end;
  780. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  781. end;
  782. function tlinkerwin32.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  783. type
  784. tdosheader = packed record
  785. e_magic : word;
  786. e_cblp : word;
  787. e_cp : word;
  788. e_crlc : word;
  789. e_cparhdr : word;
  790. e_minalloc : word;
  791. e_maxalloc : word;
  792. e_ss : word;
  793. e_sp : word;
  794. e_csum : word;
  795. e_ip : word;
  796. e_cs : word;
  797. e_lfarlc : word;
  798. e_ovno : word;
  799. e_res : array[0..3] of word;
  800. e_oemid : word;
  801. e_oeminfo : word;
  802. e_res2 : array[0..9] of word;
  803. e_lfanew : longint;
  804. end;
  805. tpeheader = packed record
  806. PEMagic : array[0..3] of char;
  807. Machine : word;
  808. NumberOfSections : word;
  809. TimeDateStamp : longint;
  810. PointerToSymbolTable : longint;
  811. NumberOfSymbols : longint;
  812. SizeOfOptionalHeader : word;
  813. Characteristics : word;
  814. Magic : word;
  815. MajorLinkerVersion : byte;
  816. MinorLinkerVersion : byte;
  817. SizeOfCode : longint;
  818. SizeOfInitializedData : longint;
  819. SizeOfUninitializedData : longint;
  820. AddressOfEntryPoint : longint;
  821. BaseOfCode : longint;
  822. BaseOfData : longint;
  823. ImageBase : longint;
  824. SectionAlignment : longint;
  825. FileAlignment : longint;
  826. MajorOperatingSystemVersion : word;
  827. MinorOperatingSystemVersion : word;
  828. MajorImageVersion : word;
  829. MinorImageVersion : word;
  830. MajorSubsystemVersion : word;
  831. MinorSubsystemVersion : word;
  832. Reserved1 : longint;
  833. SizeOfImage : longint;
  834. SizeOfHeaders : longint;
  835. CheckSum : longint;
  836. Subsystem : word;
  837. DllCharacteristics : word;
  838. SizeOfStackReserve : longint;
  839. SizeOfStackCommit : longint;
  840. SizeOfHeapReserve : longint;
  841. SizeOfHeapCommit : longint;
  842. LoaderFlags : longint;
  843. NumberOfRvaAndSizes : longint;
  844. DataDirectory : array[1..$80] of byte;
  845. end;
  846. tcoffsechdr=packed record
  847. name : array[0..7] of char;
  848. vsize : longint;
  849. rvaofs : longint;
  850. datalen : longint;
  851. datapos : longint;
  852. relocpos : longint;
  853. lineno1 : longint;
  854. nrelocs : word;
  855. lineno2 : word;
  856. flags : longint;
  857. end;
  858. psecfill=^tsecfill;
  859. tsecfill=record
  860. fillpos,
  861. fillsize : longint;
  862. next : psecfill;
  863. end;
  864. var
  865. f : file;
  866. dosheader : tdosheader;
  867. peheader : tpeheader;
  868. firstsecpos,
  869. maxfillsize,
  870. i,l,peheaderpos : longint;
  871. coffsec : tcoffsechdr;
  872. secroot,hsecroot : psecfill;
  873. zerobuf : pointer;
  874. begin
  875. postprocessexecutable:=false;
  876. { when -s is used or it's a dll then quit }
  877. if (cs_link_extern in aktglobalswitches) then
  878. begin
  879. postprocessexecutable:=true;
  880. exit;
  881. end;
  882. { open file }
  883. assign(f,fn);
  884. {$I-}
  885. reset(f,1);
  886. if ioresult<>0 then
  887. Message1(execinfo_f_cant_open_executable,fn);
  888. { read headers }
  889. blockread(f,dosheader,sizeof(tdosheader));
  890. peheaderpos:=dosheader.e_lfanew;
  891. seek(f,peheaderpos);
  892. blockread(f,peheader,sizeof(tpeheader));
  893. { write info }
  894. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  895. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  896. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  897. { change stack size (PM) }
  898. { I am not sure that the default value is adequate !! }
  899. peheader.SizeOfStackReserve:=stacksize;
  900. { change the header }
  901. { sub system }
  902. { gui=2 }
  903. { cui=3 }
  904. if apptype=at_gui then
  905. peheader.Subsystem:=2
  906. else if apptype=at_cui then
  907. peheader.Subsystem:=3;
  908. seek(f,peheaderpos);
  909. blockwrite(f,peheader,sizeof(tpeheader));
  910. if ioresult<>0 then
  911. Message1(execinfo_f_cant_process_executable,fn);
  912. seek(f,peheaderpos);
  913. blockread(f,peheader,sizeof(tpeheader));
  914. { write the value after the change }
  915. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  916. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  917. { read section info }
  918. maxfillsize:=0;
  919. firstsecpos:=0;
  920. secroot:=nil;
  921. for l:=1to peheader.NumberOfSections do
  922. begin
  923. blockread(f,coffsec,sizeof(tcoffsechdr));
  924. if coffsec.datapos>0 then
  925. begin
  926. if secroot=nil then
  927. firstsecpos:=coffsec.datapos;
  928. new(hsecroot);
  929. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  930. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  931. hsecroot^.next:=secroot;
  932. secroot:=hsecroot;
  933. if secroot^.fillsize>maxfillsize then
  934. maxfillsize:=secroot^.fillsize;
  935. end;
  936. end;
  937. if firstsecpos>0 then
  938. begin
  939. l:=firstsecpos-filepos(f);
  940. if l>maxfillsize then
  941. maxfillsize:=l;
  942. end
  943. else
  944. l:=0;
  945. { get zero buffer }
  946. getmem(zerobuf,maxfillsize);
  947. fillchar(zerobuf^,maxfillsize,0);
  948. { zero from sectioninfo until first section }
  949. blockwrite(f,zerobuf^,l);
  950. { zero section alignments }
  951. while assigned(secroot) do
  952. begin
  953. seek(f,secroot^.fillpos);
  954. blockwrite(f,zerobuf^,secroot^.fillsize);
  955. hsecroot:=secroot;
  956. secroot:=secroot^.next;
  957. dispose(hsecroot);
  958. end;
  959. freemem(zerobuf,maxfillsize);
  960. close(f);
  961. {$I+}
  962. i:=ioresult;
  963. postprocessexecutable:=true;
  964. end;
  965. end.
  966. {
  967. $Log$
  968. Revision 1.1 1999-10-21 14:29:38 peter
  969. * redesigned linker object
  970. + library support for linux (only procedures can be exported)
  971. }