shared libs for OSX

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Mon May 25 12:40:51 PDT 2015


On Mon, 25 May 2015 14:39:37 -0400, Jacob Carlborg <doob at me.com> wrote:

> On 2015-05-25 16:33, bitwise wrote:
>> I've been reading through the Mach-O docs[1], and it seems that dynamic
>> libs are treated the same as static libs in that exported symbols can
>> only be defined once, even across dynamically loaded libraries. This is
>> why calling rt_init from my dylib ended up calling the one that was
>> already defined in the application image. I was able to call the rt_init
>> from the dylib by specifically requesting it through dlsym, but then,
>> all the global variables it used(_isRuntimeInitialized, etc) still ended
>> up resolving to the ones in the application image.
>>
>> At this point, my impression is that it would be very impractical, if
>> not impossible to have separate druntimes for each shared library. Even
>> when you do link separate runtimes, dyld still treats all the exported
>> symbols as shared.
>>
>>
>> @Martin
>> So correct me if I'm wrong, but it seems the _only_ choice is a shared
>> druntime for osx.
>> Could you elaborate a bit on how you've managed to do this for linux?
>
> If I recall correctly, this is how it works:
>
> On Linux the compiler will generate a function which is placed in a  
> special section in the binary, same as annotating a C function with  
> __attribute__((constructor)). This function calls a function in the  
> druntime. This will give similar properties as  
> _dyld_register_func_for_add_image on OS X but without the issues with  
> registered callbacks
>
> Each image then becomes responsible to initialize itself. The image  
> updates the shared data structure containing the necessary data (TLS,  
> exception handling tables, ...). As you noticed yourself, all global  
> symbols are shared across all images.
>

So then I think I have a full solution:
1) _dyld_register_func_for_add_image should be taken care of with the  
above two fixes
2) __attribute__((constructor/destructor)) can be added to druntime when  
building for osx like in the file dylib_fixes.c [1]
3) copy paste rt_init/rt_term, rename them to dylib_init/dylib_term and  
remove everything except whats needed to initialize a shared lib's image.

Does this make sense?

Thanks,
   Bit


[1]  
https://github.com/D-Programming-Language/druntime/blob/61ba4b8d3c0052065c17ffc8eef4f11496f3db3e/src/rt/dylib_fixes.c


More information about the Digitalmars-d mailing list