t_win.pas 66 KB

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