t_win.pas 68 KB

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