t_win.pas 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  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. end;
  36. timportlibwin32=class(timportlib)
  37. private
  38. procedure win32importproc(const module : string;index : longint;const name : string);
  39. procedure importvariable_str(const s:string;const name,module:string);
  40. procedure importprocedure_str(const module:string;index:longint;const name:string);
  41. procedure generateimportlib;
  42. procedure generateidatasection;
  43. public
  44. procedure preparelib(const s:string);override;
  45. procedure importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);override;
  46. procedure importvariable(vs:tglobalvarsym;const name,module:string);override;
  47. procedure generatelib;override;
  48. end;
  49. texportlibwin32=class(texportlib)
  50. st : string;
  51. EList_indexed:TFPList;
  52. EList_nonindexed:TFPList;
  53. procedure preparelib(const s:string);override;
  54. procedure exportprocedure(hp : texported_item);override;
  55. procedure exportvar(hp : texported_item);override;
  56. procedure exportfromlist(hp : texported_item);
  57. procedure generatelib;override;
  58. procedure generatenasmlib;virtual;
  59. end;
  60. tlinkerwin32=class(texternallinker)
  61. private
  62. Function WriteResponseFile(isdll:boolean) : Boolean;
  63. Function PostProcessExecutable(const fn:string;isdll:boolean) : Boolean;
  64. public
  65. Constructor Create;override;
  66. Procedure SetDefaultInfo;override;
  67. function MakeExecutable:boolean;override;
  68. function MakeSharedLibrary:boolean;override;
  69. end;
  70. tDLLScannerWin32=class(tDLLScanner)
  71. private
  72. importfound : boolean;
  73. procedure CheckDLLFunc(const dllname,funcname:string);
  74. public
  75. function Scan(const binname:string):boolean;override;
  76. end;
  77. implementation
  78. uses
  79. cpuinfo,cgutils,dbgbase,
  80. owar,ogbase,ogcoff;
  81. const
  82. res_gnu_windres_info : tresinfo =
  83. (
  84. id : res_gnu_windres;
  85. resbin : 'windres';
  86. rescmd : '--include $INC -O coff -o $OBJ $RES'
  87. );
  88. res_gnu_wince_windres_info : tresinfo =
  89. (
  90. id : res_gnu_wince_windres;
  91. resbin : 'windres';
  92. rescmd : '--include $INC -O coff -o $OBJ $RES'
  93. );
  94. {*****************************************************************************
  95. TIMPORTLIBWIN32
  96. *****************************************************************************}
  97. procedure timportlibwin32.preparelib(const s : string);
  98. begin
  99. if current_asmdata.asmlists[al_imports]=nil then
  100. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  101. end;
  102. procedure timportlibwin32.win32importproc(const module : string;index : longint;const name : string);
  103. var
  104. hp1 : timportList;
  105. hp2 : twin32imported_item;
  106. hs : string;
  107. begin
  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. hp2:=twin32imported_item(hp1.imported_items.first);
  126. while assigned(hp2) do
  127. begin
  128. if (hp2.name^=name) and (hp2.ordnr=index) then
  129. break;
  130. hp2:=twin32imported_item(hp2.next);
  131. end;
  132. if not assigned(hp2) then
  133. begin
  134. hp2:=twin32imported_item.create(name,name,index);
  135. hp1.imported_items.concat(hp2);
  136. end;
  137. end;
  138. procedure timportlibwin32.importprocedure(aprocdef:tprocdef;const module : string;index : longint;const name : string);
  139. begin
  140. win32importproc(module,index,name);
  141. end;
  142. procedure timportlibwin32.importprocedure_str(const module : string;index : longint;const name : string);
  143. begin
  144. win32importproc(module,index,name);
  145. end;
  146. procedure timportlibwin32.importvariable(vs:tglobalvarsym;const name,module:string);
  147. begin
  148. importvariable_str(vs.mangledname,name,module);
  149. end;
  150. procedure timportlibwin32.importvariable_str(const s:string;const name,module:string);
  151. var
  152. hp1 : timportList;
  153. hp2 : twin32imported_item;
  154. hs : string;
  155. begin
  156. hs:=AddExtension(module,target_info.sharedlibext);
  157. { search for the module }
  158. hp1:=timportlist(current_module.imports.first);
  159. while assigned(hp1) do
  160. begin
  161. if hs=hp1.dllname^ then
  162. break;
  163. hp1:=timportlist(hp1.next);
  164. end;
  165. { generate a new item ? }
  166. if not(assigned(hp1)) then
  167. begin
  168. hp1:=timportlist.create(hs);
  169. current_module.imports.concat(hp1);
  170. end;
  171. hp2:=twin32imported_item.create_var(s,name);
  172. hp1.imported_items.concat(hp2);
  173. end;
  174. procedure timportlibwin32.generateimportlib;
  175. var
  176. ObjWriter : tarobjectwriter;
  177. ObjOutput : TPECoffObjOutput;
  178. basedllname : string;
  179. AsmPrefix : string;
  180. idatalabnr,
  181. SmartFilesCount,
  182. SmartHeaderCount : longint;
  183. function CreateObjData(place:tcutplace):TObjData;
  184. var
  185. s : string;
  186. begin
  187. s:='';
  188. case place of
  189. cut_begin :
  190. begin
  191. inc(SmartHeaderCount);
  192. s:=asmprefix+tostr(SmartHeaderCount)+'h';
  193. end;
  194. cut_normal :
  195. s:=asmprefix+tostr(SmartHeaderCount)+'s';
  196. cut_end :
  197. s:=asmprefix+tostr(SmartHeaderCount)+'t';
  198. end;
  199. inc(SmartFilesCount);
  200. result:=ObjOutput.NewObjData(FixFileName(s+tostr(SmartFilesCount)+target_info.objext));
  201. ObjOutput.startobjectfile(Result.Name);
  202. end;
  203. procedure WriteObjData(objdata:TObjData);
  204. begin
  205. ObjOutput.writeobjectfile(ObjData);
  206. end;
  207. procedure StartImport(const dllname:string);
  208. var
  209. headlabel,
  210. idata4label,
  211. idata5label,
  212. idata7label : TObjSymbol;
  213. emptyint : longint;
  214. objdata : TObjData;
  215. idata2objsection,
  216. idata4objsection,
  217. idata5objsection : TObjSection;
  218. begin
  219. objdata:=CreateObjData(cut_begin);
  220. idata2objsection:=objdata.createsection(sec_idata2,'');
  221. idata4objsection:=objdata.createsection(sec_idata4,'');
  222. idata5objsection:=objdata.createsection(sec_idata5,'');
  223. emptyint:=0;
  224. basedllname:=splitfilename(dllname);
  225. { idata4 }
  226. objdata.SetSection(idata4objsection);
  227. idata4label:=objdata.SymbolDefine(asmprefix+'_names_'+basedllname,AB_GLOBAL,AT_DATA);
  228. { idata5 }
  229. objdata.SetSection(idata5objsection);
  230. idata5label:=objdata.SymbolDefine(asmprefix+'_fixup_'+basedllname,AB_GLOBAL,AT_DATA);
  231. { idata2 }
  232. objdata.SetSection(idata2objsection);
  233. headlabel:=objdata.SymbolDefine(asmprefix+'_head_'+basedllname,AB_GLOBAL,AT_DATA);
  234. ObjOutput.exportsymbol(headlabel);
  235. objdata.writereloc(0,sizeof(longint),idata4label,RELOC_RVA);
  236. objdata.writebytes(emptyint,sizeof(emptyint));
  237. objdata.writebytes(emptyint,sizeof(emptyint));
  238. idata7label:=objdata.SymbolRef(asmprefix+'_dll_'+basedllname);
  239. objdata.writereloc(0,sizeof(longint),idata7label,RELOC_RVA);
  240. objdata.writereloc(0,sizeof(longint),idata5label,RELOC_RVA);
  241. WriteObjData(objdata);
  242. objdata.free;
  243. end;
  244. procedure EndImport;
  245. var
  246. idata7label : TObjSymbol;
  247. emptyint : longint;
  248. objdata : TObjData;
  249. idata4objsection,
  250. idata5objsection,
  251. idata7objsection : TObjSection;
  252. begin
  253. objdata:=CreateObjData(cut_end);
  254. idata4objsection:=objdata.createsection(sec_idata4,'');
  255. idata5objsection:=objdata.createsection(sec_idata5,'');
  256. idata7objsection:=objdata.createsection(sec_idata7,'');
  257. emptyint:=0;
  258. { idata4 }
  259. objdata.SetSection(idata4objsection);
  260. objdata.writebytes(emptyint,sizeof(emptyint));
  261. if target_info.system=system_x86_64_win64 then
  262. objdata.writebytes(emptyint,sizeof(emptyint));
  263. { idata5 }
  264. objdata.SetSection(idata5objsection);
  265. objdata.writebytes(emptyint,sizeof(emptyint));
  266. if target_info.system=system_x86_64_win64 then
  267. objdata.writebytes(emptyint,sizeof(emptyint));
  268. { idata7 }
  269. objdata.SetSection(idata7objsection);
  270. idata7label:=objdata.SymbolDefine(asmprefix+'_dll_'+basedllname,AB_GLOBAL,AT_DATA);
  271. objoutput.exportsymbol(idata7label);
  272. objdata.writebytes(basedllname[1],length(basedllname));
  273. objdata.writebytes(emptyint,1);
  274. WriteObjData(objdata);
  275. objdata.free;
  276. end;
  277. procedure AddImport(const afuncname:string;ordnr:word;isvar:boolean);
  278. const
  279. {$ifdef x86_64}
  280. jmpopcode : array[0..2] of byte = (
  281. $ff,$24,$25
  282. );
  283. {$else x86_64}
  284. {$ifdef arm}
  285. jmpopcode : array[0..7] of byte = (
  286. $00,$c0,$9f,$e5, // ldr ip, [pc, #0]
  287. $00,$f0,$9c,$e5 // ldr pc, [ip]
  288. );
  289. {$else arm}
  290. jmpopcode : array[0..1] of byte = (
  291. $ff,$25
  292. );
  293. {$endif arm}
  294. {$endif x86_64}
  295. nopopcodes : array[0..1] of byte = (
  296. $90,$90
  297. );
  298. var
  299. implabel,
  300. idata2label,
  301. idata5label,
  302. idata6label : TObjSymbol;
  303. emptyint : longint;
  304. objdata : TObjData;
  305. textobjsection,
  306. idata4objsection,
  307. idata5objsection,
  308. idata6objsection,
  309. idata7objsection : TObjSection;
  310. begin
  311. objdata:=CreateObjData(cut_normal);
  312. if not isvar then
  313. textobjsection:=objdata.createsection(sec_code,'');
  314. idata4objsection:=objdata.createsection(sec_idata4,'');
  315. idata5objsection:=objdata.createsection(sec_idata5,'');
  316. idata6objsection:=objdata.createsection(sec_idata6,'');
  317. idata7objsection:=objdata.createsection(sec_idata7,'');
  318. emptyint:=0;
  319. { idata7, link to head }
  320. objdata.SetSection(idata7objsection);
  321. idata2label:=objdata.SymbolRef(asmprefix+'_head_'+basedllname);
  322. objdata.writereloc(0,sizeof(longint),idata2label,RELOC_RVA);
  323. { idata6, import data (ordnr+name) }
  324. objdata.SetSection(idata6objsection);
  325. inc(idatalabnr);
  326. idata6label:=objdata.SymbolDefine(asmprefix+'_'+tostr(idatalabnr),AB_LOCAL,AT_DATA);
  327. objdata.writebytes(ordnr,2);
  328. objdata.writebytes(afuncname[1],length(afuncname));
  329. objdata.writebytes(emptyint,1);
  330. objdata.writebytes(emptyint,align(objdata.CurrObjSec.size,2)-objdata.CurrObjSec.size);
  331. { idata4, import lookup table }
  332. objdata.SetSection(idata4objsection);
  333. if afuncname<>'' then
  334. begin
  335. objdata.writereloc(0,sizeof(longint),idata6label,RELOC_RVA);
  336. if target_info.system=system_x86_64_win64 then
  337. objdata.writebytes(emptyint,sizeof(emptyint));
  338. end
  339. else
  340. begin
  341. emptyint:=ordnr;
  342. if target_info.system=system_x86_64_win64 then
  343. begin
  344. objdata.writebytes(emptyint,sizeof(emptyint));
  345. emptyint:=$80000000;
  346. objdata.writebytes(emptyint,sizeof(emptyint));
  347. end
  348. else
  349. begin
  350. emptyint:=emptyint or $80000000;
  351. objdata.writebytes(emptyint,sizeof(emptyint));
  352. end;
  353. emptyint:=0;
  354. end;
  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. if afuncname <> '' then
  369. implabel:=objdata.SymbolDefine(afuncname,AB_GLOBAL,AT_FUNCTION)
  370. else
  371. implabel:=objdata.SymbolDefine(basedllname+'_index_'+tostr(ordnr),AB_GLOBAL,AT_FUNCTION);
  372. objdata.writebytes(jmpopcode,sizeof(jmpopcode));
  373. objdata.writereloc(0,sizeof(longint),idata5label,RELOC_ABSOLUTE32);
  374. objdata.writebytes(nopopcodes,align(objdata.CurrObjSec.size,sizeof(nopopcodes))-objdata.CurrObjSec.size);
  375. end;
  376. ObjOutput.exportsymbol(implabel);
  377. WriteObjData(objdata);
  378. objdata.free;
  379. end;
  380. var
  381. hp1 : timportList;
  382. hp2 : twin32imported_item;
  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. AddImport(hp2.name^,hp2.ordnr,hp2.is_var);
  399. hp2:=twin32imported_item(hp2.next);
  400. end;
  401. EndImport;
  402. hp1:=timportlist(hp1.next);
  403. end;
  404. ObjOutput.Free;
  405. ObjWriter.Free;
  406. end;
  407. procedure timportlibwin32.generateidatasection;
  408. var
  409. hp1 : timportList;
  410. hp2 : twin32imported_item;
  411. l1,l2,l3,l4 {$ifdef ARM} ,l5 {$endif ARM} : tasmlabel;
  412. importname : string;
  413. suffix : integer;
  414. href : treference;
  415. begin
  416. if (target_asm.id in [as_i386_masm,as_i386_tasm,as_i386_nasmwin32]) then
  417. begin
  418. new_section(current_asmdata.asmlists[al_imports],sec_code,'',0);
  419. hp1:=timportlist(current_module.imports.first);
  420. while assigned(hp1) do
  421. begin
  422. hp2:=twin32imported_item(hp1.imported_items.first);
  423. while assigned(hp2) do
  424. begin
  425. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_extern,hp2.func^));
  426. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_nasm_import,hp2.func^+' '+hp1.dllname^+' '+hp2.name^));
  427. hp2:=twin32imported_item(hp2.next);
  428. end;
  429. hp1:=timportlist(hp1.next);
  430. end;
  431. exit;
  432. end;
  433. hp1:=timportlist(current_module.imports.first);
  434. while assigned(hp1) do
  435. begin
  436. { align al_procedures for the jumps }
  437. new_section(current_asmdata.asmlists[al_imports],sec_code,'',sizeof(aint));
  438. { Get labels for the sections }
  439. current_asmdata.getjumplabel(l1);
  440. current_asmdata.getjumplabel(l2);
  441. current_asmdata.getjumplabel(l3);
  442. new_section(current_asmdata.asmlists[al_imports],sec_idata2,'',0);
  443. { pointer to procedure names }
  444. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_rva_sym(l2));
  445. { two empty entries follow }
  446. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  447. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  448. { pointer to dll name }
  449. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_rva_sym(l1));
  450. { pointer to fixups }
  451. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_rva_sym(l3));
  452. { only create one section for each else it will
  453. create a lot of idata* }
  454. { first write the name references }
  455. new_section(current_asmdata.asmlists[al_imports],sec_idata4,'',0);
  456. current_asmdata.asmlists[al_imports].concat(Tai_label.Create(l2));
  457. hp2:=twin32imported_item(hp1.imported_items.first);
  458. while assigned(hp2) do
  459. begin
  460. current_asmdata.getjumplabel(tasmlabel(hp2.lab));
  461. if hp2.name^<>'' then
  462. begin
  463. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_rva_sym(hp2.lab));
  464. if target_info.system=system_x86_64_win64 then
  465. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  466. end
  467. else
  468. begin
  469. if target_info.system=system_x86_64_win64 then
  470. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_64bit(int64($8000000000000000) or hp2.ordnr))
  471. else
  472. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(longint($80000000) or hp2.ordnr));
  473. end;
  474. hp2:=twin32imported_item(hp2.next);
  475. end;
  476. { finalize the names ... }
  477. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  478. if target_info.system=system_x86_64_win64 then
  479. current_asmdata.asmlists[al_imports].concat(Tai_const.Create_32bit(0));
  480. { then the addresses and create also the indirect jump }
  481. new_section(current_asmdata.asmlists[al_imports],sec_idata5,'',0);
  482. current_asmdata.asmlists[al_imports].concat(Tai_label.Create(l3));
  483. hp2:=twin32imported_item(hp1.imported_items.first);
  484. while assigned(hp2) do
  485. begin
  486. if not hp2.is_var then
  487. begin
  488. current_asmdata.getjumplabel(l4);
  489. {$ifdef ARM}
  490. current_asmdata.getjumplabel(l5);
  491. {$endif ARM}
  492. { create indirect jump and }
  493. { place jump in al_procedures }
  494. new_section(current_asmdata.asmlists[al_imports],sec_code,'',0);
  495. if hp2.name^ <> '' then
  496. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0))
  497. else
  498. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Createname_global(splitfilename(hp1.dllname^)+'_index_'+tostr(hp2.ordnr),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. while not ObjectFiles.Empty do
  935. begin
  936. s:=ObjectFiles.GetFirst;
  937. if s<>'' then
  938. AddFileName(MaybeQuoted(s));
  939. end;
  940. Add(')');
  941. end;
  942. { Write staticlibraries }
  943. if (not StaticLibFiles.Empty) then
  944. begin
  945. Add('GROUP(');
  946. While not StaticLibFiles.Empty do
  947. begin
  948. S:=StaticLibFiles.GetFirst;
  949. AddFileName(MaybeQuoted(s));
  950. end;
  951. Add(')');
  952. end;
  953. { Write sharedlibraries (=import libraries) }
  954. if not SharedLibFiles.Empty then
  955. begin
  956. Add('INPUT(') ;
  957. While not SharedLibFiles.Empty do
  958. begin
  959. S:=SharedLibFiles.GetFirst;
  960. if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  961. begin
  962. Add(MaybeQuoted(s2));
  963. continue;
  964. end;
  965. if pos(target_info.sharedlibprefix,s)=1 then
  966. s:=copy(s,length(target_info.sharedlibprefix)+1,255);
  967. i:=Pos(target_info.sharedlibext,S);
  968. if i>0 then
  969. Delete(S,i,255);
  970. Add('-l'+s);
  971. end;
  972. Add(')');
  973. end;
  974. Add('SEARCH_DIR("/usr/i686-pc-cygwin/lib"); SEARCH_DIR("/usr/lib"); SEARCH_DIR("/usr/lib/w32api");');
  975. Add('OUTPUT_FORMAT(pei-i386)');
  976. Add('ENTRY(_mainCRTStartup)');
  977. Add('SECTIONS');
  978. Add('{');
  979. Add(' . = SIZEOF_HEADERS;');
  980. Add(' . = ALIGN(__section_alignment__);');
  981. Add(' .text __image_base__ + ( __section_alignment__ < 0x1000 ? . : __section_alignment__ ) :');
  982. Add(' {');
  983. {$ifdef arm}
  984. Add(' *(.pdata.FPC_EH_PROLOG)');
  985. {$endif arm}
  986. Add(' *(.init)');
  987. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  988. Add(' *(SORT(.text$*))');
  989. Add(' ___CTOR_LIST__ = .; __CTOR_LIST__ = . ;');
  990. Add(' LONG (-1);*(.ctors); *(.ctor); *(SORT(.ctors.*)); LONG (0);');
  991. Add(' ___DTOR_LIST__ = .; __DTOR_LIST__ = . ;');
  992. Add(' LONG (-1); *(.dtors); *(.dtor); *(SORT(.dtors.*)); LONG (0);');
  993. Add(' *(.fini)');
  994. Add(' PROVIDE (etext = .);');
  995. Add(' *(.gcc_except_table)');
  996. Add(' }');
  997. Add(' .data BLOCK(__section_alignment__) :');
  998. Add(' {');
  999. Add(' __data_start__ = . ;');
  1000. add(' *(.data .data.* .gnu.linkonce.d.*)');
  1001. Add(' *(.data2)');
  1002. Add(' *(SORT(.data$*))');
  1003. Add(' __data_end__ = . ;');
  1004. Add(' *(.data_cygwin_nocopy)');
  1005. Add(' }');
  1006. Add(' .rdata BLOCK(__section_alignment__) :');
  1007. Add(' {');
  1008. Add(' *(.rdata)');
  1009. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  1010. Add(' *(SORT(.rdata$*))');
  1011. Add(' *(.eh_frame)');
  1012. Add(' ___RUNTIME_PSEUDO_RELOC_LIST__ = .;');
  1013. Add(' __RUNTIME_PSEUDO_RELOC_LIST__ = .;');
  1014. Add(' *(.rdata_runtime_pseudo_reloc)');
  1015. Add(' ___RUNTIME_PSEUDO_RELOC_LIST_END__ = .;');
  1016. Add(' __RUNTIME_PSEUDO_RELOC_LIST_END__ = .;');
  1017. Add(' }');
  1018. Add(' .pdata BLOCK(__section_alignment__) : { *(.pdata) }');
  1019. Add(' .bss BLOCK(__section_alignment__) :');
  1020. Add(' {');
  1021. Add(' __bss_start__ = . ;');
  1022. Add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  1023. Add(' *(SORT(.bss$*))');
  1024. Add(' *(COMMON)');
  1025. Add(' __bss_end__ = . ;');
  1026. Add(' }');
  1027. Add(' .edata BLOCK(__section_alignment__) : { *(.edata) }');
  1028. Add(' .idata BLOCK(__section_alignment__) :');
  1029. Add(' {');
  1030. Add(' SORT(*)(.idata$2)');
  1031. Add(' SORT(*)(.idata$3)');
  1032. Add(' /* These zeroes mark the end of the import list. */');
  1033. Add(' LONG (0); LONG (0); LONG (0); LONG (0); LONG (0);');
  1034. Add(' SORT(*)(.idata$4)');
  1035. Add(' SORT(*)(.idata$5)');
  1036. Add(' SORT(*)(.idata$6)');
  1037. Add(' SORT(*)(.idata$7)');
  1038. Add(' }');
  1039. Add(' .CRT BLOCK(__section_alignment__) :');
  1040. Add(' {');
  1041. Add(' ___crt_xc_start__ = . ;');
  1042. Add(' *(SORT(.CRT$XC*)) /* C initialization */');
  1043. Add(' ___crt_xc_end__ = . ;');
  1044. Add(' ___crt_xi_start__ = . ;');
  1045. Add(' *(SORT(.CRT$XI*)) /* C++ initialization */');
  1046. Add(' ___crt_xi_end__ = . ;');
  1047. Add(' ___crt_xl_start__ = . ;');
  1048. Add(' *(SORT(.CRT$XL*)) /* TLS callbacks */');
  1049. Add(' /* ___crt_xl_end__ is defined in the TLS Directory support code */');
  1050. Add(' ___crt_xp_start__ = . ;');
  1051. Add(' *(SORT(.CRT$XP*)) /* Pre-termination */');
  1052. Add(' ___crt_xp_end__ = . ;');
  1053. Add(' ___crt_xt_start__ = . ;');
  1054. Add(' *(SORT(.CRT$XT*)) /* Termination */');
  1055. Add(' ___crt_xt_end__ = . ;');
  1056. Add(' }');
  1057. Add(' .tls BLOCK(__section_alignment__) :');
  1058. Add(' {');
  1059. Add(' ___tls_start__ = . ;');
  1060. Add(' *(.tls .tls.*)');
  1061. Add(' *(.tls$)');
  1062. Add(' *(SORT(.tls$*))');
  1063. Add(' ___tls_end__ = . ;');
  1064. Add(' }');
  1065. Add(' .rsrc BLOCK(__section_alignment__) :');
  1066. Add(' {');
  1067. Add(' *(.rsrc)');
  1068. Add(' *(SORT(.rsrc$*))');
  1069. Add(' }');
  1070. Add(' .reloc BLOCK(__section_alignment__) : { *(.reloc) }');
  1071. Add(' .stab BLOCK(__section_alignment__) (NOLOAD) : { *(.stab) }');
  1072. Add(' .stabstr BLOCK(__section_alignment__) (NOLOAD) : { *(.stabstr) }');
  1073. Add(' .debug_aranges BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_aranges) }');
  1074. Add(' .debug_pubnames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_pubnames) }');
  1075. Add(' .debug_info BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  1076. Add(' .debug_abbrev BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_abbrev) }');
  1077. Add(' .debug_line BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_line) }');
  1078. Add(' .debug_frame BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_frame) }');
  1079. Add(' .debug_str BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_str) }');
  1080. Add(' .debug_loc BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_loc) }');
  1081. Add(' .debug_macinfo BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_macinfo) }');
  1082. Add(' .debug_weaknames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_weaknames) }');
  1083. Add(' .debug_funcnames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_funcnames) }');
  1084. Add(' .debug_typenames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_typenames) }');
  1085. Add(' .debug_varnames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_varnames) }');
  1086. Add(' .debug_ranges BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_ranges) }');
  1087. Add('}');
  1088. { Write and Close response }
  1089. writetodisk;
  1090. Free;
  1091. end;
  1092. WriteResponseFile:=True;
  1093. end;
  1094. function TLinkerWin32.MakeExecutable:boolean;
  1095. var
  1096. MapStr,
  1097. binstr : String;
  1098. cmdstr : TCmdStr;
  1099. success : boolean;
  1100. cmds,i : longint;
  1101. AsBinStr : string[80];
  1102. GCSectionsStr,
  1103. StripStr,
  1104. RelocStr,
  1105. AppTypeStr,
  1106. EntryStr,
  1107. ImageBaseStr : string[40];
  1108. begin
  1109. if not(cs_link_nolink in aktglobalswitches) then
  1110. Message1(exec_i_linking,current_module.exefilename^);
  1111. { Create some replacements }
  1112. RelocStr:='';
  1113. AppTypeStr:='';
  1114. EntryStr:='';
  1115. ImageBaseStr:='';
  1116. StripStr:='';
  1117. MapStr:='';
  1118. GCSectionsStr:='';
  1119. AsBinStr:=FindUtil(utilsprefix+'as');
  1120. if RelocSection then
  1121. RelocStr:='--base-file base.$$$';
  1122. if use_smartlink_section then
  1123. GCSectionsStr:='--gc-sections';
  1124. if target_info.system in [system_arm_wince,system_i386_wince] then
  1125. AppTypeStr:='--subsystem wince'
  1126. else
  1127. begin
  1128. if apptype=app_gui then
  1129. AppTypeStr:='--subsystem windows';
  1130. end;
  1131. if apptype=app_gui then
  1132. EntryStr:='--entry=_WinMainCRTStartup'
  1133. else
  1134. EntryStr:='--entry=_mainCRTStartup';
  1135. if assigned(DLLImageBase) then
  1136. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  1137. if (cs_link_strip in aktglobalswitches) then
  1138. StripStr:='-s';
  1139. if (cs_link_map in aktglobalswitches) then
  1140. MapStr:='-Map '+maybequoted(ForceExtension(current_module.exefilename^,'.map'));
  1141. { Write used files and libraries }
  1142. WriteResponseFile(false);
  1143. { Call linker }
  1144. success:=false;
  1145. if RelocSection or (not Deffile.empty) then
  1146. cmds:=3
  1147. else
  1148. cmds:=1;
  1149. for i:=1 to cmds do
  1150. begin
  1151. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  1152. if binstr<>'' then
  1153. begin
  1154. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  1155. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1156. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1157. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  1158. Replace(cmdstr,'$ENTRY',EntryStr);
  1159. Replace(cmdstr,'$ASBIN',AsbinStr);
  1160. Replace(cmdstr,'$RELOC',RelocStr);
  1161. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  1162. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1163. Replace(cmdstr,'$STRIP',StripStr);
  1164. Replace(cmdstr,'$MAP',MapStr);
  1165. if not DefFile.Empty then
  1166. begin
  1167. DefFile.WriteFile;
  1168. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  1169. end
  1170. else
  1171. Replace(cmdstr,'$DEF','');
  1172. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  1173. if not success then
  1174. break;
  1175. end;
  1176. end;
  1177. { Post process }
  1178. if success then
  1179. success:=PostProcessExecutable(current_module.exefilename^,false);
  1180. { Remove ReponseFile }
  1181. if (success) and not(cs_link_nolink in aktglobalswitches) then
  1182. begin
  1183. RemoveFile(outputexedir+Info.ResName);
  1184. RemoveFile('base.$$$');
  1185. RemoveFile('exp.$$$');
  1186. RemoveFile('deffile.$$$');
  1187. end;
  1188. MakeExecutable:=success; { otherwise a recursive call to link method }
  1189. end;
  1190. Function TLinkerWin32.MakeSharedLibrary:boolean;
  1191. var
  1192. MapStr,
  1193. binstr : String;
  1194. cmdstr : TCmdStr;
  1195. success : boolean;
  1196. cmds,
  1197. i : longint;
  1198. AsBinStr : string[80];
  1199. StripStr,
  1200. GCSectionsStr,
  1201. RelocStr,
  1202. AppTypeStr,
  1203. EntryStr,
  1204. ImageBaseStr : string[40];
  1205. begin
  1206. MakeSharedLibrary:=false;
  1207. if not(cs_link_nolink in aktglobalswitches) then
  1208. Message1(exec_i_linking,current_module.sharedlibfilename^);
  1209. { Create some replacements }
  1210. RelocStr:='';
  1211. AppTypeStr:='';
  1212. EntryStr:='';
  1213. ImageBaseStr:='';
  1214. StripStr:='';
  1215. MapStr:='';
  1216. GCSectionsStr:='';
  1217. AsBinStr:=FindUtil(utilsprefix+'as');
  1218. if RelocSection then
  1219. RelocStr:='--base-file base.$$$';
  1220. if use_smartlink_section then
  1221. GCSectionsStr:='--gc-sections';
  1222. if apptype=app_gui then
  1223. begin
  1224. AppTypeStr:='--subsystem windows';
  1225. EntryStr:='--entry _DLLWinMainCRTStartup'
  1226. end
  1227. else
  1228. EntryStr:='--entry _DLLMainCRTStartup';
  1229. if assigned(DLLImageBase) then
  1230. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  1231. if (cs_link_strip in aktglobalswitches) then
  1232. StripStr:='-s';
  1233. if (cs_link_map in aktglobalswitches) then
  1234. MapStr:='-Map '+maybequoted(ForceExtension(current_module.exefilename^,'.map'));
  1235. { Write used files and libraries }
  1236. WriteResponseFile(true);
  1237. { Call linker }
  1238. success:=false;
  1239. if RelocSection or (not Deffile.empty) then
  1240. cmds:=3
  1241. else
  1242. cmds:=1;
  1243. for i:=1 to cmds do
  1244. begin
  1245. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  1246. if binstr<>'' then
  1247. begin
  1248. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1249. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1250. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1251. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  1252. Replace(cmdstr,'$ENTRY',EntryStr);
  1253. Replace(cmdstr,'$ASBIN',AsbinStr);
  1254. Replace(cmdstr,'$RELOC',RelocStr);
  1255. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  1256. Replace(cmdstr,'$STRIP',StripStr);
  1257. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1258. Replace(cmdstr,'$MAP',MapStr);
  1259. if not DefFile.Empty then
  1260. begin
  1261. DefFile.WriteFile;
  1262. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  1263. end
  1264. else
  1265. Replace(cmdstr,'$DEF','');
  1266. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  1267. if not success then
  1268. break;
  1269. end;
  1270. end;
  1271. { Post process }
  1272. if success then
  1273. success:=PostProcessExecutable(current_module.sharedlibfilename^,true);
  1274. { Remove ReponseFile }
  1275. if (success) and not(cs_link_nolink in aktglobalswitches) then
  1276. begin
  1277. RemoveFile(outputexedir+Info.ResName);
  1278. RemoveFile('base.$$$');
  1279. RemoveFile('exp.$$$');
  1280. RemoveFile('deffile.$$$');
  1281. end;
  1282. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1283. end;
  1284. function tlinkerwin32.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1285. type
  1286. tdosheader = packed record
  1287. e_magic : word;
  1288. e_cblp : word;
  1289. e_cp : word;
  1290. e_crlc : word;
  1291. e_cparhdr : word;
  1292. e_minalloc : word;
  1293. e_maxalloc : word;
  1294. e_ss : word;
  1295. e_sp : word;
  1296. e_csum : word;
  1297. e_ip : word;
  1298. e_cs : word;
  1299. e_lfarlc : word;
  1300. e_ovno : word;
  1301. e_res : array[0..3] of word;
  1302. e_oemid : word;
  1303. e_oeminfo : word;
  1304. e_res2 : array[0..9] of word;
  1305. e_lfanew : longint;
  1306. end;
  1307. tpeheader = packed record
  1308. PEMagic : array[0..3] of char;
  1309. Machine : word;
  1310. NumberOfSections : word;
  1311. TimeDateStamp : longint;
  1312. PointerToSymbolTable : longint;
  1313. NumberOfSymbols : longint;
  1314. SizeOfOptionalHeader : word;
  1315. Characteristics : word;
  1316. Magic : word;
  1317. MajorLinkerVersion : byte;
  1318. MinorLinkerVersion : byte;
  1319. SizeOfCode : longint;
  1320. SizeOfInitializedData : longint;
  1321. SizeOfUninitializedData : longint;
  1322. AddressOfEntryPoint : longint;
  1323. BaseOfCode : longint;
  1324. BaseOfData : longint;
  1325. ImageBase : longint;
  1326. SectionAlignment : longint;
  1327. FileAlignment : longint;
  1328. MajorOperatingSystemVersion : word;
  1329. MinorOperatingSystemVersion : word;
  1330. MajorImageVersion : word;
  1331. MinorImageVersion : word;
  1332. MajorSubsystemVersion : word;
  1333. MinorSubsystemVersion : word;
  1334. Reserved1 : longint;
  1335. SizeOfImage : longint;
  1336. SizeOfHeaders : longint;
  1337. CheckSum : longint;
  1338. Subsystem : word;
  1339. DllCharacteristics : word;
  1340. SizeOfStackReserve : longint;
  1341. SizeOfStackCommit : longint;
  1342. SizeOfHeapReserve : longint;
  1343. SizeOfHeapCommit : longint;
  1344. LoaderFlags : longint;
  1345. NumberOfRvaAndSizes : longint;
  1346. DataDirectory : array[1..$80] of byte;
  1347. end;
  1348. tcoffsechdr=packed record
  1349. name : array[0..7] of char;
  1350. vsize : longint;
  1351. rvaofs : longint;
  1352. datalen : longint;
  1353. datapos : longint;
  1354. relocpos : longint;
  1355. lineno1 : longint;
  1356. nrelocs : word;
  1357. lineno2 : word;
  1358. flags : longint;
  1359. end;
  1360. psecfill=^TSecfill;
  1361. TSecfill=record
  1362. fillpos,
  1363. fillsize : longint;
  1364. next : psecfill;
  1365. end;
  1366. var
  1367. f : file;
  1368. cmdstr : string;
  1369. dosheader : tdosheader;
  1370. peheader : tpeheader;
  1371. firstsecpos,
  1372. maxfillsize,
  1373. l,peheaderpos : longint;
  1374. coffsec : tcoffsechdr;
  1375. secroot,hsecroot : psecfill;
  1376. zerobuf : pointer;
  1377. begin
  1378. postprocessexecutable:=false;
  1379. { when -s is used or it's a dll then quit }
  1380. if (cs_link_nolink in aktglobalswitches) then
  1381. begin
  1382. case apptype of
  1383. app_native :
  1384. cmdstr:='--subsystem native';
  1385. app_gui :
  1386. cmdstr:='--subsystem gui';
  1387. app_cui :
  1388. cmdstr:='--subsystem console';
  1389. end;
  1390. if dllversion<>'' then
  1391. cmdstr:=cmdstr+' --version '+dllversion;
  1392. cmdstr:=cmdstr+' --input '+maybequoted(fn);
  1393. cmdstr:=cmdstr+' --stack '+tostr(stacksize);
  1394. DoExec(FindUtil(utilsprefix+'postw32'),cmdstr,false,false);
  1395. postprocessexecutable:=true;
  1396. exit;
  1397. end;
  1398. { open file }
  1399. assign(f,fn);
  1400. {$I-}
  1401. reset(f,1);
  1402. if ioresult<>0 then
  1403. Message1(execinfo_f_cant_open_executable,fn);
  1404. { read headers }
  1405. blockread(f,dosheader,sizeof(tdosheader));
  1406. peheaderpos:=dosheader.e_lfanew;
  1407. seek(f,peheaderpos);
  1408. blockread(f,peheader,sizeof(tpeheader));
  1409. { write info }
  1410. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  1411. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  1412. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  1413. { change stack size (PM) }
  1414. { I am not sure that the default value is adequate !! }
  1415. peheader.SizeOfStackReserve:=stacksize;
  1416. { change the header }
  1417. { sub system }
  1418. { gui=2 }
  1419. { cui=3 }
  1420. { wincegui=9 }
  1421. if target_info.system in [system_arm_wince,system_i386_wince] then
  1422. peheader.Subsystem:=9
  1423. else
  1424. case apptype of
  1425. app_native :
  1426. peheader.Subsystem:=1;
  1427. app_gui :
  1428. peheader.Subsystem:=2;
  1429. app_cui :
  1430. peheader.Subsystem:=3;
  1431. end;
  1432. if dllversion<>'' then
  1433. begin
  1434. peheader.MajorImageVersion:=dllmajor;
  1435. peheader.MinorImageVersion:=dllminor;
  1436. end;
  1437. { reset timestamp }
  1438. peheader.TimeDateStamp:=0;
  1439. { write header back }
  1440. seek(f,peheaderpos);
  1441. blockwrite(f,peheader,sizeof(tpeheader));
  1442. if ioresult<>0 then
  1443. Message1(execinfo_f_cant_process_executable,fn);
  1444. seek(f,peheaderpos);
  1445. blockread(f,peheader,sizeof(tpeheader));
  1446. { write the value after the change }
  1447. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  1448. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  1449. { read section info }
  1450. maxfillsize:=0;
  1451. firstsecpos:=0;
  1452. secroot:=nil;
  1453. for l:=1 to peheader.NumberOfSections do
  1454. begin
  1455. blockread(f,coffsec,sizeof(tcoffsechdr));
  1456. if coffsec.datapos>0 then
  1457. begin
  1458. if secroot=nil then
  1459. firstsecpos:=coffsec.datapos;
  1460. new(hsecroot);
  1461. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  1462. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  1463. hsecroot^.next:=secroot;
  1464. secroot:=hsecroot;
  1465. if secroot^.fillsize>maxfillsize then
  1466. maxfillsize:=secroot^.fillsize;
  1467. end;
  1468. end;
  1469. if firstsecpos>0 then
  1470. begin
  1471. l:=firstsecpos-filepos(f);
  1472. if l>maxfillsize then
  1473. maxfillsize:=l;
  1474. end
  1475. else
  1476. l:=0;
  1477. { get zero buffer }
  1478. getmem(zerobuf,maxfillsize);
  1479. fillchar(zerobuf^,maxfillsize,0);
  1480. { zero from sectioninfo until first section }
  1481. blockwrite(f,zerobuf^,l);
  1482. { zero section alignments }
  1483. while assigned(secroot) do
  1484. begin
  1485. seek(f,secroot^.fillpos);
  1486. blockwrite(f,zerobuf^,secroot^.fillsize);
  1487. hsecroot:=secroot;
  1488. secroot:=secroot^.next;
  1489. dispose(hsecroot);
  1490. end;
  1491. freemem(zerobuf,maxfillsize);
  1492. close(f);
  1493. {$I+}
  1494. if ioresult<>0 then;
  1495. postprocessexecutable:=true;
  1496. end;
  1497. {****************************************************************************
  1498. TDLLScannerWin32
  1499. ****************************************************************************}
  1500. procedure tDLLScannerWin32.CheckDLLFunc(const dllname,funcname:string);
  1501. var
  1502. hp : tExternalsItem;
  1503. begin
  1504. hp:=tExternalsItem(current_module.Externals.first);
  1505. while assigned(hp)do
  1506. begin
  1507. if (not hp.found) and
  1508. assigned(hp.data) and
  1509. (hp.data^=funcname) then
  1510. begin
  1511. hp.found:=true;
  1512. if not(current_module.uses_imports) then
  1513. begin
  1514. current_module.uses_imports:=true;
  1515. importlib.preparelib(current_module.modulename^);
  1516. end;
  1517. timportlibwin32(importlib).importprocedure_str(dllname,0,funcname);
  1518. importfound:=true;
  1519. exit;
  1520. end;
  1521. hp:=tExternalsItem(hp.next);
  1522. end;
  1523. end;
  1524. function tDLLScannerWin32.scan(const binname:string):boolean;
  1525. var
  1526. hs,
  1527. dllname : string;
  1528. begin
  1529. result:=false;
  1530. { is there already an import library the we will use that one }
  1531. if FindLibraryFile(binname,target_info.staticClibprefix,target_info.staticClibext,hs) then
  1532. exit;
  1533. { check if we can find the dll }
  1534. hs:=AddExtension(binname,target_info.sharedlibext);
  1535. if not FindDll(hs,dllname) then
  1536. exit;
  1537. importfound:=false;
  1538. ReadDLLImports(dllname,@CheckDLLFunc);
  1539. result:=importfound;
  1540. end;
  1541. {*****************************************************************************
  1542. Initialize
  1543. *****************************************************************************}
  1544. initialization
  1545. {$ifdef i386}
  1546. RegisterExternalLinker(system_i386_win32_info,TLinkerWin32);
  1547. RegisterInternalLinker(system_i386_win32_info,TPECoffLinker);
  1548. RegisterImport(system_i386_win32,TImportLibWin32);
  1549. RegisterExport(system_i386_win32,TExportLibWin32);
  1550. RegisterDLLScanner(system_i386_win32,TDLLScannerWin32);
  1551. RegisterRes(res_gnu_windres_info);
  1552. RegisterTarget(system_i386_win32_info);
  1553. RegisterExternalLinker(system_i386_wince_info,TLinkerWin32);
  1554. RegisterImport(system_i386_wince,TImportLibWin32);
  1555. RegisterExport(system_i386_wince,TExportLibWin32);
  1556. RegisterDLLScanner(system_i386_wince,TDLLScannerWin32);
  1557. RegisterTarget(system_i386_wince_info);
  1558. {$endif i386}
  1559. {$ifdef x86_64}
  1560. RegisterInternalLinker(system_x64_win64_info,TPECoffLinker);
  1561. RegisterImport(system_x86_64_win64,TImportLibWin32);
  1562. RegisterExport(system_x86_64_win64,TExportLibWin32);
  1563. RegisterDLLScanner(system_x86_64_win64,TDLLScannerWin32);
  1564. RegisterRes(res_gnu_windres_info);
  1565. RegisterTarget(system_x64_win64_info);
  1566. {$endif x86_64}
  1567. {$ifdef arm}
  1568. RegisterExternalLinker(system_arm_wince_info,TLinkerWin32);
  1569. RegisterInternalLinker(system_arm_wince_info,TPECoffLinker);
  1570. RegisterImport(system_arm_wince,TImportLibWin32);
  1571. RegisterExport(system_arm_wince,TExportLibWin32);
  1572. RegisterRes(res_gnu_wince_windres_info);
  1573. RegisterTarget(system_arm_wince_info);
  1574. {$endif arm}
  1575. end.