2
0

t_win.pas 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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. if EList_indexed=nil then
  541. EList_indexed:=tFPList.Create;
  542. if EList_nonindexed=nil then
  543. EList_nonindexed:=tFPList.Create;
  544. end;
  545. procedure TExportLibWin.exportvar(hp : texported_item);
  546. begin
  547. { same code used !! PM }
  548. exportprocedure(hp);
  549. end;
  550. var
  551. Gl_DoubleIndex:boolean;
  552. Gl_DoubleIndexValue:longint;
  553. function IdxCompare(Item1, Item2: Pointer): Integer;
  554. var
  555. I1:texported_item absolute Item1;
  556. I2:texported_item absolute Item2;
  557. begin
  558. Result:=I1.index-I2.index;
  559. if(Result=0)and(Item1<>Item2)then
  560. begin
  561. Gl_DoubleIndex:=true;
  562. Gl_DoubleIndexValue:=I1.index;
  563. end;
  564. end;
  565. procedure TExportLibWin.exportprocedure(hp : texported_item);
  566. begin
  567. if ((hp.options and eo_index)<>0)and((hp.index<=0) or (hp.index>$ffff)) then
  568. begin
  569. message1(parser_e_export_invalid_index,tostr(hp.index));
  570. exit;
  571. end;
  572. if hp.options and eo_index=eo_index then
  573. EList_indexed.Add(hp)
  574. else
  575. EList_nonindexed.Add(hp);
  576. end;
  577. procedure TExportLibWin.exportfromlist(hp : texported_item);
  578. //formerly TExportLibWin.exportprocedure
  579. { must be ordered at least for win32 !! }
  580. var
  581. hp2 : texported_item;
  582. begin
  583. hp2:=texported_item(current_module._exports.first);
  584. while assigned(hp2) and
  585. (hp.name^>hp2.name^) do
  586. hp2:=texported_item(hp2.next);
  587. { insert hp there !! }
  588. if hp2=nil then
  589. current_module._exports.concat(hp)
  590. else
  591. begin
  592. if hp2.name^=hp.name^ then
  593. begin
  594. { this is not allowed !! }
  595. message1(parser_e_export_name_double,hp.name^);
  596. exit;
  597. end;
  598. current_module._exports.insertbefore(hp,hp2);
  599. end;
  600. end;
  601. procedure TExportLibWin.generatelib;
  602. var
  603. ordinal_base,ordinal_max,ordinal_min : longint;
  604. current_index : longint;
  605. entries,named_entries : longint;
  606. name_label,dll_name_label,export_address_table : tasmlabel;
  607. export_name_table_pointers,export_ordinal_table : tasmlabel;
  608. hp,hp2 : texported_item;
  609. temtexport : TLinkedList;
  610. address_table,name_table_pointers,
  611. name_table,ordinal_table : TAsmList;
  612. i,autoindex,ni_high : longint;
  613. hole : boolean;
  614. begin
  615. Gl_DoubleIndex:=false;
  616. ELIst_indexed.Sort(@IdxCompare);
  617. if Gl_DoubleIndex then
  618. begin
  619. message1(parser_e_export_ordinal_double,tostr(Gl_DoubleIndexValue));
  620. FreeAndNil(EList_indexed);
  621. FreeAndNil(EList_nonindexed);
  622. exit;
  623. end;
  624. autoindex:=1;
  625. while EList_nonindexed.Count>0 do
  626. begin
  627. hole:=(EList_indexed.Count>0)and(texported_item(EList_indexed.Items[0]).index>1);
  628. if not hole then
  629. for i:=autoindex to pred(EList_indexed.Count)do
  630. if texported_item(EList_indexed.Items[i]).index-texported_item(EList_indexed.Items[pred(i)]).index>1 then
  631. begin
  632. autoindex:=succ(texported_item(EList_indexed.Items[pred(i)]).index);
  633. hole:=true;
  634. break;
  635. end;
  636. ni_high:=pred(EList_nonindexed.Count);
  637. if not hole then
  638. begin
  639. autoindex:=succ(EList_indexed.Count);
  640. EList_indexed.Add(EList_nonindexed.Items[ni_high]);
  641. end
  642. else
  643. EList_indexed.Insert(pred(AutoIndex),EList_nonindexed.Items[ni_high]);
  644. EList_nonindexed.Delete(ni_high);
  645. texported_item(EList_indexed.Items[pred(AutoIndex)]).index:=autoindex;
  646. end;
  647. FreeAndNil(EList_nonindexed);
  648. for i:=0 to pred(EList_indexed.Count)do
  649. exportfromlist(texported_item(EList_indexed.Items[i]));
  650. FreeAndNil(EList_indexed);
  651. if (target_asm.id in [as_i386_masm,as_i386_tasm,as_i386_nasmwin32]) then
  652. begin
  653. generatenasmlib;
  654. exit;
  655. end;
  656. hp:=texported_item(current_module._exports.first);
  657. if not assigned(hp) then
  658. exit;
  659. ordinal_max:=0;
  660. ordinal_min:=$7FFFFFFF;
  661. entries:=0;
  662. named_entries:=0;
  663. current_asmdata.getjumplabel(dll_name_label);
  664. current_asmdata.getjumplabel(export_address_table);
  665. current_asmdata.getjumplabel(export_name_table_pointers);
  666. current_asmdata.getjumplabel(export_ordinal_table);
  667. { count entries }
  668. while assigned(hp) do
  669. begin
  670. inc(entries);
  671. if (hp.index>ordinal_max) then
  672. ordinal_max:=hp.index;
  673. if (hp.index>0) and (hp.index<ordinal_min) then
  674. ordinal_min:=hp.index;
  675. if assigned(hp.name) then
  676. inc(named_entries);
  677. hp:=texported_item(hp.next);
  678. end;
  679. { no support for higher ordinal base yet !! }
  680. ordinal_base:=1;
  681. current_index:=ordinal_base;
  682. { we must also count the holes !! }
  683. entries:=ordinal_max-ordinal_base+1;
  684. new_section(current_asmdata.asmlists[al_exports],sec_edata,'',0);
  685. { create label to reference from main so smartlink will include
  686. the .edata section }
  687. current_asmdata.asmlists[al_exports].concat(Tai_symbol.Createname_global(make_mangledname('EDATA',current_module.localsymtable,''),AT_DATA,0));
  688. { export flags }
  689. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_32bit(0));
  690. { date/time stamp }
  691. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_32bit(0));
  692. { major version }
  693. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_16bit(0));
  694. { minor version }
  695. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_16bit(0));
  696. { pointer to dll name }
  697. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_rva_sym(dll_name_label));
  698. { ordinal base normally set to 1 }
  699. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_32bit(ordinal_base));
  700. { number of entries }
  701. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_32bit(entries));
  702. { number of named entries }
  703. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_32bit(named_entries));
  704. { address of export address table }
  705. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_rva_sym(export_address_table));
  706. { address of name pointer pointers }
  707. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_rva_sym(export_name_table_pointers));
  708. { address of ordinal number pointers }
  709. current_asmdata.asmlists[al_exports].concat(Tai_const.Create_rva_sym(export_ordinal_table));
  710. { the name }
  711. current_asmdata.asmlists[al_exports].concat(Tai_label.Create(dll_name_label));
  712. if st='' then
  713. current_asmdata.asmlists[al_exports].concat(Tai_string.Create(current_module.modulename^+target_info.sharedlibext+#0))
  714. else
  715. current_asmdata.asmlists[al_exports].concat(Tai_string.Create(st+target_info.sharedlibext+#0));
  716. { export address table }
  717. address_table:=TAsmList.create;
  718. address_table.concat(Tai_align.Create_op(4,0));
  719. address_table.concat(Tai_label.Create(export_address_table));
  720. name_table_pointers:=TAsmList.create;
  721. name_table_pointers.concat(Tai_align.Create_op(4,0));
  722. name_table_pointers.concat(Tai_label.Create(export_name_table_pointers));
  723. ordinal_table:=TAsmList.create;
  724. ordinal_table.concat(Tai_align.Create_op(4,0));
  725. ordinal_table.concat(Tai_label.Create(export_ordinal_table));
  726. name_table:=TAsmList.Create;
  727. name_table.concat(Tai_align.Create_op(4,0));
  728. { write each address }
  729. hp:=texported_item(current_module._exports.first);
  730. while assigned(hp) do
  731. begin
  732. if (hp.options and eo_name)<>0 then
  733. begin
  734. current_asmdata.getjumplabel(name_label);
  735. name_table_pointers.concat(Tai_const.Create_rva_sym(name_label));
  736. ordinal_table.concat(Tai_const.Create_16bit(hp.index-ordinal_base));
  737. name_table.concat(Tai_align.Create_op(2,0));
  738. name_table.concat(Tai_label.Create(name_label));
  739. name_table.concat(Tai_string.Create(hp.name^+#0));
  740. end;
  741. hp:=texported_item(hp.next);
  742. end;
  743. { order in increasing ordinal values }
  744. { into temtexport list }
  745. temtexport:=TLinkedList.Create;
  746. hp:=texported_item(current_module._exports.first);
  747. while assigned(hp) do
  748. begin
  749. current_module._exports.remove(hp);
  750. hp2:=texported_item(temtexport.first);
  751. while assigned(hp2) and (hp.index>hp2.index) do
  752. hp2:=texported_item(hp2.next);
  753. if hp2=nil then
  754. temtexport.concat(hp)
  755. else
  756. temtexport.insertbefore(hp,hp2);
  757. hp:=texported_item(current_module._exports.first);;
  758. end;
  759. { write the export adress table }
  760. current_index:=ordinal_base;
  761. hp:=texported_item(temtexport.first);
  762. while assigned(hp) do
  763. begin
  764. { fill missing values }
  765. while current_index<hp.index do
  766. begin
  767. address_table.concat(Tai_const.Create_32bit(0));
  768. inc(current_index);
  769. end;
  770. case hp.sym.typ of
  771. staticvarsym :
  772. address_table.concat(Tai_const.Createname_rva(tstaticvarsym(hp.sym).mangledname));
  773. procsym :
  774. address_table.concat(Tai_const.Createname_rva(tprocdef(tprocsym(hp.sym).ProcdefList[0]).mangledname));
  775. end;
  776. inc(current_index);
  777. hp:=texported_item(hp.next);
  778. end;
  779. current_asmdata.asmlists[al_exports].concatlist(address_table);
  780. current_asmdata.asmlists[al_exports].concatlist(name_table_pointers);
  781. current_asmdata.asmlists[al_exports].concatlist(ordinal_table);
  782. current_asmdata.asmlists[al_exports].concatlist(name_table);
  783. address_table.Free;
  784. name_table_pointers.free;
  785. ordinal_table.free;
  786. name_table.free;
  787. temtexport.free;
  788. end;
  789. procedure TExportLibWin.generatenasmlib;
  790. var
  791. hp : texported_item;
  792. p : pchar;
  793. s : string;
  794. begin
  795. new_section(current_asmdata.asmlists[al_exports],sec_code,'',0);
  796. hp:=texported_item(current_module._exports.first);
  797. while assigned(hp) do
  798. begin
  799. case hp.sym.typ of
  800. staticvarsym :
  801. s:=tstaticvarsym(hp.sym).mangledname;
  802. procsym :
  803. s:=tprocdef(tprocsym(hp.sym).ProcdefList[0]).mangledname;
  804. else
  805. s:='';
  806. end;
  807. p:=strpnew(#9+'export '+s+' '+hp.Name^+' '+tostr(hp.index));
  808. {current_asmdata.asmlists[al_exports].concat(tai_direct.create(p));}
  809. hp:=texported_item(hp.next);
  810. end;
  811. end;
  812. {****************************************************************************
  813. TInternalLinkerWin
  814. ****************************************************************************}
  815. constructor TInternalLinkerWin.Create;
  816. begin
  817. inherited Create;
  818. CExeoutput:=TPECoffexeoutput;
  819. CObjInput:=TPECoffObjInput;
  820. end;
  821. procedure TInternalLinkerWin.DefaultLinkScript;
  822. var
  823. s,s2,
  824. ibase : TCmdStr;
  825. begin
  826. with LinkScript do
  827. begin
  828. while not ObjectFiles.Empty do
  829. begin
  830. s:=ObjectFiles.GetFirst;
  831. if s<>'' then
  832. Concat('READOBJECT '+MaybeQuoted(s));
  833. end;
  834. while not StaticLibFiles.Empty do
  835. begin
  836. s:=StaticLibFiles.GetFirst;
  837. if s<>'' then
  838. Concat('READSTATICLIBRARY '+MaybeQuoted(s));
  839. end;
  840. While not SharedLibFiles.Empty do
  841. begin
  842. S:=SharedLibFiles.GetFirst;
  843. if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  844. Concat('READSTATICLIBRARY '+MaybeQuoted(s2))
  845. else
  846. Comment(V_Error,'Import library not found for '+S);
  847. end;
  848. if IsSharedLibrary then
  849. begin
  850. Concat('ISSHAREDLIBRARY');
  851. if apptype=app_gui then
  852. Concat('ENTRYNAME _DLLWinMainCRTStartup')
  853. else
  854. Concat('ENTRYNAME _DLLMainCRTStartup');
  855. end
  856. else
  857. begin
  858. if apptype=app_gui then
  859. Concat('ENTRYNAME _WinMainCRTStartup')
  860. else
  861. Concat('ENTRYNAME _mainCRTStartup');
  862. end;
  863. ibase:='';
  864. if assigned(DLLImageBase) then
  865. ibase:=DLLImageBase^
  866. else
  867. begin
  868. if IsSharedLibrary then
  869. ibase:='10000000'
  870. else
  871. if target_info.system in [system_arm_wince] then
  872. ibase:='10000'
  873. else
  874. ibase:='400000';
  875. end;
  876. Concat('IMAGEBASE $' + ibase);
  877. Concat('HEADER');
  878. Concat('EXESECTION .text');
  879. Concat(' OBJSECTION .text*');
  880. Concat(' SYMBOL ___CTOR_LIST__');
  881. Concat(' SYMBOL __CTOR_LIST__');
  882. Concat(' LONG -1');
  883. Concat(' OBJSECTION .ctor*');
  884. Concat(' LONG 0');
  885. Concat(' SYMBOL ___DTOR_LIST__');
  886. Concat(' SYMBOL __DTOR_LIST__');
  887. Concat(' LONG -1');
  888. Concat(' OBJSECTION .dtor*');
  889. Concat(' LONG 0');
  890. Concat(' SYMBOL etext');
  891. Concat('ENDEXESECTION');
  892. Concat('EXESECTION .data');
  893. Concat(' SYMBOL __data_start__');
  894. Concat(' OBJSECTION .data*');
  895. Concat(' OBJSECTION .fpc*');
  896. Concat(' SYMBOL edata');
  897. Concat(' SYMBOL __data_end__');
  898. Concat('ENDEXESECTION');
  899. Concat('EXESECTION .rdata');
  900. Concat(' SYMBOL ___RUNTIME_PSEUDO_RELOC_LIST__');
  901. Concat(' SYMBOL __RUNTIME_PSEUDO_RELOC_LIST__');
  902. Concat(' OBJSECTION .rdata_runtime_pseudo_reloc');
  903. Concat(' SYMBOL ___RUNTIME_PSEUDO_RELOC_LIST_END__');
  904. Concat(' SYMBOL __RUNTIME_PSEUDO_RELOC_LIST_END__');
  905. Concat(' OBJSECTION .rdata*');
  906. Concat(' OBJSECTION .rodata*');
  907. Concat('ENDEXESECTION');
  908. Concat('EXESECTION .pdata');
  909. Concat(' OBJSECTION .pdata');
  910. Concat('ENDEXESECTION');
  911. Concat('EXESECTION .bss');
  912. Concat(' SYMBOL __bss_start__');
  913. Concat(' OBJSECTION .bss*');
  914. Concat(' SYMBOL __bss_end__');
  915. Concat('ENDEXESECTION');
  916. Concat('EXESECTION .idata');
  917. Concat(' OBJSECTION .idata$2*');
  918. Concat(' OBJSECTION .idata$3*');
  919. Concat(' ZEROS 20');
  920. Concat(' OBJSECTION .idata$4*');
  921. Concat(' OBJSECTION .idata$5*');
  922. Concat(' OBJSECTION .idata$6*');
  923. Concat(' OBJSECTION .idata$7*');
  924. Concat('ENDEXESECTION');
  925. Concat('EXESECTION .edata');
  926. Concat(' OBJSECTION .edata*');
  927. Concat('ENDEXESECTION');
  928. Concat('EXESECTION .rsrc');
  929. Concat(' OBJSECTION .rsrc*');
  930. Concat('ENDEXESECTION');
  931. Concat('EXESECTION .reloc');
  932. Concat(' OBJSECTION .reloc');
  933. Concat('ENDEXESECTION');
  934. Concat('EXESECTION .stab');
  935. Concat(' OBJSECTION .stab');
  936. Concat('ENDEXESECTION');
  937. Concat('EXESECTION .stabstr');
  938. Concat(' OBJSECTION .stabstr');
  939. Concat('ENDEXESECTION');
  940. Concat('STABS');
  941. Concat('SYMBOLS');
  942. end;
  943. end;
  944. procedure TInternalLinkerWin.InitSysInitUnitName;
  945. begin
  946. if target_info.system=system_i386_win32 then
  947. GlobalInitSysInitUnitName(self);
  948. end;
  949. {****************************************************************************
  950. TExternalLinkerWin
  951. ****************************************************************************}
  952. Constructor TExternalLinkerWin.Create;
  953. begin
  954. Inherited Create;
  955. { allow duplicated libs (PM) }
  956. SharedLibFiles.doubles:=true;
  957. StaticLibFiles.doubles:=true;
  958. end;
  959. Procedure TExternalLinkerWin.SetDefaultInfo;
  960. var
  961. targetopts: string;
  962. begin
  963. with Info do
  964. begin
  965. if target_info.system=system_arm_wince then
  966. targetopts:='-m armpe'
  967. else
  968. targetopts:='-b pe-i386 -m i386pe';
  969. ExeCmd[1]:='ld '+targetopts+' $OPT $GCSECTIONS $MAP $STRIP $APPTYPE $ENTRY $IMAGEBASE $RELOC -o $EXE $RES';
  970. DllCmd[1]:='ld '+targetopts+' $OPT $GCSECTIONS $MAP $STRIP --dll $APPTYPE $ENTRY $IMAGEBASE $RELOC -o $EXE $RES';
  971. { ExeCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF';
  972. use short forms to avoid 128 char limitation problem }
  973. ExeCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  974. ExeCmd[3]:='ld '+targetopts+' $OPT $STRIP $APPTYPE $ENTRY $IMAGEBASE -o $EXE $RES exp.$$$';
  975. { DllCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF'; }
  976. DllCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  977. DllCmd[3]:='ld '+targetopts+' $OPT $STRIP --dll $APPTYPE $ENTRY $IMAGEBASE -o $EXE $RES exp.$$$';
  978. end;
  979. end;
  980. Function TExternalLinkerWin.WriteResponseFile(isdll:boolean) : Boolean;
  981. Var
  982. linkres : TLinkRes;
  983. HPath : TCmdStrListItem;
  984. s,s2 : TCmdStr;
  985. i : integer;
  986. begin
  987. WriteResponseFile:=False;
  988. if (cs_profile in current_settings.moduleswitches) then
  989. begin
  990. SharedLibFiles.Concat('gmon');
  991. SharedLibFiles.Concat('c');
  992. SharedLibFiles.Concat('gcc');
  993. SharedLibFiles.Concat('kernel32');
  994. end;
  995. { Open link.res file }
  996. LinkRes:=TLinkres.Create(outputexedir+Info.ResName);
  997. with linkres do
  998. begin
  999. { Write path to search libraries }
  1000. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  1001. while assigned(HPath) do
  1002. begin
  1003. Add('SEARCH_DIR('+MaybeQuoted(HPath.Str)+')');
  1004. HPath:=TCmdStrListItem(HPath.Next);
  1005. end;
  1006. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  1007. while assigned(HPath) do
  1008. begin
  1009. Add('SEARCH_DIR('+MaybeQuoted(HPath.Str)+')');
  1010. HPath:=TCmdStrListItem(HPath.Next);
  1011. end;
  1012. { add objectfiles, start with prt0 always }
  1013. { profiling of shared libraries is currently not supported }
  1014. if not ObjectFiles.Empty then
  1015. begin
  1016. Add('INPUT(');
  1017. while not ObjectFiles.Empty do
  1018. begin
  1019. s:=ObjectFiles.GetFirst;
  1020. if s<>'' then
  1021. AddFileName(MaybeQuoted(s));
  1022. end;
  1023. Add(')');
  1024. end;
  1025. { Write staticlibraries }
  1026. if (not StaticLibFiles.Empty) then
  1027. begin
  1028. Add('GROUP(');
  1029. While not StaticLibFiles.Empty do
  1030. begin
  1031. S:=StaticLibFiles.GetFirst;
  1032. AddFileName(MaybeQuoted(s));
  1033. end;
  1034. Add(')');
  1035. end;
  1036. { Write sharedlibraries (=import libraries) }
  1037. if not SharedLibFiles.Empty then
  1038. begin
  1039. Add('INPUT(') ;
  1040. While not SharedLibFiles.Empty do
  1041. begin
  1042. S:=SharedLibFiles.GetFirst;
  1043. if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  1044. begin
  1045. Add(MaybeQuoted(s2));
  1046. continue;
  1047. end;
  1048. if pos(target_info.sharedlibprefix,s)=1 then
  1049. s:=copy(s,length(target_info.sharedlibprefix)+1,255);
  1050. i:=Pos(target_info.sharedlibext,S);
  1051. if i>0 then
  1052. Delete(S,i,255);
  1053. Add('-l'+s);
  1054. end;
  1055. Add(')');
  1056. end;
  1057. Add('SEARCH_DIR("/usr/i686-pc-cygwin/lib"); SEARCH_DIR("/usr/lib"); SEARCH_DIR("/usr/lib/w32api");');
  1058. Add('OUTPUT_FORMAT(pei-i386)');
  1059. Add('ENTRY(_mainCRTStartup)');
  1060. Add('SECTIONS');
  1061. Add('{');
  1062. Add(' . = SIZEOF_HEADERS;');
  1063. Add(' . = ALIGN(__section_alignment__);');
  1064. Add(' .text __image_base__ + ( __section_alignment__ < 0x1000 ? . : __section_alignment__ ) :');
  1065. Add(' {');
  1066. Add(' *(.init)');
  1067. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  1068. Add(' *(SORT(.text$*))');
  1069. Add(' ___CTOR_LIST__ = .; __CTOR_LIST__ = . ;');
  1070. Add(' LONG (-1);*(.ctors); *(.ctor); *(SORT(.ctors.*)); LONG (0);');
  1071. Add(' ___DTOR_LIST__ = .; __DTOR_LIST__ = . ;');
  1072. Add(' LONG (-1); *(.dtors); *(.dtor); *(SORT(.dtors.*)); LONG (0);');
  1073. Add(' *(.fini)');
  1074. Add(' PROVIDE (etext = .);');
  1075. Add(' *(.gcc_except_table)');
  1076. Add(' }');
  1077. Add(' .data BLOCK(__section_alignment__) :');
  1078. Add(' {');
  1079. Add(' __data_start__ = . ;');
  1080. add(' *(.data .data.* .gnu.linkonce.d.* .fpc*)');
  1081. Add(' *(.data2)');
  1082. Add(' *(SORT(.data$*))');
  1083. Add(' __data_end__ = . ;');
  1084. Add(' *(.data_cygwin_nocopy)');
  1085. Add(' }');
  1086. Add(' .rdata BLOCK(__section_alignment__) :');
  1087. Add(' {');
  1088. Add(' *(.rdata)');
  1089. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  1090. Add(' *(SORT(.rdata$*))');
  1091. Add(' *(.eh_frame)');
  1092. Add(' ___RUNTIME_PSEUDO_RELOC_LIST__ = .;');
  1093. Add(' __RUNTIME_PSEUDO_RELOC_LIST__ = .;');
  1094. Add(' *(.rdata_runtime_pseudo_reloc)');
  1095. Add(' ___RUNTIME_PSEUDO_RELOC_LIST_END__ = .;');
  1096. Add(' __RUNTIME_PSEUDO_RELOC_LIST_END__ = .;');
  1097. Add(' }');
  1098. Add(' .pdata BLOCK(__section_alignment__) : { *(.pdata) }');
  1099. Add(' .bss BLOCK(__section_alignment__) :');
  1100. Add(' {');
  1101. Add(' __bss_start__ = . ;');
  1102. Add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  1103. Add(' *(SORT(.bss$*))');
  1104. Add(' *(COMMON)');
  1105. Add(' __bss_end__ = . ;');
  1106. Add(' }');
  1107. Add(' .edata BLOCK(__section_alignment__) : { *(.edata) }');
  1108. Add(' .idata BLOCK(__section_alignment__) :');
  1109. Add(' {');
  1110. Add(' SORT(*)(.idata$2)');
  1111. Add(' SORT(*)(.idata$3)');
  1112. Add(' /* These zeroes mark the end of the import list. */');
  1113. Add(' LONG (0); LONG (0); LONG (0); LONG (0); LONG (0);');
  1114. Add(' SORT(*)(.idata$4)');
  1115. Add(' SORT(*)(.idata$5)');
  1116. Add(' SORT(*)(.idata$6)');
  1117. Add(' SORT(*)(.idata$7)');
  1118. Add(' }');
  1119. Add(' .CRT BLOCK(__section_alignment__) :');
  1120. Add(' {');
  1121. Add(' ___crt_xc_start__ = . ;');
  1122. Add(' *(SORT(.CRT$XC*)) /* C initialization */');
  1123. Add(' ___crt_xc_end__ = . ;');
  1124. Add(' ___crt_xi_start__ = . ;');
  1125. Add(' *(SORT(.CRT$XI*)) /* C++ initialization */');
  1126. Add(' ___crt_xi_end__ = . ;');
  1127. Add(' ___crt_xl_start__ = . ;');
  1128. Add(' *(SORT(.CRT$XL*)) /* TLS callbacks */');
  1129. Add(' /* ___crt_xl_end__ is defined in the TLS Directory support code */');
  1130. Add(' ___crt_xp_start__ = . ;');
  1131. Add(' *(SORT(.CRT$XP*)) /* Pre-termination */');
  1132. Add(' ___crt_xp_end__ = . ;');
  1133. Add(' ___crt_xt_start__ = . ;');
  1134. Add(' *(SORT(.CRT$XT*)) /* Termination */');
  1135. Add(' ___crt_xt_end__ = . ;');
  1136. Add(' }');
  1137. Add(' .tls BLOCK(__section_alignment__) :');
  1138. Add(' {');
  1139. Add(' ___tls_start__ = . ;');
  1140. Add(' *(.tls .tls.*)');
  1141. Add(' *(.tls$)');
  1142. Add(' *(SORT(.tls$*))');
  1143. Add(' ___tls_end__ = . ;');
  1144. Add(' }');
  1145. Add(' .rsrc BLOCK(__section_alignment__) :');
  1146. Add(' {');
  1147. Add(' *(.rsrc)');
  1148. Add(' *(SORT(.rsrc$*))');
  1149. Add(' }');
  1150. Add(' .reloc BLOCK(__section_alignment__) : { *(.reloc) }');
  1151. Add(' .stab BLOCK(__section_alignment__) (NOLOAD) : { *(.stab) }');
  1152. Add(' .stabstr BLOCK(__section_alignment__) (NOLOAD) : { *(.stabstr) }');
  1153. Add(' .debug_aranges BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_aranges) }');
  1154. Add(' .debug_pubnames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_pubnames) }');
  1155. Add(' .debug_info BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  1156. Add(' .debug_abbrev BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_abbrev) }');
  1157. Add(' .debug_line BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_line) }');
  1158. Add(' .debug_frame BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_frame) }');
  1159. Add(' .debug_str BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_str) }');
  1160. Add(' .debug_loc BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_loc) }');
  1161. Add(' .debug_macinfo BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_macinfo) }');
  1162. Add(' .debug_weaknames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_weaknames) }');
  1163. Add(' .debug_funcnames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_funcnames) }');
  1164. Add(' .debug_typenames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_typenames) }');
  1165. Add(' .debug_varnames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_varnames) }');
  1166. Add(' .debug_ranges BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_ranges) }');
  1167. Add('}');
  1168. { Write and Close response }
  1169. writetodisk;
  1170. Free;
  1171. end;
  1172. WriteResponseFile:=True;
  1173. end;
  1174. function TExternalLinkerWin.MakeExecutable:boolean;
  1175. var
  1176. MapStr,
  1177. binstr : String;
  1178. cmdstr : TCmdStr;
  1179. success : boolean;
  1180. cmds,i : longint;
  1181. AsBinStr : string[80];
  1182. GCSectionsStr,
  1183. StripStr,
  1184. RelocStr,
  1185. AppTypeStr,
  1186. EntryStr,
  1187. ImageBaseStr : string[40];
  1188. begin
  1189. if not(cs_link_nolink in current_settings.globalswitches) then
  1190. Message1(exec_i_linking,current_module.exefilename^);
  1191. { Create some replacements }
  1192. RelocStr:='';
  1193. AppTypeStr:='';
  1194. EntryStr:='';
  1195. ImageBaseStr:='';
  1196. StripStr:='';
  1197. MapStr:='';
  1198. GCSectionsStr:='';
  1199. AsBinStr:=FindUtil(utilsprefix+'as');
  1200. if RelocSection then
  1201. RelocStr:='--base-file base.$$$';
  1202. if use_smartlink_section then
  1203. GCSectionsStr:='--gc-sections';
  1204. if target_info.system in [system_arm_wince,system_i386_wince] then
  1205. AppTypeStr:='--subsystem wince'
  1206. else
  1207. begin
  1208. if apptype=app_gui then
  1209. AppTypeStr:='--subsystem windows';
  1210. end;
  1211. if apptype=app_gui then
  1212. EntryStr:='--entry=_WinMainCRTStartup'
  1213. else
  1214. EntryStr:='--entry=_mainCRTStartup';
  1215. if assigned(DLLImageBase) then
  1216. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  1217. if (cs_link_strip in current_settings.globalswitches) then
  1218. StripStr:='-s';
  1219. if (cs_link_map in current_settings.globalswitches) then
  1220. MapStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  1221. { Write used files and libraries }
  1222. WriteResponseFile(false);
  1223. { Call linker }
  1224. success:=false;
  1225. if RelocSection or (not Deffile.empty) then
  1226. cmds:=3
  1227. else
  1228. cmds:=1;
  1229. for i:=1 to cmds do
  1230. begin
  1231. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  1232. if binstr<>'' then
  1233. begin
  1234. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  1235. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1236. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1237. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  1238. Replace(cmdstr,'$ENTRY',EntryStr);
  1239. Replace(cmdstr,'$ASBIN',AsbinStr);
  1240. Replace(cmdstr,'$RELOC',RelocStr);
  1241. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  1242. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1243. Replace(cmdstr,'$STRIP',StripStr);
  1244. Replace(cmdstr,'$MAP',MapStr);
  1245. if not DefFile.Empty then
  1246. begin
  1247. DefFile.WriteFile;
  1248. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  1249. end
  1250. else
  1251. Replace(cmdstr,'$DEF','');
  1252. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  1253. if not success then
  1254. break;
  1255. end;
  1256. end;
  1257. { Post process }
  1258. if success then
  1259. success:=PostProcessExecutable(current_module.exefilename^,false);
  1260. { Remove ReponseFile }
  1261. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1262. begin
  1263. DeleteFile(outputexedir+Info.ResName);
  1264. DeleteFile('base.$$$');
  1265. DeleteFile('exp.$$$');
  1266. DeleteFile('deffile.$$$');
  1267. end;
  1268. MakeExecutable:=success; { otherwise a recursive call to link method }
  1269. end;
  1270. Function TExternalLinkerWin.MakeSharedLibrary:boolean;
  1271. var
  1272. MapStr,
  1273. binstr : String;
  1274. cmdstr : TCmdStr;
  1275. success : boolean;
  1276. cmds,
  1277. i : longint;
  1278. AsBinStr : string[80];
  1279. StripStr,
  1280. GCSectionsStr,
  1281. RelocStr,
  1282. AppTypeStr,
  1283. EntryStr,
  1284. ImageBaseStr : string[40];
  1285. begin
  1286. MakeSharedLibrary:=false;
  1287. if not(cs_link_nolink in current_settings.globalswitches) then
  1288. Message1(exec_i_linking,current_module.sharedlibfilename^);
  1289. { Create some replacements }
  1290. RelocStr:='';
  1291. AppTypeStr:='';
  1292. EntryStr:='';
  1293. ImageBaseStr:='';
  1294. StripStr:='';
  1295. MapStr:='';
  1296. GCSectionsStr:='';
  1297. AsBinStr:=FindUtil(utilsprefix+'as');
  1298. if RelocSection then
  1299. RelocStr:='--base-file base.$$$';
  1300. if use_smartlink_section then
  1301. GCSectionsStr:='--gc-sections';
  1302. if apptype=app_gui then
  1303. begin
  1304. AppTypeStr:='--subsystem windows';
  1305. EntryStr:='--entry _DLLWinMainCRTStartup'
  1306. end
  1307. else
  1308. EntryStr:='--entry _DLLMainCRTStartup';
  1309. if assigned(DLLImageBase) then
  1310. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  1311. if (cs_link_strip in current_settings.globalswitches) then
  1312. StripStr:='-s';
  1313. if (cs_link_map in current_settings.globalswitches) then
  1314. MapStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  1315. { Write used files and libraries }
  1316. WriteResponseFile(true);
  1317. { Call linker }
  1318. success:=false;
  1319. if RelocSection or (not Deffile.empty) then
  1320. cmds:=3
  1321. else
  1322. cmds:=1;
  1323. for i:=1 to cmds do
  1324. begin
  1325. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  1326. if binstr<>'' then
  1327. begin
  1328. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1329. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1330. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1331. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  1332. Replace(cmdstr,'$ENTRY',EntryStr);
  1333. Replace(cmdstr,'$ASBIN',AsbinStr);
  1334. Replace(cmdstr,'$RELOC',RelocStr);
  1335. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  1336. Replace(cmdstr,'$STRIP',StripStr);
  1337. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1338. Replace(cmdstr,'$MAP',MapStr);
  1339. if not DefFile.Empty then
  1340. begin
  1341. DefFile.WriteFile;
  1342. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  1343. end
  1344. else
  1345. Replace(cmdstr,'$DEF','');
  1346. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  1347. if not success then
  1348. break;
  1349. end;
  1350. end;
  1351. { Post process }
  1352. if success then
  1353. success:=PostProcessExecutable(current_module.sharedlibfilename^,true);
  1354. { Remove ReponseFile }
  1355. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1356. begin
  1357. DeleteFile(outputexedir+Info.ResName);
  1358. DeleteFile('base.$$$');
  1359. DeleteFile('exp.$$$');
  1360. DeleteFile('deffile.$$$');
  1361. end;
  1362. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1363. end;
  1364. function TExternalLinkerWin.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1365. type
  1366. tdosheader = packed record
  1367. e_magic : word;
  1368. e_cblp : word;
  1369. e_cp : word;
  1370. e_crlc : word;
  1371. e_cparhdr : word;
  1372. e_minalloc : word;
  1373. e_maxalloc : word;
  1374. e_ss : word;
  1375. e_sp : word;
  1376. e_csum : word;
  1377. e_ip : word;
  1378. e_cs : word;
  1379. e_lfarlc : word;
  1380. e_ovno : word;
  1381. e_res : array[0..3] of word;
  1382. e_oemid : word;
  1383. e_oeminfo : word;
  1384. e_res2 : array[0..9] of word;
  1385. e_lfanew : longint;
  1386. end;
  1387. psecfill=^TSecfill;
  1388. TSecfill=record
  1389. fillpos,
  1390. fillsize : longint;
  1391. next : psecfill;
  1392. end;
  1393. var
  1394. f : file;
  1395. cmdstr : string;
  1396. dosheader : tdosheader;
  1397. peheader : tcoffheader;
  1398. peoptheader : tcoffpeoptheader;
  1399. firstsecpos,
  1400. maxfillsize,
  1401. l,peheaderpos : longint;
  1402. coffsec : tcoffsechdr;
  1403. secroot,hsecroot : psecfill;
  1404. zerobuf : pointer;
  1405. begin
  1406. postprocessexecutable:=false;
  1407. { when -s is used or it's a dll then quit }
  1408. if (cs_link_nolink in current_settings.globalswitches) then
  1409. begin
  1410. case apptype of
  1411. app_native :
  1412. cmdstr:='--subsystem native';
  1413. app_gui :
  1414. cmdstr:='--subsystem gui';
  1415. app_cui :
  1416. cmdstr:='--subsystem console';
  1417. end;
  1418. if dllversion<>'' then
  1419. cmdstr:=cmdstr+' --version '+dllversion;
  1420. cmdstr:=cmdstr+' --input '+maybequoted(fn);
  1421. cmdstr:=cmdstr+' --stack '+tostr(stacksize);
  1422. DoExec(FindUtil(utilsprefix+'postw32'),cmdstr,false,false);
  1423. postprocessexecutable:=true;
  1424. exit;
  1425. end;
  1426. { open file }
  1427. assign(f,fn);
  1428. {$I-}
  1429. reset(f,1);
  1430. if ioresult<>0 then
  1431. Message1(execinfo_f_cant_open_executable,fn);
  1432. { read headers }
  1433. blockread(f,dosheader,sizeof(tdosheader));
  1434. peheaderpos:=dosheader.e_lfanew;
  1435. { skip to headerpos and skip pe magic }
  1436. seek(f,peheaderpos+4);
  1437. blockread(f,peheader,sizeof(tcoffheader));
  1438. blockread(f,peoptheader,sizeof(tcoffpeoptheader));
  1439. { write info }
  1440. Message1(execinfo_x_codesize,tostr(peoptheader.tsize));
  1441. Message1(execinfo_x_initdatasize,tostr(peoptheader.dsize));
  1442. Message1(execinfo_x_uninitdatasize,tostr(peoptheader.bsize));
  1443. { change stack size (PM) }
  1444. { I am not sure that the default value is adequate !! }
  1445. peoptheader.SizeOfStackReserve:=stacksize;
  1446. if SetPEFlagsSetExplicity then
  1447. peoptheader.LoaderFlags:=peflags;
  1448. if ImageBaseSetExplicity then
  1449. peoptheader.ImageBase:=imagebase;
  1450. if MinStackSizeSetExplicity then
  1451. peoptheader.SizeOfStackCommit:=minstacksize;
  1452. if MaxStackSizeSetExplicity then
  1453. peoptheader.SizeOfStackReserve:=maxstacksize;
  1454. { change the header }
  1455. { sub system }
  1456. { gui=2 }
  1457. { cui=3 }
  1458. { wincegui=9 }
  1459. if target_info.system in [system_arm_wince,system_i386_wince] then
  1460. peoptheader.Subsystem:=9
  1461. else
  1462. case apptype of
  1463. app_native :
  1464. peoptheader.Subsystem:=1;
  1465. app_gui :
  1466. peoptheader.Subsystem:=2;
  1467. app_cui :
  1468. peoptheader.Subsystem:=3;
  1469. end;
  1470. if dllversion<>'' then
  1471. begin
  1472. peoptheader.MajorImageVersion:=dllmajor;
  1473. peoptheader.MinorImageVersion:=dllminor;
  1474. end;
  1475. { reset timestamp }
  1476. peheader.time:=0;
  1477. { write header back, skip pe magic }
  1478. seek(f,peheaderpos+4);
  1479. blockwrite(f,peheader,sizeof(tcoffheader));
  1480. if ioresult<>0 then
  1481. Message1(execinfo_f_cant_process_executable,fn);
  1482. blockwrite(f,peoptheader,sizeof(tcoffpeoptheader));
  1483. if ioresult<>0 then
  1484. Message1(execinfo_f_cant_process_executable,fn);
  1485. { skip to headerpos and skip pe magic }
  1486. seek(f,peheaderpos+4);
  1487. blockread(f,peheader,sizeof(tcoffheader));
  1488. blockread(f,peoptheader,sizeof(tcoffpeoptheader));
  1489. { write the value after the change }
  1490. Message1(execinfo_x_stackreserve,tostr(peoptheader.SizeOfStackReserve));
  1491. Message1(execinfo_x_stackcommit,tostr(peoptheader.SizeOfStackCommit));
  1492. { read section info }
  1493. maxfillsize:=0;
  1494. firstsecpos:=0;
  1495. secroot:=nil;
  1496. for l:=1 to peheader.nsects do
  1497. begin
  1498. blockread(f,coffsec,sizeof(tcoffsechdr));
  1499. if coffsec.datapos>0 then
  1500. begin
  1501. if secroot=nil then
  1502. firstsecpos:=coffsec.datapos;
  1503. new(hsecroot);
  1504. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  1505. hsecroot^.fillsize:=coffsec.datasize-coffsec.vsize;
  1506. hsecroot^.next:=secroot;
  1507. secroot:=hsecroot;
  1508. if secroot^.fillsize>maxfillsize then
  1509. maxfillsize:=secroot^.fillsize;
  1510. end;
  1511. end;
  1512. if firstsecpos>0 then
  1513. begin
  1514. l:=firstsecpos-filepos(f);
  1515. if l>maxfillsize then
  1516. maxfillsize:=l;
  1517. end
  1518. else
  1519. l:=0;
  1520. { get zero buffer }
  1521. getmem(zerobuf,maxfillsize);
  1522. fillchar(zerobuf^,maxfillsize,0);
  1523. { zero from sectioninfo until first section }
  1524. blockwrite(f,zerobuf^,l);
  1525. { zero section alignments }
  1526. while assigned(secroot) do
  1527. begin
  1528. seek(f,secroot^.fillpos);
  1529. blockwrite(f,zerobuf^,secroot^.fillsize);
  1530. hsecroot:=secroot;
  1531. secroot:=secroot^.next;
  1532. dispose(hsecroot);
  1533. end;
  1534. freemem(zerobuf,maxfillsize);
  1535. close(f);
  1536. {$I+}
  1537. if ioresult<>0 then;
  1538. postprocessexecutable:=true;
  1539. end;
  1540. procedure TExternalLinkerWin.InitSysInitUnitName;
  1541. begin
  1542. if target_info.system=system_i386_win32 then
  1543. GlobalInitSysInitUnitName(self);
  1544. end;
  1545. {****************************************************************************
  1546. TDLLScannerWin
  1547. ****************************************************************************}
  1548. procedure TDLLScannerWin.CheckDLLFunc(const dllname,funcname:string);
  1549. var
  1550. i : longint;
  1551. ExtName : string;
  1552. begin
  1553. for i:=0 to current_module.dllscannerinputlist.count-1 do
  1554. begin
  1555. ExtName:=current_module.dllscannerinputlist.NameOfIndex(i);
  1556. if (ExtName=funcname) then
  1557. begin
  1558. current_module.AddExternalImport(dllname,funcname,0,false);
  1559. importfound:=true;
  1560. current_module.dllscannerinputlist.Delete(i);
  1561. exit;
  1562. end;
  1563. end;
  1564. end;
  1565. function TDLLScannerWin.scan(const binname:string):boolean;
  1566. var
  1567. hs,
  1568. dllname : TCmdStr;
  1569. begin
  1570. result:=false;
  1571. { is there already an import library the we will use that one }
  1572. if FindLibraryFile(binname,target_info.staticClibprefix,target_info.staticClibext,hs) then
  1573. exit;
  1574. { check if we can find the dll }
  1575. hs:=binname;
  1576. if ExtractFileExt(hs)='' then
  1577. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1578. if not FindDll(hs,dllname) then
  1579. exit;
  1580. importfound:=false;
  1581. ReadDLLImports(dllname,@CheckDLLFunc);
  1582. if importfound then
  1583. current_module.dllscannerinputlist.Pack;
  1584. result:=importfound;
  1585. end;
  1586. {*****************************************************************************
  1587. Initialize
  1588. *****************************************************************************}
  1589. initialization
  1590. {$ifdef i386}
  1591. { Win32 }
  1592. RegisterExternalLinker(system_i386_win32_info,TExternalLinkerWin);
  1593. RegisterInternalLinker(system_i386_win32_info,TInternalLinkerWin);
  1594. RegisterImport(system_i386_win32,TImportLibWin);
  1595. RegisterExport(system_i386_win32,TExportLibWin);
  1596. RegisterDLLScanner(system_i386_win32,TDLLScannerWin);
  1597. RegisterRes(res_gnu_windres_info);
  1598. RegisterTarget(system_i386_win32_info);
  1599. { WinCE }
  1600. RegisterExternalLinker(system_i386_wince_info,TExternalLinkerWin);
  1601. RegisterInternalLinker(system_i386_wince_info,TInternalLinkerWin);
  1602. RegisterImport(system_i386_wince,TImportLibWin);
  1603. RegisterExport(system_i386_wince,TExportLibWin);
  1604. RegisterDLLScanner(system_i386_wince,TDLLScannerWin);
  1605. RegisterTarget(system_i386_wince_info);
  1606. {$endif i386}
  1607. {$ifdef x86_64}
  1608. RegisterInternalLinker(system_x64_win64_info,TInternalLinkerWin);
  1609. RegisterImport(system_x86_64_win64,TImportLibWin);
  1610. RegisterExport(system_x86_64_win64,TExportLibWin);
  1611. RegisterDLLScanner(system_x86_64_win64,TDLLScannerWin);
  1612. RegisterRes(res_gnu_windres_info);
  1613. RegisterTarget(system_x64_win64_info);
  1614. {$endif x86_64}
  1615. {$ifdef arm}
  1616. RegisterExternalLinker(system_arm_wince_info,TExternalLinkerWin);
  1617. RegisterInternalLinker(system_arm_wince_info,TInternalLinkerWin);
  1618. RegisterImport(system_arm_wince,TImportLibWin);
  1619. RegisterExport(system_arm_wince,TExportLibWin);
  1620. RegisterRes(res_gnu_wince_windres_info);
  1621. RegisterTarget(system_arm_wince_info);
  1622. {$endif arm}
  1623. end.