t_win.pas 61 KB

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