t_win.pas 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i386) Win32 target
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit t_win;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. dos,
  23. cutils,cclasses,
  24. aasmbase,aasmtai,aasmcpu,fmodule,globtype,globals,systems,verbose,
  25. symconst,symdef,symsym,
  26. script,gendef,
  27. cpubase,
  28. import,export,link,cgobj,i_win;
  29. const
  30. MAX_DEFAULT_EXTENSIONS = 3;
  31. type
  32. tStr4=array[1..MAX_DEFAULT_EXTENSIONS] of string[4];
  33. pStr4=^tStr4;
  34. twin32imported_item = class(timported_item)
  35. procdef : tprocdef;
  36. end;
  37. timportlibwin32=class(timportlib)
  38. private
  39. procedure win32importproc(aprocdef:tprocdef;const func,module : string;index : longint;const name : string);
  40. procedure importvariable_str(const s:string;const name,module:string);
  41. procedure importprocedure_str(const func,module:string;index:longint;const name:string);
  42. public
  43. procedure preparelib(const s:string);override;
  44. procedure importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);override;
  45. procedure importvariable(vs:tglobalvarsym;const name,module:string);override;
  46. procedure generatelib;override;
  47. procedure generatenasmlib;virtual;
  48. procedure generatesmartlib;override;
  49. end;
  50. texportlibwin32=class(texportlib)
  51. st : string;
  52. EList_indexed:tList;
  53. EList_nonindexed:tList;
  54. procedure preparelib(const s:string);override;
  55. procedure exportprocedure(hp : texported_item);override;
  56. procedure exportvar(hp : texported_item);override;
  57. procedure exportfromlist(hp : texported_item);
  58. procedure generatelib;override;
  59. procedure generatenasmlib;virtual;
  60. end;
  61. tlinkerwin32=class(texternallinker)
  62. private
  63. Function WriteResponseFile(isdll:boolean) : Boolean;
  64. Function PostProcessExecutable(const fn:string;isdll:boolean) : Boolean;
  65. public
  66. Constructor Create;override;
  67. Procedure SetDefaultInfo;override;
  68. function MakeExecutable:boolean;override;
  69. function MakeSharedLibrary:boolean;override;
  70. end;
  71. tDLLScannerWin32=class(tDLLScanner)
  72. private
  73. cstring : array[0..127]of char;
  74. function DOSstubOK(var x:cardinal):boolean;
  75. function 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 asmlist[al_imports]=nil then
  103. asmlist[al_imports]:=TAAsmoutput.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_allways);
  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_allways);
  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(asmlist[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. asmlist[al_imports].concat(tai_directive.create(asd_extern,hp2.func^));
  224. asmlist[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. objectlibrary.getdatalabel(lhead);
  251. objectlibrary.getdatalabel(lname);
  252. objectlibrary.getaddrlabel(lidata4);
  253. objectlibrary.getaddrlabel(lidata5);
  254. { create header for this importmodule }
  255. asmlist[al_imports].concat(Tai_cutobject.Create_begin);
  256. new_section(asmlist[al_imports],sec_idata2,'',0);
  257. asmlist[al_imports].concat(Tai_label.Create(lhead));
  258. { pointer to procedure names }
  259. asmlist[al_imports].concat(Tai_const.Create_rva_sym(lidata4));
  260. { two empty entries follow }
  261. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  262. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  263. { pointer to dll name }
  264. asmlist[al_imports].concat(Tai_const.Create_rva_sym(lname));
  265. { pointer to fixups }
  266. asmlist[al_imports].concat(Tai_const.Create_rva_sym(lidata5));
  267. { first write the name references }
  268. new_section(asmlist[al_imports],sec_idata4,'',0);
  269. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  270. asmlist[al_imports].concat(Tai_label.Create(lidata4));
  271. { then the addresses and create also the indirect jump }
  272. new_section(asmlist[al_imports],sec_idata5,'',0);
  273. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  274. asmlist[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. asmlist[al_imports].concat(Tai_cutobject.Create);
  281. { create indirect jump }
  282. if not hp2.is_var then
  283. begin
  284. objectlibrary.getjumplabel(lcode);
  285. {$ifdef ARM}
  286. objectlibrary.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(asmlist[al_imports],sec_code,'',0);
  291. if assigned(hp2.procdef) then
  292. mangledstring:=hp2.procdef.mangledname
  293. else
  294. mangledstring:=hp2.func^;
  295. asmlist[al_imports].concat(Tai_symbol.Createname_global(mangledstring,AT_FUNCTION,0));
  296. asmlist[al_imports].concat(Tai_function_name.Create(''));
  297. {$ifdef ARM}
  298. reference_reset_symbol(href,lpcode,0);
  299. asmlist[al_imports].concat(Taicpu.op_reg_ref(A_LDR,NR_R12,href));
  300. reference_reset_base(href,NR_R12,0);
  301. asmlist[al_imports].concat(Taicpu.op_reg_ref(A_LDR,NR_R15,href));
  302. asmlist[al_imports].concat(Tai_label.Create(lpcode));
  303. reference_reset_symbol(href,lcode,0);
  304. asmlist[al_imports].concat(tai_const.create_sym_offset(href.symbol,href.offset));
  305. {$else ARM}
  306. reference_reset_symbol(href,lcode,0);
  307. asmlist[al_imports].concat(Taicpu.Op_ref(A_JMP,S_NO,href));
  308. asmlist[al_imports].concat(Tai_align.Create_op(4,$90));
  309. {$endif ARM}
  310. end;
  311. { create head link }
  312. new_section(asmlist[al_imports],sec_idata7,'',0);
  313. asmlist[al_imports].concat(Tai_const.Create_rva_sym(lhead));
  314. { fixup }
  315. objectlibrary.getjumplabel(tasmlabel(hp2.lab));
  316. new_section(asmlist[al_imports],sec_idata4,'',0);
  317. asmlist[al_imports].concat(Tai_const.Create_rva_sym(hp2.lab));
  318. { add jump field to al_imports }
  319. new_section(asmlist[al_imports],sec_idata5,'',0);
  320. if hp2.is_var then
  321. asmlist[al_imports].concat(Tai_symbol.Createname_global(hp2.func^,AT_FUNCTION,0))
  322. else
  323. asmlist[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(objectlibrary.getasmsymbol(importname)) do
  331. begin
  332. inc(suffix);
  333. importname:='__imp_'+hp2.name^+'_'+tostr(suffix);
  334. end;
  335. asmlist[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(objectlibrary.getasmsymbol(importname)) do
  342. begin
  343. inc(suffix);
  344. importname:='__imp_by_ordinal'+tostr(hp2.ordnr)+'_'+tostr(suffix);
  345. end;
  346. asmlist[al_imports].concat(tai_symbol.createname(importname,AT_FUNCTION,4));
  347. end;
  348. end;
  349. if hp2.name^<>'' then
  350. asmlist[al_imports].concat(Tai_const.Create_rva_sym(hp2.lab))
  351. else
  352. asmlist[al_imports].concat(Tai_const.Create_32bit(longint($80000000) or longint(hp2.ordnr)));
  353. { finally the import information }
  354. new_section(asmlist[al_imports],sec_idata6,'',0);
  355. asmlist[al_imports].concat(Tai_label.Create(hp2.lab));
  356. asmlist[al_imports].concat(Tai_const.Create_16bit(hp2.ordnr));
  357. asmlist[al_imports].concat(Tai_string.Create(hp2.name^+#0));
  358. asmlist[al_imports].concat(Tai_align.Create_op(2,0));
  359. hp2:=twin32imported_item(hp2.next);
  360. end;
  361. { write final section }
  362. asmlist[al_imports].concat(Tai_cutobject.Create_end);
  363. { end of name references }
  364. new_section(asmlist[al_imports],sec_idata4,'',0);
  365. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  366. { end if addresses }
  367. new_section(asmlist[al_imports],sec_idata5,'',0);
  368. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  369. { dllname }
  370. new_section(asmlist[al_imports],sec_idata7,'',0);
  371. asmlist[al_imports].concat(Tai_label.Create(lname));
  372. asmlist[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(asmlist[al_imports],sec_code,'',sizeof(aint));
  396. { Get labels for the sections }
  397. objectlibrary.getjumplabel(l1);
  398. objectlibrary.getjumplabel(l2);
  399. objectlibrary.getjumplabel(l3);
  400. new_section(asmlist[al_imports],sec_idata2,'',0);
  401. { pointer to procedure names }
  402. asmlist[al_imports].concat(Tai_const.Create_rva_sym(l2));
  403. { two empty entries follow }
  404. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  405. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  406. { pointer to dll name }
  407. asmlist[al_imports].concat(Tai_const.Create_rva_sym(l1));
  408. { pointer to fixups }
  409. asmlist[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(asmlist[al_imports],sec_idata4,'',0);
  414. asmlist[al_imports].concat(Tai_label.Create(l2));
  415. hp2:=twin32imported_item(hp1.imported_items.first);
  416. while assigned(hp2) do
  417. begin
  418. objectlibrary.getjumplabel(tasmlabel(hp2.lab));
  419. if hp2.name^<>'' then
  420. asmlist[al_imports].concat(Tai_const.Create_rva_sym(hp2.lab))
  421. else
  422. asmlist[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. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  427. { then the addresses and create also the indirect jump }
  428. new_section(asmlist[al_imports],sec_idata5,'',0);
  429. asmlist[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. objectlibrary.getjumplabel(l4);
  436. {$ifdef ARM}
  437. objectlibrary.getjumplabel(l5);
  438. {$endif ARM}
  439. { create indirect jump and }
  440. { place jump in al_procedures }
  441. new_section(asmlist[al_imports],sec_code,'',0);
  442. if assigned(hp2.procdef) then
  443. mangledstring:=hp2.procdef.mangledname
  444. else
  445. mangledstring:=hp2.func^;
  446. asmlist[al_imports].concat(Tai_symbol.Createname_global(mangledstring,AT_FUNCTION,0));
  447. asmlist[al_imports].concat(tai_function_name.create(''));
  448. {$ifdef ARM}
  449. reference_reset_symbol(href,l5,0);
  450. asmlist[al_imports].concat(Taicpu.op_reg_ref(A_LDR,NR_R12,href));
  451. reference_reset_base(href,NR_R12,0);
  452. asmlist[al_imports].concat(Taicpu.op_reg_ref(A_LDR,NR_R15,href));
  453. asmlist[al_imports].concat(Tai_label.Create(l5));
  454. reference_reset_symbol(href,l4,0);
  455. asmlist[al_imports].concat(tai_const.create_sym_offset(href.symbol,href.offset));
  456. {$else ARM}
  457. reference_reset_symbol(href,l4,0);
  458. asmlist[al_imports].concat(Taicpu.Op_ref(A_JMP,S_NO,href));
  459. asmlist[al_imports].concat(Tai_align.Create_op(4,$90));
  460. {$endif ARM}
  461. { add jump field to al_imports }
  462. new_section(asmlist[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(objectlibrary.getasmsymbol(importname)) do
  470. begin
  471. inc(suffix);
  472. importname:='__imp_'+hp2.name^+'_'+tostr(suffix);
  473. end;
  474. asmlist[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(objectlibrary.getasmsymbol(importname)) do
  481. begin
  482. inc(suffix);
  483. importname:='__imp_by_ordinal'+tostr(hp2.ordnr)+'_'+tostr(suffix);
  484. end;
  485. asmlist[al_imports].concat(tai_symbol.createname(importname,AT_FUNCTION,4));
  486. end;
  487. end;
  488. asmlist[al_imports].concat(Tai_label.Create(l4));
  489. end
  490. else
  491. begin
  492. asmlist[al_imports].concat(Tai_symbol.Createname_global(hp2.func^,AT_DATA,0));
  493. end;
  494. asmlist[al_imports].concat(Tai_const.Create_rva_sym(hp2.lab));
  495. hp2:=twin32imported_item(hp2.next);
  496. end;
  497. { finalize the addresses }
  498. asmlist[al_imports].concat(Tai_const.Create_32bit(0));
  499. { finally the import information }
  500. new_section(asmlist[al_imports],sec_idata6,'',0);
  501. hp2:=twin32imported_item(hp1.imported_items.first);
  502. while assigned(hp2) do
  503. begin
  504. asmlist[al_imports].concat(Tai_label.Create(hp2.lab));
  505. { the ordinal number }
  506. asmlist[al_imports].concat(Tai_const.Create_16bit(hp2.ordnr));
  507. asmlist[al_imports].concat(Tai_string.Create(hp2.name^+#0));
  508. asmlist[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(asmlist[al_imports],sec_idata7,'',0);
  513. asmlist[al_imports].concat(Tai_label.Create(l1));
  514. asmlist[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 asmlist[al_exports]=nil then
  524. asmlist[al_exports]:=TAAsmoutput.create;
  525. EList_indexed:=tList.Create;
  526. EList_nonindexed:=tList.Create;
  527. objectlibrary.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 : TAAsmoutput;
  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. objectlibrary.getjumplabel(dll_name_label);
  648. objectlibrary.getjumplabel(export_address_table);
  649. objectlibrary.getjumplabel(export_name_table_pointers);
  650. objectlibrary.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(asmlist[al_exports],sec_edata,'',0);
  669. { create label to reference from main so smartlink will include
  670. the .edata section }
  671. asmlist[al_exports].concat(Tai_symbol.Create_global(edatalabel,0));
  672. { export flags }
  673. asmlist[al_exports].concat(Tai_const.Create_32bit(0));
  674. { date/time stamp }
  675. asmlist[al_exports].concat(Tai_const.Create_32bit(0));
  676. { major version }
  677. asmlist[al_exports].concat(Tai_const.Create_16bit(0));
  678. { minor version }
  679. asmlist[al_exports].concat(Tai_const.Create_16bit(0));
  680. { pointer to dll name }
  681. asmlist[al_exports].concat(Tai_const.Create_rva_sym(dll_name_label));
  682. { ordinal base normally set to 1 }
  683. asmlist[al_exports].concat(Tai_const.Create_32bit(ordinal_base));
  684. { number of entries }
  685. asmlist[al_exports].concat(Tai_const.Create_32bit(entries));
  686. { number of named entries }
  687. asmlist[al_exports].concat(Tai_const.Create_32bit(named_entries));
  688. { address of export address table }
  689. asmlist[al_exports].concat(Tai_const.Create_rva_sym(export_address_table));
  690. { address of name pointer pointers }
  691. asmlist[al_exports].concat(Tai_const.Create_rva_sym(export_name_table_pointers));
  692. { address of ordinal number pointers }
  693. asmlist[al_exports].concat(Tai_const.Create_rva_sym(export_ordinal_table));
  694. { the name }
  695. asmlist[al_exports].concat(Tai_label.Create(dll_name_label));
  696. if st='' then
  697. asmlist[al_exports].concat(Tai_string.Create(current_module.modulename^+target_info.sharedlibext+#0))
  698. else
  699. asmlist[al_exports].concat(Tai_string.Create(st+target_info.sharedlibext+#0));
  700. { export address table }
  701. address_table:=TAAsmoutput.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:=TAAsmoutput.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:=TAAsmoutput.create;
  708. ordinal_table.concat(Tai_align.Create_op(4,0));
  709. ordinal_table.concat(Tai_label.Create(export_ordinal_table));
  710. name_table:=TAAsmoutput.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. objectlibrary.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. asmlist[al_exports].concatlist(address_table);
  766. asmlist[al_exports].concatlist(name_table_pointers);
  767. asmlist[al_exports].concatlist(ordinal_table);
  768. asmlist[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(asmlist[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. {asmlist[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 $STRIP $APPTYPE $IMAGEBASE $RELOC -o $EXE $RES';
  822. DllCmd[1]:='ld '+targetopts+' $OPT $GCSECTIONS $STRIP --dll $APPTYPE $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 $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 $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('c');
  845. SharedLibFiles.Concat('gcc');
  846. SharedLibFiles.Concat('gmon');
  847. SharedLibFiles.Concat('kernel32');
  848. end;
  849. { Open link.res file }
  850. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  851. { Write path to search libraries }
  852. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  853. while assigned(HPath) do
  854. begin
  855. LinkRes.Add('SEARCH_DIR('+MaybeQuoted(HPath.Str)+')');
  856. HPath:=TStringListItem(HPath.Next);
  857. end;
  858. HPath:=TStringListItem(LibrarySearchPath.First);
  859. while assigned(HPath) do
  860. begin
  861. LinkRes.Add('SEARCH_DIR('+MaybeQuoted(HPath.Str)+')');
  862. HPath:=TStringListItem(HPath.Next);
  863. end;
  864. { add objectfiles, start with prt0 always }
  865. { profiling of shared libraries is currently not supported }
  866. LinkRes.Add('INPUT(');
  867. if isdll then
  868. LinkRes.AddFileName(MaybeQuoted(FindObjectFile('wdllprt0','',false)))
  869. else
  870. if (cs_profile in aktmoduleswitches) then
  871. LinkRes.AddFileName(MaybeQuoted(FindObjectFile('gprt0','',false)))
  872. else
  873. begin
  874. if linklibcygwin then
  875. LinkRes.AddFileName(MaybeQuoted(FindObjectFile('wcygprt0','',false)))
  876. else
  877. LinkRes.AddFileName(MaybeQuoted(FindObjectFile('wprt0','',false)));
  878. end;
  879. while not ObjectFiles.Empty do
  880. begin
  881. s:=ObjectFiles.GetFirst;
  882. if s<>'' then
  883. LinkRes.AddFileName(MaybeQuoted(s));
  884. end;
  885. LinkRes.Add(')');
  886. { Write staticlibraries }
  887. if (not StaticLibFiles.Empty) then
  888. begin
  889. LinkRes.Add('GROUP(');
  890. While not StaticLibFiles.Empty do
  891. begin
  892. S:=StaticLibFiles.GetFirst;
  893. LinkRes.AddFileName(MaybeQuoted(s));
  894. end;
  895. LinkRes.Add(')');
  896. end;
  897. { Write sharedlibraries (=import libraries) }
  898. if not SharedLibFiles.Empty then
  899. begin
  900. LinkRes.Add('INPUT(') ;
  901. While not SharedLibFiles.Empty do
  902. begin
  903. S:=SharedLibFiles.GetFirst;
  904. if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  905. begin
  906. LinkRes.Add(MaybeQuoted(s2));
  907. continue;
  908. end;
  909. if pos(target_info.sharedlibprefix,s)=1 then
  910. s:=copy(s,length(target_info.sharedlibprefix)+1,255);
  911. i:=Pos(target_info.sharedlibext,S);
  912. if i>0 then
  913. Delete(S,i,255);
  914. LinkRes.Add('-l'+s);
  915. end;
  916. LinkRes.Add(')');
  917. end;
  918. { Write DLLs (=direct DLL linking) }
  919. if not DLLFiles.Empty then
  920. begin
  921. LinkRes.Add('INPUT(') ;
  922. While not DLLFiles.Empty do
  923. begin
  924. s:=DLLFiles.GetFirst;
  925. if FindDLL(s,s2) then
  926. LinkRes.Add(MaybeQuoted(s2))
  927. else
  928. LinkRes.Add('-l'+s);
  929. end;
  930. LinkRes.Add(')');
  931. end;
  932. { Write and Close response }
  933. linkres.writetodisk;
  934. LinkRes.Free;
  935. WriteResponseFile:=True;
  936. end;
  937. function TLinkerWin32.MakeExecutable:boolean;
  938. var
  939. binstr : String;
  940. cmdstr : TCmdStr;
  941. success : boolean;
  942. cmds,i : longint;
  943. AsBinStr : string[80];
  944. GCSectionsStr,
  945. StripStr,
  946. RelocStr,
  947. AppTypeStr,
  948. ImageBaseStr : string[40];
  949. begin
  950. if not(cs_link_extern in aktglobalswitches) then
  951. Message1(exec_i_linking,current_module.exefilename^);
  952. { Create some replacements }
  953. RelocStr:='';
  954. AppTypeStr:='';
  955. ImageBaseStr:='';
  956. StripStr:='';
  957. GCSectionsStr:='';
  958. AsBinStr:=FindUtil(utilsprefix+'as');
  959. if RelocSection then
  960. RelocStr:='--base-file base.$$$';
  961. if (af_smartlink_sections in target_asm.flags) and
  962. (tf_smartlink_sections in target_info.flags) then
  963. GCSectionsStr:='--gc-sections';
  964. if target_info.system in [system_arm_wince,system_i386_wince] then
  965. begin
  966. AppTypeStr:='--subsystem wince';
  967. if apptype <> app_gui then
  968. AppTypeStr:=AppTypeStr + ' --entry=mainCRTStartup';
  969. end
  970. else
  971. if apptype=app_gui then
  972. AppTypeStr:='--subsystem windows';
  973. if assigned(DLLImageBase) then
  974. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  975. if (cs_link_strip in aktglobalswitches) then
  976. StripStr:='-s';
  977. { Write used files and libraries }
  978. WriteResponseFile(false);
  979. { Call linker }
  980. success:=false;
  981. if RelocSection or (not Deffile.empty) then
  982. cmds:=3
  983. else
  984. cmds:=1;
  985. for i:=1 to cmds do
  986. begin
  987. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  988. if binstr<>'' then
  989. begin
  990. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  991. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  992. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  993. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  994. Replace(cmdstr,'$ASBIN',AsbinStr);
  995. Replace(cmdstr,'$RELOC',RelocStr);
  996. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  997. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  998. Replace(cmdstr,'$STRIP',StripStr);
  999. if not DefFile.Empty then
  1000. begin
  1001. DefFile.WriteFile;
  1002. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  1003. end
  1004. else
  1005. Replace(cmdstr,'$DEF','');
  1006. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  1007. if not success then
  1008. break;
  1009. end;
  1010. end;
  1011. { Post process }
  1012. if success then
  1013. success:=PostProcessExecutable(current_module.exefilename^,false);
  1014. { Remove ReponseFile }
  1015. if (success) and not(cs_link_extern in aktglobalswitches) then
  1016. begin
  1017. RemoveFile(outputexedir+Info.ResName);
  1018. RemoveFile('base.$$$');
  1019. RemoveFile('exp.$$$');
  1020. RemoveFile('deffile.$$$');
  1021. end;
  1022. MakeExecutable:=success; { otherwise a recursive call to link method }
  1023. end;
  1024. Function TLinkerWin32.MakeSharedLibrary:boolean;
  1025. var
  1026. binstr : String;
  1027. cmdstr : TCmdStr;
  1028. success : boolean;
  1029. cmds,
  1030. i : longint;
  1031. AsBinStr : string[80];
  1032. StripStr,
  1033. RelocStr,
  1034. AppTypeStr,
  1035. ImageBaseStr : string[40];
  1036. begin
  1037. MakeSharedLibrary:=false;
  1038. if not(cs_link_extern in aktglobalswitches) then
  1039. Message1(exec_i_linking,current_module.sharedlibfilename^);
  1040. { Create some replacements }
  1041. RelocStr:='';
  1042. AppTypeStr:='';
  1043. ImageBaseStr:='';
  1044. StripStr:='';
  1045. AsBinStr:=FindUtil(utilsprefix+'as');
  1046. if RelocSection then
  1047. RelocStr:='--base-file base.$$$';
  1048. if apptype=app_gui then
  1049. AppTypeStr:='--subsystem windows';
  1050. if assigned(DLLImageBase) then
  1051. ImageBaseStr:='--image-base=0x'+DLLImageBase^;
  1052. if (cs_link_strip in aktglobalswitches) then
  1053. StripStr:='-s';
  1054. { Write used files and libraries }
  1055. WriteResponseFile(true);
  1056. { Call linker }
  1057. success:=false;
  1058. if RelocSection or (not Deffile.empty) then
  1059. cmds:=3
  1060. else
  1061. cmds:=1;
  1062. for i:=1 to cmds do
  1063. begin
  1064. SplitBinCmd(Info.DllCmd[i],binstr,cmdstr);
  1065. if binstr<>'' then
  1066. begin
  1067. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1068. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1069. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1070. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  1071. Replace(cmdstr,'$ASBIN',AsbinStr);
  1072. Replace(cmdstr,'$RELOC',RelocStr);
  1073. Replace(cmdstr,'$IMAGEBASE',ImageBaseStr);
  1074. Replace(cmdstr,'$STRIP',StripStr);
  1075. if not DefFile.Empty then
  1076. begin
  1077. DefFile.WriteFile;
  1078. Replace(cmdstr,'$DEF','-d '+maybequoted(deffile.fname));
  1079. end
  1080. else
  1081. Replace(cmdstr,'$DEF','');
  1082. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false);
  1083. if not success then
  1084. break;
  1085. end;
  1086. end;
  1087. { Post process }
  1088. if success then
  1089. success:=PostProcessExecutable(current_module.sharedlibfilename^,true);
  1090. { Remove ReponseFile }
  1091. if (success) and not(cs_link_extern in aktglobalswitches) then
  1092. begin
  1093. RemoveFile(outputexedir+Info.ResName);
  1094. RemoveFile('base.$$$');
  1095. RemoveFile('exp.$$$');
  1096. RemoveFile('deffile.$$$');
  1097. end;
  1098. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1099. end;
  1100. function tlinkerwin32.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1101. type
  1102. tdosheader = packed record
  1103. e_magic : word;
  1104. e_cblp : word;
  1105. e_cp : word;
  1106. e_crlc : word;
  1107. e_cparhdr : word;
  1108. e_minalloc : word;
  1109. e_maxalloc : word;
  1110. e_ss : word;
  1111. e_sp : word;
  1112. e_csum : word;
  1113. e_ip : word;
  1114. e_cs : word;
  1115. e_lfarlc : word;
  1116. e_ovno : word;
  1117. e_res : array[0..3] of word;
  1118. e_oemid : word;
  1119. e_oeminfo : word;
  1120. e_res2 : array[0..9] of word;
  1121. e_lfanew : longint;
  1122. end;
  1123. tpeheader = packed record
  1124. PEMagic : array[0..3] of char;
  1125. Machine : word;
  1126. NumberOfSections : word;
  1127. TimeDateStamp : longint;
  1128. PointerToSymbolTable : longint;
  1129. NumberOfSymbols : longint;
  1130. SizeOfOptionalHeader : word;
  1131. Characteristics : word;
  1132. Magic : word;
  1133. MajorLinkerVersion : byte;
  1134. MinorLinkerVersion : byte;
  1135. SizeOfCode : longint;
  1136. SizeOfInitializedData : longint;
  1137. SizeOfUninitializedData : longint;
  1138. AddressOfEntryPoint : longint;
  1139. BaseOfCode : longint;
  1140. BaseOfData : longint;
  1141. ImageBase : longint;
  1142. SectionAlignment : longint;
  1143. FileAlignment : longint;
  1144. MajorOperatingSystemVersion : word;
  1145. MinorOperatingSystemVersion : word;
  1146. MajorImageVersion : word;
  1147. MinorImageVersion : word;
  1148. MajorSubsystemVersion : word;
  1149. MinorSubsystemVersion : word;
  1150. Reserved1 : longint;
  1151. SizeOfImage : longint;
  1152. SizeOfHeaders : longint;
  1153. CheckSum : longint;
  1154. Subsystem : word;
  1155. DllCharacteristics : word;
  1156. SizeOfStackReserve : longint;
  1157. SizeOfStackCommit : longint;
  1158. SizeOfHeapReserve : longint;
  1159. SizeOfHeapCommit : longint;
  1160. LoaderFlags : longint;
  1161. NumberOfRvaAndSizes : longint;
  1162. DataDirectory : array[1..$80] of byte;
  1163. end;
  1164. tcoffsechdr=packed record
  1165. name : array[0..7] of char;
  1166. vsize : longint;
  1167. rvaofs : longint;
  1168. datalen : longint;
  1169. datapos : longint;
  1170. relocpos : longint;
  1171. lineno1 : longint;
  1172. nrelocs : word;
  1173. lineno2 : word;
  1174. flags : longint;
  1175. end;
  1176. psecfill=^TSecfill;
  1177. TSecfill=record
  1178. fillpos,
  1179. fillsize : longint;
  1180. next : psecfill;
  1181. end;
  1182. var
  1183. f : file;
  1184. cmdstr : string;
  1185. dosheader : tdosheader;
  1186. peheader : tpeheader;
  1187. firstsecpos,
  1188. maxfillsize,
  1189. l,peheaderpos : longint;
  1190. coffsec : tcoffsechdr;
  1191. secroot,hsecroot : psecfill;
  1192. zerobuf : pointer;
  1193. begin
  1194. postprocessexecutable:=false;
  1195. { when -s is used or it's a dll then quit }
  1196. if (cs_link_extern in aktglobalswitches) then
  1197. begin
  1198. case apptype of
  1199. app_native :
  1200. cmdstr:='--subsystem native';
  1201. app_gui :
  1202. cmdstr:='--subsystem gui';
  1203. app_cui :
  1204. cmdstr:='--subsystem console';
  1205. end;
  1206. if dllversion<>'' then
  1207. cmdstr:=cmdstr+' --version '+dllversion;
  1208. cmdstr:=cmdstr+' --input '+maybequoted(fn);
  1209. cmdstr:=cmdstr+' --stack '+tostr(stacksize);
  1210. DoExec(FindUtil(utilsprefix+'postw32'),cmdstr,false,false);
  1211. postprocessexecutable:=true;
  1212. exit;
  1213. end;
  1214. { open file }
  1215. assign(f,fn);
  1216. {$I-}
  1217. reset(f,1);
  1218. if ioresult<>0 then
  1219. Message1(execinfo_f_cant_open_executable,fn);
  1220. { read headers }
  1221. blockread(f,dosheader,sizeof(tdosheader));
  1222. peheaderpos:=dosheader.e_lfanew;
  1223. seek(f,peheaderpos);
  1224. blockread(f,peheader,sizeof(tpeheader));
  1225. { write info }
  1226. Message1(execinfo_x_codesize,tostr(peheader.SizeOfCode));
  1227. Message1(execinfo_x_initdatasize,tostr(peheader.SizeOfInitializedData));
  1228. Message1(execinfo_x_uninitdatasize,tostr(peheader.SizeOfUninitializedData));
  1229. { change stack size (PM) }
  1230. { I am not sure that the default value is adequate !! }
  1231. peheader.SizeOfStackReserve:=stacksize;
  1232. { change the header }
  1233. { sub system }
  1234. { gui=2 }
  1235. { cui=3 }
  1236. { wincegui=9 }
  1237. if target_info.system in [system_arm_wince,system_i386_wince] then
  1238. peheader.Subsystem:=9
  1239. else
  1240. case apptype of
  1241. app_native :
  1242. peheader.Subsystem:=1;
  1243. app_gui :
  1244. peheader.Subsystem:=2;
  1245. app_cui :
  1246. peheader.Subsystem:=3;
  1247. end;
  1248. if dllversion<>'' then
  1249. begin
  1250. peheader.MajorImageVersion:=dllmajor;
  1251. peheader.MinorImageVersion:=dllminor;
  1252. end;
  1253. { reset timestamp }
  1254. peheader.TimeDateStamp:=0;
  1255. { write header back }
  1256. seek(f,peheaderpos);
  1257. blockwrite(f,peheader,sizeof(tpeheader));
  1258. if ioresult<>0 then
  1259. Message1(execinfo_f_cant_process_executable,fn);
  1260. seek(f,peheaderpos);
  1261. blockread(f,peheader,sizeof(tpeheader));
  1262. { write the value after the change }
  1263. Message1(execinfo_x_stackreserve,tostr(peheader.SizeOfStackReserve));
  1264. Message1(execinfo_x_stackcommit,tostr(peheader.SizeOfStackCommit));
  1265. { read section info }
  1266. maxfillsize:=0;
  1267. firstsecpos:=0;
  1268. secroot:=nil;
  1269. for l:=1 to peheader.NumberOfSections do
  1270. begin
  1271. blockread(f,coffsec,sizeof(tcoffsechdr));
  1272. if coffsec.datapos>0 then
  1273. begin
  1274. if secroot=nil then
  1275. firstsecpos:=coffsec.datapos;
  1276. new(hsecroot);
  1277. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  1278. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  1279. hsecroot^.next:=secroot;
  1280. secroot:=hsecroot;
  1281. if secroot^.fillsize>maxfillsize then
  1282. maxfillsize:=secroot^.fillsize;
  1283. end;
  1284. end;
  1285. if firstsecpos>0 then
  1286. begin
  1287. l:=firstsecpos-filepos(f);
  1288. if l>maxfillsize then
  1289. maxfillsize:=l;
  1290. end
  1291. else
  1292. l:=0;
  1293. { get zero buffer }
  1294. getmem(zerobuf,maxfillsize);
  1295. fillchar(zerobuf^,maxfillsize,0);
  1296. { zero from sectioninfo until first section }
  1297. blockwrite(f,zerobuf^,l);
  1298. { zero section alignments }
  1299. while assigned(secroot) do
  1300. begin
  1301. seek(f,secroot^.fillpos);
  1302. blockwrite(f,zerobuf^,secroot^.fillsize);
  1303. hsecroot:=secroot;
  1304. secroot:=secroot^.next;
  1305. dispose(hsecroot);
  1306. end;
  1307. freemem(zerobuf,maxfillsize);
  1308. close(f);
  1309. {$I+}
  1310. if ioresult<>0 then;
  1311. postprocessexecutable:=true;
  1312. end;
  1313. {****************************************************************************
  1314. TDLLScannerWin32
  1315. ****************************************************************************}
  1316. function tDLLScannerWin32.DOSstubOK(var x:cardinal):boolean;
  1317. begin
  1318. blockread(f,TheWord,2,loaded);
  1319. if loaded<>2 then
  1320. DOSstubOK:=false
  1321. else
  1322. begin
  1323. DOSstubOK:=(TheWord='MZ');
  1324. seek(f,$3C);
  1325. blockread(f,x,4,loaded);
  1326. if(loaded<>4)or(longint(x)>filesize(f))then
  1327. DOSstubOK:=false;
  1328. end;
  1329. end;
  1330. function tDLLScannerWin32.ExtractDllName(Const Name : string) : string;
  1331. var n : string;
  1332. begin
  1333. n:=Upper(SplitExtension(Name));
  1334. if (n='.DLL') or (n='.DRV') or (n='.EXE') then
  1335. ExtractDllName:=Name
  1336. else
  1337. ExtractDllName:=Name+target_info.sharedlibext;
  1338. end;
  1339. function tDLLScannerWin32.isSuitableFileType(x:cardinal):longbool;
  1340. begin
  1341. seek(f,x);
  1342. blockread(f,TheWord,2,loaded);
  1343. isSuitableFileType:=(loaded=2)and(TheWord='PE');
  1344. end;
  1345. function tDLLScannerWin32.GetEdata(HeaderEntry:cardinal):longbool;
  1346. type
  1347. TObjInfo=packed record
  1348. ObjName:array[0..7]of char;
  1349. VirtSize,
  1350. VirtAddr,
  1351. RawSize,
  1352. RawOffset,
  1353. Reloc,
  1354. LineNum:cardinal;
  1355. RelCount,
  1356. LineCount:word;
  1357. flags:cardinal;
  1358. end;
  1359. var
  1360. i:cardinal;
  1361. ObjOfs:cardinal;
  1362. Obj:TObjInfo;
  1363. APE_obj,APE_Optsize:word;
  1364. ExportRVA:cardinal;
  1365. delta:cardinal;
  1366. const
  1367. IMAGE_SCN_CNT_CODE=$00000020;
  1368. var
  1369. _d:dirstr;
  1370. _n:namestr;
  1371. _e:extstr;
  1372. function isUsedFunction(name:pchar):longbool;
  1373. var
  1374. hp:tExternalsItem;
  1375. begin
  1376. isUsedFunction:=false;
  1377. hp:=tExternalsItem(current_module.Externals.first);
  1378. while assigned(hp)do
  1379. begin
  1380. if(assigned(hp.data))and(not hp.found)then
  1381. if hp.data^=StrPas(name)then
  1382. begin
  1383. isUsedFunction:=true;
  1384. hp.found:=true;
  1385. exit;
  1386. end;
  1387. hp:=tExternalsItem(hp.next);
  1388. end;
  1389. end;
  1390. procedure Store(index:cardinal;name:pchar;isData:longbool);
  1391. begin
  1392. if not isUsedFunction(name)then
  1393. exit;
  1394. if not(current_module.uses_imports) then
  1395. begin
  1396. current_module.uses_imports:=true;
  1397. importlib.preparelib(current_module.modulename^);
  1398. end;
  1399. if IsData then
  1400. timportlibwin32(importlib).importvariable_str(name,_n,name)
  1401. else
  1402. timportlibwin32(importlib).importprocedure_str(name,_n,index,name);
  1403. end;
  1404. procedure ProcessEdata;
  1405. type
  1406. a8=array[0..7]of char;
  1407. function GetSectionName(rva:cardinal;var Flags:cardinal):a8;
  1408. var
  1409. i:cardinal;
  1410. LocObjOfs:cardinal;
  1411. LocObj:TObjInfo;
  1412. begin
  1413. GetSectionName:='';
  1414. Flags:=0;
  1415. LocObjOfs:=APE_OptSize+HeaderOffset+24;
  1416. for i:=1 to APE_obj do
  1417. begin
  1418. seek(f,LocObjOfs);
  1419. blockread(f,LocObj,sizeof(LocObj));
  1420. if(rva>=LocObj.VirtAddr)and(rva<=LocObj.VirtAddr+LocObj.RawSize)then
  1421. begin
  1422. GetSectionName:=a8(LocObj.ObjName);
  1423. Flags:=LocObj.flags;
  1424. end;
  1425. end;
  1426. end;
  1427. var
  1428. j,Fl:cardinal;
  1429. ulongval,procEntry:cardinal;
  1430. Ordinal:word;
  1431. isData:longbool;
  1432. ExpDir:packed record
  1433. flag,
  1434. stamp:cardinal;
  1435. Major,
  1436. Minor:word;
  1437. Name,
  1438. Base,
  1439. NumFuncs,
  1440. NumNames,
  1441. AddrFuncs,
  1442. AddrNames,
  1443. AddrOrds:cardinal;
  1444. end;
  1445. begin
  1446. with Obj do
  1447. begin
  1448. seek(f,RawOffset+delta);
  1449. blockread(f,ExpDir,sizeof(ExpDir));
  1450. fsplit(impname,_d,_n,_e);
  1451. for j:=0 to pred(ExpDir.NumNames)do
  1452. begin
  1453. { Don't know why but this gives serious problems with overflow checking on }
  1454. {$IFOPT Q+}
  1455. {$DEFINE OVERFLOW_CHECK_WAS_ON}
  1456. {$ENDIF}
  1457. {$Q-}
  1458. seek(f,RawOffset-VirtAddr+ExpDir.AddrOrds+j*2);
  1459. blockread(f,Ordinal,2);
  1460. seek(f,RawOffset-VirtAddr+ExpDir.AddrFuncs+cardinal(Ordinal)*4);
  1461. blockread(f,ProcEntry,4);
  1462. seek(f,RawOffset-VirtAddr+ExpDir.AddrNames+j*4);
  1463. blockread(f,ulongval,4);
  1464. seek(f,RawOffset-VirtAddr+ulongval);
  1465. blockread(f,cstring,sizeof(cstring));
  1466. isData:=GetSectionName(procentry,Fl)='';
  1467. {$IFDEF OVERFLOW_CHECK_WAS_ON}
  1468. {$Q+}
  1469. {$ENDIF}
  1470. if not isData then
  1471. isData:=Fl and IMAGE_SCN_CNT_CODE<>IMAGE_SCN_CNT_CODE;
  1472. Store(succ(Ordinal),cstring,isData);
  1473. end;
  1474. end;
  1475. end;
  1476. begin
  1477. GetEdata:=false;
  1478. seek(f,HeaderEntry+120);
  1479. blockread(f,ExportRVA,4);
  1480. seek(f,HeaderEntry+6);
  1481. blockread(f,APE_Obj,2);
  1482. seek(f,HeaderEntry+20);
  1483. blockread(f,APE_OptSize,2);
  1484. ObjOfs:=APE_OptSize+HeaderOffset+24;
  1485. for i:=1 to APE_obj do
  1486. begin
  1487. seek(f,ObjOfs);
  1488. blockread(f,Obj,sizeof(Obj));
  1489. inc(ObjOfs,sizeof(Obj));
  1490. with Obj do
  1491. if(VirtAddr<=ExportRva)and(ExportRva<VirtAddr+VirtSize)then
  1492. begin
  1493. delta:=ExportRva-VirtAddr;
  1494. ProcessEdata;
  1495. GetEdata:=true;
  1496. end;
  1497. end;
  1498. end;
  1499. function tDLLScannerWin32.scan(const binname:string):longbool;
  1500. var
  1501. OldFileMode:longint;
  1502. hs,
  1503. foundimp : string;
  1504. begin
  1505. Scan:=false;
  1506. { is there already an import library the we will use that one }
  1507. if FindLibraryFile(binname,target_info.staticClibprefix,target_info.staticClibext,foundimp) then
  1508. exit;
  1509. { check if we can find the dll }
  1510. hs:=AddExtension(binname,target_info.sharedlibext);
  1511. if not FindDll(hs,impname) then
  1512. exit;
  1513. { read the dll file }
  1514. assign(f,impname);
  1515. OldFileMode:=filemode;
  1516. filemode:=0;
  1517. reset(f,1);
  1518. filemode:=OldFileMode;
  1519. if not DOSstubOK(HeaderOffset)then
  1520. scan:=false
  1521. else if not isSuitableFileType(HeaderOffset)then
  1522. scan:=false
  1523. else
  1524. scan:=GetEdata(HeaderOffset);
  1525. close(f);
  1526. end;
  1527. {*****************************************************************************
  1528. Initialize
  1529. *****************************************************************************}
  1530. initialization
  1531. {$ifdef i386}
  1532. RegisterExternalLinker(system_i386_win32_info,TLinkerWin32);
  1533. RegisterImport(system_i386_win32,TImportLibWin32);
  1534. RegisterExport(system_i386_win32,TExportLibWin32);
  1535. RegisterDLLScanner(system_i386_win32,TDLLScannerWin32);
  1536. RegisterRes(res_gnu_windres_info);
  1537. RegisterTarget(system_i386_win32_info);
  1538. RegisterExternalLinker(system_i386_wince_info,TLinkerWin32);
  1539. RegisterImport(system_i386_wince,TImportLibWin32);
  1540. RegisterExport(system_i386_wince,TExportLibWin32);
  1541. RegisterDLLScanner(system_i386_wince,TDLLScannerWin32);
  1542. RegisterTarget(system_i386_wince_info);
  1543. {$endif i386}
  1544. {$ifdef x86_64}
  1545. RegisterExternalLinker(system_x64_win64_info,TLinkerWin32);
  1546. RegisterImport(system_x86_64_win64,TImportLibWin32);
  1547. RegisterExport(system_x86_64_win64,TExportLibWin32);
  1548. RegisterDLLScanner(system_x86_64_win64,TDLLScannerWin32);
  1549. RegisterRes(res_gnu_windres_info);
  1550. RegisterTarget(system_x64_win64_info);
  1551. {$endif x86_64}
  1552. {$ifdef arm}
  1553. RegisterExternalLinker(system_arm_wince_info,TLinkerWin32);
  1554. RegisterImport(system_arm_wince,TImportLibWin32);
  1555. RegisterExport(system_arm_wince,TExportLibWin32);
  1556. RegisterRes(res_gnu_wince_windres_info);
  1557. RegisterTarget(system_arm_wince_info);
  1558. {$endif arm}
  1559. end.