pkgutil.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. {
  2. Copyright (c) 2013-2016 by Free Pascal Development Team
  3. This unit implements basic parts of the package system
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pkgutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. fmodule,fpkg,link,cstreams,cclasses;
  22. procedure createimportlibfromexternals;
  23. Function RewritePPU(const PPUFn:String;OutStream:TCStream):Boolean;
  24. procedure export_unit(u:tmodule);
  25. procedure load_packages;
  26. procedure add_package(const name:string;ignoreduplicates:boolean;direct:boolean);
  27. procedure add_package_unit_ref(package:tpackage);
  28. procedure add_package_libs(l:tlinker);
  29. procedure check_for_indirect_package_usages(modules:tlinkedlist);
  30. implementation
  31. uses
  32. sysutils,
  33. globtype,systems,
  34. cutils,
  35. globals,verbose,
  36. aasmbase,aasmdata,aasmcnst,
  37. symtype,symconst,symsym,symdef,symbase,symtable,
  38. psub,pdecsub,
  39. ppu,entfile,fpcp,
  40. export;
  41. procedure procexport(const s : string);
  42. var
  43. hp : texported_item;
  44. begin
  45. hp:=texported_item.create;
  46. hp.name:=stringdup(s);
  47. hp.options:=hp.options+[eo_name];
  48. exportlib.exportprocedure(hp);
  49. end;
  50. procedure varexport(const s : string);
  51. var
  52. hp : texported_item;
  53. begin
  54. hp:=texported_item.create;
  55. hp.name:=stringdup(s);
  56. hp.options:=hp.options+[eo_name];
  57. exportlib.exportvar(hp);
  58. end;
  59. procedure exportprocsym(sym:tprocsym;symtable:tsymtable);
  60. var
  61. i : longint;
  62. pd : tprocdef;
  63. begin
  64. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  65. begin
  66. pd:=tprocdef(tprocsym(sym).procdeflist[i]);
  67. if not(pd.proccalloption in [pocall_internproc]) and
  68. not (df_generic in pd.defoptions) and
  69. ((pd.procoptions*[po_external])=[]) and
  70. (
  71. (symtable.symtabletype in [globalsymtable,recordsymtable,objectsymtable]) or
  72. (
  73. (symtable.symtabletype=staticsymtable) and
  74. (
  75. ([po_public,po_has_public_name]*pd.procoptions<>[]) or
  76. (df_has_global_ref in pd.defoptions)
  77. )
  78. )
  79. ) then
  80. begin
  81. exportallprocdefnames(tprocsym(sym),pd,[eo_name,eo_no_sym_name]);
  82. end;
  83. end;
  84. end;
  85. procedure exportabstractrecorddef(def:tabstractrecorddef;symtable:tsymtable); forward;
  86. procedure exportabstractrecordsymproc(sym:tobject;arg:pointer);
  87. begin
  88. case tsym(sym).typ of
  89. typesym:
  90. begin
  91. case ttypesym(sym).typedef.typ of
  92. objectdef,
  93. recorddef:
  94. exportabstractrecorddef(tabstractrecorddef(ttypesym(sym).typedef),tsymtable(arg));
  95. else
  96. ;
  97. end;
  98. end;
  99. procsym:
  100. begin
  101. { don't export methods of interfaces }
  102. if is_interface(tdef(tabstractrecordsymtable(arg).defowner)) then
  103. exit;
  104. exportprocsym(tprocsym(sym),tsymtable(arg));
  105. end;
  106. staticvarsym:
  107. begin
  108. varexport(tsym(sym).mangledname);
  109. end;
  110. else
  111. ;
  112. end;
  113. end;
  114. procedure exportname(const s:tsymstr);
  115. var
  116. hp : texported_item;
  117. begin
  118. hp:=texported_item.create;
  119. hp.name:=stringdup(s);
  120. hp.options:=hp.options+[eo_name];
  121. exportlib.exportvar(hp);
  122. end;
  123. procedure exportabstractrecorddef(def:tabstractrecorddef;symtable:tsymtable);
  124. begin
  125. { for cross unit type aliases this might happen }
  126. if def.owner<>symtable then
  127. exit;
  128. { don't export generics or their nested types }
  129. if df_generic in def.defoptions then
  130. exit;
  131. def.symtable.SymList.ForEachCall(@exportabstractrecordsymproc,def.symtable);
  132. if def.typ=objectdef then
  133. begin
  134. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  135. exportname(tobjectdef(def).vmt_mangledname);
  136. if is_interface(def) then
  137. begin
  138. if assigned(tobjectdef(def).iidguid) then
  139. exportname(make_mangledname('IID',def.owner,def.objname^));
  140. exportname(make_mangledname('IIDSTR',def.owner,def.objname^));
  141. end;
  142. end;
  143. end;
  144. procedure export_typedef(def:tdef;symtable:tsymtable;global:boolean);
  145. begin
  146. if not (global or is_class(def)) or
  147. ([df_internal,df_generic]*def.defoptions<>[]) or
  148. { happens with type renaming declarations ("abc = xyz") }
  149. (def.owner<>symtable) then
  150. exit;
  151. if ds_rtti_table_written in def.defstates then
  152. exportname(def.rtti_mangledname(fullrtti));
  153. if (ds_init_table_written in def.defstates) and
  154. def.needs_separate_initrtti then
  155. exportname(def.rtti_mangledname(initrtti));
  156. case def.typ of
  157. recorddef,
  158. objectdef:
  159. exportabstractrecorddef(tabstractrecorddef(def),symtable);
  160. else
  161. ;
  162. end;
  163. end;
  164. procedure insert_export(sym : TObject;arg:pointer);
  165. var
  166. isglobal,
  167. publiconly : boolean;
  168. begin
  169. publiconly:=tsymtable(arg).symtabletype=staticsymtable;
  170. isglobal:=tsymtable(arg).symtabletype=globalsymtable;
  171. case TSym(sym).typ of
  172. { ignore: }
  173. unitsym,
  174. syssym,
  175. namespacesym,
  176. propertysym,
  177. enumsym:
  178. ;
  179. constsym:
  180. begin
  181. if tconstsym(sym).consttyp=constresourcestring then
  182. varexport(make_mangledname('RESSTR',tsym(sym).owner,tsym(sym).name));
  183. end;
  184. typesym:
  185. begin
  186. export_typedef(ttypesym(sym).typedef,tsymtable(arg),isglobal);
  187. end;
  188. procsym:
  189. begin
  190. exportprocsym(tprocsym(sym),tsymtable(arg));
  191. end;
  192. staticvarsym:
  193. begin
  194. if publiconly and ([vo_is_public,vo_has_global_ref]*tstaticvarsym(sym).varoptions=[]) then
  195. exit;
  196. varexport(tsym(sym).mangledname);
  197. end;
  198. absolutevarsym:
  199. ;
  200. else
  201. begin
  202. //writeln('unknown: ',TSym(sym).typ);
  203. internalerror(2016080501);
  204. end;
  205. end;
  206. end;
  207. procedure export_unit(u: tmodule);
  208. var
  209. i : longint;
  210. sym : tasmsymbol;
  211. begin
  212. u.globalsymtable.symlist.ForEachCall(@insert_export,u.globalsymtable);
  213. { check localsymtable for exports too to get public symbols }
  214. u.localsymtable.symlist.ForEachCall(@insert_export,u.localsymtable);
  215. { create special exports }
  216. if mf_init in u.moduleflags then
  217. procexport(make_mangledname('INIT$',u.globalsymtable,''));
  218. if mf_finalize in u.moduleflags then
  219. procexport(make_mangledname('FINALIZE$',u.globalsymtable,''));
  220. if mf_threadvars in u.moduleflags then
  221. varexport(make_mangledname('THREADVARLIST',u.globalsymtable,''));
  222. if mf_has_resourcestrings in u.moduleflags then
  223. begin
  224. varexport(ctai_typedconstbuilder.get_vectorized_dead_strip_section_symbol_start('RESSTR',u.localsymtable,[]).name);
  225. varexport(ctai_typedconstbuilder.get_vectorized_dead_strip_section_symbol_end('RESSTR',u.localsymtable,[]).name);
  226. end;
  227. if not (target_info.system in systems_indirect_var_imports) then
  228. for i:=0 to u.publicasmsyms.count-1 do
  229. begin
  230. sym:=tasmsymbol(u.publicasmsyms[i]);
  231. if sym.bind=AB_INDIRECT then
  232. varexport(sym.name);
  233. end;
  234. end;
  235. Function RewritePPU(const PPUFn:String;OutStream:TCStream):Boolean;
  236. Var
  237. MakeStatic : Boolean;
  238. Var
  239. buffer : array[0..$1fff] of byte;
  240. inppu,
  241. outppu : tppufile;
  242. b,
  243. untilb : byte;
  244. l,m : longint;
  245. ext,
  246. s : string;
  247. ppuversion : dword;
  248. begin
  249. Result:=false;
  250. MakeStatic:=False;
  251. inppu:=tppufile.create(PPUFn);
  252. if not inppu.openfile then
  253. begin
  254. inppu.free;
  255. Comment(V_Error,'Could not open : '+PPUFn);
  256. Exit;
  257. end;
  258. { Check the ppufile }
  259. if not inppu.CheckPPUId then
  260. begin
  261. inppu.free;
  262. Comment(V_Error,'Not a PPU File : '+PPUFn);
  263. Exit;
  264. end;
  265. ppuversion:=inppu.getversion;
  266. if ppuversion<>CurrentPPUVersion then
  267. begin
  268. inppu.free;
  269. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  270. Exit;
  271. end;
  272. { Already a lib? }
  273. if (inppu.header.common.flags and uf_in_library)<>0 then
  274. begin
  275. inppu.free;
  276. Comment(V_Error,'PPU is already in a library : '+PPUFn);
  277. Exit;
  278. end;
  279. { We need a static linked unit, but we also accept those without .o file }
  280. if (inppu.header.common.flags and (uf_static_linked or uf_no_link))=0 then
  281. begin
  282. inppu.free;
  283. Comment(V_Error,'PPU is not static linked : '+PPUFn);
  284. Exit;
  285. end;
  286. { Check if shared is allowed }
  287. if tsystem(inppu.header.common.target) in [system_i386_go32v2] then
  288. begin
  289. Comment(V_Error,'Shared library not supported for ppu target, switching to static library');
  290. MakeStatic:=true;
  291. end;
  292. { Create the new ppu }
  293. outppu:=tppufile.create(PPUFn);
  294. outppu.createstream(OutStream);
  295. { Create new header, with the new flags }
  296. outppu.header:=inppu.header;
  297. outppu.header.common.flags:=outppu.header.common.flags or uf_in_library;
  298. if MakeStatic then
  299. outppu.header.common.flags:=outppu.header.common.flags or uf_static_linked
  300. else
  301. outppu.header.common.flags:=outppu.header.common.flags or uf_shared_linked;
  302. { read until the object files are found }
  303. untilb:=iblinkunitofiles;
  304. repeat
  305. b:=inppu.readentry;
  306. if b in [ibendinterface,ibend] then
  307. begin
  308. inppu.free;
  309. outppu.free;
  310. Comment(V_Error,'No files to be linked found : '+PPUFn);
  311. Exit;
  312. end;
  313. if b<>untilb then
  314. begin
  315. repeat
  316. inppu.getdatabuf(buffer,sizeof(buffer),l);
  317. outppu.putdata(buffer,l);
  318. until l<sizeof(buffer);
  319. outppu.writeentry(b);
  320. end;
  321. until (b=untilb);
  322. { we have now reached the section for the files which need to be added,
  323. now add them to the list }
  324. case b of
  325. iblinkunitofiles :
  326. begin
  327. { add all o files, and save the entry when not creating a static
  328. library to keep staticlinking possible }
  329. while not inppu.endofentry do
  330. begin
  331. s:=inppu.getstring;
  332. m:=inppu.getlongint;
  333. if not MakeStatic then
  334. begin
  335. outppu.putstring(s);
  336. outppu.putlongint(m);
  337. end;
  338. current_module.linkotherofiles.add(s,link_always);;
  339. end;
  340. if not MakeStatic then
  341. outppu.writeentry(b);
  342. end;
  343. { iblinkunitstaticlibs :
  344. begin
  345. AddToLinkFiles(ExtractLib(inppu.getstring));
  346. if not inppu.endofentry then
  347. begin
  348. repeat
  349. inppu.getdatabuf(buffer^,bufsize,l);
  350. outppu.putdata(buffer^,l);
  351. until l<bufsize;
  352. outppu.writeentry(b);
  353. end;
  354. end; }
  355. end;
  356. { just add a new entry with the new lib }
  357. if MakeStatic then
  358. begin
  359. outppu.putstring('imp'+current_module.realmodulename^);
  360. outppu.putlongint(link_static);
  361. outppu.writeentry(iblinkunitstaticlibs)
  362. end
  363. else
  364. begin
  365. outppu.putstring('imp'+current_module.realmodulename^);
  366. outppu.putlongint(link_shared);
  367. outppu.writeentry(iblinkunitsharedlibs);
  368. end;
  369. { read all entries until the end and write them also to the new ppu }
  370. repeat
  371. b:=inppu.readentry;
  372. { don't write ibend, that's written automatically }
  373. if b<>ibend then
  374. begin
  375. if b=iblinkothersharedlibs then
  376. begin
  377. while not inppu.endofentry do
  378. begin
  379. s:=inppu.getstring;
  380. m:=inppu.getlongint;
  381. outppu.putstring(s);
  382. outppu.putlongint(m);
  383. { strip lib prefix }
  384. if copy(s,1,3)='lib' then
  385. delete(s,1,3);
  386. ext:=ExtractFileExt(s);
  387. if ext<>'' then
  388. delete(s,length(s)-length(ext)+1,length(ext));
  389. current_module.linkOtherSharedLibs.add(s,link_always);
  390. end;
  391. end
  392. else
  393. repeat
  394. inppu.getdatabuf(buffer,sizeof(buffer),l);
  395. outppu.putdata(buffer,l);
  396. until l<sizeof(buffer);
  397. outppu.writeentry(b);
  398. end;
  399. until b=ibend;
  400. { write the last stuff and close }
  401. outppu.flush;
  402. outppu.writeheader;
  403. outppu.free;
  404. inppu.free;
  405. Result:=True;
  406. end;
  407. procedure load_packages;
  408. var
  409. i,j : longint;
  410. pcp: tpcppackage;
  411. entry,
  412. entryreq : ppackageentry;
  413. name,
  414. uname : string;
  415. begin
  416. if not (tf_supports_packages in target_info.flags) then
  417. exit;
  418. i:=0;
  419. while i<packagelist.count do
  420. begin
  421. entry:=ppackageentry(packagelist[i]);
  422. if assigned(entry^.package) then
  423. internalerror(2013053104);
  424. Comment(V_Info,'Loading package: '+entry^.realpkgname);
  425. pcp:=tpcppackage.create(entry^.realpkgname);
  426. pcp.loadpcp;
  427. entry^.package:=pcp;
  428. { add all required packages that are not yet part of packagelist }
  429. for j:=0 to pcp.requiredpackages.count-1 do
  430. begin
  431. name:=pcp.requiredpackages.NameOfIndex(j);
  432. uname:=upper(name);
  433. if not assigned(packagelist.Find(uname)) then
  434. begin
  435. New(entryreq);
  436. entryreq^.realpkgname:=name;
  437. entryreq^.package:=nil;
  438. entryreq^.usedunits:=0;
  439. entryreq^.direct:=false;
  440. packagelist.add(uname,entryreq);
  441. end;
  442. end;
  443. Inc(i);
  444. end;
  445. { all packages are now loaded, so we can fill in the links of the required packages }
  446. for i:=0 to packagelist.count-1 do
  447. begin
  448. entry:=ppackageentry(packagelist[i]);
  449. if not assigned(entry^.package) then
  450. internalerror(2015111301);
  451. for j:=0 to entry^.package.requiredpackages.count-1 do
  452. begin
  453. if assigned(entry^.package.requiredpackages[j]) then
  454. internalerror(2015111303);
  455. entryreq:=packagelist.find(upper(entry^.package.requiredpackages.NameOfIndex(j)));
  456. if not assigned(entryreq) then
  457. internalerror(2015111302);
  458. entry^.package.requiredpackages[j]:=entryreq^.package;
  459. end;
  460. end;
  461. end;
  462. procedure add_package(const name:string;ignoreduplicates:boolean;direct:boolean);
  463. var
  464. entry : ppackageentry;
  465. i : longint;
  466. begin
  467. for i:=0 to packagelist.count-1 do
  468. begin
  469. if packagelist.nameofindex(i)=name then
  470. begin
  471. if not ignoreduplicates then
  472. Message1(package_e_duplicate_package,name);
  473. exit;
  474. end;
  475. end;
  476. new(entry);
  477. entry^.package:=nil;
  478. entry^.realpkgname:=name;
  479. entry^.usedunits:=0;
  480. entry^.direct:=direct;
  481. packagelist.add(upper(name),entry);
  482. end;
  483. procedure add_package_unit_ref(package: tpackage);
  484. var
  485. pkgentry : ppackageentry;
  486. begin
  487. pkgentry:=ppackageentry(packagelist.find(package.packagename^));
  488. if not assigned(pkgentry) then
  489. internalerror(2015100301);
  490. inc(pkgentry^.usedunits);
  491. end;
  492. procedure add_package_libs(l:tlinker);
  493. var
  494. pkgentry : ppackageentry;
  495. i : longint;
  496. pkgname : tpathstr;
  497. begin
  498. if target_info.system in systems_indirect_var_imports then
  499. { we're using import libraries anyway }
  500. exit;
  501. for i:=0 to packagelist.count-1 do
  502. begin
  503. pkgentry:=ppackageentry(packagelist[i]);
  504. if pkgentry^.usedunits>0 then
  505. begin
  506. //writeln('package used: ',pkgentry^.realpkgname);
  507. pkgname:=pkgentry^.package.pplfilename;
  508. if copy(pkgname,1,length(target_info.sharedlibprefix))=target_info.sharedlibprefix then
  509. delete(pkgname,1,length(target_info.sharedlibprefix));
  510. if copy(pkgname,length(pkgname)-length(target_info.sharedlibext)+1,length(target_info.sharedlibext))=target_info.sharedlibext then
  511. delete(pkgname,length(pkgname)-length(target_info.sharedlibext)+1,length(target_info.sharedlibext));
  512. //writeln('adding library: ', pkgname);
  513. l.sharedlibfiles.concat(pkgname);
  514. end
  515. else
  516. {writeln('ignoring package: ',pkgentry^.realpkgname)};
  517. end;
  518. end;
  519. procedure check_for_indirect_package_usages(modules:tlinkedlist);
  520. var
  521. uu : tused_unit;
  522. pentry : ppackageentry;
  523. begin
  524. uu:=tused_unit(modules.first);
  525. while assigned(uu) do
  526. begin
  527. if assigned(uu.u.package) then
  528. begin
  529. pentry:=ppackageentry(packagelist.find(uu.u.package.packagename^));
  530. if not assigned(pentry) then
  531. internalerror(2015112304);
  532. if not pentry^.direct then
  533. Message2(package_w_unit_from_indirect_package,uu.u.realmodulename^,uu.u.package.realpackagename^);
  534. end;
  535. uu:=tused_unit(uu.Next);
  536. end;
  537. end;
  538. procedure createimportlibfromexternals;
  539. type
  540. tcacheentry=record
  541. pkg:tpackage;
  542. sym:tasmsymbol;
  543. end;
  544. pcacheentry=^tcacheentry;
  545. var
  546. cache : tfphashlist;
  547. alreadyloaded : tfpobjectlist;
  548. function findpackagewithsym(symname:tsymstr):tcacheentry;
  549. var
  550. i,j : longint;
  551. pkgentry : ppackageentry;
  552. unitentry : pcontainedunit;
  553. begin
  554. for i:=0 to packagelist.count-1 do
  555. begin
  556. pkgentry:=ppackageentry(packagelist[i]);
  557. for j:=0 to pkgentry^.package.containedmodules.count-1 do
  558. begin
  559. unitentry:=pcontainedunit(pkgentry^.package.containedmodules[j]);
  560. if not assigned(unitentry^.module) then
  561. { the unit is not loaded }
  562. continue;
  563. result.sym:=tasmsymbol(tmodule(unitentry^.module).publicasmsyms.find(symname));
  564. if assigned(result.sym) then
  565. begin
  566. { completely ignore other external symbols }
  567. if result.sym.bind in [ab_external,ab_weak_external] then
  568. begin
  569. result.sym:=nil;
  570. continue;
  571. end;
  572. { only accept global symbols of the used unit }
  573. if result.sym.bind<>ab_global then
  574. begin
  575. result.sym:=nil;
  576. result.pkg:=nil;
  577. end
  578. else
  579. result.pkg:=pkgentry^.package;
  580. exit;
  581. end;
  582. end;
  583. end;
  584. result.sym:=nil;
  585. result.pkg:=nil;
  586. end;
  587. procedure processasmsyms(symlist:tfphashobjectlist);
  588. var
  589. i,j,k : longint;
  590. sym : tasmsymbol;
  591. cacheentry : pcacheentry;
  592. psym : tsymentry;
  593. pd : tprocdef;
  594. found : boolean;
  595. impname,symname : TSymStr;
  596. suffixidx : longint;
  597. begin
  598. for i:=0 to symlist.count-1 do
  599. begin
  600. sym:=tasmsymbol(symlist[i]);
  601. if not (sym.bind in [ab_external,ab_external_indirect]) then
  602. continue;
  603. { remove the indirect suffix }
  604. symname:=sym.name;
  605. if sym.bind=ab_external_indirect then
  606. begin
  607. suffixidx:=pos(suffix_indirect,symname);
  608. if suffixidx=length(symname)-length(suffix_indirect)+1 then
  609. symname:=copy(symname,1,suffixidx-1)
  610. else
  611. internalerror(2016062401);
  612. end;
  613. { did we already import the symbol? }
  614. cacheentry:=pcacheentry(cache.find(symname));
  615. if assigned(cacheentry) then
  616. continue;
  617. { was the symbol already imported in the previous pass? }
  618. found:=false;
  619. for j:=0 to alreadyloaded.count-1 do
  620. begin
  621. psym:=tsymentry(alreadyloaded[j]);
  622. case psym.typ of
  623. procsym:
  624. for k:=0 to tprocsym(psym).procdeflist.count-1 do
  625. begin
  626. pd:=tprocdef(tprocsym(psym).procdeflist[k]);
  627. if pd.has_alias_name(symname) or
  628. (
  629. ([po_external,po_has_importdll]*pd.procoptions=[po_external,po_has_importdll]) and
  630. (symname=proc_get_importname(pd))
  631. ) then
  632. begin
  633. found:=true;
  634. break;
  635. end;
  636. end;
  637. staticvarsym:
  638. if tstaticvarsym(psym).mangledname=symname then
  639. found:=true;
  640. constsym:
  641. begin
  642. if tconstsym(psym).consttyp<>constresourcestring then
  643. internalerror(2016072202);
  644. if make_mangledname('RESSTR',psym.owner,psym.name)=symname then
  645. found:=true;
  646. end;
  647. else
  648. internalerror(2014101003);
  649. end;
  650. if found then
  651. break;
  652. end;
  653. if found then begin
  654. { add a dummy entry }
  655. new(cacheentry);
  656. cacheentry^.pkg:=nil;
  657. cacheentry^.sym:=sym;
  658. cache.add(symname,cacheentry);
  659. continue;
  660. end;
  661. new(cacheentry);
  662. cacheentry^:=findpackagewithsym(symname);
  663. cache.add(symname,cacheentry);
  664. { use cacheentry^.sym instead of sym, because for the later typ
  665. is always at_none in case of an external symbol }
  666. if assigned(cacheentry^.pkg) then
  667. begin
  668. impname:=symname;
  669. if cacheentry^.sym.typ=AT_DATA then
  670. { import as the $indirect symbol if it as a variable }
  671. impname:=symname+suffix_indirect;
  672. current_module.addexternalimport(cacheentry^.pkg.pplfilename,symname,impname,0,cacheentry^.sym.typ=at_data,false);
  673. end;
  674. end;
  675. end;
  676. procedure import_proc_symbol(pd:tprocdef;pkg:tpackage);
  677. var
  678. item : TCmdStrListItem;
  679. begin
  680. item := TCmdStrListItem(pd.aliasnames.first);
  681. if not assigned(item) then
  682. { at least import the mangled name }
  683. current_module.addexternalimport(pkg.pplfilename,pd.mangledname,pd.mangledname,0,false,false);
  684. while assigned(item) do
  685. begin
  686. current_module.addexternalimport(pkg.pplfilename,item.str,item.str,0,false,false);
  687. item := TCmdStrListItem(item.next);
  688. end;
  689. end;
  690. procedure processimportedsyms(syms:tfpobjectlist);
  691. var
  692. i,j,k,l : longint;
  693. pkgentry : ppackageentry;
  694. sym : TSymEntry;
  695. srsymtable : tsymtable;
  696. module : tmodule;
  697. unitentry : pcontainedunit;
  698. name : tsymstr;
  699. pd : tprocdef;
  700. begin
  701. for i:=0 to syms.count-1 do
  702. begin
  703. sym:=tsymentry(syms[i]);
  704. if not (sym.typ in [staticvarsym,procsym,constsym]) or
  705. (
  706. (sym.typ=constsym) and
  707. (tconstsym(sym).consttyp<>constresourcestring)
  708. ) then
  709. continue;
  710. if alreadyloaded.indexof(sym)>=0 then
  711. continue;
  712. { determine the unit of the symbol }
  713. srsymtable:=sym.owner;
  714. while not (srsymtable.symtabletype in [staticsymtable,globalsymtable]) do
  715. srsymtable:=srsymtable.defowner.owner;
  716. module:=tmodule(loaded_units.first);
  717. while assigned(module) do
  718. begin
  719. if (module.globalsymtable=srsymtable) or (module.localsymtable=srsymtable) then
  720. break;
  721. module:=tmodule(module.next);
  722. end;
  723. if not assigned(module) then
  724. internalerror(2014101001);
  725. if (uf_in_library and module.headerflags)=0 then
  726. { unit is not part of a package, so no need to handle it }
  727. continue;
  728. { loaded by a package? }
  729. for j:=0 to packagelist.count-1 do
  730. begin
  731. pkgentry:=ppackageentry(packagelist[j]);
  732. for k:=0 to pkgentry^.package.containedmodules.count-1 do
  733. begin
  734. unitentry:=pcontainedunit(pkgentry^.package.containedmodules[k]);
  735. if unitentry^.module=module then
  736. begin
  737. case sym.typ of
  738. constsym:
  739. begin
  740. if tconstsym(sym).consttyp<>constresourcestring then
  741. internalerror(2016072201);
  742. name:=make_mangledname('RESSTR',sym.owner,sym.name);
  743. current_module.addexternalimport(pkgentry^.package.pplfilename,name,name+suffix_indirect,0,true,false);
  744. end;
  745. staticvarsym:
  746. begin
  747. name:=tstaticvarsym(sym).mangledname;
  748. current_module.addexternalimport(pkgentry^.package.pplfilename,name,name+suffix_indirect,0,true,false);
  749. end;
  750. procsym:
  751. begin
  752. for l:=0 to tprocsym(sym).procdeflist.count-1 do
  753. begin
  754. pd:=tprocdef(tprocsym(sym).procdeflist[l]);
  755. if [po_external,po_has_importdll]*pd.procoptions=[po_external,po_has_importdll] then
  756. { if we use an external procedure of another unit we
  757. need to import it ourselves from the correct library }
  758. import_external_proc(pd)
  759. else
  760. import_proc_symbol(pd,pkgentry^.package);
  761. end;
  762. end;
  763. else
  764. internalerror(2014101002);
  765. end;
  766. alreadyloaded.add(sym);
  767. end;
  768. end;
  769. end;
  770. end;
  771. end;
  772. var
  773. module : tmodule;
  774. i : longint;
  775. begin
  776. cache:=tfphashlist.create;
  777. { check each external asm symbol of each unit of the package whether it is
  778. contained in the unit of a loaded package (and thus an import entry
  779. is needed) }
  780. alreadyloaded:=tfpobjectlist.create(false);
  781. { first pass to find all symbols that were not loaded by asm name }
  782. module:=tmodule(loaded_units.first);
  783. while assigned(module) do
  784. begin
  785. if not assigned(module.package) then
  786. processimportedsyms(module.unitimportsyms);
  787. module:=tmodule(module.next);
  788. end;
  789. { second pass to find all symbols that were loaded by asm name }
  790. module:=tmodule(loaded_units.first);
  791. while assigned(module) do
  792. begin
  793. if not assigned(module.package) then
  794. processasmsyms(module.externasmsyms);
  795. module:=tmodule(module.next);
  796. end;
  797. alreadyloaded.free;
  798. for i:=0 to cache.count-1 do
  799. dispose(pcacheentry(cache[i]));
  800. cache.free;
  801. end;
  802. end.