t_win.pas 67 KB

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