t_win32.pas 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. This unit implements support import,export,link routines
  5. for the (i386) Win32 target
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit t_win32;
  20. {$i defines.inc}
  21. interface
  22. implementation
  23. uses
  24. {$ifdef Delphi}
  25. dmisc,
  26. {$else Delphi}
  27. dos,
  28. {$endif Delphi}
  29. cutils,cclasses,
  30. aasm,fmodule,globtype,globals,systems,verbose,
  31. script,gendef,
  32. cpubase,cpuasm,
  33. {$ifdef GDB}
  34. gdb,
  35. {$endif}
  36. import,export,link;
  37. type
  38. timportlibwin32=class(timportlib)
  39. procedure preparelib(const s:string);override;
  40. procedure importprocedure(const func,module:string;index:longint;const name:string);override;
  41. procedure importvariable(const varname,module:string;const name:string);override;
  42. procedure generatelib;override;
  43. procedure generatenasmlib;virtual;
  44. procedure generatesmartlib;override;
  45. end;
  46. texportlibwin32=class(texportlib)
  47. st : string;
  48. last_index : longint;
  49. procedure preparelib(const s:string);override;
  50. procedure exportprocedure(hp : texported_item);override;
  51. procedure exportvar(hp : texported_item);override;
  52. procedure generatelib;override;
  53. procedure generatenasmlib;virtual;
  54. end;
  55. tlinkerwin32=class(tlinker)
  56. private
  57. Function WriteResponseFile(isdll:boolean) : Boolean;
  58. Function PostProcessExecutable(const fn:string;isdll:boolean) : Boolean;
  59. public
  60. Constructor Create;
  61. Procedure SetDefaultInfo;override;
  62. function MakeExecutable:boolean;override;
  63. function MakeSharedLibrary:boolean;override;
  64. end;
  65. tDLLScannerWin32=class(tDLLScanner)
  66. private
  67. cstring : array[0..127]of char;
  68. function DOSstubOK(var x:cardinal):boolean;
  69. function FindDLL(const s:string;var founddll:string):boolean;
  70. function DllName(Const Name : string) : string;
  71. public
  72. function isSuitableFileType(x:cardinal):longbool;override;
  73. function GetEdata(HeaderEntry:cardinal):longbool;override;
  74. function Scan(const binname:string):longbool;override;
  75. end;
  76. function DllName(Const Name : string) : string;
  77. var n : string;
  78. begin
  79. n:=Upper(SplitExtension(Name));
  80. if (n='.DLL') or (n='.DRV') or (n='.EXE') then
  81. DllName:=Name
  82. else
  83. DllName:=Name+target_info.sharedlibext;
  84. end;
  85. {*****************************************************************************
  86. TIMPORTLIBWIN32
  87. *****************************************************************************}
  88. procedure timportlibwin32.preparelib(const s : string);
  89. begin
  90. if not(assigned(importssection)) then
  91. importssection:=TAAsmoutput.create;
  92. end;
  93. procedure timportlibwin32.importprocedure(const func,module : string;index : longint;const name : string);
  94. var
  95. hp1 : timportlist;
  96. hp2 : timported_item;
  97. hs : string;
  98. begin
  99. hs:=DllName(module);
  100. { search for the module }
  101. hp1:=timportlist(current_module.imports.first);
  102. while assigned(hp1) do
  103. begin
  104. if hs=hp1.dllname^ then
  105. break;
  106. hp1:=timportlist(hp1.next);
  107. end;
  108. { generate a new item ? }
  109. if not(assigned(hp1)) then
  110. begin
  111. hp1:=timportlist.create(hs);
  112. current_module.imports.concat(hp1);
  113. end;
  114. { search for reuse of old import item }
  115. hp2:=timported_item(hp1.imported_items.first);
  116. while assigned(hp2) do
  117. begin
  118. if hp2.func^=func then
  119. break;
  120. hp2:=timported_item(hp2.next);
  121. end;
  122. if not assigned(hp2) then
  123. begin
  124. hp2:=timported_item.create(func,name,index);
  125. hp1.imported_items.concat(hp2);
  126. end;
  127. end;
  128. procedure timportlibwin32.importvariable(const varname,module:string;const name:string);
  129. var
  130. hp1 : timportlist;
  131. hp2 : timported_item;
  132. hs : string;
  133. begin
  134. hs:=DllName(module);
  135. { search for the module }
  136. hp1:=timportlist(current_module.imports.first);
  137. while assigned(hp1) do
  138. begin
  139. if hs=hp1.dllname^ then
  140. break;
  141. hp1:=timportlist(hp1.next);
  142. end;
  143. { generate a new item ? }
  144. if not(assigned(hp1)) then
  145. begin
  146. hp1:=timportlist.create(hs);
  147. current_module.imports.concat(hp1);
  148. end;
  149. hp2:=timported_item.create_var(varname,name);
  150. hp1.imported_items.concat(hp2);
  151. end;
  152. procedure timportlibwin32.generatenasmlib;
  153. var
  154. hp1 : timportlist;
  155. hp2 : timported_item;
  156. p : pchar;
  157. begin
  158. importssection.concat(tai_section.create(sec_code));
  159. hp1:=timportlist(current_module.imports.first);
  160. while assigned(hp1) do
  161. begin
  162. hp2:=timported_item(hp1.imported_items.first);
  163. while assigned(hp2) do
  164. begin
  165. if (aktoutputformat=as_i386_tasm) or
  166. (aktoutputformat=as_i386_masm) then
  167. p:=strpnew(#9+'EXTRN '+hp2.func^)
  168. else
  169. p:=strpnew(#9+'EXTERN '+hp2.func^);
  170. importssection.concat(tai_direct.create(p));
  171. p:=strpnew(#9+'import '+hp2.func^+' '+hp1.dllname^+' '+hp2.name^);
  172. importssection.concat(tai_direct.create(p));
  173. hp2:=timported_item(hp2.next);
  174. end;
  175. hp1:=timportlist(hp1.next);
  176. end;
  177. end;
  178. procedure timportlibwin32.generatesmartlib;
  179. var
  180. hp1 : timportlist;
  181. hp2 : timported_item;
  182. lhead,lname,lcode,
  183. lidata4,lidata5 : tasmlabel;
  184. r : preference;
  185. begin
  186. if (aktoutputformat<>as_i386_asw) and
  187. (aktoutputformat<>as_i386_pecoff) then
  188. begin
  189. generatenasmlib;
  190. exit;
  191. end;
  192. hp1:=timportlist(current_module.imports.first);
  193. while assigned(hp1) do
  194. begin
  195. { Get labels for the sections }
  196. getdatalabel(lhead);
  197. getdatalabel(lname);
  198. getaddrlabel(lidata4);
  199. getaddrlabel(lidata5);
  200. { create header for this importmodule }
  201. importsSection.concat(Tai_cut.Create_begin);
  202. importsSection.concat(Tai_section.Create(sec_idata2));
  203. importsSection.concat(Tai_label.Create(lhead));
  204. { pointer to procedure names }
  205. importsSection.concat(Tai_const_symbol.Create_rva(lidata4));
  206. { two empty entries follow }
  207. importsSection.concat(Tai_const.Create_32bit(0));
  208. importsSection.concat(Tai_const.Create_32bit(0));
  209. { pointer to dll name }
  210. importsSection.concat(Tai_const_symbol.Create_rva(lname));
  211. { pointer to fixups }
  212. importsSection.concat(Tai_const_symbol.Create_rva(lidata5));
  213. { first write the name references }
  214. importsSection.concat(Tai_section.Create(sec_idata4));
  215. importsSection.concat(Tai_const.Create_32bit(0));
  216. importsSection.concat(Tai_label.Create(lidata4));
  217. { then the addresses and create also the indirect jump }
  218. importsSection.concat(Tai_section.Create(sec_idata5));
  219. importsSection.concat(Tai_const.Create_32bit(0));
  220. importsSection.concat(Tai_label.Create(lidata5));
  221. { create procedures }
  222. hp2:=timported_item(hp1.imported_items.first);
  223. while assigned(hp2) do
  224. begin
  225. { insert cuts }
  226. importsSection.concat(Tai_cut.Create);
  227. { create indirect jump }
  228. if not hp2.is_var then
  229. begin
  230. getlabel(lcode);
  231. new(r);
  232. reset_reference(r^);
  233. r^.symbol:=lcode;
  234. { place jump in codesegment, insert a code section in the
  235. importsection to reduce the amount of .s files (PFV) }
  236. importsSection.concat(Tai_section.Create(sec_code));
  237. {$IfDef GDB}
  238. if (cs_debuginfo in aktmoduleswitches) then
  239. importsSection.concat(Tai_stab_function_name.Create(nil));
  240. {$EndIf GDB}
  241. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,0));
  242. importsSection.concat(Taicpu.Op_ref(A_JMP,S_NO,r));
  243. importsSection.concat(Tai_align.Create_op(4,$90));
  244. end;
  245. { create head link }
  246. importsSection.concat(Tai_section.Create(sec_idata7));
  247. importsSection.concat(Tai_const_symbol.Create_rva(lhead));
  248. { fixup }
  249. getlabel(tasmlabel(hp2.lab));
  250. importsSection.concat(Tai_section.Create(sec_idata4));
  251. importsSection.concat(Tai_const_symbol.Create_rva(hp2.lab));
  252. { add jump field to importsection }
  253. importsSection.concat(Tai_section.Create(sec_idata5));
  254. if hp2.is_var then
  255. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,0))
  256. else
  257. importsSection.concat(Tai_label.Create(lcode));
  258. if hp2.name^<>'' then
  259. importsSection.concat(Tai_const_symbol.Create_rva(hp2.lab))
  260. else
  261. importsSection.concat(Tai_const.Create_32bit($80000000 or hp2.ordnr));
  262. { finally the import information }
  263. importsSection.concat(Tai_section.Create(sec_idata6));
  264. importsSection.concat(Tai_label.Create(hp2.lab));
  265. importsSection.concat(Tai_const.Create_16bit(hp2.ordnr));
  266. importsSection.concat(Tai_string.Create(hp2.name^+#0));
  267. importsSection.concat(Tai_align.Create_op(2,0));
  268. hp2:=timported_item(hp2.next);
  269. end;
  270. { write final section }
  271. importsSection.concat(Tai_cut.Create_end);
  272. { end of name references }
  273. importsSection.concat(Tai_section.Create(sec_idata4));
  274. importsSection.concat(Tai_const.Create_32bit(0));
  275. { end if addresses }
  276. importsSection.concat(Tai_section.Create(sec_idata5));
  277. importsSection.concat(Tai_const.Create_32bit(0));
  278. { dllname }
  279. importsSection.concat(Tai_section.Create(sec_idata7));
  280. importsSection.concat(Tai_label.Create(lname));
  281. importsSection.concat(Tai_string.Create(hp1.dllname^+#0));
  282. hp1:=timportlist(hp1.next);
  283. end;
  284. end;
  285. procedure timportlibwin32.generatelib;
  286. var
  287. hp1 : timportlist;
  288. hp2 : timported_item;
  289. l1,l2,l3,l4 : tasmlabel;
  290. r : preference;
  291. begin
  292. if (aktoutputformat<>as_i386_asw) and
  293. (aktoutputformat<>as_i386_pecoff) then
  294. begin
  295. generatenasmlib;
  296. exit;
  297. end;
  298. hp1:=timportlist(current_module.imports.first);
  299. while assigned(hp1) do
  300. begin
  301. { align codesegment for the jumps }
  302. importsSection.concat(Tai_section.Create(sec_code));
  303. importsSection.concat(Tai_align.Create_op(4,$90));
  304. { Get labels for the sections }
  305. getlabel(l1);
  306. getlabel(l2);
  307. getlabel(l3);
  308. importsSection.concat(Tai_section.Create(sec_idata2));
  309. { pointer to procedure names }
  310. importsSection.concat(Tai_const_symbol.Create_rva(l2));
  311. { two empty entries follow }
  312. importsSection.concat(Tai_const.Create_32bit(0));
  313. importsSection.concat(Tai_const.Create_32bit(0));
  314. { pointer to dll name }
  315. importsSection.concat(Tai_const_symbol.Create_rva(l1));
  316. { pointer to fixups }
  317. importsSection.concat(Tai_const_symbol.Create_rva(l3));
  318. { only create one section for each else it will
  319. create a lot of idata* }
  320. { first write the name references }
  321. importsSection.concat(Tai_section.Create(sec_idata4));
  322. importsSection.concat(Tai_label.Create(l2));
  323. hp2:=timported_item(hp1.imported_items.first);
  324. while assigned(hp2) do
  325. begin
  326. getlabel(tasmlabel(hp2.lab));
  327. if hp2.name^<>'' then
  328. importsSection.concat(Tai_const_symbol.Create_rva(hp2.lab))
  329. else
  330. importsSection.concat(Tai_const.Create_32bit($80000000 or hp2.ordnr));
  331. hp2:=timported_item(hp2.next);
  332. end;
  333. { finalize the names ... }
  334. importsSection.concat(Tai_const.Create_32bit(0));
  335. { then the addresses and create also the indirect jump }
  336. importsSection.concat(Tai_section.Create(sec_idata5));
  337. importsSection.concat(Tai_label.Create(l3));
  338. hp2:=timported_item(hp1.imported_items.first);
  339. while assigned(hp2) do
  340. begin
  341. if not hp2.is_var then
  342. begin
  343. getlabel(l4);
  344. { create indirect jump }
  345. new(r);
  346. reset_reference(r^);
  347. r^.symbol:=l4;
  348. { place jump in codesegment }
  349. importsSection.concat(Tai_section.Create(sec_code));
  350. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,0));
  351. importsSection.concat(Taicpu.Op_ref(A_JMP,S_NO,r));
  352. importsSection.concat(Tai_align.Create_op(4,$90));
  353. { add jump field to importsection }
  354. importsSection.concat(Tai_section.Create(sec_idata5));
  355. importsSection.concat(Tai_label.Create(l4));
  356. end
  357. else
  358. begin
  359. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,0));
  360. end;
  361. importsSection.concat(Tai_const_symbol.Create_rva(hp2.lab));
  362. hp2:=timported_item(hp2.next);
  363. end;
  364. { finalize the addresses }
  365. importsSection.concat(Tai_const.Create_32bit(0));
  366. { finally the import information }
  367. importsSection.concat(Tai_section.Create(sec_idata6));
  368. hp2:=timported_item(hp1.imported_items.first);
  369. while assigned(hp2) do
  370. begin
  371. importsSection.concat(Tai_label.Create(hp2.lab));
  372. { the ordinal number }
  373. importsSection.concat(Tai_const.Create_16bit(hp2.ordnr));
  374. importsSection.concat(Tai_string.Create(hp2.name^+#0));
  375. importsSection.concat(Tai_align.Create_op(2,0));
  376. hp2:=timported_item(hp2.next);
  377. end;
  378. { create import dll name }
  379. importsSection.concat(Tai_section.Create(sec_idata7));
  380. importsSection.concat(Tai_label.Create(l1));
  381. importsSection.concat(Tai_string.Create(hp1.dllname^+#0));
  382. hp1:=timportlist(hp1.next);
  383. end;
  384. end;
  385. {*****************************************************************************
  386. TEXPORTLIBWIN32
  387. *****************************************************************************}
  388. procedure texportlibwin32.preparelib(const s:string);
  389. begin
  390. if not(assigned(exportssection)) then
  391. exportssection:=TAAsmoutput.create;
  392. last_index:=0;
  393. end;
  394. procedure texportlibwin32.exportvar(hp : texported_item);
  395. begin
  396. { same code used !! PM }
  397. exportprocedure(hp);
  398. end;
  399. procedure texportlibwin32.exportprocedure(hp : texported_item);
  400. { must be ordered at least for win32 !! }
  401. var
  402. hp2 : texported_item;
  403. begin
  404. { first test the index value }
  405. if (hp.options and eo_index)<>0 then
  406. begin
  407. if (hp.index<=0) or (hp.index>$ffff) then
  408. begin
  409. message1(parser_e_export_invalid_index,tostr(hp.index));
  410. exit;
  411. end;
  412. if (hp.index<=last_index) then
  413. begin
  414. message1(parser_e_export_ordinal_double,tostr(hp.index));
  415. { disregard index value }
  416. inc(last_index);
  417. hp.index:=last_index;
  418. exit;
  419. end
  420. else
  421. begin
  422. last_index:=hp.index;
  423. end;
  424. end
  425. else
  426. begin
  427. inc(last_index);
  428. hp.index:=last_index;
  429. end;
  430. { now place in correct order }
  431. hp2:=texported_item(current_module._exports.first);
  432. while assigned(hp2) and
  433. (hp.name^>hp2.name^) do
  434. hp2:=texported_item(hp2.next);
  435. { insert hp there !! }
  436. if assigned(hp2) and (hp2.name^=hp.name^) then
  437. begin
  438. { this is not allowed !! }
  439. message1(parser_e_export_name_double,hp.name^);
  440. exit;
  441. end;
  442. if hp2=texported_item(current_module._exports.first) then
  443. current_module._exports.concat(hp)
  444. else if assigned(hp2) then
  445. begin
  446. hp.next:=hp2;
  447. hp.previous:=hp2.previous;
  448. if assigned(hp2.previous) then
  449. hp2.previous.next:=hp;
  450. hp2.previous:=hp;
  451. end
  452. else
  453. current_module._exports.concat(hp);
  454. end;
  455. procedure texportlibwin32.generatelib;
  456. var
  457. ordinal_base,ordinal_max,ordinal_min : longint;
  458. current_index : longint;
  459. entries,named_entries : longint;
  460. name_label,dll_name_label,export_address_table : tasmlabel;
  461. export_name_table_pointers,export_ordinal_table : tasmlabel;
  462. hp,hp2 : texported_item;
  463. temtexport : TLinkedList;
  464. address_table,name_table_pointers,
  465. name_table,ordinal_table : TAAsmoutput;
  466. begin
  467. if (aktoutputformat<>as_i386_asw) and
  468. (aktoutputformat<>as_i386_pecoff) then
  469. begin
  470. generatenasmlib;
  471. exit;
  472. end;
  473. hp:=texported_item(current_module._exports.first);
  474. if not assigned(hp) then
  475. exit;
  476. ordinal_max:=0;
  477. ordinal_min:=$7FFFFFFF;
  478. entries:=0;
  479. named_entries:=0;
  480. getlabel(dll_name_label);
  481. getlabel(export_address_table);
  482. getlabel(export_name_table_pointers);
  483. getlabel(export_ordinal_table);
  484. { count entries }
  485. while assigned(hp) do
  486. begin
  487. inc(entries);
  488. if (hp.index>ordinal_max) then
  489. ordinal_max:=hp.index;
  490. if (hp.index>0) and (hp.index<ordinal_min) then
  491. ordinal_min:=hp.index;
  492. if assigned(hp.name) then
  493. inc(named_entries);
  494. hp:=texported_item(hp.next);
  495. end;
  496. { no support for higher ordinal base yet !! }
  497. ordinal_base:=1;
  498. current_index:=ordinal_base;
  499. { we must also count the holes !! }
  500. entries:=ordinal_max-ordinal_base+1;
  501. exportsSection.concat(Tai_section.Create(sec_edata));
  502. { export flags }
  503. exportsSection.concat(Tai_const.Create_32bit(0));
  504. { date/time stamp }
  505. exportsSection.concat(Tai_const.Create_32bit(0));
  506. { major version }
  507. exportsSection.concat(Tai_const.Create_16bit(0));
  508. { minor version }
  509. exportsSection.concat(Tai_const.Create_16bit(0));
  510. { pointer to dll name }
  511. exportsSection.concat(Tai_const_symbol.Create_rva(dll_name_label));
  512. { ordinal base normally set to 1 }
  513. exportsSection.concat(Tai_const.Create_32bit(ordinal_base));
  514. { number of entries }
  515. exportsSection.concat(Tai_const.Create_32bit(entries));
  516. { number of named entries }
  517. exportsSection.concat(Tai_const.Create_32bit(named_entries));
  518. { address of export address table }
  519. exportsSection.concat(Tai_const_symbol.Create_rva(export_address_table));
  520. { address of name pointer pointers }
  521. exportsSection.concat(Tai_const_symbol.Create_rva(export_name_table_pointers));
  522. { address of ordinal number pointers }
  523. exportsSection.concat(Tai_const_symbol.Create_rva(export_ordinal_table));
  524. { the name }
  525. exportsSection.concat(Tai_label.Create(dll_name_label));
  526. if st='' then
  527. exportsSection.concat(Tai_string.Create(current_module.modulename^+target_info.sharedlibext+#0))
  528. else
  529. exportsSection.concat(Tai_string.Create(st+target_info.sharedlibext+#0));
  530. { export address table }
  531. address_table:=TAAsmoutput.create;
  532. address_table.concat(Tai_align.Create_op(4,0));
  533. address_table.concat(Tai_label.Create(export_address_table));
  534. name_table_pointers:=TAAsmoutput.create;
  535. name_table_pointers.concat(Tai_align.Create_op(4,0));
  536. name_table_pointers.concat(Tai_label.Create(export_name_table_pointers));
  537. ordinal_table:=TAAsmoutput.create;
  538. ordinal_table.concat(Tai_align.Create_op(4,0));
  539. ordinal_table.concat(Tai_label.Create(export_ordinal_table));
  540. name_table:=TAAsmoutput.Create;
  541. name_table.concat(Tai_align.Create_op(4,0));
  542. { write each address }
  543. hp:=texported_item(current_module._exports.first);
  544. while assigned(hp) do
  545. begin
  546. if (hp.options and eo_name)<>0 then
  547. begin
  548. getlabel(name_label);
  549. name_table_pointers.concat(Tai_const_symbol.Create_rva(name_label));
  550. ordinal_table.concat(Tai_const.Create_16bit(hp.index-ordinal_base));
  551. name_table.concat(Tai_align.Create_op(2,0));
  552. name_table.concat(Tai_label.Create(name_label));
  553. name_table.concat(Tai_string.Create(hp.name^+#0));
  554. end;
  555. hp:=texported_item(hp.next);
  556. end;
  557. { order in increasing ordinal values }
  558. { into temtexport list }
  559. temtexport:=TLinkedList.Create;
  560. hp:=texported_item(current_module._exports.first);
  561. while assigned(hp) do
  562. begin
  563. current_module._exports.remove(hp);
  564. hp2:=texported_item(temtexport.first);
  565. while assigned(hp2) and (hp.index>hp2.index) do
  566. begin
  567. hp2:=texported_item(hp2.next);
  568. end;
  569. if hp2=texported_item(temtexport.first) then
  570. temtexport.insert(hp)
  571. else
  572. begin
  573. if assigned(hp2) then
  574. begin
  575. hp.next:=hp2;
  576. hp.previous:=hp2.previous;
  577. hp2.previous:=hp;
  578. if assigned(hp.previous) then
  579. hp.previous.next:=hp;
  580. end
  581. else
  582. temtexport.concat(hp);
  583. end;
  584. hp:=texported_item(current_module._exports.first);;
  585. end;
  586. { write the export adress table }
  587. current_index:=ordinal_base;
  588. hp:=texported_item(temtexport.first);
  589. while assigned(hp) do
  590. begin
  591. { fill missing values }
  592. while current_index<hp.index do
  593. begin
  594. address_table.concat(Tai_const.Create_32bit(0));
  595. inc(current_index);
  596. end;
  597. address_table.concat(Tai_const_symbol.Createname_rva(hp.sym.mangledname));
  598. inc(current_index);
  599. hp:=texported_item(hp.next);
  600. end;
  601. exportsSection.concatlist(address_table);
  602. exportsSection.concatlist(name_table_pointers);
  603. exportsSection.concatlist(ordinal_table);
  604. exportsSection.concatlist(name_table);
  605. address_table.Free;
  606. name_table_pointers.free;
  607. ordinal_table.free;
  608. name_table.free;
  609. temtexport.free;
  610. end;
  611. procedure texportlibwin32.generatenasmlib;
  612. var
  613. hp : texported_item;
  614. p : pchar;
  615. begin
  616. exportssection.concat(tai_section.create(sec_code));
  617. hp:=texported_item(current_module._exports.first);
  618. while assigned(hp) do
  619. begin
  620. p:=strpnew(#9+'export '+hp.sym.mangledname+' '+hp.name^+' '+tostr(hp.index));
  621. exportssection.concat(tai_direct.create(p));
  622. hp:=texported_item(hp.next);
  623. end;
  624. end;
  625. {****************************************************************************
  626. TLINKERWIN32
  627. ****************************************************************************}
  628. Constructor TLinkerWin32.Create;
  629. begin
  630. Inherited Create;
  631. { allow duplicated libs (PM) }
  632. SharedLibFiles.doubles:=true;
  633. StaticLibFiles.doubles:=true;
  634. If not ForceDeffileForExport then
  635. UseDeffileForExport:=false;
  636. end;
  637. Procedure TLinkerWin32.SetDefaultInfo;
  638. begin
  639. with Info do
  640. begin
  641. ExeCmd[1]:='ldw $OPT $STRIP $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  642. DllCmd[1]:='ldw $OPT $STRIP --dll $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  643. if RelocSection or UseDeffileForExport then
  644. begin
  645. { ExeCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF';
  646. use short forms to avoid 128 char limitation problem }
  647. ExeCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  648. ExeCmd[3]:='ldw $OPT $STRIP $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  649. { DllCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF'; }
  650. DllCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  651. DllCmd[3]:='ldw $OPT $STRIP --dll $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  652. end;
  653. end;
  654. end;
  655. Function TLinkerWin32.WriteResponseFile(isdll:boolean) : Boolean;
  656. Var
  657. linkres : TLinkRes;
  658. HPath : TStringListItem;
  659. s : string;
  660. begin
  661. WriteResponseFile:=False;
  662. { Open link.res file }
  663. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  664. { Write path to search libraries }
  665. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  666. while assigned(HPath) do
  667. begin
  668. LinkRes.Add('SEARCH_DIR('+GetShortName(HPath.Str)+')');
  669. HPath:=TStringListItem(HPath.Next);
  670. end;
  671. HPath:=TStringListItem(LibrarySearchPath.First);
  672. while assigned(HPath) do
  673. begin
  674. LinkRes.Add('SEARCH_DIR('+GetShortName(HPath.Str)+')');
  675. HPath:=TStringListItem(HPath.Next);
  676. end;
  677. { add objectfiles, start with prt0 always }
  678. LinkRes.Add('INPUT(');
  679. if isdll then
  680. LinkRes.AddFileName(GetShortName(FindObjectFile('wdllprt0','')))
  681. else
  682. LinkRes.AddFileName(GetShortName(FindObjectFile('wprt0','')));
  683. while not ObjectFiles.Empty do
  684. begin
  685. s:=ObjectFiles.GetFirst;
  686. if s<>'' then
  687. LinkRes.AddFileName(GetShortName(s));
  688. end;
  689. LinkRes.Add(')');
  690. { Write staticlibraries }
  691. if not StaticLibFiles.Empty then
  692. begin
  693. LinkRes.Add('GROUP(');
  694. While not StaticLibFiles.Empty do
  695. begin
  696. S:=StaticLibFiles.GetFirst;
  697. LinkRes.AddFileName(GetShortName(s));
  698. end;
  699. LinkRes.Add(')');
  700. end;
  701. { Write and Close response }
  702. linkres.writetodisk;
  703. LinkRes.Free;
  704. WriteResponseFile:=True;
  705. end;
  706. function TLinkerWin32.MakeExecutable:boolean;
  707. var
  708. binstr,
  709. cmdstr : string;
  710. success : boolean;
  711. i : longint;
  712. AsBinStr : string[80];
  713. StripStr,
  714. RelocStr,
  715. AppTypeStr,
  716. ImageBaseStr : string[40];
  717. begin
  718. if not(cs_link_extern in aktglobalswitches) then
  719. Message1(exec_i_linking,current_module.exefilename^);
  720. { Create some replacements }
  721. RelocStr:='';
  722. AppTypeStr:='';
  723. ImageBaseStr:='';
  724. StripStr:='';
  725. FindExe('asw',AsBinStr);
  726. if RelocSection then
  727. { Using short form to avoid problems with 128 char limitation under Dos. }
  728. RelocStr:='-b base.$$$';
  729. if apptype=app_gui then
  730. AppTypeStr:='--subsystem windows';
  731. if assigned(DLLImageBase) then
  732. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  733. if (cs_link_strip in aktglobalswitches) then
  734. StripStr:='-s';
  735. { Write used files and libraries }
  736. WriteResponseFile(false);
  737. { Call linker }
  738. success:=false;
  739. for i:=1 to 3 do
  740. begin
  741. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  742. if binstr<>'' then
  743. begin
  744. Replace(cmdstr,'$EXE',current_module.exefilename^);
  745. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  746. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  747. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  748. Replace(cmdstr,'$ASBIN',AsbinStr);
  749. Replace(cmdstr,'$RELOC',RelocStr);
  750. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  751. Replace(cmdstr,'$STRIP',StripStr);
  752. if not DefFile.Empty {and UseDefFileForExport} then
  753. begin
  754. DefFile.WriteFile;
  755. Replace(cmdstr,'$DEF','-d '+deffile.fname);
  756. end
  757. else
  758. Replace(cmdstr,'$DEF','');
  759. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  760. if not success then
  761. break;
  762. end;
  763. end;
  764. { Post process }
  765. if success then
  766. success:=PostProcessExecutable(current_module.exefilename^,false);
  767. { Remove ReponseFile }
  768. if (success) and not(cs_link_extern in aktglobalswitches) then
  769. begin
  770. RemoveFile(outputexedir+Info.ResName);
  771. RemoveFile('base.$$$');
  772. RemoveFile('exp.$$$');
  773. RemoveFile('deffile.$$$');
  774. end;
  775. MakeExecutable:=success; { otherwise a recursive call to link method }
  776. end;
  777. Function TLinkerWin32.MakeSharedLibrary:boolean;
  778. var
  779. binstr,
  780. cmdstr : string;
  781. success : boolean;
  782. i : longint;
  783. AsBinStr : string[80];
  784. StripStr,
  785. RelocStr,
  786. AppTypeStr,
  787. ImageBaseStr : string[40];
  788. begin
  789. MakeSharedLibrary:=false;
  790. if not(cs_link_extern in aktglobalswitches) then
  791. Message1(exec_i_linking,current_module.sharedlibfilename^);
  792. { Create some replacements }
  793. RelocStr:='';
  794. AppTypeStr:='';
  795. ImageBaseStr:='';
  796. StripStr:='';
  797. FindExe('asw',AsBinStr);
  798. if RelocSection then
  799. { Using short form to avoid problems with 128 char limitation under Dos. }
  800. RelocStr:='-b base.$$$';
  801. if apptype=app_gui then
  802. AppTypeStr:='--subsystem windows';
  803. if assigned(DLLImageBase) then
  804. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  805. if (cs_link_strip in aktglobalswitches) then
  806. StripStr:='-s';
  807. { Write used files and libraries }
  808. WriteResponseFile(true);
  809. { Call linker }
  810. success:=false;
  811. for i:=1 to 3 do
  812. begin
  813. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  814. if binstr<>'' then
  815. begin
  816. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  817. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  818. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  819. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  820. Replace(cmdstr,'$ASBIN',AsbinStr);
  821. Replace(cmdstr,'$RELOC',RelocStr);
  822. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  823. Replace(cmdstr,'$STRIP',StripStr);
  824. if not DefFile.Empty {and UseDefFileForExport} then
  825. begin
  826. DefFile.WriteFile;
  827. Replace(cmdstr,'$DEF','-d '+deffile.fname);
  828. end
  829. else
  830. Replace(cmdstr,'$DEF','');
  831. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  832. if not success then
  833. break;
  834. end;
  835. end;
  836. { Post process }
  837. if success then
  838. success:=PostProcessExecutable(current_module.sharedlibfilename^,true);
  839. { Remove ReponseFile }
  840. if (success) and not(cs_link_extern in aktglobalswitches) then
  841. begin
  842. RemoveFile(outputexedir+Info.ResName);
  843. RemoveFile('base.$$$');
  844. RemoveFile('exp.$$$');
  845. end;
  846. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  847. end;
  848. function tlinkerwin32.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  849. type
  850. tdosheader = packed record
  851. e_magic : word;
  852. e_cblp : word;
  853. e_cp : word;
  854. e_crlc : word;
  855. e_cparhdr : word;
  856. e_minalloc : word;
  857. e_maxalloc : word;
  858. e_ss : word;
  859. e_sp : word;
  860. e_csum : word;
  861. e_ip : word;
  862. e_cs : word;
  863. e_lfarlc : word;
  864. e_ovno : word;
  865. e_res : array[0..3] of word;
  866. e_oemid : word;
  867. e_oeminfo : word;
  868. e_res2 : array[0..9] of word;
  869. e_lfanew : longint;
  870. end;
  871. tpeheader = packed record
  872. PEMagic : array[0..3] of char;
  873. Machine : word;
  874. NumberOfSections : word;
  875. TimeDateStamp : longint;
  876. PointerToSymbolTable : longint;
  877. NumberOfSymbols : longint;
  878. SizeOfOptionalHeader : word;
  879. Characteristics : word;
  880. Magic : word;
  881. MajorLinkerVersion : byte;
  882. MinorLinkerVersion : byte;
  883. SizeOfCode : longint;
  884. SizeOfInitializedData : longint;
  885. SizeOfUninitializedData : longint;
  886. AddressOfEntryPoint : longint;
  887. BaseOfCode : longint;
  888. BaseOfData : longint;
  889. ImageBase : longint;
  890. SectionAlignment : longint;
  891. FileAlignment : longint;
  892. MajorOperatingSystemVersion : word;
  893. MinorOperatingSystemVersion : word;
  894. MajorImageVersion : word;
  895. MinorImageVersion : word;
  896. MajorSubsystemVersion : word;
  897. MinorSubsystemVersion : word;
  898. Reserved1 : longint;
  899. SizeOfImage : longint;
  900. SizeOfHeaders : longint;
  901. CheckSum : longint;
  902. Subsystem : word;
  903. DllCharacteristics : word;
  904. SizeOfStackReserve : longint;
  905. SizeOfStackCommit : longint;
  906. SizeOfHeapReserve : longint;
  907. SizeOfHeapCommit : longint;
  908. LoaderFlags : longint;
  909. NumberOfRvaAndSizes : longint;
  910. DataDirectory : array[1..$80] of byte;
  911. end;
  912. tcoffsechdr=packed record
  913. name : array[0..7] of char;
  914. vsize : longint;
  915. rvaofs : longint;
  916. datalen : longint;
  917. datapos : longint;
  918. relocpos : longint;
  919. lineno1 : longint;
  920. nrelocs : word;
  921. lineno2 : word;
  922. flags : longint;
  923. end;
  924. psecfill=^tsecfill;
  925. tsecfill=record
  926. fillpos,
  927. fillsize : longint;
  928. next : psecfill;
  929. end;
  930. var
  931. f : file;
  932. cmdstr : string;
  933. dosheader : tdosheader;
  934. peheader : tpeheader;
  935. firstsecpos,
  936. maxfillsize,
  937. l,peheaderpos : longint;
  938. coffsec : tcoffsechdr;
  939. secroot,hsecroot : psecfill;
  940. zerobuf : pointer;
  941. begin
  942. postprocessexecutable:=false;
  943. { when -s is used or it's a dll then quit }
  944. if (cs_link_extern in aktglobalswitches) then
  945. begin
  946. case apptype of
  947. app_gui :
  948. cmdstr:='--subsystem gui';
  949. app_cui :
  950. cmdstr:='--subsystem console';
  951. end;
  952. if dllversion<>'' then
  953. cmdstr:=cmdstr+' --version '+dllversion;
  954. cmdstr:=cmdstr+' --input '+fn;
  955. cmdstr:=cmdstr+' --stack '+tostr(stacksize);
  956. DoExec(FindUtil('postw32'),cmdstr,false,false);
  957. postprocessexecutable:=true;
  958. exit;
  959. end;
  960. { open file }
  961. assign(f,fn);
  962. {$I-}
  963. reset(f,1);
  964. if ioresult<>0 then
  965. Message1(execinfo_f_cant_open_executable,fn);
  966. { read headers }
  967. blockread(f,dosheader,sizeof(tdosheader));
  968. peheaderpos:=dosheader.e_lfanew;
  969. seek(f,peheaderpos);
  970. blockread(f,peheader,sizeof(tpeheader));
  971. { write info }
  972. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  973. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  974. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  975. { change stack size (PM) }
  976. { I am not sure that the default value is adequate !! }
  977. peheader.SizeOfStackReserve:=stacksize;
  978. { change the header }
  979. { sub system }
  980. { gui=2 }
  981. { cui=3 }
  982. case apptype of
  983. app_gui :
  984. peheader.Subsystem:=2;
  985. app_cui :
  986. peheader.Subsystem:=3;
  987. end;
  988. if dllversion<>'' then
  989. begin
  990. peheader.MajorImageVersion:=dllmajor;
  991. peheader.MinorImageVersion:=dllminor;
  992. end;
  993. { reset timestamp }
  994. peheader.TimeDateStamp:=0;
  995. { write header back }
  996. seek(f,peheaderpos);
  997. blockwrite(f,peheader,sizeof(tpeheader));
  998. if ioresult<>0 then
  999. Message1(execinfo_f_cant_process_executable,fn);
  1000. seek(f,peheaderpos);
  1001. blockread(f,peheader,sizeof(tpeheader));
  1002. { write the value after the change }
  1003. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  1004. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  1005. { read section info }
  1006. maxfillsize:=0;
  1007. firstsecpos:=0;
  1008. secroot:=nil;
  1009. for l:=1 to peheader.NumberOfSections do
  1010. begin
  1011. blockread(f,coffsec,sizeof(tcoffsechdr));
  1012. if coffsec.datapos>0 then
  1013. begin
  1014. if secroot=nil then
  1015. firstsecpos:=coffsec.datapos;
  1016. new(hsecroot);
  1017. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  1018. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  1019. hsecroot^.next:=secroot;
  1020. secroot:=hsecroot;
  1021. if secroot^.fillsize>maxfillsize then
  1022. maxfillsize:=secroot^.fillsize;
  1023. end;
  1024. end;
  1025. if firstsecpos>0 then
  1026. begin
  1027. l:=firstsecpos-filepos(f);
  1028. if l>maxfillsize then
  1029. maxfillsize:=l;
  1030. end
  1031. else
  1032. l:=0;
  1033. { get zero buffer }
  1034. getmem(zerobuf,maxfillsize);
  1035. fillchar(zerobuf^,maxfillsize,0);
  1036. { zero from sectioninfo until first section }
  1037. blockwrite(f,zerobuf^,l);
  1038. { zero section alignments }
  1039. while assigned(secroot) do
  1040. begin
  1041. seek(f,secroot^.fillpos);
  1042. blockwrite(f,zerobuf^,secroot^.fillsize);
  1043. hsecroot:=secroot;
  1044. secroot:=secroot^.next;
  1045. dispose(hsecroot);
  1046. end;
  1047. freemem(zerobuf,maxfillsize);
  1048. close(f);
  1049. {$I+}
  1050. if ioresult<>0 then;
  1051. postprocessexecutable:=true;
  1052. end;
  1053. {****************************************************************************
  1054. TDLLScannerWin32
  1055. ****************************************************************************}
  1056. function tDLLScannerWin32.DOSstubOK(var x:cardinal):boolean;
  1057. begin
  1058. blockread(f,TheWord,2,loaded);
  1059. if loaded<>2 then
  1060. DOSstubOK:=false
  1061. else
  1062. begin
  1063. DOSstubOK:=(TheWord='MZ');
  1064. seek(f,$3C);
  1065. blockread(f,x,4,loaded);
  1066. if(loaded<>4)or(longint(x)>filesize(f))then
  1067. DOSstubOK:=false;
  1068. end;
  1069. end;
  1070. function TDLLScannerWin32.FindDLL(const s:string;var founddll:string):boolean;
  1071. var
  1072. sysdir : string;
  1073. Found : boolean;
  1074. begin
  1075. Found:=false;
  1076. { Look for DLL in:
  1077. 1. Current dir
  1078. 2. Library Path
  1079. 3. windir,windir/system,windir/system32 }
  1080. Found:=FindFile(s,'.'+DirSep,founddll);
  1081. if (not found) then
  1082. Found:=librarysearchpath.FindFile(s,founddll);
  1083. if (not found) then
  1084. begin
  1085. sysdir:=FixPath(GetEnv('windir'),false);
  1086. Found:=FindFile(s,sysdir+';'+sysdir+'system'+DirSep+';'+sysdir+'system32'+DirSep,founddll);
  1087. end;
  1088. if (not found) then
  1089. begin
  1090. message1(exec_w_libfile_not_found,s);
  1091. FoundDll:=s;
  1092. end;
  1093. FindDll:=Found;
  1094. end;
  1095. function tDLLScannerWin32.DllName(Const Name : string) : string;
  1096. var n : string;
  1097. begin
  1098. n:=Upper(SplitExtension(Name));
  1099. if (n='.DLL') or (n='.DRV') or (n='.EXE') then
  1100. DllName:=Name
  1101. else
  1102. DllName:=Name+target_info.sharedlibext;
  1103. end;
  1104. function tDLLScannerWin32.isSuitableFileType(x:cardinal):longbool;
  1105. begin
  1106. seek(f,x);
  1107. blockread(f,TheWord,2,loaded);
  1108. isSuitableFileType:=(loaded=2)and(TheWord='PE');
  1109. end;
  1110. function tDLLScannerWin32.GetEdata(HeaderEntry:cardinal):longbool;
  1111. type
  1112. TObjInfo=packed record
  1113. ObjName:array[0..7]of char;
  1114. VirtSize,
  1115. VirtAddr,
  1116. RawSize,
  1117. RawOffset,
  1118. Reloc,
  1119. LineNum:cardinal;
  1120. RelCount,
  1121. LineCount:word;
  1122. flags:cardinal;
  1123. end;
  1124. var
  1125. i:cardinal;
  1126. ObjOfs:cardinal;
  1127. Obj:TObjInfo;
  1128. APE_obj,APE_Optsize:word;
  1129. ExportRVA:cardinal;
  1130. delta:cardinal;
  1131. const
  1132. IMAGE_SCN_CNT_CODE=$00000020;
  1133. var
  1134. _d:dirstr;
  1135. _n:namestr;
  1136. _e:extstr;
  1137. function isUsedFunction(name:pchar):longbool;
  1138. var
  1139. hp:tExternalsItem;
  1140. begin
  1141. isUsedFunction:=false;
  1142. hp:=tExternalsItem(current_module.Externals.first);
  1143. while assigned(hp)do
  1144. begin
  1145. if(assigned(hp.data))and(not hp.found)then
  1146. if hp.data^=StrPas(name)then
  1147. begin
  1148. isUsedFunction:=true;
  1149. hp.found:=true;
  1150. exit;
  1151. end;
  1152. hp:=tExternalsItem(hp.next);
  1153. end;
  1154. end;
  1155. procedure Store(index:cardinal;name:pchar;isData:longbool);
  1156. begin
  1157. if not isUsedFunction(name)then
  1158. exit;
  1159. if not(current_module.uses_imports) then
  1160. begin
  1161. current_module.uses_imports:=true;
  1162. importlib.preparelib(current_module.modulename^);
  1163. end;
  1164. if IsData then
  1165. importlib.importvariable(name,_n,name)
  1166. else
  1167. importlib.importprocedure(name,_n,index,name);
  1168. end;
  1169. procedure ProcessEdata;
  1170. type
  1171. a8=array[0..7]of char;
  1172. function GetSectionName(rva:cardinal;var Flags:cardinal):a8;
  1173. var
  1174. i:cardinal;
  1175. LocObjOfs:cardinal;
  1176. LocObj:TObjInfo;
  1177. begin
  1178. GetSectionName:='';
  1179. Flags:=0;
  1180. LocObjOfs:=APE_OptSize+HeaderOffset+24;
  1181. for i:=1 to APE_obj do
  1182. begin
  1183. seek(f,LocObjOfs);
  1184. blockread(f,LocObj,sizeof(LocObj));
  1185. if(rva>=LocObj.VirtAddr)and(rva<=LocObj.VirtAddr+LocObj.RawSize)then
  1186. begin
  1187. GetSectionName:=a8(LocObj.ObjName);
  1188. Flags:=LocObj.flags;
  1189. end;
  1190. end;
  1191. end;
  1192. var
  1193. j,Fl:cardinal;
  1194. ulongval,procEntry:cardinal;
  1195. Ordinal:word;
  1196. isData:longbool;
  1197. ExpDir:packed record
  1198. flag,
  1199. stamp:cardinal;
  1200. Major,
  1201. Minor:word;
  1202. Name,
  1203. Base,
  1204. NumFuncs,
  1205. NumNames,
  1206. AddrFuncs,
  1207. AddrNames,
  1208. AddrOrds:cardinal;
  1209. end;
  1210. begin
  1211. with Obj do
  1212. begin
  1213. seek(f,RawOffset+delta);
  1214. blockread(f,ExpDir,sizeof(ExpDir));
  1215. fsplit(impname,_d,_n,_e);
  1216. for j:=0 to pred(ExpDir.NumNames)do
  1217. begin
  1218. seek(f,RawOffset-VirtAddr+ExpDir.AddrOrds+j*2);
  1219. blockread(f,Ordinal,2);
  1220. seek(f,RawOffset-VirtAddr+ExpDir.AddrFuncs+cardinal(Ordinal)*4);
  1221. blockread(f,ProcEntry,4);
  1222. seek(f,RawOffset-VirtAddr+ExpDir.AddrNames+j*4);
  1223. blockread(f,ulongval,4);
  1224. seek(f,RawOffset-VirtAddr+ulongval);
  1225. blockread(f,cstring,sizeof(cstring));
  1226. isData:=GetSectionName(procentry,Fl)='';
  1227. if not isData then
  1228. isData:=Fl and IMAGE_SCN_CNT_CODE<>IMAGE_SCN_CNT_CODE;
  1229. Store(succ(Ordinal),cstring,isData);
  1230. end;
  1231. end;
  1232. end;
  1233. begin
  1234. GetEdata:=false;
  1235. seek(f,HeaderEntry+120);
  1236. blockread(f,ExportRVA,4);
  1237. seek(f,HeaderEntry+6);
  1238. blockread(f,APE_Obj,2);
  1239. seek(f,HeaderEntry+20);
  1240. blockread(f,APE_OptSize,2);
  1241. ObjOfs:=APE_OptSize+HeaderOffset+24;
  1242. for i:=1 to APE_obj do
  1243. begin
  1244. seek(f,ObjOfs);
  1245. blockread(f,Obj,sizeof(Obj));
  1246. inc(ObjOfs,sizeof(Obj));
  1247. with Obj do
  1248. if(VirtAddr<=ExportRva)and(ExportRva<VirtAddr+VirtSize)then
  1249. begin
  1250. delta:=ExportRva-VirtAddr;
  1251. ProcessEdata;
  1252. GetEdata:=true;
  1253. end;
  1254. end;
  1255. end;
  1256. function tDLLScannerWin32.scan(const binname:string):longbool;
  1257. var
  1258. OldFileMode:longint;
  1259. begin
  1260. if not FindDll(DLLName(binname),impname) then
  1261. exit;
  1262. assign(f,impname);
  1263. OldFileMode:=filemode;
  1264. filemode:=0;
  1265. reset(f,1);
  1266. filemode:=OldFileMode;
  1267. if not DOSstubOK(HeaderOffset)then
  1268. scan:=false
  1269. else if not isSuitableFileType(HeaderOffset)then
  1270. scan:=false
  1271. else
  1272. scan:=GetEdata(HeaderOffset);
  1273. close(f);
  1274. end;
  1275. {*****************************************************************************
  1276. Initialize
  1277. *****************************************************************************}
  1278. const
  1279. ar_gnu_arw_info : tarinfo =
  1280. (
  1281. id : ar_gnu_arw;
  1282. arcmd : 'arw rs $LIB $FILES'
  1283. );
  1284. const
  1285. res_gnu_windres_info : tresinfo =
  1286. (
  1287. id : res_gnu_windres;
  1288. resbin : 'windres';
  1289. rescmd : '--include $INC -O coff -o $OBJ $RES'
  1290. );
  1291. const
  1292. target_i386_win32_info : ttargetinfo =
  1293. (
  1294. target : target_i386_WIN32;
  1295. name : 'Win32 for i386';
  1296. shortname : 'Win32';
  1297. flags : [];
  1298. cpu : i386;
  1299. unit_env : 'WIN32UNITS';
  1300. sharedlibext : '.dll';
  1301. staticlibext : '.aw';
  1302. sourceext : '.pp';
  1303. pasext : '.pas';
  1304. exeext : '.exe';
  1305. defext : '.def';
  1306. scriptext : '.bat';
  1307. smartext : '.slw';
  1308. unitext : '.ppw';
  1309. unitlibext : '.ppl';
  1310. asmext : '.sw';
  1311. objext : '.ow';
  1312. resext : '.rc';
  1313. resobjext : '.owr';
  1314. libprefix : 'libp';
  1315. Cprefix : '_';
  1316. newline : #13#10;
  1317. assem : as_i386_pecoff;
  1318. assemextern : as_i386_asw;
  1319. link : ld_i386_win32;
  1320. linkextern : ld_i386_win32;
  1321. ar : ar_gnu_arw;
  1322. res : res_gnu_windres;
  1323. endian : endian_little;
  1324. stackalignment : 4;
  1325. maxCrecordalignment : 16;
  1326. size_of_pointer : 4;
  1327. size_of_longint : 4;
  1328. heapsize : 256*1024;
  1329. maxheapsize : 32*1024*1024;
  1330. stacksize : 32*1024*1024;
  1331. DllScanSupported:true;
  1332. use_bound_instruction : false;
  1333. use_function_relative_addresses : true
  1334. );
  1335. initialization
  1336. RegisterLinker(ld_i386_win32,TLinkerWin32);
  1337. RegisterImport(target_i386_win32,TImportLibWin32);
  1338. RegisterExport(target_i386_win32,TExportLibWin32);
  1339. RegisterDLLScanner(target_i386_win32,TDLLScannerWin32);
  1340. RegisterAr(ar_gnu_arw_info);
  1341. RegisterRes(res_gnu_windres_info);
  1342. RegisterTarget(target_i386_win32_info);
  1343. end.
  1344. {
  1345. $Log$
  1346. Revision 1.7 2001-04-21 13:33:16 peter
  1347. * move winstackpagesize const to cgai386 to remove uses t_win32
  1348. Revision 1.6 2001/04/18 22:02:04 peter
  1349. * registration of targets and assemblers
  1350. Revision 1.5 2001/04/13 23:51:02 peter
  1351. * fix for the stricter compilemode
  1352. Revision 1.4 2001/04/13 01:22:22 peter
  1353. * symtable change to classes
  1354. * range check generation and errors fixed, make cycle DEBUG=1 works
  1355. * memory leaks fixed
  1356. Revision 1.3 2001/04/02 21:20:40 peter
  1357. * resulttype rewrite
  1358. Revision 1.2 2001/03/06 18:28:02 peter
  1359. * patch from Pavel with a new and much faster DLL Scanner for
  1360. automatic importing so $linklib works for DLLs. Thanks Pavel!
  1361. Revision 1.1 2001/02/26 19:43:11 peter
  1362. * moved target units to subdir
  1363. Revision 1.10 2001/02/20 21:41:16 peter
  1364. * new fixfilename, findfile for unix. Look first for lowercase, then
  1365. NormalCase and last for UPPERCASE names.
  1366. Revision 1.9 2001/01/13 00:09:22 peter
  1367. * made Pavel O. happy ;)
  1368. Revision 1.8 2000/12/30 22:53:25 peter
  1369. * export with the case provided in the exports section
  1370. Revision 1.7 2000/12/25 00:07:30 peter
  1371. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1372. tlinkedlist objects)
  1373. Revision 1.6 2000/11/12 22:20:37 peter
  1374. * create generic toutputsection for binary writers
  1375. Revision 1.5 2000/09/24 15:06:31 peter
  1376. * use defines.inc
  1377. Revision 1.4 2000/08/27 16:11:54 peter
  1378. * moved some util functions from globals,cobjects to cutils
  1379. * splitted files into finput,fmodule
  1380. Revision 1.3 2000/07/21 15:14:02 jonas
  1381. + added is_addr field for labels, if they are only used for getting the address
  1382. (e.g. for io checks) and corresponding getaddrlabel() procedure
  1383. Revision 1.2 2000/07/13 11:32:50 michael
  1384. + removed logs
  1385. }