www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - DSO with DMD 2.052 on Linux?

reply Alexander <aldem+dmars nk7.net> writes:
Is dynamic linking supported on Linux with DMD 2.052 (and D2 in general)?

I am not talking about Phobos, just modules which I want to compile as DSO.

There is -fPIC switch to DMD - does it work as intended?

PS: gdc is not an option...

-- 
/Alexander
May 02 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
 Is dynamic linking supported on Linux with DMD 2.052 (and D2 in general)?
 
 I am not talking about Phobos, just modules which I want to compile as DSO.
 
 There is -fPIC switch to DMD - does it work as intended?
 
 PS: gdc is not an option...

The short answer: no. As for the long answer, someone else could answer far better than I could. IIRC, the main problem is the runtime. It has issues when you try and split your D code with .so files (I believe that you end up with multiple instances of the runtime). It _might_ work if you don't use the garbage collector at all, but there could be other issues. I don't remember exactly what all of the problems are. But regardless, don't expect shared libraries to work at the moment. Getting shared libraries to work is near the top of the TODO list, but it hasn't been done yet. - Jonathan M Davis
May 02 2011
next sibling parent Kai Meyer <kai unixlords.com> writes:
On 05/02/2011 03:10 PM, Jonathan M Davis wrote:
 Is dynamic linking supported on Linux with DMD 2.052 (and D2 in general)?

 I am not talking about Phobos, just modules which I want to compile as DSO.

 There is -fPIC switch to DMD - does it work as intended?

 PS: gdc is not an option...

The short answer: no. As for the long answer, someone else could answer far better than I could. IIRC, the main problem is the runtime. It has issues when you try and split your D code with .so files (I believe that you end up with multiple instances of the runtime). It _might_ work if you don't use the garbage collector at all, but there could be other issues. I don't remember exactly what all of the problems are. But regardless, don't expect shared libraries to work at the moment. Getting shared libraries to work is near the top of the TODO list, but it hasn't been done yet. - Jonathan M Davis

I thought I saw somewhere that you simply have to have the runtime initialized before calling into the shared library (which is done for you when dmd is supposed to produce an executable): http://www.digitalmars.com/d/2.0/phobos/core_runtime.html I may be incorrect too :) -Kai Meyer
May 02 2011
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2011-05-02 23:10, Jonathan M Davis wrote:
 Is dynamic linking supported on Linux with DMD 2.052 (and D2 in general)?

 I am not talking about Phobos, just modules which I want to compile as DSO.

 There is -fPIC switch to DMD - does it work as intended?

 PS: gdc is not an option...

The short answer: no. As for the long answer, someone else could answer far better than I could. IIRC, the main problem is the runtime. It has issues when you try and split your D code with .so files (I believe that you end up with multiple instances of the runtime). It _might_ work if you don't use the garbage collector at all, but there could be other issues. I don't remember exactly what all of the problems are. But regardless, don't expect shared libraries to work at the moment.

This is a showstopper for shared libraries on Linux: http://d.puremagic.com/issues/show_bug.cgi?id=4583 A few things in the runtime need to be changed as well.
 Getting shared libraries to work is near the top of the TODO list, but it
 hasn't been done yet.

 - Jonathan M Davis

Shared libraries have been working for Tango on Mac OS X from quite some time now. Almost all of the same patches are applied to druntime as well. Just one small change to actually enable the changes. -- /Jacob Carlborg
May 03 2011
parent reply Alexander <aldem+dmars nk7.net> writes:
On 03.05.2011 09:15, Jacob Carlborg wrote:

 This is a showstopper for shared libraries on Linux:
http://d.puremagic.com/issues/show_bug.cgi?id=4583
 A few things in the runtime need to be changed as well.

OK, I see. But isn't simple linking to DSO (no dlopen calls) should be handled by the linker itself? Or even this needs runtime support? Long time ago, I remember, I did a few tests, and everything was fine. Though, I am not sure that this was D2, and I lost my test code... /Alexander
May 03 2011
parent Jacob Carlborg <doob me.com> writes:
On 2011-05-03 11:14, Alexander wrote:
 On 03.05.2011 09:15, Jacob Carlborg wrote:

 This is a showstopper for shared libraries on Linux:
http://d.puremagic.com/issues/show_bug.cgi?id=4583
 A few things in the runtime need to be changed as well.

OK, I see. But isn't simple linking to DSO (no dlopen calls) should be handled by the linker itself? Or even this needs runtime support? Long time ago, I remember, I did a few tests, and everything was fine. Though, I am not sure that this was D2, and I lost my test code... /Alexander

If DMD doesn't generate the correct PIC code if won't work (I think). You will also get missing symbol error for the "main" function when building a shared library. This can be suppressed by making it a week symbol, using a gcc __attribute__ extension. Then when the library is loaded you would need to make some initializations like: * Exception handling tables * TLS * Run module constructors * Module info * Probably a few things I missed -- /Jacob Carlborg
May 03 2011