t_win.pas 65 KB

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