t_win.pas 66 KB

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