pkgutil.pas 29 KB

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