t_win.pas 69 KB

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