t_win32.pas 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.inc}
  21. interface
  22. uses
  23. {$ifdef Delphi}
  24. dmisc,
  25. sysutils,
  26. {$else Delphi}
  27. dos,
  28. {$endif Delphi}
  29. cutils,cclasses,
  30. aasmbase,aasmtai,aasmcpu,fmodule,globtype,globals,systems,verbose,
  31. symconst,symdef,symsym,
  32. script,gendef,
  33. cpubase,
  34. {$ifdef GDB}
  35. gdb,
  36. {$endif}
  37. import,export,link,cgobj,i_win32;
  38. const
  39. MAX_DEFAULT_EXTENSIONS = 3;
  40. type
  41. tStr4=array[1..MAX_DEFAULT_EXTENSIONS]of string[4];
  42. pStr4=^tStr4;
  43. twin32imported_item = class(timported_item)
  44. procdef : tprocdef;
  45. end;
  46. timportlibwin32=class(timportlib)
  47. private
  48. procedure win32importproc(aprocdef:tprocdef;const func,module : string;index : longint;const name : string);
  49. procedure importvariable_str(const s:string;const name,module:string);
  50. procedure importprocedure_str(const func,module:string;index:longint;const name:string);
  51. public
  52. procedure GetDefExt(var N:longint;var P:pStr4);virtual;
  53. procedure preparelib(const s:string);override;
  54. procedure importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);override;
  55. procedure importvariable(vs:tvarsym;const name,module:string);override;
  56. procedure generatelib;override;
  57. procedure generatenasmlib;virtual;
  58. procedure generatesmartlib;override;
  59. end;
  60. texportlibwin32=class(texportlib)
  61. st : string;
  62. EList_indexed:tList;
  63. EList_nonindexed:tList;
  64. procedure preparelib(const s:string);override;
  65. procedure exportprocedure(hp : texported_item);override;
  66. procedure exportvar(hp : texported_item);override;
  67. procedure exportfromlist(hp : texported_item);
  68. procedure generatelib;override;
  69. procedure generatenasmlib;virtual;
  70. end;
  71. tlinkerwin32=class(texternallinker)
  72. private
  73. Function WriteResponseFile(isdll:boolean) : Boolean;
  74. Function PostProcessExecutable(const fn:string;isdll:boolean) : Boolean;
  75. public
  76. Constructor Create;override;
  77. Procedure SetDefaultInfo;override;
  78. function MakeExecutable:boolean;override;
  79. function MakeSharedLibrary:boolean;override;
  80. end;
  81. tDLLScannerWin32=class(tDLLScanner)
  82. private
  83. cstring : array[0..127]of char;
  84. function DOSstubOK(var x:cardinal):boolean;
  85. function FindDLL(const s:string;var founddll:string):boolean;
  86. function ExtractDllName(Const Name : string) : string;
  87. public
  88. procedure GetDefExt(var N:longint;var P:pStr4);virtual;
  89. function isSuitableFileType(x:cardinal):longbool;override;
  90. function GetEdata(HeaderEntry:cardinal):longbool;override;
  91. function Scan(const binname:string):longbool;override;
  92. end;
  93. implementation
  94. uses
  95. cpuinfo,cgutils;
  96. function DllName(Const Name : string;NdefExt:longint;DefExt:pStr4) : string;
  97. var n : string;
  98. i:longint;
  99. begin
  100. n:=Upper(SplitExtension(Name));
  101. for i:=1 to NdefExt do
  102. if n=DefExt^[i]then
  103. begin
  104. DllName:=Name;
  105. exit;
  106. end
  107. else
  108. DllName:=Name+target_info.sharedlibext;
  109. end;
  110. const
  111. DefaultDLLExtensions:array[1..MAX_DEFAULT_EXTENSIONS]of string[4]=('.DLL','.DRV','.EXE');
  112. {*****************************************************************************
  113. TIMPORTLIBWIN32
  114. *****************************************************************************}
  115. procedure timportlibwin32.GetDefExt(var N:longint;var P:pStr4);
  116. begin
  117. N:=sizeof(DefaultDLLExtensions)div sizeof(DefaultDLLExtensions[1]);
  118. pointer(P):=@DefaultDLLExtensions;
  119. end;
  120. procedure timportlibwin32.preparelib(const s : string);
  121. begin
  122. if not(assigned(importssection)) then
  123. importssection:=TAAsmoutput.create;
  124. end;
  125. procedure timportlibwin32.win32importproc(aprocdef:tprocdef;const func,module : string;index : longint;const name : string);
  126. var
  127. hp1 : timportlist;
  128. hp2 : twin32imported_item;
  129. hs : string;
  130. PP : pStr4;
  131. NN : longint;
  132. begin
  133. { force the current mangledname }
  134. if assigned(aprocdef) then
  135. aprocdef.has_mangledname:=true;
  136. { append extension if required }
  137. GetDefExt(NN,PP);
  138. hs:=DllName(module,NN,PP);
  139. { search for the module }
  140. hp1:=timportlist(current_module.imports.first);
  141. while assigned(hp1) do
  142. begin
  143. if hs=hp1.dllname^ then
  144. break;
  145. hp1:=timportlist(hp1.next);
  146. end;
  147. { generate a new item ? }
  148. if not(assigned(hp1)) then
  149. begin
  150. hp1:=timportlist.create(hs);
  151. current_module.imports.concat(hp1);
  152. end;
  153. { search for reuse of old import item }
  154. hp2:=twin32imported_item(hp1.imported_items.first);
  155. while assigned(hp2) do
  156. begin
  157. if hp2.func^=func then
  158. break;
  159. hp2:=twin32imported_item(hp2.next);
  160. end;
  161. if not assigned(hp2) then
  162. begin
  163. hp2:=twin32imported_item.create(func,name,index);
  164. hp2.procdef:=aprocdef;
  165. hp1.imported_items.concat(hp2);
  166. end;
  167. end;
  168. procedure timportlibwin32.importprocedure(aprocdef:tprocdef;const module : string;index : longint;const name : string);
  169. begin
  170. win32importproc(aprocdef,aprocdef.mangledname,module,index,name);
  171. end;
  172. procedure timportlibwin32.importprocedure_str(const func,module : string;index : longint;const name : string);
  173. begin
  174. win32importproc(nil,func,module,index,name);
  175. end;
  176. procedure timportlibwin32.importvariable(vs:tvarsym;const name,module:string);
  177. begin
  178. importvariable_str(vs.mangledname,name,module);
  179. end;
  180. procedure timportlibwin32.importvariable_str(const s:string;const name,module:string);
  181. var
  182. hp1 : timportlist;
  183. hp2 : twin32imported_item;
  184. hs : string;
  185. NN : longint;
  186. PP : pStr4;
  187. begin
  188. GetDefExt(NN,PP);
  189. hs:=DllName(module,NN,PP);
  190. { search for the module }
  191. hp1:=timportlist(current_module.imports.first);
  192. while assigned(hp1) do
  193. begin
  194. if hs=hp1.dllname^ then
  195. break;
  196. hp1:=timportlist(hp1.next);
  197. end;
  198. { generate a new item ? }
  199. if not(assigned(hp1)) then
  200. begin
  201. hp1:=timportlist.create(hs);
  202. current_module.imports.concat(hp1);
  203. end;
  204. hp2:=twin32imported_item.create_var(s,name);
  205. hp2.procdef:=nil;
  206. hp1.imported_items.concat(hp2);
  207. end;
  208. procedure timportlibwin32.generatenasmlib;
  209. var
  210. hp1 : timportlist;
  211. hp2 : twin32imported_item;
  212. p : pchar;
  213. begin
  214. new_section(importssection,sec_code,'',0);
  215. hp1:=timportlist(current_module.imports.first);
  216. while assigned(hp1) do
  217. begin
  218. hp2:=twin32imported_item(hp1.imported_items.first);
  219. while assigned(hp2) do
  220. begin
  221. if (aktoutputformat in [as_i386_tasm,as_i386_masm]) then
  222. p:=strpnew(#9+'EXTRN '+hp2.func^)
  223. else
  224. p:=strpnew(#9+'EXTERN '+hp2.func^);
  225. importssection.concat(tai_direct.create(p));
  226. p:=strpnew(#9+'import '+hp2.func^+' '+hp1.dllname^+' '+hp2.name^);
  227. importssection.concat(tai_direct.create(p));
  228. hp2:=twin32imported_item(hp2.next);
  229. end;
  230. hp1:=timportlist(hp1.next);
  231. end;
  232. end;
  233. procedure timportlibwin32.generatesmartlib;
  234. var
  235. hp1 : timportlist;
  236. {$ifdef GDB}
  237. importname : string;
  238. mangledstring : string;
  239. suffix : integer;
  240. {$endif GDB}
  241. hp2 : twin32imported_item;
  242. lhead,lname,lcode,
  243. lidata4,lidata5 : tasmlabel;
  244. href : treference;
  245. begin
  246. if (aktoutputformat in [as_i386_masm,as_i386_tasm,as_i386_nasmwin32]) then
  247. begin
  248. generatenasmlib;
  249. exit;
  250. end;
  251. hp1:=timportlist(current_module.imports.first);
  252. while assigned(hp1) do
  253. begin
  254. { Get labels for the sections }
  255. objectlibrary.getdatalabel(lhead);
  256. objectlibrary.getdatalabel(lname);
  257. objectlibrary.getaddrlabel(lidata4);
  258. objectlibrary.getaddrlabel(lidata5);
  259. { create header for this importmodule }
  260. importsSection.concat(Tai_cutobject.Create_begin);
  261. new_section(importsSection,sec_idata2,'',0);
  262. importsSection.concat(Tai_label.Create(lhead));
  263. { pointer to procedure names }
  264. importsSection.concat(Tai_const.Create_rva_sym(lidata4));
  265. { two empty entries follow }
  266. importsSection.concat(Tai_const.Create_32bit(0));
  267. importsSection.concat(Tai_const.Create_32bit(0));
  268. { pointer to dll name }
  269. importsSection.concat(Tai_const.Create_rva_sym(lname));
  270. { pointer to fixups }
  271. importsSection.concat(Tai_const.Create_rva_sym(lidata5));
  272. { first write the name references }
  273. new_section(importsSection,sec_idata4,'',0);
  274. importsSection.concat(Tai_const.Create_32bit(0));
  275. importsSection.concat(Tai_label.Create(lidata4));
  276. { then the addresses and create also the indirect jump }
  277. new_section(importsSection,sec_idata5,'',0);
  278. importsSection.concat(Tai_const.Create_32bit(0));
  279. importsSection.concat(Tai_label.Create(lidata5));
  280. { create procedures }
  281. hp2:=twin32imported_item(hp1.imported_items.first);
  282. while assigned(hp2) do
  283. begin
  284. { insert cuts }
  285. importsSection.concat(Tai_cutobject.Create);
  286. { create indirect jump }
  287. if not hp2.is_var then
  288. begin
  289. objectlibrary.getlabel(lcode);
  290. reference_reset_symbol(href,lcode,0);
  291. { place jump in codesegment, insert a code section in the
  292. imporTSection to reduce the amount of .s files (PFV) }
  293. new_section(importsSection,sec_code,'',0);
  294. {$IfDef GDB}
  295. if (cs_debuginfo in aktmoduleswitches) then
  296. importsSection.concat(Tai_stab_function_name.Create(nil));
  297. {$EndIf GDB}
  298. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,AT_FUNCTION,0));
  299. importsSection.concat(Taicpu.Op_ref(A_JMP,S_NO,href));
  300. importsSection.concat(Tai_align.Create_op(4,$90));
  301. {$IfDef GDB}
  302. if (cs_debuginfo in aktmoduleswitches) and assigned(hp2.procdef) then
  303. begin
  304. mangledstring:=hp2.procdef.mangledname;
  305. hp2.procdef.setmangledname(hp2.func^);
  306. hp2.procdef.concatstabto(importssection);
  307. hp2.procdef.setmangledname(mangledstring);
  308. end;
  309. {$EndIf GDB}
  310. end;
  311. { create head link }
  312. new_section(importsSection,sec_idata7,'',0);
  313. importsSection.concat(Tai_const.Create_rva_sym(lhead));
  314. { fixup }
  315. objectlibrary.getlabel(tasmlabel(hp2.lab));
  316. new_section(importsSection,sec_idata4,'',0);
  317. importsSection.concat(Tai_const.Create_rva_sym(hp2.lab));
  318. { add jump field to imporTSection }
  319. new_section(importsSection,sec_idata5,'',0);
  320. if hp2.is_var then
  321. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,AT_FUNCTION,0))
  322. else
  323. importsSection.concat(Tai_label.Create(lcode));
  324. {$ifdef GDB}
  325. if (cs_debuginfo in aktmoduleswitches) then
  326. begin
  327. if assigned(hp2.name) then
  328. begin
  329. importname:='__imp_'+hp2.name^;
  330. suffix:=0;
  331. while assigned(objectlibrary.getasmsymbol(importname)) do
  332. begin
  333. inc(suffix);
  334. importname:='__imp_'+hp2.name^+'_'+tostr(suffix);
  335. end;
  336. importssection.concat(tai_symbol.createname(importname,AT_FUNCTION,4));
  337. end
  338. else
  339. begin
  340. importname:='__imp_by_ordinal'+tostr(hp2.ordnr);
  341. suffix:=0;
  342. while assigned(objectlibrary.getasmsymbol(importname)) do
  343. begin
  344. inc(suffix);
  345. importname:='__imp_by_ordinal'+tostr(hp2.ordnr)+'_'+tostr(suffix);
  346. end;
  347. importssection.concat(tai_symbol.createname(importname,AT_FUNCTION,4));
  348. end;
  349. end;
  350. {$endif GDB}
  351. if hp2.name^<>'' then
  352. importsSection.concat(Tai_const.Create_rva_sym(hp2.lab))
  353. else
  354. importsSection.concat(Tai_const.Create_32bit(longint($80000000) or longint(hp2.ordnr)));
  355. { finally the import information }
  356. new_section(importsSection,sec_idata6,'',0);
  357. importsSection.concat(Tai_label.Create(hp2.lab));
  358. importsSection.concat(Tai_const.Create_16bit(hp2.ordnr));
  359. importsSection.concat(Tai_string.Create(hp2.name^+#0));
  360. importsSection.concat(Tai_align.Create_op(2,0));
  361. hp2:=twin32imported_item(hp2.next);
  362. end;
  363. { write final section }
  364. importsSection.concat(Tai_cutobject.Create_end);
  365. { end of name references }
  366. new_section(importsSection,sec_idata4,'',0);
  367. importsSection.concat(Tai_const.Create_32bit(0));
  368. { end if addresses }
  369. new_section(importsSection,sec_idata5,'',0);
  370. importsSection.concat(Tai_const.Create_32bit(0));
  371. { dllname }
  372. new_section(importsSection,sec_idata7,'',0);
  373. importsSection.concat(Tai_label.Create(lname));
  374. importsSection.concat(Tai_string.Create(hp1.dllname^+#0));
  375. hp1:=timportlist(hp1.next);
  376. end;
  377. end;
  378. procedure timportlibwin32.generatelib;
  379. var
  380. hp1 : timportlist;
  381. hp2 : twin32imported_item;
  382. l1,l2,l3,l4 : tasmlabel;
  383. {$ifdef GDB}
  384. importname : string;
  385. mangledstring : string;
  386. suffix : integer;
  387. {$endif GDB}
  388. href : treference;
  389. begin
  390. if (aktoutputformat in [as_i386_masm,as_i386_tasm,as_i386_nasmwin32]) then
  391. begin
  392. generatenasmlib;
  393. exit;
  394. end;
  395. hp1:=timportlist(current_module.imports.first);
  396. while assigned(hp1) do
  397. begin
  398. { align codesegment for the jumps }
  399. new_section(importsSection,sec_code,'',sizeof(aint));
  400. { Get labels for the sections }
  401. objectlibrary.getlabel(l1);
  402. objectlibrary.getlabel(l2);
  403. objectlibrary.getlabel(l3);
  404. new_section(importsSection,sec_idata2,'',0);
  405. { pointer to procedure names }
  406. importsSection.concat(Tai_const.Create_rva_sym(l2));
  407. { two empty entries follow }
  408. importsSection.concat(Tai_const.Create_32bit(0));
  409. importsSection.concat(Tai_const.Create_32bit(0));
  410. { pointer to dll name }
  411. importsSection.concat(Tai_const.Create_rva_sym(l1));
  412. { pointer to fixups }
  413. importsSection.concat(Tai_const.Create_rva_sym(l3));
  414. { only create one section for each else it will
  415. create a lot of idata* }
  416. { first write the name references }
  417. new_section(importsSection,sec_idata4,'',0);
  418. importsSection.concat(Tai_label.Create(l2));
  419. hp2:=twin32imported_item(hp1.imported_items.first);
  420. while assigned(hp2) do
  421. begin
  422. objectlibrary.getlabel(tasmlabel(hp2.lab));
  423. if hp2.name^<>'' then
  424. importsSection.concat(Tai_const.Create_rva_sym(hp2.lab))
  425. else
  426. importsSection.concat(Tai_const.Create_32bit(longint($80000000) or hp2.ordnr));
  427. hp2:=twin32imported_item(hp2.next);
  428. end;
  429. { finalize the names ... }
  430. importsSection.concat(Tai_const.Create_32bit(0));
  431. { then the addresses and create also the indirect jump }
  432. new_section(importsSection,sec_idata5,'',0);
  433. importsSection.concat(Tai_label.Create(l3));
  434. hp2:=twin32imported_item(hp1.imported_items.first);
  435. while assigned(hp2) do
  436. begin
  437. if not hp2.is_var then
  438. begin
  439. objectlibrary.getlabel(l4);
  440. { create indirect jump }
  441. reference_reset_symbol(href,l4,0);
  442. { place jump in codesegment }
  443. new_section(importsSection,sec_code,'',0);
  444. {$IfDef GDB}
  445. if (cs_debuginfo in aktmoduleswitches) then
  446. importssection.concat(tai_stab_function_name.create(nil));
  447. {$EndIf GDB}
  448. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,AT_FUNCTION,0));
  449. importsSection.concat(Taicpu.Op_ref(A_JMP,S_NO,href));
  450. importsSection.concat(Tai_align.Create_op(4,$90));
  451. {$IfDef GDB}
  452. if (cs_debuginfo in aktmoduleswitches) and assigned(hp2.procdef) then
  453. begin
  454. mangledstring:=hp2.procdef.mangledname;
  455. hp2.procdef.setmangledname(hp2.func^);
  456. hp2.procdef.concatstabto(importssection);
  457. hp2.procdef.setmangledname(mangledstring);
  458. end;
  459. {$EndIf GDB}
  460. { add jump field to imporTSection }
  461. new_section(importsSection,sec_idata5,'',0);
  462. {$ifdef GDB}
  463. if (cs_debuginfo in aktmoduleswitches) then
  464. begin
  465. if assigned(hp2.name) then
  466. begin
  467. importname:='__imp_'+hp2.name^;
  468. suffix:=0;
  469. while assigned(objectlibrary.getasmsymbol(importname)) do
  470. begin
  471. inc(suffix);
  472. importname:='__imp_'+hp2.name^+'_'+tostr(suffix);
  473. end;
  474. importssection.concat(tai_symbol.createname(importname,AT_FUNCTION,4));
  475. end
  476. else
  477. begin
  478. importname:='__imp_by_ordinal'+tostr(hp2.ordnr);
  479. suffix:=0;
  480. while assigned(objectlibrary.getasmsymbol(importname)) do
  481. begin
  482. inc(suffix);
  483. importname:='__imp_by_ordinal'+tostr(hp2.ordnr)+'_'+tostr(suffix);
  484. end;
  485. importssection.concat(tai_symbol.createname(importname,AT_FUNCTION,4));
  486. end;
  487. end;
  488. {$endif GDB}
  489. importsSection.concat(Tai_label.Create(l4));
  490. end
  491. else
  492. begin
  493. importsSection.concat(Tai_symbol.Createname_global(hp2.func^,AT_DATA,0));
  494. end;
  495. importsSection.concat(Tai_const.Create_rva_sym(hp2.lab));
  496. hp2:=twin32imported_item(hp2.next);
  497. end;
  498. { finalize the addresses }
  499. importsSection.concat(Tai_const.Create_32bit(0));
  500. { finally the import information }
  501. new_section(importsSection,sec_idata6,'',0);
  502. hp2:=twin32imported_item(hp1.imported_items.first);
  503. while assigned(hp2) do
  504. begin
  505. importsSection.concat(Tai_label.Create(hp2.lab));
  506. { the ordinal number }
  507. importsSection.concat(Tai_const.Create_16bit(hp2.ordnr));
  508. importsSection.concat(Tai_string.Create(hp2.name^+#0));
  509. importsSection.concat(Tai_align.Create_op(2,0));
  510. hp2:=twin32imported_item(hp2.next);
  511. end;
  512. { create import dll name }
  513. new_section(importsSection,sec_idata7,'',0);
  514. importsSection.concat(Tai_label.Create(l1));
  515. importsSection.concat(Tai_string.Create(hp1.dllname^+#0));
  516. hp1:=timportlist(hp1.next);
  517. end;
  518. end;
  519. {*****************************************************************************
  520. TEXPORTLIBWIN32
  521. *****************************************************************************}
  522. procedure texportlibwin32.preparelib(const s:string);
  523. begin
  524. if not(assigned(exportssection)) then
  525. exportssection:=TAAsmoutput.create;
  526. EList_indexed:=tList.Create;
  527. EList_nonindexed:=tList.Create;
  528. objectlibrary.getdatalabel(edatalabel);
  529. end;
  530. procedure texportlibwin32.exportvar(hp : texported_item);
  531. begin
  532. { same code used !! PM }
  533. exportprocedure(hp);
  534. end;
  535. var
  536. Gl_DoubleIndex:boolean;
  537. Gl_DoubleIndexValue:longint;
  538. function IdxCompare(Item1, Item2: Pointer): Integer;
  539. var
  540. I1:texported_item absolute Item1;
  541. I2:texported_item absolute Item2;
  542. begin
  543. Result:=I1.index-I2.index;
  544. if(Result=0)and(Item1<>Item2)then
  545. begin
  546. Gl_DoubleIndex:=true;
  547. Gl_DoubleIndexValue:=I1.index;
  548. end;
  549. end;
  550. procedure texportlibwin32.exportprocedure(hp : texported_item);
  551. begin
  552. if ((hp.options and eo_index)<>0)and((hp.index<=0) or (hp.index>$ffff)) then
  553. begin
  554. message1(parser_e_export_invalid_index,tostr(hp.index));
  555. exit;
  556. end;
  557. if hp.options and eo_index=eo_index then
  558. EList_indexed.Add(hp)
  559. else
  560. EList_nonindexed.Add(hp);
  561. end;
  562. procedure texportlibwin32.exportfromlist(hp : texported_item);
  563. //formerly texportlibwin32.exportprocedure
  564. { must be ordered at least for win32 !! }
  565. var
  566. hp2 : texported_item;
  567. begin
  568. hp2:=texported_item(current_module._exports.first);
  569. while assigned(hp2) and
  570. (hp.name^>hp2.name^) do
  571. hp2:=texported_item(hp2.next);
  572. { insert hp there !! }
  573. if hp2=nil then
  574. current_module._exports.concat(hp)
  575. else
  576. begin
  577. if hp2.name^=hp.name^ then
  578. begin
  579. { this is not allowed !! }
  580. message1(parser_e_export_name_double,hp.name^);
  581. exit;
  582. end;
  583. current_module._exports.insertbefore(hp,hp2);
  584. end;
  585. end;
  586. procedure texportlibwin32.generatelib;
  587. var
  588. ordinal_base,ordinal_max,ordinal_min : longint;
  589. current_index : longint;
  590. entries,named_entries : longint;
  591. name_label,dll_name_label,export_address_table : tasmlabel;
  592. export_name_table_pointers,export_ordinal_table : tasmlabel;
  593. hp,hp2 : texported_item;
  594. temtexport : TLinkedList;
  595. address_table,name_table_pointers,
  596. name_table,ordinal_table : TAAsmoutput;
  597. i,autoindex,ni_high : longint;
  598. hole : boolean;
  599. begin
  600. Gl_DoubleIndex:=false;
  601. ELIst_indexed.Sort(@IdxCompare);
  602. if Gl_DoubleIndex then
  603. begin
  604. message1(parser_e_export_ordinal_double,tostr(Gl_DoubleIndexValue));
  605. EList_indexed.Free;
  606. EList_nonindexed.Free;
  607. exit;
  608. end;
  609. autoindex:=1;
  610. while EList_nonindexed.Count>0 do
  611. begin
  612. hole:=(EList_indexed.Count>0)and(texported_item(EList_indexed.Items[0]).index>1);
  613. if not hole then
  614. for i:=autoindex to pred(EList_indexed.Count)do
  615. if texported_item(EList_indexed.Items[i]).index-texported_item(EList_indexed.Items[pred(i)]).index>1 then
  616. begin
  617. autoindex:=succ(texported_item(EList_indexed.Items[pred(i)]).index);
  618. hole:=true;
  619. break;
  620. end;
  621. ni_high:=pred(EList_nonindexed.Count);
  622. if not hole then
  623. begin
  624. autoindex:=succ(EList_indexed.Count);
  625. EList_indexed.Add(EList_nonindexed.Items[ni_high]);
  626. end
  627. else
  628. EList_indexed.Insert(pred(AutoIndex),EList_nonindexed.Items[ni_high]);
  629. EList_nonindexed.Delete(ni_high);
  630. texported_item(EList_indexed.Items[pred(AutoIndex)]).index:=autoindex;
  631. end;
  632. EList_nonindexed.Free;
  633. for i:=0 to pred(EList_indexed.Count)do
  634. exportfromlist(texported_item(EList_indexed.Items[i]));
  635. EList_indexed.Free;
  636. if (aktoutputformat in [as_i386_masm,as_i386_tasm,as_i386_nasmwin32]) then
  637. begin
  638. generatenasmlib;
  639. exit;
  640. end;
  641. hp:=texported_item(current_module._exports.first);
  642. if not assigned(hp) then
  643. exit;
  644. ordinal_max:=0;
  645. ordinal_min:=$7FFFFFFF;
  646. entries:=0;
  647. named_entries:=0;
  648. objectlibrary.getlabel(dll_name_label);
  649. objectlibrary.getlabel(export_address_table);
  650. objectlibrary.getlabel(export_name_table_pointers);
  651. objectlibrary.getlabel(export_ordinal_table);
  652. { count entries }
  653. while assigned(hp) do
  654. begin
  655. inc(entries);
  656. if (hp.index>ordinal_max) then
  657. ordinal_max:=hp.index;
  658. if (hp.index>0) and (hp.index<ordinal_min) then
  659. ordinal_min:=hp.index;
  660. if assigned(hp.name) then
  661. inc(named_entries);
  662. hp:=texported_item(hp.next);
  663. end;
  664. { no support for higher ordinal base yet !! }
  665. ordinal_base:=1;
  666. current_index:=ordinal_base;
  667. { we must also count the holes !! }
  668. entries:=ordinal_max-ordinal_base+1;
  669. new_section(exportsSection,sec_edata,'',0);
  670. { create label to reference from main so smartlink will include
  671. the .edata section }
  672. exportsSection.concat(Tai_symbol.Create_global(edatalabel,0));
  673. { export flags }
  674. exportsSection.concat(Tai_const.Create_32bit(0));
  675. { date/time stamp }
  676. exportsSection.concat(Tai_const.Create_32bit(0));
  677. { major version }
  678. exportsSection.concat(Tai_const.Create_16bit(0));
  679. { minor version }
  680. exportsSection.concat(Tai_const.Create_16bit(0));
  681. { pointer to dll name }
  682. exportsSection.concat(Tai_const.Create_rva_sym(dll_name_label));
  683. { ordinal base normally set to 1 }
  684. exportsSection.concat(Tai_const.Create_32bit(ordinal_base));
  685. { number of entries }
  686. exportsSection.concat(Tai_const.Create_32bit(entries));
  687. { number of named entries }
  688. exportsSection.concat(Tai_const.Create_32bit(named_entries));
  689. { address of export address table }
  690. exportsSection.concat(Tai_const.Create_rva_sym(export_address_table));
  691. { address of name pointer pointers }
  692. exportsSection.concat(Tai_const.Create_rva_sym(export_name_table_pointers));
  693. { address of ordinal number pointers }
  694. exportsSection.concat(Tai_const.Create_rva_sym(export_ordinal_table));
  695. { the name }
  696. exportsSection.concat(Tai_label.Create(dll_name_label));
  697. if st='' then
  698. exportsSection.concat(Tai_string.Create(current_module.modulename^+target_info.sharedlibext+#0))
  699. else
  700. exportsSection.concat(Tai_string.Create(st+target_info.sharedlibext+#0));
  701. { export address table }
  702. address_table:=TAAsmoutput.create;
  703. address_table.concat(Tai_align.Create_op(4,0));
  704. address_table.concat(Tai_label.Create(export_address_table));
  705. name_table_pointers:=TAAsmoutput.create;
  706. name_table_pointers.concat(Tai_align.Create_op(4,0));
  707. name_table_pointers.concat(Tai_label.Create(export_name_table_pointers));
  708. ordinal_table:=TAAsmoutput.create;
  709. ordinal_table.concat(Tai_align.Create_op(4,0));
  710. ordinal_table.concat(Tai_label.Create(export_ordinal_table));
  711. name_table:=TAAsmoutput.Create;
  712. name_table.concat(Tai_align.Create_op(4,0));
  713. { write each address }
  714. hp:=texported_item(current_module._exports.first);
  715. while assigned(hp) do
  716. begin
  717. if (hp.options and eo_name)<>0 then
  718. begin
  719. objectlibrary.getlabel(name_label);
  720. name_table_pointers.concat(Tai_const.Create_rva_sym(name_label));
  721. ordinal_table.concat(Tai_const.Create_16bit(hp.index-ordinal_base));
  722. name_table.concat(Tai_align.Create_op(2,0));
  723. name_table.concat(Tai_label.Create(name_label));
  724. name_table.concat(Tai_string.Create(hp.name^+#0));
  725. end;
  726. hp:=texported_item(hp.next);
  727. end;
  728. { order in increasing ordinal values }
  729. { into temtexport list }
  730. temtexport:=TLinkedList.Create;
  731. hp:=texported_item(current_module._exports.first);
  732. while assigned(hp) do
  733. begin
  734. current_module._exports.remove(hp);
  735. hp2:=texported_item(temtexport.first);
  736. while assigned(hp2) and (hp.index>hp2.index) do
  737. hp2:=texported_item(hp2.next);
  738. if hp2=nil then
  739. temtexport.concat(hp)
  740. else
  741. temtexport.insertbefore(hp,hp2);
  742. hp:=texported_item(current_module._exports.first);;
  743. end;
  744. { write the export adress table }
  745. current_index:=ordinal_base;
  746. hp:=texported_item(temtexport.first);
  747. while assigned(hp) do
  748. begin
  749. { fill missing values }
  750. while current_index<hp.index do
  751. begin
  752. address_table.concat(Tai_const.Create_32bit(0));
  753. inc(current_index);
  754. end;
  755. case hp.sym.typ of
  756. varsym :
  757. address_table.concat(Tai_const.Createname_rva(tvarsym(hp.sym).mangledname));
  758. typedconstsym :
  759. address_table.concat(Tai_const.Createname_rva(ttypedconstsym(hp.sym).mangledname));
  760. procsym :
  761. address_table.concat(Tai_const.Createname_rva(tprocsym(hp.sym).first_procdef.mangledname));
  762. end;
  763. inc(current_index);
  764. hp:=texported_item(hp.next);
  765. end;
  766. exportsSection.concatlist(address_table);
  767. exportsSection.concatlist(name_table_pointers);
  768. exportsSection.concatlist(ordinal_table);
  769. exportsSection.concatlist(name_table);
  770. address_table.Free;
  771. name_table_pointers.free;
  772. ordinal_table.free;
  773. name_table.free;
  774. temtexport.free;
  775. end;
  776. procedure texportlibwin32.generatenasmlib;
  777. var
  778. hp : texported_item;
  779. p : pchar;
  780. s : string;
  781. begin
  782. new_section(exportssection,sec_code,'',0);
  783. hp:=texported_item(current_module._exports.first);
  784. while assigned(hp) do
  785. begin
  786. case hp.sym.typ of
  787. varsym :
  788. s:=tvarsym(hp.sym).mangledname;
  789. typedconstsym :
  790. s:=ttypedconstsym(hp.sym).mangledname;
  791. procsym :
  792. s:=tprocsym(hp.sym).first_procdef.mangledname;
  793. else
  794. s:='';
  795. end;
  796. p:=strpnew(#9+'export '+s+' '+hp.name^+' '+tostr(hp.index));
  797. exportssection.concat(tai_direct.create(p));
  798. hp:=texported_item(hp.next);
  799. end;
  800. end;
  801. {****************************************************************************
  802. TLINKERWIN32
  803. ****************************************************************************}
  804. Constructor TLinkerWin32.Create;
  805. begin
  806. Inherited Create;
  807. { allow duplicated libs (PM) }
  808. SharedLibFiles.doubles:=true;
  809. StaticLibFiles.doubles:=true;
  810. end;
  811. Procedure TLinkerWin32.SetDefaultInfo;
  812. begin
  813. with Info do
  814. begin
  815. ExeCmd[1]:='ld $OPT $STRIP $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  816. DllCmd[1]:='ld $OPT $STRIP --dll $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  817. { ExeCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF';
  818. use short forms to avoid 128 char limitation problem }
  819. ExeCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  820. ExeCmd[3]:='ld $OPT $STRIP $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  821. { DllCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF'; }
  822. DllCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  823. DllCmd[3]:='ld $OPT $STRIP --dll $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  824. end;
  825. end;
  826. Function TLinkerWin32.WriteResponseFile(isdll:boolean) : Boolean;
  827. Var
  828. linkres : TLinkRes;
  829. HPath : TStringListItem;
  830. s,s2 : string;
  831. i : integer;
  832. linklibc : boolean;
  833. begin
  834. WriteResponseFile:=False;
  835. linklibc:=false;
  836. { Open link.res file }
  837. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  838. { Write path to search libraries }
  839. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  840. while assigned(HPath) do
  841. begin
  842. LinkRes.Add('SEARCH_DIR('+MaybeQuoted(HPath.Str)+')');
  843. HPath:=TStringListItem(HPath.Next);
  844. end;
  845. HPath:=TStringListItem(LibrarySearchPath.First);
  846. while assigned(HPath) do
  847. begin
  848. LinkRes.Add('SEARCH_DIR('+MaybeQuoted(HPath.Str)+')');
  849. HPath:=TStringListItem(HPath.Next);
  850. end;
  851. { add objectfiles, start with prt0 always }
  852. { profiling of shared libraries is currently not supported }
  853. LinkRes.Add('INPUT(');
  854. if isdll then
  855. LinkRes.AddFileName(MaybeQuoted(FindObjectFile('wdllprt0','',false)))
  856. else
  857. if (cs_profile in aktmoduleswitches) then
  858. LinkRes.AddFileName(MaybeQuoted(FindObjectFile('gprt0','',false)))
  859. else
  860. LinkRes.AddFileName(MaybeQuoted(FindObjectFile('wprt0','',false)));
  861. while not ObjectFiles.Empty do
  862. begin
  863. s:=ObjectFiles.GetFirst;
  864. if s<>'' then
  865. LinkRes.AddFileName(MaybeQuoted(s));
  866. end;
  867. LinkRes.Add(')');
  868. { Write staticlibraries }
  869. if (not StaticLibFiles.Empty) or (cs_profile in aktmoduleswitches) then
  870. begin
  871. LinkRes.Add('GROUP(');
  872. if (cs_profile in aktmoduleswitches) then
  873. begin
  874. LinkRes.Add('-lgcc');
  875. LinkRes.Add('-lmoldname');
  876. LinkRes.Add('-lmsvcrt');
  877. LinkRes.Add('-lgmon');
  878. LinkRes.Add('-lkernel32');
  879. end;
  880. While not StaticLibFiles.Empty do
  881. begin
  882. S:=StaticLibFiles.GetFirst;
  883. LinkRes.AddFileName(MaybeQuoted(s));
  884. end;
  885. LinkRes.Add(')');
  886. end;
  887. { Write sharedlibraries }
  888. if not SharedLibFiles.Empty then
  889. begin
  890. LinkRes.Add('INPUT(') ;
  891. While not SharedLibFiles.Empty do
  892. begin
  893. S:=SharedLibFiles.GetFirst;
  894. if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  895. begin
  896. LinkRes.Add(MaybeQuoted(s2));
  897. continue;
  898. end;
  899. if pos(target_info.sharedlibprefix,s)=1 then
  900. s:=copy(s,length(target_info.sharedlibprefix)+1,255);
  901. if s<>'c' then
  902. begin
  903. i:=Pos(target_info.sharedlibext,S);
  904. if i>0 then
  905. Delete(S,i,255);
  906. LinkRes.Add('-l'+s);
  907. end
  908. else
  909. begin
  910. LinkRes.Add('-l'+s);
  911. linklibc:=true;
  912. end;
  913. end;
  914. { be sure that libc is the last lib }
  915. if linklibc then
  916. LinkRes.Add('-lc');
  917. LinkRes.Add(')');
  918. end;
  919. { Write and Close response }
  920. linkres.writetodisk;
  921. LinkRes.Free;
  922. WriteResponseFile:=True;
  923. end;
  924. function TLinkerWin32.MakeExecutable:boolean;
  925. var
  926. binstr : String;
  927. cmdstr : TCmdStr;
  928. success : boolean;
  929. cmds,i : longint;
  930. AsBinStr : string[80];
  931. StripStr,
  932. RelocStr,
  933. AppTypeStr,
  934. ImageBaseStr : string[40];
  935. begin
  936. if not(cs_link_extern in aktglobalswitches) then
  937. Message1(exec_i_linking,current_module.exefilename^);
  938. { Create some replacements }
  939. RelocStr:='';
  940. AppTypeStr:='';
  941. ImageBaseStr:='';
  942. StripStr:='';
  943. AsBinStr:=FindUtil(utilsprefix+'as');
  944. if RelocSection then
  945. RelocStr:='--base-file base.$$$';
  946. if apptype=app_gui then
  947. AppTypeStr:='--subsystem windows';
  948. if assigned(DLLImageBase) then
  949. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  950. if (cs_link_strip in aktglobalswitches) then
  951. StripStr:='-s';
  952. { Write used files and libraries }
  953. WriteResponseFile(false);
  954. { Call linker }
  955. success:=false;
  956. if RelocSection or (not Deffile.empty) then
  957. cmds:=3
  958. else
  959. cmds:=1;
  960. for i:=1 to cmds do
  961. begin
  962. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  963. if binstr<>'' then
  964. begin
  965. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  966. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  967. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  968. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  969. Replace(cmdstr,'$ASBIN',AsbinStr);
  970. Replace(cmdstr,'$RELOC',RelocStr);
  971. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  972. Replace(cmdstr,'$STRIP',StripStr);
  973. if not DefFile.Empty then
  974. begin
  975. DefFile.WriteFile;
  976. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  977. end
  978. else
  979. Replace(cmdstr,'$DEF','');
  980. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  981. if not success then
  982. break;
  983. end;
  984. end;
  985. { Post process }
  986. if success then
  987. success:=PostProcessExecutable(current_module.exefilename^,false);
  988. { Remove ReponseFile }
  989. if (success) and not(cs_link_extern in aktglobalswitches) then
  990. begin
  991. RemoveFile(outputexedir+Info.ResName);
  992. RemoveFile('base.$$$');
  993. RemoveFile('exp.$$$');
  994. RemoveFile('deffile.$$$');
  995. end;
  996. MakeExecutable:=success; { otherwise a recursive call to link method }
  997. end;
  998. Function TLinkerWin32.MakeSharedLibrary:boolean;
  999. var
  1000. binstr : String;
  1001. cmdstr : TCmdStr;
  1002. success : boolean;
  1003. cmds,
  1004. i : longint;
  1005. AsBinStr : string[80];
  1006. StripStr,
  1007. RelocStr,
  1008. AppTypeStr,
  1009. ImageBaseStr : string[40];
  1010. begin
  1011. MakeSharedLibrary:=false;
  1012. if not(cs_link_extern in aktglobalswitches) then
  1013. Message1(exec_i_linking,current_module.sharedlibfilename^);
  1014. { Create some replacements }
  1015. RelocStr:='';
  1016. AppTypeStr:='';
  1017. ImageBaseStr:='';
  1018. StripStr:='';
  1019. AsBinStr:=FindUtil(utilsprefix+'as');
  1020. if RelocSection then
  1021. RelocStr:='--base-file base.$$$';
  1022. if apptype=app_gui then
  1023. AppTypeStr:='--subsystem windows';
  1024. if assigned(DLLImageBase) then
  1025. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  1026. if (cs_link_strip in aktglobalswitches) then
  1027. StripStr:='-s';
  1028. { Write used files and libraries }
  1029. WriteResponseFile(true);
  1030. { Call linker }
  1031. success:=false;
  1032. if RelocSection or (not Deffile.empty) then
  1033. cmds:=3
  1034. else
  1035. cmds:=1;
  1036. for i:=1 to cmds do
  1037. begin
  1038. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  1039. if binstr<>'' then
  1040. begin
  1041. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1042. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1043. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1044. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  1045. Replace(cmdstr,'$ASBIN',AsbinStr);
  1046. Replace(cmdstr,'$RELOC',RelocStr);
  1047. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  1048. Replace(cmdstr,'$STRIP',StripStr);
  1049. if not DefFile.Empty then
  1050. begin
  1051. DefFile.WriteFile;
  1052. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  1053. end
  1054. else
  1055. Replace(cmdstr,'$DEF','');
  1056. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  1057. if not success then
  1058. break;
  1059. end;
  1060. end;
  1061. { Post process }
  1062. if success then
  1063. success:=PostProcessExecutable(current_module.sharedlibfilename^,true);
  1064. { Remove ReponseFile }
  1065. if (success) and not(cs_link_extern in aktglobalswitches) then
  1066. begin
  1067. RemoveFile(outputexedir+Info.ResName);
  1068. RemoveFile('base.$$$');
  1069. RemoveFile('exp.$$$');
  1070. RemoveFile('deffile.$$$');
  1071. end;
  1072. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1073. end;
  1074. function tlinkerwin32.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1075. type
  1076. tdosheader = packed record
  1077. e_magic : word;
  1078. e_cblp : word;
  1079. e_cp : word;
  1080. e_crlc : word;
  1081. e_cparhdr : word;
  1082. e_minalloc : word;
  1083. e_maxalloc : word;
  1084. e_ss : word;
  1085. e_sp : word;
  1086. e_csum : word;
  1087. e_ip : word;
  1088. e_cs : word;
  1089. e_lfarlc : word;
  1090. e_ovno : word;
  1091. e_res : array[0..3] of word;
  1092. e_oemid : word;
  1093. e_oeminfo : word;
  1094. e_res2 : array[0..9] of word;
  1095. e_lfanew : longint;
  1096. end;
  1097. tpeheader = packed record
  1098. PEMagic : array[0..3] of char;
  1099. Machine : word;
  1100. NumberOfSections : word;
  1101. TimeDateStamp : longint;
  1102. PointerToSymbolTable : longint;
  1103. NumberOfSymbols : longint;
  1104. SizeOfOptionalHeader : word;
  1105. Characteristics : word;
  1106. Magic : word;
  1107. MajorLinkerVersion : byte;
  1108. MinorLinkerVersion : byte;
  1109. SizeOfCode : longint;
  1110. SizeOfInitializedData : longint;
  1111. SizeOfUninitializedData : longint;
  1112. AddressOfEntryPoint : longint;
  1113. BaseOfCode : longint;
  1114. BaseOfData : longint;
  1115. ImageBase : longint;
  1116. SectionAlignment : longint;
  1117. FileAlignment : longint;
  1118. MajorOperatingSystemVersion : word;
  1119. MinorOperatingSystemVersion : word;
  1120. MajorImageVersion : word;
  1121. MinorImageVersion : word;
  1122. MajorSubsystemVersion : word;
  1123. MinorSubsystemVersion : word;
  1124. Reserved1 : longint;
  1125. SizeOfImage : longint;
  1126. SizeOfHeaders : longint;
  1127. CheckSum : longint;
  1128. Subsystem : word;
  1129. DllCharacteristics : word;
  1130. SizeOfStackReserve : longint;
  1131. SizeOfStackCommit : longint;
  1132. SizeOfHeapReserve : longint;
  1133. SizeOfHeapCommit : longint;
  1134. LoaderFlags : longint;
  1135. NumberOfRvaAndSizes : longint;
  1136. DataDirectory : array[1..$80] of byte;
  1137. end;
  1138. tcoffsechdr=packed record
  1139. name : array[0..7] of char;
  1140. vsize : longint;
  1141. rvaofs : longint;
  1142. datalen : longint;
  1143. datapos : longint;
  1144. relocpos : longint;
  1145. lineno1 : longint;
  1146. nrelocs : word;
  1147. lineno2 : word;
  1148. flags : longint;
  1149. end;
  1150. psecfill=^TSecfill;
  1151. TSecfill=record
  1152. fillpos,
  1153. fillsize : longint;
  1154. next : psecfill;
  1155. end;
  1156. var
  1157. f : file;
  1158. cmdstr : string;
  1159. dosheader : tdosheader;
  1160. peheader : tpeheader;
  1161. firstsecpos,
  1162. maxfillsize,
  1163. l,peheaderpos : longint;
  1164. coffsec : tcoffsechdr;
  1165. secroot,hsecroot : psecfill;
  1166. zerobuf : pointer;
  1167. begin
  1168. postprocessexecutable:=false;
  1169. { when -s is used or it's a dll then quit }
  1170. if (cs_link_extern in aktglobalswitches) then
  1171. begin
  1172. case apptype of
  1173. app_gui :
  1174. cmdstr:='--subsystem gui';
  1175. app_cui :
  1176. cmdstr:='--subsystem console';
  1177. end;
  1178. if dllversion<>'' then
  1179. cmdstr:=cmdstr+' --version '+dllversion;
  1180. cmdstr:=cmdstr+' --input '+maybequoted(fn);
  1181. cmdstr:=cmdstr+' --stack '+tostr(stacksize);
  1182. DoExec(FindUtil(utilsprefix+'postw32'),cmdstr,false,false);
  1183. postprocessexecutable:=true;
  1184. exit;
  1185. end;
  1186. { open file }
  1187. assign(f,fn);
  1188. {$I-}
  1189. reset(f,1);
  1190. if ioresult<>0 then
  1191. Message1(execinfo_f_cant_open_executable,fn);
  1192. { read headers }
  1193. blockread(f,dosheader,sizeof(tdosheader));
  1194. peheaderpos:=dosheader.e_lfanew;
  1195. seek(f,peheaderpos);
  1196. blockread(f,peheader,sizeof(tpeheader));
  1197. { write info }
  1198. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  1199. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  1200. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  1201. { change stack size (PM) }
  1202. { I am not sure that the default value is adequate !! }
  1203. peheader.SizeOfStackReserve:=stacksize;
  1204. { change the header }
  1205. { sub system }
  1206. { gui=2 }
  1207. { cui=3 }
  1208. case apptype of
  1209. app_gui :
  1210. peheader.Subsystem:=2;
  1211. app_cui :
  1212. peheader.Subsystem:=3;
  1213. end;
  1214. if dllversion<>'' then
  1215. begin
  1216. peheader.MajorImageVersion:=dllmajor;
  1217. peheader.MinorImageVersion:=dllminor;
  1218. end;
  1219. { reset timestamp }
  1220. peheader.TimeDateStamp:=0;
  1221. { write header back }
  1222. seek(f,peheaderpos);
  1223. blockwrite(f,peheader,sizeof(tpeheader));
  1224. if ioresult<>0 then
  1225. Message1(execinfo_f_cant_process_executable,fn);
  1226. seek(f,peheaderpos);
  1227. blockread(f,peheader,sizeof(tpeheader));
  1228. { write the value after the change }
  1229. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  1230. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  1231. { read section info }
  1232. maxfillsize:=0;
  1233. firstsecpos:=0;
  1234. secroot:=nil;
  1235. for l:=1 to peheader.NumberOfSections do
  1236. begin
  1237. blockread(f,coffsec,sizeof(tcoffsechdr));
  1238. if coffsec.datapos>0 then
  1239. begin
  1240. if secroot=nil then
  1241. firstsecpos:=coffsec.datapos;
  1242. new(hsecroot);
  1243. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  1244. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  1245. hsecroot^.next:=secroot;
  1246. secroot:=hsecroot;
  1247. if secroot^.fillsize>maxfillsize then
  1248. maxfillsize:=secroot^.fillsize;
  1249. end;
  1250. end;
  1251. if firstsecpos>0 then
  1252. begin
  1253. l:=firstsecpos-filepos(f);
  1254. if l>maxfillsize then
  1255. maxfillsize:=l;
  1256. end
  1257. else
  1258. l:=0;
  1259. { get zero buffer }
  1260. getmem(zerobuf,maxfillsize);
  1261. fillchar(zerobuf^,maxfillsize,0);
  1262. { zero from sectioninfo until first section }
  1263. blockwrite(f,zerobuf^,l);
  1264. { zero section alignments }
  1265. while assigned(secroot) do
  1266. begin
  1267. seek(f,secroot^.fillpos);
  1268. blockwrite(f,zerobuf^,secroot^.fillsize);
  1269. hsecroot:=secroot;
  1270. secroot:=secroot^.next;
  1271. dispose(hsecroot);
  1272. end;
  1273. freemem(zerobuf,maxfillsize);
  1274. close(f);
  1275. {$I+}
  1276. if ioresult<>0 then;
  1277. postprocessexecutable:=true;
  1278. end;
  1279. {****************************************************************************
  1280. TDLLScannerWin32
  1281. ****************************************************************************}
  1282. procedure tDLLScannerWin32.GetDefExt(var N:longint;var P:pStr4);
  1283. begin
  1284. N:=sizeof(DefaultDLLExtensions)div sizeof(DefaultDLLExtensions[1]);
  1285. pointer(P):=@DefaultDLLExtensions;
  1286. end;
  1287. function tDLLScannerWin32.DOSstubOK(var x:cardinal):boolean;
  1288. begin
  1289. blockread(f,TheWord,2,loaded);
  1290. if loaded<>2 then
  1291. DOSstubOK:=false
  1292. else
  1293. begin
  1294. DOSstubOK:=(TheWord='MZ');
  1295. seek(f,$3C);
  1296. blockread(f,x,4,loaded);
  1297. if(loaded<>4)or(longint(x)>filesize(f))then
  1298. DOSstubOK:=false;
  1299. end;
  1300. end;
  1301. function TDLLScannerWin32.FindDLL(const s:string;var founddll:string):boolean;
  1302. var
  1303. sysdir : string;
  1304. Found : boolean;
  1305. begin
  1306. Found:=false;
  1307. { Look for DLL in:
  1308. 1. Current dir
  1309. 2. Library Path
  1310. 3. windir,windir/system,windir/system32 }
  1311. Found:=FindFile(s,'.'+source_info.DirSep,founddll);
  1312. if (not found) then
  1313. Found:=librarysearchpath.FindFile(s,founddll);
  1314. if (not found) then
  1315. begin
  1316. sysdir:=FixPath(GetEnv('windir'),false);
  1317. Found:=FindFile(s,sysdir+';'+sysdir+'system'+source_info.DirSep+';'+sysdir+'system32'+source_info.DirSep,founddll);
  1318. end;
  1319. if (not found) then
  1320. begin
  1321. message1(exec_w_libfile_not_found,s);
  1322. FoundDll:=s;
  1323. end;
  1324. FindDll:=Found;
  1325. end;
  1326. function tDLLScannerWin32.ExtractDllName(Const Name : string) : string;
  1327. var n : string;
  1328. begin
  1329. n:=Upper(SplitExtension(Name));
  1330. if (n='.DLL') or (n='.DRV') or (n='.EXE') then
  1331. ExtractDllName:=Name
  1332. else
  1333. ExtractDllName:=Name+target_info.sharedlibext;
  1334. end;
  1335. function tDLLScannerWin32.isSuitableFileType(x:cardinal):longbool;
  1336. begin
  1337. seek(f,x);
  1338. blockread(f,TheWord,2,loaded);
  1339. isSuitableFileType:=(loaded=2)and(TheWord='PE');
  1340. end;
  1341. function tDLLScannerWin32.GetEdata(HeaderEntry:cardinal):longbool;
  1342. type
  1343. TObjInfo=packed record
  1344. ObjName:array[0..7]of char;
  1345. VirtSize,
  1346. VirtAddr,
  1347. RawSize,
  1348. RawOffset,
  1349. Reloc,
  1350. LineNum:cardinal;
  1351. RelCount,
  1352. LineCount:word;
  1353. flags:cardinal;
  1354. end;
  1355. var
  1356. i:cardinal;
  1357. ObjOfs:cardinal;
  1358. Obj:TObjInfo;
  1359. APE_obj,APE_Optsize:word;
  1360. ExportRVA:cardinal;
  1361. delta:cardinal;
  1362. const
  1363. IMAGE_SCN_CNT_CODE=$00000020;
  1364. var
  1365. _d:dirstr;
  1366. _n:namestr;
  1367. _e:extstr;
  1368. function isUsedFunction(name:pchar):longbool;
  1369. var
  1370. hp:tExternalsItem;
  1371. begin
  1372. isUsedFunction:=false;
  1373. hp:=tExternalsItem(current_module.Externals.first);
  1374. while assigned(hp)do
  1375. begin
  1376. if(assigned(hp.data))and(not hp.found)then
  1377. if hp.data^=StrPas(name)then
  1378. begin
  1379. isUsedFunction:=true;
  1380. hp.found:=true;
  1381. exit;
  1382. end;
  1383. hp:=tExternalsItem(hp.next);
  1384. end;
  1385. end;
  1386. procedure Store(index:cardinal;name:pchar;isData:longbool);
  1387. begin
  1388. if not isUsedFunction(name)then
  1389. exit;
  1390. if not(current_module.uses_imports) then
  1391. begin
  1392. current_module.uses_imports:=true;
  1393. importlib.preparelib(current_module.modulename^);
  1394. end;
  1395. if IsData then
  1396. timportlibwin32(importlib).importvariable_str(name,_n,name)
  1397. else
  1398. timportlibwin32(importlib).importprocedure_str(name,_n,index,name);
  1399. end;
  1400. procedure ProcessEdata;
  1401. type
  1402. a8=array[0..7]of char;
  1403. function GetSectionName(rva:cardinal;var Flags:cardinal):a8;
  1404. var
  1405. i:cardinal;
  1406. LocObjOfs:cardinal;
  1407. LocObj:TObjInfo;
  1408. begin
  1409. GetSectionName:='';
  1410. Flags:=0;
  1411. LocObjOfs:=APE_OptSize+HeaderOffset+24;
  1412. for i:=1 to APE_obj do
  1413. begin
  1414. seek(f,LocObjOfs);
  1415. blockread(f,LocObj,sizeof(LocObj));
  1416. if(rva>=LocObj.VirtAddr)and(rva<=LocObj.VirtAddr+LocObj.RawSize)then
  1417. begin
  1418. GetSectionName:=a8(LocObj.ObjName);
  1419. Flags:=LocObj.flags;
  1420. end;
  1421. end;
  1422. end;
  1423. var
  1424. j,Fl:cardinal;
  1425. ulongval,procEntry:cardinal;
  1426. Ordinal:word;
  1427. isData:longbool;
  1428. ExpDir:packed record
  1429. flag,
  1430. stamp:cardinal;
  1431. Major,
  1432. Minor:word;
  1433. Name,
  1434. Base,
  1435. NumFuncs,
  1436. NumNames,
  1437. AddrFuncs,
  1438. AddrNames,
  1439. AddrOrds:cardinal;
  1440. end;
  1441. begin
  1442. with Obj do
  1443. begin
  1444. seek(f,RawOffset+delta);
  1445. blockread(f,ExpDir,sizeof(ExpDir));
  1446. fsplit(impname,_d,_n,_e);
  1447. for j:=0 to pred(ExpDir.NumNames)do
  1448. begin
  1449. { Don't know why but this gives serious problems with overflow checking on }
  1450. {$IFOPT Q+}
  1451. {$DEFINE OVERFLOW_CHECK_WAS_ON}
  1452. {$ENDIF}
  1453. {$Q-}
  1454. seek(f,RawOffset-VirtAddr+ExpDir.AddrOrds+j*2);
  1455. blockread(f,Ordinal,2);
  1456. seek(f,RawOffset-VirtAddr+ExpDir.AddrFuncs+cardinal(Ordinal)*4);
  1457. blockread(f,ProcEntry,4);
  1458. seek(f,RawOffset-VirtAddr+ExpDir.AddrNames+j*4);
  1459. blockread(f,ulongval,4);
  1460. seek(f,RawOffset-VirtAddr+ulongval);
  1461. blockread(f,cstring,sizeof(cstring));
  1462. isData:=GetSectionName(procentry,Fl)='';
  1463. {$IFDEF OVERFLOW_CHECK_WAS_ON}
  1464. {$Q+}
  1465. {$ENDIF}
  1466. if not isData then
  1467. isData:=Fl and IMAGE_SCN_CNT_CODE<>IMAGE_SCN_CNT_CODE;
  1468. Store(succ(Ordinal),cstring,isData);
  1469. end;
  1470. end;
  1471. end;
  1472. begin
  1473. GetEdata:=false;
  1474. seek(f,HeaderEntry+120);
  1475. blockread(f,ExportRVA,4);
  1476. seek(f,HeaderEntry+6);
  1477. blockread(f,APE_Obj,2);
  1478. seek(f,HeaderEntry+20);
  1479. blockread(f,APE_OptSize,2);
  1480. ObjOfs:=APE_OptSize+HeaderOffset+24;
  1481. for i:=1 to APE_obj do
  1482. begin
  1483. seek(f,ObjOfs);
  1484. blockread(f,Obj,sizeof(Obj));
  1485. inc(ObjOfs,sizeof(Obj));
  1486. with Obj do
  1487. if(VirtAddr<=ExportRva)and(ExportRva<VirtAddr+VirtSize)then
  1488. begin
  1489. delta:=ExportRva-VirtAddr;
  1490. ProcessEdata;
  1491. GetEdata:=true;
  1492. end;
  1493. end;
  1494. end;
  1495. function tDLLScannerWin32.scan(const binname:string):longbool;
  1496. var
  1497. OldFileMode:longint;
  1498. foundimp : string;
  1499. NN:longint;PP:pStr4;
  1500. begin
  1501. Scan:=false;
  1502. { is there already an import library the we will use that one }
  1503. if FindLibraryFile(binname,target_info.staticClibprefix,target_info.staticClibext,foundimp) then
  1504. exit;
  1505. { check if we can find the dll }
  1506. GetDefExt(NN,PP);
  1507. if not FindDll(DLLName(binname,NN,PP),impname) then
  1508. exit;
  1509. { read the dll file }
  1510. assign(f,impname);
  1511. OldFileMode:=filemode;
  1512. filemode:=0;
  1513. reset(f,1);
  1514. filemode:=OldFileMode;
  1515. if not DOSstubOK(HeaderOffset)then
  1516. scan:=false
  1517. else if not isSuitableFileType(HeaderOffset)then
  1518. scan:=false
  1519. else
  1520. scan:=GetEdata(HeaderOffset);
  1521. close(f);
  1522. end;
  1523. {*****************************************************************************
  1524. Initialize
  1525. *****************************************************************************}
  1526. initialization
  1527. {$ifdef i386}
  1528. RegisterExternalLinker(system_i386_win32_info,TLinkerWin32);
  1529. RegisterImport(system_i386_win32,TImportLibWin32);
  1530. RegisterExport(system_i386_win32,TExportLibWin32);
  1531. RegisterDLLScanner(system_i386_win32,TDLLScannerWin32);
  1532. RegisterRes(res_gnu_windres_info);
  1533. RegisterTarget(system_i386_win32_info);
  1534. {$endif i386}
  1535. end.
  1536. {
  1537. $Log$
  1538. Revision 1.38 2004-10-14 18:16:17 mazen
  1539. * USE_SYSUTILS merged successfully : cycles with and without defines
  1540. * Need to be optimized in performance
  1541. Revision 1.37 2004/09/22 15:25:14 mazen
  1542. * Fix error committing : previous version must be in branch USE_SYSUTILS
  1543. Revision 1.35 2004/06/20 08:55:32 florian
  1544. * logs truncated
  1545. Revision 1.34 2004/06/18 15:16:46 peter
  1546. * remove obsolete cardinal() typecasts
  1547. Revision 1.33 2004/06/16 20:07:11 florian
  1548. * dwarf branch merged
  1549. Revision 1.32 2004/04/28 18:02:54 peter
  1550. * add TList to cclasses, remove classes dependency from t_win32
  1551. Revision 1.31 2004/04/24 17:32:05 peter
  1552. index number generation for mixed index-nonindexed fixed, patch by Pavel V. Ozerski
  1553. Revision 1.30.2.5 2004/05/03 14:59:58 peter
  1554. * no dlltool needed for win32 linking executables
  1555. }