t_win.pas 68 KB

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