t_win.pas 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  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,aasmcpu,fmodule,globtype,globals,systems,verbose,
  25. symconst,symdef,symsym,
  26. script,gendef,
  27. cpubase,
  28. import,export,link,cgobj,i_win;
  29. const
  30. MAX_DEFAULT_EXTENSIONS = 3;
  31. type
  32. tStr4=array[1..MAX_DEFAULT_EXTENSIONS] of string[4];
  33. pStr4=^tStr4;
  34. twin32imported_item = class(timported_item)
  35. procdef : tprocdef;
  36. end;
  37. timportlibwin32=class(timportlib)
  38. private
  39. procedure win32importproc(aprocdef:tprocdef;const func,module : string;index : longint;const name : string);
  40. procedure importvariable_str(const s:string;const name,module:string);
  41. procedure importprocedure_str(const func,module:string;index:longint;const name:string);
  42. public
  43. procedure preparelib(const s:string);override;
  44. procedure importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);override;
  45. procedure importvariable(vs:tglobalvarsym;const name,module:string);override;
  46. procedure generatelib;override;
  47. procedure generatenasmlib;virtual;
  48. procedure generatesmartlib;override;
  49. end;
  50. texportlibwin32=class(texportlib)
  51. st : string;
  52. EList_indexed:tList;
  53. EList_nonindexed:tList;
  54. procedure preparelib(const s:string);override;
  55. procedure exportprocedure(hp : texported_item);override;
  56. procedure exportvar(hp : texported_item);override;
  57. procedure exportfromlist(hp : texported_item);
  58. procedure generatelib;override;
  59. procedure generatenasmlib;virtual;
  60. end;
  61. tlinkerwin32=class(texternallinker)
  62. private
  63. Function WriteResponseFile(isdll:boolean) : Boolean;
  64. Function PostProcessExecutable(const fn:string;isdll:boolean) : Boolean;
  65. public
  66. Constructor Create;override;
  67. Procedure SetDefaultInfo;override;
  68. function MakeExecutable:boolean;override;
  69. function MakeSharedLibrary:boolean;override;
  70. end;
  71. tDLLScannerWin32=class(tDLLScanner)
  72. private
  73. cstring : array[0..127]of char;
  74. function DOSstubOK(var x:cardinal):boolean;
  75. function FindDLL(const s:string;var founddll:string):boolean;
  76. function ExtractDllName(Const Name : string) : string;
  77. public
  78. function isSuitableFileType(x:cardinal):longbool;override;
  79. function GetEdata(HeaderEntry:cardinal):longbool;override;
  80. function Scan(const binname:string):longbool;override;
  81. end;
  82. implementation
  83. uses
  84. cpuinfo,cgutils,dbgbase;
  85. const
  86. res_gnu_windres_info : tresinfo =
  87. (
  88. id : res_gnu_windres;
  89. resbin : 'windres';
  90. rescmd : '--include $INC -O coff -o $OBJ $RES'
  91. );
  92. res_gnu_wince_windres_info : tresinfo =
  93. (
  94. id : res_gnu_wince_windres;
  95. resbin : 'windres';
  96. rescmd : '--include $INC -O coff -o $OBJ $RES'
  97. );
  98. {*****************************************************************************
  99. TIMPORTLIBWIN32
  100. *****************************************************************************}
  101. procedure timportlibwin32.preparelib(const s : string);
  102. begin
  103. if asmlist[al_imports]=nil then
  104. asmlist[al_imports]:=TAAsmoutput.create;
  105. end;
  106. procedure timportlibwin32.win32importproc(aprocdef:tprocdef;const func,module : string;index : longint;const name : string);
  107. var
  108. hp1 : timportlist;
  109. hp2 : twin32imported_item;
  110. hs : string;
  111. begin
  112. { procdef or funcname must be give, not both }
  113. if assigned(aprocdef) and (func<>'') then
  114. internalerror(200411161);
  115. { append extension if required }
  116. hs:=AddExtension(module,target_info.sharedlibext);
  117. { search for the module }
  118. hp1:=timportlist(current_module.imports.first);
  119. while assigned(hp1) do
  120. begin
  121. if hs=hp1.dllname^ then
  122. break;
  123. hp1:=timportlist(hp1.next);
  124. end;
  125. { generate a new item ? }
  126. if not(assigned(hp1)) then
  127. begin
  128. hp1:=timportlist.create(hs);
  129. current_module.imports.concat(hp1);
  130. end;
  131. { search for reuse of old import item }
  132. if assigned(aprocdef) then
  133. begin
  134. hp2:=twin32imported_item(hp1.imported_items.first);
  135. while assigned(hp2) do
  136. begin
  137. if (hp2.procdef=aprocdef) then
  138. break;
  139. hp2:=twin32imported_item(hp2.next);
  140. end;
  141. end
  142. else
  143. begin
  144. hp2:=twin32imported_item(hp1.imported_items.first);
  145. while assigned(hp2) do
  146. begin
  147. if (hp2.func^=func) then
  148. break;
  149. hp2:=twin32imported_item(hp2.next);
  150. end;
  151. end;
  152. if not assigned(hp2) then
  153. begin
  154. hp2:=twin32imported_item.create(func,name,index);
  155. hp2.procdef:=aprocdef;
  156. hp1.imported_items.concat(hp2);
  157. end;
  158. end;
  159. procedure timportlibwin32.importprocedure(aprocdef:tprocdef;const module : string;index : longint;const name : string);
  160. begin
  161. win32importproc(aprocdef,'',module,index,name);
  162. end;
  163. procedure timportlibwin32.importprocedure_str(const func,module : string;index : longint;const name : string);
  164. begin
  165. win32importproc(nil,func,module,index,name);
  166. end;
  167. procedure timportlibwin32.importvariable(vs:tglobalvarsym;const name,module:string);
  168. begin
  169. importvariable_str(vs.mangledname,name,module);
  170. end;
  171. procedure timportlibwin32.importvariable_str(const s:string;const name,module:string);
  172. var
  173. hp1 : timportlist;
  174. hp2 : twin32imported_item;
  175. hs : string;
  176. begin
  177. hs:=AddExtension(module,target_info.sharedlibext);
  178. { search for the module }
  179. hp1:=timportlist(current_module.imports.first);
  180. while assigned(hp1) do
  181. begin
  182. if hs=hp1.dllname^ then
  183. break;
  184. hp1:=timportlist(hp1.next);
  185. end;
  186. { generate a new item ? }
  187. if not(assigned(hp1)) then
  188. begin
  189. hp1:=timportlist.create(hs);
  190. current_module.imports.concat(hp1);
  191. end;
  192. hp2:=twin32imported_item.create_var(s,name);
  193. hp2.procdef:=nil;
  194. hp1.imported_items.concat(hp2);
  195. end;
  196. procedure timportlibwin32.generatenasmlib;
  197. var
  198. hp1 : timportlist;
  199. hp2 : twin32imported_item;
  200. begin
  201. new_section(asmlist[al_imports],sec_code,'',0);
  202. hp1:=timportlist(current_module.imports.first);
  203. while assigned(hp1) do
  204. begin
  205. hp2:=twin32imported_item(hp1.imported_items.first);
  206. while assigned(hp2) do
  207. begin
  208. asmlist[al_imports].concat(tai_directive.create(asd_extern,hp2.func^));
  209. asmlist[al_imports].concat(tai_directive.create(asd_nasm_import,hp2.func^+' '+hp1.dllname^+' '+hp2.name^));
  210. hp2:=twin32imported_item(hp2.next);
  211. end;
  212. hp1:=timportlist(hp1.next);
  213. end;
  214. end;
  215. procedure timportlibwin32.generatesmartlib;
  216. var
  217. hp1 : timportlist;
  218. mangledstring : string;
  219. importname : string;
  220. suffix : integer;
  221. hp2 : twin32imported_item;
  222. lhead,lname,lcode, {$ifdef ARM} lpcode, {$endif ARM}
  223. lidata4,lidata5 : tasmlabel;
  224. href : treference;
  225. begin
  226. if (target_asm.id in [as_i386_masm,as_i386_tasm,as_i386_nasmwin32]) then
  227. begin
  228. generatenasmlib;
  229. exit;
  230. end;
  231. hp1:=timportlist(current_module.imports.first);
  232. while assigned(hp1) do
  233. begin
  234. { Get labels for the sections }
  235. objectlibrary.getdatalabel(lhead);
  236. objectlibrary.getdatalabel(lname);
  237. objectlibrary.getaddrlabel(lidata4);
  238. objectlibrary.getaddrlabel(lidata5);
  239. { create header for this importmodule }
  240. asmlist[al_imports].concat(Tai_cutobject.Create_begin);
  241. new_section(asmlist[al_imports],sec_idata2,'',0);
  242. asmlist[al_imports].concat(Tai_label.Create(lhead));
  243. { pointer to procedure names }
  244. asmlist[al_imports].concat(Tai_const.Create_rva_sym(lidata4));
  245. { two empty entries follow }
  246. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  247. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  248. { pointer to dll name }
  249. asmlist[al_imports].concat(Tai_const.Create_rva_sym(lname));
  250. { pointer to fixups }
  251. asmlist[al_imports].concat(Tai_const.Create_rva_sym(lidata5));
  252. { first write the name references }
  253. new_section(asmlist[al_imports],sec_idata4,'',0);
  254. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  255. asmlist[al_imports].concat(Tai_label.Create(lidata4));
  256. { then the addresses and create also the indirect jump }
  257. new_section(asmlist[al_imports],sec_idata5,'',0);
  258. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  259. asmlist[al_imports].concat(Tai_label.Create(lidata5));
  260. { create procedures }
  261. hp2:=twin32imported_item(hp1.imported_items.first);
  262. while assigned(hp2) do
  263. begin
  264. { insert cuts }
  265. asmlist[al_imports].concat(Tai_cutobject.Create);
  266. { create indirect jump }
  267. if not hp2.is_var then
  268. begin
  269. objectlibrary.getjumplabel(lcode);
  270. {$ifdef ARM}
  271. objectlibrary.getjumplabel(lpcode);
  272. {$endif ARM}
  273. { place jump in al_procedures, insert a code section in the
  274. al_imports to reduce the amount of .s files (PFV) }
  275. new_section(asmlist[al_imports],sec_code,'',0);
  276. if assigned(hp2.procdef) then
  277. mangledstring:=hp2.procdef.mangledname
  278. else
  279. mangledstring:=hp2.func^;
  280. asmlist[al_imports].concat(Tai_symbol.Createname_global(mangledstring,AT_FUNCTION,0));
  281. asmlist[al_imports].concat(Tai_function_name.Create(''));
  282. {$ifdef ARM}
  283. reference_reset_symbol(href,lpcode,0);
  284. asmlist[al_imports].concat(Taicpu.op_reg_ref(A_LDR,NR_R12,href));
  285. reference_reset_base(href,NR_R12,0);
  286. asmlist[al_imports].concat(Taicpu.op_reg_ref(A_LDR,NR_R15,href));
  287. asmlist[al_imports].concat(Tai_label.Create(lpcode));
  288. reference_reset_symbol(href,lcode,0);
  289. asmlist[al_imports].concat(tai_const.create_sym_offset(href.symbol,href.offset));
  290. {$else ARM}
  291. reference_reset_symbol(href,lcode,0);
  292. asmlist[al_imports].concat(Taicpu.Op_ref(A_JMP,S_NO,href));
  293. asmlist[al_imports].concat(Tai_align.Create_op(4,$90));
  294. {$endif ARM}
  295. if (cs_debuginfo in aktmoduleswitches) and
  296. assigned(hp2.procdef) then
  297. debuginfo.insertdef(asmlist[al_imports],hp2.procdef);
  298. end;
  299. { create head link }
  300. new_section(asmlist[al_imports],sec_idata7,'',0);
  301. asmlist[al_imports].concat(Tai_const.Create_rva_sym(lhead));
  302. { fixup }
  303. objectlibrary.getjumplabel(tasmlabel(hp2.lab));
  304. new_section(asmlist[al_imports],sec_idata4,'',0);
  305. asmlist[al_imports].concat(Tai_const.Create_rva_sym(hp2.lab));
  306. { add jump field to al_imports }
  307. new_section(asmlist[al_imports],sec_idata5,'',0);
  308. if hp2.is_var then
  309. asmlist[al_imports].concat(Tai_symbol.Createname_global(hp2.func^,AT_FUNCTION,0))
  310. else
  311. asmlist[al_imports].concat(Tai_label.Create(lcode));
  312. if (cs_debuginfo in aktmoduleswitches) then
  313. begin
  314. if assigned(hp2.name) then
  315. begin
  316. importname:='__imp_'+hp2.name^;
  317. suffix:=0;
  318. while assigned(objectlibrary.getasmsymbol(importname)) do
  319. begin
  320. inc(suffix);
  321. importname:='__imp_'+hp2.name^+'_'+tostr(suffix);
  322. end;
  323. asmlist[al_imports].concat(tai_symbol.createname(importname,AT_FUNCTION,4));
  324. end
  325. else
  326. begin
  327. importname:='__imp_by_ordinal'+tostr(hp2.ordnr);
  328. suffix:=0;
  329. while assigned(objectlibrary.getasmsymbol(importname)) do
  330. begin
  331. inc(suffix);
  332. importname:='__imp_by_ordinal'+tostr(hp2.ordnr)+'_'+tostr(suffix);
  333. end;
  334. asmlist[al_imports].concat(tai_symbol.createname(importname,AT_FUNCTION,4));
  335. end;
  336. end;
  337. if hp2.name^<>'' then
  338. asmlist[al_imports].concat(Tai_const.Create_rva_sym(hp2.lab))
  339. else
  340. asmlist[al_imports].concat(Tai_const.Create_32bit(longint($80000000) or longint(hp2.ordnr)));
  341. { finally the import information }
  342. new_section(asmlist[al_imports],sec_idata6,'',0);
  343. asmlist[al_imports].concat(Tai_label.Create(hp2.lab));
  344. asmlist[al_imports].concat(Tai_const.Create_16bit(hp2.ordnr));
  345. asmlist[al_imports].concat(Tai_string.Create(hp2.name^+#0));
  346. asmlist[al_imports].concat(Tai_align.Create_op(2,0));
  347. hp2:=twin32imported_item(hp2.next);
  348. end;
  349. { write final section }
  350. asmlist[al_imports].concat(Tai_cutobject.Create_end);
  351. { end of name references }
  352. new_section(asmlist[al_imports],sec_idata4,'',0);
  353. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  354. { end if addresses }
  355. new_section(asmlist[al_imports],sec_idata5,'',0);
  356. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  357. { dllname }
  358. new_section(asmlist[al_imports],sec_idata7,'',0);
  359. asmlist[al_imports].concat(Tai_label.Create(lname));
  360. asmlist[al_imports].concat(Tai_string.Create(hp1.dllname^+#0));
  361. hp1:=timportlist(hp1.next);
  362. end;
  363. end;
  364. procedure timportlibwin32.generatelib;
  365. var
  366. hp1 : timportlist;
  367. hp2 : twin32imported_item;
  368. l1,l2,l3,l4 {$ifdef ARM} ,l5 {$endif ARM} : tasmlabel;
  369. mangledstring : string;
  370. importname : string;
  371. suffix : integer;
  372. href : treference;
  373. begin
  374. if (target_asm.id in [as_i386_masm,as_i386_tasm,as_i386_nasmwin32]) then
  375. begin
  376. generatenasmlib;
  377. exit;
  378. end;
  379. hp1:=timportlist(current_module.imports.first);
  380. while assigned(hp1) do
  381. begin
  382. { align al_procedures for the jumps }
  383. new_section(asmlist[al_imports],sec_code,'',sizeof(aint));
  384. { Get labels for the sections }
  385. objectlibrary.getjumplabel(l1);
  386. objectlibrary.getjumplabel(l2);
  387. objectlibrary.getjumplabel(l3);
  388. new_section(asmlist[al_imports],sec_idata2,'',0);
  389. { pointer to procedure names }
  390. asmlist[al_imports].concat(Tai_const.Create_rva_sym(l2));
  391. { two empty entries follow }
  392. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  393. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  394. { pointer to dll name }
  395. asmlist[al_imports].concat(Tai_const.Create_rva_sym(l1));
  396. { pointer to fixups }
  397. asmlist[al_imports].concat(Tai_const.Create_rva_sym(l3));
  398. { only create one section for each else it will
  399. create a lot of idata* }
  400. { first write the name references }
  401. new_section(asmlist[al_imports],sec_idata4,'',0);
  402. asmlist[al_imports].concat(Tai_label.Create(l2));
  403. hp2:=twin32imported_item(hp1.imported_items.first);
  404. while assigned(hp2) do
  405. begin
  406. objectlibrary.getjumplabel(tasmlabel(hp2.lab));
  407. if hp2.name^<>'' then
  408. asmlist[al_imports].concat(Tai_const.Create_rva_sym(hp2.lab))
  409. else
  410. asmlist[al_imports].concat(Tai_const.Create_32bit(longint($80000000) or hp2.ordnr));
  411. hp2:=twin32imported_item(hp2.next);
  412. end;
  413. { finalize the names ... }
  414. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  415. { then the addresses and create also the indirect jump }
  416. new_section(asmlist[al_imports],sec_idata5,'',0);
  417. asmlist[al_imports].concat(Tai_label.Create(l3));
  418. hp2:=twin32imported_item(hp1.imported_items.first);
  419. while assigned(hp2) do
  420. begin
  421. if not hp2.is_var then
  422. begin
  423. objectlibrary.getjumplabel(l4);
  424. {$ifdef ARM}
  425. objectlibrary.getjumplabel(l5);
  426. {$endif ARM}
  427. { create indirect jump and }
  428. { place jump in al_procedures }
  429. new_section(asmlist[al_imports],sec_code,'',0);
  430. if assigned(hp2.procdef) then
  431. mangledstring:=hp2.procdef.mangledname
  432. else
  433. mangledstring:=hp2.func^;
  434. asmlist[al_imports].concat(Tai_symbol.Createname_global(mangledstring,AT_FUNCTION,0));
  435. asmlist[al_imports].concat(tai_function_name.create(''));
  436. {$ifdef ARM}
  437. reference_reset_symbol(href,l5,0);
  438. asmlist[al_imports].concat(Taicpu.op_reg_ref(A_LDR,NR_R12,href));
  439. reference_reset_base(href,NR_R12,0);
  440. asmlist[al_imports].concat(Taicpu.op_reg_ref(A_LDR,NR_R15,href));
  441. asmlist[al_imports].concat(Tai_label.Create(l5));
  442. reference_reset_symbol(href,l4,0);
  443. asmlist[al_imports].concat(tai_const.create_sym_offset(href.symbol,href.offset));
  444. {$else ARM}
  445. reference_reset_symbol(href,l4,0);
  446. asmlist[al_imports].concat(Taicpu.Op_ref(A_JMP,S_NO,href));
  447. asmlist[al_imports].concat(Tai_align.Create_op(4,$90));
  448. {$endif ARM}
  449. if (cs_debuginfo in aktmoduleswitches) and
  450. assigned(hp2.procdef) then
  451. debuginfo.insertdef(asmlist[al_imports],hp2.procdef);
  452. { add jump field to al_imports }
  453. new_section(asmlist[al_imports],sec_idata5,'',0);
  454. if (cs_debuginfo in aktmoduleswitches) then
  455. begin
  456. if assigned(hp2.name) then
  457. begin
  458. importname:='__imp_'+hp2.name^;
  459. suffix:=0;
  460. while assigned(objectlibrary.getasmsymbol(importname)) do
  461. begin
  462. inc(suffix);
  463. importname:='__imp_'+hp2.name^+'_'+tostr(suffix);
  464. end;
  465. asmlist[al_imports].concat(tai_symbol.createname(importname,AT_FUNCTION,4));
  466. end
  467. else
  468. begin
  469. importname:='__imp_by_ordinal'+tostr(hp2.ordnr);
  470. suffix:=0;
  471. while assigned(objectlibrary.getasmsymbol(importname)) do
  472. begin
  473. inc(suffix);
  474. importname:='__imp_by_ordinal'+tostr(hp2.ordnr)+'_'+tostr(suffix);
  475. end;
  476. asmlist[al_imports].concat(tai_symbol.createname(importname,AT_FUNCTION,4));
  477. end;
  478. end;
  479. asmlist[al_imports].concat(Tai_label.Create(l4));
  480. end
  481. else
  482. begin
  483. asmlist[al_imports].concat(Tai_symbol.Createname_global(hp2.func^,AT_DATA,0));
  484. end;
  485. asmlist[al_imports].concat(Tai_const.Create_rva_sym(hp2.lab));
  486. hp2:=twin32imported_item(hp2.next);
  487. end;
  488. { finalize the addresses }
  489. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  490. { finally the import information }
  491. new_section(asmlist[al_imports],sec_idata6,'',0);
  492. hp2:=twin32imported_item(hp1.imported_items.first);
  493. while assigned(hp2) do
  494. begin
  495. asmlist[al_imports].concat(Tai_label.Create(hp2.lab));
  496. { the ordinal number }
  497. asmlist[al_imports].concat(Tai_const.Create_16bit(hp2.ordnr));
  498. asmlist[al_imports].concat(Tai_string.Create(hp2.name^+#0));
  499. asmlist[al_imports].concat(Tai_align.Create_op(2,0));
  500. hp2:=twin32imported_item(hp2.next);
  501. end;
  502. { create import dll name }
  503. new_section(asmlist[al_imports],sec_idata7,'',0);
  504. asmlist[al_imports].concat(Tai_label.Create(l1));
  505. asmlist[al_imports].concat(Tai_string.Create(hp1.dllname^+#0));
  506. hp1:=timportlist(hp1.next);
  507. end;
  508. end;
  509. {*****************************************************************************
  510. TEXPORTLIBWIN32
  511. *****************************************************************************}
  512. procedure texportlibwin32.preparelib(const s:string);
  513. begin
  514. if asmlist[al_exports]=nil then
  515. asmlist[al_exports]:=TAAsmoutput.create;
  516. EList_indexed:=tList.Create;
  517. EList_nonindexed:=tList.Create;
  518. objectlibrary.getdatalabel(edatalabel);
  519. end;
  520. procedure texportlibwin32.exportvar(hp : texported_item);
  521. begin
  522. { same code used !! PM }
  523. exportprocedure(hp);
  524. end;
  525. var
  526. Gl_DoubleIndex:boolean;
  527. Gl_DoubleIndexValue:longint;
  528. function IdxCompare(Item1, Item2: Pointer): Integer;
  529. var
  530. I1:texported_item absolute Item1;
  531. I2:texported_item absolute Item2;
  532. begin
  533. Result:=I1.index-I2.index;
  534. if(Result=0)and(Item1<>Item2)then
  535. begin
  536. Gl_DoubleIndex:=true;
  537. Gl_DoubleIndexValue:=I1.index;
  538. end;
  539. end;
  540. procedure texportlibwin32.exportprocedure(hp : texported_item);
  541. begin
  542. if ((hp.options and eo_index)<>0)and((hp.index<=0) or (hp.index>$ffff)) then
  543. begin
  544. message1(parser_e_export_invalid_index,tostr(hp.index));
  545. exit;
  546. end;
  547. if hp.options and eo_index=eo_index then
  548. EList_indexed.Add(hp)
  549. else
  550. EList_nonindexed.Add(hp);
  551. end;
  552. procedure texportlibwin32.exportfromlist(hp : texported_item);
  553. //formerly texportlibwin32.exportprocedure
  554. { must be ordered at least for win32 !! }
  555. var
  556. hp2 : texported_item;
  557. begin
  558. hp2:=texported_item(current_module._exports.first);
  559. while assigned(hp2) and
  560. (hp.name^>hp2.name^) do
  561. hp2:=texported_item(hp2.next);
  562. { insert hp there !! }
  563. if hp2=nil then
  564. current_module._exports.concat(hp)
  565. else
  566. begin
  567. if hp2.name^=hp.name^ then
  568. begin
  569. { this is not allowed !! }
  570. message1(parser_e_export_name_double,hp.name^);
  571. exit;
  572. end;
  573. current_module._exports.insertbefore(hp,hp2);
  574. end;
  575. end;
  576. procedure texportlibwin32.generatelib;
  577. var
  578. ordinal_base,ordinal_max,ordinal_min : longint;
  579. current_index : longint;
  580. entries,named_entries : longint;
  581. name_label,dll_name_label,export_address_table : tasmlabel;
  582. export_name_table_pointers,export_ordinal_table : tasmlabel;
  583. hp,hp2 : texported_item;
  584. temtexport : TLinkedList;
  585. address_table,name_table_pointers,
  586. name_table,ordinal_table : TAAsmoutput;
  587. i,autoindex,ni_high : longint;
  588. hole : boolean;
  589. begin
  590. Gl_DoubleIndex:=false;
  591. ELIst_indexed.Sort(@IdxCompare);
  592. if Gl_DoubleIndex then
  593. begin
  594. message1(parser_e_export_ordinal_double,tostr(Gl_DoubleIndexValue));
  595. EList_indexed.Free;
  596. EList_nonindexed.Free;
  597. exit;
  598. end;
  599. autoindex:=1;
  600. while EList_nonindexed.Count>0 do
  601. begin
  602. hole:=(EList_indexed.Count>0)and(texported_item(EList_indexed.Items[0]).index>1);
  603. if not hole then
  604. for i:=autoindex to pred(EList_indexed.Count)do
  605. if texported_item(EList_indexed.Items[i]).index-texported_item(EList_indexed.Items[pred(i)]).index>1 then
  606. begin
  607. autoindex:=succ(texported_item(EList_indexed.Items[pred(i)]).index);
  608. hole:=true;
  609. break;
  610. end;
  611. ni_high:=pred(EList_nonindexed.Count);
  612. if not hole then
  613. begin
  614. autoindex:=succ(EList_indexed.Count);
  615. EList_indexed.Add(EList_nonindexed.Items[ni_high]);
  616. end
  617. else
  618. EList_indexed.Insert(pred(AutoIndex),EList_nonindexed.Items[ni_high]);
  619. EList_nonindexed.Delete(ni_high);
  620. texported_item(EList_indexed.Items[pred(AutoIndex)]).index:=autoindex;
  621. end;
  622. EList_nonindexed.Free;
  623. for i:=0 to pred(EList_indexed.Count)do
  624. exportfromlist(texported_item(EList_indexed.Items[i]));
  625. EList_indexed.Free;
  626. if (target_asm.id in [as_i386_masm,as_i386_tasm,as_i386_nasmwin32]) then
  627. begin
  628. generatenasmlib;
  629. exit;
  630. end;
  631. hp:=texported_item(current_module._exports.first);
  632. if not assigned(hp) then
  633. exit;
  634. ordinal_max:=0;
  635. ordinal_min:=$7FFFFFFF;
  636. entries:=0;
  637. named_entries:=0;
  638. objectlibrary.getjumplabel(dll_name_label);
  639. objectlibrary.getjumplabel(export_address_table);
  640. objectlibrary.getjumplabel(export_name_table_pointers);
  641. objectlibrary.getjumplabel(export_ordinal_table);
  642. { count entries }
  643. while assigned(hp) do
  644. begin
  645. inc(entries);
  646. if (hp.index>ordinal_max) then
  647. ordinal_max:=hp.index;
  648. if (hp.index>0) and (hp.index<ordinal_min) then
  649. ordinal_min:=hp.index;
  650. if assigned(hp.name) then
  651. inc(named_entries);
  652. hp:=texported_item(hp.next);
  653. end;
  654. { no support for higher ordinal base yet !! }
  655. ordinal_base:=1;
  656. current_index:=ordinal_base;
  657. { we must also count the holes !! }
  658. entries:=ordinal_max-ordinal_base+1;
  659. new_section(asmlist[al_exports],sec_edata,'',0);
  660. { create label to reference from main so smartlink will include
  661. the .edata section }
  662. asmlist[al_exports].concat(Tai_symbol.Create_global(edatalabel,0));
  663. { export flags }
  664. asmlist[al_exports].concat(Tai_const.Create_32bit(0));
  665. { date/time stamp }
  666. asmlist[al_exports].concat(Tai_const.Create_32bit(0));
  667. { major version }
  668. asmlist[al_exports].concat(Tai_const.Create_16bit(0));
  669. { minor version }
  670. asmlist[al_exports].concat(Tai_const.Create_16bit(0));
  671. { pointer to dll name }
  672. asmlist[al_exports].concat(Tai_const.Create_rva_sym(dll_name_label));
  673. { ordinal base normally set to 1 }
  674. asmlist[al_exports].concat(Tai_const.Create_32bit(ordinal_base));
  675. { number of entries }
  676. asmlist[al_exports].concat(Tai_const.Create_32bit(entries));
  677. { number of named entries }
  678. asmlist[al_exports].concat(Tai_const.Create_32bit(named_entries));
  679. { address of export address table }
  680. asmlist[al_exports].concat(Tai_const.Create_rva_sym(export_address_table));
  681. { address of name pointer pointers }
  682. asmlist[al_exports].concat(Tai_const.Create_rva_sym(export_name_table_pointers));
  683. { address of ordinal number pointers }
  684. asmlist[al_exports].concat(Tai_const.Create_rva_sym(export_ordinal_table));
  685. { the name }
  686. asmlist[al_exports].concat(Tai_label.Create(dll_name_label));
  687. if st='' then
  688. asmlist[al_exports].concat(Tai_string.Create(current_module.modulename^+target_info.sharedlibext+#0))
  689. else
  690. asmlist[al_exports].concat(Tai_string.Create(st+target_info.sharedlibext+#0));
  691. { export address table }
  692. address_table:=TAAsmoutput.create;
  693. address_table.concat(Tai_align.Create_op(4,0));
  694. address_table.concat(Tai_label.Create(export_address_table));
  695. name_table_pointers:=TAAsmoutput.create;
  696. name_table_pointers.concat(Tai_align.Create_op(4,0));
  697. name_table_pointers.concat(Tai_label.Create(export_name_table_pointers));
  698. ordinal_table:=TAAsmoutput.create;
  699. ordinal_table.concat(Tai_align.Create_op(4,0));
  700. ordinal_table.concat(Tai_label.Create(export_ordinal_table));
  701. name_table:=TAAsmoutput.Create;
  702. name_table.concat(Tai_align.Create_op(4,0));
  703. { write each address }
  704. hp:=texported_item(current_module._exports.first);
  705. while assigned(hp) do
  706. begin
  707. if (hp.options and eo_name)<>0 then
  708. begin
  709. objectlibrary.getjumplabel(name_label);
  710. name_table_pointers.concat(Tai_const.Create_rva_sym(name_label));
  711. ordinal_table.concat(Tai_const.Create_16bit(hp.index-ordinal_base));
  712. name_table.concat(Tai_align.Create_op(2,0));
  713. name_table.concat(Tai_label.Create(name_label));
  714. name_table.concat(Tai_string.Create(hp.name^+#0));
  715. end;
  716. hp:=texported_item(hp.next);
  717. end;
  718. { order in increasing ordinal values }
  719. { into temtexport list }
  720. temtexport:=TLinkedList.Create;
  721. hp:=texported_item(current_module._exports.first);
  722. while assigned(hp) do
  723. begin
  724. current_module._exports.remove(hp);
  725. hp2:=texported_item(temtexport.first);
  726. while assigned(hp2) and (hp.index>hp2.index) do
  727. hp2:=texported_item(hp2.next);
  728. if hp2=nil then
  729. temtexport.concat(hp)
  730. else
  731. temtexport.insertbefore(hp,hp2);
  732. hp:=texported_item(current_module._exports.first);;
  733. end;
  734. { write the export adress table }
  735. current_index:=ordinal_base;
  736. hp:=texported_item(temtexport.first);
  737. while assigned(hp) do
  738. begin
  739. { fill missing values }
  740. while current_index<hp.index do
  741. begin
  742. address_table.concat(Tai_const.Create_32bit(0));
  743. inc(current_index);
  744. end;
  745. case hp.sym.typ of
  746. globalvarsym :
  747. address_table.concat(Tai_const.Createname_rva(tglobalvarsym(hp.sym).mangledname));
  748. typedconstsym :
  749. address_table.concat(Tai_const.Createname_rva(ttypedconstsym(hp.sym).mangledname));
  750. procsym :
  751. address_table.concat(Tai_const.Createname_rva(tprocsym(hp.sym).first_procdef.mangledname));
  752. end;
  753. inc(current_index);
  754. hp:=texported_item(hp.next);
  755. end;
  756. asmlist[al_exports].concatlist(address_table);
  757. asmlist[al_exports].concatlist(name_table_pointers);
  758. asmlist[al_exports].concatlist(ordinal_table);
  759. asmlist[al_exports].concatlist(name_table);
  760. address_table.Free;
  761. name_table_pointers.free;
  762. ordinal_table.free;
  763. name_table.free;
  764. temtexport.free;
  765. end;
  766. procedure texportlibwin32.generatenasmlib;
  767. var
  768. hp : texported_item;
  769. p : pchar;
  770. s : string;
  771. begin
  772. new_section(asmlist[al_exports],sec_code,'',0);
  773. hp:=texported_item(current_module._exports.first);
  774. while assigned(hp) do
  775. begin
  776. case hp.sym.typ of
  777. globalvarsym :
  778. s:=tglobalvarsym(hp.sym).mangledname;
  779. typedconstsym :
  780. s:=ttypedconstsym(hp.sym).mangledname;
  781. procsym :
  782. s:=tprocsym(hp.sym).first_procdef.mangledname;
  783. else
  784. s:='';
  785. end;
  786. p:=strpnew(#9+'export '+s+' '+hp.name^+' '+tostr(hp.index));
  787. {asmlist[al_exports].concat(tai_direct.create(p));}
  788. hp:=texported_item(hp.next);
  789. end;
  790. end;
  791. {****************************************************************************
  792. TLINKERWIN32
  793. ****************************************************************************}
  794. Constructor TLinkerWin32.Create;
  795. begin
  796. Inherited Create;
  797. { allow duplicated libs (PM) }
  798. SharedLibFiles.doubles:=true;
  799. StaticLibFiles.doubles:=true;
  800. end;
  801. Procedure TLinkerWin32.SetDefaultInfo;
  802. var
  803. targetopts: string;
  804. begin
  805. with Info do
  806. begin
  807. {$ifdef ARM}
  808. targetopts:='-m armpe';
  809. {$else ARM}
  810. targetopts:='-b pe-i386 -m i386pe';
  811. {$endif ARM}
  812. ExeCmd[1]:='ld '+targetopts+' $OPT $STRIP $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  813. DllCmd[1]:='ld '+targetopts+' $OPT $STRIP --dll $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  814. { ExeCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF';
  815. use short forms to avoid 128 char limitation problem }
  816. ExeCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  817. ExeCmd[3]:='ld '+targetopts+' $OPT $STRIP $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  818. { DllCmd[2]:='dlltool --as $ASBIN --dllname $EXE --output-exp exp.$$$ $RELOC $DEF'; }
  819. DllCmd[2]:='dlltool -S $ASBIN -D $EXE -e exp.$$$ $RELOC $DEF';
  820. DllCmd[3]:='ld '+targetopts+' $OPT $STRIP --dll $APPTYPE $IMAGEBASE -o $EXE $RES exp.$$$';
  821. end;
  822. end;
  823. Function TLinkerWin32.WriteResponseFile(isdll:boolean) : Boolean;
  824. Var
  825. linkres : TLinkRes;
  826. HPath : TStringListItem;
  827. s,s2 : string;
  828. i : integer;
  829. linklibcygwin : boolean;
  830. begin
  831. WriteResponseFile:=False;
  832. linklibcygwin:=(SharedLibFiles.Find('cygwin')<>nil);
  833. { Open link.res file }
  834. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  835. { Write path to search libraries }
  836. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  837. while assigned(HPath) do
  838. begin
  839. LinkRes.Add('SEARCH_DIR('+MaybeQuoted(HPath.Str)+')');
  840. HPath:=TStringListItem(HPath.Next);
  841. end;
  842. HPath:=TStringListItem(LibrarySearchPath.First);
  843. while assigned(HPath) do
  844. begin
  845. LinkRes.Add('SEARCH_DIR('+MaybeQuoted(HPath.Str)+')');
  846. HPath:=TStringListItem(HPath.Next);
  847. end;
  848. { add objectfiles, start with prt0 always }
  849. { profiling of shared libraries is currently not supported }
  850. LinkRes.Add('INPUT(');
  851. if isdll then
  852. LinkRes.AddFileName(MaybeQuoted(FindObjectFile('wdllprt0','',false)))
  853. else
  854. if (cs_profile in aktmoduleswitches) then
  855. LinkRes.AddFileName(MaybeQuoted(FindObjectFile('gprt0','',false)))
  856. else
  857. begin
  858. if linklibcygwin then
  859. LinkRes.AddFileName(MaybeQuoted(FindObjectFile('wcygprt0','',false)))
  860. else
  861. LinkRes.AddFileName(MaybeQuoted(FindObjectFile('wprt0','',false)));
  862. end;
  863. while not ObjectFiles.Empty do
  864. begin
  865. s:=ObjectFiles.GetFirst;
  866. if s<>'' then
  867. LinkRes.AddFileName(MaybeQuoted(s));
  868. end;
  869. LinkRes.Add(')');
  870. { Write staticlibraries }
  871. if (not StaticLibFiles.Empty) or (cs_profile in aktmoduleswitches) then
  872. begin
  873. LinkRes.Add('GROUP(');
  874. if (cs_profile in aktmoduleswitches) then
  875. begin
  876. LinkRes.Add('-lc');
  877. LinkRes.Add('-lgcc');
  878. LinkRes.Add('-lgmon');
  879. LinkRes.Add('-lkernel32');
  880. end;
  881. While not StaticLibFiles.Empty do
  882. begin
  883. S:=StaticLibFiles.GetFirst;
  884. LinkRes.AddFileName(MaybeQuoted(s));
  885. end;
  886. LinkRes.Add(')');
  887. end;
  888. { Write sharedlibraries }
  889. if not SharedLibFiles.Empty then
  890. begin
  891. LinkRes.Add('INPUT(') ;
  892. While not SharedLibFiles.Empty do
  893. begin
  894. S:=SharedLibFiles.GetFirst;
  895. if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  896. begin
  897. LinkRes.Add(MaybeQuoted(s2));
  898. continue;
  899. end;
  900. if pos(target_info.sharedlibprefix,s)=1 then
  901. s:=copy(s,length(target_info.sharedlibprefix)+1,255);
  902. i:=Pos(target_info.sharedlibext,S);
  903. if i>0 then
  904. Delete(S,i,255);
  905. LinkRes.Add('-l'+s);
  906. end;
  907. LinkRes.Add(')');
  908. end;
  909. { Write and Close response }
  910. linkres.writetodisk;
  911. LinkRes.Free;
  912. WriteResponseFile:=True;
  913. end;
  914. function TLinkerWin32.MakeExecutable:boolean;
  915. var
  916. binstr : String;
  917. cmdstr : TCmdStr;
  918. success : boolean;
  919. cmds,i : longint;
  920. AsBinStr : string[80];
  921. StripStr,
  922. RelocStr,
  923. AppTypeStr,
  924. ImageBaseStr : string[40];
  925. begin
  926. if not(cs_link_extern in aktglobalswitches) then
  927. Message1(exec_i_linking,current_module.exefilename^);
  928. { Create some replacements }
  929. RelocStr:='';
  930. AppTypeStr:='';
  931. ImageBaseStr:='';
  932. StripStr:='';
  933. AsBinStr:=FindUtil(utilsprefix+'as');
  934. if RelocSection then
  935. RelocStr:='--base-file base.$$$';
  936. if target_info.system in [system_arm_wince,system_i386_wince] then
  937. begin
  938. AppTypeStr:='--subsystem wince';
  939. if apptype <> app_gui then
  940. AppTypeStr:=AppTypeStr + ' --entry=mainCRTStartup';
  941. end
  942. else
  943. if apptype=app_gui then
  944. AppTypeStr:='--subsystem windows';
  945. if assigned(DLLImageBase) then
  946. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  947. if (cs_link_strip in aktglobalswitches) then
  948. StripStr:='-s';
  949. { Write used files and libraries }
  950. WriteResponseFile(false);
  951. { Call linker }
  952. success:=false;
  953. if RelocSection or (not Deffile.empty) then
  954. cmds:=3
  955. else
  956. cmds:=1;
  957. for i:=1 to cmds do
  958. begin
  959. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  960. if binstr<>'' then
  961. begin
  962. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  963. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  964. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  965. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  966. Replace(cmdstr,'$ASBIN',AsbinStr);
  967. Replace(cmdstr,'$RELOC',RelocStr);
  968. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  969. Replace(cmdstr,'$STRIP',StripStr);
  970. if not DefFile.Empty then
  971. begin
  972. DefFile.WriteFile;
  973. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  974. end
  975. else
  976. Replace(cmdstr,'$DEF','');
  977. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  978. if not success then
  979. break;
  980. end;
  981. end;
  982. { Post process }
  983. if success then
  984. success:=PostProcessExecutable(current_module.exefilename^,false);
  985. { Remove ReponseFile }
  986. if (success) and not(cs_link_extern in aktglobalswitches) then
  987. begin
  988. RemoveFile(outputexedir+Info.ResName);
  989. RemoveFile('base.$$$');
  990. RemoveFile('exp.$$$');
  991. RemoveFile('deffile.$$$');
  992. end;
  993. MakeExecutable:=success; { otherwise a recursive call to link method }
  994. end;
  995. Function TLinkerWin32.MakeSharedLibrary:boolean;
  996. var
  997. binstr : String;
  998. cmdstr : TCmdStr;
  999. success : boolean;
  1000. cmds,
  1001. i : longint;
  1002. AsBinStr : string[80];
  1003. StripStr,
  1004. RelocStr,
  1005. AppTypeStr,
  1006. ImageBaseStr : string[40];
  1007. begin
  1008. MakeSharedLibrary:=false;
  1009. if not(cs_link_extern in aktglobalswitches) then
  1010. Message1(exec_i_linking,current_module.sharedlibfilename^);
  1011. { Create some replacements }
  1012. RelocStr:='';
  1013. AppTypeStr:='';
  1014. ImageBaseStr:='';
  1015. StripStr:='';
  1016. AsBinStr:=FindUtil(utilsprefix+'as');
  1017. if RelocSection then
  1018. RelocStr:='--base-file base.$$$';
  1019. if apptype=app_gui then
  1020. AppTypeStr:='--subsystem windows';
  1021. if assigned(DLLImageBase) then
  1022. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  1023. if (cs_link_strip in aktglobalswitches) then
  1024. StripStr:='-s';
  1025. { Write used files and libraries }
  1026. WriteResponseFile(true);
  1027. { Call linker }
  1028. success:=false;
  1029. if RelocSection or (not Deffile.empty) then
  1030. cmds:=3
  1031. else
  1032. cmds:=1;
  1033. for i:=1 to cmds do
  1034. begin
  1035. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  1036. if binstr<>'' then
  1037. begin
  1038. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1039. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1040. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1041. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  1042. Replace(cmdstr,'$ASBIN',AsbinStr);
  1043. Replace(cmdstr,'$RELOC',RelocStr);
  1044. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  1045. Replace(cmdstr,'$STRIP',StripStr);
  1046. if not DefFile.Empty then
  1047. begin
  1048. DefFile.WriteFile;
  1049. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  1050. end
  1051. else
  1052. Replace(cmdstr,'$DEF','');
  1053. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  1054. if not success then
  1055. break;
  1056. end;
  1057. end;
  1058. { Post process }
  1059. if success then
  1060. success:=PostProcessExecutable(current_module.sharedlibfilename^,true);
  1061. { Remove ReponseFile }
  1062. if (success) and not(cs_link_extern in aktglobalswitches) then
  1063. begin
  1064. RemoveFile(outputexedir+Info.ResName);
  1065. RemoveFile('base.$$$');
  1066. RemoveFile('exp.$$$');
  1067. RemoveFile('deffile.$$$');
  1068. end;
  1069. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1070. end;
  1071. function tlinkerwin32.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1072. type
  1073. tdosheader = packed record
  1074. e_magic : word;
  1075. e_cblp : word;
  1076. e_cp : word;
  1077. e_crlc : word;
  1078. e_cparhdr : word;
  1079. e_minalloc : word;
  1080. e_maxalloc : word;
  1081. e_ss : word;
  1082. e_sp : word;
  1083. e_csum : word;
  1084. e_ip : word;
  1085. e_cs : word;
  1086. e_lfarlc : word;
  1087. e_ovno : word;
  1088. e_res : array[0..3] of word;
  1089. e_oemid : word;
  1090. e_oeminfo : word;
  1091. e_res2 : array[0..9] of word;
  1092. e_lfanew : longint;
  1093. end;
  1094. tpeheader = packed record
  1095. PEMagic : array[0..3] of char;
  1096. Machine : word;
  1097. NumberOfSections : word;
  1098. TimeDateStamp : longint;
  1099. PointerToSymbolTable : longint;
  1100. NumberOfSymbols : longint;
  1101. SizeOfOptionalHeader : word;
  1102. Characteristics : word;
  1103. Magic : word;
  1104. MajorLinkerVersion : byte;
  1105. MinorLinkerVersion : byte;
  1106. SizeOfCode : longint;
  1107. SizeOfInitializedData : longint;
  1108. SizeOfUninitializedData : longint;
  1109. AddressOfEntryPoint : longint;
  1110. BaseOfCode : longint;
  1111. BaseOfData : longint;
  1112. ImageBase : longint;
  1113. SectionAlignment : longint;
  1114. FileAlignment : longint;
  1115. MajorOperatingSystemVersion : word;
  1116. MinorOperatingSystemVersion : word;
  1117. MajorImageVersion : word;
  1118. MinorImageVersion : word;
  1119. MajorSubsystemVersion : word;
  1120. MinorSubsystemVersion : word;
  1121. Reserved1 : longint;
  1122. SizeOfImage : longint;
  1123. SizeOfHeaders : longint;
  1124. CheckSum : longint;
  1125. Subsystem : word;
  1126. DllCharacteristics : word;
  1127. SizeOfStackReserve : longint;
  1128. SizeOfStackCommit : longint;
  1129. SizeOfHeapReserve : longint;
  1130. SizeOfHeapCommit : longint;
  1131. LoaderFlags : longint;
  1132. NumberOfRvaAndSizes : longint;
  1133. DataDirectory : array[1..$80] of byte;
  1134. end;
  1135. tcoffsechdr=packed record
  1136. name : array[0..7] of char;
  1137. vsize : longint;
  1138. rvaofs : longint;
  1139. datalen : longint;
  1140. datapos : longint;
  1141. relocpos : longint;
  1142. lineno1 : longint;
  1143. nrelocs : word;
  1144. lineno2 : word;
  1145. flags : longint;
  1146. end;
  1147. psecfill=^TSecfill;
  1148. TSecfill=record
  1149. fillpos,
  1150. fillsize : longint;
  1151. next : psecfill;
  1152. end;
  1153. var
  1154. f : file;
  1155. cmdstr : string;
  1156. dosheader : tdosheader;
  1157. peheader : tpeheader;
  1158. firstsecpos,
  1159. maxfillsize,
  1160. l,peheaderpos : longint;
  1161. coffsec : tcoffsechdr;
  1162. secroot,hsecroot : psecfill;
  1163. zerobuf : pointer;
  1164. begin
  1165. postprocessexecutable:=false;
  1166. { when -s is used or it's a dll then quit }
  1167. if (cs_link_extern in aktglobalswitches) then
  1168. begin
  1169. case apptype of
  1170. app_native :
  1171. cmdstr:='--subsystem native';
  1172. app_gui :
  1173. cmdstr:='--subsystem gui';
  1174. app_cui :
  1175. cmdstr:='--subsystem console';
  1176. end;
  1177. if dllversion<>'' then
  1178. cmdstr:=cmdstr+' --version '+dllversion;
  1179. cmdstr:=cmdstr+' --input '+maybequoted(fn);
  1180. cmdstr:=cmdstr+' --stack '+tostr(stacksize);
  1181. DoExec(FindUtil(utilsprefix+'postw32'),cmdstr,false,false);
  1182. postprocessexecutable:=true;
  1183. exit;
  1184. end;
  1185. { open file }
  1186. assign(f,fn);
  1187. {$I-}
  1188. reset(f,1);
  1189. if ioresult<>0 then
  1190. Message1(execinfo_f_cant_open_executable,fn);
  1191. { read headers }
  1192. blockread(f,dosheader,sizeof(tdosheader));
  1193. peheaderpos:=dosheader.e_lfanew;
  1194. seek(f,peheaderpos);
  1195. blockread(f,peheader,sizeof(tpeheader));
  1196. { write info }
  1197. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  1198. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  1199. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  1200. { change stack size (PM) }
  1201. { I am not sure that the default value is adequate !! }
  1202. peheader.SizeOfStackReserve:=stacksize;
  1203. { change the header }
  1204. { sub system }
  1205. { gui=2 }
  1206. { cui=3 }
  1207. { wincegui=9 }
  1208. if target_info.system in [system_arm_wince,system_i386_wince] then
  1209. peheader.Subsystem:=9
  1210. else
  1211. case apptype of
  1212. app_native :
  1213. peheader.Subsystem:=1;
  1214. app_gui :
  1215. peheader.Subsystem:=2;
  1216. app_cui :
  1217. peheader.Subsystem:=3;
  1218. end;
  1219. if dllversion<>'' then
  1220. begin
  1221. peheader.MajorImageVersion:=dllmajor;
  1222. peheader.MinorImageVersion:=dllminor;
  1223. end;
  1224. { reset timestamp }
  1225. peheader.TimeDateStamp:=0;
  1226. { write header back }
  1227. seek(f,peheaderpos);
  1228. blockwrite(f,peheader,sizeof(tpeheader));
  1229. if ioresult<>0 then
  1230. Message1(execinfo_f_cant_process_executable,fn);
  1231. seek(f,peheaderpos);
  1232. blockread(f,peheader,sizeof(tpeheader));
  1233. { write the value after the change }
  1234. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  1235. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  1236. { read section info }
  1237. maxfillsize:=0;
  1238. firstsecpos:=0;
  1239. secroot:=nil;
  1240. for l:=1 to peheader.NumberOfSections do
  1241. begin
  1242. blockread(f,coffsec,sizeof(tcoffsechdr));
  1243. if coffsec.datapos>0 then
  1244. begin
  1245. if secroot=nil then
  1246. firstsecpos:=coffsec.datapos;
  1247. new(hsecroot);
  1248. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  1249. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  1250. hsecroot^.next:=secroot;
  1251. secroot:=hsecroot;
  1252. if secroot^.fillsize>maxfillsize then
  1253. maxfillsize:=secroot^.fillsize;
  1254. end;
  1255. end;
  1256. if firstsecpos>0 then
  1257. begin
  1258. l:=firstsecpos-filepos(f);
  1259. if l>maxfillsize then
  1260. maxfillsize:=l;
  1261. end
  1262. else
  1263. l:=0;
  1264. { get zero buffer }
  1265. getmem(zerobuf,maxfillsize);
  1266. fillchar(zerobuf^,maxfillsize,0);
  1267. { zero from sectioninfo until first section }
  1268. blockwrite(f,zerobuf^,l);
  1269. { zero section alignments }
  1270. while assigned(secroot) do
  1271. begin
  1272. seek(f,secroot^.fillpos);
  1273. blockwrite(f,zerobuf^,secroot^.fillsize);
  1274. hsecroot:=secroot;
  1275. secroot:=secroot^.next;
  1276. dispose(hsecroot);
  1277. end;
  1278. freemem(zerobuf,maxfillsize);
  1279. close(f);
  1280. {$I+}
  1281. if ioresult<>0 then;
  1282. postprocessexecutable:=true;
  1283. end;
  1284. {****************************************************************************
  1285. TDLLScannerWin32
  1286. ****************************************************************************}
  1287. function tDLLScannerWin32.DOSstubOK(var x:cardinal):boolean;
  1288. begin
  1289. blockread(f,TheWord,2,loaded);
  1290. if loaded<>2 then
  1291. DOSstubOK:=false
  1292. else
  1293. begin
  1294. DOSstubOK:=(TheWord='MZ');
  1295. seek(f,$3C);
  1296. blockread(f,x,4,loaded);
  1297. if(loaded<>4)or(longint(x)>filesize(f))then
  1298. DOSstubOK:=false;
  1299. end;
  1300. end;
  1301. function TDLLScannerWin32.FindDLL(const s:string;var founddll:string):boolean;
  1302. var
  1303. sysdir : string;
  1304. Found : boolean;
  1305. begin
  1306. Found:=false;
  1307. { Look for DLL in:
  1308. 1. Current dir
  1309. 2. Library Path
  1310. 3. windir,windir/system,windir/system32 }
  1311. Found:=FindFile(s,'.'+source_info.DirSep,founddll);
  1312. if (not found) then
  1313. Found:=librarysearchpath.FindFile(s,founddll);
  1314. if (not found) then
  1315. begin
  1316. sysdir:=FixPath(GetEnv('windir'),false);
  1317. Found:=FindFile(s,sysdir+';'+sysdir+'system'+source_info.DirSep+';'+sysdir+'system32'+source_info.DirSep,founddll);
  1318. end;
  1319. if (not found) then
  1320. begin
  1321. message1(exec_w_libfile_not_found,s);
  1322. FoundDll:=s;
  1323. end;
  1324. FindDll:=Found;
  1325. end;
  1326. function tDLLScannerWin32.ExtractDllName(Const Name : string) : string;
  1327. var n : string;
  1328. begin
  1329. n:=Upper(SplitExtension(Name));
  1330. if (n='.DLL') or (n='.DRV') or (n='.EXE') then
  1331. ExtractDllName:=Name
  1332. else
  1333. ExtractDllName:=Name+target_info.sharedlibext;
  1334. end;
  1335. function tDLLScannerWin32.isSuitableFileType(x:cardinal):longbool;
  1336. begin
  1337. seek(f,x);
  1338. blockread(f,TheWord,2,loaded);
  1339. isSuitableFileType:=(loaded=2)and(TheWord='PE');
  1340. end;
  1341. function tDLLScannerWin32.GetEdata(HeaderEntry:cardinal):longbool;
  1342. type
  1343. TObjInfo=packed record
  1344. ObjName:array[0..7]of char;
  1345. VirtSize,
  1346. VirtAddr,
  1347. RawSize,
  1348. RawOffset,
  1349. Reloc,
  1350. LineNum:cardinal;
  1351. RelCount,
  1352. LineCount:word;
  1353. flags:cardinal;
  1354. end;
  1355. var
  1356. i:cardinal;
  1357. ObjOfs:cardinal;
  1358. Obj:TObjInfo;
  1359. APE_obj,APE_Optsize:word;
  1360. ExportRVA:cardinal;
  1361. delta:cardinal;
  1362. const
  1363. IMAGE_SCN_CNT_CODE=$00000020;
  1364. var
  1365. _d:dirstr;
  1366. _n:namestr;
  1367. _e:extstr;
  1368. function isUsedFunction(name:pchar):longbool;
  1369. var
  1370. hp:tExternalsItem;
  1371. begin
  1372. isUsedFunction:=false;
  1373. hp:=tExternalsItem(current_module.Externals.first);
  1374. while assigned(hp)do
  1375. begin
  1376. if(assigned(hp.data))and(not hp.found)then
  1377. if hp.data^=StrPas(name)then
  1378. begin
  1379. isUsedFunction:=true;
  1380. hp.found:=true;
  1381. exit;
  1382. end;
  1383. hp:=tExternalsItem(hp.next);
  1384. end;
  1385. end;
  1386. procedure Store(index:cardinal;name:pchar;isData:longbool);
  1387. begin
  1388. if not isUsedFunction(name)then
  1389. exit;
  1390. if not(current_module.uses_imports) then
  1391. begin
  1392. current_module.uses_imports:=true;
  1393. importlib.preparelib(current_module.modulename^);
  1394. end;
  1395. if IsData then
  1396. timportlibwin32(importlib).importvariable_str(name,_n,name)
  1397. else
  1398. timportlibwin32(importlib).importprocedure_str(name,_n,index,name);
  1399. end;
  1400. procedure ProcessEdata;
  1401. type
  1402. a8=array[0..7]of char;
  1403. function GetSectionName(rva:cardinal;var Flags:cardinal):a8;
  1404. var
  1405. i:cardinal;
  1406. LocObjOfs:cardinal;
  1407. LocObj:TObjInfo;
  1408. begin
  1409. GetSectionName:='';
  1410. Flags:=0;
  1411. LocObjOfs:=APE_OptSize+HeaderOffset+24;
  1412. for i:=1 to APE_obj do
  1413. begin
  1414. seek(f,LocObjOfs);
  1415. blockread(f,LocObj,sizeof(LocObj));
  1416. if(rva>=LocObj.VirtAddr)and(rva<=LocObj.VirtAddr+LocObj.RawSize)then
  1417. begin
  1418. GetSectionName:=a8(LocObj.ObjName);
  1419. Flags:=LocObj.flags;
  1420. end;
  1421. end;
  1422. end;
  1423. var
  1424. j,Fl:cardinal;
  1425. ulongval,procEntry:cardinal;
  1426. Ordinal:word;
  1427. isData:longbool;
  1428. ExpDir:packed record
  1429. flag,
  1430. stamp:cardinal;
  1431. Major,
  1432. Minor:word;
  1433. Name,
  1434. Base,
  1435. NumFuncs,
  1436. NumNames,
  1437. AddrFuncs,
  1438. AddrNames,
  1439. AddrOrds:cardinal;
  1440. end;
  1441. begin
  1442. with Obj do
  1443. begin
  1444. seek(f,RawOffset+delta);
  1445. blockread(f,ExpDir,sizeof(ExpDir));
  1446. fsplit(impname,_d,_n,_e);
  1447. for j:=0 to pred(ExpDir.NumNames)do
  1448. begin
  1449. { Don't know why but this gives serious problems with overflow checking on }
  1450. {$IFOPT Q+}
  1451. {$DEFINE OVERFLOW_CHECK_WAS_ON}
  1452. {$ENDIF}
  1453. {$Q-}
  1454. seek(f,RawOffset-VirtAddr+ExpDir.AddrOrds+j*2);
  1455. blockread(f,Ordinal,2);
  1456. seek(f,RawOffset-VirtAddr+ExpDir.AddrFuncs+cardinal(Ordinal)*4);
  1457. blockread(f,ProcEntry,4);
  1458. seek(f,RawOffset-VirtAddr+ExpDir.AddrNames+j*4);
  1459. blockread(f,ulongval,4);
  1460. seek(f,RawOffset-VirtAddr+ulongval);
  1461. blockread(f,cstring,sizeof(cstring));
  1462. isData:=GetSectionName(procentry,Fl)='';
  1463. {$IFDEF OVERFLOW_CHECK_WAS_ON}
  1464. {$Q+}
  1465. {$ENDIF}
  1466. if not isData then
  1467. isData:=Fl and IMAGE_SCN_CNT_CODE<>IMAGE_SCN_CNT_CODE;
  1468. Store(succ(Ordinal),cstring,isData);
  1469. end;
  1470. end;
  1471. end;
  1472. begin
  1473. GetEdata:=false;
  1474. seek(f,HeaderEntry+120);
  1475. blockread(f,ExportRVA,4);
  1476. seek(f,HeaderEntry+6);
  1477. blockread(f,APE_Obj,2);
  1478. seek(f,HeaderEntry+20);
  1479. blockread(f,APE_OptSize,2);
  1480. ObjOfs:=APE_OptSize+HeaderOffset+24;
  1481. for i:=1 to APE_obj do
  1482. begin
  1483. seek(f,ObjOfs);
  1484. blockread(f,Obj,sizeof(Obj));
  1485. inc(ObjOfs,sizeof(Obj));
  1486. with Obj do
  1487. if(VirtAddr<=ExportRva)and(ExportRva<VirtAddr+VirtSize)then
  1488. begin
  1489. delta:=ExportRva-VirtAddr;
  1490. ProcessEdata;
  1491. GetEdata:=true;
  1492. end;
  1493. end;
  1494. end;
  1495. function tDLLScannerWin32.scan(const binname:string):longbool;
  1496. var
  1497. OldFileMode:longint;
  1498. hs,
  1499. foundimp : string;
  1500. begin
  1501. Scan:=false;
  1502. { is there already an import library the we will use that one }
  1503. if FindLibraryFile(binname,target_info.staticClibprefix,target_info.staticClibext,foundimp) then
  1504. exit;
  1505. { check if we can find the dll }
  1506. hs:=AddExtension(binname,target_info.sharedlibext);
  1507. if not FindDll(hs,impname) then
  1508. exit;
  1509. { read the dll file }
  1510. assign(f,impname);
  1511. OldFileMode:=filemode;
  1512. filemode:=0;
  1513. reset(f,1);
  1514. filemode:=OldFileMode;
  1515. if not DOSstubOK(HeaderOffset)then
  1516. scan:=false
  1517. else if not isSuitableFileType(HeaderOffset)then
  1518. scan:=false
  1519. else
  1520. scan:=GetEdata(HeaderOffset);
  1521. close(f);
  1522. end;
  1523. {*****************************************************************************
  1524. Initialize
  1525. *****************************************************************************}
  1526. initialization
  1527. {$ifdef i386}
  1528. RegisterExternalLinker(system_i386_win32_info,TLinkerWin32);
  1529. RegisterImport(system_i386_win32,TImportLibWin32);
  1530. RegisterExport(system_i386_win32,TExportLibWin32);
  1531. RegisterDLLScanner(system_i386_win32,TDLLScannerWin32);
  1532. RegisterRes(res_gnu_windres_info);
  1533. RegisterTarget(system_i386_win32_info);
  1534. RegisterExternalLinker(system_i386_wince_info,TLinkerWin32);
  1535. RegisterImport(system_i386_wince,TImportLibWin32);
  1536. RegisterExport(system_i386_wince,TExportLibWin32);
  1537. RegisterDLLScanner(system_i386_wince,TDLLScannerWin32);
  1538. RegisterTarget(system_i386_wince_info);
  1539. {$endif i386}
  1540. {$ifdef x86_64}
  1541. RegisterExternalLinker(system_x64_win64_info,TLinkerWin32);
  1542. RegisterImport(system_x86_64_win64,TImportLibWin32);
  1543. RegisterExport(system_x86_64_win64,TExportLibWin32);
  1544. RegisterDLLScanner(system_x86_64_win64,TDLLScannerWin32);
  1545. RegisterRes(res_gnu_windres_info);
  1546. RegisterTarget(system_x64_win64_info);
  1547. {$endif x86_64}
  1548. {$ifdef arm}
  1549. RegisterExternalLinker(system_arm_wince_info,TLinkerWin32);
  1550. RegisterImport(system_arm_wince,TImportLibWin32);
  1551. RegisterExport(system_arm_wince,TExportLibWin32);
  1552. RegisterRes(res_gnu_wince_windres_info);
  1553. RegisterTarget(system_arm_wince_info);
  1554. {$endif arm}
  1555. end.