t_win.pas 62 KB

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