shared libs for OSX
Jacob Carlborg via Digitalmars-d
digitalmars-d at puremagic.com
Thu May 21 12:34:56 PDT 2015
On 2015-05-21 10:12, Timothee Cour via Digitalmars-d wrote:
> dlopen() of a D shared library works when called from a C++ file, and
> that a C++ file can call multiple D shared libraries simultaneously when
> using RTLD_LAZY option.
>
> Before waiting for a full solution with an integrated druntime, is there
> at least a way to have a separate runtime in each shared library, so
> that dlopen() can be called from a D file?
No, not out of the box.
I'm not sure if I remember all the details correctly, but this is
basically how it works:
The runtime uses the "_dyld_register_func_for_add_image" function
provided by the dynamic linker. This function is used to register a
callback. The callback will be called for each currently loaded image
(executable/dynamic library) in the executable. The callback will also
be called for every newly loaded image, i.e. using dlopen. You need to
keep track of which image is yourself and which are other images you
should ignore.
Then, the other problem with "_dyld_register_func_for_add_image" is that
it's not possible to unregister the callback. Which means, if you
register a callback using this function from a dynamic library loaded
with dlopen and then unload it. Next time dlopen is called it will
crash, because the callback points to an address that doesn't exist anymore.
There's an other, undocumented, function that does basically the same
but prevents the issue with the callback by not allowing the dynamic
library to be unloaded.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list