pkgutil.pas 29 KB

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