pkgutil.pas 26 KB

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