.. I hope that the code you posted isn't copied from Delphi. If it is however I ask you to edit your post and remove it.
It was from Delphi's system.pas. Oops!?
I apologize. Now I did remove that.
It is already clear what needs to be done. The problem merely is time and to rework the RTL to support this correctly.
By trying read the fpc's compiler source code, yes it is clear. That was seem as something like chicken and egg problem for me.
-->Chicken will be only available from egg, and egg require chicken to be existed.
==>RTL (and another) dynamic-loading-package will be only available when is supported by compiler, and compiler requires RTL to be existed in dynamically-loaded-package shape.
It maybe inaccurate compared to the actual fpc compilers logic, since there are thousands of code to be understood carefully, and I didn't found any documentation about it unless that (possibly) outdated one.
So, I depend on a feeling that Delphi has more elegant trick when managing type/class/RTTI than fpc plan:
When a dynamic package is loaded using LoadPackages() or unloaded with UnloadPackage() there are various house keeping tasks that the RTL needs to do:
- initialize or uninitialize units as needed
- insert new types into the public type list for correct support in the Rtti unit
- insert new resource strings into the internal resource string list for correct support ...
I think, the better place to register class is
not in the global type list, but in each package itself.
So each package has it own list (container) of registered class/type. soon that list is integrated/registered to global list of class used by FindClass().
This way, when the module is being unloaded, it will be easy to destroy that list because the member of list is limited containing inside the package.
Otherwise, when all registered class are
mixed in one global list, it would be hard for RTL/RTTI/resource to remove classes from a package being unloaded: which class/type/resource to be removed is relevant/belong to the package being unloaded?
----------
Another magic (elegant trick) of dynamic-loading-package:
Even Delphi uses all BPL on almost Delphi running/lifetime as dynamic linking library, it is not a must for application generated.
It mean that any BPL can also be used by compiler to built into static linking application.
However, above are my stupid humble opinion that usually just one or two step understanding achievement of tens step done by fpc teams.
[/size][/font]