t_win.pas 66 KB

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