t_win.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i386) Win32 target
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit t_win;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. dos,
  23. cutils,cclasses,
  24. aasmbase,aasmtai,aasmdata,aasmcpu,fmodule,globtype,globals,systems,verbose,
  25. symconst,symdef,symsym,
  26. script,gendef,
  27. cpubase,
  28. import,export,link,cgobj,i_win;
  29. const
  30. MAX_DEFAULT_EXTENSIONS = 3;
  31. type
  32. tStr4=array[1..MAX_DEFAULT_EXTENSIONS] of string[4];
  33. pStr4=^tStr4;
  34. twin32imported_item = class(timported_item)
  35. procdef : tprocdef;
  36. end;
  37. timportlibwin32=class(timportlib)
  38. private
  39. procedure win32importproc(aprocdef:tprocdef;const func,module : string;index : longint;const name : string);
  40. procedure importvariable_str(const s:string;const name,module:string);
  41. procedure importprocedure_str(const func,module:string;index:longint;const name:string);
  42. procedure generateimportlib;
  43. procedure generateidatasection;
  44. public
  45. procedure preparelib(const s:string);override;
  46. procedure importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);override;
  47. procedure importvariable(vs:tglobalvarsym;const name,module:string);override;
  48. procedure generatelib;override;
  49. end;
  50. texportlibwin32=class(texportlib)
  51. st : string;
  52. EList_indexed:TFPList;
  53. EList_nonindexed:TFPList;
  54. procedure preparelib(const s:string);override;
  55. procedure exportprocedure(hp : texported_item);override;
  56. procedure exportvar(hp : texported_item);override;
  57. procedure exportfromlist(hp : texported_item);
  58. procedure generatelib;override;
  59. procedure generatenasmlib;virtual;
  60. end;
  61. tlinkerwin32=class(texternallinker)
  62. private
  63. Function WriteResponseFile(isdll:boolean) : Boolean;
  64. Function PostProcessExecutable(const fn:string;isdll:boolean) : Boolean;
  65. public
  66. Constructor Create;override;
  67. Procedure SetDefaultInfo;override;
  68. function MakeExecutable:boolean;override;
  69. function MakeSharedLibrary:boolean;override;
  70. end;
  71. tDLLScannerWin32=class(tDLLScanner)
  72. function Scan(const binname:string):boolean;override;
  73. end;
  74. implementation
  75. uses
  76. cpuinfo,cgutils,dbgbase,
  77. owar,ogbase,ogcoff;
  78. const
  79. res_gnu_windres_info : tresinfo =
  80. (
  81. id : res_gnu_windres;
  82. resbin : 'windres';
  83. rescmd : '--include $INC -O coff -o $OBJ $RES'
  84. );
  85. res_gnu_wince_windres_info : tresinfo =
  86. (
  87. id : res_gnu_wince_windres;
  88. resbin : 'windres';
  89. rescmd : '--include $INC -O coff -o $OBJ $RES'
  90. );
  91. {*****************************************************************************
  92. TIMPORTLIBWIN32
  93. *****************************************************************************}
  94. procedure timportlibwin32.preparelib(const s : string);
  95. begin
  96. if current_asmdata.asmlists[al_imports]=nil then
  97. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  98. end;
  99. procedure timportlibwin32.win32importproc(aprocdef:tprocdef;const func,module : string;index : longint;const name : string);
  100. var
  101. hp1 : timportList;
  102. hp2 : twin32imported_item;
  103. hs : string;
  104. begin
  105. { procdef or funcname must be give, not both }
  106. if assigned(aprocdef) and (func<>'') then
  107. internalerror(200411161);
  108. { append extension if required }
  109. hs:=AddExtension(module,target_info.sharedlibext);
  110. { search for the module }
  111. hp1:=timportlist(current_module.imports.first);
  112. while assigned(hp1) do
  113. begin
  114. if hs=hp1.dllname^ then
  115. break;
  116. hp1:=timportlist(hp1.next);
  117. end;
  118. { generate a new item ? }
  119. if not(assigned(hp1)) then
  120. begin
  121. hp1:=timportlist.create(hs);
  122. current_module.imports.concat(hp1);
  123. end;
  124. { search for reuse of old import item }
  125. if assigned(aprocdef) then
  126. begin
  127. hp2:=twin32imported_item(hp1.imported_items.first);
  128. while assigned(hp2) do
  129. begin
  130. if (hp2.procdef=aprocdef) then
  131. break;
  132. hp2:=twin32imported_item(hp2.next);
  133. end;
  134. end
  135. else
  136. begin
  137. hp2:=twin32imported_item(hp1.imported_items.first);
  138. while assigned(hp2) do
  139. begin
  140. if (hp2.func^=func) then
  141. break;
  142. hp2:=twin32imported_item(hp2.next);
  143. end;
  144. end;
  145. if not assigned(hp2) then
  146. begin
  147. hp2:=twin32imported_item.create(func,name,index);
  148. hp2.procdef:=aprocdef;
  149. hp1.imported_items.concat(hp2);
  150. end;
  151. end;
  152. procedure timportlibwin32.importprocedure(aprocdef:tprocdef;const module : string;index : longint;const name : string);
  153. begin
  154. win32importproc(aprocdef,'',module,index,name);
  155. end;
  156. procedure timportlibwin32.importprocedure_str(const func,module : string;index : longint;const name : string);
  157. begin
  158. win32importproc(nil,func,module,index,name);
  159. end;
  160. procedure timportlibwin32.importvariable(vs:tglobalvarsym;const name,module:string);
  161. begin
  162. importvariable_str(vs.mangledname,name,module);
  163. end;
  164. procedure timportlibwin32.importvariable_str(const s:string;const name,module:string);
  165. var
  166. hp1 : timportList;
  167. hp2 : twin32imported_item;
  168. hs : string;
  169. begin
  170. hs:=AddExtension(module,target_info.sharedlibext);
  171. { search for the module }
  172. hp1:=timportlist(current_module.imports.first);
  173. while assigned(hp1) do
  174. begin
  175. if hs=hp1.dllname^ then
  176. break;
  177. hp1:=timportlist(hp1.next);
  178. end;
  179. { generate a new item ? }
  180. if not(assigned(hp1)) then
  181. begin
  182. hp1:=timportlist.create(hs);
  183. current_module.imports.concat(hp1);
  184. end;
  185. hp2:=twin32imported_item.create_var(s,name);
  186. hp2.procdef:=nil;
  187. hp1.imported_items.concat(hp2);
  188. end;
  189. procedure timportlibwin32.generateimportlib;
  190. var
  191. ObjWriter : tarobjectwriter;
  192. ObjOutput : TPECoffObjOutput;
  193. basedllname : string;
  194. AsmPrefix : string;
  195. idatalabnr,
  196. SmartFilesCount,
  197. SmartHeaderCount : longint;
  198. function CreateObjData(place:tcutplace):TObjData;
  199. var
  200. s : string;
  201. begin
  202. s:='';
  203. case place of
  204. cut_begin :
  205. begin
  206. inc(SmartHeaderCount);
  207. s:=asmprefix+tostr(SmartHeaderCount)+'h';
  208. end;
  209. cut_normal :
  210. s:=asmprefix+tostr(SmartHeaderCount)+'s';
  211. cut_end :
  212. s:=asmprefix+tostr(SmartHeaderCount)+'t';
  213. end;
  214. inc(SmartFilesCount);
  215. result:=ObjOutput.NewObjData(FixFileName(s+tostr(SmartFilesCount)+target_info.objext));
  216. ObjOutput.startobjectfile(Result.Name);
  217. end;
  218. procedure WriteObjData(objdata:TObjData);
  219. begin
  220. ObjOutput.writeobjectfile(ObjData);
  221. end;
  222. procedure StartImport(const dllname:string);
  223. var
  224. headlabel,
  225. idata4label,
  226. idata5label,
  227. idata7label : TObjSymbol;
  228. emptyint : longint;
  229. objdata : TObjData;
  230. idata2objsection,
  231. idata4objsection,
  232. idata5objsection : TObjSection;
  233. begin
  234. objdata:=CreateObjData(cut_begin);
  235. idata2objsection:=objdata.createsection(sec_idata2,'');
  236. idata4objsection:=objdata.createsection(sec_idata4,'');
  237. idata5objsection:=objdata.createsection(sec_idata5,'');
  238. emptyint:=0;
  239. basedllname:=splitfilename(dllname);
  240. { idata4 }
  241. objdata.SetSection(idata4objsection);
  242. idata4label:=objdata.SymbolDefine(asmprefix+'_names_'+basedllname,AB_GLOBAL,AT_DATA);
  243. { idata5 }
  244. objdata.SetSection(idata5objsection);
  245. idata5label:=objdata.SymbolDefine(asmprefix+'_fixup_'+basedllname,AB_GLOBAL,AT_DATA);
  246. { idata2 }
  247. objdata.SetSection(idata2objsection);
  248. headlabel:=objdata.SymbolDefine(asmprefix+'_head_'+basedllname,AB_GLOBAL,AT_DATA);
  249. ObjOutput.exportsymbol(headlabel);
  250. objdata.writereloc(0,sizeof(longint),idata4label,RELOC_RVA);
  251. objdata.writebytes(emptyint,sizeof(emptyint));
  252. objdata.writebytes(emptyint,sizeof(emptyint));
  253. idata7label:=objdata.SymbolRef(asmprefix+'_dll_'+basedllname);
  254. objdata.writereloc(0,sizeof(longint),idata7label,RELOC_RVA);
  255. objdata.writereloc(0,sizeof(longint),idata5label,RELOC_RVA);
  256. WriteObjData(objdata);
  257. objdata.free;
  258. end;
  259. procedure EndImport;
  260. var
  261. idata7label : TObjSymbol;
  262. emptyint : longint;
  263. objdata : TObjData;
  264. idata4objsection,
  265. idata5objsection,
  266. idata7objsection : TObjSection;
  267. begin
  268. objdata:=CreateObjData(cut_end);
  269. idata4objsection:=objdata.createsection(sec_idata4,'');
  270. idata5objsection:=objdata.createsection(sec_idata5,'');
  271. idata7objsection:=objdata.createsection(sec_idata7,'');
  272. emptyint:=0;
  273. { idata4 }
  274. objdata.SetSection(idata4objsection);
  275. objdata.writebytes(emptyint,sizeof(emptyint));
  276. if target_info.system=system_x86_64_win64 then
  277. objdata.writebytes(emptyint,sizeof(emptyint));
  278. { idata5 }
  279. objdata.SetSection(idata5objsection);
  280. objdata.writebytes(emptyint,sizeof(emptyint));
  281. if target_info.system=system_x86_64_win64 then
  282. objdata.writebytes(emptyint,sizeof(emptyint));
  283. { idata7 }
  284. objdata.SetSection(idata7objsection);
  285. idata7label:=objdata.SymbolDefine(asmprefix+'_dll_'+basedllname,AB_GLOBAL,AT_DATA);
  286. objoutput.exportsymbol(idata7label);
  287. objdata.writebytes(basedllname[1],length(basedllname));
  288. objdata.writebytes(emptyint,1);
  289. WriteObjData(objdata);
  290. objdata.free;
  291. end;
  292. procedure AddImport(const afuncname:string;ordnr:word;isvar:boolean);
  293. const
  294. {$ifdef x86_64}
  295. jmpopcode : array[0..2] of byte = (
  296. $ff,$24,$25
  297. );
  298. {$else x86_64}
  299. jmpopcode : array[0..1] of byte = (
  300. $ff,$25
  301. );
  302. {$endif x86_64}
  303. nopopcodes : array[0..1] of byte = (
  304. $90,$90
  305. );
  306. var
  307. implabel,
  308. idata2label,
  309. idata5label,
  310. idata6label : TObjSymbol;
  311. emptyint : longint;
  312. objdata : TObjData;
  313. textobjsection,
  314. idata4objsection,
  315. idata5objsection,
  316. idata6objsection,
  317. idata7objsection : TObjSection;
  318. begin
  319. objdata:=CreateObjData(cut_normal);
  320. if not isvar then
  321. textobjsection:=objdata.createsection(sec_code,'');
  322. idata4objsection:=objdata.createsection(sec_idata4,'');
  323. idata5objsection:=objdata.createsection(sec_idata5,'');
  324. idata6objsection:=objdata.createsection(sec_idata6,'');
  325. idata7objsection:=objdata.createsection(sec_idata7,'');
  326. emptyint:=0;
  327. { idata7, link to head }
  328. objdata.SetSection(idata7objsection);
  329. idata2label:=objdata.SymbolRef(asmprefix+'_head_'+basedllname);
  330. objdata.writereloc(0,sizeof(longint),idata2label,RELOC_RVA);
  331. { idata6, import data (ordnr+name) }
  332. objdata.SetSection(idata6objsection);
  333. inc(idatalabnr);
  334. idata6label:=objdata.SymbolDefine(asmprefix+'_'+tostr(idatalabnr),AB_LOCAL,AT_DATA);
  335. objdata.writebytes(ordnr,2);
  336. objdata.writebytes(afuncname[1],length(afuncname));
  337. objdata.writebytes(emptyint,1);
  338. objdata.writebytes(emptyint,align(objdata.CurrObjSec.size,2)-objdata.CurrObjSec.size);
  339. { idata4, import lookup table }
  340. objdata.SetSection(idata4objsection);
  341. objdata.writereloc(0,sizeof(longint),idata6label,RELOC_RVA);
  342. if target_info.system=system_x86_64_win64 then
  343. objdata.writebytes(emptyint,sizeof(emptyint));
  344. { idata5, import address table }
  345. objdata.SetSection(idata5objsection);
  346. if isvar then
  347. implabel:=objdata.SymbolDefine(afuncname,AB_GLOBAL,AT_DATA)
  348. else
  349. idata5label:=objdata.SymbolDefine(asmprefix+'_'+afuncname,AB_LOCAL,AT_DATA);
  350. objdata.writereloc(0,sizeof(longint),idata6label,RELOC_RVA);
  351. if target_info.system=system_x86_64_win64 then
  352. objdata.writebytes(emptyint,sizeof(emptyint));
  353. { text, jmp }
  354. if not isvar then
  355. begin
  356. objdata.SetSection(textobjsection);
  357. implabel:=objdata.SymbolDefine(afuncname,AB_GLOBAL,AT_FUNCTION);
  358. objdata.writebytes(jmpopcode,sizeof(jmpopcode));
  359. objdata.writereloc(0,sizeof(longint),idata5label,RELOC_ABSOLUTE32);
  360. objdata.writebytes(nopopcodes,align(objdata.CurrObjSec.size,sizeof(nopopcodes))-objdata.CurrObjSec.size);
  361. end;
  362. ObjOutput.exportsymbol(implabel);
  363. WriteObjData(objdata);
  364. objdata.free;
  365. end;
  366. var
  367. hp1 : timportList;
  368. hp2 : twin32imported_item;
  369. begin
  370. AsmPrefix:='imp'+Lower(current_module.modulename^);
  371. idatalabnr:=0;
  372. SmartFilesCount:=0;
  373. SmartHeaderCount:=0;
  374. current_module.linkotherstaticlibs.add(current_module.importlibfilename^,link_always);
  375. ObjWriter:=TARObjectWriter.create(current_module.importlibfilename^);
  376. ObjOutput:=TPECoffObjOutput.Create(ObjWriter);
  377. hp1:=timportlist(current_module.imports.first);
  378. while assigned(hp1) do
  379. begin
  380. StartImport(hp1.dllname^);
  381. hp2:=twin32imported_item(hp1.imported_items.first);
  382. while assigned(hp2) do
  383. begin
  384. AddImport(hp2.name^,hp2.ordnr,hp2.is_var);
  385. hp2:=twin32imported_item(hp2.next);
  386. end;
  387. EndImport;
  388. hp1:=timportlist(hp1.next);
  389. end;
  390. ObjOutput.Free;
  391. ObjWriter.Free;
  392. end;
  393. procedure timportlibwin32.generateidatasection;
  394. var
  395. hp1 : timportList;
  396. hp2 : twin32imported_item;
  397. l1,l2,l3,l4 {$ifdef ARM} ,l5 {$endif ARM} : tasmlabel;
  398. mangledstring : string;
  399. importname : string;
  400. suffix : integer;
  401. href : treference;
  402. begin
  403. if (target_asm.id in [as_i386_masm,as_i386_tasm,as_i386_nasmwin32]) then
  404. begin
  405. new_section(current_asmdata.asmlists[al_imports],sec_code,'',0);
  406. hp1:=timportlist(current_module.imports.first);
  407. while assigned(hp1) do
  408. begin
  409. hp2:=twin32imported_item(hp1.imported_items.first);
  410. while assigned(hp2) do
  411. begin
  412. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_extern,hp2.func^));
  413. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_nasm_import,hp2.func^+' '+hp1.dllname^+' '+hp2.name^));
  414. hp2:=twin32imported_item(hp2.next);
  415. end;
  416. hp1:=timportlist(hp1.next);
  417. end;
  418. exit;
  419. end;
  420. hp1:=timportlist(current_module.imports.first);
  421. while assigned(hp1) do
  422. begin
  423. { align al_procedures for the jumps }
  424. new_section(current_asmdata.asmlists[al_imports],sec_code,'',sizeof(aint));
  425. { Get labels for the sections }
  426. current_asmdata.getjumplabel(l1);
  427. current_asmdata.getjumplabel(l2);
  428. current_asmdata.getjumplabel(l3);
  429. new_section(current_asmdata.asmlists[al_imports],sec_idata2,'',0);
  430. { pointer to procedure names }
  431. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_rva_sym(l2));
  432. { two empty entries follow }
  433. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  434. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  435. { pointer to dll name }
  436. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_rva_sym(l1));
  437. { pointer to fixups }
  438. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_rva_sym(l3));
  439. { only create one section for each else it will
  440. create a lot of idata* }
  441. { first write the name references }
  442. new_section(current_asmdata.asmlists[al_imports],sec_idata4,'',0);
  443. current_asmdata.asmlists[al_imports].concat(Tai_label.Create(l2));
  444. hp2:=twin32imported_item(hp1.imported_items.first);
  445. while assigned(hp2) do
  446. begin
  447. current_asmdata.getjumplabel(tasmlabel(hp2.lab));
  448. if hp2.name^<>'' then
  449. begin
  450. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_rva_sym(hp2.lab));
  451. if target_info.system=system_x86_64_win64 then
  452. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  453. end
  454. else
  455. begin
  456. if target_info.system=system_x86_64_win64 then
  457. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_64bit(int64($8000000000000000) or hp2.ordnr))
  458. else
  459. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(longint($80000000) or hp2.ordnr));
  460. end;
  461. hp2:=twin32imported_item(hp2.next);
  462. end;
  463. { finalize the names ... }
  464. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  465. if target_info.system=system_x86_64_win64 then
  466. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  467. { then the addresses and create also the indirect jump }
  468. new_section(current_asmdata.asmlists[al_imports],sec_idata5,'',0);
  469. current_asmdata.asmlists[al_imports].concat(Tai_label.Create(l3));
  470. hp2:=twin32imported_item(hp1.imported_items.first);
  471. while assigned(hp2) do
  472. begin
  473. if not hp2.is_var then
  474. begin
  475. current_asmdata.getjumplabel(l4);
  476. {$ifdef ARM}
  477. current_asmdata.getjumplabel(l5);
  478. {$endif ARM}
  479. { create indirect jump and }
  480. { place jump in al_procedures }
  481. new_section(current_asmdata.asmlists[al_imports],sec_code,'',0);
  482. if assigned(hp2.procdef) then
  483. mangledstring:=hp2.procdef.mangledname
  484. else
  485. mangledstring:=hp2.func^;
  486. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Createname_global(mangledstring,AT_FUNCTION,0));
  487. current_asmdata.asmlists[al_imports].concat(tai_function_name.create(''));
  488. {$ifdef ARM}
  489. reference_reset_symbol(href,l5,0);
  490. current_asmdata.asmlists[al_imports].concat(Taicpu.op_reg_ref(A_LDR,NR_R12,href));
  491. reference_reset_base(href,NR_R12,0);
  492. current_asmdata.asmlists[al_imports].concat(Taicpu.op_reg_ref(A_LDR,NR_R15,href));
  493. current_asmdata.asmlists[al_imports].concat(Tai_label.Create(l5));
  494. reference_reset_symbol(href,l4,0);
  495. current_asmdata.asmlists[al_imports].concat(tai_const.create_sym_offset(href.symbol,href.offset));
  496. {$else ARM}
  497. reference_reset_symbol(href,l4,0);
  498. current_asmdata.asmlists[al_imports].concat(Taicpu.Op_ref(A_JMP,S_NO,href));
  499. current_asmdata.asmlists[al_imports].concat(Tai_align.Create_op(4,$90));
  500. {$endif ARM}
  501. { add jump field to al_imports }
  502. new_section(current_asmdata.asmlists[al_imports],sec_idata5,'',0);
  503. if (cs_debuginfo in aktmoduleswitches) then
  504. begin
  505. if assigned(hp2.name) then
  506. begin
  507. importname:='__imp_'+hp2.name^;
  508. suffix:=0;
  509. while assigned(current_asmdata.getasmsymbol(importname)) do
  510. begin
  511. inc(suffix);
  512. importname:='__imp_'+hp2.name^+'_'+tostr(suffix);
  513. end;
  514. current_asmdata.asmlists[al_imports].concat(tai_symbol.createname(importname,AT_FUNCTION,4));
  515. end
  516. else
  517. begin
  518. importname:='__imp_by_ordinal'+tostr(hp2.ordnr);
  519. suffix:=0;
  520. while assigned(current_asmdata.getasmsymbol(importname)) do
  521. begin
  522. inc(suffix);
  523. importname:='__imp_by_ordinal'+tostr(hp2.ordnr)+'_'+tostr(suffix);
  524. end;
  525. current_asmdata.asmlists[al_imports].concat(tai_symbol.createname(importname,AT_FUNCTION,4));
  526. end;
  527. end;
  528. current_asmdata.asmlists[al_imports].concat(Tai_label.Create(l4));
  529. end
  530. else
  531. begin
  532. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Createname_global(hp2.func^,AT_DATA,0));
  533. end;
  534. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_rva_sym(hp2.lab));
  535. if target_info.system=system_x86_64_win64 then
  536. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  537. hp2:=twin32imported_item(hp2.next);
  538. end;
  539. { finalize the addresses }
  540. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  541. if target_info.system=system_x86_64_win64 then
  542. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  543. { finally the import information }
  544. new_section(current_asmdata.asmlists[al_imports],sec_idata6,'',0);
  545. hp2:=twin32imported_item(hp1.imported_items.first);
  546. while assigned(hp2) do
  547. begin
  548. current_asmdata.asmlists[al_imports].concat(Tai_label.Create(hp2.lab));
  549. { the ordinal number }
  550. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_16bit(hp2.ordnr));
  551. current_asmdata.asmlists[al_imports].concat(Tai_string.Create(hp2.name^+#0));
  552. current_asmdata.asmlists[al_imports].concat(Tai_align.Create_op(2,0));
  553. hp2:=twin32imported_item(hp2.next);
  554. end;
  555. { create import dll name }
  556. new_section(current_asmdata.asmlists[al_imports],sec_idata7,'',0);
  557. current_asmdata.asmlists[al_imports].concat(Tai_label.Create(l1));
  558. current_asmdata.asmlists[al_imports].concat(Tai_string.Create(hp1.dllname^+#0));
  559. hp1:=timportlist(hp1.next);
  560. end;
  561. end;
  562. procedure timportlibwin32.generatelib;
  563. begin
  564. if GenerateImportSection then
  565. generateidatasection
  566. else
  567. generateimportlib;
  568. end;
  569. {*****************************************************************************
  570. TEXPORTLIBWIN32
  571. *****************************************************************************}
  572. procedure texportlibwin32.preparelib(const s:string);
  573. begin
  574. if current_asmdata.asmlists[al_exports]=nil then
  575. current_asmdata.asmlists[al_exports]:=TAsmList.create;
  576. EList_indexed:=tFPList.Create;
  577. EList_nonindexed:=tFPList.Create;
  578. current_asmdata.getdatalabel(edatalabel);
  579. end;
  580. procedure texportlibwin32.exportvar(hp : texported_item);
  581. begin
  582. { same code used !! PM }
  583. exportprocedure(hp);
  584. end;
  585. var
  586. Gl_DoubleIndex:boolean;
  587. Gl_DoubleIndexValue:longint;
  588. function IdxCompare(Item1, Item2: Pointer): Integer;
  589. var
  590. I1:texported_item absolute Item1;
  591. I2:texported_item absolute Item2;
  592. begin
  593. Result:=I1.index-I2.index;
  594. if(Result=0)and(Item1<>Item2)then
  595. begin
  596. Gl_DoubleIndex:=true;
  597. Gl_DoubleIndexValue:=I1.index;
  598. end;
  599. end;
  600. procedure texportlibwin32.exportprocedure(hp : texported_item);
  601. begin
  602. if ((hp.options and eo_index)<>0)and((hp.index<=0) or (hp.index>$ffff)) then
  603. begin
  604. message1(parser_e_export_invalid_index,tostr(hp.index));
  605. exit;
  606. end;
  607. if hp.options and eo_index=eo_index then
  608. EList_indexed.Add(hp)
  609. else
  610. EList_nonindexed.Add(hp);
  611. end;
  612. procedure texportlibwin32.exportfromlist(hp : texported_item);
  613. //formerly texportlibwin32.exportprocedure
  614. { must be ordered at least for win32 !! }
  615. var
  616. hp2 : texported_item;
  617. begin
  618. hp2:=texported_item(current_module._exports.first);
  619. while assigned(hp2) and
  620. (hp.name^>hp2.name^) do
  621. hp2:=texported_item(hp2.next);
  622. { insert hp there !! }
  623. if hp2=nil then
  624. current_module._exports.concat(hp)
  625. else
  626. begin
  627. if hp2.name^=hp.name^ then
  628. begin
  629. { this is not allowed !! }
  630. message1(parser_e_export_name_double,hp.name^);
  631. exit;
  632. end;
  633. current_module._exports.insertbefore(hp,hp2);
  634. end;
  635. end;
  636. procedure texportlibwin32.generatelib;
  637. var
  638. ordinal_base,ordinal_max,ordinal_min : longint;
  639. current_index : longint;
  640. entries,named_entries : longint;
  641. name_label,dll_name_label,export_address_table : tasmlabel;
  642. export_name_table_pointers,export_ordinal_table : tasmlabel;
  643. hp,hp2 : texported_item;
  644. temtexport : TLinkedList;
  645. address_table,name_table_pointers,
  646. name_table,ordinal_table : TAsmList;
  647. i,autoindex,ni_high : longint;
  648. hole : boolean;
  649. begin
  650. Gl_DoubleIndex:=false;
  651. ELIst_indexed.Sort(@IdxCompare);
  652. if Gl_DoubleIndex then
  653. begin
  654. message1(parser_e_export_ordinal_double,tostr(Gl_DoubleIndexValue));
  655. EList_indexed.Free;
  656. EList_nonindexed.Free;
  657. exit;
  658. end;
  659. autoindex:=1;
  660. while EList_nonindexed.Count>0 do
  661. begin
  662. hole:=(EList_indexed.Count>0)and(texported_item(EList_indexed.Items[0]).index>1);
  663. if not hole then
  664. for i:=autoindex to pred(EList_indexed.Count)do
  665. if texported_item(EList_indexed.Items[i]).index-texported_item(EList_indexed.Items[pred(i)]).index>1 then
  666. begin
  667. autoindex:=succ(texported_item(EList_indexed.Items[pred(i)]).index);
  668. hole:=true;
  669. break;
  670. end;
  671. ni_high:=pred(EList_nonindexed.Count);
  672. if not hole then
  673. begin
  674. autoindex:=succ(EList_indexed.Count);
  675. EList_indexed.Add(EList_nonindexed.Items[ni_high]);
  676. end
  677. else
  678. EList_indexed.Insert(pred(AutoIndex),EList_nonindexed.Items[ni_high]);
  679. EList_nonindexed.Delete(ni_high);
  680. texported_item(EList_indexed.Items[pred(AutoIndex)]).index:=autoindex;
  681. end;
  682. EList_nonindexed.Free;
  683. for i:=0 to pred(EList_indexed.Count)do
  684. exportfromlist(texported_item(EList_indexed.Items[i]));
  685. EList_indexed.Free;
  686. if (target_asm.id in [as_i386_masm,as_i386_tasm,as_i386_nasmwin32]) then
  687. begin
  688. generatenasmlib;
  689. exit;
  690. end;
  691. hp:=texported_item(current_module._exports.first);
  692. if not assigned(hp) then
  693. exit;
  694. ordinal_max:=0;
  695. ordinal_min:=$7FFFFFFF;
  696. entries:=0;
  697. named_entries:=0;
  698. current_asmdata.getjumplabel(dll_name_label);
  699. current_asmdata.getjumplabel(export_address_table);
  700. current_asmdata.getjumplabel(export_name_table_pointers);
  701. current_asmdata.getjumplabel(export_ordinal_table);
  702. { count entries }
  703. while assigned(hp) do
  704. begin
  705. inc(entries);
  706. if (hp.index>ordinal_max) then
  707. ordinal_max:=hp.index;
  708. if (hp.index>0) and (hp.index<ordinal_min) then
  709. ordinal_min:=hp.index;
  710. if assigned(hp.name) then
  711. inc(named_entries);
  712. hp:=texported_item(hp.next);
  713. end;
  714. { no support for higher ordinal base yet !! }
  715. ordinal_base:=1;
  716. current_index:=ordinal_base;
  717. { we must also count the holes !! }
  718. entries:=ordinal_max-ordinal_base+1;
  719. new_section(current_asmdata.asmlists[al_exports],sec_edata,'',0);
  720. { create label to reference from main so smartlink will include
  721. the .edata section }
  722. current_asmdata.asmlists[al_exports].concat(Tai_symbol.Create_global(edatalabel,0));
  723. { export flags }
  724. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_32bit(0));
  725. { date/time stamp }
  726. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_32bit(0));
  727. { major version }
  728. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_16bit(0));
  729. { minor version }
  730. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_16bit(0));
  731. { pointer to dll name }
  732. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_rva_sym(dll_name_label));
  733. { ordinal base normally set to 1 }
  734. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_32bit(ordinal_base));
  735. { number of entries }
  736. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_32bit(entries));
  737. { number of named entries }
  738. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_32bit(named_entries));
  739. { address of export address table }
  740. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_rva_sym(export_address_table));
  741. { address of name pointer pointers }
  742. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_rva_sym(export_name_table_pointers));
  743. { address of ordinal number pointers }
  744. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_rva_sym(export_ordinal_table));
  745. { the name }
  746. current_asmdata.asmlists[al_exports].concat(Tai_label.Create(dll_name_label));
  747. if st='' then
  748. current_asmdata.asmlists[al_exports].concat(Tai_string.Create(current_module.modulename^+target_info.sharedlibext+#0))
  749. else
  750. current_asmdata.asmlists[al_exports].concat(Tai_string.Create(st+target_info.sharedlibext+#0));
  751. { export address table }
  752. address_table:=TAsmList.create;
  753. address_table.concat(Tai_align.Create_op(4,0));
  754. address_table.concat(Tai_label.Create(export_address_table));
  755. name_table_pointers:=TAsmList.create;
  756. name_table_pointers.concat(Tai_align.Create_op(4,0));
  757. name_table_pointers.concat(Tai_label.Create(export_name_table_pointers));
  758. ordinal_table:=TAsmList.create;
  759. ordinal_table.concat(Tai_align.Create_op(4,0));
  760. ordinal_table.concat(Tai_label.Create(export_ordinal_table));
  761. name_table:=TAsmList.Create;
  762. name_table.concat(Tai_align.Create_op(4,0));
  763. { write each address }
  764. hp:=texported_item(current_module._exports.first);
  765. while assigned(hp) do
  766. begin
  767. if (hp.options and eo_name)<>0 then
  768. begin
  769. current_asmdata.getjumplabel(name_label);
  770. name_table_pointers.concat(Tai_const.Create_rva_sym(name_label));
  771. ordinal_table.concat(Tai_const.Create_16bit(hp.index-ordinal_base));
  772. name_table.concat(Tai_align.Create_op(2,0));
  773. name_table.concat(Tai_label.Create(name_label));
  774. name_table.concat(Tai_string.Create(hp.name^+#0));
  775. end;
  776. hp:=texported_item(hp.next);
  777. end;
  778. { order in increasing ordinal values }
  779. { into temtexport list }
  780. temtexport:=TLinkedList.Create;
  781. hp:=texported_item(current_module._exports.first);
  782. while assigned(hp) do
  783. begin
  784. current_module._exports.remove(hp);
  785. hp2:=texported_item(temtexport.first);
  786. while assigned(hp2) and (hp.index>hp2.index) do
  787. hp2:=texported_item(hp2.next);
  788. if hp2=nil then
  789. temtexport.concat(hp)
  790. else
  791. temtexport.insertbefore(hp,hp2);
  792. hp:=texported_item(current_module._exports.first);;
  793. end;
  794. { write the export adress table }
  795. current_index:=ordinal_base;
  796. hp:=texported_item(temtexport.first);
  797. while assigned(hp) do
  798. begin
  799. { fill missing values }
  800. while current_index<hp.index do
  801. begin
  802. address_table.concat(Tai_const.Create_32bit(0));
  803. inc(current_index);
  804. end;
  805. case hp.sym.typ of
  806. globalvarsym :
  807. address_table.concat(Tai_const.Createname_rva(tglobalvarsym(hp.sym).mangledname));
  808. typedconstsym :
  809. address_table.concat(Tai_const.Createname_rva(ttypedconstsym(hp.sym).mangledname));
  810. procsym :
  811. address_table.concat(Tai_const.Createname_rva(tprocsym(hp.sym).first_procdef.mangledname));
  812. end;
  813. inc(current_index);
  814. hp:=texported_item(hp.next);
  815. end;
  816. current_asmdata.asmlists[al_exports].concatlist(address_table);
  817. current_asmdata.asmlists[al_exports].concatlist(name_table_pointers);
  818. current_asmdata.asmlists[al_exports].concatlist(ordinal_table);
  819. current_asmdata.asmlists[al_exports].concatlist(name_table);
  820. address_table.Free;
  821. name_table_pointers.free;
  822. ordinal_table.free;
  823. name_table.free;
  824. temtexport.free;
  825. end;
  826. procedure texportlibwin32.generatenasmlib;
  827. var
  828. hp : texported_item;
  829. p : pchar;
  830. s : string;
  831. begin
  832. new_section(current_asmdata.asmlists[al_exports],sec_code,'',0);
  833. hp:=texported_item(current_module._exports.first);
  834. while assigned(hp) do
  835. begin
  836. case hp.sym.typ of
  837. globalvarsym :
  838. s:=tglobalvarsym(hp.sym).mangledname;
  839. typedconstsym :
  840. s:=ttypedconstsym(hp.sym).mangledname;
  841. procsym :
  842. s:=tprocsym(hp.sym).first_procdef.mangledname;
  843. else
  844. s:='';
  845. end;
  846. p:=strpnew(#9+'export '+s+' '+hp.name^+' '+tostr(hp.index));
  847. {current_asmdata.asmlists[al_exports].concat(tai_direct.create(p));}
  848. hp:=texported_item(hp.next);
  849. end;
  850. end;
  851. {****************************************************************************
  852. TLINKERWIN32
  853. ****************************************************************************}
  854. Constructor TLinkerWin32.Create;
  855. begin
  856. Inherited Create;
  857. { allow duplicated libs (PM) }
  858. SharedLibFiles.doubles:=true;
  859. StaticLibFiles.doubles:=true; if not Dontlinkstdlibpath Then
  860. end;
  861. Procedure TLinkerWin32.SetDefaultInfo;
  862. var
  863. targetopts: string;
  864. begin
  865. with Info do
  866. begin
  867. {$ifdef ARM}
  868. targetopts:='-m armpe';
  869. {$else ARM}
  870. targetopts:='-b pe-i386 -m i386pe';
  871. {$endif ARM}
  872. ExeCmd[1]:='ld '+targetopts+' $OPT $GCSECTIONS $MAP $STRIP $APPTYPE $ENTRY $IMAGEBASE $RELOC -o $EXE $RES';
  873. DllCmd[1]:='ld '+targetopts+' $OPT $GCSECTIONS $MAP $STRIP --dll $APPTYPE $ENTRY $IMAGEBASE $RELOC -o $EXE $RES';
  874. { ExeCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF';
  875. use short forms to avoid 128 char limitation problem }
  876. ExeCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  877. ExeCmd[3]:='ld '+targetopts+' $OPT $STRIP $APPTYPE $ENTRY $IMAGEBASE -o $EXE $RES exp.$$$';
  878. { DllCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF'; }
  879. DllCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  880. DllCmd[3]:='ld '+targetopts+' $OPT $STRIP --dll $APPTYPE $ENTRY $IMAGEBASE -o $EXE $RES exp.$$$';
  881. end;
  882. end;
  883. Function TLinkerWin32.WriteResponseFile(isdll:boolean) : Boolean;
  884. Var
  885. linkres : TLinkRes;
  886. HPath : TStringListItem;
  887. s,s2 : string;
  888. i : integer;
  889. linklibcygwin : boolean;
  890. begin
  891. WriteResponseFile:=False;
  892. linklibcygwin:=(SharedLibFiles.Find('cygwin')<>nil);
  893. if (cs_profile in aktmoduleswitches) then
  894. begin
  895. SharedLibFiles.Concat('gmon');
  896. SharedLibFiles.Concat('c');
  897. SharedLibFiles.Concat('gcc');
  898. SharedLibFiles.Concat('kernel32');
  899. end;
  900. { Open link.res file }
  901. LinkRes:=TLinkres.Create(outputexedir+Info.ResName);
  902. with linkres do
  903. begin
  904. { Write path to search libraries }
  905. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  906. while assigned(HPath) do
  907. begin
  908. Add('SEARCH_DIR('+MaybeQuoted(HPath.Str)+')');
  909. HPath:=TStringListItem(HPath.Next);
  910. end;
  911. HPath:=TStringListItem(LibrarySearchPath.First);
  912. while assigned(HPath) do
  913. begin
  914. Add('SEARCH_DIR('+MaybeQuoted(HPath.Str)+')');
  915. HPath:=TStringListItem(HPath.Next);
  916. end;
  917. { add objectfiles, start with prt0 always }
  918. { profiling of shared libraries is currently not supported }
  919. if not ObjectFiles.Empty then
  920. begin
  921. Add('INPUT(');
  922. { For wince external startup file is used and placed first, }
  923. { because ARM prolog structure must be located at the very }
  924. { beginning of code. Otherwise exceptions do not work properly. }
  925. if target_info.system in [system_arm_wince,system_i386_wince] then
  926. LinkRes.AddFileName(MaybeQuoted(FindObjectFile('wprt0','',false)));
  927. while not ObjectFiles.Empty do
  928. begin
  929. s:=ObjectFiles.GetFirst;
  930. if s<>'' then
  931. AddFileName(MaybeQuoted(s));
  932. end;
  933. Add(')');
  934. end;
  935. { Write staticlibraries }
  936. if (not StaticLibFiles.Empty) then
  937. begin
  938. Add('GROUP(');
  939. While not StaticLibFiles.Empty do
  940. begin
  941. S:=StaticLibFiles.GetFirst;
  942. AddFileName(MaybeQuoted(s));
  943. end;
  944. Add(')');
  945. end;
  946. { Write sharedlibraries (=import libraries) }
  947. if not SharedLibFiles.Empty then
  948. begin
  949. Add('INPUT(') ;
  950. While not SharedLibFiles.Empty do
  951. begin
  952. S:=SharedLibFiles.GetFirst;
  953. if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  954. begin
  955. Add(MaybeQuoted(s2));
  956. continue;
  957. end;
  958. if pos(target_info.sharedlibprefix,s)=1 then
  959. s:=copy(s,length(target_info.sharedlibprefix)+1,255);
  960. i:=Pos(target_info.sharedlibext,S);
  961. if i>0 then
  962. Delete(S,i,255);
  963. Add('-l'+s);
  964. end;
  965. Add(')');
  966. end;
  967. Add('SEARCH_DIR("/usr/i686-pc-cygwin/lib"); SEARCH_DIR("/usr/lib"); SEARCH_DIR("/usr/lib/w32api");');
  968. Add('OUTPUT_FORMAT(pei-i386)');
  969. Add('ENTRY(_mainCRTStartup)');
  970. Add('SECTIONS');
  971. Add('{');
  972. Add(' . = SIZEOF_HEADERS;');
  973. Add(' . = ALIGN(__section_alignment__);');
  974. Add(' .text __image_base__ + ( __section_alignment__ < 0x1000 ? . : __section_alignment__ ) :');
  975. Add(' {');
  976. Add(' *(.init)');
  977. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  978. Add(' *(SORT(.text$*))');
  979. Add(' ___CTOR_LIST__ = .; __CTOR_LIST__ = . ;');
  980. Add(' LONG (-1);*(.ctors); *(.ctor); *(SORT(.ctors.*)); LONG (0);');
  981. Add(' ___DTOR_LIST__ = .; __DTOR_LIST__ = . ;');
  982. Add(' LONG (-1); *(.dtors); *(.dtor); *(SORT(.dtors.*)); LONG (0);');
  983. Add(' *(.fini)');
  984. Add(' PROVIDE (etext = .);');
  985. Add(' *(.gcc_except_table)');
  986. Add(' }');
  987. Add(' .data BLOCK(__section_alignment__) :');
  988. Add(' {');
  989. Add(' __data_start__ = . ;');
  990. add(' *(.data .data.* .gnu.linkonce.d.*)');
  991. Add(' *(.data2)');
  992. Add(' *(SORT(.data$*))');
  993. Add(' __data_end__ = . ;');
  994. Add(' *(.data_cygwin_nocopy)');
  995. Add(' }');
  996. Add(' .rdata BLOCK(__section_alignment__) :');
  997. Add(' {');
  998. Add(' *(.rdata)');
  999. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  1000. Add(' *(SORT(.rdata$*))');
  1001. Add(' *(.eh_frame)');
  1002. Add(' ___RUNTIME_PSEUDO_RELOC_LIST__ = .;');
  1003. Add(' __RUNTIME_PSEUDO_RELOC_LIST__ = .;');
  1004. Add(' *(.rdata_runtime_pseudo_reloc)');
  1005. Add(' ___RUNTIME_PSEUDO_RELOC_LIST_END__ = .;');
  1006. Add(' __RUNTIME_PSEUDO_RELOC_LIST_END__ = .;');
  1007. Add(' }');
  1008. Add(' .pdata BLOCK(__section_alignment__) : { *(.pdata) }');
  1009. Add(' .bss BLOCK(__section_alignment__) :');
  1010. Add(' {');
  1011. Add(' __bss_start__ = . ;');
  1012. Add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  1013. Add(' *(SORT(.bss$*))');
  1014. Add(' *(COMMON)');
  1015. Add(' __bss_end__ = . ;');
  1016. Add(' }');
  1017. Add(' .edata BLOCK(__section_alignment__) : { *(.edata) }');
  1018. Add(' .idata BLOCK(__section_alignment__) :');
  1019. Add(' {');
  1020. Add(' SORT(*)(.idata$2)');
  1021. Add(' SORT(*)(.idata$3)');
  1022. Add(' /* These zeroes mark the end of the import list. */');
  1023. Add(' LONG (0); LONG (0); LONG (0); LONG (0); LONG (0);');
  1024. Add(' SORT(*)(.idata$4)');
  1025. Add(' SORT(*)(.idata$5)');
  1026. Add(' SORT(*)(.idata$6)');
  1027. Add(' SORT(*)(.idata$7)');
  1028. Add(' }');
  1029. Add(' .CRT BLOCK(__section_alignment__) :');
  1030. Add(' {');
  1031. Add(' ___crt_xc_start__ = . ;');
  1032. Add(' *(SORT(.CRT$XC*)) /* C initialization */');
  1033. Add(' ___crt_xc_end__ = . ;');
  1034. Add(' ___crt_xi_start__ = . ;');
  1035. Add(' *(SORT(.CRT$XI*)) /* C++ initialization */');
  1036. Add(' ___crt_xi_end__ = . ;');
  1037. Add(' ___crt_xl_start__ = . ;');
  1038. Add(' *(SORT(.CRT$XL*)) /* TLS callbacks */');
  1039. Add(' /* ___crt_xl_end__ is defined in the TLS Directory support code */');
  1040. Add(' ___crt_xp_start__ = . ;');
  1041. Add(' *(SORT(.CRT$XP*)) /* Pre-termination */');
  1042. Add(' ___crt_xp_end__ = . ;');
  1043. Add(' ___crt_xt_start__ = . ;');
  1044. Add(' *(SORT(.CRT$XT*)) /* Termination */');
  1045. Add(' ___crt_xt_end__ = . ;');
  1046. Add(' }');
  1047. Add(' .tls BLOCK(__section_alignment__) :');
  1048. Add(' {');
  1049. Add(' ___tls_start__ = . ;');
  1050. Add(' *(.tls .tls.*)');
  1051. Add(' *(.tls$)');
  1052. Add(' *(SORT(.tls$*))');
  1053. Add(' ___tls_end__ = . ;');
  1054. Add(' }');
  1055. Add(' .rsrc BLOCK(__section_alignment__) :');
  1056. Add(' {');
  1057. Add(' *(.rsrc)');
  1058. Add(' *(SORT(.rsrc$*))');
  1059. Add(' }');
  1060. Add(' .reloc BLOCK(__section_alignment__) : { *(.reloc) }');
  1061. Add(' .stab BLOCK(__section_alignment__) (NOLOAD) : { *(.stab) }');
  1062. Add(' .stabstr BLOCK(__section_alignment__) (NOLOAD) : { *(.stabstr) }');
  1063. Add(' .debug_aranges BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_aranges) }');
  1064. Add(' .debug_pubnames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_pubnames) }');
  1065. Add(' .debug_info BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  1066. Add(' .debug_abbrev BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_abbrev) }');
  1067. Add(' .debug_line BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_line) }');
  1068. Add(' .debug_frame BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_frame) }');
  1069. Add(' .debug_str BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_str) }');
  1070. Add(' .debug_loc BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_loc) }');
  1071. Add(' .debug_macinfo BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_macinfo) }');
  1072. Add(' .debug_weaknames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_weaknames) }');
  1073. Add(' .debug_funcnames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_funcnames) }');
  1074. Add(' .debug_typenames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_typenames) }');
  1075. Add(' .debug_varnames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_varnames) }');
  1076. Add(' .debug_ranges BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_ranges) }');
  1077. Add('}');
  1078. { Write and Close response }
  1079. writetodisk;
  1080. Free;
  1081. end;
  1082. WriteResponseFile:=True;
  1083. end;
  1084. function TLinkerWin32.MakeExecutable:boolean;
  1085. var
  1086. MapStr,
  1087. binstr : String;
  1088. cmdstr : TCmdStr;
  1089. success : boolean;
  1090. cmds,i : longint;
  1091. AsBinStr : string[80];
  1092. GCSectionsStr,
  1093. StripStr,
  1094. RelocStr,
  1095. AppTypeStr,
  1096. EntryStr,
  1097. ImageBaseStr : string[40];
  1098. begin
  1099. if not(cs_link_extern in aktglobalswitches) then
  1100. Message1(exec_i_linking,current_module.exefilename^);
  1101. { Create some replacements }
  1102. RelocStr:='';
  1103. AppTypeStr:='';
  1104. EntryStr:='';
  1105. ImageBaseStr:='';
  1106. StripStr:='';
  1107. MapStr:='';
  1108. GCSectionsStr:='';
  1109. AsBinStr:=FindUtil(utilsprefix+'as');
  1110. if RelocSection then
  1111. RelocStr:='--base-file base.$$$';
  1112. if use_smartlink_section then
  1113. GCSectionsStr:='--gc-sections';
  1114. if target_info.system in [system_arm_wince,system_i386_wince] then
  1115. AppTypeStr:='--subsystem wince'
  1116. else
  1117. begin
  1118. if apptype=app_gui then
  1119. AppTypeStr:='--subsystem windows';
  1120. end;
  1121. if apptype=app_gui then
  1122. EntryStr:='--entry=_WinMainCRTStartup'
  1123. else
  1124. EntryStr:='--entry=_mainCRTStartup';
  1125. if assigned(DLLImageBase) then
  1126. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  1127. if (cs_link_strip in aktglobalswitches) then
  1128. StripStr:='-s';
  1129. if (cs_link_map in aktglobalswitches) then
  1130. MapStr:='-Map '+maybequoted(ForceExtension(current_module.exefilename^,'.map'));
  1131. { Write used files and libraries }
  1132. WriteResponseFile(false);
  1133. { Call linker }
  1134. success:=false;
  1135. if RelocSection or (not Deffile.empty) then
  1136. cmds:=3
  1137. else
  1138. cmds:=1;
  1139. for i:=1 to cmds do
  1140. begin
  1141. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  1142. if binstr<>'' then
  1143. begin
  1144. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  1145. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1146. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1147. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  1148. Replace(cmdstr,'$ENTRY',EntryStr);
  1149. Replace(cmdstr,'$ASBIN',AsbinStr);
  1150. Replace(cmdstr,'$RELOC',RelocStr);
  1151. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  1152. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1153. Replace(cmdstr,'$STRIP',StripStr);
  1154. Replace(cmdstr,'$MAP',MapStr);
  1155. if not DefFile.Empty then
  1156. begin
  1157. DefFile.WriteFile;
  1158. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  1159. end
  1160. else
  1161. Replace(cmdstr,'$DEF','');
  1162. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  1163. if not success then
  1164. break;
  1165. end;
  1166. end;
  1167. { Post process }
  1168. if success then
  1169. success:=PostProcessExecutable(current_module.exefilename^,false);
  1170. { Remove ReponseFile }
  1171. if (success) and not(cs_link_extern in aktglobalswitches) then
  1172. begin
  1173. RemoveFile(outputexedir+Info.ResName);
  1174. RemoveFile('base.$$$');
  1175. RemoveFile('exp.$$$');
  1176. RemoveFile('deffile.$$$');
  1177. end;
  1178. MakeExecutable:=success; { otherwise a recursive call to link method }
  1179. end;
  1180. Function TLinkerWin32.MakeSharedLibrary:boolean;
  1181. var
  1182. MapStr,
  1183. binstr : String;
  1184. cmdstr : TCmdStr;
  1185. success : boolean;
  1186. cmds,
  1187. i : longint;
  1188. AsBinStr : string[80];
  1189. StripStr,
  1190. GCSectionsStr,
  1191. RelocStr,
  1192. AppTypeStr,
  1193. EntryStr,
  1194. ImageBaseStr : string[40];
  1195. begin
  1196. MakeSharedLibrary:=false;
  1197. if not(cs_link_extern in aktglobalswitches) then
  1198. Message1(exec_i_linking,current_module.sharedlibfilename^);
  1199. { Create some replacements }
  1200. RelocStr:='';
  1201. AppTypeStr:='';
  1202. EntryStr:='';
  1203. ImageBaseStr:='';
  1204. StripStr:='';
  1205. MapStr:='';
  1206. GCSectionsStr:='';
  1207. AsBinStr:=FindUtil(utilsprefix+'as');
  1208. if RelocSection then
  1209. RelocStr:='--base-file base.$$$';
  1210. if use_smartlink_section then
  1211. GCSectionsStr:='--gc-sections';
  1212. if apptype=app_gui then
  1213. begin
  1214. AppTypeStr:='--subsystem windows';
  1215. EntryStr:='--entry _DLLWinMainCRTStartup'
  1216. end
  1217. else
  1218. EntryStr:='--entry _DLLMainCRTStartup';
  1219. if assigned(DLLImageBase) then
  1220. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  1221. if (cs_link_strip in aktglobalswitches) then
  1222. StripStr:='-s';
  1223. if (cs_link_map in aktglobalswitches) then
  1224. MapStr:='-Map '+maybequoted(ForceExtension(current_module.exefilename^,'.map'));
  1225. { Write used files and libraries }
  1226. WriteResponseFile(true);
  1227. { Call linker }
  1228. success:=false;
  1229. if RelocSection or (not Deffile.empty) then
  1230. cmds:=3
  1231. else
  1232. cmds:=1;
  1233. for i:=1 to cmds do
  1234. begin
  1235. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  1236. if binstr<>'' then
  1237. begin
  1238. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1239. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1240. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1241. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  1242. Replace(cmdstr,'$ENTRY',EntryStr);
  1243. Replace(cmdstr,'$ASBIN',AsbinStr);
  1244. Replace(cmdstr,'$RELOC',RelocStr);
  1245. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  1246. Replace(cmdstr,'$STRIP',StripStr);
  1247. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1248. Replace(cmdstr,'$MAP',MapStr);
  1249. if not DefFile.Empty then
  1250. begin
  1251. DefFile.WriteFile;
  1252. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  1253. end
  1254. else
  1255. Replace(cmdstr,'$DEF','');
  1256. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  1257. if not success then
  1258. break;
  1259. end;
  1260. end;
  1261. { Post process }
  1262. if success then
  1263. success:=PostProcessExecutable(current_module.sharedlibfilename^,true);
  1264. { Remove ReponseFile }
  1265. if (success) and not(cs_link_extern in aktglobalswitches) then
  1266. begin
  1267. RemoveFile(outputexedir+Info.ResName);
  1268. RemoveFile('base.$$$');
  1269. RemoveFile('exp.$$$');
  1270. RemoveFile('deffile.$$$');
  1271. end;
  1272. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1273. end;
  1274. function tlinkerwin32.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1275. type
  1276. tdosheader = packed record
  1277. e_magic : word;
  1278. e_cblp : word;
  1279. e_cp : word;
  1280. e_crlc : word;
  1281. e_cparhdr : word;
  1282. e_minalloc : word;
  1283. e_maxalloc : word;
  1284. e_ss : word;
  1285. e_sp : word;
  1286. e_csum : word;
  1287. e_ip : word;
  1288. e_cs : word;
  1289. e_lfarlc : word;
  1290. e_ovno : word;
  1291. e_res : array[0..3] of word;
  1292. e_oemid : word;
  1293. e_oeminfo : word;
  1294. e_res2 : array[0..9] of word;
  1295. e_lfanew : longint;
  1296. end;
  1297. tpeheader = packed record
  1298. PEMagic : array[0..3] of char;
  1299. Machine : word;
  1300. NumberOfSections : word;
  1301. TimeDateStamp : longint;
  1302. PointerToSymbolTable : longint;
  1303. NumberOfSymbols : longint;
  1304. SizeOfOptionalHeader : word;
  1305. Characteristics : word;
  1306. Magic : word;
  1307. MajorLinkerVersion : byte;
  1308. MinorLinkerVersion : byte;
  1309. SizeOfCode : longint;
  1310. SizeOfInitializedData : longint;
  1311. SizeOfUninitializedData : longint;
  1312. AddressOfEntryPoint : longint;
  1313. BaseOfCode : longint;
  1314. BaseOfData : longint;
  1315. ImageBase : longint;
  1316. SectionAlignment : longint;
  1317. FileAlignment : longint;
  1318. MajorOperatingSystemVersion : word;
  1319. MinorOperatingSystemVersion : word;
  1320. MajorImageVersion : word;
  1321. MinorImageVersion : word;
  1322. MajorSubsystemVersion : word;
  1323. MinorSubsystemVersion : word;
  1324. Reserved1 : longint;
  1325. SizeOfImage : longint;
  1326. SizeOfHeaders : longint;
  1327. CheckSum : longint;
  1328. Subsystem : word;
  1329. DllCharacteristics : word;
  1330. SizeOfStackReserve : longint;
  1331. SizeOfStackCommit : longint;
  1332. SizeOfHeapReserve : longint;
  1333. SizeOfHeapCommit : longint;
  1334. LoaderFlags : longint;
  1335. NumberOfRvaAndSizes : longint;
  1336. DataDirectory : array[1..$80] of byte;
  1337. end;
  1338. tcoffsechdr=packed record
  1339. name : array[0..7] of char;
  1340. vsize : longint;
  1341. rvaofs : longint;
  1342. datalen : longint;
  1343. datapos : longint;
  1344. relocpos : longint;
  1345. lineno1 : longint;
  1346. nrelocs : word;
  1347. lineno2 : word;
  1348. flags : longint;
  1349. end;
  1350. psecfill=^TSecfill;
  1351. TSecfill=record
  1352. fillpos,
  1353. fillsize : longint;
  1354. next : psecfill;
  1355. end;
  1356. var
  1357. f : file;
  1358. cmdstr : string;
  1359. dosheader : tdosheader;
  1360. peheader : tpeheader;
  1361. firstsecpos,
  1362. maxfillsize,
  1363. l,peheaderpos : longint;
  1364. coffsec : tcoffsechdr;
  1365. secroot,hsecroot : psecfill;
  1366. zerobuf : pointer;
  1367. begin
  1368. postprocessexecutable:=false;
  1369. { when -s is used or it's a dll then quit }
  1370. if (cs_link_extern in aktglobalswitches) then
  1371. begin
  1372. case apptype of
  1373. app_native :
  1374. cmdstr:='--subsystem native';
  1375. app_gui :
  1376. cmdstr:='--subsystem gui';
  1377. app_cui :
  1378. cmdstr:='--subsystem console';
  1379. end;
  1380. if dllversion<>'' then
  1381. cmdstr:=cmdstr+' --version '+dllversion;
  1382. cmdstr:=cmdstr+' --input '+maybequoted(fn);
  1383. cmdstr:=cmdstr+' --stack '+tostr(stacksize);
  1384. DoExec(FindUtil(utilsprefix+'postw32'),cmdstr,false,false);
  1385. postprocessexecutable:=true;
  1386. exit;
  1387. end;
  1388. { open file }
  1389. assign(f,fn);
  1390. {$I-}
  1391. reset(f,1);
  1392. if ioresult<>0 then
  1393. Message1(execinfo_f_cant_open_executable,fn);
  1394. { read headers }
  1395. blockread(f,dosheader,sizeof(tdosheader));
  1396. peheaderpos:=dosheader.e_lfanew;
  1397. seek(f,peheaderpos);
  1398. blockread(f,peheader,sizeof(tpeheader));
  1399. { write info }
  1400. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  1401. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  1402. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  1403. { change stack size (PM) }
  1404. { I am not sure that the default value is adequate !! }
  1405. peheader.SizeOfStackReserve:=stacksize;
  1406. { change the header }
  1407. { sub system }
  1408. { gui=2 }
  1409. { cui=3 }
  1410. { wincegui=9 }
  1411. if target_info.system in [system_arm_wince,system_i386_wince] then
  1412. peheader.Subsystem:=9
  1413. else
  1414. case apptype of
  1415. app_native :
  1416. peheader.Subsystem:=1;
  1417. app_gui :
  1418. peheader.Subsystem:=2;
  1419. app_cui :
  1420. peheader.Subsystem:=3;
  1421. end;
  1422. if dllversion<>'' then
  1423. begin
  1424. peheader.MajorImageVersion:=dllmajor;
  1425. peheader.MinorImageVersion:=dllminor;
  1426. end;
  1427. { reset timestamp }
  1428. peheader.TimeDateStamp:=0;
  1429. { write header back }
  1430. seek(f,peheaderpos);
  1431. blockwrite(f,peheader,sizeof(tpeheader));
  1432. if ioresult<>0 then
  1433. Message1(execinfo_f_cant_process_executable,fn);
  1434. seek(f,peheaderpos);
  1435. blockread(f,peheader,sizeof(tpeheader));
  1436. { write the value after the change }
  1437. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  1438. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  1439. { read section info }
  1440. maxfillsize:=0;
  1441. firstsecpos:=0;
  1442. secroot:=nil;
  1443. for l:=1 to peheader.NumberOfSections do
  1444. begin
  1445. blockread(f,coffsec,sizeof(tcoffsechdr));
  1446. if coffsec.datapos>0 then
  1447. begin
  1448. if secroot=nil then
  1449. firstsecpos:=coffsec.datapos;
  1450. new(hsecroot);
  1451. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  1452. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  1453. hsecroot^.next:=secroot;
  1454. secroot:=hsecroot;
  1455. if secroot^.fillsize>maxfillsize then
  1456. maxfillsize:=secroot^.fillsize;
  1457. end;
  1458. end;
  1459. if firstsecpos>0 then
  1460. begin
  1461. l:=firstsecpos-filepos(f);
  1462. if l>maxfillsize then
  1463. maxfillsize:=l;
  1464. end
  1465. else
  1466. l:=0;
  1467. { get zero buffer }
  1468. getmem(zerobuf,maxfillsize);
  1469. fillchar(zerobuf^,maxfillsize,0);
  1470. { zero from sectioninfo until first section }
  1471. blockwrite(f,zerobuf^,l);
  1472. { zero section alignments }
  1473. while assigned(secroot) do
  1474. begin
  1475. seek(f,secroot^.fillpos);
  1476. blockwrite(f,zerobuf^,secroot^.fillsize);
  1477. hsecroot:=secroot;
  1478. secroot:=secroot^.next;
  1479. dispose(hsecroot);
  1480. end;
  1481. freemem(zerobuf,maxfillsize);
  1482. close(f);
  1483. {$I+}
  1484. if ioresult<>0 then;
  1485. postprocessexecutable:=true;
  1486. end;
  1487. {****************************************************************************
  1488. TDLLScannerWin32
  1489. ****************************************************************************}
  1490. procedure CheckDLLFunc(const dllname,funcname:string);
  1491. var
  1492. hp : tExternalsItem;
  1493. begin
  1494. hp:=tExternalsItem(current_module.Externals.first);
  1495. while assigned(hp)do
  1496. begin
  1497. if (not hp.found) and
  1498. assigned(hp.data) and
  1499. (hp.data^=funcname) then
  1500. begin
  1501. hp.found:=true;
  1502. if not(current_module.uses_imports) then
  1503. begin
  1504. current_module.uses_imports:=true;
  1505. importlib.preparelib(current_module.modulename^);
  1506. end;
  1507. // if IsData then
  1508. // timportlibwin32(importlib).importvariable_str(funcname,dllname,funcname)
  1509. // else
  1510. timportlibwin32(importlib).importprocedure_str(funcname,dllname,0,funcname);
  1511. exit;
  1512. end;
  1513. hp:=tExternalsItem(hp.next);
  1514. end;
  1515. end;
  1516. function tDLLScannerWin32.scan(const binname:string):boolean;
  1517. var
  1518. hs,
  1519. dllname : string;
  1520. begin
  1521. result:=true;
  1522. { is there already an import library the we will use that one }
  1523. if FindLibraryFile(binname,target_info.staticClibprefix,target_info.staticClibext,hs) then
  1524. exit;
  1525. { check if we can find the dll }
  1526. hs:=AddExtension(binname,target_info.sharedlibext);
  1527. if not FindDll(hs,dllname) then
  1528. exit;
  1529. ReadDLLImports(dllname,@CheckDLLFunc);
  1530. end;
  1531. {*****************************************************************************
  1532. Initialize
  1533. *****************************************************************************}
  1534. initialization
  1535. {$ifdef i386}
  1536. RegisterExternalLinker(system_i386_win32_info,TLinkerWin32);
  1537. RegisterInternalLinker(system_i386_win32_info,TPECoffLinker);
  1538. RegisterImport(system_i386_win32,TImportLibWin32);
  1539. RegisterExport(system_i386_win32,TExportLibWin32);
  1540. RegisterDLLScanner(system_i386_win32,TDLLScannerWin32);
  1541. RegisterRes(res_gnu_windres_info);
  1542. RegisterTarget(system_i386_win32_info);
  1543. RegisterExternalLinker(system_i386_wince_info,TLinkerWin32);
  1544. RegisterImport(system_i386_wince,TImportLibWin32);
  1545. RegisterExport(system_i386_wince,TExportLibWin32);
  1546. RegisterDLLScanner(system_i386_wince,TDLLScannerWin32);
  1547. RegisterTarget(system_i386_wince_info);
  1548. {$endif i386}
  1549. {$ifdef x86_64}
  1550. RegisterExternalLinker(system_x64_win64_info,TLinkerWin32);
  1551. RegisterInternalLinker(system_x64_win64_info,TPECoffLinker);
  1552. RegisterImport(system_x86_64_win64,TImportLibWin32);
  1553. RegisterExport(system_x86_64_win64,TExportLibWin32);
  1554. RegisterDLLScanner(system_x86_64_win64,TDLLScannerWin32);
  1555. RegisterRes(res_gnu_windres_info);
  1556. RegisterTarget(system_x64_win64_info);
  1557. {$endif x86_64}
  1558. {$ifdef arm}
  1559. RegisterExternalLinker(system_arm_wince_info,TLinkerWin32);
  1560. RegisterImport(system_arm_wince,TImportLibWin32);
  1561. RegisterExport(system_arm_wince,TExportLibWin32);
  1562. RegisterRes(res_gnu_wince_windres_info);
  1563. RegisterTarget(system_arm_wince_info);
  1564. {$endif arm}
  1565. end.