t_win.pas 67 KB

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