t_win.pas 61 KB

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