t_win.pas 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  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. secname,
  829. secnames : string;
  830. begin
  831. with LinkScript do
  832. begin
  833. while not ObjectFiles.Empty do
  834. begin
  835. s:=ObjectFiles.GetFirst;
  836. if s<>'' then
  837. Concat('READOBJECT '+MaybeQuoted(s));
  838. end;
  839. while not StaticLibFiles.Empty do
  840. begin
  841. s:=StaticLibFiles.GetFirst;
  842. if s<>'' then
  843. Concat('READSTATICLIBRARY '+MaybeQuoted(s));
  844. end;
  845. While not SharedLibFiles.Empty do
  846. begin
  847. S:=SharedLibFiles.GetFirst;
  848. if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  849. Concat('READSTATICLIBRARY '+MaybeQuoted(s2))
  850. else
  851. Comment(V_Error,'Import library not found for '+S);
  852. end;
  853. if IsSharedLibrary then
  854. begin
  855. Concat('ISSHAREDLIBRARY');
  856. if apptype=app_gui then
  857. Concat('ENTRYNAME _DLLWinMainCRTStartup')
  858. else
  859. Concat('ENTRYNAME _DLLMainCRTStartup');
  860. end
  861. else
  862. begin
  863. if apptype=app_gui then
  864. Concat('ENTRYNAME _WinMainCRTStartup')
  865. else
  866. Concat('ENTRYNAME _mainCRTStartup');
  867. end;
  868. ibase:='';
  869. if assigned(DLLImageBase) then
  870. ibase:=DLLImageBase^
  871. else
  872. begin
  873. if IsSharedLibrary then
  874. ibase:='10000000'
  875. else
  876. if target_info.system in [system_arm_wince] then
  877. ibase:='10000'
  878. else
  879. ibase:='400000';
  880. end;
  881. Concat('IMAGEBASE $' + ibase);
  882. Concat('HEADER');
  883. Concat('EXESECTION .text');
  884. Concat(' SYMBOL __text_start__');
  885. Concat(' OBJSECTION .text*');
  886. Concat(' SYMBOL ___CTOR_LIST__');
  887. Concat(' SYMBOL __CTOR_LIST__');
  888. Concat(' LONG -1');
  889. Concat(' OBJSECTION .ctor*');
  890. Concat(' LONG 0');
  891. Concat(' SYMBOL ___DTOR_LIST__');
  892. Concat(' SYMBOL __DTOR_LIST__');
  893. Concat(' LONG -1');
  894. Concat(' OBJSECTION .dtor*');
  895. Concat(' LONG 0');
  896. Concat(' SYMBOL etext');
  897. Concat('ENDEXESECTION');
  898. Concat('EXESECTION .data');
  899. Concat(' SYMBOL __data_start__');
  900. Concat(' OBJSECTION .data*');
  901. Concat(' OBJSECTION .fpc*');
  902. Concat(' SYMBOL edata');
  903. Concat(' SYMBOL __data_end__');
  904. Concat('ENDEXESECTION');
  905. Concat('EXESECTION .rdata');
  906. Concat(' SYMBOL ___RUNTIME_PSEUDO_RELOC_LIST__');
  907. Concat(' SYMBOL __RUNTIME_PSEUDO_RELOC_LIST__');
  908. Concat(' OBJSECTION .rdata_runtime_pseudo_reloc');
  909. Concat(' SYMBOL ___RUNTIME_PSEUDO_RELOC_LIST_END__');
  910. Concat(' SYMBOL __RUNTIME_PSEUDO_RELOC_LIST_END__');
  911. Concat(' OBJSECTION .rdata*');
  912. Concat(' OBJSECTION .rodata*');
  913. Concat('ENDEXESECTION');
  914. Concat('EXESECTION .pdata');
  915. Concat(' OBJSECTION .pdata');
  916. Concat('ENDEXESECTION');
  917. Concat('EXESECTION .bss');
  918. Concat(' SYMBOL __bss_start__');
  919. Concat(' OBJSECTION .bss*');
  920. Concat(' SYMBOL __bss_end__');
  921. Concat('ENDEXESECTION');
  922. Concat('EXESECTION .idata');
  923. Concat(' OBJSECTION .idata$2*');
  924. Concat(' OBJSECTION .idata$3*');
  925. Concat(' ZEROS 20');
  926. Concat(' OBJSECTION .idata$4*');
  927. Concat(' OBJSECTION .idata$5*');
  928. Concat(' OBJSECTION .idata$6*');
  929. Concat(' OBJSECTION .idata$7*');
  930. Concat('ENDEXESECTION');
  931. secnames:='.edata,.rsrc,.reloc,'+
  932. '.debug_aranges,.debug_pubnames,.debug_info,.debug_abbrev,.debug_line,.debug_frame,.debug_str,.debug_loc,'+
  933. '.debug_macinfo,.debug_weaknames,.debug_funcnames,.debug_typenames,.debug_varnames,.debug_ranges';
  934. repeat
  935. secname:=gettoken(secnames,',');
  936. if secname='' then
  937. break;
  938. Concat('EXESECTION '+secname);
  939. Concat(' OBJSECTION '+secname+'*');
  940. Concat('ENDEXESECTION');
  941. until false;
  942. { Can't use the generic rules, because that will add also .stabstr to .stab }
  943. Concat('EXESECTION .stab');
  944. Concat(' OBJSECTION .stab');
  945. Concat('ENDEXESECTION');
  946. Concat('EXESECTION .stabstr');
  947. Concat(' OBJSECTION .stabstr');
  948. Concat('ENDEXESECTION');
  949. Concat('STABS');
  950. Concat('SYMBOLS');
  951. end;
  952. end;
  953. procedure TInternalLinkerWin.InitSysInitUnitName;
  954. begin
  955. if target_info.system=system_i386_win32 then
  956. GlobalInitSysInitUnitName(self);
  957. end;
  958. {****************************************************************************
  959. TExternalLinkerWin
  960. ****************************************************************************}
  961. Constructor TExternalLinkerWin.Create;
  962. begin
  963. Inherited Create;
  964. { allow duplicated libs (PM) }
  965. SharedLibFiles.doubles:=true;
  966. StaticLibFiles.doubles:=true;
  967. end;
  968. Procedure TExternalLinkerWin.SetDefaultInfo;
  969. var
  970. targetopts: string;
  971. begin
  972. with Info do
  973. begin
  974. if target_info.system=system_arm_wince then
  975. targetopts:='-m armpe'
  976. else
  977. targetopts:='-b pe-i386 -m i386pe';
  978. ExeCmd[1]:='ld '+targetopts+' $OPT $GCSECTIONS $MAP $STRIP $APPTYPE $ENTRY $IMAGEBASE $RELOC -o $EXE $RES';
  979. DllCmd[1]:='ld '+targetopts+' $OPT $GCSECTIONS $MAP $STRIP --dll $APPTYPE $ENTRY $IMAGEBASE $RELOC -o $EXE $RES';
  980. { ExeCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF';
  981. use short forms to avoid 128 char limitation problem }
  982. ExeCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  983. ExeCmd[3]:='ld '+targetopts+' $OPT $STRIP $APPTYPE $ENTRY $IMAGEBASE -o $EXE $RES exp.$$$';
  984. { DllCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF'; }
  985. DllCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  986. DllCmd[3]:='ld '+targetopts+' $OPT $STRIP --dll $APPTYPE $ENTRY $IMAGEBASE -o $EXE $RES exp.$$$';
  987. end;
  988. end;
  989. Function TExternalLinkerWin.WriteResponseFile(isdll:boolean) : Boolean;
  990. Var
  991. linkres : TLinkRes;
  992. HPath : TCmdStrListItem;
  993. s,s2 : TCmdStr;
  994. i : integer;
  995. begin
  996. WriteResponseFile:=False;
  997. if (cs_profile in current_settings.moduleswitches) then
  998. begin
  999. SharedLibFiles.Concat('gmon');
  1000. SharedLibFiles.Concat('c');
  1001. SharedLibFiles.Concat('gcc');
  1002. SharedLibFiles.Concat('kernel32');
  1003. end;
  1004. { Open link.res file }
  1005. LinkRes:=TLinkres.Create(outputexedir+Info.ResName);
  1006. with linkres do
  1007. begin
  1008. { Write path to search libraries }
  1009. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  1010. while assigned(HPath) do
  1011. begin
  1012. Add('SEARCH_DIR('+MaybeQuoted(HPath.Str)+')');
  1013. HPath:=TCmdStrListItem(HPath.Next);
  1014. end;
  1015. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  1016. while assigned(HPath) do
  1017. begin
  1018. Add('SEARCH_DIR('+MaybeQuoted(HPath.Str)+')');
  1019. HPath:=TCmdStrListItem(HPath.Next);
  1020. end;
  1021. { add objectfiles, start with prt0 always }
  1022. { profiling of shared libraries is currently not supported }
  1023. if not ObjectFiles.Empty then
  1024. begin
  1025. Add('INPUT(');
  1026. while not ObjectFiles.Empty do
  1027. begin
  1028. s:=ObjectFiles.GetFirst;
  1029. if s<>'' then
  1030. AddFileName(MaybeQuoted(s));
  1031. end;
  1032. Add(')');
  1033. end;
  1034. { Write staticlibraries }
  1035. if (not StaticLibFiles.Empty) then
  1036. begin
  1037. Add('GROUP(');
  1038. While not StaticLibFiles.Empty do
  1039. begin
  1040. S:=StaticLibFiles.GetFirst;
  1041. AddFileName(MaybeQuoted(s));
  1042. end;
  1043. Add(')');
  1044. end;
  1045. { Write sharedlibraries (=import libraries) }
  1046. if not SharedLibFiles.Empty then
  1047. begin
  1048. Add('INPUT(') ;
  1049. While not SharedLibFiles.Empty do
  1050. begin
  1051. S:=SharedLibFiles.GetFirst;
  1052. if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  1053. begin
  1054. Add(MaybeQuoted(s2));
  1055. continue;
  1056. end;
  1057. if pos(target_info.sharedlibprefix,s)=1 then
  1058. s:=copy(s,length(target_info.sharedlibprefix)+1,255);
  1059. i:=Pos(target_info.sharedlibext,S);
  1060. if i>0 then
  1061. Delete(S,i,255);
  1062. Add('-l'+s);
  1063. end;
  1064. Add(')');
  1065. end;
  1066. Add('SEARCH_DIR("/usr/i686-pc-cygwin/lib"); SEARCH_DIR("/usr/lib"); SEARCH_DIR("/usr/lib/w32api");');
  1067. Add('OUTPUT_FORMAT(pei-i386)');
  1068. Add('ENTRY(_mainCRTStartup)');
  1069. Add('SECTIONS');
  1070. Add('{');
  1071. Add(' . = SIZEOF_HEADERS;');
  1072. Add(' . = ALIGN(__section_alignment__);');
  1073. Add(' .text __image_base__ + ( __section_alignment__ < 0x1000 ? . : __section_alignment__ ) :');
  1074. Add(' {');
  1075. Add(' *(.init)');
  1076. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  1077. Add(' *(SORT(.text$*))');
  1078. Add(' ___CTOR_LIST__ = .; __CTOR_LIST__ = . ;');
  1079. Add(' LONG (-1);*(.ctors); *(.ctor); *(SORT(.ctors.*)); LONG (0);');
  1080. Add(' ___DTOR_LIST__ = .; __DTOR_LIST__ = . ;');
  1081. Add(' LONG (-1); *(.dtors); *(.dtor); *(SORT(.dtors.*)); LONG (0);');
  1082. Add(' *(.fini)');
  1083. Add(' PROVIDE (etext = .);');
  1084. Add(' *(.gcc_except_table)');
  1085. Add(' }');
  1086. Add(' .data BLOCK(__section_alignment__) :');
  1087. Add(' {');
  1088. Add(' __data_start__ = . ;');
  1089. add(' *(.data .data.* .gnu.linkonce.d.* .fpc*)');
  1090. Add(' *(.data2)');
  1091. Add(' *(SORT(.data$*))');
  1092. Add(' __data_end__ = . ;');
  1093. Add(' *(.data_cygwin_nocopy)');
  1094. Add(' }');
  1095. Add(' .rdata BLOCK(__section_alignment__) :');
  1096. Add(' {');
  1097. Add(' *(.rdata)');
  1098. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  1099. Add(' *(SORT(.rdata$*))');
  1100. Add(' *(.eh_frame)');
  1101. Add(' ___RUNTIME_PSEUDO_RELOC_LIST__ = .;');
  1102. Add(' __RUNTIME_PSEUDO_RELOC_LIST__ = .;');
  1103. Add(' *(.rdata_runtime_pseudo_reloc)');
  1104. Add(' ___RUNTIME_PSEUDO_RELOC_LIST_END__ = .;');
  1105. Add(' __RUNTIME_PSEUDO_RELOC_LIST_END__ = .;');
  1106. Add(' }');
  1107. Add(' .pdata BLOCK(__section_alignment__) : { *(.pdata) }');
  1108. Add(' .bss BLOCK(__section_alignment__) :');
  1109. Add(' {');
  1110. Add(' __bss_start__ = . ;');
  1111. Add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  1112. Add(' *(SORT(.bss$*))');
  1113. Add(' *(COMMON)');
  1114. Add(' __bss_end__ = . ;');
  1115. Add(' }');
  1116. Add(' .edata BLOCK(__section_alignment__) : { *(.edata) }');
  1117. Add(' .idata BLOCK(__section_alignment__) :');
  1118. Add(' {');
  1119. Add(' SORT(*)(.idata$2)');
  1120. Add(' SORT(*)(.idata$3)');
  1121. Add(' /* These zeroes mark the end of the import list. */');
  1122. Add(' LONG (0); LONG (0); LONG (0); LONG (0); LONG (0);');
  1123. Add(' SORT(*)(.idata$4)');
  1124. Add(' SORT(*)(.idata$5)');
  1125. Add(' SORT(*)(.idata$6)');
  1126. Add(' SORT(*)(.idata$7)');
  1127. Add(' }');
  1128. Add(' .CRT BLOCK(__section_alignment__) :');
  1129. Add(' {');
  1130. Add(' ___crt_xc_start__ = . ;');
  1131. Add(' *(SORT(.CRT$XC*)) /* C initialization */');
  1132. Add(' ___crt_xc_end__ = . ;');
  1133. Add(' ___crt_xi_start__ = . ;');
  1134. Add(' *(SORT(.CRT$XI*)) /* C++ initialization */');
  1135. Add(' ___crt_xi_end__ = . ;');
  1136. Add(' ___crt_xl_start__ = . ;');
  1137. Add(' *(SORT(.CRT$XL*)) /* TLS callbacks */');
  1138. Add(' /* ___crt_xl_end__ is defined in the TLS Directory support code */');
  1139. Add(' ___crt_xp_start__ = . ;');
  1140. Add(' *(SORT(.CRT$XP*)) /* Pre-termination */');
  1141. Add(' ___crt_xp_end__ = . ;');
  1142. Add(' ___crt_xt_start__ = . ;');
  1143. Add(' *(SORT(.CRT$XT*)) /* Termination */');
  1144. Add(' ___crt_xt_end__ = . ;');
  1145. Add(' }');
  1146. Add(' .tls BLOCK(__section_alignment__) :');
  1147. Add(' {');
  1148. Add(' ___tls_start__ = . ;');
  1149. Add(' *(.tls .tls.*)');
  1150. Add(' *(.tls$)');
  1151. Add(' *(SORT(.tls$*))');
  1152. Add(' ___tls_end__ = . ;');
  1153. Add(' }');
  1154. Add(' .rsrc BLOCK(__section_alignment__) :');
  1155. Add(' {');
  1156. Add(' *(.rsrc)');
  1157. Add(' *(SORT(.rsrc$*))');
  1158. Add(' }');
  1159. Add(' .reloc BLOCK(__section_alignment__) : { *(.reloc) }');
  1160. Add(' .stab BLOCK(__section_alignment__) (NOLOAD) : { *(.stab) }');
  1161. Add(' .stabstr BLOCK(__section_alignment__) (NOLOAD) : { *(.stabstr) }');
  1162. Add(' .debug_aranges BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_aranges) }');
  1163. Add(' .debug_pubnames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_pubnames) }');
  1164. Add(' .debug_info BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  1165. Add(' .debug_abbrev BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_abbrev) }');
  1166. Add(' .debug_line BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_line) }');
  1167. Add(' .debug_frame BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_frame) }');
  1168. Add(' .debug_str BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_str) }');
  1169. Add(' .debug_loc BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_loc) }');
  1170. Add(' .debug_macinfo BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_macinfo) }');
  1171. Add(' .debug_weaknames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_weaknames) }');
  1172. Add(' .debug_funcnames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_funcnames) }');
  1173. Add(' .debug_typenames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_typenames) }');
  1174. Add(' .debug_varnames BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_varnames) }');
  1175. Add(' .debug_ranges BLOCK(__section_alignment__) (NOLOAD) : { *(.debug_ranges) }');
  1176. Add('}');
  1177. { Write and Close response }
  1178. writetodisk;
  1179. Free;
  1180. end;
  1181. WriteResponseFile:=True;
  1182. end;
  1183. function TExternalLinkerWin.MakeExecutable:boolean;
  1184. var
  1185. MapStr,
  1186. binstr,
  1187. cmdstr : TCmdStr;
  1188. success : boolean;
  1189. cmds,i : longint;
  1190. AsBinStr : string[80];
  1191. GCSectionsStr,
  1192. StripStr,
  1193. RelocStr,
  1194. AppTypeStr,
  1195. EntryStr,
  1196. ImageBaseStr : string[40];
  1197. begin
  1198. if not(cs_link_nolink in current_settings.globalswitches) then
  1199. Message1(exec_i_linking,current_module.exefilename^);
  1200. { Create some replacements }
  1201. RelocStr:='';
  1202. AppTypeStr:='';
  1203. EntryStr:='';
  1204. ImageBaseStr:='';
  1205. StripStr:='';
  1206. MapStr:='';
  1207. GCSectionsStr:='';
  1208. AsBinStr:=FindUtil(utilsprefix+'as');
  1209. if RelocSection then
  1210. RelocStr:='--base-file base.$$$';
  1211. if use_smartlink_section then
  1212. GCSectionsStr:='--gc-sections';
  1213. if target_info.system in [system_arm_wince,system_i386_wince] then
  1214. AppTypeStr:='--subsystem wince'
  1215. else
  1216. begin
  1217. if apptype=app_gui then
  1218. AppTypeStr:='--subsystem windows';
  1219. end;
  1220. if apptype=app_gui then
  1221. EntryStr:='--entry=_WinMainCRTStartup'
  1222. else
  1223. EntryStr:='--entry=_mainCRTStartup';
  1224. if assigned(DLLImageBase) then
  1225. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  1226. if (cs_link_strip in current_settings.globalswitches) then
  1227. StripStr:='-s';
  1228. if (cs_link_map in current_settings.globalswitches) then
  1229. MapStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  1230. { Write used files and libraries }
  1231. WriteResponseFile(false);
  1232. { Call linker }
  1233. success:=false;
  1234. if RelocSection or (not Deffile.empty) then
  1235. cmds:=3
  1236. else
  1237. cmds:=1;
  1238. for i:=1 to cmds do
  1239. begin
  1240. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  1241. if binstr<>'' then
  1242. begin
  1243. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  1244. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1245. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1246. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  1247. Replace(cmdstr,'$ENTRY',EntryStr);
  1248. Replace(cmdstr,'$ASBIN',AsbinStr);
  1249. Replace(cmdstr,'$RELOC',RelocStr);
  1250. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  1251. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1252. Replace(cmdstr,'$STRIP',StripStr);
  1253. Replace(cmdstr,'$MAP',MapStr);
  1254. if not DefFile.Empty then
  1255. begin
  1256. DefFile.WriteFile;
  1257. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  1258. end
  1259. else
  1260. Replace(cmdstr,'$DEF','');
  1261. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  1262. if not success then
  1263. break;
  1264. end;
  1265. end;
  1266. { Post process }
  1267. if success then
  1268. success:=PostProcessExecutable(current_module.exefilename^,false);
  1269. { Remove ReponseFile }
  1270. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1271. begin
  1272. DeleteFile(outputexedir+Info.ResName);
  1273. DeleteFile('base.$$$');
  1274. DeleteFile('exp.$$$');
  1275. DeleteFile('deffile.$$$');
  1276. end;
  1277. MakeExecutable:=success; { otherwise a recursive call to link method }
  1278. end;
  1279. Function TExternalLinkerWin.MakeSharedLibrary:boolean;
  1280. var
  1281. MapStr,
  1282. binstr,
  1283. cmdstr : TCmdStr;
  1284. success : boolean;
  1285. cmds,
  1286. i : longint;
  1287. AsBinStr : string[80];
  1288. StripStr,
  1289. GCSectionsStr,
  1290. RelocStr,
  1291. AppTypeStr,
  1292. EntryStr,
  1293. ImageBaseStr : string[40];
  1294. begin
  1295. MakeSharedLibrary:=false;
  1296. if not(cs_link_nolink in current_settings.globalswitches) then
  1297. Message1(exec_i_linking,current_module.sharedlibfilename^);
  1298. { Create some replacements }
  1299. RelocStr:='';
  1300. AppTypeStr:='';
  1301. EntryStr:='';
  1302. ImageBaseStr:='';
  1303. StripStr:='';
  1304. MapStr:='';
  1305. GCSectionsStr:='';
  1306. AsBinStr:=FindUtil(utilsprefix+'as');
  1307. if RelocSection then
  1308. RelocStr:='--base-file base.$$$';
  1309. if use_smartlink_section then
  1310. GCSectionsStr:='--gc-sections';
  1311. if apptype=app_gui then
  1312. begin
  1313. AppTypeStr:='--subsystem windows';
  1314. EntryStr:='--entry _DLLWinMainCRTStartup'
  1315. end
  1316. else
  1317. EntryStr:='--entry _DLLMainCRTStartup';
  1318. if assigned(DLLImageBase) then
  1319. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  1320. if (cs_link_strip in current_settings.globalswitches) then
  1321. StripStr:='-s';
  1322. if (cs_link_map in current_settings.globalswitches) then
  1323. MapStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  1324. { Write used files and libraries }
  1325. WriteResponseFile(true);
  1326. { Call linker }
  1327. success:=false;
  1328. if RelocSection or (not Deffile.empty) then
  1329. cmds:=3
  1330. else
  1331. cmds:=1;
  1332. for i:=1 to cmds do
  1333. begin
  1334. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  1335. if binstr<>'' then
  1336. begin
  1337. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1338. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1339. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1340. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  1341. Replace(cmdstr,'$ENTRY',EntryStr);
  1342. Replace(cmdstr,'$ASBIN',AsbinStr);
  1343. Replace(cmdstr,'$RELOC',RelocStr);
  1344. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  1345. Replace(cmdstr,'$STRIP',StripStr);
  1346. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1347. Replace(cmdstr,'$MAP',MapStr);
  1348. if not DefFile.Empty then
  1349. begin
  1350. DefFile.WriteFile;
  1351. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  1352. end
  1353. else
  1354. Replace(cmdstr,'$DEF','');
  1355. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  1356. if not success then
  1357. break;
  1358. end;
  1359. end;
  1360. { Post process }
  1361. if success then
  1362. success:=PostProcessExecutable(current_module.sharedlibfilename^,true);
  1363. { Remove ReponseFile }
  1364. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1365. begin
  1366. DeleteFile(outputexedir+Info.ResName);
  1367. DeleteFile('base.$$$');
  1368. DeleteFile('exp.$$$');
  1369. DeleteFile('deffile.$$$');
  1370. end;
  1371. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1372. end;
  1373. function TExternalLinkerWin.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1374. type
  1375. tdosheader = packed record
  1376. e_magic : word;
  1377. e_cblp : word;
  1378. e_cp : word;
  1379. e_crlc : word;
  1380. e_cparhdr : word;
  1381. e_minalloc : word;
  1382. e_maxalloc : word;
  1383. e_ss : word;
  1384. e_sp : word;
  1385. e_csum : word;
  1386. e_ip : word;
  1387. e_cs : word;
  1388. e_lfarlc : word;
  1389. e_ovno : word;
  1390. e_res : array[0..3] of word;
  1391. e_oemid : word;
  1392. e_oeminfo : word;
  1393. e_res2 : array[0..9] of word;
  1394. e_lfanew : longint;
  1395. end;
  1396. psecfill=^TSecfill;
  1397. TSecfill=record
  1398. fillpos,
  1399. fillsize : longint;
  1400. next : psecfill;
  1401. end;
  1402. var
  1403. f : file;
  1404. cmdstr : string;
  1405. dosheader : tdosheader;
  1406. peheader : tcoffheader;
  1407. peoptheader : tcoffpeoptheader;
  1408. firstsecpos,
  1409. maxfillsize,
  1410. l,peheaderpos : longint;
  1411. coffsec : tcoffsechdr;
  1412. secroot,hsecroot : psecfill;
  1413. zerobuf : pointer;
  1414. begin
  1415. postprocessexecutable:=false;
  1416. { when -s is used or it's a dll then quit }
  1417. if (cs_link_nolink in current_settings.globalswitches) then
  1418. begin
  1419. case apptype of
  1420. app_native :
  1421. cmdstr:='--subsystem native';
  1422. app_gui :
  1423. cmdstr:='--subsystem gui';
  1424. app_cui :
  1425. cmdstr:='--subsystem console';
  1426. end;
  1427. if dllversion<>'' then
  1428. cmdstr:=cmdstr+' --version '+dllversion;
  1429. cmdstr:=cmdstr+' --input '+maybequoted(fn);
  1430. cmdstr:=cmdstr+' --stack '+tostr(stacksize);
  1431. DoExec(FindUtil(utilsprefix+'postw32'),cmdstr,false,false);
  1432. postprocessexecutable:=true;
  1433. exit;
  1434. end;
  1435. { open file }
  1436. assign(f,fn);
  1437. {$I-}
  1438. reset(f,1);
  1439. if ioresult<>0 then
  1440. Message1(execinfo_f_cant_open_executable,fn);
  1441. { read headers }
  1442. blockread(f,dosheader,sizeof(tdosheader));
  1443. peheaderpos:=dosheader.e_lfanew;
  1444. { skip to headerpos and skip pe magic }
  1445. seek(f,peheaderpos+4);
  1446. blockread(f,peheader,sizeof(tcoffheader));
  1447. blockread(f,peoptheader,sizeof(tcoffpeoptheader));
  1448. { write info }
  1449. Message1(execinfo_x_codesize,tostr(peoptheader.tsize));
  1450. Message1(execinfo_x_initdatasize,tostr(peoptheader.dsize));
  1451. Message1(execinfo_x_uninitdatasize,tostr(peoptheader.bsize));
  1452. { change stack size (PM) }
  1453. { I am not sure that the default value is adequate !! }
  1454. peoptheader.SizeOfStackReserve:=stacksize;
  1455. if SetPEFlagsSetExplicity then
  1456. peoptheader.LoaderFlags:=peflags;
  1457. if ImageBaseSetExplicity then
  1458. peoptheader.ImageBase:=imagebase;
  1459. if MinStackSizeSetExplicity then
  1460. peoptheader.SizeOfStackCommit:=minstacksize;
  1461. if MaxStackSizeSetExplicity then
  1462. peoptheader.SizeOfStackReserve:=maxstacksize;
  1463. { change the header }
  1464. { sub system }
  1465. { gui=2 }
  1466. { cui=3 }
  1467. { wincegui=9 }
  1468. if target_info.system in [system_arm_wince,system_i386_wince] then
  1469. peoptheader.Subsystem:=9
  1470. else
  1471. case apptype of
  1472. app_native :
  1473. peoptheader.Subsystem:=1;
  1474. app_gui :
  1475. peoptheader.Subsystem:=2;
  1476. app_cui :
  1477. peoptheader.Subsystem:=3;
  1478. end;
  1479. if dllversion<>'' then
  1480. begin
  1481. peoptheader.MajorImageVersion:=dllmajor;
  1482. peoptheader.MinorImageVersion:=dllminor;
  1483. end;
  1484. { reset timestamp }
  1485. peheader.time:=0;
  1486. { write header back, skip pe magic }
  1487. seek(f,peheaderpos+4);
  1488. blockwrite(f,peheader,sizeof(tcoffheader));
  1489. if ioresult<>0 then
  1490. Message1(execinfo_f_cant_process_executable,fn);
  1491. blockwrite(f,peoptheader,sizeof(tcoffpeoptheader));
  1492. if ioresult<>0 then
  1493. Message1(execinfo_f_cant_process_executable,fn);
  1494. { skip to headerpos and skip pe magic }
  1495. seek(f,peheaderpos+4);
  1496. blockread(f,peheader,sizeof(tcoffheader));
  1497. blockread(f,peoptheader,sizeof(tcoffpeoptheader));
  1498. { write the value after the change }
  1499. Message1(execinfo_x_stackreserve,tostr(peoptheader.SizeOfStackReserve));
  1500. Message1(execinfo_x_stackcommit,tostr(peoptheader.SizeOfStackCommit));
  1501. { read section info }
  1502. maxfillsize:=0;
  1503. firstsecpos:=0;
  1504. secroot:=nil;
  1505. for l:=1 to peheader.nsects do
  1506. begin
  1507. blockread(f,coffsec,sizeof(tcoffsechdr));
  1508. if coffsec.datapos>0 then
  1509. begin
  1510. if secroot=nil then
  1511. firstsecpos:=coffsec.datapos;
  1512. new(hsecroot);
  1513. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  1514. hsecroot^.fillsize:=coffsec.datasize-coffsec.vsize;
  1515. hsecroot^.next:=secroot;
  1516. secroot:=hsecroot;
  1517. if secroot^.fillsize>maxfillsize then
  1518. maxfillsize:=secroot^.fillsize;
  1519. end;
  1520. end;
  1521. if firstsecpos>0 then
  1522. begin
  1523. l:=firstsecpos-filepos(f);
  1524. if l>maxfillsize then
  1525. maxfillsize:=l;
  1526. end
  1527. else
  1528. l:=0;
  1529. { get zero buffer }
  1530. getmem(zerobuf,maxfillsize);
  1531. fillchar(zerobuf^,maxfillsize,0);
  1532. { zero from sectioninfo until first section }
  1533. blockwrite(f,zerobuf^,l);
  1534. { zero section alignments }
  1535. while assigned(secroot) do
  1536. begin
  1537. seek(f,secroot^.fillpos);
  1538. blockwrite(f,zerobuf^,secroot^.fillsize);
  1539. hsecroot:=secroot;
  1540. secroot:=secroot^.next;
  1541. dispose(hsecroot);
  1542. end;
  1543. freemem(zerobuf,maxfillsize);
  1544. close(f);
  1545. {$I+}
  1546. if ioresult<>0 then;
  1547. postprocessexecutable:=true;
  1548. end;
  1549. procedure TExternalLinkerWin.InitSysInitUnitName;
  1550. begin
  1551. if target_info.system=system_i386_win32 then
  1552. GlobalInitSysInitUnitName(self);
  1553. end;
  1554. {****************************************************************************
  1555. TDLLScannerWin
  1556. ****************************************************************************}
  1557. procedure TDLLScannerWin.CheckDLLFunc(const dllname,funcname:string);
  1558. var
  1559. i : longint;
  1560. ExtName : string;
  1561. begin
  1562. for i:=0 to current_module.dllscannerinputlist.count-1 do
  1563. begin
  1564. ExtName:=current_module.dllscannerinputlist.NameOfIndex(i);
  1565. if (ExtName=funcname) then
  1566. begin
  1567. current_module.AddExternalImport(dllname,funcname,0,false);
  1568. importfound:=true;
  1569. current_module.dllscannerinputlist.Delete(i);
  1570. exit;
  1571. end;
  1572. end;
  1573. end;
  1574. function TDLLScannerWin.scan(const binname:string):boolean;
  1575. var
  1576. hs,
  1577. dllname : TCmdStr;
  1578. begin
  1579. result:=false;
  1580. { is there already an import library the we will use that one }
  1581. if FindLibraryFile(binname,target_info.staticClibprefix,target_info.staticClibext,hs) then
  1582. exit;
  1583. { check if we can find the dll }
  1584. hs:=binname;
  1585. if ExtractFileExt(hs)='' then
  1586. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1587. if not FindDll(hs,dllname) then
  1588. exit;
  1589. importfound:=false;
  1590. ReadDLLImports(dllname,@CheckDLLFunc);
  1591. if importfound then
  1592. current_module.dllscannerinputlist.Pack;
  1593. result:=importfound;
  1594. end;
  1595. {****************************************************************************
  1596. TWinResourceFile
  1597. ****************************************************************************}
  1598. procedure TWinResourceFile.PostProcessResourcefile(const s : ansistring);
  1599. var
  1600. f : file;
  1601. w : word;
  1602. begin
  1603. {$ifdef arm}
  1604. assign(f,s);
  1605. reset(f,1);
  1606. w:=COFF_MAGIC;
  1607. blockwrite(f,w,2);
  1608. close(f);
  1609. {$endif arm}
  1610. end;
  1611. {*****************************************************************************
  1612. Initialize
  1613. *****************************************************************************}
  1614. initialization
  1615. {$ifdef i386}
  1616. { Win32 }
  1617. RegisterExternalLinker(system_i386_win32_info,TExternalLinkerWin);
  1618. RegisterInternalLinker(system_i386_win32_info,TInternalLinkerWin);
  1619. RegisterImport(system_i386_win32,TImportLibWin);
  1620. RegisterExport(system_i386_win32,TExportLibWin);
  1621. RegisterDLLScanner(system_i386_win32,TDLLScannerWin);
  1622. RegisterRes(res_gnu_windres_info,TWinResourceFile);
  1623. RegisterTarget(system_i386_win32_info);
  1624. { WinCE }
  1625. RegisterExternalLinker(system_i386_wince_info,TExternalLinkerWin);
  1626. RegisterInternalLinker(system_i386_wince_info,TInternalLinkerWin);
  1627. RegisterImport(system_i386_wince,TImportLibWin);
  1628. RegisterExport(system_i386_wince,TExportLibWin);
  1629. RegisterDLLScanner(system_i386_wince,TDLLScannerWin);
  1630. RegisterTarget(system_i386_wince_info);
  1631. {$endif i386}
  1632. {$ifdef x86_64}
  1633. RegisterInternalLinker(system_x64_win64_info,TInternalLinkerWin);
  1634. RegisterImport(system_x86_64_win64,TImportLibWin);
  1635. RegisterExport(system_x86_64_win64,TExportLibWin);
  1636. RegisterDLLScanner(system_x86_64_win64,TDLLScannerWin);
  1637. RegisterRes(res_gnu_windres_info,TWinResourceFile);
  1638. RegisterTarget(system_x64_win64_info);
  1639. {$endif x86_64}
  1640. {$ifdef arm}
  1641. RegisterExternalLinker(system_arm_wince_info,TExternalLinkerWin);
  1642. RegisterInternalLinker(system_arm_wince_info,TInternalLinkerWin);
  1643. RegisterImport(system_arm_wince,TImportLibWin);
  1644. RegisterExport(system_arm_wince,TExportLibWin);
  1645. RegisterRes(res_gnu_wince_windres_info,TWinResourceFile);
  1646. RegisterTarget(system_arm_wince_info);
  1647. {$endif arm}
  1648. end.