pkgutil.pas 29 KB

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