pkgutil.pas 29 KB

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