t_win.pas 68 KB

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