Shared library

This is an old revision of Shared library, edited by Marcov at . The current revision is 40400.

(here if I say shared library I mean both .so as .dll, unless I really say "unix shared library" or "dll")

Currently there are a lot of Shared libraries bug reports. However, to my knowledge there is no real documentation about how shared libraries work in combination with FPC. That is both how shared libraries work now, how they should work, and how Delphi treats them.

Let's start with a simple sketch of what forms Delphi supports, because we will of course try to be as compatible as multiplatformwise reasonable. Note that this is all from memory and webpages. If you can detail it more, or specify more exact borderconditions, by all means, do)

Delphi

Delphi to my knowledge knows three (or four) ways of dynamic linking.

Besides these, Delphi can also generate dlls that are ActiveX components. T.B.D.

Sharemem implementation details

As said, sharemem switches the memory manager to a global one. Under Windows this is afaik the COM manager. On *nix a similar memmanager doesn't always exist ( Gnome and KDE component architectures might have something similar), but it is not guaranteed there.

One could probably simply have all programs use cmem, which could be a "level 0" implementation for everything that runs in this process.

I explictely name this because, it might be necessary to impose an own initialisation order (independant of OS shared lib initialisation) to allow the main program to initialise units right after the RTL (system unit) initializations, but before other libs. e.g. a different memory manager.

VMT duplication

I've heard various opinions about if VMT duplication is a real problem when passing classes between DLL and mainprogram (for the various scenario's). I don't exactly know when it is a problem or not. (if you do, feel free to explain it here). Therefore I just list for all possible scenario's if VMTs are duplicated or not.

In earlier discussions about packages, there was some confusion about this topic. People assumed that packages would somehow tap into the RTL VMT's in the main binary. I'm however pretty sure this is not the case, at least not in Delphi, since when using packages, the RTL always is a package too. And every dependancy of an unit in a package must be in the same package or in a package it has a dependancy on. This means an unit only exists once in the greater program (main program + its packages)

Initialization and finalization sections and RTL

In all cases of shared libraries, the problem of how to initialize and finalize. The ground rule is of course that a unit, and all its unit it depends on must be initialized before it is used. In the case of packages this can lead to a required order of initialization. Also the (possible?) ability in the mainprogram to insert units before the system unit is used is a problem.

For stand-alone libraries with internal copies of RTL to function, the RTL needs to be initialized, and the initialization section of the library needs to be called as well.

Roughly there are two options here:

Mixed forms are also possible, e.g. initialize standalone shared libraries via the identifiers, but with packages use an own definition.

E.g. for packages there are two (possible) special requirements that influence the ini/finalize behaviour:

For the latter, assume the mainprogram code looks like this:

uses 
     Drivers, cwstring,cmem;
 begin
 end.

Nearly all of the 4 units (include unit system) allocate and modify hooks globally in their startup. Their initialisation order is important.

However Drivers is part of FV, cwstring and System of the RTL, cmem is in the packages. So at least three packages are involved.

It is logical to leave cmem (and similar units) out of a package, and cwstring can't be in the RTL package anyway because it links to libc. (avoiding which is the prime reason for its existence)