D runtime in os x dylib

John Colvin john.loughran.colvin at gmail.com
Sat Jun 23 13:54:45 PDT 2012


There seems to be a problem with starting the d runtime in a d 
dylib loaded in a c program. Whenever I call Runtime.initialize() 
i get a segfault.

C code:

     #include <stdio.h>
     #include <dlfcn.h>

     int main() {
         void *library;
         int (*fptr)();
         library = dlopen("testlib.dylib", RTLD_LAZY);
         if(library == NULL)
             puts("couldn't load the library");
         else {
             *(void **)(&fptr) = dlsym(library,"number");
             if(fptr == NULL) {
    	        puts("couldn't load function");
             }
             else {
                 printf("the result is %d\n",(*fptr)());
             }
         }
         return(0);
     }

compiled with gcc, no flags.

d code:

     import core.runtime;

     extern(C) int number() {
     	Runtime.initialize();
     	return(4);
     }

compiled with dmd -shared

Backtrace:

     Program received signal EXC_BAD_ACCESS, Could not access 
memory.
     Reason: 13 at address: 0x0000000000000000
     0x000000010003de28 in __tls_get_addr ()
     (gdb) bt
     #0  0x000000010003de28 in __tls_get_addr ()
     #1  0x000000010003cdfc in thread_attachThis ()
     #2  0x000000010003ccb8 in thread_init ()
     #3  0x000000010003e312 in gc_init ()
     #4  0x0000000100044ff5 in rt_init ()
     #5  0x000000010003b637 in 
D4core7runtime7Runtime10initializeFDFC6object9ThrowableZvZb ()
     #6  0x0000000100034ee9 in number ()
     #7  0x0000000100000e84 in main ()

All compilation and debugging done in OS X 10.7

Note: if the Runtime.initialize(); line is commented out, the 
library is loaded and runs correctly.


More information about the Digitalmars-d mailing list