pkgutil.pas 25 KB

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