www.digitalmars.com         C & C++   DMDScript  

D.gnu - dlopen() with GDC

After a good deal of fiddling about, I got a shared library generated from D to
load using dlopen() in a D program.

The shared library was built from:
int foo()
    return 42;

gdc -c loaded.d
gcc -shared -o liblddd.so loaded.o -lc

The loader was - excluding a lot of error checking code:

import std.stdio;
import std.c.linux.linux;

   RTLD_LAZY =  1,
   RTLD_NOW =  2,
extern (C)
   int errno;  // Where is this in Phobos?

void main()
   void* handle = dlopen("./liblddd.so\0".ptr, RTLD_LAZY);
   int function() fp = cast(int function()) dlsym(handle,
   int n = fp();
   writefln("n = %d", n);

Finally it worked, and I was just patting myself on the back when I realized I
had missed out the -fPIC in the compilation of the library. So I did:

gdc -fPIC -c loaded.d
gcc -shared -o liblddd.so loaded.o -lc

and then it stopped working - dlopen() returning null.

Has anyone any idea why this should be so? Dumps from readelf of the header and
symbols are quite similar except that the symbols for the version compiled with
-fPIC has a line:

9: 0000200c     4 OBJECT  LOCAL  HIDDEN   22 DW.ref.__gdc_personality_
Jan 19 2010