t_win.pas 67 KB

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