pkgutil.pas 28 KB

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