pkgutil.pas 29 KB

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