t_win.pas 67 KB

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