t_win.pas 69 KB

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