2
0

pkgutil.pas 25 KB

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