t_win.pas 67 KB

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