dlopen: I'm stumped

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

dlopen: I'm stumped

Schwab,Wilhelm K
Hello all,

Attached is my attempt at calling dlopen() from Pharo, and it is not going well.  The C code below works as expected, so the problem has to be either in my code or something in Pharo or the VM itself.

I am fairly certain that loading the attached is safe, but

                DynamicLinkingLibrary
                        getProcAddress:'cos'
                        from:'libm.so'.

crashes the vm in my experience.  Any ideas?

Bill



/*
        http://www.unix.com/man-page/All/3/dlopen/

        Build as:

                gcc -rdynamic -o fubar fubar.c -ldl
*/

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

       int
       main(int argc, char **argv)
       {
           void *handle;
           double (*cosine)(double);
           char *error;

           handle = dlopen("libm.so", RTLD_LAZY);
           if (!handle) {
               fprintf(stderr, "%s\n", dlerror());
               exit(EXIT_FAILURE);
           }

           dlerror(); /* Clear any existing error */

           /* Writing: cosine = (double (*)(double)) dlsym(handle, "cos");
              would seem more natural, but the C99 standard leaves
              casting from "void *" to a function pointer undefined.
              The assignment used below is the POSIX.1-2003 (Technical
              Corrigendum 1) workaround; see the Rationale for the
              POSIX specification of dlsym(). */

           *(void **) (&cosine) = dlsym(handle, "cos");

           if ((error = dlerror()) != NULL)  {
               fprintf(stderr, "%s\n", error);
               exit(EXIT_FAILURE);
           }

           printf("%f\n", (*cosine)(2.0));
           dlclose(handle);
           exit(EXIT_SUCCESS);
       }



DynamicLinkingLibrary.st (3K) Download Attachment