t_win.pas 67 KB

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